schedule_api_controller.go 58KB

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