smart_sch.go 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package service
  2. import (
  3. "XT_New/models"
  4. "github.com/jinzhu/gorm"
  5. "time"
  6. )
  7. func GetSmartSchPatientByKeyWord(orgID int64, keywords string) (patient []*models.SmartSchPatient, err error) {
  8. db := readDb.Model(&models.SmartSchPatient{}).Where("user_org_id=? and status=1", orgID)
  9. if len(keywords) > 0 {
  10. likekey := "%" + keywords + "%"
  11. err = db.Where("name LIKE ? OR dialysis_no LIKE ? ", likekey, likekey).Find(&patient).Error
  12. } else {
  13. err = db.Find(&patient).Error
  14. }
  15. for _, item := range patient {
  16. var sch []*models.SmartSchedule
  17. now := time.Now()
  18. offset := int(time.Monday - now.Weekday())
  19. if offset > 0 {
  20. offset = -6
  21. }
  22. weekStart := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, offset)
  23. readDb.Model(&models.SmartSchedule{}).Where("user_org_id = ? AND status = 1 AND patient_id = ? AND schedule_date >= ?", orgID, item.ID, weekStart.Unix()).
  24. Preload("DeviceZone", "status = 1").
  25. Preload("SmSchDialysisOrder", "status = 1").
  26. Preload("DeviceNumber", "status = 1").Find(&sch)
  27. item.SmartSchedule = sch
  28. }
  29. return
  30. }
  31. func GetSmartSchTemplatePatientByKeyWord(orgID int64, keywords string) (patient []*models.SmartSchTemplatePatient, err error) {
  32. db := readDb.Model(&models.SmartSchTemplatePatient{}).Where("user_org_id=? and status=1", orgID)
  33. if len(keywords) > 0 {
  34. likekey := "%" + keywords + "%"
  35. err = db.Where("name LIKE ? OR dialysis_no LIKE ? ", likekey, likekey).Find(&patient).Error
  36. } else {
  37. err = db.Find(&patient).Error
  38. }
  39. for _, item := range patient {
  40. mode, _ := GetOrgPatientScheduleTemplateModeTwo(item.UserOrgId)
  41. if mode.ID > 0 {
  42. if mode.Mode == 1 {
  43. schs, _ := GetSmartSchTemplatePatientByID(item.UserOrgId, item.ID, 1)
  44. item.XtPatientScheduleTemplateId = schs
  45. } else if mode.Mode == 2 {
  46. schs, _ := GetSmartSchTemplatePatientByID(item.UserOrgId, item.ID, 2)
  47. item.XtPatientScheduleTemplateId = schs
  48. } else if mode.Mode == 3 {
  49. schs, _ := GetSmartSchTemplatePatientByID(item.UserOrgId, item.ID, 3)
  50. item.XtPatientScheduleTemplateId = schs
  51. } else if mode.Mode == 4 {
  52. schs, _ := GetSmartSchTemplatePatientByID(item.UserOrgId, item.ID, 4)
  53. item.XtPatientScheduleTemplateId = schs
  54. }
  55. }
  56. }
  57. return
  58. }
  59. func GetSmartSchPatientByID(orgID int64, patient_id int64) (schs []*models.SmartSchedule, err error) {
  60. now := time.Now()
  61. offset := int(time.Monday - now.Weekday())
  62. if offset > 0 {
  63. offset = -6
  64. }
  65. weekStart := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, offset)
  66. err = readDb.Model(&models.SmartSchedule{}).Where("user_org_id = ? AND status = 1 AND patient_id = ? AND schedule_date >= ?", orgID, patient_id, weekStart.Unix()).
  67. Preload("DeviceZone", "status = 1").
  68. Preload("SmSchDialysisOrder", "status = 1").
  69. Preload("DeviceNumber", "status = 1").Find(&schs).Error
  70. return
  71. }
  72. func GetSmartSchTemplatePatientByID(orgID int64, patient_id int64, count int) (schs []*models.XtPatientScheduleTemplateId, err error) {
  73. err = readDb.Model(&models.XtPatientScheduleTemplateId{}).Preload("XtPatientScheduleTemplateItem", func(db *gorm.DB) *gorm.DB {
  74. return db.Where("status = 1 AND patient_id = ?", patient_id).Order("weekday")
  75. }).Where("org_id = ? AND status = 1", orgID).Limit(count).Find(&schs).Error
  76. for _, items := range schs {
  77. for _, subItems := range items.XtPatientScheduleTemplateItem {
  78. number, _ := GetBedNumberById(subItems.DeviceNumberId, subItems.OrgId)
  79. subItems.ZoneId = number.ZoneID
  80. }
  81. }
  82. return
  83. }
  84. func GetAllAvaildDeviceNumbersByZone(orgID int64, record_date int64, schedule_type int, zone_id int64, patient_id int64) ([]*DeviceNumberViewModel, error) {
  85. var vms []*DeviceNumberViewModel = make([]*DeviceNumberViewModel, 0)
  86. rows, err := readDb.Raw("SELECT n.*, z.name as zone_name FROM xt_device_number as n join xt_device_zone as z on z.id = n.zone_id AND z.id = ? WHERE (n.org_id = ? AND n.status = 1) AND NOT EXISTS (Select * FROM xt_schedule as s Where s.`schedule_date` = ? AND s.patient_id <> ? AND s.user_org_id = n.org_id AND s.`bed_id` = n.id AND s.`schedule_type` = ? AND s.status = 1 )", zone_id, orgID, record_date, patient_id, schedule_type).Rows()
  87. defer rows.Close()
  88. if err != nil {
  89. return nil, err
  90. }
  91. for rows.Next() {
  92. var vm DeviceNumberViewModel
  93. readDb.ScanRows(rows, &vm)
  94. vms = append(vms, &vm)
  95. }
  96. return vms, nil
  97. }
  98. func GetScheduleByZoneAndBed(org_id int64, partitionId int64, bed_id int64, schedule_date int64, sch_type int64) (sch models.SmartSchedule, err error) {
  99. err = readDb.Model(&models.SmartSchedule{}).Where("bed_id = ? AND user_org_id = ? AND schedule_date = ? AND partition_id = ? AND schedule_type = ? AND status = 1", bed_id, org_id, schedule_date, partitionId, sch_type).First(&sch).Error
  100. return
  101. }