From c14a02b50338ed30151023343ae8c22b95e473d6 Mon Sep 17 00:00:00 2001 From: zcy <290198252@qq.com> Date: Thu, 14 Apr 2022 00:38:16 +0800 Subject: [PATCH] no message --- controller/blog.go | 78 +++++++++++++++++++++++++++++++++++++++++++ main.go | 2 ++ test/portData_test.go | 54 +++++++++++++++--------------- 3 files changed, 108 insertions(+), 26 deletions(-) diff --git a/controller/blog.go b/controller/blog.go index d3419ed..906ebec 100644 --- a/controller/blog.go +++ b/controller/blog.go @@ -6,12 +6,65 @@ import ( "background/model" "fmt" "strconv" + "sync" "github.com/gin-gonic/gin" "gopkg.in/olivere/elastic.v7" "qiniupkg.com/x/log.v7" ) +type DocTree struct{ + ID int64 `json:"id"` + Label string `json:"label"` + Children []*DocTree `json:"children"` +} + +var gDocMapCache map[int]*DocTree = nil +var gLock sync.Mutex + +func init(){ + if nil == gDocMapCache{ + gDocMapCache = make(map[int]*DocTree) + } +} + +func InitDocCache(){ + gLock.Lock() + for k := range gDocMapCache { + delete(gDocMapCache, k) + } + + articles := []model.Doc{} + e := db.GetMysqlClient().Query2("select id,title,father,level from doc_copy1", &articles) + if nil != e { + logs.Error(e.Error()) + return + } + for _,v := range articles{ + tmp := new (DocTree) + tmp.ID = v.ID + tmp.Label = v.Title + tmp.Children = make([]*DocTree,0) + gDocMapCache[int(v.ID)] = tmp + } + for _,v := range articles{ + tmp := new (DocTree) + tmp.ID = v.ID + tmp.Label = v.Title + tmp.Children = make([]*DocTree,0) + gDocMapCache[int(v.ID)] = tmp + } + for _,v := range articles{ + if v.Father != 0{ + gDocMapCache[int(v.Father)].Children = append(gDocMapCache[int(v.Father)].Children, gDocMapCache[int(v.ID)]) + } + } + + gLock.Unlock() + log.Print(gDocMapCache) + +} + func GetPageParaFromQuery(c *gin.Context) (int, int) { limit := c.Query("limit") offset := c.Query("offset") @@ -27,6 +80,31 @@ func GetPageParaFromQuery(c *gin.Context) (int, int) { return iLmit, iOffset } +func GetArticlesTree(c *gin.Context) { + rsp := RespBase{"ERR", -1, nil} + defer func() { + c.JSON(200, rsp) + }() + articles := []model.Doc{} + e := db.GetMysqlClient().Query2("select id,title,father,level from doc_copy1", &articles) + if nil != e { + logs.Error(e.Error()) + return + } + + InitDocCache() + + ret := []*DocTree{} + for _, v := range articles { + if v.Level == 0{ + ret = append(ret, gDocMapCache[int(v.ID)]) + } + } + + rsp.Data = ret + rsp.Msg = "OK" + rsp.Status = 0 +} func GetArticles(c *gin.Context) { type ReqArticles struct { Name string `json:"name"` diff --git a/main.go b/main.go index 47b921b..181fd22 100644 --- a/main.go +++ b/main.go @@ -159,6 +159,8 @@ func main() { api.GET("/article/:id", controller.GetArticle) //获取文章 api.POST("/articles", controller.GetArticles) // 获取所有文章 + api.GET("/articles_tree", controller.GetArticlesTree) // 获取所有文章 + api.PUT("/article", controller.AddArticle) // 添加文章 api.PUT("/article_search", controller.SearchArticles) // 添加文章 api.GET("/article_type", controller.ArticlesType) //获取所有文章分类 diff --git a/test/portData_test.go b/test/portData_test.go index dbf7709..a0e295b 100644 --- a/test/portData_test.go +++ b/test/portData_test.go @@ -137,13 +137,13 @@ func TestPortDocVersion(t *testing.T) { InitMysql() docs := []model.Doc{} - // doc_groups := []model.DocGroup{} + doc_groups := []model.DocGroup{} doc_types := []model.DocType{} - // e := db.GetMysqlClient().Query2("select * from doc_group",&doc_groups) + e := db.GetMysqlClient().Query2("select * from doc_group",&doc_groups) - // if nil != e{ - // log.Print(e.Error()) - // } + if nil != e{ + log.Print(e.Error()) + } // for k,v := range doc_groups{ // log.Print(k,v) // e := db.GetMysqlClient().Query2("select * from doc_group",&doc_groups) @@ -151,13 +151,13 @@ func TestPortDocVersion(t *testing.T) { // log.Print(e.Error()) // } - // // str := fmt.Sprintf("insert into doc_copy2 (title,type,content,author,create_time,version,is_public, deleted,origin_url,es_id,father,level) values ('%s',0,'','admin','%s', 1,1,0,'','',0,0)", - // // v.Name,time.Now().Format("2006-01-02 15:04:05")) - // // log.Print(str) - // // _,e = db.GetMysqlClient().Query(str) - // // if nil != e{ - // // log.Print(e.Error()) - // // } + // str := fmt.Sprintf("insert into doc_copy1 (title,type,content,author,create_time,version,is_public, deleted,origin_url,es_id,father,level) values ('%s',0,'','admin','%s', 1,1,0,'','',0,0)", + // v.Name,time.Now().Format("2006-01-02 15:04:05")) + // log.Print(str) + // _,e = db.GetMysqlClient().Query(str) + // if nil != e{ + // log.Print(e.Error()) + // } // } // e = db.GetMysqlClient().Query2("select * from doc_type",&doc_types) // if(nil != e){ @@ -172,23 +172,23 @@ func TestPortDocVersion(t *testing.T) { // } // log.Print(doc_group[0]) // doc := []model.Doc{} - // e = db.GetMysqlClient().Query2(fmt.Sprintf("select * from doc_copy2 where doc_copy2.title = '%s' and doc_copy2.father=0",doc_group[0].Name),&doc) + // e = db.GetMysqlClient().Query2(fmt.Sprintf("select * from doc_copy1 where doc_copy1.title = '%s' and doc_copy1.father=0",doc_group[0].Name),&doc) // if nil != e{ // log.Print(e.Error()) // } // log.Print(doc) - // // str := fmt.Sprintf("insert into doc_copy2 (title,type,content,author,create_time,version,is_public, deleted,origin_url,es_id,father,level) values ('%s',0,'','admin','%s', 1,1,0,'','',%d,1)", - // // v.TypeName,time.Now().Format("2006-01-02 15:04:05"),doc[0].ID) - // // log.Print(str) - // // _,e = db.GetMysqlClient().Query(str) - // // if nil != e{ - // // log.Print(e.Error()) - // // } + // str := fmt.Sprintf("insert into doc_copy1 (title,type,content,author,create_time,version,is_public, deleted,origin_url,es_id,father,level) values ('%s',0,'','admin','%s', 1,1,0,'','',%d,1)", + // v.TypeName,time.Now().Format("2006-01-02 15:04:05"),doc[0].ID) + // log.Print(str) + // _,e = db.GetMysqlClient().Query(str) + // if nil != e{ + // log.Print(e.Error()) + // } // } - e := db.GetMysqlClient().Query2("select id,title,type from doc_copy2 where doc_copy2.father is NULL ",&docs) + e = db.GetMysqlClient().Query2("select id,title,type from doc_copy1 where doc_copy1.father is NULL ",&docs) if(nil != e){ log.Print(e.Error()) } @@ -202,20 +202,22 @@ func TestPortDocVersion(t *testing.T) { log.Print(doc_types) if len(doc_types) > 0{ docsfortype := []model.Doc{} - e = db.GetMysqlClient().Query2(fmt.Sprintf("select * from doc_copy2 where doc_copy2.title = '%s' and doc_copy2.father is not NULL ", + e = db.GetMysqlClient().Query2(fmt.Sprintf("select * from doc_copy1 where doc_copy1.title = '%s' and doc_copy1.level = 1", doc_types[0].TypeName),&docsfortype) if nil != e{ log.Print(e.Error()) } log.Print(docsfortype) if len(docsfortype) > 0{ - str := fmt.Sprintf("update doc_copy2 set father = %d ,level = %d where doc_copy2.id = %d", - docsfortype[0].ID,docsfortype[0].Level,(v.ID+1)) + str := fmt.Sprintf("update doc_copy1 set father = %d ,level = %d where doc_copy1.id = %d", + docsfortype[0].ID,docsfortype[0].Level + 1,(v.ID)) log.Print(str) + _,e = db.GetMysqlClient().Query(str) + if nil != e{ + log.Printf(e.Error()) + } } } - - } }