document_history.go 4.2 KB

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