schedule_api_controller.go 58KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901
  1. package controllers
  2. import (
  3. "XT_New/enums"
  4. "XT_New/models"
  5. "XT_New/service"
  6. "XT_New/utils"
  7. "encoding/json"
  8. "fmt"
  9. "github.com/jinzhu/gorm"
  10. "reflect"
  11. "strconv"
  12. "strings"
  13. "time"
  14. "github.com/astaxie/beego"
  15. )
  16. type ScheduleApiController struct {
  17. BaseAuthAPIController
  18. }
  19. func ScheduleApiRegistRouters() {
  20. beego.Router("/api/schedule/weekpanel", &ScheduleApiController{}, "Get:GetWeekPanels")
  21. beego.Router("/api/schedule/schedules", &ScheduleApiController{}, "Get:GetSchedules")
  22. beego.Router("/api/excel_date/init", &ScheduleApiController{}, "Get:GetInitExcelInitDate")
  23. beego.Router("/api/schedule/patients", &ScheduleApiController{}, "Get:GetPatients")
  24. beego.Router("/api/schedule/create", &ScheduleApiController{}, "Post:CreateSchedule")
  25. beego.Router("/api/schedule/delete", &ScheduleApiController{}, "Delete:DeleteSchedule")
  26. beego.Router("/api/schedule/change", &ScheduleApiController{}, "Put:ChangeSchedule")
  27. beego.Router("/api/schedule/urgentinit", &ScheduleApiController{}, "Get:UrgentScheduleData")
  28. beego.Router("/api/schedule/print/initdata", &ScheduleApiController{}, "get:PrintInitData")
  29. beego.Router("/api/schedule/search", &ScheduleApiController{}, "get:SearchSchedulePatients")
  30. beego.Router("/api/schedule/week", &ScheduleApiController{}, "get:GetWeekDaySchedule")
  31. beego.Router("/api/schedule/export", &ScheduleApiController{}, "post:ExportSchedule")
  32. beego.Router("/api/schedule_template/export", &ScheduleApiController{}, "post:ExportScheduleTemplate")
  33. beego.Router("/api/schedule_template/search", &ScheduleApiController{}, "get:SearchTemplateSchedulePatients")
  34. beego.Router("/api/schedule_template/cancel", &ScheduleApiController{}, "post:CancelScheduleTemplate")
  35. }
  36. func (c *ScheduleApiController) GetWeekPanels() {
  37. data, _ := c.GetInt64("data", 1)
  38. adminInfo := c.GetAdminUserInfo()
  39. thisTime := time.Now()
  40. year, monthTime, day := thisTime.Date()
  41. month := int(monthTime)
  42. _, theWeek := thisTime.ISOWeek()
  43. weekDay := int(thisTime.Weekday())
  44. if weekDay == 0 {
  45. weekDay = 7
  46. }
  47. weekEnd := 7 - weekDay
  48. weekStart := weekEnd - 6
  49. weekDays := make([]string, 0)
  50. for index := weekStart; index <= weekEnd; index++ {
  51. theDay := thisTime.AddDate(0, 0, index)
  52. indexYear, indexMonthTime, indexDay := theDay.Date()
  53. indexMonth := int(indexMonthTime)
  54. indexWeek := strconv.Itoa(indexYear) + "." + strconv.Itoa(indexMonth) + "." + strconv.Itoa(indexDay)
  55. weekDays = append(weekDays, indexWeek)
  56. }
  57. returnData := map[string]interface{}{
  58. "year": year,
  59. "month": month,
  60. "day": day,
  61. "theWeek": theWeek,
  62. "weekDay": weekDay,
  63. "weekDays": weekDays,
  64. }
  65. if data == 1 {
  66. partitions, _ := service.GetSchedulePartitionPanel(adminInfo.CurrentOrgId)
  67. returnData["partitions"] = partitions
  68. }
  69. c.ServeSuccessJSON(returnData)
  70. return
  71. }
  72. func (c *ScheduleApiController) GetSchedules() {
  73. week, _ := c.GetInt64("weekTime", 0) //1:last, 2:this 3:next 4 nextTwo
  74. adminInfo := c.GetAdminUserInfo()
  75. thisTime := time.Now()
  76. today := thisTime.Format("2006-01-02")
  77. if week < 1 || week > 4 {
  78. week = 2
  79. }
  80. if week == 1 {
  81. thisTime = thisTime.AddDate(0, 0, -7)
  82. } else if week == 3 {
  83. thisTime = thisTime.AddDate(0, 0, 7)
  84. } else if week == 4 {
  85. thisTime = thisTime.AddDate(0, 0, 14)
  86. }
  87. weekDay := int(thisTime.Weekday())
  88. if weekDay == 0 {
  89. weekDay = 7
  90. }
  91. weekEnd := 7 - weekDay
  92. weekStart := weekEnd - 6
  93. weekTitle := make([]string, 0)
  94. days := make([]string, 0)
  95. for index := weekStart; index <= weekEnd; index++ {
  96. theDay := thisTime.AddDate(0, 0, index)
  97. indexYear, indexMonthTime, indexDay := theDay.Date()
  98. indexMonth := int(indexMonthTime)
  99. indexWeek := strconv.Itoa(indexYear) + "." + strconv.Itoa(indexMonth) + "." + strconv.Itoa(indexDay)
  100. weekTitle = append(weekTitle, indexWeek)
  101. days = append(days, theDay.Format("2006-01-02"))
  102. }
  103. weekStartDay := thisTime.AddDate(0, 0, weekStart)
  104. weekEndDay := thisTime.AddDate(0, 0, weekEnd)
  105. weekStartTime := weekStartDay.Format("2006-01-02") + " 00:00:00"
  106. weekEndTime := weekEndDay.Format("2006-01-02") + " 23:59:59"
  107. timeLayout := "2006-01-02 15:04:05"
  108. loc, _ := time.LoadLocation("Local")
  109. theStarTime, _ := time.ParseInLocation(timeLayout, weekStartTime, loc)
  110. theEndTime, _ := time.ParseInLocation(timeLayout, weekEndTime, loc)
  111. weekStartPoint := theStarTime.Unix()
  112. weekEndPoint := theEndTime.Unix()
  113. schdules, _ := service.GetWeekSchedule(adminInfo.CurrentOrgId, weekStartPoint, weekEndPoint)
  114. c.ServeSuccessJSON(map[string]interface{}{
  115. "days": days,
  116. "weekTitle": weekTitle,
  117. "schdules": schdules,
  118. "today": today,
  119. })
  120. return
  121. }
  122. func (c *ScheduleApiController) GetPatients() {
  123. keywords := c.GetString("keywords", "")
  124. schedule, _ := c.GetInt64("schedule", 0) //1已2未
  125. contagion, _ := c.GetInt64("contagion", 0)
  126. adminInfo := c.GetAdminUserInfo()
  127. thisTime := time.Now()
  128. weekDay := int(thisTime.Weekday())
  129. if weekDay == 0 {
  130. weekDay = 7
  131. }
  132. thisWeekEnd := 7 - weekDay
  133. weekStartPoint := thisWeekEnd - 6
  134. weekStartDay := thisTime.AddDate(0, 0, weekStartPoint)
  135. weekEndPoint := thisWeekEnd + 7
  136. weekEndDay := thisTime.AddDate(0, 0, weekEndPoint)
  137. fmt.Println(weekStartPoint, weekStartDay, weekEndPoint, weekEndDay)
  138. weekStartTime := weekStartDay.Format("2006-01-02") + " 00:00:00"
  139. weekEndTime := weekEndDay.Format("2006-01-02") + " 23:59:59"
  140. fmt.Println(weekStartTime, weekEndTime)
  141. timeLayout := "2006-01-02 15:04:05"
  142. loc, _ := time.LoadLocation("Local")
  143. theStarTime, _ := time.ParseInLocation(timeLayout, weekStartTime, loc)
  144. theEndTime, _ := time.ParseInLocation(timeLayout, weekEndTime, loc)
  145. weekStart := theStarTime.Unix()
  146. weekEnd := theEndTime.Unix()
  147. patients, _ := service.GetPatientWithScheduleAndSolution(adminInfo.CurrentOrgId, keywords, weekStart, weekEnd, schedule, contagion)
  148. c.ServeSuccessJSON(map[string]interface{}{
  149. "patients": patients,
  150. })
  151. return
  152. }
  153. func (c *ScheduleApiController) CreateSchedule() {
  154. patientID, _ := c.GetInt64("patient_id", 0)
  155. if patientID <= 0 {
  156. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  157. return
  158. }
  159. adminUserInfo := c.GetAdminUserInfo()
  160. patientInfo, _ := service.FindPatientById(adminUserInfo.CurrentOrgId, patientID)
  161. if patientInfo.ID == 0 {
  162. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePatientNoExist)
  163. return
  164. }
  165. var schedule models.Schedule
  166. dataBody := make(map[string]interface{}, 0)
  167. err := json.Unmarshal(c.Ctx.Input.RequestBody, &dataBody)
  168. if err != nil {
  169. utils.ErrorLog(err.Error())
  170. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  171. return
  172. }
  173. if dataBody["schedule_date"] == nil || reflect.TypeOf(dataBody["schedule_date"]).String() != "string" {
  174. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  175. return
  176. }
  177. scheduleDate, _ := dataBody["schedule_date"].(string)
  178. if len(scheduleDate) == 0 {
  179. utils.ErrorLog("len(schedule_date) == 0")
  180. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  181. return
  182. }
  183. timeLayout := "2006-01-02"
  184. loc, _ := time.LoadLocation("Local")
  185. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", scheduleDate+" 00:00:00", loc)
  186. if err != nil {
  187. utils.ErrorLog(err.Error())
  188. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  189. return
  190. }
  191. schedule.ScheduleDate = theTime.Unix()
  192. timeNow := time.Now().Format("2006-01-02")
  193. if timeNow > scheduleDate {
  194. utils.ErrorLog(timeNow)
  195. utils.ErrorLog(scheduleDate)
  196. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCantSetScheduleBeforeNow)
  197. return
  198. }
  199. if dataBody["schedule_type"] == nil || reflect.TypeOf(dataBody["schedule_type"]).String() != "float64" {
  200. utils.ErrorLog("schedule_type")
  201. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  202. return
  203. }
  204. scheduleType := int64(dataBody["schedule_type"].(float64))
  205. if scheduleType < 1 || scheduleType > 3 {
  206. utils.ErrorLog("scheduleType < 3")
  207. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  208. return
  209. }
  210. schedule.ScheduleType = scheduleType
  211. if dataBody["bed_id"] == nil || reflect.TypeOf(dataBody["bed_id"]).String() != "float64" {
  212. utils.ErrorLog("bed_id")
  213. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  214. return
  215. }
  216. bedId := int64(dataBody["bed_id"].(float64))
  217. if bedId < 1 {
  218. utils.ErrorLog("bedId < 1")
  219. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  220. return
  221. }
  222. schedule.BedId = bedId
  223. if dataBody["partition_id"] == nil || reflect.TypeOf(dataBody["partition_id"]).String() != "float64" {
  224. utils.ErrorLog("partition_id")
  225. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  226. return
  227. }
  228. partitionId := int64(dataBody["partition_id"].(float64))
  229. if partitionId < 1 {
  230. utils.ErrorLog("partitionId < 1")
  231. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  232. return
  233. }
  234. schedule.PartitionId = partitionId
  235. if dataBody["schedule_week"] == nil || reflect.TypeOf(dataBody["schedule_week"]).String() != "float64" {
  236. utils.ErrorLog("schedule_week")
  237. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  238. return
  239. }
  240. scheduleWeek := int64(dataBody["schedule_week"].(float64))
  241. if scheduleWeek < 1 || scheduleWeek > 7 {
  242. utils.ErrorLog("scheduleWeek < 1")
  243. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  244. return
  245. }
  246. schedule.ScheduleWeek = scheduleWeek
  247. if dataBody["mode_id"] == nil || reflect.TypeOf(dataBody["mode_id"]).String() != "float64" {
  248. utils.ErrorLog("mode_id")
  249. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  250. return
  251. }
  252. modeId := int64(dataBody["mode_id"].(float64))
  253. if modeId < 1 && modeId > 14 {
  254. utils.ErrorLog("modeId < 1")
  255. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  256. return
  257. }
  258. schedule.ModeId = modeId
  259. schedule.PatientId = patientID
  260. schedule.CreatedTime = time.Now().Unix()
  261. schedule.UpdatedTime = time.Now().Unix()
  262. schedule.Status = 1
  263. schedule.UserOrgId = adminUserInfo.CurrentOrgId
  264. bed, _ := service.GetDeviceNumberByID(adminUserInfo.CurrentOrgId, schedule.BedId)
  265. if bed == nil {
  266. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDeviceNumberNotExist)
  267. return
  268. }
  269. if bed.ZoneID != schedule.PartitionId {
  270. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDeviceNumberNotTheZone)
  271. return
  272. }
  273. scheduleDateStart := scheduleDate + " 00:00:00"
  274. scheduleDateEnd := scheduleDate + " 23:59:59"
  275. timeLayout = "2006-01-02 15:04:05"
  276. theStartTime, _ := time.ParseInLocation(timeLayout, scheduleDateStart, loc)
  277. theEndTime, _ := time.ParseInLocation(timeLayout, scheduleDateEnd, loc)
  278. startTime := theStartTime.Unix()
  279. endTime := theEndTime.Unix()
  280. //一天只有排班一次
  281. daySchedule, err := service.GetDaySchedule(adminUserInfo.CurrentOrgId, startTime, endTime, patientID)
  282. if daySchedule.ID > 0 {
  283. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCantSetScheduleAgainOneDay)
  284. return
  285. }
  286. //同天同位置只能排一个
  287. pointSchedule, err := service.GetPointSchedule(adminUserInfo.CurrentOrgId, schedule.ScheduleDate, schedule.ScheduleWeek, schedule.ScheduleType, schedule.BedId)
  288. if err != nil {
  289. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  290. return
  291. }
  292. if pointSchedule.ID > 0 {
  293. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePointScheduleExist)
  294. return
  295. }
  296. redis := service.RedisClient()
  297. defer redis.Close()
  298. err = service.CreateSchedule(&schedule)
  299. key := "scheduals_" + scheduleDate + "_" + strconv.FormatInt(adminUserInfo.CurrentOrgId, 10)
  300. redis.Set(key, "", time.Second)
  301. fmt.Println(err)
  302. if err != nil {
  303. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCreateScheduleFail)
  304. return
  305. }
  306. schedule.Patient = patientInfo.Name
  307. c.ServeSuccessJSON(map[string]interface{}{
  308. "msg": "ok",
  309. "schedule": schedule,
  310. })
  311. return
  312. }
  313. func (c *ScheduleApiController) DeleteSchedule() {
  314. id, _ := c.GetInt64("id", 0)
  315. if id <= 0 {
  316. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  317. return
  318. }
  319. adminINfo := c.GetAdminUserInfo()
  320. schedule, _ := service.GetSchedule(adminINfo.CurrentOrgId, id)
  321. if schedule == nil {
  322. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeScheduleNotExist)
  323. return
  324. }
  325. order, err := service.GetOneDialysisOrder(adminINfo.CurrentOrgId, schedule.ScheduleDate, schedule.PatientId)
  326. if err != nil {
  327. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  328. return
  329. }
  330. if order != nil {
  331. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDelScheduleFailByDialysis)
  332. return
  333. }
  334. schedule.Status = 0
  335. schedule.UpdatedTime = time.Now().Unix()
  336. err = service.UpdateSchedule(schedule)
  337. if err != nil {
  338. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDeleteScheduleFail)
  339. return
  340. }
  341. c.ServeSuccessJSON(map[string]interface{}{
  342. "msg": "ok",
  343. "schedule": &schedule,
  344. })
  345. }
  346. func (c *ScheduleApiController) ChangeSchedule() {
  347. id, _ := c.GetInt64("id", 0)
  348. if id <= 0 {
  349. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  350. return
  351. }
  352. adminINfo := c.GetAdminUserInfo()
  353. schedule, _ := service.GetSchedule(adminINfo.CurrentOrgId, id)
  354. if schedule == nil {
  355. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeScheduleNotExist)
  356. return
  357. }
  358. dataBody := make(map[string]interface{}, 0)
  359. err := json.Unmarshal(c.Ctx.Input.RequestBody, &dataBody)
  360. if err != nil {
  361. utils.ErrorLog(err.Error())
  362. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  363. return
  364. }
  365. if dataBody["change_action"] == nil || reflect.TypeOf(dataBody["change_action"]).String() != "string" {
  366. utils.ErrorLog("change_action")
  367. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  368. return
  369. }
  370. changeAction := dataBody["change_action"].(string)
  371. if changeAction == "change_mode" {
  372. if dataBody["mode_id"] == nil || reflect.TypeOf(dataBody["mode_id"]).String() != "float64" {
  373. utils.ErrorLog("mode_id")
  374. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  375. return
  376. }
  377. modeId := int64(dataBody["mode_id"].(float64))
  378. if modeId < 1 {
  379. utils.ErrorLog("modeId < 1")
  380. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  381. return
  382. }
  383. schedule.ModeId = modeId
  384. order, err := service.GetOneDialysisOrder(adminINfo.CurrentOrgId, schedule.ScheduleDate, schedule.PatientId)
  385. if err != nil {
  386. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  387. return
  388. }
  389. if order != nil {
  390. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeChangeMode)
  391. return
  392. }
  393. } else if changeAction == "change_device" {
  394. order, err := service.GetOneDialysisOrder(adminINfo.CurrentOrgId, schedule.ScheduleDate, schedule.PatientId)
  395. if err != nil {
  396. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  397. return
  398. }
  399. if order != nil {
  400. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeChangeDeviceNumber)
  401. return
  402. }
  403. if dataBody["schedule_type"] == nil || reflect.TypeOf(dataBody["schedule_type"]).String() != "float64" {
  404. utils.ErrorLog("schedule_type")
  405. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  406. return
  407. }
  408. scheduleType := int64(dataBody["schedule_type"].(float64))
  409. if scheduleType < 1 || scheduleType > 3 {
  410. utils.ErrorLog("scheduleType < 3")
  411. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  412. return
  413. }
  414. schedule.ScheduleType = scheduleType
  415. if dataBody["bed_id"] == nil || reflect.TypeOf(dataBody["bed_id"]).String() != "float64" {
  416. utils.ErrorLog("bed_id")
  417. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  418. return
  419. }
  420. bedId := int64(dataBody["bed_id"].(float64))
  421. if bedId < 1 {
  422. utils.ErrorLog("bedId < 1")
  423. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  424. return
  425. }
  426. schedule.BedId = bedId
  427. if dataBody["partition_id"] == nil || reflect.TypeOf(dataBody["partition_id"]).String() != "float64" {
  428. utils.ErrorLog("partition_id")
  429. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  430. return
  431. }
  432. partitionId := int64(dataBody["partition_id"].(float64))
  433. if partitionId < 1 {
  434. utils.ErrorLog("partitionId < 1")
  435. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  436. return
  437. }
  438. schedule.PartitionId = partitionId
  439. bed, _ := service.GetDeviceNumberByID(adminINfo.CurrentOrgId, schedule.BedId)
  440. if bed == nil {
  441. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDeviceNumberNotExist)
  442. return
  443. }
  444. if bed.ZoneID != schedule.PartitionId {
  445. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDeviceNumberNotTheZone)
  446. return
  447. }
  448. startTime := schedule.ScheduleDate
  449. endTime := startTime + 86399
  450. //一天只有排班一次
  451. daySchedule, err := service.GetDaySchedule(adminINfo.CurrentOrgId, startTime, endTime, schedule.PatientId)
  452. if daySchedule.ID > 0 && daySchedule.ID != schedule.ID {
  453. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCantSetScheduleAgainOneDay)
  454. return
  455. }
  456. //同天同位置只能排一个
  457. pointSchedule, err := service.GetPointSchedule(adminINfo.CurrentOrgId, schedule.ScheduleDate, schedule.ScheduleWeek, schedule.ScheduleType, schedule.BedId)
  458. if err != nil {
  459. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  460. return
  461. }
  462. if pointSchedule.ID > 0 {
  463. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePointScheduleExist)
  464. return
  465. }
  466. } else {
  467. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  468. return
  469. }
  470. schedule.UpdatedTime = time.Now().Unix()
  471. err = service.UpdateSchedule(schedule)
  472. if err != nil {
  473. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeChangeScheduleFail)
  474. return
  475. }
  476. c.ServeSuccessJSON(map[string]interface{}{
  477. "msg": "ok",
  478. "schedule": &schedule,
  479. })
  480. }
  481. // /api/schedule/print/initdata [get]
  482. // @param date:string yyyy-MM-dd 要打印的那一周中的任一天
  483. func (this *ScheduleApiController) PrintInitData() {
  484. dateStr := this.GetString("date")
  485. //week_type, _ := this.GetInt64("type", 0)
  486. var date *time.Time
  487. if len(dateStr) == 0 {
  488. now := time.Now()
  489. date = &now
  490. } else {
  491. var parseErr error
  492. date, parseErr = utils.ParseTimeStringToTime("2006-01-02", dateStr)
  493. if parseErr != nil {
  494. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  495. return
  496. }
  497. }
  498. adminUserInfo := this.GetAdminUserInfo()
  499. // 获取本周的排班
  500. var monday time.Time
  501. var sunday time.Time
  502. //switch week_type {
  503. //case 1:
  504. monday, sunday = utils.GetMondayAndSundayOfWeekDate(date)
  505. // break
  506. //case 2:
  507. // monday, sunday = utils.GetMondayAndSundayOfNextWeekDate(date)
  508. //
  509. // break
  510. //case 3:
  511. // monday, sunday = utils.GetMondayAndSundayOfNextNextWeekDate(date)
  512. // break
  513. //}
  514. schedules, getScheduleErr := service.GetPrinitWeekSchedules(adminUserInfo.CurrentOrgId, monday.Unix(), sunday.Unix())
  515. total, getScheduleErr := service.GetPrinitWeekTotal(adminUserInfo.CurrentOrgId, monday.Unix(), sunday.Unix())
  516. if getScheduleErr != nil {
  517. this.ErrorLog("获取周排班失败:%v", getScheduleErr)
  518. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  519. return
  520. }
  521. thisTime := date
  522. weekDay := int(thisTime.Weekday())
  523. if weekDay == 0 {
  524. weekDay = 7
  525. }
  526. weekEnd := 7 - weekDay
  527. weekStart := weekEnd - 6
  528. days := make([]string, 0)
  529. for index := weekStart; index <= weekEnd; index++ {
  530. theDay := thisTime.AddDate(0, 0, index)
  531. days = append(days, theDay.Format("2006-01-02"))
  532. }
  533. this.ServeSuccessJSON(map[string]interface{}{
  534. "total": total,
  535. "schedules": schedules,
  536. "monday": monday.Unix(),
  537. "days": days,
  538. })
  539. }
  540. func (this *ScheduleApiController) UrgentScheduleData() {
  541. schedule_type, _ := this.GetInt("type", 0)
  542. schedule_date := this.GetString("date")
  543. var date *time.Time
  544. if len(schedule_date) == 0 {
  545. now := time.Now()
  546. date = &now
  547. } else {
  548. var parseErr error
  549. date, parseErr = utils.ParseTimeStringToTime("2006-01-02", schedule_date)
  550. if parseErr != nil {
  551. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  552. return
  553. }
  554. }
  555. adminUserInfo := this.GetAdminUserInfo()
  556. deviceNumbers, getDeviceNumbersErr := service.MobileGetAllDeviceNumbersForUrgentSchedule(adminUserInfo.CurrentOrgId, date.Unix(), schedule_type)
  557. if getDeviceNumbersErr != nil {
  558. this.ErrorLog("获取所有床位失败:%v", getDeviceNumbersErr)
  559. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  560. return
  561. }
  562. // today := utils.ZeroHourTimeOfDay(time.Now())
  563. schedules, getSchedulesErr := service.MobileGetOtherSchedulesForUrgentSchedule(adminUserInfo.CurrentOrgId, date.Unix(), schedule_type)
  564. if getSchedulesErr != nil {
  565. this.ErrorLog("获取所有排班失败:%v", getSchedulesErr)
  566. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  567. return
  568. }
  569. this.ServeSuccessJSON(map[string]interface{}{
  570. "device_numbers": deviceNumbers,
  571. "schedules": schedules,
  572. })
  573. }
  574. func (this *ScheduleApiController) SearchSchedulePatients() {
  575. keywords := this.GetString("keywords")
  576. week_type, _ := this.GetInt64("week_type", 0)
  577. thisTime1 := time.Now()
  578. thisTime2 := time.Now()
  579. thisTime1 = thisTime1.AddDate(0, 0, 7)
  580. thisTime2 = thisTime2.AddDate(0, 0, 14)
  581. var start_time string
  582. var end_time string
  583. switch week_type {
  584. case 1:
  585. adminUserInfo := this.GetAdminUserInfo()
  586. list, _ := service.GetSchedualPatientsByKeywords(keywords, adminUserInfo.CurrentOrgId)
  587. this.ServeSuccessJSON(map[string]interface{}{
  588. "schdules": list,
  589. })
  590. break
  591. case 2:
  592. days := make([]string, 0)
  593. weekDay1 := int(thisTime1.Weekday())
  594. if weekDay1 == 0 {
  595. weekDay1 = 7
  596. }
  597. weekEnd1 := 7 - weekDay1
  598. weekStart1 := weekEnd1 - 6
  599. for index := weekStart1; index <= weekEnd1; index++ {
  600. theDay := thisTime1.AddDate(0, 0, index)
  601. days = append(days, theDay.Format("2006-01-02"))
  602. if index == -1 {
  603. start_time = theDay.Format("2006-01-02")
  604. fmt.Println(start_time)
  605. }
  606. end_time = theDay.Format("2006-01-02")
  607. }
  608. timeLayout := "2006-01-02"
  609. loc, _ := time.LoadLocation("Local")
  610. var theStartTime int64
  611. if len(start_time) > 0 {
  612. theTime, _ := time.ParseInLocation(timeLayout+" 15:04:05", start_time+" 00:00:00", loc)
  613. theStartTime = theTime.Unix()
  614. }
  615. var theEndtTime int64
  616. if len(end_time) > 0 {
  617. theTime, _ := time.ParseInLocation(timeLayout+" 15:04:05", end_time+" 23:59:59", loc)
  618. theEndtTime = theTime.Unix()
  619. }
  620. adminUserInfo := this.GetAdminUserInfo()
  621. list, _ := service.GetSchedualPatientsByKeywordsAndWeek(keywords, adminUserInfo.CurrentOrgId, theStartTime, theEndtTime)
  622. this.ServeSuccessJSON(map[string]interface{}{
  623. "schdules": list,
  624. })
  625. break
  626. case 3:
  627. days := make([]string, 0)
  628. weekDay2 := int(thisTime2.Weekday())
  629. if weekDay2 == 0 {
  630. weekDay2 = 7
  631. }
  632. weekEnd2 := 7 - weekDay2
  633. weekStart2 := weekEnd2 - 6
  634. for index := weekStart2; index <= weekEnd2; index++ {
  635. theDay := thisTime2.AddDate(0, 0, index)
  636. days = append(days, theDay.Format("2006-01-02"))
  637. if index == -1 {
  638. start_time = theDay.Format("2006-01-02")
  639. }
  640. end_time = theDay.Format("2006-01-02")
  641. }
  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. fmt.Println(week_type)
  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.GetWeekDaySchedule(adminUserInfo.CurrentOrgId, targetDay.Unix(), targetDay)
  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. }