schedule_api_controller.go 50KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648
  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. "github.com/jinzhu/gorm"
  10. "reflect"
  11. "strconv"
  12. "time"
  13. "github.com/astaxie/beego"
  14. )
  15. type ScheduleApiController struct {
  16. BaseAuthAPIController
  17. }
  18. func ScheduleApiRegistRouters() {
  19. beego.Router("/api/schedule/weekpanel", &ScheduleApiController{}, "Get:GetWeekPanels")
  20. beego.Router("/api/schedule/schedules", &ScheduleApiController{}, "Get:GetSchedules")
  21. beego.Router("/api/excel_date/init", &ScheduleApiController{}, "Get:GetInitExcelInitDate")
  22. beego.Router("/api/schedule/patients", &ScheduleApiController{}, "Get:GetPatients")
  23. beego.Router("/api/schedule/create", &ScheduleApiController{}, "Post:CreateSchedule")
  24. beego.Router("/api/schedule/delete", &ScheduleApiController{}, "Delete:DeleteSchedule")
  25. beego.Router("/api/schedule/change", &ScheduleApiController{}, "Put:ChangeSchedule")
  26. beego.Router("/api/schedule/urgentinit", &ScheduleApiController{}, "Get:UrgentScheduleData")
  27. beego.Router("/api/schedule/print/initdata", &ScheduleApiController{}, "get:PrintInitData")
  28. beego.Router("/api/schedule/search", &ScheduleApiController{}, "get:SearchSchedulePatients")
  29. beego.Router("/api/schedule/week", &ScheduleApiController{}, "get:GetWeekDaySchedule")
  30. beego.Router("/api/schedule/export", &ScheduleApiController{}, "post:ExportSchedule")
  31. beego.Router("/api/schedule_template/export", &ScheduleApiController{}, "post:ExportScheduleTemplate")
  32. }
  33. func (c *ScheduleApiController) GetWeekPanels() {
  34. data, _ := c.GetInt64("data", 1)
  35. adminInfo := c.GetAdminUserInfo()
  36. thisTime := time.Now()
  37. year, monthTime, day := thisTime.Date()
  38. month := int(monthTime)
  39. _, theWeek := thisTime.ISOWeek()
  40. weekDay := int(thisTime.Weekday())
  41. if weekDay == 0 {
  42. weekDay = 7
  43. }
  44. weekEnd := 7 - weekDay
  45. weekStart := weekEnd - 6
  46. weekDays := make([]string, 0)
  47. for index := weekStart; index <= weekEnd; index++ {
  48. theDay := thisTime.AddDate(0, 0, index)
  49. indexYear, indexMonthTime, indexDay := theDay.Date()
  50. indexMonth := int(indexMonthTime)
  51. indexWeek := strconv.Itoa(indexYear) + "." + strconv.Itoa(indexMonth) + "." + strconv.Itoa(indexDay)
  52. weekDays = append(weekDays, indexWeek)
  53. }
  54. returnData := map[string]interface{}{
  55. "year": year,
  56. "month": month,
  57. "day": day,
  58. "theWeek": theWeek,
  59. "weekDay": weekDay,
  60. "weekDays": weekDays,
  61. }
  62. if data == 1 {
  63. partitions, _ := service.GetSchedulePartitionPanel(adminInfo.CurrentOrgId)
  64. returnData["partitions"] = partitions
  65. }
  66. c.ServeSuccessJSON(returnData)
  67. return
  68. }
  69. func (c *ScheduleApiController) GetSchedules() {
  70. week, _ := c.GetInt64("weekTime", 0) //1:last, 2:this 3:next 4 nextTwo
  71. adminInfo := c.GetAdminUserInfo()
  72. thisTime := time.Now()
  73. today := thisTime.Format("2006-01-02")
  74. if week < 1 || week > 4 {
  75. week = 2
  76. }
  77. if week == 1 {
  78. thisTime = thisTime.AddDate(0, 0, -7)
  79. } else if week == 3 {
  80. thisTime = thisTime.AddDate(0, 0, 7)
  81. } else if week == 4 {
  82. thisTime = thisTime.AddDate(0, 0, 14)
  83. }
  84. weekDay := int(thisTime.Weekday())
  85. if weekDay == 0 {
  86. weekDay = 7
  87. }
  88. weekEnd := 7 - weekDay
  89. weekStart := weekEnd - 6
  90. weekTitle := make([]string, 0)
  91. days := make([]string, 0)
  92. for index := weekStart; index <= weekEnd; index++ {
  93. theDay := thisTime.AddDate(0, 0, index)
  94. indexYear, indexMonthTime, indexDay := theDay.Date()
  95. indexMonth := int(indexMonthTime)
  96. indexWeek := strconv.Itoa(indexYear) + "." + strconv.Itoa(indexMonth) + "." + strconv.Itoa(indexDay)
  97. weekTitle = append(weekTitle, indexWeek)
  98. days = append(days, theDay.Format("2006-01-02"))
  99. }
  100. weekStartDay := thisTime.AddDate(0, 0, weekStart)
  101. weekEndDay := thisTime.AddDate(0, 0, weekEnd)
  102. weekStartTime := weekStartDay.Format("2006-01-02") + " 00:00:00"
  103. weekEndTime := weekEndDay.Format("2006-01-02") + " 23:59:59"
  104. timeLayout := "2006-01-02 15:04:05"
  105. loc, _ := time.LoadLocation("Local")
  106. theStarTime, _ := time.ParseInLocation(timeLayout, weekStartTime, loc)
  107. theEndTime, _ := time.ParseInLocation(timeLayout, weekEndTime, loc)
  108. weekStartPoint := theStarTime.Unix()
  109. weekEndPoint := theEndTime.Unix()
  110. schdules, _ := service.GetWeekSchedule(adminInfo.CurrentOrgId, weekStartPoint, weekEndPoint)
  111. c.ServeSuccessJSON(map[string]interface{}{
  112. "days": days,
  113. "weekTitle": weekTitle,
  114. "schdules": schdules,
  115. "today": today,
  116. })
  117. return
  118. }
  119. func (c *ScheduleApiController) GetPatients() {
  120. keywords := c.GetString("keywords", "")
  121. schedule, _ := c.GetInt64("schedule", 0) //1已2未
  122. contagion, _ := c.GetInt64("contagion", 0)
  123. adminInfo := c.GetAdminUserInfo()
  124. thisTime := time.Now()
  125. weekDay := int(thisTime.Weekday())
  126. if weekDay == 0 {
  127. weekDay = 7
  128. }
  129. thisWeekEnd := 7 - weekDay
  130. weekStartPoint := thisWeekEnd - 6
  131. weekStartDay := thisTime.AddDate(0, 0, weekStartPoint)
  132. weekEndPoint := thisWeekEnd + 7
  133. weekEndDay := thisTime.AddDate(0, 0, weekEndPoint)
  134. fmt.Println(weekStartPoint, weekStartDay, weekEndPoint, weekEndDay)
  135. weekStartTime := weekStartDay.Format("2006-01-02") + " 00:00:00"
  136. weekEndTime := weekEndDay.Format("2006-01-02") + " 23:59:59"
  137. fmt.Println(weekStartTime, weekEndTime)
  138. timeLayout := "2006-01-02 15:04:05"
  139. loc, _ := time.LoadLocation("Local")
  140. theStarTime, _ := time.ParseInLocation(timeLayout, weekStartTime, loc)
  141. theEndTime, _ := time.ParseInLocation(timeLayout, weekEndTime, loc)
  142. weekStart := theStarTime.Unix()
  143. weekEnd := theEndTime.Unix()
  144. patients, _ := service.GetPatientWithScheduleAndSolution(adminInfo.CurrentOrgId, keywords, weekStart, weekEnd, schedule, contagion)
  145. c.ServeSuccessJSON(map[string]interface{}{
  146. "patients": patients,
  147. })
  148. return
  149. }
  150. func (c *ScheduleApiController) CreateSchedule() {
  151. patientID, _ := c.GetInt64("patient_id", 0)
  152. if patientID <= 0 {
  153. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  154. return
  155. }
  156. adminUserInfo := c.GetAdminUserInfo()
  157. patientInfo, _ := service.FindPatientById(adminUserInfo.CurrentOrgId, patientID)
  158. if patientInfo.ID == 0 {
  159. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePatientNoExist)
  160. return
  161. }
  162. var schedule models.Schedule
  163. dataBody := make(map[string]interface{}, 0)
  164. err := json.Unmarshal(c.Ctx.Input.RequestBody, &dataBody)
  165. if err != nil {
  166. utils.ErrorLog(err.Error())
  167. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  168. return
  169. }
  170. if dataBody["schedule_date"] == nil || reflect.TypeOf(dataBody["schedule_date"]).String() != "string" {
  171. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  172. return
  173. }
  174. scheduleDate, _ := dataBody["schedule_date"].(string)
  175. if len(scheduleDate) == 0 {
  176. utils.ErrorLog("len(schedule_date) == 0")
  177. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  178. return
  179. }
  180. timeLayout := "2006-01-02"
  181. loc, _ := time.LoadLocation("Local")
  182. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", scheduleDate+" 00:00:00", loc)
  183. if err != nil {
  184. utils.ErrorLog(err.Error())
  185. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  186. return
  187. }
  188. schedule.ScheduleDate = theTime.Unix()
  189. timeNow := time.Now().Format("2006-01-02")
  190. if timeNow > scheduleDate {
  191. utils.ErrorLog(timeNow)
  192. utils.ErrorLog(scheduleDate)
  193. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCantSetScheduleBeforeNow)
  194. return
  195. }
  196. if dataBody["schedule_type"] == nil || reflect.TypeOf(dataBody["schedule_type"]).String() != "float64" {
  197. utils.ErrorLog("schedule_type")
  198. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  199. return
  200. }
  201. scheduleType := int64(dataBody["schedule_type"].(float64))
  202. if scheduleType < 1 || scheduleType > 3 {
  203. utils.ErrorLog("scheduleType < 3")
  204. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  205. return
  206. }
  207. schedule.ScheduleType = scheduleType
  208. if dataBody["bed_id"] == nil || reflect.TypeOf(dataBody["bed_id"]).String() != "float64" {
  209. utils.ErrorLog("bed_id")
  210. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  211. return
  212. }
  213. bedId := int64(dataBody["bed_id"].(float64))
  214. if bedId < 1 {
  215. utils.ErrorLog("bedId < 1")
  216. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  217. return
  218. }
  219. schedule.BedId = bedId
  220. if dataBody["partition_id"] == nil || reflect.TypeOf(dataBody["partition_id"]).String() != "float64" {
  221. utils.ErrorLog("partition_id")
  222. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  223. return
  224. }
  225. partitionId := int64(dataBody["partition_id"].(float64))
  226. if partitionId < 1 {
  227. utils.ErrorLog("partitionId < 1")
  228. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  229. return
  230. }
  231. schedule.PartitionId = partitionId
  232. if dataBody["schedule_week"] == nil || reflect.TypeOf(dataBody["schedule_week"]).String() != "float64" {
  233. utils.ErrorLog("schedule_week")
  234. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  235. return
  236. }
  237. scheduleWeek := int64(dataBody["schedule_week"].(float64))
  238. if scheduleWeek < 1 || scheduleWeek > 7 {
  239. utils.ErrorLog("scheduleWeek < 1")
  240. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  241. return
  242. }
  243. schedule.ScheduleWeek = scheduleWeek
  244. if dataBody["mode_id"] == nil || reflect.TypeOf(dataBody["mode_id"]).String() != "float64" {
  245. utils.ErrorLog("mode_id")
  246. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  247. return
  248. }
  249. modeId := int64(dataBody["mode_id"].(float64))
  250. if modeId < 1 && modeId > 14 {
  251. utils.ErrorLog("modeId < 1")
  252. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  253. return
  254. }
  255. schedule.ModeId = modeId
  256. schedule.PatientId = patientID
  257. schedule.CreatedTime = time.Now().Unix()
  258. schedule.UpdatedTime = time.Now().Unix()
  259. schedule.Status = 1
  260. schedule.UserOrgId = adminUserInfo.CurrentOrgId
  261. bed, _ := service.GetDeviceNumberByID(adminUserInfo.CurrentOrgId, schedule.BedId)
  262. if bed == nil {
  263. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDeviceNumberNotExist)
  264. return
  265. }
  266. if bed.ZoneID != schedule.PartitionId {
  267. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDeviceNumberNotTheZone)
  268. return
  269. }
  270. scheduleDateStart := scheduleDate + " 00:00:00"
  271. scheduleDateEnd := scheduleDate + " 23:59:59"
  272. timeLayout = "2006-01-02 15:04:05"
  273. theStartTime, _ := time.ParseInLocation(timeLayout, scheduleDateStart, loc)
  274. theEndTime, _ := time.ParseInLocation(timeLayout, scheduleDateEnd, loc)
  275. startTime := theStartTime.Unix()
  276. endTime := theEndTime.Unix()
  277. //一天只有排班一次
  278. daySchedule, err := service.GetDaySchedule(adminUserInfo.CurrentOrgId, startTime, endTime, patientID)
  279. if daySchedule.ID > 0 {
  280. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCantSetScheduleAgainOneDay)
  281. return
  282. }
  283. //同天同位置只能排一个
  284. pointSchedule, err := service.GetPointSchedule(adminUserInfo.CurrentOrgId, schedule.ScheduleDate, schedule.ScheduleWeek, schedule.ScheduleType, schedule.BedId)
  285. if err != nil {
  286. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  287. return
  288. }
  289. if pointSchedule.ID > 0 {
  290. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePointScheduleExist)
  291. return
  292. }
  293. err = service.CreateSchedule(&schedule)
  294. fmt.Println(err)
  295. if err != nil {
  296. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCreateScheduleFail)
  297. return
  298. }
  299. schedule.Patient = patientInfo.Name
  300. c.ServeSuccessJSON(map[string]interface{}{
  301. "msg": "ok",
  302. "schedule": schedule,
  303. })
  304. return
  305. }
  306. func (c *ScheduleApiController) DeleteSchedule() {
  307. id, _ := c.GetInt64("id", 0)
  308. if id <= 0 {
  309. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  310. return
  311. }
  312. adminINfo := c.GetAdminUserInfo()
  313. schedule, _ := service.GetSchedule(adminINfo.CurrentOrgId, id)
  314. if schedule == nil {
  315. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeScheduleNotExist)
  316. return
  317. }
  318. order, err := service.GetOneDialysisOrder(adminINfo.CurrentOrgId, schedule.ScheduleDate, schedule.PatientId)
  319. if err != nil {
  320. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  321. return
  322. }
  323. if order != nil {
  324. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDelScheduleFailByDialysis)
  325. return
  326. }
  327. schedule.Status = 0
  328. schedule.UpdatedTime = time.Now().Unix()
  329. err = service.UpdateSchedule(schedule)
  330. if err != nil {
  331. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDeleteScheduleFail)
  332. return
  333. }
  334. c.ServeSuccessJSON(map[string]interface{}{
  335. "msg": "ok",
  336. "schedule": &schedule,
  337. })
  338. }
  339. func (c *ScheduleApiController) ChangeSchedule() {
  340. id, _ := c.GetInt64("id", 0)
  341. if id <= 0 {
  342. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  343. return
  344. }
  345. adminINfo := c.GetAdminUserInfo()
  346. schedule, _ := service.GetSchedule(adminINfo.CurrentOrgId, id)
  347. if schedule == nil {
  348. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeScheduleNotExist)
  349. return
  350. }
  351. dataBody := make(map[string]interface{}, 0)
  352. err := json.Unmarshal(c.Ctx.Input.RequestBody, &dataBody)
  353. if err != nil {
  354. utils.ErrorLog(err.Error())
  355. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  356. return
  357. }
  358. if dataBody["change_action"] == nil || reflect.TypeOf(dataBody["change_action"]).String() != "string" {
  359. utils.ErrorLog("change_action")
  360. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  361. return
  362. }
  363. changeAction := dataBody["change_action"].(string)
  364. if changeAction == "change_mode" {
  365. if dataBody["mode_id"] == nil || reflect.TypeOf(dataBody["mode_id"]).String() != "float64" {
  366. utils.ErrorLog("mode_id")
  367. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  368. return
  369. }
  370. modeId := int64(dataBody["mode_id"].(float64))
  371. if modeId < 1 {
  372. utils.ErrorLog("modeId < 1")
  373. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  374. return
  375. }
  376. schedule.ModeId = modeId
  377. order, err := service.GetOneDialysisOrder(adminINfo.CurrentOrgId, schedule.ScheduleDate, schedule.PatientId)
  378. if err != nil {
  379. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  380. return
  381. }
  382. if order != nil {
  383. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeChangeMode)
  384. return
  385. }
  386. } else if changeAction == "change_device" {
  387. order, err := service.GetOneDialysisOrder(adminINfo.CurrentOrgId, schedule.ScheduleDate, schedule.PatientId)
  388. if err != nil {
  389. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  390. return
  391. }
  392. if order != nil {
  393. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeChangeDeviceNumber)
  394. return
  395. }
  396. if dataBody["schedule_type"] == nil || reflect.TypeOf(dataBody["schedule_type"]).String() != "float64" {
  397. utils.ErrorLog("schedule_type")
  398. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  399. return
  400. }
  401. scheduleType := int64(dataBody["schedule_type"].(float64))
  402. if scheduleType < 1 || scheduleType > 3 {
  403. utils.ErrorLog("scheduleType < 3")
  404. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  405. return
  406. }
  407. schedule.ScheduleType = scheduleType
  408. if dataBody["bed_id"] == nil || reflect.TypeOf(dataBody["bed_id"]).String() != "float64" {
  409. utils.ErrorLog("bed_id")
  410. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  411. return
  412. }
  413. bedId := int64(dataBody["bed_id"].(float64))
  414. if bedId < 1 {
  415. utils.ErrorLog("bedId < 1")
  416. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  417. return
  418. }
  419. schedule.BedId = bedId
  420. if dataBody["partition_id"] == nil || reflect.TypeOf(dataBody["partition_id"]).String() != "float64" {
  421. utils.ErrorLog("partition_id")
  422. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  423. return
  424. }
  425. partitionId := int64(dataBody["partition_id"].(float64))
  426. if partitionId < 1 {
  427. utils.ErrorLog("partitionId < 1")
  428. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  429. return
  430. }
  431. schedule.PartitionId = partitionId
  432. bed, _ := service.GetDeviceNumberByID(adminINfo.CurrentOrgId, schedule.BedId)
  433. if bed == nil {
  434. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDeviceNumberNotExist)
  435. return
  436. }
  437. if bed.ZoneID != schedule.PartitionId {
  438. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDeviceNumberNotTheZone)
  439. return
  440. }
  441. startTime := schedule.ScheduleDate
  442. endTime := startTime + 86399
  443. //一天只有排班一次
  444. daySchedule, err := service.GetDaySchedule(adminINfo.CurrentOrgId, startTime, endTime, schedule.PatientId)
  445. if daySchedule.ID > 0 && daySchedule.ID != schedule.ID {
  446. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCantSetScheduleAgainOneDay)
  447. return
  448. }
  449. //同天同位置只能排一个
  450. pointSchedule, err := service.GetPointSchedule(adminINfo.CurrentOrgId, schedule.ScheduleDate, schedule.ScheduleWeek, schedule.ScheduleType, schedule.BedId)
  451. if err != nil {
  452. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  453. return
  454. }
  455. if pointSchedule.ID > 0 {
  456. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePointScheduleExist)
  457. return
  458. }
  459. } else {
  460. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  461. return
  462. }
  463. schedule.UpdatedTime = time.Now().Unix()
  464. err = service.UpdateSchedule(schedule)
  465. if err != nil {
  466. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeChangeScheduleFail)
  467. return
  468. }
  469. c.ServeSuccessJSON(map[string]interface{}{
  470. "msg": "ok",
  471. "schedule": &schedule,
  472. })
  473. }
  474. // /api/schedule/print/initdata [get]
  475. // @param date:string yyyy-MM-dd 要打印的那一周中的任一天
  476. func (this *ScheduleApiController) PrintInitData() {
  477. dateStr := this.GetString("date")
  478. var date *time.Time
  479. if len(dateStr) == 0 {
  480. now := time.Now()
  481. date = &now
  482. } else {
  483. var parseErr error
  484. date, parseErr = utils.ParseTimeStringToTime("2006-01-02", dateStr)
  485. if parseErr != nil {
  486. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  487. return
  488. }
  489. }
  490. adminUserInfo := this.GetAdminUserInfo()
  491. // 获取本周的排班
  492. monday, sunday := utils.GetMondayAndSundayOfWeekDate(date)
  493. schedules, getScheduleErr := service.GetPrinitWeekSchedules(adminUserInfo.CurrentOrgId, monday.Unix(), sunday.Unix())
  494. if getScheduleErr != nil {
  495. this.ErrorLog("获取周排班失败:%v", getScheduleErr)
  496. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  497. return
  498. }
  499. this.ServeSuccessJSON(map[string]interface{}{
  500. "schedules": schedules,
  501. "monday": monday.Unix(),
  502. })
  503. }
  504. func (this *ScheduleApiController) UrgentScheduleData() {
  505. schedule_type, _ := this.GetInt("type", 0)
  506. schedule_date := this.GetString("date")
  507. var date *time.Time
  508. if len(schedule_date) == 0 {
  509. now := time.Now()
  510. date = &now
  511. } else {
  512. var parseErr error
  513. date, parseErr = utils.ParseTimeStringToTime("2006-01-02", schedule_date)
  514. if parseErr != nil {
  515. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  516. return
  517. }
  518. }
  519. adminUserInfo := this.GetAdminUserInfo()
  520. deviceNumbers, getDeviceNumbersErr := service.MobileGetAllDeviceNumbersForUrgentSchedule(adminUserInfo.CurrentOrgId, date.Unix(), schedule_type)
  521. if getDeviceNumbersErr != nil {
  522. this.ErrorLog("获取所有床位失败:%v", getDeviceNumbersErr)
  523. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  524. return
  525. }
  526. // today := utils.ZeroHourTimeOfDay(time.Now())
  527. schedules, getSchedulesErr := service.MobileGetOtherSchedulesForUrgentSchedule(adminUserInfo.CurrentOrgId, date.Unix(), schedule_type)
  528. if getSchedulesErr != nil {
  529. this.ErrorLog("获取所有排班失败:%v", getSchedulesErr)
  530. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  531. return
  532. }
  533. this.ServeSuccessJSON(map[string]interface{}{
  534. "device_numbers": deviceNumbers,
  535. "schedules": schedules,
  536. })
  537. }
  538. func (this *ScheduleApiController) SearchSchedulePatients() {
  539. keywords := this.GetString("keywords")
  540. adminUserInfo := this.GetAdminUserInfo()
  541. list, _ := service.GetSchedualPatientsByKeywords(keywords, adminUserInfo.CurrentOrgId)
  542. this.ServeSuccessJSON(map[string]interface{}{
  543. "schdules": list,
  544. })
  545. }
  546. type aa struct {
  547. b int64
  548. c int64
  549. }
  550. func (this *ScheduleApiController) GetWeekDaySchedule() {
  551. week_type, _ := this.GetInt64("week_type", -1)
  552. fmt.Println(week_type)
  553. thisTime := time.Now()
  554. weekDay := int(thisTime.Weekday())
  555. if weekDay == 0 {
  556. weekDay = 7
  557. }
  558. weekEnd := 7 - weekDay
  559. weekStart := weekEnd - 6
  560. weekTitle := make([]string, 0)
  561. days := make([]string, 0)
  562. for index := weekStart; index <= weekEnd; index++ {
  563. theDay := thisTime.AddDate(0, 0, index)
  564. indexYear, indexMonthTime, indexDay := theDay.Date()
  565. indexMonth := int(indexMonthTime)
  566. indexWeek := strconv.Itoa(indexYear) + "." + strconv.Itoa(indexMonth) + "." + strconv.Itoa(indexDay)
  567. weekTitle = append(weekTitle, indexWeek)
  568. days = append(days, theDay.Format("2006-01-02"))
  569. }
  570. fmt.Println(days)
  571. var targetDayStr string
  572. switch week_type {
  573. case 1:
  574. targetDayStr = days[0]
  575. break
  576. case 2:
  577. targetDayStr = days[1]
  578. break
  579. case 3:
  580. targetDayStr = days[2]
  581. break
  582. case 4:
  583. targetDayStr = days[3]
  584. break
  585. case 5:
  586. targetDayStr = days[4]
  587. break
  588. case 6:
  589. targetDayStr = days[5]
  590. break
  591. case 7:
  592. targetDayStr = days[6]
  593. break
  594. }
  595. targetDay, parseErr := utils.ParseTimeStringToTime("2006-01-02", targetDayStr)
  596. if parseErr != nil {
  597. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  598. return
  599. }
  600. adminUserInfo := this.GetAdminUserInfo()
  601. list, _ := service.GetWeekDaySchedule(adminUserInfo.CurrentOrgId, targetDay.Unix(), targetDay)
  602. this.ServeSuccessJSON(map[string]interface{}{
  603. "schdules": list,
  604. "day": targetDayStr,
  605. })
  606. }
  607. func Struct2Map(obj interface{}) map[string]interface{} {
  608. t := reflect.TypeOf(obj)
  609. v := reflect.ValueOf(obj)
  610. var data = make(map[string]interface{})
  611. for i := 0; i < t.NumField(); i++ {
  612. data[t.Field(i).Name] = v.Field(i).Interface()
  613. }
  614. return data
  615. }
  616. func (this *ScheduleApiController) ExportSchedule() {
  617. dataBody := make(map[string]interface{}, 0)
  618. err := json.Unmarshal(this.Ctx.Input.RequestBody, &dataBody)
  619. if err != nil {
  620. utils.ErrorLog(err.Error())
  621. return
  622. }
  623. utils.ErrorLog("%v", dataBody)
  624. export_time := time.Now().Unix()
  625. var schedules []*models.Schedule
  626. var total_schedule []interface{}
  627. var patients []*models.Patients
  628. patients, _, _ = service.GetAllPatientList(this.GetAdminUserInfo().CurrentOrgId)
  629. if dataBody["schedule"] != nil || reflect.TypeOf(dataBody["schedule"]).String() == "[]interface {}" {
  630. tempSchedule := dataBody["schedule"].([]interface{})
  631. total_schedule = tempSchedule
  632. for _, schMap := range tempSchedule {
  633. schMapM := schMap.(map[string]interface{})
  634. var sch models.Schedule
  635. if schMapM["name"] == nil || reflect.TypeOf(schMapM["name"]).String() != "string" {
  636. utils.ErrorLog("name")
  637. return
  638. }
  639. name, _ := schMapM["name"].(string)
  640. fmt.Println(name)
  641. if len(name) == 0 { //名字为空则生成一条导入错误日志
  642. err_log := models.ExportErrLog{
  643. LogType: 2,
  644. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  645. ErrMsg: "第" + strconv.FormatInt(int64(schMapM["index"].(float64)), 10) + "行,第" + schMapM["row"].(string) + "列的姓名不能为空",
  646. Status: 1,
  647. CreateTime: time.Now().Unix(),
  648. UpdateTime: time.Now().Unix(),
  649. ExportTime: export_time,
  650. }
  651. service.CreateExportErrLog(&err_log)
  652. continue
  653. } else {
  654. var patient []*models.Patients
  655. for _, item := range patients {
  656. if item.Name == name {
  657. patient = append(patient, item)
  658. }
  659. }
  660. if len(patient) == 0 { //错误日志
  661. err_log := models.ExportErrLog{
  662. LogType: 2,
  663. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  664. ErrMsg: "第" + strconv.FormatInt(int64(schMapM["index"].(float64)), 10) + "行,第" + schMapM["row"].(string) + "列的姓名在系统中不存在,请在系统中添加该病人",
  665. Status: 1,
  666. CreateTime: time.Now().Unix(),
  667. UpdateTime: time.Now().Unix(),
  668. ExportTime: export_time,
  669. }
  670. service.CreateExportErrLog(&err_log)
  671. continue
  672. } else if len(patient) == 1 { //
  673. sch.PatientId = patient[0].ID
  674. } else if len(patient) > 2 { //出现同名的情况
  675. err_log := models.ExportErrLog{
  676. LogType: 2,
  677. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  678. ErrMsg: "存在同名同姓的两个病人",
  679. Status: 1,
  680. CreateTime: time.Now().Unix(),
  681. UpdateTime: time.Now().Unix(),
  682. ExportTime: export_time,
  683. }
  684. service.CreateExportErrLog(&err_log)
  685. continue
  686. }
  687. }
  688. if schMapM["schedule_type"] == nil && reflect.TypeOf(schMapM["schedule_type"]).String() != "float64" {
  689. utils.ErrorLog("schedule_type")
  690. return
  691. }
  692. schedule_type := int64(schMapM["schedule_type"].(float64))
  693. if schedule_type <= 0 { //班次格式有误,插入日志
  694. log := models.ExportLog{
  695. LogType: 2,
  696. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  697. TotalNum: int64(len(total_schedule)),
  698. FailNum: 1,
  699. SuccessNum: int64(len(schedules)),
  700. CreateTime: time.Now().Unix(),
  701. UpdateTime: time.Now().Unix(),
  702. ExportTime: export_time,
  703. Status: 1,
  704. }
  705. service.CreateExportLog(&log)
  706. err_log := models.ExportErrLog{
  707. LogType: 2,
  708. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  709. ErrMsg: "第2行第" + schMapM["row"].(string) + "列的班次格式有误",
  710. Status: 1,
  711. CreateTime: time.Now().Unix(),
  712. UpdateTime: time.Now().Unix(),
  713. ExportTime: export_time,
  714. }
  715. service.CreateExportErrLog(&err_log)
  716. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeExportError)
  717. return
  718. } else {
  719. sch.ScheduleType = schedule_type
  720. }
  721. if schMapM["partition_name"] == nil || reflect.TypeOf(schMapM["partition_name"]).String() != "string" {
  722. utils.ErrorLog("partition_name")
  723. return
  724. }
  725. partition_name, _ := schMapM["partition_name"].(string)
  726. if len(partition_name) == 0 { //分区为空
  727. log := models.ExportLog{
  728. LogType: 2,
  729. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  730. TotalNum: int64(len(total_schedule)),
  731. FailNum: 1,
  732. SuccessNum: int64(len(schedules)),
  733. CreateTime: time.Now().Unix(),
  734. UpdateTime: time.Now().Unix(),
  735. ExportTime: export_time,
  736. Status: 1,
  737. }
  738. service.CreateExportLog(&log)
  739. err_log := models.ExportErrLog{
  740. LogType: 2,
  741. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  742. //ErrMsg: "第" + strconv.FormatInt(int64(schMapM["index"].(float64)) ,10)+ "行" + "的姓名不能为空",
  743. ErrMsg: "第" + strconv.FormatInt(int64(schMapM["index"].(float64)), 10) + "行第1列所属的分区格式有误",
  744. Status: 1,
  745. CreateTime: time.Now().Unix(),
  746. UpdateTime: time.Now().Unix(),
  747. ExportTime: export_time,
  748. }
  749. service.CreateExportErrLog(&err_log)
  750. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeExportError)
  751. return
  752. } else {
  753. zone, err := service.FindDeviceZoneByName(partition_name, this.GetAdminUserInfo().CurrentOrgId)
  754. if err == nil {
  755. sch.PartitionId = zone.ID
  756. } else if err == gorm.ErrRecordNotFound { //查不到数据,插入错误日志
  757. log := models.ExportLog{
  758. LogType: 2,
  759. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  760. TotalNum: int64(len(total_schedule)),
  761. FailNum: 1,
  762. SuccessNum: int64(len(schedules)),
  763. CreateTime: time.Now().Unix(),
  764. UpdateTime: time.Now().Unix(),
  765. ExportTime: export_time,
  766. Status: 1,
  767. }
  768. service.CreateExportLog(&log)
  769. err_log := models.ExportErrLog{
  770. LogType: 2,
  771. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  772. //ErrMsg: "第" + strconv.FormatInt(int64(schMapM["index"].(float64)) ,10)+ "行" + "的姓名不能为空",
  773. ErrMsg: "第" + strconv.FormatInt(int64(schMapM["index"].(float64)), 10) + "行第1列所属的分区格式有误",
  774. Status: 1,
  775. CreateTime: time.Now().Unix(),
  776. UpdateTime: time.Now().Unix(),
  777. ExportTime: export_time,
  778. }
  779. service.CreateExportErrLog(&err_log)
  780. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeExportError)
  781. return
  782. }
  783. }
  784. if schMapM["device_number_name"] == nil || reflect.TypeOf(schMapM["device_number_name"]).String() != "string" {
  785. utils.ErrorLog("device_number_name")
  786. return
  787. }
  788. device_number_name, _ := schMapM["device_number_name"].(string)
  789. if len(device_number_name) == 0 { //分区为空
  790. log := models.ExportLog{
  791. LogType: 2,
  792. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  793. TotalNum: int64(len(total_schedule)),
  794. FailNum: 1,
  795. SuccessNum: int64(len(schedules)),
  796. CreateTime: time.Now().Unix(),
  797. UpdateTime: time.Now().Unix(),
  798. ExportTime: export_time,
  799. Status: 1,
  800. }
  801. service.CreateExportLog(&log)
  802. err_log := models.ExportErrLog{
  803. LogType: 2,
  804. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  805. //ErrMsg: "第" + strconv.FormatInt(int64(schMapM["index"].(float64)) ,10)+ "行" + "的姓名不能为空",
  806. ErrMsg: "第" + strconv.FormatInt(int64(schMapM["index"].(float64)), 10) + "行第2列机号格式有误",
  807. Status: 1,
  808. CreateTime: time.Now().Unix(),
  809. UpdateTime: time.Now().Unix(),
  810. ExportTime: export_time,
  811. }
  812. service.CreateExportErrLog(&err_log)
  813. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeExportError)
  814. return
  815. } else {
  816. device, err := service.FindDeviceByName(device_number_name, this.GetAdminUserInfo().CurrentOrgId, sch.PartitionId)
  817. if err == nil {
  818. if len(device) == 0 { //没查到数据,插入错误日志
  819. log := models.ExportLog{
  820. LogType: 2,
  821. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  822. TotalNum: int64(len(total_schedule)),
  823. FailNum: 1,
  824. SuccessNum: int64(len(schedules)),
  825. CreateTime: time.Now().Unix(),
  826. UpdateTime: time.Now().Unix(),
  827. ExportTime: export_time,
  828. Status: 1,
  829. }
  830. service.CreateExportLog(&log)
  831. err_log := models.ExportErrLog{
  832. LogType: 2,
  833. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  834. ErrMsg: "第" + strconv.FormatInt(int64(schMapM["index"].(float64)), 10) + "行第2列机号格式有误",
  835. Status: 1,
  836. CreateTime: time.Now().Unix(),
  837. UpdateTime: time.Now().Unix(),
  838. ExportTime: export_time,
  839. }
  840. service.CreateExportErrLog(&err_log)
  841. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeExportError)
  842. return
  843. } else if len(device) == 1 {
  844. sch.BedId = device[0].ID
  845. } else if len(device) > 1 { //出现重名,插入错误日志
  846. log := models.ExportLog{
  847. LogType: 2,
  848. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  849. TotalNum: int64(len(total_schedule)),
  850. FailNum: 1,
  851. SuccessNum: int64(len(schedules)),
  852. CreateTime: time.Now().Unix(),
  853. UpdateTime: time.Now().Unix(),
  854. ExportTime: export_time,
  855. Status: 1,
  856. }
  857. service.CreateExportLog(&log)
  858. err_log := models.ExportErrLog{
  859. LogType: 2,
  860. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  861. ErrMsg: "出现两个床位号相同的床位",
  862. Status: 1,
  863. CreateTime: time.Now().Unix(),
  864. UpdateTime: time.Now().Unix(),
  865. ExportTime: export_time,
  866. }
  867. service.CreateExportErrLog(&err_log)
  868. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeExportError)
  869. return
  870. }
  871. }
  872. }
  873. if schMapM["schedule_week"] == nil || reflect.TypeOf(schMapM["schedule_week"]).String() != "string" {
  874. utils.ErrorLog("schedule_week")
  875. return
  876. }
  877. schedule_week, _ := schMapM["schedule_week"].(string)
  878. if len(schedule_week) == 0 { //周几为空则生成一条导入错误日志
  879. log := models.ExportLog{
  880. LogType: 2,
  881. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  882. TotalNum: int64(len(total_schedule)),
  883. FailNum: 1,
  884. SuccessNum: int64(len(schedules)),
  885. CreateTime: time.Now().Unix(),
  886. UpdateTime: time.Now().Unix(),
  887. ExportTime: export_time,
  888. Status: 1,
  889. }
  890. service.CreateExportLog(&log)
  891. err_log := models.ExportErrLog{
  892. LogType: 2,
  893. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  894. //ErrMsg: "第" + strconv.FormatInt(int64(schMapM["index"].(float64)) ,10)+ "行" + "的姓名不能为空",
  895. ErrMsg: "第1行第" + schMapM["row"].(string) + "列的星期名格式有误",
  896. Status: 1,
  897. CreateTime: time.Now().Unix(),
  898. UpdateTime: time.Now().Unix(),
  899. ExportTime: export_time,
  900. }
  901. service.CreateExportErrLog(&err_log)
  902. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeExportError)
  903. return
  904. } else if schedule_week == "0" { //周几不符合格式则生成一条导入错误日志
  905. log := models.ExportLog{
  906. LogType: 2,
  907. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  908. TotalNum: int64(len(total_schedule)),
  909. FailNum: 1,
  910. SuccessNum: int64(len(schedules)),
  911. CreateTime: time.Now().Unix(),
  912. UpdateTime: time.Now().Unix(),
  913. ExportTime: export_time,
  914. Status: 1,
  915. }
  916. service.CreateExportLog(&log)
  917. err_log := models.ExportErrLog{
  918. LogType: 2,
  919. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  920. ErrMsg: "第1行第" + schMapM["row"].(string) + "列的星期名格式有误",
  921. Status: 1,
  922. CreateTime: time.Now().Unix(),
  923. UpdateTime: time.Now().Unix(),
  924. ExportTime: export_time,
  925. }
  926. service.CreateExportErrLog(&err_log)
  927. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeExportError)
  928. return
  929. } else {
  930. schedule_week, _ := strconv.ParseInt(schedule_week, 10, 64)
  931. sch.ScheduleWeek = schedule_week
  932. }
  933. if schMapM["schedule_date"] == nil || reflect.TypeOf(schMapM["schedule_date"]).String() != "string" {
  934. utils.ErrorLog("schedule_date")
  935. return
  936. }
  937. schedule_date, _ := schMapM["schedule_date"].(string)
  938. if len(schedule_date) == 0 { //周几为空则生成一条导入错误日志
  939. log := models.ExportLog{
  940. LogType: 2,
  941. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  942. TotalNum: int64(len(total_schedule)),
  943. FailNum: 1,
  944. SuccessNum: int64(len(schedules)),
  945. CreateTime: time.Now().Unix(),
  946. UpdateTime: time.Now().Unix(),
  947. ExportTime: export_time,
  948. Status: 1,
  949. }
  950. service.CreateExportLog(&log)
  951. err_log := models.ExportErrLog{
  952. LogType: 2,
  953. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  954. ErrMsg: "第1行第" + schMapM["row"].(string) + "列的星期名格式有误",
  955. Status: 1,
  956. CreateTime: time.Now().Unix(),
  957. UpdateTime: time.Now().Unix(),
  958. ExportTime: export_time,
  959. }
  960. service.CreateExportErrLog(&err_log)
  961. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeExportError)
  962. return
  963. } else if schedule_date == "0" { //周几不符合格式则生成一条导入错误日志
  964. log := models.ExportLog{
  965. LogType: 2,
  966. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  967. TotalNum: int64(len(total_schedule)),
  968. FailNum: 1,
  969. SuccessNum: int64(len(schedules)),
  970. CreateTime: time.Now().Unix(),
  971. UpdateTime: time.Now().Unix(),
  972. ExportTime: export_time,
  973. Status: 1,
  974. }
  975. service.CreateExportLog(&log)
  976. err_log := models.ExportErrLog{
  977. LogType: 2,
  978. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  979. ErrMsg: "第1行第" + schMapM["row"].(string) + "列的星期名格式有误",
  980. Status: 1,
  981. CreateTime: time.Now().Unix(),
  982. UpdateTime: time.Now().Unix(),
  983. ExportTime: export_time,
  984. }
  985. service.CreateExportErrLog(&err_log)
  986. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeExportError)
  987. return
  988. } else {
  989. date, _ := utils.ParseTimeStringToTime("2006-01-02", schedule_date)
  990. sch.ScheduleDate = date.Unix()
  991. }
  992. sch.CreatedTime = time.Now().Unix()
  993. sch.UpdatedTime = time.Now().Unix()
  994. sch.ModeId = 1
  995. sch.Status = 1
  996. sch.UserOrgId = this.GetAdminUserInfo().CurrentOrgId
  997. sch.IsExport = 1
  998. schedules = append(schedules, &sch)
  999. }
  1000. errLogs, _ := service.FindSchedualExportLog(this.GetAdminUserInfo().CurrentOrgId, export_time)
  1001. if len(schedules) > 0 {
  1002. schedule_date := this.GetString("date")
  1003. date, _ := utils.ParseTimeStringToTime("2006-01-02", schedule_date)
  1004. clear_schedule_date := date.Unix() //根据日期去清除,该日期以及未来的排班数据
  1005. err := service.UpdateScheduleStatus(clear_schedule_date, this.GetAdminUserInfo().CurrentOrgId)
  1006. if err == nil {
  1007. for _, item := range schedules {
  1008. service.CreateSchedule(item)
  1009. }
  1010. log := models.ExportLog{
  1011. LogType: 2,
  1012. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  1013. TotalNum: int64(len(total_schedule)),
  1014. FailNum: int64(len(errLogs)),
  1015. SuccessNum: int64(len(schedules)),
  1016. CreateTime: time.Now().Unix(),
  1017. UpdateTime: time.Now().Unix(),
  1018. ExportTime: export_time,
  1019. Status: 1,
  1020. }
  1021. service.CreateExportLog(&log)
  1022. this.ServeSuccessJSON(map[string]interface{}{
  1023. "msg": "导入成功",
  1024. "total_num": len(total_schedule),
  1025. "success_num": len(schedules),
  1026. "fail_num": int64(len(errLogs)),
  1027. })
  1028. } else {
  1029. log := models.ExportLog{
  1030. LogType: 2,
  1031. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  1032. TotalNum: int64(len(total_schedule)),
  1033. FailNum: int64(len(errLogs)),
  1034. SuccessNum: int64(len(schedules)),
  1035. CreateTime: time.Now().Unix(),
  1036. UpdateTime: time.Now().Unix(),
  1037. ExportTime: export_time,
  1038. Status: 1,
  1039. }
  1040. service.CreateExportLog(&log)
  1041. this.ServeSuccessJSON(map[string]interface{}{
  1042. "msg": "导入成功",
  1043. "total_num": len(total_schedule),
  1044. "success_num": len(schedules),
  1045. "fail_num": int64(len(errLogs)),
  1046. })
  1047. }
  1048. } else {
  1049. log := models.ExportLog{
  1050. LogType: 2,
  1051. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  1052. TotalNum: int64(len(total_schedule)),
  1053. FailNum: int64(len(errLogs)),
  1054. SuccessNum: int64(len(schedules)),
  1055. CreateTime: time.Now().Unix(),
  1056. UpdateTime: time.Now().Unix(),
  1057. ExportTime: export_time,
  1058. Status: 1,
  1059. }
  1060. service.CreateExportLog(&log)
  1061. this.ServeSuccessJSON(map[string]interface{}{
  1062. "msg": "导入成功",
  1063. "total_num": len(total_schedule),
  1064. "success_num": len(schedules),
  1065. "fail_num": int64(len(errLogs)),
  1066. })
  1067. }
  1068. }
  1069. }
  1070. func (this *ScheduleApiController) ExportScheduleTemplate() {
  1071. dataBody := make(map[string]interface{}, 0)
  1072. err := json.Unmarshal(this.Ctx.Input.RequestBody, &dataBody)
  1073. if err != nil {
  1074. utils.ErrorLog(err.Error())
  1075. return
  1076. }
  1077. utils.ErrorLog("%v", dataBody)
  1078. export_time := time.Now().Unix()
  1079. var schedules []*models.PatientScheduleTemplateItem
  1080. var total_schedule []interface{}
  1081. patients, _, _ := service.GetAllPatientList(this.GetAdminUserInfo().CurrentOrgId)
  1082. if dataBody["schedule"] != nil || reflect.TypeOf(dataBody["schedule"]).String() == "[]interface {}" {
  1083. tempSchedule := dataBody["schedule"].([]interface{})
  1084. total_schedule = tempSchedule
  1085. for _, schMap := range tempSchedule {
  1086. schMapM := schMap.(map[string]interface{})
  1087. var sch models.PatientScheduleTemplateItem
  1088. if schMapM["name"] == nil || reflect.TypeOf(schMapM["name"]).String() != "string" {
  1089. utils.ErrorLog("name")
  1090. return
  1091. }
  1092. name, _ := schMapM["name"].(string)
  1093. fmt.Println(name)
  1094. if len(name) == 0 { //名字为空则生成一条导入错误日志
  1095. err_log := models.ExportErrLog{
  1096. LogType: 3,
  1097. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  1098. ErrMsg: "第" + strconv.FormatInt(int64(schMapM["index"].(float64)), 10) + "行,第" + schMapM["row"].(string) + "列的姓名不能为空",
  1099. Status: 1,
  1100. CreateTime: time.Now().Unix(),
  1101. UpdateTime: time.Now().Unix(),
  1102. ExportTime: export_time,
  1103. }
  1104. service.CreateExportErrLog(&err_log)
  1105. continue
  1106. } else {
  1107. var patient []*models.Patients
  1108. for _, item := range patients {
  1109. if item.Name == name {
  1110. patient = append(patient, item)
  1111. }
  1112. }
  1113. if len(patient) == 0 { //错误日志
  1114. err_log := models.ExportErrLog{
  1115. LogType: 3,
  1116. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  1117. ErrMsg: "第" + strconv.FormatInt(int64(schMapM["index"].(float64)), 10) + "行,第" + schMapM["row"].(string) + "列的姓名在系统中不存在,请在系统中添加该病人",
  1118. Status: 1,
  1119. CreateTime: time.Now().Unix(),
  1120. UpdateTime: time.Now().Unix(),
  1121. ExportTime: export_time,
  1122. }
  1123. service.CreateExportErrLog(&err_log)
  1124. continue
  1125. } else if len(patient) == 1 { //
  1126. sch.PatientID = patient[0].ID
  1127. } else if len(patient) > 2 { //出现同名的情况
  1128. err_log := models.ExportErrLog{
  1129. LogType: 3,
  1130. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  1131. ErrMsg: "存在同名同姓的两个病人",
  1132. Status: 1,
  1133. CreateTime: time.Now().Unix(),
  1134. UpdateTime: time.Now().Unix(),
  1135. ExportTime: export_time,
  1136. }
  1137. service.CreateExportErrLog(&err_log)
  1138. continue
  1139. }
  1140. }
  1141. if schMapM["schedule_type"] == nil && reflect.TypeOf(schMapM["schedule_type"]).String() != "float64" {
  1142. utils.ErrorLog("schedule_type")
  1143. return
  1144. }
  1145. schedule_type := int64(schMapM["schedule_type"].(float64))
  1146. if schedule_type <= 0 { //班次格式有误,插入日志
  1147. log := models.ExportLog{
  1148. LogType: 3,
  1149. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  1150. TotalNum: int64(len(total_schedule)),
  1151. FailNum: 1,
  1152. SuccessNum: int64(len(schedules)),
  1153. CreateTime: time.Now().Unix(),
  1154. UpdateTime: time.Now().Unix(),
  1155. ExportTime: export_time,
  1156. Status: 1,
  1157. }
  1158. service.CreateExportLog(&log)
  1159. err_log := models.ExportErrLog{
  1160. LogType: 3,
  1161. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  1162. ErrMsg: "第2行第" + schMapM["row"].(string) + "列的班次格式有误",
  1163. Status: 1,
  1164. CreateTime: time.Now().Unix(),
  1165. UpdateTime: time.Now().Unix(),
  1166. ExportTime: export_time,
  1167. }
  1168. service.CreateExportErrLog(&err_log)
  1169. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeExportError)
  1170. return
  1171. } else {
  1172. sch.TimeType = int8(schedule_type)
  1173. }
  1174. if schMapM["template_id"] == nil && reflect.TypeOf(schMapM["template_id"]).String() != "float64" {
  1175. utils.ErrorLog("template_id")
  1176. return
  1177. }
  1178. template_id := int64(schMapM["template_id"].(float64))
  1179. sch.TemplateID = template_id
  1180. if schMapM["device_number_name"] == nil || reflect.TypeOf(schMapM["device_number_name"]).String() != "string" {
  1181. utils.ErrorLog("device_number_name")
  1182. return
  1183. }
  1184. partition_name, _ := schMapM["partition_name"].(string)
  1185. zone, _ := service.FindDeviceZoneByName(partition_name, this.GetAdminUserInfo().CurrentOrgId)
  1186. device_number_name, _ := schMapM["device_number_name"].(string)
  1187. if len(device_number_name) == 0 { //分区为空
  1188. log := models.ExportLog{
  1189. LogType: 3,
  1190. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  1191. TotalNum: int64(len(total_schedule)),
  1192. FailNum: 1,
  1193. SuccessNum: int64(len(schedules)),
  1194. CreateTime: time.Now().Unix(),
  1195. UpdateTime: time.Now().Unix(),
  1196. ExportTime: export_time,
  1197. Status: 1,
  1198. }
  1199. service.CreateExportLog(&log)
  1200. err_log := models.ExportErrLog{
  1201. LogType: 3,
  1202. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  1203. //ErrMsg: "第" + strconv.FormatInt(int64(schMapM["index"].(float64)) ,10)+ "行" + "的姓名不能为空",
  1204. ErrMsg: "第" + strconv.FormatInt(int64(schMapM["index"].(float64)), 10) + "行第2列机号格式有误",
  1205. Status: 1,
  1206. CreateTime: time.Now().Unix(),
  1207. UpdateTime: time.Now().Unix(),
  1208. ExportTime: export_time,
  1209. }
  1210. service.CreateExportErrLog(&err_log)
  1211. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeExportError)
  1212. return
  1213. } else {
  1214. device, err := service.FindDeviceByNameTwo(device_number_name, this.GetAdminUserInfo().CurrentOrgId, zone.ID)
  1215. if err == nil {
  1216. if len(device) == 0 { //没查到数据,插入错误日志
  1217. log := models.ExportLog{
  1218. LogType: 3,
  1219. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  1220. TotalNum: int64(len(total_schedule)),
  1221. FailNum: 1,
  1222. SuccessNum: int64(len(schedules)),
  1223. CreateTime: time.Now().Unix(),
  1224. UpdateTime: time.Now().Unix(),
  1225. ExportTime: export_time,
  1226. Status: 1,
  1227. }
  1228. service.CreateExportLog(&log)
  1229. err_log := models.ExportErrLog{
  1230. LogType: 3,
  1231. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  1232. ErrMsg: "第" + strconv.FormatInt(int64(schMapM["index"].(float64)), 10) + "行第2列机号格式有误",
  1233. Status: 1,
  1234. CreateTime: time.Now().Unix(),
  1235. UpdateTime: time.Now().Unix(),
  1236. ExportTime: export_time,
  1237. }
  1238. service.CreateExportErrLog(&err_log)
  1239. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeExportError)
  1240. return
  1241. } else if len(device) == 1 {
  1242. sch.DeviceNumberID = device[0].ID
  1243. } else if len(device) > 1 { //出现重名,插入错误日志
  1244. log := models.ExportLog{
  1245. LogType: 3,
  1246. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  1247. TotalNum: int64(len(total_schedule)),
  1248. FailNum: 1,
  1249. SuccessNum: int64(len(schedules)),
  1250. CreateTime: time.Now().Unix(),
  1251. UpdateTime: time.Now().Unix(),
  1252. ExportTime: export_time,
  1253. Status: 1,
  1254. }
  1255. service.CreateExportLog(&log)
  1256. err_log := models.ExportErrLog{
  1257. LogType: 3,
  1258. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  1259. ErrMsg: "出现两个床位号相同的床位",
  1260. Status: 1,
  1261. CreateTime: time.Now().Unix(),
  1262. UpdateTime: time.Now().Unix(),
  1263. ExportTime: export_time,
  1264. }
  1265. service.CreateExportErrLog(&err_log)
  1266. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeExportError)
  1267. return
  1268. }
  1269. }
  1270. }
  1271. if schMapM["schedule_week"] == nil || reflect.TypeOf(schMapM["schedule_week"]).String() != "string" {
  1272. utils.ErrorLog("schedule_week")
  1273. return
  1274. }
  1275. schedule_week, _ := schMapM["schedule_week"].(string)
  1276. if len(schedule_week) == 0 { //周几为空则生成一条导入错误日志
  1277. log := models.ExportLog{
  1278. LogType: 3,
  1279. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  1280. TotalNum: int64(len(total_schedule)),
  1281. FailNum: 1,
  1282. SuccessNum: int64(len(schedules)),
  1283. CreateTime: time.Now().Unix(),
  1284. UpdateTime: time.Now().Unix(),
  1285. ExportTime: export_time,
  1286. Status: 1,
  1287. }
  1288. service.CreateExportLog(&log)
  1289. err_log := models.ExportErrLog{
  1290. LogType: 3,
  1291. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  1292. //ErrMsg: "第" + strconv.FormatInt(int64(schMapM["index"].(float64)) ,10)+ "行" + "的姓名不能为空",
  1293. ErrMsg: "第1行第" + schMapM["row"].(string) + "列的星期名格式有误",
  1294. Status: 1,
  1295. CreateTime: time.Now().Unix(),
  1296. UpdateTime: time.Now().Unix(),
  1297. ExportTime: export_time,
  1298. }
  1299. service.CreateExportErrLog(&err_log)
  1300. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeExportError)
  1301. return
  1302. } else if schedule_week == "0" { //周几不符合格式则生成一条导入错误日志
  1303. log := models.ExportLog{
  1304. LogType: 3,
  1305. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  1306. TotalNum: int64(len(total_schedule)),
  1307. FailNum: 1,
  1308. SuccessNum: int64(len(schedules)),
  1309. CreateTime: time.Now().Unix(),
  1310. UpdateTime: time.Now().Unix(),
  1311. ExportTime: export_time,
  1312. Status: 1,
  1313. }
  1314. service.CreateExportLog(&log)
  1315. err_log := models.ExportErrLog{
  1316. LogType: 3,
  1317. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  1318. ErrMsg: "第1行第" + schMapM["row"].(string) + "列的星期名格式有误",
  1319. Status: 1,
  1320. CreateTime: time.Now().Unix(),
  1321. UpdateTime: time.Now().Unix(),
  1322. ExportTime: export_time,
  1323. }
  1324. service.CreateExportErrLog(&err_log)
  1325. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeExportError)
  1326. return
  1327. } else {
  1328. schedule_week, _ := strconv.ParseInt(schedule_week, 10, 64)
  1329. sch.Weekday = int8(schedule_week)
  1330. }
  1331. sch.CreateTime = time.Now().Unix()
  1332. sch.ModifyTime = time.Now().Unix()
  1333. sch.TreatMode = 1
  1334. sch.Status = 1
  1335. sch.OrgID = this.GetAdminUserInfo().CurrentOrgId
  1336. sch.IsExport = 1
  1337. schedules = append(schedules, &sch)
  1338. }
  1339. errLogs, _ := service.FindSchedualTemplateExportLog(this.GetAdminUserInfo().CurrentOrgId, export_time)
  1340. if len(schedules) > 0 {
  1341. err := service.UpdateScheduleTemplateStatus(this.GetAdminUserInfo().CurrentOrgId)
  1342. if err == nil {
  1343. for _, item := range schedules {
  1344. service.CreateScheduleTemplate(item)
  1345. }
  1346. log := models.ExportLog{
  1347. LogType: 3,
  1348. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  1349. TotalNum: int64(len(total_schedule)),
  1350. FailNum: int64(len(errLogs)),
  1351. SuccessNum: int64(len(schedules)),
  1352. CreateTime: time.Now().Unix(),
  1353. UpdateTime: time.Now().Unix(),
  1354. ExportTime: export_time,
  1355. Status: 1,
  1356. }
  1357. service.CreateExportLog(&log)
  1358. this.ServeSuccessJSON(map[string]interface{}{
  1359. "msg": "导入成功",
  1360. "total_num": len(total_schedule),
  1361. "success_num": len(schedules),
  1362. "fail_num": int64(len(errLogs)),
  1363. })
  1364. } else {
  1365. log := models.ExportLog{
  1366. LogType: 3,
  1367. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  1368. TotalNum: int64(len(total_schedule)),
  1369. FailNum: int64(len(errLogs)),
  1370. SuccessNum: int64(len(schedules)),
  1371. CreateTime: time.Now().Unix(),
  1372. UpdateTime: time.Now().Unix(),
  1373. ExportTime: export_time,
  1374. Status: 1,
  1375. }
  1376. service.CreateExportLog(&log)
  1377. this.ServeSuccessJSON(map[string]interface{}{
  1378. "msg": "导入成功",
  1379. "total_num": len(total_schedule),
  1380. "success_num": len(schedules),
  1381. "fail_num": int64(len(errLogs)),
  1382. })
  1383. }
  1384. } else {
  1385. log := models.ExportLog{
  1386. LogType: 3,
  1387. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  1388. TotalNum: int64(len(total_schedule)),
  1389. FailNum: int64(len(errLogs)),
  1390. SuccessNum: int64(len(schedules)),
  1391. CreateTime: time.Now().Unix(),
  1392. UpdateTime: time.Now().Unix(),
  1393. ExportTime: export_time,
  1394. Status: 1,
  1395. }
  1396. service.CreateExportLog(&log)
  1397. this.ServeSuccessJSON(map[string]interface{}{
  1398. "msg": "导入成功",
  1399. "total_num": len(total_schedule),
  1400. "success_num": len(schedules),
  1401. "fail_num": int64(len(errLogs)),
  1402. })
  1403. }
  1404. }
  1405. }
  1406. func (c *ScheduleApiController) GetInitExcelInitDate() {
  1407. thisTime := time.Now()
  1408. thisTime1 := time.Now()
  1409. thisTime2 := time.Now()
  1410. thisTime1 = thisTime1.AddDate(0, 0, 7)
  1411. thisTime2 = thisTime2.AddDate(0, 0, 14)
  1412. weekDay := int(thisTime.Weekday())
  1413. if weekDay == 0 {
  1414. weekDay = 7
  1415. }
  1416. weekEnd := 7 - weekDay
  1417. weekStart := weekEnd - 6
  1418. days := make([]string, 0)
  1419. for index := weekStart; index <= weekEnd; index++ {
  1420. theDay := thisTime.AddDate(0, 0, index)
  1421. days = append(days, theDay.Format("2006-01-02"))
  1422. }
  1423. weekDay1 := int(thisTime1.Weekday())
  1424. if weekDay1 == 0 {
  1425. weekDay1 = 7
  1426. }
  1427. weekEnd1 := 7 - weekDay1
  1428. weekStart1 := weekEnd1 - 6
  1429. for index := weekStart1; index <= weekEnd1; index++ {
  1430. theDay := thisTime1.AddDate(0, 0, index)
  1431. days = append(days, theDay.Format("2006-01-02"))
  1432. }
  1433. weekDay2 := int(thisTime2.Weekday())
  1434. if weekDay2 == 0 {
  1435. weekDay2 = 7
  1436. }
  1437. weekEnd2 := 7 - weekDay2
  1438. weekStart2 := weekEnd2 - 6
  1439. for index := weekStart2; index <= weekEnd2; index++ {
  1440. theDay := thisTime2.AddDate(0, 0, index)
  1441. days = append(days, theDay.Format("2006-01-02"))
  1442. }
  1443. c.ServeSuccessJSON(map[string]interface{}{
  1444. "days": days,
  1445. })
  1446. return
  1447. }