DocumentHistory.go 5.2 KB

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