document_history.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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(id int) (*DocumentHistory,error) {
  46. o := orm.NewOrm()
  47. err := o.QueryTable(m.TableNameWithPrefix()).Filter("history_id",id).One(m)
  48. return m,err
  49. }
  50. //清空指定文档的历史.
  51. func (m *DocumentHistory) Clear(doc_id int) error {
  52. o := orm.NewOrm()
  53. _, err := o.Raw("DELETE md_document_history WHERE document_id = ?", doc_id).Exec()
  54. return err
  55. }
  56. //删除历史.
  57. func (m *DocumentHistory) Delete(history_id,doc_id int) error {
  58. o := orm.NewOrm()
  59. _, err := o.QueryTable(m.TableNameWithPrefix()).Filter("history_id",history_id).Filter("document_id",doc_id).Delete()
  60. return err
  61. }
  62. //恢复指定历史的文档.
  63. func (m *DocumentHistory) Restore(history_id,doc_id,uid int) error {
  64. o := orm.NewOrm()
  65. err := o.QueryTable(m.TableNameWithPrefix()).Filter("history_id", history_id).Filter("document_id",doc_id).One(m)
  66. if err != nil {
  67. return err
  68. }
  69. doc, err := NewDocument().Find(m.DocumentId)
  70. if err != nil {
  71. return err
  72. }
  73. history := NewDocumentHistory()
  74. history.DocumentId = doc_id
  75. history.Content = doc.Content
  76. history.Markdown = doc.Markdown
  77. history.DocumentName = doc.DocumentName
  78. history.ModifyAt = uid
  79. history.MemberId = doc.MemberId
  80. history.ParentId = doc.ParentId
  81. history.Version = time.Now().Unix()
  82. history.Action = "restore"
  83. history.ActionName = "恢复文档"
  84. history.InsertOrUpdate()
  85. doc.DocumentName = m.DocumentName
  86. doc.Content = m.Content
  87. doc.Markdown = m.Markdown
  88. doc.Release = m.Content
  89. doc.Version = time.Now().Unix()
  90. _, err = o.Update(doc)
  91. return err
  92. }
  93. func (m *DocumentHistory) InsertOrUpdate() (history *DocumentHistory,err error) {
  94. o := orm.NewOrm()
  95. history = m
  96. if m.HistoryId > 0 {
  97. _,err = o.Update(m)
  98. }else{
  99. _,err = o.Insert(m)
  100. }
  101. return
  102. }
  103. //分页查询指定文档的历史.
  104. func (m *DocumentHistory) FindToPager(doc_id, page_index, page_size int) (docs []*DocumentHistorySimpleResult, totalCount int, err error) {
  105. o := orm.NewOrm()
  106. offset := (page_index - 1) * page_size
  107. totalCount = 0
  108. sql := `SELECT history.*,m1.account,m2.account as modify_name
  109. FROM md_document_history AS history
  110. LEFT JOIN md_members AS m1 ON history.member_id = m1.member_id
  111. LEFT JOIN md_members AS m2 ON history.modify_at = m2.member_id
  112. WHERE history.document_id = ? ORDER BY history.history_id DESC LIMIT ?,?;`
  113. _, err = o.Raw(sql,doc_id,offset,page_size).QueryRows(&docs)
  114. if err != nil {
  115. return
  116. }
  117. var count int64
  118. count, err = o.QueryTable(m.TableNameWithPrefix()).Filter("document_id", doc_id).Count()
  119. if err != nil {
  120. return
  121. }
  122. totalCount = int(count)
  123. return
  124. }