dialysis_service.go 50KB

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