auto_create_staff_schedule_service.go 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package service
  2. import (
  3. "XT_New/models"
  4. "fmt"
  5. "github.com/jinzhu/gorm"
  6. "github.com/robfig/cron"
  7. "time"
  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. //查询该机构是否开启连续排班功能
  61. schdule, _ := GetAutoContinueSchdule(item.ID)
  62. //如果状态值未开启不生成连续排班
  63. if schdule.IsStatus == 0 {
  64. fmt.Println("连续排班未开启")
  65. }
  66. //连续排班开启
  67. if schdule.IsStatus == 1 {
  68. //查询该机构本周的排班数据
  69. schedules, _ := GeThisWeekScheduleData(item.ID, start_time, end_time)
  70. for _, it := range schedules {
  71. staffSchedule := models.StaffSchedule{
  72. DoctorId: it.DoctorId,
  73. DoctorType: it.DoctorType,
  74. ScheduleType: it.ScheduleType,
  75. ScheduleWeek: it.ScheduleWeek,
  76. UserOrgId: it.UserOrgId,
  77. StartTime: it.StartTime + 604800,
  78. EndTime: it.EndTime + 604800,
  79. Status: 1,
  80. Ctime: time.Now().Unix(),
  81. ScheduleDate: it.ScheduleDate + 604800,
  82. }
  83. //产询该机构下周是否填加了数据
  84. _, errcode := GetNextWeekData(it.UserOrgId, it.DoctorId, it.ScheduleDate+604800)
  85. if errcode == gorm.ErrRecordNotFound {
  86. AddSchedules(&staffSchedule)
  87. } else {
  88. fmt.Println("下周数据已生成")
  89. }
  90. //if len(schedules) != len(schedule) {
  91. // AddSchedules(&staffSchedule)
  92. //}else{
  93. // fmt.Println("下周数据已生成")
  94. // //createStaffScheduleCronJob.Stop()
  95. //}
  96. }
  97. }
  98. }
  99. }
  100. func GetAllOrgs() (orgs []*models.SgjUserOrg, err error) {
  101. err = UserReadDB().Model(&orgs).Where("status = 1").Find(&orgs).Error
  102. return orgs, err
  103. }
  104. func GetAutoContinueSchdule(orgid int64) (models.ContinueSchedule, error) {
  105. schedule := models.ContinueSchedule{}
  106. err := XTReadDB().Model(&schedule).Where("user_org_id = ? and status = 1", orgid).Find(&schedule).Error
  107. return schedule, err
  108. }
  109. func GeThisWeekScheduleData(orgid int64, startime int64, endtime int64) (staffschedule []*models.StaffSchedule, err error) {
  110. err = XTReadDB().Model(&staffschedule).Where("user_org_id = ? and status = 1 and start_time>=? and end_time<=?", orgid, startime, endtime).Find(&staffschedule).Error
  111. return staffschedule, err
  112. }
  113. func AddSchedules(schedule *models.StaffSchedule) error {
  114. tx := writeDb.Begin()
  115. err := XTWriteDB().Model(&schedule).Create(&schedule).Error
  116. if err != nil {
  117. tx.Rollback()
  118. return err
  119. }
  120. tx.Commit()
  121. return nil
  122. }
  123. func GetNextWeekData(orgid int64, docid int64, scheduledate int64) (*models.StaffSchedule, error) {
  124. schedule := models.StaffSchedule{}
  125. err := XTReadDB().Model(&schedule).Where("user_org_id = ? and status = 1 and doctor_id =? and schedule_date =?", orgid, docid, scheduledate).Find(&schedule).Error
  126. if err == gorm.ErrRecordNotFound {
  127. return nil, err
  128. }
  129. if err != nil {
  130. return nil, err
  131. }
  132. return &schedule, nil
  133. }