auto_create_week_schedules_service.go 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. package service
  2. import (
  3. "fmt"
  4. "strings"
  5. "time"
  6. "Xcx_New/models"
  7. "Xcx_New/utils"
  8. "github.com/jinzhu/gorm"
  9. "github.com/robfig/cron"
  10. )
  11. // cron表达式 https://www.cnblogs.com/zuxingyu/p/6023919.html
  12. var createWeekSchedulesCronJob *cron.Cron
  13. // func init() {
  14. // utils.InfoLog("开启自动排班定时任务")
  15. // createWeekSchedulesCronJob = cron.New()
  16. // spec := "0 0 0 ? * MON" // 每周一凌晨0点生成排班
  17. // createWeekSchedulesCronJob.AddFunc(spec, func() {
  18. // AutoCreateWeekSchedules()
  19. // })
  20. // }
  21. func BeginAutoCreateWeekSchedulesJob() {
  22. createWeekSchedulesCronJob.Start()
  23. }
  24. func AutoCreateWeekSchedules() {
  25. utils.TraceLog("自动排班任务开始执行")
  26. var modes []*models.PatientScheduleTemplateMode
  27. getModesErr := readDb.Model(&models.PatientScheduleTemplateMode{}).Where("mode <> 0 AND status = 1").Find(&modes).Error
  28. if getModesErr != nil {
  29. utils.ErrorLog("自动排班-获取启用模板模式列表失败:%v", getModesErr)
  30. CreateDBErrorRecord(0, "自动排班-获取启用模板模式列表失败:%v", getModesErr)
  31. return
  32. }
  33. for _, mode := range modes {
  34. templates, getTemplatesErr := GetOrgPatientScheduleTemplateIDs(mode.OrgID)
  35. if getTemplatesErr != nil {
  36. utils.ErrorLog("自动排班-org_id: %v :获取模板 ID 失败:%v", mode.OrgID, getTemplatesErr)
  37. CreateDBErrorRecord(mode.OrgID, "自动排班-org_id: %v :获取模板 ID 失败:%v", mode.OrgID, getTemplatesErr)
  38. continue
  39. } else if len(templates) == 0 {
  40. utils.WarningLog("自动排班-org_id: %v :没有模板", mode.OrgID)
  41. continue
  42. }
  43. now := time.Now()
  44. nextSecWeek := now.AddDate(0, 0, 14)
  45. tx := writeDb.Begin()
  46. if mode.Mode == 1 { // 用第一周模板生成下第二周的排班
  47. items, getItemsErr := GetOrgPatientScheduleTemplateItemsWithoutPatientByTemplateID(mode.OrgID, templates[0].ID)
  48. if getItemsErr != nil {
  49. utils.ErrorLog("自动排班-org_id: %v :获取第一周模板Item失败:%v", mode.OrgID, getItemsErr)
  50. CreateDBErrorRecord(mode.OrgID, "自动排班-org_id: %v :获取第一周模板Item失败:%v", mode.OrgID, getItemsErr)
  51. continue
  52. }
  53. insertErr := _refreshWeekSchedulesWithTemplateItems(tx, mode.OrgID, nextSecWeek, items, nil)
  54. if insertErr != nil {
  55. utils.ErrorLog("自动排班-org_id: %v :使用第一周模板插入下第二周排班失败:%v", mode.OrgID, insertErr)
  56. CreateDBErrorRecord(mode.OrgID, "自动排班-org_id: %v :使用第一周模板插入下第二周排班失败:%v", mode.OrgID, insertErr)
  57. tx.Rollback()
  58. continue
  59. }
  60. } else if mode.Mode == 2 { // mode.ExecuteTimes % 2 的结果,如果为0,用第一周模板生成下第二周的排班,否则用第二周的模板生成排班
  61. if mode.ExecuteTimes%2 == 0 {
  62. items, getItemsErr := GetOrgPatientScheduleTemplateItemsWithoutPatientByTemplateID(mode.OrgID, templates[0].ID)
  63. if getItemsErr != nil {
  64. utils.ErrorLog("自动排班-org_id: %v :获取第一周模板Item失败:%v", mode.OrgID, getItemsErr)
  65. CreateDBErrorRecord(mode.OrgID, "自动排班-org_id: %v :获取第一周模板Item失败:%v", mode.OrgID, getItemsErr)
  66. continue
  67. }
  68. insertErr := _refreshWeekSchedulesWithTemplateItems(tx, mode.OrgID, nextSecWeek, items, nil)
  69. if insertErr != nil {
  70. utils.ErrorLog("自动排班-org_id: %v :使用第一周模板插入下第二周排班失败:%v", mode.OrgID, insertErr)
  71. CreateDBErrorRecord(mode.OrgID, "自动排班-org_id: %v :使用第一周模板插入下第二周排班失败:%v", mode.OrgID, insertErr)
  72. tx.Rollback()
  73. continue
  74. }
  75. } else {
  76. if len(templates) < 2 {
  77. utils.WarningLog("自动排班-org_id: %v :没有第二周的模板")
  78. continue
  79. }
  80. items, getItemsErr := GetOrgPatientScheduleTemplateItemsWithoutPatientByTemplateID(mode.OrgID, templates[1].ID)
  81. if getItemsErr != nil {
  82. utils.ErrorLog("自动排班-org_id: %v :获取第二周模板Item失败:%v", mode.OrgID, getItemsErr)
  83. CreateDBErrorRecord(mode.OrgID, "自动排班-org_id: %v :获取第二周模板Item失败:%v", mode.OrgID, getItemsErr)
  84. continue
  85. }
  86. insertErr := _refreshWeekSchedulesWithTemplateItems(tx, mode.OrgID, nextSecWeek, items, nil)
  87. if insertErr != nil {
  88. utils.ErrorLog("自动排班-org_id: %v :使用第二周模板插入下第二周排班失败:%v", mode.OrgID, insertErr)
  89. CreateDBErrorRecord(mode.OrgID, "自动排班-org_id: %v :使用第二周模板插入下第二周排班失败:%v", mode.OrgID, insertErr)
  90. tx.Rollback()
  91. continue
  92. }
  93. }
  94. }
  95. mode.ExecuteTimes += 1
  96. mode.ModifyTime = now.Unix()
  97. updateModeErr := tx.Save(mode).Error
  98. if updateModeErr != nil {
  99. utils.ErrorLog("自动排班-org_id:%v :更新启用模板模式的执行次数失败:%v", mode.OrgID, updateModeErr)
  100. CreateDBErrorRecord(mode.OrgID, "自动排班-org_id:%v :更新启用模板模式的执行次数失败:%v", mode.OrgID, updateModeErr)
  101. tx.Rollback()
  102. continue
  103. }
  104. tx.Commit()
  105. utils.SuccessLog("自动排班-插入排班成功:org_id: %v", mode.OrgID)
  106. time.Sleep(time.Second * 30)
  107. }
  108. utils.SuccessLog("自动排班任务完成")
  109. }
  110. func _refreshWeekSchedulesWithTemplateItems(tx *gorm.DB, orgID int64, week time.Time, templateItems []*models.PatientScheduleTemplateItem, deviceNumberMap map[int64]*DeviceNumberViewModel) error {
  111. if deviceNumberMap == nil {
  112. deviceNumbers, getDeviceNumbersErr := GetAllValidDeviceNumbers(orgID)
  113. if getDeviceNumbersErr != nil {
  114. return getDeviceNumbersErr
  115. }
  116. deviceNumberMap = make(map[int64]*DeviceNumberViewModel, 0)
  117. for _, deviceNumber := range deviceNumbers {
  118. deviceNumberMap[deviceNumber.ID] = deviceNumber
  119. }
  120. }
  121. now := time.Now()
  122. monday, sunday := utils.GetMondayAndSundayOfWeekDate(&week)
  123. disableErr := tx.Model(&models.Schedule{}).Where("user_org_id = ? AND status = 1 AND schedule_date >= ? AND schedule_date <= ?", orgID, monday.Unix(), sunday.Unix()).Updates(map[string]interface{}{"status": 0, "mtime": now.Unix()}).Error
  124. if disableErr != nil {
  125. return disableErr
  126. }
  127. // 批量插入排班
  128. if len(templateItems) > 0 {
  129. valueStrs := make([]string, 0, len(templateItems))
  130. values := make([]interface{}, 0, len(templateItems)*11)
  131. for _, item := range templateItems {
  132. deviceNumber := deviceNumberMap[item.DeviceNumberID]
  133. if deviceNumber == nil {
  134. continue
  135. }
  136. fmt.Println("orgID", orgID)
  137. fmt.Println("deviceNumber.ZoneID", deviceNumber.ZoneID)
  138. fmt.Println("item.DeviceNumberID", item.DeviceNumberID)
  139. fmt.Println("item.PatientID", item.PatientID)
  140. fmt.Println("monday.AddDate(0, 0, int(item.Weekday-1)).Unix()", monday.AddDate(0, 0, int(item.Weekday-1)).Unix())
  141. fmt.Println("monday.AddDate(0, 0, int(item.Weekday-1)).Unix()", item.Weekday)
  142. fmt.Println("monday.AddDate(0, 0, int(item.Weekday-1)).Unix()", item.TimeType)
  143. fmt.Println("monday.AddDate(0, 0, int(item.Weekday-1)).Unix()", item.TreatMode)
  144. valueStrs = append(valueStrs, "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")
  145. values = append(values, orgID)
  146. values = append(values, deviceNumber.ZoneID)
  147. values = append(values, item.DeviceNumberID)
  148. values = append(values, item.PatientID)
  149. values = append(values, monday.AddDate(0, 0, int(item.Weekday-1)).Unix())
  150. values = append(values, item.Weekday)
  151. values = append(values, item.TimeType)
  152. values = append(values, item.TreatMode)
  153. values = append(values, 1)
  154. values = append(values, now.Unix())
  155. values = append(values, now.Unix())
  156. }
  157. sql := fmt.Sprintf("INSERT INTO xt_schedule (user_org_id, partition_id, bed_id, patient_id, schedule_date, schedule_week, schedule_type, mode_id, status, created_time, updated_time) VALUES %v;", strings.Join(valueStrs, ", "))
  158. if insertErr := tx.Exec(sql, values...).Error; insertErr != nil {
  159. return insertErr
  160. }
  161. }
  162. return nil
  163. }
  164. func FakeAutoCreateNextTwoWeekSchedules(orgID int64) {
  165. mode, getModeErr := GetOrgPatientScheduleTemplateMode(orgID)
  166. if getModeErr != nil {
  167. utils.ErrorLog("%v", getModeErr)
  168. return
  169. }
  170. templates, getTemplatesErr := GetOrgPatientScheduleTemplateIDs(mode.OrgID)
  171. if getTemplatesErr != nil {
  172. utils.ErrorLog("获取模板 ID 失败:%v", getTemplatesErr)
  173. return
  174. } else if len(templates) == 0 {
  175. utils.WarningLog("没有模板")
  176. return
  177. }
  178. deviceNumbers, getDeviceNumbersErr := GetAllValidDeviceNumbers(orgID)
  179. if getDeviceNumbersErr != nil {
  180. utils.ErrorLog("获取床位列表失败:%v", getDeviceNumbersErr)
  181. return
  182. }
  183. deviceNumberMap := make(map[int64]*DeviceNumberViewModel, 0)
  184. for _, deviceNumber := range deviceNumbers {
  185. deviceNumberMap[deviceNumber.ID] = deviceNumber
  186. }
  187. now := time.Now()
  188. tx := writeDb.Begin()
  189. if mode.Mode == 1 { // 用第一周模板生成下两周的排班
  190. items, getItemsErr := GetOrgPatientScheduleTemplateItemsWithoutPatientByTemplateID(mode.OrgID, templates[0].ID)
  191. if getItemsErr != nil {
  192. utils.ErrorLog("获取第一周模板Item失败:%v", getItemsErr)
  193. return
  194. }
  195. insertErr := _refreshWeekSchedulesWithTemplateItems(tx, mode.OrgID, now.AddDate(0, 0, 7), items, deviceNumberMap)
  196. if insertErr != nil {
  197. utils.ErrorLog("使用第一周模板插入下第一周排班失败:%v", insertErr)
  198. tx.Rollback()
  199. return
  200. }
  201. insertErr = _refreshWeekSchedulesWithTemplateItems(tx, mode.OrgID, now.AddDate(0, 0, 14), items, deviceNumberMap)
  202. if insertErr != nil {
  203. utils.ErrorLog("使用第一周模板插入下第二周排班失败:%v", insertErr)
  204. tx.Rollback()
  205. return
  206. }
  207. } else if mode.Mode == 2 {
  208. firstItems, getFirstItemsErr := GetOrgPatientScheduleTemplateItemsWithoutPatientByTemplateID(mode.OrgID, templates[0].ID)
  209. if getFirstItemsErr != nil {
  210. utils.ErrorLog("获取第一周模板Item失败:%v", getFirstItemsErr)
  211. return
  212. }
  213. secItems, getSecItemsErr := GetOrgPatientScheduleTemplateItemsWithoutPatientByTemplateID(mode.OrgID, templates[1].ID)
  214. if getSecItemsErr != nil {
  215. utils.ErrorLog("获取第一周模板Item失败:%v", getSecItemsErr)
  216. return
  217. }
  218. if mode.ExecuteTimes%2 == 0 {
  219. insertErr := _refreshWeekSchedulesWithTemplateItems(tx, mode.OrgID, now.AddDate(0, 0, 7), firstItems, deviceNumberMap)
  220. if insertErr != nil {
  221. utils.ErrorLog("使用第一周模板插入下第一周排班失败:%v", insertErr)
  222. tx.Rollback()
  223. return
  224. }
  225. insertErr = _refreshWeekSchedulesWithTemplateItems(tx, mode.OrgID, now.AddDate(0, 0, 14), secItems, deviceNumberMap)
  226. if insertErr != nil {
  227. utils.ErrorLog("使用第二周模板插入下第二周排班失败:%v", insertErr)
  228. tx.Rollback()
  229. return
  230. }
  231. } else { // mode.ExecuteTimes%2 == 1
  232. insertErr := _refreshWeekSchedulesWithTemplateItems(tx, mode.OrgID, now.AddDate(0, 0, 7), secItems, deviceNumberMap)
  233. if insertErr != nil {
  234. utils.ErrorLog("使用第二周模板插入下第一周排班失败:%v", mode.OrgID, insertErr)
  235. tx.Rollback()
  236. return
  237. }
  238. insertErr = _refreshWeekSchedulesWithTemplateItems(tx, mode.OrgID, now.AddDate(0, 0, 14), firstItems, deviceNumberMap)
  239. if insertErr != nil {
  240. utils.ErrorLog("使用第一周模板插入下第二周排班失败:%v", mode.OrgID, insertErr)
  241. tx.Rollback()
  242. return
  243. }
  244. }
  245. }
  246. mode.ExecuteTimes += 2
  247. mode.ModifyTime = now.Unix()
  248. updateModeErr := tx.Save(mode).Error
  249. if updateModeErr != nil {
  250. utils.ErrorLog("更新启用模板模式的执行次数失败:%v", mode.OrgID, updateModeErr)
  251. tx.Rollback()
  252. return
  253. }
  254. tx.Commit()
  255. utils.SuccessLog("模拟自动更新下两周排班成功")
  256. }