schedule_service.go 55KB

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