TaskList.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package TimeTask
  2. import (
  3. "ColdVerify_server/lib"
  4. "ColdVerify_server/lib/wx"
  5. "ColdVerify_server/logs"
  6. "ColdVerify_server/models/Account"
  7. "ColdVerify_server/models/AllotTask"
  8. "ColdVerify_server/models/InfoCollection"
  9. "ColdVerify_server/models/Task"
  10. "fmt"
  11. "time"
  12. "github.com/robfig/cron/v3"
  13. )
  14. func TaskList() {
  15. crontab := cron.New(cron.WithSeconds())
  16. // 现有的任务回款检查 - 每天上午10点执行
  17. ss := "0 10 * * * *"
  18. _, err := crontab.AddFunc(ss, TaskList_return)
  19. if err != nil {
  20. fmt.Printf("TaskList_return cron err: %v\n", err)
  21. }
  22. // 新增的AllotTask超时检查 - 每30分钟执行一次
  23. allotTaskCron := "0 */30 * * * *" // 每30分钟执行一次
  24. _, err2 := crontab.AddFunc(allotTaskCron, AllotTask_Timeout_Check)
  25. if err2 != nil {
  26. fmt.Printf("AllotTask_Timeout_Check cron err: %v\n", err2)
  27. }
  28. crontab.Start()
  29. defer crontab.Stop()
  30. select {}
  31. }
  32. // 回款
  33. func TaskList_return() {
  34. logs.Println("------- 任务回款 定时任务 --------")
  35. List, _ := Task.Read_Task_List_All()
  36. for _, T := range List {
  37. logs.Println("任务ID:", T.Id)
  38. var InfoCollection_r InfoCollection.InfoCollection //信息采集
  39. if len(T.T_InfoCollection_id) == 0 {
  40. logs.Println("信息采集表获取 为空!!")
  41. continue
  42. }
  43. var is bool
  44. InfoCollection_r, is = InfoCollection.Read_InfoCollection(T.T_InfoCollection_id)
  45. if !is {
  46. logs.Println("信息采集表获取错误 T_InfoCollection_id ", T.T_InfoCollection_id)
  47. continue
  48. }
  49. if InfoCollection_r.T_State == 5 {
  50. logs.Println("已经回款")
  51. continue
  52. }
  53. if len(T.T_reporting_end_time) == 0 {
  54. logs.Println("正在进行中")
  55. continue
  56. }
  57. T_collection_time_interval, _ := lib.MinutesDifference(T.T_reporting_end_time, time.Now().Format("2006-01-02 15:04:05"))
  58. T_collection_time_interval = T_collection_time_interval / 60 / 24
  59. logs.Println("用时 天:", T_collection_time_interval)
  60. if T_collection_time_interval <= 30 {
  61. logs.Println("未超时!!!")
  62. continue
  63. }
  64. _, company_r := Account.Read_User_ByT_uuid(T.T_uuid)
  65. //AdminMap := Account.AdminListToMap(Account.Read_Admin_List_ALL_1())
  66. //go System.Send_Weichat_News(AdminMap[T.T_scheme], fmt.Sprintf("【项目回款】项目回款已经超过%d天【%s-%s】", int(T_collection_time_interval) ,company_r.T_name, T.T_name), "")
  67. go wx.WxSend(T.T_scheme, fmt.Sprintf("【项目回款】项目回款已经超过%d天【%s-%s】", int(T_collection_time_interval), company_r.T_name, T.T_name))
  68. }
  69. }
  70. // AllotTask超时检查 - 每30分钟执行一次
  71. func AllotTask_Timeout_Check() {
  72. logs.Println("------- AllotTask超时检查 定时任务 --------")
  73. // 获取超时的AllotTask列表
  74. timeoutTasks, err := AllotTask.Read_AllotTask_Timeout_List()
  75. if err != nil {
  76. logs.Error("AllotTask_Timeout_Check 获取超时任务失败:", err)
  77. return
  78. }
  79. if len(timeoutTasks) == 0 {
  80. logs.Println("AllotTask_Timeout_Check: 没有超时的任务")
  81. return
  82. }
  83. logs.Println(fmt.Sprintf("AllotTask_Timeout_Check: 找到 %d 个超过24小时未接收的任务", len(timeoutTasks)))
  84. // 处理每个超时任务
  85. for _, task := range timeoutTasks {
  86. logs.Println(fmt.Sprintf("处理超时任务 ID: %s, 名称: %s, 创建时间: %s",
  87. task.T_allot_task_id, task.T_name, task.CreateTime.Format("2006-01-02 15:04:05")))
  88. // 更新状态为已拒绝并添加拒绝记录
  89. reason := fmt.Sprintf("任务派发超过24小时未接收,系统自动拒绝 (创建时间: %s)",
  90. task.CreateTime.Format("2006-01-02 15:04:05"))
  91. if AllotTask.Update_AllotTask_To_Refused(task, reason) {
  92. logs.Println(fmt.Sprintf("成功将任务 %s 更新为已拒绝状态", task.T_allot_task_id))
  93. // 发送企业微信通知给实施方案负责人
  94. if len(task.T_scheme) > 0 {
  95. _, company_r := Account.Read_User_ByT_uuid(task.T_uuid)
  96. notificationMsg := fmt.Sprintf("【任务超时拒绝】任务「%s-%s」已超过24小时未接收,系统已自动拒绝",
  97. company_r.T_name, task.T_name)
  98. go wx.WxSend(task.T_scheme, notificationMsg)
  99. logs.Println(fmt.Sprintf("已发送企业微信通知给负责人: %s", task.T_scheme))
  100. }
  101. } else {
  102. logs.Error(fmt.Sprintf("更新任务 %s 状态失败", task.T_allot_task_id))
  103. }
  104. }
  105. logs.Println("------- AllotTask超时检查 定时任务完成 --------")
  106. }