schedule_template_api_controller.go 14KB

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