document_history.go 3.7 KB

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