schedule_api_controller.go 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  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. "reflect"
  10. "strconv"
  11. "time"
  12. "github.com/astaxie/beego"
  13. )
  14. type ScheduleApiController struct {
  15. BaseAuthAPIController
  16. }
  17. func ScheduleApiRegistRouters() {
  18. beego.Router("/api/schedule/weekpanel", &ScheduleApiController{}, "Get:GetWeekPanels")
  19. beego.Router("/api/schedule/schedules", &ScheduleApiController{}, "Get:GetSchedules")
  20. beego.Router("/api/schedule/patients", &ScheduleApiController{}, "Get:GetPatients")
  21. beego.Router("/api/schedule/create", &ScheduleApiController{}, "Post:CreateSchedule")
  22. beego.Router("/api/schedule/delete", &ScheduleApiController{}, "Delete:DeleteSchedule")
  23. beego.Router("/api/schedule/change", &ScheduleApiController{}, "Put:ChangeSchedule")
  24. beego.Router("/api/schedule/urgentinit", &ScheduleApiController{}, "Get:UrgentScheduleData")
  25. beego.Router("/api/schedule/print/initdata", &ScheduleApiController{}, "get:PrintInitData")
  26. beego.Router("/api/schedule/search", &ScheduleApiController{}, "get:SearchSchedulePatients")
  27. beego.Router("/api/schedule/week", &ScheduleApiController{}, "get:GetWeekDaySchedule")
  28. }
  29. func (c *ScheduleApiController) GetWeekPanels() {
  30. data, _ := c.GetInt64("data", 1)
  31. adminInfo := c.GetAdminUserInfo()
  32. thisTime := time.Now()
  33. year, monthTime, day := thisTime.Date()
  34. month := int(monthTime)
  35. _, theWeek := thisTime.ISOWeek()
  36. weekDay := int(thisTime.Weekday())
  37. if weekDay == 0 {
  38. weekDay = 7
  39. }
  40. weekEnd := 7 - weekDay
  41. weekStart := weekEnd - 6
  42. weekDays := make([]string, 0)
  43. for index := weekStart; index <= weekEnd; index++ {
  44. theDay := thisTime.AddDate(0, 0, index)
  45. indexYear, indexMonthTime, indexDay := theDay.Date()
  46. indexMonth := int(indexMonthTime)
  47. indexWeek := strconv.Itoa(indexYear) + "." + strconv.Itoa(indexMonth) + "." + strconv.Itoa(indexDay)
  48. weekDays = append(weekDays, indexWeek)
  49. }
  50. returnData := map[string]interface{}{
  51. "year": year,
  52. "month": month,
  53. "day": day,
  54. "theWeek": theWeek,
  55. "weekDay": weekDay,
  56. "weekDays": weekDays,
  57. }
  58. if data == 1 {
  59. partitions, _ := service.GetSchedulePartitionPanel(adminInfo.CurrentOrgId)
  60. returnData["partitions"] = partitions
  61. }
  62. c.ServeSuccessJSON(returnData)
  63. return
  64. }
  65. func (c *ScheduleApiController) GetSchedules() {
  66. week, _ := c.GetInt64("weekTime", 0) //1:last, 2:this 3:next 4 nextTwo
  67. adminInfo := c.GetAdminUserInfo()
  68. thisTime := time.Now()
  69. today := thisTime.Format("2006-01-02")
  70. if week < 1 || week > 4 {
  71. week = 2
  72. }
  73. if week == 1 {
  74. thisTime = thisTime.AddDate(0, 0, -7)
  75. } else if week == 3 {
  76. thisTime = thisTime.AddDate(0, 0, 7)
  77. } else if week == 4 {
  78. thisTime = thisTime.AddDate(0, 0, 14)
  79. }
  80. weekDay := int(thisTime.Weekday())
  81. if weekDay == 0 {
  82. weekDay = 7
  83. }
  84. weekEnd := 7 - weekDay
  85. weekStart := weekEnd - 6
  86. weekTitle := make([]string, 0)
  87. days := make([]string, 0)
  88. for index := weekStart; index <= weekEnd; index++ {
  89. theDay := thisTime.AddDate(0, 0, index)
  90. indexYear, indexMonthTime, indexDay := theDay.Date()
  91. indexMonth := int(indexMonthTime)
  92. indexWeek := strconv.Itoa(indexYear) + "." + strconv.Itoa(indexMonth) + "." + strconv.Itoa(indexDay)
  93. weekTitle = append(weekTitle, indexWeek)
  94. days = append(days, theDay.Format("2006-01-02"))
  95. }
  96. weekStartDay := thisTime.AddDate(0, 0, weekStart)
  97. weekEndDay := thisTime.AddDate(0, 0, weekEnd)
  98. weekStartTime := weekStartDay.Format("2006-01-02") + " 00:00:00"
  99. weekEndTime := weekEndDay.Format("2006-01-02") + " 23:59:59"
  100. timeLayout := "2006-01-02 15:04:05"
  101. loc, _ := time.LoadLocation("Local")
  102. theStarTime, _ := time.ParseInLocation(timeLayout, weekStartTime, loc)
  103. theEndTime, _ := time.ParseInLocation(timeLayout, weekEndTime, loc)
  104. weekStartPoint := theStarTime.Unix()
  105. weekEndPoint := theEndTime.Unix()
  106. schdules, _ := service.GetWeekSchedule(adminInfo.CurrentOrgId, weekStartPoint, weekEndPoint)
  107. c.ServeSuccessJSON(map[string]interface{}{
  108. "days": days,
  109. "weekTitle": weekTitle,
  110. "schdules": schdules,
  111. "today": today,
  112. })
  113. return
  114. }
  115. func (c *ScheduleApiController) GetPatients() {
  116. keywords := c.GetString("keywords", "")
  117. schedule, _ := c.GetInt64("schedule", 0) //1已2未
  118. contagion, _ := c.GetInt64("contagion", 0)
  119. adminInfo := c.GetAdminUserInfo()
  120. thisTime := time.Now()
  121. weekDay := int(thisTime.Weekday())
  122. if weekDay == 0 {
  123. weekDay = 7
  124. }
  125. thisWeekEnd := 7 - weekDay
  126. weekStartPoint := thisWeekEnd - 6
  127. weekStartDay := thisTime.AddDate(0, 0, weekStartPoint)
  128. weekEndPoint := thisWeekEnd + 7
  129. weekEndDay := thisTime.AddDate(0, 0, weekEndPoint)
  130. fmt.Println(weekStartPoint, weekStartDay, weekEndPoint, weekEndDay)
  131. weekStartTime := weekStartDay.Format("2006-01-02") + " 00:00:00"
  132. weekEndTime := weekEndDay.Format("2006-01-02") + " 23:59:59"
  133. fmt.Println(weekStartTime, weekEndTime)
  134. timeLayout := "2006-01-02 15:04:05"
  135. loc, _ := time.LoadLocation("Local")
  136. theStarTime, _ := time.ParseInLocation(timeLayout, weekStartTime, loc)
  137. theEndTime, _ := time.ParseInLocation(timeLayout, weekEndTime, loc)
  138. weekStart := theStarTime.Unix()
  139. weekEnd := theEndTime.Unix()
  140. patients, _ := service.GetPatientWithScheduleAndSolution(adminInfo.CurrentOrgId, keywords, weekStart, weekEnd, schedule, contagion)
  141. c.ServeSuccessJSON(map[string]interface{}{
  142. "patients": patients,
  143. })
  144. return
  145. }
  146. func (c *ScheduleApiController) CreateSchedule() {
  147. patientID, _ := c.GetInt64("patient_id", 0)
  148. if patientID <= 0 {
  149. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  150. return
  151. }
  152. adminUserInfo := c.GetAdminUserInfo()
  153. patientInfo, _ := service.FindPatientById(adminUserInfo.CurrentOrgId, patientID)
  154. if patientInfo.ID == 0 {
  155. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePatientNoExist)
  156. return
  157. }
  158. var schedule models.Schedule
  159. dataBody := make(map[string]interface{}, 0)
  160. err := json.Unmarshal(c.Ctx.Input.RequestBody, &dataBody)
  161. if err != nil {
  162. utils.ErrorLog(err.Error())
  163. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  164. return
  165. }
  166. if dataBody["schedule_date"] == nil || reflect.TypeOf(dataBody["schedule_date"]).String() != "string" {
  167. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  168. return
  169. }
  170. scheduleDate, _ := dataBody["schedule_date"].(string)
  171. if len(scheduleDate) == 0 {
  172. utils.ErrorLog("len(schedule_date) == 0")
  173. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  174. return
  175. }
  176. timeLayout := "2006-01-02"
  177. loc, _ := time.LoadLocation("Local")
  178. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", scheduleDate+" 00:00:00", loc)
  179. if err != nil {
  180. utils.ErrorLog(err.Error())
  181. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  182. return
  183. }
  184. schedule.ScheduleDate = theTime.Unix()
  185. timeNow := time.Now().Format("2006-01-02")
  186. if timeNow > scheduleDate {
  187. utils.ErrorLog(timeNow)
  188. utils.ErrorLog(scheduleDate)
  189. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCantSetScheduleBeforeNow)
  190. return
  191. }
  192. if dataBody["schedule_type"] == nil || reflect.TypeOf(dataBody["schedule_type"]).String() != "float64" {
  193. utils.ErrorLog("schedule_type")
  194. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  195. return
  196. }
  197. scheduleType := int64(dataBody["schedule_type"].(float64))
  198. if scheduleType < 1 || scheduleType > 3 {
  199. utils.ErrorLog("scheduleType < 3")
  200. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  201. return
  202. }
  203. schedule.ScheduleType = scheduleType
  204. if dataBody["bed_id"] == nil || reflect.TypeOf(dataBody["bed_id"]).String() != "float64" {
  205. utils.ErrorLog("bed_id")
  206. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  207. return
  208. }
  209. bedId := int64(dataBody["bed_id"].(float64))
  210. if bedId < 1 {
  211. utils.ErrorLog("bedId < 1")
  212. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  213. return
  214. }
  215. schedule.BedId = bedId
  216. if dataBody["partition_id"] == nil || reflect.TypeOf(dataBody["partition_id"]).String() != "float64" {
  217. utils.ErrorLog("partition_id")
  218. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  219. return
  220. }
  221. partitionId := int64(dataBody["partition_id"].(float64))
  222. if partitionId < 1 {
  223. utils.ErrorLog("partitionId < 1")
  224. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  225. return
  226. }
  227. schedule.PartitionId = partitionId
  228. if dataBody["schedule_week"] == nil || reflect.TypeOf(dataBody["schedule_week"]).String() != "float64" {
  229. utils.ErrorLog("schedule_week")
  230. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  231. return
  232. }
  233. scheduleWeek := int64(dataBody["schedule_week"].(float64))
  234. if scheduleWeek < 1 || scheduleWeek > 7 {
  235. utils.ErrorLog("scheduleWeek < 1")
  236. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  237. return
  238. }
  239. schedule.ScheduleWeek = scheduleWeek
  240. if dataBody["mode_id"] == nil || reflect.TypeOf(dataBody["mode_id"]).String() != "float64" {
  241. utils.ErrorLog("mode_id")
  242. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  243. return
  244. }
  245. modeId := int64(dataBody["mode_id"].(float64))
  246. if modeId < 1 && modeId > 14 {
  247. utils.ErrorLog("modeId < 1")
  248. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  249. return
  250. }
  251. schedule.ModeId = modeId
  252. schedule.PatientId = patientID
  253. schedule.CreatedTime = time.Now().Unix()
  254. schedule.UpdatedTime = time.Now().Unix()
  255. schedule.Status = 1
  256. schedule.UserOrgId = adminUserInfo.CurrentOrgId
  257. bed, _ := service.GetDeviceNumberByID(adminUserInfo.CurrentOrgId, schedule.BedId)
  258. if bed == nil {
  259. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDeviceNumberNotExist)
  260. return
  261. }
  262. if bed.ZoneID != schedule.PartitionId {
  263. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDeviceNumberNotTheZone)
  264. return
  265. }
  266. scheduleDateStart := scheduleDate + " 00:00:00"
  267. scheduleDateEnd := scheduleDate + " 23:59:59"
  268. timeLayout = "2006-01-02 15:04:05"
  269. theStartTime, _ := time.ParseInLocation(timeLayout, scheduleDateStart, loc)
  270. theEndTime, _ := time.ParseInLocation(timeLayout, scheduleDateEnd, loc)
  271. startTime := theStartTime.Unix()
  272. endTime := theEndTime.Unix()
  273. //一天只有排班一次
  274. daySchedule, err := service.GetDaySchedule(adminUserInfo.CurrentOrgId, startTime, endTime, patientID)
  275. if daySchedule.ID > 0 {
  276. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCantSetScheduleAgainOneDay)
  277. return
  278. }
  279. //同天同位置只能排一个
  280. pointSchedule, err := service.GetPointSchedule(adminUserInfo.CurrentOrgId, schedule.ScheduleDate, schedule.ScheduleWeek, schedule.ScheduleType, schedule.BedId)
  281. if err != nil {
  282. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  283. return
  284. }
  285. if pointSchedule.ID > 0 {
  286. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePointScheduleExist)
  287. return
  288. }
  289. err = service.CreateSchedule(&schedule)
  290. fmt.Println(err)
  291. if err != nil {
  292. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCreateScheduleFail)
  293. return
  294. }
  295. schedule.Patient = patientInfo.Name
  296. c.ServeSuccessJSON(map[string]interface{}{
  297. "msg": "ok",
  298. "schedule": schedule,
  299. })
  300. return
  301. }
  302. func (c *ScheduleApiController) DeleteSchedule() {
  303. id, _ := c.GetInt64("id", 0)
  304. if id <= 0 {
  305. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  306. return
  307. }
  308. adminINfo := c.GetAdminUserInfo()
  309. schedule, _ := service.GetSchedule(adminINfo.CurrentOrgId, id)
  310. if schedule == nil {
  311. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeScheduleNotExist)
  312. return
  313. }
  314. order, err := service.GetOneDialysisOrder(adminINfo.CurrentOrgId, schedule.ScheduleDate, schedule.PatientId)
  315. if err != nil {
  316. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  317. return
  318. }
  319. if order != nil {
  320. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDelScheduleFailByDialysis)
  321. return
  322. }
  323. schedule.Status = 0
  324. schedule.UpdatedTime = time.Now().Unix()
  325. err = service.UpdateSchedule(schedule)
  326. if err != nil {
  327. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDeleteScheduleFail)
  328. return
  329. }
  330. c.ServeSuccessJSON(map[string]interface{}{
  331. "msg": "ok",
  332. "schedule": &schedule,
  333. })
  334. }
  335. func (c *ScheduleApiController) ChangeSchedule() {
  336. id, _ := c.GetInt64("id", 0)
  337. if id <= 0 {
  338. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  339. return
  340. }
  341. adminINfo := c.GetAdminUserInfo()
  342. schedule, _ := service.GetSchedule(adminINfo.CurrentOrgId, id)
  343. if schedule == nil {
  344. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeScheduleNotExist)
  345. return
  346. }
  347. dataBody := make(map[string]interface{}, 0)
  348. err := json.Unmarshal(c.Ctx.Input.RequestBody, &dataBody)
  349. if err != nil {
  350. utils.ErrorLog(err.Error())
  351. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  352. return
  353. }
  354. if dataBody["change_action"] == nil || reflect.TypeOf(dataBody["change_action"]).String() != "string" {
  355. utils.ErrorLog("change_action")
  356. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  357. return
  358. }
  359. changeAction := dataBody["change_action"].(string)
  360. if changeAction == "change_mode" {
  361. if dataBody["mode_id"] == nil || reflect.TypeOf(dataBody["mode_id"]).String() != "float64" {
  362. utils.ErrorLog("mode_id")
  363. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  364. return
  365. }
  366. modeId := int64(dataBody["mode_id"].(float64))
  367. if modeId < 1 {
  368. utils.ErrorLog("modeId < 1")
  369. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  370. return
  371. }
  372. schedule.ModeId = modeId
  373. order, err := service.GetOneDialysisOrder(adminINfo.CurrentOrgId, schedule.ScheduleDate, schedule.PatientId)
  374. if err != nil {
  375. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  376. return
  377. }
  378. if order != nil {
  379. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeChangeMode)
  380. return
  381. }
  382. } else if changeAction == "change_device" {
  383. order, err := service.GetOneDialysisOrder(adminINfo.CurrentOrgId, schedule.ScheduleDate, schedule.PatientId)
  384. if err != nil {
  385. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  386. return
  387. }
  388. if order != nil {
  389. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeChangeDeviceNumber)
  390. return
  391. }
  392. if dataBody["schedule_type"] == nil || reflect.TypeOf(dataBody["schedule_type"]).String() != "float64" {
  393. utils.ErrorLog("schedule_type")
  394. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  395. return
  396. }
  397. scheduleType := int64(dataBody["schedule_type"].(float64))
  398. if scheduleType < 1 || scheduleType > 3 {
  399. utils.ErrorLog("scheduleType < 3")
  400. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  401. return
  402. }
  403. schedule.ScheduleType = scheduleType
  404. if dataBody["bed_id"] == nil || reflect.TypeOf(dataBody["bed_id"]).String() != "float64" {
  405. utils.ErrorLog("bed_id")
  406. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  407. return
  408. }
  409. bedId := int64(dataBody["bed_id"].(float64))
  410. if bedId < 1 {
  411. utils.ErrorLog("bedId < 1")
  412. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  413. return
  414. }
  415. schedule.BedId = bedId
  416. if dataBody["partition_id"] == nil || reflect.TypeOf(dataBody["partition_id"]).String() != "float64" {
  417. utils.ErrorLog("partition_id")
  418. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  419. return
  420. }
  421. partitionId := int64(dataBody["partition_id"].(float64))
  422. if partitionId < 1 {
  423. utils.ErrorLog("partitionId < 1")
  424. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  425. return
  426. }
  427. schedule.PartitionId = partitionId
  428. bed, _ := service.GetDeviceNumberByID(adminINfo.CurrentOrgId, schedule.BedId)
  429. if bed == nil {
  430. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDeviceNumberNotExist)
  431. return
  432. }
  433. if bed.ZoneID != schedule.PartitionId {
  434. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDeviceNumberNotTheZone)
  435. return
  436. }
  437. startTime := schedule.ScheduleDate
  438. endTime := startTime + 86399
  439. //一天只有排班一次
  440. daySchedule, err := service.GetDaySchedule(adminINfo.CurrentOrgId, startTime, endTime, schedule.PatientId)
  441. if daySchedule.ID > 0 && daySchedule.ID != schedule.ID {
  442. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCantSetScheduleAgainOneDay)
  443. return
  444. }
  445. //同天同位置只能排一个
  446. pointSchedule, err := service.GetPointSchedule(adminINfo.CurrentOrgId, schedule.ScheduleDate, schedule.ScheduleWeek, schedule.ScheduleType, schedule.BedId)
  447. if err != nil {
  448. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  449. return
  450. }
  451. if pointSchedule.ID > 0 {
  452. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePointScheduleExist)
  453. return
  454. }
  455. } else {
  456. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  457. return
  458. }
  459. schedule.UpdatedTime = time.Now().Unix()
  460. err = service.UpdateSchedule(schedule)
  461. if err != nil {
  462. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeChangeScheduleFail)
  463. return
  464. }
  465. c.ServeSuccessJSON(map[string]interface{}{
  466. "msg": "ok",
  467. "schedule": &schedule,
  468. })
  469. }
  470. // /api/schedule/print/initdata [get]
  471. // @param date:string yyyy-MM-dd 要打印的那一周中的任一天
  472. func (this *ScheduleApiController) PrintInitData() {
  473. dateStr := this.GetString("date")
  474. var date *time.Time
  475. if len(dateStr) == 0 {
  476. now := time.Now()
  477. date = &now
  478. } else {
  479. var parseErr error
  480. date, parseErr = utils.ParseTimeStringToTime("2006-01-02", dateStr)
  481. if parseErr != nil {
  482. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  483. return
  484. }
  485. }
  486. adminUserInfo := this.GetAdminUserInfo()
  487. // 获取本周的排班
  488. monday, sunday := utils.GetMondayAndSundayOfWeekDate(date)
  489. schedules, getScheduleErr := service.GetPrinitWeekSchedules(adminUserInfo.CurrentOrgId, monday.Unix(), sunday.Unix())
  490. if getScheduleErr != nil {
  491. this.ErrorLog("获取周排班失败:%v", getScheduleErr)
  492. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  493. return
  494. }
  495. this.ServeSuccessJSON(map[string]interface{}{
  496. "schedules": schedules,
  497. "monday": monday.Unix(),
  498. })
  499. }
  500. func (this *ScheduleApiController) UrgentScheduleData() {
  501. schedule_type, _ := this.GetInt("type", 0)
  502. schedule_date := this.GetString("date")
  503. var date *time.Time
  504. if len(schedule_date) == 0 {
  505. now := time.Now()
  506. date = &now
  507. } else {
  508. var parseErr error
  509. date, parseErr = utils.ParseTimeStringToTime("2006-01-02", schedule_date)
  510. if parseErr != nil {
  511. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  512. return
  513. }
  514. }
  515. adminUserInfo := this.GetAdminUserInfo()
  516. deviceNumbers, getDeviceNumbersErr := service.MobileGetAllDeviceNumbersForUrgentSchedule(adminUserInfo.CurrentOrgId, date.Unix(), schedule_type)
  517. if getDeviceNumbersErr != nil {
  518. this.ErrorLog("获取所有床位失败:%v", getDeviceNumbersErr)
  519. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  520. return
  521. }
  522. // today := utils.ZeroHourTimeOfDay(time.Now())
  523. schedules, getSchedulesErr := service.MobileGetOtherSchedulesForUrgentSchedule(adminUserInfo.CurrentOrgId, date.Unix(), schedule_type)
  524. if getSchedulesErr != nil {
  525. this.ErrorLog("获取所有排班失败:%v", getSchedulesErr)
  526. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  527. return
  528. }
  529. this.ServeSuccessJSON(map[string]interface{}{
  530. "device_numbers": deviceNumbers,
  531. "schedules": schedules,
  532. })
  533. }
  534. func (this *ScheduleApiController) SearchSchedulePatients() {
  535. keywords := this.GetString("keywords")
  536. adminUserInfo := this.GetAdminUserInfo()
  537. list, _ := service.GetSchedualPatientsByKeywords(keywords, adminUserInfo.CurrentOrgId)
  538. this.ServeSuccessJSON(map[string]interface{}{
  539. "schdules": list,
  540. })
  541. }
  542. type aa struct {
  543. b int64
  544. c int64
  545. }
  546. func (this *ScheduleApiController) GetWeekDaySchedule() {
  547. week_type, _ := this.GetInt64("week_type", -1)
  548. fmt.Println(week_type)
  549. thisTime := time.Now()
  550. weekDay := int(thisTime.Weekday())
  551. if weekDay == 0 {
  552. weekDay = 7
  553. }
  554. weekEnd := 7 - weekDay
  555. weekStart := weekEnd - 6
  556. weekTitle := make([]string, 0)
  557. days := make([]string, 0)
  558. for index := weekStart; index <= weekEnd; index++ {
  559. theDay := thisTime.AddDate(0, 0, index)
  560. indexYear, indexMonthTime, indexDay := theDay.Date()
  561. indexMonth := int(indexMonthTime)
  562. indexWeek := strconv.Itoa(indexYear) + "." + strconv.Itoa(indexMonth) + "." + strconv.Itoa(indexDay)
  563. weekTitle = append(weekTitle, indexWeek)
  564. days = append(days, theDay.Format("2006-01-02"))
  565. }
  566. fmt.Println(days)
  567. var targetDayStr string
  568. switch week_type {
  569. case 1:
  570. targetDayStr = days[0]
  571. break
  572. case 2:
  573. targetDayStr = days[1]
  574. break
  575. case 3:
  576. targetDayStr = days[2]
  577. break
  578. case 4:
  579. targetDayStr = days[3]
  580. break
  581. case 5:
  582. targetDayStr = days[4]
  583. break
  584. case 6:
  585. targetDayStr = days[5]
  586. break
  587. case 7:
  588. targetDayStr = days[6]
  589. break
  590. }
  591. targetDay, parseErr := utils.ParseTimeStringToTime("2006-01-02", targetDayStr)
  592. if parseErr != nil {
  593. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  594. return
  595. }
  596. adminUserInfo := this.GetAdminUserInfo()
  597. list, _ := service.GetWeekDaySchedule(adminUserInfo.CurrentOrgId, targetDay.Unix())
  598. this.ServeSuccessJSON(map[string]interface{}{
  599. "schdules": list,
  600. "day": targetDayStr,
  601. })
  602. }
  603. func Struct2Map(obj interface{}) map[string]interface{} {
  604. t := reflect.TypeOf(obj)
  605. v := reflect.ValueOf(obj)
  606. var data = make(map[string]interface{})
  607. for i := 0; i < t.NumField(); i++ {
  608. data[t.Field(i).Name] = v.Field(i).Interface()
  609. }
  610. return data
  611. }