1
0

DocumentModel.go 6.1 KB

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