完成文档树型结构修改,支持无穷级

master
zcy 2022-04-14 14:37:36 +08:00
parent c14a02b503
commit 558b84d052
3 changed files with 228 additions and 2 deletions

View File

@ -93,7 +93,6 @@ func GetArticlesTree(c *gin.Context) {
} }
InitDocCache() InitDocCache()
ret := []*DocTree{} ret := []*DocTree{}
for _, v := range articles { for _, v := range articles {
if v.Level == 0{ if v.Level == 0{
@ -224,7 +223,7 @@ func GetArticle(c *gin.Context) {
return return
} }
} }
query := fmt.Sprintf("select * from doc where doc.id = '%d'", id) query := fmt.Sprintf("select * from doc_copy1 where doc_copy1.id = '%d'", id)
docs := []model.Doc{} docs := []model.Doc{}
e := db.GetMysqlClient().Query2(query, &docs) e := db.GetMysqlClient().Query2(query, &docs)
if nil != e { if nil != e {
@ -296,6 +295,66 @@ func UpdateArtilce(c *gin.Context) {
} }
func UpdateArtilceTree(c *gin.Context) {
rsp := RespBase{Msg: "FAIL", Status: 210}
type ReqUpdateArticle struct {
ID int64 `json:"id"`
Title string `json:"title"`
Content string `json:"content"`
Author string `json:"author"`
Type int64 `json:"type"`
IsPublic int `json:"is_public"`
Father int `json:"father"`
Level int `json:"level"`
}
var req ReqUpdateArticle
defer func() {
c.JSON(200, rsp)
}()
er := c.BindJSON(&req)
if nil != er {
logs.Error(er.Error())
return
}
if req.Title == "" {
rsp.Msg = "title required"
return
}
query := fmt.Sprintf("select * from doc where doc.id = '%d'", req.ID)
docs := []model.Doc{}
e := db.GetMysqlClient().Query2(query, &docs)
if nil != e {
log.Print(e.Error())
return
}
if len(docs) == 0 {
rsp.Msg = "不存在该"
return
}
e = model.UpdateDocTree(
model.DocTree{
Type: int32(req.Type),
Title: req.Title,
Content: req.Content,
Author: req.Author,
ID: req.ID,
IsPublic: int32(req.IsPublic),
Version: (docs[0].Version + 1),
Father: int32(req.Father),
Level: int32(req.Level),
},
)
if nil != e {
logs.Error(e.Error())
return
}
rsp.Msg = "OK"
rsp.Status = 0
}
func SearchArticleESHightLight(c *gin.Context) { func SearchArticleESHightLight(c *gin.Context) {
type RetHighlight struct{ type RetHighlight struct{
ID int64 `json:"id" gorm:"column:id" sql:"id"` ID int64 `json:"id" gorm:"column:id" sql:"id"`
@ -401,6 +460,50 @@ func AddArticle(c *gin.Context) {
rsp.Status = 0 rsp.Status = 0
} }
func AddArticleTree(c *gin.Context) {
rsp := RespBase{Msg: "FAIL", Status: 210}
type ReqAddArticle struct {
Title string `json:"title"`
Content string `json:"content"`
Author string `json:"author"`
Type int64 `json:"type"`
Ispublic int `json:"is_public"`
Father int `json:"father"`
Level int `json:"level"`
}
var req ReqAddArticle
defer func() {
c.JSON(200, rsp)
}()
er := c.BindJSON(&req)
if nil != er {
logs.Error(er.Error())
return
}
if req.Title == "" {
rsp.Msg = "title required"
return
}
e := model.CreateDocTree(
model.DocTree{
Type: int32(req.Type),
Title: req.Title,
Content: req.Content,
Author: req.Author,
IsPublic: int32(req.Ispublic),
Father: int32(req.Father),
Level: int32(req.Level),
},
)
if nil != e {
logs.Error(e.Error())
return
}
rsp.Msg = "OK"
rsp.Status = 0
}
func ArticlesType(c *gin.Context) { func ArticlesType(c *gin.Context) {
rsp := RespBase{Msg: "FAIL", Status: 210} rsp := RespBase{Msg: "FAIL", Status: 210}
defer func() { defer func() {
@ -491,6 +594,28 @@ func DeleteArticle(c *gin.Context) {
rsp.Msg = "OK" rsp.Msg = "OK"
} }
func DeleteArticleTree(c *gin.Context) {
rsp := RespBase{Msg: "FAIL", Status: 210}
defer func() {
c.JSON(200, rsp)
}()
sid := c.Param("id")
id, err := strconv.Atoi(sid)
if nil != err {
rsp.Status = -234
c.JSON(200, rsp)
}
err = model.DeleteDocTree(int64(id))
if nil != err {
rsp.Status = -234
c.JSON(200, rsp)
}
rsp.Data = id
rsp.Status = 0
rsp.Msg = "OK"
}
func ArticlesTypes(c *gin.Context) { func ArticlesTypes(c *gin.Context) {
resp := RespBase{"unkown error", -231, nil} resp := RespBase{"unkown error", -231, nil}
defer func() { defer func() {

View File

@ -162,6 +162,8 @@ func main() {
api.GET("/articles_tree", controller.GetArticlesTree) // 获取所有文章 api.GET("/articles_tree", controller.GetArticlesTree) // 获取所有文章
api.PUT("/article", controller.AddArticle) // 添加文章 api.PUT("/article", controller.AddArticle) // 添加文章
api.PUT("/article_tree", controller.AddArticleTree) // 添加文章
api.PUT("/article_search", controller.SearchArticles) // 添加文章 api.PUT("/article_search", controller.SearchArticles) // 添加文章
api.GET("/article_type", controller.ArticlesType) //获取所有文章分类 api.GET("/article_type", controller.ArticlesType) //获取所有文章分类
api.PUT("/article_type", controller.AddArticleType) // 添加文章分类 api.PUT("/article_type", controller.AddArticleType) // 添加文章分类
@ -170,8 +172,12 @@ func main() {
api.POST("/doc_match_search_hightlight", controller.SearchArticleESHightLight) // 文章内容搜索,基于es的倒排 api.POST("/doc_match_search_hightlight", controller.SearchArticleESHightLight) // 文章内容搜索,基于es的倒排
api.POST("/article_update", controller.UpdateArtilce) //更新文章 api.POST("/article_update", controller.UpdateArtilce) //更新文章
api.POST("/article_update_tree", controller.UpdateArtilceTree) //更新文章
api.GET("/articleCount", controller.GetArticleCount) //获取所有文章个数 api.GET("/articleCount", controller.GetArticleCount) //获取所有文章个数
api.DELETE("/article/:id", controller.DeleteArticle) ////删除文章 api.DELETE("/article/:id", controller.DeleteArticle) ////删除文章
api.DELETE("/article_tree/:id", controller.DeleteArticleTree) ////删除文章
api.POST("/image_upload", fileController.OnUpload) // 上传图片,如果图片太大自动裁减为原来的一半 api.POST("/image_upload", fileController.OnUpload) // 上传图片,如果图片太大自动裁减为原来的一半
api.POST("/image_upload_origin", fileController.OnUploadOrigin) // 上传图片 api.POST("/image_upload_origin", fileController.OnUploadOrigin) // 上传图片
api.GET("/image_download/:file", fileController.OnDownLoad) // 下载图片 api.GET("/image_download/:file", fileController.OnDownLoad) // 下载图片

View File

@ -29,6 +29,31 @@ type Doc struct {
Father int32 `json:"father" gorm:"column:father" sql:"father"` Father int32 `json:"father" gorm:"column:father" sql:"father"`
Level int32 `json:"level" gorm:"column:level" sql:"level"` 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"`
}
// 设置User的表名为`profiles`
func (DocTree) TableName() string {
return "doc_copy1"
}
type DocType struct{ type DocType struct{
Id int32 `json:"id" gorm:"column:id" sql:"id"` Id int32 `json:"id" gorm:"column:id" sql:"id"`
TypeName string `json:"type_name" gorm:"column:type_name" sql:"type_name"` TypeName string `json:"type_name" gorm:"column:type_name" sql:"type_name"`
@ -100,6 +125,36 @@ func CreateDoc(doc Doc) error {
return nil return nil
} }
/*
CreateDoc
*/
func CreateDocTree(doc DocTree) error {
sql := fmt.Sprintf(`INSERT INTO doc_copy1 ( doc_copy1.title, doc_copy1.content,
doc_copy1.author, doc_copy1.type,doc_copy1.is_public,doc_copy1.create_time,father,level) SELECT
'%s',
'%s',
'%s',
%d ,
%d,
'%s',
%d,
%d
FROM
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)
_, e := db.GetMysqlClient().Query(sql)
if nil != e {
log.Print(sql)
logs.Error(e.Error())
return e
}
return nil
}
/* /*
UpdateDoc UpdateDoc
*/ */
@ -122,6 +177,31 @@ func UpdateDoc(doc Doc) error {
return nil return nil
} }
/*
UpdateDoc
*/
func UpdateDocTree(doc DocTree) error {
sql := fmt.Sprintf(`update doc_copy1 set doc_copy1.author = '%s' ,doc_copy1.title = '%s',
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)
_, e := db.GetMysqlClient().Query(sql)
if nil != e {
logs.Error(e.Error())
return e
}
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"))
_, er := db.GetMysqlClient().Query(sql)
if nil != er {
logs.Error(e.Error())
return e
}
return nil
}
/* /*
DeleteDoc DeleteDoc
*/ */
@ -135,6 +215,21 @@ func DeleteDoc(id int64) error {
return nil return nil
} }
/*
DeleteDoc
*/
func DeleteDocTree(id int64) error {
sql := fmt.Sprintf(`delete from doc_copy1 where id = %d`, id)
_, e := db.GetMysqlClient().Query(sql)
if nil != e {
logs.Error(e.Error())
return e
}
return nil
}
/* /*
AddArticleType AddArticleType
*/ */