schedule_api_controller.go 59KB

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