dialysis_solution_service.go 16KB

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