schedule_template_api_controller.go 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  1. package controllers
  2. import (
  3. "XT_New/enums"
  4. "XT_New/models"
  5. "XT_New/service"
  6. "encoding/json"
  7. "fmt"
  8. "strings"
  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/patients/list", &PatientScheduleTemplateAPIController{}, "get:GetPatientList")
  17. // beego.Router("/api/test", &TestCtr{}, "get:Test")
  18. }
  19. // type TestCtr struct {
  20. // BaseAPIController
  21. // }
  22. // func (this *TestCtr) Test() {
  23. // now := time.Now().AddDate(0, 0, 7)
  24. // weekday := int(now.Weekday())
  25. // if weekday == 0 {
  26. // weekday = 7
  27. // }
  28. // monday := now.AddDate(0, 0, 1-weekday)
  29. // sunday := now.AddDate(0, 0, 7-weekday)
  30. // // this.TraceLog("%v", weekday)
  31. // this.ServeSuccessJSON(map[string]interface{}{
  32. // "weekday": weekday,
  33. // "monday": monday.Format("2006-01-02"),
  34. // "sunday": sunday.Format("2006-01-02"),
  35. // })
  36. // }
  37. // ------- 患者排班模板 --------
  38. type PatientScheduleTemplateAPIController struct {
  39. BaseAuthAPIController
  40. }
  41. // /api/schtemp/p/initdata [get]
  42. func (this *PatientScheduleTemplateAPIController) InitData() {
  43. adminUserInfo := this.GetAdminUserInfo()
  44. patitionIdStr := this.GetString("patitionid")
  45. var ids []string
  46. if len(patitionIdStr) > 0 {
  47. ids = strings.Split(patitionIdStr, ",")
  48. }
  49. mode, getModeErr := service.GetOrgPatientScheduleTemplateMode(adminUserInfo.CurrentOrgId)
  50. if getModeErr != nil {
  51. this.ErrorLog("获取排班模板启用模式失败:%v", getModeErr)
  52. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  53. return
  54. }
  55. now := time.Now()
  56. if mode == nil {
  57. mode = &models.PatientScheduleTemplateMode{
  58. OrgID: adminUserInfo.CurrentOrgId,
  59. Mode: 0,
  60. ExecuteTimes: 0,
  61. Status: 1,
  62. CreateTime: now.Unix(),
  63. ModifyTime: now.Unix(),
  64. }
  65. createModeErr := service.SavePatientScheduleTemplateMode(mode)
  66. if createModeErr != nil {
  67. this.ErrorLog("创建排班模板启用模式失败:%v", createModeErr)
  68. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  69. return
  70. }
  71. }
  72. deviceNumbers, getDeviceNumbersErr := service.GetScheduleValidDeviceNumbers(adminUserInfo.CurrentOrgId, ids)
  73. fmt.Println("deviceNumbers233323232332232323223233223323232323", deviceNumbers)
  74. if getDeviceNumbersErr != nil {
  75. this.ErrorLog("获取床位号失败:%v", getDeviceNumbersErr)
  76. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  77. return
  78. }
  79. templates, getTemplatesErr := service.GetOrgPatientScheduleTemplateItems(adminUserInfo.CurrentOrgId)
  80. if getTemplatesErr != nil {
  81. this.ErrorLog("获取排班模板及其排班列表失败:%v", getTemplatesErr)
  82. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  83. return
  84. }
  85. if len(templates) == 0 {
  86. firstTemp, secondTemp, thirdTemp, fourTemp, createTempErr := service.CreateTwoPatientScheduleTemplates(adminUserInfo.CurrentOrgId)
  87. if createTempErr != nil {
  88. this.ErrorLog("创建4个排班模板失败:%v", createTempErr)
  89. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  90. return
  91. }
  92. templates = append(templates, firstTemp, secondTemp, thirdTemp, fourTemp)
  93. } else if len(templates) == 1 {
  94. temp, thirdTemp, fourTemp, createTempErr := service.CreatePatientScheduleTemplate(adminUserInfo.CurrentOrgId)
  95. if createTempErr != nil {
  96. this.ErrorLog("创建3个排班模板失败:%v", createTempErr)
  97. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  98. return
  99. }
  100. templates = append(templates, temp, thirdTemp, fourTemp)
  101. } else if len(templates) == 2 {
  102. thirdTemp, fourTemp, createTempErr := service.CreatePatientScheduleTemplateTwo(adminUserInfo.CurrentOrgId)
  103. if createTempErr != nil {
  104. this.ErrorLog("创建2个排班模板失败:%v", createTempErr)
  105. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  106. return
  107. }
  108. templates = append(templates, thirdTemp, fourTemp)
  109. } else if len(templates) == 3 {
  110. temp, createTempErr := service.CreatePatientScheduleTemplateThree(adminUserInfo.CurrentOrgId)
  111. if createTempErr != nil {
  112. this.ErrorLog("创建1个排班模板失败:%v", createTempErr)
  113. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  114. return
  115. }
  116. templates = append(templates, temp)
  117. }
  118. patients, getPatientsErr := service.PatientScheduleTemplateGetAllValidPatient(adminUserInfo.CurrentOrgId)
  119. if getPatientsErr != nil {
  120. this.ErrorLog("获取患者列表失败失败:%v", getPatientsErr)
  121. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  122. return
  123. }
  124. // 获取本周的排班
  125. //monday, sunday := utils.GetMondayAndSundayOfWeekDate(&now)
  126. //schedules, getScheduleErr := service.GetWeekSchedule(adminUserInfo.CurrentOrgId, monday.Unix(), sunday.Unix())
  127. //if getScheduleErr != nil {
  128. // this.ErrorLog("获取本周排班失败:%v", getScheduleErr)
  129. // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  130. // return
  131. //}
  132. this.ServeSuccessJSON(map[string]interface{}{
  133. "template_mode": mode,
  134. "device_numbers": deviceNumbers,
  135. "templates": templates,
  136. "patients": patients,
  137. //"schedules": schedules,
  138. })
  139. }
  140. // /api/schtemp/p/setmode [post]
  141. // @param mode:int 0.不启用模板 1.单周模板 2.双周模板 3.三周模版 4.四周模版
  142. func (this *PatientScheduleTemplateAPIController) SetMode() {
  143. mode, _ := this.GetInt8("mode")
  144. week_time, _ := this.GetInt8("week_time")
  145. if mode != 1 && mode != 2 && mode != 0 && mode != 3 && mode != 4 {
  146. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  147. return
  148. }
  149. if mode > 0 {
  150. if week_time == 0 {
  151. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  152. return
  153. }
  154. }
  155. adminUserInfo := this.GetAdminUserInfo()
  156. templateMode, getTemplateModeErr := service.GetOrgPatientScheduleTemplateMode(adminUserInfo.CurrentOrgId)
  157. if getTemplateModeErr != nil {
  158. this.ErrorLog("获取排班模板启用模式失败:%v", getTemplateModeErr)
  159. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  160. return
  161. } else if templateMode == nil {
  162. this.ErrorLog("获取不到排班模板启用模式")
  163. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  164. return
  165. }
  166. templateMode.Week = int64(week_time)
  167. if mode != templateMode.Mode {
  168. if templateMode.Mode == 0 { // 0 -> 1 或 0 -> 2 则直接清除下四周的排班
  169. templateMode.Mode = mode
  170. templateMode.ExecuteTimes = 0
  171. templateMode.ModifyTime = time.Now().Unix()
  172. updateErr := service.SavePatientScheduleTemplateModeAndClearNextTwoWeekSchedules(templateMode)
  173. if updateErr != nil {
  174. this.ErrorLog("更新排班模板启用方式(0 -> 1 或 0 -> 2)时失败:%v", updateErr)
  175. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  176. return
  177. }
  178. } else if mode == 0 { // 1 -> 0 或 2 -> 0 则不处理排班,但清除所有模板item
  179. templateMode.Mode = mode
  180. templateMode.ExecuteTimes = 0
  181. templateMode.ModifyTime = time.Now().Unix()
  182. updateErr := service.SavePatientScheduleTemplateModeAndClearAllTemplateItems(templateMode)
  183. if updateErr != nil {
  184. this.ErrorLog("更新排班模板启用方式(1 -> 0 或 2 -> 0)时失败:%v", updateErr)
  185. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  186. return
  187. }
  188. } else if templateMode.Mode == 1 && mode == 2 { // 1 -> 2 清除第二周排班
  189. templateMode.Mode = mode
  190. templateMode.ExecuteTimes = 0
  191. templateMode.ModifyTime = time.Now().Unix()
  192. updateErr := service.SavePatientScheduleTemplateModeAndClearNextTwoWeekSchedules(templateMode)
  193. if updateErr != nil {
  194. this.ErrorLog("更新排班模板启用方式(1 -> 2)时失败:%v", updateErr)
  195. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  196. return
  197. }
  198. } else if templateMode.Mode == 1 && mode == 3 {
  199. templateMode.Mode = mode
  200. templateMode.ExecuteTimes = 0
  201. templateMode.ModifyTime = time.Now().Unix()
  202. updateErr := service.SaveTemplateMode(templateMode)
  203. if updateErr != nil {
  204. this.ErrorLog("更新排班模板启用方式(1 -> 3)时失败:%v", updateErr)
  205. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  206. return
  207. }
  208. } else if templateMode.Mode == 1 && mode == 4 {
  209. templateMode.Mode = mode
  210. templateMode.ExecuteTimes = 0
  211. templateMode.ModifyTime = time.Now().Unix()
  212. updateErr := service.SaveTemplateMode(templateMode)
  213. if updateErr != nil {
  214. this.ErrorLog("更新排班模板启用方式(1 -> 4)时失败:%v", updateErr)
  215. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  216. return
  217. }
  218. } else if templateMode.Mode == 2 && mode == 1 { // 2 -> 1 清除第二周模板item,清除第二周排班
  219. templateIDs, getTemplateIDsErr := service.GetOrgPatientScheduleTemplateIDs(adminUserInfo.CurrentOrgId)
  220. if getTemplateIDsErr != nil {
  221. this.ErrorLog("获取排班模板失败:%v", getTemplateIDsErr)
  222. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  223. return
  224. }
  225. firstWeekItems, getFirstWeekItemsErr := service.GetOrgPatientScheduleTemplateItemsWithoutPatientByTemplateID(adminUserInfo.CurrentOrgId, templateIDs[0].ID)
  226. if getFirstWeekItemsErr != nil {
  227. this.ErrorLog("获取排班模板排班失败:%v", getFirstWeekItemsErr)
  228. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  229. return
  230. }
  231. templateMode.Mode = mode
  232. templateMode.ExecuteTimes = 0
  233. templateMode.ModifyTime = time.Now().Unix()
  234. updateErr := service.SavePatientScheduleTemplateMode2To1(templateMode, templateIDs[1].ID, firstWeekItems)
  235. if updateErr != nil {
  236. this.ErrorLog("更新排班模板启用方式(1 -> 2)时失败:%v", updateErr)
  237. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  238. return
  239. }
  240. } else if templateMode.Mode == 2 && mode == 3 { //
  241. templateMode.Mode = mode
  242. templateMode.ExecuteTimes = 0
  243. templateMode.ModifyTime = time.Now().Unix()
  244. updateErr := service.SaveTemplateMode(templateMode)
  245. if updateErr != nil {
  246. this.ErrorLog("更新排班模板启用方式(1 -> 4)时失败:%v", updateErr)
  247. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  248. return
  249. }
  250. } else if templateMode.Mode == 2 && mode == 4 { //
  251. templateMode.Mode = mode
  252. templateMode.ExecuteTimes = 0
  253. templateMode.ModifyTime = time.Now().Unix()
  254. updateErr := service.SaveTemplateMode(templateMode)
  255. if updateErr != nil {
  256. this.ErrorLog("更新排班模板启用方式(1 -> 4)时失败:%v", updateErr)
  257. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  258. return
  259. }
  260. } else if templateMode.Mode == 3 && mode == 1 { // 3 -> 1 清除第三,二周模板item,清除第三,二周排班
  261. templateIDs, getTemplateIDsErr := service.GetOrgPatientScheduleTemplateIDs(adminUserInfo.CurrentOrgId)
  262. if getTemplateIDsErr != nil {
  263. this.ErrorLog("获取排班模板失败:%v", getTemplateIDsErr)
  264. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  265. return
  266. }
  267. templateMode.Mode = mode
  268. templateMode.ExecuteTimes = 0
  269. templateMode.ModifyTime = time.Now().Unix()
  270. updateErr := service.SavePatientScheduleTemplateMode3To1(templateMode, templateIDs[1].ID, templateIDs[2].ID)
  271. if updateErr != nil {
  272. this.ErrorLog("更新排班模板启用方式(1 -> 3)时失败:%v", updateErr)
  273. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  274. return
  275. }
  276. } else if templateMode.Mode == 3 && mode == 2 { // 3 -> 1 清除第三周模板item,清除第三周排班,将第一周的排班重复到第二周
  277. templateIDs, getTemplateIDsErr := service.GetOrgPatientScheduleTemplateIDs(adminUserInfo.CurrentOrgId)
  278. if getTemplateIDsErr != nil {
  279. this.ErrorLog("获取排班模板失败:%v", getTemplateIDsErr)
  280. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  281. return
  282. }
  283. templateMode.Mode = mode
  284. templateMode.ExecuteTimes = 0
  285. templateMode.ModifyTime = time.Now().Unix()
  286. updateErr := service.SavePatientScheduleTemplateMode3To2(templateMode, templateIDs[2].ID)
  287. if updateErr != nil {
  288. this.ErrorLog("更新排班模板启用方式(2 -> 3)时失败:%v", updateErr)
  289. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  290. return
  291. }
  292. } else if templateMode.Mode == 3 && mode == 4 { // 2 -> 1 清除第二周模板item,清除第二周排班
  293. templateMode.Mode = mode
  294. templateMode.ExecuteTimes = 0
  295. templateMode.ModifyTime = time.Now().Unix()
  296. updateErr := service.SaveTemplateMode(templateMode)
  297. if updateErr != nil {
  298. this.ErrorLog("更新排班模板启用方式(1 -> 4)时失败:%v", updateErr)
  299. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  300. return
  301. }
  302. } else if templateMode.Mode == 4 && mode == 1 { // 4 -> 1 清除第三周模板item,清除第三周排班,将第一周的排班重复到第二周
  303. templateIDs, getTemplateIDsErr := service.GetOrgPatientScheduleTemplateIDs(adminUserInfo.CurrentOrgId)
  304. if getTemplateIDsErr != nil {
  305. this.ErrorLog("获取排班模板失败:%v", getTemplateIDsErr)
  306. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  307. return
  308. }
  309. templateMode.Mode = mode
  310. templateMode.ExecuteTimes = 0
  311. templateMode.ModifyTime = time.Now().Unix()
  312. updateErr := service.SavePatientScheduleTemplateMode4To1(templateMode, templateIDs[1].ID, templateIDs[2].ID, templateIDs[3].ID)
  313. if updateErr != nil {
  314. this.ErrorLog("更新排班模板启用方式(1 -> 4)时失败:%v", updateErr)
  315. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  316. return
  317. }
  318. } else if templateMode.Mode == 4 && mode == 2 { // 4 -> 2
  319. templateIDs, getTemplateIDsErr := service.GetOrgPatientScheduleTemplateIDs(adminUserInfo.CurrentOrgId)
  320. if getTemplateIDsErr != nil {
  321. this.ErrorLog("获取排班模板失败:%v", getTemplateIDsErr)
  322. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  323. return
  324. }
  325. templateMode.Mode = mode
  326. templateMode.ExecuteTimes = 0
  327. templateMode.ModifyTime = time.Now().Unix()
  328. updateErr := service.SavePatientScheduleTemplateMode4To2(templateMode, templateIDs[2].ID, templateIDs[3].ID)
  329. if updateErr != nil {
  330. this.ErrorLog("更新排班模板启用方式(2 -> 4)时失败:%v", updateErr)
  331. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  332. return
  333. }
  334. } else if templateMode.Mode == 4 && mode == 3 { // 4 -> 3
  335. templateIDs, getTemplateIDsErr := service.GetOrgPatientScheduleTemplateIDs(adminUserInfo.CurrentOrgId)
  336. if getTemplateIDsErr != nil {
  337. this.ErrorLog("获取排班模板失败:%v", getTemplateIDsErr)
  338. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  339. return
  340. }
  341. templateMode.Mode = mode
  342. templateMode.ExecuteTimes = 0
  343. templateMode.ModifyTime = time.Now().Unix()
  344. updateErr := service.SavePatientScheduleTemplateMode4To3(templateMode, templateIDs[3].ID)
  345. if updateErr != nil {
  346. this.ErrorLog("更新排班模板启用方式(3 -> 4)时失败:%v", updateErr)
  347. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  348. return
  349. }
  350. }
  351. templates, _ := service.GetOrgPatientScheduleTemplateIDs(adminUserInfo.CurrentOrgId)
  352. //处理第几周数问题
  353. if mode == 0 {
  354. service.UpdateTemplateItemWeek(templates[0].OrgID, templates[0].ID, 0)
  355. service.UpdateTemplateItemWeek(templates[1].OrgID, templates[1].ID, 0)
  356. service.UpdateTemplateItemWeek(templates[2].OrgID, templates[2].ID, 0)
  357. service.UpdateTemplateItemWeek(templates[3].OrgID, templates[3].ID, 0)
  358. } else if mode == 1 {
  359. service.UpdateTemplateItemWeek(templates[0].OrgID, templates[0].ID, week_time)
  360. service.UpdateTemplateItemWeek(templates[1].OrgID, templates[1].ID, 0)
  361. service.UpdateTemplateItemWeek(templates[2].OrgID, templates[2].ID, 0)
  362. service.UpdateTemplateItemWeek(templates[3].OrgID, templates[3].ID, 0)
  363. } else if mode == 2 {
  364. service.UpdateTemplateItemWeek(templates[0].OrgID, templates[0].ID, week_time)
  365. service.UpdateTemplateItemWeek(templates[1].OrgID, templates[1].ID, week_time+1)
  366. service.UpdateTemplateItemWeek(templates[2].OrgID, templates[2].ID, 0)
  367. service.UpdateTemplateItemWeek(templates[3].OrgID, templates[3].ID, 0)
  368. } else if mode == 3 {
  369. service.UpdateTemplateItemWeek(templates[0].OrgID, templates[0].ID, week_time)
  370. service.UpdateTemplateItemWeek(templates[1].OrgID, templates[1].ID, week_time+1)
  371. service.UpdateTemplateItemWeek(templates[2].OrgID, templates[2].ID, week_time+2)
  372. service.UpdateTemplateItemWeek(templates[3].OrgID, templates[3].ID, 0)
  373. } else if mode == 4 {
  374. service.UpdateTemplateItemWeek(templates[0].OrgID, templates[0].ID, week_time)
  375. service.UpdateTemplateItemWeek(templates[1].OrgID, templates[1].ID, week_time+1)
  376. service.UpdateTemplateItemWeek(templates[2].OrgID, templates[2].ID, week_time+2)
  377. service.UpdateTemplateItemWeek(templates[3].OrgID, templates[3].ID, week_time+3)
  378. }
  379. }
  380. this.ServeSuccessJSON(nil)
  381. }
  382. // /api/schtemp/p/update_sch [post]
  383. // @param template_id:int
  384. // @param add_schs:array'json ([ {device_number_id, patient_id, mode(treat_mode), weekday, time_type} ])
  385. // @param del_schs:array'json ([ {device_number_id, weekday, time_type} ])
  386. // @param change_schs:array'json ([ {device_number_id, patient_id, mode(treat_mode), weekday, time_type} ])
  387. func (this *PatientScheduleTemplateAPIController) UpdateSchedules() {
  388. templateID, _ := this.GetInt64("template_id")
  389. addSchedulesJSON := this.GetString("add_schs")
  390. delSchedulesJSON := this.GetString("del_schs")
  391. changeSchedulesJSON := this.GetString("change_schs")
  392. if len(addSchedulesJSON) == 0 || len(delSchedulesJSON) == 0 || len(changeSchedulesJSON) == 0 {
  393. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  394. return
  395. }
  396. var addSchedules []*ParamModelSchedule
  397. var delSchedules []*ParamModelSchedule
  398. var changeSchedules []*ParamModelSchedule
  399. parseErr := json.Unmarshal([]byte(addSchedulesJSON), &addSchedules)
  400. if parseErr != nil {
  401. this.ErrorLog("解析增加排班的数组参数失败:%v", parseErr)
  402. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  403. return
  404. }
  405. parseErr = json.Unmarshal([]byte(delSchedulesJSON), &delSchedules)
  406. if parseErr != nil {
  407. this.ErrorLog("解析删除排班的数组参数失败:%v", parseErr)
  408. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  409. return
  410. }
  411. parseErr = json.Unmarshal([]byte(changeSchedulesJSON), &changeSchedules)
  412. if parseErr != nil {
  413. this.ErrorLog("解析修改排班的数组参数失败:%v", parseErr)
  414. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  415. return
  416. }
  417. adminUserInfo := this.GetAdminUserInfo()
  418. templateIDs, getTemplateIDsErr := service.GetOrgPatientScheduleTemplateIDs(adminUserInfo.CurrentOrgId)
  419. if getTemplateIDsErr != nil {
  420. this.ErrorLog("获取排班模板失败:%v", getTemplateIDsErr)
  421. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  422. return
  423. } else {
  424. isTemplateIDExist := false
  425. for _, templateIDObject := range templateIDs {
  426. if templateIDObject.ID == templateID {
  427. isTemplateIDExist = true
  428. break
  429. }
  430. }
  431. if isTemplateIDExist == false {
  432. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeScheduleTemplateNotExist)
  433. return
  434. }
  435. }
  436. addTemplateItems := make([]*models.PatientScheduleTemplateItem, 0, len(addSchedules))
  437. delTemplateItems := make([]*models.PatientScheduleTemplateItem, 0, len(delSchedules))
  438. changeTemplateItems := make([]*models.PatientScheduleTemplateItem, 0, len(changeSchedules))
  439. for _, sch := range addSchedules {
  440. if sch.DeviceNumberID <= 0 || sch.PatientID <= 0 || sch.Weekday < 1 || sch.Weekday > 7 || sch.TimeType < 1 || sch.TimeType > 3 {
  441. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  442. return
  443. }
  444. addTemplateItems = append(addTemplateItems, &models.PatientScheduleTemplateItem{
  445. DeviceNumberID: sch.DeviceNumberID,
  446. PatientID: sch.PatientID,
  447. TreatMode: sch.Mode,
  448. Weekday: sch.Weekday,
  449. TimeType: sch.TimeType,
  450. })
  451. }
  452. for _, sch := range delSchedules {
  453. if sch.DeviceNumberID <= 0 || sch.Weekday < 1 || sch.Weekday > 7 || sch.TimeType < 1 || sch.TimeType > 3 {
  454. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  455. return
  456. }
  457. delTemplateItems = append(delTemplateItems, &models.PatientScheduleTemplateItem{
  458. DeviceNumberID: sch.DeviceNumberID,
  459. Weekday: sch.Weekday,
  460. TimeType: sch.TimeType,
  461. })
  462. }
  463. for _, sch := range changeSchedules {
  464. if sch.DeviceNumberID <= 0 || sch.PatientID <= 0 || sch.Weekday < 1 || sch.Weekday > 7 || sch.TimeType < 1 || sch.TimeType > 3 {
  465. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  466. return
  467. }
  468. changeTemplateItems = append(changeTemplateItems, &models.PatientScheduleTemplateItem{
  469. DeviceNumberID: sch.DeviceNumberID,
  470. PatientID: sch.PatientID,
  471. TreatMode: sch.Mode,
  472. Weekday: sch.Weekday,
  473. TimeType: sch.TimeType,
  474. })
  475. }
  476. templateMode, getTemplateModeErr := service.GetOrgPatientScheduleTemplateMode(adminUserInfo.CurrentOrgId)
  477. if getTemplateModeErr != nil {
  478. this.ErrorLog("获取排班模板启用模式失败:%v", getTemplateModeErr)
  479. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  480. return
  481. } else if templateMode == nil {
  482. this.ErrorLog("获取不到排班模板启用模式")
  483. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  484. return
  485. }
  486. //shouldUpdateNextWeekSchedules := false
  487. //shouldUpdateNextSecondWeekSchedules := false
  488. //shouldUpdateNextThirdWeekSchedules := false
  489. //shouldUpdateNextFourWeekSchedules := false
  490. //
  491. if templateMode.Mode == 0 {
  492. this.ErrorLog("当前未开启排班模板,所以更新模板的操作将被无视")
  493. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  494. return
  495. }
  496. //判断当前用的是模版几
  497. //templateIDIndex := 1
  498. //for index, templateIDObject := range templateIDs {
  499. // if templateIDObject.ID == templateID {
  500. // //templateIDIndex = index + 1
  501. // }
  502. //}
  503. //if templateMode.Mode == 1 {
  504. // if templateIDIndex == 1 {
  505. // shouldUpdateNextWeekSchedules = true
  506. // shouldUpdateNextSecondWeekSchedules = true
  507. // } else {
  508. // this.ErrorLog("当前开启的是单周模板,所以不会更新第二个模板")
  509. // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  510. // return
  511. // }
  512. //
  513. //} else if templateMode.Mode == 2 { // mode == 2 => 开启双周模板时,只需更新一周排班
  514. // if templateIDIndex == 1 {
  515. // if templateMode.ExecuteTimes%2 == 0 {
  516. // shouldUpdateNextWeekSchedules = true
  517. // shouldUpdateNextSecondWeekSchedules = false
  518. // } else {
  519. // shouldUpdateNextWeekSchedules = false
  520. // shouldUpdateNextSecondWeekSchedules = true
  521. // }
  522. //
  523. // } else {
  524. // if templateMode.ExecuteTimes%2 == 0 {
  525. // shouldUpdateNextWeekSchedules = false
  526. // shouldUpdateNextSecondWeekSchedules = true
  527. // } else {
  528. // shouldUpdateNextWeekSchedules = true
  529. // shouldUpdateNextSecondWeekSchedules = false
  530. // }
  531. //
  532. // }
  533. //} else if templateMode.Mode == 3 { // mode == 2 => 开启双周模板时,只需更新一周排班
  534. // if templateIDIndex == 1 { //当前修改的是第一个模版
  535. // if templateMode.ExecuteTimes%3 == 0 {
  536. // shouldUpdateNextWeekSchedules = true
  537. // shouldUpdateNextSecondWeekSchedules = false
  538. // shouldUpdateNextThirdWeekSchedules = false
  539. // } else if templateMode.ExecuteTimes%3 == 1 {
  540. // shouldUpdateNextWeekSchedules = false
  541. // shouldUpdateNextSecondWeekSchedules = true
  542. // shouldUpdateNextThirdWeekSchedules = false
  543. // } else {
  544. // shouldUpdateNextWeekSchedules = false
  545. // shouldUpdateNextSecondWeekSchedules = false
  546. // shouldUpdateNextThirdWeekSchedules = true
  547. // }
  548. // } else if templateIDIndex == 2 {
  549. // if templateMode.ExecuteTimes%2 == 0 {
  550. // shouldUpdateNextWeekSchedules = false
  551. // shouldUpdateNextSecondWeekSchedules = true
  552. // } else {
  553. // shouldUpdateNextWeekSchedules = true
  554. // shouldUpdateNextSecondWeekSchedules = false
  555. // }
  556. //
  557. // } else {
  558. //
  559. // }
  560. //}
  561. updateErr := service.UpdateScheduleTemplate(adminUserInfo.CurrentOrgId, templateID, addTemplateItems, delTemplateItems, changeTemplateItems, false, false)
  562. if updateErr != nil {
  563. this.ErrorLog("更新排班模板排班失败:%v", updateErr)
  564. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDBUpdate)
  565. return
  566. }
  567. newItems, getTemplateItemsErr := service.GetOrgPatientScheduleTemplateItemsByTemplateID(adminUserInfo.CurrentOrgId)
  568. if getTemplateItemsErr != nil {
  569. this.ErrorLog("获取排班模板排班失败:%v", getTemplateItemsErr)
  570. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  571. return
  572. }
  573. this.ServeSuccessJSON(map[string]interface{}{
  574. "items": newItems,
  575. })
  576. }
  577. type ParamModelSchedule struct {
  578. DeviceNumberID int64 `json:"device_number_id"`
  579. PatientID int64 `json:"patient_id"`
  580. Mode int64 `json:"mode"`
  581. Weekday int8 `json:"weekday"`
  582. TimeType int8 `json:"time_type"`
  583. }
  584. func (this *PatientScheduleTemplateAPIController) GetPatientList() {
  585. patients, err := service.GetSchTemplatePatientList(this.GetAdminUserInfo().CurrentOrgId)
  586. if err != nil {
  587. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  588. }
  589. this.ServeSuccessJSON(map[string]interface{}{
  590. "list": patients,
  591. })
  592. }