schedule_api_controller.go 58KB

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