task.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package scheduler
  2. import (
  3. "context"
  4. "eta/eta_mini_bridge/models"
  5. "eta/eta_mini_bridge/utils"
  6. "fmt"
  7. "strconv"
  8. "strings"
  9. "github.com/beego/beego/v2/task"
  10. )
  11. func InitJob() {
  12. fmt.Println("定时任务开启。。。")
  13. // 每天凌晨12点10分检测, 发送消息
  14. tk1 := task.NewTask("UpdateReportClassify", "0 */1 * * * *", UpdateReportClassify)
  15. task.AddTask("UpdateReportClassify", tk1)
  16. task.StartTask()
  17. }
  18. func UpdateReportClassify(ctx context.Context) (err error) {
  19. reportPdfClassifys, err := models.GetReportPdfClasssify()
  20. if err != nil {
  21. return
  22. }
  23. classifyList, err := models.GetClassifyList()
  24. if err != nil {
  25. return
  26. }
  27. updateReportOriginClassifyName(reportPdfClassifys, classifyList)
  28. classifyIdFirsts := make([]int, 0)
  29. classifyIdSeconds := make([]int, 0)
  30. for _, v := range reportPdfClassifys {
  31. if v.ClassifyIdFirst == 0 {
  32. utils.FileLog.Info("分类id错误,获取失败,错误pdf研报id:" + fmt.Sprintf("%d", v.ReportPdfId))
  33. continue
  34. }
  35. if v.ClassifyIdThird == 0 && v.ClassifyIdSecond == 0 {
  36. classifyIdFirsts = append(classifyIdFirsts, v.ClassifyIdFirst)
  37. } else if v.ClassifyIdThird == 0 {
  38. classifyIdSeconds = append(classifyIdSeconds, v.ClassifyIdSecond)
  39. }
  40. }
  41. updateReportClassify(reportPdfClassifys, classifyIdFirsts, 1)
  42. updateReportClassify(reportPdfClassifys, classifyIdSeconds, 2)
  43. return nil
  44. }
  45. // updateReportOriginClassifyName 更新原始的pdf报告的分类
  46. func updateReportOriginClassifyName(reportList []*models.ReportPdf, classifyList []*models.ClassifyView) (err error) {
  47. classifyMap := make(map[int]*models.ClassifyView)
  48. for _, v := range classifyList {
  49. classifyMap[v.Id] = v
  50. }
  51. modifyReportPdfList := make([]*models.ReportPdf, 0)
  52. var isModify bool
  53. for _, v := range reportList {
  54. isModify = false
  55. if v.ClassifyIdFirst != 0 {
  56. firstName := classifyMap[v.ClassifyIdFirst].ClassifyName
  57. if v.ClassifyNameFirst != firstName {
  58. v.ClassifyNameFirst = firstName
  59. isModify = true
  60. }
  61. }
  62. if v.ClassifyIdSecond != 0 {
  63. secondName := classifyMap[v.ClassifyIdSecond].ClassifyName
  64. if v.ClassifyNameSecond != secondName {
  65. v.ClassifyNameSecond = secondName
  66. isModify = true
  67. }
  68. }
  69. if v.ClassifyIdThird != 0 {
  70. thirdName := classifyMap[v.ClassifyIdThird].ClassifyName
  71. if v.ClassifyNameThird != thirdName {
  72. v.ClassifyNameThird = thirdName
  73. isModify = true
  74. }
  75. }
  76. if isModify {
  77. modifyReportPdfList = append(modifyReportPdfList, v)
  78. }
  79. }
  80. err = models.MultiUpdateReportPdfClassify(modifyReportPdfList)
  81. if err != nil {
  82. utils.FileLog.Warn("研报更新分类失败")
  83. }
  84. if len(modifyReportPdfList) > 0 {
  85. var reportIds []string
  86. for _, v := range modifyReportPdfList {
  87. reportIds = append(reportIds, strconv.Itoa(v.ReportPdfId))
  88. }
  89. utils.FileLog.Info(fmt.Sprintf("更新分类成功,更新研报为:%s", strings.Join(reportIds, ",")))
  90. }
  91. return
  92. }
  93. // updateReportClassify 更新增加分类pdf报告
  94. func updateReportClassify(reportList []*models.ReportPdf, classifyList []int, level int) (err error) {
  95. modifyReportList := make([]*models.ReportPdf, 0)
  96. preClassifyList, err := models.GetClassifyListByIds(classifyList)
  97. if err != nil {
  98. utils.FileLog.Info("获取一级分类失败,错误分类id:" + fmt.Sprintf("%v", classifyList))
  99. }
  100. if len(preClassifyList) > 0 {
  101. tmpClassifyIds := make([]int, 0)
  102. for _, v := range preClassifyList {
  103. if v.HasChild == 1 {
  104. tmpClassifyIds = append(tmpClassifyIds, v.Id)
  105. }
  106. }
  107. childClassify, err := models.GetChildClassifyListByIds(tmpClassifyIds)
  108. if err != nil {
  109. utils.FileLog.Info("获取二级分类失败,错误分类id:" + fmt.Sprintf("%v", tmpClassifyIds))
  110. }
  111. var isModify bool
  112. for _, v := range reportList {
  113. isModify = false
  114. for _, vv := range childClassify {
  115. switch level {
  116. case 1:
  117. if v.ClassifyIdFirst == vv.ParentId {
  118. v.ClassifyIdSecond = vv.Id
  119. v.ClassifyNameSecond = vv.ClassifyName
  120. isModify = true
  121. }
  122. case 2:
  123. if v.ClassifyIdSecond == vv.ParentId {
  124. v.ClassifyIdThird = vv.Id
  125. v.ClassifyNameThird = vv.ClassifyName
  126. isModify = true
  127. }
  128. }
  129. }
  130. if isModify {
  131. modifyReportList = append(modifyReportList, v)
  132. }
  133. }
  134. err = models.MultiUpdateReportPdfClassify(modifyReportList)
  135. if err != nil {
  136. utils.FileLog.Warn("研报更新分类失败")
  137. }
  138. if len(modifyReportList) > 0 {
  139. var reportIds []string
  140. for _, v := range modifyReportList {
  141. reportIds = append(reportIds, strconv.Itoa(v.ReportPdfId))
  142. }
  143. utils.FileLog.Info(fmt.Sprintf("更新分类成功,更新研报为:%s", strings.Join(reportIds, ",")))
  144. }
  145. }
  146. return
  147. }