smart_sch.go 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. //
  19. // offset := int(time.Monday - now.Weekday())
  20. // if offset > 0 {
  21. // offset = -6
  22. // }
  23. //
  24. // weekStart := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, offset)
  25. // readDb.Model(&models.SmartSchedule{}).Where("user_org_id = ? AND status = 1 AND patient_id = ? AND schedule_date >= ?", orgID, item.ID, weekStart.Unix()).
  26. // Preload("DeviceZone", "status = 1").
  27. // Preload("SmSchDialysisOrder", "status = 1").
  28. // Preload("DeviceNumber", "status = 1").Find(&sch)
  29. // item.SmartSchedule = sch
  30. //}
  31. return
  32. }
  33. func GetSmartSchPatientByPatientID(orgID int64, id int64) (sch []*models.SmartSchedule, err error) {
  34. now := time.Now()
  35. offset := int(time.Monday - now.Weekday())
  36. if offset > 0 {
  37. offset = -6
  38. }
  39. weekStart := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, offset)
  40. readDb.Model(&models.SmartSchedule{}).Where("user_org_id = ? AND status = 1 AND patient_id = ? AND schedule_date >= ?", orgID, id, weekStart.Unix()).
  41. Preload("DeviceZone", "status = 1").
  42. Preload("DeviceNumber", "status = 1").Find(&sch)
  43. for _, item := range sch {
  44. var order models.SmSchDialysisOrder
  45. readDb.Model(&models.SmSchDialysisOrder{}).Where("user_org_id = ? AND status = 1 AND patient_id = ? AND dialysis_date = ? AND bed_id = ?", orgID, item.PatientId, item.ScheduleDate, item.BedId).First(&order)
  46. item.SmSchDialysisOrder = order
  47. }
  48. return
  49. }
  50. func GetSmartSchTemplatePatientByKeyWord(orgID int64, keywords string) (patient []*models.SmartSchTemplatePatient, err error) {
  51. db := readDb.Model(&models.SmartSchTemplatePatient{}).Where("user_org_id=? and status=1", orgID)
  52. if len(keywords) > 0 {
  53. likekey := "%" + keywords + "%"
  54. err = db.Where("name LIKE ? OR dialysis_no LIKE ? ", likekey, likekey).Find(&patient).Error
  55. } else {
  56. err = db.Find(&patient).Error
  57. }
  58. for _, item := range patient {
  59. mode, _ := GetOrgPatientScheduleTemplateModeTwo(item.UserOrgId)
  60. if mode.ID > 0 {
  61. if mode.Mode == 1 {
  62. schs, _ := GetSmartSchTemplatePatientByID(item.UserOrgId, item.ID, 1)
  63. item.XtPatientScheduleTemplateId = schs
  64. } else if mode.Mode == 2 {
  65. schs, _ := GetSmartSchTemplatePatientByID(item.UserOrgId, item.ID, 2)
  66. item.XtPatientScheduleTemplateId = schs
  67. } else if mode.Mode == 3 {
  68. schs, _ := GetSmartSchTemplatePatientByID(item.UserOrgId, item.ID, 3)
  69. item.XtPatientScheduleTemplateId = schs
  70. } else if mode.Mode == 4 {
  71. schs, _ := GetSmartSchTemplatePatientByID(item.UserOrgId, item.ID, 4)
  72. item.XtPatientScheduleTemplateId = schs
  73. }
  74. }
  75. }
  76. return
  77. }
  78. func GetSmartSchPatientByID(orgID int64, patient_id int64) (schs []*models.SmartSchedule, err error) {
  79. now := time.Now()
  80. offset := int(time.Monday - now.Weekday())
  81. if offset > 0 {
  82. offset = -6
  83. }
  84. weekStart := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, offset)
  85. err = readDb.Model(&models.SmartSchedule{}).Where("user_org_id = ? AND status = 1 AND patient_id = ? AND schedule_date >= ?", orgID, patient_id, weekStart.Unix()).
  86. Preload("DeviceZone", "status = 1").
  87. Preload("DeviceNumber", "status = 1").Find(&schs).Error
  88. for _, item := range schs {
  89. var order models.SmSchDialysisOrder
  90. readDb.Model(&models.SmSchDialysisOrder{}).Where("user_org_id = ? AND status = 1 AND patient_id = ? AND dialysis_date = ? AND bed_id = ?", orgID, item.PatientId, item.ScheduleDate, item.BedId).First(&order)
  91. item.SmSchDialysisOrder = order
  92. }
  93. return
  94. }
  95. func GetSmartSchTemplatePatientByID(orgID int64, patient_id int64, count int) (schs []*models.XtPatientScheduleTemplateId, err error) {
  96. err = readDb.Model(&models.XtPatientScheduleTemplateId{}).Preload("XtPatientScheduleTemplateItem", func(db *gorm.DB) *gorm.DB {
  97. return db.Where("status = 1 AND patient_id = ?", patient_id).Order("weekday")
  98. }).Where("org_id = ? AND status = 1", orgID).Limit(count).Find(&schs).Error
  99. for _, items := range schs {
  100. for _, subItems := range items.XtPatientScheduleTemplateItem {
  101. number, _ := GetBedNumberById(subItems.DeviceNumberId, subItems.OrgId)
  102. subItems.ZoneId = number.ZoneID
  103. }
  104. }
  105. return
  106. }
  107. func GetAllAvaildDeviceNumbersByZone(orgID int64, record_date int64, schedule_type int, zone_id int64, patient_id int64) ([]*DeviceNumberViewModel, error) {
  108. var vms []*DeviceNumberViewModel = make([]*DeviceNumberViewModel, 0)
  109. 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()
  110. defer rows.Close()
  111. if err != nil {
  112. return nil, err
  113. }
  114. for rows.Next() {
  115. var vm DeviceNumberViewModel
  116. readDb.ScanRows(rows, &vm)
  117. vms = append(vms, &vm)
  118. }
  119. return vms, nil
  120. }
  121. func GetScheduleByZoneAndBed(org_id int64, partitionId int64, bed_id int64, schedule_date int64, sch_type int64) (sch models.SmartSchedule, err error) {
  122. 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
  123. return
  124. }
  125. func GetScheduleByDate(org_id int64, schedule_date int64, patient_id int64) (sch models.SmartSchedule, err error) {
  126. err = readDb.Model(&models.SmartSchedule{}).Where(" user_org_id = ? AND schedule_date = ? AND status = 1 AND patient_id = ?", org_id, schedule_date, patient_id).First(&sch).Error
  127. return
  128. }
  129. func SaveSmartSch(sch models.SmartSchedule) {
  130. writeDb.Save(&sch)
  131. return
  132. }
  133. func GetAllAvaildTemplateDeviceNumbersByZone(orgID int64, weekday int64, schedule_type int, zone_id int64, patient_id int64, template_id int64) ([]*DeviceNumberViewModel, error) {
  134. var vms []*DeviceNumberViewModel = make([]*DeviceNumberViewModel, 0)
  135. 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_patient_schedule_template_item as s Where s.`weekday` = ? AND s.patient_id <> ? AND s.org_id = n.org_id AND s.`device_number_id` = n.id AND s.`time_type` = ? AND s.status = 1 AND s.template_id = ?)", zone_id, orgID, weekday, patient_id, schedule_type, template_id).Rows()
  136. defer rows.Close()
  137. if err != nil {
  138. return nil, err
  139. }
  140. for rows.Next() {
  141. var vm DeviceNumberViewModel
  142. readDb.ScanRows(rows, &vm)
  143. vms = append(vms, &vm)
  144. }
  145. return vms, nil
  146. }