2017-05-17 15:21:36 +08:00
|
|
|
//数据库模型.
|
2017-04-24 18:25:17 +08:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
2017-05-12 19:14:29 +08:00
|
|
|
|
|
|
|
"os"
|
2017-06-05 13:41:47 +08:00
|
|
|
|
2017-05-12 19:14:29 +08:00
|
|
|
"github.com/astaxie/beego"
|
2017-06-05 13:41:47 +08:00
|
|
|
"github.com/astaxie/beego/orm"
|
|
|
|
"github.com/lifei6671/godoc/conf"
|
2017-04-24 18:25:17 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// Attachment struct .
|
|
|
|
type Attachment struct {
|
2017-05-12 19:14:29 +08:00
|
|
|
AttachmentId int `orm:"column(attachment_id);pk;auto;unique" json:"attachment_id"`
|
|
|
|
BookId int `orm:"column(book_id);type(int)" json:"book_id"`
|
|
|
|
DocumentId int `orm:"column(document_id);type(int);null" json:"doc_id"`
|
|
|
|
FileName string `orm:"column(file_name);size(255)" json:"file_name"`
|
|
|
|
FilePath string `orm:"column(file_path);size(2000)" json:"file_path"`
|
|
|
|
FileSize float64 `orm:"column(file_size);type(float)" json:"file_size"`
|
|
|
|
HttpPath string `orm:"column(http_path);size(2000)" json:"http_path"`
|
|
|
|
FileExt string `orm:"column(file_ext);size(50)" json:"file_ext"`
|
|
|
|
CreateTime time.Time `orm:"type(datetime);column(create_time);auto_now_add" json:"create_time"`
|
|
|
|
CreateAt int `orm:"column(create_at);type(int)" json:"create_at"`
|
2017-04-24 18:25:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// TableName 获取对应数据库表名.
|
|
|
|
func (m *Attachment) TableName() string {
|
|
|
|
return "attachment"
|
|
|
|
}
|
2017-05-12 19:14:29 +08:00
|
|
|
|
2017-04-24 18:25:17 +08:00
|
|
|
// TableEngine 获取数据使用的引擎.
|
|
|
|
func (m *Attachment) TableEngine() string {
|
|
|
|
return "INNODB"
|
|
|
|
}
|
|
|
|
func (m *Attachment) TableNameWithPrefix() string {
|
|
|
|
return conf.GetDatabasePrefix() + m.TableName()
|
|
|
|
}
|
|
|
|
|
2017-05-12 19:14:29 +08:00
|
|
|
func NewAttachment() *Attachment {
|
2017-04-24 18:25:17 +08:00
|
|
|
return &Attachment{}
|
|
|
|
}
|
|
|
|
|
2017-05-12 19:14:29 +08:00
|
|
|
func (m *Attachment) Insert() error {
|
2017-04-24 18:25:17 +08:00
|
|
|
o := orm.NewOrm()
|
|
|
|
|
2017-05-12 19:14:29 +08:00
|
|
|
_, err := o.Insert(m)
|
2017-04-24 18:25:17 +08:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
2017-04-27 18:19:37 +08:00
|
|
|
func (m *Attachment) Update() error {
|
|
|
|
o := orm.NewOrm()
|
2017-05-12 19:14:29 +08:00
|
|
|
_, err := o.Update(m)
|
2017-04-27 18:19:37 +08:00
|
|
|
return err
|
|
|
|
}
|
2017-04-24 18:25:17 +08:00
|
|
|
|
2017-05-12 19:14:29 +08:00
|
|
|
func (m *Attachment) Delete() error {
|
2017-04-24 18:25:17 +08:00
|
|
|
o := orm.NewOrm()
|
|
|
|
|
2017-06-05 13:41:47 +08:00
|
|
|
_, err := o.Delete(m)
|
2017-04-24 18:25:17 +08:00
|
|
|
|
2017-05-12 19:14:29 +08:00
|
|
|
if err == nil {
|
|
|
|
if err1 := os.Remove(m.FilePath); err1 != nil {
|
|
|
|
beego.Error(err1)
|
|
|
|
}
|
|
|
|
}
|
2017-04-24 18:25:17 +08:00
|
|
|
|
2017-05-12 19:14:29 +08:00
|
|
|
return err
|
|
|
|
}
|
2017-04-24 18:25:17 +08:00
|
|
|
|
2017-05-12 19:14:29 +08:00
|
|
|
func (m *Attachment) Find(id int) (*Attachment, error) {
|
|
|
|
if id <= 0 {
|
|
|
|
return m, ErrInvalidParameter
|
|
|
|
}
|
|
|
|
o := orm.NewOrm()
|
2017-04-24 18:25:17 +08:00
|
|
|
|
2017-05-12 19:14:29 +08:00
|
|
|
err := o.QueryTable(m.TableNameWithPrefix()).Filter("attachment_id", id).One(m)
|
2017-04-24 18:25:17 +08:00
|
|
|
|
2017-05-12 19:14:29 +08:00
|
|
|
return m, err
|
|
|
|
}
|
2017-04-24 18:25:17 +08:00
|
|
|
|
2017-05-12 19:14:29 +08:00
|
|
|
func (m *Attachment) FindListByDocumentId(doc_id int) (attaches []*Attachment, err error) {
|
|
|
|
o := orm.NewOrm()
|
2017-04-24 18:25:17 +08:00
|
|
|
|
2017-06-05 13:41:47 +08:00
|
|
|
_, err = o.QueryTable(m.TableNameWithPrefix()).Filter("document_id", doc_id).OrderBy("-attachment_id").All(&attaches)
|
2017-05-12 19:14:29 +08:00
|
|
|
return
|
|
|
|
}
|