mindoc/models/DocumentModel.go

228 lines
6.2 KiB
Go
Raw Normal View History

2017-04-21 18:20:35 +08:00
package models
2017-04-22 17:24:17 +08:00
import (
"time"
2018-03-25 14:59:42 +08:00
"encoding/json"
2017-05-13 12:12:37 +08:00
"fmt"
2018-03-25 14:59:42 +08:00
"strconv"
2017-05-19 17:20:33 +08:00
"github.com/astaxie/beego"
"github.com/astaxie/beego/orm"
2018-02-27 17:20:42 +08:00
"github.com/lifei6671/mindoc/cache"
2018-03-25 14:59:42 +08:00
"github.com/lifei6671/mindoc/conf"
2017-04-22 17:24:17 +08:00
)
2017-04-21 18:20:35 +08:00
// Document struct.
type Document struct {
2017-05-19 17:20:33 +08:00
DocumentId int `orm:"pk;auto;unique;column(document_id)" json:"doc_id"`
DocumentName string `orm:"column(document_name);size(500)" json:"doc_name"`
2017-04-21 18:20:35 +08:00
// Identify 文档唯一标识
2017-05-19 17:20:33 +08:00
Identify string `orm:"column(identify);size(100);index;null;default(null)" json:"identify"`
BookId int `orm:"column(book_id);type(int);index" json:"book_id"`
ParentId int `orm:"column(parent_id);type(int);index;default(0)" json:"parent_id"`
OrderSort int `orm:"column(order_sort);default(0);type(int);index" json:"order_sort"`
2017-04-21 18:20:35 +08:00
// Markdown markdown格式文档.
2017-05-19 17:20:33 +08:00
Markdown string `orm:"column(markdown);type(text);null" json:"markdown"`
2017-04-21 18:20:35 +08:00
// Release 发布后的Html格式内容.
2017-05-19 17:20:33 +08:00
Release string `orm:"column(release);type(text);null" json:"release"`
2017-04-21 18:20:35 +08:00
// Content 未发布的 Html 格式内容.
2017-05-19 17:20:33 +08:00
Content string `orm:"column(content);type(text);null" json:"content"`
CreateTime time.Time `orm:"column(create_time);type(datetime);auto_now_add" json:"create_time"`
MemberId int `orm:"column(member_id);type(int)" json:"member_id"`
ModifyTime time.Time `orm:"column(modify_time);type(datetime);auto_now" json:"modify_time"`
2017-05-19 17:20:33 +08:00
ModifyAt int `orm:"column(modify_at);type(int)" json:"-"`
Version int64 `orm:"type(bigint);column(version)" json:"version"`
2017-05-12 19:14:29 +08:00
AttachList []*Attachment `orm:"-" json:"attach"`
2017-04-21 18:20:35 +08:00
}
2018-03-24 17:24:02 +08:00
// 多字段唯一键
func (m *Document) TableUnique() [][]string {
return [][]string{
[]string{"book_id", "identify"},
}
}
2018-03-25 14:59:42 +08:00
2017-04-21 18:20:35 +08:00
// TableName 获取对应数据库表名.
func (m *Document) TableName() string {
return "documents"
}
2017-05-19 17:20:33 +08:00
2017-04-21 18:20:35 +08:00
// TableEngine 获取数据使用的引擎.
func (m *Document) TableEngine() string {
return "INNODB"
}
2017-04-22 17:24:17 +08:00
2017-05-19 17:20:33 +08:00
func (m *Document) TableNameWithPrefix() string {
2017-04-22 17:24:17 +08:00
return conf.GetDatabasePrefix() + m.TableName()
}
2017-05-19 17:20:33 +08:00
func NewDocument() *Document {
return &Document{
Version: time.Now().Unix(),
}
2017-04-22 17:24:17 +08:00
}
2017-05-15 14:59:23 +08:00
//根据文档ID查询指定文档.
2017-05-19 17:20:33 +08:00
func (m *Document) Find(id int) (*Document, error) {
if id <= 0 {
2017-05-19 17:20:33 +08:00
return m, ErrInvalidParameter
}
o := orm.NewOrm()
2017-04-22 17:24:17 +08:00
2017-05-19 17:20:33 +08:00
err := o.QueryTable(m.TableNameWithPrefix()).Filter("document_id", id).One(m)
2017-04-22 17:24:17 +08:00
2017-05-19 17:20:33 +08:00
if err == orm.ErrNoRows {
return m, ErrDataNotExist
}
2017-05-19 17:20:33 +08:00
return m, nil
}
//插入和更新文档.
2017-05-19 17:20:33 +08:00
func (m *Document) InsertOrUpdate(cols ...string) error {
2017-04-27 18:19:37 +08:00
o := orm.NewOrm()
2018-02-27 17:20:42 +08:00
var err error
2017-04-27 18:19:37 +08:00
if m.DocumentId > 0 {
2018-03-25 14:59:42 +08:00
_, err = o.Update(m, cols...)
2017-05-19 17:20:33 +08:00
} else {
2018-03-30 17:21:16 +08:00
if m.Identify == "" {
book := NewBook()
identify := "docs"
2018-06-06 20:45:24 +08:00
if err := o.QueryTable(book.TableNameWithPrefix()).Filter("book_id",m.BookId).One(book,"identify");err == nil {
2018-03-30 17:21:16 +08:00
identify = book.Identify
}
2018-06-06 20:45:24 +08:00
m.Identify = fmt.Sprintf("%s-%s",identify,strconv.FormatInt(time.Now().UnixNano(), 32))
}
2018-07-10 18:53:41 +08:00
2018-06-06 20:45:24 +08:00
if m.OrderSort == 0{
sort,_ := o.QueryTable(m.TableNameWithPrefix()).Filter("book_id",m.BookId).Filter("parent_id",m.ParentId).Count()
m.OrderSort = int(sort) + 1
2018-03-30 17:21:16 +08:00
}
2018-02-27 17:20:42 +08:00
_, err = o.Insert(m)
NewBook().ResetDocumentNumber(m.BookId)
2018-02-27 17:20:42 +08:00
}
if err != nil {
2017-04-27 18:19:37 +08:00
return err
}
2018-02-27 17:20:42 +08:00
return nil
2017-04-27 18:19:37 +08:00
}
2018-03-23 11:17:52 +08:00
//根据文档识别编号和项目id获取一篇文档
2018-03-25 14:59:42 +08:00
func (m *Document) FindByIdentityFirst(identify string, bookId int) (*Document, error) {
2017-04-27 18:19:37 +08:00
o := orm.NewOrm()
2018-03-25 14:59:42 +08:00
err := o.QueryTable(m.TableNameWithPrefix()).Filter("book_id", bookId).Filter("identify", identify).One(m)
2017-04-27 18:19:37 +08:00
2017-05-19 17:20:33 +08:00
return m, err
2017-04-27 18:19:37 +08:00
}
2017-04-22 17:24:17 +08:00
2017-04-28 18:08:01 +08:00
//递归删除一个文档.
2018-02-27 17:20:42 +08:00
func (m *Document) RecursiveDocument(docId int) error {
2017-04-28 18:08:01 +08:00
o := orm.NewOrm()
2018-02-27 17:20:42 +08:00
if doc, err := m.Find(docId); err == nil {
o.Delete(doc)
2017-05-20 15:27:03 +08:00
NewDocumentHistory().Clear(doc.DocumentId)
}
2018-02-27 17:20:42 +08:00
var maps []orm.Params
2017-04-28 18:08:01 +08:00
2018-02-27 17:20:42 +08:00
_, err := o.Raw("SELECT document_id FROM " + m.TableNameWithPrefix() + " WHERE parent_id=" + strconv.Itoa(docId)).Values(&maps)
2017-04-28 18:08:01 +08:00
if err != nil {
2017-05-19 17:20:33 +08:00
beego.Error("RecursiveDocument => ", err)
2017-04-28 18:08:01 +08:00
return err
}
2018-02-27 17:20:42 +08:00
for _, item := range maps {
2018-03-25 14:59:42 +08:00
if docId, ok := item["document_id"].(string); ok {
id, _ := strconv.Atoi(docId)
2018-02-27 17:20:42 +08:00
o.QueryTable(m.TableNameWithPrefix()).Filter("document_id", id).Delete()
m.RecursiveDocument(id)
}
2017-04-28 18:08:01 +08:00
}
2017-04-28 18:08:01 +08:00
return nil
}
2017-04-22 17:24:17 +08:00
2018-02-27 17:20:42 +08:00
//将文档写入缓存
2018-03-25 14:59:42 +08:00
func (m *Document) PutToCache() {
go func(m Document) {
2018-03-25 14:59:42 +08:00
if v, err := json.Marshal(&m); err == nil {
if m.Identify == "" {
2018-03-25 14:59:42 +08:00
if err := cache.Put("Document.Id."+strconv.Itoa(m.DocumentId), v, time.Second*3600); err != nil {
beego.Info("文档缓存失败:", m.DocumentId)
}
2018-03-25 14:59:42 +08:00
} else {
if err := cache.Put(fmt.Sprintf("Document.BookId.%d.Identify.%s", m.BookId, m.Identify), v, time.Second*3600); err != nil {
beego.Info("文档缓存失败:", m.DocumentId)
}
2018-02-27 17:20:42 +08:00
}
}
}(*m)
}
2018-03-25 14:59:42 +08:00
//清除缓存
func (m *Document) RemoveCache() {
go func(m Document) {
2018-03-25 14:59:42 +08:00
cache.Put("Document.Id."+strconv.Itoa(m.DocumentId), m, time.Second*3600)
if m.Identify != "" {
2018-03-25 14:59:42 +08:00
cache.Put(fmt.Sprintf("Document.BookId.%d.Identify.%s", m.BookId, m.Identify), m, time.Second*3600)
}
}(*m)
2018-02-27 17:20:42 +08:00
}
2018-02-27 17:20:42 +08:00
//从缓存获取
2018-03-25 14:59:42 +08:00
func (m *Document) FromCacheById(id int) (*Document, error) {
2018-02-27 17:20:42 +08:00
b := cache.Get("Document.Id." + strconv.Itoa(id))
2018-03-25 14:59:42 +08:00
if v, ok := b.([]byte); ok {
2018-03-25 14:59:42 +08:00
if err := json.Unmarshal(v, m); err == nil {
beego.Info("从缓存中获取文档信息成功", m.DocumentId)
return m, nil
2018-02-27 17:20:42 +08:00
}
}
2018-03-01 10:12:31 +08:00
defer func() {
if m.DocumentId > 0 {
m.PutToCache()
}
}()
return m.Find(id)
2018-02-27 17:20:42 +08:00
}
2018-03-24 17:24:02 +08:00
//根据文档标识从缓存中查询文档
2018-03-25 14:59:42 +08:00
func (m *Document) FromCacheByIdentify(identify string, bookId int) (*Document, error) {
b := cache.Get(fmt.Sprintf("Document.BookId.%d.Identify.%s", bookId, identify))
if v, ok := b.([]byte); ok {
if err := json.Unmarshal(v, m); err == nil {
beego.Info("从缓存中获取文档信息成功", m.DocumentId, identify)
return m, nil
2018-02-27 17:20:42 +08:00
}
}
2018-03-01 10:12:31 +08:00
defer func() {
if m.DocumentId > 0 {
m.PutToCache()
}
}()
2018-03-25 14:59:42 +08:00
return m.FindByIdentityFirst(identify, bookId)
2018-02-27 17:20:42 +08:00
}
2017-05-15 14:59:23 +08:00
//根据项目ID查询文档列表.
2018-02-27 17:20:42 +08:00
func (m *Document) FindListByBookId(bookId int) (docs []*Document, err error) {
2017-05-06 16:16:27 +08:00
o := orm.NewOrm()
2018-02-27 17:20:42 +08:00
_, err = o.QueryTable(m.TableNameWithPrefix()).Filter("book_id", bookId).OrderBy("order_sort").All(&docs)
2017-04-22 17:24:17 +08:00
2017-05-06 16:16:27 +08:00
return
}
2018-07-13 19:04:51 +08:00
//判断文章是否存在
func (m *Document) IsExist(documentId int) bool {
o := orm.NewOrm()
return o.QueryTable(m.TableNameWithPrefix()).Filter("document_id",documentId).Exist()
}