实现文档保存

This commit is contained in:
Minho
2017-04-28 18:08:01 +08:00
parent ca1b1a4b83
commit f39bd0263a
9 changed files with 445 additions and 81 deletions

View File

@@ -41,6 +41,9 @@ func NewBookResult() *BookResult {
// 根据项目标识查询项目以及指定用户权限的信息.
func (m *BookResult) FindByIdentify(identify string,member_id int) (*BookResult,error) {
if identify == "" || member_id <= 0 {
return m,ErrInvalidParameter
}
o := orm.NewOrm()
book := NewBook()

View File

@@ -5,6 +5,7 @@ import (
"github.com/lifei6671/godoc/conf"
"github.com/astaxie/beego/orm"
"github.com/astaxie/beego/logs"
)
// Document struct.
@@ -12,7 +13,7 @@ type Document struct {
DocumentId int `orm:"pk;auto;unique;column(document_id)" json:"doc_id"`
DocumentName string `orm:"column(document_name);size(500)" json:"doc_name"`
// Identify 文档唯一标识
Identify string `orm:"column(identify);size(100);unique;null;default(null)" json:"identify"`
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" json:"parent_id"`
OrderSort int `orm:"column(order_sort);default(0);type(int);index" json:"order_sort"`
@@ -26,19 +27,10 @@ type Document struct {
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"`
ModifyAt int `orm:"column(modify_at);type(int)" json:"-"`
Version int64 `orm:"type(bigint);column(version)" json:"-"`
Version int64 `orm:"type(bigint);column(version)" json:"version"`
}
type DocumentTree struct {
DocumentId int `json:"id,string"`
DocumentName string `json:"text"`
ParentId int `json:"parent_id,string"`
State *DocumentSelected `json:"state,omitempty"`
}
type DocumentSelected struct {
Selected bool `json:"selected"`
Opened bool `json:"opened"`
}
// TableName 获取对应数据库表名.
func (m *Document) TableName() string {
return "documents"
@@ -63,7 +55,7 @@ func (m *Document) Find(id int) (*Document,error) {
o := orm.NewOrm()
err := o.Read(m)
err := o.QueryTable(m.TableNameWithPrefix()).Filter("document_id",id).One(m)
if err == orm.ErrNoRows{
return m,ErrDataNotExist
@@ -71,37 +63,6 @@ func (m *Document) Find(id int) (*Document,error) {
return m,nil
}
func (m *Document) FindDocumentTree(book_id int) ([]*DocumentTree,error){
o := orm.NewOrm()
trees := make([]*DocumentTree,0)
var docs []*Document
count ,err := o.QueryTable(m).Filter("book_id",book_id).OrderBy("-order_sort","document_id").All(&docs,"document_id","document_name","parent_id")
if err != nil {
return trees,err
}
trees = make([]*DocumentTree,count)
for index,item := range docs {
tree := &DocumentTree{}
if index == 0{
tree.State = &DocumentSelected{ Selected: true, Opened: true }
}
tree.DocumentId = item.DocumentId
tree.ParentId = item.ParentId
tree.DocumentName = item.DocumentName
trees[index] = tree
}
return trees,nil
}
func (m *Document) InsertOrUpdate(cols... string) error {
o := orm.NewOrm()
@@ -122,6 +83,30 @@ func (m *Document) FindByFieldFirst(field string,v interface{}) (*Document,error
return m,err
}
//递归删除一个文档.
func (m *Document) RecursiveDocument(doc_id int) error {
o := orm.NewOrm()
var docs []*Document
_,err := o.QueryTable(m.TableNameWithPrefix()).Filter("parent_id",doc_id).All(&docs)
if err != nil {
logs.Error("",err)
return err
}
for _,item := range docs {
doc_id := item.DocumentId
o.QueryTable(m.TableNameWithPrefix()).Filter("document_id",doc_id).Delete()
m.RecursiveDocument(doc_id)
}
if doc,err := m.Find(doc_id); err != nil {
o.Delete(doc)
}
return nil
}

56
models/document_tree.go Normal file
View File

@@ -0,0 +1,56 @@
package models
import (
"github.com/astaxie/beego/orm"
)
type DocumentTree struct {
DocumentId int `json:"id"`
DocumentName string `json:"text"`
ParentId interface{} `json:"parent"`
Identify string `json:"identify"`
Version int64 `json:"version"`
State *DocumentSelected `json:"state,omitempty"`
}
type DocumentSelected struct {
Selected bool `json:"selected"`
Opened bool `json:"opened"`
}
func (m *Document) FindDocumentTree(book_id int) ([]*DocumentTree,error){
o := orm.NewOrm()
trees := make([]*DocumentTree,0)
var docs []*Document
count ,err := o.QueryTable(m).Filter("book_id",book_id).OrderBy("-order_sort","document_id").All(&docs,"document_id","version","document_name","parent_id","identify")
if err != nil {
return trees,err
}
trees = make([]*DocumentTree,count)
for index,item := range docs {
tree := &DocumentTree{}
if index == 0{
tree.State = &DocumentSelected{ Selected: true, Opened: true }
}
tree.DocumentId = item.DocumentId
tree.Identify = item.Identify
tree.Version = item.Version
if item.ParentId > 0 {
tree.ParentId = item.ParentId
}else{
tree.ParentId = "#"
}
tree.DocumentName = item.DocumentName
trees[index] = tree
}
return trees,nil
}