dialysis_parameter_service.go 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. package service
  2. import (
  3. "fmt"
  4. "XT_New/models"
  5. "github.com/jinzhu/gorm"
  6. )
  7. type VMDialysisSchedule struct {
  8. ID int64 `gorm:"column:id" json:"id" form:"id"`
  9. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id" form:"user_org_id"`
  10. PartitionId int64 `gorm:"column:partition_id" json:"partition_id" form:"partition_id"`
  11. BedId int64 `gorm:"column:bed_id" json:"bed_id" form:"bed_id"`
  12. PatientId int64 `gorm:"column:patient_id" json:"patient_id" form:"patient_id"`
  13. ScheduleDate int64 `gorm:"column:schedule_date" json:"schedule_date" form:"schedule_date"`
  14. ScheduleType int64 `gorm:"column:schedule_type" json:"schedule_type" form:"schedule_type"`
  15. ScheduleWeek int64 `gorm:"column:schedule_week" json:"schedule_week" form:"schedule_week"`
  16. ModeId int64 `gorm:"column:mode_id" json:"mode_id" form:"mode_id"`
  17. Status int64 `gorm:"column:status" json:"status" form:"status"`
  18. CreatedTime int64 `gorm:"column:created_time" json:"created_time" form:"created_time"`
  19. UpdatedTime int64 `gorm:"column:updated_time" json:"updated_time" form:"updated_time"`
  20. MonitorPatients models.MonitorPatients `gorm:"ForeignKey:PatientId" json:"patient"`
  21. DeviceNumber models.DeviceNumber `gorm:"ForeignKey:BedId" json:"device_number"`
  22. DeviceZone models.DeviceZone `gorm:"ForeignKey:PartitionId" json:"device_zone"`
  23. TreatmentMode models.TreatmentMode `gorm:"ForeignKey:ModeId" json:"treatment_mode"`
  24. DialysisOrder models.MonitorDialysisOrder `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"dialysis_order"`
  25. Prescription models.DialysisPrescription `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"prescription"`
  26. AssessmentBeforeDislysis models.PredialysisEvaluation `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"assessment_before_dislysis"`
  27. AssessmentAfterDislysis AssessmentAfterDislysis `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"assessment_after_dislysis"`
  28. MonitoringRecord []models.MonitoringRecord `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"monitoring_record"`
  29. //Patient string `gorm:"-" json:"patient" form:"patient"`
  30. //SchedualPatient SchedualPatient `gorm:"ForeignKey:PatientId"`
  31. //DeviceNumber DeviceNumber `gorm:"ForeignKey:BedId"`
  32. //DeviceZone DeviceZone `gorm:"ForeignKey:PartitionId"`
  33. //TreatmentMode TreatmentMode `gorm:"ForeignKey:ModeId"`
  34. }
  35. func (VMDialysisSchedule) TableName() string {
  36. return "xt_schedule"
  37. }
  38. type MonitorDialysisSchedule struct {
  39. ID int64 `gorm:"column:id" json:"id"`
  40. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id"`
  41. PartitionId int64 `gorm:"column:partition_id" json:"partition_id"`
  42. BedId int64 `gorm:"column:bed_id" json:"bed_id"`
  43. PatientId int64 `gorm:"column:patient_id" json:"patient_id"`
  44. ScheduleDate int64 `gorm:"column:schedule_date" json:"schedule_date"`
  45. ScheduleType int64 `gorm:"column:schedule_type" json:"schedule_type"`
  46. ModeId int64 `gorm:"column:mode_id" json:"mode_id"`
  47. Status int64 `gorm:"column:status" json:"status"`
  48. MonitorPatients models.MonitorPatients `gorm:"ForeignKey:PatientId" json:"patient"`
  49. DeviceNumber models.DeviceNumber `gorm:"ForeignKey:BedId" json:"device_number"`
  50. DeviceZone models.DeviceZone `gorm:"ForeignKey:PartitionId" json:"device_zone"`
  51. TreatmentMode models.TreatmentMode `gorm:"ForeignKey:ModeId" json:"treatment_mode"`
  52. DialysisOrder models.MonitorDialysisOrder `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"dialysis_order"`
  53. Prescription models.DialysisPrescription `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"prescription"`
  54. AssessmentBeforeDislysis models.PredialysisEvaluation `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"assessment_before_dislysis"`
  55. AssessmentAfterDislysis models.AssessmentAfterDislysis `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"assessment_after_dislysis"`
  56. MonitoringRecord []models.MonitoringRecord `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"monitoring_record"`
  57. DialysisSolution models.DialysisSolution `gorm:"ForeignKey:PatientId,ModeId;AssociationForeignKey:PatientId,ModeId" json:"dialysis_solution"`
  58. }
  59. func (MonitorDialysisSchedule) TableName() string {
  60. return "xt_schedule"
  61. }
  62. func GetDialysisParametersByKeyword(orgID int64, keyword string, schedulType int64, partitionType int64, page int64, limit int64, schedulDate int64) ([]*VMDialysisSchedule, error, int64) {
  63. var patients []*models.Patients
  64. 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
  65. if getPatientErr != nil {
  66. return nil, getPatientErr, 0
  67. }
  68. patientIDs := make([]int64, len(patients))
  69. for index, patient := range patients {
  70. patientIDs[index] = patient.ID
  71. }
  72. db := readDb.
  73. Model(&VMDialysisSchedule{}).
  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 = ? AND record_date = ?", orgID, schedulDate).
  78. Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ? AND assessment_date = ?", orgID, schedulDate).
  79. Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ? AND assessment_date = ?", orgID, schedulDate).
  80. Preload("MonitoringRecord", "status = 1 AND user_org_id = ? AND monitoring_date = ?", orgID, schedulDate).
  81. Preload("DialysisOrder", "status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, schedulDate).
  82. Preload("MonitorPatients", "status = 1 AND user_org_id = ?", orgID)
  83. db = db.Where("xt_schedule.status = 1 AND patient_id in (?)", patientIDs)
  84. if schedulType > 0 {
  85. db = db.Where("schedule_type = ?", schedulType)
  86. }
  87. if partitionType > 0 {
  88. 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)
  89. }
  90. if schedulDate > 0 {
  91. db = db.Where("schedule_date = ?", schedulDate)
  92. }
  93. var schedules []*VMDialysisSchedule
  94. total := int64(0)
  95. err := db.Count(&total).Offset(limit * (page - 1)).Limit(limit).Order("schedule_date desc").Find(&schedules).Error
  96. return schedules, err, total
  97. }
  98. func GetDialysisParameter(orgID int64, schedulDate int64, schedulType int64, partitionType int64, page int64, limit int64) (schedule []*MonitorDialysisSchedule, err error, total int64) {
  99. db := readDb.
  100. Model(&MonitorDialysisSchedule{}).
  101. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  102. Preload("TreatmentMode", "status = 1").
  103. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, schedulDate).
  104. Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ? AND assessment_date = ?", orgID, schedulDate).
  105. Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ? AND assessment_date = ?", orgID, schedulDate).
  106. Preload("MonitoringRecord", "status = 1 AND user_org_id = ? AND monitoring_date = ?", orgID, schedulDate).
  107. Preload("DialysisOrder", "status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, schedulDate).
  108. Preload("MonitorPatients", "status = 1 AND user_org_id = ?", orgID).
  109. Preload("DialysisSolution", "status=1 and user_org_id = ? and solution_status = 1", orgID)
  110. db = db.Where("xt_schedule.status = 1 AND user_org_id = ?", orgID)
  111. if schedulDate > 0 {
  112. db = db.Where("schedule_date = ?", schedulDate)
  113. }
  114. if schedulType > 0 {
  115. db = db.Where("schedule_type = ?", schedulType)
  116. }
  117. if partitionType > 0 {
  118. 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)
  119. }
  120. offset := (page - 1) * limit
  121. err = db.Count(&total).Offset(offset).Limit(limit).Order("id desc").Find(&schedule).Error
  122. return schedule, err, total
  123. }
  124. func GetDialysisBatchParameters(schIDs []string, orgID int64) (schedule []*models.DialysisParameter, err error) {
  125. db := readDb.
  126. Model(&models.DialysisSchedule{}).
  127. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  128. // Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  129. Preload("TreatmentMode", "status = 1").
  130. Preload("Prescription", "status = 1 AND user_org_id = ?", orgID).
  131. Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ?", orgID).
  132. Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ?", orgID).
  133. Preload("MonitoringRecord", "status = 1 AND user_org_id = ?", orgID).
  134. Preload("DialysisOrder", "status = 1 AND user_org_id = ?", orgID).
  135. Preload("MonitorPatients", "status = 1 AND user_org_id = ?", orgID)
  136. db = db.Where("xt_schedule.status = 1 AND id in (?)", schIDs)
  137. var schedules []*models.DialysisParameter
  138. err = db.Order("schedule_date desc").Find(&schedules).Error
  139. return schedules, err
  140. }
  141. func GetWareHouseOutList(startime int64, endtime int64, orgid int64) (warehouse []*models.XtWarehouseOutInfo, err error) {
  142. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status = 1")
  143. if startime > 0 {
  144. db = db.Where("x.sys_record_time >=?", startime)
  145. }
  146. if endtime > 0 {
  147. db = db.Where("x.sys_record_time<=?", endtime)
  148. }
  149. if orgid > 0 {
  150. db = db.Where("x.org_id = ?", orgid)
  151. }
  152. 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
  153. return warehouse, err
  154. }
  155. func GetAllMaterial(startime int64, endtime int64, orgid int64) (reducedetail []*models.XtAutomaticReduceDetail, err error) {
  156. db := XTReadDB().Table("xt_automatic_reduce_detail as x").Where("x.status = 1")
  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.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
  167. return reducedetail, err
  168. }
  169. func GetCollectList(limit int64, page int64, partitionType int64, schedulType int64, schedulDate int64, orgID int64, keyword string) ([]*models.DialysisParameter, error, int64) {
  170. var patients []*models.Patients
  171. 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
  172. if getPatientErr != nil {
  173. return nil, getPatientErr, 0
  174. }
  175. patientIDs := make([]int64, len(patients))
  176. for index, patient := range patients {
  177. patientIDs[index] = patient.ID
  178. }
  179. db := readDb.
  180. Model(&models.DialysisSchedule{}).
  181. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  182. // Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  183. Preload("TreatmentMode", "status = 1").
  184. Preload("Prescription", "status = 1 AND user_org_id = ?", orgID).
  185. Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ?", orgID).
  186. Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ?", orgID).
  187. Preload("MonitoringRecord", "status = 1 AND user_org_id = ?", orgID).
  188. Preload("DialysisOrder", "status = 1 AND user_org_id = ?", orgID).
  189. Preload("MonitorPatients", "status = 1 AND user_org_id = ?", orgID)
  190. db = db.Where("xt_schedule.status = 1 AND patient_id in (?)", patientIDs)
  191. if schedulType > 0 {
  192. db = db.Where("schedule_type = ?", schedulType)
  193. }
  194. if partitionType > 0 {
  195. 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)
  196. // db = db.Where("partition_id = ?", partitionType)
  197. }
  198. if schedulDate > 0 {
  199. db = db.Where("schedule_date = ?", schedulDate)
  200. }
  201. var schedules []*models.DialysisParameter
  202. total := int64(0)
  203. err := db.Count(&total).Offset(limit * (page - 1)).Limit(limit).Order("schedule_date desc").Find(&schedules).Error
  204. return schedules, err, total
  205. }
  206. func GetDialysisConsumables(startime int64, endtime int64, orgid int64) (reducedetail []*models.XtAutomaticReduceDetail, err error) {
  207. db := XTReadDB().Table("xt_automatic_reduce_detail as x").Where("x.status = 1")
  208. table := XTReadDB().Table("xt_warehouse_out_info as f").Where("f.status = 1 and f.org_id = ?", orgid)
  209. fmt.Println(table)
  210. if startime > 0 {
  211. db = db.Where("x.record_time >=?", startime)
  212. }
  213. if endtime > 0 {
  214. db = db.Where("x.record_time <=?", endtime)
  215. }
  216. if orgid > 0 {
  217. db = db.Where("x.org_id =?", orgid)
  218. }
  219. 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
  220. return reducedetail, err
  221. }
  222. func GetBatchCollection(orgID int64, schIDs []string) (schedules []*models.DialysisParameter, err error) {
  223. db := readDb.
  224. Model(&models.DialysisSchedule{}).
  225. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  226. // Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  227. Preload("TreatmentMode", "status = 1").
  228. Preload("Prescription", "status = 1 AND user_org_id = ?", orgID).
  229. Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ?", orgID).
  230. Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ?", orgID).
  231. Preload("MonitoringRecord", "status = 1 AND user_org_id = ?", orgID).
  232. Preload("DialysisOrder", "status = 1 AND user_org_id = ?", orgID).
  233. Preload("MonitorPatients", "status = 1 AND user_org_id = ?", orgID)
  234. db = db.Where("xt_schedule.status = 1 AND id in (?)", schIDs)
  235. err = db.Order("schedule_date desc").Find(&schedules).Error
  236. return schedules, err
  237. }
  238. func GetAnticoagulantCount(startime int64, endtime int64, orgid int64) (prescriptionCount []*models.DialysisPrescriptionCount, err error) {
  239. db := XTReadDB().Table("xt_dialysis_prescription as x").Where("x.status = 1")
  240. if orgid > 0 {
  241. db = db.Where("x.user_org_id = ? and x.anticoagulant <> 0", orgid)
  242. }
  243. if startime > 0 {
  244. db = db.Where("x.record_date >= ?", startime)
  245. }
  246. if endtime > 0 {
  247. db = db.Where("x.record_date<=?", endtime)
  248. }
  249. err = db.Select("x.patient_id,x.anticoagulant,x.record_date,count(id) as count").Group("x.anticoagulant").Scan(&prescriptionCount).Error
  250. return prescriptionCount, err
  251. }
  252. func GetDialysisTodaySchedulePatient(orgid int64, startime int64, page int64, limit int64, keywords string, schedulType int64, partiontype int64) (schedule []*models.XtScheduleTwo, err error) {
  253. //offset := (page - 1) * limit
  254. //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
  255. //return schedule, err
  256. keywords = "%" + keywords + "%"
  257. offset := (page - 1) * limit
  258. db := XTReadDB().Table("xt_schedule as x").Where("x.status = 1")
  259. if schedulType > 0 {
  260. db = db.Where("x.schedule_type = ?", schedulType)
  261. }
  262. if partiontype > 0 {
  263. db = db.Where("x.partition_id = ?", partiontype)
  264. }
  265. table := XTReadDB().Table("xt_patients as s")
  266. fmt.Println(table)
  267. if len(keywords) > 0 {
  268. 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
  269. //db = db.Where("s.name like ?", keywords)
  270. } else {
  271. 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
  272. }
  273. return schedule, err
  274. }
  275. func GetToDayDialysisPrescription(patientid int64, orgid int64, startime int64) (*models.XtDialysisPrescription, error) {
  276. prescription := models.XtDialysisPrescription{}
  277. err := XTReadDB().Model(&prescription).Where("patient_id = ? and user_org_id = ? and record_date = ? and status = 1", patientid, orgid, startime).Find(&prescription).Error
  278. if err == gorm.ErrRecordNotFound {
  279. return nil, err
  280. }
  281. if err != nil {
  282. return nil, err
  283. }
  284. return &prescription, nil
  285. }
  286. func GetLastDialysisPrescription(patientid int64, orgid int64) (models.XtDialysisPrescription, error) {
  287. prescription := models.XtDialysisPrescription{}
  288. err = XTReadDB().Model(&prescription).Where("patient_id = ? and user_org_id = ? and status = 1", patientid, orgid).Last(&prescription).Error
  289. return prescription, err
  290. }
  291. func GetLastAssessmentBeforDialysis(patientid int64, orgid int64) (models.PredialysisEvaluation, error) {
  292. evaluation := models.PredialysisEvaluation{}
  293. err := XTReadDB().Model(&evaluation).Where("patient_id = ? and user_org_id = ? and status = 1", patientid, orgid).Last(&evaluation).Error
  294. return evaluation, err
  295. }
  296. func GetPatientDryList(patientid int64, orgid int64) (dryweight []*models.SgjPatientDryweight, err error) {
  297. err = XTReadDB().Where("patient_id =? and user_org_id =? and status=1", patientid, orgid).Order("ctime desc").Limit(6).Find(&dryweight).Error
  298. return dryweight, err
  299. }
  300. func GetDialysisPrescriptionList(patientid int64, orgid int64, startime int64) (models.DialysisPrescriptionParameter, error) {
  301. prescription := models.DialysisPrescriptionParameter{}
  302. err := XTReadDB().Model(&prescription).Where("patient_id = ? and user_org_id = ? and status = 1 and record_date = ?", patientid, orgid, startime).Find(&prescription).Error
  303. return prescription, err
  304. }
  305. func GetTodayAssessmentBeforDialysis(patientid int64, orgid int64, startime int64) (*models.PredialysisEvaluationParameter, error) {
  306. evaluation := models.PredialysisEvaluationParameter{}
  307. err := XTReadDB().Model(&evaluation).Where("patient_id = ? and user_org_id = ? and assessment_date = ? and status = 1", patientid, orgid, startime).Find(&evaluation).Error
  308. if err == gorm.ErrRecordNotFound {
  309. return nil, err
  310. }
  311. if err != nil {
  312. return nil, err
  313. }
  314. return &evaluation, nil
  315. }
  316. func GetAssessmentBeforDialysisByStartime(patientid int64, orgid int64, startime int64) (models.PredialysisEvaluation, error) {
  317. evaluation := models.PredialysisEvaluation{}
  318. err := XTReadDB().Model(&evaluation).Where("patient_id = ? and user_org_id = ? and assessment_date = ? and status = 1", patientid, orgid, startime).Find(&evaluation).Error
  319. return evaluation, err
  320. }
  321. func GetAotoMaticReduceByPatientId(patientid int64, orgid int64, startime int64, endtime int64) (*models.BloodAutomaticReduceDetail, error) {
  322. detail := models.BloodAutomaticReduceDetail{}
  323. 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
  324. if err == gorm.ErrRecordNotFound {
  325. return nil, err
  326. }
  327. if err != nil {
  328. return nil, err
  329. }
  330. return &detail, nil
  331. }
  332. func GetMaticReduece(patientid int64, orgid int64) (models.BloodAutomaticReduceDetail, error) {
  333. detail := models.BloodAutomaticReduceDetail{}
  334. err := XTReadDB().Model(&detail).Where("patient_id = ? and org_id= ? and status = 1", patientid, orgid).Last(&detail).Error
  335. return detail, err
  336. }
  337. func GetMaticeReduceByPatientId(patientid int64, orgid int64, startime int64, endtime int64) (models.BloodAutomaticReduceDetail, error) {
  338. detail := models.BloodAutomaticReduceDetail{}
  339. 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
  340. return detail, err
  341. }
  342. func PCGetDialysisGoodsTwo(orgID int64, scheduleDate int64, schedule_type int64, partition_id int64, page int64, limit int64, keywords string) ([]*MDialysisGoodsVM, error, int64) {
  343. var vms []*MDialysisGoodsVM
  344. var total int64
  345. db := readDb.
  346. Model(&models.Schedule{}).Select("patient_id,mode_id").
  347. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  348. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  349. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  350. Preload("DialysisBeforePrepare", func(db *gorm.DB) *gorm.DB {
  351. 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)
  352. }).
  353. Preload("AutomaticReduceDetail", func(db *gorm.DB) *gorm.DB {
  354. 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)
  355. }).Where("xt_schedule.status = 1 AND xt_schedule.user_org_id = ?", orgID)
  356. if scheduleDate != 0 {
  357. db = db.Where("schedule_date = ?", scheduleDate)
  358. }
  359. if len(keywords) != 0 {
  360. keywords = "%" + keywords + "%"
  361. 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)
  362. } else {
  363. if schedule_type != 0 {
  364. db = db.Where("schedule_type = ?", schedule_type)
  365. }
  366. if partition_id != 0 {
  367. db = db.Where("partition_id = ?", partition_id)
  368. }
  369. db = db.Count(&total)
  370. offset := (page - 1) * limit
  371. db = db.Offset(offset).Limit(limit)
  372. }
  373. err := db.Find(&vms).Error
  374. return vms, err, total
  375. }
  376. func PCGetDialysisGoodsThree(orgID int64, scheduleDate int64) ([]*MDialysisGoodsVM, error) {
  377. var vms []*MDialysisGoodsVM
  378. db := readDb.
  379. Model(&models.Schedule{}).
  380. Select("patient_id,mode_id").
  381. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  382. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  383. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  384. Preload("DialysisBeforePrepare", func(db *gorm.DB) *gorm.DB {
  385. 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)
  386. }).
  387. Preload("AutomaticReduceDetail", func(db *gorm.DB) *gorm.DB {
  388. 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)
  389. }).Where("xt_schedule.status = 1 AND xt_schedule.user_org_id = ?", orgID)
  390. if scheduleDate != 0 {
  391. db = db.Where("schedule_date = ?", scheduleDate)
  392. }
  393. err = db.Find(&vms).Error
  394. return vms, err
  395. }
  396. func GetLastDialysisPrescriptionTwenty(patientid int64, record_date int64, orgid int64) (models.XtDialysisPrescription, error) {
  397. prescription := models.XtDialysisPrescription{}
  398. err = XTReadDB().Model(&prescription).Where("patient_id = ? and user_org_id = ? and record_date =? and status = 1", record_date, patientid, orgid).Last(&prescription).Error
  399. return prescription, err
  400. }