diff --git a/.vscode/settings.json b/.vscode/settings.json index 5bbb269..c6521a2 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +1,4 @@ { - "vue3snippets.enable-compile-vue-file-on-did-save-code": false + "vue3snippets.enable-compile-vue-file-on-did-save-code": false, + "C_Cpp.intelliSenseEngineFallback": "enabled" } \ No newline at end of file diff --git a/config/config.go b/config/config.go index 01dbb7f..f964601 100644 --- a/config/config.go +++ b/config/config.go @@ -78,6 +78,7 @@ var gConf ConfAPI func ApiConfig() ConfAPI{ return gConf } + func Init(path string) error { file, e := os.Open(path) if nil != e { diff --git a/config/redisConf.go b/config/redisConf.go index 4043348..4e8cf8e 100644 --- a/config/redisConf.go +++ b/config/redisConf.go @@ -52,6 +52,8 @@ func InitRedis() error { } return nil } + + func RedisOne() *redis.Client { return gRedis1 } diff --git a/controller/blog.go b/controller/blog.go index bce35f0..bb783b5 100644 --- a/controller/blog.go +++ b/controller/blog.go @@ -7,6 +7,7 @@ import ( "background/model" "fmt" "strconv" + "strings" "sync" "github.com/gin-gonic/gin" @@ -14,56 +15,56 @@ import ( "qiniupkg.com/x/log.v7" ) -type DocTree struct{ - ID int64 `json:"id"` - Label string `json:"label"` - Level int `json:"level"` +type DocTree struct { + ID int64 `json:"id"` + Label string `json:"label"` + Level int `json:"level"` Children []*DocTree `json:"children"` } -var gDocMapCache map[int]*DocTree = nil +var gDocMapCache map[int]*DocTree = nil var gLock sync.Mutex -func init(){ - if nil == gDocMapCache{ +func init() { + if nil == gDocMapCache { gDocMapCache = make(map[int]*DocTree) } } -func InitDocCache(){ +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 + for _, v := range articles { + tmp := new(DocTree) + tmp.ID = v.ID tmp.Level = int(v.Level) tmp.Label = v.Title - tmp.Children = make([]*DocTree,0) + tmp.Children = make([]*DocTree, 0) gDocMapCache[int(v.ID)] = tmp } - for _,v := range articles{ - tmp := new (DocTree) - tmp.ID = v.ID + for _, v := range articles { + tmp := new(DocTree) + tmp.ID = v.ID tmp.Label = v.Title tmp.Level = int(v.Level) - tmp.Children = make([]*DocTree,0) + tmp.Children = make([]*DocTree, 0) gDocMapCache[int(v.ID)] = tmp } - for _,v := range articles{ - if v.Father != 0{ + 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) @@ -99,11 +100,11 @@ func GetArticlesTree(c *gin.Context) { InitDocCache() ret := []*DocTree{} for _, v := range articles { - if v.Level == 0{ + if v.Level == 0 { ret = append(ret, gDocMapCache[int(v.ID)]) } } - + rsp.Data = ret rsp.Msg = "OK" rsp.Status = 0 @@ -167,7 +168,7 @@ func GetArticles(c *gin.Context) { } sql = fmt.Sprintf("SELECT doc.id,doc.author,LEFT(doc.content,50) as content,doc.title,doc.type from doc where doc.type = %d and is_public = %d "+ " order by id desc limit %d offset %d", req.Type, is_public, limit, offset) - + } } log.Print(sql) @@ -213,7 +214,7 @@ func GetArticleCount(c *gin.Context) { func AddArticleHistory(c *gin.Context) { rsp := RespBase{Msg: "FAIL", Status: 211} type ReqUpdateArticle struct { - ID int64 `json:"id"` + ID int64 `json:"id"` Title string `json:"title"` Content string `json:"content"` Author string `json:"author"` @@ -238,15 +239,15 @@ func AddArticleHistory(c *gin.Context) { } e := model.InsertDocHistory(model.DocTree{ - ID: (req.ID), - Type: int32(req.Type), - Title: req.Title, - Content: req.Content, - Author: req.Author, - Father: int32(req.Father), - Level: int32(req.Level), - },) - if(nil != e){ + ID: (req.ID), + Type: int32(req.Type), + Title: req.Title, + Content: req.Content, + Author: req.Author, + Father: int32(req.Father), + Level: int32(req.Level), + }) + if nil != e { logs.Error(er.Error()) return } @@ -277,7 +278,7 @@ func GetArticleHistory(c *gin.Context) { } func GetArticle(c *gin.Context) { - + resp := RespBase{Msg: "FAIL", Status: 211} sid := c.Param("id") var id int @@ -298,7 +299,7 @@ func GetArticle(c *gin.Context) { query := fmt.Sprintf("select * from doc_copy1 where doc_copy1.id = '%d'", id) docs := []model.Doc{} e := db.GetMysqlClient().Query2(query, &docs) - fmt.Print(query,docs) + fmt.Print(query, docs) if nil != e { log.Print(e.Error()) @@ -315,7 +316,7 @@ func GetArticle(c *gin.Context) { func UpdateArtilce(c *gin.Context) { rsp := RespBase{Msg: "FAIL", Status: 210} type ReqUpdateArticle struct { - ID int64 `json:"id"` + ID int64 `json:"id"` Title string `json:"title"` Content string `json:"content"` Author string `json:"author"` @@ -327,8 +328,6 @@ func UpdateArtilce(c *gin.Context) { c.JSON(200, rsp) }() - - er := c.BindJSON(&req) if nil != er { logs.Error(er.Error()) @@ -357,7 +356,7 @@ func UpdateArtilce(c *gin.Context) { Author: req.Author, ID: req.ID, IsPublic: int32(req.IsPublic), - Version: (docs[0].Version + 1), + Version: (docs[0].Version + 1), }, ) if nil != e { @@ -369,12 +368,10 @@ func UpdateArtilce(c *gin.Context) { } - - func UpdateArtilceTree(c *gin.Context) { rsp := RespBase{Msg: "FAIL", Status: 210} type ReqUpdateArticle struct { - ID int64 `json:"id"` + ID int64 `json:"id"` Title string `json:"title"` Content string `json:"content"` Author string `json:"author"` @@ -408,6 +405,8 @@ func UpdateArtilceTree(c *gin.Context) { rsp.Msg = "不存在该" return } + log.Print(req.Content) + req.Content = strings.ReplaceAll(req.Content, "\\", "\\\\") e = model.UpdateDocTree( model.DocTree{ Type: int32(req.Type), @@ -416,37 +415,38 @@ func UpdateArtilceTree(c *gin.Context) { Author: req.Author, ID: req.ID, IsPublic: int32(req.IsPublic), - Version: (docs[0].Version + 1), - Father: int32(req.Father), - Level: int32(req.Level), + Version: (docs[0].Version + 1), + Father: int32(req.Father), + Level: int32(req.Level), }, ) if nil != e { logs.Error(e.Error()) return } - config.RedisOne().Del("artilcetree") rsp.Msg = "OK" rsp.Status = 0 + config.RedisOne().Del("artilcetree") + } func SearchArticleESHightLight(c *gin.Context) { - type RetHighlight struct{ - ID int64 `json:"id" gorm:"column:id" sql:"id"` - Title string `json:"title" gorm:"column:title" sql:"title"` - Type int32 `json:"type" gorm:"column:type" sql:"type"` - Highlight []string `json:"highlight" gorm:"column:highlight" sql:"highlight"` + type RetHighlight struct { + ID int64 `json:"id" gorm:"column:id" sql:"id"` + Title string `json:"title" gorm:"column:title" sql:"title"` + Type int32 `json:"type" gorm:"column:type" sql:"type"` + Highlight []string `json:"highlight" gorm:"column:highlight" sql:"highlight"` } rsp := RespBase{Msg: "FAIL", Status: 210} defer func() { c.JSON(200, rsp) }() - type Req struct{ + type Req struct { Term string `json:"term"` } var req Req e := c.BindJSON(&req) - if nil != e{ + if nil != e { log.Print(e.Error()) return } @@ -457,7 +457,7 @@ func SearchArticleESHightLight(c *gin.Context) { highlight = highlight.PreTags("").PostTags("") x := []RetHighlight{} _, e = db.GetElastic(). - QueryHighlight("doc", query, &x,highlight, 10, 0) + QueryHighlight("doc", query, &x, highlight, 10, 0) if nil != e { log.Print(e.Error()) return @@ -473,16 +473,16 @@ func SearchArticleES(c *gin.Context) { defer func() { c.JSON(200, rsp) }() - type Req struct{ + type Req struct { Term string `json:"term"` } var req Req e := c.BindJSON(&req) - if nil != e{ + if nil != e { log.Print(e.Error()) return } - + query := elastic.NewTermQuery("content", req.Term) x := []model.Doc{} _, e = db.GetElastic().Query("doc", query, &x, 10, 0) @@ -535,7 +535,6 @@ func AddArticle(c *gin.Context) { rsp.Status = 0 } - func AddArticleTree(c *gin.Context) { rsp := RespBase{Msg: "FAIL", Status: 210} type ReqAddArticle struct { @@ -544,7 +543,7 @@ func AddArticleTree(c *gin.Context) { Author string `json:"author"` Type int64 `json:"type"` Ispublic int `json:"is_public"` - Father int `json:"father"` + Father int `json:"father"` Level int `json:"level"` } var req ReqAddArticle @@ -569,17 +568,19 @@ func AddArticleTree(c *gin.Context) { Content: req.Content, Author: req.Author, IsPublic: int32(req.Ispublic), - Father: int32(req.Father), - Level: int32(req.Level), + Father: int32(req.Father), + Level: int32(req.Level), }, ) if nil != e { logs.Error(e.Error()) return } - config.RedisOne().Del("artilcetree") rsp.Msg = "OK" rsp.Status = 0 + rsp.Data = model.GetIdFromTitle(req.Title) + config.RedisOne().Del("artilcetree") + } func ArticlesType(c *gin.Context) { @@ -672,7 +673,6 @@ func DeleteArticle(c *gin.Context) { rsp.Msg = "OK" } - func DeleteArticleTree(c *gin.Context) { rsp := RespBase{Msg: "FAIL", Status: 210} defer func() { @@ -713,8 +713,8 @@ func ArticlesTypes(c *gin.Context) { } func SearchArticles(c *gin.Context) { - type ReqSearch struct{ - Term string `json:"term"` + type ReqSearch struct { + Term string `json:"term"` } resp := RespBase{"unkown error", -231, nil} defer func() { @@ -778,7 +778,6 @@ func CreateBook(c *gin.Context) { resp.Status = 0 } - func BookCount(c *gin.Context) { resp := RespBase{"unkown error", -231, nil} defer func() { @@ -791,10 +790,9 @@ func BookCount(c *gin.Context) { resp.Status = 0 } - func Delbook(c *gin.Context) { - type Req struct{ - ID int32 `json:"id"` + type Req struct { + ID int32 `json:"id"` } var req Req resp := RespBase{"unkown error", -231, nil} @@ -810,7 +808,7 @@ func Delbook(c *gin.Context) { } count := 0 e = db.GetOrm().Model(&model.Book{}).Delete(model.Book{ID: int64(req.ID)}).Error - if nil != e{ + if nil != e { log.Printf(e.Error()) return } @@ -897,17 +895,17 @@ func GetPageBook(c *gin.Context) { } books := []model.Book{} - if req.BookName != ""{ + if req.BookName != "" { e = db.GetOrm().Model(&model.Book{}). - Where(fmt.Sprintf("book_name like '%%%s%%' ",req.BookName)). - Limit(iLmit).Offset(iOffset).Find(&books).Error + Where(fmt.Sprintf("book_name like '%%%s%%' ", req.BookName)). + Limit(iLmit).Offset(iOffset).Find(&books).Error if nil != e { log.Print(e.Error()) return } - }else{ + } else { e = db.GetOrm().Model(&model.Book{}). - Limit(iLmit).Offset(iOffset).Find(&books).Error + Limit(iLmit).Offset(iOffset).Find(&books).Error if nil != e { log.Print(e.Error()) return @@ -1046,7 +1044,6 @@ func GetMemos(c *gin.Context) { } - func GetMemoCnt(c *gin.Context) { rsp := RespBase{"ERR", -1, nil} defer func() { @@ -1060,7 +1057,6 @@ func GetMemoCnt(c *gin.Context) { } - func GetMemo(c *gin.Context) { rsp := RespBase{"ERR", -1, nil} defer func() { @@ -1076,7 +1072,6 @@ func GetMemo(c *gin.Context) { rsp.Data = dat } - func DeleteMemos(c *gin.Context) { resp := RespBase{"unkown error", -231, nil} defer func() { @@ -1101,7 +1096,6 @@ func DeleteMemos(c *gin.Context) { resp.Status = 0 } - func CreateDocTemplate(c *gin.Context) { resp := RespBase{} defer func() { @@ -1115,7 +1109,7 @@ func CreateDocTemplate(c *gin.Context) { return } e = model.CreateDocTemplate(req) - if nil != e{ + if nil != e { log.Print(e.Error()) return } @@ -1124,7 +1118,6 @@ func CreateDocTemplate(c *gin.Context) { resp.Status = 0 } - func UpdateDocTemplate(c *gin.Context) { resp := RespBase{} defer func() { @@ -1147,21 +1140,21 @@ func UpdateDocTemplate(c *gin.Context) { } func GetDocTemplate(c *gin.Context) { - type ReqGet struct{ - Title string `json:"title"` + type ReqGet struct { + Title string `json:"title"` } req := ReqGet{} resp := RespBase{} - defer func() { + defer func() { c.JSON(200, resp) }() e := c.BindJSON(&req) - if nil != e{ + if nil != e { log.Print(e.Error()) return } - ret,e := model.GetDocTemplate(req.Title,5,0) - if nil != e{ + ret, e := model.GetDocTemplate(req.Title, 5, 0) + if nil != e { log.Print(e.Error()) return } @@ -1176,13 +1169,13 @@ func DeleteDocTemplate(c *gin.Context) { c.JSON(200, resp) }() sid := c.Param("id") - id,e := strconv.Atoi(sid) - if nil != e{ + id, e := strconv.Atoi(sid) + if nil != e { log.Print(e.Error()) return } e = model.DeleteDocTemplate(int32(id)) - if nil != e{ + if nil != e { log.Print(e.Error()) return } @@ -1197,21 +1190,21 @@ func GetDocTemplateId(c *gin.Context) { c.JSON(200, resp) }() sid := c.Param("id") - id,e := strconv.Atoi(sid) - if nil != e{ + id, e := strconv.Atoi(sid) + if nil != e { log.Print(e.Error()) return } - ret,e := model.ReadDocTemplate(int32(id)) - if nil != e{ + ret, e := model.ReadDocTemplate(int32(id)) + if nil != e { log.Print(e.Error()) return } - if len(ret) > 0{ + if len(ret) > 0 { resp.Data = ret[0] - }else{ + } else { resp.Data = nil } resp.Msg = "OK" resp.Status = 0 -} \ No newline at end of file +} diff --git a/db/elasticEngine.go b/db/elasticEngine.go index f6be2ac..d57fa79 100644 --- a/db/elasticEngine.go +++ b/db/elasticEngine.go @@ -60,8 +60,7 @@ func (p *ElkEngine) Delete(query elastic.Query, index string) error { return nil } - -func (p *ElkEngine) QueryHighlight(index string, query elastic.Query, v interface{},hightlight *elastic.Highlight, +func (p *ElkEngine) QueryHighlight(index string, query elastic.Query, v interface{}, hightlight *elastic.Highlight, limit int, offset int) ([]string, error) { if reflect.ValueOf(v).Kind() != reflect.Ptr { @@ -186,7 +185,7 @@ func (p *ElkEngine) Query(index string, query elastic.Query, v interface{}, } else { res, err := p.cli.Search(index).Query(query).Size(limit).From(limit * offset).Do(context.Background()) if err != nil { - print(err) + log.Print(err.Error()) return nil, err } id := []string{} diff --git a/main.go b/main.go index 72d04f9..a76dd63 100644 --- a/main.go +++ b/main.go @@ -50,7 +50,7 @@ func CORSMiddleware(c *gin.Context) { c.Writer.Header().Set("Access-Control-Expose-Headers", "Content-Length") c.Writer.Header().Set("Access-Control-Allow-Credentials", "true") - log.Print("remote addr is ",c.Request.RemoteAddr) + log.Print("remote addr is ", c.Request.RemoteAddr) if c.Request.Method == "OPTIONS" { log.Println("OPTIONS") @@ -128,6 +128,7 @@ func main() { if nil != e { } }() + go func() { // programatically set swagger info @@ -157,13 +158,13 @@ func main() { /** 修改密码**/ api.POST("modify_pass", middle.AuthMiddle, userController.ModifyPasswd) - api.GET("/article/:id", controller.GetArticle) //获取文章 - api.POST("/articles", controller.GetArticles) // 获取所有文章 - api.GET("/articles_tree", controller.GetArticlesTree) // 获取所有文章 + 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_tree", controller.AddArticleTree) // 添加文章 - api.PUT("/article_tree_history", controller.AddArticleHistory) // 添加文章 + api.PUT("/article", controller.AddArticle) // 添加文章 + api.PUT("/article_tree", controller.AddArticleTree) // 添加文章 + api.PUT("/article_tree_history", controller.AddArticleHistory) // 添加文章 api.PUT("/article_search", controller.SearchArticles) // 添加文章 api.GET("/article_type", controller.ArticlesType) //获取所有文章分类 @@ -172,13 +173,13 @@ func main() { api.POST("/doc_search_term", controller.SearchArticleES) // 文章内容搜索,基于es的倒排 api.POST("/doc_match_search_hightlight", controller.SearchArticleESHightLight) // 文章内容搜索,基于es的倒排 - api.POST("/article_update", controller.UpdateArtilce) //更新文章 - api.POST("/article_update_tree", controller.UpdateArtilceTree) //更新文章 + api.POST("/article_update", controller.UpdateArtilce) //更新文章 + api.POST("/article_update_tree", controller.UpdateArtilceTree) //更新文章 - api.GET("/articleCount", controller.GetArticleCount) //获取所有文章个数 - api.DELETE("/article/:id", controller.DeleteArticle) ////删除文章 - api.DELETE("/article_tree/:id", controller.DeleteArticleTree) ////删除文章 - api.GET("/article_history",controller.GetArticleHistory) // 历史 + api.GET("/articleCount", controller.GetArticleCount) //获取所有文章个数 + api.DELETE("/article/:id", controller.DeleteArticle) ////删除文章 + api.DELETE("/article_tree/:id", controller.DeleteArticleTree) ////删除文章 + api.GET("/article_history", controller.GetArticleHistory) // 历史 api.POST("/image_upload", fileController.OnUpload) // 上传图片,如果图片太大自动裁减为原来的一半 api.POST("/image_upload_origin", fileController.OnUploadOrigin) // 上传图片 @@ -235,6 +236,7 @@ func main() { api.DELETE("/doc_template/:id", controller.DeleteDocTemplate) // 删除文章模板 api.GET("/doc_template/:id", controller.GetDocTemplateId) //获得单个文章模板信息 } + openapi := r.Group("openapi") { openapi.POST("/diff") // 比较两个文档差异 diff --git a/model/blog.go b/model/blog.go index a015426..447f0d9 100644 --- a/model/blog.go +++ b/model/blog.go @@ -12,59 +12,59 @@ import ( ) type Doc struct { - ID int64 `json:"id" gorm:"column:id" sql:"id"` - Title string `json:"title" gorm:"column:title" sql:"title"` - Type int32 `json:"type" gorm:"column:type" sql:"type"` - Content string `json:"content" gorm:"column:content" sql:"content"` - Author string `json:"author" gorm:"column:author" sql:"author"` - CreateTime time.Time `json:"create_time" gorm:"column:create_time" sql:"create_time"` - UpdateTime time.Time `json:"update_time" gorm:"column:update_time" sql:"update_time"` - DeleteTime time.Time `json:"delete_time" gorm:"column:delete_time" sql:"delete_time"` - Version int32 `json:"version" gorm:"column:version" sql:"version"` - IsPublic int32 `json:"is_public" gorm:"column:is_public" sql:"is_public"` - Deleted int32 `json:"deleted" gorm:"column:deleted" sql:"deleted"` - OriginUrl string `json:"origin_url" gorm:"column:origin_url" sql:"origin_url"` - EsId string `json:"es_id" gorm:"column:es_id" sql:"es_id"` - DeletedAt time.Time `json:"deleted_at" gorm:"column:deleted_at" sql:"deleted_at"` - Father int32 `json:"father" gorm:"column:father" sql:"father"` - Level int32 `json:"level" gorm:"column:level" sql:"level"` + ID int64 `json:"id" gorm:"column:id" sql:"id"` + Title string `json:"title" gorm:"column:title" sql:"title"` + Type int32 `json:"type" gorm:"column:type" sql:"type"` + Content string `json:"content" gorm:"column:content" sql:"content"` + Author string `json:"author" gorm:"column:author" sql:"author"` + CreateTime time.Time `json:"create_time" gorm:"column:create_time" sql:"create_time"` + UpdateTime time.Time `json:"update_time" gorm:"column:update_time" sql:"update_time"` + DeleteTime time.Time `json:"delete_time" gorm:"column:delete_time" sql:"delete_time"` + Version int32 `json:"version" gorm:"column:version" sql:"version"` + IsPublic int32 `json:"is_public" gorm:"column:is_public" sql:"is_public"` + Deleted int32 `json:"deleted" gorm:"column:deleted" sql:"deleted"` + OriginUrl string `json:"origin_url" gorm:"column:origin_url" sql:"origin_url"` + EsId string `json:"es_id" gorm:"column:es_id" sql:"es_id"` + DeletedAt time.Time `json:"deleted_at" gorm:"column:deleted_at" sql:"deleted_at"` + Father int32 `json:"father" gorm:"column:father" sql:"father"` + Level int32 `json:"level" gorm:"column:level" sql:"level"` } - type DocTree struct { - ID int64 `json:"id" gorm:"column:id" sql:"id"` - Title string `json:"title" gorm:"column:title" sql:"title"` - Type int32 `json:"type" gorm:"column:type" sql:"type"` - Content string `json:"content" gorm:"column:content" sql:"content"` - Author string `json:"author" gorm:"column:author" sql:"author"` - CreateTime time.Time `json:"create_time" gorm:"column:create_time" sql:"create_time"` - UpdateTime time.Time `json:"update_time" gorm:"column:update_time" sql:"update_time"` - DeleteTime time.Time `json:"delete_time" gorm:"column:delete_time" sql:"delete_time"` - Version int32 `json:"version" gorm:"column:version" sql:"version"` - IsPublic int32 `json:"is_public" gorm:"column:is_public" sql:"is_public"` - Deleted int32 `json:"deleted" gorm:"column:deleted" sql:"deleted"` - OriginUrl string `json:"origin_url" gorm:"column:origin_url" sql:"origin_url"` - EsId string `json:"es_id" gorm:"column:es_id" sql:"es_id"` - DeletedAt time.Time `json:"deleted_at" gorm:"column:deleted_at" sql:"deleted_at"` - Father int32 `json:"father" gorm:"column:father" sql:"father"` - Level int32 `json:"level" gorm:"column:level" sql:"level"` + ID int64 `json:"id" gorm:"column:id" sql:"id"` + Title string `json:"title" gorm:"column:title" sql:"title"` + Type int32 `json:"type" gorm:"column:type" sql:"type"` + Content string `json:"content" gorm:"column:content" sql:"content"` + Author string `json:"author" gorm:"column:author" sql:"author"` + CreateTime time.Time `json:"create_time" gorm:"column:create_time" sql:"create_time"` + UpdateTime time.Time `json:"update_time" gorm:"column:update_time" sql:"update_time"` + DeleteTime time.Time `json:"delete_time" gorm:"column:delete_time" sql:"delete_time"` + Version int32 `json:"version" gorm:"column:version" sql:"version"` + IsPublic int32 `json:"is_public" gorm:"column:is_public" sql:"is_public"` + Deleted int32 `json:"deleted" gorm:"column:deleted" sql:"deleted"` + OriginUrl string `json:"origin_url" gorm:"column:origin_url" sql:"origin_url"` + EsId string `json:"es_id" gorm:"column:es_id" sql:"es_id"` + DeletedAt time.Time `json:"deleted_at" gorm:"column:deleted_at" sql:"deleted_at"` + Father int32 `json:"father" gorm:"column:father" sql:"father"` + Level int32 `json:"level" gorm:"column:level" sql:"level"` } + // 设置User的表名为`profiles` func (DocTree) TableName() string { return "doc_copy1" } -type DocType struct{ - Id int32 `json:"id" gorm:"column:id" sql:"id"` - TypeName string `json:"type_name" gorm:"column:type_name" sql:"type_name"` - Group int32 `json:"group" gorm:"column:group" sql:"group"` +type DocType struct { + Id int32 `json:"id" gorm:"column:id" sql:"id"` + TypeName string `json:"type_name" gorm:"column:type_name" sql:"type_name"` + Group int32 `json:"group" gorm:"column:group" sql:"group"` } -type DocHistory struct{ - ID int32 `json:"id" gorm:"column:id" sql:"id"` - Title string `json:"title" gorm:"column:title" sql:"title"` - DocId int32 `json:"doc_id" gorm:"column:doc_id" sql:"doc_id"` - Content string `json:"content" gorm:"column:content" sql:"content"` - EditTime string `json:"edit_time" gorm:"column:edit_time" sql:"edit_time"` +type DocHistory struct { + ID int32 `json:"id" gorm:"column:id" sql:"id"` + Title string `json:"title" gorm:"column:title" sql:"title"` + DocId int32 `json:"doc_id" gorm:"column:doc_id" sql:"doc_id"` + Content string `json:"content" gorm:"column:content" sql:"content"` + EditTime string `json:"edit_time" gorm:"column:edit_time" sql:"edit_time"` } type DocGroup struct { @@ -125,8 +125,6 @@ func CreateDoc(doc Doc) error { return nil } - - /* CreateDoc 新建文档 */ @@ -145,7 +143,7 @@ func CreateDocTree(doc DocTree) error { DUAL WHERE NOT EXISTS ( SELECT * FROM doc_copy1 WHERE doc_copy1.title = '%s' );`, doc.Title, strings.Replace(doc.Content, "'", "\\'", -1), - doc.Author, doc.Type, doc.IsPublic, time.Now().Format("2006-01-02 15:04:05"), doc.Father,doc.Level,doc.Title) + doc.Author, doc.Type, doc.IsPublic, time.Now().Format("2006-01-02 15:04:05"), doc.Father, doc.Level, doc.Title) _, e := db.GetMysqlClient().Query(sql) if nil != e { log.Print(sql) @@ -155,13 +153,27 @@ func CreateDocTree(doc DocTree) error { return nil } +func GetIdFromTitle(title string) int { + + motors := []DocTree{} + db.GetOrm().LogMode(true) + db.GetOrm().Model(&DocTree{}).Find(&motors, fmt.Sprintf("title = '%s'", title)) + if len(motors) == 0 { + return -1 + } + db.GetOrm().LogMode(false) + + return int(motors[0].ID) + +} + /* UpdateDoc 更新文档 */ func UpdateDoc(doc Doc) error { sql := fmt.Sprintf(`update doc set doc.author = '%s' ,doc.title = '%s',doc.type = '%d',doc.content = '%s' ,doc.update_time = '%s' ,doc.version = '%d' where doc.id = '%d'; `, doc.Author, doc.Title, doc.Type, - strings.Replace(doc.Content, "'", "\\'", -1), time.Now().Format("2006-01-02 15:04:05"),doc.Version, doc.ID) + strings.Replace(doc.Content, "'", "\\'", -1), time.Now().Format("2006-01-02 15:04:05"), doc.Version, doc.ID) _, e := db.GetMysqlClient().Query(sql) if nil != e { logs.Error(e.Error()) @@ -177,11 +189,10 @@ func UpdateDoc(doc Doc) error { return nil } - // 添加文件版本。 -func InsertDocHistory(doc DocTree) error{ +func InsertDocHistory(doc DocTree) error { sql := fmt.Sprintf(`insert into doc_history(title,doc_id,content,edit_time) values('%s',%d,'%s','%s');`, - doc.Title,doc.ID,strings.Replace(doc.Content, "'", "\\'", -1), time.Now().Format("2006-01-02 15:04:05")) + doc.Title, doc.ID, strings.Replace(doc.Content, "'", "\\'", -1), time.Now().Format("2006-01-02 15:04:05")) _, er := db.GetMysqlClient().Query(sql) if nil != er { logs.Error(er.Error()) @@ -198,7 +209,7 @@ func UpdateDocTree(doc DocTree) error { doc_copy1.type = '%d',doc_copy1.content = '%s' ,doc_copy1.update_time = '%s' , doc_copy1.version = '%d' where doc_copy1.id = '%d'; `, doc.Author, doc.Title, doc.Type, - strings.Replace(doc.Content, "'", "\\'", -1), time.Now().Format("2006-01-02 15:04:05"),doc.Version, doc.ID) + strings.Replace(doc.Content, "'", "\\'", -1), time.Now().Format("2006-01-02 15:04:05"), doc.Version, doc.ID) _, e := db.GetMysqlClient().Query(sql) if nil != e { logs.Error(e.Error()) @@ -220,8 +231,6 @@ func DeleteDoc(id int64) error { return nil } - - /* DeleteDoc 删除文档 */ @@ -276,9 +285,9 @@ func DeleteArticleType(id int32) error { return nil } -func GetAllDocs() ([]Doc, error) { - ret := []Doc{} - sql := fmt.Sprintf("select * from doc") +func GetAllDocs() ([]DocTree, error) { + ret := []DocTree{} + sql := fmt.Sprintf("select * from doc_copy1") e := db.GetMysqlClient().Query2(sql, &ret) if nil != e { logs.Error(e.Error()) @@ -287,7 +296,6 @@ func GetAllDocs() ([]Doc, error) { return ret, nil } - func GetAllGroup() ([]DocGroup, error) { ret := []DocGroup{} sql := fmt.Sprintf("select * from doc_group") diff --git a/model/port.go b/model/port.go index d7de6cb..fa5e409 100644 --- a/model/port.go +++ b/model/port.go @@ -35,6 +35,7 @@ func MysqlToElasticSearchMapping(types string, Key string) string { } return "" } + /* "settings":{ "number_of_shards":1, @@ -76,11 +77,11 @@ func QueryDocument(query elastic.Query, limit int, offset int) ([]Hardware, erro } return ret, nil } -func InsertDocToElaticSearch(doc Doc) error { +func InsertDocToElaticSearch(doc DocTree) error { matchPhraseQuery := elastic.NewMatchQuery("title", doc.Title) existedHardware, e := QueryHardwares(matchPhraseQuery, 10, 0) log.Print(e, existedHardware) - + for _, v := range existedHardware { if v.Name == doc.Title { log.Print(v.ID) @@ -97,7 +98,7 @@ func InsertDocToElaticSearch(doc Doc) error { func CreateIndexFromMysqlTable(tblname string) error { columns := []Field{} - e := db.GetMysqlClient().Query2("describe " + tblname, &columns) + e := db.GetMysqlClient().Query2("describe "+tblname, &columns) if nil != e { logger.Debug(e.Error()) return e @@ -155,7 +156,8 @@ func PortDocumentToElasticsearch(tblname string) error { "analysis": map[string]interface{}{ "analyzer": map[string]interface{}{ "default": map[string]interface{}{ - "type": "smartcn", + "type": "ik_smart", + // "type": "smartcn", }, }, }, @@ -172,6 +174,7 @@ func PortDocumentToElasticsearch(tblname string) error { if nil != e { log.Print(e.Error()) } + log.Print(string(dat)) e = db.GetElastic().CreateIndex(tblname, string(dat)) if nil != e { log.Print(e.Error()) diff --git a/test/goroutine_test.go b/test/goroutine_test.go index 6e0918d..d908e5a 100644 --- a/test/goroutine_test.go +++ b/test/goroutine_test.go @@ -15,17 +15,16 @@ import ( "time" ) - func TestWaitGroup(t *testing.T) { var wg sync.WaitGroup wg.Add(2) - go func () { - time.Sleep(3*time.Second) + go func() { + time.Sleep(3 * time.Second) t.Logf("job 1 done") wg.Done() }() - go func () { + go func() { time.Sleep(2 * time.Second) t.Logf("job 2 done") wg.Done() @@ -33,106 +32,103 @@ func TestWaitGroup(t *testing.T) { wg.Wait() t.Log("all finish") - } -func TestChanel(t *testing.T){ +func TestChanel(t *testing.T) { exit := make(chan bool) - go func () { + go func() { for { select { case <-exit: t.Logf("exit 1") return - case <- time.After(1 * time.Second) : + case <-time.After(1 * time.Second): t.Logf("time out ") } - } + } }() - go func () { + go func() { for { select { case <-exit: t.Logf("exit 2") return - case <- time.After(1 * time.Second) : + case <-time.After(1 * time.Second): t.Logf("time out ") } - } + } }() time.Sleep(5 * time.Second) exit <- true t.Logf("finish") - - + } -type TestInherit struct{ +type TestInherit struct { context.Context x int32 - } -func TestTimeoutContext(t * testing.T){ - ctx,_ := context.WithTimeout(context.Background(), time.Second * 1) - aa := TestInherit{ctx,1} - go func (aa *TestInherit) { +func TestTimeoutContext(t *testing.T) { + ctx, _ := context.WithTimeout(context.Background(), time.Second*1) + aa := TestInherit{ctx, 1} + + go func(aa *TestInherit) { for { select { case <-aa.Done(): t.Logf("exit") return - case <- time.After(5 * time.Second) : + case <-time.After(5 * time.Second): t.Logf("time out ") } - } + } }(&aa) - + time.Sleep(10 * time.Second) } -func TestContext(t *testing.T){ - ctx,cancel := context.WithCancel(context.Background()) +func TestContext(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) - go func () { + go func() { for { select { case <-ctx.Done(): t.Logf("exit") return - case <- time.After(1 * time.Second) : + case <-time.After(1 * time.Second): t.Logf("time out ") } - } + } }() - - go func () { + + go func() { for { select { case <-ctx.Done(): t.Logf("exit") return - case <- time.After(1 * time.Second) : + case <-time.After(1 * time.Second): t.Logf("time out ") } - } + } }() - - go func () { + + go func() { for { select { case <-ctx.Done(): t.Logf("exit") return - case <- time.After(1 * time.Second) : + case <-time.After(1 * time.Second): t.Logf("time out ") } - } + } }() time.Sleep(5 * time.Second) cancel() - t.Logf("finish") -} \ No newline at end of file +} diff --git a/test/portData_test.go b/test/portData_test.go index f7ed711..e02898f 100644 --- a/test/portData_test.go +++ b/test/portData_test.go @@ -45,9 +45,9 @@ func InitElasticSearch() { } func InitLogs() { - logs.Init(config.GetLogConfig().Dir, - config.GetLogConfig().File, - config.GetLogConfig().Level, + logs.Init(config.GetLogConfig().Dir, + config.GetLogConfig().File, + config.GetLogConfig().Level, config.GetLogConfig().SaveFile) } @@ -59,7 +59,19 @@ func TestPortDocToElastic(t *testing.T) { InitRedisConfig() InitMysql() db.InitELK() - e := model.PortDocumentToElasticsearch("doc") + e := model.PortDocumentToElasticsearch("doc_copy1") + if nil != e { + log.Print(e.Error()) + } +} + +func TestPortDocTreeToElastic(t *testing.T) { + InitConfig() + InitLogs() + InitRedisConfig() + InitMysql() + db.InitELK() + e := model.PortDocumentToElasticsearch("doc_copy1") if nil != e { log.Print(e.Error()) } @@ -129,7 +141,6 @@ func TestChangeStructFieldThroughStruct(t *testing.T) { fmt.Println(cap(arr1)) } - func TestPortDocVersion(t *testing.T) { InitConfig() InitLogs() @@ -139,9 +150,9 @@ func TestPortDocVersion(t *testing.T) { docs := []model.Doc{} 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{ + if nil != e { log.Print(e.Error()) } // for k,v := range doc_groups{ @@ -149,7 +160,7 @@ func TestPortDocVersion(t *testing.T) { // e := db.GetMysqlClient().Query2("select * from doc_group",&doc_groups) // 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")) @@ -183,37 +194,127 @@ func TestPortDocVersion(t *testing.T) { // _,e = db.GetMysqlClient().Query(str) // if nil != e{ // log.Print(e.Error()) - // } - + // } + // } - - e = db.GetMysqlClient().Query2("select id,title,type from doc_copy1 where doc_copy1.father is NULL ",&docs) - if(nil != e){ + 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()) } - for k,v := range docs{ - log.Print(k,v) + for k, v := range docs { + log.Print(k, v) - e = db.GetMysqlClient().Query2(fmt.Sprintf("select * from doc_type where doc_type.id = %d ",v.Type),&doc_types) - if nil != e{ + e = db.GetMysqlClient().Query2(fmt.Sprintf("select * from doc_type where doc_type.id = %d ", v.Type), &doc_types) + if nil != e { log.Print(e.Error()) } log.Print(doc_types) - if len(doc_types) > 0{ + if len(doc_types) > 0 { docsfortype := []model.Doc{} 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{ + doc_types[0].TypeName), &docsfortype) + if nil != e { log.Print(e.Error()) } - log.Print(docsfortype) - if len(docsfortype) > 0{ + log.Print(docsfortype) + if len(docsfortype) > 0 { 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)) + docsfortype[0].ID, docsfortype[0].Level+1, (v.ID)) log.Print(str) - _,e = db.GetMysqlClient().Query(str) - if nil != e{ + _, e = db.GetMysqlClient().Query(str) + if nil != e { + log.Printf(e.Error()) + } + } + } + } + +} + +func TestPortDocTree(t *testing.T) { + InitConfig() + InitLogs() + InitRedisConfig() + InitMysql() + + docs := []model.Doc{} + doc_groups := []model.DocGroup{} + doc_types := []model.DocType{} + e := db.GetMysqlClient().Query2("select * from doc_group", &doc_groups) + + 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) + // 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){ + // log.Print(e.Error()) + // } + // for k,v := range doc_types{ + // log.Print(k,v) + // doc_group := []model.DocGroup{} + // e = db.GetMysqlClient().Query2(fmt.Sprintf("select * from doc_group where doc_group.int = '%d'",v.Group),&doc_group) + // if nil != e{ + // log.Print(e.Error()) + // } + // log.Print(doc_group[0]) + // doc := []model.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_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_copy1 where doc_copy1.father is NULL ", &docs) + if nil != e { + log.Print(e.Error()) + } + for k, v := range docs { + log.Print(k, v) + + e = db.GetMysqlClient().Query2(fmt.Sprintf("select * from doc_type where doc_type.id = %d ", v.Type), &doc_types) + if nil != e { + log.Print(e.Error()) + } + log.Print(doc_types) + if len(doc_types) > 0 { + docsfortype := []model.Doc{} + 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_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()) } }