schedule_api_controller.go 61KB

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