schedule_template_api_controller.go 30KB

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