123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- 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"
- "github.com/beego/beego/v2/adapter/logs"
- "github.com/beego/beego/v2/server/web"
- "github.com/beego/beego/v2/server/web/context"
- )
- func main() {
- if web.BConfig.RunMode == "dev" {
- web.BConfig.WebConfig.DirectoryIndex = true
- web.BConfig.WebConfig.StaticDir["/swagger"] = "swagger"
- }
- MigrateKnowledgeResource()
- // go services.Task()
- // 初始化表数据入库
- models.AfterInitTable()
- // 异常处理
- web.ErrorController(&controllers.ErrorController{})
- // 内存调整
- web.BConfig.MaxMemory = 1024 * 1024 * 128
- web.BConfig.RecoverFunc = Recover
- web.Run()
- }
- func Recover(ctx *context.Context, conf *web.Config) {
- if err := recover(); err != nil {
- if err == web.ErrAbort {
- return
- }
- if !web.BConfig.RecoverPanic {
- panic(err)
- }
- stack := ""
- msg := fmt.Sprintf("The request url is %v", ctx.Input.URL())
- stack += msg + "</br>"
- logs.Critical(msg)
- msg = fmt.Sprintf("The request data is %v", string(ctx.Input.RequestBody))
- stack += msg + "</br>"
- logs.Critical(msg)
- msg = fmt.Sprintf("Handler crashed with error %v", err)
- stack += msg + "</br>"
- logs.Critical(msg)
- for i := 1; ; i++ {
- _, file, line, ok := runtime.Caller(i)
- if !ok {
- break
- }
- logs.Critical(fmt.Sprintf("%s:%d", file, line))
- stack = stack + fmt.Sprintln(fmt.Sprintf("%s:%d</br>", file, line))
- }
- go alarm_msg.SendAlarmMsg(utils.APPNAME+"崩了"+time.Now().Format("2006-01-02 15:04:05")+"<br/>"+stack, 3)
- }
- 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)
- }
- }
|