Bladeren bron

fix:修复知识资源历史数据

zqbao 2 maanden geleden
bovenliggende
commit
7a911d20b0
2 gewijzigde bestanden met toevoegingen van 224 en 88 verwijderingen
  1. 147 6
      main.go
  2. 77 82
      services/task.go

+ 147 - 6
main.go

@@ -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)
+
+	}
+}

+ 77 - 82
services/task.go

@@ -1,25 +1,20 @@
 package services
 
 import (
-	"eta_gn/eta_api/models"
-	"eta_gn/eta_api/services/data"
-	"eta_gn/eta_api/utils"
 	"fmt"
-	"strconv"
-	"strings"
 )
 
 func Task() {
 	fmt.Println("task start")
 
 	//手工数据表格导入后的指标库刷新
-	go ImportManualDataRefresh()
+	// go ImportManualDataRefresh()
 
-	// 指标刷新
-	go data.HandleEdbRefreshQueue()
+	// // 指标刷新
+	// go data.HandleEdbRefreshQueue()
 
-	// 进行指标替换操作
-	go DealReplaceEdbCache()
+	// // 进行指标替换操作
+	// go DealReplaceEdbCache()
 
 	// 修复分类LevelPath
 	//FixClassifyLevelPath()
@@ -27,81 +22,81 @@ func Task() {
 	fmt.Println("task end")
 }
 
-// ImportManualDataRefresh 导入手工数据后的刷新
-func ImportManualDataRefresh() {
-	defer func() {
-		if err := recover(); err != nil {
-			fmt.Println("[ImportManualDataRefresh]", err)
-		}
-	}()
-	for {
-		utils.Rc.Brpop(utils.CACHE_IMPORT_MANUAL_DATA, func(b []byte) {
-			edbCode := string(b)
-			edbCode = strings.TrimPrefix(edbCode, `"`)
-			edbCode = strings.TrimSuffix(edbCode, `"`)
+// // ImportManualDataRefresh 导入手工数据后的刷新
+// func ImportManualDataRefresh() {
+// 	defer func() {
+// 		if err := recover(); err != nil {
+// 			fmt.Println("[ImportManualDataRefresh]", err)
+// 		}
+// 	}()
+// 	for {
+// 		utils.Rc.Brpop(utils.CACHE_IMPORT_MANUAL_DATA, func(b []byte) {
+// 			edbCode := string(b)
+// 			edbCode = strings.TrimPrefix(edbCode, `"`)
+// 			edbCode = strings.TrimSuffix(edbCode, `"`)
 
-			data.RefreshManualData(edbCode)
-		})
-	}
-}
+// 			data.RefreshManualData(edbCode)
+// 		})
+// 	}
+// }
 
-func FixClassifyLevelPath() {
-	var err error
-	defer func() {
-		if err != nil {
-			fmt.Println(err)
-		}
-	}()
-	classifyOb := new(models.Classify)
-	classifies, e := classifyOb.GetItemsByCondition("", []interface{}{}, []string{}, "")
-	if e != nil {
-		err = fmt.Errorf("获取分类列表失败, %v", e)
-		return
-	}
-	fmt.Println("开始修复")
+// func FixClassifyLevelPath() {
+// 	var err error
+// 	defer func() {
+// 		if err != nil {
+// 			fmt.Println(err)
+// 		}
+// 	}()
+// 	classifyOb := new(models.Classify)
+// 	classifies, e := classifyOb.GetItemsByCondition("", []interface{}{}, []string{}, "")
+// 	if e != nil {
+// 		err = fmt.Errorf("获取分类列表失败, %v", e)
+// 		return
+// 	}
+// 	fmt.Println("开始修复")
 
-	// 先更新所有一级分类
-	for _, v := range classifies {
-		if v.ParentId > 0 {
-			continue
-		}
-		v.LevelPath = strconv.Itoa(v.Id)
-		if e = v.UpdateClassify([]string{"LevelPath"}); e != nil {
-			err = fmt.Errorf("更新LevelPath失败, ID: %d", v.Id)
-			return
-		}
-	}
+// 	// 先更新所有一级分类
+// 	for _, v := range classifies {
+// 		if v.ParentId > 0 {
+// 			continue
+// 		}
+// 		v.LevelPath = strconv.Itoa(v.Id)
+// 		if e = v.UpdateClassify([]string{"LevelPath"}); e != nil {
+// 			err = fmt.Errorf("更新LevelPath失败, ID: %d", v.Id)
+// 			return
+// 		}
+// 	}
 
-	// 再更新二级
-	parentMap := make(map[int]string)
-	for _, v := range classifies {
-		if v.Level != 2 {
-			continue
-		}
-		v.LevelPath = fmt.Sprintf("%d,%d", v.ParentId, v.Id)
-		if e = v.UpdateClassify([]string{"LevelPath"}); e != nil {
-			err = fmt.Errorf("更新二级LevelPath失败, ID: %d", v.Id)
-			return
-		}
-		parentMap[v.Id] = v.LevelPath
-	}
+// 	// 再更新二级
+// 	parentMap := make(map[int]string)
+// 	for _, v := range classifies {
+// 		if v.Level != 2 {
+// 			continue
+// 		}
+// 		v.LevelPath = fmt.Sprintf("%d,%d", v.ParentId, v.Id)
+// 		if e = v.UpdateClassify([]string{"LevelPath"}); e != nil {
+// 			err = fmt.Errorf("更新二级LevelPath失败, ID: %d", v.Id)
+// 			return
+// 		}
+// 		parentMap[v.Id] = v.LevelPath
+// 	}
 
-	// 再更新三级,没四级了
-	for _, v := range classifies {
-		if v.Level != 3 {
-			continue
-		}
-		str := parentMap[v.ParentId]
-		if str == "" {
-			err = fmt.Errorf("二级LevelPath为空, ID: %d", v.ParentId)
-			return
-		}
+// 	// 再更新三级,没四级了
+// 	for _, v := range classifies {
+// 		if v.Level != 3 {
+// 			continue
+// 		}
+// 		str := parentMap[v.ParentId]
+// 		if str == "" {
+// 			err = fmt.Errorf("二级LevelPath为空, ID: %d", v.ParentId)
+// 			return
+// 		}
 
-		v.LevelPath = fmt.Sprintf("%s,%d", str, v.Id)
-		if e = v.UpdateClassify([]string{"LevelPath"}); e != nil {
-			err = fmt.Errorf("更新三级LevelPath失败, ID: %d", v.Id)
-			return
-		}
-	}
-	fmt.Println("修复成功")
-}
+// 		v.LevelPath = fmt.Sprintf("%s,%d", str, v.Id)
+// 		if e = v.UpdateClassify([]string{"LevelPath"}); e != nil {
+// 			err = fmt.Errorf("更新三级LevelPath失败, ID: %d", v.Id)
+// 			return
+// 		}
+// 	}
+// 	fmt.Println("修复成功")
+// }