schedule_api_controller.go 61KB

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