浏览代码

修复生成树状文档结构时子节点重复的问题

Minho 8 年之前
父节点
当前提交
dc6ea88c58
共有 5 个文件被更改,包括 37 次插入14 次删除
  1. 3 1
      controllers/book.go
  2. 10 5
      controllers/document.go
  3. 11 0
      models/book.go
  4. 4 0
      models/document.go
  5. 9 8
      models/document_tree.go

+ 3 - 1
controllers/book.go

@@ -70,6 +70,7 @@ func (c *BookController) Dashboard() {
 		if err == models.ErrPermissionDenied {
 			c.Abort("403")
 		}
+		beego.Error(err)
 		c.Abort("500")
 	}
 
@@ -501,7 +502,7 @@ func (c *BookController) Delete() {
 	c.JsonResult(0,"ok")
 }
 
-//发布项目
+//发布项目.
 func (c *BookController) Release() {
 	c.Prepare()
 
@@ -527,6 +528,7 @@ func (c *BookController) Release() {
 	c.JsonResult(0,"发布任务已推送到任务队列,稍后将在后台执行。")
 }
 
+//文档排序.
 func (c *BookController) SaveSort() {
 	c.Prepare()
 

+ 10 - 5
controllers/document.go

@@ -17,11 +17,12 @@ import (
 	"github.com/astaxie/beego/orm"
 )
 
+//DocumentController struct.
 type DocumentController struct {
 	BaseController
 }
 
-//判断用户是否可以阅读文档
+//判断用户是否可以阅读文档.
 func isReadable (identify,token string,c *DocumentController) *models.BookResult {
 	book, err := models.NewBook().FindByFieldFirst("identify", identify)
 
@@ -81,7 +82,7 @@ func isReadable (identify,token string,c *DocumentController) *models.BookResult
 	return bookResult
 }
 
-
+//文档首页.
 func (c *DocumentController) Index()  {
 	c.Prepare()
 	identify := c.Ctx.Input.Param(":key")
@@ -108,7 +109,7 @@ func (c *DocumentController) Index()  {
 	c.Data["Title"] = "概要"
 	c.Data["Content"] = bookResult.Description
 }
-
+//阅读文档.
 func (c *DocumentController) Read() {
 	c.Prepare()
 	identify := c.Ctx.Input.Param(":key")
@@ -167,6 +168,7 @@ func (c *DocumentController) Read() {
 	c.Data["Content"] = template.HTML(doc.Release)
 }
 
+//编辑文档.
 func (c *DocumentController) Edit()  {
 	c.Prepare()
 
@@ -273,7 +275,7 @@ func (c *DocumentController) Create() {
 		beego.Error("InsertOrUpdate => ",err)
 		c.JsonResult(6005,"保存失败")
 	}else{
-		beego.Info("",document)
+
 		c.JsonResult(0,"ok",document)
 	}
 }
@@ -446,6 +448,7 @@ func (c *DocumentController) DownloadAttachment()  {
 	c.StopRun()
 }
 
+//删除文档.
 func (c *DocumentController) Delete() {
 	c.Prepare()
 
@@ -476,10 +479,12 @@ func (c *DocumentController) Delete() {
 	if err != nil {
 		c.JsonResult(6005,"删除失败")
 	}
-
+	//重置文档数量统计
+	models.NewBook().ResetDocumentNumber(doc.BookId)
 	c.JsonResult(0,"ok")
 }
 
+//获取文档内容.
 func (c *DocumentController) Content()  {
 	c.Prepare()
 

+ 11 - 0
models/book.go

@@ -7,6 +7,7 @@ import (
 	"github.com/lifei6671/godoc/conf"
 	"github.com/astaxie/beego/logs"
 	"strings"
+	"github.com/astaxie/beego"
 )
 
 // Book struct .
@@ -325,7 +326,17 @@ func (book *Book) ToBookResult() *BookResult {
 	return m
 }
 
+//重置文档数量
+func (m *Book) ResetDocumentNumber(book_id int)  {
+	o := orm.NewOrm()
 
+	totalCount,err := o.QueryTable(NewDocument().TableNameWithPrefix()).Filter("book_id",book_id).Count()
+	if err == nil {
+		o.Raw("UPDATE md_books SET doc_count = ? WHERE book_id = ?",int(totalCount),book_id).Exec()
+	}else{
+		beego.Error(err)
+	}
+}
 
 
 

+ 4 - 0
models/document.go

@@ -65,6 +65,7 @@ func (m *Document) Find(id int) (*Document,error) {
 	return m,nil
 }
 
+//插入和更新文档.
 func (m *Document) InsertOrUpdate(cols... string) error {
 	o := orm.NewOrm()
 
@@ -73,10 +74,12 @@ func (m *Document) InsertOrUpdate(cols... string) error {
 		return err
 	}else{
 		_,err := o.Insert(m)
+		NewBook().ResetDocumentNumber(m.BookId)
 		return err
 	}
 }
 
+//根据指定字段查询一条文档.
 func (m *Document) FindByFieldFirst(field string,v interface{}) (*Document,error) {
 	o := orm.NewOrm()
 
@@ -112,6 +115,7 @@ func (m *Document) RecursiveDocument(doc_id int) error {
 	return nil
 }
 
+//发布文档
 func (m *Document) ReleaseContent(book_id int)  {
 
 	o := orm.NewOrm()

+ 9 - 8
models/document_tree.go

@@ -96,7 +96,7 @@ func getDocumentTree(array []*DocumentTree,parent_id int,selected_id int,selecte
 	for _,item := range array {
 		pid := 0
 
-		if p,ok := item.ParentId.(int);ok {
+		if p, ok := item.ParentId.(int); ok {
 			pid = p
 		}
 		if pid == parent_id {
@@ -126,11 +126,11 @@ func getDocumentTree(array []*DocumentTree,parent_id int,selected_id int,selecte
 			buf.WriteString("\"")
 			buf.WriteString(selected_li)
 			buf.WriteString("><a href=\"")
-			if item.Identify != ""{
-				uri := beego.URLFor("DocumentController.Read",":key",item.BookIdentify,":id" ,item.Identify)
+			if item.Identify != "" {
+				uri := beego.URLFor("DocumentController.Read", ":key", item.BookIdentify, ":id", item.Identify)
 				buf.WriteString(uri)
-			}else{
-				uri := beego.URLFor("DocumentController.Read",":key",item.BookIdentify,":id" ,item.DocumentId)
+			} else {
+				uri := beego.URLFor("DocumentController.Read", ":key", item.BookIdentify, ":id", item.DocumentId)
 				buf.WriteString(uri)
 			}
 			buf.WriteString("\" title=\"")
@@ -138,9 +138,10 @@ func getDocumentTree(array []*DocumentTree,parent_id int,selected_id int,selecte
 			buf.WriteString(selected + ">")
 			buf.WriteString(template.HTMLEscapeString(item.DocumentName) + "</a>")
 
-			for _,sub := range array {
-				if p,ok := sub.ParentId.(int);ok && p == item.DocumentId{
-					getDocumentTree(array,p,selected_id,selected_parent_id,buf)
+			for _, sub := range array {
+				if p, ok := sub.ParentId.(int); ok && p == item.DocumentId {
+					getDocumentTree(array, p, selected_id, selected_parent_id, buf)
+					break
 				}
 			}
 			buf.WriteString("</li>")