schedule_service.go 54KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312
  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. utx.Save(&schone)
  293. utx.Save(&schtwo)
  294. }
  295. func GetPatientScheduleFormToday(orgId, id, start int64) (schedules []*models.PatientSchedule, err error) {
  296. err = readDb.Table("xt_schedule as s").
  297. Preload("DeviceZone", "org_id=? and status=1", orgId).
  298. Preload("DeviceNumber", "org_id=? and status=1", orgId).
  299. Where("s.patient_id =? and s.user_org_id=? and s.schedule_date>=? and s.status=1", id, orgId, start).
  300. Find(&schedules).Error
  301. return
  302. }
  303. type WeekSchedulePrintVM struct {
  304. ID int64 `gorm:"column:id" json:"id"`
  305. ZoneID int64 `gorm:"column:partition_id" json:"zone_id"`
  306. DeviceNumberID int64 `gorm:"column:bed_id" json:"device_number_id"`
  307. PatientID int64 `gorm:"column:patient_id" json:"patient_id"`
  308. ScheduleDate int64 `gorm:"column:schedule_date" json:"schedule_date"`
  309. ScheduleType int64 `gorm:"column:schedule_type" json:"schedule_type"`
  310. ScheduleWeek int64 `gorm:"column:schedule_week" json:"schedule_week"`
  311. ModeID int64 `gorm:"column:mode_id" json:"mode_id"`
  312. Patient *models.Patients `gorm:"ForeignKey:PatientID" json:"patient"`
  313. DeviceNumber *models.DeviceNumber `gorm:"ForeignKey:DeviceNumberID" json:"device_number"`
  314. Zone *models.DeviceZone `gorm:"ForeignKey:ZoneID" json:"zone"`
  315. Mode *models.TreatmentMode `gorm:"ForeignKey:ModeID" json:"mode"`
  316. }
  317. type PrintCount struct {
  318. ScheduleType int64 `gorm:"column:schedule_type" json:"schedule_type"`
  319. ScheduleWeek int64 `gorm:"column:schedule_week" json:"schedule_week"`
  320. Count int64
  321. }
  322. func GetPrinitWeekSchedules(orgID int64, start, end int64) ([]*WeekSchedulePrintVM, error) {
  323. var schedules []*WeekSchedulePrintVM
  324. err := readDb.Table("xt_schedule as s").
  325. Preload("Patient", "status = 1 and user_org_id = ?", orgID).
  326. Preload("DeviceNumber", "status = 1 and org_id = ?", orgID).
  327. Preload("Zone", "status = 1 and org_id = ?", orgID).
  328. Preload("Mode", "status = 1").
  329. // Joins("JOIN xt_patients as p ON p.id = s.patient_id").
  330. Where("s.user_org_id = ? and s.schedule_date >= ? and s.schedule_date <= ? and s.status = 1", orgID, start, end).
  331. // 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").
  332. Find(&schedules).Error
  333. return schedules, err
  334. }
  335. func GetPrinitWeekTotal(orgid int64, startime int64, endtime int64) (print []*PrintCount, err error) {
  336. db := XTReadDB().Table("xt_schedule as x").Where("x.status = 1")
  337. if orgid > 0 {
  338. db = db.Where("x.user_org_id = ?", orgid)
  339. }
  340. if startime > 0 {
  341. db = db.Where("x.schedule_date >=?", startime)
  342. }
  343. if endtime > 0 {
  344. db = db.Where("x.schedule_date <=?", endtime)
  345. }
  346. 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
  347. return print, err
  348. }
  349. func GetBloodPrintWeekTotal(orgid int64, startime int64, endtime int64) (print []*PrintCount, err error) {
  350. db := XTReadDB().Table("xt_schedule as x").Where("x.status = 1")
  351. if orgid > 0 {
  352. db = db.Where("x.user_org_id = ?", orgid)
  353. }
  354. if startime > 0 {
  355. db = db.Where("x.schedule_date >=?", startime)
  356. }
  357. if endtime > 0 {
  358. db = db.Where("x.schedule_date <=?", endtime)
  359. }
  360. db = db.Where("x.mode_id = 2")
  361. 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
  362. return print, err
  363. }
  364. func GetPerfusionWeekTotal(orgid int64, startime int64, endtime int64) (print []*PrintCount, err error) {
  365. db := XTReadDB().Table("xt_schedule as x").Where("x.status = 1")
  366. if orgid > 0 {
  367. db = db.Where("x.user_org_id = ?", orgid)
  368. }
  369. if startime > 0 {
  370. db = db.Where("x.schedule_date >=?", startime)
  371. }
  372. if endtime > 0 {
  373. db = db.Where("x.schedule_date <=?", endtime)
  374. }
  375. db = db.Where("x.mode_id = 3")
  376. 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
  377. return print, err
  378. }
  379. // func ExchangeScheduleTimeWithWeekSchedules(orgID int64, nextWeekSchs []*models.Schedule, nextTwoWeekSchs []*models.Schedule) error {
  380. // templateMode, getTemplateModeErr := GetOrgPatientScheduleTemplateMode(orgID)
  381. // if getTemplateModeErr != nil {
  382. // return getTemplateModeErr
  383. // }
  384. // if templateMode == nil {
  385. // return errors.New("模板模式不存在")
  386. // }
  387. // tx := writeDb.Begin()
  388. // updateTime := time.Now().Unix()
  389. // for _, sch := range nextWeekSchs {
  390. // sch.PatientInfectiousDiseases = nil
  391. // date := time.Unix(sch.ScheduleDate, 0)
  392. // sch.ScheduleDate = date.AddDate(0, 0, 7).Unix()
  393. // sch.UpdatedTime = updateTime
  394. // updateSchErr := tx.Save(sch).Error
  395. // if updateSchErr != nil {
  396. // tx.Rollback()
  397. // return updateSchErr
  398. // }
  399. // }
  400. // for _, sch := range nextTwoWeekSchs {
  401. // sch.PatientInfectiousDiseases = nil
  402. // date := time.Unix(sch.ScheduleDate, 0)
  403. // sch.ScheduleDate = date.AddDate(0, 0, -7).Unix()
  404. // sch.UpdatedTime = updateTime
  405. // updateSchErr := tx.Save(sch).Error
  406. // if updateSchErr != nil {
  407. // tx.Rollback()
  408. // return updateSchErr
  409. // }
  410. // }
  411. // templateMode.ModifyTime = updateTime
  412. // templateMode.ExecuteTimes += 1
  413. // updateModeErr := tx.Save(templateMode).Error
  414. // if updateModeErr != nil {
  415. // tx.Rollback()
  416. // return updateModeErr
  417. // }
  418. // tx.Commit()
  419. // return nil
  420. // }
  421. func GetSchedualPatientsByKeywordsAndWeek(keywords string, org_id int64, start_time int64, end_time int64) (schedule []*models.Schedule, err error) {
  422. likeKey := "%" + keywords + "%"
  423. err = readDb.
  424. Table("xt_schedule as s").
  425. Preload("PatientInfectiousDiseases", "status = 1 ").
  426. Preload("DeviceZone", "status = 1 ").
  427. Preload("DeviceNumber", "status = 1 ").
  428. Preload("TreatmentMode", "status = 1 ").
  429. Joins("JOIN xt_patients as p ON p.id = s.patient_id AND (p.name LIKE ? OR p.dialysis_no LIKE ?)", likeKey, likeKey).
  430. Where("s.user_org_id=? and s.schedule_date >= ? and s.schedule_date <= ? and s.status=1", org_id, start_time, end_time).
  431. 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").
  432. Order("schedule_date asc").
  433. Find(&schedule).Error
  434. return
  435. }
  436. func GetSchedualPatientsByKeywords(keywords string, org_id int64, startime int64, endtime int64) (schedule []*models.Schedule, err error) {
  437. likeKey := "%" + keywords + "%"
  438. err = readDb.
  439. Table("xt_schedule as s").
  440. Preload("PatientInfectiousDiseases", "status = 1 ").
  441. Preload("DeviceZone", "status = 1 ").
  442. Preload("DeviceNumber", "status = 1 ").
  443. Preload("TreatmentMode", "status = 1 ").
  444. Joins("JOIN xt_patients as p ON p.id = s.patient_id AND (p.name LIKE ? OR p.dialysis_no LIKE ?)", likeKey, likeKey).
  445. Where("s.user_org_id=? and s.schedule_date >= ? and s.schedule_date <= ? and s.status=1", org_id, startime, endtime).
  446. 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").
  447. Order("schedule_date asc").
  448. Find(&schedule).Error
  449. return
  450. }
  451. func GetWeekDayScheduleTwo(orgID int64, scheduleDate int64, scheduleDates *time.Time, weektime int64) (schedules []*models.WeekSchedule, err error) {
  452. db := readDb.Table("xt_schedule as s ").Where("s.status =1")
  453. if orgID > 0 {
  454. db = db.Where("s.user_org_id = ?", orgID)
  455. }
  456. if scheduleDate > 0 {
  457. db = db.Where("s.schedule_date = ?", scheduleDate)
  458. }
  459. if weektime > 0 {
  460. db = db.Where("s.schedule_type = ?", weektime)
  461. }
  462. err = db.
  463. Preload("DeviceZone", "status = 1 ").
  464. Preload("DeviceNumber", "status = 1 ").
  465. Preload("DialysisPrescription", func(db *gorm.DB) *gorm.DB {
  466. return db.Where("status = 1").Order("id asc")
  467. }).
  468. Joins("JOIN xt_patients as p ON p.id = s.patient_id").
  469. 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").
  470. Order("s.partition_id asc").
  471. Find(&schedules).Error
  472. for _, item := range schedules {
  473. _, config := FindDoctorAdviceRecordByOrgId(item.UserOrgId)
  474. if config.IsOpenRemind > 0 {
  475. //开启推送提醒逻辑 提交长期处方的时候,弹起长期医嘱推送
  476. var advice_three []*models.DoctorAdvice
  477. advices, _ := GetLastLongAdviceByGroupNoOther(item.UserOrgId, item.PatientId)
  478. advices_two, _ := GetLastLongAdviceByGroupNoThreeOther(item.UserOrgId, item.PatientId)
  479. for _, advice := range advices {
  480. advice.IsCheck = 1
  481. if advice.FrequencyType == 3 {
  482. week := int(scheduleDates.Weekday())
  483. switch week {
  484. case 1:
  485. if strings.Index(advice.WeekDay, "周一") > -1 {
  486. advice_three = append(advice_three, advice)
  487. }
  488. break
  489. case 2:
  490. if strings.Index(advice.WeekDay, "周二") > -1 {
  491. advice_three = append(advice_three, advice)
  492. }
  493. break
  494. case 3:
  495. if strings.Index(advice.WeekDay, "周三") > -1 {
  496. advice_three = append(advice_three, advice)
  497. }
  498. break
  499. case 4:
  500. if strings.Index(advice.WeekDay, "周四") > -1 {
  501. advice_three = append(advice_three, advice)
  502. }
  503. break
  504. case 5:
  505. if strings.Index(advice.WeekDay, "周五") > -1 {
  506. advice_three = append(advice_three, advice)
  507. }
  508. break
  509. case 6:
  510. if strings.Index(advice.WeekDay, "周六") > -1 {
  511. advice_three = append(advice_three, advice)
  512. }
  513. break
  514. case 0:
  515. if strings.Index(advice.WeekDay, "周日") > -1 {
  516. advice_three = append(advice_three, advice)
  517. }
  518. break
  519. }
  520. } else if advice.FrequencyType == 1 {
  521. advice_three = append(advice_three, advice)
  522. }
  523. }
  524. for _, advice := range advices_two {
  525. now := scheduleDates.Unix()
  526. dayStr := strconv.FormatInt(advice.DayCount, 10)
  527. dayStr2 := "-" + dayStr
  528. count, _ := strconv.ParseInt(dayStr2, 10, 64)
  529. oldTime := scheduleDates.AddDate(0, 0, int(count)).Unix()
  530. advices, _ := FindAllDoctorAdviceByTimeOther(now, oldTime, item.PatientId, item.UserOrgId, advice.TemplateId)
  531. if len(advices) == 0 {
  532. advice_three = append(advice_three, advice)
  533. }
  534. }
  535. item.DoctorAdvice = append(item.DoctorAdvice, advice_three...)
  536. }
  537. }
  538. return
  539. }
  540. func GetWeekDaySchedule(orgID int64, scheduleDate int64, scheduleDates *time.Time) (schedules []*models.WeekSchedule, err error) {
  541. err = readDb.
  542. Table("xt_schedule as s").
  543. Preload("DeviceZone", "status = 1 ").
  544. Preload("DeviceNumber", "status = 1 ").
  545. Preload("DialysisPrescription", func(db *gorm.DB) *gorm.DB {
  546. return db.Where("status = 1").Order("id asc")
  547. }).
  548. Joins("JOIN xt_patients as p ON p.id = s.patient_id").
  549. Where("s.user_org_id=? and s.schedule_date = ? and s.status=1", orgID, scheduleDate).
  550. 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").
  551. Order("schedule_type").
  552. Find(&schedules).Error
  553. for _, item := range schedules {
  554. _, config := FindDoctorAdviceRecordByOrgId(item.UserOrgId)
  555. if config.IsOpenRemind > 0 {
  556. //开启推送提醒逻辑 提交长期处方的时候,弹起长期医嘱推送
  557. var advice_three []*models.DoctorAdvice
  558. advices, _ := GetLastLongAdviceByGroupNoOther(item.UserOrgId, item.PatientId)
  559. advices_two, _ := GetLastLongAdviceByGroupNoThreeOther(item.UserOrgId, item.PatientId)
  560. for _, advice := range advices {
  561. advice.IsCheck = 1
  562. if advice.FrequencyType == 3 {
  563. week := int(scheduleDates.Weekday())
  564. fmt.Println(advice.WeekDay)
  565. switch week {
  566. case 1:
  567. if strings.Index(advice.WeekDay, "周一") > -1 {
  568. advice_three = append(advice_three, advice)
  569. }
  570. break
  571. case 2:
  572. if strings.Index(advice.WeekDay, "周二") > -1 {
  573. advice_three = append(advice_three, advice)
  574. }
  575. break
  576. case 3:
  577. if strings.Index(advice.WeekDay, "周三") > -1 {
  578. advice_three = append(advice_three, advice)
  579. }
  580. break
  581. case 4:
  582. if strings.Index(advice.WeekDay, "周四") > -1 {
  583. advice_three = append(advice_three, advice)
  584. }
  585. break
  586. case 5:
  587. if strings.Index(advice.WeekDay, "周五") > -1 {
  588. advice_three = append(advice_three, advice)
  589. }
  590. break
  591. case 6:
  592. if strings.Index(advice.WeekDay, "周六") > -1 {
  593. advice_three = append(advice_three, advice)
  594. }
  595. break
  596. case 0:
  597. if strings.Index(advice.WeekDay, "周日") > -1 {
  598. advice_three = append(advice_three, advice)
  599. }
  600. break
  601. }
  602. } else if advice.FrequencyType == 1 {
  603. advice_three = append(advice_three, advice)
  604. }
  605. }
  606. for _, advice := range advices_two {
  607. now := scheduleDates.Unix()
  608. dayStr := strconv.FormatInt(advice.DayCount, 10)
  609. dayStr2 := "-" + dayStr
  610. count, _ := strconv.ParseInt(dayStr2, 10, 64)
  611. oldTime := scheduleDates.AddDate(0, 0, int(count)).Unix()
  612. advices, _ := FindAllDoctorAdviceByTimeOther(now, oldTime, item.PatientId, item.UserOrgId, advice.TemplateId)
  613. if len(advices) == 0 {
  614. advice_three = append(advice_three, advice)
  615. }
  616. }
  617. item.DoctorAdvice = append(item.DoctorAdvice, advice_three...)
  618. }
  619. }
  620. return
  621. }
  622. func GetWeekDayScheduleById(orgid int64, scheduleDate int64, scheduleType int64, zone int64) (list []*models.VmSchedulesRemind, err error) {
  623. db := XTReadDB().Table("xt_schedule as x").Where("x.status = 1")
  624. if orgid > 0 {
  625. db = db.Where("x.user_org_id = ?", orgid)
  626. }
  627. if scheduleDate > 0 {
  628. db = db.Where("x.schedule_date = ?", scheduleDate)
  629. }
  630. if scheduleType > 0 {
  631. db = db.Where("x.schedule_type = ?", scheduleType)
  632. }
  633. if zone > 0 {
  634. db = db.Where("x.partition_id = ?", zone)
  635. }
  636. 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 {
  637. return db.Where("status =1 and parent_id = 0").Preload("ChildDoctorAdvice", "status = 1")
  638. }).Preload("HisDoctorAdviceInfo", "status = 1").Preload("DialysisPrescription", "status = 1").Preload("DialysisSolution", func(db *gorm.DB) *gorm.DB {
  639. return db.Where("status =1").Order("id asc")
  640. }).Find(&list).Error
  641. return list, err
  642. }
  643. func GetWeekDayScheduleByIdThee(orgid int64, scheduleDate int64, scheduleType int64, zone []string) (list []*models.VmSchedulesRemind, err error) {
  644. db := XTReadDB().Table("xt_schedule as x").Where("x.status = 1")
  645. if orgid > 0 {
  646. db = db.Where("x.user_org_id = ?", orgid)
  647. }
  648. if scheduleDate > 0 {
  649. db = db.Where("x.schedule_date = ?", scheduleDate)
  650. }
  651. if scheduleType > 0 {
  652. db = db.Where("x.schedule_type = ?", scheduleType)
  653. }
  654. if len(zone) > 0 {
  655. db = db.Where("x.partition_id in (?)", zone)
  656. }
  657. 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 {
  658. return db.Where("status =1 and parent_id = 0 and advice_date = ?", scheduleDate).Preload("ChildDoctorAdvice", "status = 1")
  659. }).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 {
  660. return db.Where("status =1").Order("id asc")
  661. }).Preload("LastAfterWeight", func(db *gorm.DB) *gorm.DB {
  662. return db.Where("user_org_id = ? and status = 1 and assessment_date < ?", orgid, scheduleDate)
  663. }).Find(&list).Error
  664. return list, err
  665. }
  666. func GetWeekDayScheduleByIdTwo(orgid int64, scheduleDate int64, scheduleType int64, startDate int64, zone []string) (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 >=? and x.schedule_date<=?", startDate, scheduleDate)
  673. }
  674. if scheduleType > 0 {
  675. db = db.Where("x.schedule_type = ?", scheduleType)
  676. }
  677. if len(zone) > 0 {
  678. db = db.Where("x.partition_id in (?)", 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,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 {
  681. return db.Where("status =1 and parent_id = 0 and advice_date = ?", scheduleDate).Preload("ChildDoctorAdvice", "status = 1")
  682. }).Preload("HisDoctorAdviceInfo", "status = 1 and advice_date = ?", scheduleDate).Preload("DialysisPrescription", "status = 1 and record_date = ?", scheduleDate).Preload("DialysisSolution", func(db *gorm.DB) *gorm.DB {
  683. return db.Where("status =1").Order("id asc")
  684. }).Preload("LastAfterWeight", func(db *gorm.DB) *gorm.DB {
  685. return db.Where("user_org_id = ? and status = 1 and assessment_date < ?", orgid, scheduleDate)
  686. }).Find(&list).Error
  687. return list, err
  688. }
  689. func RemoveRepeatedElement(arr []*models.DoctorAdvice) (newArr []*models.DoctorAdvice) {
  690. newArr = make([]*models.DoctorAdvice, 0)
  691. for i := 0; i < len(arr); i++ {
  692. repeat := false
  693. for j := i + 1; j < len(arr); j++ {
  694. if arr[i].ID == arr[j].ID {
  695. repeat = true
  696. break
  697. }
  698. }
  699. if !repeat {
  700. newArr = append(newArr, arr[i])
  701. }
  702. }
  703. return
  704. }
  705. func GetLastLongAdviceByGroupNoOther(orgId int64, patient_id int64) (advice []*models.DoctorAdvice, err error) {
  706. 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
  707. return
  708. }
  709. func GetLastLongAdviceByGroupNoThreeOther(orgId int64, patient_id int64) (advice []*models.DoctorAdvice, err error) {
  710. 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
  711. return
  712. }
  713. func FindAllDoctorAdviceByTimeOther(now int64, end int64, patient_id int64, orgId int64, temp_id string) (advice []*models.DoctorAdvice, err error) {
  714. 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
  715. return
  716. }
  717. func FindSchPatientNameByName(name string, user_org_id int64) (patient []*models.Patients, err error) {
  718. err = readDb.Model(&models.Patients{}).Where("user_org_id = ? AND name = ? AND status = 1", user_org_id, name).Find(&patient).Error
  719. return
  720. }
  721. func UpdateScheduleStatus(date int64, org_id int64) (err error) {
  722. 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
  723. return
  724. }
  725. func FindDeviceZoneByName(name string, org_id int64) (zone models.DeviceZone, err error) {
  726. err = readDb.Model(&models.DeviceZone{}).Where("org_id = ? AND name = ? AND status = 1", org_id, name).First(&zone).Error
  727. return
  728. }
  729. func FindDeviceByName(device_name string, org_id int64, zone_id int64) (devices []*models.DeviceNumber, err error) {
  730. 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
  731. return
  732. }
  733. func FindSchedualExportLog(org_id int64, export_time int64) (errLogs []*models.ExportErrLog, err error) {
  734. err = readDb.Model(&models.ExportErrLog{}).Where("user_org_id = ? AND export_time = ? AND log_type = 2", org_id, export_time).Find(&errLogs).Error
  735. return
  736. }
  737. func FindDeviceByNameTwo(device_name string, org_id int64, zone_id int64) (devices []*models.DeviceNumber, err error) {
  738. 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
  739. return
  740. }
  741. func UpdateScheduleTemplateStatus(org_id int64) (err error) {
  742. err = readDb.Model(&models.PatientScheduleTemplateItem{}).Where("org_id = ? AND status = 1", org_id).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  743. return
  744. }
  745. func CreateScheduleTemplate(m *models.PatientScheduleTemplateItem) error {
  746. return writeDb.Model(&models.PatientScheduleTemplateItem{}).Create(&m).Error
  747. }
  748. func ExportScheduleTemplate(m *models.PatientScheduleTemplateItem) (total int64) {
  749. 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)
  750. return
  751. }
  752. func FindSchedualTemplateExportLog(org_id int64, export_time int64) (errLogs []*models.ExportErrLog, err error) {
  753. err = readDb.Model(&models.ExportErrLog{}).Where("user_org_id = ? AND export_time = ? AND log_type = 3", org_id, export_time).Find(&errLogs).Error
  754. return
  755. }
  756. func GetDialysisOrderByBedId(orgID, start, bed_id int64, schedule_type int64) (*models.DialysisOrder, error) {
  757. order := models.DialysisOrder{}
  758. 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
  759. if err == gorm.ErrRecordNotFound {
  760. return nil, err
  761. }
  762. if err != nil {
  763. return nil, err
  764. }
  765. return &order, nil
  766. }
  767. type VMDeviceNumber struct {
  768. ID int64 `gorm:"column:id" json:"id"`
  769. OrgID int64 `gorm:"column:org_id" json:"-"`
  770. Number string `gorm:"column:number" json:"number"`
  771. GroupID int64 `gorm:"column:group_id" json:"group_id"`
  772. ZoneID int64 `gorm:"column:zone_id" json:"zone_id"`
  773. Status int8 `json:"-"`
  774. CreateTime int64 `gorm:"column:ctime" json:"-"`
  775. ModifyTime int64 `gorm:"column:mtime" json:"-"`
  776. DeviceZone models.DeviceZone `json:"zone" gorm:"foreignkey:ID;AssociationForeignKey:ZoneID;"`
  777. }
  778. func (VMDeviceNumber) TableName() string {
  779. return "xt_device_number"
  780. }
  781. type VMPatientScheduleTemplateItem struct {
  782. ID int64 `gorm:"column:id" json:"id" form:"id"`
  783. OrgID int64 `gorm:"column:org_id" json:"-" form:"org_id"`
  784. TemplateID int64 `gorm:"column:template_id" json:"template_id" form:"template_id"`
  785. DeviceNumberID int64 `gorm:"column:device_number_id" json:"device_number_id" form:"device_number_id"`
  786. PatientID int64 `gorm:"column:patient_id" json:"patient_id" form:"patient_id"`
  787. TreatMode int64 `gorm:"column:treat_mode" json:"treat_mode" form:"treat_mode"` // 治疗模式
  788. Weekday int8 `gorm:"column :weekday" json:"weekday" form:"weekday"` // 周一-周日 1-7
  789. TimeType int8 `gorm:"column:time_type" json:"time_type" form:"time_type"` // 上下晚 123
  790. Status int8 `gorm:"column:status" json:"status" form:"status"`
  791. CreateTime int64 `gorm:"column:ctime" json:"-" form:"ctime"`
  792. ModifyTime int64 `gorm:"column:mtime" json:"-" form:"mtime"`
  793. IsExport int64 `gorm:"column:is_export" json:"is_export" form:"is_export"`
  794. Patient string `gorm:"-" json:"patient" form:"patient"`
  795. DeviceNumber VMDeviceNumber `json:"number" gorm:"foreignkey:ID;AssociationForeignKey:DeviceNumberID;"`
  796. PatientInfectiousDiseases []*models.InfectiousDiseases `json:"patient_contagions" gorm:"foreignkey:PatientId;AssociationForeignKey:PatientId;"`
  797. }
  798. func (VMPatientScheduleTemplateItem) TableName() string {
  799. return "xt_patient_schedule_template_item"
  800. }
  801. func GetTemplateSchedualPatientsByKeywords(keywords string, org_id int64, template_id int64) (schedule []*VMPatientScheduleTemplateItem, err error) {
  802. likeKey := "%" + keywords + "%"
  803. err = readDb.
  804. Table("xt_patient_schedule_template_item as s").
  805. Preload("PatientInfectiousDiseases", "status = 1 ").
  806. Preload("DeviceNumber", func(db *gorm.DB) *gorm.DB {
  807. return db.Where("org_id = ? AND status = 1", org_id).Preload("DeviceZone", "org_id = ? AND status = 1", org_id)
  808. }).
  809. Joins("JOIN xt_patients as p ON p.id = s.patient_id AND (p.name LIKE ? OR p.dialysis_no LIKE ?)", likeKey, likeKey).
  810. Where("s.template_id = ? AND s.org_id=? and s.status=1", template_id, org_id).
  811. 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").
  812. Order("weekday asc").
  813. Find(&schedule).Error
  814. return
  815. }
  816. func UpdateNewScheduleTemplateStatus(id int64, org_id int64) (err error) {
  817. err = readDb.Model(&models.PatientScheduleTemplateItem{}).Where("org_id = ? AND id = ?", org_id, id).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  818. return
  819. }
  820. func GetNextWeekDaySchedule(weektype int64, weektime int64, startime int64, endtime int64, orgID int64, zone []string, record_date int64) (schedules []*models.WeekScheduleSix, err error) {
  821. db := XTReadDB().Table("xt_schedule as s ").Where("s.status =1")
  822. if orgID > 0 {
  823. db = db.Where("s.user_org_id = ?", orgID)
  824. }
  825. if startime > 0 {
  826. db = db.Where("s.schedule_date >= ?", startime)
  827. }
  828. if endtime > 0 {
  829. db = db.Where("s.schedule_date <= ?", endtime)
  830. }
  831. if weektime > 0 {
  832. db = db.Where("s.schedule_type = ?", weektime)
  833. }
  834. if weektype > 0 {
  835. db = db.Where("s.schedule_week = ?", weektype)
  836. }
  837. if len(zone) > 0 {
  838. db = db.Where("s.partition_id in (?)", zone)
  839. }
  840. err = db.
  841. Preload("DeviceZone", "status = 1 ").
  842. Preload("DeviceNumber", "status = 1 ").
  843. Preload("XtAssessmentBeforeDislysis", func(db *gorm.DB) *gorm.DB {
  844. return db.Where("status = 1 and assessment_date = ?", record_date)
  845. }).Preload("DialysisPrescription", func(db *gorm.DB) *gorm.DB {
  846. return db.Where("status = 1").Order("id asc")
  847. }).Preload("DialysisSolution", func(db *gorm.DB) *gorm.DB {
  848. return db.Where("status = 1").Order("id asc")
  849. }).Preload("DoctorAdvice", func(db *gorm.DB) *gorm.DB {
  850. return db.Where("status =1 and parent_id = 0 and advice_date = ?", record_date).Preload("ChildDoctorAdvice", "status = 1 and advice_date = ?", record_date)
  851. }).Preload("HisDoctorAdviceInfo", "status = 1 and advice_date = ?", record_date).Joins("JOIN xt_patients as p ON p.id = s.patient_id").
  852. 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").
  853. Order("s.partition_id desc").
  854. Find(&schedules).Error
  855. return schedules, err
  856. }
  857. func GetThreeWeekList(startime int64, endtime int64, orgid int64, scheduletype int64, partionid int64) (schedule []*models.VmSchedule, err error) {
  858. db := XTReadDB().Table("xt_schedule as x").Where("x.status =1")
  859. if startime > 0 {
  860. db = db.Where("x.schedule_date >=?", startime)
  861. }
  862. if endtime > 0 {
  863. db = db.Where("x.schedule_date <=?", endtime)
  864. }
  865. if orgid > 0 {
  866. db = db.Where("x.user_org_id = ?", orgid)
  867. }
  868. if scheduletype > 0 {
  869. db = db.Where("x.schedule_type = ?", scheduletype)
  870. }
  871. if partionid > 0 {
  872. db = db.Where("x.partition_id = ?", partionid)
  873. }
  874. 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
  875. return schedule, err
  876. }
  877. func UpdatedDialysisPrescription(prescription *models.DialysisPrescription, recordDate int64, patientid int64, orgid int64) error {
  878. 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
  879. return err
  880. }
  881. func GetWeekSchedules(orgid int64, startime int64, endtime int64) (scheudle []*models.XtSchedule, err error) {
  882. err = XTReadDB().Model(&scheudle).Where("user_org_id = ? and status = 1 and schedule_date>=? and schedule_date<=?", orgid, startime, endtime).Find(&scheudle).Error
  883. return scheudle, err
  884. }
  885. func GetLastWeekSchedule(orgid int64, startime int64, endtime int64) (*models.XtSchedule, error) {
  886. schedule := models.XtSchedule{}
  887. err := XTReadDB().Where("user_org_id = ? and status =1 and schedule_date>=? and schedule_date<=?", orgid, startime, endtime).Find(&schedule).Error
  888. if err == gorm.ErrRecordNotFound {
  889. return nil, err
  890. }
  891. if err != nil {
  892. return nil, err
  893. }
  894. return &schedule, nil
  895. }
  896. func AddPatientSchedule(schedule *models.XtSchedule) error {
  897. utx := XTWriteDB().Begin()
  898. err := utx.Create(&schedule).Error
  899. utx.Commit()
  900. return err
  901. }
  902. func UpdatePatientSchedule(orgid int64, startime int64, endtime int64) error {
  903. schedule := models.XtSchedule{}
  904. 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
  905. return err
  906. }
  907. func GetSettingById(id int64) (*models.XtRemindPrintSetting, error) {
  908. setting := models.XtRemindPrintSetting{}
  909. err := XTReadDB().Model(&setting).Where("id = ? and status = 1", id).Find(&setting).Error
  910. if err == gorm.ErrRecordNotFound {
  911. return nil, err
  912. }
  913. if err != nil {
  914. return nil, err
  915. }
  916. return &setting, nil
  917. }
  918. func CreateSetting(setting *models.XtRemindPrintSetting) error {
  919. err := XTReadDB().Create(&setting).Error
  920. return err
  921. }
  922. func UpdatedRemindPrint(set *models.XtRemindPrintSetting, id int64) error {
  923. 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
  924. return err
  925. }
  926. func GetRemindPrintList(id int64) (models.XtRemindPrintSetting, error) {
  927. setting := models.XtRemindPrintSetting{}
  928. err := XTReadDB().Model(&setting).Where("user_org_id = ? and status =1", id).Find(&setting).Error
  929. return setting, err
  930. }
  931. func GetScheudleTemplateById(orgid int64) (*models.ScheduleTemplate, error) {
  932. schedule := models.ScheduleTemplate{}
  933. err := XTReadDB().Model(&schedule).Where("user_org_id = ? and status = 1", orgid).Find(&schedule).Error
  934. if err == gorm.ErrRecordNotFound {
  935. return nil, err
  936. }
  937. if err != nil {
  938. return nil, err
  939. }
  940. return &schedule, nil
  941. }
  942. func CreateXtscheduleTemplate(template *models.ScheduleTemplate) error {
  943. err := XTWriteDB().Create(&template).Error
  944. return err
  945. }
  946. func UpdateXtScheduleTemplate(template *models.ScheduleTemplate, orgid int64) error {
  947. err := XTWriteDB().Model(&template).Where("user_org_id = ? and status = 1", orgid).Updates(map[string]interface{}{"template_id": template.TemplateId}).Error
  948. return err
  949. }
  950. func GetScheduleTemplate(orgid int64) (models.ScheduleTemplate, error) {
  951. template := models.ScheduleTemplate{}
  952. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&template).Error
  953. return template, err
  954. }
  955. func GetDataPrintByOrgId(orgid int64) (*models.XtDataPrint, error) {
  956. dataPrint := models.XtDataPrint{}
  957. err := XTReadDB().Model(&dataPrint).Where("user_org_id = ? and status = 1", orgid).Find(&dataPrint).Error
  958. if err == gorm.ErrRecordNotFound {
  959. return nil, err
  960. }
  961. if err != nil {
  962. return nil, err
  963. }
  964. return &dataPrint, nil
  965. }
  966. func CreateDataPrint(dataPrint *models.XtDataPrint) error {
  967. err := XTWriteDB().Create(&dataPrint).Error
  968. return err
  969. }
  970. func UpdteDataPrint(orgid int64, dataPrint *models.XtDataPrint) error {
  971. err := XTWriteDB().Model(&dataPrint).Where("user_org_id = ? and status = 1", orgid).Updates(map[string]interface{}{"is_open": dataPrint.IsOpen}).Error
  972. return err
  973. }
  974. func GetDataPrint(orgid int64) (models.XtDataPrint, error) {
  975. dataPrint := models.XtDataPrint{}
  976. err := XTReadDB().Model(&dataPrint).Where("user_org_id = ? and status = 1", orgid).Find(&dataPrint).Error
  977. return dataPrint, err
  978. }
  979. func GetNextWeekPanels(startime int64, endtime int64, orgid int64) (schedule []*models.XtSchedule, err error) {
  980. 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 {
  981. return db.Where("user_org_id = ? and status = 1 and assessment_date < ?", orgid, endtime)
  982. }).Find(&schedule).Error
  983. return schedule, err
  984. }
  985. func GetTemplateListByTemplateId(id int64, orgid int64) (list []*models.VmPatientScheduleTemplateItem, err error) {
  986. err = XTReadDB().Where("template_id =? and org_id = ? and status = 1", id, orgid).Find(&list).Error
  987. return list, err
  988. }
  989. func DeletePatientSchedule(startime int64, endtime int64, org_id int64) (models.XtSchedule, error) {
  990. schedule := models.XtSchedule{}
  991. 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
  992. return schedule, err
  993. }
  994. func DeletePatientScheduleToday(startime int64, endtime int64, org_id int64) (models.XtSchedule, error) {
  995. schedule := models.XtSchedule{}
  996. 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
  997. return schedule, err
  998. }
  999. func GetBedNumberById(id int64, org_id int64) (models.DeviceNumber, error) {
  1000. number := models.DeviceNumber{}
  1001. err := XTReadDB().Where("id = ? and status = 1 and org_id =?", id, org_id).Find(&number).Error
  1002. return number, err
  1003. }
  1004. func CreatePatientSchedule(schedule *models.XtSchedule) error {
  1005. utx := XTWriteDB().Begin()
  1006. err := utx.Create(&schedule).Error
  1007. utx.Commit()
  1008. return err
  1009. }
  1010. func GetPatientScheduleTempalate(orgid int64) (models.PatientScheduleTemplateMode, error) {
  1011. mode := models.PatientScheduleTemplateMode{}
  1012. err := XTReadDB().Where("org_id = ? and status =1", orgid).Find(&mode).Error
  1013. return mode, err
  1014. }
  1015. func GetSolutionSchedule(orgid int64) (solution []*models.VmDialysisSolution, err error) {
  1016. 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 {
  1017. return db.Where("user_org_id = ? and status = 1", orgid).Order("id asc")
  1018. }).Find(&solution).Error
  1019. return solution, err
  1020. }
  1021. func GetSolutionScheduleBySchedule(orgid int64, zonid int64, classtype int64, schedule_date int64) (schedule []*models.XtScheduleList, err error) {
  1022. db := XTReadDB().Model(&schedule).Where("status = 1")
  1023. if orgid > 0 {
  1024. db = db.Where("user_org_id =?", orgid)
  1025. }
  1026. if zonid > 0 {
  1027. db = db.Where("partition_id = ?", zonid)
  1028. }
  1029. if classtype > 0 {
  1030. db = db.Where("schedule_type = ?", classtype)
  1031. }
  1032. if schedule_date > 0 {
  1033. db = db.Where("schedule_date = ?", schedule_date)
  1034. }
  1035. err = db.Preload("DialysisSolution", func(db *gorm.DB) *gorm.DB {
  1036. 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 {
  1037. return db.Where("user_org_id = ? and status = 1", orgid).Order("id asc")
  1038. })
  1039. }).Find(&schedule).Error
  1040. return schedule, err
  1041. }
  1042. func GetDeathPatient(orgid int64) (patient []*models.XtPatients, err error) {
  1043. err = XTReadDB().Where("user_org_id = ? and (lapseto = 2 || lapseto = 3) and status = 1", orgid).Find(&patient).Error
  1044. return patient, err
  1045. }
  1046. func GetInitPrintData(patient_id int64, scheduleDate int64, orgid int64) (list []*models.VmSchedulesRemind, err error) {
  1047. db := XTReadDB().Table("xt_schedule as x").Where("x.status = 1")
  1048. if orgid > 0 {
  1049. db = db.Where("x.user_org_id = ?", orgid)
  1050. }
  1051. if patient_id > 0 {
  1052. db = db.Where("x.patient_id = ?", patient_id)
  1053. }
  1054. if scheduleDate > 0 {
  1055. db = db.Where("x.schedule_date >=? and x.schedule_date<=?", scheduleDate, scheduleDate)
  1056. }
  1057. 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 {
  1058. return db.Where("status =1 and patient_id = ?", patient_id).Order("id asc")
  1059. }).Find(&list).Error
  1060. return list, err
  1061. }
  1062. func GetLastWeightAfter(patient_id int64, scheduleDate int64, orgid int64) (models.AssessmentAfterDislysis, error) {
  1063. dislysis := models.AssessmentAfterDislysis{}
  1064. 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
  1065. return dislysis, err
  1066. }
  1067. func GetScheduleTwo(orgID, id int64) (models.Schedule, error) {
  1068. var schedule models.Schedule
  1069. var err error
  1070. err = readDb.Model(&models.Schedule{}).Where("id = ? and user_org_id=? and status=1", id, orgID).First(&schedule).Error
  1071. return schedule, err
  1072. }
  1073. func UpdateSchDName(sch models.Schedule) (err error) {
  1074. err = writeDb.Model(&models.Schedule{}).Where("status=1 AND id = ?", sch.ID).Updates(map[string]interface{}{"dialysis_machine_name": sch.DialysisMachineName}).Error
  1075. return
  1076. }
  1077. func GetDialysisOrderByBedIdTwo(orgID, start, bed_id int64, schedule_type int64) (models.DialysisOrder, error) {
  1078. order := models.DialysisOrder{}
  1079. 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
  1080. return order, nil
  1081. }
  1082. func ChangeScheduleClass(schedule models.Schedule) error {
  1083. 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
  1084. return err
  1085. }
  1086. func GetLastDialysisOrder(org_id int64, patient_id int64, dialysis_date int64) (models.XtDialysisOrderSix, error) {
  1087. order := models.XtDialysisOrderSix{}
  1088. 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
  1089. return order, err
  1090. }
  1091. func GetLastDialysisSolution(org_id int64, patient_id int64, mode_id int64, schedule_date int64) (models.DialysisPrescription, error) {
  1092. dialysisprescription := models.DialysisPrescription{}
  1093. 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
  1094. return dialysisprescription, err
  1095. }