document.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. package models
  2. import (
  3. "time"
  4. "encoding/json"
  5. "fmt"
  6. "strconv"
  7. "github.com/astaxie/beego"
  8. "github.com/astaxie/beego/orm"
  9. "github.com/lifei6671/mindoc/cache"
  10. "github.com/lifei6671/mindoc/conf"
  11. )
  12. // Document struct.
  13. type Document struct {
  14. DocumentId int `orm:"pk;auto;unique;column(document_id)" json:"doc_id"`
  15. DocumentName string `orm:"column(document_name);size(500)" json:"doc_name"`
  16. // Identify 文档唯一标识
  17. Identify string `orm:"column(identify);size(100);index;null;default(null)" json:"identify"`
  18. BookId int `orm:"column(book_id);type(int);index" json:"book_id"`
  19. ParentId int `orm:"column(parent_id);type(int);index;default(0)" json:"parent_id"`
  20. OrderSort int `orm:"column(order_sort);default(0);type(int);index" json:"order_sort"`
  21. // Markdown markdown格式文档.
  22. Markdown string `orm:"column(markdown);type(text);null" json:"markdown"`
  23. // Release 发布后的Html格式内容.
  24. Release string `orm:"column(release);type(text);null" json:"release"`
  25. // Content 未发布的 Html 格式内容.
  26. Content string `orm:"column(content);type(text);null" json:"content"`
  27. CreateTime time.Time `orm:"column(create_time);type(datetime);auto_now_add" json:"create_time"`
  28. MemberId int `orm:"column(member_id);type(int)" json:"member_id"`
  29. ModifyTime time.Time `orm:"column(modify_time);type(datetime);auto_now" json:"modify_time"`
  30. ModifyAt int `orm:"column(modify_at);type(int)" json:"-"`
  31. Version int64 `orm:"type(bigint);column(version)" json:"version"`
  32. AttachList []*Attachment `orm:"-" json:"attach"`
  33. }
  34. // 多字段唯一键
  35. func (m *Document) TableUnique() [][]string {
  36. return [][]string{
  37. []string{"book_id", "identify"},
  38. }
  39. }
  40. // TableName 获取对应数据库表名.
  41. func (m *Document) TableName() string {
  42. return "documents"
  43. }
  44. // TableEngine 获取数据使用的引擎.
  45. func (m *Document) TableEngine() string {
  46. return "INNODB"
  47. }
  48. func (m *Document) TableNameWithPrefix() string {
  49. return conf.GetDatabasePrefix() + m.TableName()
  50. }
  51. func NewDocument() *Document {
  52. return &Document{
  53. Version: time.Now().Unix(),
  54. }
  55. }
  56. //根据文档ID查询指定文档.
  57. func (m *Document) Find(id int) (*Document, error) {
  58. if id <= 0 {
  59. return m, ErrInvalidParameter
  60. }
  61. o := orm.NewOrm()
  62. err := o.QueryTable(m.TableNameWithPrefix()).Filter("document_id", id).One(m)
  63. if err == orm.ErrNoRows {
  64. return m, ErrDataNotExist
  65. }
  66. return m, nil
  67. }
  68. //插入和更新文档.
  69. func (m *Document) InsertOrUpdate(cols ...string) error {
  70. o := orm.NewOrm()
  71. var err error
  72. if m.DocumentId > 0 {
  73. _, err = o.Update(m, cols...)
  74. } else {
  75. if m.Identify == "" {
  76. book := NewBook()
  77. identify := "docs"
  78. if err := o.QueryTable(book.TableNameWithPrefix()).One(book,"identify");err == nil {
  79. identify = book.Identify
  80. }
  81. m.Identify = fmt.Sprintf("%s-%d%d",identify,m.BookId,time.Now().Unix())
  82. }
  83. _, err = o.Insert(m)
  84. NewBook().ResetDocumentNumber(m.BookId)
  85. }
  86. if err != nil {
  87. return err
  88. }
  89. return nil
  90. }
  91. //根据文档识别编号和项目id获取一篇文档
  92. func (m *Document) FindByIdentityFirst(identify string, bookId int) (*Document, error) {
  93. o := orm.NewOrm()
  94. err := o.QueryTable(m.TableNameWithPrefix()).Filter("book_id", bookId).Filter("identify", identify).One(m)
  95. return m, err
  96. }
  97. //递归删除一个文档.
  98. func (m *Document) RecursiveDocument(docId int) error {
  99. o := orm.NewOrm()
  100. if doc, err := m.Find(docId); err == nil {
  101. o.Delete(doc)
  102. NewDocumentHistory().Clear(doc.DocumentId)
  103. }
  104. var maps []orm.Params
  105. _, err := o.Raw("SELECT document_id FROM " + m.TableNameWithPrefix() + " WHERE parent_id=" + strconv.Itoa(docId)).Values(&maps)
  106. if err != nil {
  107. beego.Error("RecursiveDocument => ", err)
  108. return err
  109. }
  110. for _, item := range maps {
  111. if docId, ok := item["document_id"].(string); ok {
  112. id, _ := strconv.Atoi(docId)
  113. o.QueryTable(m.TableNameWithPrefix()).Filter("document_id", id).Delete()
  114. m.RecursiveDocument(id)
  115. }
  116. }
  117. return nil
  118. }
  119. //将文档写入缓存
  120. func (m *Document) PutToCache() {
  121. go func(m Document) {
  122. if v, err := json.Marshal(&m); err == nil {
  123. if m.Identify == "" {
  124. if err := cache.Put("Document.Id."+strconv.Itoa(m.DocumentId), v, time.Second*3600); err != nil {
  125. beego.Info("文档缓存失败:", m.DocumentId)
  126. }
  127. } else {
  128. if err := cache.Put(fmt.Sprintf("Document.BookId.%d.Identify.%s", m.BookId, m.Identify), v, time.Second*3600); err != nil {
  129. beego.Info("文档缓存失败:", m.DocumentId)
  130. }
  131. }
  132. }
  133. }(*m)
  134. }
  135. //清除缓存
  136. func (m *Document) RemoveCache() {
  137. go func(m Document) {
  138. cache.Put("Document.Id."+strconv.Itoa(m.DocumentId), m, time.Second*3600)
  139. if m.Identify != "" {
  140. cache.Put(fmt.Sprintf("Document.BookId.%d.Identify.%s", m.BookId, m.Identify), m, time.Second*3600)
  141. }
  142. }(*m)
  143. }
  144. //从缓存获取
  145. func (m *Document) FromCacheById(id int) (*Document, error) {
  146. b := cache.Get("Document.Id." + strconv.Itoa(id))
  147. if v, ok := b.([]byte); ok {
  148. if err := json.Unmarshal(v, m); err == nil {
  149. beego.Info("从缓存中获取文档信息成功", m.DocumentId)
  150. return m, nil
  151. }
  152. }
  153. defer func() {
  154. if m.DocumentId > 0 {
  155. m.PutToCache()
  156. }
  157. }()
  158. return m.Find(id)
  159. }
  160. //根据文档标识从缓存中查询文档
  161. func (m *Document) FromCacheByIdentify(identify string, bookId int) (*Document, error) {
  162. b := cache.Get(fmt.Sprintf("Document.BookId.%d.Identify.%s", bookId, identify))
  163. if v, ok := b.([]byte); ok {
  164. if err := json.Unmarshal(v, m); err == nil {
  165. beego.Info("从缓存中获取文档信息成功", m.DocumentId, identify)
  166. return m, nil
  167. }
  168. }
  169. defer func() {
  170. if m.DocumentId > 0 {
  171. m.PutToCache()
  172. }
  173. }()
  174. return m.FindByIdentityFirst(identify, bookId)
  175. }
  176. //根据项目ID查询文档列表.
  177. func (m *Document) FindListByBookId(bookId int) (docs []*Document, err error) {
  178. o := orm.NewOrm()
  179. _, err = o.QueryTable(m.TableNameWithPrefix()).Filter("book_id", bookId).OrderBy("order_sort").All(&docs)
  180. return
  181. }