schedule_api_controller.go 61KB

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