schedule_service.go 53KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260
  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).Order("").Find(&partitions).Error
  13. return
  14. }
  15. func GetSchedulePartitionPanelOne(orgID int64, partionId []string) (partitions []*models.Partition, err error) {
  16. //if len(partionId) > 0 {
  17. // err = readDb.Model(&models.DeviceZone{}).Preload("Jihaos", "org_id=? and status=1 and zone_id in (?)", orgID, partionId).Where("org_id=? and status=1 and id ?", orgID, partionId).Find(&partitions).Error
  18. //}
  19. //if len(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 GetPatientWithScheduleAndSolutionTwo(orgID int64, keywords string, weekStart, weekEnd, schedule, contagion int64) (patients []*models.Patients, err error) {
  55. db := readDb.Table("xt_patients as p").
  56. Preload("DialysisSolutions", "user_org_id=? and initiate_mode=1 and affirm_state=1 and parent_id=0 and status=1", orgID).
  57. //Preload("Schedules", "user_org_id=? and schedule_date>=? and schedule_date<= ? and status=1", orgID, weekStart, weekEnd).
  58. Where("p.user_org_id=? and p.lapseto=1 and p.status=1", orgID)
  59. if len(keywords) > 0 {
  60. likeKey := "%" + keywords + "%"
  61. db = db.Where("p.name LIKE ?", likeKey)
  62. }
  63. err = db.Find(&patients).Error
  64. return
  65. }
  66. func GetWeekSchedule(orgID int64, start, end int64) (schedules []*models.Schedule, err error) {
  67. err = readDb.Table("xt_schedule as s").Preload("PatientInfectiousDiseases", "status = 1 ").
  68. Joins("JOIN xt_patients as p ON p.id = s.patient_id").
  69. Where("s.user_org_id=? and s.schedule_date>=? and s.schedule_date<=? and s.status=1", orgID, start, end).
  70. 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,s.dialysis_machine_name, p.name as patient").
  71. Order("updated_time asc").
  72. Find(&schedules).Error
  73. return
  74. }
  75. func GetWeekScheduleOne(orgID int64, start, end int64, partionid int64, scheduletype int64) (schedules []*models.Schedule, err error) {
  76. db := readDb.Table("xt_schedule as s")
  77. if partionid > 0 {
  78. db = db.Where("s.partition_id = ?", partionid)
  79. }
  80. if scheduletype > 0 {
  81. db = db.Where("s.schedule_type = ?", scheduletype)
  82. }
  83. err = db.Preload("PatientInfectiousDiseases", "status = 1 ").
  84. Joins("JOIN xt_patients as p ON p.id = s.patient_id").
  85. Where("s.user_org_id=? and s.schedule_date>=? and s.schedule_date<=? and s.status=1", orgID, start, end).
  86. 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.dialysis_machine_name, s.updated_time, p.name as patient").
  87. Order("updated_time asc").
  88. Find(&schedules).Error
  89. return
  90. }
  91. func GetWeekScheduleTwo(orgID int64, start, end int64, schIds []string, scheduletype int64) (schedules []*models.Schedule, err error) {
  92. db := readDb.Table("xt_schedule as s")
  93. fmt.Println("schIds2222222")
  94. if len(schIds) > 0 {
  95. db = db.Where("s.partition_id in (?)", schIds)
  96. }
  97. if scheduletype > 0 {
  98. db = db.Where("s.schedule_type = ?", scheduletype)
  99. }
  100. err = db.Preload("PatientInfectiousDiseases", "status = 1 ").
  101. Joins("JOIN xt_patients as p ON p.id = s.patient_id").
  102. Where("s.user_org_id=? and s.schedule_date>=? and s.schedule_date<=? and s.status=1", orgID, start, end).
  103. 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,s.dialysis_machine_name, p.name as patient").
  104. Order("updated_time asc").
  105. Find(&schedules).Error
  106. return
  107. }
  108. func GetDaySchedule(orgID, start, end, patientID int64) (schedule models.Schedule, err error) {
  109. 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
  110. return
  111. }
  112. func GetDayScheduleTwo(orgID, start, patientID int64) (schedule models.Schedule, err error) {
  113. 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
  114. return
  115. }
  116. func GetDayScheduleByBedid(orgID, start, bed_id int64, schedule_type int64) (schedule models.Schedule, err error) {
  117. 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
  118. return
  119. }
  120. func GetSourceSch(orgId int64, patient_id int64, record_date int64) (schedule models.Schedule, err error) {
  121. err = readDb.Model(&models.Schedule{}).Where("user_org_id=? and schedule_date=? and patient_id =? and status=1", orgId, record_date, patient_id).First(&schedule).Error
  122. return
  123. }
  124. func GetPatientScheduleFormDay(orgID, start, end, patientID int64) (schedules []*models.PatientSchedule, err error) {
  125. err = readDb.Table("xt_schedule as s").
  126. Preload("DeviceZone", "org_id=? and status=1", orgID).
  127. Preload("DeviceNumber", "org_id=? and status=1", orgID).
  128. 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).
  129. Find(&schedules).Error
  130. return
  131. }
  132. func GetNextSchedule(orgID, end, patientID int64) (schedules []*models.PatientSchedule, err error) {
  133. err = readDb.Table("xt_schedule as s").
  134. Preload("DeviceZone", "org_id=? and status=1", orgID).
  135. Preload("DeviceNumber", "org_id=? and status=1", orgID).
  136. Where("s.patient_id =? and s.user_org_id=? and s.schedule_date>=? and s.status=1", patientID, orgID, end).
  137. Find(&schedules).Error
  138. return
  139. }
  140. func GetPointSchedule(orgID, scheduleDate, scheduleWeek, scheduleType, bedId int64) (schedule models.Schedule, err error) {
  141. 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
  142. if err == gorm.ErrRecordNotFound {
  143. err = nil
  144. return
  145. }
  146. return
  147. }
  148. func CreateSchedule(m *models.Schedule) error {
  149. utx := XTWriteDB().Begin()
  150. err := utx.Create(&m).Error
  151. utx.Commit()
  152. return err
  153. }
  154. func DeleteScheduleExist(scheduleData int64, user_org_id int64) (models.XtSchedule, error) {
  155. schedule := models.XtSchedule{}
  156. err := XTWriteDB().Model(&schedule).Where("schedule_date= ? and user_org_id = ? and status = 1", scheduleData, user_org_id).Updates(map[string]interface{}{"status": 0}).Error
  157. return schedule, err
  158. }
  159. func GetTodayScheduleIsExist(patientid int64, schedudate int64, orgid int64, mode_id int64, schedule_type int64) (*models.XtSchedule, error) {
  160. var scheudle models.XtSchedule
  161. var err error
  162. 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
  163. if err == gorm.ErrRecordNotFound {
  164. return nil, err
  165. }
  166. if err != nil {
  167. return nil, err
  168. }
  169. return &scheudle, nil
  170. }
  171. func GetTodayScheduleIsExistOne(patientid int64, schedudate int64, orgid int64) (*models.XtSchedule, error) {
  172. var scheudle models.XtSchedule
  173. var err error
  174. err = XTReadDB().Model(&scheudle).Where("patient_id = ? and schedule_date= ? and user_org_id = ? and status = 1", patientid, schedudate, orgid).Find(&scheudle).Error
  175. if err == gorm.ErrRecordNotFound {
  176. return nil, err
  177. }
  178. if err != nil {
  179. return nil, err
  180. }
  181. return &scheudle, nil
  182. }
  183. func GetPatientByBed(schedule_date int64, bed_id int64, scheduletype int64, orgid int64) (*models.XtSchedule, error) {
  184. var scheudle models.XtSchedule
  185. var err error
  186. err = XTReadDB().Model(&scheudle).Where(" schedule_date= ? and bed_id = ? and schedule_type = ? and user_org_id = ? and status = 1", schedule_date, bed_id, scheduletype, orgid).Find(&scheudle).Error
  187. if err == gorm.ErrRecordNotFound {
  188. return nil, err
  189. }
  190. if err != nil {
  191. return nil, err
  192. }
  193. return &scheudle, nil
  194. }
  195. func ModeFyScheduleById(schedule_date int64, bedid int64, scheduletype int64, orgid int64) error {
  196. schedule := models.XtSchedule{}
  197. err := XTWriteDB().Model(&schedule).Where("schedule_date = ? and bed_id =? and schedule_type = ? and status = 1 and user_org_id = ?", schedule_date, bedid, scheduletype, orgid).Updates(map[string]interface{}{"status": 0}).Error
  198. return err
  199. }
  200. func ModeScheduleByTime(startime int64, endtime int64, orgid int64) error {
  201. schedule := models.XtSchedule{}
  202. err := XTWriteDB().Model(&schedule).Where("schedule_date >? and schedule_date<=? and status = 1 and user_org_id = ?", startime, endtime, orgid).Updates(map[string]interface{}{"status": 0}).Error
  203. return err
  204. }
  205. func ModeScheduleByTimeOne(startime int64, endtime int64, orgid int64) error {
  206. schedule := models.XtSchedule{}
  207. err := XTWriteDB().Model(&schedule).Where("schedule_date >=? and schedule_date<=? and status = 1 and user_org_id = ?", startime, endtime, orgid).Updates(map[string]interface{}{"status": 0}).Error
  208. return err
  209. }
  210. func UpdateScheduleByOrgId(patientid int64, scheduledate int64, orgid int64, mode_id int64, schedule_type int64, sch *models.XtSchedule) error {
  211. 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
  212. return err
  213. }
  214. func UpdateScheduleByOrgIdOne(patientid int64, scheduledate int64, orgid int64, sch *models.XtSchedule) error {
  215. err := XTWriteDB().Model(&sch).Where("patient_id = ? and schedule_date= ? and user_org_id = ? and status = 1", patientid, scheduledate, orgid).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
  216. return err
  217. }
  218. func GetSchedule(orgID, id int64) (*models.Schedule, error) {
  219. var schedule models.Schedule
  220. var err error
  221. err = readDb.Model(&models.Schedule{}).Where("id = ? and user_org_id=? and status=1", id, orgID).First(&schedule).Error
  222. if err != nil {
  223. return nil, err
  224. }
  225. return &schedule, nil
  226. }
  227. func GetScheduleNight(orgID, id int64) (*models.Schedule, error) {
  228. var schedule models.Schedule
  229. var err error
  230. err = readDb.Model(&models.Schedule{}).Where("id = ? and user_org_id=?", id, orgID).First(&schedule).Error
  231. if err != nil {
  232. return nil, err
  233. }
  234. return &schedule, nil
  235. }
  236. func UpdateSchedule(m *models.Schedule) error {
  237. utx := XTWriteDB().Begin()
  238. err := utx.Save(&m).Error
  239. utx.Commit()
  240. return err
  241. }
  242. func UpdateScheduleOne(id int64, schedule models.Schedule) error {
  243. utx := XTWriteDB().Begin()
  244. err := XTWriteDB().Model(&schedule).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"partition_id": schedule.PartitionId, "bed_id": schedule.BedId, "schedule_type": schedule.ScheduleType}).Error
  245. utx.Commit()
  246. return err
  247. }
  248. //参数一:当前操作的患者的排班 参数二被替换床位对应的排班
  249. func UpdateScheduleTwo(schone models.Schedule, schtwo models.Schedule) {
  250. utx := XTWriteDB()
  251. var tempPatientID int64
  252. var tempPatientIDTwo int64
  253. tempPatientID = schone.PatientId
  254. tempPatientIDTwo = schtwo.PatientId
  255. schone.PatientId = tempPatientIDTwo
  256. schtwo.PatientId = tempPatientID
  257. utx.Save(&schone)
  258. utx.Save(&schtwo)
  259. }
  260. func GetPatientScheduleFormToday(orgId, id, start int64) (schedules []*models.PatientSchedule, err error) {
  261. err = readDb.Table("xt_schedule as s").
  262. Preload("DeviceZone", "org_id=? and status=1", orgId).
  263. Preload("DeviceNumber", "org_id=? and status=1", orgId).
  264. Where("s.patient_id =? and s.user_org_id=? and s.schedule_date>=? and s.status=1", id, orgId, start).
  265. Find(&schedules).Error
  266. return
  267. }
  268. type WeekSchedulePrintVM struct {
  269. ID int64 `gorm:"column:id" json:"id"`
  270. ZoneID int64 `gorm:"column:partition_id" json:"zone_id"`
  271. DeviceNumberID int64 `gorm:"column:bed_id" json:"device_number_id"`
  272. PatientID int64 `gorm:"column:patient_id" json:"patient_id"`
  273. ScheduleDate int64 `gorm:"column:schedule_date" json:"schedule_date"`
  274. ScheduleType int64 `gorm:"column:schedule_type" json:"schedule_type"`
  275. ScheduleWeek int64 `gorm:"column:schedule_week" json:"schedule_week"`
  276. ModeID int64 `gorm:"column:mode_id" json:"mode_id"`
  277. Patient *models.Patients `gorm:"ForeignKey:PatientID" json:"patient"`
  278. DeviceNumber *models.DeviceNumber `gorm:"ForeignKey:DeviceNumberID" json:"device_number"`
  279. Zone *models.DeviceZone `gorm:"ForeignKey:ZoneID" json:"zone"`
  280. Mode *models.TreatmentMode `gorm:"ForeignKey:ModeID" json:"mode"`
  281. }
  282. type PrintCount struct {
  283. ScheduleType int64 `gorm:"column:schedule_type" json:"schedule_type"`
  284. ScheduleWeek int64 `gorm:"column:schedule_week" json:"schedule_week"`
  285. Count int64
  286. }
  287. func GetPrinitWeekSchedules(orgID int64, start, end int64) ([]*WeekSchedulePrintVM, error) {
  288. var schedules []*WeekSchedulePrintVM
  289. err := readDb.Table("xt_schedule as s").
  290. Preload("Patient", "status = 1 and user_org_id = ?", orgID).
  291. Preload("DeviceNumber", "status = 1 and org_id = ?", orgID).
  292. Preload("Zone", "status = 1 and org_id = ?", orgID).
  293. Preload("Mode", "status = 1").
  294. // Joins("JOIN xt_patients as p ON p.id = s.patient_id").
  295. Where("s.user_org_id = ? and s.schedule_date >= ? and s.schedule_date <= ? and s.status = 1", orgID, start, end).
  296. // 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").
  297. Find(&schedules).Error
  298. return schedules, err
  299. }
  300. func GetPrinitWeekTotal(orgid int64, startime int64, endtime int64) (print []*PrintCount, err error) {
  301. db := XTReadDB().Table("xt_schedule as x").Where("x.status = 1")
  302. if orgid > 0 {
  303. db = db.Where("x.user_org_id = ?", orgid)
  304. }
  305. if startime > 0 {
  306. db = db.Where("x.schedule_date >=?", startime)
  307. }
  308. if endtime > 0 {
  309. db = db.Where("x.schedule_date <=?", endtime)
  310. }
  311. 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
  312. return print, err
  313. }
  314. func GetBloodPrintWeekTotal(orgid int64, startime int64, endtime int64) (print []*PrintCount, err error) {
  315. db := XTReadDB().Table("xt_schedule as x").Where("x.status = 1")
  316. if orgid > 0 {
  317. db = db.Where("x.user_org_id = ?", orgid)
  318. }
  319. if startime > 0 {
  320. db = db.Where("x.schedule_date >=?", startime)
  321. }
  322. if endtime > 0 {
  323. db = db.Where("x.schedule_date <=?", endtime)
  324. }
  325. db = db.Where("x.mode_id = 2")
  326. 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
  327. return print, err
  328. }
  329. func GetPerfusionWeekTotal(orgid int64, startime int64, endtime int64) (print []*PrintCount, err error) {
  330. db := XTReadDB().Table("xt_schedule as x").Where("x.status = 1")
  331. if orgid > 0 {
  332. db = db.Where("x.user_org_id = ?", orgid)
  333. }
  334. if startime > 0 {
  335. db = db.Where("x.schedule_date >=?", startime)
  336. }
  337. if endtime > 0 {
  338. db = db.Where("x.schedule_date <=?", endtime)
  339. }
  340. db = db.Where("x.mode_id = 3")
  341. 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
  342. return print, err
  343. }
  344. // func ExchangeScheduleTimeWithWeekSchedules(orgID int64, nextWeekSchs []*models.Schedule, nextTwoWeekSchs []*models.Schedule) error {
  345. // templateMode, getTemplateModeErr := GetOrgPatientScheduleTemplateMode(orgID)
  346. // if getTemplateModeErr != nil {
  347. // return getTemplateModeErr
  348. // }
  349. // if templateMode == nil {
  350. // return errors.New("模板模式不存在")
  351. // }
  352. // tx := writeDb.Begin()
  353. // updateTime := time.Now().Unix()
  354. // for _, sch := range nextWeekSchs {
  355. // sch.PatientInfectiousDiseases = nil
  356. // date := time.Unix(sch.ScheduleDate, 0)
  357. // sch.ScheduleDate = date.AddDate(0, 0, 7).Unix()
  358. // sch.UpdatedTime = updateTime
  359. // updateSchErr := tx.Save(sch).Error
  360. // if updateSchErr != nil {
  361. // tx.Rollback()
  362. // return updateSchErr
  363. // }
  364. // }
  365. // for _, sch := range nextTwoWeekSchs {
  366. // sch.PatientInfectiousDiseases = nil
  367. // date := time.Unix(sch.ScheduleDate, 0)
  368. // sch.ScheduleDate = date.AddDate(0, 0, -7).Unix()
  369. // sch.UpdatedTime = updateTime
  370. // updateSchErr := tx.Save(sch).Error
  371. // if updateSchErr != nil {
  372. // tx.Rollback()
  373. // return updateSchErr
  374. // }
  375. // }
  376. // templateMode.ModifyTime = updateTime
  377. // templateMode.ExecuteTimes += 1
  378. // updateModeErr := tx.Save(templateMode).Error
  379. // if updateModeErr != nil {
  380. // tx.Rollback()
  381. // return updateModeErr
  382. // }
  383. // tx.Commit()
  384. // return nil
  385. // }
  386. func GetSchedualPatientsByKeywordsAndWeek(keywords string, org_id int64, start_time int64, end_time int64) (schedule []*models.Schedule, err error) {
  387. likeKey := "%" + keywords + "%"
  388. err = readDb.
  389. Table("xt_schedule as s").
  390. Preload("PatientInfectiousDiseases", "status = 1 ").
  391. Preload("DeviceZone", "status = 1 ").
  392. Preload("DeviceNumber", "status = 1 ").
  393. Preload("TreatmentMode", "status = 1 ").
  394. Joins("JOIN xt_patients as p ON p.id = s.patient_id AND (p.name LIKE ? OR p.dialysis_no LIKE ?)", likeKey, likeKey).
  395. Where("s.user_org_id=? and s.schedule_date >= ? and s.schedule_date <= ? and s.status=1", org_id, start_time, end_time).
  396. 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").
  397. Order("schedule_date asc").
  398. Find(&schedule).Error
  399. return
  400. }
  401. func GetSchedualPatientsByKeywords(keywords string, org_id int64, startime int64, endtime int64) (schedule []*models.Schedule, err error) {
  402. likeKey := "%" + keywords + "%"
  403. err = readDb.
  404. Table("xt_schedule as s").
  405. Preload("PatientInfectiousDiseases", "status = 1 ").
  406. Preload("DeviceZone", "status = 1 ").
  407. Preload("DeviceNumber", "status = 1 ").
  408. Preload("TreatmentMode", "status = 1 ").
  409. Joins("JOIN xt_patients as p ON p.id = s.patient_id AND (p.name LIKE ? OR p.dialysis_no LIKE ?)", likeKey, likeKey).
  410. Where("s.user_org_id=? and s.schedule_date >= ? and s.schedule_date <= ? and s.status=1", org_id, startime, endtime).
  411. 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").
  412. Order("schedule_date asc").
  413. Find(&schedule).Error
  414. return
  415. }
  416. func GetWeekDayScheduleTwo(orgID int64, scheduleDate int64, scheduleDates *time.Time, weektime int64) (schedules []*models.WeekSchedule, err error) {
  417. db := readDb.Table("xt_schedule as s ").Where("s.status =1")
  418. if orgID > 0 {
  419. db = db.Where("s.user_org_id = ?", orgID)
  420. }
  421. if scheduleDate > 0 {
  422. db = db.Where("s.schedule_date = ?", scheduleDate)
  423. }
  424. if weektime > 0 {
  425. db = db.Where("s.schedule_type = ?", weektime)
  426. }
  427. err = db.
  428. Preload("DeviceZone", "status = 1 ").
  429. Preload("DeviceNumber", "status = 1 ").
  430. Preload("DialysisPrescription", func(db *gorm.DB) *gorm.DB {
  431. return db.Where("status = 1").Order("id asc")
  432. }).
  433. Joins("JOIN xt_patients as p ON p.id = s.patient_id").
  434. 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").
  435. Order("s.partition_id asc").
  436. Find(&schedules).Error
  437. for _, item := range schedules {
  438. _, config := FindDoctorAdviceRecordByOrgId(item.UserOrgId)
  439. if config.IsOpenRemind > 0 {
  440. //开启推送提醒逻辑 提交长期处方的时候,弹起长期医嘱推送
  441. var advice_three []*models.DoctorAdvice
  442. advices, _ := GetLastLongAdviceByGroupNoOther(item.UserOrgId, item.PatientId)
  443. advices_two, _ := GetLastLongAdviceByGroupNoThreeOther(item.UserOrgId, item.PatientId)
  444. for _, advice := range advices {
  445. advice.IsCheck = 1
  446. if advice.FrequencyType == 3 {
  447. week := int(scheduleDates.Weekday())
  448. switch week {
  449. case 1:
  450. if strings.Index(advice.WeekDay, "周一") > -1 {
  451. advice_three = append(advice_three, advice)
  452. }
  453. break
  454. case 2:
  455. if strings.Index(advice.WeekDay, "周二") > -1 {
  456. advice_three = append(advice_three, advice)
  457. }
  458. break
  459. case 3:
  460. if strings.Index(advice.WeekDay, "周三") > -1 {
  461. advice_three = append(advice_three, advice)
  462. }
  463. break
  464. case 4:
  465. if strings.Index(advice.WeekDay, "周四") > -1 {
  466. advice_three = append(advice_three, advice)
  467. }
  468. break
  469. case 5:
  470. if strings.Index(advice.WeekDay, "周五") > -1 {
  471. advice_three = append(advice_three, advice)
  472. }
  473. break
  474. case 6:
  475. if strings.Index(advice.WeekDay, "周六") > -1 {
  476. advice_three = append(advice_three, advice)
  477. }
  478. break
  479. case 0:
  480. if strings.Index(advice.WeekDay, "周日") > -1 {
  481. advice_three = append(advice_three, advice)
  482. }
  483. break
  484. }
  485. } else if advice.FrequencyType == 1 {
  486. advice_three = append(advice_three, advice)
  487. }
  488. }
  489. for _, advice := range advices_two {
  490. now := scheduleDates.Unix()
  491. dayStr := strconv.FormatInt(advice.DayCount, 10)
  492. dayStr2 := "-" + dayStr
  493. count, _ := strconv.ParseInt(dayStr2, 10, 64)
  494. oldTime := scheduleDates.AddDate(0, 0, int(count)).Unix()
  495. advices, _ := FindAllDoctorAdviceByTimeOther(now, oldTime, item.PatientId, item.UserOrgId, advice.TemplateId)
  496. if len(advices) == 0 {
  497. advice_three = append(advice_three, advice)
  498. }
  499. }
  500. item.DoctorAdvice = append(item.DoctorAdvice, advice_three...)
  501. }
  502. }
  503. return
  504. }
  505. func GetWeekDaySchedule(orgID int64, scheduleDate int64, scheduleDates *time.Time) (schedules []*models.WeekSchedule, err error) {
  506. err = readDb.
  507. Table("xt_schedule as s").
  508. Preload("DeviceZone", "status = 1 ").
  509. Preload("DeviceNumber", "status = 1 ").
  510. Preload("DialysisPrescription", func(db *gorm.DB) *gorm.DB {
  511. return db.Where("status = 1").Order("id asc")
  512. }).
  513. Joins("JOIN xt_patients as p ON p.id = s.patient_id").
  514. Where("s.user_org_id=? and s.schedule_date = ? and s.status=1", orgID, scheduleDate).
  515. 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").
  516. Order("schedule_type").
  517. Find(&schedules).Error
  518. for _, item := range schedules {
  519. _, config := FindDoctorAdviceRecordByOrgId(item.UserOrgId)
  520. if config.IsOpenRemind > 0 {
  521. //开启推送提醒逻辑 提交长期处方的时候,弹起长期医嘱推送
  522. var advice_three []*models.DoctorAdvice
  523. advices, _ := GetLastLongAdviceByGroupNoOther(item.UserOrgId, item.PatientId)
  524. advices_two, _ := GetLastLongAdviceByGroupNoThreeOther(item.UserOrgId, item.PatientId)
  525. for _, advice := range advices {
  526. advice.IsCheck = 1
  527. if advice.FrequencyType == 3 {
  528. week := int(scheduleDates.Weekday())
  529. fmt.Println(advice.WeekDay)
  530. switch week {
  531. case 1:
  532. if strings.Index(advice.WeekDay, "周一") > -1 {
  533. advice_three = append(advice_three, advice)
  534. }
  535. break
  536. case 2:
  537. if strings.Index(advice.WeekDay, "周二") > -1 {
  538. advice_three = append(advice_three, advice)
  539. }
  540. break
  541. case 3:
  542. if strings.Index(advice.WeekDay, "周三") > -1 {
  543. advice_three = append(advice_three, advice)
  544. }
  545. break
  546. case 4:
  547. if strings.Index(advice.WeekDay, "周四") > -1 {
  548. advice_three = append(advice_three, advice)
  549. }
  550. break
  551. case 5:
  552. if strings.Index(advice.WeekDay, "周五") > -1 {
  553. advice_three = append(advice_three, advice)
  554. }
  555. break
  556. case 6:
  557. if strings.Index(advice.WeekDay, "周六") > -1 {
  558. advice_three = append(advice_three, advice)
  559. }
  560. break
  561. case 0:
  562. if strings.Index(advice.WeekDay, "周日") > -1 {
  563. advice_three = append(advice_three, advice)
  564. }
  565. break
  566. }
  567. } else if advice.FrequencyType == 1 {
  568. advice_three = append(advice_three, advice)
  569. }
  570. }
  571. for _, advice := range advices_two {
  572. now := scheduleDates.Unix()
  573. dayStr := strconv.FormatInt(advice.DayCount, 10)
  574. dayStr2 := "-" + dayStr
  575. count, _ := strconv.ParseInt(dayStr2, 10, 64)
  576. oldTime := scheduleDates.AddDate(0, 0, int(count)).Unix()
  577. advices, _ := FindAllDoctorAdviceByTimeOther(now, oldTime, item.PatientId, item.UserOrgId, advice.TemplateId)
  578. if len(advices) == 0 {
  579. advice_three = append(advice_three, advice)
  580. }
  581. }
  582. item.DoctorAdvice = append(item.DoctorAdvice, advice_three...)
  583. }
  584. }
  585. return
  586. }
  587. func GetWeekDayScheduleById(orgid int64, scheduleDate int64, scheduleType int64, zone int64) (list []*models.VmSchedulesRemind, err error) {
  588. db := XTReadDB().Table("xt_schedule as x").Where("x.status = 1")
  589. if orgid > 0 {
  590. db = db.Where("x.user_org_id = ?", orgid)
  591. }
  592. if scheduleDate > 0 {
  593. db = db.Where("x.schedule_date = ?", scheduleDate)
  594. }
  595. if scheduleType > 0 {
  596. db = db.Where("x.schedule_type = ?", scheduleType)
  597. }
  598. if zone > 0 {
  599. db = db.Where("x.partition_id = ?", zone)
  600. }
  601. 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 {
  602. return db.Where("status =1 and parent_id = 0").Preload("ChildDoctorAdvice", "status = 1")
  603. }).Preload("HisDoctorAdviceInfo", "status = 1").Preload("DialysisPrescription", "status = 1").Preload("DialysisSolution", func(db *gorm.DB) *gorm.DB {
  604. return db.Where("status =1").Order("id asc")
  605. }).Find(&list).Error
  606. return list, err
  607. }
  608. func GetWeekDayScheduleByIdThee(orgid int64, scheduleDate int64, scheduleType int64, zone []string) (list []*models.VmSchedulesRemind, err error) {
  609. db := XTReadDB().Table("xt_schedule as x").Where("x.status = 1")
  610. if orgid > 0 {
  611. db = db.Where("x.user_org_id = ?", orgid)
  612. }
  613. if scheduleDate > 0 {
  614. db = db.Where("x.schedule_date = ?", scheduleDate)
  615. }
  616. if scheduleType > 0 {
  617. db = db.Where("x.schedule_type = ?", scheduleType)
  618. }
  619. if len(zone) > 0 {
  620. db = db.Where("x.partition_id in (?)", zone)
  621. }
  622. 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 and dialysis_date = ?", scheduleDate).Preload("XtAssessmentBeforeDislysis", "status = 1 and assessment_date =?", scheduleDate).Preload("DoctorAdvice", func(db *gorm.DB) *gorm.DB {
  623. return db.Where("status =1 and parent_id = 0 and advice_date = ?", scheduleDate).Preload("ChildDoctorAdvice", "status = 1")
  624. }).Preload("HisDoctorAdviceInfo", "status = 1 and advice_date = ? and user_org_id = ? ", scheduleDate, orgid).Preload("DialysisPrescription", "status = 1 and record_date =? and user_org_id = ?", scheduleDate, orgid).Preload("DialysisSolution", func(db *gorm.DB) *gorm.DB {
  625. return db.Where("status =1").Order("id asc")
  626. }).Preload("LastAfterWeight", func(db *gorm.DB) *gorm.DB {
  627. return db.Where("user_org_id = ? and status = 1 and assessment_date < ?", orgid, scheduleDate)
  628. }).Find(&list).Error
  629. return list, err
  630. }
  631. func GetWeekDayScheduleByIdTwo(orgid int64, scheduleDate int64, scheduleType int64, startDate int64, zone []string) (list []*models.VmSchedulesRemind, err error) {
  632. db := XTReadDB().Table("xt_schedule as x").Where("x.status = 1")
  633. if orgid > 0 {
  634. db = db.Where("x.user_org_id = ?", orgid)
  635. }
  636. if scheduleDate > 0 {
  637. db = db.Where("x.schedule_date >=? and x.schedule_date<=?", startDate, scheduleDate)
  638. }
  639. if scheduleType > 0 {
  640. db = db.Where("x.schedule_type = ?", scheduleType)
  641. }
  642. if len(zone) > 0 {
  643. db = db.Where("x.partition_id in (?)", zone)
  644. }
  645. 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,x.mode_id").Preload("XtPatients", "status = 1").Preload("DeviceZone", "status = 1").Preload("DeviceNumber", "status = 1").Preload("DialysisOrder", "status = 1 and dialysis_date =?", scheduleDate).Preload("XtAssessmentBeforeDislysis", "status = 1 and assessment_date =?", scheduleDate).Preload("DoctorAdvice", func(db *gorm.DB) *gorm.DB {
  646. return db.Where("status =1 and parent_id = 0 and advice_date = ?", scheduleDate).Preload("ChildDoctorAdvice", "status = 1")
  647. }).Preload("HisDoctorAdviceInfo", "status = 1 and advice_date = ?", scheduleDate).Preload("DialysisPrescription", "status = 1 and record_date = ?", scheduleDate).Preload("DialysisSolution", func(db *gorm.DB) *gorm.DB {
  648. return db.Where("status =1").Order("id asc")
  649. }).Preload("LastAfterWeight", func(db *gorm.DB) *gorm.DB {
  650. return db.Where("user_org_id = ? and status = 1 and assessment_date < ?", orgid, scheduleDate)
  651. }).Find(&list).Error
  652. return list, err
  653. }
  654. func RemoveRepeatedElement(arr []*models.DoctorAdvice) (newArr []*models.DoctorAdvice) {
  655. newArr = make([]*models.DoctorAdvice, 0)
  656. for i := 0; i < len(arr); i++ {
  657. repeat := false
  658. for j := i + 1; j < len(arr); j++ {
  659. if arr[i].ID == arr[j].ID {
  660. repeat = true
  661. break
  662. }
  663. }
  664. if !repeat {
  665. newArr = append(newArr, arr[i])
  666. }
  667. }
  668. return
  669. }
  670. func GetLastLongAdviceByGroupNoOther(orgId int64, patient_id int64) (advice []*models.DoctorAdvice, err error) {
  671. 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
  672. return
  673. }
  674. func GetLastLongAdviceByGroupNoThreeOther(orgId int64, patient_id int64) (advice []*models.DoctorAdvice, err error) {
  675. 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
  676. return
  677. }
  678. func FindAllDoctorAdviceByTimeOther(now int64, end int64, patient_id int64, orgId int64, temp_id string) (advice []*models.DoctorAdvice, err error) {
  679. 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
  680. return
  681. }
  682. func FindSchPatientNameByName(name string, user_org_id int64) (patient []*models.Patients, err error) {
  683. err = readDb.Model(&models.Patients{}).Where("user_org_id = ? AND name = ? AND status = 1", user_org_id, name).Find(&patient).Error
  684. return
  685. }
  686. func UpdateScheduleStatus(date int64, org_id int64) (err error) {
  687. 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
  688. return
  689. }
  690. func FindDeviceZoneByName(name string, org_id int64) (zone models.DeviceZone, err error) {
  691. err = readDb.Model(&models.DeviceZone{}).Where("org_id = ? AND name = ? AND status = 1", org_id, name).First(&zone).Error
  692. return
  693. }
  694. func FindDeviceByName(device_name string, org_id int64, zone_id int64) (devices []*models.DeviceNumber, err error) {
  695. 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
  696. return
  697. }
  698. func FindSchedualExportLog(org_id int64, export_time int64) (errLogs []*models.ExportErrLog, err error) {
  699. err = readDb.Model(&models.ExportErrLog{}).Where("user_org_id = ? AND export_time = ? AND log_type = 2", org_id, export_time).Find(&errLogs).Error
  700. return
  701. }
  702. func FindDeviceByNameTwo(device_name string, org_id int64, zone_id int64) (devices []*models.DeviceNumber, err error) {
  703. 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
  704. return
  705. }
  706. func UpdateScheduleTemplateStatus(org_id int64) (err error) {
  707. err = readDb.Model(&models.PatientScheduleTemplateItem{}).Where("org_id = ? AND status = 1", org_id).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  708. return
  709. }
  710. func CreateScheduleTemplate(m *models.PatientScheduleTemplateItem) error {
  711. return writeDb.Model(&models.PatientScheduleTemplateItem{}).Create(&m).Error
  712. }
  713. func FindSchedualTemplateExportLog(org_id int64, export_time int64) (errLogs []*models.ExportErrLog, err error) {
  714. err = readDb.Model(&models.ExportErrLog{}).Where("user_org_id = ? AND export_time = ? AND log_type = 3", org_id, export_time).Find(&errLogs).Error
  715. return
  716. }
  717. func GetDialysisOrderByBedId(orgID, start, bed_id int64, schedule_type int64) (*models.DialysisOrder, error) {
  718. order := models.DialysisOrder{}
  719. err = readDb.Model(&order).Where("user_org_id=? and dialysis_date=? and bed_id=? and status=1 AND schedual_type = ?", orgID, start, bed_id, schedule_type).Find(&order).Error
  720. if err == gorm.ErrRecordNotFound {
  721. return nil, err
  722. }
  723. if err != nil {
  724. return nil, err
  725. }
  726. return &order, nil
  727. }
  728. type VMDeviceNumber struct {
  729. ID int64 `gorm:"column:id" json:"id"`
  730. OrgID int64 `gorm:"column:org_id" json:"-"`
  731. Number string `gorm:"column:number" json:"number"`
  732. GroupID int64 `gorm:"column:group_id" json:"group_id"`
  733. ZoneID int64 `gorm:"column:zone_id" json:"zone_id"`
  734. Status int8 `json:"-"`
  735. CreateTime int64 `gorm:"column:ctime" json:"-"`
  736. ModifyTime int64 `gorm:"column:mtime" json:"-"`
  737. DeviceZone models.DeviceZone `json:"zone" gorm:"foreignkey:ID;AssociationForeignKey:ZoneID;"`
  738. }
  739. func (VMDeviceNumber) TableName() string {
  740. return "xt_device_number"
  741. }
  742. type VMPatientScheduleTemplateItem struct {
  743. ID int64 `gorm:"column:id" json:"id" form:"id"`
  744. OrgID int64 `gorm:"column:org_id" json:"-" form:"org_id"`
  745. TemplateID int64 `gorm:"column:template_id" json:"template_id" form:"template_id"`
  746. DeviceNumberID int64 `gorm:"column:device_number_id" json:"device_number_id" form:"device_number_id"`
  747. PatientID int64 `gorm:"column:patient_id" json:"patient_id" form:"patient_id"`
  748. TreatMode int64 `gorm:"column:treat_mode" json:"treat_mode" form:"treat_mode"` // 治疗模式
  749. Weekday int8 `gorm:"column :weekday" json:"weekday" form:"weekday"` // 周一-周日 1-7
  750. TimeType int8 `gorm:"column:time_type" json:"time_type" form:"time_type"` // 上下晚 123
  751. Status int8 `gorm:"column:status" json:"status" form:"status"`
  752. CreateTime int64 `gorm:"column:ctime" json:"-" form:"ctime"`
  753. ModifyTime int64 `gorm:"column:mtime" json:"-" form:"mtime"`
  754. IsExport int64 `gorm:"column:is_export" json:"is_export" form:"is_export"`
  755. Patient string `gorm:"-" json:"patient" form:"patient"`
  756. DeviceNumber VMDeviceNumber `json:"number" gorm:"foreignkey:ID;AssociationForeignKey:DeviceNumberID;"`
  757. PatientInfectiousDiseases []*models.InfectiousDiseases `json:"patient_contagions" gorm:"foreignkey:PatientId;AssociationForeignKey:PatientId;"`
  758. }
  759. func (VMPatientScheduleTemplateItem) TableName() string {
  760. return "xt_patient_schedule_template_item"
  761. }
  762. func GetTemplateSchedualPatientsByKeywords(keywords string, org_id int64, template_id int64) (schedule []*VMPatientScheduleTemplateItem, err error) {
  763. likeKey := "%" + keywords + "%"
  764. err = readDb.
  765. Table("xt_patient_schedule_template_item as s").
  766. Preload("PatientInfectiousDiseases", "status = 1 ").
  767. Preload("DeviceNumber", func(db *gorm.DB) *gorm.DB {
  768. return db.Where("org_id = ? AND status = 1", org_id).Preload("DeviceZone", "org_id = ? AND status = 1", org_id)
  769. }).
  770. Joins("JOIN xt_patients as p ON p.id = s.patient_id AND (p.name LIKE ? OR p.dialysis_no LIKE ?)", likeKey, likeKey).
  771. Where("s.template_id = ? AND s.org_id=? and s.status=1", template_id, org_id).
  772. 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").
  773. Order("weekday asc").
  774. Find(&schedule).Error
  775. return
  776. }
  777. func UpdateNewScheduleTemplateStatus(id int64, org_id int64) (err error) {
  778. err = readDb.Model(&models.PatientScheduleTemplateItem{}).Where("org_id = ? AND id = ?", org_id, id).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  779. return
  780. }
  781. func GetNextWeekDaySchedule(weektype int64, weektime int64, startime int64, endtime int64, orgID int64, zone []string, record_date int64) (schedules []*models.WeekScheduleSix, err error) {
  782. db := XTReadDB().Table("xt_schedule as s ").Where("s.status =1")
  783. if orgID > 0 {
  784. db = db.Where("s.user_org_id = ?", orgID)
  785. }
  786. if startime > 0 {
  787. db = db.Where("s.schedule_date >= ?", startime)
  788. }
  789. if endtime > 0 {
  790. db = db.Where("s.schedule_date <= ?", endtime)
  791. }
  792. if weektime > 0 {
  793. db = db.Where("s.schedule_type = ?", weektime)
  794. }
  795. if weektype > 0 {
  796. db = db.Where("s.schedule_week = ?", weektype)
  797. }
  798. if len(zone) > 0 {
  799. db = db.Where("s.partition_id in (?)", zone)
  800. }
  801. err = db.
  802. Preload("DeviceZone", "status = 1 ").
  803. Preload("DeviceNumber", "status = 1 ").
  804. Preload("XtAssessmentBeforeDislysis", func(db *gorm.DB) *gorm.DB {
  805. return db.Where("status = 1 and assessment_date = ?", record_date)
  806. }).Preload("DialysisPrescription", func(db *gorm.DB) *gorm.DB {
  807. return db.Where("status = 1").Order("id asc")
  808. }).Preload("DialysisSolution", func(db *gorm.DB) *gorm.DB {
  809. return db.Where("status = 1").Order("id asc")
  810. }).Preload("DoctorAdvice", func(db *gorm.DB) *gorm.DB {
  811. return db.Where("status =1 and parent_id = 0 and advice_date = ?", record_date).Preload("ChildDoctorAdvice", "status = 1 and advice_date = ?", record_date)
  812. }).Preload("HisDoctorAdviceInfo", "status = 1 and advice_date = ?", record_date).Joins("JOIN xt_patients as p ON p.id = s.patient_id").
  813. 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,p.id_card_no as id_card_no,p.dialysis_no as dialysis_no,p.gender as gender,p.phone as phone").
  814. Order("s.partition_id desc").
  815. Find(&schedules).Error
  816. return schedules, err
  817. }
  818. func GetThreeWeekList(startime int64, endtime int64, orgid int64, scheduletype int64, partionid int64) (schedule []*models.VmSchedule, err error) {
  819. db := XTReadDB().Table("xt_schedule as x").Where("x.status =1")
  820. if startime > 0 {
  821. db = db.Where("x.schedule_date >=?", startime)
  822. }
  823. if endtime > 0 {
  824. db = db.Where("x.schedule_date <=?", endtime)
  825. }
  826. if orgid > 0 {
  827. db = db.Where("x.user_org_id = ?", orgid)
  828. }
  829. if scheduletype > 0 {
  830. db = db.Where("x.schedule_type = ?", scheduletype)
  831. }
  832. if partionid > 0 {
  833. db = db.Where("x.partition_id = ?", partionid)
  834. }
  835. 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
  836. return schedule, err
  837. }
  838. func UpdatedDialysisPrescription(prescription *models.DialysisPrescription, recordDate int64, patientid int64, orgid int64) error {
  839. 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
  840. return err
  841. }
  842. func GetWeekSchedules(orgid int64, startime int64, endtime int64) (scheudle []*models.XtSchedule, err error) {
  843. err = XTReadDB().Model(&scheudle).Where("user_org_id = ? and status = 1 and schedule_date>=? and schedule_date<=?", orgid, startime, endtime).Find(&scheudle).Error
  844. return scheudle, err
  845. }
  846. func GetLastWeekSchedule(orgid int64, startime int64, endtime int64) (*models.XtSchedule, error) {
  847. schedule := models.XtSchedule{}
  848. err := XTReadDB().Where("user_org_id = ? and status =1 and schedule_date>=? and schedule_date<=?", orgid, startime, endtime).Find(&schedule).Error
  849. if err == gorm.ErrRecordNotFound {
  850. return nil, err
  851. }
  852. if err != nil {
  853. return nil, err
  854. }
  855. return &schedule, nil
  856. }
  857. func AddPatientSchedule(schedule *models.XtSchedule) error {
  858. utx := XTWriteDB().Begin()
  859. err := utx.Create(&schedule).Error
  860. utx.Commit()
  861. return err
  862. }
  863. func UpdatePatientSchedule(orgid int64, startime int64, endtime int64) error {
  864. schedule := models.XtSchedule{}
  865. 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
  866. return err
  867. }
  868. func GetSettingById(id int64) (*models.XtRemindPrintSetting, error) {
  869. setting := models.XtRemindPrintSetting{}
  870. err := XTReadDB().Model(&setting).Where("id = ? and status = 1", id).Find(&setting).Error
  871. if err == gorm.ErrRecordNotFound {
  872. return nil, err
  873. }
  874. if err != nil {
  875. return nil, err
  876. }
  877. return &setting, nil
  878. }
  879. func CreateSetting(setting *models.XtRemindPrintSetting) error {
  880. err := XTReadDB().Create(&setting).Error
  881. return err
  882. }
  883. func UpdatedRemindPrint(set *models.XtRemindPrintSetting, id int64) error {
  884. 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
  885. return err
  886. }
  887. func GetRemindPrintList(id int64) (models.XtRemindPrintSetting, error) {
  888. setting := models.XtRemindPrintSetting{}
  889. err := XTReadDB().Model(&setting).Where("user_org_id = ? and status =1", id).Find(&setting).Error
  890. return setting, err
  891. }
  892. func GetScheudleTemplateById(orgid int64) (*models.ScheduleTemplate, error) {
  893. schedule := models.ScheduleTemplate{}
  894. err := XTReadDB().Model(&schedule).Where("user_org_id = ? and status = 1", orgid).Find(&schedule).Error
  895. if err == gorm.ErrRecordNotFound {
  896. return nil, err
  897. }
  898. if err != nil {
  899. return nil, err
  900. }
  901. return &schedule, nil
  902. }
  903. func CreateXtscheduleTemplate(template *models.ScheduleTemplate) error {
  904. err := XTWriteDB().Create(&template).Error
  905. return err
  906. }
  907. func UpdateXtScheduleTemplate(template *models.ScheduleTemplate, orgid int64) error {
  908. err := XTWriteDB().Model(&template).Where("user_org_id = ? and status = 1", orgid).Updates(map[string]interface{}{"template_id": template.TemplateId}).Error
  909. return err
  910. }
  911. func GetScheduleTemplate(orgid int64) (models.ScheduleTemplate, error) {
  912. template := models.ScheduleTemplate{}
  913. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&template).Error
  914. return template, err
  915. }
  916. func GetDataPrintByOrgId(orgid int64) (*models.XtDataPrint, error) {
  917. dataPrint := models.XtDataPrint{}
  918. err := XTReadDB().Model(&dataPrint).Where("user_org_id = ? and status = 1", orgid).Find(&dataPrint).Error
  919. if err == gorm.ErrRecordNotFound {
  920. return nil, err
  921. }
  922. if err != nil {
  923. return nil, err
  924. }
  925. return &dataPrint, nil
  926. }
  927. func CreateDataPrint(dataPrint *models.XtDataPrint) error {
  928. err := XTWriteDB().Create(&dataPrint).Error
  929. return err
  930. }
  931. func UpdteDataPrint(orgid int64, dataPrint *models.XtDataPrint) error {
  932. err := XTWriteDB().Model(&dataPrint).Where("user_org_id = ? and status = 1", orgid).Updates(map[string]interface{}{"is_open": dataPrint.IsOpen}).Error
  933. return err
  934. }
  935. func GetDataPrint(orgid int64) (models.XtDataPrint, error) {
  936. dataPrint := models.XtDataPrint{}
  937. err := XTReadDB().Model(&dataPrint).Where("user_org_id = ? and status = 1", orgid).Find(&dataPrint).Error
  938. return dataPrint, err
  939. }
  940. func GetNextWeekPanels(startime int64, endtime int64, orgid int64) (schedule []*models.XtSchedule, err error) {
  941. err = XTReadDB().Model(&schedule).Where("schedule_date >=? and schedule_date<=? and status = 1 and user_org_id = ?", startime, endtime, orgid).Preload("LastAfterWeight", func(db *gorm.DB) *gorm.DB {
  942. return db.Where("user_org_id = ? and status = 1 and assessment_date < ?", orgid, endtime)
  943. }).Find(&schedule).Error
  944. return schedule, err
  945. }
  946. func GetTemplateListByTemplateId(id int64, orgid int64) (list []*models.VmPatientScheduleTemplateItem, err error) {
  947. err = XTReadDB().Where("template_id =? and org_id = ? and status = 1", id, orgid).Find(&list).Error
  948. return list, err
  949. }
  950. func DeletePatientSchedule(startime int64, endtime int64, org_id int64) (models.XtSchedule, error) {
  951. schedule := models.XtSchedule{}
  952. err := XTWriteDB().Model(&schedule).Where("schedule_date>=? and schedule_date<=? and user_org_id = ? and status = 1", startime, endtime, org_id).Updates(map[string]interface{}{"status": 0}).Error
  953. return schedule, err
  954. }
  955. func DeletePatientScheduleToday(startime int64, endtime int64, org_id int64) (models.XtSchedule, error) {
  956. schedule := models.XtSchedule{}
  957. err := XTWriteDB().Model(&schedule).Where("schedule_date>? and schedule_date<=? and user_org_id = ? and status = 1", startime, endtime, org_id).Updates(map[string]interface{}{"status": 0}).Error
  958. return schedule, err
  959. }
  960. func GetBedNumberById(id int64, org_id int64) (models.DeviceNumber, error) {
  961. number := models.DeviceNumber{}
  962. err := XTReadDB().Where("id = ? and status = 1 and org_id =?", id, org_id).Find(&number).Error
  963. return number, err
  964. }
  965. func CreatePatientSchedule(schedule *models.XtSchedule) error {
  966. utx := XTWriteDB().Begin()
  967. err := utx.Create(&schedule).Error
  968. utx.Commit()
  969. return err
  970. }
  971. func GetPatientScheduleTempalate(orgid int64) (models.PatientScheduleTemplateMode, error) {
  972. mode := models.PatientScheduleTemplateMode{}
  973. err := XTReadDB().Where("org_id = ? and status =1", orgid).Find(&mode).Error
  974. return mode, err
  975. }
  976. func GetSolutionSchedule(orgid int64) (solution []*models.VmDialysisSolution, err error) {
  977. err = XTReadDB().Where("user_org_id = ? and status = 1", orgid).Group("patient_id,mode_id").Order("created_time desc").Preload("DialysisSolution", func(db *gorm.DB) *gorm.DB {
  978. return db.Where("user_org_id = ? and status = 1", orgid).Order("id asc")
  979. }).Find(&solution).Error
  980. return solution, err
  981. }
  982. func GetSolutionScheduleBySchedule(orgid int64, zonid int64, classtype int64, schedule_date int64) (schedule []*models.XtScheduleList, err error) {
  983. db := XTReadDB().Model(&schedule).Where("status = 1")
  984. if orgid > 0 {
  985. db = db.Where("user_org_id =?", orgid)
  986. }
  987. if zonid > 0 {
  988. db = db.Where("partition_id = ?", zonid)
  989. }
  990. if classtype > 0 {
  991. db = db.Where("schedule_type = ?", classtype)
  992. }
  993. if schedule_date > 0 {
  994. db = db.Where("schedule_date = ?", schedule_date)
  995. }
  996. err = db.Preload("DialysisSolution", func(db *gorm.DB) *gorm.DB {
  997. return db.Where("user_org_id = ? and status = 1", orgid).Group("patient_id,mode_id").Order("created_time asc").Preload("DialysisSolution", func(db *gorm.DB) *gorm.DB {
  998. return db.Where("user_org_id = ? and status = 1", orgid).Order("id asc")
  999. })
  1000. }).Find(&schedule).Error
  1001. return schedule, err
  1002. }
  1003. func GetDeathPatient(orgid int64) (patient []*models.XtPatients, err error) {
  1004. err = XTReadDB().Where("user_org_id = ? and (lapseto = 2 || lapseto = 3) and status = 1", orgid).Find(&patient).Error
  1005. return patient, err
  1006. }
  1007. func GetInitPrintData(patient_id int64, scheduleDate int64, orgid int64) (list []*models.VmSchedulesRemind, err error) {
  1008. db := XTReadDB().Table("xt_schedule as x").Where("x.status = 1")
  1009. if orgid > 0 {
  1010. db = db.Where("x.user_org_id = ?", orgid)
  1011. }
  1012. if patient_id > 0 {
  1013. db = db.Where("x.patient_id = ?", patient_id)
  1014. }
  1015. if scheduleDate > 0 {
  1016. db = db.Where("x.schedule_date >=? and x.schedule_date<=?", scheduleDate, scheduleDate)
  1017. }
  1018. 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("XtAssessmentBeforeDislysis", "status = 1 and assessment_date =? and patient_id = ?", scheduleDate, patient_id).Preload("DialysisPrescription", "status = 1 and record_date = ? and patient_id = ?", scheduleDate, patient_id).Preload("DialysisSolution", func(db *gorm.DB) *gorm.DB {
  1019. return db.Where("status =1 and patient_id = ?", patient_id).Order("id asc")
  1020. }).Find(&list).Error
  1021. return list, err
  1022. }
  1023. func GetLastWeightAfter(patient_id int64, scheduleDate int64, orgid int64) (models.AssessmentAfterDislysis, error) {
  1024. dislysis := models.AssessmentAfterDislysis{}
  1025. err := XTReadDB().Where("user_org_id = ? and patient_id = ? and assessment_date < ? and status = 1", orgid, patient_id, scheduleDate).Order("id desc").First(&dislysis).Error
  1026. return dislysis, err
  1027. }
  1028. func GetScheduleTwo(orgID, id int64) (models.Schedule, error) {
  1029. var schedule models.Schedule
  1030. var err error
  1031. err = readDb.Model(&models.Schedule{}).Where("id = ? and user_org_id=? and status=1", id, orgID).First(&schedule).Error
  1032. return schedule, err
  1033. }
  1034. func UpdateSchDName(sch models.Schedule) (err error) {
  1035. err = writeDb.Model(&models.Schedule{}).Where("status=1 AND id = ?", sch.ID).Updates(map[string]interface{}{"dialysis_machine_name": sch.DialysisMachineName}).Error
  1036. return
  1037. }
  1038. func GetDialysisOrderByBedIdTwo(orgID, start, bed_id int64, schedule_type int64) (models.DialysisOrder, error) {
  1039. order := models.DialysisOrder{}
  1040. err = readDb.Model(&order).Where("user_org_id=? and dialysis_date=? and bed_id=? and status=1 AND schedual_type = ?", orgID, start, bed_id, schedule_type).Find(&order).Error
  1041. return order, nil
  1042. }
  1043. func ChangeScheduleClass(schedule models.Schedule) error {
  1044. err := XTWriteDB().Model(&schedule).Where("patient_id = ? and schedule_date = ? and user_org_id = ? and status = 1", schedule.PatientId, schedule.ScheduleDate, schedule.UserOrgId).Update(map[string]interface{}{"schedule_type": schedule.ScheduleType}).Error
  1045. return err
  1046. }
  1047. func GetLastDialysisOrder(org_id int64, patient_id int64, dialysis_date int64) (models.XtDialysisOrderSix, error) {
  1048. order := models.XtDialysisOrderSix{}
  1049. err := XTReadDB().Where("user_org_id = ? and patient_id = ? and dialysis_date < ? and status = 1", org_id, patient_id, dialysis_date).Order("id desc").First(&order).Error
  1050. return order, err
  1051. }
  1052. func GetLastDialysisSolution(org_id int64, patient_id int64, mode_id int64, schedule_date int64) (models.DialysisPrescription, error) {
  1053. dialysisprescription := models.DialysisPrescription{}
  1054. err := XTReadDB().Where("user_org_id = ? and patient_id = ? and mode_id = ? and status = 1 and record_date<?", org_id, patient_id, mode_id, schedule_date).Order("id desc").First(&dialysisprescription).Error
  1055. return dialysisprescription, err
  1056. }