dialysis_service.go 53KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084
  1. package service
  2. import (
  3. "Xcx_New/models"
  4. "fmt"
  5. "strings"
  6. "time"
  7. "github.com/jinzhu/gorm"
  8. )
  9. //GetPatientList 返回患者的列表
  10. func GetSchedualPatientList(orgID int64, schedulDate int64, schedulType int64, partitionType int64, keywords string, page int64, limit int64) (schedule []*models.DialysisSchedule, err error) {
  11. var total int64
  12. //if len(keywords) > 0 {
  13. // likeKey := "%" + keywords + "%"
  14. // //db = db.Preload("MonitorPatients", "status = 1 AND user_org_id = ? AND `name` LIKE ? ", orgID, likeKey).Preload("DeviceNumber", "status = 1 AND org_id = ? ", orgID).Preload("PredialysisEvaluation", "status = 1 AND user_org_id = ? AND assessment_date = ? ", orgID, schedulDate).Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ? AND assessment_date = ? ", orgID, schedulDate).Preload("MonitoringRecord", "status = 1 AND user_org_id = ? AND monitoring_date = ?", orgID, schedulDate).Where("status = 1 AND user_org_id = ? ", orgID)
  15. // db = readDb.
  16. // Model(&models.MonitorDialysisSchedule{}).
  17. // Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  18. // Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  19. // Preload("TreatmentMode", "status = 1").
  20. // Preload("Prescription", "status = 1 AND user_org_id = ?", orgID).
  21. // Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ?", orgID).
  22. // Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ?", orgID).
  23. // Preload("MonitoringRecord", "status = 1 AND user_org_id = ?", orgID).
  24. // Preload("DialysisOrder", "status = 1 AND user_org_id = ?", orgID).
  25. // Preload("MonitorPatients", "status = 1 AND user_org_id = ? AND `name` LIKE ?", orgID,likeKey)
  26. //
  27. //
  28. //} else {
  29. //}
  30. db := readDb.
  31. Model(&models.MonitorDialysisSchedule{}).
  32. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  33. // Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  34. Preload("TreatmentMode", "status = 1").
  35. Preload("Prescription", "status = 1 AND user_org_id = ?", orgID).
  36. Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ?", orgID).
  37. Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ?", orgID).
  38. Preload("MonitoringRecord", "status = 1 AND user_org_id = ?", orgID).
  39. Preload("DialysisOrder", "status = 1 AND user_org_id = ?", orgID).
  40. Preload("MonitorPatients", "status = 1 AND user_org_id = ?", orgID)
  41. db = db.Where("status = 1")
  42. if schedulDate > 0 {
  43. db = db.Where("schedule_date = ?", schedulDate)
  44. }
  45. db = db.Count(&total)
  46. fmt.Println(total)
  47. if schedulType > 0 {
  48. db = db.Where("schedule_type = ?", schedulType)
  49. }
  50. if partitionType > 0 {
  51. db = db.Where("partition_id = ?", partitionType)
  52. }
  53. offset := (page - 1) * limit
  54. errors := db.Offset(offset).Limit(limit).Find(&schedule).Error
  55. return schedule, errors
  56. }
  57. //用户基本信息
  58. func FindPatientInfoById(orgID int64, id int64, time int64) (patient models.SchedualPatient2, err error) {
  59. err = readDb.Preload("DialysisSchedule", func(db *gorm.DB) *gorm.DB {
  60. return db.Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).Where("patient_id = ? AND user_org_id = ? AND schedule_date = ? ", id, orgID, time)
  61. }).Where("id = ? and user_org_id=? and status=1", id, orgID).First(&patient).Error
  62. return
  63. }
  64. //透析处方
  65. func FindPatientPrescribeById(orgID int64, patientsId int64, recordDate int64) (patient models.DialysisPrescription, err error) {
  66. err = readDb.Model(&models.DialysisPrescription{}).Where("patient_id = ? and user_org_id=? and status=1 and record_date = ? ", patientsId, orgID, recordDate).First(&patient).Error
  67. return
  68. }
  69. //透析方案中的透析处方
  70. func FindDialysisSolution(orgID int64, patientsId int64) (patient models.DialysisSolution, err error) {
  71. err = readDb.Model(&models.DialysisSolution{}).Where("patient_id = ? and user_org_id=? and status=1", patientsId, orgID).Last(&patient).Error
  72. return
  73. }
  74. //透析方案中的透析模式
  75. func FindTreatmentModeById(modeId int64) (mode models.TreatmentMode, err error) {
  76. err = readDb.Model(&models.TreatmentMode{}).Where("id = ? AND status=1", modeId).First(&mode).Error
  77. return
  78. }
  79. //接诊评估
  80. func FindReceiverTreatmentAccessRecordById(orgID int64, patientsId int64, recordDate int64) (receiveTreatmentAsses models.ReceiveTreatmentAsses, err error) {
  81. err = readDb.Model(&models.ReceiveTreatmentAsses{}).Where("patient_id = ? and user_org_id=? and status=1 and record_date = ?", patientsId, orgID, recordDate).First(&receiveTreatmentAsses).Error
  82. return
  83. }
  84. //透前评估
  85. func FindPredialysisEvaluationById(orgID int64, patientsId int64, recordDate int64) (patient models.PredialysisEvaluation, err error) {
  86. err = readDb.Model(&models.PredialysisEvaluation{}).Where("patient_id = ? and user_org_id=? and status=1 and assessment_date = ?", patientsId, orgID, recordDate).First(&patient).Error
  87. return
  88. }
  89. func FindPredialysisName(id int64, orgid int64) (models.Dataconfig, error) {
  90. dataconfig := models.Dataconfig{}
  91. err := readDb.Where("value = ? and org_id =? and status = 1", id, orgid).Find(&dataconfig).Error
  92. return dataconfig, err
  93. }
  94. //临时医嘱
  95. func FindDoctorAdviceById(orgID int64, patientsId int64, recordDate int64) (patient []*models.DoctorAdvice, err error) {
  96. err = readDb.Model(&models.DoctorAdvice{}).Where("patient_id = ? and user_org_id=? and status=1 and record_date = ? AND advice_type = 2", patientsId, orgID, recordDate).Find(&patient).Error
  97. return
  98. }
  99. func FindDoctorAdviceByInfo(orgID int64, patientsId int64, recordDate int64) (advices []*models.XtDoctorAdvice, err error) {
  100. //db := readDb.Table("xt_doctor_advice as a").Where("a.status = ?", 1)
  101. //err = db.Group("a.advice_name").Select("a.id,a.user_org_id,a.patient_id,a.advice_type,a.advice_date,a.start_time,a.advice_name,a.advice_desc,a.reminder_date,a.single_dose,a.single_dose_unit,a.prescribing_number,a.prescribing_number_unit,a.delivery_way,a.execution_frequency,a.advice_doctor,a.status,a.created_time,a.updated_time,a.advice_affirm,a.remark,a.stop_time,a.stop_reason,a.stop_doctor,a.stop_state,a.parent_id,a.execution_time,a.execution_staff,a.execution_state,a.checker,a.record_date,a.dialysis_order_id,a.check_time,a.check_state,a.drug_spec,a.drug_spec_unit,a.groupno,a.remind_type,a.frequency_type,a.day_count,a.week_day,a.template_id,r.user_name,s.user_name").Joins(" left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = a.execution_staff").Joins("left join sgj_users.sgj_user_admin_role as s on s.admin_user_id = a.checker").Where("a.user_org_id = ? and a.patient_id = ? and a.advice_date = ?", orgID, patientsId, recordDate).Find(&advices).Error
  102. //return
  103. err = readDb.Model(&models.DoctorAdvice{}).
  104. Where("patient_id = ? and user_org_id=? and status=1 and record_date = ? and advice_type = 2", patientsId, orgID, recordDate).
  105. Select("id, user_org_id, patient_id, advice_type, advice_date, record_date, start_time, advice_name,advice_desc, reminder_date, drug_spec, drug_spec_unit, single_dose, single_dose_unit, prescribing_number, prescribing_number_unit, delivery_way, execution_frequency, advice_doctor, status, created_time,updated_time, advice_affirm, remark, stop_time, stop_reason, stop_doctor, stop_state, parent_id, execution_time, execution_staff, execution_state, checker, check_state, check_time, groupno, IF(parent_id > 0, parent_id, id) as advice_order").Order("start_time asc, groupno desc, advice_order desc, id asc").
  106. Find(&advices).Error
  107. return
  108. }
  109. func GetTotalDialysisCout(orgid int64, patientid int64) (order []*models.DialysisOrder, total int64, err error) {
  110. err = readDb.Model(&order).Where("user_org_id = ? and patient_id = ? and status = 1", orgid, patientid).Find(&order).Count(&total).Error
  111. return order, total, err
  112. }
  113. //临时医嘱
  114. func FindDoctorAdviceOrderById(orgID int64, patientsId int64, recordDate int64) (patient []*models.DoctorAdvice, err error) {
  115. err = readDb.Model(&models.DoctorAdvice{}).
  116. Where("patient_id = ? and user_org_id=? and status=1 and record_date = ? and advice_type = 2", patientsId, orgID, recordDate).
  117. Select("id, user_org_id, patient_id, advice_type, advice_date, record_date, start_time, advice_name,advice_desc, reminder_date, drug_spec, drug_spec_unit, single_dose, single_dose_unit, prescribing_number, prescribing_number_unit, delivery_way, execution_frequency, advice_doctor, status, created_time,updated_time, advice_affirm, remark, stop_time, stop_reason, stop_doctor, stop_state, parent_id, execution_time, execution_staff, execution_state, checker, check_state, check_time, groupno, IF(parent_id > 0, parent_id, id) as advice_order").Order("start_time asc, groupno desc, advice_order desc, id asc").
  118. Find(&patient).Error
  119. return
  120. }
  121. //双人核对
  122. func FindDoubleCheckById(orgID int64, patientsId int64, recordDate int64) (patient models.DoubleCheck, err error) {
  123. err = readDb.Model(&models.DoubleCheck{}).Where("patient_id = ? and user_org_id=? and status=1 and check_date = ? ", patientsId, orgID, recordDate).First(&patient).Error
  124. return
  125. }
  126. //透析监测
  127. func FindAllMonitorRecord(orgID int64, patientsId int64, recordDate int64) (record []*models.MonitoringRecord, err error) {
  128. err = readDb.Model(&models.MonitoringRecord{}).Where("patient_id = ? and user_org_id=? and status=1 and monitoring_date = ?", patientsId, orgID, recordDate).Order("operate_time ASC").Find(&record).Error
  129. return
  130. }
  131. func FindAllMonitor(orgID int64, patientsId int64, recordDate int64) (record []*models.MonitoringRecord, err error) {
  132. err = readDb.Model(&models.MonitoringRecord{}).Where("patient_id = ? and user_org_id=? and status=1 and monitoring_date = ?", patientsId, orgID, recordDate).Order("operate_time ASC").Find(&record).Error
  133. return
  134. }
  135. //透后评估
  136. func FindAssessmentAfterDislysisById(orgID int64, patientsId int64, recordDate int64) (patient models.AssessmentAfterDislysis, err error) {
  137. err = readDb.Model(&models.AssessmentAfterDislysis{}).Where("patient_id = ? and user_org_id=? and status=1 and assessment_date =?", patientsId, orgID, recordDate).First(&patient).Error
  138. return
  139. }
  140. //治疗小结
  141. func FindTreatmentSummaryById(orgID int64, patientsId int64, recordDate int64) (patient models.TreatmentSummary, err error) {
  142. err = readDb.Model(&models.TreatmentSummary{}).Where("patient_id = ? and user_org_id=? and status=1 and assessment_date = ? ", patientsId, orgID, recordDate).First(&patient).Error
  143. return
  144. }
  145. //所有设备信息
  146. func FindAllDeviceInfo(orgID int64) (device []*models.Device, err error) {
  147. err = readDb.Model(&models.Device{}).Where("org_id=? and status=1 and device_type = 1", orgID).Find(&device).Error
  148. return device, err
  149. }
  150. //所有设备区域
  151. func GetAllDeviceZone(orgId int64) (error, []*models.DeviceZone) {
  152. var zone []*models.DeviceZone
  153. err := readDb.Model(&zone).Where("org_id = ? AND status = 1", orgId).Find(&zone).Error
  154. return err, zone
  155. }
  156. //获取所有子药物
  157. func FindAllSubDoctorAdvice(id int64, parentId int64) (error, []models.DoctorAdvice) {
  158. var subAdvies []models.DoctorAdvice
  159. err := readDb.Model(&subAdvies).Where("patient_id = ? AND parent_id = ?", id, parentId).Find(&subAdvies).Error
  160. return err, subAdvies
  161. }
  162. func FindAdivceById(orgId int64, patientId int64, id int64) (error, models.DoctorAdvice) {
  163. advices := models.DoctorAdvice{}
  164. err := readDb.Model(&advices).Where("user_org_id = ? AND patient_id = ? AND id = ? AND status = 1", orgId, patientId, id).Find(&advices).Error
  165. return err, advices
  166. }
  167. func FindSubAdivceById(patientId int64, parentId int64) []*models.DoctorAdvice {
  168. var subAdvices []*models.DoctorAdvice
  169. readDb.Model(&subAdvices).Where("patient_id = ? AND parent_id = ?", patientId, parentId).Find(&subAdvices)
  170. return subAdvices
  171. }
  172. func AddSigleRecord(prescription *models.DialysisPrescription) (err error) {
  173. tx := writeDb.Begin()
  174. errors := tx.Model(&models.DialysisPrescription{}).Create(&prescription).Error
  175. if err != nil {
  176. tx.Rollback()
  177. }
  178. tx.Commit()
  179. return errors
  180. }
  181. func AddSigleSummaryRecord(summary *models.TreatmentSummary) (err error) {
  182. tx := writeDb.Begin()
  183. errors := tx.Model(&models.TreatmentSummary{}).Create(&summary).Error
  184. if err != nil {
  185. tx.Rollback()
  186. }
  187. tx.Commit()
  188. return errors
  189. }
  190. func AddSigleReceiveTreatmentAssesRecord(receiveTreatmentAsses *models.ReceiveTreatmentAsses) (err error) {
  191. tx := writeDb.Begin()
  192. errors := tx.Model(&models.ReceiveTreatmentAsses{}).Create(&receiveTreatmentAsses).Error
  193. if err != nil {
  194. tx.Rollback()
  195. }
  196. tx.Commit()
  197. return errors
  198. }
  199. func AddSigleAssessmentAfterDislysisRecord(assessmentAfterDislysis *models.AssessmentAfterDislysis) (err error) {
  200. tx := writeDb.Begin()
  201. errors := tx.Model(&models.AssessmentAfterDislysis{}).Create(&assessmentAfterDislysis).Error
  202. if err != nil {
  203. tx.Rollback()
  204. }
  205. tx.Commit()
  206. return errors
  207. }
  208. func AddSigleAssessmentBeforeDislysisRecord(assessmentBeforeDislysis *models.PredialysisEvaluation) (err error) {
  209. tx := writeDb.Begin()
  210. errors := tx.Model(&models.PredialysisEvaluation{}).Create(&assessmentBeforeDislysis).Error
  211. if err != nil {
  212. tx.Rollback()
  213. }
  214. tx.Commit()
  215. return errors
  216. }
  217. func AddSigleDoubleCheck(check *models.DoubleCheck) (err error) {
  218. tx := writeDb.Begin()
  219. errors := tx.Model(&models.PredialysisEvaluation{}).Create(&check).Error
  220. if err != nil {
  221. tx.Rollback()
  222. }
  223. tx.Commit()
  224. return errors
  225. }
  226. //修改透析处方
  227. func UpDateDialysisPrescription(dialysisPrescription *models.DialysisPrescription) error {
  228. err := writeDb.Save(&dialysisPrescription).Error
  229. return err
  230. }
  231. //修改接诊评估
  232. func UpadateReceiveTreatmentAsses(receiveTreatmentAsses *models.ReceiveTreatmentAsses) error {
  233. err := writeDb.Save(receiveTreatmentAsses).Error
  234. return err
  235. }
  236. //修改透前评估
  237. func UpadatePredialysisEvaluation(predialysisEvaluation *models.PredialysisEvaluation) error {
  238. err := writeDb.Save(&predialysisEvaluation).Error
  239. return err
  240. }
  241. //修改双人核对
  242. func UpdateDoubleCheck(check *models.DoubleCheck) error {
  243. err := writeDb.Save(&check).Error
  244. return err
  245. }
  246. //修改透后评估
  247. func UpdateAssessmentAfterDislysisRecord(assessmentAfterDislysis *models.AssessmentAfterDislysis) error {
  248. err := writeDb.Save(&assessmentAfterDislysis).Error
  249. return err
  250. }
  251. //修改
  252. func UpdateSummeRecord(treatmentSummary *models.TreatmentSummary) error {
  253. err := writeDb.Save(&treatmentSummary).Error
  254. return err
  255. }
  256. // func UpdateDoctorAdviceById(m *models.DoctorAdvice, id int64, patientId int64) error {
  257. // tx := writeDb.Begin()
  258. // err := tx.Model(&models.DoctorAdvice{}).Where("patient_id = ? AND id = ? AND status = 1 ", patientId, id).Updates(map[string]interface{}{"execution_time": m.ExecutionTime, "execution_staff": m.ExecutionStaff, "execution_state": 1, "checker": m.Checker}).Error
  259. // if err != nil {
  260. // tx.Rollback()
  261. // }
  262. // tx.Commit()
  263. // return err
  264. // }
  265. func ExceDoctorAdviceById(m *models.DoctorAdvice, id int64, patientId int64) error {
  266. err := writeDb.Model(&models.DoctorAdvice{}).Where("patient_id = ? AND (id = ? Or parent_id=?) AND status = 1 ", patientId, id, id).Updates(map[string]interface{}{"execution_time": m.ExecutionTime, "execution_staff": m.ExecutionStaff, "execution_state": 1}).Error
  267. return err
  268. }
  269. func CheckDoctorAdviceById(m *models.DoctorAdvice, id int64, patientId int64) error {
  270. err := writeDb.Model(&models.DoctorAdvice{}).Where("patient_id = ? AND (id = ? Or parent_id=?) AND status = 1 ", patientId, id, id).Updates(map[string]interface{}{"check_time": m.CheckTime, "checker": m.Checker, "check_state": 1}).Error
  271. return err
  272. }
  273. func CreateMonitor(m *models.MonitoringRecord) (err error) {
  274. return writeDb.Create(m).Error
  275. }
  276. func UpdateMonitor(m *models.MonitoringRecord) (err error) {
  277. return writeDb.Save(m).Error
  278. }
  279. func DelMonitor(patientId int64, recordId int64) error {
  280. tx := writeDb.Begin()
  281. updateTime := time.Now().Unix()
  282. err := tx.Model(&models.MonitoringRecord{}).Where("patient_id = ? AND id = ? AND status = 1 ", patientId, recordId).Updates(map[string]interface{}{"status": 0, "updated_time": updateTime}).Error
  283. if err != nil {
  284. tx.Rollback()
  285. }
  286. tx.Commit()
  287. return err
  288. }
  289. //修改长期处方
  290. func UpdateSolution(solution *models.DialysisSolution) {
  291. writeDb.Save(&solution)
  292. }
  293. //创建长期处方
  294. func CreateSolution(solution *models.DialysisSolution) {
  295. writeDb.Create(&solution)
  296. }
  297. func SavePrescriptionAndCreateSolution(solution *models.DialysisSolution, prescription *models.DialysisPrescription) (err error) {
  298. tx := writeDb.Begin()
  299. if prescription.ID > 0 {
  300. err = tx.Save(&prescription).Error
  301. } else {
  302. err = tx.Model(&models.DialysisPrescription{}).Create(&prescription).Error
  303. }
  304. if err != nil {
  305. tx.Rollback()
  306. return
  307. }
  308. err = tx.Create(&solution).Error
  309. if err != nil {
  310. tx.Rollback()
  311. return
  312. }
  313. tx.Commit()
  314. return
  315. }
  316. //透析处方
  317. func FindDialysisPrescriptionByReordDate(id int64, recordDate int64, orgId int64) (error, *models.DialysisPrescription) {
  318. dialysisPrescription := models.DialysisPrescription{}
  319. err := readDb.Model(&dialysisPrescription).Where("patient_id = ? AND record_date = ? AND status = 1 AND user_org_id = ? ", id, recordDate, orgId).Find(&dialysisPrescription).Error
  320. return err, &dialysisPrescription
  321. }
  322. func FindReceiveTreatmentAssesByReordDate(id int64, recordDate int64, orgId int64) (error, *models.ReceiveTreatmentAsses) {
  323. receiveTreatmentAsses := models.ReceiveTreatmentAsses{}
  324. err := readDb.Model(&receiveTreatmentAsses).Where("patient_id = ? AND record_date = ? AND status = 1 AND user_org_id = ?", id, recordDate, orgId).Find(&receiveTreatmentAsses).Error
  325. return err, &receiveTreatmentAsses
  326. }
  327. //透前评估
  328. func FindPredialysisEvaluationByReordDate(id int64, recordDate int64, orgId int64) (error, *models.PredialysisEvaluation) {
  329. dialysisEvaluation := models.PredialysisEvaluation{}
  330. err := readDb.Model(&models.PredialysisEvaluation{}).Where("patient_id = ? AND assessment_date = ? AND status = 1 AND user_org_id = ?", id, recordDate, orgId).First(&dialysisEvaluation).Error
  331. return err, &dialysisEvaluation
  332. }
  333. //双人核对
  334. func FindDoubleCheckByReordDate(id int64, recordDate int64, orgId int64) (error, *models.DoubleCheck) {
  335. check := models.DoubleCheck{}
  336. err := readDb.Model(&models.DoubleCheck{}).Where("patient_id = ? AND check_date = ? AND status = 1 AND user_org_id = ?", id, recordDate, orgId).First(&check).Error
  337. return err, &check
  338. }
  339. func FindAssessmentAfterDislysisByReordDate(id int64, recordDate int64, orgId int64) (error, *models.AssessmentAfterDislysis) {
  340. assessmentAfterDislysis := models.AssessmentAfterDislysis{}
  341. err := readDb.Model(&models.AssessmentAfterDislysis{}).Where("patient_id = ? AND assessment_date = ? AND status = 1 AND user_org_id = ?", id, recordDate, orgId).First(&assessmentAfterDislysis).Error
  342. return err, &assessmentAfterDislysis
  343. }
  344. func FindTreatmentSummaryByReordDate(id int64, recordDate int64, orgId int64) (error, *models.TreatmentSummary) {
  345. summary := models.TreatmentSummary{}
  346. err := readDb.Model(&models.TreatmentSummary{}).Where("patient_id = ? AND assessment_date = ? AND status = 1 AND user_org_id = ?", id, recordDate, orgId).First(&summary).Error
  347. return err, &summary
  348. }
  349. func FindDoctorAdviceByReordDate(id int64, recordDate int64, orgId int64) (error, *models.DoctorAdvice) {
  350. advice := models.DoctorAdvice{}
  351. err := readDb.Model(&models.DoctorAdvice{}).Where("patient_id = ? AND record_date = ? AND parent_id = 0 AND status = 1 AND user_org_id = ? ", id, recordDate, orgId).First(&advice).Error
  352. return err, &advice
  353. }
  354. func FindDialysisRecordById(orgId int64, id int64, recordDate int64) (error, *models.DialysisOrder) {
  355. var count int64
  356. dialysisRecord := models.DialysisOrder{}
  357. err := readDb.Model(&models.DialysisOrder{}).Where("user_org_id = ? AND patient_id = ? AND dialysis_date = ?", orgId, id, recordDate).Find(&dialysisRecord).Count(&count).Error
  358. if count <= 0 {
  359. return err, nil
  360. }
  361. return err, &dialysisRecord
  362. }
  363. func FindDialysisSchedualById(orgId int64, id int64, recordDate int64) (error, *models.Schedule) {
  364. var count int64
  365. schedule := models.Schedule{}
  366. err := readDb.Model(&models.Schedule{}).Where("user_org_id = ? AND patient_id = ? AND schedule_date = ?", orgId, id, recordDate).Find(&schedule).Count(&count).Error
  367. if count <= 0 {
  368. return err, nil
  369. }
  370. return err, &schedule
  371. }
  372. func CreateDialysisRecord(id int64, orgId int64, record *models.DialysisOrder) error {
  373. err := writeDb.Model(&models.DialysisOrder{}).Where("user_org_id = ? AND patient_id = ? ", orgId, id).Create(&record).Error
  374. if err != nil {
  375. return err
  376. }
  377. dialysisPrescriptionErr := UpDateRecordIdToDialysisPrescription(id, orgId, record.DialysisDate, record.ID)
  378. if dialysisPrescriptionErr != nil {
  379. return dialysisPrescriptionErr
  380. }
  381. receiveTreatmentAssesErr := UpDateRecordIdToReceiveTreatmentAsses(id, orgId, record.DialysisDate, record.ID)
  382. if receiveTreatmentAssesErr != nil {
  383. return receiveTreatmentAssesErr
  384. }
  385. predialysisEvaluationErr := UpDateRecordIdToPredialysisEvaluation(id, orgId, record.DialysisDate, record.ID)
  386. if predialysisEvaluationErr != nil {
  387. return predialysisEvaluationErr
  388. }
  389. treatmentSummaryErr := UpDateRecordIdToTreatmentSummary(id, orgId, record.DialysisDate, record.ID)
  390. if treatmentSummaryErr != nil {
  391. return treatmentSummaryErr
  392. }
  393. doubleCheckErr := UpDateRecordIdToDoubleCheck(id, orgId, record.DialysisDate, record.ID)
  394. if doubleCheckErr != nil {
  395. return doubleCheckErr
  396. }
  397. adviceErrr := UpDateRecordIdToDoctorAdvice(id, orgId, record.DialysisDate, record.ID)
  398. if adviceErrr != nil {
  399. return adviceErrr
  400. }
  401. //透析监测
  402. records, err := FindAllMonitorRecord(orgId, id, record.DialysisDate)
  403. if err != nil {
  404. return err
  405. }
  406. var Ids []int64
  407. for _, item := range records {
  408. Ids = append(Ids, item.ID)
  409. }
  410. monitorErr := UpDateRecordIdToMonitor(Ids, record.ID)
  411. if monitorErr != nil {
  412. return monitorErr
  413. }
  414. return nil
  415. }
  416. func ModifyDialysisRecord(id int64, finishNurse int64, end_time int64, admin_user_id int64) error {
  417. err := writeDb.Model(&models.DialysisOrder{}).Where("id = ? AND status = 1 ", id).Updates(map[string]interface{}{"updated_time": time.Now().Unix(), "stage": 2, "finish_nurse": finishNurse, "end_time": end_time, "finish_creator": admin_user_id, "finish_modifier": admin_user_id}).Error
  418. return err
  419. }
  420. func UpdateSolutionByPatientId(id int64) error {
  421. err := writeDb.Model(&models.Patients{}).Where("id = ? AND status = 1 ", id).UpdateColumn("total_dialysis", gorm.Expr("total_dialysis + ?", 1)).Error
  422. return err
  423. }
  424. //更新透析处方记录ID
  425. func UpDateRecordIdToDialysisPrescription(patientId int64, orgId int64, recordTime int64, recordId int64) error {
  426. errors := writeDb.Model(&models.DialysisPrescription{}).Where("user_org_id = ? AND patient_id = ? AND record_date = ? ", orgId, patientId, recordTime).Updates(map[string]interface{}{"record_id": recordId}).Error
  427. return errors
  428. }
  429. func UpDateRecordIdToReceiveTreatmentAsses(patientId int64, orgId int64, recordTime int64, recordId int64) error {
  430. errors := writeDb.Model(&models.ReceiveTreatmentAsses{}).Where("user_org_id = ? AND patient_id = ? AND record_date = ? ", orgId, patientId, recordTime).Updates(map[string]interface{}{"record_id": recordId}).Error
  431. return errors
  432. }
  433. func UpDateRecordIdToPredialysisEvaluation(patientId int64, orgId int64, recordTime int64, recordId int64) error {
  434. errors := writeDb.Model(&models.PredialysisEvaluation{}).Where("user_org_id = ? AND patient_id = ? AND assessment_date = ? ", orgId, patientId, recordTime).Updates(map[string]interface{}{"dialysis_order_id": recordId}).Error
  435. return errors
  436. }
  437. func UpDateRecordIdToAssessmentAfterDislysis(patientId int64, orgId int64, recordTime int64, recordId int64) error {
  438. errors := writeDb.Model(&models.AssessmentAfterDislysis{}).Where("user_org_id = ? AND patient_id = ? AND assessment_date = ? ", orgId, patientId, recordTime).Updates(map[string]interface{}{"dialysis_order_id": recordId}).Error
  439. return errors
  440. }
  441. func UpDateRecordIdToTreatmentSummary(patientId int64, orgId int64, recordTime int64, recordId int64) error {
  442. errors := writeDb.Model(&models.TreatmentSummary{}).Where("user_org_id = ? AND patient_id = ? AND assessment_date = ? ", orgId, patientId, recordTime).Updates(map[string]interface{}{"dialysis_order_id": recordId}).Error
  443. return errors
  444. }
  445. func UpDateRecordIdToMonitor(Ids []int64, recordId int64) error {
  446. errors := writeDb.Model(&models.MonitoringRecord{}).Where("id IN (?)", Ids).Update(map[string]interface{}{"dialysis_order_id": recordId}).Error
  447. return errors
  448. }
  449. func UpDateRecordIdToDoubleCheck(patientId int64, orgId int64, recordTime int64, recordId int64) error {
  450. errors := writeDb.Model(&models.DoubleCheck{}).Where("user_org_id = ? AND patient_id = ? AND check_date = ? ", orgId, patientId, recordTime).Updates(map[string]interface{}{"dialysis_order_id": recordId}).Error
  451. return errors
  452. }
  453. func UpDateRecordIdToDoctorAdvice(patientId int64, orgId int64, recordTime int64, recordId int64) error {
  454. errors := writeDb.Model(&models.DoctorAdvice{}).Where("user_org_id = ? AND patient_id = ? AND record_date = ? ", orgId, patientId, recordTime).Updates(map[string]interface{}{"dialysis_order_id": recordId}).Error
  455. return errors
  456. }
  457. //透析处方
  458. func FindPatientPrescribeWidyDevideById(orgID int64, patientsId int64, recordDate int64) (patient models.DialysisPrescriptionAddtional, err error) {
  459. err = readDb.Preload("Device", "status = 1 AND org_id = ?", orgID).
  460. Model(&models.DialysisPrescriptionAddtional{}).
  461. Where("patient_id = ? and user_org_id=? and status=1 and record_date = ? ", patientsId, orgID, recordDate).
  462. First(&patient).Error
  463. return
  464. }
  465. func FindDialysisMode(orgID int64, patientId int64, scheduleDate int64) (models.XtSchedule, error) {
  466. schedule := models.XtSchedule{}
  467. err = readDb.Where("user_org_id = ? and patient_id = ? and schedule_date = ? ", orgID, patientId, scheduleDate).Find(&schedule).Error
  468. return schedule, err
  469. }
  470. func FindDialysisMacher(orgid int64, patientid int64, dialysisDate int64) (models.XtDialysisOrder, error) {
  471. orders := models.XtDialysisOrder{}
  472. db := readDb.Table("xt_dialysis_order as o").Where("o.status = 1")
  473. table := readDb.Table("xt_device_number as n")
  474. fmt.Print("table", table)
  475. err := db.Select("o.id,o.dialysis_date,o.user_org_id,o.patient_id,o.prescription_id,o.stage,remark,o.bed_id,o.start_nurse,o.finish_nurse,o.status,o.created_time,o.updated_time,o.start_time,o.end_time,o.puncture_nurse,o.creator,o.modifier,o.finish_creator,o.finish_modifier,o.schedual_type,n.number,r.user_name").Joins("left join xt_device_number as n on n.id = o.bed_id").Joins("left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = o.start_nurse").Where("o.user_org_id = ? and o.patient_id = ? and o.dialysis_date =?", orgid, patientid, dialysisDate).Scan(&orders).Error
  476. return orders, err
  477. }
  478. func FindPunctureNurse(adminid int64, appid int64, orgid int64) (models.App_Roles, error) {
  479. role := models.App_Roles{}
  480. //err := UserReadDB().Where("admin_user_id = ? and app_id = ? and org_id = ?", adminid, appid, orgid).Find(&role).Error
  481. //return role, err
  482. db := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status =1")
  483. err := db.Select("r.id,r.admin_user_id,r.org_id,r.role_id,r.user_name,r.avatar,r.user_type,r.user_title,r.intro,r.status,r.ctime,e.url").Joins("left join sgj_user_admin_electronic_signature as e on e.creator = r.admin_user_id").Where("r.admin_user_id = ? and r.app_id = ? and r.org_id = ?", adminid, appid, orgid).Find(&role).Error
  484. return role, err
  485. }
  486. func FindeStartNuse(adminid int64, appid int64, orgid int64) (models.App_Roles, error) {
  487. role := models.App_Roles{}
  488. db := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  489. err := db.Select("r.id,r.admin_user_id,r.org_id,r.role_id,r.user_name,r.avatar,r.user_type,r.user_title,r.intro,r.status,r.ctime,e.url").Joins("left join sgj_user_admin_electronic_signature as e on e.creator = r.admin_user_id").Where("r.admin_user_id = ? and r.app_id = ? and r.org_id = ?", adminid, appid, orgid).Find(&role).Error
  490. //err := UserReadDB().Where("admin_user_id = ? and app_id = ? and org_id = ?", adminid, appid, orgid).Find(&role).Error
  491. return role, err
  492. }
  493. func FindDialysisWay(orgid int64, patientid int64, recorddate int64) (models.DialysisPrescription, error) {
  494. prescription := models.DialysisPrescription{}
  495. err := readDb.Where("user_org_id = ? and patient_id = ? and record_date = ? and status =1", orgid, patientid, recorddate).Find(&prescription).Error
  496. return prescription, err
  497. }
  498. func FindDoctorOrder(orgid int64, patientid int64, adviceDate int64) (models.XtDoctorAdvice, error) {
  499. advices := models.XtDoctorAdvice{}
  500. db := readDb.Table("xt_doctor_advice as a").Where("a.status = ?", 1)
  501. err := db.Select("a.id,a.user_org_id,a.patient_id,a.advice_type,a.advice_date,a.start_time,a.advice_name,a.advice_desc,a.reminder_date,a.single_dose,a.single_dose_unit,a.prescribing_number,a.prescribing_number_unit,a.delivery_way,a.execution_frequency,a.advice_doctor,a.status,a.created_time,a.updated_time,a.advice_affirm,a.remark,a.stop_time,a.stop_reason,a.stop_doctor,a.stop_state,a.parent_id,a.execution_time,a.execution_staff,a.execution_state,a.checker,a.record_date,a.dialysis_order_id,a.check_time,a.check_state,a.drug_spec,a.drug_spec_unit,a.groupno,a.remind_type,a.frequency_type,a.day_count,a.week_day,a.template_id").Where("a.user_org_id = ? and a.patient_id = ? and a.advice_date = ?", orgid, patientid, adviceDate).Find(&advices).Error
  502. return advices, err
  503. }
  504. func FindDoctor(adminuserid int64, appid int64, orgid int64) (models.App_Role, error) {
  505. role := models.App_Role{}
  506. err := UserReadDB().Where("admin_user_id = ? and app_id = ? and org_id = ?", adminuserid, appid, orgid).Find(&role).Error
  507. return role, err
  508. }
  509. func FindDoctorName(adminid int64, appid int64, orgid int64) (models.App_Roles, error) {
  510. role := models.App_Roles{}
  511. db := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  512. err := db.Select("r.id,r.admin_user_id,r.org_id,r.role_id,r.user_name,r.avatar,r.user_type,r.user_title,r.intro,r.status,r.ctime,e.url").Joins("left join sgj_user_admin_electronic_signature as e on e.creator = r.admin_user_id").Where("r.admin_user_id = ? and r.app_id = ? and r.org_id = ?", adminid, appid, orgid).Find(&role).Error
  513. //err := UserReadDB().Where("admin_user_id = ? and app_id = ? and org_id = ?", adminid, appid, orgid).Find(&role).Error
  514. return role, err
  515. }
  516. /////////////// 新 18-10-22
  517. func GetDialysisScheduals(orgID int64, scheduleDate int64) ([]*MDialysisScheduleVM, error) {
  518. var vms []*MDialysisScheduleVM
  519. db := readDb.
  520. Table("xt_schedule").
  521. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  522. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  523. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  524. // Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  525. Preload("DialysisOrder", "status = 1 AND user_org_id = ?", orgID).
  526. Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ?", orgID).
  527. Preload("Prescription", "status = 1 AND user_org_id = ?", orgID).
  528. Preload("TreatmentSummary", "status = 1 AND user_org_id = ?", orgID).
  529. // Preload("DialysisOrder.MonitoringRecords", "status = 1 AND user_org_id = ?", orgID).
  530. Where("status = 1 AND user_org_id = ?", orgID)
  531. if scheduleDate != 0 {
  532. db = db.Where("schedule_date = ?", scheduleDate)
  533. }
  534. err := db.Find(&vms).Error
  535. return vms, err
  536. }
  537. func GetLastAcceptsAssessment(patient int64, orgId int64) (error, models.ReceiveTreatmentAsses) {
  538. var receiveTreatmentAsses models.ReceiveTreatmentAsses
  539. err := readDb.Model(&models.ReceiveTreatmentAsses{}).Where("patient_id = ? AND status = 1 AND user_org_id = ?", patient, orgId).Last(&receiveTreatmentAsses).Error
  540. return err, receiveTreatmentAsses
  541. }
  542. func GetDialysisWatch(orgID int64, schedulDate int64, schedulType int64, partitionType int64, page int64, limit int64) (schedule []*models.DialysisScheduleOne, err error, total int64) {
  543. db := readDb.
  544. Model(&models.MonitorDialysisSchedule{}).
  545. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  546. // Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  547. Preload("TreatmentMode", "status = 1 ").
  548. Preload("Prescription", "status = 1 AND user_org_id = ? and record_date = ?", orgID, schedulDate).
  549. Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ? and assessment_date = ?", orgID, schedulDate).
  550. Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ? and assessment_date = ?", orgID).
  551. Preload("MonitoringRecord", func(db *gorm.DB) *gorm.DB {
  552. return db.Where("status = 1 AND user_org_id = ? and monitoring_date = ?", orgID, schedulDate).Order("operate_time asc")
  553. }).
  554. Preload("DialysisOrder", "status = 1 AND user_org_id = ? and dialysis_date = ?", orgID, schedulDate).
  555. Preload("MonitorPatients", "status = 1 AND user_org_id = ?", orgID)
  556. db = db.Where("xt_schedule.status = 1 AND user_org_id = ?", orgID)
  557. if schedulDate > 0 {
  558. db = db.Where("schedule_date = ?", schedulDate)
  559. }
  560. if schedulType > 0 {
  561. db = db.Where("schedule_type = ?", schedulType)
  562. }
  563. if partitionType > 0 {
  564. 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)
  565. // db = db.Where("partition_id = ?", partitionType)
  566. }
  567. offset := (page - 1) * limit
  568. err = db.Count(&total).Offset(offset).Limit(limit).Order("bed_id desc").Find(&schedule).Error
  569. return schedule, err, total
  570. }
  571. func GetDialysisWatchByKeyword(orgID int64, keyword string, schedulType int64, partitionType int64, page int64, limit int64) ([]*models.DialysisSchedule, error, int64) {
  572. var patients []*models.Patients
  573. 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
  574. if getPatientErr != nil {
  575. return nil, getPatientErr, 0
  576. }
  577. patientIDs := make([]int64, len(patients))
  578. for index, patient := range patients {
  579. patientIDs[index] = patient.ID
  580. }
  581. db := readDb.
  582. Model(&models.DialysisSchedule{}).
  583. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  584. // Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  585. Preload("TreatmentMode", "status = 1").
  586. Preload("Prescription", "status = 1 AND user_org_id = ?", orgID).
  587. Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ?", orgID).
  588. Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ?", orgID).
  589. Preload("MonitoringRecord", "status = 1 AND user_org_id = ?", orgID).
  590. Preload("DialysisOrder", "status = 1 AND user_org_id = ?", orgID).
  591. Preload("MonitorPatients", "status = 1 AND user_org_id = ?", orgID)
  592. db = db.Where("xt_schedule.status = 1 AND patient_id in (?)", patientIDs)
  593. if schedulType > 0 {
  594. db = db.Where("schedule_type = ?", schedulType)
  595. }
  596. if partitionType > 0 {
  597. 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)
  598. // db = db.Where("partition_id = ?", partitionType)
  599. }
  600. var schedules []*models.DialysisSchedule
  601. total := int64(0)
  602. err := db.Count(&total).Offset(limit * (page - 1)).Limit(limit).Order("schedule_date desc").Find(&schedules).Error
  603. return schedules, err, total
  604. }
  605. func GetAdminUserES(orgID int64, appID int64, ids []int64) (es []*models.AdminUserElectronicSignature, err error) {
  606. err = readUserDb.Model(&models.AdminUserElectronicSignature{}).Where("org_id=? and app_id=? and creator in (?) and status=1", orgID, appID, ids).Find(&es).Error
  607. return
  608. }
  609. //
  610. //func GetAdminUserEsOne(orgid int64) (admin []*models.UserAdminRoles, err error) {
  611. //
  612. // db := readUserDb.Table("sgj_user_admin_role as x").Where("x.status = 1")
  613. // table := readUserDb.Table("sgj_user_admin_electronic_signature as s")
  614. // fmt.Println("table", table)
  615. // err = db.Select("x.id,x.admin_user_id,x.user_name,x.org_id,s.creator,s.url,s.hash").Joins("left join sgj_user_admin_electronic_signature as s on s.creator = x.admin_user_id").Where("x.org_id = ?", orgid).Scan(&admin).Error
  616. // return admin, err
  617. //}
  618. func GetAdminUserEsOne(orgid int64) (admin []*models.UserAdminRoles, err error) {
  619. db := readUserDb.Table("sgj_user_admin_role as x").Where("x.status =1")
  620. table := readUserDb.Table("sgj_user_admin_electronic_signature as s")
  621. fmt.Println("table", table)
  622. err = db.Select("x.id,x.admin_user_id,x.user_name,x.org_id,s.creator,s.url,s.hash").Joins("left join sgj_user_admin_electronic_signature as s on s.creator = x.admin_user_id").Where("x.org_id = ?", orgid).Scan(&admin).Error
  623. return admin, err
  624. }
  625. func UpDateDialysisPrescriptionDoctorSign(patient int64, date int64, orgId int64, doctor_id int64) (err error) {
  626. err = writeDb.Model(&models.DialysisPrescription{}).Where("user_org_id = ? AND patient_id = ? AND record_date = ? ", orgId, patient, date).Updates(map[string]interface{}{"prescription_doctor": doctor_id}).Error
  627. return
  628. }
  629. func GetAllAdminUserES(orgID int64, appId int64) (es []*models.AdminUserElectronicSignature, err error) {
  630. err = readUserDb.Model(&models.AdminUserElectronicSignature{}).Where("org_id=? and app_id = ? and status=1", orgID, appId).Find(&es).Error
  631. return
  632. }
  633. func GetAllStarfEs(orgid int64) (es []*models.SgjUserAdminRoles, err error) {
  634. db := readUserDb.Table("sgj_user_admin_role as x").Where("x.status = 1")
  635. table := readUserDb.Table("sgj_user_admin_electronic_signature as s")
  636. fmt.Println("table", table)
  637. err = db.Select("x.id,x.admin_user_id,x.org_id,x.app_id,x.role_id,x.user_name,x.avatar,x.user_type,s.creator,s.url,s.hash").Joins("left join sgj_user_admin_electronic_signature as s on s.creator = x.admin_user_id and s.status = 1").Where("x.org_id = ?", orgid).Scan(&es).Error
  638. return es, err
  639. }
  640. func FindLastMonitorRecord(patient_id int64, org_id int64) (record models.MonitoringRecord, err error) {
  641. err = readDb.Model(&models.MonitoringRecord{}).Where("patient_id = ? AND status = 1 AND user_org_id = ?", patient_id, org_id).Last(&record).Error
  642. return
  643. }
  644. func FindLastMonitorRecordToday(patient_id int64, org_id int64, monitoring_date int64) (record models.MonitoringRecord, err error) {
  645. err = readDb.Model(&models.MonitoringRecord{}).Where("patient_id = ? AND status = 1 AND user_org_id = ? AND monitoring_date = ?", patient_id, org_id, monitoring_date).Last(&record).Error
  646. return
  647. }
  648. func FindFirstMonitorRecordToday(patient_id int64, org_id int64, monitoring_date int64) (record models.MonitoringRecord, err error) {
  649. err = readDb.Model(&models.MonitoringRecord{}).Where("patient_id = ? AND status = 1 AND user_org_id = ? AND monitoring_date = ?", patient_id, org_id, monitoring_date).First(&record).Error
  650. return
  651. }
  652. func ExceDoctorAdviceByGroupNo(m *models.DoctorAdvice, groupNo int64, org_id int64) (err error) {
  653. ut := writeDb.Begin()
  654. err = ut.Model(&models.DoctorAdvice{}).Where("groupno = ? AND user_org_id = ? AND status = 1 AND execution_state = 2", groupNo, org_id).Updates(map[string]interface{}{"execution_time": m.ExecutionTime, "execution_staff": m.ExecutionStaff, "execution_state": 1}).Error
  655. if err != nil {
  656. ut.Rollback()
  657. return
  658. }
  659. ut.Commit()
  660. return err
  661. }
  662. func ExceOldDoctorAdvice(m *models.DoctorAdvice, advice_id int64) (err error) {
  663. ut := writeDb.Begin()
  664. err = ut.Model(&models.DoctorAdvice{}).Where("status = 1 AND id = ?", advice_id).Updates(map[string]interface{}{"execution_time": m.ExecutionTime, "execution_staff": m.ExecutionStaff, "execution_state": 1}).Error
  665. if err != nil {
  666. ut.Rollback()
  667. return
  668. }
  669. err = ut.Model(&models.DoctorAdvice{}).Where("status = 1 AND parent_id = ?", advice_id).Updates(map[string]interface{}{"execution_time": m.ExecutionTime, "execution_staff": m.ExecutionStaff, "execution_state": 1}).Error
  670. if err != nil {
  671. ut.Rollback()
  672. return
  673. }
  674. ut.Commit()
  675. return err
  676. }
  677. func CheckDoctorAdviceByGroupNo(m *models.DoctorAdvice, groupNo int64, org_id int64) (err error) {
  678. ut := writeDb.Begin()
  679. err = ut.Model(&models.DoctorAdvice{}).Where("groupno = ? AND user_org_id = ? AND status = 1 AND check_state = 0", groupNo, org_id).Updates(map[string]interface{}{"check_time": m.CheckTime, "checker": m.Checker, "check_state": 1}).Error
  680. if err != nil {
  681. ut.Rollback()
  682. return
  683. }
  684. ut.Commit()
  685. return err
  686. }
  687. func CheckOldDoctorAdvice(m *models.DoctorAdvice, advice_id int64) (err error) {
  688. ut := writeDb.Begin()
  689. err = writeDb.Model(&models.DoctorAdvice{}).Where("status = 1 AND id = ?", advice_id).Updates(map[string]interface{}{"check_time": m.CheckTime, "checker": m.Checker, "check_state": 1}).Error
  690. if err != nil {
  691. ut.Rollback()
  692. return
  693. }
  694. err = writeDb.Model(&models.DoctorAdvice{}).Where("status = 1 AND parent_id = ?", advice_id).Updates(map[string]interface{}{"check_time": m.CheckTime, "checker": m.Checker, "check_state": 1}).Error
  695. if err != nil {
  696. ut.Rollback()
  697. return
  698. }
  699. ut.Commit()
  700. return err
  701. }
  702. func DeleteDoctorAdviceByGroupNo(m *models.DoctorAdvice) (err error) {
  703. ut := writeDb.Begin()
  704. err = ut.Model(&models.DoctorAdvice{}).Where("groupno = ? AND user_org_id = ?", m.GroupNo, m.UserOrgId).Update(map[string]interface{}{"UpdatedTime": m.UpdatedTime, "Status": 0, "modifier": m.Modifier}).Error
  705. if err != nil {
  706. ut.Rollback()
  707. return
  708. }
  709. ut.Commit()
  710. return
  711. }
  712. func GetDialysisBoard(orgID int64, scheduleDate int64, offset, limit int64) ([]*models.DialysisBoardVM, error) {
  713. var boards []*models.DialysisBoardVM
  714. db := readDb.
  715. Table("xt_schedule").
  716. Preload("Patient", "status = 1 AND user_org_id = ?", orgID).
  717. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  718. Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  719. Preload("DialysisOrder", "status = 1 AND user_org_id = ?", orgID).
  720. Preload("SigninAndWeigh", "status = 1 AND user_org_id = ?", orgID).
  721. Preload("NextSchedules", func(db *gorm.DB) *gorm.DB {
  722. return db.Where("user_org_id = ? AND schedule_date > ? AND status = 1", orgID, scheduleDate).
  723. Order("schedule_date asc, id asc")
  724. }).
  725. Where("status = 1 AND user_org_id = ?", orgID)
  726. if scheduleDate != 0 {
  727. db = db.Where("schedule_date = ?", scheduleDate)
  728. }
  729. err := db.Offset(offset).Limit(limit).Find(&boards).Error
  730. return boards, err
  731. }
  732. func GetOneDialysisOrder(orgID, orderData int64, patient_id int64) (*models.DialysisOrder, error) {
  733. var order models.DialysisOrder
  734. var err error
  735. err = readDb.Model(&models.DialysisOrder{}).Where("user_org_id=? and dialysis_date=? and status=1 and patient_id = ?", orgID, orderData, patient_id).First(&order).Error
  736. if err == gorm.ErrRecordNotFound {
  737. return nil, nil
  738. }
  739. if err != nil {
  740. return nil, err
  741. }
  742. return &order, nil
  743. }
  744. func GetOrgInfoTemplate(orgID int64) (models.GobalTemplate, error) {
  745. var templateInfo models.GobalTemplate
  746. var err error
  747. err = readDb.Model(&models.GobalTemplate{}).Where("org_id=? and status=1", orgID).First(&templateInfo).Error
  748. return templateInfo, err
  749. }
  750. func FindAdminRoleTypeById(orgId int64, adminUserId int64, appId int64) (models.App_Role, error) {
  751. var appRole models.App_Role
  752. var err error
  753. err = readUserDb.Model(&models.App_Role{}).Where("org_id =? and app_id = ? and admin_user_id = ? and status=1", orgId, appId, adminUserId).First(&appRole).Error
  754. return appRole, err
  755. }
  756. func BatchExceOldDoctorAdvice(m *models.DoctorAdvice, ids []string) (err error) {
  757. ut := writeDb.Begin()
  758. err = ut.Model(&models.DoctorAdvice{}).Where("status = 1 AND id IN (?) AND execution_state = 2", ids).Updates(map[string]interface{}{"execution_time": m.ExecutionTime, "execution_staff": m.ExecutionStaff, "execution_state": 1}).Error
  759. if err != nil {
  760. ut.Rollback()
  761. return
  762. }
  763. err = ut.Model(&models.DoctorAdvice{}).Where("status = 1 AND parent_id IN (?) AND execution_state = 2 ", ids).Updates(map[string]interface{}{"execution_time": m.ExecutionTime, "execution_staff": m.ExecutionStaff, "execution_state": 1}).Error
  764. if err != nil {
  765. ut.Rollback()
  766. return
  767. }
  768. ut.Commit()
  769. return err
  770. }
  771. func BatchCheckOldDoctorAdvice(m *models.DoctorAdvice, ids []string, org_id int64, date int64) (err error) {
  772. ut := writeDb.Begin()
  773. err = writeDb.Model(&models.DoctorAdvice{}).Where("status = 1 AND id IN (?) AND user_org_id = ? AND advice_date = ?", ids, org_id, date).Updates(map[string]interface{}{"check_time": m.CheckTime, "checker": m.Checker, "check_state": 1}).Error
  774. if err != nil {
  775. ut.Rollback()
  776. return
  777. }
  778. err = writeDb.Model(&models.DoctorAdvice{}).Where("status = 1 AND parent_id IN (?) AND user_org_id = ? AND advice_date = ?", ids, org_id, date).Updates(map[string]interface{}{"check_time": m.CheckTime, "checker": m.Checker, "check_state": 1}).Error
  779. if err != nil {
  780. ut.Rollback()
  781. return
  782. }
  783. ut.Commit()
  784. return err
  785. }
  786. func ModifyScheduleMode(mode_id int64, patient_id int64, recordDate int64, org_id int64) {
  787. ut := writeDb.Begin()
  788. err = writeDb.Model(&models.Schedule{}).Where("status = 1 AND user_org_id = ? AND patient_id = ? AND schedule_date = ? ", org_id, patient_id, recordDate).Updates(map[string]interface{}{"updated_time": time.Now().Unix(), "mode_id": mode_id}).Error
  789. if err != nil {
  790. ut.Rollback()
  791. return
  792. }
  793. ut.Commit()
  794. }
  795. func FindDialysisOrderById(id int64) (error, *models.DialysisOrder) {
  796. dialysisRecord := models.DialysisOrder{}
  797. err := readDb.Model(&models.DialysisOrder{}).Where("id = ? AND status = 1", id).Find(&dialysisRecord).Error
  798. return err, &dialysisRecord
  799. }
  800. func ModifyExceDoctorAdviceByGroupNo(m *models.DoctorAdvice, groupNo int64, org_id int64) (err error) {
  801. ut := writeDb.Begin()
  802. err = ut.Model(&models.DoctorAdvice{}).Where("groupno = ? AND user_org_id = ? AND status = 1 AND execution_state = 1", groupNo, org_id).Updates(map[string]interface{}{"execution_time": m.ExecutionTime}).Error
  803. if err != nil {
  804. ut.Rollback()
  805. return
  806. }
  807. ut.Commit()
  808. return err
  809. }
  810. func BatchModifyExceOldDoctorAdvice(m *models.DoctorAdvice, ids []string) (err error) {
  811. ut := writeDb.Begin()
  812. err = ut.Model(&models.DoctorAdvice{}).Where("status = 1 AND id IN (?) AND execution_state = 1", ids).Updates(map[string]interface{}{"execution_time": m.ExecutionTime}).Error
  813. if err != nil {
  814. ut.Rollback()
  815. return
  816. }
  817. err = ut.Model(&models.DoctorAdvice{}).Where("status = 1 AND parent_id IN (?) AND execution_state = 1 ", ids).Updates(map[string]interface{}{"execution_time": m.ExecutionTime}).Error
  818. if err != nil {
  819. ut.Rollback()
  820. return
  821. }
  822. ut.Commit()
  823. return err
  824. }
  825. func UpdateOrderCount(good_type_id int64, good_id int64, number string, org_id int64) {
  826. writeDb.Model(&models.WarehouseOutInfo{}).Where("good_id = ? AND good_type_id = ? AND org_id = ? AND status = 1 AND is_sys = 1 AND warehouse_out_order_number = ? ", good_id, good_type_id, org_id, number).UpdateColumn("count", gorm.Expr("count - ?", 1))
  827. }
  828. func UpdateOrderInfoDetails(good_type_id int64, good_id int64, time int64, org_id int64, patient_id int64, newNiprocart int64, out *models.WarehouseOutInfo) {
  829. writeDb.Model(&models.AutomaticReduceDetail{}).Where("good_id = ? AND good_type_id = ? AND org_id = ? AND status = 1 AND warehouse_out_order_number = ? AND patient_id = ?", good_id, good_type_id, org_id, out.WarehouseOutOrderNumber, patient_id).Updates(map[string]interface{}{"good_id": newNiprocart, "warehouse_out_id": out.ID, "warehouse_out_order_number": out.WarehouseOutOrderNumber})
  830. }
  831. func FindOrderInfoByGoodId(good_type_id int64, good_id int64, number string, org_id int64) (err error, out models.WarehouseOutInfo) {
  832. err = writeDb.Model(&models.WarehouseOutInfo{}).Where("good_id = ? AND good_type_id = ? AND org_id = ? AND status = 1 AND warehouse_out_order_number = ? ", good_id, good_type_id, org_id, number).Find(&out).Error
  833. return
  834. }
  835. func FindGoodUserById(good_type_id int64, good_id int64, id int64, org_id int64, number string) (user models.AutomaticReduceDetail, err error) {
  836. err = readDb.Model(&models.AutomaticReduceDetail{}).Where("good_id = ? AND good_type_id = ? AND patient_id = ? AND org_id = ? AND warehouse_out_order_number = ? ", good_id, good_type_id, id, org_id, number).First(&user).Error
  837. return
  838. }
  839. func FindOldOrderInfoByGoodId(time int64, org_id int64) (err error, out models.WarehouseOutInfo) {
  840. err = readDb.Model(&models.WarehouseOutInfo{}).Where(" org_id = ? AND status = 1 AND sys_record_time = ? ", org_id, time).First(&out).Error
  841. return
  842. }
  843. func FindStockOutOrderNumberByTime(time int64, org_id int64) (err error, out models.WarehouseOut) {
  844. err = readDb.Model(&models.WarehouseOut{}).Where(" org_id = ? AND status = 1 AND warehouse_out_time = ? ", org_id, time).First(&out).Error
  845. return
  846. }
  847. func UpdateDetails(good_type_id int64, good_id int64, time int64, org_id int64, patient_id int64, newNiprocart int64, out *models.WarehouseOutInfo) {
  848. writeDb.Model(&models.AutomaticReduceDetail{}).Where("good_id = ? AND good_type_id = ? AND org_id = ? AND status = 1 AND warehouse_out_order_number = ? AND patient_id = ?", good_id, good_type_id, org_id, out.WarehouseOutOrderNumber, patient_id).Updates(map[string]interface{}{"good_id": newNiprocart, "warehouse_out_id": out.ID, "warehouse_out_order_number": out.WarehouseOutOrderNumber})
  849. }
  850. func FindPatientIsOpenRemindById(patient_id int64, org_id int64) (patients models.Patients, err error) {
  851. err = readDb.Model(&models.Patients{}).Where("status = 1 AND id = ? AND user_org_id = ?", patient_id, org_id).First(&patients).Error
  852. return
  853. }
  854. func GetMaxLongAdviceGroupID(orgId int64, patient_id int64) (group int64) {
  855. var advice models.DoctorAdvice
  856. err := readDb.Table("xt_doctor_advice").Where("user_org_id=? AND patient_id = ? AND status = 1 AND advice_type = 1 AND frequency_type > 0", orgId, patient_id).Select("max(groupno) as groupno").First(&advice).Error
  857. if err != nil {
  858. fmt.Println(err)
  859. group = 0
  860. }
  861. group = advice.GroupNo
  862. return
  863. }
  864. func GetLastLongAdviceByGroupNo(orgId int64, patient_id int64) (advice []*models.DoctorAdvice, err error) {
  865. err = readDb.Model(&models.DoctorAdvice{}).Where("status = 1 AND user_org_id = ? AND patient_id = ? AND advice_type = 1 AND stop_state = 2 AND parent_id = 0", orgId, patient_id).Preload("ChildDoctorAdvice", "status = 1").Find(&advice).Error
  866. return
  867. }
  868. func GetLastLongAdviceByGroupNoThree(orgId int64, patient_id int64) (advice []*models.DoctorAdvice, err error) {
  869. err = readDb.Model(&models.DoctorAdvice{}).Where("status = 1 AND user_org_id = ? AND patient_id = ? AND advice_type = 1 AND frequency_type = 2 AND stop_state = 2 AND parent_id = 0 ", orgId, patient_id).Preload("ChildDoctorAdvice", "status = 1").Find(&advice).Error
  870. return
  871. }
  872. func FindAllDoctorAdviceByTime(now int64, end int64, patient_id int64, orgId int64, temp_id string) (advice []*models.DoctorAdvice, err error) {
  873. err = readDb.Model(&models.DoctorAdvice{}).Where("status = 1 AND user_org_id = ? AND patient_id = ? AND advice_type = 2 AND advice_date > ? AND advice_date <= ? AND template_id = ? ", orgId, patient_id, end, now, temp_id).Preload("ChildDoctorAdvice", "status = 1").Find(&advice).Error
  874. return
  875. }
  876. func UpdateScheduleModeId(patient_id int64, org_id int64, date int64, mode_id int64) (err error) {
  877. err = writeDb.Model(&models.Schedule{}).Where("status =1 AND user_org_id = ? AND patient_id = ? AND schedule_date = ?", org_id, patient_id, date).Updates(map[string]interface{}{"mode_id": mode_id, "updated_time": time.Now().Unix()}).Error
  878. return
  879. }
  880. func UpdateAssessmentAfterDate(patient_id int64, org_id int64, date int64, actual_treatment_hour int64, actual_treatment_minute int64) (err error) {
  881. err = writeDb.Model(&models.AssessmentAfterDislysis{}).Where("status =1 AND user_org_id = ? AND patient_id = ? AND assessment_date = ?", org_id, patient_id, date).Updates(map[string]interface{}{"actual_treatment_hour": actual_treatment_hour, "actual_treatment_minute": actual_treatment_minute}).Error
  882. return
  883. }
  884. func CreateDialysisBeforePrepare(beforePrepares []*models.DialysisBeforePrepare) (err error) {
  885. if len(beforePrepares) > 0 {
  886. utx := writeDb.Begin()
  887. if len(beforePrepares) > 0 {
  888. thisSQL := "INSERT INTO dialysis_before_prepare (user_org_id, patient_id, record_date, good_id, good_type_id,count,ctime,mtime,creater,modifier,status) VALUES "
  889. insertParams := make([]string, 0)
  890. insertData := make([]interface{}, 0)
  891. for _, prepare := range beforePrepares {
  892. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?)")
  893. insertData = append(insertData, prepare.UserOrgId)
  894. insertData = append(insertData, prepare.PatientId)
  895. insertData = append(insertData, prepare.RecordDate)
  896. insertData = append(insertData, prepare.GoodId)
  897. insertData = append(insertData, prepare.GoodTypeId)
  898. insertData = append(insertData, prepare.Count)
  899. insertData = append(insertData, prepare.Ctime)
  900. insertData = append(insertData, prepare.Mtime)
  901. insertData = append(insertData, prepare.Creater)
  902. insertData = append(insertData, prepare.Modifier)
  903. insertData = append(insertData, 1)
  904. }
  905. thisSQL += strings.Join(insertParams, ", ")
  906. err = utx.Exec(thisSQL, insertData...).Error
  907. if err != nil {
  908. utx.Rollback()
  909. return
  910. }
  911. }
  912. utx.Commit()
  913. }
  914. return
  915. }
  916. func UpdateDialysisBeforePrepare(info *models.DialysisBeforePrepare) (err error) {
  917. err = writeDb.Save(&info).Error
  918. return err
  919. }
  920. func UpdateOrderCountSub(good_type_id int64, good_id int64, number string, org_id int64, count int64) {
  921. writeDb.Model(&models.WarehouseOutInfo{}).Where("good_id = ? AND good_type_id = ? AND org_id = ? AND status = 1 AND is_sys = 1 AND warehouse_out_order_number = ? ", good_id, good_type_id, org_id, number).UpdateColumn("count", gorm.Expr("count - ?", count))
  922. }
  923. func UpdateOrderCountAdd(good_type_id int64, good_id int64, number string, org_id int64, count int64) {
  924. writeDb.Model(&models.WarehouseOutInfo{}).Where("good_id = ? AND good_type_id = ? AND org_id = ? AND status = 1 AND is_sys = 1 AND warehouse_out_order_number = ? ", good_id, good_type_id, org_id, number).UpdateColumn("count", gorm.Expr("count + ?", count))
  925. }
  926. func UpdateUserInfoDetails(good_type_id int64, good_id int64, time int64, org_id int64, patient_id int64, count int64, out *models.WarehouseOutInfo) {
  927. writeDb.Model(&models.AutomaticReduceDetail{}).Where("good_id = ? AND good_type_id = ? AND org_id = ? AND status = 1 AND warehouse_out_order_number = ? AND patient_id = ?", good_id, good_type_id, org_id, out.WarehouseOutOrderNumber, patient_id).Updates(map[string]interface{}{"count": count})
  928. //writeDb.Model(&models.AutomaticReduceDetail{}).Where("good_id = ? AND good_type_id = ? AND org_id = ? AND status = 1 AND is_sys = 1 AND warehouse_out_order_number = ? ", good_id, good_type_id, org_id, out.WarehouseOutOrderNumber).UpdateColumn("count", count)
  929. }