schedule_template_api_controller.go 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. package controllers
  2. import (
  3. "Xcx_New/enums"
  4. "Xcx_New/models"
  5. "Xcx_New/service"
  6. "Xcx_New/utils"
  7. "encoding/json"
  8. "time"
  9. "github.com/astaxie/beego"
  10. )
  11. func PatientScheduleTemplateAPIControllerRegistRouters() {
  12. beego.Router("/api/schtemp/p/initdata", &PatientScheduleTemplateAPIController{}, "get:InitData")
  13. beego.Router("/api/schtemp/p/setmode", &PatientScheduleTemplateAPIController{}, "post:SetMode")
  14. beego.Router("/api/schtemp/p/update_sch", &PatientScheduleTemplateAPIController{}, "post:UpdateSchedules")
  15. // beego.Router("/api/test", &TestCtr{}, "get:Test")
  16. }
  17. // type TestCtr struct {
  18. // BaseAPIController
  19. // }
  20. // func (this *TestCtr) Test() {
  21. // now := time.Now().AddDate(0, 0, 7)
  22. // weekday := int(now.Weekday())
  23. // if weekday == 0 {
  24. // weekday = 7
  25. // }
  26. // monday := now.AddDate(0, 0, 1-weekday)
  27. // sunday := now.AddDate(0, 0, 7-weekday)
  28. // // this.TraceLog("%v", weekday)
  29. // this.ServeSuccessJSON(map[string]interface{}{
  30. // "weekday": weekday,
  31. // "monday": monday.Format("2006-01-02"),
  32. // "sunday": sunday.Format("2006-01-02"),
  33. // })
  34. // }
  35. // ------- 患者排班模板 --------
  36. type PatientScheduleTemplateAPIController struct {
  37. BaseAuthAPIController
  38. }
  39. // /api/schtemp/p/initdata [get]
  40. func (this *PatientScheduleTemplateAPIController) InitData() {
  41. adminUserInfo := this.GetAdminUserInfo()
  42. mode, getModeErr := service.GetOrgPatientScheduleTemplateMode(adminUserInfo.CurrentOrgId)
  43. if getModeErr != nil {
  44. this.ErrorLog("获取排班模板启用模式失败:%v", getModeErr)
  45. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  46. return
  47. }
  48. now := time.Now()
  49. if mode == nil {
  50. mode = &models.PatientScheduleTemplateMode{
  51. OrgID: adminUserInfo.CurrentOrgId,
  52. Mode: 0,
  53. ExecuteTimes: 0,
  54. Status: 1,
  55. CreateTime: now.Unix(),
  56. ModifyTime: now.Unix(),
  57. }
  58. createModeErr := service.SavePatientScheduleTemplateMode(mode)
  59. if createModeErr != nil {
  60. this.ErrorLog("创建排班模板启用模式失败:%v", createModeErr)
  61. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  62. return
  63. }
  64. }
  65. deviceNumbers, getDeviceNumbersErr := service.GetScheduleValidDeviceNumbers(adminUserInfo.CurrentOrgId)
  66. if getDeviceNumbersErr != nil {
  67. this.ErrorLog("获取床位号失败:%v", getDeviceNumbersErr)
  68. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  69. return
  70. }
  71. templates, getTemplatesErr := service.GetOrgPatientScheduleTemplateItems(adminUserInfo.CurrentOrgId)
  72. if getTemplatesErr != nil {
  73. this.ErrorLog("获取排班模板及其排班列表失败:%v", getTemplatesErr)
  74. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  75. return
  76. }
  77. if len(templates) == 0 {
  78. firstTemp, secondTemp, createTempErr := service.CreateTwoPatientScheduleTemplates(adminUserInfo.CurrentOrgId)
  79. if createTempErr != nil {
  80. this.ErrorLog("创建两个排班模板失败:%v", createTempErr)
  81. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  82. return
  83. }
  84. templates = append(templates, firstTemp, secondTemp)
  85. } else if len(templates) == 1 {
  86. temp, createTempErr := service.CreatePatientScheduleTemplate(adminUserInfo.CurrentOrgId)
  87. if createTempErr != nil {
  88. this.ErrorLog("创建两个排班模板失败:%v", createTempErr)
  89. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  90. return
  91. }
  92. templates = append(templates, temp)
  93. }
  94. patients, getPatientsErr := service.PatientScheduleTemplateGetAllValidPatient(adminUserInfo.CurrentOrgId)
  95. if getPatientsErr != nil {
  96. this.ErrorLog("获取患者列表失败失败:%v", getPatientsErr)
  97. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  98. return
  99. }
  100. // 获取本周的排班
  101. monday, sunday := utils.GetMondayAndSundayOfWeekDate(&now)
  102. schedules, getScheduleErr := service.GetWeekSchedule(adminUserInfo.CurrentOrgId, monday.Unix(), sunday.Unix())
  103. if getScheduleErr != nil {
  104. this.ErrorLog("获取本周排班失败:%v", getScheduleErr)
  105. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  106. return
  107. }
  108. this.ServeSuccessJSON(map[string]interface{}{
  109. "template_mode": mode,
  110. "device_numbers": deviceNumbers,
  111. "templates": templates,
  112. "patients": patients,
  113. "schedules": schedules,
  114. })
  115. }
  116. // /api/schtemp/p/setmode [post]
  117. // @param mode:int 0.不启用模板 1.单周模板 2.双周模板
  118. func (this *PatientScheduleTemplateAPIController) SetMode() {
  119. mode, _ := this.GetInt8("mode")
  120. if mode != 1 && mode != 2 && mode != 0 && mode != 3 && mode != 4 {
  121. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  122. return
  123. }
  124. adminUserInfo := this.GetAdminUserInfo()
  125. templateMode, getTemplateModeErr := service.GetOrgPatientScheduleTemplateMode(adminUserInfo.CurrentOrgId)
  126. if getTemplateModeErr != nil {
  127. this.ErrorLog("获取排班模板启用模式失败:%v", getTemplateModeErr)
  128. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  129. return
  130. } else if templateMode == nil {
  131. this.ErrorLog("获取不到排班模板启用模式")
  132. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  133. return
  134. }
  135. if mode != templateMode.Mode {
  136. if templateMode.Mode == 0 { // 0 -> 1 或 0 -> 2 则直接清除下两周的排班
  137. templateMode.Mode = mode
  138. templateMode.ExecuteTimes = 0
  139. templateMode.ModifyTime = time.Now().Unix()
  140. updateErr := service.SavePatientScheduleTemplateModeAndClearNextTwoWeekSchedules(templateMode)
  141. if updateErr != nil {
  142. this.ErrorLog("更新排班模板启用方式(0 -> 1 或 0 -> 2)时失败:%v", updateErr)
  143. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  144. return
  145. }
  146. } else if mode == 0 { // 1 -> 0 或 2 -> 0 则不处理排班,但清除所有模板item
  147. templateMode.Mode = mode
  148. templateMode.ExecuteTimes = 0
  149. templateMode.ModifyTime = time.Now().Unix()
  150. updateErr := service.SavePatientScheduleTemplateModeAndClearAllTemplateItems(templateMode)
  151. if updateErr != nil {
  152. this.ErrorLog("更新排班模板启用方式(1 -> 0 或 2 -> 0)时失败:%v", updateErr)
  153. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  154. return
  155. }
  156. } else if templateMode.Mode == 1 && mode == 2 { // 1 -> 2 清除第二周排班
  157. templateMode.Mode = mode
  158. templateMode.ExecuteTimes = 0
  159. templateMode.ModifyTime = time.Now().Unix()
  160. updateErr := service.SavePatientScheduleTemplateModeAndClearNextSecondWeekSchedules(templateMode)
  161. if updateErr != nil {
  162. this.ErrorLog("更新排班模板启用方式(1 -> 2)时失败:%v", updateErr)
  163. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  164. return
  165. }
  166. } else if templateMode.Mode == 2 && mode == 1 { // 2 -> 1 清除第二周模板item,清除第二周排班,将第一周的排班重复到第二周
  167. templateIDs, getTemplateIDsErr := service.GetOrgPatientScheduleTemplateIDs(adminUserInfo.CurrentOrgId)
  168. if getTemplateIDsErr != nil {
  169. this.ErrorLog("获取排班模板失败:%v", getTemplateIDsErr)
  170. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  171. return
  172. }
  173. firstWeekItems, getFirstWeekItemsErr := service.GetOrgPatientScheduleTemplateItemsWithoutPatientByTemplateID(adminUserInfo.CurrentOrgId, templateIDs[0].ID)
  174. if getFirstWeekItemsErr != nil {
  175. this.ErrorLog("获取排班模板排班失败:%v", getFirstWeekItemsErr)
  176. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  177. return
  178. }
  179. templateMode.Mode = mode
  180. templateMode.ExecuteTimes = 0
  181. templateMode.ModifyTime = time.Now().Unix()
  182. updateErr := service.SavePatientScheduleTemplateMode2To1(templateMode, templateIDs[1].ID, firstWeekItems)
  183. if updateErr != nil {
  184. this.ErrorLog("更新排班模板启用方式(1 -> 2)时失败:%v", updateErr)
  185. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  186. return
  187. }
  188. }
  189. }
  190. this.ServeSuccessJSON(nil)
  191. }
  192. // /api/schtemp/p/update_sch [post]
  193. // @param template_id:int
  194. // @param add_schs:array'json ([ {device_number_id, patient_id, mode(treat_mode), weekday, time_type} ])
  195. // @param del_schs:array'json ([ {device_number_id, weekday, time_type} ])
  196. // @param change_schs:array'json ([ {device_number_id, patient_id, mode(treat_mode), weekday, time_type} ])
  197. func (this *PatientScheduleTemplateAPIController) UpdateSchedules() {
  198. templateID, _ := this.GetInt64("template_id")
  199. addSchedulesJSON := this.GetString("add_schs")
  200. delSchedulesJSON := this.GetString("del_schs")
  201. changeSchedulesJSON := this.GetString("change_schs")
  202. if templateID <= 0 || len(addSchedulesJSON) == 0 || len(delSchedulesJSON) == 0 || len(changeSchedulesJSON) == 0 {
  203. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  204. return
  205. }
  206. var addSchedules []*ParamModelSchedule
  207. var delSchedules []*ParamModelSchedule
  208. var changeSchedules []*ParamModelSchedule
  209. parseErr := json.Unmarshal([]byte(addSchedulesJSON), &addSchedules)
  210. if parseErr != nil {
  211. this.ErrorLog("解析增加排班的数组参数失败:%v", parseErr)
  212. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  213. return
  214. }
  215. parseErr = json.Unmarshal([]byte(delSchedulesJSON), &delSchedules)
  216. if parseErr != nil {
  217. this.ErrorLog("解析删除排班的数组参数失败:%v", parseErr)
  218. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  219. return
  220. }
  221. parseErr = json.Unmarshal([]byte(changeSchedulesJSON), &changeSchedules)
  222. if parseErr != nil {
  223. this.ErrorLog("解析修改排班的数组参数失败:%v", parseErr)
  224. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  225. return
  226. }
  227. adminUserInfo := this.GetAdminUserInfo()
  228. templateIDs, getTemplateIDsErr := service.GetOrgPatientScheduleTemplateIDs(adminUserInfo.CurrentOrgId)
  229. if getTemplateIDsErr != nil {
  230. this.ErrorLog("获取排班模板失败:%v", getTemplateIDsErr)
  231. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  232. return
  233. } else {
  234. isTemplateIDExist := false
  235. for _, templateIDObject := range templateIDs {
  236. if templateIDObject.ID == templateID {
  237. isTemplateIDExist = true
  238. break
  239. }
  240. }
  241. if isTemplateIDExist == false {
  242. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeScheduleTemplateNotExist)
  243. return
  244. }
  245. }
  246. addTemplateItems := make([]*models.PatientScheduleTemplateItem, 0, len(addSchedules))
  247. delTemplateItems := make([]*models.PatientScheduleTemplateItem, 0, len(delSchedules))
  248. changeTemplateItems := make([]*models.PatientScheduleTemplateItem, 0, len(changeSchedules))
  249. for _, sch := range addSchedules {
  250. if sch.DeviceNumberID <= 0 || sch.PatientID <= 0 || sch.Weekday < 1 || sch.Weekday > 7 || sch.TimeType < 1 || sch.TimeType > 3 {
  251. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  252. return
  253. }
  254. addTemplateItems = append(addTemplateItems, &models.PatientScheduleTemplateItem{
  255. DeviceNumberID: sch.DeviceNumberID,
  256. PatientID: sch.PatientID,
  257. TreatMode: sch.Mode,
  258. Weekday: sch.Weekday,
  259. TimeType: sch.TimeType,
  260. })
  261. }
  262. for _, sch := range delSchedules {
  263. if sch.DeviceNumberID <= 0 || sch.Weekday < 1 || sch.Weekday > 7 || sch.TimeType < 1 || sch.TimeType > 3 {
  264. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  265. return
  266. }
  267. delTemplateItems = append(delTemplateItems, &models.PatientScheduleTemplateItem{
  268. DeviceNumberID: sch.DeviceNumberID,
  269. Weekday: sch.Weekday,
  270. TimeType: sch.TimeType,
  271. })
  272. }
  273. for _, sch := range changeSchedules {
  274. if sch.DeviceNumberID <= 0 || sch.PatientID <= 0 || sch.Weekday < 1 || sch.Weekday > 7 || sch.TimeType < 1 || sch.TimeType > 3 {
  275. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  276. return
  277. }
  278. changeTemplateItems = append(changeTemplateItems, &models.PatientScheduleTemplateItem{
  279. DeviceNumberID: sch.DeviceNumberID,
  280. PatientID: sch.PatientID,
  281. TreatMode: sch.Mode,
  282. Weekday: sch.Weekday,
  283. TimeType: sch.TimeType,
  284. })
  285. }
  286. templateMode, getTemplateModeErr := service.GetOrgPatientScheduleTemplateMode(adminUserInfo.CurrentOrgId)
  287. if getTemplateModeErr != nil {
  288. this.ErrorLog("获取排班模板启用模式失败:%v", getTemplateModeErr)
  289. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  290. return
  291. } else if templateMode == nil {
  292. this.ErrorLog("获取不到排班模板启用模式")
  293. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  294. return
  295. }
  296. shouldUpdateNextWeekSchedules := false
  297. shouldUpdateNextSecondWeekSchedules := false
  298. if templateMode.Mode == 0 {
  299. this.ErrorLog("当前未开启排班模板,所以更新模板的操作将被无视")
  300. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  301. return
  302. }
  303. templateIDIndex := 1
  304. for index, templateIDObject := range templateIDs {
  305. if templateIDObject.ID == templateID {
  306. templateIDIndex = index + 1
  307. }
  308. }
  309. if templateMode.Mode == 1 {
  310. if templateIDIndex == 1 {
  311. shouldUpdateNextWeekSchedules = true
  312. shouldUpdateNextSecondWeekSchedules = true
  313. } else {
  314. this.ErrorLog("当前开启的是单周模板,所以不会更新第二个模板")
  315. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  316. return
  317. }
  318. } else { // mode == 2 => 开启双周模板时,只需更新一周排班
  319. if templateIDIndex == 1 {
  320. if templateMode.ExecuteTimes%2 == 0 {
  321. shouldUpdateNextWeekSchedules = true
  322. shouldUpdateNextSecondWeekSchedules = false
  323. } else {
  324. shouldUpdateNextWeekSchedules = false
  325. shouldUpdateNextSecondWeekSchedules = true
  326. }
  327. } else {
  328. if templateMode.ExecuteTimes%2 == 0 {
  329. shouldUpdateNextWeekSchedules = false
  330. shouldUpdateNextSecondWeekSchedules = true
  331. } else {
  332. shouldUpdateNextWeekSchedules = true
  333. shouldUpdateNextSecondWeekSchedules = false
  334. }
  335. }
  336. }
  337. updateErr := service.UpdateScheduleTemplate(adminUserInfo.CurrentOrgId, templateID, addTemplateItems, delTemplateItems, changeTemplateItems, shouldUpdateNextWeekSchedules, shouldUpdateNextSecondWeekSchedules)
  338. if updateErr != nil {
  339. this.ErrorLog("更新排班模板排班失败:%v", updateErr)
  340. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDBUpdate)
  341. return
  342. }
  343. newItems, getTemplateItemsErr := service.GetOrgPatientScheduleTemplateItemsByTemplateID(adminUserInfo.CurrentOrgId, templateID)
  344. if getTemplateItemsErr != nil {
  345. this.ErrorLog("获取排班模板排班失败:%v", getTemplateItemsErr)
  346. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  347. return
  348. }
  349. this.ServeSuccessJSON(map[string]interface{}{
  350. "items": newItems,
  351. })
  352. }
  353. type ParamModelSchedule struct {
  354. DeviceNumberID int64 `json:"device_number_id"`
  355. PatientID int64 `json:"patient_id"`
  356. Mode int64 `json:"mode"`
  357. Weekday int8 `json:"weekday"`
  358. TimeType int8 `json:"time_type"`
  359. }