123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- package scheduler
- import (
- "context"
- "eta/eta_mini_bridge/models"
- "eta/eta_mini_bridge/utils"
- "fmt"
- "strconv"
- "strings"
- "github.com/beego/beego/v2/task"
- )
- func InitJob() {
- fmt.Println("定时任务开启。。。")
- // 每天凌晨12点10分检测, 发送消息
- tk1 := task.NewTask("UpdateReportClassify", "0 */1 * * * *", UpdateReportClassify)
- task.AddTask("UpdateReportClassify", tk1)
- task.StartTask()
- }
- func UpdateReportClassify(ctx context.Context) (err error) {
- reportPdfClassifys, err := models.GetReportPdfClasssify()
- if err != nil {
- return
- }
- classifyList, err := models.GetClassifyList()
- if err != nil {
- return
- }
- updateReportOriginClassifyName(reportPdfClassifys, classifyList)
- classifyIdFirsts := make([]int, 0)
- classifyIdSeconds := make([]int, 0)
- for _, v := range reportPdfClassifys {
- if v.ClassifyIdFirst == 0 {
- utils.FileLog.Info("分类id错误,获取失败,错误pdf研报id:" + fmt.Sprintf("%d", v.ReportPdfId))
- continue
- }
- if v.ClassifyIdThird == 0 && v.ClassifyIdSecond == 0 {
- classifyIdFirsts = append(classifyIdFirsts, v.ClassifyIdFirst)
- } else if v.ClassifyIdThird == 0 {
- classifyIdSeconds = append(classifyIdSeconds, v.ClassifyIdSecond)
- }
- }
- updateReportClassify(reportPdfClassifys, classifyIdFirsts, 1)
- updateReportClassify(reportPdfClassifys, classifyIdSeconds, 2)
- return nil
- }
- // updateReportOriginClassifyName 更新原始的pdf报告的分类
- func updateReportOriginClassifyName(reportList []*models.ReportPdf, classifyList []*models.ClassifyView) (err error) {
- classifyMap := make(map[int]*models.ClassifyView)
- for _, v := range classifyList {
- classifyMap[v.Id] = v
- }
- modifyReportPdfList := make([]*models.ReportPdf, 0)
- var isModify bool
- for _, v := range reportList {
- isModify = false
- if v.ClassifyIdFirst != 0 {
- firstName := classifyMap[v.ClassifyIdFirst].ClassifyName
- if v.ClassifyNameFirst != firstName {
- v.ClassifyNameFirst = firstName
- isModify = true
- }
- }
- if v.ClassifyIdSecond != 0 {
- secondName := classifyMap[v.ClassifyIdSecond].ClassifyName
- if v.ClassifyNameSecond != secondName {
- v.ClassifyNameSecond = secondName
- isModify = true
- }
- }
- if v.ClassifyIdThird != 0 {
- thirdName := classifyMap[v.ClassifyIdThird].ClassifyName
- if v.ClassifyNameThird != thirdName {
- v.ClassifyNameThird = thirdName
- isModify = true
- }
- }
- if isModify {
- modifyReportPdfList = append(modifyReportPdfList, v)
- }
- }
- err = models.MultiUpdateReportPdfClassify(modifyReportPdfList)
- if err != nil {
- utils.FileLog.Warn("研报更新分类失败")
- }
- if len(modifyReportPdfList) > 0 {
- var reportIds []string
- for _, v := range modifyReportPdfList {
- reportIds = append(reportIds, strconv.Itoa(v.ReportPdfId))
- }
- utils.FileLog.Info(fmt.Sprintf("更新分类成功,更新研报为:%s", strings.Join(reportIds, ",")))
- }
- return
- }
- // updateReportClassify 更新增加分类pdf报告
- func updateReportClassify(reportList []*models.ReportPdf, classifyList []int, level int) (err error) {
- modifyReportList := make([]*models.ReportPdf, 0)
- preClassifyList, err := models.GetClassifyListByIds(classifyList)
- if err != nil {
- utils.FileLog.Info("获取一级分类失败,错误分类id:" + fmt.Sprintf("%v", classifyList))
- }
- if len(preClassifyList) > 0 {
- tmpClassifyIds := make([]int, 0)
- for _, v := range preClassifyList {
- if v.HasChild == 1 {
- tmpClassifyIds = append(tmpClassifyIds, v.Id)
- }
- }
- childClassify, err := models.GetChildClassifyListByIds(tmpClassifyIds)
- if err != nil {
- utils.FileLog.Info("获取二级分类失败,错误分类id:" + fmt.Sprintf("%v", tmpClassifyIds))
- }
- var isModify bool
- for _, v := range reportList {
- isModify = false
- for _, vv := range childClassify {
- switch level {
- case 1:
- if v.ClassifyIdFirst == vv.ParentId {
- v.ClassifyIdSecond = vv.Id
- v.ClassifyNameSecond = vv.ClassifyName
- isModify = true
- }
- case 2:
- if v.ClassifyIdSecond == vv.ParentId {
- v.ClassifyIdThird = vv.Id
- v.ClassifyNameThird = vv.ClassifyName
- isModify = true
- }
- }
- }
- if isModify {
- modifyReportList = append(modifyReportList, v)
- }
- }
- err = models.MultiUpdateReportPdfClassify(modifyReportList)
- if err != nil {
- utils.FileLog.Warn("研报更新分类失败")
- }
- if len(modifyReportList) > 0 {
- var reportIds []string
- for _, v := range modifyReportList {
- reportIds = append(reportIds, strconv.Itoa(v.ReportPdfId))
- }
- utils.FileLog.Info(fmt.Sprintf("更新分类成功,更新研报为:%s", strings.Join(reportIds, ",")))
- }
- }
- return
- }
|