DocumentModel.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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()).Filter("book_id",m.BookId).One(book,"identify");err == nil {
  79. identify = book.Identify
  80. }
  81. m.Identify = fmt.Sprintf("%s-%s",identify,strconv.FormatInt(time.Now().UnixNano(), 32))
  82. }
  83. if m.OrderSort == 0{
  84. sort,_ := o.QueryTable(m.TableNameWithPrefix()).Filter("book_id",m.BookId).Filter("parent_id",m.ParentId).Count()
  85. m.OrderSort = int(sort) + 1
  86. }
  87. _, err = o.Insert(m)
  88. NewBook().ResetDocumentNumber(m.BookId)
  89. }
  90. if err != nil {
  91. return err
  92. }
  93. return nil
  94. }
  95. //根据文档识别编号和项目id获取一篇文档
  96. func (m *Document) FindByIdentityFirst(identify string, bookId int) (*Document, error) {
  97. o := orm.NewOrm()
  98. err := o.QueryTable(m.TableNameWithPrefix()).Filter("book_id", bookId).Filter("identify", identify).One(m)
  99. return m, err
  100. }
  101. //递归删除一个文档.
  102. func (m *Document) RecursiveDocument(docId int) error {
  103. o := orm.NewOrm()
  104. if doc, err := m.Find(docId); err == nil {
  105. o.Delete(doc)
  106. NewDocumentHistory().Clear(doc.DocumentId)
  107. }
  108. var maps []orm.Params
  109. _, err := o.Raw("SELECT document_id FROM " + m.TableNameWithPrefix() + " WHERE parent_id=" + strconv.Itoa(docId)).Values(&maps)
  110. if err != nil {
  111. beego.Error("RecursiveDocument => ", err)
  112. return err
  113. }
  114. for _, item := range maps {
  115. if docId, ok := item["document_id"].(string); ok {
  116. id, _ := strconv.Atoi(docId)
  117. o.QueryTable(m.TableNameWithPrefix()).Filter("document_id", id).Delete()
  118. m.RecursiveDocument(id)
  119. }
  120. }
  121. return nil
  122. }
  123. //将文档写入缓存
  124. func (m *Document) PutToCache() {
  125. go func(m Document) {
  126. if v, err := json.Marshal(&m); err == nil {
  127. if m.Identify == "" {
  128. if err := cache.Put("Document.Id."+strconv.Itoa(m.DocumentId), v, time.Second*3600); err != nil {
  129. beego.Info("文档缓存失败:", m.DocumentId)
  130. }
  131. } else {
  132. if err := cache.Put(fmt.Sprintf("Document.BookId.%d.Identify.%s", m.BookId, m.Identify), v, time.Second*3600); err != nil {
  133. beego.Info("文档缓存失败:", m.DocumentId)
  134. }
  135. }
  136. }
  137. }(*m)
  138. }
  139. //清除缓存
  140. func (m *Document) RemoveCache() {
  141. go func(m Document) {
  142. cache.Put("Document.Id."+strconv.Itoa(m.DocumentId), m, time.Second*3600)
  143. if m.Identify != "" {
  144. cache.Put(fmt.Sprintf("Document.BookId.%d.Identify.%s", m.BookId, m.Identify), m, time.Second*3600)
  145. }
  146. }(*m)
  147. }
  148. //从缓存获取
  149. func (m *Document) FromCacheById(id int) (*Document, error) {
  150. b := cache.Get("Document.Id." + strconv.Itoa(id))
  151. if v, ok := b.([]byte); ok {
  152. if err := json.Unmarshal(v, m); err == nil {
  153. beego.Info("从缓存中获取文档信息成功", m.DocumentId)
  154. return m, nil
  155. }
  156. }
  157. defer func() {
  158. if m.DocumentId > 0 {
  159. m.PutToCache()
  160. }
  161. }()
  162. return m.Find(id)
  163. }
  164. //根据文档标识从缓存中查询文档
  165. func (m *Document) FromCacheByIdentify(identify string, bookId int) (*Document, error) {
  166. b := cache.Get(fmt.Sprintf("Document.BookId.%d.Identify.%s", bookId, identify))
  167. if v, ok := b.([]byte); ok {
  168. if err := json.Unmarshal(v, m); err == nil {
  169. beego.Info("从缓存中获取文档信息成功", m.DocumentId, identify)
  170. return m, nil
  171. }
  172. }
  173. defer func() {
  174. if m.DocumentId > 0 {
  175. m.PutToCache()
  176. }
  177. }()
  178. return m.FindByIdentityFirst(identify, bookId)
  179. }
  180. //根据项目ID查询文档列表.
  181. func (m *Document) FindListByBookId(bookId int) (docs []*Document, err error) {
  182. o := orm.NewOrm()
  183. _, err = o.QueryTable(m.TableNameWithPrefix()).Filter("book_id", bookId).OrderBy("order_sort").All(&docs)
  184. return
  185. }
  186. //判断文章是否存在
  187. func (m *Document) IsExist(documentId int) bool {
  188. o := orm.NewOrm()
  189. return o.QueryTable(m.TableNameWithPrefix()).Filter("document_id",documentId).Exist()
  190. }