auto_clear_schedules_service.go 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package service
  2. import (
  3. "Xcx_New/models"
  4. "Xcx_New/utils"
  5. "time"
  6. "github.com/robfig/cron"
  7. )
  8. // cron表达式 https://www.cnblogs.com/zuxingyu/p/6023919.html
  9. var clearSchedulesCronJob *cron.Cron
  10. // func init() {
  11. // utils.InfoLog("开启自动清除排班定时任务")
  12. // clearSchedulesCronJob = cron.New()
  13. // spec := "0 0 1 * * ?" // 每天凌晨1点清除已排班未上机的排班数据
  14. // clearSchedulesCronJob.AddFunc(spec, func() {
  15. // AutoClearSchedules()
  16. // })
  17. // }
  18. func BeginAutoClearSchedulesJob() {
  19. clearSchedulesCronJob.Start()
  20. }
  21. func AutoClearSchedules() {
  22. utils.TraceLog("自动清除排班任务开始执行")
  23. //查出所有机构
  24. var schedules []*models.Schedule
  25. err = readDb.Table("xt_schedule as s").Where("s.status = 1").Select("distinct(s.user_org_id)").Find(&schedules).Error
  26. //遍历所有机构
  27. scheduleDate := utils.ZeroHourTimeOfDay(time.Now())
  28. for _, item := range schedules {
  29. //将今天(不包括今天)以前的的排班数据进行软删除
  30. writeDb.
  31. Table("xt_schedule as s").
  32. Where("s.schedule_date < ? AND s.status = 1 AND "+
  33. "s.user_org_id = ? AND "+
  34. "NOT EXISTS (Select * FROM `xt_dialysis_order` as d Where d.`status` = 1 AND"+
  35. " d.`patient_id` = s.patient_id AND "+
  36. " d.user_org_id = s.user_org_id AND d.dialysis_date = s.schedule_date)", scheduleDate.Unix(), item.UserOrgId).
  37. Updates(map[string]interface{}{"status": 0, "updated_time": time.Now().Unix()})
  38. }
  39. utils.SuccessLog("自动清除排班任务完成")
  40. }