1
0

attachment.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package models
  2. import (
  3. "time"
  4. "github.com/lifei6671/godoc/conf"
  5. "github.com/astaxie/beego/orm"
  6. )
  7. // Attachment struct .
  8. type Attachment struct {
  9. AttachmentId int `orm:"column(attachment_id);pk;auto;unique" json:"attachment_id"`
  10. BookId int `orm:"column(book_id);type(int)" json:"book_id"`
  11. DocumentId int `orm:"column(document_id);type(int);null" json:"doc_id"`
  12. FileName string `orm:"column(file_name);size(255)" json:"file_name"`
  13. FilePath string `orm:"column(file_path);size(2000)" json:"file_path"`
  14. FileSize float64 `orm:"column(file_size);type(float)" json:"file_size"`
  15. HttpPath string `orm:"column(http_path);size(2000)" json:"http_path"`
  16. FileExt string `orm:"column(file_ext);size(50)" json:"file_ext"`
  17. CreateTime time.Time `orm:"type(datetime);column(create_time);auto_now_add"`
  18. CreateAt int `orm:"column(create_at);type(int)" json:"create_at"`
  19. }
  20. // TableName 获取对应数据库表名.
  21. func (m *Attachment) TableName() string {
  22. return "attachment"
  23. }
  24. // TableEngine 获取数据使用的引擎.
  25. func (m *Attachment) TableEngine() string {
  26. return "INNODB"
  27. }
  28. func (m *Attachment) TableNameWithPrefix() string {
  29. return conf.GetDatabasePrefix() + m.TableName()
  30. }
  31. func NewAttachment() *Attachment {
  32. return &Attachment{}
  33. }
  34. func (m *Attachment) Insert() error {
  35. o := orm.NewOrm()
  36. _,err := o.Insert(m)
  37. return err
  38. }
  39. func (m *Attachment) Update() error {
  40. o := orm.NewOrm()
  41. _,err := o.Update(m)
  42. return err
  43. }
  44. func (m *Attachment) Find(id int) (*Attachment,error) {
  45. if id <= 0 {
  46. return m,ErrInvalidParameter
  47. }
  48. o := orm.NewOrm()
  49. err := o.QueryTable(m.TableNameWithPrefix()).Filter("attachment_id",id).One(m)
  50. return m,err
  51. }