DocumentHistory.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. package models
  2. import (
  3. "time"
  4. "github.com/beego/beego/v2/client/orm"
  5. "github.com/beego/beego/v2/core/logs"
  6. "github.com/mindoc-org/mindoc/conf"
  7. )
  8. type DocumentHistory struct {
  9. HistoryId int `orm:"column(history_id);pk;auto;unique" json:"history_id"`
  10. Action string `orm:"column(action);size(255);description(modify)" json:"action"`
  11. ActionName string `orm:"column(action_name);size(255);description(修改文档)" json:"action_name"`
  12. DocumentId int `orm:"column(document_id);type(int);index;description(关联文档id)" json:"doc_id"`
  13. DocumentName string `orm:"column(document_name);size(500);description(关联文档id)" json:"doc_name"`
  14. ParentId int `orm:"column(parent_id);type(int);index;default(0);description(父级文档id)" json:"parent_id"`
  15. Markdown string `orm:"column(markdown);type(text);null;description(文档内容)" json:"markdown"`
  16. Content string `orm:"column(content);type(text);null;description(文档内容)" json:"content"`
  17. MemberId int `orm:"column(member_id);type(int);description(作者id)" json:"member_id"`
  18. ModifyTime time.Time `orm:"column(modify_time);type(datetime);auto_now;description(修改时间)" json:"modify_time"`
  19. ModifyAt int `orm:"column(modify_at);type(int);description(修改人id)" json:"-"`
  20. Version int64 `orm:"type(bigint);column(version);description(版本)" json:"version"`
  21. IsOpen int `orm:"column(is_open);type(int);default(0);description(是否展开子目录 0:阅读时关闭节点 1:阅读时展开节点 2:空目录 单击时会展开下级节点)" json:"is_open"`
  22. }
  23. type DocumentHistorySimpleResult struct {
  24. HistoryId int `json:"history_id"`
  25. ActionName string `json:"action_name"`
  26. MemberId int `json:"member_id"`
  27. Account string `json:"account"`
  28. ModifyAt int `json:"modify_at"`
  29. ModifyName string `json:"modify_name"`
  30. ModifyTime time.Time `json:"modify_time"`
  31. Version int64 `json:"version"`
  32. }
  33. // TableName 获取对应数据库表名.
  34. func (m *DocumentHistory) TableName() string {
  35. return "document_history"
  36. }
  37. // TableEngine 获取数据使用的引擎.
  38. func (m *DocumentHistory) TableEngine() string {
  39. return "INNODB"
  40. }
  41. func (m *DocumentHistory) TableNameWithPrefix() string {
  42. return conf.GetDatabasePrefix() + m.TableName()
  43. }
  44. func NewDocumentHistory() *DocumentHistory {
  45. return &DocumentHistory{}
  46. }
  47. func (m *DocumentHistory) Find(id int) (*DocumentHistory, error) {
  48. o := orm.NewOrm()
  49. err := o.QueryTable(m.TableNameWithPrefix()).Filter("history_id", id).One(m)
  50. return m, err
  51. }
  52. //清空指定文档的历史.
  53. func (m *DocumentHistory) Clear(docId int) error {
  54. o := orm.NewOrm()
  55. _, err := o.Raw("DELETE md_document_history WHERE document_id = ?", docId).Exec()
  56. return err
  57. }
  58. //删除历史.
  59. func (m *DocumentHistory) Delete(historyId, docId int) error {
  60. o := orm.NewOrm()
  61. _, err := o.QueryTable(m.TableNameWithPrefix()).Filter("history_id", historyId).Filter("document_id", docId).Delete()
  62. return err
  63. }
  64. //恢复指定历史的文档.
  65. func (m *DocumentHistory) Restore(historyId, docId, uid int) error {
  66. o := orm.NewOrm()
  67. err := o.QueryTable(m.TableNameWithPrefix()).Filter("history_id", historyId).Filter("document_id", docId).One(m)
  68. if err != nil {
  69. return err
  70. }
  71. doc, err := NewDocument().Find(m.DocumentId)
  72. if err != nil {
  73. return err
  74. }
  75. history := NewDocumentHistory()
  76. history.DocumentId = docId
  77. history.Content = doc.Content
  78. history.Markdown = doc.Markdown
  79. history.DocumentName = doc.DocumentName
  80. history.ModifyAt = uid
  81. history.MemberId = doc.MemberId
  82. history.ParentId = doc.ParentId
  83. history.Version = time.Now().Unix()
  84. history.Action = "restore"
  85. history.ActionName = "恢复文档"
  86. history.IsOpen = doc.IsOpen
  87. history.InsertOrUpdate()
  88. doc.DocumentName = m.DocumentName
  89. doc.Content = m.Content
  90. doc.Markdown = m.Markdown
  91. doc.Release = m.Content
  92. doc.Version = time.Now().Unix()
  93. doc.IsOpen = m.IsOpen
  94. _, err = o.Update(doc)
  95. return err
  96. }
  97. func (m *DocumentHistory) InsertOrUpdate() (history *DocumentHistory, err error) {
  98. o := orm.NewOrm()
  99. history = m
  100. if m.HistoryId > 0 {
  101. _, err = o.Update(m)
  102. } else {
  103. _, err = o.Insert(m)
  104. if err == nil {
  105. if doc, e := NewDocument().Find(m.DocumentId); e == nil {
  106. if book, e := NewBook().Find(doc.BookId); e == nil && book.HistoryCount > 0 {
  107. //如果已存在的历史记录大于指定的记录,则清除旧记录
  108. if c, e := o.QueryTable(m.TableNameWithPrefix()).Filter("document_id", doc.DocumentId).Count(); e == nil && c > int64(book.HistoryCount) {
  109. count := c - int64(book.HistoryCount)
  110. logs.Info("需要删除的历史文档数量:", count)
  111. var lists []DocumentHistory
  112. if _, e := o.QueryTable(m.TableNameWithPrefix()).Filter("document_id", doc.DocumentId).OrderBy("history_id").Limit(count).All(&lists, "history_id"); e == nil {
  113. for _, d := range lists {
  114. o.Delete(&d)
  115. }
  116. }
  117. } else {
  118. logs.Info(book.HistoryCount)
  119. }
  120. }
  121. }
  122. }
  123. }
  124. return
  125. }
  126. //分页查询指定文档的历史.
  127. func (m *DocumentHistory) FindToPager(docId, pageIndex, pageSize int) (docs []*DocumentHistorySimpleResult, totalCount int, err error) {
  128. o := orm.NewOrm()
  129. offset := (pageIndex - 1) * pageSize
  130. totalCount = 0
  131. sql := `SELECT history.*,m1.account,m2.account as modify_name
  132. FROM md_document_history AS history
  133. LEFT JOIN md_members AS m1 ON history.member_id = m1.member_id
  134. LEFT JOIN md_members AS m2 ON history.modify_at = m2.member_id
  135. WHERE history.document_id = ? ORDER BY history.history_id DESC LIMIT ?,?;`
  136. _, err = o.Raw(sql, docId, offset, pageSize).QueryRows(&docs)
  137. if err != nil {
  138. return
  139. }
  140. var count int64
  141. count, err = o.QueryTable(m.TableNameWithPrefix()).Filter("document_id", docId).Count()
  142. if err != nil {
  143. return
  144. }
  145. totalCount = int(count)
  146. return
  147. }