schedule_service.go 79KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954
  1. package service
  2. import (
  3. "fmt"
  4. "strconv"
  5. "strings"
  6. "time"
  7. "XT_New/models"
  8. "github.com/jinzhu/gorm"
  9. )
  10. // GetSchedulePartitionPanel 取出排班面板,分区-机号
  11. func GetSchedulePartitionPanel(orgID int64) (partitions []*models.Partition, err error) {
  12. err = readDb.Model(&models.DeviceZone{}).Preload("Jihaos", "org_id=? and status=1", orgID).Where("org_id=? and status=1", orgID).Order("").Find(&partitions).Error
  13. return
  14. }
  15. func GetSchedulePartitionPanelOne(orgID int64, partionId []string) (partitions []*models.Partition, err error) {
  16. err = readDb.Model(&models.DeviceZone{}).Preload("Jihaos", "org_id=? and status=1", orgID).Where("org_id=? and status=1", orgID).Find(&partitions).Error
  17. //}
  18. return
  19. }
  20. func GetSchedulePartitionPanelTwo(orgID int64, schIDs []string) (partitions []*models.Partition, err error) {
  21. if len(schIDs) == 0 {
  22. err = readDb.Model(&models.DeviceZone{}).Preload("Jihaos", "org_id=? and status=1", orgID).Where("org_id=? and status=1", orgID).Find(&partitions).Error
  23. }
  24. if len(schIDs) > 0 {
  25. err = readDb.Model(&models.DeviceZone{}).Preload("Jihaos", func(db *gorm.DB) *gorm.DB {
  26. return db.Where("org_id=? and status=1 and zone_id in (?)", orgID, schIDs).Order("sort desc")
  27. }).Where("org_id=? and status=1 and id in(?)", orgID, schIDs).Find(&partitions).Error
  28. }
  29. return
  30. }
  31. func GetPatientWithScheduleAndSolution(orgID int64, keywords string, weekStart, weekEnd, schedule, contagion int64) (patients []*models.Patients, err error) {
  32. db := readDb.Table("xt_patients as p").
  33. Preload("DialysisSolutions", "user_org_id=? and initiate_mode=1 and affirm_state=1 and parent_id=0 and status=1", orgID).
  34. Preload("Schedules", "user_org_id=? and schedule_date>=? and schedule_date<= ? and status=1", orgID, weekStart, weekEnd).
  35. Preload("Contagions", "status=1").
  36. Where("p.user_org_id=? and p.lapseto=1 and p.status=1", orgID)
  37. if len(keywords) > 0 {
  38. likeKey := "%" + keywords + "%"
  39. db = db.Where("p.name LIKE ? or p.first_letter LIKE ?", likeKey, likeKey)
  40. }
  41. if schedule == 1 {
  42. db = db.Where("EXISTS (?)", readDb.Table("xt_schedule as iss").Where("iss.patient_id=p.id and iss.schedule_date>=? and iss.schedule_date<= ? and iss.status=1", weekStart, weekEnd).QueryExpr())
  43. } else if schedule == 2 {
  44. db = db.Where("NOT EXISTS (?)", readDb.Table("xt_schedule as iss").Where("iss.patient_id=p.id and iss.schedule_date>=? and iss.schedule_date<= ? and iss.status=1", weekStart, weekEnd).QueryExpr())
  45. }
  46. if contagion > 0 {
  47. db = db.Where("EXISTS (?)", readDb.Table("xt_patients_infectious_diseases as pid").Where("pid.patient_id=p.id and pid.disease_id=? and pid.status=1", contagion).QueryExpr())
  48. }
  49. err = db.Find(&patients).Error
  50. return
  51. }
  52. func GetPatientWithScheduleAndSolutionTwo(orgID int64, keywords string, weekStart, weekEnd, schedule, contagion int64) (patients []*models.Patients, err error) {
  53. db := readDb.Table("xt_patients as p").
  54. Preload("DialysisSolutions", "user_org_id=? and initiate_mode=1 and affirm_state=1 and parent_id=0 and status=1", orgID).
  55. //Preload("Schedules", "user_org_id=? and schedule_date>=? and schedule_date<= ? and status=1", orgID, weekStart, weekEnd).
  56. Where("p.user_org_id=? and p.lapseto=1 and p.status=1", orgID)
  57. if len(keywords) > 0 {
  58. likeKey := "%" + keywords + "%"
  59. db = db.Where("p.name LIKE ?", likeKey)
  60. }
  61. err = db.Find(&patients).Error
  62. return
  63. }
  64. func GetWeekSchedule(orgID int64, start, end int64) (schedules []*models.Schedule, err error) {
  65. err = readDb.Table("xt_schedule as s").Preload("PatientInfectiousDiseases", "status = 1 ").
  66. Joins("JOIN xt_patients as p ON p.id = s.patient_id").
  67. Where("s.user_org_id=? and s.schedule_date>=? and s.schedule_date<=? and s.status=1", orgID, start, end).
  68. Select("s.id, s.user_org_id, s.partition_id, s.bed_id, s.patient_id, s.schedule_date, s.schedule_type, s.schedule_week, s.mode_id, s.status, s.created_time, s.updated_time,s.dialysis_machine_name, p.name as patient").
  69. Order("updated_time asc").
  70. Find(&schedules).Error
  71. return
  72. }
  73. func GetWeekScheduleOne(orgID int64, start, end int64, partionid int64, scheduletype int64) (schedules []*models.Schedule, err error) {
  74. db := readDb.Table("xt_schedule as s")
  75. if partionid > 0 {
  76. db = db.Where("s.partition_id = ?", partionid)
  77. }
  78. if scheduletype > 0 {
  79. db = db.Where("s.schedule_type = ?", scheduletype)
  80. }
  81. err = db.Preload("PatientInfectiousDiseases", "status = 1 ").
  82. Joins("JOIN xt_patients as p ON p.id = s.patient_id").
  83. Where("s.user_org_id=? and s.schedule_date>=? and s.schedule_date<=? and s.status=1", orgID, start, end).
  84. Select("s.id, s.user_org_id, s.partition_id, s.bed_id, s.patient_id, s.schedule_date, s.schedule_type, s.schedule_week, s.mode_id, s.status, s.created_time,s.dialysis_machine_name, s.updated_time, p.name as patient").
  85. Order("updated_time asc").
  86. Find(&schedules).Error
  87. return
  88. }
  89. func GetWeekScheduleTwo(orgID int64, start, end int64, schIds []string, scheduletype int64) (schedules []*models.ScheduleTwo, err error) {
  90. db := readDb.Table("xt_schedule as s")
  91. if len(schIds) > 0 {
  92. db = db.Where("s.partition_id in (?)", schIds)
  93. }
  94. if scheduletype > 0 {
  95. db = db.Where("s.schedule_type = ?", scheduletype)
  96. }
  97. err = db.Preload("PatientInfectiousDiseases", "status = 1 ").
  98. Joins("JOIN xt_patients as p ON p.id = s.patient_id").
  99. Where("s.user_org_id=? and s.schedule_date>=? and s.schedule_date<=? and s.status=1", orgID, start, end).
  100. Select("s.id, s.user_org_id, s.partition_id, s.bed_id, s.patient_id, s.schedule_date, s.schedule_type, s.schedule_week, s.mode_id, s.status, s.created_time, s.updated_time,s.dialysis_machine_name, p.name as patient").
  101. Order("updated_time asc").
  102. Find(&schedules).Error
  103. return
  104. }
  105. func GetWeekScheduleFive(orgID int64, start, end int64, schIds []string, scheduletype int64) (schedules []*models.ScheduleTwo, err error) {
  106. db := readDb.Table("xt_schedule as s").Where("s.status=1 and s.dialysis_machine_name=''")
  107. if len(schIds) > 0 {
  108. db = db.Where("s.partition_id in (?)", schIds)
  109. }
  110. if scheduletype > 0 {
  111. db = db.Where("s.schedule_type = ?", scheduletype)
  112. }
  113. err = db.Preload("PatientInfectiousDiseases", "status = 1 ").
  114. Joins("JOIN xt_patients as p ON p.id = s.patient_id").
  115. Where("s.user_org_id=? and s.schedule_date>=? and s.schedule_date<=? and s.status=1", orgID, start, end).
  116. Select("s.id, s.user_org_id, s.partition_id, s.bed_id, s.patient_id, s.schedule_date, s.schedule_type, s.schedule_week, s.mode_id, s.status, s.created_time, s.updated_time,s.dialysis_machine_name, p.name as patient").
  117. Order("updated_time asc").
  118. Find(&schedules).Error
  119. return
  120. }
  121. func UpdateSchOne(id int64, dialysis_machine_name string) (err error) {
  122. err = writeDb.Model(&models.ScheduleTwo{}).Where("status=1 AND id = ?", id).Updates(map[string]interface{}{"dialysis_machine_name": dialysis_machine_name}).Error
  123. return
  124. }
  125. func GetScheduleTotalCount(orgID int64, start, end int64, schIds []string, scheduletype int64) (schedules []*models.MySchedule, err error) {
  126. db := readDb.Table("xt_schedule as s").Where("status= 1 and (mode_id = 2 or mode_id= 3)")
  127. if len(schIds) > 0 {
  128. db = db.Where("s.partition_id in (?)", schIds)
  129. }
  130. if scheduletype > 0 {
  131. db = db.Where("s.schedule_type = ?", scheduletype)
  132. }
  133. if orgID > 0 {
  134. db = db.Where("s.user_org_id = ?", orgID)
  135. }
  136. if start > 0 {
  137. db = db.Where("s.schedule_date>=?", start)
  138. }
  139. if end > 0 {
  140. db = db.Where("s.schedule_date<=?", end)
  141. }
  142. err = db.Select("Count(id) as count,schedule_date,mode_id,user_org_id,schedule_week,schedule_type").Group("schedule_date,schedule_type,mode_id").Find(&schedules).Error
  143. return schedules, err
  144. }
  145. func GetWeekScheduleThree(orgID int64, start, end int64, schIds []string, scheduletype int64, WeekDayIds []string) (schedules []*models.ScheduleTwo, err error) {
  146. db := readDb.Table("xt_schedule as s")
  147. if len(schIds) > 0 {
  148. db = db.Where("s.partition_id in (?)", schIds)
  149. }
  150. if scheduletype > 0 {
  151. db = db.Where("s.schedule_type = ?", scheduletype)
  152. }
  153. if len(WeekDayIds) > 0 {
  154. db = db.Where("s.schedule_week in(?)", WeekDayIds)
  155. }
  156. err = db.Preload("PatientInfectiousDiseases", "status = 1 ").
  157. Preload("DialysisSolution", "status= 1 and solution_status = 1").
  158. Joins("JOIN xt_patients as p ON p.id = s.patient_id").
  159. Where("s.user_org_id=? and s.schedule_date>=? and s.schedule_date<=? and s.status=1", orgID, start, end).
  160. Select("s.id, s.user_org_id, s.partition_id, s.bed_id, s.patient_id, s.schedule_date, s.schedule_type, s.schedule_week, s.mode_id, s.status, s.created_time, s.updated_time,s.dialysis_machine_name, p.name as patient").
  161. Order("updated_time asc").
  162. Find(&schedules).Error
  163. return
  164. }
  165. func GetWeekScheduleFour(orgID int64, start, end int64, schIds []string, scheduletype int64, WeekDayIds []string) (schedules []*models.ScheduleThree, err error) {
  166. db := readDb.Table("xt_schedule as s")
  167. if len(schIds) > 0 {
  168. db = db.Where("s.partition_id in (?)", schIds)
  169. }
  170. if scheduletype > 0 {
  171. db = db.Where("s.schedule_type = ?", scheduletype)
  172. }
  173. if len(WeekDayIds) > 0 {
  174. db = db.Where("s.schedule_week in(?)", WeekDayIds)
  175. }
  176. err = db.Preload("DeviceNumber", "status = 1 and org_id =?", orgID).
  177. Preload("DialysisSolution", "status= 1 and solution_status = 1").
  178. Joins("JOIN xt_patients as p ON p.id = s.patient_id").
  179. Where("s.user_org_id=? and s.schedule_date>=? and s.schedule_date<=? and s.status=1", orgID, start, end).
  180. Select("s.id, s.user_org_id, s.partition_id, s.bed_id, s.patient_id, s.schedule_date, s.schedule_type, s.schedule_week, s.mode_id, s.status, s.created_time, s.updated_time,s.dialysis_machine_name, p.name as patient").
  181. Order("updated_time asc").
  182. Find(&schedules).Error
  183. return
  184. }
  185. func GetDaySchedule(orgID, start, end, patientID int64) (schedule models.Schedule, err error) {
  186. err = readDb.Model(&models.Schedule{}).Where("user_org_id=? and patient_id=? and schedule_date>=? and schedule_date<=? and status=1", orgID, patientID, start, end).First(&schedule).Error
  187. return
  188. }
  189. func GetDayScheduleTwo(orgID, start, patientID int64) (schedule models.Schedule, err error) {
  190. err = readDb.Model(&models.Schedule{}).Where("user_org_id=? and patient_id=? and schedule_date = ? and status=1", orgID, patientID, start).Preload("DialysisOrder", "status = 1").First(&schedule).Error
  191. return
  192. }
  193. func GetDayScheduleByBedid(orgID, start, bed_id int64, schedule_type int64) (schedule models.Schedule, err error) {
  194. err = readDb.Model(&models.Schedule{}).Where("user_org_id=? and schedule_date=? and bed_id=? and status=1 AND schedule_type = ?", orgID, start, bed_id, schedule_type).Preload("DialysisOrder", "status = 1").First(&schedule).Error
  195. return
  196. }
  197. func GetDayScheduleByOrder(orgID, start, bed_id int64, schedule_type int64) (schedule models.DialysisOrder, err error) {
  198. err = readDb.Model(&models.DialysisOrder{}).Where("user_org_id=? and dialysis_date=? and bed_id=? and status=1 AND schedual_type = ?", orgID, start, bed_id, schedule_type).First(&schedule).Error
  199. return
  200. }
  201. func GetSourceSch(orgId int64, patient_id int64, record_date int64) (schedule models.Schedule, err error) {
  202. err = readDb.Model(&models.Schedule{}).Where("user_org_id=? and schedule_date=? and patient_id =? and status=1", orgId, record_date, patient_id).First(&schedule).Error
  203. return
  204. }
  205. func GetPatientScheduleFormDay(orgID, start, end, patientID int64) (schedules []*models.PatientSchedule, err error) {
  206. err = readDb.Table("xt_schedule as s").
  207. Preload("DeviceZone", "org_id=? and status=1", orgID).
  208. Preload("DeviceNumber", "org_id=? and status=1", orgID).
  209. Where("s.patient_id =? and s.user_org_id=? and s.schedule_date>=? and s.schedule_date < ? and s.status=1", patientID, orgID, start, end).
  210. Find(&schedules).Error
  211. return
  212. }
  213. func GetNextSchedule(orgID, end, patientID int64) (schedules []*models.PatientSchedule, err error) {
  214. err = readDb.Table("xt_schedule as s").
  215. Preload("DeviceZone", "org_id=? and status=1", orgID).
  216. Preload("DeviceNumber", "org_id=? and status=1", orgID).
  217. Where("s.patient_id =? and s.user_org_id=? and s.schedule_date>=? and s.status=1", patientID, orgID, end).
  218. Find(&schedules).Error
  219. return
  220. }
  221. func GetNextScheduleByList(patient_id int64, schedule_date int64, user_org_id int64) (models.SchedulePatientsOne, error) {
  222. patientsOne := models.SchedulePatientsOne{}
  223. err := XTReadDB().Where("patient_id = ? and schedule_date >? and user_org_id = ? and status=1", patient_id, schedule_date, user_org_id).Order("schedule_date asc").First(&patientsOne).Error
  224. return patientsOne, err
  225. }
  226. func GetPointSchedule(orgID, scheduleDate, scheduleWeek, scheduleType, bedId int64) (schedule models.Schedule, err error) {
  227. err = readDb.Model(&models.Schedule{}).Where("user_org_id=? and schedule_date=? and bed_id=? and schedule_week=? AND schedule_type=? and status=1", orgID, scheduleDate, bedId, scheduleWeek, scheduleType).First(&schedule).Error
  228. if err == gorm.ErrRecordNotFound {
  229. err = nil
  230. return
  231. }
  232. return
  233. }
  234. func CreateSchedule(m *models.Schedule, id int64) (err error) {
  235. utx := XTWriteDB().Begin()
  236. schedule := models.XtSchedule{}
  237. err = utx.Model(&schedule).Where("id=? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  238. if err != nil {
  239. utx.Rollback()
  240. return err
  241. }
  242. err = utx.Create(&m).Error
  243. if err != nil {
  244. utx.Rollback()
  245. return err
  246. }
  247. utx.Commit()
  248. return err
  249. }
  250. func DeleteScheduleExist(scheduleData int64, user_org_id int64) (models.XtSchedule, error) {
  251. schedule := models.XtSchedule{}
  252. err := XTWriteDB().Model(&schedule).Where("schedule_date= ? and user_org_id = ? and status = 1", scheduleData, user_org_id).Updates(map[string]interface{}{"status": 0}).Error
  253. return schedule, err
  254. }
  255. func GetTodayScheduleIsExist(patientid int64, schedudate int64, orgid int64, mode_id int64, schedule_type int64) (*models.XtSchedule, error) {
  256. var scheudle models.XtSchedule
  257. var err error
  258. err = XTReadDB().Model(&scheudle).Where("patient_id = ? and schedule_date= ? and user_org_id = ? and status = 1 and mode_id = ? and schedule_type = ?", patientid, schedudate, orgid, mode_id, schedule_type).Find(&scheudle).Error
  259. if err == gorm.ErrRecordNotFound {
  260. return nil, err
  261. }
  262. if err != nil {
  263. return nil, err
  264. }
  265. return &scheudle, nil
  266. }
  267. func GetTodayScheduleIsExistOne(patientid int64, schedudate int64, orgid int64) (*models.XtSchedule, error) {
  268. var scheudle models.XtSchedule
  269. var err error
  270. err = XTReadDB().Model(&scheudle).Where("patient_id = ? and schedule_date= ? and user_org_id = ? and status = 1", patientid, schedudate, orgid).Find(&scheudle).Error
  271. if err == gorm.ErrRecordNotFound {
  272. return nil, err
  273. }
  274. if err != nil {
  275. return nil, err
  276. }
  277. return &scheudle, nil
  278. }
  279. func GetPatientByBed(schedule_date int64, bed_id int64, scheduletype int64, orgid int64) (*models.XtSchedule, error) {
  280. var scheudle models.XtSchedule
  281. var err error
  282. err = XTReadDB().Model(&scheudle).Where(" schedule_date= ? and bed_id = ? and schedule_type = ? and user_org_id = ? and status = 1", schedule_date, bed_id, scheduletype, orgid).Find(&scheudle).Error
  283. if err == gorm.ErrRecordNotFound {
  284. return nil, err
  285. }
  286. if err != nil {
  287. return nil, err
  288. }
  289. return &scheudle, nil
  290. }
  291. func ModeFyScheduleById(schedule_date int64, bedid int64, scheduletype int64, orgid int64) error {
  292. schedule := models.XtSchedule{}
  293. err := XTWriteDB().Model(&schedule).Where("schedule_date = ? and bed_id =? and schedule_type = ? and status = 1 and user_org_id = ?", schedule_date, bedid, scheduletype, orgid).Updates(map[string]interface{}{"status": 0}).Error
  294. return err
  295. }
  296. func ModeScheduleByTime(startime int64, endtime int64, orgid int64) error {
  297. schedule := models.XtSchedule{}
  298. err := XTWriteDB().Model(&schedule).Where("schedule_date >? and schedule_date<=? and status = 1 and user_org_id = ?", startime, endtime, orgid).Updates(map[string]interface{}{"status": 0}).Error
  299. return err
  300. }
  301. func ModeScheduleByTimeOne(startime int64, endtime int64, orgid int64) error {
  302. schedule := models.XtSchedule{}
  303. err := XTWriteDB().Model(&schedule).Where("schedule_date >=? and schedule_date<=? and status = 1 and user_org_id = ?", startime, endtime, orgid).Updates(map[string]interface{}{"status": 0}).Error
  304. return err
  305. }
  306. func UpdateScheduleByOrgId(patientid int64, scheduledate int64, orgid int64, mode_id int64, schedule_type int64, sch *models.XtSchedule) error {
  307. err := XTWriteDB().Model(&sch).Where("patient_id = ? and schedule_date= ? and user_org_id = ? and status = 1 and mode_id = ? and schedule_type = ?", patientid, scheduledate, orgid, mode_id, schedule_type).Updates(map[string]interface{}{"partition_id": sch.PartitionId, "bed_id": sch.BedId, "schedule_type": sch.ScheduleType, "schedule_week": sch.ScheduleWeek, "mode_id": sch.ModeId, "updated_time": time.Now().Unix()}).Error
  308. return err
  309. }
  310. func UpdateScheduleByOrgIdOne(patientid int64, scheduledate int64, orgid int64, sch *models.XtSchedule) error {
  311. err := XTWriteDB().Model(&sch).Where("patient_id = ? and schedule_date= ? and user_org_id = ? and status = 1", patientid, scheduledate, orgid).Updates(map[string]interface{}{"partition_id": sch.PartitionId, "bed_id": sch.BedId, "schedule_type": sch.ScheduleType, "schedule_week": sch.ScheduleWeek, "mode_id": sch.ModeId, "updated_time": time.Now().Unix(), "dialysis_machine_name": sch.DialysisMachineName}).Error
  312. return err
  313. }
  314. func GetSchedule(orgID, id int64) (*models.Schedule, error) {
  315. var schedule models.Schedule
  316. var err error
  317. err = readDb.Model(&models.Schedule{}).Where("id = ? and user_org_id=? and status=1", id, orgID).First(&schedule).Error
  318. if err != nil {
  319. return nil, err
  320. }
  321. return &schedule, nil
  322. }
  323. func GetScheduleNight(orgID, id int64) (*models.Schedule, error) {
  324. var schedule models.Schedule
  325. var err error
  326. err = readDb.Model(&models.Schedule{}).Where("id = ? and user_org_id=?", id, orgID).First(&schedule).Error
  327. if err != nil {
  328. return nil, err
  329. }
  330. return &schedule, nil
  331. }
  332. func UpdateSchedule(m *models.Schedule) error {
  333. utx := XTWriteDB().Begin()
  334. err := utx.Save(&m).Error
  335. utx.Commit()
  336. return err
  337. }
  338. func UpdateScheduleOne(id int64, schedule models.Schedule) error {
  339. utx := XTWriteDB().Begin()
  340. err := XTWriteDB().Model(&schedule).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"partition_id": schedule.PartitionId, "bed_id": schedule.BedId, "schedule_type": schedule.ScheduleType}).Error
  341. utx.Commit()
  342. return err
  343. }
  344. // 参数一:当前操作的患者的排班 参数二被替换床位对应的排班
  345. func UpdateScheduleTwo(schone models.Schedule, schtwo models.Schedule) (err error) {
  346. utx := XTWriteDB().Begin()
  347. //床位
  348. var tempBedID int64
  349. var tempBedIDTwo int64
  350. //分區
  351. var tempPID int64
  352. var tempPIDTwo int64
  353. //上下午
  354. var tempTypeID int64
  355. var tempTypeTwo int64
  356. tempBedID = schone.BedId
  357. tempBedIDTwo = schtwo.BedId
  358. tempPID = schone.PartitionId
  359. tempPIDTwo = schtwo.PartitionId
  360. tempTypeID = schone.ScheduleType
  361. tempTypeTwo = schtwo.ScheduleType
  362. schone.BedId = tempBedIDTwo
  363. schtwo.BedId = tempBedID
  364. schone.PartitionId = tempPIDTwo
  365. schtwo.PartitionId = tempPID
  366. schone.ScheduleType = tempTypeTwo
  367. schtwo.ScheduleType = tempTypeID
  368. if err = utx.Model(&models.Schedule{}).Where("id = ?", schone.ID).Updates(map[string]interface{}{"partition_id": schone.PartitionId, "bed_id": schone.BedId, "schedule_type": schone.ScheduleType, "is_export": 90000}).Error; err != nil {
  369. utx.Rollback()
  370. return err
  371. }
  372. if err = utx.Model(&models.Schedule{}).Where("id = ?", schtwo.ID).Updates(map[string]interface{}{"partition_id": schtwo.PartitionId, "bed_id": schtwo.BedId, "schedule_type": schtwo.ScheduleType, "is_export": 90000}).Error; err != nil {
  373. utx.Rollback()
  374. return err
  375. }
  376. utx.Commit()
  377. return err
  378. }
  379. func UpdateScheduleThree(schone *models.Schedule, schtwo *models.Schedule) (err error) {
  380. utx := XTWriteDB().Begin()
  381. //床位
  382. var tempBedID int64
  383. var tempBedIDTwo int64
  384. //分區
  385. var tempPID int64
  386. var tempPIDTwo int64
  387. //上下午
  388. var tempTypeID int64
  389. var tempTypeTwo int64
  390. //日期
  391. var sch_date_one int64
  392. var sch_date_two int64
  393. //周数
  394. var sch_week_one int64
  395. var sch_week_two int64
  396. //原数据
  397. tempBedID = schone.BedId
  398. tempBedIDTwo = schtwo.BedId
  399. tempPID = schone.PartitionId
  400. tempPIDTwo = schtwo.PartitionId
  401. tempTypeID = schone.ScheduleType
  402. tempTypeTwo = schtwo.ScheduleType
  403. sch_date_one = schone.ScheduleDate
  404. sch_date_two = schtwo.ScheduleDate
  405. sch_week_one = schone.ScheduleWeek
  406. sch_week_two = schtwo.ScheduleWeek
  407. //交换数据
  408. schone.BedId = tempBedIDTwo
  409. schtwo.BedId = tempBedID
  410. schone.PartitionId = tempPIDTwo
  411. schtwo.PartitionId = tempPID
  412. schone.ScheduleType = tempTypeTwo
  413. schtwo.ScheduleType = tempTypeID
  414. schone.ScheduleDate = sch_date_two
  415. schtwo.ScheduleDate = sch_date_one
  416. schone.ScheduleWeek = sch_week_two
  417. schtwo.ScheduleWeek = sch_week_one
  418. if err = utx.Model(&models.Schedule{}).Where("id = ?", schone.ID).Updates(map[string]interface{}{"partition_id": schone.PartitionId, "bed_id": schone.BedId, "schedule_type": schone.ScheduleType, "schedule_date": schone.ScheduleDate, "schedule_week": schone.ScheduleWeek, "is_export": 80000}).Error; err != nil {
  419. utx.Rollback()
  420. return err
  421. }
  422. if err = utx.Model(&models.Schedule{}).Where("id = ?", schtwo.ID).Updates(map[string]interface{}{"partition_id": schtwo.PartitionId, "bed_id": schtwo.BedId, "schedule_type": schtwo.ScheduleType, "schedule_date": schtwo.ScheduleDate, "schedule_week": schtwo.ScheduleWeek, "is_export": 80000}).Error; err != nil {
  423. utx.Rollback()
  424. return err
  425. }
  426. utx.Commit()
  427. return err
  428. }
  429. func GetPatientScheduleFormToday(orgId, id, start int64) (schedules []*models.PatientSchedule, err error) {
  430. err = readDb.Table("xt_schedule as s").
  431. Preload("DeviceZone", "org_id=? and status=1", orgId).
  432. Preload("DeviceNumber", "org_id=? and status=1", orgId).
  433. Where("s.patient_id =? and s.user_org_id=? and s.schedule_date>=? and s.status=1", id, orgId, start).
  434. Find(&schedules).Error
  435. return
  436. }
  437. type WeekSchedulePrintVM struct {
  438. ID int64 `gorm:"column:id" json:"id"`
  439. ZoneID int64 `gorm:"column:partition_id" json:"zone_id"`
  440. DeviceNumberID int64 `gorm:"column:bed_id" json:"device_number_id"`
  441. PatientID int64 `gorm:"column:patient_id" json:"patient_id"`
  442. ScheduleDate int64 `gorm:"column:schedule_date" json:"schedule_date"`
  443. ScheduleType int64 `gorm:"column:schedule_type" json:"schedule_type"`
  444. ScheduleWeek int64 `gorm:"column:schedule_week" json:"schedule_week"`
  445. ModeID int64 `gorm:"column:mode_id" json:"mode_id"`
  446. Patient *models.Patients `gorm:"ForeignKey:PatientID" json:"patient"`
  447. DeviceNumber *models.DeviceNumber `gorm:"ForeignKey:DeviceNumberID" json:"device_number"`
  448. Zone *models.DeviceZone `gorm:"ForeignKey:ZoneID" json:"zone"`
  449. Mode *models.TreatmentMode `gorm:"ForeignKey:ModeID" json:"mode"`
  450. }
  451. type WeekSchedulePrintPatients struct {
  452. ID int64 `gorm:"column:id" json:"id" form:"id"`
  453. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id" form:"user_org_id"`
  454. Name string `gorm:"column:name" json:"name" form:"name"`
  455. Status int64 `gorm:"column:status" json:"status" form:"status"`
  456. }
  457. func (WeekSchedulePrintPatients) TableName() string {
  458. return "xt_patients"
  459. }
  460. type WeekSchedulePrintDeviceNumber struct {
  461. ID int64 `gorm:"column:id" json:"id"`
  462. OrgID int64 `gorm:"column:org_id" json:"-"`
  463. Number string `gorm:"column:number" json:"number"`
  464. GroupID int64 `gorm:"column:group_id" json:"group_id"`
  465. ZoneID int64 `gorm:"column:zone_id" json:"zone_id"`
  466. Status int64 `gorm:"column:status" json:"status" form:"status"`
  467. CreateTime int64 `gorm:"column:ctime" json:"-"`
  468. ModifyTime int64 `gorm:"column:mtime" json:"-"`
  469. Sort int64 `gorm:"column:sort" json:"sort" form:"sort"`
  470. }
  471. func (WeekSchedulePrintDeviceNumber) TableName() string {
  472. return "xt_device_number"
  473. }
  474. type WeekSchedulePrintVMTwo struct {
  475. ID int64 `gorm:"column:id" json:"id"`
  476. ZoneID int64 `gorm:"column:partition_id" json:"zone_id"`
  477. DeviceNumberID int64 `gorm:"column:bed_id" json:"device_number_id"`
  478. PatientID int64 `gorm:"column:patient_id" json:"patient_id"`
  479. ScheduleDate int64 `gorm:"column:schedule_date" json:"schedule_date"`
  480. ScheduleType int64 `gorm:"column:schedule_type" json:"schedule_type"`
  481. ScheduleWeek int64 `gorm:"column:schedule_week" json:"schedule_week"`
  482. ModeID int64 `gorm:"column:mode_id" json:"mode_id"`
  483. Patient *WeekSchedulePrintPatients `gorm:"ForeignKey:PatientID" json:"patient"`
  484. DeviceNumber *WeekSchedulePrintDeviceNumber `gorm:"ForeignKey:DeviceNumberID" json:"device_number"`
  485. Zone *models.DeviceZone `gorm:"ForeignKey:ZoneID" json:"zone"`
  486. //Mode *models.TreatmentMode `gorm:"ForeignKey:ModeID" json:"mode"`
  487. Mode *models.XtTreatmentMode `json:"mode" gorm:"foreignkey:ModeID,ModeID;AssociationForeignKey:ModeID,ModeID"`
  488. }
  489. type PrintCount struct {
  490. ScheduleType int64 `gorm:"column:schedule_type" json:"schedule_type"`
  491. ScheduleWeek int64 `gorm:"column:schedule_week" json:"schedule_week"`
  492. Count int64
  493. }
  494. func GetPrinitWeekSchedules(orgID int64, start, end int64) ([]*WeekSchedulePrintVMTwo, error) {
  495. var schedules []*WeekSchedulePrintVMTwo
  496. err := readDb.Table("xt_schedule as s").
  497. Preload("Patient", "status = 1 and user_org_id = ?", orgID).
  498. Preload("DeviceNumber", "status = 1 and org_id = ?", orgID).
  499. Preload("Zone", "status = 1 and org_id = ?", orgID).
  500. Preload("Mode", "status = 1").
  501. // Joins("JOIN xt_patients as p ON p.id = s.patient_id").
  502. Where("s.user_org_id = ? and s.schedule_date >= ? and s.schedule_date <= ? and s.status = 1", orgID, start, end).
  503. // Select("s.id, s.user_org_id, s.partition_id, s.bed_id, s.patient_id, s.schedule_date, s.schedule_type, s.schedule_week, s.mode_id").
  504. Find(&schedules).Error
  505. return schedules, err
  506. }
  507. func GetPrinitWeekTotal(orgid int64, startime int64, endtime int64) (print []*PrintCount, err error) {
  508. db := XTReadDB().Table("xt_schedule as x").Where("x.status = 1")
  509. if orgid > 0 {
  510. db = db.Where("x.user_org_id = ?", orgid)
  511. }
  512. if startime > 0 {
  513. db = db.Where("x.schedule_date >=?", startime)
  514. }
  515. if endtime > 0 {
  516. db = db.Where("x.schedule_date <=?", endtime)
  517. }
  518. err = db.Select("x.schedule_type,x.schedule_week,count(x.patient_id) as count").Group("x.schedule_date,x.schedule_type").Scan(&print).Error
  519. return print, err
  520. }
  521. func GetBloodPrintWeekTotal(orgid int64, startime int64, endtime int64) (print []*PrintCount, err error) {
  522. db := XTReadDB().Table("xt_schedule as x").Where("x.status = 1")
  523. if orgid > 0 {
  524. db = db.Where("x.user_org_id = ?", orgid)
  525. }
  526. if startime > 0 {
  527. db = db.Where("x.schedule_date >=?", startime)
  528. }
  529. if endtime > 0 {
  530. db = db.Where("x.schedule_date <=?", endtime)
  531. }
  532. db = db.Where("x.mode_id = 2")
  533. err = db.Select("x.schedule_type,x.schedule_week,count(x.patient_id) as count").Group("x.schedule_date,x.schedule_type").Scan(&print).Error
  534. return print, err
  535. }
  536. func GetPerfusionWeekTotal(orgid int64, startime int64, endtime int64) (print []*PrintCount, err error) {
  537. db := XTReadDB().Table("xt_schedule as x").Where("x.status = 1")
  538. if orgid > 0 {
  539. db = db.Where("x.user_org_id = ?", orgid)
  540. }
  541. if startime > 0 {
  542. db = db.Where("x.schedule_date >=?", startime)
  543. }
  544. if endtime > 0 {
  545. db = db.Where("x.schedule_date <=?", endtime)
  546. }
  547. db = db.Where("x.mode_id = 3")
  548. err = db.Select("x.schedule_type,x.schedule_week,count(x.patient_id) as count").Group("x.schedule_date,x.schedule_type").Scan(&print).Error
  549. return print, err
  550. }
  551. // func ExchangeScheduleTimeWithWeekSchedules(orgID int64, nextWeekSchs []*models.Schedule, nextTwoWeekSchs []*models.Schedule) error {
  552. // templateMode, getTemplateModeErr := GetOrgPatientScheduleTemplateMode(orgID)
  553. // if getTemplateModeErr != nil {
  554. // return getTemplateModeErr
  555. // }
  556. // if templateMode == nil {
  557. // return errors.New("模板模式不存在")
  558. // }
  559. // tx := writeDb.Begin()
  560. // updateTime := time.Now().Unix()
  561. // for _, sch := range nextWeekSchs {
  562. // sch.PatientInfectiousDiseases = nil
  563. // date := time.Unix(sch.ScheduleDate, 0)
  564. // sch.ScheduleDate = date.AddDate(0, 0, 7).Unix()
  565. // sch.UpdatedTime = updateTime
  566. // updateSchErr := tx.Save(sch).Error
  567. // if updateSchErr != nil {
  568. // tx.Rollback()
  569. // return updateSchErr
  570. // }
  571. // }
  572. // for _, sch := range nextTwoWeekSchs {
  573. // sch.PatientInfectiousDiseases = nil
  574. // date := time.Unix(sch.ScheduleDate, 0)
  575. // sch.ScheduleDate = date.AddDate(0, 0, -7).Unix()
  576. // sch.UpdatedTime = updateTime
  577. // updateSchErr := tx.Save(sch).Error
  578. // if updateSchErr != nil {
  579. // tx.Rollback()
  580. // return updateSchErr
  581. // }
  582. // }
  583. // templateMode.ModifyTime = updateTime
  584. // templateMode.ExecuteTimes += 1
  585. // updateModeErr := tx.Save(templateMode).Error
  586. // if updateModeErr != nil {
  587. // tx.Rollback()
  588. // return updateModeErr
  589. // }
  590. // tx.Commit()
  591. // return nil
  592. // }
  593. func GetSchedualPatientsByKeywordsAndWeek(keywords string, org_id int64, start_time int64, end_time int64) (schedule []*models.Schedule, err error) {
  594. likeKey := "%" + keywords + "%"
  595. err = readDb.
  596. Table("xt_schedule as s").
  597. Preload("PatientInfectiousDiseases", "status = 1 ").
  598. Preload("DeviceZone", "status = 1 ").
  599. Preload("DeviceNumber", "status = 1 ").
  600. Preload("TreatmentMode", "status = 1 ").
  601. Joins("JOIN xt_patients as p ON p.id = s.patient_id AND (p.name LIKE ? OR p.dialysis_no LIKE ? OR p.first_letter LIKE ?)", likeKey, likeKey, likeKey).
  602. Where("s.user_org_id=? and s.schedule_date >= ? and s.schedule_date <= ? and s.status=1", org_id, start_time, end_time).
  603. Select("s.id, s.user_org_id, s.partition_id, s.bed_id, s.patient_id, s.schedule_date, s.schedule_type, s.schedule_week, s.mode_id, s.status, s.created_time, s.updated_time, p.name as patient").
  604. Order("schedule_date asc").
  605. Find(&schedule).Error
  606. return
  607. }
  608. func GetSchedualPatientsByKeywords(keywords string, org_id int64, startime int64, endtime int64) (schedule []*models.Schedule, err error) {
  609. likeKey := "%" + keywords + "%"
  610. err = readDb.
  611. Table("xt_schedule as s").
  612. Preload("PatientInfectiousDiseases", "status = 1 ").
  613. Preload("DeviceZone", "status = 1 ").
  614. Preload("DeviceNumber", "status = 1 ").
  615. Preload("TreatmentMode", "status = 1 ").
  616. Joins("JOIN xt_patients as p ON p.id = s.patient_id AND (p.name LIKE ? OR p.dialysis_no LIKE ? OR p.first_letter LIKE ?)", likeKey, likeKey, likeKey).
  617. Where("s.user_org_id=? and s.schedule_date >= ? and s.schedule_date <= ? and s.status=1", org_id, startime, endtime).
  618. Select("s.id, s.user_org_id, s.partition_id, s.bed_id, s.patient_id, s.schedule_date, s.schedule_type, s.schedule_week, s.mode_id, s.status, s.created_time, s.updated_time, p.name as patient").
  619. Order("schedule_date asc").
  620. Find(&schedule).Error
  621. return
  622. }
  623. func GetWeekDayScheduleTwo(orgID int64, scheduleDate int64, scheduleDates *time.Time, weektime int64) (schedules []*models.WeekSchedule, err error) {
  624. db := readDb.Table("xt_schedule as s ").Where("s.status =1")
  625. if orgID > 0 {
  626. db = db.Where("s.user_org_id = ?", orgID)
  627. }
  628. if scheduleDate > 0 {
  629. db = db.Where("s.schedule_date = ?", scheduleDate)
  630. }
  631. if weektime > 0 {
  632. db = db.Where("s.schedule_type = ?", weektime)
  633. }
  634. err = db.
  635. Preload("DeviceZone", "status = 1 ").
  636. Preload("DeviceNumber", "status = 1 ").
  637. Preload("DialysisPrescription", func(db *gorm.DB) *gorm.DB {
  638. return db.Where("status = 1").Order("id asc")
  639. }).
  640. Joins("JOIN xt_patients as p ON p.id = s.patient_id").
  641. Select("s.id, s.user_org_id, s.partition_id, s.bed_id, s.patient_id, s.schedule_date, s.schedule_type, s.schedule_week, s.mode_id, s.status, s.created_time, s.updated_time, p.name as patient").
  642. Order("s.partition_id asc").
  643. Find(&schedules).Error
  644. for _, item := range schedules {
  645. _, config := FindDoctorAdviceRecordByOrgId(item.UserOrgId)
  646. if config.IsOpenRemind > 0 {
  647. //开启推送提醒逻辑 提交长期处方的时候,弹起长期医嘱推送
  648. var advice_three []*models.DoctorAdvice
  649. advices, _ := GetLastLongAdviceByGroupNoOther(item.UserOrgId, item.PatientId)
  650. advices_two, _ := GetLastLongAdviceByGroupNoThreeOther(item.UserOrgId, item.PatientId)
  651. for _, advice := range advices {
  652. advice.IsCheck = 1
  653. if advice.FrequencyType == 3 {
  654. week := int(scheduleDates.Weekday())
  655. switch week {
  656. case 1:
  657. if strings.Index(advice.WeekDay, "周一") > -1 {
  658. advice_three = append(advice_three, advice)
  659. }
  660. break
  661. case 2:
  662. if strings.Index(advice.WeekDay, "周二") > -1 {
  663. advice_three = append(advice_three, advice)
  664. }
  665. break
  666. case 3:
  667. if strings.Index(advice.WeekDay, "周三") > -1 {
  668. advice_three = append(advice_three, advice)
  669. }
  670. break
  671. case 4:
  672. if strings.Index(advice.WeekDay, "周四") > -1 {
  673. advice_three = append(advice_three, advice)
  674. }
  675. break
  676. case 5:
  677. if strings.Index(advice.WeekDay, "周五") > -1 {
  678. advice_three = append(advice_three, advice)
  679. }
  680. break
  681. case 6:
  682. if strings.Index(advice.WeekDay, "周六") > -1 {
  683. advice_three = append(advice_three, advice)
  684. }
  685. break
  686. case 0:
  687. if strings.Index(advice.WeekDay, "周日") > -1 {
  688. advice_three = append(advice_three, advice)
  689. }
  690. break
  691. }
  692. } else if advice.FrequencyType == 1 {
  693. advice_three = append(advice_three, advice)
  694. }
  695. }
  696. for _, advice := range advices_two {
  697. now := scheduleDates.Unix()
  698. dayStr := strconv.FormatInt(advice.DayCount, 10)
  699. dayStr2 := "-" + dayStr
  700. count, _ := strconv.ParseInt(dayStr2, 10, 64)
  701. oldTime := scheduleDates.AddDate(0, 0, int(count)).Unix()
  702. advices, _ := FindAllDoctorAdviceByTimeOther(now, oldTime, item.PatientId, item.UserOrgId, advice.TemplateId)
  703. if len(advices) == 0 {
  704. advice_three = append(advice_three, advice)
  705. }
  706. }
  707. item.DoctorAdvice = append(item.DoctorAdvice, advice_three...)
  708. }
  709. }
  710. return
  711. }
  712. func GetWeekDaySchedule(orgID int64, scheduleDate int64, scheduleDates *time.Time) (schedules []*models.WeekSchedule, err error) {
  713. err = readDb.
  714. Table("xt_schedule as s").
  715. Preload("DeviceZone", "status = 1 ").
  716. Preload("DeviceNumber", "status = 1 ").
  717. Preload("DialysisPrescription", func(db *gorm.DB) *gorm.DB {
  718. return db.Where("status = 1").Order("id asc")
  719. }).
  720. Joins("JOIN xt_patients as p ON p.id = s.patient_id").
  721. Where("s.user_org_id=? and s.schedule_date = ? and s.status=1", orgID, scheduleDate).
  722. Select("s.id, s.user_org_id, s.partition_id, s.bed_id, s.patient_id, s.schedule_date, s.schedule_type, s.schedule_week, s.mode_id, s.status, s.created_time, s.updated_time, p.name as patient").
  723. Order("schedule_type").
  724. Find(&schedules).Error
  725. for _, item := range schedules {
  726. _, config := FindDoctorAdviceRecordByOrgId(item.UserOrgId)
  727. if config.IsOpenRemind > 0 {
  728. //开启推送提醒逻辑 提交长期处方的时候,弹起长期医嘱推送
  729. var advice_three []*models.DoctorAdvice
  730. advices, _ := GetLastLongAdviceByGroupNoOther(item.UserOrgId, item.PatientId)
  731. advices_two, _ := GetLastLongAdviceByGroupNoThreeOther(item.UserOrgId, item.PatientId)
  732. for _, advice := range advices {
  733. advice.IsCheck = 1
  734. if advice.FrequencyType == 3 {
  735. week := int(scheduleDates.Weekday())
  736. fmt.Println(advice.WeekDay)
  737. switch week {
  738. case 1:
  739. if strings.Index(advice.WeekDay, "周一") > -1 {
  740. advice_three = append(advice_three, advice)
  741. }
  742. break
  743. case 2:
  744. if strings.Index(advice.WeekDay, "周二") > -1 {
  745. advice_three = append(advice_three, advice)
  746. }
  747. break
  748. case 3:
  749. if strings.Index(advice.WeekDay, "周三") > -1 {
  750. advice_three = append(advice_three, advice)
  751. }
  752. break
  753. case 4:
  754. if strings.Index(advice.WeekDay, "周四") > -1 {
  755. advice_three = append(advice_three, advice)
  756. }
  757. break
  758. case 5:
  759. if strings.Index(advice.WeekDay, "周五") > -1 {
  760. advice_three = append(advice_three, advice)
  761. }
  762. break
  763. case 6:
  764. if strings.Index(advice.WeekDay, "周六") > -1 {
  765. advice_three = append(advice_three, advice)
  766. }
  767. break
  768. case 0:
  769. if strings.Index(advice.WeekDay, "周日") > -1 {
  770. advice_three = append(advice_three, advice)
  771. }
  772. break
  773. }
  774. } else if advice.FrequencyType == 1 {
  775. advice_three = append(advice_three, advice)
  776. }
  777. }
  778. for _, advice := range advices_two {
  779. now := scheduleDates.Unix()
  780. dayStr := strconv.FormatInt(advice.DayCount, 10)
  781. dayStr2 := "-" + dayStr
  782. count, _ := strconv.ParseInt(dayStr2, 10, 64)
  783. oldTime := scheduleDates.AddDate(0, 0, int(count)).Unix()
  784. advices, _ := FindAllDoctorAdviceByTimeOther(now, oldTime, item.PatientId, item.UserOrgId, advice.TemplateId)
  785. if len(advices) == 0 {
  786. advice_three = append(advice_three, advice)
  787. }
  788. }
  789. item.DoctorAdvice = append(item.DoctorAdvice, advice_three...)
  790. }
  791. }
  792. return
  793. }
  794. func GetWeekDayScheduleById(orgid int64, scheduleDate int64, scheduleType int64, zone int64) (list []*models.VmSchedulesRemind, err error) {
  795. db := XTReadDB().Table("xt_schedule as x").Where("x.status = 1")
  796. if orgid > 0 {
  797. db = db.Where("x.user_org_id = ?", orgid)
  798. }
  799. if scheduleDate > 0 {
  800. db = db.Where("x.schedule_date = ?", scheduleDate)
  801. }
  802. if scheduleType > 0 {
  803. db = db.Where("x.schedule_type = ?", scheduleType)
  804. }
  805. if zone > 0 {
  806. db = db.Where("x.partition_id = ?", zone)
  807. }
  808. err = db.Select("x.id,x.user_org_id,x.partition_id,x.bed_id,x.patient_id,x.schedule_date,x.schedule_type,x.schedule_week,x.mode_id,x.is_export").Preload("XtPatients", "status = 1").Preload("DeviceZone", "status = 1").Preload("DeviceNumber", "status = 1").Preload("DialysisOrder", "status = 1").Preload("DoctorAdvice", func(db *gorm.DB) *gorm.DB {
  809. return db.Where("status =1 and parent_id = 0").Preload("ChildDoctorAdvice", "status = 1")
  810. }).Preload("HisDoctorAdviceInfo", "status = 1").Preload("DialysisPrescription", "status = 1").Preload("DialysisSolution", func(db *gorm.DB) *gorm.DB {
  811. return db.Where("status =1 and solution_status =1").Order("id asc")
  812. }).Find(&list).Error
  813. return list, err
  814. }
  815. func GetWeekDayScheduleByIdThee(orgid int64, scheduleDate int64, scheduleType int64, zone []string, limit int64, page int64, ids []string) (list []*models.VmSchedulesRemind, total int64, err error) {
  816. db := XTReadDB().Table("xt_schedule as x").Where("x.status = 1")
  817. offset := (page - 1) * limit
  818. if orgid > 0 {
  819. db = db.Where("x.user_org_id = ?", orgid)
  820. }
  821. if scheduleDate > 0 {
  822. db = db.Where("x.schedule_date = ?", scheduleDate)
  823. }
  824. if scheduleType > 0 {
  825. db = db.Where("x.schedule_type = ?", scheduleType)
  826. }
  827. if len(zone) > 0 {
  828. db = db.Where("x.partition_id in (?)", zone)
  829. }
  830. if len(ids) > 0 {
  831. db = db.Where("x.patient_id in(?)", ids)
  832. }
  833. err = db.Offset(offset).Limit(limit).Select("x.id,x.user_org_id,x.partition_id,x.bed_id,x.patient_id,x.schedule_date,x.schedule_type,x.schedule_week,x.mode_id,x.is_export").Preload("XtPatients", "status = 1").Preload("DeviceZone", "status = 1").Preload("DeviceNumber", "status = 1").Count(&total).Find(&list).Error
  834. return list, total, err
  835. }
  836. func GetWeekDayScheduleByIdTwo(orgid int64, scheduleDate int64, scheduleType int64, startDate int64, zone []string, limit int64, page int64, ids []string) (list []*models.VmSchedulesRemind, total int64, err error) {
  837. db := XTReadDB().Table("xt_schedule as x").Where("x.status = 1")
  838. offset := (page - 1) * limit
  839. if orgid > 0 {
  840. db = db.Where("x.user_org_id = ?", orgid)
  841. }
  842. if scheduleDate > 0 {
  843. db = db.Where("x.schedule_date >=? and x.schedule_date<=?", startDate, scheduleDate)
  844. }
  845. if scheduleType > 0 {
  846. db = db.Where("x.schedule_type = ?", scheduleType)
  847. }
  848. if len(zone) > 0 {
  849. db = db.Where("x.partition_id in (?)", zone)
  850. }
  851. if len(ids) > 0 {
  852. db = db.Where("x.patient_id in(?)", ids)
  853. }
  854. err = db.Offset(offset).Limit(limit).Select("x.id,x.user_org_id,x.partition_id,x.bed_id,x.patient_id,x.schedule_date,x.schedule_type,x.schedule_week,x.mode_id,x.is_export,x.mode_id").Preload("XtPatients", "status = 1").Preload("DeviceZone", "status = 1").Preload("DeviceNumber", "status = 1").Count(&total).Find(&list).Error
  855. return list, total, err
  856. }
  857. func RemoveRepeatedElement(arr []*models.DoctorAdvice) (newArr []*models.DoctorAdvice) {
  858. newArr = make([]*models.DoctorAdvice, 0)
  859. for i := 0; i < len(arr); i++ {
  860. repeat := false
  861. for j := i + 1; j < len(arr); j++ {
  862. if arr[i].ID == arr[j].ID {
  863. repeat = true
  864. break
  865. }
  866. }
  867. if !repeat {
  868. newArr = append(newArr, arr[i])
  869. }
  870. }
  871. return
  872. }
  873. func GetLastLongAdviceByGroupNoOther(orgId int64, patient_id int64) (advice []*models.DoctorAdvice, err error) {
  874. err = readDb.Model(&models.DoctorAdvice{}).Where("status = 1 AND user_org_id = ? AND patient_id = ? AND advice_type = 1 AND stop_state = 2 AND parent_id = 0", orgId, patient_id).Preload("ChildDoctorAdvice", "status = 1").Find(&advice).Error
  875. return
  876. }
  877. func GetLastLongAdviceByGroupNoThreeOther(orgId int64, patient_id int64) (advice []*models.DoctorAdvice, err error) {
  878. err = readDb.Model(&models.DoctorAdvice{}).Where("status = 1 AND user_org_id = ? AND patient_id = ? AND advice_type = 1 AND frequency_type = 2 AND stop_state = 2 AND parent_id = 0 ", orgId, patient_id).Preload("ChildDoctorAdvice", "status = 1").Find(&advice).Error
  879. return
  880. }
  881. func FindAllDoctorAdviceByTimeOther(now int64, end int64, patient_id int64, orgId int64, temp_id string) (advice []*models.DoctorAdvice, err error) {
  882. err = readDb.Model(&models.DoctorAdvice{}).Where("status = 1 AND user_org_id = ? AND patient_id = ? AND advice_type = 2 AND frequency_type = 2 AND advice_date > ? AND advice_date < ? AND template_id = ? ", orgId, patient_id, end, now, temp_id).Preload("ChildDoctorAdvice", "status = 1").Find(&advice).Error
  883. return
  884. }
  885. func FindSchPatientNameByName(name string, user_org_id int64) (patient []*models.Patients, err error) {
  886. err = readDb.Model(&models.Patients{}).Where("user_org_id = ? AND name = ? AND status = 1", user_org_id, name).Find(&patient).Error
  887. return
  888. }
  889. func UpdateScheduleStatus(date int64, org_id int64) (err error) {
  890. err = readDb.Model(&models.Schedule{}).Where("user_org_id = ? AND schedule_date > ? AND status = 1", org_id, date).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  891. return
  892. }
  893. func FindDeviceZoneByName(name string, org_id int64) (zone models.DeviceZone, err error) {
  894. err = readDb.Model(&models.DeviceZone{}).Where("org_id = ? AND name = ? AND status = 1", org_id, name).First(&zone).Error
  895. return
  896. }
  897. func FindDeviceByName(device_name string, org_id int64, zone_id int64) (devices []*models.DeviceNumber, err error) {
  898. err = readDb.Model(&models.DeviceNumber{}).Where("org_id = ? AND number = ? AND zone_id = ? AND status = 1", org_id, device_name, zone_id).Find(&devices).Error
  899. return
  900. }
  901. func FindSchedualExportLog(org_id int64, export_time int64) (errLogs []*models.ExportErrLog, err error) {
  902. err = readDb.Model(&models.ExportErrLog{}).Where("user_org_id = ? AND export_time = ? AND log_type = 2", org_id, export_time).Find(&errLogs).Error
  903. return
  904. }
  905. func FindDeviceByNameTwo(device_name string, org_id int64, zone_id int64) (devices []*models.DeviceNumber, err error) {
  906. err = readDb.Model(&models.DeviceNumber{}).Where("org_id = ? AND number = ? AND status = 1 AND zone_id = ?", org_id, device_name, zone_id).Find(&devices).Error
  907. return
  908. }
  909. func UpdateScheduleTemplateStatus(org_id int64) (err error) {
  910. err = readDb.Model(&models.PatientScheduleTemplateItem{}).Where("org_id = ? AND status = 1", org_id).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  911. return
  912. }
  913. func CreateScheduleTemplate(m *models.PatientScheduleTemplateItem) error {
  914. return writeDb.Model(&models.PatientScheduleTemplateItem{}).Create(&m).Error
  915. }
  916. func ExportScheduleTemplate(m *models.PatientScheduleTemplateItem) (total int64) {
  917. readDb.Model(&models.PatientScheduleTemplateItem{}).Where("template_id = ? AND patient_id = ? AND weekday = ? AND org_id = ? and status =1", m.TemplateID, m.PatientID, m.Weekday, m.OrgID).Count(&total)
  918. return
  919. }
  920. func FindSchedualTemplateExportLog(org_id int64, export_time int64) (errLogs []*models.ExportErrLog, err error) {
  921. err = readDb.Model(&models.ExportErrLog{}).Where("user_org_id = ? AND export_time = ? AND log_type = 3", org_id, export_time).Find(&errLogs).Error
  922. return
  923. }
  924. func GetDialysisOrderByBedId(orgID, start, bed_id int64, schedule_type int64) (*models.DialysisOrder, error) {
  925. order := models.DialysisOrder{}
  926. err = readDb.Model(&order).Where("user_org_id=? and dialysis_date=? and bed_id=? and status=1 AND schedual_type = ?", orgID, start, bed_id, schedule_type).Find(&order).Error
  927. if err == gorm.ErrRecordNotFound {
  928. return nil, err
  929. }
  930. if err != nil {
  931. return nil, err
  932. }
  933. return &order, nil
  934. }
  935. type VMDeviceNumber struct {
  936. ID int64 `gorm:"column:id" json:"id"`
  937. OrgID int64 `gorm:"column:org_id" json:"-"`
  938. Number string `gorm:"column:number" json:"number"`
  939. GroupID int64 `gorm:"column:group_id" json:"group_id"`
  940. ZoneID int64 `gorm:"column:zone_id" json:"zone_id"`
  941. Status int8 `json:"-"`
  942. CreateTime int64 `gorm:"column:ctime" json:"-"`
  943. ModifyTime int64 `gorm:"column:mtime" json:"-"`
  944. DeviceZone models.DeviceZone `json:"zone" gorm:"foreignkey:ID;AssociationForeignKey:ZoneID;"`
  945. }
  946. func (VMDeviceNumber) TableName() string {
  947. return "xt_device_number"
  948. }
  949. type VMPatientScheduleTemplateItem struct {
  950. ID int64 `gorm:"column:id" json:"id" form:"id"`
  951. OrgID int64 `gorm:"column:org_id" json:"-" form:"org_id"`
  952. TemplateID int64 `gorm:"column:template_id" json:"template_id" form:"template_id"`
  953. DeviceNumberID int64 `gorm:"column:device_number_id" json:"device_number_id" form:"device_number_id"`
  954. PatientID int64 `gorm:"column:patient_id" json:"patient_id" form:"patient_id"`
  955. TreatMode int64 `gorm:"column:treat_mode" json:"treat_mode" form:"treat_mode"` // 治疗模式
  956. Weekday int8 `gorm:"column :weekday" json:"weekday" form:"weekday"` // 周一-周日 1-7
  957. TimeType int8 `gorm:"column:time_type" json:"time_type" form:"time_type"` // 上下晚 123
  958. Status int8 `gorm:"column:status" json:"status" form:"status"`
  959. CreateTime int64 `gorm:"column:ctime" json:"-" form:"ctime"`
  960. ModifyTime int64 `gorm:"column:mtime" json:"-" form:"mtime"`
  961. IsExport int64 `gorm:"column:is_export" json:"is_export" form:"is_export"`
  962. Patient string `gorm:"-" json:"patient" form:"patient"`
  963. DeviceNumber VMDeviceNumber `json:"number" gorm:"foreignkey:ID;AssociationForeignKey:DeviceNumberID;"`
  964. PatientInfectiousDiseases []*models.InfectiousDiseases `json:"patient_contagions" gorm:"foreignkey:PatientId;AssociationForeignKey:PatientId;"`
  965. }
  966. func (VMPatientScheduleTemplateItem) TableName() string {
  967. return "xt_patient_schedule_template_item"
  968. }
  969. func GetTemplateSchedualPatientsByKeywords(keywords string, org_id int64, template_id int64) (schedule []*VMPatientScheduleTemplateItem, err error) {
  970. likeKey := "%" + keywords + "%"
  971. err = readDb.
  972. Table("xt_patient_schedule_template_item as s").
  973. Preload("PatientInfectiousDiseases", "status = 1 ").
  974. Preload("DeviceNumber", func(db *gorm.DB) *gorm.DB {
  975. return db.Where("org_id = ? AND status = 1", org_id).Preload("DeviceZone", "org_id = ? AND status = 1", org_id)
  976. }).
  977. Joins("JOIN xt_patients as p ON p.id = s.patient_id AND (p.name LIKE ? OR p.dialysis_no LIKE ?)", likeKey, likeKey).
  978. Where("s.template_id = ? AND s.org_id=? and s.status=1", template_id, org_id).
  979. Select("s.id, s.org_id, s.template_id, s.device_number_id, s.patient_id, s.treat_mode, s.weekday, s.time_type, s.status, s.ctime, s.mtime, p.name as patient").
  980. Order("weekday asc").
  981. Find(&schedule).Error
  982. return
  983. }
  984. func UpdateNewScheduleTemplateStatus(id int64, org_id int64) (err error) {
  985. err = readDb.Model(&models.PatientScheduleTemplateItem{}).Where("org_id = ? AND id = ?", org_id, id).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  986. return
  987. }
  988. func GetNextWeekDaySchedule(weektype int64, weektime int64, startime int64, endtime int64, orgID int64, zone []string, record_date int64, ids []string) (schedules []*models.WeekScheduleSix, err error) {
  989. db := XTReadDB().Table("xt_schedule as s ").Where("s.status =1")
  990. if orgID > 0 {
  991. db = db.Where("s.user_org_id = ?", orgID)
  992. }
  993. if weektype == 0 {
  994. if startime > 0 {
  995. db = db.Where("s.schedule_date >= ?", startime)
  996. }
  997. if endtime > 0 {
  998. db = db.Where("s.schedule_date <= ?", endtime)
  999. }
  1000. }
  1001. if weektype > 0 {
  1002. if record_date > 0 {
  1003. db = db.Where("s.schedule_date = ?", record_date)
  1004. }
  1005. }
  1006. if weektime > 0 {
  1007. db = db.Where("s.schedule_type = ?", weektime)
  1008. }
  1009. if weektype > 0 {
  1010. db = db.Where("s.schedule_week = ?", weektype)
  1011. }
  1012. if len(zone) > 0 {
  1013. db = db.Where("s.partition_id in (?)", zone)
  1014. }
  1015. if len(ids) > 0 {
  1016. db = db.Where("s.patient_id in (?)", ids)
  1017. }
  1018. err = db.
  1019. Preload("DeviceZone", "status = 1 ").
  1020. Preload("DeviceNumber", "status = 1 ").Joins("JOIN xt_patients as p ON p.id = s.patient_id").
  1021. Select("s.id,s.user_org_id, s.partition_id, s.bed_id, s.patient_id, s.schedule_date, s.schedule_type, s.schedule_week, s.mode_id, s.status, s.created_time, s.updated_time, p.name as patient,p.id_card_no as id_card_no,p.dialysis_no as dialysis_no,p.gender as gender,p.phone as phone").
  1022. Order("s.partition_id desc").
  1023. Find(&schedules).Error
  1024. return schedules, err
  1025. }
  1026. func GetThreeWeekList(startime int64, endtime int64, orgid int64, scheduletype int64, partionid int64) (schedule []*models.VmSchedule, err error) {
  1027. db := XTReadDB().Table("xt_schedule as x").Where("x.status =1")
  1028. if startime > 0 {
  1029. db = db.Where("x.schedule_date >=?", startime)
  1030. }
  1031. if endtime > 0 {
  1032. db = db.Where("x.schedule_date <=?", endtime)
  1033. }
  1034. if orgid > 0 {
  1035. db = db.Where("x.user_org_id = ?", orgid)
  1036. }
  1037. if scheduletype > 0 {
  1038. db = db.Where("x.schedule_type = ?", scheduletype)
  1039. }
  1040. if partionid > 0 {
  1041. db = db.Where("x.partition_id = ?", partionid)
  1042. }
  1043. err = db.Select("x.schedule_date,x.schedule_type,x.schedule_week,x.bed_id,x.partition_id,t.name,n.number,z.name,x.dialysis_machine_name,x.mode_id").Joins("left join xt_patients as t on t.id = x.patient_id and t.user_org_id = ? and t.status = 1", orgid).Joins("left join xt_device_zone as z on z.id = x.partition_id and z.org_id = ? and z.status =1", orgid).Joins("left join xt_device_number as n on n.id = x.bed_id and n.org_id = ? and n.status =1", orgid).Scan(&schedule).Error
  1044. return schedule, err
  1045. }
  1046. func UpdatedDialysisPrescription(prescription *models.DialysisPrescription, recordDate int64, patientid int64, orgid int64) error {
  1047. err := XTWriteDB().Model(&prescription).Where("record_date = ? and patient_id = ? and user_org_id = ?", recordDate, patientid, orgid).Update(map[string]interface{}{"mode_id": prescription.ModeId, "updated_time": time.Now().Unix()}).Error
  1048. return err
  1049. }
  1050. func GetWeekSchedules(orgid int64, startime int64, endtime int64) (scheudle []*models.XtSchedule, err error) {
  1051. err = XTReadDB().Model(&scheudle).Where("user_org_id = ? and status = 1 and schedule_date>=? and schedule_date<=?", orgid, startime, endtime).Find(&scheudle).Error
  1052. return scheudle, err
  1053. }
  1054. func GetLastWeekSchedule(orgid int64, startime int64, endtime int64) (*models.XtSchedule, error) {
  1055. schedule := models.XtSchedule{}
  1056. err := XTReadDB().Where("user_org_id = ? and status =1 and schedule_date>=? and schedule_date<=?", orgid, startime, endtime).Find(&schedule).Error
  1057. if err == gorm.ErrRecordNotFound {
  1058. return nil, err
  1059. }
  1060. if err != nil {
  1061. return nil, err
  1062. }
  1063. return &schedule, nil
  1064. }
  1065. func AddPatientSchedule(schedule *models.XtSchedule) error {
  1066. utx := XTWriteDB().Begin()
  1067. err := utx.Create(&schedule).Error
  1068. utx.Commit()
  1069. return err
  1070. }
  1071. func UpdatePatientSchedule(orgid int64, startime int64, endtime int64) error {
  1072. schedule := models.XtSchedule{}
  1073. err := XTWriteDB().Model(&schedule).Where("user_org_id = ? and schedule_date>=? and schedule_date<=? and status =1", orgid, startime, endtime).Updates(map[string]interface{}{"status": 0}).Error
  1074. return err
  1075. }
  1076. func GetSettingById(id int64) (*models.XtRemindPrintSetting, error) {
  1077. setting := models.XtRemindPrintSetting{}
  1078. err := XTReadDB().Model(&setting).Where("id = ? and status = 1", id).Find(&setting).Error
  1079. if err == gorm.ErrRecordNotFound {
  1080. return nil, err
  1081. }
  1082. if err != nil {
  1083. return nil, err
  1084. }
  1085. return &setting, nil
  1086. }
  1087. func CreateSetting(setting *models.XtRemindPrintSetting) error {
  1088. err := XTWriteDB().Create(&setting).Error
  1089. return err
  1090. }
  1091. func UpdatedRemindPrint(set *models.XtRemindPrintSetting, id int64) error {
  1092. err := XTWriteDB().Model(&set).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"prescription_status": set.PrescriptionStatus, "week": set.Week, "name": set.Name, "zone": set.Zone, "classes": set.Classes, "number": set.Number, "mode": set.Mode, "dialyzers": set.Dialyzers, "perfusion_apparatus": set.PerfusionApparatus, "anticoagulant": set.Anticoagulant, "doctor_advice": set.DoctorAdvice, "anticoagulant_zongliang": set.AnticoagulantZongliang}).Error
  1093. return err
  1094. }
  1095. func GetRemindPrintList(id int64) (models.XtRemindPrintSetting, error) {
  1096. setting := models.XtRemindPrintSetting{}
  1097. err := XTReadDB().Model(&setting).Where("user_org_id = ? and status =1", id).Find(&setting).Error
  1098. return setting, err
  1099. }
  1100. func GetScheudleTemplateById(orgid int64) (*models.ScheduleTemplate, error) {
  1101. schedule := models.ScheduleTemplate{}
  1102. err := XTReadDB().Model(&schedule).Where("user_org_id = ? and status = 1", orgid).Find(&schedule).Error
  1103. if err == gorm.ErrRecordNotFound {
  1104. return nil, err
  1105. }
  1106. if err != nil {
  1107. return nil, err
  1108. }
  1109. return &schedule, nil
  1110. }
  1111. func CreateXtscheduleTemplate(template *models.ScheduleTemplate) error {
  1112. err := XTWriteDB().Create(&template).Error
  1113. return err
  1114. }
  1115. func UpdateXtScheduleTemplate(template *models.ScheduleTemplate, orgid int64) error {
  1116. err := XTWriteDB().Model(&template).Where("user_org_id = ? and status = 1", orgid).Updates(map[string]interface{}{"template_id": template.TemplateId}).Error
  1117. return err
  1118. }
  1119. func GetScheduleTemplate(orgid int64) (models.ScheduleTemplate, error) {
  1120. template := models.ScheduleTemplate{}
  1121. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&template).Error
  1122. return template, err
  1123. }
  1124. func GetDataPrintByOrgId(orgid int64) (*models.XtDataPrint, error) {
  1125. dataPrint := models.XtDataPrint{}
  1126. err := XTReadDB().Model(&dataPrint).Where("user_org_id = ? and status = 1", orgid).Find(&dataPrint).Error
  1127. if err == gorm.ErrRecordNotFound {
  1128. return nil, err
  1129. }
  1130. if err != nil {
  1131. return nil, err
  1132. }
  1133. return &dataPrint, nil
  1134. }
  1135. func CreateDataPrint(dataPrint *models.XtDataPrint) error {
  1136. err := XTWriteDB().Create(&dataPrint).Error
  1137. return err
  1138. }
  1139. func UpdteDataPrint(orgid int64, dataPrint *models.XtDataPrint) error {
  1140. err := XTWriteDB().Model(&dataPrint).Where("user_org_id = ? and status = 1", orgid).Updates(map[string]interface{}{"is_open": dataPrint.IsOpen}).Error
  1141. return err
  1142. }
  1143. func GetDataPrint(orgid int64) (models.XtDataPrint, error) {
  1144. dataPrint := models.XtDataPrint{}
  1145. err := XTReadDB().Model(&dataPrint).Where("user_org_id = ? and status = 1", orgid).Find(&dataPrint).Error
  1146. return dataPrint, err
  1147. }
  1148. func GetNextWeekPanels(startime int64, endtime int64, orgid int64) (schedule []*models.XtSchedule, err error) {
  1149. err = XTReadDB().Model(&schedule).Where("schedule_date >=? and schedule_date<=? and status = 1 and user_org_id = ?", startime, endtime, orgid).Preload("LastAfterWeight", func(db *gorm.DB) *gorm.DB {
  1150. return db.Where("user_org_id = ? and status = 1 and assessment_date>=1704038400 and assessment_date < ?", orgid, endtime)
  1151. }).Find(&schedule).Error
  1152. return schedule, err
  1153. }
  1154. func GetTemplateListByTemplateId(id int64, orgid int64) (list []*models.VmPatientScheduleTemplateItem, err error) {
  1155. err = XTReadDB().Where("template_id =? and org_id = ? and status = 1", id, orgid).Find(&list).Error
  1156. return list, err
  1157. }
  1158. func DeletePatientSchedule(startime int64, endtime int64, org_id int64) (models.XtSchedule, error) {
  1159. schedule := models.XtSchedule{}
  1160. err := XTWriteDB().Model(&schedule).Where("schedule_date>=? and schedule_date<=? and user_org_id = ? and status = 1", startime, endtime, org_id).Updates(map[string]interface{}{"status": 0}).Error
  1161. return schedule, err
  1162. }
  1163. func DeletePatientScheduleToday(startime int64, endtime int64, org_id int64) (models.XtSchedule, error) {
  1164. schedule := models.XtSchedule{}
  1165. err := XTWriteDB().Model(&schedule).Where("schedule_date>? and schedule_date<=? and user_org_id = ? and status = 1", startime, endtime, org_id).Updates(map[string]interface{}{"status": 0}).Error
  1166. return schedule, err
  1167. }
  1168. func GetBedNumberById(id int64, org_id int64) (models.DeviceNumber, error) {
  1169. number := models.DeviceNumber{}
  1170. err := XTReadDB().Where("id = ? and status = 1 and org_id =?", id, org_id).Find(&number).Error
  1171. return number, err
  1172. }
  1173. func CreatePatientSchedule(schedule *models.XtSchedule) error {
  1174. utx := XTWriteDB().Begin()
  1175. err := utx.Create(&schedule).Error
  1176. utx.Commit()
  1177. return err
  1178. }
  1179. func GetPatientScheduleTempalate(orgid int64) (models.PatientScheduleTemplateMode, error) {
  1180. mode := models.PatientScheduleTemplateMode{}
  1181. err := XTReadDB().Where("org_id = ? and status =1", orgid).Find(&mode).Error
  1182. return mode, err
  1183. }
  1184. func GetSolutionSchedule(orgid int64) (solution []*models.VmDialysisSolution, err error) {
  1185. err = XTReadDB().Where("user_org_id = ? and status = 1", orgid).Group("patient_id,mode_id").Order("created_time desc").Preload("DialysisSolution", func(db *gorm.DB) *gorm.DB {
  1186. return db.Where("user_org_id = ? and status = 1", orgid).Order("id asc")
  1187. }).Find(&solution).Error
  1188. return solution, err
  1189. }
  1190. func GetSolutionScheduleBySchedule(orgid int64, zonid int64, classtype int64, schedule_date int64) (schedule []*models.XtScheduleList, err error) {
  1191. db := XTReadDB().Model(&schedule).Where("status = 1")
  1192. if orgid > 0 {
  1193. db = db.Where("user_org_id =?", orgid)
  1194. }
  1195. if zonid > 0 {
  1196. db = db.Where("partition_id = ?", zonid)
  1197. }
  1198. if classtype > 0 {
  1199. db = db.Where("schedule_type = ?", classtype)
  1200. }
  1201. if schedule_date > 0 {
  1202. db = db.Where("schedule_date = ?", schedule_date)
  1203. }
  1204. err = db.Preload("DialysisSolution", func(db *gorm.DB) *gorm.DB {
  1205. return db.Where("user_org_id = ? and status = 1", orgid).Group("patient_id,mode_id").Order("created_time asc").Preload("DialysisSolution", func(db *gorm.DB) *gorm.DB {
  1206. return db.Where("user_org_id = ? and status = 1", orgid).Order("id asc")
  1207. })
  1208. }).Find(&schedule).Error
  1209. return schedule, err
  1210. }
  1211. func GetDeathPatient(orgid int64) (patient []*models.XtPatients, err error) {
  1212. err = XTReadDB().Where("user_org_id = ? and (lapseto = 2 || lapseto = 3) and status = 1", orgid).Find(&patient).Error
  1213. return patient, err
  1214. }
  1215. func GetInitPrintData(patient_id int64, scheduleDate int64, orgid int64) (list []*models.VmSchedulesRemind, err error) {
  1216. db := XTReadDB().Table("xt_schedule as x").Where("x.status = 1")
  1217. if orgid > 0 {
  1218. db = db.Where("x.user_org_id = ?", orgid)
  1219. }
  1220. if patient_id > 0 {
  1221. db = db.Where("x.patient_id = ?", patient_id)
  1222. }
  1223. if scheduleDate > 0 {
  1224. db = db.Where("x.schedule_date >=? and x.schedule_date<=?", scheduleDate, scheduleDate)
  1225. }
  1226. err = db.Select("x.id,x.user_org_id,x.partition_id,x.bed_id,x.patient_id,x.schedule_date,x.schedule_type,x.schedule_week,x.mode_id,x.is_export").Preload("XtPatients", "status = 1").Preload("DeviceZone", "status = 1").Preload("DeviceNumber", "status = 1").Preload("XtAssessmentBeforeDislysis", "status = 1 and assessment_date =? and patient_id = ?", scheduleDate, patient_id).Preload("DialysisPrescription", "status = 1 and record_date = ? and patient_id = ?", scheduleDate, patient_id).Preload("DialysisSolution", func(db *gorm.DB) *gorm.DB {
  1227. return db.Where("status =1 and patient_id = ?", patient_id).Order("id asc")
  1228. }).Find(&list).Error
  1229. return list, err
  1230. }
  1231. func GetLastWeightAfter(patient_id int64, scheduleDate int64, orgid int64) (models.AssessmentAfterDislysis, error) {
  1232. dislysis := models.AssessmentAfterDislysis{}
  1233. err := XTReadDB().Where("user_org_id = ? and patient_id = ? and assessment_date < ? and status = 1", orgid, patient_id, scheduleDate).Order("id desc").First(&dislysis).Error
  1234. return dislysis, err
  1235. }
  1236. func GetScheduleTwo(orgID, id int64) (models.Schedule, error) {
  1237. var schedule models.Schedule
  1238. var err error
  1239. err = readDb.Model(&models.Schedule{}).Where("id = ? and user_org_id=? and status=1", id, orgID).First(&schedule).Error
  1240. return schedule, err
  1241. }
  1242. func UpdateSchDName(sch models.Schedule) (err error) {
  1243. err = writeDb.Model(&models.Schedule{}).Where("status=1 AND id = ?", sch.ID).Updates(map[string]interface{}{"dialysis_machine_name": sch.DialysisMachineName}).Error
  1244. return
  1245. }
  1246. func GetDialysisOrderByBedIdTwo(orgID, start, bed_id int64, schedule_type int64) (models.DialysisOrder, error) {
  1247. order := models.DialysisOrder{}
  1248. err = readDb.Model(&order).Where("user_org_id=? and dialysis_date=? and bed_id=? and status=1 AND schedual_type = ?", orgID, start, bed_id, schedule_type).Find(&order).Error
  1249. return order, nil
  1250. }
  1251. func ChangeScheduleClass(schedule models.Schedule) error {
  1252. err := XTWriteDB().Model(&schedule).Where("patient_id = ? and schedule_date = ? and user_org_id = ? and status = 1", schedule.PatientId, schedule.ScheduleDate, schedule.UserOrgId).Update(map[string]interface{}{"schedule_type": schedule.ScheduleType}).Error
  1253. return err
  1254. }
  1255. func GetLastDialysisOrder(org_id int64, patient_id int64, dialysis_date int64) (models.XtDialysisOrderSix, error) {
  1256. order := models.XtDialysisOrderSix{}
  1257. err := XTReadDB().Where("user_org_id = ? and patient_id = ? and dialysis_date < ? and status = 1", org_id, patient_id, dialysis_date).Order("id desc").First(&order).Error
  1258. return order, err
  1259. }
  1260. func GetHisDoctorAdviceList(org_id int64, patient_id int64, dialysis_date int64) (adviceInfo []*models.HisDoctorAdviceInfo, err error) {
  1261. err = XTReadDB().Where("patient_id=? and user_org_id = ? and advice_date= ? and status=1", patient_id, org_id, dialysis_date).Find(&adviceInfo).Error
  1262. return adviceInfo, err
  1263. }
  1264. func GetMobileDoctorAdviceList(org_id int64, patient_id int64, dialysis_date int64) (advice []*models.DoctorAdvice, err error) {
  1265. err = XTReadDB().Where("patient_id=? and user_org_id = ? and advice_date= ? and status=1 and advice_type =2", patient_id, org_id, dialysis_date).Find(&advice).Error
  1266. return advice, err
  1267. }
  1268. func UpdateHisAdviceList(id int64, creater int64) (models.HisDoctorAdvice, error) {
  1269. doctorAdvice := models.HisDoctorAdvice{}
  1270. err := XTWriteDB().Model(&models.HisDoctorAdvice{}).Where("id = ? and status=1", id).Updates(map[string]interface{}{"checker": creater, "check_state": 1, "check_time": time.Now().Unix()}).Error
  1271. return doctorAdvice, err
  1272. }
  1273. func UpdateAdviceList(id int64, creater int64) (models.DoctorAdvice, error) {
  1274. advice := models.DoctorAdvice{}
  1275. err := XTWriteDB().Model(&models.DoctorAdvice{}).Where("id = ? and status=1", id).Updates(map[string]interface{}{"checker": creater, "check_state": 1, "check_time": time.Now().Unix()}).Error
  1276. return advice, err
  1277. }
  1278. func GetDoctorAdviceListSchedule(org_id int64, patient_id int64) (advices []*models.DoctorAdvice, err error) {
  1279. err = XTReadDB().Where("patient_id=? and user_org_id = ? and status=1 and parent_id=0 and advice_type =1", patient_id, org_id).Preload("ChildDoctorAdvice", "status = 1").Find(&advices).Error
  1280. return advices, err
  1281. }
  1282. func GetLastDialysisPrescriptionTwo(org_id int64, patient_id int64, mode_id int64, schedule_date int64) (models.NewDialysisPrescription, error) {
  1283. dialysisprescription := models.NewDialysisPrescription{}
  1284. if org_id == 10340 {
  1285. err := XTReadDB().Where("user_org_id = ? and patient_id = ? and mode_id = ? and status = 1 and record_date <= ?", org_id, patient_id, mode_id, schedule_date).Order("id desc").Last(&dialysisprescription).Error
  1286. return dialysisprescription, err
  1287. } else {
  1288. err := XTReadDB().Where("user_org_id = ? and patient_id = ? and mode_id = ? and status = 1 and record_date = ?", org_id, patient_id, mode_id, schedule_date).Order("id desc").First(&dialysisprescription).Error
  1289. return dialysisprescription, err
  1290. }
  1291. }
  1292. func GetLastDialysisSolution(org_id int64, patient_id int64, mode_id int64) (models.DialysisSolution, error) {
  1293. dialysisSolution := models.DialysisSolution{}
  1294. err := XTReadDB().Where("user_org_id =? and patient_id = ? and mode_id = ? and status=1 and solution_status=1", org_id, patient_id, mode_id).Find(&dialysisSolution).Error
  1295. return dialysisSolution, err
  1296. }
  1297. func GetLastDialysisAfter(org_id int64, patient_id int64, schedule_date int64) (models.AssessmentAfterDislysis, error) {
  1298. dislysis := models.AssessmentAfterDislysis{}
  1299. err := XTReadDB().Where("user_org_id = ? and patient_id =? and assessment_date <= ? and status=1", org_id, patient_id, schedule_date).Order("id desc").Last(&dislysis).Error
  1300. return dislysis, err
  1301. }
  1302. func GetDoctorAdviceLastWeek(org_id int64, patient_id int64, schedule_date int64) (advice []*models.DoctorAdvice, err error) {
  1303. err = XTReadDB().Where("user_org_id = ? and patient_id = ? and status= 1 and parent_id = 0 and advice_date = ?", org_id, patient_id, schedule_date).Preload("ChildDoctorAdvice", "status = 1 and advice_date = ?", schedule_date).Find(&advice).Error
  1304. return advice, err
  1305. }
  1306. func GetHisDoctorAdviceListLastWeek(org_id int64, patient_id int64, schedule_date int64) (advice []*models.HisDoctorAdviceInfo, err error) {
  1307. err = XTReadDB().Where("user_org_id = ? and patient_id = ? and advice_date = ? and status= 1", org_id, patient_id, schedule_date).Find(&advice).Error
  1308. return advice, err
  1309. }
  1310. func GetDialysisSolutionLastWeek(user_org_id int64, patient_id int64, mode_id int64) (models.DialysisSolution, error) {
  1311. solution := models.DialysisSolution{}
  1312. err := XTReadDB().Where("user_org_id =? and status = 1 and patient_id = ? and mode_id = ? and solution_status = 1", user_org_id, patient_id, mode_id).Find(&solution).Error
  1313. return solution, err
  1314. }
  1315. func GetNextWeekDayScheduleSix(weektype int64, weektime int64, startime int64, endtime int64, orgID int64, zone []string, record_date int64, ids []string) (schedules []*models.WeekScheduleSeven, err error) {
  1316. db := XTReadDB().Table("xt_schedule as s ").Where("s.status =1")
  1317. if orgID > 0 {
  1318. db = db.Where("s.user_org_id = ?", orgID)
  1319. }
  1320. if weektype == 0 {
  1321. if startime > 0 {
  1322. db = db.Where("s.schedule_date >= ?", startime)
  1323. }
  1324. if endtime > 0 {
  1325. db = db.Where("s.schedule_date <= ?", endtime)
  1326. }
  1327. }
  1328. if weektype > 0 {
  1329. if record_date > 0 {
  1330. db = db.Where("s.schedule_date = ?", record_date)
  1331. }
  1332. }
  1333. if weektime > 0 {
  1334. db = db.Where("s.schedule_type = ?", weektime)
  1335. }
  1336. if weektype > 0 {
  1337. db = db.Where("s.schedule_week = ?", weektype)
  1338. }
  1339. if len(zone) > 0 {
  1340. db = db.Where("s.partition_id in (?)", zone)
  1341. }
  1342. if len(ids) > 0 {
  1343. db = db.Where("s.patient_id in(?)", ids)
  1344. }
  1345. err = db.
  1346. Preload("DeviceZone", "status = 1 ").
  1347. Preload("DeviceNumber", "status = 1 ").
  1348. Preload("XtAssessmentBeforeDislysis", func(db *gorm.DB) *gorm.DB {
  1349. return db.Where("status = 1 and assessment_date = ?", record_date)
  1350. }).Joins("JOIN xt_patients as p ON p.id = s.patient_id").
  1351. Select("s.id,s.user_org_id, s.partition_id, s.bed_id, s.patient_id, s.schedule_date, s.schedule_type, s.schedule_week, s.mode_id, s.status, s.created_time, s.updated_time, p.name as patient,p.id_card_no as id_card_no,p.dialysis_no as dialysis_no,p.gender as gender,p.phone as phone").
  1352. Order("s.partition_id desc").
  1353. Find(&schedules).Error
  1354. return schedules, err
  1355. }
  1356. func GetAssessmentBeforListLastWeek(schedule_date int64, patient_id int64, org_id int64) (models.XtAssessmentBeforeDislysis, error) {
  1357. dislysis := models.XtAssessmentBeforeDislysis{}
  1358. err := XTReadDB().Where("assessment_date = ? and patient_id = ? and user_org_id =? and status = 1", schedule_date, patient_id, org_id).Find(&dislysis).Error
  1359. return dislysis, err
  1360. }
  1361. func GetLastDialysisSolutionType(org_id int64, patient_id int64, mode_id int64) (models.NewDialysisSolutionOne, error) {
  1362. dialysisprescription := models.NewDialysisSolutionOne{}
  1363. err := XTReadDB().Where("user_org_id = ? and patient_id = ? and mode_id = ? and status = 1 and solution_status =1", org_id, patient_id, mode_id).First(&dialysisprescription).Error
  1364. return dialysisprescription, err
  1365. }
  1366. func GetScheduleTen(orgID, id int64) (models.Schedule, error) {
  1367. var schedule models.Schedule
  1368. var err error
  1369. err = readDb.Model(&models.Schedule{}).Where("id = ? and user_org_id=? and status=1", id, orgID).First(&schedule).Error
  1370. return schedule, err
  1371. }
  1372. func GetDialysisOrderByPatientAndBed(org_id int64, record_date int64, bed_id int64, patient_id int64) (order models.DialysisOrder, err error) {
  1373. err = readDb.Model(&models.DialysisOrder{}).Where("user_org_id = ? AND status = 1 AND dialysis_date = ? AND bed_id = ? AND patient_id = ?", org_id, record_date, bed_id, patient_id).First(&order).Error
  1374. return
  1375. }
  1376. func GetScheduleTemplateTen(orgID, id int64) (models.PatientScheduleTemplateItem, error) {
  1377. var schedule models.PatientScheduleTemplateItem
  1378. var err error
  1379. err = readDb.Model(&models.PatientScheduleTemplateItem{}).Where("id = ? and user_org_id=? and status=1", id, orgID).First(&schedule).Error
  1380. return schedule, err
  1381. }
  1382. func GetScheduleTemplateCountByDate(org_id int64, week_day int8, patient_id int64, template_id int64) (total int64, err error) {
  1383. err = readDb.Model(&models.PatientScheduleTemplateItem{}).Where(" org_id = ? AND weekday = ? AND status = 1 AND patient_id = ? AND template_id = ?", org_id, week_day, patient_id, template_id).Count(&total).Error
  1384. return
  1385. }
  1386. func SaveSchTemplateTwo(sch models.PatientScheduleTemplateItem, sch_two models.PatientScheduleTemplateItem, new_sch models.PatientScheduleTemplateItem) (err error) {
  1387. utx := writeDb.Begin()
  1388. if err = utx.Model(&models.PatientScheduleTemplateItem{}).Where("status=1 AND id = ?", sch.ID).Updates(map[string]interface{}{"status": 0}).Error; err != nil {
  1389. utx.Rollback()
  1390. return err
  1391. }
  1392. if err = utx.Model(&models.PatientScheduleTemplateItem{}).Where("status=1 AND id = ?", sch_two.ID).Updates(map[string]interface{}{"status": 0}).Error; err != nil {
  1393. utx.Rollback()
  1394. return err
  1395. }
  1396. if err = utx.Save(&new_sch).Error; err != nil {
  1397. utx.Rollback()
  1398. return err
  1399. }
  1400. utx.Commit()
  1401. return err
  1402. }
  1403. func SaveSchTemplate(sch models.PatientScheduleTemplateItem, sch_two models.PatientScheduleTemplateItem) (err error) {
  1404. utx := writeDb.Begin()
  1405. if err = utx.Save(&sch).Error; err != nil {
  1406. utx.Rollback()
  1407. return err
  1408. }
  1409. if err = utx.Save(&sch_two).Error; err != nil {
  1410. utx.Rollback()
  1411. return err
  1412. }
  1413. utx.Commit()
  1414. return err
  1415. }
  1416. func UpdateScheduleTemplateThree(schone models.PatientScheduleTemplateItem, schtwo models.PatientScheduleTemplateItem) error {
  1417. utx := XTWriteDB().Begin()
  1418. //床位
  1419. var tempBedID int64
  1420. var tempBedIDTwo int64
  1421. //上下午
  1422. var tempTypeID int8
  1423. var tempTypeTwo int8
  1424. //周数
  1425. var sch_week_one int8
  1426. var sch_week_two int8
  1427. tempBedID = schone.DeviceNumberID
  1428. tempBedIDTwo = schtwo.DeviceNumberID
  1429. tempTypeID = schone.TimeType
  1430. tempTypeTwo = schtwo.TimeType
  1431. sch_week_one = schone.Weekday
  1432. sch_week_two = schtwo.Weekday
  1433. schone.DeviceNumberID = tempBedIDTwo
  1434. schtwo.DeviceNumberID = tempBedID
  1435. schone.TimeType = tempTypeTwo
  1436. schtwo.TimeType = tempTypeID
  1437. schone.Weekday = sch_week_two
  1438. schtwo.Weekday = sch_week_one
  1439. if err = utx.Model(&models.PatientScheduleTemplateItem{}).Where("id = ?", schone.ID).Updates(map[string]interface{}{"device_number_id": schone.DeviceNumberID, "time_type": schone.TimeType, "weekday": schone.Weekday, "is_export": 70000}).Error; err != nil {
  1440. utx.Rollback()
  1441. return err
  1442. }
  1443. if err = utx.Model(&models.PatientScheduleTemplateItem{}).Where("id = ?", schtwo.ID).Updates(map[string]interface{}{"device_number_id": schtwo.DeviceNumberID, "time_type": schtwo.TimeType, "weekday": schtwo.Weekday, "is_export": 70000}).Error; err != nil {
  1444. utx.Rollback()
  1445. return err
  1446. }
  1447. utx.Commit()
  1448. return err
  1449. }
  1450. func GetScheduleTemplateForDeviceNumberTwo(orgID int64, deviceNumberID int64, template_id int64, week_type int64, time_type int64, patient_id int64) (models.PatientScheduleTemplateItem, error) {
  1451. var item models.PatientScheduleTemplateItem
  1452. err := readDb.Model(&models.PatientScheduleTemplateItem{}).Where("device_number_id = ? AND org_id = ? AND status = 1 AND template_id = ? AND weekday = ? AND time_type = ? AND patient_id = ?", deviceNumberID, orgID, template_id, week_type, time_type, patient_id).First(&item).Error
  1453. return item, err
  1454. }
  1455. func ClearSchedules(user_org_id int64, start_time int64) (err error) {
  1456. err = writeDb.Table("xt_schedule as s").
  1457. Where("s.schedule_date > ? AND s.status = 1 AND "+
  1458. "s.user_org_id = ?", start_time, user_org_id).
  1459. Updates(map[string]interface{}{"status": 0, "updated_time": time.Now().Unix()}).Error
  1460. return
  1461. }
  1462. func CreateScheduleTwo(m *models.Schedule) error {
  1463. utx := XTWriteDB().Begin()
  1464. err = utx.Create(&m).Error
  1465. if err != nil {
  1466. utx.Rollback()
  1467. return err
  1468. }
  1469. utx.Commit()
  1470. return err
  1471. }
  1472. func GetLongSolutionByModeId(patient_id int64, mode_id int64, user_org_id int64) (models.DialysisSolution, error) {
  1473. solution := models.DialysisSolution{}
  1474. err := XTReadDB().Where("patient_id = ? and mode_id = ? and user_org_id = ? and status= 1 and solution_status =1", patient_id, mode_id, user_org_id).Find(&solution).Error
  1475. return solution, err
  1476. }
  1477. func UpdateScheduleByDialysis(patient_id int64, mode_id int64, schedule_date int64, user_org_id int64, dialyzer_perfusion_apparatus string, schedule_type int64) error {
  1478. schedule := models.XtSchedule{}
  1479. err := XTWriteDB().Model(&schedule).Where("patient_id = ? and mode_id = ? and schedule_date = ? and user_org_id = ? and schedule_type = ?", patient_id, mode_id, schedule_date, user_org_id, schedule_type).Updates(map[string]interface{}{"dialysis_machine_name": dialyzer_perfusion_apparatus}).Error
  1480. return err
  1481. }
  1482. func GetScheduleByBedId(bed_id int64, schedule_type int64, user_org_id int64, schedule_date int64) (models.XtSchedule, error) {
  1483. schedule := models.XtSchedule{}
  1484. err := XTReadDB().Model(&schedule).Where("bed_id = ? and schedule_type =? and user_org_id = ? and schedule_date = ? and status =1", bed_id, schedule_type, user_org_id, schedule_date).Find(&schedule).Error
  1485. return schedule, err
  1486. }
  1487. func UpdateScheduleByScheduleId(id int64) error {
  1488. err := XTWriteDB().Model(&models.XtSchedule{}).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  1489. return err
  1490. }
  1491. func UpdateScheduleSixTy(id int64, schedules models.XtSchedule) error {
  1492. err := XTWriteDB().Model(&models.XtSchedule{}).Where("id = ? and status =1", id).Updates(map[string]interface{}{"mode_id": schedules.ModeId, "partition_id": schedules.PartitionId, "bed_id": schedules.BedId, "patient_id": schedules.PatientId, "schedule_date": schedules.ScheduleDate, "schedule_type": schedules.ScheduleType, "schedule_week": schedules.ScheduleWeek}).Error
  1493. return err
  1494. }
  1495. func CreateNewSchedule(schedule models.XtSchedule) error {
  1496. err := XTWriteDB().Create(&schedule).Error
  1497. return err
  1498. }
  1499. func GetNumberListById(id int64, org_id int64) (number []*models.DeviceNumber, err error) {
  1500. err = XTReadDB().Where("zone_id = ? and org_id = ? and status= 1", id, org_id).Find(&number).Error
  1501. return number, err
  1502. }
  1503. func CreateScheduleLog(log models.XtScheduleLog) error {
  1504. err := XTWriteDB().Create(&log).Error
  1505. return err
  1506. }
  1507. func GetDoctorAdviceListTwenty(patient_id int64, schedule_date int64, user_org_id int64) (advice []*models.XtDoctorAdvice, err error) {
  1508. err = XTReadDB().Where("patient_id = ? and advice_date = ? and user_org_id = ? and status=1", patient_id, schedule_date, user_org_id).Find(&advice).Error
  1509. return advice, err
  1510. }
  1511. func GetHisDoctorAdviceListTwenty(patient_id int64, schedule_date int64, user_org_id int64) (advice []*models.HisDoctorAdvice, err error) {
  1512. err = XTReadDB().Where("patient_id = ? and advice_date = ? and user_org_id = ? and status=1", patient_id, schedule_date, user_org_id).Find(&advice).Error
  1513. return advice, err
  1514. }
  1515. func GetHisDoctorAdviceListTwentyOne(patient_id int64, schedule_date int64, user_org_id int64) (advice []*models.HisDoctorAdviceInfo, err error) {
  1516. err = XTReadDB().Where("patient_id = ? and advice_date = ? and user_org_id = ? and status=1", patient_id, schedule_date, user_org_id).Find(&advice).Error
  1517. return advice, err
  1518. }
  1519. func GetHisPrescriptionProjectList(patient_id int64, record_date int64, user_org_id int64) (project []*models.HisPrescriptionProject, err error) {
  1520. err = XTReadDB().Where("patient_id = ? and record_date = ? and user_org_id = ? and status=1", patient_id, record_date, user_org_id).Find(&project).Error
  1521. return project, err
  1522. }
  1523. func UpdateProjectObj(patient_id int64, schedule_date int64, user_org_id int64) (err error) {
  1524. err = XTWriteDB().Model(&models.HisPrescriptionProject{}).Where("patient_id = ? and record_date =? and status=1 and user_org_id = ?", patient_id, schedule_date, user_org_id).Updates(map[string]interface{}{"status": 0}).Error
  1525. err = XTWriteDB().Model(&models.HisPrescription{}).Where("patient_id = ? and status=1 and record_date = ? and user_org_id =?", patient_id, schedule_date, user_org_id).Updates(map[string]interface{}{"status": 0}).Error
  1526. err = XTWriteDB().Model(&models.HisPrescriptionInfo{}).Where("patient_id = ? and status=1 and record_date = ? and user_org_id = ?", patient_id, schedule_date, user_org_id).Updates(map[string]interface{}{"status": 0}).Error
  1527. return err
  1528. }
  1529. func UpdateAdviceObj(patient_id int64, schedule_date int64, user_org_id int64) (err error) {
  1530. err = XTWriteDB().Model(&models.XtDoctorAdvice{}).Where("patient_id = ? and advice_date =? and status=1 and user_org_id = ?", patient_id, schedule_date, user_org_id).Updates(map[string]interface{}{"status": 0}).Error
  1531. return err
  1532. }
  1533. func UpdateHisAdviceObj(patient_id int64, schedule_date int64, user_org_id int64) (err error) {
  1534. err = XTWriteDB().Model(&models.HisDoctorAdvice{}).Where("patient_id = ? and advice_date =? and status=1 and user_org_id = ?", patient_id, schedule_date, user_org_id).Updates(map[string]interface{}{"status": 0}).Error
  1535. return err
  1536. }
  1537. func GetLastSchedule(patient_id int64, user_org_id int64) (models.XtSchedule, error) {
  1538. schedule := models.XtSchedule{}
  1539. err := XTReadDB().Where("patient_id = ? and status =1 and user_org_id = ?", patient_id, user_org_id).Find(&schedule).Error
  1540. return schedule, err
  1541. }
  1542. func GetPatientScheduleToday(patient_id int64, schedule_date int64, user_org_id int64) (models.XtSchedule, error) {
  1543. schedule := models.XtSchedule{}
  1544. err = XTReadDB().Where("patient_id = ? and schedule_date = ? and user_org_id = ? and status=0", patient_id, schedule_date, user_org_id).Find(&schedule).Error
  1545. return schedule, err
  1546. }
  1547. func UpdateRepeatSchStatus(user_org_id int64, schedule_date int64) {
  1548. var schedules []models.XtSchedule
  1549. // Find duplicate records based on patient_id, schedule_date, and user_org_id
  1550. XTReadDB().Where("user_org_id = ? AND schedule_date = ? AND status = ?", user_org_id, schedule_date, 1).
  1551. Group("patient_id, schedule_date").
  1552. Having("COUNT(*) > 1").
  1553. Select("patient_id, schedule_date, MAX(id) as max_id").
  1554. Find(&schedules)
  1555. for _, schedule := range schedules {
  1556. // Update records with status = 0 where id is less than max_id
  1557. XTReadDB().Model(&models.XtSchedule{}).Where("patient_id = ? AND schedule_date = ? AND id < ?", schedule.PatientId, schedule.ScheduleDate, schedule.MaxId).Update("status", 0)
  1558. }
  1559. }
  1560. func GetSchedulePatientIdByPatient(patient_id int64, schedule_date int64, user_org_id int64) (models.Schedule, error) {
  1561. schedule := models.Schedule{}
  1562. err := XTReadDB().Where("patient_id = ? and schedule_date = ? and user_org_id = ? and status =1", patient_id, schedule_date, user_org_id).Find(&schedule).Error
  1563. return schedule, err
  1564. }
  1565. func UpdateMobileSchedule(id_two int64, mode_id int64, bed_id int64, zone_id int64, schedule_type int64, id int64) error {
  1566. schedule := models.Schedule{}
  1567. err = XTWriteDB().Model(&schedule).Where("id = ? and status=1", id).Updates(map[string]interface{}{"patient_id": id_two, "mode_id": mode_id, "bed_id": bed_id, "partition_id": zone_id, "schedule_type": schedule_type}).Error
  1568. return err
  1569. }
  1570. func GetPatientGoodCountToday(patient_id int64, schedule_date int64, user_org_id int64) (list []*models.WarehouseOutInfo, err error) {
  1571. err = XTReadDB().Where("patient_id =? and sys_record_time=? and org_id = ? and status=1 and count>0", patient_id, schedule_date, user_org_id).Find(&list).Error
  1572. return list, err
  1573. }
  1574. func GetPatientDrugCountToday(patient_id int64, schedule_date int64, user_org_id int64) (list []*models.DrugWarehouseOutInfo, err error) {
  1575. err = XTReadDB().Where("patient_id =? and sys_record_time=? and org_id = ? and status=1", patient_id, schedule_date, user_org_id).Find(&list).Error
  1576. return list, err
  1577. }