smart_sch.go 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. db := readDb.Model(&models.SmartSchPatient{}).Where("user_org_id=? and status=1 and lapseto !=3", 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 = ? ", orgID, item.PatientId, item.ScheduleDate).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 and lapseto=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. //
  65. // } else if mode.Mode == 2 {
  66. // schs, _ := GetSmartSchTemplatePatientByID(item.UserOrgId, item.ID, 2)
  67. //
  68. // item.XtPatientScheduleTemplateId = schs
  69. //
  70. // } else if mode.Mode == 3 {
  71. // schs, _ := GetSmartSchTemplatePatientByID(item.UserOrgId, item.ID, 3)
  72. // item.XtPatientScheduleTemplateId = schs
  73. //
  74. // } else if mode.Mode == 4 {
  75. //
  76. // schs, _ := GetSmartSchTemplatePatientByID(item.UserOrgId, item.ID, 4)
  77. // item.XtPatientScheduleTemplateId = schs
  78. //
  79. // }
  80. // }
  81. //
  82. //}
  83. return
  84. }
  85. func GetSmartSchPatientByID(orgID int64, patient_id int64) (schs []*models.SmartSchedule, err error) {
  86. now := time.Now()
  87. offset := int(time.Monday - now.Weekday())
  88. if offset > 0 {
  89. offset = -6
  90. }
  91. weekStart := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, offset)
  92. err = readDb.Model(&models.SmartSchedule{}).Where("user_org_id = ? AND status = 1 AND patient_id = ? AND schedule_date >= ?", orgID, patient_id, weekStart.Unix()).
  93. Preload("DeviceZone", "status = 1").
  94. Preload("DeviceNumber", "status = 1").Find(&schs).Error
  95. for _, item := range schs {
  96. var order models.SmSchDialysisOrder
  97. readDb.Model(&models.SmSchDialysisOrder{}).Where("user_org_id = ? AND status = 1 AND patient_id = ? AND dialysis_date = ? ", orgID, item.PatientId, item.ScheduleDate).First(&order)
  98. item.SmSchDialysisOrder = order
  99. }
  100. return
  101. }
  102. func GetSmartSchTemplatePatientByID(orgID int64, patient_id int64, count int) (schs []*models.XtPatientScheduleTemplateId, err error) {
  103. err = readDb.Model(&models.XtPatientScheduleTemplateId{}).Preload("XtPatientScheduleTemplateItem", func(db *gorm.DB) *gorm.DB {
  104. return db.Where("status = 1 AND patient_id = ?", patient_id).Order("weekday")
  105. }).Where("org_id = ? AND status = 1", orgID).Limit(count).Find(&schs).Error
  106. for _, items := range schs {
  107. for _, subItems := range items.XtPatientScheduleTemplateItem {
  108. number, _ := GetBedNumberById(subItems.DeviceNumberId, subItems.OrgId)
  109. subItems.ZoneId = number.ZoneID
  110. }
  111. }
  112. return
  113. }
  114. func GetAllAvaildDeviceNumbersByZone(orgID int64, record_date int64, schedule_type int, zone_id int64, patient_id int64) ([]*DeviceNumberViewModel, error) {
  115. var vms []*DeviceNumberViewModel = make([]*DeviceNumberViewModel, 0)
  116. 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()
  117. defer rows.Close()
  118. if err != nil {
  119. return nil, err
  120. }
  121. for rows.Next() {
  122. var vm DeviceNumberViewModel
  123. readDb.ScanRows(rows, &vm)
  124. vms = append(vms, &vm)
  125. }
  126. return vms, nil
  127. }
  128. func GetScheduleByZoneAndBed(org_id int64, partitionId int64, bed_id int64, schedule_date int64, sch_type int64) (sch models.SmartSchedule, err error) {
  129. 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
  130. return
  131. }
  132. func GetScheduleByDate(org_id int64, schedule_date int64, patient_id int64) (sch models.SmartSchedule, err error) {
  133. 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
  134. return
  135. }
  136. func SaveSmartSch(sch models.SmartSchedule) {
  137. writeDb.Save(&sch)
  138. return
  139. }
  140. func GetAllAvaildTemplateDeviceNumbersByZone(orgID int64, weekday int64, schedule_type int, zone_id int64, patient_id int64, template_id int64) ([]*DeviceNumberViewModel, error) {
  141. var vms []*DeviceNumberViewModel = make([]*DeviceNumberViewModel, 0)
  142. 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()
  143. defer rows.Close()
  144. if err != nil {
  145. return nil, err
  146. }
  147. for rows.Next() {
  148. var vm DeviceNumberViewModel
  149. readDb.ScanRows(rows, &vm)
  150. vms = append(vms, &vm)
  151. }
  152. return vms, nil
  153. }
  154. func GetScheduleCountByDate(org_id int64, schedule_date int64, patient_id int64) (total int64, err error) {
  155. 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
  156. return
  157. }