package service import ( "XT_New/models" "XT_New/utils" "time" "github.com/robfig/cron" ) // cron表达式 https://www.cnblogs.com/zuxingyu/p/6023919.html var clearSchedulesCronJob *cron.Cron func init() { utils.InfoLog("开启自动清除排班定时任务") clearSchedulesCronJob = cron.New() spec := "0 0 1 * * ?" // 每天凌晨1点清除已排班未上机的排班数据 clearSchedulesCronJob.AddFunc(spec, func() { AutoClearSchedules() }) } func BeginAutoClearSchedulesJob() { clearSchedulesCronJob.Start() } func AutoClearSchedules() { utils.TraceLog("自动清除排班任务开始执行") //查出所有机构 var schedules []*models.Schedule err = readDb.Table("xt_schedule as s").Where("s.status = 1").Select("distinct(s.user_org_id)").Find(&schedules).Error //遍历所有机构 scheduleDate := utils.ZeroHourTimeOfDay(time.Now()) for _, item := range schedules { //将今天(不包括今天)以前的的排班数据进行软删除 writeDb.Table("xt_schedule as s"). Where("s.schedule_date < ? AND s.status = 1 AND "+ "s.user_org_id = ? AND "+ "NOT EXISTS (Select * FROM `xt_dialysis_order` as d Where d.`status` = 1 AND"+ " d.`patient_id` = s.patient_id AND "+ " d.user_org_id = s.user_org_id AND d.dialysis_date = s.schedule_date)", scheduleDate.Unix(), item.UserOrgId). Updates(map[string]interface{}{"status": 0, "updated_time": time.Now().Unix()}) } utils.SuccessLog("自动清除排班任务完成") }