schedule_service.go 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. package service
  2. import (
  3. "XT_New/models"
  4. "github.com/jinzhu/gorm"
  5. )
  6. //GetSchedulePartitionPanel 取出排班面板,分区-机号
  7. func GetSchedulePartitionPanel(orgID int64) (partitions []*models.Partition, err error) {
  8. err = readDb.Model(&models.DeviceZone{}).Preload("Jihaos", "org_id=? and status=1", orgID).Where("org_id=? and status=1", orgID).Find(&partitions).Error
  9. return
  10. }
  11. func GetPatientWithScheduleAndSolution(orgID int64, keywords string, weekStart, weekEnd, schedule, contagion int64) (patients []*models.Patients, err error) {
  12. db := readDb.Table("xt_patients as p").
  13. Preload("DialysisSolutions", "user_org_id=? and initiate_mode=1 and affirm_state=1 and parent_id=0 and status=1", orgID).
  14. Preload("Schedules", "user_org_id=? and schedule_date>=? and schedule_date<= ? and status=1", orgID, weekStart, weekEnd).
  15. Preload("Contagions", "status=1").
  16. Where("p.user_org_id=? and p.lapseto=1 and p.status=1", orgID)
  17. if len(keywords) > 0 {
  18. likeKey := "%" + keywords + "%"
  19. db = db.Where("p.name LIKE ?", likeKey)
  20. }
  21. if schedule == 1 {
  22. 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())
  23. } else if schedule == 2 {
  24. 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())
  25. }
  26. if contagion > 0 {
  27. 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())
  28. }
  29. err = db.Find(&patients).Error
  30. return
  31. }
  32. func GetWeekSchedule(orgID int64, start, end int64) (schedules []*models.Schedule, err error) {
  33. err = readDb.
  34. Table("xt_schedule as s").
  35. Preload("PatientInfectiousDiseases", "status = 1 ").
  36. Joins("JOIN xt_patients as p ON p.id = s.patient_id").
  37. Where("s.user_org_id=? and s.schedule_date>=? and s.schedule_date<=? and s.status=1", orgID, start, end).
  38. 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").
  39. Order("updated_time asc").
  40. Find(&schedules).Error
  41. return
  42. }
  43. func GetDaySchedule(orgID, start, end, patientID int64) (schedule models.Schedule, err error) {
  44. 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
  45. return
  46. }
  47. func GetDayScheduleTwo(orgID, start, patientID int64) (schedule models.Schedule, err error) {
  48. 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
  49. return
  50. }
  51. func GetDayScheduleByBedid(orgID, start, bed_id int64, schedule_type int64) (schedule models.Schedule, err error) {
  52. 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
  53. return
  54. }
  55. func GetPatientScheduleFormDay(orgID, start, end, patientID int64) (schedules []*models.PatientSchedule, err error) {
  56. err = readDb.Table("xt_schedule as s").
  57. Preload("DeviceZone", "org_id=? and status=1", orgID).
  58. Preload("DeviceNumber", "org_id=? and status=1", orgID).
  59. 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).
  60. Find(&schedules).Error
  61. return
  62. }
  63. func GetNextSchedule(orgID, end, patientID int64) (schedules []*models.PatientSchedule, err error) {
  64. err = readDb.Table("xt_schedule as s").
  65. Preload("DeviceZone", "org_id=? and status=1", orgID).
  66. Preload("DeviceNumber", "org_id=? and status=1", orgID).
  67. Where("s.patient_id =? and s.user_org_id=? and s.schedule_date>=? and s.status=1", patientID, orgID, end).
  68. Find(&schedules).Error
  69. return
  70. }
  71. func GetPointSchedule(orgID, scheduleDate, scheduleWeek, scheduleType, bedId int64) (schedule models.Schedule, err error) {
  72. 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
  73. if err == gorm.ErrRecordNotFound {
  74. err = nil
  75. return
  76. }
  77. return
  78. }
  79. func CreateSchedule(m *models.Schedule) error {
  80. return writeDb.Model(&models.Schedule{}).Create(&m).Error
  81. }
  82. func GetSchedule(orgID, id int64) (*models.Schedule, error) {
  83. var schedule models.Schedule
  84. var err error
  85. err = readDb.Model(&models.Schedule{}).Where("id = ? and user_org_id=? and status=1", id, orgID).First(&schedule).Error
  86. if err != nil {
  87. return nil, err
  88. }
  89. return &schedule, nil
  90. }
  91. func UpdateSchedule(m *models.Schedule) error {
  92. return writeDb.Model(&models.Schedule{}).Save(&m).Error
  93. }
  94. func GetPatientScheduleFormToday(orgId, id, start int64) (schedules []*models.PatientSchedule, err error) {
  95. err = readDb.Table("xt_schedule as s").
  96. Preload("DeviceZone", "org_id=? and status=1", orgId).
  97. Preload("DeviceNumber", "org_id=? and status=1", orgId).
  98. Where("s.patient_id =? and s.user_org_id=? and s.schedule_date>=? and s.status=1", id, orgId, start).
  99. Find(&schedules).Error
  100. return
  101. }
  102. type WeekSchedulePrintVM struct {
  103. ID int64 `gorm:"column:id" json:"id"`
  104. ZoneID int64 `gorm:"column:partition_id" json:"zone_id"`
  105. DeviceNumberID int64 `gorm:"column:bed_id" json:"device_number_id"`
  106. PatientID int64 `gorm:"column:patient_id" json:"patient_id"`
  107. ScheduleDate int64 `gorm:"column:schedule_date" json:"schedule_date"`
  108. ScheduleType int64 `gorm:"column:schedule_type" json:"schedule_type"`
  109. ScheduleWeek int64 `gorm:"column:schedule_week" json:"schedule_week"`
  110. ModeID int64 `gorm:"column:mode_id" json:"mode_id"`
  111. Patient *models.Patients `gorm:"ForeignKey:PatientID" json:"patient"`
  112. DeviceNumber *models.DeviceNumber `gorm:"ForeignKey:DeviceNumberID" json:"device_number"`
  113. Zone *models.DeviceZone `gorm:"ForeignKey:ZoneID" json:"zone"`
  114. Mode *models.TreatmentMode `gorm:"ForeignKey:ModeID" json:"mode"`
  115. }
  116. func GetPrinitWeekSchedules(orgID int64, start, end int64) ([]*WeekSchedulePrintVM, error) {
  117. var schedules []*WeekSchedulePrintVM
  118. err := readDb.Table("xt_schedule as s").
  119. Preload("Patient", "status = 1 and user_org_id = ?", orgID).
  120. Preload("DeviceNumber", "status = 1 and org_id = ?", orgID).
  121. Preload("Zone", "status = 1 and org_id = ?", orgID).
  122. Preload("Mode", "status = 1").
  123. // Joins("JOIN xt_patients as p ON p.id = s.patient_id").
  124. Where("s.user_org_id = ? and s.schedule_date >= ? and s.schedule_date <= ? and s.status = 1", orgID, start, end).
  125. // 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").
  126. Find(&schedules).Error
  127. return schedules, err
  128. }
  129. // func ExchangeScheduleTimeWithWeekSchedules(orgID int64, nextWeekSchs []*models.Schedule, nextTwoWeekSchs []*models.Schedule) error {
  130. // templateMode, getTemplateModeErr := GetOrgPatientScheduleTemplateMode(orgID)
  131. // if getTemplateModeErr != nil {
  132. // return getTemplateModeErr
  133. // }
  134. // if templateMode == nil {
  135. // return errors.New("模板模式不存在")
  136. // }
  137. // tx := writeDb.Begin()
  138. // updateTime := time.Now().Unix()
  139. // for _, sch := range nextWeekSchs {
  140. // sch.PatientInfectiousDiseases = nil
  141. // date := time.Unix(sch.ScheduleDate, 0)
  142. // sch.ScheduleDate = date.AddDate(0, 0, 7).Unix()
  143. // sch.UpdatedTime = updateTime
  144. // updateSchErr := tx.Save(sch).Error
  145. // if updateSchErr != nil {
  146. // tx.Rollback()
  147. // return updateSchErr
  148. // }
  149. // }
  150. // for _, sch := range nextTwoWeekSchs {
  151. // sch.PatientInfectiousDiseases = nil
  152. // date := time.Unix(sch.ScheduleDate, 0)
  153. // sch.ScheduleDate = date.AddDate(0, 0, -7).Unix()
  154. // sch.UpdatedTime = updateTime
  155. // updateSchErr := tx.Save(sch).Error
  156. // if updateSchErr != nil {
  157. // tx.Rollback()
  158. // return updateSchErr
  159. // }
  160. // }
  161. // templateMode.ModifyTime = updateTime
  162. // templateMode.ExecuteTimes += 1
  163. // updateModeErr := tx.Save(templateMode).Error
  164. // if updateModeErr != nil {
  165. // tx.Rollback()
  166. // return updateModeErr
  167. // }
  168. // tx.Commit()
  169. // return nil
  170. // }
  171. func GetSchedualPatientsByKeywords(keywords string, org_id int64) (schedule []*models.Schedule, err error) {
  172. likeKey := "%" + keywords + "%"
  173. err = readDb.
  174. Table("xt_schedule as s").
  175. Preload("PatientInfectiousDiseases", "status = 1 ").
  176. Preload("DeviceZone", "status = 1 ").
  177. Preload("DeviceNumber", "status = 1 ").
  178. Preload("TreatmentMode", "status = 1 ").
  179. Joins("JOIN xt_patients as p ON p.id = s.patient_id AND (p.name LIKE ? OR p.dialysis_no LIKE ?)", likeKey, likeKey).
  180. Where("s.user_org_id=? and s.schedule_date >= UNIX_TIMESTAMP(DATE_FORMAT(NOW(),'%Y-%m-%d 00:00:00')) and s.schedule_date <= UNIX_TIMESTAMP(DATE_FORMAT( SUBDATE(CURDATE(),DATE_FORMAT(CURDATE(),'%w')-7), '%Y-%m-%d 23:59:59')) and s.status=1", org_id).
  181. 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").
  182. Order("schedule_date asc").
  183. Find(&schedule).Error
  184. return
  185. }
  186. func GetWeekDaySchedule(orgID int64, scheduleDate int64) (schedules []*models.WeekSchedule, err error) {
  187. err = readDb.
  188. Table("xt_schedule as s").
  189. Preload("DeviceZone", "status = 1 ").
  190. Preload("DeviceNumber", "status = 1 ").
  191. Preload("DoctorAdvice", func(db *gorm.DB) *gorm.DB {
  192. return db.Where("status = 1 AND advice_type = 1").Preload("ChildDoctorAdvice", "status = 1")
  193. }).
  194. Preload("DialysisPrescription", func(db *gorm.DB) *gorm.DB {
  195. return db.Where("status = 1").Order("id desc")
  196. }).
  197. Joins("JOIN xt_patients as p ON p.id = s.patient_id").
  198. Where("s.user_org_id=? and s.schedule_date = ? and s.status=1", orgID, scheduleDate).
  199. 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").
  200. Order("schedule_type").
  201. Find(&schedules).Error
  202. return
  203. }