schedule_api_controller.go 58KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898
  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 == 0 {
  603. start_time = theDay.Format("2006-01-02")
  604. }
  605. end_time = theDay.Format("2006-01-02")
  606. }
  607. timeLayout := "2006-01-02"
  608. loc, _ := time.LoadLocation("Local")
  609. var theStartTime int64
  610. if len(start_time) > 0 {
  611. theTime, _ := time.ParseInLocation(timeLayout+" 15:04:05", start_time+" 00:00:00", loc)
  612. theStartTime = theTime.Unix()
  613. }
  614. var theEndtTime int64
  615. if len(end_time) > 0 {
  616. theTime, _ := time.ParseInLocation(timeLayout+" 15:04:05", end_time+" 23:59:59", loc)
  617. theEndtTime = theTime.Unix()
  618. }
  619. adminUserInfo := this.GetAdminUserInfo()
  620. list, _ := service.GetSchedualPatientsByKeywordsAndWeek(keywords, adminUserInfo.CurrentOrgId, theStartTime, theEndtTime)
  621. this.ServeSuccessJSON(map[string]interface{}{
  622. "schdules": list,
  623. })
  624. break
  625. case 3:
  626. days := make([]string, 0)
  627. weekDay2 := int(thisTime2.Weekday())
  628. if weekDay2 == 0 {
  629. weekDay2 = 7
  630. }
  631. weekEnd2 := 7 - weekDay2
  632. weekStart2 := weekEnd2 - 6
  633. for index := weekStart2; index <= weekEnd2; index++ {
  634. theDay := thisTime2.AddDate(0, 0, index)
  635. days = append(days, theDay.Format("2006-01-02"))
  636. if index == 0 {
  637. start_time = theDay.Format("2006-01-02")
  638. }
  639. end_time = theDay.Format("2006-01-02")
  640. }
  641. timeLayout := "2006-01-02"
  642. loc, _ := time.LoadLocation("Local")
  643. var theStartTime int64
  644. if len(start_time) > 0 {
  645. theTime, _ := time.ParseInLocation(timeLayout+" 15:04:05", start_time+" 00:00:00", loc)
  646. theStartTime = theTime.Unix()
  647. }
  648. var theEndtTime int64
  649. if len(end_time) > 0 {
  650. theTime, _ := time.ParseInLocation(timeLayout+" 15:04:05", end_time+" 23:59:59", loc)
  651. theEndtTime = theTime.Unix()
  652. }
  653. adminUserInfo := this.GetAdminUserInfo()
  654. list, _ := service.GetSchedualPatientsByKeywordsAndWeek(keywords, adminUserInfo.CurrentOrgId, theStartTime, theEndtTime)
  655. this.ServeSuccessJSON(map[string]interface{}{
  656. "schdules": list,
  657. })
  658. break
  659. }
  660. }
  661. func (this *ScheduleApiController) GetWeekDaySchedule() {
  662. week_type, _ := this.GetInt64("week_type", -1)
  663. fmt.Println(week_type)
  664. thisTime := time.Now()
  665. weekDay := int(thisTime.Weekday())
  666. if weekDay == 0 {
  667. weekDay = 7
  668. }
  669. weekEnd := 7 - weekDay
  670. weekStart := weekEnd - 6
  671. weekTitle := make([]string, 0)
  672. days := make([]string, 0)
  673. for index := weekStart; index <= weekEnd; index++ {
  674. theDay := thisTime.AddDate(0, 0, index)
  675. indexYear, indexMonthTime, indexDay := theDay.Date()
  676. indexMonth := int(indexMonthTime)
  677. indexWeek := strconv.Itoa(indexYear) + "." + strconv.Itoa(indexMonth) + "." + strconv.Itoa(indexDay)
  678. weekTitle = append(weekTitle, indexWeek)
  679. days = append(days, theDay.Format("2006-01-02"))
  680. }
  681. fmt.Println(days)
  682. var targetDayStr string
  683. switch week_type {
  684. case 1:
  685. targetDayStr = days[0]
  686. break
  687. case 2:
  688. targetDayStr = days[1]
  689. break
  690. case 3:
  691. targetDayStr = days[2]
  692. break
  693. case 4:
  694. targetDayStr = days[3]
  695. break
  696. case 5:
  697. targetDayStr = days[4]
  698. break
  699. case 6:
  700. targetDayStr = days[5]
  701. break
  702. case 7:
  703. targetDayStr = days[6]
  704. break
  705. }
  706. targetDay, parseErr := utils.ParseTimeStringToTime("2006-01-02", targetDayStr)
  707. if parseErr != nil {
  708. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  709. return
  710. }
  711. adminUserInfo := this.GetAdminUserInfo()
  712. list, _ := service.GetWeekDaySchedule(adminUserInfo.CurrentOrgId, targetDay.Unix(), targetDay)
  713. this.ServeSuccessJSON(map[string]interface{}{
  714. "schdules": list,
  715. "day": targetDayStr,
  716. })
  717. }
  718. func Struct2Map(obj interface{}) map[string]interface{} {
  719. t := reflect.TypeOf(obj)
  720. v := reflect.ValueOf(obj)
  721. var data = make(map[string]interface{})
  722. for i := 0; i < t.NumField(); i++ {
  723. data[t.Field(i).Name] = v.Field(i).Interface()
  724. }
  725. return data
  726. }
  727. func (this *ScheduleApiController) ExportSchedule() {
  728. dataBody := make(map[string]interface{}, 0)
  729. err := json.Unmarshal(this.Ctx.Input.RequestBody, &dataBody)
  730. if err != nil {
  731. utils.ErrorLog(err.Error())
  732. return
  733. }
  734. utils.ErrorLog("%v", dataBody)
  735. export_time := time.Now().Unix()
  736. var schedules []*models.Schedule
  737. var failed_total int
  738. var total_schedule []interface{}
  739. var patients []*models.Patients
  740. patients, _, _ = service.GetAllPatientList(this.GetAdminUserInfo().CurrentOrgId)
  741. if dataBody["failed_schedule"] != nil || reflect.TypeOf(dataBody["failed_schedule"]).String() == "[]interface {}" {
  742. tempSchedule := dataBody["failed_schedule"].([]interface{})
  743. failed_total = len(tempSchedule)
  744. }
  745. if dataBody["schedule"] != nil || reflect.TypeOf(dataBody["schedule"]).String() == "[]interface {}" {
  746. tempSchedule := dataBody["schedule"].([]interface{})
  747. total_schedule = tempSchedule
  748. for _, schMap := range tempSchedule {
  749. schMapM := schMap.(map[string]interface{})
  750. var sch models.Schedule
  751. if schMapM["name"] == nil || reflect.TypeOf(schMapM["name"]).String() != "string" {
  752. utils.ErrorLog("name")
  753. return
  754. }
  755. name, _ := schMapM["name"].(string)
  756. fmt.Println(name)
  757. if len(name) == 0 { //名字为空则生成一条导入错误日志
  758. err_log := models.ExportErrLog{
  759. LogType: 2,
  760. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  761. ErrMsg: "第" + strconv.FormatInt(int64(schMapM["index"].(float64)), 10) + "行,第" + schMapM["row"].(string) + "列的姓名不能为空",
  762. Status: 1,
  763. CreateTime: time.Now().Unix(),
  764. UpdateTime: time.Now().Unix(),
  765. ExportTime: export_time,
  766. }
  767. service.CreateExportErrLog(&err_log)
  768. continue
  769. } else {
  770. var patient []*models.Patients
  771. for _, item := range patients {
  772. if strings.Replace(item.Name, " ", "", -1) == name {
  773. patient = append(patient, item)
  774. }
  775. }
  776. if len(patient) == 0 { //错误日志
  777. err_log := models.ExportErrLog{
  778. LogType: 2,
  779. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  780. ErrMsg: "第" + strconv.FormatInt(int64(schMapM["index"].(float64)), 10) + "行,第" + schMapM["row"].(string) + "列,患者姓名为\"" + name + "\"在系统中不存在,请在系统中添加该患者",
  781. Status: 1,
  782. CreateTime: time.Now().Unix(),
  783. UpdateTime: time.Now().Unix(),
  784. ExportTime: export_time,
  785. }
  786. service.CreateExportErrLog(&err_log)
  787. continue
  788. } else if len(patient) == 1 { //
  789. sch.PatientId = patient[0].ID
  790. } else if len(patient) > 1 { //出现同名的情况
  791. err_log := models.ExportErrLog{
  792. LogType: 2,
  793. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  794. //ErrMsg: "第" + strconv.FormatInt(int64(schMapM["index"].(float64)), 10) + "行,第" + schMapM["row"].(string) + "列" + "患者姓名为" + name + "在当前机构中存在重名,无法确定到具体患者",
  795. ErrMsg: "第" + strconv.FormatInt(int64(schMapM["index"].(float64)), 10) + "行,第" + schMapM["row"].(string) + "列,患者姓名为\"" + name + "\"在在当前机构中存在重名,无法确定到具体患者",
  796. Status: 1,
  797. CreateTime: time.Now().Unix(),
  798. UpdateTime: time.Now().Unix(),
  799. ExportTime: export_time,
  800. }
  801. service.CreateExportErrLog(&err_log)
  802. continue
  803. }
  804. }
  805. if schMapM["schedule_type"] == nil && reflect.TypeOf(schMapM["schedule_type"]).String() != "float64" {
  806. utils.ErrorLog("schedule_type")
  807. return
  808. }
  809. schedule_type := int64(schMapM["schedule_type"].(float64))
  810. if schedule_type <= 0 { //班次格式有误,插入日志
  811. log := models.ExportLog{
  812. LogType: 2,
  813. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  814. TotalNum: int64(len(total_schedule) + failed_total),
  815. FailNum: 1 + int64(failed_total),
  816. SuccessNum: int64(len(schedules)),
  817. CreateTime: time.Now().Unix(),
  818. UpdateTime: time.Now().Unix(),
  819. ExportTime: export_time,
  820. Status: 1,
  821. }
  822. service.CreateExportLog(&log)
  823. err_log := models.ExportErrLog{
  824. LogType: 2,
  825. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  826. ErrMsg: "第2行第" + schMapM["row"].(string) + "列的班次格式有误",
  827. Status: 1,
  828. CreateTime: time.Now().Unix(),
  829. UpdateTime: time.Now().Unix(),
  830. ExportTime: export_time,
  831. }
  832. service.CreateExportErrLog(&err_log)
  833. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeExportError)
  834. return
  835. } else {
  836. sch.ScheduleType = schedule_type
  837. }
  838. if schMapM["partition_name"] == nil || reflect.TypeOf(schMapM["partition_name"]).String() != "string" {
  839. utils.ErrorLog("partition_name")
  840. return
  841. }
  842. partition_name, _ := schMapM["partition_name"].(string)
  843. if len(partition_name) == 0 { //分区为空
  844. log := models.ExportLog{
  845. LogType: 2,
  846. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  847. TotalNum: int64(len(total_schedule) + failed_total),
  848. FailNum: 1 + int64(failed_total),
  849. SuccessNum: int64(len(schedules)),
  850. CreateTime: time.Now().Unix(),
  851. UpdateTime: time.Now().Unix(),
  852. ExportTime: export_time,
  853. Status: 1,
  854. }
  855. service.CreateExportLog(&log)
  856. err_log := models.ExportErrLog{
  857. LogType: 2,
  858. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  859. //ErrMsg: "第" + strconv.FormatInt(int64(schMapM["index"].(float64)) ,10)+ "行" + "的姓名不能为空",
  860. ErrMsg: "第" + strconv.FormatInt(int64(schMapM["index"].(float64)), 10) + "行第1列所属的分区格式有误",
  861. Status: 1,
  862. CreateTime: time.Now().Unix(),
  863. UpdateTime: time.Now().Unix(),
  864. ExportTime: export_time,
  865. }
  866. service.CreateExportErrLog(&err_log)
  867. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeExportError)
  868. return
  869. } else {
  870. zone, err := service.FindDeviceZoneByName(partition_name, this.GetAdminUserInfo().CurrentOrgId)
  871. if err == nil {
  872. sch.PartitionId = zone.ID
  873. } else if err == gorm.ErrRecordNotFound { //查不到数据,插入错误日志
  874. log := models.ExportLog{
  875. LogType: 2,
  876. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  877. TotalNum: int64(len(total_schedule) + failed_total),
  878. FailNum: 1 + int64(failed_total),
  879. SuccessNum: int64(len(schedules)),
  880. CreateTime: time.Now().Unix(),
  881. UpdateTime: time.Now().Unix(),
  882. ExportTime: export_time,
  883. Status: 1,
  884. }
  885. service.CreateExportLog(&log)
  886. err_log := models.ExportErrLog{
  887. LogType: 2,
  888. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  889. //ErrMsg: "第" + strconv.FormatInt(int64(schMapM["index"].(float64)) ,10)+ "行" + "的姓名不能为空",
  890. ErrMsg: "第" + strconv.FormatInt(int64(schMapM["index"].(float64)), 10) + "行第1列所属的分区格式有误",
  891. Status: 1,
  892. CreateTime: time.Now().Unix(),
  893. UpdateTime: time.Now().Unix(),
  894. ExportTime: export_time,
  895. }
  896. service.CreateExportErrLog(&err_log)
  897. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeExportError)
  898. return
  899. }
  900. }
  901. if schMapM["device_number_name"] == nil || reflect.TypeOf(schMapM["device_number_name"]).String() != "string" {
  902. utils.ErrorLog("device_number_name")
  903. return
  904. }
  905. device_number_name, _ := schMapM["device_number_name"].(string)
  906. if len(device_number_name) == 0 { //分区为空
  907. log := models.ExportLog{
  908. LogType: 2,
  909. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  910. TotalNum: int64(len(total_schedule) + failed_total),
  911. FailNum: 1 + int64(failed_total),
  912. SuccessNum: int64(len(schedules)),
  913. CreateTime: time.Now().Unix(),
  914. UpdateTime: time.Now().Unix(),
  915. ExportTime: export_time,
  916. Status: 1,
  917. }
  918. service.CreateExportLog(&log)
  919. err_log := models.ExportErrLog{
  920. LogType: 2,
  921. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  922. //ErrMsg: "第" + strconv.FormatInt(int64(schMapM["index"].(float64)) ,10)+ "行" + "的姓名不能为空",
  923. ErrMsg: "第" + strconv.FormatInt(int64(schMapM["index"].(float64)), 10) + "行第2列机号格式有误",
  924. Status: 1,
  925. CreateTime: time.Now().Unix(),
  926. UpdateTime: time.Now().Unix(),
  927. ExportTime: export_time,
  928. }
  929. service.CreateExportErrLog(&err_log)
  930. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeExportError)
  931. return
  932. } else {
  933. device, err := service.FindDeviceByName(device_number_name, this.GetAdminUserInfo().CurrentOrgId, sch.PartitionId)
  934. if err == nil {
  935. if len(device) == 0 { //没查到数据,插入错误日志
  936. log := models.ExportLog{
  937. LogType: 2,
  938. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  939. TotalNum: int64(len(total_schedule) + failed_total),
  940. FailNum: 1 + int64(failed_total),
  941. SuccessNum: int64(len(schedules)),
  942. CreateTime: time.Now().Unix(),
  943. UpdateTime: time.Now().Unix(),
  944. ExportTime: export_time,
  945. Status: 1,
  946. }
  947. service.CreateExportLog(&log)
  948. err_log := models.ExportErrLog{
  949. LogType: 2,
  950. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  951. ErrMsg: "第" + strconv.FormatInt(int64(schMapM["index"].(float64)), 10) + "行第2列机号格式有误",
  952. Status: 1,
  953. CreateTime: time.Now().Unix(),
  954. UpdateTime: time.Now().Unix(),
  955. ExportTime: export_time,
  956. }
  957. service.CreateExportErrLog(&err_log)
  958. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeExportError)
  959. return
  960. } else if len(device) == 1 {
  961. sch.BedId = device[0].ID
  962. } else if len(device) > 1 { //出现重名,插入错误日志
  963. log := models.ExportLog{
  964. LogType: 2,
  965. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  966. TotalNum: int64(len(total_schedule) + failed_total),
  967. FailNum: 1 + int64(failed_total),
  968. SuccessNum: int64(len(schedules)),
  969. CreateTime: time.Now().Unix(),
  970. UpdateTime: time.Now().Unix(),
  971. ExportTime: export_time,
  972. Status: 1,
  973. }
  974. service.CreateExportLog(&log)
  975. err_log := models.ExportErrLog{
  976. LogType: 2,
  977. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  978. ErrMsg: "出现两个床位号相同的床位",
  979. Status: 1,
  980. CreateTime: time.Now().Unix(),
  981. UpdateTime: time.Now().Unix(),
  982. ExportTime: export_time,
  983. }
  984. service.CreateExportErrLog(&err_log)
  985. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeExportError)
  986. return
  987. }
  988. }
  989. }
  990. if schMapM["schedule_week"] == nil || reflect.TypeOf(schMapM["schedule_week"]).String() != "string" {
  991. utils.ErrorLog("schedule_week")
  992. return
  993. }
  994. schedule_week, _ := schMapM["schedule_week"].(string)
  995. if len(schedule_week) == 0 { //周几为空则生成一条导入错误日志
  996. log := models.ExportLog{
  997. LogType: 2,
  998. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  999. TotalNum: int64(len(total_schedule) + failed_total),
  1000. FailNum: 1 + int64(failed_total),
  1001. SuccessNum: int64(len(schedules)),
  1002. CreateTime: time.Now().Unix(),
  1003. UpdateTime: time.Now().Unix(),
  1004. ExportTime: export_time,
  1005. Status: 1,
  1006. }
  1007. service.CreateExportLog(&log)
  1008. err_log := models.ExportErrLog{
  1009. LogType: 2,
  1010. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  1011. //ErrMsg: "第" + strconv.FormatInt(int64(schMapM["index"].(float64)) ,10)+ "行" + "的姓名不能为空",
  1012. ErrMsg: "第1行第" + schMapM["row"].(string) + "列的星期名格式有误",
  1013. Status: 1,
  1014. CreateTime: time.Now().Unix(),
  1015. UpdateTime: time.Now().Unix(),
  1016. ExportTime: export_time,
  1017. }
  1018. service.CreateExportErrLog(&err_log)
  1019. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeExportError)
  1020. return
  1021. } else if schedule_week == "0" { //周几不符合格式则生成一条导入错误日志
  1022. log := models.ExportLog{
  1023. LogType: 2,
  1024. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  1025. TotalNum: int64(len(total_schedule) + failed_total),
  1026. FailNum: 1 + int64(failed_total),
  1027. SuccessNum: int64(len(schedules)),
  1028. CreateTime: time.Now().Unix(),
  1029. UpdateTime: time.Now().Unix(),
  1030. ExportTime: export_time,
  1031. Status: 1,
  1032. }
  1033. service.CreateExportLog(&log)
  1034. err_log := models.ExportErrLog{
  1035. LogType: 2,
  1036. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  1037. ErrMsg: "第1行第" + schMapM["row"].(string) + "列的星期名格式有误",
  1038. Status: 1,
  1039. CreateTime: time.Now().Unix(),
  1040. UpdateTime: time.Now().Unix(),
  1041. ExportTime: export_time,
  1042. }
  1043. service.CreateExportErrLog(&err_log)
  1044. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeExportError)
  1045. return
  1046. } else {
  1047. schedule_week, _ := strconv.ParseInt(schedule_week, 10, 64)
  1048. sch.ScheduleWeek = schedule_week
  1049. }
  1050. if schMapM["schedule_date"] == nil || reflect.TypeOf(schMapM["schedule_date"]).String() != "string" {
  1051. utils.ErrorLog("schedule_date")
  1052. return
  1053. }
  1054. schedule_date, _ := schMapM["schedule_date"].(string)
  1055. if len(schedule_date) == 0 { //周几为空则生成一条导入错误日志
  1056. log := models.ExportLog{
  1057. LogType: 2,
  1058. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  1059. TotalNum: int64(len(total_schedule) + failed_total),
  1060. FailNum: 1 + int64(failed_total),
  1061. SuccessNum: int64(len(schedules)),
  1062. CreateTime: time.Now().Unix(),
  1063. UpdateTime: time.Now().Unix(),
  1064. ExportTime: export_time,
  1065. Status: 1,
  1066. }
  1067. service.CreateExportLog(&log)
  1068. err_log := models.ExportErrLog{
  1069. LogType: 2,
  1070. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  1071. ErrMsg: "第1行第" + schMapM["row"].(string) + "列的星期名格式有误",
  1072. Status: 1,
  1073. CreateTime: time.Now().Unix(),
  1074. UpdateTime: time.Now().Unix(),
  1075. ExportTime: export_time,
  1076. }
  1077. service.CreateExportErrLog(&err_log)
  1078. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeExportError)
  1079. return
  1080. } else if schedule_date == "0" { //周几不符合格式则生成一条导入错误日志
  1081. log := models.ExportLog{
  1082. LogType: 2,
  1083. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  1084. TotalNum: int64(len(total_schedule) + failed_total),
  1085. FailNum: 1 + int64(failed_total),
  1086. SuccessNum: int64(len(schedules)),
  1087. CreateTime: time.Now().Unix(),
  1088. UpdateTime: time.Now().Unix(),
  1089. ExportTime: export_time,
  1090. Status: 1,
  1091. }
  1092. service.CreateExportLog(&log)
  1093. err_log := models.ExportErrLog{
  1094. LogType: 2,
  1095. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  1096. ErrMsg: "第1行第" + schMapM["row"].(string) + "列的星期名格式有误",
  1097. Status: 1,
  1098. CreateTime: time.Now().Unix(),
  1099. UpdateTime: time.Now().Unix(),
  1100. ExportTime: export_time,
  1101. }
  1102. service.CreateExportErrLog(&err_log)
  1103. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeExportError)
  1104. return
  1105. } else {
  1106. date, _ := utils.ParseTimeStringToTime("2006-01-02", schedule_date)
  1107. sch.ScheduleDate = date.Unix()
  1108. }
  1109. sch.CreatedTime = time.Now().Unix()
  1110. sch.UpdatedTime = time.Now().Unix()
  1111. sch.ModeId = 1
  1112. sch.Status = 1
  1113. sch.UserOrgId = this.GetAdminUserInfo().CurrentOrgId
  1114. sch.IsExport = 1
  1115. schedules = append(schedules, &sch)
  1116. }
  1117. errLogs, _ := service.FindSchedualExportLog(this.GetAdminUserInfo().CurrentOrgId, export_time)
  1118. if len(schedules) > 0 {
  1119. schedule_date := time.Now().Format("2006-01-02")
  1120. date, _ := utils.ParseTimeStringToTime("2006-01-02", schedule_date)
  1121. clear_schedule_date := date.Unix() //根据日期去清除,该日期未来的排班数据
  1122. err := service.UpdateScheduleStatus(clear_schedule_date, this.GetAdminUserInfo().CurrentOrgId)
  1123. if err == nil {
  1124. for _, item := range schedules {
  1125. service.CreateSchedule(item)
  1126. }
  1127. log := models.ExportLog{
  1128. LogType: 2,
  1129. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  1130. TotalNum: int64(len(total_schedule) + failed_total),
  1131. FailNum: int64(len(errLogs) + failed_total),
  1132. SuccessNum: int64(len(schedules)),
  1133. CreateTime: time.Now().Unix(),
  1134. UpdateTime: time.Now().Unix(),
  1135. ExportTime: export_time,
  1136. Status: 1,
  1137. }
  1138. service.CreateExportLog(&log)
  1139. if failed_total > 0 {
  1140. err_log := models.ExportErrLog{
  1141. LogType: 2,
  1142. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  1143. ErrMsg: "当前日期(" + time.Now().Format("2006-01-02") + " " + GetWeekString(time.Now().Weekday().String()) +
  1144. ")及以前的排班数据" + strconv.FormatInt(int64(failed_total), 10) + "条,不执行导入",
  1145. Status: 1,
  1146. CreateTime: time.Now().Unix(),
  1147. UpdateTime: time.Now().Unix(),
  1148. ExportTime: export_time,
  1149. }
  1150. service.CreateExportErrLog(&err_log)
  1151. }
  1152. this.ServeSuccessJSON(map[string]interface{}{
  1153. "msg": "导入成功",
  1154. "total_num": len(total_schedule) + failed_total,
  1155. "success_num": len(schedules),
  1156. "fail_num": int64(len(errLogs)),
  1157. })
  1158. } else {
  1159. log := models.ExportLog{
  1160. LogType: 2,
  1161. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  1162. TotalNum: int64(len(total_schedule) + failed_total),
  1163. FailNum: int64(len(errLogs) + failed_total),
  1164. SuccessNum: int64(len(schedules)),
  1165. CreateTime: time.Now().Unix(),
  1166. UpdateTime: time.Now().Unix(),
  1167. ExportTime: export_time,
  1168. Status: 1,
  1169. }
  1170. service.CreateExportLog(&log)
  1171. if failed_total > 0 {
  1172. err_log := models.ExportErrLog{
  1173. LogType: 2,
  1174. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  1175. ErrMsg: "当前日期(" + time.Now().Format("2006-01-02") + " " + GetWeekString(time.Now().Weekday().String()) +
  1176. ")及以前的排班数据" + strconv.FormatInt(int64(failed_total), 10) + "条,不执行导入",
  1177. Status: 1,
  1178. CreateTime: time.Now().Unix(),
  1179. UpdateTime: time.Now().Unix(),
  1180. ExportTime: export_time,
  1181. }
  1182. service.CreateExportErrLog(&err_log)
  1183. }
  1184. this.ServeSuccessJSON(map[string]interface{}{
  1185. "msg": "导入成功",
  1186. "total_num": len(total_schedule),
  1187. "success_num": len(schedules),
  1188. "fail_num": int64(len(errLogs)),
  1189. })
  1190. }
  1191. } else {
  1192. log := models.ExportLog{
  1193. LogType: 2,
  1194. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  1195. TotalNum: int64(len(total_schedule) + failed_total),
  1196. FailNum: int64(len(errLogs) + failed_total),
  1197. SuccessNum: int64(len(schedules)),
  1198. CreateTime: time.Now().Unix(),
  1199. UpdateTime: time.Now().Unix(),
  1200. ExportTime: export_time,
  1201. Status: 1,
  1202. }
  1203. service.CreateExportLog(&log)
  1204. if failed_total > 0 {
  1205. err_log := models.ExportErrLog{
  1206. LogType: 2,
  1207. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  1208. ErrMsg: "当前日期(" + time.Now().Format("2006-01-02") + " " + GetWeekString(time.Now().Weekday().String()) +
  1209. ")及以前的排班数据" + strconv.FormatInt(int64(failed_total), 10) + "条,不执行导入",
  1210. Status: 1,
  1211. CreateTime: time.Now().Unix(),
  1212. UpdateTime: time.Now().Unix(),
  1213. ExportTime: export_time,
  1214. }
  1215. service.CreateExportErrLog(&err_log)
  1216. }
  1217. this.ServeSuccessJSON(map[string]interface{}{
  1218. "msg": "导入成功",
  1219. "total_num": len(total_schedule),
  1220. "success_num": len(schedules),
  1221. "fail_num": int64(len(errLogs)),
  1222. })
  1223. }
  1224. }
  1225. }
  1226. func (this *ScheduleApiController) ExportScheduleTemplate() {
  1227. dataBody := make(map[string]interface{}, 0)
  1228. err := json.Unmarshal(this.Ctx.Input.RequestBody, &dataBody)
  1229. if err != nil {
  1230. utils.ErrorLog(err.Error())
  1231. return
  1232. }
  1233. utils.ErrorLog("%v", dataBody)
  1234. export_time := time.Now().Unix()
  1235. var schedules []*models.PatientScheduleTemplateItem
  1236. var total_schedule []interface{}
  1237. patients, _, _ := service.GetAllPatientList(this.GetAdminUserInfo().CurrentOrgId)
  1238. if dataBody["schedule"] != nil || reflect.TypeOf(dataBody["schedule"]).String() == "[]interface {}" {
  1239. tempSchedule := dataBody["schedule"].([]interface{})
  1240. total_schedule = tempSchedule
  1241. for _, schMap := range tempSchedule {
  1242. schMapM := schMap.(map[string]interface{})
  1243. var sch models.PatientScheduleTemplateItem
  1244. if schMapM["name"] == nil || reflect.TypeOf(schMapM["name"]).String() != "string" {
  1245. utils.ErrorLog("name")
  1246. return
  1247. }
  1248. name, _ := schMapM["name"].(string)
  1249. fmt.Println(name)
  1250. if len(name) == 0 { //名字为空则生成一条导入错误日志
  1251. err_log := models.ExportErrLog{
  1252. LogType: 3,
  1253. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  1254. ErrMsg: "第" + strconv.FormatInt(int64(schMapM["index"].(float64)), 10) + "行,第" + schMapM["row"].(string) + "列的姓名不能为空",
  1255. Status: 1,
  1256. CreateTime: time.Now().Unix(),
  1257. UpdateTime: time.Now().Unix(),
  1258. ExportTime: export_time,
  1259. }
  1260. service.CreateExportErrLog(&err_log)
  1261. continue
  1262. } else {
  1263. var patient []*models.Patients
  1264. for _, item := range patients {
  1265. if strings.Replace(item.Name, " ", "", -1) == name {
  1266. patient = append(patient, item)
  1267. }
  1268. }
  1269. if len(patient) == 0 { //错误日志
  1270. err_log := models.ExportErrLog{
  1271. LogType: 3,
  1272. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  1273. //ErrMsg: "第" + strconv.FormatInt(int64(schMapM["index"].(float64)), 10) + "行,第" + schMapM["row"].(string) + "列的姓名在系统中不存在,请在系统中添加该病人",
  1274. ErrMsg: "第" + strconv.FormatInt(int64(schMapM["index"].(float64)), 10) + "行,第" + schMapM["row"].(string) + "列患者姓名为\"" + name + "\"在系统中不存在,请在系统中添加该患者",
  1275. Status: 1,
  1276. CreateTime: time.Now().Unix(),
  1277. UpdateTime: time.Now().Unix(),
  1278. ExportTime: export_time,
  1279. }
  1280. service.CreateExportErrLog(&err_log)
  1281. continue
  1282. } else if len(patient) == 1 { //
  1283. sch.PatientID = patient[0].ID
  1284. } else if len(patient) > 1 { //出现同名的情况
  1285. err_log := models.ExportErrLog{
  1286. LogType: 3,
  1287. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  1288. //ErrMsg: "第" + strconv.FormatInt(int64(schMapM["index"].(float64)), 10) + "行,第" + schMapM["row"].(string) + "列" + "患者姓名为" + name + "在当前机构中存在重名,无法确定到具体患者",
  1289. ErrMsg: "第" + strconv.FormatInt(int64(schMapM["index"].(float64)), 10) + "行,第" + schMapM["row"].(string) + "列患者姓名为\"" + name + "\"在当前机构中存在重名,无法确定到具体患者",
  1290. Status: 1,
  1291. CreateTime: time.Now().Unix(),
  1292. UpdateTime: time.Now().Unix(),
  1293. ExportTime: export_time,
  1294. }
  1295. service.CreateExportErrLog(&err_log)
  1296. continue
  1297. }
  1298. }
  1299. if schMapM["schedule_type"] == nil && reflect.TypeOf(schMapM["schedule_type"]).String() != "float64" {
  1300. utils.ErrorLog("schedule_type")
  1301. return
  1302. }
  1303. schedule_type := int64(schMapM["schedule_type"].(float64))
  1304. if schedule_type <= 0 { //班次格式有误,插入日志
  1305. log := models.ExportLog{
  1306. LogType: 3,
  1307. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  1308. TotalNum: int64(len(total_schedule)),
  1309. FailNum: 1,
  1310. SuccessNum: int64(len(schedules)),
  1311. CreateTime: time.Now().Unix(),
  1312. UpdateTime: time.Now().Unix(),
  1313. ExportTime: export_time,
  1314. Status: 1,
  1315. }
  1316. service.CreateExportLog(&log)
  1317. err_log := models.ExportErrLog{
  1318. LogType: 3,
  1319. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  1320. ErrMsg: "第2行第" + schMapM["row"].(string) + "列的班次格式有误",
  1321. Status: 1,
  1322. CreateTime: time.Now().Unix(),
  1323. UpdateTime: time.Now().Unix(),
  1324. ExportTime: export_time,
  1325. }
  1326. service.CreateExportErrLog(&err_log)
  1327. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeExportError)
  1328. return
  1329. } else {
  1330. sch.TimeType = int8(schedule_type)
  1331. }
  1332. if schMapM["template_id"] == nil && reflect.TypeOf(schMapM["template_id"]).String() != "float64" {
  1333. utils.ErrorLog("template_id")
  1334. return
  1335. }
  1336. template_id := int64(schMapM["template_id"].(float64))
  1337. sch.TemplateID = template_id
  1338. if schMapM["device_number_name"] == nil || reflect.TypeOf(schMapM["device_number_name"]).String() != "string" {
  1339. utils.ErrorLog("device_number_name")
  1340. return
  1341. }
  1342. partition_name, _ := schMapM["partition_name"].(string)
  1343. zone, _ := service.FindDeviceZoneByName(partition_name, this.GetAdminUserInfo().CurrentOrgId)
  1344. device_number_name, _ := schMapM["device_number_name"].(string)
  1345. if len(device_number_name) == 0 { //分区为空
  1346. log := models.ExportLog{
  1347. LogType: 3,
  1348. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  1349. TotalNum: int64(len(total_schedule)),
  1350. FailNum: 1,
  1351. SuccessNum: int64(len(schedules)),
  1352. CreateTime: time.Now().Unix(),
  1353. UpdateTime: time.Now().Unix(),
  1354. ExportTime: export_time,
  1355. Status: 1,
  1356. }
  1357. service.CreateExportLog(&log)
  1358. err_log := models.ExportErrLog{
  1359. LogType: 3,
  1360. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  1361. //ErrMsg: "第" + strconv.FormatInt(int64(schMapM["index"].(float64)) ,10)+ "行" + "的姓名不能为空",
  1362. ErrMsg: "第" + strconv.FormatInt(int64(schMapM["index"].(float64)), 10) + "行第2列机号格式有误",
  1363. Status: 1,
  1364. CreateTime: time.Now().Unix(),
  1365. UpdateTime: time.Now().Unix(),
  1366. ExportTime: export_time,
  1367. }
  1368. service.CreateExportErrLog(&err_log)
  1369. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeExportError)
  1370. return
  1371. } else {
  1372. device, err := service.FindDeviceByNameTwo(device_number_name, this.GetAdminUserInfo().CurrentOrgId, zone.ID)
  1373. if err == nil {
  1374. if len(device) == 0 { //没查到数据,插入错误日志
  1375. log := models.ExportLog{
  1376. LogType: 3,
  1377. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  1378. TotalNum: int64(len(total_schedule)),
  1379. FailNum: 1,
  1380. SuccessNum: int64(len(schedules)),
  1381. CreateTime: time.Now().Unix(),
  1382. UpdateTime: time.Now().Unix(),
  1383. ExportTime: export_time,
  1384. Status: 1,
  1385. }
  1386. service.CreateExportLog(&log)
  1387. err_log := models.ExportErrLog{
  1388. LogType: 3,
  1389. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  1390. ErrMsg: "第" + strconv.FormatInt(int64(schMapM["index"].(float64)), 10) + "行第2列机号格式有误",
  1391. Status: 1,
  1392. CreateTime: time.Now().Unix(),
  1393. UpdateTime: time.Now().Unix(),
  1394. ExportTime: export_time,
  1395. }
  1396. service.CreateExportErrLog(&err_log)
  1397. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeExportError)
  1398. return
  1399. } else if len(device) == 1 {
  1400. sch.DeviceNumberID = device[0].ID
  1401. } else if len(device) > 1 { //出现重名,插入错误日志
  1402. log := models.ExportLog{
  1403. LogType: 3,
  1404. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  1405. TotalNum: int64(len(total_schedule)),
  1406. FailNum: 1,
  1407. SuccessNum: int64(len(schedules)),
  1408. CreateTime: time.Now().Unix(),
  1409. UpdateTime: time.Now().Unix(),
  1410. ExportTime: export_time,
  1411. Status: 1,
  1412. }
  1413. service.CreateExportLog(&log)
  1414. err_log := models.ExportErrLog{
  1415. LogType: 3,
  1416. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  1417. ErrMsg: "出现两个床位号相同的床位",
  1418. Status: 1,
  1419. CreateTime: time.Now().Unix(),
  1420. UpdateTime: time.Now().Unix(),
  1421. ExportTime: export_time,
  1422. }
  1423. service.CreateExportErrLog(&err_log)
  1424. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeExportError)
  1425. return
  1426. }
  1427. }
  1428. }
  1429. if schMapM["schedule_week"] == nil || reflect.TypeOf(schMapM["schedule_week"]).String() != "string" {
  1430. utils.ErrorLog("schedule_week")
  1431. return
  1432. }
  1433. schedule_week, _ := schMapM["schedule_week"].(string)
  1434. if len(schedule_week) == 0 { //周几为空则生成一条导入错误日志
  1435. log := models.ExportLog{
  1436. LogType: 3,
  1437. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  1438. TotalNum: int64(len(total_schedule)),
  1439. FailNum: 1,
  1440. SuccessNum: int64(len(schedules)),
  1441. CreateTime: time.Now().Unix(),
  1442. UpdateTime: time.Now().Unix(),
  1443. ExportTime: export_time,
  1444. Status: 1,
  1445. }
  1446. service.CreateExportLog(&log)
  1447. err_log := models.ExportErrLog{
  1448. LogType: 3,
  1449. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  1450. //ErrMsg: "第" + strconv.FormatInt(int64(schMapM["index"].(float64)) ,10)+ "行" + "的姓名不能为空",
  1451. ErrMsg: "第1行第" + schMapM["row"].(string) + "列的星期名格式有误",
  1452. Status: 1,
  1453. CreateTime: time.Now().Unix(),
  1454. UpdateTime: time.Now().Unix(),
  1455. ExportTime: export_time,
  1456. }
  1457. service.CreateExportErrLog(&err_log)
  1458. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeExportError)
  1459. return
  1460. } else if schedule_week == "0" { //周几不符合格式则生成一条导入错误日志
  1461. log := models.ExportLog{
  1462. LogType: 3,
  1463. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  1464. TotalNum: int64(len(total_schedule)),
  1465. FailNum: 1,
  1466. SuccessNum: int64(len(schedules)),
  1467. CreateTime: time.Now().Unix(),
  1468. UpdateTime: time.Now().Unix(),
  1469. ExportTime: export_time,
  1470. Status: 1,
  1471. }
  1472. service.CreateExportLog(&log)
  1473. err_log := models.ExportErrLog{
  1474. LogType: 3,
  1475. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  1476. ErrMsg: "第1行第" + schMapM["row"].(string) + "列的星期名格式有误",
  1477. Status: 1,
  1478. CreateTime: time.Now().Unix(),
  1479. UpdateTime: time.Now().Unix(),
  1480. ExportTime: export_time,
  1481. }
  1482. service.CreateExportErrLog(&err_log)
  1483. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeExportError)
  1484. return
  1485. } else {
  1486. schedule_week, _ := strconv.ParseInt(schedule_week, 10, 64)
  1487. sch.Weekday = int8(schedule_week)
  1488. }
  1489. sch.CreateTime = time.Now().Unix()
  1490. sch.ModifyTime = time.Now().Unix()
  1491. sch.TreatMode = 1
  1492. sch.Status = 1
  1493. sch.OrgID = this.GetAdminUserInfo().CurrentOrgId
  1494. sch.IsExport = 1
  1495. schedules = append(schedules, &sch)
  1496. }
  1497. errLogs, _ := service.FindSchedualTemplateExportLog(this.GetAdminUserInfo().CurrentOrgId, export_time)
  1498. if len(schedules) > 0 {
  1499. err := service.UpdateScheduleTemplateStatus(this.GetAdminUserInfo().CurrentOrgId)
  1500. if err == nil {
  1501. for _, item := range schedules {
  1502. service.CreateScheduleTemplate(item)
  1503. }
  1504. log := models.ExportLog{
  1505. LogType: 3,
  1506. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  1507. TotalNum: int64(len(total_schedule)),
  1508. FailNum: int64(len(errLogs)),
  1509. SuccessNum: int64(len(schedules)),
  1510. CreateTime: time.Now().Unix(),
  1511. UpdateTime: time.Now().Unix(),
  1512. ExportTime: export_time,
  1513. Status: 1,
  1514. }
  1515. service.CreateExportLog(&log)
  1516. this.ServeSuccessJSON(map[string]interface{}{
  1517. "msg": "导入成功",
  1518. "total_num": len(total_schedule),
  1519. "success_num": len(schedules),
  1520. "fail_num": int64(len(errLogs)),
  1521. })
  1522. } else {
  1523. log := models.ExportLog{
  1524. LogType: 3,
  1525. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  1526. TotalNum: int64(len(total_schedule)),
  1527. FailNum: int64(len(errLogs)),
  1528. SuccessNum: int64(len(schedules)),
  1529. CreateTime: time.Now().Unix(),
  1530. UpdateTime: time.Now().Unix(),
  1531. ExportTime: export_time,
  1532. Status: 1,
  1533. }
  1534. service.CreateExportLog(&log)
  1535. this.ServeSuccessJSON(map[string]interface{}{
  1536. "msg": "导入成功",
  1537. "total_num": len(total_schedule),
  1538. "success_num": len(schedules),
  1539. "fail_num": int64(len(errLogs)),
  1540. })
  1541. }
  1542. } else {
  1543. log := models.ExportLog{
  1544. LogType: 3,
  1545. UserOrgId: this.GetAdminUserInfo().CurrentOrgId,
  1546. TotalNum: int64(len(total_schedule)),
  1547. FailNum: int64(len(errLogs)),
  1548. SuccessNum: int64(len(schedules)),
  1549. CreateTime: time.Now().Unix(),
  1550. UpdateTime: time.Now().Unix(),
  1551. ExportTime: export_time,
  1552. Status: 1,
  1553. }
  1554. service.CreateExportLog(&log)
  1555. this.ServeSuccessJSON(map[string]interface{}{
  1556. "msg": "导入成功",
  1557. "total_num": len(total_schedule),
  1558. "success_num": len(schedules),
  1559. "fail_num": int64(len(errLogs)),
  1560. })
  1561. }
  1562. }
  1563. }
  1564. func (c *ScheduleApiController) GetInitExcelInitDate() {
  1565. thisTime := time.Now()
  1566. thisTime1 := time.Now()
  1567. thisTime2 := time.Now()
  1568. thisTime1 = thisTime1.AddDate(0, 0, 7)
  1569. thisTime2 = thisTime2.AddDate(0, 0, 14)
  1570. weekDay := int(thisTime.Weekday())
  1571. if weekDay == 0 {
  1572. weekDay = 7
  1573. }
  1574. weekEnd := 7 - weekDay
  1575. weekStart := weekEnd - 6
  1576. days := make([]string, 0)
  1577. for index := weekStart; index <= weekEnd; index++ {
  1578. theDay := thisTime.AddDate(0, 0, index)
  1579. days = append(days, theDay.Format("2006-01-02"))
  1580. }
  1581. weekDay1 := int(thisTime1.Weekday())
  1582. if weekDay1 == 0 {
  1583. weekDay1 = 7
  1584. }
  1585. weekEnd1 := 7 - weekDay1
  1586. weekStart1 := weekEnd1 - 6
  1587. for index := weekStart1; index <= weekEnd1; index++ {
  1588. theDay := thisTime1.AddDate(0, 0, index)
  1589. days = append(days, theDay.Format("2006-01-02"))
  1590. }
  1591. weekDay2 := int(thisTime2.Weekday())
  1592. if weekDay2 == 0 {
  1593. weekDay2 = 7
  1594. }
  1595. weekEnd2 := 7 - weekDay2
  1596. weekStart2 := weekEnd2 - 6
  1597. for index := weekStart2; index <= weekEnd2; index++ {
  1598. theDay := thisTime2.AddDate(0, 0, index)
  1599. days = append(days, theDay.Format("2006-01-02"))
  1600. }
  1601. c.ServeSuccessJSON(map[string]interface{}{
  1602. "days": days,
  1603. })
  1604. return
  1605. }
  1606. func GetWeekString(week string) string {
  1607. var weekStr string
  1608. switch week {
  1609. case "Sunday":
  1610. weekStr = "周日"
  1611. break
  1612. case "Monday":
  1613. weekStr = "周一"
  1614. break
  1615. case "Tuesday":
  1616. weekStr = "周二"
  1617. break
  1618. case "Wednesday":
  1619. weekStr = "周三"
  1620. break
  1621. case "Thursday":
  1622. weekStr = "周四"
  1623. break
  1624. case "Friday":
  1625. weekStr = "周五"
  1626. break
  1627. case "Saturday":
  1628. weekStr = "周六"
  1629. break
  1630. default:
  1631. weekStr = ""
  1632. break
  1633. }
  1634. return weekStr
  1635. }
  1636. func (c *ScheduleApiController) SearchTemplateSchedulePatients() {
  1637. template_id, _ := c.GetInt64("template_id", 0)
  1638. keywords := c.GetString("keywords")
  1639. adminInfo := c.GetAdminUserInfo()
  1640. scheduleItems, _ := service.GetTemplateSchedualPatientsByKeywords(keywords, adminInfo.CurrentOrgId, template_id)
  1641. c.ServeSuccessJSON(map[string]interface{}{
  1642. "schdules": scheduleItems,
  1643. })
  1644. }
  1645. func (c *ScheduleApiController) CancelScheduleTemplate() {
  1646. item_id, _ := c.GetInt64("id")
  1647. adminInfo := c.GetAdminUserInfo()
  1648. err := service.UpdateNewScheduleTemplateStatus(item_id, adminInfo.CurrentOrgId)
  1649. if err == nil {
  1650. c.ServeSuccessJSON(map[string]interface{}{
  1651. "msg": "取消成功",
  1652. })
  1653. return
  1654. } else {
  1655. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeExportError)
  1656. return
  1657. }
  1658. }