schedule_service.go 60KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465
  1. package service
  2. import (
  3. "XT_New/models"
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. "time"
  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. //if len(partionId) > 0 {
  17. // err = readDb.Model(&models.DeviceZone{}).Preload("Jihaos", "org_id=? and status=1 and zone_id in (?)", orgID, partionId).Where("org_id=? and status=1 and id ?", orgID, partionId).Find(&partitions).Error
  18. //}
  19. //if len(partionId) == 0 {
  20. err = readDb.Model(&models.DeviceZone{}).Preload("Jihaos", "org_id=? and status=1", orgID).Where("org_id=? and status=1", orgID).Find(&partitions).Error
  21. //}
  22. return
  23. }
  24. func GetSchedulePartitionPanelTwo(orgID int64, schIDs []string) (partitions []*models.Partition, err error) {
  25. if len(schIDs) == 0 {
  26. err = readDb.Model(&models.DeviceZone{}).Preload("Jihaos", "org_id=? and status=1", orgID).Where("org_id=? and status=1", orgID).Find(&partitions).Error
  27. }
  28. if len(schIDs) > 0 {
  29. err = readDb.Model(&models.DeviceZone{}).Preload("Jihaos", func(db *gorm.DB) *gorm.DB {
  30. return db.Where("org_id=? and status=1 and zone_id in (?)", orgID, schIDs).Order("sort desc")
  31. }).Where("org_id=? and status=1 and id in(?)", orgID, schIDs).Find(&partitions).Error
  32. }
  33. return
  34. }
  35. func GetPatientWithScheduleAndSolution(orgID int64, keywords string, weekStart, weekEnd, schedule, contagion int64) (patients []*models.Patients, err error) {
  36. db := readDb.Table("xt_patients as p").
  37. Preload("DialysisSolutions", "user_org_id=? and initiate_mode=1 and affirm_state=1 and parent_id=0 and status=1", orgID).
  38. Preload("Schedules", "user_org_id=? and schedule_date>=? and schedule_date<= ? and status=1", orgID, weekStart, weekEnd).
  39. Preload("Contagions", "status=1").
  40. Where("p.user_org_id=? and p.lapseto=1 and p.status=1", orgID)
  41. if len(keywords) > 0 {
  42. likeKey := "%" + keywords + "%"
  43. db = db.Where("p.name LIKE ?", likeKey)
  44. }
  45. if schedule == 1 {
  46. 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())
  47. } else if schedule == 2 {
  48. 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())
  49. }
  50. if contagion > 0 {
  51. 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())
  52. }
  53. err = db.Find(&patients).Error
  54. return
  55. }
  56. func GetPatientWithScheduleAndSolutionTwo(orgID int64, keywords string, weekStart, weekEnd, schedule, contagion int64) (patients []*models.Patients, err error) {
  57. db := readDb.Table("xt_patients as p").
  58. Preload("DialysisSolutions", "user_org_id=? and initiate_mode=1 and affirm_state=1 and parent_id=0 and status=1", orgID).
  59. //Preload("Schedules", "user_org_id=? and schedule_date>=? and schedule_date<= ? and status=1", orgID, weekStart, weekEnd).
  60. Where("p.user_org_id=? and p.lapseto=1 and p.status=1", orgID)
  61. if len(keywords) > 0 {
  62. likeKey := "%" + keywords + "%"
  63. db = db.Where("p.name LIKE ?", likeKey)
  64. }
  65. err = db.Find(&patients).Error
  66. return
  67. }
  68. func GetWeekSchedule(orgID int64, start, end int64) (schedules []*models.Schedule, err error) {
  69. err = readDb.Table("xt_schedule as s").Preload("PatientInfectiousDiseases", "status = 1 ").
  70. Joins("JOIN xt_patients as p ON p.id = s.patient_id").
  71. Where("s.user_org_id=? and s.schedule_date>=? and s.schedule_date<=? and s.status=1", orgID, start, end).
  72. 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").
  73. Order("updated_time asc").
  74. Find(&schedules).Error
  75. return
  76. }
  77. func GetWeekScheduleOne(orgID int64, start, end int64, partionid int64, scheduletype int64) (schedules []*models.Schedule, err error) {
  78. db := readDb.Table("xt_schedule as s")
  79. if partionid > 0 {
  80. db = db.Where("s.partition_id = ?", partionid)
  81. }
  82. if scheduletype > 0 {
  83. db = db.Where("s.schedule_type = ?", scheduletype)
  84. }
  85. err = db.Preload("PatientInfectiousDiseases", "status = 1 ").
  86. Joins("JOIN xt_patients as p ON p.id = s.patient_id").
  87. Where("s.user_org_id=? and s.schedule_date>=? and s.schedule_date<=? and s.status=1", orgID, start, end).
  88. 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").
  89. Order("updated_time asc").
  90. Find(&schedules).Error
  91. return
  92. }
  93. func GetWeekScheduleTwo(orgID int64, start, end int64, schIds []string, scheduletype int64) (schedules []*models.Schedule, err error) {
  94. db := readDb.Table("xt_schedule as s")
  95. if len(schIds) > 0 {
  96. db = db.Where("s.partition_id in (?)", schIds)
  97. }
  98. if scheduletype > 0 {
  99. db = db.Where("s.schedule_type = ?", scheduletype)
  100. }
  101. err = db.Preload("PatientInfectiousDiseases", "status = 1 ").
  102. Joins("JOIN xt_patients as p ON p.id = s.patient_id").
  103. Where("s.user_org_id=? and s.schedule_date>=? and s.schedule_date<=? and s.status=1", orgID, start, end).
  104. 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").
  105. Order("updated_time asc").
  106. Find(&schedules).Error
  107. return
  108. }
  109. func GetWeekScheduleThree(orgID int64, start, end int64, schIds []string, scheduletype int64, WeekDayIds []string) (schedules []*models.Schedule, err error) {
  110. db := readDb.Table("xt_schedule as s")
  111. if len(schIds) > 0 {
  112. db = db.Where("s.partition_id in (?)", schIds)
  113. }
  114. if scheduletype > 0 {
  115. db = db.Where("s.schedule_type = ?", scheduletype)
  116. }
  117. if len(WeekDayIds) > 0 {
  118. db = db.Where("s.schedule_week in(?)", WeekDayIds)
  119. }
  120. err = db.Preload("PatientInfectiousDiseases", "status = 1 ").
  121. Joins("JOIN xt_patients as p ON p.id = s.patient_id").
  122. Where("s.user_org_id=? and s.schedule_date>=? and s.schedule_date<=? and s.status=1", orgID, start, end).
  123. 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").
  124. Order("updated_time asc").
  125. Find(&schedules).Error
  126. return
  127. }
  128. func GetDaySchedule(orgID, start, end, patientID int64) (schedule models.Schedule, err error) {
  129. 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
  130. return
  131. }
  132. func GetDayScheduleTwo(orgID, start, patientID int64) (schedule models.Schedule, err error) {
  133. 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
  134. return
  135. }
  136. func GetDayScheduleByBedid(orgID, start, bed_id int64, schedule_type int64) (schedule models.Schedule, err error) {
  137. 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
  138. return
  139. }
  140. func GetSourceSch(orgId int64, patient_id int64, record_date int64) (schedule models.Schedule, err error) {
  141. 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
  142. return
  143. }
  144. func GetPatientScheduleFormDay(orgID, start, end, patientID int64) (schedules []*models.PatientSchedule, err error) {
  145. err = readDb.Table("xt_schedule as s").
  146. Preload("DeviceZone", "org_id=? and status=1", orgID).
  147. Preload("DeviceNumber", "org_id=? and status=1", orgID).
  148. 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).
  149. Find(&schedules).Error
  150. return
  151. }
  152. func GetNextSchedule(orgID, end, patientID int64) (schedules []*models.PatientSchedule, err error) {
  153. err = readDb.Table("xt_schedule as s").
  154. Preload("DeviceZone", "org_id=? and status=1", orgID).
  155. Preload("DeviceNumber", "org_id=? and status=1", orgID).
  156. Where("s.patient_id =? and s.user_org_id=? and s.schedule_date>=? and s.status=1", patientID, orgID, end).
  157. Find(&schedules).Error
  158. return
  159. }
  160. func GetPointSchedule(orgID, scheduleDate, scheduleWeek, scheduleType, bedId int64) (schedule models.Schedule, err error) {
  161. 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
  162. if err == gorm.ErrRecordNotFound {
  163. err = nil
  164. return
  165. }
  166. return
  167. }
  168. func CreateSchedule(m *models.Schedule) error {
  169. utx := XTWriteDB().Begin()
  170. err := utx.Create(&m).Error
  171. utx.Commit()
  172. return err
  173. }
  174. func DeleteScheduleExist(scheduleData int64, user_org_id int64) (models.XtSchedule, error) {
  175. schedule := models.XtSchedule{}
  176. 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
  177. return schedule, err
  178. }
  179. func GetTodayScheduleIsExist(patientid int64, schedudate int64, orgid int64, mode_id int64, schedule_type int64) (*models.XtSchedule, error) {
  180. var scheudle models.XtSchedule
  181. var err error
  182. 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
  183. if err == gorm.ErrRecordNotFound {
  184. return nil, err
  185. }
  186. if err != nil {
  187. return nil, err
  188. }
  189. return &scheudle, nil
  190. }
  191. func GetTodayScheduleIsExistOne(patientid int64, schedudate int64, orgid int64) (*models.XtSchedule, error) {
  192. var scheudle models.XtSchedule
  193. var err error
  194. err = XTReadDB().Model(&scheudle).Where("patient_id = ? and schedule_date= ? and user_org_id = ? and status = 1", patientid, schedudate, orgid).Find(&scheudle).Error
  195. if err == gorm.ErrRecordNotFound {
  196. return nil, err
  197. }
  198. if err != nil {
  199. return nil, err
  200. }
  201. return &scheudle, nil
  202. }
  203. func GetPatientByBed(schedule_date int64, bed_id int64, scheduletype int64, orgid int64) (*models.XtSchedule, error) {
  204. var scheudle models.XtSchedule
  205. var err error
  206. 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
  207. if err == gorm.ErrRecordNotFound {
  208. return nil, err
  209. }
  210. if err != nil {
  211. return nil, err
  212. }
  213. return &scheudle, nil
  214. }
  215. func ModeFyScheduleById(schedule_date int64, bedid int64, scheduletype int64, orgid int64) error {
  216. schedule := models.XtSchedule{}
  217. 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
  218. return err
  219. }
  220. func ModeScheduleByTime(startime int64, endtime int64, orgid int64) error {
  221. schedule := models.XtSchedule{}
  222. err := XTWriteDB().Model(&schedule).Where("schedule_date >? and schedule_date<=? and status = 1 and user_org_id = ?", startime, endtime, orgid).Updates(map[string]interface{}{"status": 0}).Error
  223. return err
  224. }
  225. func ModeScheduleByTimeOne(startime int64, endtime int64, orgid int64) error {
  226. schedule := models.XtSchedule{}
  227. err := XTWriteDB().Model(&schedule).Where("schedule_date >=? and schedule_date<=? and status = 1 and user_org_id = ?", startime, endtime, orgid).Updates(map[string]interface{}{"status": 0}).Error
  228. return err
  229. }
  230. func UpdateScheduleByOrgId(patientid int64, scheduledate int64, orgid int64, mode_id int64, schedule_type int64, sch *models.XtSchedule) error {
  231. err := XTWriteDB().Model(&sch).Where("patient_id = ? and schedule_date= ? and user_org_id = ? and status = 1 and mode_id = ? and schedule_type = ?", patientid, scheduledate, orgid, mode_id, schedule_type).Updates(map[string]interface{}{"partition_id": sch.PartitionId, "bed_id": sch.BedId, "schedule_type": sch.ScheduleType, "schedule_week": sch.ScheduleWeek, "mode_id": sch.ModeId, "updated_time": time.Now().Unix()}).Error
  232. return err
  233. }
  234. func UpdateScheduleByOrgIdOne(patientid int64, scheduledate int64, orgid int64, sch *models.XtSchedule) error {
  235. 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
  236. return err
  237. }
  238. func GetSchedule(orgID, id int64) (*models.Schedule, error) {
  239. var schedule models.Schedule
  240. var err error
  241. err = readDb.Model(&models.Schedule{}).Where("id = ? and user_org_id=? and status=1", id, orgID).First(&schedule).Error
  242. if err != nil {
  243. return nil, err
  244. }
  245. return &schedule, nil
  246. }
  247. func GetScheduleNight(orgID, id int64) (*models.Schedule, error) {
  248. var schedule models.Schedule
  249. var err error
  250. err = readDb.Model(&models.Schedule{}).Where("id = ? and user_org_id=?", id, orgID).First(&schedule).Error
  251. if err != nil {
  252. return nil, err
  253. }
  254. return &schedule, nil
  255. }
  256. func UpdateSchedule(m *models.Schedule) error {
  257. utx := XTWriteDB().Begin()
  258. err := utx.Save(&m).Error
  259. utx.Commit()
  260. return err
  261. }
  262. func UpdateScheduleOne(id int64, schedule models.Schedule) error {
  263. utx := XTWriteDB().Begin()
  264. 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
  265. utx.Commit()
  266. return err
  267. }
  268. // 参数一:当前操作的患者的排班 参数二被替换床位对应的排班
  269. func UpdateScheduleTwo(schone models.Schedule, schtwo models.Schedule) {
  270. utx := XTWriteDB()
  271. //床位
  272. var tempBedID int64
  273. var tempBedIDTwo int64
  274. //分區
  275. var tempPID int64
  276. var tempPIDTwo int64
  277. //上下午
  278. var tempTypeID int64
  279. var tempTypeTwo int64
  280. tempBedID = schone.BedId
  281. tempBedIDTwo = schtwo.BedId
  282. tempPID = schone.PartitionId
  283. tempPIDTwo = schtwo.PartitionId
  284. tempTypeID = schone.ScheduleType
  285. tempTypeTwo = schtwo.ScheduleType
  286. schone.BedId = tempBedIDTwo
  287. schtwo.BedId = tempBedID
  288. schone.PartitionId = tempPIDTwo
  289. schtwo.PartitionId = tempPID
  290. schone.ScheduleType = tempTypeTwo
  291. schtwo.ScheduleType = tempTypeID
  292. schone.IsExport = 9000
  293. schtwo.IsExport = 9000
  294. 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": 9000})
  295. 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": 9000})
  296. }
  297. func UpdateScheduleThree(schone *models.Schedule, schtwo *models.Schedule) {
  298. utx := XTWriteDB()
  299. //床位
  300. var tempBedID int64
  301. var tempBedIDTwo int64
  302. //分區
  303. var tempPID int64
  304. var tempPIDTwo int64
  305. //上下午
  306. var tempTypeID int64
  307. var tempTypeTwo int64
  308. //日期
  309. var sch_date_one int64
  310. var sch_date_two int64
  311. //周数
  312. var sch_week_one int64
  313. var sch_week_two int64
  314. tempBedID = schone.BedId
  315. tempBedIDTwo = schtwo.BedId
  316. tempPID = schone.PartitionId
  317. tempPIDTwo = schtwo.PartitionId
  318. tempTypeID = schone.ScheduleType
  319. tempTypeTwo = schtwo.ScheduleType
  320. sch_date_one = schone.ScheduleDate
  321. sch_date_two = schtwo.ScheduleDate
  322. sch_week_one = schone.ScheduleWeek
  323. sch_week_two = schtwo.ScheduleWeek
  324. schone.BedId = tempBedIDTwo
  325. schtwo.BedId = tempBedID
  326. schone.PartitionId = tempPIDTwo
  327. schtwo.PartitionId = tempPID
  328. schone.ScheduleType = tempTypeTwo
  329. schtwo.ScheduleType = tempTypeID
  330. schone.ScheduleDate = sch_date_two
  331. schtwo.ScheduleDate = sch_date_one
  332. schone.ScheduleWeek = sch_week_two
  333. schtwo.ScheduleWeek = sch_week_one
  334. schone.IsExport = 9000
  335. schtwo.IsExport = 9000
  336. 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": 9000})
  337. 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": 9000})
  338. }
  339. func GetPatientScheduleFormToday(orgId, id, start int64) (schedules []*models.PatientSchedule, err error) {
  340. err = readDb.Table("xt_schedule as s").
  341. Preload("DeviceZone", "org_id=? and status=1", orgId).
  342. Preload("DeviceNumber", "org_id=? and status=1", orgId).
  343. Where("s.patient_id =? and s.user_org_id=? and s.schedule_date>=? and s.status=1", id, orgId, start).
  344. Find(&schedules).Error
  345. return
  346. }
  347. type WeekSchedulePrintVM struct {
  348. ID int64 `gorm:"column:id" json:"id"`
  349. ZoneID int64 `gorm:"column:partition_id" json:"zone_id"`
  350. DeviceNumberID int64 `gorm:"column:bed_id" json:"device_number_id"`
  351. PatientID int64 `gorm:"column:patient_id" json:"patient_id"`
  352. ScheduleDate int64 `gorm:"column:schedule_date" json:"schedule_date"`
  353. ScheduleType int64 `gorm:"column:schedule_type" json:"schedule_type"`
  354. ScheduleWeek int64 `gorm:"column:schedule_week" json:"schedule_week"`
  355. ModeID int64 `gorm:"column:mode_id" json:"mode_id"`
  356. Patient *models.Patients `gorm:"ForeignKey:PatientID" json:"patient"`
  357. DeviceNumber *models.DeviceNumber `gorm:"ForeignKey:DeviceNumberID" json:"device_number"`
  358. Zone *models.DeviceZone `gorm:"ForeignKey:ZoneID" json:"zone"`
  359. Mode *models.TreatmentMode `gorm:"ForeignKey:ModeID" json:"mode"`
  360. }
  361. type PrintCount struct {
  362. ScheduleType int64 `gorm:"column:schedule_type" json:"schedule_type"`
  363. ScheduleWeek int64 `gorm:"column:schedule_week" json:"schedule_week"`
  364. Count int64
  365. }
  366. func GetPrinitWeekSchedules(orgID int64, start, end int64) ([]*WeekSchedulePrintVM, error) {
  367. var schedules []*WeekSchedulePrintVM
  368. err := readDb.Table("xt_schedule as s").
  369. Preload("Patient", "status = 1 and user_org_id = ?", orgID).
  370. Preload("DeviceNumber", "status = 1 and org_id = ?", orgID).
  371. Preload("Zone", "status = 1 and org_id = ?", orgID).
  372. Preload("Mode", "status = 1").
  373. // Joins("JOIN xt_patients as p ON p.id = s.patient_id").
  374. Where("s.user_org_id = ? and s.schedule_date >= ? and s.schedule_date <= ? and s.status = 1", orgID, start, end).
  375. // 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").
  376. Find(&schedules).Error
  377. return schedules, err
  378. }
  379. func GetPrinitWeekTotal(orgid int64, startime int64, endtime int64) (print []*PrintCount, err error) {
  380. db := XTReadDB().Table("xt_schedule as x").Where("x.status = 1")
  381. if orgid > 0 {
  382. db = db.Where("x.user_org_id = ?", orgid)
  383. }
  384. if startime > 0 {
  385. db = db.Where("x.schedule_date >=?", startime)
  386. }
  387. if endtime > 0 {
  388. db = db.Where("x.schedule_date <=?", endtime)
  389. }
  390. 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
  391. return print, err
  392. }
  393. func GetBloodPrintWeekTotal(orgid int64, startime int64, endtime int64) (print []*PrintCount, err error) {
  394. db := XTReadDB().Table("xt_schedule as x").Where("x.status = 1")
  395. if orgid > 0 {
  396. db = db.Where("x.user_org_id = ?", orgid)
  397. }
  398. if startime > 0 {
  399. db = db.Where("x.schedule_date >=?", startime)
  400. }
  401. if endtime > 0 {
  402. db = db.Where("x.schedule_date <=?", endtime)
  403. }
  404. db = db.Where("x.mode_id = 2")
  405. 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
  406. return print, err
  407. }
  408. func GetPerfusionWeekTotal(orgid int64, startime int64, endtime int64) (print []*PrintCount, err error) {
  409. db := XTReadDB().Table("xt_schedule as x").Where("x.status = 1")
  410. if orgid > 0 {
  411. db = db.Where("x.user_org_id = ?", orgid)
  412. }
  413. if startime > 0 {
  414. db = db.Where("x.schedule_date >=?", startime)
  415. }
  416. if endtime > 0 {
  417. db = db.Where("x.schedule_date <=?", endtime)
  418. }
  419. db = db.Where("x.mode_id = 3")
  420. 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
  421. return print, err
  422. }
  423. // func ExchangeScheduleTimeWithWeekSchedules(orgID int64, nextWeekSchs []*models.Schedule, nextTwoWeekSchs []*models.Schedule) error {
  424. // templateMode, getTemplateModeErr := GetOrgPatientScheduleTemplateMode(orgID)
  425. // if getTemplateModeErr != nil {
  426. // return getTemplateModeErr
  427. // }
  428. // if templateMode == nil {
  429. // return errors.New("模板模式不存在")
  430. // }
  431. // tx := writeDb.Begin()
  432. // updateTime := time.Now().Unix()
  433. // for _, sch := range nextWeekSchs {
  434. // sch.PatientInfectiousDiseases = nil
  435. // date := time.Unix(sch.ScheduleDate, 0)
  436. // sch.ScheduleDate = date.AddDate(0, 0, 7).Unix()
  437. // sch.UpdatedTime = updateTime
  438. // updateSchErr := tx.Save(sch).Error
  439. // if updateSchErr != nil {
  440. // tx.Rollback()
  441. // return updateSchErr
  442. // }
  443. // }
  444. // for _, sch := range nextTwoWeekSchs {
  445. // sch.PatientInfectiousDiseases = nil
  446. // date := time.Unix(sch.ScheduleDate, 0)
  447. // sch.ScheduleDate = date.AddDate(0, 0, -7).Unix()
  448. // sch.UpdatedTime = updateTime
  449. // updateSchErr := tx.Save(sch).Error
  450. // if updateSchErr != nil {
  451. // tx.Rollback()
  452. // return updateSchErr
  453. // }
  454. // }
  455. // templateMode.ModifyTime = updateTime
  456. // templateMode.ExecuteTimes += 1
  457. // updateModeErr := tx.Save(templateMode).Error
  458. // if updateModeErr != nil {
  459. // tx.Rollback()
  460. // return updateModeErr
  461. // }
  462. // tx.Commit()
  463. // return nil
  464. // }
  465. func GetSchedualPatientsByKeywordsAndWeek(keywords string, org_id int64, start_time int64, end_time int64) (schedule []*models.Schedule, err error) {
  466. likeKey := "%" + keywords + "%"
  467. err = readDb.
  468. Table("xt_schedule as s").
  469. Preload("PatientInfectiousDiseases", "status = 1 ").
  470. Preload("DeviceZone", "status = 1 ").
  471. Preload("DeviceNumber", "status = 1 ").
  472. Preload("TreatmentMode", "status = 1 ").
  473. Joins("JOIN xt_patients as p ON p.id = s.patient_id AND (p.name LIKE ? OR p.dialysis_no LIKE ?)", likeKey, likeKey).
  474. Where("s.user_org_id=? and s.schedule_date >= ? and s.schedule_date <= ? and s.status=1", org_id, start_time, end_time).
  475. 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").
  476. Order("schedule_date asc").
  477. Find(&schedule).Error
  478. return
  479. }
  480. func GetSchedualPatientsByKeywords(keywords string, org_id int64, startime int64, endtime int64) (schedule []*models.Schedule, err error) {
  481. likeKey := "%" + keywords + "%"
  482. err = readDb.
  483. Table("xt_schedule as s").
  484. Preload("PatientInfectiousDiseases", "status = 1 ").
  485. Preload("DeviceZone", "status = 1 ").
  486. Preload("DeviceNumber", "status = 1 ").
  487. Preload("TreatmentMode", "status = 1 ").
  488. Joins("JOIN xt_patients as p ON p.id = s.patient_id AND (p.name LIKE ? OR p.dialysis_no LIKE ?)", likeKey, likeKey).
  489. Where("s.user_org_id=? and s.schedule_date >= ? and s.schedule_date <= ? and s.status=1", org_id, startime, endtime).
  490. 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").
  491. Order("schedule_date asc").
  492. Find(&schedule).Error
  493. return
  494. }
  495. func GetWeekDayScheduleTwo(orgID int64, scheduleDate int64, scheduleDates *time.Time, weektime int64) (schedules []*models.WeekSchedule, err error) {
  496. db := readDb.Table("xt_schedule as s ").Where("s.status =1")
  497. if orgID > 0 {
  498. db = db.Where("s.user_org_id = ?", orgID)
  499. }
  500. if scheduleDate > 0 {
  501. db = db.Where("s.schedule_date = ?", scheduleDate)
  502. }
  503. if weektime > 0 {
  504. db = db.Where("s.schedule_type = ?", weektime)
  505. }
  506. err = db.
  507. Preload("DeviceZone", "status = 1 ").
  508. Preload("DeviceNumber", "status = 1 ").
  509. Preload("DialysisPrescription", func(db *gorm.DB) *gorm.DB {
  510. return db.Where("status = 1").Order("id asc")
  511. }).
  512. Joins("JOIN xt_patients as p ON p.id = s.patient_id").
  513. 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").
  514. Order("s.partition_id asc").
  515. Find(&schedules).Error
  516. for _, item := range schedules {
  517. _, config := FindDoctorAdviceRecordByOrgId(item.UserOrgId)
  518. if config.IsOpenRemind > 0 {
  519. //开启推送提醒逻辑 提交长期处方的时候,弹起长期医嘱推送
  520. var advice_three []*models.DoctorAdvice
  521. advices, _ := GetLastLongAdviceByGroupNoOther(item.UserOrgId, item.PatientId)
  522. advices_two, _ := GetLastLongAdviceByGroupNoThreeOther(item.UserOrgId, item.PatientId)
  523. for _, advice := range advices {
  524. advice.IsCheck = 1
  525. if advice.FrequencyType == 3 {
  526. week := int(scheduleDates.Weekday())
  527. switch week {
  528. case 1:
  529. if strings.Index(advice.WeekDay, "周一") > -1 {
  530. advice_three = append(advice_three, advice)
  531. }
  532. break
  533. case 2:
  534. if strings.Index(advice.WeekDay, "周二") > -1 {
  535. advice_three = append(advice_three, advice)
  536. }
  537. break
  538. case 3:
  539. if strings.Index(advice.WeekDay, "周三") > -1 {
  540. advice_three = append(advice_three, advice)
  541. }
  542. break
  543. case 4:
  544. if strings.Index(advice.WeekDay, "周四") > -1 {
  545. advice_three = append(advice_three, advice)
  546. }
  547. break
  548. case 5:
  549. if strings.Index(advice.WeekDay, "周五") > -1 {
  550. advice_three = append(advice_three, advice)
  551. }
  552. break
  553. case 6:
  554. if strings.Index(advice.WeekDay, "周六") > -1 {
  555. advice_three = append(advice_three, advice)
  556. }
  557. break
  558. case 0:
  559. if strings.Index(advice.WeekDay, "周日") > -1 {
  560. advice_three = append(advice_three, advice)
  561. }
  562. break
  563. }
  564. } else if advice.FrequencyType == 1 {
  565. advice_three = append(advice_three, advice)
  566. }
  567. }
  568. for _, advice := range advices_two {
  569. now := scheduleDates.Unix()
  570. dayStr := strconv.FormatInt(advice.DayCount, 10)
  571. dayStr2 := "-" + dayStr
  572. count, _ := strconv.ParseInt(dayStr2, 10, 64)
  573. oldTime := scheduleDates.AddDate(0, 0, int(count)).Unix()
  574. advices, _ := FindAllDoctorAdviceByTimeOther(now, oldTime, item.PatientId, item.UserOrgId, advice.TemplateId)
  575. if len(advices) == 0 {
  576. advice_three = append(advice_three, advice)
  577. }
  578. }
  579. item.DoctorAdvice = append(item.DoctorAdvice, advice_three...)
  580. }
  581. }
  582. return
  583. }
  584. func GetWeekDaySchedule(orgID int64, scheduleDate int64, scheduleDates *time.Time) (schedules []*models.WeekSchedule, err error) {
  585. err = readDb.
  586. Table("xt_schedule as s").
  587. Preload("DeviceZone", "status = 1 ").
  588. Preload("DeviceNumber", "status = 1 ").
  589. Preload("DialysisPrescription", func(db *gorm.DB) *gorm.DB {
  590. return db.Where("status = 1").Order("id asc")
  591. }).
  592. Joins("JOIN xt_patients as p ON p.id = s.patient_id").
  593. Where("s.user_org_id=? and s.schedule_date = ? and s.status=1", orgID, scheduleDate).
  594. 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").
  595. Order("schedule_type").
  596. Find(&schedules).Error
  597. for _, item := range schedules {
  598. _, config := FindDoctorAdviceRecordByOrgId(item.UserOrgId)
  599. if config.IsOpenRemind > 0 {
  600. //开启推送提醒逻辑 提交长期处方的时候,弹起长期医嘱推送
  601. var advice_three []*models.DoctorAdvice
  602. advices, _ := GetLastLongAdviceByGroupNoOther(item.UserOrgId, item.PatientId)
  603. advices_two, _ := GetLastLongAdviceByGroupNoThreeOther(item.UserOrgId, item.PatientId)
  604. for _, advice := range advices {
  605. advice.IsCheck = 1
  606. if advice.FrequencyType == 3 {
  607. week := int(scheduleDates.Weekday())
  608. fmt.Println(advice.WeekDay)
  609. switch week {
  610. case 1:
  611. if strings.Index(advice.WeekDay, "周一") > -1 {
  612. advice_three = append(advice_three, advice)
  613. }
  614. break
  615. case 2:
  616. if strings.Index(advice.WeekDay, "周二") > -1 {
  617. advice_three = append(advice_three, advice)
  618. }
  619. break
  620. case 3:
  621. if strings.Index(advice.WeekDay, "周三") > -1 {
  622. advice_three = append(advice_three, advice)
  623. }
  624. break
  625. case 4:
  626. if strings.Index(advice.WeekDay, "周四") > -1 {
  627. advice_three = append(advice_three, advice)
  628. }
  629. break
  630. case 5:
  631. if strings.Index(advice.WeekDay, "周五") > -1 {
  632. advice_three = append(advice_three, advice)
  633. }
  634. break
  635. case 6:
  636. if strings.Index(advice.WeekDay, "周六") > -1 {
  637. advice_three = append(advice_three, advice)
  638. }
  639. break
  640. case 0:
  641. if strings.Index(advice.WeekDay, "周日") > -1 {
  642. advice_three = append(advice_three, advice)
  643. }
  644. break
  645. }
  646. } else if advice.FrequencyType == 1 {
  647. advice_three = append(advice_three, advice)
  648. }
  649. }
  650. for _, advice := range advices_two {
  651. now := scheduleDates.Unix()
  652. dayStr := strconv.FormatInt(advice.DayCount, 10)
  653. dayStr2 := "-" + dayStr
  654. count, _ := strconv.ParseInt(dayStr2, 10, 64)
  655. oldTime := scheduleDates.AddDate(0, 0, int(count)).Unix()
  656. advices, _ := FindAllDoctorAdviceByTimeOther(now, oldTime, item.PatientId, item.UserOrgId, advice.TemplateId)
  657. if len(advices) == 0 {
  658. advice_three = append(advice_three, advice)
  659. }
  660. }
  661. item.DoctorAdvice = append(item.DoctorAdvice, advice_three...)
  662. }
  663. }
  664. return
  665. }
  666. func GetWeekDayScheduleById(orgid int64, scheduleDate int64, scheduleType int64, zone int64) (list []*models.VmSchedulesRemind, err error) {
  667. db := XTReadDB().Table("xt_schedule as x").Where("x.status = 1")
  668. if orgid > 0 {
  669. db = db.Where("x.user_org_id = ?", orgid)
  670. }
  671. if scheduleDate > 0 {
  672. db = db.Where("x.schedule_date = ?", scheduleDate)
  673. }
  674. if scheduleType > 0 {
  675. db = db.Where("x.schedule_type = ?", scheduleType)
  676. }
  677. if zone > 0 {
  678. db = db.Where("x.partition_id = ?", zone)
  679. }
  680. 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 {
  681. return db.Where("status =1 and parent_id = 0").Preload("ChildDoctorAdvice", "status = 1")
  682. }).Preload("HisDoctorAdviceInfo", "status = 1").Preload("DialysisPrescription", "status = 1").Preload("DialysisSolution", func(db *gorm.DB) *gorm.DB {
  683. return db.Where("status =1").Order("id asc")
  684. }).Find(&list).Error
  685. return list, err
  686. }
  687. func GetWeekDayScheduleByIdThee(orgid int64, scheduleDate int64, scheduleType int64, zone []string) (list []*models.VmSchedulesRemind, err error) {
  688. db := XTReadDB().Table("xt_schedule as x").Where("x.status = 1")
  689. if orgid > 0 {
  690. db = db.Where("x.user_org_id = ?", orgid)
  691. }
  692. if scheduleDate > 0 {
  693. db = db.Where("x.schedule_date = ?", scheduleDate)
  694. }
  695. if scheduleType > 0 {
  696. db = db.Where("x.schedule_type = ?", scheduleType)
  697. }
  698. if len(zone) > 0 {
  699. db = db.Where("x.partition_id in (?)", zone)
  700. }
  701. 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 and dialysis_date = ?", scheduleDate).Preload("XtAssessmentBeforeDislysis", "status = 1 and assessment_date =?", scheduleDate).Preload("DoctorAdvice", func(db *gorm.DB) *gorm.DB {
  702. return db.Where("status =1 and parent_id = 0 and advice_date = ?", scheduleDate).Preload("ChildDoctorAdvice", "status = 1")
  703. }).Preload("HisDoctorAdviceInfo", "status = 1 and advice_date = ? and user_org_id = ? ", scheduleDate, orgid).Preload("DialysisPrescription", "status = 1 and record_date =? and user_org_id = ?", scheduleDate, orgid).Preload("DialysisSolution", func(db *gorm.DB) *gorm.DB {
  704. return db.Where("status =1").Order("id asc")
  705. }).Preload("LastAfterWeight", func(db *gorm.DB) *gorm.DB {
  706. return db.Where("user_org_id = ? and status = 1 and assessment_date < ?", orgid, scheduleDate)
  707. }).Find(&list).Error
  708. return list, err
  709. }
  710. func GetWeekDayScheduleByIdTwo(orgid int64, scheduleDate int64, scheduleType int64, startDate int64, zone []string) (list []*models.VmSchedulesRemind, err error) {
  711. db := XTReadDB().Table("xt_schedule as x").Where("x.status = 1")
  712. if orgid > 0 {
  713. db = db.Where("x.user_org_id = ?", orgid)
  714. }
  715. if scheduleDate > 0 {
  716. db = db.Where("x.schedule_date >=? and x.schedule_date<=?", startDate, scheduleDate)
  717. }
  718. if scheduleType > 0 {
  719. db = db.Where("x.schedule_type = ?", scheduleType)
  720. }
  721. if len(zone) > 0 {
  722. db = db.Where("x.partition_id in (?)", zone)
  723. }
  724. 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,x.mode_id").Preload("XtPatients", "status = 1").Preload("DeviceZone", "status = 1").Preload("DeviceNumber", "status = 1").Preload("DialysisOrder", "status = 1 and dialysis_date =?", scheduleDate).Preload("XtAssessmentBeforeDislysis", "status = 1 and assessment_date =?", scheduleDate).Preload("DoctorAdvice", func(db *gorm.DB) *gorm.DB {
  725. return db.Where("status =1 and parent_id = 0 and advice_date = ?", scheduleDate).Preload("ChildDoctorAdvice", "status = 1")
  726. }).Preload("HisDoctorAdviceInfo", "status = 1 and advice_date = ?", scheduleDate).Preload("DialysisPrescription", "status = 1 and record_date = ?", scheduleDate).Preload("DialysisSolution", func(db *gorm.DB) *gorm.DB {
  727. return db.Where("status =1").Order("id asc")
  728. }).Preload("LastAfterWeight", func(db *gorm.DB) *gorm.DB {
  729. return db.Where("user_org_id = ? and status = 1 and assessment_date < ?", orgid, scheduleDate)
  730. }).Find(&list).Error
  731. return list, err
  732. }
  733. func RemoveRepeatedElement(arr []*models.DoctorAdvice) (newArr []*models.DoctorAdvice) {
  734. newArr = make([]*models.DoctorAdvice, 0)
  735. for i := 0; i < len(arr); i++ {
  736. repeat := false
  737. for j := i + 1; j < len(arr); j++ {
  738. if arr[i].ID == arr[j].ID {
  739. repeat = true
  740. break
  741. }
  742. }
  743. if !repeat {
  744. newArr = append(newArr, arr[i])
  745. }
  746. }
  747. return
  748. }
  749. func GetLastLongAdviceByGroupNoOther(orgId int64, patient_id int64) (advice []*models.DoctorAdvice, err error) {
  750. 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
  751. return
  752. }
  753. func GetLastLongAdviceByGroupNoThreeOther(orgId int64, patient_id int64) (advice []*models.DoctorAdvice, err error) {
  754. 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
  755. return
  756. }
  757. func FindAllDoctorAdviceByTimeOther(now int64, end int64, patient_id int64, orgId int64, temp_id string) (advice []*models.DoctorAdvice, err error) {
  758. 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
  759. return
  760. }
  761. func FindSchPatientNameByName(name string, user_org_id int64) (patient []*models.Patients, err error) {
  762. err = readDb.Model(&models.Patients{}).Where("user_org_id = ? AND name = ? AND status = 1", user_org_id, name).Find(&patient).Error
  763. return
  764. }
  765. func UpdateScheduleStatus(date int64, org_id int64) (err error) {
  766. 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
  767. return
  768. }
  769. func FindDeviceZoneByName(name string, org_id int64) (zone models.DeviceZone, err error) {
  770. err = readDb.Model(&models.DeviceZone{}).Where("org_id = ? AND name = ? AND status = 1", org_id, name).First(&zone).Error
  771. return
  772. }
  773. func FindDeviceByName(device_name string, org_id int64, zone_id int64) (devices []*models.DeviceNumber, err error) {
  774. 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
  775. return
  776. }
  777. func FindSchedualExportLog(org_id int64, export_time int64) (errLogs []*models.ExportErrLog, err error) {
  778. err = readDb.Model(&models.ExportErrLog{}).Where("user_org_id = ? AND export_time = ? AND log_type = 2", org_id, export_time).Find(&errLogs).Error
  779. return
  780. }
  781. func FindDeviceByNameTwo(device_name string, org_id int64, zone_id int64) (devices []*models.DeviceNumber, err error) {
  782. 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
  783. return
  784. }
  785. func UpdateScheduleTemplateStatus(org_id int64) (err error) {
  786. err = readDb.Model(&models.PatientScheduleTemplateItem{}).Where("org_id = ? AND status = 1", org_id).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  787. return
  788. }
  789. func CreateScheduleTemplate(m *models.PatientScheduleTemplateItem) error {
  790. return writeDb.Model(&models.PatientScheduleTemplateItem{}).Create(&m).Error
  791. }
  792. func ExportScheduleTemplate(m *models.PatientScheduleTemplateItem) (total int64) {
  793. readDb.Model(&models.PatientScheduleTemplateItem{}).Where("template_id = ? AND patient_id = ? AND week_day = ? AND org_id = ?", m.TemplateID, m.PatientID, m.Weekday, m.OrgID).Count(&total)
  794. return
  795. }
  796. func FindSchedualTemplateExportLog(org_id int64, export_time int64) (errLogs []*models.ExportErrLog, err error) {
  797. err = readDb.Model(&models.ExportErrLog{}).Where("user_org_id = ? AND export_time = ? AND log_type = 3", org_id, export_time).Find(&errLogs).Error
  798. return
  799. }
  800. func GetDialysisOrderByBedId(orgID, start, bed_id int64, schedule_type int64) (*models.DialysisOrder, error) {
  801. order := models.DialysisOrder{}
  802. 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
  803. if err == gorm.ErrRecordNotFound {
  804. return nil, err
  805. }
  806. if err != nil {
  807. return nil, err
  808. }
  809. return &order, nil
  810. }
  811. type VMDeviceNumber struct {
  812. ID int64 `gorm:"column:id" json:"id"`
  813. OrgID int64 `gorm:"column:org_id" json:"-"`
  814. Number string `gorm:"column:number" json:"number"`
  815. GroupID int64 `gorm:"column:group_id" json:"group_id"`
  816. ZoneID int64 `gorm:"column:zone_id" json:"zone_id"`
  817. Status int8 `json:"-"`
  818. CreateTime int64 `gorm:"column:ctime" json:"-"`
  819. ModifyTime int64 `gorm:"column:mtime" json:"-"`
  820. DeviceZone models.DeviceZone `json:"zone" gorm:"foreignkey:ID;AssociationForeignKey:ZoneID;"`
  821. }
  822. func (VMDeviceNumber) TableName() string {
  823. return "xt_device_number"
  824. }
  825. type VMPatientScheduleTemplateItem struct {
  826. ID int64 `gorm:"column:id" json:"id" form:"id"`
  827. OrgID int64 `gorm:"column:org_id" json:"-" form:"org_id"`
  828. TemplateID int64 `gorm:"column:template_id" json:"template_id" form:"template_id"`
  829. DeviceNumberID int64 `gorm:"column:device_number_id" json:"device_number_id" form:"device_number_id"`
  830. PatientID int64 `gorm:"column:patient_id" json:"patient_id" form:"patient_id"`
  831. TreatMode int64 `gorm:"column:treat_mode" json:"treat_mode" form:"treat_mode"` // 治疗模式
  832. Weekday int8 `gorm:"column :weekday" json:"weekday" form:"weekday"` // 周一-周日 1-7
  833. TimeType int8 `gorm:"column:time_type" json:"time_type" form:"time_type"` // 上下晚 123
  834. Status int8 `gorm:"column:status" json:"status" form:"status"`
  835. CreateTime int64 `gorm:"column:ctime" json:"-" form:"ctime"`
  836. ModifyTime int64 `gorm:"column:mtime" json:"-" form:"mtime"`
  837. IsExport int64 `gorm:"column:is_export" json:"is_export" form:"is_export"`
  838. Patient string `gorm:"-" json:"patient" form:"patient"`
  839. DeviceNumber VMDeviceNumber `json:"number" gorm:"foreignkey:ID;AssociationForeignKey:DeviceNumberID;"`
  840. PatientInfectiousDiseases []*models.InfectiousDiseases `json:"patient_contagions" gorm:"foreignkey:PatientId;AssociationForeignKey:PatientId;"`
  841. }
  842. func (VMPatientScheduleTemplateItem) TableName() string {
  843. return "xt_patient_schedule_template_item"
  844. }
  845. func GetTemplateSchedualPatientsByKeywords(keywords string, org_id int64, template_id int64) (schedule []*VMPatientScheduleTemplateItem, err error) {
  846. likeKey := "%" + keywords + "%"
  847. err = readDb.
  848. Table("xt_patient_schedule_template_item as s").
  849. Preload("PatientInfectiousDiseases", "status = 1 ").
  850. Preload("DeviceNumber", func(db *gorm.DB) *gorm.DB {
  851. return db.Where("org_id = ? AND status = 1", org_id).Preload("DeviceZone", "org_id = ? AND status = 1", org_id)
  852. }).
  853. Joins("JOIN xt_patients as p ON p.id = s.patient_id AND (p.name LIKE ? OR p.dialysis_no LIKE ?)", likeKey, likeKey).
  854. Where("s.template_id = ? AND s.org_id=? and s.status=1", template_id, org_id).
  855. 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").
  856. Order("weekday asc").
  857. Find(&schedule).Error
  858. return
  859. }
  860. func UpdateNewScheduleTemplateStatus(id int64, org_id int64) (err error) {
  861. err = readDb.Model(&models.PatientScheduleTemplateItem{}).Where("org_id = ? AND id = ?", org_id, id).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  862. return
  863. }
  864. func GetNextWeekDaySchedule(weektype int64, weektime int64, startime int64, endtime int64, orgID int64, zone []string, record_date int64) (schedules []*models.WeekScheduleSix, err error) {
  865. db := XTReadDB().Table("xt_schedule as s ").Where("s.status =1")
  866. if orgID > 0 {
  867. db = db.Where("s.user_org_id = ?", orgID)
  868. }
  869. if weektype == 0 {
  870. if startime > 0 {
  871. db = db.Where("s.schedule_date >= ?", startime)
  872. }
  873. if endtime > 0 {
  874. db = db.Where("s.schedule_date <= ?", endtime)
  875. }
  876. }
  877. if weektype > 0 {
  878. if record_date > 0 {
  879. db = db.Where("s.schedule_date = ?", record_date)
  880. }
  881. }
  882. if weektime > 0 {
  883. db = db.Where("s.schedule_type = ?", weektime)
  884. }
  885. if weektype > 0 {
  886. db = db.Where("s.schedule_week = ?", weektype)
  887. }
  888. if len(zone) > 0 {
  889. db = db.Where("s.partition_id in (?)", zone)
  890. }
  891. err = db.
  892. Preload("DeviceZone", "status = 1 ").
  893. Preload("DeviceNumber", "status = 1 ").Joins("JOIN xt_patients as p ON p.id = s.patient_id").
  894. 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").
  895. Order("s.partition_id desc").
  896. Find(&schedules).Error
  897. return schedules, err
  898. }
  899. func GetThreeWeekList(startime int64, endtime int64, orgid int64, scheduletype int64, partionid int64) (schedule []*models.VmSchedule, err error) {
  900. db := XTReadDB().Table("xt_schedule as x").Where("x.status =1")
  901. if startime > 0 {
  902. db = db.Where("x.schedule_date >=?", startime)
  903. }
  904. if endtime > 0 {
  905. db = db.Where("x.schedule_date <=?", endtime)
  906. }
  907. if orgid > 0 {
  908. db = db.Where("x.user_org_id = ?", orgid)
  909. }
  910. if scheduletype > 0 {
  911. db = db.Where("x.schedule_type = ?", scheduletype)
  912. }
  913. if partionid > 0 {
  914. db = db.Where("x.partition_id = ?", partionid)
  915. }
  916. err = db.Select("x.schedule_date,x.schedule_type,x.schedule_week,x.bed_id,x.partition_id,t.name,n.number,z.name").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
  917. return schedule, err
  918. }
  919. func UpdatedDialysisPrescription(prescription *models.DialysisPrescription, recordDate int64, patientid int64, orgid int64) error {
  920. 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
  921. return err
  922. }
  923. func GetWeekSchedules(orgid int64, startime int64, endtime int64) (scheudle []*models.XtSchedule, err error) {
  924. err = XTReadDB().Model(&scheudle).Where("user_org_id = ? and status = 1 and schedule_date>=? and schedule_date<=?", orgid, startime, endtime).Find(&scheudle).Error
  925. return scheudle, err
  926. }
  927. func GetLastWeekSchedule(orgid int64, startime int64, endtime int64) (*models.XtSchedule, error) {
  928. schedule := models.XtSchedule{}
  929. err := XTReadDB().Where("user_org_id = ? and status =1 and schedule_date>=? and schedule_date<=?", orgid, startime, endtime).Find(&schedule).Error
  930. if err == gorm.ErrRecordNotFound {
  931. return nil, err
  932. }
  933. if err != nil {
  934. return nil, err
  935. }
  936. return &schedule, nil
  937. }
  938. func AddPatientSchedule(schedule *models.XtSchedule) error {
  939. utx := XTWriteDB().Begin()
  940. err := utx.Create(&schedule).Error
  941. utx.Commit()
  942. return err
  943. }
  944. func UpdatePatientSchedule(orgid int64, startime int64, endtime int64) error {
  945. schedule := models.XtSchedule{}
  946. 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
  947. return err
  948. }
  949. func GetSettingById(id int64) (*models.XtRemindPrintSetting, error) {
  950. setting := models.XtRemindPrintSetting{}
  951. err := XTReadDB().Model(&setting).Where("id = ? and status = 1", id).Find(&setting).Error
  952. if err == gorm.ErrRecordNotFound {
  953. return nil, err
  954. }
  955. if err != nil {
  956. return nil, err
  957. }
  958. return &setting, nil
  959. }
  960. func CreateSetting(setting *models.XtRemindPrintSetting) error {
  961. err := XTReadDB().Create(&setting).Error
  962. return err
  963. }
  964. func UpdatedRemindPrint(set *models.XtRemindPrintSetting, id int64) error {
  965. 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
  966. return err
  967. }
  968. func GetRemindPrintList(id int64) (models.XtRemindPrintSetting, error) {
  969. setting := models.XtRemindPrintSetting{}
  970. err := XTReadDB().Model(&setting).Where("user_org_id = ? and status =1", id).Find(&setting).Error
  971. return setting, err
  972. }
  973. func GetScheudleTemplateById(orgid int64) (*models.ScheduleTemplate, error) {
  974. schedule := models.ScheduleTemplate{}
  975. err := XTReadDB().Model(&schedule).Where("user_org_id = ? and status = 1", orgid).Find(&schedule).Error
  976. if err == gorm.ErrRecordNotFound {
  977. return nil, err
  978. }
  979. if err != nil {
  980. return nil, err
  981. }
  982. return &schedule, nil
  983. }
  984. func CreateXtscheduleTemplate(template *models.ScheduleTemplate) error {
  985. err := XTWriteDB().Create(&template).Error
  986. return err
  987. }
  988. func UpdateXtScheduleTemplate(template *models.ScheduleTemplate, orgid int64) error {
  989. err := XTWriteDB().Model(&template).Where("user_org_id = ? and status = 1", orgid).Updates(map[string]interface{}{"template_id": template.TemplateId}).Error
  990. return err
  991. }
  992. func GetScheduleTemplate(orgid int64) (models.ScheduleTemplate, error) {
  993. template := models.ScheduleTemplate{}
  994. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&template).Error
  995. return template, err
  996. }
  997. func GetDataPrintByOrgId(orgid int64) (*models.XtDataPrint, error) {
  998. dataPrint := models.XtDataPrint{}
  999. err := XTReadDB().Model(&dataPrint).Where("user_org_id = ? and status = 1", orgid).Find(&dataPrint).Error
  1000. if err == gorm.ErrRecordNotFound {
  1001. return nil, err
  1002. }
  1003. if err != nil {
  1004. return nil, err
  1005. }
  1006. return &dataPrint, nil
  1007. }
  1008. func CreateDataPrint(dataPrint *models.XtDataPrint) error {
  1009. err := XTWriteDB().Create(&dataPrint).Error
  1010. return err
  1011. }
  1012. func UpdteDataPrint(orgid int64, dataPrint *models.XtDataPrint) error {
  1013. err := XTWriteDB().Model(&dataPrint).Where("user_org_id = ? and status = 1", orgid).Updates(map[string]interface{}{"is_open": dataPrint.IsOpen}).Error
  1014. return err
  1015. }
  1016. func GetDataPrint(orgid int64) (models.XtDataPrint, error) {
  1017. dataPrint := models.XtDataPrint{}
  1018. err := XTReadDB().Model(&dataPrint).Where("user_org_id = ? and status = 1", orgid).Find(&dataPrint).Error
  1019. return dataPrint, err
  1020. }
  1021. func GetNextWeekPanels(startime int64, endtime int64, orgid int64) (schedule []*models.XtSchedule, err error) {
  1022. 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 {
  1023. return db.Where("user_org_id = ? and status = 1 and assessment_date>=1672502400 and assessment_date < ?", orgid, endtime)
  1024. }).Find(&schedule).Error
  1025. return schedule, err
  1026. }
  1027. func GetTemplateListByTemplateId(id int64, orgid int64) (list []*models.VmPatientScheduleTemplateItem, err error) {
  1028. err = XTReadDB().Where("template_id =? and org_id = ? and status = 1", id, orgid).Find(&list).Error
  1029. return list, err
  1030. }
  1031. func DeletePatientSchedule(startime int64, endtime int64, org_id int64) (models.XtSchedule, error) {
  1032. schedule := models.XtSchedule{}
  1033. 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
  1034. return schedule, err
  1035. }
  1036. func DeletePatientScheduleToday(startime int64, endtime int64, org_id int64) (models.XtSchedule, error) {
  1037. schedule := models.XtSchedule{}
  1038. 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
  1039. return schedule, err
  1040. }
  1041. func GetBedNumberById(id int64, org_id int64) (models.DeviceNumber, error) {
  1042. number := models.DeviceNumber{}
  1043. err := XTReadDB().Where("id = ? and status = 1 and org_id =?", id, org_id).Find(&number).Error
  1044. return number, err
  1045. }
  1046. func CreatePatientSchedule(schedule *models.XtSchedule) error {
  1047. utx := XTWriteDB().Begin()
  1048. err := utx.Create(&schedule).Error
  1049. utx.Commit()
  1050. return err
  1051. }
  1052. func GetPatientScheduleTempalate(orgid int64) (models.PatientScheduleTemplateMode, error) {
  1053. mode := models.PatientScheduleTemplateMode{}
  1054. err := XTReadDB().Where("org_id = ? and status =1", orgid).Find(&mode).Error
  1055. return mode, err
  1056. }
  1057. func GetSolutionSchedule(orgid int64) (solution []*models.VmDialysisSolution, err error) {
  1058. 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 {
  1059. return db.Where("user_org_id = ? and status = 1", orgid).Order("id asc")
  1060. }).Find(&solution).Error
  1061. return solution, err
  1062. }
  1063. func GetSolutionScheduleBySchedule(orgid int64, zonid int64, classtype int64, schedule_date int64) (schedule []*models.XtScheduleList, err error) {
  1064. db := XTReadDB().Model(&schedule).Where("status = 1")
  1065. if orgid > 0 {
  1066. db = db.Where("user_org_id =?", orgid)
  1067. }
  1068. if zonid > 0 {
  1069. db = db.Where("partition_id = ?", zonid)
  1070. }
  1071. if classtype > 0 {
  1072. db = db.Where("schedule_type = ?", classtype)
  1073. }
  1074. if schedule_date > 0 {
  1075. db = db.Where("schedule_date = ?", schedule_date)
  1076. }
  1077. err = db.Preload("DialysisSolution", func(db *gorm.DB) *gorm.DB {
  1078. 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 {
  1079. return db.Where("user_org_id = ? and status = 1", orgid).Order("id asc")
  1080. })
  1081. }).Find(&schedule).Error
  1082. return schedule, err
  1083. }
  1084. func GetDeathPatient(orgid int64) (patient []*models.XtPatients, err error) {
  1085. err = XTReadDB().Where("user_org_id = ? and (lapseto = 2 || lapseto = 3) and status = 1", orgid).Find(&patient).Error
  1086. return patient, err
  1087. }
  1088. func GetInitPrintData(patient_id int64, scheduleDate int64, orgid int64) (list []*models.VmSchedulesRemind, err error) {
  1089. db := XTReadDB().Table("xt_schedule as x").Where("x.status = 1")
  1090. if orgid > 0 {
  1091. db = db.Where("x.user_org_id = ?", orgid)
  1092. }
  1093. if patient_id > 0 {
  1094. db = db.Where("x.patient_id = ?", patient_id)
  1095. }
  1096. if scheduleDate > 0 {
  1097. db = db.Where("x.schedule_date >=? and x.schedule_date<=?", scheduleDate, scheduleDate)
  1098. }
  1099. 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 {
  1100. return db.Where("status =1 and patient_id = ?", patient_id).Order("id asc")
  1101. }).Find(&list).Error
  1102. return list, err
  1103. }
  1104. func GetLastWeightAfter(patient_id int64, scheduleDate int64, orgid int64) (models.AssessmentAfterDislysis, error) {
  1105. dislysis := models.AssessmentAfterDislysis{}
  1106. 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
  1107. return dislysis, err
  1108. }
  1109. func GetScheduleTwo(orgID, id int64) (models.Schedule, error) {
  1110. var schedule models.Schedule
  1111. var err error
  1112. err = readDb.Model(&models.Schedule{}).Where("id = ? and user_org_id=? and status=1", id, orgID).First(&schedule).Error
  1113. return schedule, err
  1114. }
  1115. func UpdateSchDName(sch models.Schedule) (err error) {
  1116. err = writeDb.Model(&models.Schedule{}).Where("status=1 AND id = ?", sch.ID).Updates(map[string]interface{}{"dialysis_machine_name": sch.DialysisMachineName}).Error
  1117. return
  1118. }
  1119. func GetDialysisOrderByBedIdTwo(orgID, start, bed_id int64, schedule_type int64) (models.DialysisOrder, error) {
  1120. order := models.DialysisOrder{}
  1121. 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
  1122. return order, nil
  1123. }
  1124. func ChangeScheduleClass(schedule models.Schedule) error {
  1125. 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
  1126. return err
  1127. }
  1128. func GetLastDialysisOrder(org_id int64, patient_id int64, dialysis_date int64) (models.XtDialysisOrderSix, error) {
  1129. order := models.XtDialysisOrderSix{}
  1130. 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
  1131. return order, err
  1132. }
  1133. func GetLastDialysisSolution(org_id int64, patient_id int64, mode_id int64, schedule_date int64) (models.NewDialysisPrescription, error) {
  1134. dialysisprescription := models.NewDialysisPrescription{}
  1135. err := XTReadDB().Where("user_org_id = ? and patient_id = ? and mode_id = ? and status = 1 and record_date>=1672502400 and record_date<?", org_id, patient_id, mode_id, schedule_date).Order("id desc").First(&dialysisprescription).Error
  1136. return dialysisprescription, err
  1137. }
  1138. func GetDoctorAdviceLastWeek(org_id int64, patient_id int64, schedule_date int64) (advice []*models.DoctorAdvice, err error) {
  1139. 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
  1140. return advice, err
  1141. }
  1142. func GetHisDoctorAdviceListLastWeek(org_id int64, patient_id int64, schedule_date int64) (advice []*models.HisDoctorAdviceInfo, err error) {
  1143. err = XTReadDB().Where("user_org_id = ? and patient_id = ? and advice_date = ? and status= 1", org_id, patient_id, schedule_date).Find(&advice).Error
  1144. return advice, err
  1145. }
  1146. func GetDialysisSolutionLastWeek(user_org_id int64, patient_id int64, mode_id int64) (models.DialysisSolution, error) {
  1147. solution := models.DialysisSolution{}
  1148. 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
  1149. return solution, err
  1150. }
  1151. func GetNextWeekDayScheduleSix(weektype int64, weektime int64, startime int64, endtime int64, orgID int64, zone []string, record_date int64) (schedules []*models.WeekScheduleSeven, err error) {
  1152. db := XTReadDB().Table("xt_schedule as s ").Where("s.status =1")
  1153. if orgID > 0 {
  1154. db = db.Where("s.user_org_id = ?", orgID)
  1155. }
  1156. if weektype == 0 {
  1157. if startime > 0 {
  1158. db = db.Where("s.schedule_date >= ?", startime)
  1159. }
  1160. if endtime > 0 {
  1161. db = db.Where("s.schedule_date <= ?", endtime)
  1162. }
  1163. }
  1164. if weektype > 0 {
  1165. if record_date > 0 {
  1166. db = db.Where("s.schedule_date = ?", record_date)
  1167. }
  1168. }
  1169. if weektime > 0 {
  1170. db = db.Where("s.schedule_type = ?", weektime)
  1171. }
  1172. if weektype > 0 {
  1173. db = db.Where("s.schedule_week = ?", weektype)
  1174. }
  1175. if len(zone) > 0 {
  1176. db = db.Where("s.partition_id in (?)", zone)
  1177. }
  1178. err = db.
  1179. Preload("DeviceZone", "status = 1 ").
  1180. Preload("DeviceNumber", "status = 1 ").
  1181. Preload("XtAssessmentBeforeDislysis", func(db *gorm.DB) *gorm.DB {
  1182. return db.Where("status = 1 and assessment_date = ?", record_date)
  1183. }).Joins("JOIN xt_patients as p ON p.id = s.patient_id").
  1184. 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").
  1185. Order("s.partition_id desc").
  1186. Find(&schedules).Error
  1187. return schedules, err
  1188. }
  1189. func GetAssessmentBeforListLastWeek(schedule_date int64, patient_id int64, org_id int64) (models.XtAssessmentBeforeDislysis, error) {
  1190. dislysis := models.XtAssessmentBeforeDislysis{}
  1191. err := XTReadDB().Where("assessment_date = ? and patient_id = ? and user_org_id =? and status = 1", schedule_date, patient_id, org_id).Find(&dislysis).Error
  1192. return dislysis, err
  1193. }
  1194. func GetLastDialysisSolutionType(org_id int64, patient_id int64, mode_id int64) (models.NewDialysisSolutionOne, error) {
  1195. dialysisprescription := models.NewDialysisSolutionOne{}
  1196. 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
  1197. return dialysisprescription, err
  1198. }
  1199. func GetScheduleTen(orgID, id int64) (models.Schedule, error) {
  1200. var schedule models.Schedule
  1201. var err error
  1202. err = readDb.Model(&models.Schedule{}).Where("id = ? and user_org_id=? and status=1", id, orgID).First(&schedule).Error
  1203. return schedule, err
  1204. }
  1205. func GetDialysisOrderByPatientAndBed(org_id int64, record_date int64, bed_id int64, patient_id int64) (order models.DialysisOrder, err error) {
  1206. 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
  1207. return
  1208. }