dialysis_solution_service.go 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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) (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(keywords) > 0 {
  45. keywords = "%" + keywords + "%"
  46. db = db.Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  47. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  48. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  49. Preload("DialysisSolution", "status = 1 AND user_org_id = ? and solution_status = 1", orgID)
  50. 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)
  51. err = db.Count(&total).Offset(offset).Limit(limit).Find(&schedule).Error
  52. } else {
  53. err = db.Count(&total).Offset(offset).Limit(limit).Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  54. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  55. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  56. Preload("DialysisSolution", "status = 1 AND user_org_id = ? and solution_status = 1", orgID).Find(&schedule).Error
  57. }
  58. return schedule, total, err
  59. }
  60. func GetDialysisAdviceTemplateList(keywords string, limit int64, page int64, partition_id int64, schedule_type int64, scheduleDate int64, orgID int64) (schedule []*models.VmBloodSchedule, total int64, err error) {
  61. db := XTReadDB().Model(&models.VmBloodSchedule{}).Where("xt_schedule.status = 1")
  62. offset := (page - 1) * limit
  63. if scheduleDate > 0 {
  64. db = db.Where("xt_schedule.schedule_date = ?", scheduleDate)
  65. }
  66. if schedule_type > 0 {
  67. db = db.Where("xt_schedule.schedule_type = ?", schedule_type)
  68. }
  69. if partition_id > 0 {
  70. db = db.Where("xt_schedule.partition_id = ?", partition_id)
  71. }
  72. if orgID > 0 {
  73. db = db.Where("xt_schedule.user_org_id = ?", orgID)
  74. }
  75. if len(keywords) > 0 {
  76. keywords = "%" + keywords + "%"
  77. 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)
  78. err = db.Count(&total).Offset(offset).Limit(limit).Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  79. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  80. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).Find(&schedule).Error
  81. } else {
  82. err = db.Count(&total).Offset(offset).Limit(limit).Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  83. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  84. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).Find(&schedule).Error
  85. }
  86. return schedule, total, err
  87. }
  88. func GetAllLongAdviceList(user_org_id int64) (advice []*models.DoctorAdvice, err error) {
  89. err = XTReadDB().Where("user_org_id = ? and advice_type = 1 and status = 1", user_org_id).Find(&advice).Error
  90. return advice, err
  91. }
  92. func GetAllHisAdviceTemplateList(user_org_id int64) (advice []*models.HisPrescriptionAdviceTemplate, err error) {
  93. err = XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&advice).Error
  94. return advice, err
  95. }
  96. func SaveDialysisSetting(setting models.XtDialysisSetting) error {
  97. err := XTWriteDB().Create(&setting).Error
  98. return err
  99. }
  100. func GetDialysisSetting(orgid int64) (models.XtDialysisSetting, error) {
  101. setting := models.XtDialysisSetting{}
  102. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&setting).Error
  103. return setting, err
  104. }
  105. func GetDialysisParameterList(keywords string, limit int64, page int64, partition_id int64, schedule_type int64, scheduleDate int64, orgID int64) (schedule []*models.VmBloodSchedule, total int64, err error) {
  106. db := XTReadDB().Model(&models.VmBloodSchedule{}).Where("xt_schedule.status = 1")
  107. offset := (page - 1) * limit
  108. if scheduleDate > 0 {
  109. db = db.Where("xt_schedule.schedule_date = ?", scheduleDate)
  110. }
  111. if schedule_type > 0 {
  112. db = db.Where("xt_schedule.schedule_type = ?", schedule_type)
  113. }
  114. if partition_id > 0 {
  115. db = db.Where("xt_schedule.partition_id = ?", partition_id)
  116. }
  117. if orgID > 0 {
  118. db = db.Where("xt_schedule.user_org_id = ?", orgID)
  119. }
  120. if len(keywords) > 0 {
  121. keywords = "%" + keywords + "%"
  122. 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)
  123. err = db.Count(&total).Offset(offset).Limit(limit).Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  124. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  125. Preload("DialysisSolution", "status = 1 AND user_org_id = ?", orgID).
  126. Preload("DialysisPrescription", "status = 1 AND user_org_id = ? and record_date = ?", orgID, scheduleDate).
  127. Preload("XtAssessmentBeforeDislysis", "status = 1 AND user_org_id = ? and assessment_date = ?", orgID, scheduleDate).
  128. Preload("ReceiveTreatmentAsses", "status = 1 AND user_org_id = ? and record_date = ?", orgID, scheduleDate).
  129. Preload("XtAssessmentAfterDislysis", "status = 1 AND user_org_id = ? and assessment_date = ?", orgID, scheduleDate).
  130. Preload("LastAfterWeight", func(db *gorm.DB) *gorm.DB {
  131. return db.Where("user_org_id = ? and status = 1 and assessment_date < ?", orgID, scheduleDate)
  132. }).
  133. Preload("XtDoctorAdvice", "status = 1 AND user_org_id = ? and advice_date = ? and advice_type = 1", orgID, scheduleDate).
  134. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).Find(&schedule).Error
  135. } else {
  136. err = db.Count(&total).Offset(offset).Limit(limit).Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  137. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  138. Preload("DialysisSolution", "status = 1 AND user_org_id = ?", orgID).
  139. Preload("DialysisPrescription", "status = 1 AND user_org_id = ? and record_date = ?", orgID, scheduleDate).
  140. Preload("XtAssessmentBeforeDislysis", "status = 1 AND user_org_id = ? and assessment_date = ?", orgID, scheduleDate).
  141. Preload("ReceiveTreatmentAsses", "status = 1 AND user_org_id = ? and record_date = ?", orgID, scheduleDate).
  142. Preload("XtAssessmentAfterDislysis", "status = 1 AND user_org_id = ? and assessment_date = ?", orgID, scheduleDate).
  143. Preload("XtDoctorAdvice", "status = 1 AND user_org_id = ? and advice_date = ? and advice_type = 1", orgID, scheduleDate).
  144. Preload("LastAfterWeight", func(db *gorm.DB) *gorm.DB {
  145. return db.Where("user_org_id = ? and status = 1 and assessment_date < ?", orgID, scheduleDate)
  146. }).Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).Find(&schedule).Error
  147. }
  148. return schedule, total, err
  149. }
  150. func GetDialysisGoodTotalCount(orgID int64, schedule_type int64, partition_id int64, scheduleDate int64) (schedule []*models.VmBloodSchedule, err error) {
  151. db := XTReadDB().Model(&models.VmBloodSchedule{}).Where("status = 1")
  152. if scheduleDate > 0 {
  153. db = db.Where("schedule_date = ?", scheduleDate)
  154. }
  155. if schedule_type > 0 {
  156. db = db.Where("schedule_type = ?", schedule_type)
  157. }
  158. if partition_id > 0 {
  159. db = db.Where("partition_id = ?", partition_id)
  160. }
  161. if orgID > 0 {
  162. db = db.Where("user_org_id = ?", orgID)
  163. }
  164. err = db.Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  165. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  166. Preload("DialysisPrescription", "status = 1 AND user_org_id = ? and record_date = ?", orgID, scheduleDate).
  167. Preload("DialysisSolution", "status = 1 AND user_org_id = ? AND solution_status = 1", orgID).
  168. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).Find(&schedule).Error
  169. return schedule, err
  170. }
  171. func GetDialysisAdviceSchedulist(orgID int64, schedule_type int64, partition_id int64, scheduleDate int64) (schedule []*models.VmBloodSchedule, err error) {
  172. db := XTReadDB().Model(&models.VmBloodSchedule{}).Where("status = 1")
  173. if scheduleDate > 0 {
  174. db = db.Where("schedule_date = ?", scheduleDate)
  175. }
  176. if schedule_type > 0 {
  177. db = db.Where("schedule_type = ?", schedule_type)
  178. }
  179. if partition_id > 0 {
  180. db = db.Where("partition_id = ?", partition_id)
  181. }
  182. if orgID > 0 {
  183. db = db.Where("user_org_id = ?", orgID)
  184. }
  185. err = db.Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  186. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  187. Preload("XtDoctorAdvice", "status = 1 AND user_org_id = ? and advice_date = ?", orgID, scheduleDate).
  188. Preload("HisDoctorAdvice", "status = 1 AND user_org_id = ? and advice_date = ?", orgID, scheduleDate).
  189. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).Find(&schedule).Error
  190. return schedule, err
  191. }
  192. func GetAllDrugMap(org_id int64) (list []*BaseDrugLib, err error) {
  193. err = readDb.Model(&BaseDrugLib{}).Where("org_id = ? AND status = 1", org_id).Find(&list).Error
  194. return list, err
  195. }
  196. func GetDialysisParameterGoodList(orgID int64, schedule_type int64, partition_id int64, scheduleDate int64) (schedule []*models.VmBloodSchedule, err error) {
  197. db := XTReadDB().Model(&models.VmBloodSchedule{}).Where("status = 1")
  198. if scheduleDate > 0 {
  199. db = db.Where("schedule_date = ?", scheduleDate)
  200. }
  201. if schedule_type > 0 {
  202. db = db.Where("schedule_type = ?", schedule_type)
  203. }
  204. if partition_id > 0 {
  205. db = db.Where("partition_id = ?", 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("DialysisPrescription", "status = 1 and user_org_id = ?", orgID).
  212. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  213. Preload("XtDoctorAdvice", "status = 1 AND user_org_id = ? and advice_date = ?", orgID, scheduleDate).
  214. Preload("HisDoctorAdvice", "status = 1 AND user_org_id = ? and advice_date = ?", orgID, scheduleDate).
  215. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).Find(&schedule).Error
  216. return schedule, err
  217. }
  218. func SaveHisDialysis(orgid int64, ids []string) error {
  219. goodsType := models.BloodGoodsType{}
  220. err := XTWriteDB().Model(&goodsType).Where("org_id = ? and status = 1 and id in(?)", orgid, ids).Update(map[string]interface{}{"is_open": 1}).Error
  221. err = XTWriteDB().Model(&goodsType).Where("org_id = ? and status = 1 and id not in(?)", orgid, ids).Update(map[string]interface{}{"is_open": 0}).Error
  222. return err
  223. }
  224. func GetHisPatientDialysisSolutionGroupList(keywords string, limit int64, page int64, partition_id int64, schedule_type int64, scheduleDate int64, orgID int64) (schedule []*models.VmBloodScheduleTwo, total int64, err error) {
  225. db := XTReadDB().Model(&models.VmBloodScheduleTwo{}).Where("xt_schedule.status = 1")
  226. offset := (page - 1) * limit
  227. if scheduleDate > 0 {
  228. db = db.Where("xt_schedule.schedule_date = ?", scheduleDate)
  229. }
  230. if schedule_type > 0 {
  231. db = db.Where("xt_schedule.schedule_type = ?", schedule_type)
  232. }
  233. if partition_id > 0 {
  234. db = db.Where("xt_schedule.partition_id = ?", partition_id)
  235. }
  236. if orgID > 0 {
  237. db = db.Where("xt_schedule.user_org_id = ?", orgID)
  238. }
  239. if len(keywords) > 0 {
  240. keywords = "%" + keywords + "%"
  241. db = db.Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  242. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  243. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  244. Preload("HisPrescriptionTemplateSix", func(db *gorm.DB) *gorm.DB {
  245. return db.Where("user_org_id = ? and status = 1", orgID).Preload("HisPrescriptionInfoTemplateSix", func(db *gorm.DB) *gorm.DB {
  246. return db.Where("user_org_id = ? and status = 1 and type = 2", orgID).Preload("HisPrescriptionProjectTemplateSeven", func(db *gorm.DB) *gorm.DB {
  247. return db.Where("user_org_id = ? and status = 1 and type = 3", orgID).Preload("GoodInfo", "org_id =? and status = 1", orgID)
  248. })
  249. })
  250. })
  251. 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)
  252. err = db.Count(&total).Offset(offset).Limit(limit).Find(&schedule).Error
  253. } else {
  254. err = db.Count(&total).Offset(offset).Limit(limit).
  255. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  256. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  257. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  258. Preload("HisPrescriptionTemplateSix", func(db *gorm.DB) *gorm.DB {
  259. return db.Where("user_org_id = ? and status = 1", orgID).Preload("HisPrescriptionInfoTemplateSix", func(db *gorm.DB) *gorm.DB {
  260. return db.Where("user_org_id = ? and status = 1 and type = 2", orgID).Preload("HisPrescriptionProjectTemplateSeven", func(db *gorm.DB) *gorm.DB {
  261. return db.Where("user_org_id = ? and status = 1 and type = 3", orgID).Preload("GoodInfo", "org_id =? and status = 1", orgID)
  262. })
  263. })
  264. }).Find(&schedule).Error
  265. }
  266. return schedule, total, err
  267. }
  268. func GetHisDialysisGoodCount(orgID int64, schedule_type int64, partition_id int64, scheduleDate int64) (schedule []*models.VmBloodScheduleTwo, err error) {
  269. db := XTReadDB().Model(&models.VmBloodScheduleTwo{}).Where("xt_schedule.status = 1")
  270. if scheduleDate > 0 {
  271. db = db.Where("xt_schedule.schedule_date = ?", scheduleDate)
  272. }
  273. if schedule_type > 0 {
  274. db = db.Where("xt_schedule.schedule_type = ?", schedule_type)
  275. }
  276. if partition_id > 0 {
  277. db = db.Where("xt_schedule.partition_id = ?", partition_id)
  278. }
  279. if orgID > 0 {
  280. db = db.Where("xt_schedule.user_org_id = ?", orgID)
  281. }
  282. err = db.
  283. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  284. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  285. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  286. Preload("HisPrescriptionTemplateSix", func(db *gorm.DB) *gorm.DB {
  287. return db.Where("user_org_id = ? and status = 1", orgID).Preload("HisPrescriptionInfoTemplateSix", func(db *gorm.DB) *gorm.DB {
  288. return db.Where("user_org_id = ? and status = 1 and type = 2", orgID).Preload("HisPrescriptionProjectTemplateSeven", func(db *gorm.DB) *gorm.DB {
  289. return db.Where("user_org_id = ? and status = 1 and type = 3", orgID).Preload("GoodInfo", "org_id =? and status = 1", orgID)
  290. })
  291. })
  292. }).Find(&schedule).Error
  293. return schedule, err
  294. }