schedule_service.go 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. package service
  2. import (
  3. "XT_New/models"
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. "time"
  8. "github.com/jinzhu/gorm"
  9. )
  10. //GetSchedulePartitionPanel 取出排班面板,分区-机号
  11. func GetSchedulePartitionPanel(orgID int64) (partitions []*models.Partition, err error) {
  12. err = readDb.Model(&models.DeviceZone{}).Preload("Jihaos", "org_id=? and status=1", orgID).Where("org_id=? and status=1", orgID).Find(&partitions).Error
  13. return
  14. }
  15. func GetPatientWithScheduleAndSolution(orgID int64, keywords string, weekStart, weekEnd, schedule, contagion int64) (patients []*models.Patients, err error) {
  16. db := readDb.Table("xt_patients as p").
  17. Preload("DialysisSolutions", "user_org_id=? and initiate_mode=1 and affirm_state=1 and parent_id=0 and status=1", orgID).
  18. Preload("Schedules", "user_org_id=? and schedule_date>=? and schedule_date<= ? and status=1", orgID, weekStart, weekEnd).
  19. Preload("Contagions", "status=1").
  20. Where("p.user_org_id=? and p.lapseto=1 and p.status=1", orgID)
  21. if len(keywords) > 0 {
  22. likeKey := "%" + keywords + "%"
  23. db = db.Where("p.name LIKE ?", likeKey)
  24. }
  25. if schedule == 1 {
  26. 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())
  27. } else if schedule == 2 {
  28. 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())
  29. }
  30. if contagion > 0 {
  31. 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())
  32. }
  33. err = db.Find(&patients).Error
  34. return
  35. }
  36. func GetWeekSchedule(orgID int64, start, end int64) (schedules []*models.Schedule, err error) {
  37. err = readDb.
  38. Table("xt_schedule as s").
  39. Preload("PatientInfectiousDiseases", "status = 1 ").
  40. Joins("JOIN xt_patients as p ON p.id = s.patient_id").
  41. Where("s.user_org_id=? and s.schedule_date>=? and s.schedule_date<=? and s.status=1", orgID, start, end).
  42. 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").
  43. Order("updated_time asc").
  44. Find(&schedules).Error
  45. return
  46. }
  47. func GetDaySchedule(orgID, start, end, patientID int64) (schedule models.Schedule, err error) {
  48. 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
  49. return
  50. }
  51. func GetDayScheduleTwo(orgID, start, patientID int64) (schedule models.Schedule, err error) {
  52. 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
  53. return
  54. }
  55. func GetDayScheduleByBedid(orgID, start, bed_id int64, schedule_type int64) (schedule models.Schedule, err error) {
  56. 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
  57. return
  58. }
  59. func GetPatientScheduleFormDay(orgID, start, end, patientID int64) (schedules []*models.PatientSchedule, err error) {
  60. err = readDb.Table("xt_schedule as s").
  61. Preload("DeviceZone", "org_id=? and status=1", orgID).
  62. Preload("DeviceNumber", "org_id=? and status=1", orgID).
  63. 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).
  64. Find(&schedules).Error
  65. return
  66. }
  67. func GetNextSchedule(orgID, end, patientID int64) (schedules []*models.PatientSchedule, err error) {
  68. err = readDb.Table("xt_schedule as s").
  69. Preload("DeviceZone", "org_id=? and status=1", orgID).
  70. Preload("DeviceNumber", "org_id=? and status=1", orgID).
  71. Where("s.patient_id =? and s.user_org_id=? and s.schedule_date>=? and s.status=1", patientID, orgID, end).
  72. Find(&schedules).Error
  73. return
  74. }
  75. func GetPointSchedule(orgID, scheduleDate, scheduleWeek, scheduleType, bedId int64) (schedule models.Schedule, err error) {
  76. 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
  77. if err == gorm.ErrRecordNotFound {
  78. err = nil
  79. return
  80. }
  81. return
  82. }
  83. func CreateSchedule(m *models.Schedule) error {
  84. return writeDb.Model(&models.Schedule{}).Create(&m).Error
  85. }
  86. func GetSchedule(orgID, id int64) (*models.Schedule, error) {
  87. var schedule models.Schedule
  88. var err error
  89. err = readDb.Model(&models.Schedule{}).Where("id = ? and user_org_id=? and status=1", id, orgID).First(&schedule).Error
  90. if err != nil {
  91. return nil, err
  92. }
  93. return &schedule, nil
  94. }
  95. func UpdateSchedule(m *models.Schedule) error {
  96. return writeDb.Model(&models.Schedule{}).Save(&m).Error
  97. }
  98. func GetPatientScheduleFormToday(orgId, id, start int64) (schedules []*models.PatientSchedule, err error) {
  99. err = readDb.Table("xt_schedule as s").
  100. Preload("DeviceZone", "org_id=? and status=1", orgId).
  101. Preload("DeviceNumber", "org_id=? and status=1", orgId).
  102. Where("s.patient_id =? and s.user_org_id=? and s.schedule_date>=? and s.status=1", id, orgId, start).
  103. Find(&schedules).Error
  104. return
  105. }
  106. type WeekSchedulePrintVM struct {
  107. ID int64 `gorm:"column:id" json:"id"`
  108. ZoneID int64 `gorm:"column:partition_id" json:"zone_id"`
  109. DeviceNumberID int64 `gorm:"column:bed_id" json:"device_number_id"`
  110. PatientID int64 `gorm:"column:patient_id" json:"patient_id"`
  111. ScheduleDate int64 `gorm:"column:schedule_date" json:"schedule_date"`
  112. ScheduleType int64 `gorm:"column:schedule_type" json:"schedule_type"`
  113. ScheduleWeek int64 `gorm:"column:schedule_week" json:"schedule_week"`
  114. ModeID int64 `gorm:"column:mode_id" json:"mode_id"`
  115. Patient *models.Patients `gorm:"ForeignKey:PatientID" json:"patient"`
  116. DeviceNumber *models.DeviceNumber `gorm:"ForeignKey:DeviceNumberID" json:"device_number"`
  117. Zone *models.DeviceZone `gorm:"ForeignKey:ZoneID" json:"zone"`
  118. Mode *models.TreatmentMode `gorm:"ForeignKey:ModeID" json:"mode"`
  119. }
  120. func GetPrinitWeekSchedules(orgID int64, start, end int64) ([]*WeekSchedulePrintVM, error) {
  121. var schedules []*WeekSchedulePrintVM
  122. err := readDb.Table("xt_schedule as s").
  123. Preload("Patient", "status = 1 and user_org_id = ?", orgID).
  124. Preload("DeviceNumber", "status = 1 and org_id = ?", orgID).
  125. Preload("Zone", "status = 1 and org_id = ?", orgID).
  126. Preload("Mode", "status = 1").
  127. // Joins("JOIN xt_patients as p ON p.id = s.patient_id").
  128. Where("s.user_org_id = ? and s.schedule_date >= ? and s.schedule_date <= ? and s.status = 1", orgID, start, end).
  129. // 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").
  130. Find(&schedules).Error
  131. return schedules, err
  132. }
  133. // func ExchangeScheduleTimeWithWeekSchedules(orgID int64, nextWeekSchs []*models.Schedule, nextTwoWeekSchs []*models.Schedule) error {
  134. // templateMode, getTemplateModeErr := GetOrgPatientScheduleTemplateMode(orgID)
  135. // if getTemplateModeErr != nil {
  136. // return getTemplateModeErr
  137. // }
  138. // if templateMode == nil {
  139. // return errors.New("模板模式不存在")
  140. // }
  141. // tx := writeDb.Begin()
  142. // updateTime := time.Now().Unix()
  143. // for _, sch := range nextWeekSchs {
  144. // sch.PatientInfectiousDiseases = nil
  145. // date := time.Unix(sch.ScheduleDate, 0)
  146. // sch.ScheduleDate = date.AddDate(0, 0, 7).Unix()
  147. // sch.UpdatedTime = updateTime
  148. // updateSchErr := tx.Save(sch).Error
  149. // if updateSchErr != nil {
  150. // tx.Rollback()
  151. // return updateSchErr
  152. // }
  153. // }
  154. // for _, sch := range nextTwoWeekSchs {
  155. // sch.PatientInfectiousDiseases = nil
  156. // date := time.Unix(sch.ScheduleDate, 0)
  157. // sch.ScheduleDate = date.AddDate(0, 0, -7).Unix()
  158. // sch.UpdatedTime = updateTime
  159. // updateSchErr := tx.Save(sch).Error
  160. // if updateSchErr != nil {
  161. // tx.Rollback()
  162. // return updateSchErr
  163. // }
  164. // }
  165. // templateMode.ModifyTime = updateTime
  166. // templateMode.ExecuteTimes += 1
  167. // updateModeErr := tx.Save(templateMode).Error
  168. // if updateModeErr != nil {
  169. // tx.Rollback()
  170. // return updateModeErr
  171. // }
  172. // tx.Commit()
  173. // return nil
  174. // }
  175. func GetSchedualPatientsByKeywords(keywords string, org_id int64) (schedule []*models.Schedule, err error) {
  176. likeKey := "%" + keywords + "%"
  177. err = readDb.
  178. Table("xt_schedule as s").
  179. Preload("PatientInfectiousDiseases", "status = 1 ").
  180. Preload("DeviceZone", "status = 1 ").
  181. Preload("DeviceNumber", "status = 1 ").
  182. Preload("TreatmentMode", "status = 1 ").
  183. Joins("JOIN xt_patients as p ON p.id = s.patient_id AND (p.name LIKE ? OR p.dialysis_no LIKE ?)", likeKey, likeKey).
  184. 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).
  185. 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").
  186. Order("schedule_date asc").
  187. Find(&schedule).Error
  188. return
  189. }
  190. func GetWeekDaySchedule(orgID int64, scheduleDate int64, scheduleDates *time.Time) (schedules []*models.WeekSchedule, err error) {
  191. err = readDb.
  192. Table("xt_schedule as s").
  193. Preload("DeviceZone", "status = 1 ").
  194. Preload("DeviceNumber", "status = 1 ").
  195. Preload("DialysisPrescription", func(db *gorm.DB) *gorm.DB {
  196. return db.Where("status = 1").Order("id asc")
  197. }).
  198. Joins("JOIN xt_patients as p ON p.id = s.patient_id").
  199. Where("s.user_org_id=? and s.schedule_date = ? and s.status=1", orgID, scheduleDate).
  200. 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").
  201. Order("schedule_type").
  202. Find(&schedules).Error
  203. for _, item := range schedules {
  204. _, config := FindDoctorAdviceRecordByOrgId(item.UserOrgId)
  205. if config.IsOpenRemind > 0 {
  206. //开启推送提醒逻辑 提交长期处方的时候,弹起长期医嘱推送
  207. var advice_three []*models.DoctorAdvice
  208. advices, _ := GetLastLongAdviceByGroupNoOther(item.UserOrgId, item.PatientId)
  209. advices_two, _ := GetLastLongAdviceByGroupNoThreeOther(item.UserOrgId, item.PatientId)
  210. for _, advice := range advices {
  211. advice.IsCheck = 1
  212. if advice.FrequencyType == 3 {
  213. week := int(scheduleDates.Weekday())
  214. fmt.Println(advice.WeekDay)
  215. switch week {
  216. case 1:
  217. if strings.Index(advice.WeekDay, "周一") > -1 {
  218. advice_three = append(advice_three, advice)
  219. }
  220. break
  221. case 2:
  222. if strings.Index(advice.WeekDay, "周二") > -1 {
  223. advice_three = append(advice_three, advice)
  224. }
  225. break
  226. case 3:
  227. if strings.Index(advice.WeekDay, "周三") > -1 {
  228. advice_three = append(advice_three, advice)
  229. }
  230. break
  231. case 4:
  232. if strings.Index(advice.WeekDay, "周四") > -1 {
  233. advice_three = append(advice_three, advice)
  234. }
  235. break
  236. case 5:
  237. if strings.Index(advice.WeekDay, "周五") > -1 {
  238. advice_three = append(advice_three, advice)
  239. }
  240. break
  241. case 6:
  242. if strings.Index(advice.WeekDay, "周六") > -1 {
  243. advice_three = append(advice_three, advice)
  244. }
  245. break
  246. case 0:
  247. if strings.Index(advice.WeekDay, "周日") > -1 {
  248. advice_three = append(advice_three, advice)
  249. }
  250. break
  251. }
  252. } else if advice.FrequencyType == 1 {
  253. advice_three = append(advice_three, advice)
  254. }
  255. }
  256. for _, advice := range advices_two {
  257. now := scheduleDates.Unix()
  258. dayStr := strconv.FormatInt(advice.DayCount, 10)
  259. dayStr2 := "-" + dayStr
  260. count, _ := strconv.ParseInt(dayStr2, 10, 64)
  261. oldTime := scheduleDates.AddDate(0, 0, int(count)).Unix()
  262. advices, _ := FindAllDoctorAdviceByTimeOther(now, oldTime, item.PatientId, item.UserOrgId, advice.TemplateId)
  263. if len(advices) == 0 {
  264. advice_three = append(advice_three, advice)
  265. }
  266. }
  267. item.DoctorAdvice = append(item.DoctorAdvice, advice_three...)
  268. }
  269. }
  270. return
  271. }
  272. func RemoveRepeatedElement(arr []*models.DoctorAdvice) (newArr []*models.DoctorAdvice) {
  273. newArr = make([]*models.DoctorAdvice, 0)
  274. for i := 0; i < len(arr); i++ {
  275. repeat := false
  276. for j := i + 1; j < len(arr); j++ {
  277. if arr[i].ID == arr[j].ID {
  278. repeat = true
  279. break
  280. }
  281. }
  282. if !repeat {
  283. newArr = append(newArr, arr[i])
  284. }
  285. }
  286. return
  287. }
  288. func GetLastLongAdviceByGroupNoOther(orgId int64, patient_id int64) (advice []*models.DoctorAdvice, err error) {
  289. err = readDb.Model(&models.DoctorAdvice{}).Where("status = 1 AND user_org_id = ? AND patient_id = ? AND advice_type = 1 AND stop_state = 2 AND parent_id = 0", orgId, patient_id).Preload("ChildDoctorAdvice", "status = 1").Find(&advice).Error
  290. return
  291. }
  292. func GetLastLongAdviceByGroupNoThreeOther(orgId int64, patient_id int64) (advice []*models.DoctorAdvice, err error) {
  293. err = readDb.Model(&models.DoctorAdvice{}).Where("status = 1 AND user_org_id = ? AND patient_id = ? AND advice_type = 1 AND frequency_type = 2 AND stop_state = 2 AND parent_id = 0 ", orgId, patient_id).Preload("ChildDoctorAdvice", "status = 1").Find(&advice).Error
  294. return
  295. }
  296. func FindAllDoctorAdviceByTimeOther(now int64, end int64, patient_id int64, orgId int64, temp_id string) (advice []*models.DoctorAdvice, err error) {
  297. err = readDb.Model(&models.DoctorAdvice{}).Where("status = 1 AND user_org_id = ? AND patient_id = ? AND advice_type = 2 AND frequency_type = 2 AND advice_date > ? AND advice_date < ? AND template_id = ? ", orgId, patient_id, end, now, temp_id).Preload("ChildDoctorAdvice", "status = 1").Find(&advice).Error
  298. return
  299. }
  300. func FindSchPatientNameByName(name string, user_org_id int64) (patient []*models.Patients, err error) {
  301. err = readDb.Model(&models.Patients{}).Where("user_org_id = ? AND name = ? AND status = 1", user_org_id, name).Find(&patient).Error
  302. return
  303. }
  304. func UpdateScheduleStatus(date int64, org_id int64) (err error) {
  305. err = readDb.Model(&models.Schedule{}).Where("user_org_id = ? AND schedule_date > ? AND status = 1", org_id, date).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  306. return
  307. }
  308. func FindDeviceZoneByName(name string, org_id int64) (zone models.DeviceZone, err error) {
  309. err = readDb.Model(&models.DeviceZone{}).Where("org_id = ? AND name = ? AND status = 1", org_id, name).First(&zone).Error
  310. return
  311. }
  312. func FindDeviceByName(device_name string, org_id int64, zone_id int64) (devices []*models.DeviceNumber, err error) {
  313. err = readDb.Model(&models.DeviceNumber{}).Where("org_id = ? AND number = ? AND zone_id = ? AND status = 1", org_id, device_name, zone_id).Find(&devices).Error
  314. return
  315. }
  316. func FindSchedualExportLog(org_id int64, export_time int64) (errLogs []*models.ExportErrLog, err error) {
  317. err = readDb.Model(&models.ExportErrLog{}).Where("user_org_id = ? AND export_time = ? AND log_type = 2", org_id, export_time).Find(&errLogs).Error
  318. return
  319. }
  320. func FindDeviceByNameTwo(device_name string, org_id int64, zone_id int64) (devices []*models.DeviceNumber, err error) {
  321. err = readDb.Model(&models.DeviceNumber{}).Where("org_id = ? AND number = ? AND status = 1 AND zone_id = ?", org_id, device_name, zone_id).Find(&devices).Error
  322. return
  323. }
  324. func UpdateScheduleTemplateStatus(org_id int64) (err error) {
  325. err = readDb.Model(&models.PatientScheduleTemplateItem{}).Where("org_id = ? AND status = 1", org_id).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  326. return
  327. }
  328. func CreateScheduleTemplate(m *models.PatientScheduleTemplateItem) error {
  329. return writeDb.Model(&models.PatientScheduleTemplateItem{}).Create(&m).Error
  330. }
  331. func FindSchedualTemplateExportLog(org_id int64, export_time int64) (errLogs []*models.ExportErrLog, err error) {
  332. err = readDb.Model(&models.ExportErrLog{}).Where("user_org_id = ? AND export_time = ? AND log_type = 3", org_id, export_time).Find(&errLogs).Error
  333. return
  334. }
  335. func GetDialysisOrderByBedId(orgID, start, bed_id int64, schedule_type int64) (schedule models.DialysisOrder, err error) {
  336. err = readDb.Model(&models.DialysisOrder{}).Where("user_org_id=? and dialysis_date=? and bed_id=? and status=1 AND schedual_type = ?", orgID, start, bed_id, schedule_type).First(&schedule).Error
  337. return
  338. }