task.go 2.3 KB

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