|
@@ -2,17 +2,18 @@ package main
|
|
|
|
|
|
import (
|
|
|
"eta_gn/eta_api/controllers"
|
|
|
+ _ "eta_gn/eta_api/global"
|
|
|
"eta_gn/eta_api/models"
|
|
|
+ "eta_gn/eta_api/models/knowledge"
|
|
|
+ _ "eta_gn/eta_api/routers"
|
|
|
"eta_gn/eta_api/services/alarm_msg"
|
|
|
+ knowledgeSrv "eta_gn/eta_api/services/knowledge"
|
|
|
+ "eta_gn/eta_api/utils"
|
|
|
"fmt"
|
|
|
"runtime"
|
|
|
+ "sort"
|
|
|
"time"
|
|
|
|
|
|
- _ "eta_gn/eta_api/global"
|
|
|
- _ "eta_gn/eta_api/routers"
|
|
|
- "eta_gn/eta_api/services"
|
|
|
- "eta_gn/eta_api/utils"
|
|
|
-
|
|
|
"github.com/beego/beego/v2/adapter/logs"
|
|
|
"github.com/beego/beego/v2/server/web"
|
|
|
"github.com/beego/beego/v2/server/web/context"
|
|
@@ -23,7 +24,9 @@ func main() {
|
|
|
web.BConfig.WebConfig.DirectoryIndex = true
|
|
|
web.BConfig.WebConfig.StaticDir["/swagger"] = "swagger"
|
|
|
}
|
|
|
- go services.Task()
|
|
|
+
|
|
|
+ MigrateKnowledgeResource()
|
|
|
+ // go services.Task()
|
|
|
|
|
|
// 初始化表数据入库
|
|
|
models.AfterInitTable()
|
|
@@ -68,3 +71,141 @@ func Recover(ctx *context.Context, conf *web.Config) {
|
|
|
}
|
|
|
return
|
|
|
}
|
|
|
+
|
|
|
+func MigrateKnowledgeResource() {
|
|
|
+ var condition string
|
|
|
+ var pars []interface{}
|
|
|
+ condition = " AND resource_type IN (?)"
|
|
|
+ // pars = []interface{}{[]int{knowledge.KnowledgeResourceTypeOpinion, knowledge.KnowledgeResourceTypeReport}}
|
|
|
+ pars = []interface{}{[]int{knowledge.KnowledgeResourceTypeReport}}
|
|
|
+ condition += " AND is_file = ?"
|
|
|
+ pars = append(pars, 0)
|
|
|
+ // 分类映射
|
|
|
+ classifyObj := new(knowledge.KnowledgeClassify)
|
|
|
+ classifyList, err := classifyObj.GetAllClassify(knowledge.KnowledgeResourceTypeReport)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("GetAllClassify err:", err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ classifyMap := make(map[int]*knowledge.KnowledgeClassify)
|
|
|
+ for _, item := range classifyList {
|
|
|
+ classifyMap[item.ClassifyId] = item
|
|
|
+ }
|
|
|
+ sort.Slice(classifyList, func(i, j int) bool {
|
|
|
+ if classifyList[i].Level != classifyList[j].Level {
|
|
|
+ return classifyList[i].Level < classifyList[j].Level
|
|
|
+ }
|
|
|
+ return classifyList[i].ClassifyId < classifyList[j].ClassifyId
|
|
|
+ })
|
|
|
+ classifyPathMap := make(map[int]map[int]int)
|
|
|
+ for _, classify := range classifyList {
|
|
|
+ if _, ok := classifyPathMap[classify.ClassifyId]; !ok {
|
|
|
+ classifyPathMap[classify.ClassifyId] = make(map[int]int)
|
|
|
+ }
|
|
|
+ currClassifyMap := classifyPathMap[classify.ClassifyId]
|
|
|
+ currClassify := classify
|
|
|
+ for i := classify.Level; i > 0; i-- {
|
|
|
+ currClassifyMap[currClassify.Level] = currClassify.ClassifyId
|
|
|
+ if currClassify.ParentId == 0 {
|
|
|
+ break
|
|
|
+ }
|
|
|
+ currClassify = classifyMap[currClassify.ParentId]
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 标签映射
|
|
|
+ tagObj := new(knowledge.KnowledgeTag)
|
|
|
+ tagList, err := tagObj.GetAllTag(knowledge.KnowledgeResourceTypeReport)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("GetAllTag err:", err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ tagMap := make(map[int]*knowledge.KnowledgeTag)
|
|
|
+ for _, item := range tagList {
|
|
|
+ tagMap[item.TagId] = item
|
|
|
+ }
|
|
|
+
|
|
|
+ oldClassifyToNewClassifyMap := make(map[int]int)
|
|
|
+ oldTagToNewTagMap := make(map[int]int)
|
|
|
+ for {
|
|
|
+ list, err := knowledge.GetBatchKnowledgeResourceByCondition(condition, pars, 100)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("GetBatchKnowledgeResourceByCondition err:", err)
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ if len(list) == 0 {
|
|
|
+ break
|
|
|
+ }
|
|
|
+ for _, item := range list {
|
|
|
+ var parentId int
|
|
|
+ oldClassifyId := item.ClassifyId
|
|
|
+ newClassifyId, classifyOk := oldClassifyToNewClassifyMap[oldClassifyId]
|
|
|
+ if classifyOk {
|
|
|
+ parentId = newClassifyId
|
|
|
+ } else {
|
|
|
+ levelMap := classifyPathMap[item.ClassifyId]
|
|
|
+ fmt.Println("--------", classifyMap[item.ClassifyId])
|
|
|
+ if classifyMap[item.ClassifyId] == nil {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ for i := 1; i <= 3; i++ {
|
|
|
+ if classifyId, ok := levelMap[i]; ok {
|
|
|
+ tmp := classifyMap[classifyId]
|
|
|
+ tmp.ClassifyId = 0
|
|
|
+ tmp.ParentId = parentId
|
|
|
+ tmp.CreateTime = time.Now()
|
|
|
+ tmp.ModifyTime = time.Now()
|
|
|
+ tmp.ResourceType = knowledge.KnowledgeResourceTypeOpinion
|
|
|
+ err = tmp.Add(tmp)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("Add classify err:", err)
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ parentId = tmp.ClassifyId
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ var tmpTagId, oldTagId int
|
|
|
+ newTagId, tagOk := oldTagToNewTagMap[item.TagId]
|
|
|
+ if tagOk {
|
|
|
+ tmpTagId = newTagId
|
|
|
+ } else {
|
|
|
+ if item.TagId > 0 {
|
|
|
+ oldTagId = item.TagId
|
|
|
+ tmpTag := tagMap[item.TagId]
|
|
|
+ tmpTag.TagId = 0
|
|
|
+ tmpTag.CreateTime = time.Now()
|
|
|
+ tmpTag.ModifyTime = time.Now()
|
|
|
+ tmpTag.ResourceType = knowledge.KnowledgeResourceTypeOpinion
|
|
|
+ err = tmpTag.Add(tmpTag)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("Add tag err:", err)
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ tmpTagId = tmpTag.TagId
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ item.State = knowledge.KnowledgeResourceStateUnpublished
|
|
|
+ item.TagId = tmpTagId
|
|
|
+ item.ClassifyId = parentId
|
|
|
+ item.ResourceType = knowledge.KnowledgeResourceTypeOpinion
|
|
|
+ err = item.Update([]string{"state", "classify_id", "tag_id", "resource_type"})
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("Update classify_id err:", err)
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ delete(classifyPathMap, oldClassifyId)
|
|
|
+ oldClassifyToNewClassifyMap[oldClassifyId] = item.ClassifyId
|
|
|
+ oldTagToNewTagMap[oldTagId] = tmpTagId
|
|
|
+ }
|
|
|
+ esErr := knowledgeSrv.EsBatchAddOrEditKnowledgeResource(list)
|
|
|
+ if esErr != nil {
|
|
|
+ fmt.Println("EsBatchAddOrEditKnowledgeResource err:", esErr)
|
|
|
+ }
|
|
|
+ condition += " AND knowledge_resource_id > ?"
|
|
|
+ pars = append(pars, list[len(list)-1].KnowledgeResourceId)
|
|
|
+
|
|
|
+ }
|
|
|
+}
|