schedule_service.go 40KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978
  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).Order("").Find(&partitions).Error
  13. return
  14. }
  15. func GetSchedulePartitionPanelOne(orgID int64, partionId int64) (partitions []*models.Partition, err error) {
  16. if partionId > 0 {
  17. err = readDb.Model(&models.DeviceZone{}).Preload("Jihaos", "org_id=? and status=1 and zone_id = ?", orgID, partionId).Where("org_id=? and status=1 and id = ?", orgID, partionId).Find(&partitions).Error
  18. }
  19. if partionId == 0 {
  20. err = readDb.Model(&models.DeviceZone{}).Preload("Jihaos", "org_id=? and status=1", orgID).Where("org_id=? and status=1", orgID).Find(&partitions).Error
  21. }
  22. return
  23. }
  24. func GetSchedulePartitionPanelTwo(orgID int64, schIDs []string) (partitions []*models.Partition, err error) {
  25. if len(schIDs) == 0 {
  26. err = readDb.Model(&models.DeviceZone{}).Preload("Jihaos", "org_id=? and status=1", orgID).Where("org_id=? and status=1", orgID).Find(&partitions).Error
  27. }
  28. if len(schIDs) > 0 {
  29. err = readDb.Model(&models.DeviceZone{}).Preload("Jihaos", "org_id=? and status=1 and zone_id in(?)", orgID, schIDs).Where("org_id=? and status=1 and id in(?)", orgID, schIDs).Find(&partitions).Error
  30. }
  31. return
  32. }
  33. func GetPatientWithScheduleAndSolution(orgID int64, keywords string, weekStart, weekEnd, schedule, contagion int64) (patients []*models.Patients, err error) {
  34. db := readDb.Table("xt_patients as p").
  35. Preload("DialysisSolutions", "user_org_id=? and initiate_mode=1 and affirm_state=1 and parent_id=0 and status=1", orgID).
  36. Preload("Schedules", "user_org_id=? and schedule_date>=? and schedule_date<= ? and status=1", orgID, weekStart, weekEnd).
  37. Preload("Contagions", "status=1").
  38. Where("p.user_org_id=? and p.lapseto=1 and p.status=1", orgID)
  39. if len(keywords) > 0 {
  40. likeKey := "%" + keywords + "%"
  41. db = db.Where("p.name LIKE ?", likeKey)
  42. }
  43. if schedule == 1 {
  44. 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())
  45. } else if schedule == 2 {
  46. 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())
  47. }
  48. if contagion > 0 {
  49. 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())
  50. }
  51. err = db.Find(&patients).Error
  52. return
  53. }
  54. func GetWeekSchedule(orgID int64, start, end int64) (schedules []*models.Schedule, err error) {
  55. err = readDb.Table("xt_schedule as s").Preload("PatientInfectiousDiseases", "status = 1 ").
  56. Joins("JOIN xt_patients as p ON p.id = s.patient_id").
  57. Where("s.user_org_id=? and s.schedule_date>=? and s.schedule_date<=? and s.status=1", orgID, start, end).
  58. 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").
  59. Order("updated_time asc").
  60. Find(&schedules).Error
  61. return
  62. }
  63. func GetWeekScheduleOne(orgID int64, start, end int64, partionid int64, scheduletype int64) (schedules []*models.Schedule, err error) {
  64. db := readDb.Table("xt_schedule as s")
  65. if partionid > 0 {
  66. db = db.Where("s.partition_id = ?", partionid)
  67. }
  68. if scheduletype > 0 {
  69. db = db.Where("s.schedule_type = ?", scheduletype)
  70. }
  71. err = db.Preload("PatientInfectiousDiseases", "status = 1 ").
  72. Joins("JOIN xt_patients as p ON p.id = s.patient_id").
  73. Where("s.user_org_id=? and s.schedule_date>=? and s.schedule_date<=? and s.status=1", orgID, start, end).
  74. 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").
  75. Order("updated_time asc").
  76. Find(&schedules).Error
  77. return
  78. }
  79. func GetWeekScheduleTwo(orgID int64, start, end int64, schIds []string, scheduletype int64) (schedules []*models.Schedule, err error) {
  80. db := readDb.Table("xt_schedule as s")
  81. fmt.Println("schIds2222222")
  82. if len(schIds) > 0 {
  83. db = db.Where("s.partition_id in (?)", schIds)
  84. }
  85. if scheduletype > 0 {
  86. db = db.Where("s.schedule_type = ?", scheduletype)
  87. }
  88. err = db.Preload("PatientInfectiousDiseases", "status = 1 ").
  89. Joins("JOIN xt_patients as p ON p.id = s.patient_id").
  90. Where("s.user_org_id=? and s.schedule_date>=? and s.schedule_date<=? and s.status=1", orgID, start, end).
  91. 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").
  92. Order("updated_time asc").
  93. Find(&schedules).Error
  94. return
  95. }
  96. func GetDaySchedule(orgID, start, end, patientID int64) (schedule models.Schedule, err error) {
  97. 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
  98. return
  99. }
  100. func GetDayScheduleTwo(orgID, start, patientID int64) (schedule models.Schedule, err error) {
  101. 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
  102. return
  103. }
  104. func GetDayScheduleByBedid(orgID, start, bed_id int64, schedule_type int64) (schedule models.Schedule, err error) {
  105. 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
  106. return
  107. }
  108. func GetPatientScheduleFormDay(orgID, start, end, patientID int64) (schedules []*models.PatientSchedule, err error) {
  109. err = readDb.Table("xt_schedule as s").
  110. Preload("DeviceZone", "org_id=? and status=1", orgID).
  111. Preload("DeviceNumber", "org_id=? and status=1", orgID).
  112. 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).
  113. Find(&schedules).Error
  114. return
  115. }
  116. func GetNextSchedule(orgID, end, patientID int64) (schedules []*models.PatientSchedule, err error) {
  117. err = readDb.Table("xt_schedule as s").
  118. Preload("DeviceZone", "org_id=? and status=1", orgID).
  119. Preload("DeviceNumber", "org_id=? and status=1", orgID).
  120. Where("s.patient_id =? and s.user_org_id=? and s.schedule_date>=? and s.status=1", patientID, orgID, end).
  121. Find(&schedules).Error
  122. return
  123. }
  124. func GetPointSchedule(orgID, scheduleDate, scheduleWeek, scheduleType, bedId int64) (schedule models.Schedule, err error) {
  125. 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
  126. if err == gorm.ErrRecordNotFound {
  127. err = nil
  128. return
  129. }
  130. return
  131. }
  132. func CreateSchedule(m *models.Schedule) error {
  133. return writeDb.Model(&models.Schedule{}).Create(&m).Error
  134. }
  135. func GetTodayScheduleIsExist(patientid int64, schedudate int64, orgid int64, mode_id int64, schedule_type int64) (*models.XtSchedule, error) {
  136. var scheudle models.XtSchedule
  137. var err error
  138. err = XTReadDB().Model(&scheudle).Where("patient_id = ? and schedule_date= ? and user_org_id = ? and status = 1 and mode_id = ? and schedule_type = ?", patientid, schedudate, orgid, mode_id, schedule_type).Find(&scheudle).Error
  139. if err == gorm.ErrRecordNotFound {
  140. return nil, err
  141. }
  142. if err != nil {
  143. return nil, err
  144. }
  145. return &scheudle, nil
  146. }
  147. func UpdateScheduleByOrgId(patientid int64, scheduledate int64, orgid int64, mode_id int64, schedule_type int64, sch *models.XtSchedule) error {
  148. err := XTWriteDB().Model(&sch).Where("patient_id = ? and schedule_date= ? and user_org_id = ? and status = 1 and mode_id = ? and schedule_type = ?", patientid, scheduledate, orgid, mode_id, schedule_type).Updates(map[string]interface{}{"partition_id": sch.PartitionId, "bed_id": sch.BedId, "schedule_type": sch.ScheduleType, "schedule_week": sch.ScheduleWeek, "mode_id": sch.ModeId, "updated_time": time.Now().Unix()}).Error
  149. return err
  150. }
  151. func GetSchedule(orgID, id int64) (*models.Schedule, error) {
  152. var schedule models.Schedule
  153. var err error
  154. err = readDb.Model(&models.Schedule{}).Where("id = ? and user_org_id=? and status=1", id, orgID).First(&schedule).Error
  155. if err != nil {
  156. return nil, err
  157. }
  158. return &schedule, nil
  159. }
  160. func UpdateSchedule(m *models.Schedule) error {
  161. return writeDb.Model(&models.Schedule{}).Save(&m).Error
  162. }
  163. func GetPatientScheduleFormToday(orgId, id, start int64) (schedules []*models.PatientSchedule, err error) {
  164. err = readDb.Table("xt_schedule as s").
  165. Preload("DeviceZone", "org_id=? and status=1", orgId).
  166. Preload("DeviceNumber", "org_id=? and status=1", orgId).
  167. Where("s.patient_id =? and s.user_org_id=? and s.schedule_date>=? and s.status=1", id, orgId, start).
  168. Find(&schedules).Error
  169. return
  170. }
  171. type WeekSchedulePrintVM struct {
  172. ID int64 `gorm:"column:id" json:"id"`
  173. ZoneID int64 `gorm:"column:partition_id" json:"zone_id"`
  174. DeviceNumberID int64 `gorm:"column:bed_id" json:"device_number_id"`
  175. PatientID int64 `gorm:"column:patient_id" json:"patient_id"`
  176. ScheduleDate int64 `gorm:"column:schedule_date" json:"schedule_date"`
  177. ScheduleType int64 `gorm:"column:schedule_type" json:"schedule_type"`
  178. ScheduleWeek int64 `gorm:"column:schedule_week" json:"schedule_week"`
  179. ModeID int64 `gorm:"column:mode_id" json:"mode_id"`
  180. Patient *models.Patients `gorm:"ForeignKey:PatientID" json:"patient"`
  181. DeviceNumber *models.DeviceNumber `gorm:"ForeignKey:DeviceNumberID" json:"device_number"`
  182. Zone *models.DeviceZone `gorm:"ForeignKey:ZoneID" json:"zone"`
  183. Mode *models.TreatmentMode `gorm:"ForeignKey:ModeID" json:"mode"`
  184. }
  185. type PrintCount struct {
  186. ScheduleType int64 `gorm:"column:schedule_type" json:"schedule_type"`
  187. ScheduleWeek int64 `gorm:"column:schedule_week" json:"schedule_week"`
  188. Count int64
  189. }
  190. func GetPrinitWeekSchedules(orgID int64, start, end int64) ([]*WeekSchedulePrintVM, error) {
  191. var schedules []*WeekSchedulePrintVM
  192. err := readDb.Table("xt_schedule as s").
  193. Preload("Patient", "status = 1 and user_org_id = ?", orgID).
  194. Preload("DeviceNumber", "status = 1 and org_id = ?", orgID).
  195. Preload("Zone", "status = 1 and org_id = ?", orgID).
  196. Preload("Mode", "status = 1").
  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.schedule_date <= ? and s.status = 1", orgID, start, end).
  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").
  200. Find(&schedules).Error
  201. return schedules, err
  202. }
  203. func GetPrinitWeekTotal(orgid int64, startime int64, endtime int64) (print []*PrintCount, err error) {
  204. db := XTReadDB().Table("xt_schedule as x").Where("x.status = 1")
  205. if orgid > 0 {
  206. db = db.Where("x.user_org_id = ?", orgid)
  207. }
  208. if startime > 0 {
  209. db = db.Where("x.schedule_date >=?", startime)
  210. }
  211. if endtime > 0 {
  212. db = db.Where("x.schedule_date <=?", endtime)
  213. }
  214. 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
  215. return print, err
  216. }
  217. func GetBloodPrintWeekTotal(orgid int64, startime int64, endtime int64) (print []*PrintCount, err error) {
  218. db := XTReadDB().Table("xt_schedule as x").Where("x.status = 1")
  219. if orgid > 0 {
  220. db = db.Where("x.user_org_id = ?", orgid)
  221. }
  222. if startime > 0 {
  223. db = db.Where("x.schedule_date >=?", startime)
  224. }
  225. if endtime > 0 {
  226. db = db.Where("x.schedule_date <=?", endtime)
  227. }
  228. db = db.Where("x.mode_id = 2")
  229. 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
  230. return print, err
  231. }
  232. func GetPerfusionWeekTotal(orgid int64, startime int64, endtime int64) (print []*PrintCount, err error) {
  233. db := XTReadDB().Table("xt_schedule as x").Where("x.status = 1")
  234. if orgid > 0 {
  235. db = db.Where("x.user_org_id = ?", orgid)
  236. }
  237. if startime > 0 {
  238. db = db.Where("x.schedule_date >=?", startime)
  239. }
  240. if endtime > 0 {
  241. db = db.Where("x.schedule_date <=?", endtime)
  242. }
  243. db = db.Where("x.mode_id = 3")
  244. 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
  245. return print, err
  246. }
  247. // func ExchangeScheduleTimeWithWeekSchedules(orgID int64, nextWeekSchs []*models.Schedule, nextTwoWeekSchs []*models.Schedule) error {
  248. // templateMode, getTemplateModeErr := GetOrgPatientScheduleTemplateMode(orgID)
  249. // if getTemplateModeErr != nil {
  250. // return getTemplateModeErr
  251. // }
  252. // if templateMode == nil {
  253. // return errors.New("模板模式不存在")
  254. // }
  255. // tx := writeDb.Begin()
  256. // updateTime := time.Now().Unix()
  257. // for _, sch := range nextWeekSchs {
  258. // sch.PatientInfectiousDiseases = nil
  259. // date := time.Unix(sch.ScheduleDate, 0)
  260. // sch.ScheduleDate = date.AddDate(0, 0, 7).Unix()
  261. // sch.UpdatedTime = updateTime
  262. // updateSchErr := tx.Save(sch).Error
  263. // if updateSchErr != nil {
  264. // tx.Rollback()
  265. // return updateSchErr
  266. // }
  267. // }
  268. // for _, sch := range nextTwoWeekSchs {
  269. // sch.PatientInfectiousDiseases = nil
  270. // date := time.Unix(sch.ScheduleDate, 0)
  271. // sch.ScheduleDate = date.AddDate(0, 0, -7).Unix()
  272. // sch.UpdatedTime = updateTime
  273. // updateSchErr := tx.Save(sch).Error
  274. // if updateSchErr != nil {
  275. // tx.Rollback()
  276. // return updateSchErr
  277. // }
  278. // }
  279. // templateMode.ModifyTime = updateTime
  280. // templateMode.ExecuteTimes += 1
  281. // updateModeErr := tx.Save(templateMode).Error
  282. // if updateModeErr != nil {
  283. // tx.Rollback()
  284. // return updateModeErr
  285. // }
  286. // tx.Commit()
  287. // return nil
  288. // }
  289. func GetSchedualPatientsByKeywordsAndWeek(keywords string, org_id int64, start_time int64, end_time int64) (schedule []*models.Schedule, err error) {
  290. likeKey := "%" + keywords + "%"
  291. err = readDb.
  292. Table("xt_schedule as s").
  293. Preload("PatientInfectiousDiseases", "status = 1 ").
  294. Preload("DeviceZone", "status = 1 ").
  295. Preload("DeviceNumber", "status = 1 ").
  296. Preload("TreatmentMode", "status = 1 ").
  297. Joins("JOIN xt_patients as p ON p.id = s.patient_id AND (p.name LIKE ? OR p.dialysis_no LIKE ?)", likeKey, likeKey).
  298. Where("s.user_org_id=? and s.schedule_date >= ? and s.schedule_date <= ? and s.status=1", org_id, start_time, end_time).
  299. 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").
  300. Order("schedule_date asc").
  301. Find(&schedule).Error
  302. return
  303. }
  304. func GetSchedualPatientsByKeywords(keywords string, org_id int64) (schedule []*models.Schedule, err error) {
  305. likeKey := "%" + keywords + "%"
  306. err = readDb.
  307. Table("xt_schedule as s").
  308. Preload("PatientInfectiousDiseases", "status = 1 ").
  309. Preload("DeviceZone", "status = 1 ").
  310. Preload("DeviceNumber", "status = 1 ").
  311. Preload("TreatmentMode", "status = 1 ").
  312. Joins("JOIN xt_patients as p ON p.id = s.patient_id AND (p.name LIKE ? OR p.dialysis_no LIKE ?)", likeKey, likeKey).
  313. 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).
  314. 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").
  315. Order("schedule_date asc").
  316. Find(&schedule).Error
  317. return
  318. }
  319. func GetWeekDayScheduleTwo(orgID int64, scheduleDate int64, scheduleDates *time.Time, weektime int64) (schedules []*models.WeekSchedule, err error) {
  320. db := readDb.Table("xt_schedule as s ").Where("s.status =1")
  321. if orgID > 0 {
  322. db = db.Where("s.user_org_id = ?", orgID)
  323. }
  324. if scheduleDate > 0 {
  325. db = db.Where("s.schedule_date = ?", scheduleDate)
  326. }
  327. if weektime > 0 {
  328. db = db.Where("s.schedule_type = ?", weektime)
  329. }
  330. err = db.
  331. Preload("DeviceZone", "status = 1 ").
  332. Preload("DeviceNumber", "status = 1 ").
  333. Preload("DialysisPrescription", func(db *gorm.DB) *gorm.DB {
  334. return db.Where("status = 1").Order("id asc")
  335. }).
  336. Joins("JOIN xt_patients as p ON p.id = s.patient_id").
  337. 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").
  338. Order("s.partition_id asc").
  339. Find(&schedules).Error
  340. for _, item := range schedules {
  341. _, config := FindDoctorAdviceRecordByOrgId(item.UserOrgId)
  342. if config.IsOpenRemind > 0 {
  343. //开启推送提醒逻辑 提交长期处方的时候,弹起长期医嘱推送
  344. var advice_three []*models.DoctorAdvice
  345. advices, _ := GetLastLongAdviceByGroupNoOther(item.UserOrgId, item.PatientId)
  346. advices_two, _ := GetLastLongAdviceByGroupNoThreeOther(item.UserOrgId, item.PatientId)
  347. for _, advice := range advices {
  348. advice.IsCheck = 1
  349. if advice.FrequencyType == 3 {
  350. week := int(scheduleDates.Weekday())
  351. switch week {
  352. case 1:
  353. if strings.Index(advice.WeekDay, "周一") > -1 {
  354. advice_three = append(advice_three, advice)
  355. }
  356. break
  357. case 2:
  358. if strings.Index(advice.WeekDay, "周二") > -1 {
  359. advice_three = append(advice_three, advice)
  360. }
  361. break
  362. case 3:
  363. if strings.Index(advice.WeekDay, "周三") > -1 {
  364. advice_three = append(advice_three, advice)
  365. }
  366. break
  367. case 4:
  368. if strings.Index(advice.WeekDay, "周四") > -1 {
  369. advice_three = append(advice_three, advice)
  370. }
  371. break
  372. case 5:
  373. if strings.Index(advice.WeekDay, "周五") > -1 {
  374. advice_three = append(advice_three, advice)
  375. }
  376. break
  377. case 6:
  378. if strings.Index(advice.WeekDay, "周六") > -1 {
  379. advice_three = append(advice_three, advice)
  380. }
  381. break
  382. case 0:
  383. if strings.Index(advice.WeekDay, "周日") > -1 {
  384. advice_three = append(advice_three, advice)
  385. }
  386. break
  387. }
  388. } else if advice.FrequencyType == 1 {
  389. advice_three = append(advice_three, advice)
  390. }
  391. }
  392. for _, advice := range advices_two {
  393. now := scheduleDates.Unix()
  394. dayStr := strconv.FormatInt(advice.DayCount, 10)
  395. dayStr2 := "-" + dayStr
  396. count, _ := strconv.ParseInt(dayStr2, 10, 64)
  397. oldTime := scheduleDates.AddDate(0, 0, int(count)).Unix()
  398. advices, _ := FindAllDoctorAdviceByTimeOther(now, oldTime, item.PatientId, item.UserOrgId, advice.TemplateId)
  399. if len(advices) == 0 {
  400. advice_three = append(advice_three, advice)
  401. }
  402. }
  403. item.DoctorAdvice = append(item.DoctorAdvice, advice_three...)
  404. }
  405. }
  406. return
  407. }
  408. func GetWeekDaySchedule(orgID int64, scheduleDate int64, scheduleDates *time.Time) (schedules []*models.WeekSchedule, err error) {
  409. err = readDb.
  410. Table("xt_schedule as s").
  411. Preload("DeviceZone", "status = 1 ").
  412. Preload("DeviceNumber", "status = 1 ").
  413. Preload("DialysisPrescription", func(db *gorm.DB) *gorm.DB {
  414. return db.Where("status = 1").Order("id asc")
  415. }).
  416. Joins("JOIN xt_patients as p ON p.id = s.patient_id").
  417. Where("s.user_org_id=? and s.schedule_date = ? and s.status=1", orgID, scheduleDate).
  418. 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").
  419. Order("schedule_type").
  420. Find(&schedules).Error
  421. for _, item := range schedules {
  422. _, config := FindDoctorAdviceRecordByOrgId(item.UserOrgId)
  423. if config.IsOpenRemind > 0 {
  424. //开启推送提醒逻辑 提交长期处方的时候,弹起长期医嘱推送
  425. var advice_three []*models.DoctorAdvice
  426. advices, _ := GetLastLongAdviceByGroupNoOther(item.UserOrgId, item.PatientId)
  427. advices_two, _ := GetLastLongAdviceByGroupNoThreeOther(item.UserOrgId, item.PatientId)
  428. for _, advice := range advices {
  429. advice.IsCheck = 1
  430. if advice.FrequencyType == 3 {
  431. week := int(scheduleDates.Weekday())
  432. fmt.Println(advice.WeekDay)
  433. switch week {
  434. case 1:
  435. if strings.Index(advice.WeekDay, "周一") > -1 {
  436. advice_three = append(advice_three, advice)
  437. }
  438. break
  439. case 2:
  440. if strings.Index(advice.WeekDay, "周二") > -1 {
  441. advice_three = append(advice_three, advice)
  442. }
  443. break
  444. case 3:
  445. if strings.Index(advice.WeekDay, "周三") > -1 {
  446. advice_three = append(advice_three, advice)
  447. }
  448. break
  449. case 4:
  450. if strings.Index(advice.WeekDay, "周四") > -1 {
  451. advice_three = append(advice_three, advice)
  452. }
  453. break
  454. case 5:
  455. if strings.Index(advice.WeekDay, "周五") > -1 {
  456. advice_three = append(advice_three, advice)
  457. }
  458. break
  459. case 6:
  460. if strings.Index(advice.WeekDay, "周六") > -1 {
  461. advice_three = append(advice_three, advice)
  462. }
  463. break
  464. case 0:
  465. if strings.Index(advice.WeekDay, "周日") > -1 {
  466. advice_three = append(advice_three, advice)
  467. }
  468. break
  469. }
  470. } else if advice.FrequencyType == 1 {
  471. advice_three = append(advice_three, advice)
  472. }
  473. }
  474. for _, advice := range advices_two {
  475. now := scheduleDates.Unix()
  476. dayStr := strconv.FormatInt(advice.DayCount, 10)
  477. dayStr2 := "-" + dayStr
  478. count, _ := strconv.ParseInt(dayStr2, 10, 64)
  479. oldTime := scheduleDates.AddDate(0, 0, int(count)).Unix()
  480. advices, _ := FindAllDoctorAdviceByTimeOther(now, oldTime, item.PatientId, item.UserOrgId, advice.TemplateId)
  481. if len(advices) == 0 {
  482. advice_three = append(advice_three, advice)
  483. }
  484. }
  485. item.DoctorAdvice = append(item.DoctorAdvice, advice_three...)
  486. }
  487. }
  488. return
  489. }
  490. func GetWeekDayScheduleById(orgid int64, scheduleDate int64, scheduleType int64, zone int64) (list []*models.VmSchedulesRemind, err error) {
  491. db := XTReadDB().Table("xt_schedule as x").Where("x.status = 1")
  492. if orgid > 0 {
  493. db = db.Where("x.user_org_id = ?", orgid)
  494. }
  495. if scheduleDate > 0 {
  496. db = db.Where("x.schedule_date = ?", scheduleDate)
  497. }
  498. if scheduleType > 0 {
  499. db = db.Where("x.schedule_type = ?", scheduleType)
  500. }
  501. if zone > 0 {
  502. db = db.Where("x.partition_id = ?", zone)
  503. }
  504. err = db.Select("x.id,x.user_org_id,x.partition_id,x.bed_id,x.patient_id,x.schedule_date,x.schedule_type,x.schedule_week,x.mode_id,x.is_export").Preload("XtPatients", "status = 1").Preload("DeviceZone", "status = 1").Preload("DeviceNumber", "status = 1").Preload("DialysisOrder", "status = 1").Preload("DoctorAdvice", func(db *gorm.DB) *gorm.DB {
  505. return db.Where("status =1 and parent_id = 0").Preload("ChildDoctorAdvice", "status = 1")
  506. }).Preload("HisDoctorAdviceInfo", "status = 1").Preload("DialysisPrescription", "status = 1").Preload("DialysisSolution", func(db *gorm.DB) *gorm.DB {
  507. return db.Where("status =1").Order("id asc")
  508. }).Find(&list).Error
  509. return list, err
  510. }
  511. func GetWeekDayScheduleByIdThee(orgid int64, scheduleDate int64, scheduleType int64, zone []string) (list []*models.VmSchedulesRemind, err error) {
  512. db := XTReadDB().Table("xt_schedule as x").Where("x.status = 1")
  513. if orgid > 0 {
  514. db = db.Where("x.user_org_id = ?", orgid)
  515. }
  516. if scheduleDate > 0 {
  517. db = db.Where("x.schedule_date = ?", scheduleDate)
  518. }
  519. if scheduleType > 0 {
  520. db = db.Where("x.schedule_type = ?", scheduleType)
  521. }
  522. if len(zone) > 0 {
  523. db = db.Where("x.partition_id in (?)", zone)
  524. }
  525. err = db.Select("x.id,x.user_org_id,x.partition_id,x.bed_id,x.patient_id,x.schedule_date,x.schedule_type,x.schedule_week,x.mode_id,x.is_export").Preload("XtPatients", "status = 1").Preload("DeviceZone", "status = 1").Preload("DeviceNumber", "status = 1").Preload("DialysisOrder", "status = 1").Preload("DoctorAdvice", func(db *gorm.DB) *gorm.DB {
  526. return db.Where("status =1 and parent_id = 0").Preload("ChildDoctorAdvice", "status = 1")
  527. }).Preload("HisDoctorAdviceInfo", "status = 1").Preload("DialysisPrescription", "status = 1").Preload("DialysisSolution", func(db *gorm.DB) *gorm.DB {
  528. return db.Where("status =1").Order("id asc")
  529. }).Find(&list).Error
  530. return list, err
  531. }
  532. func GetWeekDayScheduleByIdTwo(orgid int64, scheduleDate int64, scheduleType int64, startDate int64, zone []string) (list []*models.VmSchedulesRemind, err error) {
  533. db := XTReadDB().Table("xt_schedule as x").Where("x.status = 1")
  534. if orgid > 0 {
  535. db = db.Where("x.user_org_id = ?", orgid)
  536. }
  537. if scheduleDate > 0 {
  538. db = db.Where("x.schedule_date >=? and x.schedule_date<=?", startDate, scheduleDate)
  539. }
  540. if scheduleType > 0 {
  541. db = db.Where("x.schedule_type = ?", scheduleType)
  542. }
  543. if len(zone) > 0 {
  544. db = db.Where("x.partition_id in (?)", zone)
  545. }
  546. err = db.Select("x.id,x.user_org_id,x.partition_id,x.bed_id,x.patient_id,x.schedule_date,x.schedule_type,x.schedule_week,x.mode_id,x.is_export").Preload("XtPatients", "status = 1").Preload("DeviceZone", "status = 1").Preload("DeviceNumber", "status = 1").Preload("DialysisOrder", "status = 1").Preload("DoctorAdvice", func(db *gorm.DB) *gorm.DB {
  547. return db.Where("status =1 and parent_id = 0").Preload("ChildDoctorAdvice", "status = 1")
  548. }).Preload("HisDoctorAdviceInfo", "status = 1").Preload("DialysisPrescription", "status = 1").Preload("DialysisSolution", func(db *gorm.DB) *gorm.DB {
  549. return db.Where("status =1").Order("id asc")
  550. }).Find(&list).Error
  551. return list, err
  552. }
  553. func RemoveRepeatedElement(arr []*models.DoctorAdvice) (newArr []*models.DoctorAdvice) {
  554. newArr = make([]*models.DoctorAdvice, 0)
  555. for i := 0; i < len(arr); i++ {
  556. repeat := false
  557. for j := i + 1; j < len(arr); j++ {
  558. if arr[i].ID == arr[j].ID {
  559. repeat = true
  560. break
  561. }
  562. }
  563. if !repeat {
  564. newArr = append(newArr, arr[i])
  565. }
  566. }
  567. return
  568. }
  569. func GetLastLongAdviceByGroupNoOther(orgId int64, patient_id int64) (advice []*models.DoctorAdvice, err error) {
  570. 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
  571. return
  572. }
  573. func GetLastLongAdviceByGroupNoThreeOther(orgId int64, patient_id int64) (advice []*models.DoctorAdvice, err error) {
  574. 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
  575. return
  576. }
  577. func FindAllDoctorAdviceByTimeOther(now int64, end int64, patient_id int64, orgId int64, temp_id string) (advice []*models.DoctorAdvice, err error) {
  578. 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
  579. return
  580. }
  581. func FindSchPatientNameByName(name string, user_org_id int64) (patient []*models.Patients, err error) {
  582. err = readDb.Model(&models.Patients{}).Where("user_org_id = ? AND name = ? AND status = 1", user_org_id, name).Find(&patient).Error
  583. return
  584. }
  585. func UpdateScheduleStatus(date int64, org_id int64) (err error) {
  586. 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
  587. return
  588. }
  589. func FindDeviceZoneByName(name string, org_id int64) (zone models.DeviceZone, err error) {
  590. err = readDb.Model(&models.DeviceZone{}).Where("org_id = ? AND name = ? AND status = 1", org_id, name).First(&zone).Error
  591. return
  592. }
  593. func FindDeviceByName(device_name string, org_id int64, zone_id int64) (devices []*models.DeviceNumber, err error) {
  594. 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
  595. return
  596. }
  597. func FindSchedualExportLog(org_id int64, export_time int64) (errLogs []*models.ExportErrLog, err error) {
  598. err = readDb.Model(&models.ExportErrLog{}).Where("user_org_id = ? AND export_time = ? AND log_type = 2", org_id, export_time).Find(&errLogs).Error
  599. return
  600. }
  601. func FindDeviceByNameTwo(device_name string, org_id int64, zone_id int64) (devices []*models.DeviceNumber, err error) {
  602. 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
  603. return
  604. }
  605. func UpdateScheduleTemplateStatus(org_id int64) (err error) {
  606. err = readDb.Model(&models.PatientScheduleTemplateItem{}).Where("org_id = ? AND status = 1", org_id).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  607. return
  608. }
  609. func CreateScheduleTemplate(m *models.PatientScheduleTemplateItem) error {
  610. return writeDb.Model(&models.PatientScheduleTemplateItem{}).Create(&m).Error
  611. }
  612. func FindSchedualTemplateExportLog(org_id int64, export_time int64) (errLogs []*models.ExportErrLog, err error) {
  613. err = readDb.Model(&models.ExportErrLog{}).Where("user_org_id = ? AND export_time = ? AND log_type = 3", org_id, export_time).Find(&errLogs).Error
  614. return
  615. }
  616. func GetDialysisOrderByBedId(orgID, start, bed_id int64, schedule_type int64) (schedule models.DialysisOrder, err error) {
  617. 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
  618. return
  619. }
  620. type VMDeviceNumber struct {
  621. ID int64 `gorm:"column:id" json:"id"`
  622. OrgID int64 `gorm:"column:org_id" json:"-"`
  623. Number string `gorm:"column:number" json:"number"`
  624. GroupID int64 `gorm:"column:group_id" json:"group_id"`
  625. ZoneID int64 `gorm:"column:zone_id" json:"zone_id"`
  626. Status int8 `json:"-"`
  627. CreateTime int64 `gorm:"column:ctime" json:"-"`
  628. ModifyTime int64 `gorm:"column:mtime" json:"-"`
  629. DeviceZone models.DeviceZone `json:"zone" gorm:"foreignkey:ID;AssociationForeignKey:ZoneID;"`
  630. }
  631. func (VMDeviceNumber) TableName() string {
  632. return "xt_device_number"
  633. }
  634. type VMPatientScheduleTemplateItem struct {
  635. ID int64 `gorm:"column:id" json:"id" form:"id"`
  636. OrgID int64 `gorm:"column:org_id" json:"-" form:"org_id"`
  637. TemplateID int64 `gorm:"column:template_id" json:"template_id" form:"template_id"`
  638. DeviceNumberID int64 `gorm:"column:device_number_id" json:"device_number_id" form:"device_number_id"`
  639. PatientID int64 `gorm:"column:patient_id" json:"patient_id" form:"patient_id"`
  640. TreatMode int64 `gorm:"column:treat_mode" json:"treat_mode" form:"treat_mode"` // 治疗模式
  641. Weekday int8 `gorm:"column :weekday" json:"weekday" form:"weekday"` // 周一-周日 1-7
  642. TimeType int8 `gorm:"column:time_type" json:"time_type" form:"time_type"` // 上下晚 123
  643. Status int8 `gorm:"column:status" json:"status" form:"status"`
  644. CreateTime int64 `gorm:"column:ctime" json:"-" form:"ctime"`
  645. ModifyTime int64 `gorm:"column:mtime" json:"-" form:"mtime"`
  646. IsExport int64 `gorm:"column:is_export" json:"is_export" form:"is_export"`
  647. Patient string `gorm:"-" json:"patient" form:"patient"`
  648. DeviceNumber VMDeviceNumber `json:"number" gorm:"foreignkey:ID;AssociationForeignKey:DeviceNumberID;"`
  649. PatientInfectiousDiseases []*models.InfectiousDiseases `json:"patient_contagions" gorm:"foreignkey:PatientId;AssociationForeignKey:PatientId;"`
  650. }
  651. func (VMPatientScheduleTemplateItem) TableName() string {
  652. return "xt_patient_schedule_template_item"
  653. }
  654. func GetTemplateSchedualPatientsByKeywords(keywords string, org_id int64, template_id int64) (schedule []*VMPatientScheduleTemplateItem, err error) {
  655. likeKey := "%" + keywords + "%"
  656. err = readDb.
  657. Table("xt_patient_schedule_template_item as s").
  658. Preload("PatientInfectiousDiseases", "status = 1 ").
  659. Preload("DeviceNumber", func(db *gorm.DB) *gorm.DB {
  660. return db.Where("org_id = ? AND status = 1", org_id).Preload("DeviceZone", "org_id = ? AND status = 1", org_id)
  661. }).
  662. Joins("JOIN xt_patients as p ON p.id = s.patient_id AND (p.name LIKE ? OR p.dialysis_no LIKE ?)", likeKey, likeKey).
  663. Where("s.template_id = ? AND s.org_id=? and s.status=1", template_id, org_id).
  664. 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").
  665. Order("weekday asc").
  666. Find(&schedule).Error
  667. return
  668. }
  669. func UpdateNewScheduleTemplateStatus(id int64, org_id int64) (err error) {
  670. err = readDb.Model(&models.PatientScheduleTemplateItem{}).Where("org_id = ? AND id = ?", org_id, id).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  671. return
  672. }
  673. func GetNextWeekDaySchedule(weektype int64, weektime int64, startime int64, endtime int64, orgID int64, zone []string) (schedules []*models.WeekSchedule, err error) {
  674. db := readDb.Table("xt_schedule as s ").Where("s.status =1")
  675. if orgID > 0 {
  676. db = db.Where("s.user_org_id = ?", orgID)
  677. }
  678. if startime > 0 {
  679. db = db.Where("s.schedule_date >= ?", startime)
  680. }
  681. if endtime > 0 {
  682. db = db.Where("s.schedule_date <= ?", endtime)
  683. }
  684. if weektime > 0 {
  685. db = db.Where("s.schedule_type = ?", weektime)
  686. }
  687. if weektype > 0 {
  688. db = db.Where("s.schedule_week = ?", weektype)
  689. }
  690. if len(zone) > 0 {
  691. db = db.Where("s.partition_id in (?)", zone)
  692. }
  693. err = db.
  694. Preload("DeviceZone", "status = 1 ").
  695. Preload("DeviceNumber", "status = 1 ").
  696. Preload("DialysisPrescription", func(db *gorm.DB) *gorm.DB {
  697. return db.Where("status = 1").Order("id asc")
  698. }).Preload("DialysisSolution", func(db *gorm.DB) *gorm.DB {
  699. return db.Where("status = 1").Order("id asc")
  700. }).Preload("DoctorAdvice", func(db *gorm.DB) *gorm.DB {
  701. return db.Where("status =1 and parent_id = 0").Preload("ChildDoctorAdvice", "status = 1")
  702. }).Preload("HisDoctorAdviceInfo", "status = 1").Joins("JOIN xt_patients as p ON p.id = s.patient_id").
  703. 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").
  704. Order("s.partition_id asc").
  705. Find(&schedules).Error
  706. return schedules, err
  707. }
  708. func GetThreeWeekList(startime int64, endtime int64, orgid int64, scheduletype int64, partionid int64) (schedule []*models.VmSchedule, err error) {
  709. db := XTReadDB().Table("xt_schedule as x").Where("x.status =1")
  710. if startime > 0 {
  711. db = db.Where("x.schedule_date >=?", startime)
  712. }
  713. if endtime > 0 {
  714. db = db.Where("x.schedule_date <=?", endtime)
  715. }
  716. if orgid > 0 {
  717. db = db.Where("x.user_org_id = ?", orgid)
  718. }
  719. if scheduletype > 0 {
  720. db = db.Where("x.schedule_type = ?", scheduletype)
  721. }
  722. if partionid > 0 {
  723. db = db.Where("x.partition_id = ?", partionid)
  724. }
  725. err = db.Select("x.schedule_date,x.schedule_type,x.schedule_week,x.bed_id,x.partition_id,t.name,n.number,z.name").Joins("left join xt_patients as t on t.id = x.patient_id and t.user_org_id = ? and t.status = 1", orgid).Joins("left join xt_device_zone as z on z.id = x.partition_id and z.org_id = ? and z.status =1", orgid).Joins("left join xt_device_number as n on n.id = x.bed_id and n.org_id = ? and n.status =1", orgid).Scan(&schedule).Error
  726. return schedule, err
  727. }
  728. func UpdatedDialysisPrescription(prescription *models.DialysisPrescription, recordDate int64, patientid int64, orgid int64) error {
  729. err := XTWriteDB().Model(&prescription).Where("record_date = ? and patient_id = ? and user_org_id = ?", recordDate, patientid, orgid).Update(map[string]interface{}{"mode_id": prescription.ModeId, "updated_time": time.Now().Unix()}).Error
  730. return err
  731. }
  732. func GetWeekSchedules(orgid int64, startime int64, endtime int64) (scheudle []*models.XtSchedule, err error) {
  733. err = XTReadDB().Model(&scheudle).Where("user_org_id = ? and status = 1 and schedule_date>=? and schedule_date<=?", orgid, startime, endtime).Find(&scheudle).Error
  734. return scheudle, err
  735. }
  736. func GetLastWeekSchedule(orgid int64, startime int64, endtime int64) (*models.XtSchedule, error) {
  737. schedule := models.XtSchedule{}
  738. err := XTReadDB().Where("user_org_id = ? and status =1 and schedule_date>=? and schedule_date<=?", orgid, startime, endtime).Find(&schedule).Error
  739. if err == gorm.ErrRecordNotFound {
  740. return nil, err
  741. }
  742. if err != nil {
  743. return nil, err
  744. }
  745. return &schedule, nil
  746. }
  747. func AddPatientSchedule(schedule *models.XtSchedule) error {
  748. utx := XTWriteDB().Begin()
  749. err := utx.Create(&schedule).Error
  750. utx.Commit()
  751. return err
  752. }
  753. func UpdatePatientSchedule(orgid int64, startime int64, endtime int64) error {
  754. schedule := models.XtSchedule{}
  755. err := XTWriteDB().Model(&schedule).Where("user_org_id = ? and schedule_date>=? and schedule_date<=? and status =1", orgid, startime, endtime).Updates(map[string]interface{}{"status": 0}).Error
  756. return err
  757. }
  758. func GetSettingById(id int64) (*models.XtRemindPrintSetting, error) {
  759. setting := models.XtRemindPrintSetting{}
  760. err := XTReadDB().Model(&setting).Where("id = ? and status = 1", id).Find(&setting).Error
  761. if err == gorm.ErrRecordNotFound {
  762. return nil, err
  763. }
  764. if err != nil {
  765. return nil, err
  766. }
  767. return &setting, nil
  768. }
  769. func CreateSetting(setting *models.XtRemindPrintSetting) error {
  770. err := XTReadDB().Create(&setting).Error
  771. return err
  772. }
  773. func UpdatedRemindPrint(set *models.XtRemindPrintSetting, id int64) error {
  774. err := XTWriteDB().Model(&set).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"prescription_status": set.PrescriptionStatus, "week": set.Week, "name": set.Name, "zone": set.Zone, "classes": set.Classes, "number": set.Number, "mode": set.Mode, "dialyzers": set.Dialyzers, "perfusion_apparatus": set.PerfusionApparatus, "anticoagulant": set.Anticoagulant, "doctor_advice": set.DoctorAdvice, "anticoagulant_zongliang": set.AnticoagulantZongliang}).Error
  775. return err
  776. }
  777. func GetRemindPrintList(id int64) (models.XtRemindPrintSetting, error) {
  778. setting := models.XtRemindPrintSetting{}
  779. err := XTReadDB().Model(&setting).Where("user_org_id = ? and status =1", id).Find(&setting).Error
  780. return setting, err
  781. }
  782. func GetScheudleTemplateById(orgid int64) (*models.ScheduleTemplate, error) {
  783. schedule := models.ScheduleTemplate{}
  784. err := XTReadDB().Model(&schedule).Where("user_org_id = ? and status = 1", orgid).Find(&schedule).Error
  785. if err == gorm.ErrRecordNotFound {
  786. return nil, err
  787. }
  788. if err != nil {
  789. return nil, err
  790. }
  791. return &schedule, nil
  792. }
  793. func CreateXtscheduleTemplate(template *models.ScheduleTemplate) error {
  794. err := XTWriteDB().Create(&template).Error
  795. return err
  796. }
  797. func UpdateXtScheduleTemplate(template *models.ScheduleTemplate, orgid int64) error {
  798. err := XTWriteDB().Model(&template).Where("user_org_id = ? and status = 1", orgid).Updates(map[string]interface{}{"template_id": template.TemplateId}).Error
  799. return err
  800. }
  801. func GetScheduleTemplate(orgid int64) (models.ScheduleTemplate, error) {
  802. template := models.ScheduleTemplate{}
  803. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&template).Error
  804. return template, err
  805. }
  806. func GetDataPrintByOrgId(orgid int64) (*models.XtDataPrint, error) {
  807. dataPrint := models.XtDataPrint{}
  808. err := XTReadDB().Model(&dataPrint).Where("user_org_id = ? and status = 1", orgid).Find(&dataPrint).Error
  809. if err == gorm.ErrRecordNotFound {
  810. return nil, err
  811. }
  812. if err != nil {
  813. return nil, err
  814. }
  815. return &dataPrint, nil
  816. }
  817. func CreateDataPrint(dataPrint *models.XtDataPrint) error {
  818. err := XTWriteDB().Create(&dataPrint).Error
  819. return err
  820. }
  821. func UpdteDataPrint(orgid int64, dataPrint *models.XtDataPrint) error {
  822. err := XTWriteDB().Model(&dataPrint).Where("user_org_id = ? and status = 1", orgid).Updates(map[string]interface{}{"is_open": dataPrint.IsOpen}).Error
  823. return err
  824. }
  825. func GetDataPrint(orgid int64) (models.XtDataPrint, error) {
  826. dataPrint := models.XtDataPrint{}
  827. err := XTReadDB().Model(&dataPrint).Where("user_org_id = ? and status = 1", orgid).Find(&dataPrint).Error
  828. return dataPrint, err
  829. }