auto_task_execute.go 5.1 KB

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