| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- package TimeTask
- import (
- "ColdVerify_server/lib"
- "ColdVerify_server/lib/wx"
- "ColdVerify_server/logs"
- "ColdVerify_server/models/Account"
- "ColdVerify_server/models/AllotTask"
- "ColdVerify_server/models/InfoCollection"
- "ColdVerify_server/models/Task"
- "fmt"
- "time"
- "github.com/robfig/cron/v3"
- )
- func TaskList() {
- crontab := cron.New(cron.WithSeconds())
- // 现有的任务回款检查 - 每天上午10点执行
- ss := "0 10 * * * *"
- _, err := crontab.AddFunc(ss, TaskList_return)
- if err != nil {
- fmt.Printf("TaskList_return cron err: %v\n", err)
- }
- // 新增的AllotTask超时检查 - 每30分钟执行一次
- allotTaskCron := "0 */30 * * * *" // 每30分钟执行一次
- _, err2 := crontab.AddFunc(allotTaskCron, AllotTask_Timeout_Check)
- if err2 != nil {
- fmt.Printf("AllotTask_Timeout_Check cron err: %v\n", err2)
- }
- crontab.Start()
- defer crontab.Stop()
- select {}
- }
- // 回款
- func TaskList_return() {
- logs.Println("------- 任务回款 定时任务 --------")
- List, _ := Task.Read_Task_List_All()
- for _, T := range List {
- logs.Println("任务ID:", T.Id)
- var InfoCollection_r InfoCollection.InfoCollection //信息采集
- if len(T.T_InfoCollection_id) == 0 {
- logs.Println("信息采集表获取 为空!!")
- continue
- }
- var is bool
- InfoCollection_r, is = InfoCollection.Read_InfoCollection(T.T_InfoCollection_id)
- if !is {
- logs.Println("信息采集表获取错误 T_InfoCollection_id ", T.T_InfoCollection_id)
- continue
- }
- if InfoCollection_r.T_State == 5 {
- logs.Println("已经回款")
- continue
- }
- if len(T.T_reporting_end_time) == 0 {
- logs.Println("正在进行中")
- continue
- }
- T_collection_time_interval, _ := lib.MinutesDifference(T.T_reporting_end_time, time.Now().Format("2006-01-02 15:04:05"))
- T_collection_time_interval = T_collection_time_interval / 60 / 24
- logs.Println("用时 天:", T_collection_time_interval)
- if T_collection_time_interval <= 30 {
- logs.Println("未超时!!!")
- continue
- }
- _, company_r := Account.Read_User_ByT_uuid(T.T_uuid)
- //AdminMap := Account.AdminListToMap(Account.Read_Admin_List_ALL_1())
- //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), "")
- go wx.WxSend(T.T_scheme, fmt.Sprintf("【项目回款】项目回款已经超过%d天【%s-%s】", int(T_collection_time_interval), company_r.T_name, T.T_name))
- }
- }
- // AllotTask超时检查 - 每30分钟执行一次
- func AllotTask_Timeout_Check() {
- logs.Println("------- AllotTask超时检查 定时任务 --------")
- // 获取超时的AllotTask列表
- timeoutTasks, err := AllotTask.Read_AllotTask_Timeout_List()
- if err != nil {
- logs.Error("AllotTask_Timeout_Check 获取超时任务失败:", err)
- return
- }
- if len(timeoutTasks) == 0 {
- logs.Println("AllotTask_Timeout_Check: 没有超时的任务")
- return
- }
- logs.Println(fmt.Sprintf("AllotTask_Timeout_Check: 找到 %d 个超过24小时未接收的任务", len(timeoutTasks)))
- // 处理每个超时任务
- for _, task := range timeoutTasks {
- logs.Println(fmt.Sprintf("处理超时任务 ID: %s, 名称: %s, 创建时间: %s",
- task.T_allot_task_id, task.T_name, task.CreateTime.Format("2006-01-02 15:04:05")))
- // 更新状态为已拒绝并添加拒绝记录
- reason := fmt.Sprintf("任务派发超过24小时未接收,系统自动拒绝 (创建时间: %s)",
- task.CreateTime.Format("2006-01-02 15:04:05"))
- if AllotTask.Update_AllotTask_To_Refused(task, reason) {
- logs.Println(fmt.Sprintf("成功将任务 %s 更新为已拒绝状态", task.T_allot_task_id))
- // 发送企业微信通知给实施方案负责人
- if len(task.T_scheme) > 0 {
- _, company_r := Account.Read_User_ByT_uuid(task.T_uuid)
- notificationMsg := fmt.Sprintf("【任务超时拒绝】任务「%s-%s」已超过24小时未接收,系统已自动拒绝",
- company_r.T_name, task.T_name)
- go wx.WxSend(task.T_scheme, notificationMsg)
- logs.Println(fmt.Sprintf("已发送企业微信通知给负责人: %s", task.T_scheme))
- }
- } else {
- logs.Error(fmt.Sprintf("更新任务 %s 状态失败", task.T_allot_task_id))
- }
- }
- logs.Println("------- AllotTask超时检查 定时任务完成 --------")
- }
|