task.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package services
  2. import (
  3. "eta_gn/eta_api/models"
  4. "eta_gn/eta_api/services/data"
  5. "eta_gn/eta_api/utils"
  6. "fmt"
  7. "strconv"
  8. "strings"
  9. )
  10. func Task() {
  11. fmt.Println("task start")
  12. //手工数据表格导入后的指标库刷新
  13. go ImportManualDataRefresh()
  14. // 指标刷新
  15. go data.HandleEdbRefreshQueue()
  16. // 进行指标替换操作
  17. go DealReplaceEdbCache()
  18. // 修复分类LevelPath
  19. //FixClassifyLevelPath()
  20. fmt.Println("task end")
  21. }
  22. // ImportManualDataRefresh 导入手工数据后的刷新
  23. func ImportManualDataRefresh() {
  24. defer func() {
  25. if err := recover(); err != nil {
  26. fmt.Println("[ImportManualDataRefresh]", err)
  27. }
  28. }()
  29. for {
  30. utils.Rc.Brpop(utils.CACHE_IMPORT_MANUAL_DATA, func(b []byte) {
  31. edbCode := string(b)
  32. edbCode = strings.TrimPrefix(edbCode, `"`)
  33. edbCode = strings.TrimSuffix(edbCode, `"`)
  34. data.RefreshManualData(edbCode)
  35. })
  36. }
  37. }
  38. func FixClassifyLevelPath() {
  39. var err error
  40. defer func() {
  41. if err != nil {
  42. fmt.Println(err)
  43. }
  44. }()
  45. classifyOb := new(models.Classify)
  46. classifies, e := classifyOb.GetItemsByCondition("", []interface{}{}, []string{}, "")
  47. if e != nil {
  48. err = fmt.Errorf("获取分类列表失败, %v", e)
  49. return
  50. }
  51. fmt.Println("开始修复")
  52. // 先更新所有一级分类
  53. for _, v := range classifies {
  54. if v.ParentId > 0 {
  55. continue
  56. }
  57. v.LevelPath = strconv.Itoa(v.Id)
  58. if e = v.UpdateClassify([]string{"LevelPath"}); e != nil {
  59. err = fmt.Errorf("更新LevelPath失败, ID: %d", v.Id)
  60. return
  61. }
  62. }
  63. // 再更新二级
  64. parentMap := make(map[int]string)
  65. for _, v := range classifies {
  66. if v.Level != 2 {
  67. continue
  68. }
  69. v.LevelPath = fmt.Sprintf("%d,%d", v.ParentId, v.Id)
  70. if e = v.UpdateClassify([]string{"LevelPath"}); e != nil {
  71. err = fmt.Errorf("更新二级LevelPath失败, ID: %d", v.Id)
  72. return
  73. }
  74. parentMap[v.Id] = v.LevelPath
  75. }
  76. // 再更新三级,没四级了
  77. for _, v := range classifies {
  78. if v.Level != 3 {
  79. continue
  80. }
  81. str := parentMap[v.ParentId]
  82. if str == "" {
  83. err = fmt.Errorf("二级LevelPath为空, ID: %d", v.ParentId)
  84. return
  85. }
  86. v.LevelPath = fmt.Sprintf("%s,%d", str, v.Id)
  87. if e = v.UpdateClassify([]string{"LevelPath"}); e != nil {
  88. err = fmt.Errorf("更新三级LevelPath失败, ID: %d", v.Id)
  89. return
  90. }
  91. }
  92. fmt.Println("修复成功")
  93. }