schedule_api_controller.go 66KB

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