smart_sch.go 7.8KB

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