auto_task_execute.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package schedule
  2. import (
  3. "github.com/robfig/cron/v3"
  4. "log"
  5. "time"
  6. "youngee_b_api/app/dao"
  7. "youngee_b_api/app/entity"
  8. )
  9. func AutoTaskExecute() error {
  10. // 新建一个定时任务对象
  11. crontab := cron.New(cron.WithSeconds()) // 精确到秒
  12. spec := "0 */10 * * * ?" //cron表达式,每10分钟一次
  13. // 添加定时任务
  14. // 定时任务1 初稿未审稿自动执行
  15. _, err1 := crontab.AddFunc(spec, AutoSketchExecuteTask)
  16. if err1 != nil {
  17. return err1
  18. }
  19. // 定时任务2 链接未质检自动执行
  20. _, err2 := crontab.AddFunc(spec, AutoLinkExecuteTask)
  21. if err2 != nil {
  22. return err2
  23. }
  24. // 定时任务3 数据未质检自动执行
  25. _, err3 := crontab.AddFunc(spec, AutoDataExecuteTask)
  26. if err3 != nil {
  27. return err3
  28. }
  29. // 启动定时器
  30. crontab.Start()
  31. // 定时任务是另起协程执行的,这里使用 select 简单阻塞.需要根据实际情况进行控制
  32. //select {} //阻塞主线程停止
  33. return nil
  34. }
  35. // 定时任务1 初稿未审稿自动执行
  36. func AutoSketchExecuteTask() {
  37. log.Println("AutoSketchExecuteTask running Start, Time :", time.Now())
  38. var sketchInfos []*entity.SketchInfo
  39. err := dao.Db.Model(&entity.SketchInfo{}).Where("is_review = ? and is_ok = ?", 0, 0).Select("sketch_id, task_id, auto_agree_at, task_type").Find(&sketchInfos).Error
  40. if err != nil {
  41. log.Println(err.Error())
  42. return
  43. }
  44. var sketchIds []int64
  45. var projectTaskIds []string
  46. var localTaskIds []string
  47. for _, sketchInfo := range sketchInfos {
  48. sketchId := sketchInfo.SketchID
  49. if time.Now().After(sketchInfo.AutoAgreeAt) {
  50. sketchIds = append(sketchIds, sketchId)
  51. if sketchInfo.TaskType == 1 {
  52. projectTaskIds = append(projectTaskIds, sketchInfo.TaskID)
  53. } else if sketchInfo.TaskType == 2 {
  54. localTaskIds = append(localTaskIds, sketchInfo.TaskID)
  55. }
  56. }
  57. }
  58. dao.Db.Model(&entity.SketchInfo{}).Where("sketch_id in ?", sketchIds).Updates(&entity.SketchInfo{
  59. IsReview: 1,
  60. IsOk: 1,
  61. AgreeAt: time.Now(),
  62. BOperatorType: 1,
  63. })
  64. dao.Db.Model(&entity.ProjectTaskInfo{}).Where("task_id in ?", projectTaskIds).Updates(&entity.ProjectTaskInfo{
  65. TaskStage: 11,
  66. SketchStatus: 5,
  67. })
  68. dao.Db.Model(&entity.LocalLifeTaskInfo{}).Where("task_id in ?", localTaskIds).Updates(&entity.LocalLifeTaskInfo{
  69. TaskStage: 11,
  70. SketchStatus: 5,
  71. })
  72. log.Println("AutoSketchExecuteTask running End, Time :", time.Now())
  73. }
  74. // 定时任务2 链接未质检自动执行
  75. func AutoLinkExecuteTask() {
  76. log.Println("AutoLinkExecuteTask running Start, Time :", time.Now())
  77. var LinInfos []*entity.LinkInfo
  78. err := dao.Db.Model(&entity.LinkInfo{}).Where("is_review = ? and is_ok = ?", 0, 0).Select("link_id, task_id, auto_agree_at, task_type").Find(&LinInfos).Error
  79. if err != nil {
  80. log.Println(err.Error())
  81. return
  82. }
  83. var linkIds []int64
  84. var projectTaskIds []string
  85. var localTaskIds []string
  86. for _, LinInfo := range LinInfos {
  87. linkId := LinInfo.LinkID
  88. if time.Now().After(LinInfo.AutoAgreeAt) {
  89. linkIds = append(linkIds, linkId)
  90. if LinInfo.TaskType == 1 {
  91. projectTaskIds = append(projectTaskIds, LinInfo.TaskID)
  92. } else if LinInfo.TaskType == 2 {
  93. localTaskIds = append(localTaskIds, LinInfo.TaskID)
  94. }
  95. }
  96. }
  97. dao.Db.Model(&entity.LinkInfo{}).Where("link_id in ?", linkIds).Updates(&entity.LinkInfo{
  98. IsReview: 1,
  99. IsOk: 1,
  100. AgreeAt: time.Now(),
  101. BOperatorType: 1,
  102. })
  103. dao.Db.Model(&entity.ProjectTaskInfo{}).Where("task_id in ?", projectTaskIds).Updates(&entity.ProjectTaskInfo{
  104. TaskStage: 13,
  105. LinkStatus: 5,
  106. })
  107. dao.Db.Model(&entity.LocalLifeTaskInfo{}).Where("task_id in ?", localTaskIds).Updates(&entity.LocalLifeTaskInfo{
  108. TaskStage: 13,
  109. LinkStatus: 5,
  110. })
  111. log.Println("AutoLinkExecuteTask running End, Time :", time.Now())
  112. }
  113. // 定时任务3 数据未质检自动执行
  114. func AutoDataExecuteTask() {
  115. log.Println("AutoDataExecuteTask running Start, Time :", time.Now())
  116. var dataInfos []*entity.DataInfo
  117. err := dao.Db.Model(&entity.DataInfo{}).Where("is_review = ? and is_ok = ?", 0, 0).Select("data_id, task_id, auto_agree_at, task_type").Find(&dataInfos).Error
  118. if err != nil {
  119. log.Println(err.Error())
  120. return
  121. }
  122. var dataIds []int64
  123. var projectTaskIds []string
  124. var localTaskIds []string
  125. for _, dataInfo := range dataInfos {
  126. dataId := dataInfo.DataID
  127. if time.Now().After(dataInfo.AutoAgreeAt) {
  128. dataIds = append(dataIds, dataId)
  129. if dataInfo.TaskType == 1 {
  130. projectTaskIds = append(projectTaskIds, dataInfo.TaskID)
  131. } else if dataInfo.TaskType == 2 {
  132. localTaskIds = append(localTaskIds, dataInfo.TaskID)
  133. }
  134. }
  135. }
  136. dao.Db.Model(&entity.DataInfo{}).Where("data_id in ?", dataIds).Updates(&entity.DataInfo{
  137. IsReview: 1,
  138. IsOk: 1,
  139. AgreeAt: time.Now(),
  140. BOperatorType: 1,
  141. })
  142. dao.Db.Model(&entity.ProjectTaskInfo{}).Where("task_id in ?", projectTaskIds).Updates(&entity.ProjectTaskInfo{
  143. TaskStage: 15,
  144. DataStatus: 5,
  145. })
  146. dao.Db.Model(&entity.LocalLifeTaskInfo{}).Where("task_id in ?", localTaskIds).Updates(&entity.LocalLifeTaskInfo{
  147. TaskStage: 15,
  148. DataStatus: 5,
  149. })
  150. log.Println("AutoDataExecuteTask running End, Time :", time.Now())
  151. }