document.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. //是否锁定,锁定状态下不能编辑,0 否/1 是
  33. IsLock int `orm:"column(is_lock);type(int);default(0)" json:"is_lock"`
  34. AttachList []*Attachment `orm:"-" json:"attach"`
  35. }
  36. // 多字段唯一键
  37. func (m *Document) TableUnique() [][]string {
  38. return [][]string{
  39. []string{"book_id", "identify"},
  40. }
  41. }
  42. // TableName 获取对应数据库表名.
  43. func (m *Document) TableName() string {
  44. return "documents"
  45. }
  46. // TableEngine 获取数据使用的引擎.
  47. func (m *Document) TableEngine() string {
  48. return "INNODB"
  49. }
  50. func (m *Document) TableNameWithPrefix() string {
  51. return conf.GetDatabasePrefix() + m.TableName()
  52. }
  53. func NewDocument() *Document {
  54. return &Document{
  55. Version: time.Now().Unix(),
  56. }
  57. }
  58. //根据文档ID查询指定文档.
  59. func (m *Document) Find(id int) (*Document, error) {
  60. if id <= 0 {
  61. return m, ErrInvalidParameter
  62. }
  63. o := orm.NewOrm()
  64. err := o.QueryTable(m.TableNameWithPrefix()).Filter("document_id", id).One(m)
  65. if err == orm.ErrNoRows {
  66. return m, ErrDataNotExist
  67. }
  68. return m, nil
  69. }
  70. //插入和更新文档.
  71. func (m *Document) InsertOrUpdate(cols ...string) error {
  72. o := orm.NewOrm()
  73. var err error
  74. if m.DocumentId > 0 {
  75. _, err = o.Update(m, cols...)
  76. } else {
  77. if m.Identify == "" {
  78. book := NewBook()
  79. identify := "docs"
  80. if err := o.QueryTable(book.TableNameWithPrefix()).Filter("book_id",m.BookId).One(book,"identify");err == nil {
  81. identify = book.Identify
  82. }
  83. m.Identify = fmt.Sprintf("%s-%d%d",identify,m.BookId,time.Now().Unix())
  84. }
  85. _, err = o.Insert(m)
  86. NewBook().ResetDocumentNumber(m.BookId)
  87. }
  88. if err != nil {
  89. return err
  90. }
  91. return nil
  92. }
  93. //根据文档识别编号和项目id获取一篇文档
  94. func (m *Document) FindByIdentityFirst(identify string, bookId int) (*Document, error) {
  95. o := orm.NewOrm()
  96. err := o.QueryTable(m.TableNameWithPrefix()).Filter("book_id", bookId).Filter("identify", identify).One(m)
  97. return m, err
  98. }
  99. //递归删除一个文档.
  100. func (m *Document) RecursiveDocument(docId int) error {
  101. o := orm.NewOrm()
  102. if doc, err := m.Find(docId); err == nil {
  103. o.Delete(doc)
  104. NewDocumentHistory().Clear(doc.DocumentId)
  105. }
  106. var maps []orm.Params
  107. _, err := o.Raw("SELECT document_id FROM " + m.TableNameWithPrefix() + " WHERE parent_id=" + strconv.Itoa(docId)).Values(&maps)
  108. if err != nil {
  109. beego.Error("RecursiveDocument => ", err)
  110. return err
  111. }
  112. for _, item := range maps {
  113. if docId, ok := item["document_id"].(string); ok {
  114. id, _ := strconv.Atoi(docId)
  115. o.QueryTable(m.TableNameWithPrefix()).Filter("document_id", id).Delete()
  116. m.RecursiveDocument(id)
  117. }
  118. }
  119. return nil
  120. }
  121. //将文档写入缓存
  122. func (m *Document) PutToCache() {
  123. go func(m Document) {
  124. if v, err := json.Marshal(&m); err == nil {
  125. if m.Identify == "" {
  126. if err := cache.Put("Document.Id."+strconv.Itoa(m.DocumentId), v, 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), v, time.Second*3600); err != nil {
  131. beego.Info("文档缓存失败:", m.DocumentId)
  132. }
  133. }
  134. }
  135. }(*m)
  136. }
  137. //清除缓存
  138. func (m *Document) RemoveCache() {
  139. go func(m Document) {
  140. cache.Put("Document.Id."+strconv.Itoa(m.DocumentId), m, time.Second*3600)
  141. if m.Identify != "" {
  142. cache.Put(fmt.Sprintf("Document.BookId.%d.Identify.%s", m.BookId, m.Identify), m, time.Second*3600)
  143. }
  144. }(*m)
  145. }
  146. //从缓存获取
  147. func (m *Document) FromCacheById(id int) (*Document, error) {
  148. b := cache.Get("Document.Id." + strconv.Itoa(id))
  149. if v, ok := b.([]byte); ok {
  150. if err := json.Unmarshal(v, m); err == nil {
  151. beego.Info("从缓存中获取文档信息成功", m.DocumentId)
  152. return m, nil
  153. }
  154. }
  155. defer func() {
  156. if m.DocumentId > 0 {
  157. m.PutToCache()
  158. }
  159. }()
  160. return m.Find(id)
  161. }
  162. //根据文档标识从缓存中查询文档
  163. func (m *Document) FromCacheByIdentify(identify string, bookId int) (*Document, error) {
  164. b := cache.Get(fmt.Sprintf("Document.BookId.%d.Identify.%s", bookId, identify))
  165. if v, ok := b.([]byte); ok {
  166. if err := json.Unmarshal(v, m); err == nil {
  167. beego.Info("从缓存中获取文档信息成功", m.DocumentId, identify)
  168. return m, nil
  169. }
  170. }
  171. defer func() {
  172. if m.DocumentId > 0 {
  173. m.PutToCache()
  174. }
  175. }()
  176. return m.FindByIdentityFirst(identify, bookId)
  177. }
  178. //根据项目ID查询文档列表.
  179. func (m *Document) FindListByBookId(bookId int) (docs []*Document, err error) {
  180. o := orm.NewOrm()
  181. _, err = o.QueryTable(m.TableNameWithPrefix()).Filter("book_id", bookId).OrderBy("order_sort").All(&docs)
  182. return
  183. }
  184. //判断项目是否锁定
  185. func (m *Document) IsLockBook(docId int) bool {
  186. document := NewDocument()
  187. book := NewBook()
  188. o := orm.NewOrm()
  189. err := o.QueryTable(m.TableNameWithPrefix()).Filter("document_id",docId).One(document,"book_id")
  190. if err != nil {
  191. beego.Error("查询文档失败 =>",err)
  192. return false
  193. }
  194. err = o.QueryTable(book.TableNameWithPrefix()).Filter("book_id",document.BookId).One(book,"is_lock")
  195. if err != nil {
  196. beego.Error("查询项目失败 =>",err)
  197. return false
  198. }
  199. return book.IsLock == 1
  200. }