AttachmentModel.go 4.6 KB

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