1
0

AttachmentModel.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. //数据库模型.
  2. package models
  3. import (
  4. "time"
  5. "os"
  6. "strings"
  7. "github.com/beego/beego/v2/client/orm"
  8. "github.com/beego/beego/v2/core/logs"
  9. "github.com/mindoc-org/mindoc/conf"
  10. "github.com/mindoc-org/mindoc/utils/filetil"
  11. )
  12. // Attachment struct .
  13. type Attachment struct {
  14. AttachmentId int `orm:"column(attachment_id);pk;auto;unique" json:"attachment_id"`
  15. BookId int `orm:"column(book_id);type(int);description(所属book id)" json:"book_id"`
  16. DocumentId int `orm:"column(document_id);type(int);null;description(所属文档id)" json:"doc_id"`
  17. FileName string `orm:"column(file_name);size(255);description(文件名称)" json:"file_name"`
  18. FilePath string `orm:"column(file_path);size(2000);description(文件路径)" json:"file_path"`
  19. FileSize float64 `orm:"column(file_size);type(float);description(文件大小 字节)" json:"file_size"`
  20. HttpPath string `orm:"column(http_path);size(2000);description(文件路径)" json:"http_path"`
  21. FileExt string `orm:"column(file_ext);size(50);description(文件后缀)" json:"file_ext"`
  22. CreateTime time.Time `orm:"type(datetime);column(create_time);auto_now_add;description(创建时间)" json:"create_time"`
  23. CreateAt int `orm:"column(create_at);type(int);description(创建人id)" json:"create_at"`
  24. }
  25. // TableName 获取对应上传附件数据库表名.
  26. func (m *Attachment) TableName() string {
  27. return "attachment"
  28. }
  29. // TableEngine 获取数据使用的引擎.
  30. func (m *Attachment) TableEngine() string {
  31. return "INNODB"
  32. }
  33. func (m *Attachment) TableNameWithPrefix() string {
  34. return conf.GetDatabasePrefix() + m.TableName()
  35. }
  36. func NewAttachment() *Attachment {
  37. return &Attachment{}
  38. }
  39. func (m *Attachment) Insert() error {
  40. o := orm.NewOrm()
  41. _, err := o.Insert(m)
  42. return err
  43. }
  44. func (m *Attachment) Update() error {
  45. o := orm.NewOrm()
  46. _, err := o.Update(m)
  47. return err
  48. }
  49. func (m *Attachment) Delete() error {
  50. o := orm.NewOrm()
  51. _, err := o.Delete(m)
  52. if err == nil {
  53. if err1 := os.Remove(m.FilePath); err1 != nil {
  54. logs.Error(err1)
  55. }
  56. }
  57. return err
  58. }
  59. func (m *Attachment) Find(id int) (*Attachment, error) {
  60. if id <= 0 {
  61. return m, ErrInvalidParameter
  62. }
  63. o := orm.NewOrm()
  64. err := o.QueryTable(m.TableNameWithPrefix()).Filter("attachment_id", id).One(m)
  65. return m, err
  66. }
  67. //查询指定文档的附件列表
  68. func (m *Attachment) FindListByDocumentId(docId int) (attaches []*Attachment, err error) {
  69. o := orm.NewOrm()
  70. _, err = o.QueryTable(m.TableNameWithPrefix()).Filter("document_id", docId).Filter("book_id__gt", 0).OrderBy("-attachment_id").All(&attaches)
  71. return
  72. }
  73. //分页查询附件
  74. func (m *Attachment) FindToPager(pageIndex, pageSize int) (attachList []*AttachmentResult, totalCount int, err error) {
  75. o := orm.NewOrm()
  76. total, err := o.QueryTable(m.TableNameWithPrefix()).Count()
  77. if err != nil {
  78. return nil, 0, err
  79. }
  80. totalCount = int(total)
  81. var list []*Attachment
  82. offset := (pageIndex - 1) * pageSize
  83. if pageSize == 0 {
  84. _, err = o.QueryTable(m.TableNameWithPrefix()).OrderBy("-attachment_id").Offset(offset).Limit(pageSize).All(&list)
  85. } else {
  86. _, err = o.QueryTable(m.TableNameWithPrefix()).OrderBy("-attachment_id").All(&list)
  87. }
  88. if err != nil {
  89. if err == orm.ErrNoRows {
  90. logs.Info("没有查到附件 ->", err)
  91. err = nil
  92. }
  93. return
  94. }
  95. for _, item := range list {
  96. attach := &AttachmentResult{}
  97. attach.Attachment = *item
  98. attach.FileShortSize = filetil.FormatBytes(int64(attach.FileSize))
  99. //当项目ID为0标识是文章的附件
  100. if item.BookId == 0 && item.DocumentId > 0 {
  101. blog := NewBlog()
  102. if err := o.QueryTable(blog.TableNameWithPrefix()).Filter("blog_id", item.DocumentId).One(blog, "blog_title"); err == nil {
  103. attach.BookName = blog.BlogTitle
  104. } else {
  105. attach.BookName = "[文章不存在]"
  106. }
  107. } else {
  108. book := NewBook()
  109. if e := o.QueryTable(book.TableNameWithPrefix()).Filter("book_id", item.BookId).One(book, "book_name"); e == nil {
  110. attach.BookName = book.BookName
  111. doc := NewDocument()
  112. if e := o.QueryTable(doc.TableNameWithPrefix()).Filter("document_id", item.DocumentId).One(doc, "document_name"); e == nil {
  113. attach.DocumentName = doc.DocumentName
  114. } else {
  115. attach.DocumentName = "[文档不存在]"
  116. }
  117. } else {
  118. attach.BookName = "[项目不存在]"
  119. }
  120. }
  121. attach.LocalHttpPath = strings.Replace(item.FilePath, "\\", "/", -1)
  122. attachList = append(attachList, attach)
  123. }
  124. return
  125. }