no message

master
zcy 2022-04-14 00:38:16 +08:00
parent 12899f01ea
commit c14a02b503
3 changed files with 108 additions and 26 deletions

View File

@ -6,12 +6,65 @@ import (
"background/model" "background/model"
"fmt" "fmt"
"strconv" "strconv"
"sync"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"gopkg.in/olivere/elastic.v7" "gopkg.in/olivere/elastic.v7"
"qiniupkg.com/x/log.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) { func GetPageParaFromQuery(c *gin.Context) (int, int) {
limit := c.Query("limit") limit := c.Query("limit")
offset := c.Query("offset") offset := c.Query("offset")
@ -27,6 +80,31 @@ func GetPageParaFromQuery(c *gin.Context) (int, int) {
return iLmit, iOffset 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) { func GetArticles(c *gin.Context) {
type ReqArticles struct { type ReqArticles struct {
Name string `json:"name"` Name string `json:"name"`

View File

@ -159,6 +159,8 @@ func main() {
api.GET("/article/:id", controller.GetArticle) //获取文章 api.GET("/article/:id", controller.GetArticle) //获取文章
api.POST("/articles", controller.GetArticles) // 获取所有文章 api.POST("/articles", controller.GetArticles) // 获取所有文章
api.GET("/articles_tree", controller.GetArticlesTree) // 获取所有文章
api.PUT("/article", controller.AddArticle) // 添加文章 api.PUT("/article", controller.AddArticle) // 添加文章
api.PUT("/article_search", controller.SearchArticles) // 添加文章 api.PUT("/article_search", controller.SearchArticles) // 添加文章
api.GET("/article_type", controller.ArticlesType) //获取所有文章分类 api.GET("/article_type", controller.ArticlesType) //获取所有文章分类

View File

@ -137,13 +137,13 @@ func TestPortDocVersion(t *testing.T) {
InitMysql() InitMysql()
docs := []model.Doc{} docs := []model.Doc{}
// doc_groups := []model.DocGroup{} doc_groups := []model.DocGroup{}
doc_types := []model.DocType{} 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{ if nil != e{
// log.Print(e.Error()) log.Print(e.Error())
// } }
// for k,v := range doc_groups{ // for k,v := range doc_groups{
// log.Print(k,v) // log.Print(k,v)
// e := db.GetMysqlClient().Query2("select * from doc_group",&doc_groups) // e := db.GetMysqlClient().Query2("select * from doc_group",&doc_groups)
@ -151,13 +151,13 @@ func TestPortDocVersion(t *testing.T) {
// log.Print(e.Error()) // 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)", // 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")) // v.Name,time.Now().Format("2006-01-02 15:04:05"))
// // log.Print(str) // log.Print(str)
// // _,e = db.GetMysqlClient().Query(str) // _,e = db.GetMysqlClient().Query(str)
// // if nil != e{ // if nil != e{
// // log.Print(e.Error()) // log.Print(e.Error())
// // } // }
// } // }
// e = db.GetMysqlClient().Query2("select * from doc_type",&doc_types) // e = db.GetMysqlClient().Query2("select * from doc_type",&doc_types)
// if(nil != e){ // if(nil != e){
@ -172,23 +172,23 @@ func TestPortDocVersion(t *testing.T) {
// } // }
// log.Print(doc_group[0]) // log.Print(doc_group[0])
// doc := []model.Doc{} // 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{ // if nil != e{
// log.Print(e.Error()) // log.Print(e.Error())
// } // }
// log.Print(doc) // 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)", // 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) // v.TypeName,time.Now().Format("2006-01-02 15:04:05"),doc[0].ID)
// // log.Print(str) // log.Print(str)
// // _,e = db.GetMysqlClient().Query(str) // _,e = db.GetMysqlClient().Query(str)
// // if nil != e{ // if nil != e{
// // log.Print(e.Error()) // 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){ if(nil != e){
log.Print(e.Error()) log.Print(e.Error())
} }
@ -202,20 +202,22 @@ func TestPortDocVersion(t *testing.T) {
log.Print(doc_types) log.Print(doc_types)
if len(doc_types) > 0{ if len(doc_types) > 0{
docsfortype := []model.Doc{} 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) doc_types[0].TypeName),&docsfortype)
if nil != e{ if nil != e{
log.Print(e.Error()) log.Print(e.Error())
} }
log.Print(docsfortype) log.Print(docsfortype)
if len(docsfortype) > 0{ if len(docsfortype) > 0{
str := fmt.Sprintf("update doc_copy2 set father = %d ,level = %d where doc_copy2.id = %d", str := fmt.Sprintf("update doc_copy1 set father = %d ,level = %d where doc_copy1.id = %d",
docsfortype[0].ID,docsfortype[0].Level,(v.ID+1)) docsfortype[0].ID,docsfortype[0].Level + 1,(v.ID))
log.Print(str) log.Print(str)
_,e = db.GetMysqlClient().Query(str)
if nil != e{
log.Printf(e.Error())
}
} }
} }
} }
} }