dialysis_parameter_service.go 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. package service
  2. import (
  3. "XT_New/models"
  4. "fmt"
  5. "github.com/jinzhu/gorm"
  6. )
  7. func GetDialysisParametersByKeyword(orgID int64, keyword string, schedulType int64, partitionType int64, page int64, limit int64, schedulDate int64) ([]*models.DialysisParameter, error, int64) {
  8. fmt.Println("scheduletye ======================", schedulType)
  9. var patients []*models.Patients
  10. getPatientErr := readDb.Model(&models.Patients{}).Where("status = 1 AND user_org_id = ? AND (name like ? OR dialysis_no like ?)", orgID, "%"+keyword+"%", "%"+keyword+"%").Find(&patients).Error
  11. if getPatientErr != nil {
  12. return nil, getPatientErr, 0
  13. }
  14. patientIDs := make([]int64, len(patients))
  15. for index, patient := range patients {
  16. patientIDs[index] = patient.ID
  17. }
  18. db := readDb.
  19. Model(&models.DialysisSchedule{}).
  20. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  21. // Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  22. Preload("TreatmentMode", "status = 1").
  23. Preload("Prescription", "status = 1 AND user_org_id = ?", orgID).
  24. Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ?", orgID).
  25. Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ?", orgID).
  26. Preload("MonitoringRecord", "status = 1 AND user_org_id = ?", orgID).
  27. Preload("DialysisOrder", "status = 1 AND user_org_id = ?", orgID).
  28. Preload("MonitorPatients", "status = 1 AND user_org_id = ?", orgID)
  29. db = db.Where("xt_schedule.status = 1 AND patient_id in (?)", patientIDs)
  30. if schedulType > 0 {
  31. db = db.Where("schedule_type = ?", schedulType)
  32. }
  33. if partitionType > 0 {
  34. db = db.Joins("inner join xt_device_number as d_n on d_n.id = xt_schedule.bed_id and d_n.zone_id = ? and d_n.status = 1", partitionType)
  35. }
  36. if schedulDate > 0 {
  37. db = db.Where("schedule_date = ?", schedulDate)
  38. }
  39. var schedules []*models.DialysisParameter
  40. total := int64(0)
  41. err := db.Count(&total).Offset(limit * (page - 1)).Limit(limit).Order("schedule_date desc").Find(&schedules).Error
  42. return schedules, err, total
  43. }
  44. func GetDialysisParameter(orgID int64, schedulDate int64, schedulType int64, partitionType int64, page int64, limit int64) (schedule []*models.DialysisParameter, err error, total int64) {
  45. db := readDb.
  46. Model(&models.MonitorDialysisSchedule{}).
  47. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  48. Preload("TreatmentMode", "status = 1").
  49. Preload("Prescription", "status = 1 AND user_org_id = ?", orgID).
  50. Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ?", orgID).
  51. Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ?", orgID).
  52. Preload("MonitoringRecord", func(db *gorm.DB) *gorm.DB {
  53. return db.Where("status = 1 AND user_org_id = ?", orgID).Order("operate_time asc")
  54. }).
  55. Preload("DialysisOrder", "status = 1 AND user_org_id = ?", orgID).
  56. Preload("MonitorPatients", "status = 1 AND user_org_id = ?", orgID)
  57. db = db.Where("xt_schedule.status = 1 AND user_org_id = ?", orgID)
  58. if schedulDate > 0 {
  59. db = db.Where("schedule_date = ?", schedulDate)
  60. }
  61. if schedulType > 0 {
  62. db = db.Where("schedule_type = ?", schedulType)
  63. }
  64. if partitionType > 0 {
  65. db = db.Joins("inner join xt_device_number on xt_device_number.id = xt_schedule.bed_id and xt_device_number.zone_id = ? and xt_device_number.status = 1", partitionType)
  66. }
  67. offset := (page - 1) * limit
  68. err = db.Count(&total).Offset(offset).Limit(limit).Order("id desc").Find(&schedule).Error
  69. return schedule, err, total
  70. }
  71. func GetDialysisBatchParameters(schIDs []string, orgID int64) (schedule []*models.DialysisParameter, err error) {
  72. db := readDb.
  73. Model(&models.DialysisSchedule{}).
  74. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  75. // Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  76. Preload("TreatmentMode", "status = 1").
  77. Preload("Prescription", "status = 1 AND user_org_id = ?", orgID).
  78. Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ?", orgID).
  79. Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ?", orgID).
  80. Preload("MonitoringRecord", "status = 1 AND user_org_id = ?", orgID).
  81. Preload("DialysisOrder", "status = 1 AND user_org_id = ?", orgID).
  82. Preload("MonitorPatients", "status = 1 AND user_org_id = ?", orgID)
  83. db = db.Where("xt_schedule.status = 1 AND id in (?)", schIDs)
  84. var schedules []*models.DialysisParameter
  85. err = db.Order("schedule_date desc").Find(&schedules).Error
  86. return schedules, err
  87. }
  88. func GetWareHouseOutList(startime int64, endtime int64, orgid int64) (warehouse []*models.XtWarehouseOutInfo, err error) {
  89. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status = 1")
  90. if startime > 0 {
  91. db = db.Where("x.sys_record_time >=?", startime)
  92. }
  93. if endtime > 0 {
  94. db = db.Where("x.sys_record_time<=?", endtime)
  95. }
  96. if orgid > 0 {
  97. db = db.Where("x.org_id = ?", orgid)
  98. }
  99. err = db.Select("x.id,x.warehouse_out_id,x.good_id,x.good_type_id,x.count,x.warehouse_out_order_number,x.sys_record_time,s.specification_name,t.type_name").Joins("left join xt_good_information as s on s.id = x.good_id").Where("s.status = 1 and s.org_id = ?", orgid).Joins("left join xt_goods_type as t on t.id = x.good_type_id").Order("x.id desc").Scan(&warehouse).Error
  100. return warehouse, err
  101. }
  102. func GetAllMaterial(startime int64, endtime int64, orgid int64) (reducedetail []*models.XtAutomaticReduceDetail, err error) {
  103. db := XTReadDB().Table("xt_automatic_reduce_detail as x").Where("x.status = 1")
  104. if startime > 0 {
  105. db = db.Where("x.record_time>=?", startime)
  106. }
  107. if endtime > 0 {
  108. db = db.Where("x.record_time<=?", endtime)
  109. }
  110. if orgid > 0 {
  111. db = db.Where("x.org_id = ?", orgid)
  112. }
  113. err = db.Select("x.patient_id,x.record_time,x.good_id,t.type_name").Joins("left join xt_goods_type as t on t.id = x.good_type_id").Group("x.good_type_id").Scan(&reducedetail).Error
  114. return reducedetail, err
  115. }
  116. func GetCollectList(limit int64, page int64, partitionType int64, schedulType int64, schedulDate int64, orgID int64, keyword string) ([]*models.DialysisParameter, error, int64) {
  117. var patients []*models.Patients
  118. getPatientErr := readDb.Model(&models.Patients{}).Where("status = 1 AND user_org_id = ? AND (name like ? OR dialysis_no like ?)", orgID, "%"+keyword+"%", "%"+keyword+"%").Find(&patients).Error
  119. if getPatientErr != nil {
  120. return nil, getPatientErr, 0
  121. }
  122. patientIDs := make([]int64, len(patients))
  123. for index, patient := range patients {
  124. patientIDs[index] = patient.ID
  125. }
  126. db := readDb.
  127. Model(&models.DialysisSchedule{}).
  128. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  129. // Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  130. Preload("TreatmentMode", "status = 1").
  131. Preload("Prescription", "status = 1 AND user_org_id = ?", orgID).
  132. Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ?", orgID).
  133. Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ?", orgID).
  134. Preload("MonitoringRecord", "status = 1 AND user_org_id = ?", orgID).
  135. Preload("DialysisOrder", "status = 1 AND user_org_id = ?", orgID).
  136. Preload("MonitorPatients", "status = 1 AND user_org_id = ?", orgID)
  137. db = db.Where("xt_schedule.status = 1 AND patient_id in (?)", patientIDs)
  138. if schedulType > 0 {
  139. db = db.Where("schedule_type = ?", schedulType)
  140. }
  141. if partitionType > 0 {
  142. db = db.Joins("inner join xt_device_number as d_n on d_n.id = xt_schedule.bed_id and d_n.zone_id = ? and d_n.status = 1", partitionType)
  143. // db = db.Where("partition_id = ?", partitionType)
  144. }
  145. if schedulDate > 0 {
  146. db = db.Where("schedule_date = ?", schedulDate)
  147. }
  148. var schedules []*models.DialysisParameter
  149. total := int64(0)
  150. err := db.Count(&total).Offset(limit * (page - 1)).Limit(limit).Order("schedule_date desc").Find(&schedules).Error
  151. return schedules, err, total
  152. }
  153. func GetDialysisConsumables(startime int64, endtime int64, orgid int64) (reducedetail []*models.XtAutomaticReduceDetail, err error) {
  154. db := XTReadDB().Table("xt_automatic_reduce_detail as x").Where("x.status = 1")
  155. table := XTReadDB().Table("xt_warehouse_out_info as f").Where("f.status = 1 and f.org_id = ?", orgid)
  156. fmt.Println(table)
  157. if startime > 0 {
  158. db = db.Where("x.record_time >=?", startime)
  159. }
  160. if endtime > 0 {
  161. db = db.Where("x.record_time <=?", endtime)
  162. }
  163. if orgid > 0 {
  164. db = db.Where("x.org_id =?", orgid)
  165. }
  166. err = db.Select("x.patient_id,x.count,x.good_id,x.good_type_id,t.specification_name,s.type_name").Joins("left join xt_good_information as t on t.id = x.good_id").Where("t.org_id = ? and t.status = 1", orgid).Joins("left join xt_goods_type as s on s.id = x.good_type_id").Joins("left join xt_warehouse_out_info as f on f.id = x.warehouse_out_id").Scan(&reducedetail).Error
  167. return reducedetail, err
  168. }
  169. func GetBatchCollection(orgID int64, schIDs []string) (schedules []*models.DialysisParameter, err error) {
  170. db := readDb.
  171. Model(&models.DialysisSchedule{}).
  172. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  173. // Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  174. Preload("TreatmentMode", "status = 1").
  175. Preload("Prescription", "status = 1 AND user_org_id = ?", orgID).
  176. Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ?", orgID).
  177. Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ?", orgID).
  178. Preload("MonitoringRecord", "status = 1 AND user_org_id = ?", orgID).
  179. Preload("DialysisOrder", "status = 1 AND user_org_id = ?", orgID).
  180. Preload("MonitorPatients", "status = 1 AND user_org_id = ?", orgID)
  181. db = db.Where("xt_schedule.status = 1 AND id in (?)", schIDs)
  182. err = db.Order("schedule_date desc").Find(&schedules).Error
  183. return schedules, err
  184. }
  185. func GetAnticoagulantCount(startime int64, endtime int64, orgid int64) (prescriptionCount []*models.DialysisPrescriptionCount, err error) {
  186. db := XTReadDB().Table("xt_dialysis_prescription as x").Where("x.status = 1")
  187. if orgid > 0 {
  188. db = db.Where("x.user_org_id = ? and x.anticoagulant <> 0", orgid)
  189. }
  190. if startime > 0 {
  191. db = db.Where("x.record_date >= ?", startime)
  192. }
  193. if endtime > 0 {
  194. db = db.Where("x.record_date<=?", endtime)
  195. }
  196. err = db.Select("x.patient_id,x.anticoagulant,x.record_date,count(id) as count").Group("x.anticoagulant").Scan(&prescriptionCount).Error
  197. return prescriptionCount, err
  198. }
  199. func GetDialysisTodaySchedulePatient(orgid int64, startime int64, page int64, limit int64, keywords string) (schedule []*models.XtScheduleTwo, err error) {
  200. //offset := (page - 1) * limit
  201. //err = XTReadDB().Model(&schedule).Where("user_org_id = ? and schedule_date = ? and status = 1", orgid, startime).Offset(offset).Limit(limit).Order("id desc").Find(&schedule).Error
  202. //return schedule, err
  203. keywords = "%" + keywords + "%"
  204. offset := (page - 1) * limit
  205. db := XTReadDB().Table("xt_schedule as x").Where("x.status = 1")
  206. table := XTReadDB().Table("xt_patients as s")
  207. fmt.Println(table)
  208. if len(keywords) > 0 {
  209. err = db.Group("x.id").Select("x.id,x.patient_id,x.mode_id,s.name").Joins("left join xt_patients as s on s.id = x.patient_id").Where("x.user_org_id = ? and x.schedule_date = ? and x.status = 1 and s.name like ?", orgid, startime, keywords).Offset(offset).Limit(limit).Order("id desc").Scan(&schedule).Error
  210. //db = db.Where("s.name like ?", keywords)
  211. } else {
  212. err = db.Group("x.id").Select("x.id,x.patient_id,x.mode_id,s.name").Joins("left join xt_patients as s on s.id = x.patient_id").Where("x.user_org_id = ? and x.schedule_date = ? and x.status = 1", orgid, startime).Offset(offset).Limit(limit).Order("id desc").Scan(&schedule).Error
  213. }
  214. return schedule, err
  215. }
  216. func GetToDayDialysisPrescription(patientid int64, orgid int64, startime int64) (*models.XtDialysisPrescription, error) {
  217. prescription := models.XtDialysisPrescription{}
  218. err := XTReadDB().Model(&prescription).Where("patient_id = ? and user_org_id = ? and record_date = ? and status = 1", patientid, orgid, startime).Find(&prescription).Error
  219. if err == gorm.ErrRecordNotFound {
  220. return nil, err
  221. }
  222. if err != nil {
  223. return nil, err
  224. }
  225. return &prescription, nil
  226. }
  227. func GetLastDialysisPrescription(patientid int64, orgid int64) (models.XtDialysisPrescription, error) {
  228. prescription := models.XtDialysisPrescription{}
  229. err = XTReadDB().Model(&prescription).Where("patient_id = ? and user_org_id = ? and status = 1", patientid, orgid).Last(&prescription).Error
  230. return prescription, err
  231. }
  232. func GetLastAssessmentBeforDialysis(patientid int64, orgid int64) (models.PredialysisEvaluation, error) {
  233. evaluation := models.PredialysisEvaluation{}
  234. err := XTReadDB().Model(&evaluation).Where("patient_id = ? and user_org_id = ? and status = 1", patientid, orgid).Last(&evaluation).Error
  235. return evaluation, err
  236. }
  237. func GetDialysisPrescriptionList(patientid int64, orgid int64, startime int64) (models.XtDialysisPrescription, error) {
  238. prescription := models.XtDialysisPrescription{}
  239. err := XTReadDB().Model(&prescription).Where("patient_id = ? and user_org_id = ? and status = 1 and record_date = ?", patientid, orgid, startime).Find(&prescription).Error
  240. return prescription, err
  241. }
  242. func GetTodayAssessmentBeforDialysis(patientid int64, orgid int64, startime int64) (*models.PredialysisEvaluation, error) {
  243. evaluation := models.PredialysisEvaluation{}
  244. err := XTReadDB().Model(&evaluation).Where("patient_id = ? and user_org_id = ? and assessment_date = ? and status = 1", patientid, orgid, startime).Find(&evaluation).Error
  245. if err == gorm.ErrRecordNotFound {
  246. return nil, err
  247. }
  248. if err != nil {
  249. return nil, err
  250. }
  251. return &evaluation, nil
  252. }
  253. func GetAssessmentBeforDialysisByStartime(patientid int64, orgid int64, startime int64) (models.PredialysisEvaluation, error) {
  254. evaluation := models.PredialysisEvaluation{}
  255. err := XTReadDB().Model(&evaluation).Where("patient_id = ? and user_org_id = ? and assessment_date = ? and status = 1", patientid, orgid, startime).Find(&evaluation).Error
  256. return evaluation, err
  257. }
  258. func GetAotoMaticReduceByPatientId(patientid int64, orgid int64, startime int64, endtime int64) (*models.BloodAutomaticReduceDetail, error) {
  259. detail := models.BloodAutomaticReduceDetail{}
  260. err := XTReadDB().Model(&detail).Where("patient_id = ? and org_id =? and record_time >=? and record_time <=? and status = 1", patientid, orgid, startime, endtime).Find(&detail).Error
  261. if err == gorm.ErrRecordNotFound {
  262. return nil, err
  263. }
  264. if err != nil {
  265. return nil, err
  266. }
  267. return &detail, nil
  268. }
  269. func GetMaticReduece(patientid int64, orgid int64) (models.BloodAutomaticReduceDetail, error) {
  270. detail := models.BloodAutomaticReduceDetail{}
  271. err := XTReadDB().Model(&detail).Where("patient_id = ? and org_id= ? and status = 1", patientid, orgid).Last(&detail).Error
  272. return detail, err
  273. }
  274. func GetMaticeReduceByPatientId(patientid int64, orgid int64, startime int64, endtime int64) (models.BloodAutomaticReduceDetail, error) {
  275. detail := models.BloodAutomaticReduceDetail{}
  276. err := XTReadDB().Model(&detail).Where("patient_id = ? and org_id = ? and record_time >=? and record_time<=? and status =1", patientid, orgid, startime, endtime).Find(&detail).Error
  277. return detail, err
  278. }
  279. func PCGetDialysisGoodsTwo(orgID int64, scheduleDate int64, schedule_type int64, partition_id int64, page int64, limit int64, keywords string) ([]*MDialysisGoodsVM, error, int64) {
  280. var vms []*MDialysisGoodsVM
  281. var total int64
  282. db := readDb.
  283. Model(&models.Schedule{}).
  284. Select("patient_id,mode_id").
  285. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  286. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  287. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  288. Preload("DialysisBeforePrepare", func(db *gorm.DB) *gorm.DB {
  289. return db.Preload("VMGoodInfo", "status = 1 AND org_id = ? ", orgID).Preload("GoodsType", "status = 1 AND (org_id = ? OR org_id = 0) ", orgID).Where("status = 1 AND user_org_id = ? AND record_date = ? AND count > 0 ", orgID, scheduleDate)
  290. }).
  291. Preload("AutomaticReduceDetail", func(db *gorm.DB) *gorm.DB {
  292. return db.Preload("VMGoodInfo", "status = 1 AND org_id = ? ", orgID).Preload("GoodsType", "status = 1 AND (org_id = ? OR org_id = 0) ", orgID).Where("status = 1 AND org_id = ? AND record_time = ? AND count > 0", orgID, scheduleDate)
  293. }).Where("xt_schedule.status = 1 AND xt_schedule.user_org_id = ?", orgID)
  294. if scheduleDate != 0 {
  295. db = db.Where("schedule_date = ?", scheduleDate)
  296. }
  297. if len(keywords) != 0 {
  298. keywords = "%" + keywords + "%"
  299. 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)
  300. } else {
  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. db = db.Count(&total)
  308. offset := (page - 1) * limit
  309. db = db.Offset(offset).Limit(limit)
  310. }
  311. err := db.Find(&vms).Error
  312. return vms, err, total
  313. }
  314. func PCGetDialysisGoodsThree(orgID int64, scheduleDate int64) ([]*MDialysisGoodsVM, error) {
  315. var vms []*MDialysisGoodsVM
  316. db := readDb.
  317. Model(&models.Schedule{}).
  318. Select("patient_id,mode_id").
  319. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  320. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  321. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  322. Preload("DialysisBeforePrepare", func(db *gorm.DB) *gorm.DB {
  323. return db.Preload("VMGoodInfo", "status = 1 AND org_id = ? ", orgID).Preload("GoodsType", "status = 1 AND (org_id = ? OR org_id = 0) ", orgID).Where("status = 1 AND user_org_id = ? AND record_date = ? AND count > 0 ", orgID, scheduleDate)
  324. }).
  325. Preload("AutomaticReduceDetail", func(db *gorm.DB) *gorm.DB {
  326. return db.Preload("VMGoodInfo", "status = 1 AND org_id = ? ", orgID).Preload("GoodsType", "status = 1 AND (org_id = ? OR org_id = 0) ", orgID).Where("status = 1 AND org_id = ? AND record_time = ? AND count > 0", orgID, scheduleDate)
  327. }).Where("xt_schedule.status = 1 AND xt_schedule.user_org_id = ?", orgID)
  328. if scheduleDate != 0 {
  329. db = db.Where("schedule_date = ?", scheduleDate)
  330. }
  331. err = db.Find(&vms).Error
  332. return vms, err
  333. }