schedule_service.go 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. package service
  2. import (
  3. "Xcx_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. type PrintCount struct {
  121. ScheduleType int64 `gorm:"column:schedule_type" json:"schedule_type"`
  122. ScheduleWeek int64 `gorm:"column:schedule_week" json:"schedule_week"`
  123. Count int64
  124. }
  125. func GetPrinitWeekSchedules(orgID int64, start, end int64) ([]*WeekSchedulePrintVM, error) {
  126. var schedules []*WeekSchedulePrintVM
  127. err := readDb.Table("xt_schedule as s").
  128. Preload("Patient", "status = 1 and user_org_id = ?", orgID).
  129. Preload("DeviceNumber", "status = 1 and org_id = ?", orgID).
  130. Preload("Zone", "status = 1 and org_id = ?", orgID).
  131. Preload("Mode", "status = 1").
  132. // Joins("JOIN xt_patients as p ON p.id = s.patient_id").
  133. Where("s.user_org_id = ? and s.schedule_date >= ? and s.schedule_date <= ? and s.status = 1", orgID, start, end).
  134. // 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").
  135. Find(&schedules).Error
  136. return schedules, err
  137. }
  138. func GetPrinitWeekTotal(orgid int64, startime int64, endtime int64) (print []*PrintCount, err error) {
  139. db := XTReadDB().Table("xt_schedule as x").Where("x.status = 1")
  140. if orgid > 0 {
  141. db = db.Where("x.user_org_id = ?", orgid)
  142. }
  143. if startime > 0 {
  144. db = db.Where("x.schedule_date >=?", startime)
  145. }
  146. if endtime > 0 {
  147. db = db.Where("x.schedule_date <=?", endtime)
  148. }
  149. err = db.Select("x.schedule_type,x.schedule_week,count(x.patient_id) as count").Group("x.schedule_date,x.schedule_type").Scan(&print).Error
  150. return print, err
  151. }
  152. func GetBloodPrintWeekTotal(orgid int64, startime int64, endtime int64) (print []*PrintCount, err error) {
  153. db := XTReadDB().Table("xt_schedule as x").Where("x.status = 1")
  154. if orgid > 0 {
  155. db = db.Where("x.user_org_id = ?", orgid)
  156. }
  157. if startime > 0 {
  158. db = db.Where("x.schedule_date >=?", startime)
  159. }
  160. if endtime > 0 {
  161. db = db.Where("x.schedule_date <=?", endtime)
  162. }
  163. db = db.Where("x.mode_id = 2")
  164. err = db.Select("x.schedule_type,x.schedule_week,count(x.patient_id) as count").Group("x.schedule_date,x.schedule_type").Scan(&print).Error
  165. return print, err
  166. }
  167. func GetPerfusionWeekTotal(orgid int64, startime int64, endtime int64) (print []*PrintCount, err error) {
  168. db := XTReadDB().Table("xt_schedule as x").Where("x.status = 1")
  169. if orgid > 0 {
  170. db = db.Where("x.user_org_id = ?", orgid)
  171. }
  172. if startime > 0 {
  173. db = db.Where("x.schedule_date >=?", startime)
  174. }
  175. if endtime > 0 {
  176. db = db.Where("x.schedule_date <=?", endtime)
  177. }
  178. db = db.Where("x.mode_id = 3")
  179. err = db.Select("x.schedule_type,x.schedule_week,count(x.patient_id) as count").Group("x.schedule_date,x.schedule_type").Scan(&print).Error
  180. return print, err
  181. }
  182. // func ExchangeScheduleTimeWithWeekSchedules(orgID int64, nextWeekSchs []*models.Schedule, nextTwoWeekSchs []*models.Schedule) error {
  183. // templateMode, getTemplateModeErr := GetOrgPatientScheduleTemplateMode(orgID)
  184. // if getTemplateModeErr != nil {
  185. // return getTemplateModeErr
  186. // }
  187. // if templateMode == nil {
  188. // return errors.New("模板模式不存在")
  189. // }
  190. // tx := writeDb.Begin()
  191. // updateTime := time.Now().Unix()
  192. // for _, sch := range nextWeekSchs {
  193. // sch.PatientInfectiousDiseases = nil
  194. // date := time.Unix(sch.ScheduleDate, 0)
  195. // sch.ScheduleDate = date.AddDate(0, 0, 7).Unix()
  196. // sch.UpdatedTime = updateTime
  197. // updateSchErr := tx.Save(sch).Error
  198. // if updateSchErr != nil {
  199. // tx.Rollback()
  200. // return updateSchErr
  201. // }
  202. // }
  203. // for _, sch := range nextTwoWeekSchs {
  204. // sch.PatientInfectiousDiseases = nil
  205. // date := time.Unix(sch.ScheduleDate, 0)
  206. // sch.ScheduleDate = date.AddDate(0, 0, -7).Unix()
  207. // sch.UpdatedTime = updateTime
  208. // updateSchErr := tx.Save(sch).Error
  209. // if updateSchErr != nil {
  210. // tx.Rollback()
  211. // return updateSchErr
  212. // }
  213. // }
  214. // templateMode.ModifyTime = updateTime
  215. // templateMode.ExecuteTimes += 1
  216. // updateModeErr := tx.Save(templateMode).Error
  217. // if updateModeErr != nil {
  218. // tx.Rollback()
  219. // return updateModeErr
  220. // }
  221. // tx.Commit()
  222. // return nil
  223. // }
  224. func GetSchedualPatientsByKeywordsAndWeek(keywords string, org_id int64, start_time int64, end_time int64) (schedule []*models.Schedule, err error) {
  225. likeKey := "%" + keywords + "%"
  226. err = readDb.
  227. Table("xt_schedule as s").
  228. Preload("PatientInfectiousDiseases", "status = 1 ").
  229. Preload("DeviceZone", "status = 1 ").
  230. Preload("DeviceNumber", "status = 1 ").
  231. Preload("TreatmentMode", "status = 1 ").
  232. Joins("JOIN xt_patients as p ON p.id = s.patient_id AND (p.name LIKE ? OR p.dialysis_no LIKE ?)", likeKey, likeKey).
  233. Where("s.user_org_id=? and s.schedule_date >= ? and s.schedule_date <= ? and s.status=1", org_id, start_time, end_time).
  234. 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").
  235. Order("schedule_date asc").
  236. Find(&schedule).Error
  237. return
  238. }
  239. func GetSchedualPatientsByKeywords(keywords string, org_id int64) (schedule []*models.Schedule, err error) {
  240. likeKey := "%" + keywords + "%"
  241. err = readDb.
  242. Table("xt_schedule as s").
  243. Preload("PatientInfectiousDiseases", "status = 1 ").
  244. Preload("DeviceZone", "status = 1 ").
  245. Preload("DeviceNumber", "status = 1 ").
  246. Preload("TreatmentMode", "status = 1 ").
  247. Joins("JOIN xt_patients as p ON p.id = s.patient_id AND (p.name LIKE ? OR p.dialysis_no LIKE ?)", likeKey, likeKey).
  248. 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).
  249. 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").
  250. Order("schedule_date asc").
  251. Find(&schedule).Error
  252. return
  253. }
  254. func GetWeekDaySchedule(orgID int64, scheduleDate int64, scheduleDates *time.Time) (schedules []*models.WeekSchedule, err error) {
  255. err = readDb.
  256. Table("xt_schedule as s").
  257. Preload("DeviceZone", "status = 1 ").
  258. Preload("DeviceNumber", "status = 1 ").
  259. Preload("DialysisPrescription", func(db *gorm.DB) *gorm.DB {
  260. return db.Where("status = 1").Order("id asc")
  261. }).
  262. Joins("JOIN xt_patients as p ON p.id = s.patient_id").
  263. Where("s.user_org_id=? and s.schedule_date = ? and s.status=1", orgID, scheduleDate).
  264. 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").
  265. Order("schedule_type").
  266. Find(&schedules).Error
  267. for _, item := range schedules {
  268. _, config := FindDoctorAdviceRecordByOrgId(item.UserOrgId)
  269. if config.IsOpenRemind > 0 {
  270. //开启推送提醒逻辑 提交长期处方的时候,弹起长期医嘱推送
  271. var advice_three []*models.DoctorAdvice
  272. advices, _ := GetLastLongAdviceByGroupNoOther(item.UserOrgId, item.PatientId)
  273. advices_two, _ := GetLastLongAdviceByGroupNoThreeOther(item.UserOrgId, item.PatientId)
  274. for _, advice := range advices {
  275. advice.IsCheck = 1
  276. if advice.FrequencyType == 3 {
  277. week := int(scheduleDates.Weekday())
  278. fmt.Println(advice.WeekDay)
  279. switch week {
  280. case 1:
  281. if strings.Index(advice.WeekDay, "周一") > -1 {
  282. advice_three = append(advice_three, advice)
  283. }
  284. break
  285. case 2:
  286. if strings.Index(advice.WeekDay, "周二") > -1 {
  287. advice_three = append(advice_three, advice)
  288. }
  289. break
  290. case 3:
  291. if strings.Index(advice.WeekDay, "周三") > -1 {
  292. advice_three = append(advice_three, advice)
  293. }
  294. break
  295. case 4:
  296. if strings.Index(advice.WeekDay, "周四") > -1 {
  297. advice_three = append(advice_three, advice)
  298. }
  299. break
  300. case 5:
  301. if strings.Index(advice.WeekDay, "周五") > -1 {
  302. advice_three = append(advice_three, advice)
  303. }
  304. break
  305. case 6:
  306. if strings.Index(advice.WeekDay, "周六") > -1 {
  307. advice_three = append(advice_three, advice)
  308. }
  309. break
  310. case 0:
  311. if strings.Index(advice.WeekDay, "周日") > -1 {
  312. advice_three = append(advice_three, advice)
  313. }
  314. break
  315. }
  316. } else if advice.FrequencyType == 1 {
  317. advice_three = append(advice_three, advice)
  318. }
  319. }
  320. for _, advice := range advices_two {
  321. now := scheduleDates.Unix()
  322. dayStr := strconv.FormatInt(advice.DayCount, 10)
  323. dayStr2 := "-" + dayStr
  324. count, _ := strconv.ParseInt(dayStr2, 10, 64)
  325. oldTime := scheduleDates.AddDate(0, 0, int(count)).Unix()
  326. advices, _ := FindAllDoctorAdviceByTimeOther(now, oldTime, item.PatientId, item.UserOrgId, advice.TemplateId)
  327. if len(advices) == 0 {
  328. advice_three = append(advice_three, advice)
  329. }
  330. }
  331. item.DoctorAdvice = append(item.DoctorAdvice, advice_three...)
  332. }
  333. }
  334. return
  335. }
  336. func RemoveRepeatedElement(arr []*models.DoctorAdvice) (newArr []*models.DoctorAdvice) {
  337. newArr = make([]*models.DoctorAdvice, 0)
  338. for i := 0; i < len(arr); i++ {
  339. repeat := false
  340. for j := i + 1; j < len(arr); j++ {
  341. if arr[i].ID == arr[j].ID {
  342. repeat = true
  343. break
  344. }
  345. }
  346. if !repeat {
  347. newArr = append(newArr, arr[i])
  348. }
  349. }
  350. return
  351. }
  352. func GetLastLongAdviceByGroupNoOther(orgId int64, patient_id int64) (advice []*models.DoctorAdvice, err error) {
  353. 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
  354. return
  355. }
  356. func GetLastLongAdviceByGroupNoThreeOther(orgId int64, patient_id int64) (advice []*models.DoctorAdvice, err error) {
  357. 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
  358. return
  359. }
  360. func FindAllDoctorAdviceByTimeOther(now int64, end int64, patient_id int64, orgId int64, temp_id string) (advice []*models.DoctorAdvice, err error) {
  361. 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
  362. return
  363. }
  364. func FindSchPatientNameByName(name string, user_org_id int64) (patient []*models.Patients, err error) {
  365. err = readDb.Model(&models.Patients{}).Where("user_org_id = ? AND name = ? AND status = 1", user_org_id, name).Find(&patient).Error
  366. return
  367. }
  368. func UpdateScheduleStatus(date int64, org_id int64) (err error) {
  369. 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
  370. return
  371. }
  372. func FindDeviceZoneByName(name string, org_id int64) (zone models.DeviceZone, err error) {
  373. err = readDb.Model(&models.DeviceZone{}).Where("org_id = ? AND name = ? AND status = 1", org_id, name).First(&zone).Error
  374. return
  375. }
  376. func FindDeviceByName(device_name string, org_id int64, zone_id int64) (devices []*models.DeviceNumber, err error) {
  377. 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
  378. return
  379. }
  380. func FindSchedualExportLog(org_id int64, export_time int64) (errLogs []*models.ExportErrLog, err error) {
  381. err = readDb.Model(&models.ExportErrLog{}).Where("user_org_id = ? AND export_time = ? AND log_type = 2", org_id, export_time).Find(&errLogs).Error
  382. return
  383. }
  384. func FindDeviceByNameTwo(device_name string, org_id int64, zone_id int64) (devices []*models.DeviceNumber, err error) {
  385. 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
  386. return
  387. }
  388. func UpdateScheduleTemplateStatus(org_id int64) (err error) {
  389. err = readDb.Model(&models.PatientScheduleTemplateItem{}).Where("org_id = ? AND status = 1", org_id).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  390. return
  391. }
  392. func CreateScheduleTemplate(m *models.PatientScheduleTemplateItem) error {
  393. return writeDb.Model(&models.PatientScheduleTemplateItem{}).Create(&m).Error
  394. }
  395. func FindSchedualTemplateExportLog(org_id int64, export_time int64) (errLogs []*models.ExportErrLog, err error) {
  396. err = readDb.Model(&models.ExportErrLog{}).Where("user_org_id = ? AND export_time = ? AND log_type = 3", org_id, export_time).Find(&errLogs).Error
  397. return
  398. }
  399. func GetDialysisOrderByBedId(orgID, start, bed_id int64, schedule_type int64) (schedule models.DialysisOrder, err error) {
  400. 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
  401. return
  402. }
  403. type VMDeviceNumber struct {
  404. ID int64 `gorm:"column:id" json:"id"`
  405. OrgID int64 `gorm:"column:org_id" json:"-"`
  406. Number string `gorm:"column:number" json:"number"`
  407. GroupID int64 `gorm:"column:group_id" json:"group_id"`
  408. ZoneID int64 `gorm:"column:zone_id" json:"zone_id"`
  409. Status int8 `json:"-"`
  410. CreateTime int64 `gorm:"column:ctime" json:"-"`
  411. ModifyTime int64 `gorm:"column:mtime" json:"-"`
  412. DeviceZone models.DeviceZone `json:"zone" gorm:"foreignkey:ID;AssociationForeignKey:ZoneID;"`
  413. }
  414. func (VMDeviceNumber) TableName() string {
  415. return "xt_device_number"
  416. }
  417. type VMPatientScheduleTemplateItem struct {
  418. ID int64 `gorm:"column:id" json:"id" form:"id"`
  419. OrgID int64 `gorm:"column:org_id" json:"-" form:"org_id"`
  420. TemplateID int64 `gorm:"column:template_id" json:"template_id" form:"template_id"`
  421. DeviceNumberID int64 `gorm:"column:device_number_id" json:"device_number_id" form:"device_number_id"`
  422. PatientID int64 `gorm:"column:patient_id" json:"patient_id" form:"patient_id"`
  423. TreatMode int64 `gorm:"column:treat_mode" json:"treat_mode" form:"treat_mode"` // 治疗模式
  424. Weekday int8 `gorm:"column :weekday" json:"weekday" form:"weekday"` // 周一-周日 1-7
  425. TimeType int8 `gorm:"column:time_type" json:"time_type" form:"time_type"` // 上下晚 123
  426. Status int8 `gorm:"column:status" json:"status" form:"status"`
  427. CreateTime int64 `gorm:"column:ctime" json:"-" form:"ctime"`
  428. ModifyTime int64 `gorm:"column:mtime" json:"-" form:"mtime"`
  429. IsExport int64 `gorm:"column:is_export" json:"is_export" form:"is_export"`
  430. Patient string `gorm:"-" json:"patient" form:"patient"`
  431. DeviceNumber VMDeviceNumber `json:"number" gorm:"foreignkey:ID;AssociationForeignKey:DeviceNumberID;"`
  432. PatientInfectiousDiseases []*models.InfectiousDiseases `json:"patient_contagions" gorm:"foreignkey:PatientId;AssociationForeignKey:PatientId;"`
  433. }
  434. func (VMPatientScheduleTemplateItem) TableName() string {
  435. return "xt_patient_schedule_template_item"
  436. }
  437. func GetTemplateSchedualPatientsByKeywords(keywords string, org_id int64, template_id int64) (schedule []*VMPatientScheduleTemplateItem, err error) {
  438. likeKey := "%" + keywords + "%"
  439. err = readDb.
  440. Table("xt_patient_schedule_template_item as s").
  441. Preload("PatientInfectiousDiseases", "status = 1 ").
  442. Preload("DeviceNumber", func(db *gorm.DB) *gorm.DB {
  443. return db.Where("org_id = ? AND status = 1", org_id).Preload("DeviceZone", "org_id = ? AND status = 1", org_id)
  444. }).
  445. Joins("JOIN xt_patients as p ON p.id = s.patient_id AND (p.name LIKE ? OR p.dialysis_no LIKE ?)", likeKey, likeKey).
  446. Where("s.template_id = ? AND s.org_id=? and s.status=1", template_id, org_id).
  447. Select("s.id, s.org_id, s.template_id, s.device_number_id, s.patient_id, s.treat_mode, s.weekday, s.time_type, s.status, s.ctime, s.mtime, p.name as patient").
  448. Order("weekday asc").
  449. Find(&schedule).Error
  450. return
  451. }
  452. func UpdateNewScheduleTemplateStatus(id int64, org_id int64) (err error) {
  453. err = readDb.Model(&models.PatientScheduleTemplateItem{}).Where("org_id = ? AND id = ?", org_id, id).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  454. return
  455. }