main.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. package main
  2. import (
  3. "eta_gn/eta_api/controllers"
  4. _ "eta_gn/eta_api/global"
  5. "eta_gn/eta_api/models"
  6. "eta_gn/eta_api/models/knowledge"
  7. _ "eta_gn/eta_api/routers"
  8. "eta_gn/eta_api/services/alarm_msg"
  9. knowledgeSrv "eta_gn/eta_api/services/knowledge"
  10. "eta_gn/eta_api/utils"
  11. "fmt"
  12. "runtime"
  13. "sort"
  14. "time"
  15. "github.com/beego/beego/v2/adapter/logs"
  16. "github.com/beego/beego/v2/server/web"
  17. "github.com/beego/beego/v2/server/web/context"
  18. )
  19. func main() {
  20. if web.BConfig.RunMode == "dev" {
  21. web.BConfig.WebConfig.DirectoryIndex = true
  22. web.BConfig.WebConfig.StaticDir["/swagger"] = "swagger"
  23. }
  24. MigrateKnowledgeResource()
  25. // go services.Task()
  26. // 初始化表数据入库
  27. models.AfterInitTable()
  28. // 异常处理
  29. web.ErrorController(&controllers.ErrorController{})
  30. // 内存调整
  31. web.BConfig.MaxMemory = 1024 * 1024 * 128
  32. web.BConfig.RecoverFunc = Recover
  33. web.Run()
  34. }
  35. func Recover(ctx *context.Context, conf *web.Config) {
  36. if err := recover(); err != nil {
  37. if err == web.ErrAbort {
  38. return
  39. }
  40. if !web.BConfig.RecoverPanic {
  41. panic(err)
  42. }
  43. stack := ""
  44. msg := fmt.Sprintf("The request url is %v", ctx.Input.URL())
  45. stack += msg + "</br>"
  46. logs.Critical(msg)
  47. msg = fmt.Sprintf("The request data is %v", string(ctx.Input.RequestBody))
  48. stack += msg + "</br>"
  49. logs.Critical(msg)
  50. msg = fmt.Sprintf("Handler crashed with error %v", err)
  51. stack += msg + "</br>"
  52. logs.Critical(msg)
  53. for i := 1; ; i++ {
  54. _, file, line, ok := runtime.Caller(i)
  55. if !ok {
  56. break
  57. }
  58. logs.Critical(fmt.Sprintf("%s:%d", file, line))
  59. stack = stack + fmt.Sprintln(fmt.Sprintf("%s:%d</br>", file, line))
  60. }
  61. go alarm_msg.SendAlarmMsg(utils.APPNAME+"崩了"+time.Now().Format("2006-01-02 15:04:05")+"<br/>"+stack, 3)
  62. }
  63. return
  64. }
  65. func MigrateKnowledgeResource() {
  66. var condition string
  67. var pars []interface{}
  68. condition = " AND resource_type IN (?)"
  69. // pars = []interface{}{[]int{knowledge.KnowledgeResourceTypeOpinion, knowledge.KnowledgeResourceTypeReport}}
  70. pars = []interface{}{[]int{knowledge.KnowledgeResourceTypeReport}}
  71. condition += " AND is_file = ?"
  72. pars = append(pars, 0)
  73. // 分类映射
  74. classifyObj := new(knowledge.KnowledgeClassify)
  75. classifyList, err := classifyObj.GetAllClassify(knowledge.KnowledgeResourceTypeReport)
  76. if err != nil {
  77. fmt.Println("GetAllClassify err:", err)
  78. return
  79. }
  80. classifyMap := make(map[int]*knowledge.KnowledgeClassify)
  81. for _, item := range classifyList {
  82. classifyMap[item.ClassifyId] = item
  83. }
  84. sort.Slice(classifyList, func(i, j int) bool {
  85. if classifyList[i].Level != classifyList[j].Level {
  86. return classifyList[i].Level < classifyList[j].Level
  87. }
  88. return classifyList[i].ClassifyId < classifyList[j].ClassifyId
  89. })
  90. classifyPathMap := make(map[int]map[int]int)
  91. for _, classify := range classifyList {
  92. if _, ok := classifyPathMap[classify.ClassifyId]; !ok {
  93. classifyPathMap[classify.ClassifyId] = make(map[int]int)
  94. }
  95. currClassifyMap := classifyPathMap[classify.ClassifyId]
  96. currClassify := classify
  97. for i := classify.Level; i > 0; i-- {
  98. currClassifyMap[currClassify.Level] = currClassify.ClassifyId
  99. if currClassify.ParentId == 0 {
  100. break
  101. }
  102. currClassify = classifyMap[currClassify.ParentId]
  103. }
  104. }
  105. // 标签映射
  106. tagObj := new(knowledge.KnowledgeTag)
  107. tagList, err := tagObj.GetAllTag(knowledge.KnowledgeResourceTypeReport)
  108. if err != nil {
  109. fmt.Println("GetAllTag err:", err)
  110. return
  111. }
  112. tagMap := make(map[int]*knowledge.KnowledgeTag)
  113. for _, item := range tagList {
  114. tagMap[item.TagId] = item
  115. }
  116. oldClassifyToNewClassifyMap := make(map[int]int)
  117. oldTagToNewTagMap := make(map[int]int)
  118. for {
  119. list, err := knowledge.GetBatchKnowledgeResourceByCondition(condition, pars, 100)
  120. if err != nil {
  121. fmt.Println("GetBatchKnowledgeResourceByCondition err:", err)
  122. continue
  123. }
  124. if len(list) == 0 {
  125. break
  126. }
  127. for _, item := range list {
  128. var parentId int
  129. oldClassifyId := item.ClassifyId
  130. newClassifyId, classifyOk := oldClassifyToNewClassifyMap[oldClassifyId]
  131. if classifyOk {
  132. parentId = newClassifyId
  133. } else {
  134. levelMap := classifyPathMap[item.ClassifyId]
  135. fmt.Println("--------", classifyMap[item.ClassifyId])
  136. if classifyMap[item.ClassifyId] == nil {
  137. continue
  138. }
  139. for i := 1; i <= 3; i++ {
  140. if classifyId, ok := levelMap[i]; ok {
  141. tmp := classifyMap[classifyId]
  142. tmp.ClassifyId = 0
  143. tmp.ParentId = parentId
  144. tmp.CreateTime = time.Now()
  145. tmp.ModifyTime = time.Now()
  146. tmp.ResourceType = knowledge.KnowledgeResourceTypeOpinion
  147. err = tmp.Add(tmp)
  148. if err != nil {
  149. fmt.Println("Add classify err:", err)
  150. continue
  151. }
  152. parentId = tmp.ClassifyId
  153. }
  154. }
  155. }
  156. var tmpTagId, oldTagId int
  157. newTagId, tagOk := oldTagToNewTagMap[item.TagId]
  158. if tagOk {
  159. tmpTagId = newTagId
  160. } else {
  161. if item.TagId > 0 {
  162. oldTagId = item.TagId
  163. tmpTag := tagMap[item.TagId]
  164. tmpTag.TagId = 0
  165. tmpTag.CreateTime = time.Now()
  166. tmpTag.ModifyTime = time.Now()
  167. tmpTag.ResourceType = knowledge.KnowledgeResourceTypeOpinion
  168. err = tmpTag.Add(tmpTag)
  169. if err != nil {
  170. fmt.Println("Add tag err:", err)
  171. continue
  172. }
  173. tmpTagId = tmpTag.TagId
  174. }
  175. }
  176. item.State = knowledge.KnowledgeResourceStateUnpublished
  177. item.TagId = tmpTagId
  178. item.ClassifyId = parentId
  179. item.ResourceType = knowledge.KnowledgeResourceTypeOpinion
  180. err = item.Update([]string{"state", "classify_id", "tag_id", "resource_type"})
  181. if err != nil {
  182. fmt.Println("Update classify_id err:", err)
  183. continue
  184. }
  185. delete(classifyPathMap, oldClassifyId)
  186. oldClassifyToNewClassifyMap[oldClassifyId] = item.ClassifyId
  187. oldTagToNewTagMap[oldTagId] = tmpTagId
  188. }
  189. esErr := knowledgeSrv.EsBatchAddOrEditKnowledgeResource(list)
  190. if esErr != nil {
  191. fmt.Println("EsBatchAddOrEditKnowledgeResource err:", esErr)
  192. }
  193. condition += " AND knowledge_resource_id > ?"
  194. pars = append(pars, list[len(list)-1].KnowledgeResourceId)
  195. }
  196. }