dialysis_solution_service.go 31KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  1. package service
  2. import (
  3. "XT_New/models"
  4. "github.com/jinzhu/gorm"
  5. )
  6. func GetPatientSolutionGroupList(patient_id int64, orgid int64) (solution []*models.DialysisSolution, err error) {
  7. err = XTReadDB().Where("patient_id = ? and status = 1 and user_org_id = ?", patient_id, orgid).Group("mode_id").Find(&solution).Error
  8. return solution, err
  9. }
  10. func GetNewPatientSolutionByModeId(patient_id int64, mode_id int64, orgid int64) (models.DialysisSolution, error) {
  11. solution := models.DialysisSolution{}
  12. err := XTReadDB().Where("patient_id = ? and mode_id = ? and user_org_id = ? and status = 1", patient_id, mode_id, orgid).Order("updated_time desc").First(&solution).Error
  13. return solution, err
  14. }
  15. func UpdateDialysisSolutionStatus(id int64, mode_id int64, orgid int64, patient_id int64) error {
  16. err := XTWriteDB().Model(&models.DialysisSolution{}).Where("id = ? and status = 1 and user_org_id = ?", id, orgid).Update(map[string]interface{}{"solution_status": 1}).Error
  17. err = XTWriteDB().Model(&models.DialysisSolution{}).Where("id <> ? and mode_id = ? and user_org_id = ? and status = 1 and patient_id = ?", id, mode_id, orgid, patient_id).Update(map[string]interface{}{"solution_status": 2}).Error
  18. return err
  19. }
  20. func UpdateDialysisSolutionStatusTwo(id int64, mode_id int64, orgid int64, patient_id int64) error {
  21. err = XTWriteDB().Model(&models.DialysisSolution{}).Where("id <> ? and mode_id = ? and user_org_id = ? and status = 1 and patient_id = ?", id, mode_id, orgid, patient_id).Update(map[string]interface{}{"solution_status": 2}).Error
  22. return err
  23. }
  24. func GetLastPatientDialysisSolution(patient_id int64, orgid int64) (models.DialysisSolution, error) {
  25. solution := models.DialysisSolution{}
  26. err := XTReadDB().Where("patient_id = ? and user_org_id = ? and status = 1", patient_id, orgid).Last(&solution).Error
  27. return solution, err
  28. }
  29. func GetPatientDialysisSolutionGroupList(keywords string, limit int64, page int64, partition_id int64, schedule_type int64, scheduleDate int64, orgID int64, ids []string) (schedule []*models.VmBloodSchedule, total int64, err error) {
  30. db := XTReadDB().Model(&models.VmBloodSchedule{}).Where("xt_schedule.status = 1")
  31. offset := (page - 1) * limit
  32. if scheduleDate > 0 {
  33. db = db.Where("xt_schedule.schedule_date = ?", scheduleDate)
  34. }
  35. if schedule_type > 0 {
  36. db = db.Where("xt_schedule.schedule_type = ?", schedule_type)
  37. }
  38. if partition_id > 0 {
  39. db = db.Where("xt_schedule.partition_id = ?", partition_id)
  40. }
  41. if orgID > 0 {
  42. db = db.Where("xt_schedule.user_org_id = ?", orgID)
  43. }
  44. if len(ids) >= 1 {
  45. db = db.Where("xt_schedule.id in(?)", ids)
  46. }
  47. if len(keywords) > 0 {
  48. keywords = "%" + keywords + "%"
  49. db = db.Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  50. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  51. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  52. Preload("DialysisSolution", "status = 1 AND user_org_id = ? and solution_status = 1", orgID).Preload("DialysisPrescription", "status=1 and user_org_id =? and record_date =?", orgID, scheduleDate)
  53. db = db.Joins("JOIN xt_patients AS patient ON patient.id=xt_schedule.patient_id AND patient.status = 1 AND patient.user_org_id = ? AND patient.name Like ?", orgID, keywords)
  54. err = db.Count(&total).Offset(offset).Limit(limit).Find(&schedule).Error
  55. } else {
  56. err = db.Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  57. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  58. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  59. Preload("DialysisPrescription", "status=1 and user_org_id =? and record_date =?", orgID, scheduleDate).
  60. Preload("DialysisSolution", "status = 1 AND user_org_id = ? and solution_status = 1", orgID).Count(&total).Offset(offset).Limit(limit).Find(&schedule).Error
  61. }
  62. return schedule, total, err
  63. }
  64. func GetDialysisAdviceTemplateList(keywords string, limit int64, page int64, partition_id int64, schedule_type int64, scheduleDate int64, orgID int64, idArray []string) (schedule []*models.VmBloodSchedule, total int64, err error) {
  65. db := XTReadDB().Model(&models.VmBloodSchedule{}).Where("xt_schedule.status = 1")
  66. offset := (page - 1) * limit
  67. if scheduleDate > 0 {
  68. db = db.Where("xt_schedule.schedule_date = ?", scheduleDate)
  69. }
  70. if schedule_type > 0 {
  71. db = db.Where("xt_schedule.schedule_type = ?", schedule_type)
  72. }
  73. if partition_id > 0 {
  74. db = db.Where("xt_schedule.partition_id = ?", partition_id)
  75. }
  76. if orgID > 0 {
  77. db = db.Where("xt_schedule.user_org_id = ?", orgID)
  78. }
  79. if len(idArray) >= 1 {
  80. db = db.Where("xt_schedule.id in(?)", idArray)
  81. }
  82. if len(keywords) > 0 {
  83. keywords = "%" + keywords + "%"
  84. db = db.Joins("JOIN xt_patients AS patient ON patient.id=xt_schedule.patient_id AND patient.status = 1 AND patient.user_org_id = ? AND patient.name Like ?", orgID, keywords)
  85. err = db.Count(&total).Offset(offset).Limit(limit).Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  86. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  87. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).Find(&schedule).Error
  88. } else {
  89. err = db.Count(&total).Offset(offset).Limit(limit).Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  90. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  91. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).Find(&schedule).Error
  92. }
  93. return schedule, total, err
  94. }
  95. func GetAllLongAdviceList(user_org_id int64) (advice []*models.DoctorAdvice, err error) {
  96. err = XTReadDB().Where("user_org_id = ? and advice_type = 1 and status = 1 and stop_state!=1", user_org_id).Find(&advice).Error
  97. return advice, err
  98. }
  99. func GetHisLongAdviceList(user_org_id int64) (advice []*models.HisDoctorAdviceTemplate, err error) {
  100. err = XTReadDB().Where("org_id = ? and status=1", user_org_id).Find(&advice).Error
  101. return advice, err
  102. }
  103. func GetAllHisAdviceTemplateList(user_org_id int64) (advice []*models.HisPrescriptionAdviceTemplate, err error) {
  104. err = XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&advice).Error
  105. return advice, err
  106. }
  107. func SaveDialysisSetting(setting models.XtDialysisSetting) error {
  108. err := XTWriteDB().Create(&setting).Error
  109. return err
  110. }
  111. func GetDialysisSetting(orgid int64) (models.XtDialysisSetting, error) {
  112. setting := models.XtDialysisSetting{}
  113. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&setting).Error
  114. return setting, err
  115. }
  116. func GetDialysisParameterList(keywords string, limit int64, page int64, partition_id []string, schedule_type int64, scheduleDate int64, orgID int64, ids []string) (schedule []*models.VmBloodSchedule, total int64, err error) {
  117. db := XTReadDB().Model(&models.VmBloodSchedule{}).Where("xt_schedule.status = 1")
  118. offset := (page - 1) * limit
  119. if scheduleDate > 0 {
  120. db = db.Where("xt_schedule.schedule_date = ?", scheduleDate)
  121. }
  122. if schedule_type > 0 {
  123. db = db.Where("xt_schedule.schedule_type = ?", schedule_type)
  124. }
  125. //if partition_id > 0 {
  126. // db = db.Where("xt_schedule.partition_id = ?", partition_id)
  127. //}
  128. if len(partition_id) > 0 {
  129. db = db.Where("xt_schedule.partition_id in(?)", partition_id)
  130. }
  131. if orgID > 0 {
  132. db = db.Where("xt_schedule.user_org_id = ?", orgID)
  133. }
  134. if len(ids) >= 1 {
  135. db = db.Where("xt_schedule.id in(?)", ids)
  136. }
  137. if len(keywords) > 0 {
  138. keywords = "%" + keywords + "%"
  139. db = db.Joins("JOIN xt_patients AS patient ON patient.id=xt_schedule.patient_id AND patient.status = 1 AND patient.user_org_id = ? AND patient.name Like ?", orgID, keywords)
  140. err = db.Count(&total).Offset(offset).Limit(limit).Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  141. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  142. Preload("DialysisSolution", "status = 1 AND user_org_id = ? and solution_status = 1", orgID).
  143. Preload("DialysisPrescription", "status = 1 AND user_org_id = ? and record_date = ?", orgID, scheduleDate).
  144. Preload("XtAssessmentBeforeDislysis", "status = 1 AND user_org_id = ? and assessment_date = ?", orgID, scheduleDate).
  145. Preload("ReceiveTreatmentAsses", "status = 1 AND user_org_id = ? and record_date = ?", orgID, scheduleDate).
  146. Preload("XtAssessmentAfterDislysis", "status = 1 AND user_org_id = ? and assessment_date = ?", orgID, scheduleDate).
  147. Preload("LastAfterWeight", func(db *gorm.DB) *gorm.DB {
  148. return db.Where("user_org_id = ? and status = 1 and assessment_date < ?", orgID, scheduleDate)
  149. }).
  150. Preload("XtDoctorAdvice", "status = 1 AND user_org_id = ? and advice_date = ? and advice_type = 1", orgID, scheduleDate).
  151. Preload("XtDoctorAdviceOne", "status = 1 AND user_org_id = ? and advice_date = ? and (advice_type = 2 or advice_type =3)", orgID, scheduleDate).
  152. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).Find(&schedule).Error
  153. } else {
  154. err = db.Count(&total).Offset(offset).Limit(limit).Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  155. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  156. Preload("DialysisSolution", "status = 1 AND user_org_id = ? and solution_status = 1", orgID).
  157. Preload("DialysisPrescription", "status = 1 AND user_org_id = ? and record_date = ?", orgID, scheduleDate).
  158. Preload("XtAssessmentBeforeDislysis", "status = 1 AND user_org_id = ? and assessment_date = ?", orgID, scheduleDate).
  159. Preload("ReceiveTreatmentAsses", "status = 1 AND user_org_id = ? and record_date = ?", orgID, scheduleDate).
  160. Preload("XtAssessmentAfterDislysis", "status = 1 AND user_org_id = ? and assessment_date = ?", orgID, scheduleDate).
  161. Preload("XtDoctorAdvice", "status = 1 AND user_org_id = ? and advice_date = ? and advice_type = 1", orgID, scheduleDate).
  162. Preload("XtDoctorAdviceOne", "status = 1 AND user_org_id = ? and advice_date = ? and (advice_type = 2 or advice_type =3)", orgID, scheduleDate).
  163. Preload("LastAfterWeight", func(db *gorm.DB) *gorm.DB {
  164. return db.Where("user_org_id = ? and status = 1 and assessment_date < ?", orgID, scheduleDate)
  165. }).Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).Find(&schedule).Error
  166. }
  167. return schedule, total, err
  168. }
  169. func GetDialysisGoodTotalCount(orgID int64, schedule_type int64, partition_id []string, scheduleDate int64) (schedule []*models.VmBloodSchedule, err error) {
  170. db := XTReadDB().Model(&models.VmBloodSchedule{}).Where("status = 1")
  171. if scheduleDate > 0 {
  172. db = db.Where("schedule_date = ?", scheduleDate)
  173. }
  174. if schedule_type > 0 {
  175. db = db.Where("schedule_type = ?", schedule_type)
  176. }
  177. //if partition_id > 0 {
  178. // db = db.Where("partition_id = ?", partition_id)
  179. //}
  180. if len(partition_id) > 0 {
  181. db = db.Where("partition_id in(?)", partition_id)
  182. }
  183. if orgID > 0 {
  184. db = db.Where("user_org_id = ?", orgID)
  185. }
  186. err = db.Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  187. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  188. Preload("DialysisPrescription", "status = 1 AND user_org_id = ? and record_date = ?", orgID, scheduleDate).
  189. Preload("DialysisSolution", "status = 1 AND user_org_id = ? AND solution_status = 1", orgID).
  190. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).Find(&schedule).Error
  191. return schedule, err
  192. }
  193. func GetDialysisAdviceSchedulist(orgID int64, schedule_type int64, partition_id []string, scheduleDate int64) (schedule []*models.VmBloodScheduleOne, err error) {
  194. db := XTReadDB().Model(&models.VmBloodScheduleOne{}).Where("status = 1")
  195. if scheduleDate > 0 {
  196. db = db.Where("schedule_date = ?", scheduleDate)
  197. }
  198. if schedule_type > 0 {
  199. db = db.Where("schedule_type = ?", schedule_type)
  200. }
  201. //if partition_id > 0 {
  202. // db = db.Where("partition_id = ?", partition_id)
  203. //}
  204. if len(partition_id) > 0 {
  205. db = db.Where("partition_id in(?)", partition_id)
  206. }
  207. if orgID > 0 {
  208. db = db.Where("user_org_id = ?", orgID)
  209. }
  210. err = db.Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  211. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  212. Preload("XtDoctorAdvice", "status = 1 AND user_org_id = ? and advice_date = ? and advice_type = 2", orgID, scheduleDate).
  213. Preload("HisDoctorAdvice", func(db *gorm.DB) *gorm.DB {
  214. return db.Where("user_org_id = ? and status = 1 and advice_date = ?", orgID, scheduleDate).Preload("Drug", "org_id =? and status = 1", orgID)
  215. }).Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).Find(&schedule).Error
  216. return schedule, err
  217. }
  218. func GetDialysisAdviceSchedulistTwo(orgID int64, schedule_type int64, partition_id []string, scheduleDate int64) (schedule []*models.VmBloodScheduleTwo, err error) {
  219. db := XTReadDB().Model(&models.VmBloodScheduleTwo{}).Where("status = 1")
  220. if scheduleDate > 0 {
  221. db = db.Where("schedule_date = ?", scheduleDate)
  222. }
  223. if schedule_type > 0 {
  224. db = db.Where("schedule_type = ?", schedule_type)
  225. }
  226. //if partition_id > 0 {
  227. // db = db.Where("partition_id = ?", partition_id)
  228. //}
  229. if len(partition_id) > 0 {
  230. db = db.Where("partition_id in(?)", partition_id)
  231. }
  232. if orgID > 0 {
  233. db = db.Where("user_org_id = ?", orgID)
  234. }
  235. err = db.Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  236. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  237. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  238. Preload("HisPrescriptionTemplateSix", func(db *gorm.DB) *gorm.DB {
  239. return db.Where("user_org_id = ? and status = 1", orgID).Preload("HisPrescriptionInfoTemplateSix", func(db *gorm.DB) *gorm.DB {
  240. return db.Where("user_org_id = ? and status = 1 and type = 1", orgID).Preload("HisPrescriptionAdviceTemplate", func(db *gorm.DB) *gorm.DB {
  241. return db.Where("user_org_id = ? and status = 1", orgID).Preload("Drug", "org_id =? and status = 1", orgID)
  242. })
  243. })
  244. }).Find(&schedule).Error
  245. return schedule, err
  246. }
  247. func GetDialysisAdviceSchedulistSix(keywords string, limit int64, page int64, partition_id int64, schedule_type int64, scheduleDate int64, orgID int64, idArray []string) (schedule []*models.VmBloodScheduleTwo, total int64, err error) {
  248. db := XTReadDB().Model(&models.VmBloodScheduleTwo{}).Where("xt_schedule.status = 1")
  249. offset := (page - 1) * limit
  250. if scheduleDate > 0 {
  251. db = db.Where("xt_schedule.schedule_date = ?", scheduleDate)
  252. }
  253. if schedule_type > 0 {
  254. db = db.Where("xt_schedule.schedule_type = ?", schedule_type)
  255. }
  256. if partition_id > 0 {
  257. db = db.Where("xt_schedule.partition_id = ?", partition_id)
  258. }
  259. if orgID > 0 {
  260. db = db.Where("xt_schedule.user_org_id = ?", orgID)
  261. }
  262. if len(idArray) >= 1 {
  263. db = db.Where("xt_schedule.id in(?)", idArray)
  264. }
  265. if len(keywords) > 0 {
  266. keywords = "%" + keywords + "%"
  267. db = db.Joins("JOIN xt_patients AS patient ON patient.id=xt_schedule.patient_id AND patient.status = 1 AND patient.user_org_id = ? AND patient.name Like ?", orgID, keywords)
  268. err = db.Count(&total).Offset(offset).Limit(limit).Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  269. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  270. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  271. Preload("HisPrescriptionTemplateSix", func(db *gorm.DB) *gorm.DB {
  272. return db.Where("user_org_id = ? and status = 1", orgID).Preload("HisPrescriptionInfoTemplateSix", func(db *gorm.DB) *gorm.DB {
  273. return db.Where("user_org_id = ? and status = 1 and type = 1", orgID).Preload("HisPrescriptionAdviceTemplate", func(db *gorm.DB) *gorm.DB {
  274. return db.Where("user_org_id = ? and status = 1", orgID).Preload("Drug", "org_id =? and status = 1", orgID)
  275. })
  276. })
  277. }).Find(&schedule).Error
  278. } else {
  279. err = db.Count(&total).Offset(offset).Limit(limit).Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  280. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  281. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  282. Preload("HisPrescriptionTemplateSix", func(db *gorm.DB) *gorm.DB {
  283. return db.Where("user_org_id = ? and status = 1", orgID).Preload("HisPrescriptionInfoTemplateSix", func(db *gorm.DB) *gorm.DB {
  284. return db.Where("user_org_id = ? and status = 1 and type = 1", orgID).Preload("HisPrescriptionAdviceTemplate", func(db *gorm.DB) *gorm.DB {
  285. return db.Where("user_org_id = ? and status = 1", orgID).Preload("Drug", "org_id =? and status = 1", orgID)
  286. })
  287. })
  288. }).Find(&schedule).Error
  289. }
  290. return schedule, total, err
  291. }
  292. func GetAllDrugMap(org_id int64) (list []*BaseDrugLib, err error) {
  293. err = readDb.Model(&BaseDrugLib{}).Where("org_id = ? AND status = 1", org_id).Find(&list).Error
  294. return list, err
  295. }
  296. func GetDialysisParameterGoodList(orgID int64, schedule_type int64, partition_id int64, scheduleDate int64) (schedule []*models.VmBloodSchedule, err error) {
  297. db := XTReadDB().Model(&models.VmBloodSchedule{}).Where("status = 1")
  298. if scheduleDate > 0 {
  299. db = db.Where("schedule_date = ?", scheduleDate)
  300. }
  301. if schedule_type > 0 {
  302. db = db.Where("schedule_type = ?", schedule_type)
  303. }
  304. if partition_id > 0 {
  305. db = db.Where("partition_id = ?", partition_id)
  306. }
  307. if orgID > 0 {
  308. db = db.Where("user_org_id = ?", orgID)
  309. }
  310. err = db.Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  311. Preload("DialysisSolution", "status=1 and user_org_id = ? and solution_status = 1", orgID).
  312. Preload("DialysisPrescription", "status = 1 and user_org_id = ?", orgID).
  313. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  314. Preload("XtDoctorAdvice", "status = 1 AND user_org_id = ? and advice_date = ?", orgID, scheduleDate).
  315. Preload("HisDoctorAdvice", "status = 1 AND user_org_id = ? and advice_date = ?", orgID, scheduleDate).
  316. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).Find(&schedule).Error
  317. return schedule, err
  318. }
  319. func SaveHisDialysis(orgid int64, ids []string) error {
  320. goodsType := models.BloodGoodsType{}
  321. err := XTWriteDB().Model(&goodsType).Where("org_id = ? and status = 1 and id in(?)", orgid, ids).Update(map[string]interface{}{"is_open": 1}).Error
  322. err = XTWriteDB().Model(&goodsType).Where("org_id = ? and status = 1 and id not in(?)", orgid, ids).Update(map[string]interface{}{"is_open": 0}).Error
  323. return err
  324. }
  325. func GetHisPatientDialysisSolutionGroupList(keywords string, limit int64, page int64, partition_id int64, schedule_type int64, scheduleDate int64, orgID int64, ids []string) (schedule []*models.VmBloodScheduleTwo, total int64, err error) {
  326. db := XTReadDB().Model(&models.VmBloodScheduleTwo{}).Where("xt_schedule.status = 1")
  327. offset := (page - 1) * limit
  328. if scheduleDate > 0 {
  329. db = db.Where("xt_schedule.schedule_date = ?", scheduleDate)
  330. }
  331. if schedule_type > 0 {
  332. db = db.Where("xt_schedule.schedule_type = ?", schedule_type)
  333. }
  334. if partition_id > 0 {
  335. db = db.Where("xt_schedule.partition_id = ?", partition_id)
  336. }
  337. if orgID > 0 {
  338. db = db.Where("xt_schedule.user_org_id = ?", orgID)
  339. }
  340. if len(ids) >= 1 {
  341. db = db.Where("xt_schedule.id in(?)", ids)
  342. }
  343. if len(keywords) > 0 {
  344. keywords = "%" + keywords + "%"
  345. db = db.Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  346. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  347. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  348. Preload("HisPrescriptionTemplateSix", func(db *gorm.DB) *gorm.DB {
  349. return db.Where("user_org_id = ? and status = 1", orgID).Preload("HisPrescriptionInfoTemplateSix", func(db *gorm.DB) *gorm.DB {
  350. return db.Where("user_org_id = ? and status = 1 and type = 2", orgID).Preload("HisPrescriptionProjectTemplateSeven", func(db *gorm.DB) *gorm.DB {
  351. return db.Where("user_org_id = ? and status = 1 and type = 3 and (frequency_type =1 or (frequency_type =2 and day_count >0) or (frequency_type =3 and week_day<>''))", orgID).Preload("GoodInfo", "org_id =? and status = 1", orgID)
  352. })
  353. })
  354. })
  355. db = db.Joins("JOIN xt_patients AS patient ON patient.id=xt_schedule.patient_id AND patient.status = 1 AND patient.user_org_id = ? AND patient.name Like ?", orgID, keywords)
  356. err = db.Count(&total).Offset(offset).Limit(limit).Find(&schedule).Error
  357. } else {
  358. err = db.Count(&total).Offset(offset).Limit(limit).
  359. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  360. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  361. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  362. Preload("HisPrescriptionTemplateSix", func(db *gorm.DB) *gorm.DB {
  363. return db.Where("user_org_id = ? and status = 1", orgID).Preload("HisPrescriptionInfoTemplateSix", func(db *gorm.DB) *gorm.DB {
  364. return db.Where("user_org_id = ? and status = 1 and type = 2", orgID).Preload("HisPrescriptionProjectTemplateSeven", func(db *gorm.DB) *gorm.DB {
  365. return db.Where("user_org_id = ? and status = 1 and type = 3 and (frequency_type =1 or (frequency_type =2 and day_count >0) or (frequency_type =3 and week_day<>''))", orgID).Preload("GoodInfo", "org_id =? and status = 1", orgID)
  366. })
  367. })
  368. }).Find(&schedule).Error
  369. }
  370. return schedule, total, err
  371. }
  372. func GetHisDialysisGoodCount(orgID int64, schedule_type int64, partition_id int64, scheduleDate int64) (schedule []*models.VmBloodScheduleTwo, err error) {
  373. db := XTReadDB().Model(&models.VmBloodScheduleTwo{}).Where("xt_schedule.status = 1")
  374. if scheduleDate > 0 {
  375. db = db.Where("xt_schedule.schedule_date = ?", scheduleDate)
  376. }
  377. if schedule_type > 0 {
  378. db = db.Where("xt_schedule.schedule_type = ?", schedule_type)
  379. }
  380. if partition_id > 0 {
  381. db = db.Where("xt_schedule.partition_id = ?", partition_id)
  382. }
  383. if orgID > 0 {
  384. db = db.Where("xt_schedule.user_org_id = ?", orgID)
  385. }
  386. err = db.
  387. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  388. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  389. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  390. Preload("HisPrescriptionTemplateSix", func(db *gorm.DB) *gorm.DB {
  391. return db.Where("user_org_id = ? and status = 1", orgID).Preload("HisPrescriptionInfoTemplateSix", func(db *gorm.DB) *gorm.DB {
  392. return db.Where("user_org_id = ? and status = 1 and type = 2", orgID).Preload("HisPrescriptionProjectTemplateSeven", func(db *gorm.DB) *gorm.DB {
  393. return db.Where("user_org_id = ? and status = 1 and type = 3 and (frequency_type =1 or (frequency_type =2 and day_count >0) or (frequency_type =3 and week_day<>''))", orgID).Preload("GoodInfo", "org_id =? and status = 1", orgID)
  394. })
  395. })
  396. }).Find(&schedule).Error
  397. return schedule, err
  398. }
  399. func GeAdviceListToday(user_org_id int64, advice_date int64) (advice []*models.DoctorAdvice, err error) {
  400. err = XTReadDB().Where("user_org_id = ? and status = 1 and advice_date = ?", user_org_id, advice_date).Find(&advice).Error
  401. return advice, err
  402. }
  403. func GetHisAdviceListToday(user_org_id int64, advice_date int64) (advice []*models.HisDoctorAdviceFourty, err error) {
  404. err = XTReadDB().Where("user_org_id = ? and status = 1 and advice_date = ?", user_org_id, advice_date).Preload("Drug", "org_id = ? and status=1", user_org_id).Find(&advice).Error
  405. return advice, err
  406. }
  407. func GetDialysisAdviceSchedulistSeven(orgID int64, schedule_type int64, partition_id int64, scheduleDate int64) (schedule []*models.VmBloodScheduleOne, err error) {
  408. db := XTReadDB().Model(&models.VmBloodScheduleOne{}).Where("status = 1")
  409. if scheduleDate > 0 {
  410. db = db.Where("schedule_date = ?", scheduleDate)
  411. }
  412. if schedule_type > 0 {
  413. db = db.Where("schedule_type = ?", schedule_type)
  414. }
  415. if partition_id > 0 {
  416. db = db.Where("partition_id = ?", partition_id)
  417. }
  418. if orgID > 0 {
  419. db = db.Where("user_org_id = ?", orgID)
  420. }
  421. err = db.Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  422. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  423. Preload("XtDoctorAdvice", "status = 1 AND user_org_id = ? and advice_date = ? and advice_type = 2", orgID, scheduleDate).
  424. Preload("HisDoctorAdvice", func(db *gorm.DB) *gorm.DB {
  425. return db.Where("user_org_id = ? and status = 1 and advice_date = ?", orgID, scheduleDate).Preload("Drug", "org_id =? and status = 1", orgID)
  426. }).Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).Find(&schedule).Error
  427. return schedule, err
  428. }
  429. func GetDialysisAdviceSchedulistTen(orgID int64, schedule_type int64, partition_id []string, scheduleDate int64) (schedule []*models.VmBloodScheduleOne, err error) {
  430. db := XTReadDB().Model(&models.VmBloodScheduleOne{}).Where("status = 1")
  431. if scheduleDate > 0 {
  432. db = db.Where("schedule_date = ?", scheduleDate)
  433. }
  434. if schedule_type > 0 {
  435. db = db.Where("schedule_type = ?", schedule_type)
  436. }
  437. if len(partition_id) > 0 {
  438. db = db.Where("partition_id in(?)", partition_id)
  439. }
  440. if orgID > 0 {
  441. db = db.Where("user_org_id = ?", orgID)
  442. }
  443. err = db.Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  444. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  445. Preload("XtDoctorAdvice", "status = 1 AND user_org_id = ? and advice_date = ? and advice_type = 2", orgID, scheduleDate).
  446. Preload("HisDoctorAdvice", func(db *gorm.DB) *gorm.DB {
  447. return db.Where("user_org_id = ? and status = 1 and advice_date = ?", orgID, scheduleDate).Preload("Drug", "org_id =? and status = 1", orgID)
  448. }).Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).Find(&schedule).Error
  449. return schedule, err
  450. }
  451. func GetHisDialysisGoodCountTwo(orgID int64, schedule_type int64, partition_id []string, scheduleDate int64) (schedule []*models.VmBloodScheduleTwo, err error) {
  452. db := XTReadDB().Model(&models.VmBloodScheduleTwo{}).Where("xt_schedule.status = 1")
  453. if scheduleDate > 0 {
  454. db = db.Where("xt_schedule.schedule_date = ?", scheduleDate)
  455. }
  456. if schedule_type > 0 {
  457. db = db.Where("xt_schedule.schedule_type = ?", schedule_type)
  458. }
  459. if len(partition_id) > 0 {
  460. db = db.Where("xt_schedule.partition_id in (?)", partition_id)
  461. }
  462. if orgID > 0 {
  463. db = db.Where("xt_schedule.user_org_id = ?", orgID)
  464. }
  465. err = db.
  466. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  467. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  468. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  469. Preload("HisPrescriptionTemplateSix", func(db *gorm.DB) *gorm.DB {
  470. return db.Where("user_org_id = ? and status = 1", orgID).Preload("HisPrescriptionInfoTemplateSix", func(db *gorm.DB) *gorm.DB {
  471. return db.Where("user_org_id = ? and status = 1 and type = 2", orgID).Preload("HisPrescriptionProjectTemplateSeven", func(db *gorm.DB) *gorm.DB {
  472. return db.Where("user_org_id = ? and status = 1 and type = 3 and (frequency_type =1 or (frequency_type =2 and day_count >0) or (frequency_type =3 and week_day<>''))", orgID).Preload("GoodInfo", "org_id =? and status = 1", orgID)
  473. })
  474. })
  475. }).Find(&schedule).Error
  476. return schedule, err
  477. }
  478. func GetLongDialysisAdviceToday(orgID int64, schedule_type int64, partition_id []string, scheduleDate int64) (schedule []*models.VmLongBloodSchedule, err error) {
  479. db := XTReadDB().Model(&models.VmLongBloodSchedule{}).Where("status = 1")
  480. if scheduleDate > 0 {
  481. db = db.Where("schedule_date = ?", scheduleDate)
  482. }
  483. if schedule_type > 0 {
  484. db = db.Where("schedule_type = ?", schedule_type)
  485. }
  486. if len(partition_id) > 0 {
  487. db = db.Where("partition_id in(?)", partition_id)
  488. }
  489. if orgID > 0 {
  490. db = db.Where("user_org_id = ?", orgID)
  491. }
  492. err = db.Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  493. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  494. Preload("XtDoctorAdvice", "status = 1 AND user_org_id = ? and advice_type = 1", orgID).
  495. Preload("HisDoctorAdvice", func(db *gorm.DB) *gorm.DB {
  496. return db.Where("user_org_id = ? and status = 1 and advice_date = ?", orgID, scheduleDate).Preload("Drug", "org_id =? and status = 1", orgID)
  497. }).Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).Find(&schedule).Error
  498. return schedule, err
  499. }
  500. func GetDialysisGatherList(keywords string, limit int64, page int64, partition_id []string, schedule_type int64, scheduleDate int64, orgID int64, ids []string) (schedule []*models.VmBloodSchedule, total int64, err error) {
  501. db := XTReadDB().Model(&models.VmBloodSchedule{}).Where("xt_schedule.status = 1")
  502. offset := (page - 1) * limit
  503. if scheduleDate > 0 {
  504. db = db.Where("xt_schedule.schedule_date = ?", scheduleDate)
  505. }
  506. if schedule_type > 0 {
  507. db = db.Where("xt_schedule.schedule_type = ?", schedule_type)
  508. }
  509. //if partition_id > 0 {
  510. // db = db.Where("xt_schedule.partition_id = ?", partition_id)
  511. //}
  512. if len(partition_id) > 0 {
  513. db = db.Where("xt_schedule.partition_id in(?)", partition_id)
  514. }
  515. if orgID > 0 {
  516. db = db.Where("xt_schedule.user_org_id = ?", orgID)
  517. }
  518. if len(ids) >= 1 {
  519. db = db.Where("xt_schedule.id in(?)", ids)
  520. }
  521. if len(keywords) > 0 {
  522. keywords = "%" + keywords + "%"
  523. db = db.Joins("JOIN xt_patients AS patient ON patient.id=xt_schedule.patient_id AND patient.status = 1 AND patient.user_org_id = ? AND patient.name Like ?", orgID, keywords)
  524. err = db.Count(&total).Offset(offset).Limit(limit).Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  525. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  526. Preload("DialysisSolution", "status = 1 AND user_org_id = ? and solution_status = 1", orgID).
  527. Preload("DialysisPrescription", "status = 1 AND user_org_id = ? and record_date = ?", orgID, scheduleDate).
  528. Preload("XtAssessmentBeforeDislysis", "status = 1 AND user_org_id = ? and assessment_date = ?", orgID, scheduleDate).
  529. Preload("ReceiveTreatmentAsses", "status = 1 AND user_org_id = ? and record_date = ?", orgID, scheduleDate).
  530. Preload("XtAssessmentAfterDislysis", "status = 1 AND user_org_id = ? and assessment_date = ?", orgID, scheduleDate).
  531. Preload("LastAfterWeight", func(db *gorm.DB) *gorm.DB {
  532. return db.Where("user_org_id = ? and status = 1 and assessment_date < ?", orgID, scheduleDate)
  533. }).
  534. Preload("XtDoctorAdvice", "status = 1 AND user_org_id = ? and advice_date = ? and advice_type = 1", orgID, scheduleDate).
  535. Preload("XtDoctorAdviceOne", "status = 1 AND user_org_id = ? and advice_date = ? and (advice_type = 2 or advice_type =3)", orgID, scheduleDate).
  536. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).Find(&schedule).Error
  537. } else {
  538. err = db.Count(&total).Offset(offset).Limit(limit).Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  539. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  540. Preload("DialysisSolution", "status = 1 AND user_org_id = ? and solution_status = 1", orgID).
  541. Preload("DialysisPrescription", "status = 1 AND user_org_id = ? and record_date = ?", orgID, scheduleDate).
  542. Preload("XtAssessmentBeforeDislysis", "status = 1 AND user_org_id = ? and assessment_date = ?", orgID, scheduleDate).
  543. Preload("ReceiveTreatmentAsses", "status = 1 AND user_org_id = ? and record_date = ?", orgID, scheduleDate).
  544. Preload("XtAssessmentAfterDislysis", "status = 1 AND user_org_id = ? and assessment_date = ?", orgID, scheduleDate).
  545. Preload("XtDoctorAdvice", "status = 1 AND user_org_id = ? and advice_date = ? and advice_type = 1", orgID, scheduleDate).
  546. Preload("XtDoctorAdviceOne", "status = 1 AND user_org_id = ? and advice_date = ? and (advice_type = 2 or advice_type =3)", orgID, scheduleDate).
  547. Preload("LastAfterWeight", func(db *gorm.DB) *gorm.DB {
  548. return db.Where("user_org_id = ? and status = 1 and assessment_date < ?", orgID, scheduleDate)
  549. }).Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).Find(&schedule).Error
  550. }
  551. return schedule, total, err
  552. }