schedule_service.go 78KB

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