auto_create_staff_schedule_service.go 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package service
  2. import (
  3. "Xcx_New/models"
  4. "fmt"
  5. "time"
  6. "github.com/jinzhu/gorm"
  7. "github.com/robfig/cron"
  8. )
  9. //// cron表达式 https://www.cnblogs.com/zuxingyu/p/6023919.html
  10. //var createWeekDisinfectionCronJob *cron.Cron
  11. //
  12. //func init() {
  13. // createWeekDisinfectionCronJob = cron.New()
  14. // //spec := "0 0 23 * * ?" // 每天23点执行一次
  15. // spec := "0 55 23 * * ?" // 每天23点55执行一次
  16. // //spec := "0 0 5 * * ?" // 每天23点55执行一次
  17. // //spec := "0 */1 * * * ?" // 每1分钟执行一次
  18. // createWeekDisinfectionCronJob.AddFunc(spec, func() {
  19. //
  20. // AutoCreateWeekDisinfectionJob()
  21. // })
  22. //}
  23. //
  24. //func BeginAutoCreateWeekDisinfectionJob() {
  25. //
  26. // createWeekDisinfectionCronJob.Start()
  27. //}
  28. //// cron表达式 https://www.cnblogs.com/zuxingyu/p/6023919.html
  29. var createStaffScheduleCronJob *cron.Cron
  30. // func init() {
  31. // createStaffScheduleCronJob = cron.New()
  32. // //spec := "0 */1 * * * ?" // 每1分钟执行一次
  33. // //spec := "0 55 23 * * ?" // 每天23点55执行一次
  34. // spec := "0 0 0 ? * Sun"
  35. // createStaffScheduleCronJob.AddFunc(spec, func() {
  36. // AutoCreateStaffScheduleJob()
  37. // })
  38. // }
  39. func BeginAutoCreateStaffScheduleJob() {
  40. createStaffScheduleCronJob.Start()
  41. }
  42. func AutoCreateStaffScheduleJob() {
  43. now := time.Now()
  44. offset := int(time.Monday - now.Weekday())
  45. if offset > 0 {
  46. offset = -6
  47. }
  48. weekStart := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, offset)
  49. fmt.Println("weekStart", weekStart.Unix())
  50. start_time := weekStart.Unix()
  51. fmt.Println("start_time", start_time)
  52. weekEnd := time.Date(now.Year(), now.Month(), now.Day()+6, 0, 0, 0, 0, time.Local).AddDate(0, 0, offset)
  53. fmt.Println("weekEnd", weekEnd.Unix())
  54. end_time := weekEnd.Unix()
  55. fmt.Println("end_time", end_time)
  56. //查询该机构的连续排班是否开启
  57. // 获取所有机构信息
  58. orgs, _ := GetAllOrgs()
  59. for _, item := range orgs {
  60. fmt.Println(item)
  61. //查询该机构是否开启连续排班功能
  62. schdule, _ := GetAutoContinueSchdule(item.ID)
  63. //如果状态值未开启不生成连续排班
  64. if schdule.IsStatus == 0 {
  65. fmt.Println("连续排班未开启")
  66. }
  67. //连续排班开启
  68. if schdule.IsStatus == 1 {
  69. //查询该机构本周的排班数据
  70. schedules, _ := GeThisWeekScheduleData(item.ID, start_time, end_time)
  71. for _, it := range schedules {
  72. staffSchedule := models.StaffSchedule{
  73. DoctorId: it.DoctorId,
  74. DoctorType: it.DoctorType,
  75. ScheduleType: it.ScheduleType,
  76. ScheduleWeek: it.ScheduleWeek,
  77. UserOrgId: it.UserOrgId,
  78. StartTime: it.StartTime + 604800,
  79. EndTime: it.EndTime + 604800,
  80. Status: 1,
  81. Ctime: time.Now().Unix(),
  82. ScheduleDate: it.ScheduleDate + 604800,
  83. }
  84. //产询该机构下周是否填加了数据
  85. _, errcode := GetNextWeekData(it.UserOrgId, it.DoctorId, it.ScheduleDate+604800)
  86. if errcode == gorm.ErrRecordNotFound {
  87. AddSchedules(&staffSchedule)
  88. } else {
  89. fmt.Println("下周数据已生成")
  90. }
  91. //if len(schedules) != len(schedule) {
  92. // AddSchedules(&staffSchedule)
  93. //}else{
  94. // fmt.Println("下周数据已生成")
  95. // //createStaffScheduleCronJob.Stop()
  96. //}
  97. }
  98. }
  99. }
  100. }
  101. func GetAllOrgs() (orgs []*models.SgjUserOrg, err error) {
  102. err = UserReadDB().Model(&orgs).Where("status = 1").Find(&orgs).Error
  103. return orgs, err
  104. }
  105. func GetAutoContinueSchdule(orgid int64) (models.ContinueSchedule, error) {
  106. schedule := models.ContinueSchedule{}
  107. err := XTReadDB().Model(&schedule).Where("user_org_id = ? and status = 1", orgid).Find(&schedule).Error
  108. return schedule, err
  109. }
  110. func GeThisWeekScheduleData(orgid int64, startime int64, endtime int64) (staffschedule []*models.StaffSchedule, err error) {
  111. err = XTReadDB().Model(&staffschedule).Where("user_org_id = ? and status = 1 and start_time>=? and end_time<=?", orgid, startime, endtime).Find(&staffschedule).Error
  112. return staffschedule, err
  113. }
  114. func AddSchedules(schedule *models.StaffSchedule) error {
  115. err := XTWriteDB().Model(&schedule).Create(&schedule).Error
  116. return err
  117. }
  118. func GetNextWeekData(orgid int64, docid int64, scheduledate int64) (*models.StaffSchedule, error) {
  119. schedule := models.StaffSchedule{}
  120. err := XTReadDB().Model(&schedule).Where("user_org_id = ? and status = 1 and doctor_id =? and schedule_date =?", orgid, docid, scheduledate).Find(&schedule).Error
  121. if err == gorm.ErrRecordNotFound {
  122. return nil, err
  123. }
  124. if err != nil {
  125. return nil, err
  126. }
  127. return &schedule, nil
  128. }