dialysis_parameter_service.go 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. package service
  2. import (
  3. "Xcx_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("bed_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) (schedule []*models.XtSchedule, 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).Find(&schedule).Error
  202. return schedule, err
  203. }
  204. func GetToDayDialysisPrescription(patientid int64, orgid int64, startime int64) (*models.XtDialysisPrescription, error) {
  205. prescription := models.XtDialysisPrescription{}
  206. err := XTReadDB().Model(&prescription).Where("patient_id = ? and user_org_id = ? and record_date = ? and status = 1", patientid, orgid, startime).Find(&prescription).Error
  207. if err == gorm.ErrRecordNotFound {
  208. return nil, err
  209. }
  210. if err != nil {
  211. return nil, err
  212. }
  213. return &prescription, nil
  214. }
  215. func GetLastDialysisPrescription(patientid int64, orgid int64) (models.XtDialysisPrescription, error) {
  216. prescription := models.XtDialysisPrescription{}
  217. err = XTReadDB().Model(&prescription).Where("patient_id = ? and user_org_id = ? and status = 1", patientid, orgid).Last(&prescription).Error
  218. return prescription, err
  219. }
  220. func GetLastAssessmentBeforDialysis(patientid int64, orgid int64) (models.PredialysisEvaluation, error) {
  221. evaluation := models.PredialysisEvaluation{}
  222. err := XTReadDB().Model(&evaluation).Where("patient_id = ? and user_org_id = ? and status = 1", patientid, orgid).Last(&evaluation).Error
  223. return evaluation, err
  224. }
  225. func GetDialysisPrescriptionList(patientid int64, orgid int64, startime int64) (models.XtDialysisPrescription, error) {
  226. prescription := models.XtDialysisPrescription{}
  227. err := XTReadDB().Model(&prescription).Where("patient_id = ? and user_org_id = ? and status = 1 and record_date = ?", patientid, orgid, startime).Find(&prescription).Error
  228. return prescription, err
  229. }
  230. func GetTodayAssessmentBeforDialysis(patientid int64, orgid int64, startime int64) (*models.PredialysisEvaluation, error) {
  231. evaluation := models.PredialysisEvaluation{}
  232. err := XTReadDB().Model(&evaluation).Where("patient_id = ? and user_org_id = ? and assessment_date = ? and status = 1", patientid, orgid, startime).Find(&evaluation).Error
  233. if err == gorm.ErrRecordNotFound {
  234. return nil, err
  235. }
  236. if err != nil {
  237. return nil, err
  238. }
  239. return &evaluation, nil
  240. }
  241. func GetAssessmentBeforDialysisByStartime(patientid int64, orgid int64, startime int64) (models.PredialysisEvaluation, error) {
  242. evaluation := models.PredialysisEvaluation{}
  243. err := XTReadDB().Model(&evaluation).Where("patient_id = ? and user_org_id = ? and assessment_date = ? and status = 1", patientid, orgid, startime).Find(&evaluation).Error
  244. return evaluation, err
  245. }
  246. func GetAotoMaticReduceByPatientId(patientid int64, orgid int64, startime int64, endtime int64) (*models.BloodAutomaticReduceDetail, error) {
  247. detail := models.BloodAutomaticReduceDetail{}
  248. 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
  249. if err == gorm.ErrRecordNotFound {
  250. return nil, err
  251. }
  252. if err != nil {
  253. return nil, err
  254. }
  255. return &detail, nil
  256. }
  257. func GetMaticReduece(patientid int64, orgid int64) (models.BloodAutomaticReduceDetail, error) {
  258. detail := models.BloodAutomaticReduceDetail{}
  259. err := XTReadDB().Model(&detail).Where("patient_id = ? and org_id= ? and status = 1", patientid, orgid).Last(&detail).Error
  260. return detail, err
  261. }
  262. func GetMaticeReduceByPatientId(patientid int64, orgid int64, startime int64, endtime int64) (models.BloodAutomaticReduceDetail, error) {
  263. detail := models.BloodAutomaticReduceDetail{}
  264. 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
  265. return detail, err
  266. }