2017-05-17 15:21:36 +08:00
|
|
|
|
package models
|
|
|
|
|
|
2017-05-19 17:20:33 +08:00
|
|
|
|
import (
|
|
|
|
|
"time"
|
2017-05-20 15:27:03 +08:00
|
|
|
|
|
2021-03-26 11:34:02 +08:00
|
|
|
|
"github.com/beego/beego/v2/client/orm"
|
2021-03-24 00:28:13 +08:00
|
|
|
|
"github.com/beego/beego/v2/core/logs"
|
2021-03-23 15:09:17 +08:00
|
|
|
|
"github.com/mindoc-org/mindoc/conf"
|
2017-05-19 17:20:33 +08:00
|
|
|
|
)
|
|
|
|
|
|
2017-05-17 15:21:36 +08:00
|
|
|
|
type DocumentHistory struct {
|
2017-05-19 17:20:33 +08:00
|
|
|
|
HistoryId int `orm:"column(history_id);pk;auto;unique" json:"history_id"`
|
2022-10-28 12:33:36 +08:00
|
|
|
|
Action string `orm:"column(action);size(255);description(modify)" json:"action"`
|
|
|
|
|
ActionName string `orm:"column(action_name);size(255);description(修改文档)" json:"action_name"`
|
|
|
|
|
DocumentId int `orm:"column(document_id);type(int);index;description(关联文档id)" json:"doc_id"`
|
|
|
|
|
DocumentName string `orm:"column(document_name);size(500);description(关联文档id)" json:"doc_name"`
|
|
|
|
|
ParentId int `orm:"column(parent_id);type(int);index;default(0);description(父级文档id)" json:"parent_id"`
|
|
|
|
|
Markdown string `orm:"column(markdown);type(text);null;description(文档内容)" json:"markdown"`
|
|
|
|
|
Content string `orm:"column(content);type(text);null;description(文档内容)" json:"content"`
|
|
|
|
|
MemberId int `orm:"column(member_id);type(int);description(作者id)" json:"member_id"`
|
|
|
|
|
ModifyTime time.Time `orm:"column(modify_time);type(datetime);auto_now;description(修改时间)" json:"modify_time"`
|
|
|
|
|
ModifyAt int `orm:"column(modify_at);type(int);description(修改人id)" json:"-"`
|
|
|
|
|
Version int64 `orm:"type(bigint);column(version);description(版本)" json:"version"`
|
|
|
|
|
IsOpen int `orm:"column(is_open);type(int);default(0);description(是否展开子目录 0:阅读时关闭节点 1:阅读时展开节点 2:空目录 单击时会展开下级节点)" json:"is_open"`
|
2017-05-19 17:20:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-20 15:27:03 +08:00
|
|
|
|
type DocumentHistorySimpleResult struct {
|
2018-01-26 17:17:38 +08:00
|
|
|
|
HistoryId int `json:"history_id"`
|
|
|
|
|
ActionName string `json:"action_name"`
|
|
|
|
|
MemberId int `json:"member_id"`
|
|
|
|
|
Account string `json:"account"`
|
|
|
|
|
ModifyAt int `json:"modify_at"`
|
|
|
|
|
ModifyName string `json:"modify_name"`
|
|
|
|
|
ModifyTime time.Time `json:"modify_time"`
|
|
|
|
|
Version int64 `json:"version"`
|
2017-05-20 15:27:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-19 17:20:33 +08:00
|
|
|
|
// TableName 获取对应数据库表名.
|
|
|
|
|
func (m *DocumentHistory) TableName() string {
|
|
|
|
|
return "document_history"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TableEngine 获取数据使用的引擎.
|
|
|
|
|
func (m *DocumentHistory) TableEngine() string {
|
|
|
|
|
return "INNODB"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *DocumentHistory) TableNameWithPrefix() string {
|
|
|
|
|
return conf.GetDatabasePrefix() + m.TableName()
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-20 15:27:03 +08:00
|
|
|
|
func NewDocumentHistory() *DocumentHistory {
|
|
|
|
|
return &DocumentHistory{}
|
2017-05-25 15:19:17 +08:00
|
|
|
|
}
|
2018-01-26 17:17:38 +08:00
|
|
|
|
func (m *DocumentHistory) Find(id int) (*DocumentHistory, error) {
|
2017-06-12 17:58:45 +08:00
|
|
|
|
o := orm.NewOrm()
|
2018-01-26 17:17:38 +08:00
|
|
|
|
err := o.QueryTable(m.TableNameWithPrefix()).Filter("history_id", id).One(m)
|
2017-05-25 15:19:17 +08:00
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
|
return m, err
|
2017-05-20 15:27:03 +08:00
|
|
|
|
}
|
2018-01-26 17:17:38 +08:00
|
|
|
|
|
2017-05-20 15:27:03 +08:00
|
|
|
|
//清空指定文档的历史.
|
2018-02-02 23:12:29 +08:00
|
|
|
|
func (m *DocumentHistory) Clear(docId int) error {
|
2017-05-20 15:27:03 +08:00
|
|
|
|
o := orm.NewOrm()
|
|
|
|
|
|
2018-02-02 23:12:29 +08:00
|
|
|
|
_, err := o.Raw("DELETE md_document_history WHERE document_id = ?", docId).Exec()
|
2017-05-20 15:27:03 +08:00
|
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//删除历史.
|
2018-02-02 23:12:29 +08:00
|
|
|
|
func (m *DocumentHistory) Delete(historyId, docId int) error {
|
2017-05-20 15:27:03 +08:00
|
|
|
|
o := orm.NewOrm()
|
|
|
|
|
|
2018-02-02 23:12:29 +08:00
|
|
|
|
_, err := o.QueryTable(m.TableNameWithPrefix()).Filter("history_id", historyId).Filter("document_id", docId).Delete()
|
2017-05-25 15:19:17 +08:00
|
|
|
|
|
2017-05-20 15:27:03 +08:00
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//恢复指定历史的文档.
|
2018-02-02 23:12:29 +08:00
|
|
|
|
func (m *DocumentHistory) Restore(historyId, docId, uid int) error {
|
2017-05-20 15:27:03 +08:00
|
|
|
|
o := orm.NewOrm()
|
|
|
|
|
|
2018-02-02 23:12:29 +08:00
|
|
|
|
err := o.QueryTable(m.TableNameWithPrefix()).Filter("history_id", historyId).Filter("document_id", docId).One(m)
|
2017-05-20 15:27:03 +08:00
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
doc, err := NewDocument().Find(m.DocumentId)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2017-05-25 15:19:17 +08:00
|
|
|
|
history := NewDocumentHistory()
|
2018-02-02 23:12:29 +08:00
|
|
|
|
history.DocumentId = docId
|
2017-05-25 15:19:17 +08:00
|
|
|
|
history.Content = doc.Content
|
|
|
|
|
history.Markdown = doc.Markdown
|
|
|
|
|
history.DocumentName = doc.DocumentName
|
|
|
|
|
history.ModifyAt = uid
|
|
|
|
|
history.MemberId = doc.MemberId
|
|
|
|
|
history.ParentId = doc.ParentId
|
|
|
|
|
history.Version = time.Now().Unix()
|
|
|
|
|
history.Action = "restore"
|
|
|
|
|
history.ActionName = "恢复文档"
|
2018-08-14 18:17:46 +08:00
|
|
|
|
history.IsOpen = doc.IsOpen
|
2017-05-25 15:19:17 +08:00
|
|
|
|
|
|
|
|
|
history.InsertOrUpdate()
|
|
|
|
|
|
2017-05-20 15:27:03 +08:00
|
|
|
|
doc.DocumentName = m.DocumentName
|
|
|
|
|
doc.Content = m.Content
|
|
|
|
|
doc.Markdown = m.Markdown
|
|
|
|
|
doc.Release = m.Content
|
2017-05-25 15:19:17 +08:00
|
|
|
|
doc.Version = time.Now().Unix()
|
2018-08-14 18:17:46 +08:00
|
|
|
|
doc.IsOpen = m.IsOpen
|
2017-05-20 15:27:03 +08:00
|
|
|
|
|
|
|
|
|
_, err = o.Update(doc)
|
|
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
|
func (m *DocumentHistory) InsertOrUpdate() (history *DocumentHistory, err error) {
|
2017-05-20 15:27:03 +08:00
|
|
|
|
o := orm.NewOrm()
|
|
|
|
|
history = m
|
2017-05-19 17:20:33 +08:00
|
|
|
|
|
2017-05-20 15:27:03 +08:00
|
|
|
|
if m.HistoryId > 0 {
|
2018-01-26 17:17:38 +08:00
|
|
|
|
_, err = o.Update(m)
|
|
|
|
|
} else {
|
|
|
|
|
_, err = o.Insert(m)
|
2018-02-02 23:12:29 +08:00
|
|
|
|
if err == nil {
|
2021-03-23 21:55:50 +08:00
|
|
|
|
if doc, e := NewDocument().Find(m.DocumentId); e == nil {
|
|
|
|
|
if book, e := NewBook().Find(doc.BookId); e == nil && book.HistoryCount > 0 {
|
2018-02-02 23:12:29 +08:00
|
|
|
|
//如果已存在的历史记录大于指定的记录,则清除旧记录
|
2021-03-23 21:55:50 +08:00
|
|
|
|
if c, e := o.QueryTable(m.TableNameWithPrefix()).Filter("document_id", doc.DocumentId).Count(); e == nil && c > int64(book.HistoryCount) {
|
2018-02-02 23:12:29 +08:00
|
|
|
|
|
|
|
|
|
count := c - int64(book.HistoryCount)
|
2021-03-23 21:55:50 +08:00
|
|
|
|
logs.Info("需要删除的历史文档数量:", count)
|
2018-02-02 23:12:29 +08:00
|
|
|
|
var lists []DocumentHistory
|
|
|
|
|
|
2021-03-23 21:55:50 +08:00
|
|
|
|
if _, e := o.QueryTable(m.TableNameWithPrefix()).Filter("document_id", doc.DocumentId).OrderBy("history_id").Limit(count).All(&lists, "history_id"); e == nil {
|
|
|
|
|
for _, d := range lists {
|
2018-02-02 23:12:29 +08:00
|
|
|
|
o.Delete(&d)
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-23 21:55:50 +08:00
|
|
|
|
} else {
|
|
|
|
|
logs.Info(book.HistoryCount)
|
2018-02-02 23:12:29 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2017-05-20 15:27:03 +08:00
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
2018-01-26 17:17:38 +08:00
|
|
|
|
|
2017-05-20 15:27:03 +08:00
|
|
|
|
//分页查询指定文档的历史.
|
2018-02-02 23:12:29 +08:00
|
|
|
|
func (m *DocumentHistory) FindToPager(docId, pageIndex, pageSize int) (docs []*DocumentHistorySimpleResult, totalCount int, err error) {
|
2017-05-19 17:20:33 +08:00
|
|
|
|
|
|
|
|
|
o := orm.NewOrm()
|
|
|
|
|
|
2018-02-02 23:12:29 +08:00
|
|
|
|
offset := (pageIndex - 1) * pageSize
|
2017-05-19 17:20:33 +08:00
|
|
|
|
|
|
|
|
|
totalCount = 0
|
2017-05-20 15:27:03 +08:00
|
|
|
|
|
2017-05-25 15:19:17 +08:00
|
|
|
|
sql := `SELECT history.*,m1.account,m2.account as modify_name
|
2017-05-20 15:27:03 +08:00
|
|
|
|
FROM md_document_history AS history
|
|
|
|
|
LEFT JOIN md_members AS m1 ON history.member_id = m1.member_id
|
2017-05-25 15:19:17 +08:00
|
|
|
|
LEFT JOIN md_members AS m2 ON history.modify_at = m2.member_id
|
2023-04-14 11:09:53 +08:00
|
|
|
|
WHERE history.document_id = ? ORDER BY history.history_id DESC limit ? offset ?;`
|
2017-05-20 15:27:03 +08:00
|
|
|
|
|
2023-04-14 11:09:53 +08:00
|
|
|
|
_, err = o.Raw(sql, docId, pageSize, offset).QueryRows(&docs)
|
2017-05-19 17:20:33 +08:00
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
var count int64
|
2018-02-02 23:12:29 +08:00
|
|
|
|
count, err = o.QueryTable(m.TableNameWithPrefix()).Filter("document_id", docId).Count()
|
2017-05-19 17:20:33 +08:00
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
totalCount = int(count)
|
2017-05-17 15:21:36 +08:00
|
|
|
|
|
2017-05-19 17:20:33 +08:00
|
|
|
|
return
|
2017-05-20 15:27:03 +08:00
|
|
|
|
}
|