|
@@ -5,6 +5,7 @@ import (
|
|
|
|
|
|
"github.com/astaxie/beego/orm"
|
|
"github.com/astaxie/beego/orm"
|
|
"github.com/lifei6671/mindoc/conf"
|
|
"github.com/lifei6671/mindoc/conf"
|
|
|
|
+ "github.com/astaxie/beego"
|
|
)
|
|
)
|
|
|
|
|
|
type DocumentHistory struct {
|
|
type DocumentHistory struct {
|
|
@@ -58,28 +59,28 @@ func (m *DocumentHistory) Find(id int) (*DocumentHistory, error) {
|
|
}
|
|
}
|
|
|
|
|
|
//清空指定文档的历史.
|
|
//清空指定文档的历史.
|
|
-func (m *DocumentHistory) Clear(doc_id int) error {
|
|
|
|
|
|
+func (m *DocumentHistory) Clear(docId int) error {
|
|
o := orm.NewOrm()
|
|
o := orm.NewOrm()
|
|
|
|
|
|
- _, err := o.Raw("DELETE md_document_history WHERE document_id = ?", doc_id).Exec()
|
|
|
|
|
|
+ _, err := o.Raw("DELETE md_document_history WHERE document_id = ?", docId).Exec()
|
|
|
|
|
|
return err
|
|
return err
|
|
}
|
|
}
|
|
|
|
|
|
//删除历史.
|
|
//删除历史.
|
|
-func (m *DocumentHistory) Delete(history_id, doc_id int) error {
|
|
|
|
|
|
+func (m *DocumentHistory) Delete(historyId, docId int) error {
|
|
o := orm.NewOrm()
|
|
o := orm.NewOrm()
|
|
|
|
|
|
- _, err := o.QueryTable(m.TableNameWithPrefix()).Filter("history_id", history_id).Filter("document_id", doc_id).Delete()
|
|
|
|
|
|
+ _, err := o.QueryTable(m.TableNameWithPrefix()).Filter("history_id", historyId).Filter("document_id", docId).Delete()
|
|
|
|
|
|
return err
|
|
return err
|
|
}
|
|
}
|
|
|
|
|
|
//恢复指定历史的文档.
|
|
//恢复指定历史的文档.
|
|
-func (m *DocumentHistory) Restore(history_id, doc_id, uid int) error {
|
|
|
|
|
|
+func (m *DocumentHistory) Restore(historyId, docId, uid int) error {
|
|
o := orm.NewOrm()
|
|
o := orm.NewOrm()
|
|
|
|
|
|
- err := o.QueryTable(m.TableNameWithPrefix()).Filter("history_id", history_id).Filter("document_id", doc_id).One(m)
|
|
|
|
|
|
+ err := o.QueryTable(m.TableNameWithPrefix()).Filter("history_id", historyId).Filter("document_id", docId).One(m)
|
|
|
|
|
|
if err != nil {
|
|
if err != nil {
|
|
return err
|
|
return err
|
|
@@ -90,7 +91,7 @@ func (m *DocumentHistory) Restore(history_id, doc_id, uid int) error {
|
|
return err
|
|
return err
|
|
}
|
|
}
|
|
history := NewDocumentHistory()
|
|
history := NewDocumentHistory()
|
|
- history.DocumentId = doc_id
|
|
|
|
|
|
+ history.DocumentId = docId
|
|
history.Content = doc.Content
|
|
history.Content = doc.Content
|
|
history.Markdown = doc.Markdown
|
|
history.Markdown = doc.Markdown
|
|
history.DocumentName = doc.DocumentName
|
|
history.DocumentName = doc.DocumentName
|
|
@@ -122,16 +123,38 @@ func (m *DocumentHistory) InsertOrUpdate() (history *DocumentHistory, err error)
|
|
_, err = o.Update(m)
|
|
_, err = o.Update(m)
|
|
} else {
|
|
} else {
|
|
_, err = o.Insert(m)
|
|
_, err = o.Insert(m)
|
|
|
|
+ if err == nil {
|
|
|
|
+ if doc,e := NewDocument().Find(m.DocumentId);e == nil {
|
|
|
|
+ if book,e := NewBook().Find(doc.BookId);e == nil && book.HistoryCount > 0 {
|
|
|
|
+ //如果已存在的历史记录大于指定的记录,则清除旧记录
|
|
|
|
+ if c,e := o.QueryTable(m.TableNameWithPrefix()).Filter("document_id",doc.DocumentId).Count(); e == nil && c > int64(book.HistoryCount) {
|
|
|
|
+
|
|
|
|
+ count := c - int64(book.HistoryCount)
|
|
|
|
+ beego.Info("需要删除的历史文档数量:" ,count)
|
|
|
|
+ var lists []DocumentHistory
|
|
|
|
+
|
|
|
|
+ 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 {
|
|
|
|
+ o.Delete(&d)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }else{
|
|
|
|
+ beego.Info(book.HistoryCount)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
}
|
|
}
|
|
return
|
|
return
|
|
}
|
|
}
|
|
|
|
|
|
//分页查询指定文档的历史.
|
|
//分页查询指定文档的历史.
|
|
-func (m *DocumentHistory) FindToPager(doc_id, page_index, page_size int) (docs []*DocumentHistorySimpleResult, totalCount int, err error) {
|
|
|
|
|
|
+func (m *DocumentHistory) FindToPager(docId, pageIndex, pageSize int) (docs []*DocumentHistorySimpleResult, totalCount int, err error) {
|
|
|
|
|
|
o := orm.NewOrm()
|
|
o := orm.NewOrm()
|
|
|
|
|
|
- offset := (page_index - 1) * page_size
|
|
|
|
|
|
+ offset := (pageIndex - 1) * pageSize
|
|
|
|
|
|
totalCount = 0
|
|
totalCount = 0
|
|
|
|
|
|
@@ -141,13 +164,13 @@ LEFT JOIN md_members AS m1 ON history.member_id = m1.member_id
|
|
LEFT JOIN md_members AS m2 ON history.modify_at = m2.member_id
|
|
LEFT JOIN md_members AS m2 ON history.modify_at = m2.member_id
|
|
WHERE history.document_id = ? ORDER BY history.history_id DESC LIMIT ?,?;`
|
|
WHERE history.document_id = ? ORDER BY history.history_id DESC LIMIT ?,?;`
|
|
|
|
|
|
- _, err = o.Raw(sql, doc_id, offset, page_size).QueryRows(&docs)
|
|
|
|
|
|
+ _, err = o.Raw(sql, docId, offset, pageSize).QueryRows(&docs)
|
|
|
|
|
|
if err != nil {
|
|
if err != nil {
|
|
return
|
|
return
|
|
}
|
|
}
|
|
var count int64
|
|
var count int64
|
|
- count, err = o.QueryTable(m.TableNameWithPrefix()).Filter("document_id", doc_id).Count()
|
|
|
|
|
|
+ count, err = o.QueryTable(m.TableNameWithPrefix()).Filter("document_id", docId).Count()
|
|
|
|
|
|
if err != nil {
|
|
if err != nil {
|
|
return
|
|
return
|