dialysis_service.go 80KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726
  1. package service
  2. import (
  3. "XT_New/models"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/jinzhu/gorm"
  7. "strconv"
  8. "strings"
  9. "time"
  10. )
  11. //GetPatientList 返回患者的列表
  12. func GetSchedualPatientList(orgID int64, schedulDate int64, schedulType int64, partitionType int64, keywords string, page int64, limit int64) (schedule []*models.DialysisSchedule, err error) {
  13. var total int64
  14. //if len(keywords) > 0 {
  15. // likeKey := "%" + keywords + "%"
  16. // //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)
  17. // db = readDb.
  18. // Model(&models.MonitorDialysisSchedule{}).
  19. // Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  20. // Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  21. // Preload("TreatmentMode", "status = 1").
  22. // Preload("Prescription", "status = 1 AND user_org_id = ?", orgID).
  23. // Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ?", orgID).
  24. // Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ?", orgID).
  25. // Preload("MonitoringRecord", "status = 1 AND user_org_id = ?", orgID).
  26. // Preload("DialysisOrder", "status = 1 AND user_org_id = ?", orgID).
  27. // Preload("MonitorPatients", "status = 1 AND user_org_id = ? AND `name` LIKE ?", orgID,likeKey)
  28. //
  29. //
  30. //} else {
  31. //}
  32. db := readDb.
  33. Model(&models.MonitorDialysisSchedule{}).
  34. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  35. // Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  36. Preload("TreatmentMode", "status = 1").
  37. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, schedulDate).
  38. Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ? AND assessment_date = ?", orgID, schedulDate).
  39. Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ? AND assessment_date = ?", orgID, schedulDate).
  40. Preload("MonitoringRecord", "status = 1 AND user_org_id = ? AND monitoring_date = ?", orgID, schedulDate).
  41. Preload("DialysisOrder", "status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, schedulDate).
  42. Preload("MonitorPatients", "status = 1 AND user_org_id = ?", orgID)
  43. db = db.Where("status = 1")
  44. if schedulDate > 0 {
  45. db = db.Where("schedule_date = ?", schedulDate)
  46. }
  47. db = db.Count(&total)
  48. fmt.Println(total)
  49. if schedulType > 0 {
  50. db = db.Where("schedule_type = ?", schedulType)
  51. }
  52. if partitionType > 0 {
  53. db = db.Where("partition_id = ?", partitionType)
  54. }
  55. offset := (page - 1) * limit
  56. errors := db.Offset(offset).Limit(limit).Find(&schedule).Error
  57. return schedule, errors
  58. }
  59. //用户基本信息
  60. func FindPatientInfoById(orgID int64, id int64, time int64) (patient models.SchedualPatient2, err error) {
  61. err = readDb.Preload("DialysisSchedule", func(db *gorm.DB) *gorm.DB {
  62. 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)
  63. }).Where("id = ? and user_org_id=? and status=1", id, orgID).First(&patient).Error
  64. return
  65. }
  66. //透析处方
  67. func FindPatientPrescribeById(orgID int64, patientsId int64, recordDate int64) (patient models.DialysisPrescription, err error) {
  68. err = readDb.Model(&models.DialysisPrescription{}).Where("patient_id = ? and user_org_id=? and status=1 and record_date = ? ", patientsId, orgID, recordDate).First(&patient).Error
  69. return
  70. }
  71. //透析方案中的透析处方
  72. func FindDialysisSolution(orgID int64, patientsId int64) (patient models.DialysisSolution, err error) {
  73. err = readDb.Model(&models.DialysisSolution{}).Where("patient_id = ? and user_org_id=? and status=1", patientsId, orgID).Last(&patient).Error
  74. return
  75. }
  76. //透析方案中的透析模式
  77. func FindTreatmentModeById(modeId int64) (mode models.TreatmentMode, err error) {
  78. err = readDb.Model(&models.TreatmentMode{}).Where("id = ? AND status=1", modeId).First(&mode).Error
  79. return
  80. }
  81. //接诊评估
  82. func FindReceiverTreatmentAccessRecordById(orgID int64, patientsId int64, recordDate int64) (receiveTreatmentAsses models.ReceiveTreatmentAsses, err error) {
  83. err = readDb.Model(&models.ReceiveTreatmentAsses{}).Where("patient_id = ? and user_org_id=? and status=1 and record_date = ?", patientsId, orgID, recordDate).First(&receiveTreatmentAsses).Error
  84. return
  85. }
  86. //透前评估
  87. func FindPredialysisEvaluationById(orgID int64, patientsId int64, recordDate int64) (patient models.PredialysisEvaluation, err error) {
  88. err = readDb.Model(&models.PredialysisEvaluation{}).Where("patient_id = ? and user_org_id=? and status=1 and assessment_date = ?", patientsId, orgID, recordDate).First(&patient).Error
  89. return
  90. }
  91. func FindPredialysisName(id int64, orgid int64) (models.Dataconfig, error) {
  92. dataconfig := models.Dataconfig{}
  93. err := readDb.Where("value = ? and org_id =? and status = 1", id, orgid).Find(&dataconfig).Error
  94. return dataconfig, err
  95. }
  96. //临时医嘱
  97. func FindDoctorAdviceById(orgID int64, patientsId int64, recordDate int64) (patient []*models.DoctorAdvice, err error) {
  98. 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
  99. return
  100. }
  101. func FindDoctorAdviceByInfo(orgID int64, patientsId int64, recordDate int64) (advices []*models.XtDoctorAdvice, err error) {
  102. //db := readDb.Table("xt_doctor_advice as a").Where("a.status = ?", 1)
  103. //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
  104. //return
  105. err = readDb.Model(&models.DoctorAdvice{}).
  106. Where("patient_id = ? and user_org_id=? and status=1 and record_date = ? and advice_type = 2", patientsId, orgID, recordDate).
  107. 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").
  108. Find(&advices).Error
  109. return
  110. }
  111. func GetTotalDialysisCout(orgid int64, patientid int64) (order []*models.DialysisOrder, total int64, err error) {
  112. err = readDb.Model(&order).Where("user_org_id = ? and patient_id = ? and status = 1", orgid, patientid).Find(&order).Count(&total).Error
  113. return order, total, err
  114. }
  115. //临时医嘱
  116. func FindDoctorAdviceOrderById(orgID int64, patientsId int64, recordDate int64) (patient []*models.DoctorAdvice, err error) {
  117. err = readDb.Model(&models.DoctorAdvice{}).
  118. Where("patient_id = ? and user_org_id=? and status=1 and record_date = ? and advice_type = 2", patientsId, orgID, recordDate).
  119. 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").
  120. Find(&patient).Error
  121. return
  122. }
  123. //双人核对
  124. func FindDoubleCheckById(orgID int64, patientsId int64, recordDate int64) (patient models.DoubleCheck, err error) {
  125. err = readDb.Model(&models.DoubleCheck{}).Where("patient_id = ? and user_org_id=? and status=1 and check_date = ? ", patientsId, orgID, recordDate).First(&patient).Error
  126. return
  127. }
  128. //透析监测
  129. func FindAllMonitorRecord(orgID int64, patientsId int64, recordDate int64) (record []*models.MonitoringRecord, err error) {
  130. 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
  131. return
  132. }
  133. func FindAllMonitor(orgID int64, patientsId int64, recordDate int64) (record []*models.MonitoringRecord, err error) {
  134. 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
  135. return
  136. }
  137. //透后评估
  138. func FindAssessmentAfterDislysisById(orgID int64, patientsId int64, recordDate int64) (patient models.AssessmentAfterDislysis, err error) {
  139. err = readDb.Model(&models.AssessmentAfterDislysis{}).Where("patient_id = ? and user_org_id=? and status=1 and assessment_date =?", patientsId, orgID, recordDate).First(&patient).Error
  140. return
  141. }
  142. //治疗小结
  143. func FindTreatmentSummaryById(orgID int64, patientsId int64, recordDate int64) (patient models.TreatmentSummary, err error) {
  144. err = readDb.Model(&models.TreatmentSummary{}).Where("patient_id = ? and user_org_id=? and status=1 and assessment_date = ? ", patientsId, orgID, recordDate).First(&patient).Error
  145. return
  146. }
  147. //机构叫号配置
  148. func FindQueueCallInfo(orgID int64) (queueConfig models.QueueCallConfig, err error) {
  149. err = readDb.Model(&models.QueueCallConfig{}).Where("org_id=? ", orgID).First(&queueConfig).Error
  150. return
  151. }
  152. //修改机构叫号配置
  153. func UpDateQueueCallInfo(queueConfig models.QueueCallConfig) error {
  154. err := writeDb.Save(&queueConfig).Error
  155. return err
  156. }
  157. //所有设备信息
  158. func FindAllDeviceInfo(orgID int64) (device []*models.Device, err error) {
  159. err = readDb.Model(&models.Device{}).Where("org_id=? and status=1 and device_type = 1", orgID).Find(&device).Error
  160. return device, err
  161. }
  162. //所有设备区域
  163. func GetAllDeviceZone(orgId int64) (error, []*models.DeviceZone) {
  164. var zone []*models.DeviceZone
  165. err := readDb.Model(&zone).Where("org_id = ? AND status = 1", orgId).Find(&zone).Error
  166. return err, zone
  167. }
  168. //获取所有子药物
  169. func FindAllSubDoctorAdvice(id int64, parentId int64) (error, []models.DoctorAdvice) {
  170. var subAdvies []models.DoctorAdvice
  171. err := readDb.Model(&subAdvies).Where("patient_id = ? AND parent_id = ?", id, parentId).Find(&subAdvies).Error
  172. return err, subAdvies
  173. }
  174. func FindAdivceById(orgId int64, patientId int64, id int64) (error, models.DoctorAdvice) {
  175. advices := models.DoctorAdvice{}
  176. err := readDb.Model(&advices).Where("user_org_id = ? AND patient_id = ? AND id = ? AND status = 1", orgId, patientId, id).Find(&advices).Error
  177. return err, advices
  178. }
  179. func FindSubAdivceById(patientId int64, parentId int64) []*models.DoctorAdvice {
  180. var subAdvices []*models.DoctorAdvice
  181. readDb.Model(&subAdvices).Where("patient_id = ? AND parent_id = ?", patientId, parentId).Find(&subAdvices)
  182. return subAdvices
  183. }
  184. func AddSigleRecord(prescription *models.DialysisPrescription) (err error) {
  185. tx := writeDb.Begin()
  186. errors := tx.Model(&models.DialysisPrescription{}).Create(&prescription).Error
  187. if err != nil {
  188. tx.Rollback()
  189. }
  190. tx.Commit()
  191. return errors
  192. }
  193. func AddSigleSummaryRecord(summary *models.TreatmentSummary) (err error) {
  194. tx := writeDb.Begin()
  195. errors := tx.Model(&models.TreatmentSummary{}).Create(&summary).Error
  196. if err != nil {
  197. tx.Rollback()
  198. }
  199. tx.Commit()
  200. return errors
  201. }
  202. func AddSigleReceiveTreatmentAssesRecord(receiveTreatmentAsses *models.ReceiveTreatmentAsses) (err error) {
  203. tx := writeDb.Begin()
  204. errors := tx.Model(&models.ReceiveTreatmentAsses{}).Create(&receiveTreatmentAsses).Error
  205. if err != nil {
  206. tx.Rollback()
  207. }
  208. tx.Commit()
  209. return errors
  210. }
  211. func AddSigleAssessmentAfterDislysisRecord(assessmentAfterDislysis *models.AssessmentAfterDislysis) (err error) {
  212. tx := writeDb.Begin()
  213. errors := tx.Model(&models.AssessmentAfterDislysis{}).Create(&assessmentAfterDislysis).Error
  214. if err != nil {
  215. tx.Rollback()
  216. }
  217. tx.Commit()
  218. return errors
  219. }
  220. func AddSigleAssessmentBeforeDislysisRecord(assessmentBeforeDislysis *models.PredialysisEvaluation) (err error) {
  221. tx := writeDb.Begin()
  222. errors := tx.Model(&models.PredialysisEvaluation{}).Create(&assessmentBeforeDislysis).Error
  223. if err != nil {
  224. tx.Rollback()
  225. }
  226. tx.Commit()
  227. return errors
  228. }
  229. func AddSigleDoubleCheck(check *models.DoubleCheck) (err error) {
  230. tx := writeDb.Begin()
  231. errors := tx.Model(&models.PredialysisEvaluation{}).Create(&check).Error
  232. if err != nil {
  233. tx.Rollback()
  234. }
  235. tx.Commit()
  236. return errors
  237. }
  238. //修改透析处方
  239. func UpDateDialysisPrescription(dialysisPrescription *models.DialysisPrescription) error {
  240. tx := writeDb.Begin()
  241. err := writeDb.Save(&dialysisPrescription).Error
  242. if err != nil {
  243. tx.Rollback()
  244. }
  245. tx.Commit()
  246. return err
  247. }
  248. //修改接诊评估
  249. func UpadateReceiveTreatmentAsses(receiveTreatmentAsses *models.ReceiveTreatmentAsses) error {
  250. err := writeDb.Save(receiveTreatmentAsses).Error
  251. return err
  252. }
  253. //修改透前评估
  254. func UpadatePredialysisEvaluation(predialysisEvaluation *models.PredialysisEvaluation) error {
  255. err := writeDb.Save(&predialysisEvaluation).Error
  256. return err
  257. }
  258. //修改双人核对
  259. func UpdateDoubleCheck(check *models.DoubleCheck) error {
  260. err := writeDb.Save(&check).Error
  261. return err
  262. }
  263. //修改透后评估
  264. func UpdateAssessmentAfterDislysisRecord(assessmentAfterDislysis *models.AssessmentAfterDislysis) error {
  265. err := writeDb.Save(&assessmentAfterDislysis).Error
  266. return err
  267. }
  268. func UpdatePredialysisEvaluation(predialysisevaluation *models.PredialysisEvaluation, id int64) error {
  269. err := writeDb.Model(&predialysisevaluation).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"systolic_blood_pressure": predialysisevaluation.SystolicBloodPressure}).Error
  270. return err
  271. }
  272. func UpdatePredialysisEvaluationTwo(predialysisevaluation *models.PredialysisEvaluation, id int64) error {
  273. err := writeDb.Model(&predialysisevaluation).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"diastolic_blood_pressure": predialysisevaluation.DiastolicBloodPressure}).Error
  274. return err
  275. }
  276. func UpdatePredialysisEvaluationThree(predialysisevaluation *models.PredialysisEvaluation, id int64) error {
  277. err := writeDb.Model(&predialysisevaluation).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"pulse_frequency": predialysisevaluation.PulseFrequency}).Error
  278. return err
  279. }
  280. //修改
  281. func UpdateSummeRecord(treatmentSummary *models.TreatmentSummary) error {
  282. err := writeDb.Save(&treatmentSummary).Error
  283. return err
  284. }
  285. // func UpdateDoctorAdviceById(m *models.DoctorAdvice, id int64, patientId int64) error {
  286. // tx := writeDb.Begin()
  287. // 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
  288. // if err != nil {
  289. // tx.Rollback()
  290. // }
  291. // tx.Commit()
  292. // return err
  293. // }
  294. func ExceDoctorAdviceById(m *models.DoctorAdvice, id int64, patientId int64) error {
  295. 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
  296. return err
  297. }
  298. func ExceHidDoctorAdviceById(m *models.HisDoctorAdviceInfo, id int64, patientId int64) error {
  299. err := writeDb.Model(&models.HisDoctorAdviceInfo{}).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
  300. return err
  301. }
  302. func CheckDoctorAdviceById(m *models.DoctorAdvice, id int64, patientId int64) error {
  303. 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
  304. return err
  305. }
  306. func CheckHisDoctorAdviceById(m *models.HisDoctorAdviceInfo, id int64, patientId int64) error {
  307. err := writeDb.Model(&models.HisDoctorAdviceInfo{}).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
  308. return err
  309. }
  310. func CreateMonitor(m *models.MonitoringRecord) (err error) {
  311. return writeDb.Create(m).Error
  312. }
  313. func UpdateMonitor(m *models.MonitoringRecord) (err error) {
  314. return writeDb.Save(m).Error
  315. }
  316. func DelMonitor(patientId int64, recordId int64) error {
  317. tx := writeDb.Begin()
  318. updateTime := time.Now().Unix()
  319. 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
  320. if err != nil {
  321. tx.Rollback()
  322. }
  323. tx.Commit()
  324. return err
  325. }
  326. //修改长期处方
  327. func UpdateSolution(solution *models.DialysisSolution) {
  328. writeDb.Save(&solution)
  329. }
  330. //创建长期处方
  331. func CreateSolution(solution *models.DialysisSolution) {
  332. writeDb.Create(&solution)
  333. }
  334. func SavePrescriptionAndCreateSolution(solution *models.DialysisSolution, prescription *models.DialysisPrescription) (err error) {
  335. tx := writeDb.Begin()
  336. if prescription.ID > 0 {
  337. err = tx.Save(&prescription).Error
  338. } else {
  339. err = tx.Model(&models.DialysisPrescription{}).Create(&prescription).Error
  340. }
  341. if err != nil {
  342. tx.Rollback()
  343. return
  344. }
  345. err = tx.Create(&solution).Error
  346. if err != nil {
  347. tx.Rollback()
  348. return
  349. }
  350. tx.Commit()
  351. return
  352. }
  353. //透析处方
  354. func FindDialysisPrescriptionByReordDate(id int64, recordDate int64, orgId int64) (error, *models.DialysisPrescription) {
  355. dialysisPrescription := models.DialysisPrescription{}
  356. err := readDb.Model(&dialysisPrescription).Where("patient_id = ? AND record_date = ? AND status = 1 AND user_org_id = ? ", id, recordDate, orgId).Find(&dialysisPrescription).Error
  357. return err, &dialysisPrescription
  358. }
  359. func FindReceiveTreatmentAssesByReordDate(id int64, recordDate int64, orgId int64) (error, *models.ReceiveTreatmentAsses) {
  360. receiveTreatmentAsses := models.ReceiveTreatmentAsses{}
  361. err := readDb.Model(&receiveTreatmentAsses).Where("patient_id = ? AND record_date = ? AND status = 1 AND user_org_id = ?", id, recordDate, orgId).Find(&receiveTreatmentAsses).Error
  362. return err, &receiveTreatmentAsses
  363. }
  364. //透前评估
  365. func FindPredialysisEvaluationByReordDate(id int64, recordDate int64, orgId int64) (error, *models.PredialysisEvaluation) {
  366. dialysisEvaluation := models.PredialysisEvaluation{}
  367. err := readDb.Model(&models.PredialysisEvaluation{}).Where("patient_id = ? AND assessment_date = ? AND status = 1 AND user_org_id = ?", id, recordDate, orgId).First(&dialysisEvaluation).Error
  368. return err, &dialysisEvaluation
  369. }
  370. //双人核对
  371. func FindDoubleCheckByReordDate(id int64, recordDate int64, orgId int64) (error, *models.DoubleCheck) {
  372. check := models.DoubleCheck{}
  373. err := readDb.Model(&models.DoubleCheck{}).Where("patient_id = ? AND check_date = ? AND status = 1 AND user_org_id = ?", id, recordDate, orgId).First(&check).Error
  374. return err, &check
  375. }
  376. func FindAssessmentAfterDislysisByReordDate(id int64, recordDate int64, orgId int64) (error, *models.AssessmentAfterDislysis) {
  377. assessmentAfterDislysis := models.AssessmentAfterDislysis{}
  378. err := readDb.Model(&models.AssessmentAfterDislysis{}).Where("patient_id = ? AND assessment_date = ? AND status = 1 AND user_org_id = ?", id, recordDate, orgId).First(&assessmentAfterDislysis).Error
  379. return err, &assessmentAfterDislysis
  380. }
  381. func FindTreatmentSummaryByReordDate(id int64, recordDate int64, orgId int64) (error, *models.TreatmentSummary) {
  382. summary := models.TreatmentSummary{}
  383. err := readDb.Model(&models.TreatmentSummary{}).Where("patient_id = ? AND assessment_date = ? AND status = 1 AND user_org_id = ?", id, recordDate, orgId).First(&summary).Error
  384. return err, &summary
  385. }
  386. func FindDoctorAdviceByReordDate(id int64, recordDate int64, orgId int64) (error, *models.DoctorAdvice) {
  387. advice := models.DoctorAdvice{}
  388. 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
  389. return err, &advice
  390. }
  391. func FindDialysisRecordById(orgId int64, id int64, recordDate int64) (error, *models.DialysisOrder) {
  392. var count int64
  393. dialysisRecord := models.DialysisOrder{}
  394. err := readDb.Model(&models.DialysisOrder{}).Where("user_org_id = ? AND patient_id = ? AND dialysis_date = ?", orgId, id, recordDate).Find(&dialysisRecord).Count(&count).Error
  395. if count <= 0 {
  396. return err, nil
  397. }
  398. return err, &dialysisRecord
  399. }
  400. func FindDialysisSchedualById(orgId int64, id int64, recordDate int64) (error, *models.Schedule) {
  401. var count int64
  402. schedule := models.Schedule{}
  403. err := readDb.Model(&models.Schedule{}).Where("user_org_id = ? AND patient_id = ? AND schedule_date = ?", orgId, id, recordDate).Find(&schedule).Count(&count).Error
  404. if count <= 0 {
  405. return err, nil
  406. }
  407. return err, &schedule
  408. }
  409. func CreateDialysisRecord(id int64, orgId int64, record *models.DialysisOrder) error {
  410. err := writeDb.Model(&models.DialysisOrder{}).Where("user_org_id = ? AND patient_id = ? ", orgId, id).Create(&record).Error
  411. if err != nil {
  412. return err
  413. }
  414. dialysisPrescriptionErr := UpDateRecordIdToDialysisPrescription(id, orgId, record.DialysisDate, record.ID)
  415. if dialysisPrescriptionErr != nil {
  416. return dialysisPrescriptionErr
  417. }
  418. receiveTreatmentAssesErr := UpDateRecordIdToReceiveTreatmentAsses(id, orgId, record.DialysisDate, record.ID)
  419. if receiveTreatmentAssesErr != nil {
  420. return receiveTreatmentAssesErr
  421. }
  422. predialysisEvaluationErr := UpDateRecordIdToPredialysisEvaluation(id, orgId, record.DialysisDate, record.ID)
  423. if predialysisEvaluationErr != nil {
  424. return predialysisEvaluationErr
  425. }
  426. treatmentSummaryErr := UpDateRecordIdToTreatmentSummary(id, orgId, record.DialysisDate, record.ID)
  427. if treatmentSummaryErr != nil {
  428. return treatmentSummaryErr
  429. }
  430. doubleCheckErr := UpDateRecordIdToDoubleCheck(id, orgId, record.DialysisDate, record.ID)
  431. if doubleCheckErr != nil {
  432. return doubleCheckErr
  433. }
  434. adviceErrr := UpDateRecordIdToDoctorAdvice(id, orgId, record.DialysisDate, record.ID)
  435. if adviceErrr != nil {
  436. return adviceErrr
  437. }
  438. //透析监测
  439. records, err := FindAllMonitorRecord(orgId, id, record.DialysisDate)
  440. if err != nil {
  441. return err
  442. }
  443. var Ids []int64
  444. for _, item := range records {
  445. Ids = append(Ids, item.ID)
  446. }
  447. monitorErr := UpDateRecordIdToMonitor(Ids, record.ID)
  448. if monitorErr != nil {
  449. return monitorErr
  450. }
  451. return nil
  452. }
  453. func ModifyDialysisRecord(id int64, finishNurse int64, end_time int64, admin_user_id int64) error {
  454. 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
  455. return err
  456. }
  457. func UpdateSolutionByPatientId(id int64) error {
  458. err := writeDb.Model(&models.Patients{}).Where("id = ? AND status = 1 ", id).UpdateColumn("total_dialysis", gorm.Expr("total_dialysis + ?", 1)).Error
  459. return err
  460. }
  461. //更新透析处方记录ID
  462. func UpDateRecordIdToDialysisPrescription(patientId int64, orgId int64, recordTime int64, recordId int64) error {
  463. 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
  464. return errors
  465. }
  466. func UpDateRecordIdToReceiveTreatmentAsses(patientId int64, orgId int64, recordTime int64, recordId int64) error {
  467. 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
  468. return errors
  469. }
  470. func UpDateRecordIdToPredialysisEvaluation(patientId int64, orgId int64, recordTime int64, recordId int64) error {
  471. 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
  472. return errors
  473. }
  474. func UpDateRecordIdToAssessmentAfterDislysis(patientId int64, orgId int64, recordTime int64, recordId int64) error {
  475. 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
  476. return errors
  477. }
  478. func UpDateRecordIdToTreatmentSummary(patientId int64, orgId int64, recordTime int64, recordId int64) error {
  479. 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
  480. return errors
  481. }
  482. func UpDateRecordIdToMonitor(Ids []int64, recordId int64) error {
  483. errors := writeDb.Model(&models.MonitoringRecord{}).Where("id IN (?)", Ids).Update(map[string]interface{}{"dialysis_order_id": recordId}).Error
  484. return errors
  485. }
  486. func UpDateRecordIdToDoubleCheck(patientId int64, orgId int64, recordTime int64, recordId int64) error {
  487. 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
  488. return errors
  489. }
  490. func UpDateRecordIdToDoctorAdvice(patientId int64, orgId int64, recordTime int64, recordId int64) error {
  491. 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
  492. return errors
  493. }
  494. //透析处方
  495. func FindPatientPrescribeWidyDevideById(orgID int64, patientsId int64, recordDate int64) (patient models.DialysisPrescriptionAddtional, err error) {
  496. err = readDb.Preload("Device", "status = 1 AND org_id = ?", orgID).
  497. Model(&models.DialysisPrescriptionAddtional{}).
  498. Where("patient_id = ? and user_org_id=? and status=1 and record_date = ? ", patientsId, orgID, recordDate).
  499. First(&patient).Error
  500. return
  501. }
  502. func FindDialysisMode(orgID int64, patientId int64, scheduleDate int64) (models.XtSchedule, error) {
  503. schedule := models.XtSchedule{}
  504. err = readDb.Where("user_org_id = ? and patient_id = ? and schedule_date = ? ", orgID, patientId, scheduleDate).Find(&schedule).Error
  505. return schedule, err
  506. }
  507. func FindDialysisMacher(orgid int64, patientid int64, dialysisDate int64) (models.XtDialysisOrders, error) {
  508. orders := models.XtDialysisOrders{}
  509. db := readDb.Table("xt_dialysis_order as o").Where("o.status = 1")
  510. table := readDb.Table("xt_device_number as n")
  511. fmt.Print("table", table)
  512. 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
  513. return orders, err
  514. }
  515. func FindPunctureNurse(adminid int64, appid int64, orgid int64) (models.App_Roles, error) {
  516. role := models.App_Roles{}
  517. //err := UserReadDB().Where("admin_user_id = ? and app_id = ? and org_id = ?", adminid, appid, orgid).Find(&role).Error
  518. //return role, err
  519. db := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status =1")
  520. 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
  521. return role, err
  522. }
  523. func FindeStartNuse(adminid int64, appid int64, orgid int64) (models.App_Roles, error) {
  524. role := models.App_Roles{}
  525. db := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  526. 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
  527. //err := UserReadDB().Where("admin_user_id = ? and app_id = ? and org_id = ?", adminid, appid, orgid).Find(&role).Error
  528. return role, err
  529. }
  530. func FindDialysisWay(orgid int64, patientid int64, recorddate int64) (models.DialysisPrescription, error) {
  531. prescription := models.DialysisPrescription{}
  532. err := readDb.Where("user_org_id = ? and patient_id = ? and record_date = ? and status =1", orgid, patientid, recorddate).Find(&prescription).Error
  533. return prescription, err
  534. }
  535. func FindDoctorOrder(orgid int64, patientid int64, adviceDate int64) (models.XtDoctorAdvice, error) {
  536. advices := models.XtDoctorAdvice{}
  537. db := readDb.Table("xt_doctor_advice as a").Where("a.status = ?", 1)
  538. 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
  539. return advices, err
  540. }
  541. func FindDoctor(adminuserid int64, appid int64, orgid int64) (models.App_Role, error) {
  542. role := models.App_Role{}
  543. err := UserReadDB().Where("admin_user_id = ? and app_id = ? and org_id = ?", adminuserid, appid, orgid).Find(&role).Error
  544. return role, err
  545. }
  546. func FindDoctorName(adminid int64, appid int64, orgid int64) (models.App_Roles, error) {
  547. role := models.App_Roles{}
  548. db := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  549. 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
  550. //err := UserReadDB().Where("admin_user_id = ? and app_id = ? and org_id = ?", adminid, appid, orgid).Find(&role).Error
  551. return role, err
  552. }
  553. /////////////// 新 18-10-22
  554. func GetDialysisScheduals(orgID int64, scheduleDate int64) ([]*MDialysisScheduleVM, error) {
  555. var vms []*MDialysisScheduleVM
  556. db := readDb.
  557. Table("xt_schedule").
  558. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  559. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  560. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  561. Preload("DialysisOrder", "status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, scheduleDate).
  562. Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ? AND assessment_date = ?", orgID, scheduleDate).
  563. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  564. Preload("TreatmentSummary", "status = 1 AND user_org_id = ? AND assessment_date = ? ", orgID, scheduleDate).
  565. Preload("Advices", "status = 1 AND user_org_id = ? AND advice_date = ? ", orgID, scheduleDate).
  566. Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ? and assessment_date = ?", orgID, scheduleDate).
  567. Where("status = 1 AND user_org_id = ?", orgID)
  568. if scheduleDate != 0 {
  569. db = db.Where("schedule_date = ?", scheduleDate)
  570. }
  571. err := db.Find(&vms).Error
  572. return vms, err
  573. }
  574. func GetDialysisSchedualsOne(orgID int64, scheduleDate int64) ([]*MDialysisScheduleVM, error) {
  575. var vms []*MDialysisScheduleVM
  576. db := readDb.
  577. Table("xt_schedule").
  578. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  579. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  580. //Preload("DialysisOrder", "status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, scheduleDate).
  581. //Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ? AND assessment_date = ?", orgID, scheduleDate).
  582. //Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  583. //Preload("TreatmentSummary", "status = 1 AND user_org_id = ? AND assessment_date = ? ", orgID, scheduleDate).
  584. //Preload("Advices", "status = 1 AND user_org_id = ? AND advice_date = ? ", orgID, scheduleDate).
  585. //Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ? and assessment_date = ?", orgID, scheduleDate).
  586. Where("status = 1 AND user_org_id = ?", orgID)
  587. if scheduleDate != 0 {
  588. db = db.Where("schedule_date = ?", scheduleDate)
  589. }
  590. err := db.Find(&vms).Error
  591. return vms, err
  592. }
  593. func GetLastAcceptsAssessment(patient int64, orgId int64) (error, models.ReceiveTreatmentAsses) {
  594. var receiveTreatmentAsses models.ReceiveTreatmentAsses
  595. err := readDb.Model(&models.ReceiveTreatmentAsses{}).Where("patient_id = ? AND status = 1 AND user_org_id = ?", patient, orgId).Last(&receiveTreatmentAsses).Error
  596. return err, receiveTreatmentAsses
  597. }
  598. func GetDialysisWatch(orgID int64, schedulDate int64, schedulType int64, partitionType int64, page int64, limit int64, start int64, end int64) (schedule []*models.DialysisSchedule, err error, total int64) {
  599. db := readDb.Model(&models.MonitorDialysisSchedule{})
  600. if start > 0 && end > 0 {
  601. db = db.Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  602. Preload("TreatmentMode", "status = 1").
  603. Preload("Prescription", "status = 1 AND user_org_id = ? and record_date>=? and record_date <=?", orgID, start, end).
  604. Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ? and assessment_date >=? and assessment_date <=?", orgID, start, end).
  605. Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ? and assessment_date >=? and assessment_date <=?", orgID, start, end).
  606. Preload("MonitoringRecord", func(db *gorm.DB) *gorm.DB {
  607. return db.Where("status = 1 AND user_org_id = ? and monitoring_date >=? and monitoring_date<=? ", orgID, start, end).Order("operate_time asc")
  608. }).
  609. Preload("DialysisOrder", "status = 1 AND user_org_id = ? and dialysis_date>=? and dialysis_date<=?", orgID, start, end).
  610. Preload("MonitorPatients", "status = 1 AND user_org_id = ?", orgID)
  611. db = db.Where("xt_schedule.status = 1 AND user_org_id = ?", orgID)
  612. }
  613. if schedulDate > 0 {
  614. db = db.Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  615. Preload("TreatmentMode", "status = 1").
  616. Preload("Prescription", "status = 1 AND user_org_id = ? and record_date>=? and record_date <=?", orgID, schedulDate, schedulDate).
  617. Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ? and assessment_date >=? and assessment_date <=?", orgID, schedulDate, schedulDate).
  618. Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ? and assessment_date >=? and assessment_date <=?", orgID, schedulDate, schedulDate).
  619. Preload("MonitoringRecord", func(db *gorm.DB) *gorm.DB {
  620. return db.Where("status = 1 AND user_org_id = ? and monitoring_date >=? and monitoring_date<=? ", orgID, schedulDate, schedulDate).Order("operate_time asc")
  621. }).
  622. Preload("DialysisOrder", "status = 1 AND user_org_id = ? and dialysis_date>=? and dialysis_date<=?", orgID, schedulDate, schedulDate).
  623. Preload("MonitorPatients", "status = 1 AND user_org_id = ?", orgID)
  624. db = db.Where("xt_schedule.status = 1 AND user_org_id = ?", orgID)
  625. db = db.Where("schedule_date = ?", schedulDate)
  626. }
  627. if schedulType > 0 {
  628. db = db.Where("schedule_type = ?", schedulType)
  629. }
  630. if start > 0 {
  631. db = db.Where("schedule_date >= ?", start)
  632. }
  633. if end > 0 {
  634. db = db.Where("schedule_date <= ?", end)
  635. }
  636. if partitionType > 0 {
  637. 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)
  638. // db = db.Where("partition_id = ?", partitionType)
  639. }
  640. offset := (page - 1) * limit
  641. err = db.Count(&total).Offset(offset).Limit(limit).Order("bed_id desc").Find(&schedule).Error
  642. return schedule, err, total
  643. }
  644. //func GetDialysisWatchByKeyword(orgID int64, keyword string, schedulType int64, partitionType int64, page int64, limit int64) (schedules []*models.DialysisSchedule, err error, total int64) {
  645. // db := readDb.Model(&models.DialysisSchedule{}).Where("status = 1")
  646. //
  647. // if schedulType > 0 {
  648. // db = db.Where("scheudle_type = ?", schedulType)
  649. // }
  650. //
  651. // if partitionType > 0 {
  652. // db = db.Where("partition_id = ?", partitionType)
  653. // }
  654. //
  655. // err = db.Count(&total).Offset(limit * (page - 1)).Limit(limit).Order("schedule_date desc").Find(&schedules).Error
  656. // return schedules, err, total
  657. //}
  658. func GetDialysisWatchByKeyword(orgID int64, keyword string, schedulType int64, partitionType int64, page int64, limit int64, start int64, end int64) ([]*models.DialysisSchedule, error, int64) {
  659. var patients []*models.Patients
  660. 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
  661. if getPatientErr != nil {
  662. return nil, getPatientErr, 0
  663. }
  664. patientIDs := make([]int64, len(patients))
  665. for index, patient := range patients {
  666. patientIDs[index] = patient.ID
  667. }
  668. db := readDb.Model(&models.DialysisSchedule{})
  669. if start > 0 && end > 0 {
  670. db = db.Preload("DeviceNumber", "org_id = ?", orgID).
  671. // Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  672. Preload("TreatmentMode", "status = 1").
  673. Preload("Prescription", "status = 1 AND user_org_id = ? and record_date>=? and record_date<=?", orgID, start, end).
  674. Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ? and assessment_date>=? and assessment_date<=?", orgID, start, end).
  675. Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ? and assessment_date>=? and assessment_date<=?", orgID, start, end).
  676. Preload("MonitoringRecord", "status = 1 AND user_org_id = ? and monitoring_date>=? and monitoring_date<=?", orgID, start, end).
  677. Preload("DialysisOrder", "status = 1 AND user_org_id = ? and dialysis_date >= ? and dialysis_date<=?", orgID, start, end).
  678. Preload("MonitorPatients", "status = 1 AND user_org_id = ?", orgID)
  679. } else {
  680. db = db.Preload("DeviceNumber", "org_id = ?", orgID).
  681. // Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  682. Preload("TreatmentMode", "status = 1").
  683. Preload("Prescription", "status = 1 AND user_org_id = ?", orgID).
  684. Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ?", orgID).
  685. Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ?", orgID).
  686. Preload("MonitoringRecord", "status = 1 AND user_org_id = ?", orgID).
  687. Preload("DialysisOrder", "status = 1 AND user_org_id = ?", orgID).
  688. Preload("MonitorPatients", "status = 1 AND user_org_id = ?", orgID)
  689. }
  690. db = db.Where("xt_schedule.status = 1 AND patient_id in (?)", patientIDs)
  691. if schedulType > 0 {
  692. db = db.Where("schedule_type = ?", schedulType)
  693. }
  694. if start > 0 {
  695. db = db.Where("schedule_date >= ?", start)
  696. }
  697. if end > 0 {
  698. db = db.Where("schedule_date<=?", end)
  699. }
  700. if partitionType > 0 {
  701. 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)
  702. // db = db.Where("partition_id = ?", partitionType)
  703. }
  704. var schedules []*models.DialysisSchedule
  705. total := int64(0)
  706. err := db.Count(&total).Offset(limit * (page - 1)).Limit(limit).Order("schedule_date desc").Find(&schedules).Error
  707. return schedules, err, total
  708. }
  709. func GetAdminUserES(orgID int64, appID int64, ids []int64) (es []*models.AdminUserElectronicSignature, err error) {
  710. err = readUserDb.Model(&models.AdminUserElectronicSignature{}).Where("org_id=? and app_id=? and creator in (?) and status=1", orgID, appID, ids).Find(&es).Error
  711. return
  712. }
  713. //
  714. //func GetAdminUserEsOne(orgid int64) (admin []*models.UserAdminRoles, err error) {
  715. //
  716. // db := readUserDb.Table("sgj_user_admin_role as x").Where("x.status = 1")
  717. // table := readUserDb.Table("sgj_user_admin_electronic_signature as s")
  718. // fmt.Println("table", table)
  719. // 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
  720. // return admin, err
  721. //}
  722. func GetAdminUserEsOne(orgid int64) (admin []*models.UserAdminRoles, err error) {
  723. db := readUserDb.Table("sgj_user_admin_role as x").Where("x.status =1")
  724. table := readUserDb.Table("sgj_user_admin_electronic_signature as s")
  725. fmt.Println("table", table)
  726. 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
  727. return admin, err
  728. }
  729. func UpDateDialysisPrescriptionDoctorSign(patient int64, date int64, orgId int64, doctor_id int64) (err error) {
  730. 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
  731. return
  732. }
  733. func GetAllAdminUserES(orgID int64, appId int64) (es []*models.AdminUserElectronicSignature, err error) {
  734. err = readUserDb.Model(&models.AdminUserElectronicSignature{}).Where("org_id=? and app_id = ? and status=1", orgID, appId).Find(&es).Error
  735. return
  736. }
  737. func GetAllStarfEs(orgid int64) (es []*models.SgjUserAdminRoles, err error) {
  738. redis := RedisClient()
  739. defer redis.Close()
  740. // cur_date := time.Now().Format("2006-01-02")
  741. key := strconv.FormatInt(orgid, 10) + ":starfes"
  742. starfes_str, _ := redis.Get(key).Result()
  743. if len(starfes_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  744. db := readUserDb.Table("sgj_user_admin_role as x").Where("x.status = 1")
  745. 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
  746. if err != nil {
  747. if err == gorm.ErrRecordNotFound {
  748. if len(es) <= 0 {
  749. redis.Set(key, "starfes_str_null", time.Second*60*60*18)
  750. }
  751. return nil, nil
  752. } else {
  753. return nil, err
  754. }
  755. } else {
  756. if len(es) > 0 {
  757. //缓存数据
  758. starfes_str, err := json.Marshal(es)
  759. if err == nil {
  760. redis.Set(key, starfes_str, time.Second*60*60*18)
  761. return es, nil
  762. }
  763. } else {
  764. redis.Set(key, "starfes_str_null", time.Second*60*60*18)
  765. return es, nil
  766. }
  767. return es, nil
  768. }
  769. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  770. if starfes_str == "starfes_str_null" {
  771. return es, nil
  772. } else {
  773. json.Unmarshal([]byte(starfes_str), &es)
  774. return es, nil
  775. }
  776. }
  777. }
  778. func FindLastMonitorRecord(patient_id int64, org_id int64) (record models.MonitoringRecord, err error) {
  779. err = readDb.Model(&models.MonitoringRecord{}).Where("patient_id = ? AND status = 1 AND user_org_id = ?", patient_id, org_id).Last(&record).Error
  780. return
  781. }
  782. func FindLastMonitorRecordToday(patient_id int64, org_id int64, monitoring_date int64) (record models.MonitoringRecord, err error) {
  783. 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
  784. return
  785. }
  786. func FindFirstMonitorRecordToday(patient_id int64, org_id int64, monitoring_date int64) (record models.MonitoringRecord, err error) {
  787. 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
  788. return
  789. }
  790. func ExceDoctorAdviceByGroupNo(m *models.DoctorAdvice, groupNo int64, org_id int64) (err error) {
  791. ut := writeDb.Begin()
  792. 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
  793. if err != nil {
  794. ut.Rollback()
  795. return
  796. }
  797. ut.Commit()
  798. return err
  799. }
  800. func ExceOldDoctorAdvice(m *models.DoctorAdvice, advice_id int64) (err error) {
  801. ut := writeDb.Begin()
  802. 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
  803. if err != nil {
  804. ut.Rollback()
  805. return
  806. }
  807. 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
  808. if err != nil {
  809. ut.Rollback()
  810. return
  811. }
  812. ut.Commit()
  813. return err
  814. }
  815. func CheckDoctorAdviceByGroupNo(m *models.DoctorAdvice, groupNo int64, org_id int64) (err error) {
  816. ut := writeDb.Begin()
  817. 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
  818. if err != nil {
  819. ut.Rollback()
  820. return
  821. }
  822. ut.Commit()
  823. return err
  824. }
  825. func CheckOldDoctorAdvice(m *models.DoctorAdvice, advice_id int64) (err error) {
  826. ut := writeDb.Begin()
  827. 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
  828. if err != nil {
  829. ut.Rollback()
  830. return
  831. }
  832. 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
  833. if err != nil {
  834. ut.Rollback()
  835. return
  836. }
  837. ut.Commit()
  838. return err
  839. }
  840. func DeleteDoctorAdviceByGroupNo(m *models.DoctorAdvice) (err error) {
  841. ut := writeDb.Begin()
  842. 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
  843. if err != nil {
  844. ut.Rollback()
  845. return
  846. }
  847. ut.Commit()
  848. return
  849. }
  850. func GetDialysisBoard(orgID int64, scheduleDate int64, offset, limit int64) ([]*models.DialysisBoardVM, error) {
  851. var boards []*models.DialysisBoardVM
  852. db := readDb.
  853. Table("xt_schedule").
  854. Preload("Patient", "status = 1 AND user_org_id = ?", orgID).
  855. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  856. Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  857. Preload("DialysisOrder", "status = 1 AND user_org_id = ?", orgID).
  858. Preload("SigninAndWeigh", "status = 1 AND user_org_id = ?", orgID).
  859. Preload("NextSchedules", func(db *gorm.DB) *gorm.DB {
  860. return db.Where("user_org_id = ? AND schedule_date > ? AND status = 1", orgID, scheduleDate).
  861. Order("schedule_date asc, id asc")
  862. }).
  863. Where("status = 1 AND user_org_id = ?", orgID)
  864. if scheduleDate != 0 {
  865. db = db.Where("schedule_date = ?", scheduleDate)
  866. }
  867. err := db.Offset(offset).Limit(limit).Find(&boards).Error
  868. return boards, err
  869. }
  870. func GetOneDialysisOrder(orgID, orderData int64, patient_id int64) (*models.DialysisOrder, error) {
  871. var order models.DialysisOrder
  872. var err error
  873. 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
  874. if err == gorm.ErrRecordNotFound {
  875. return nil, nil
  876. }
  877. if err != nil {
  878. return nil, err
  879. }
  880. return &order, nil
  881. }
  882. func GetOrgInfoTemplate(orgID int64) (models.GobalTemplate, error) {
  883. var templateInfo models.GobalTemplate
  884. var err error
  885. err = readDb.Model(&models.GobalTemplate{}).Where("org_id=? and status=1", orgID).First(&templateInfo).Error
  886. return templateInfo, err
  887. }
  888. func FindAdminRoleTypeById(orgId int64, adminUserId int64, appId int64) (models.App_Role, error) {
  889. var appRole models.App_Role
  890. var err error
  891. 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
  892. return appRole, err
  893. }
  894. func BatchExceOldDoctorAdvice(m *models.DoctorAdvice, ids []string) (err error) {
  895. ut := writeDb.Begin()
  896. 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
  897. if err != nil {
  898. ut.Rollback()
  899. return
  900. }
  901. 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
  902. if err != nil {
  903. ut.Rollback()
  904. return
  905. }
  906. ut.Commit()
  907. return err
  908. }
  909. func BatchCheckOldDoctorAdvice(m *models.DoctorAdvice, ids []string, org_id int64, date int64) (err error) {
  910. ut := writeDb.Begin()
  911. 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
  912. if err != nil {
  913. ut.Rollback()
  914. return
  915. }
  916. 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
  917. if err != nil {
  918. ut.Rollback()
  919. return
  920. }
  921. ut.Commit()
  922. return err
  923. }
  924. func ModifyScheduleMode(mode_id int64, patient_id int64, recordDate int64, org_id int64) {
  925. ut := writeDb.Begin()
  926. 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
  927. if err != nil {
  928. ut.Rollback()
  929. return
  930. }
  931. ut.Commit()
  932. }
  933. func FindDialysisOrderById(id int64) (error, *models.DialysisOrder) {
  934. dialysisRecord := models.DialysisOrder{}
  935. err := readDb.Model(&models.DialysisOrder{}).Where("id = ? AND status = 1", id).Find(&dialysisRecord).Error
  936. return err, &dialysisRecord
  937. }
  938. func ModifyExceDoctorAdviceByGroupNo(m *models.DoctorAdvice, groupNo int64, org_id int64) (err error) {
  939. ut := writeDb.Begin()
  940. 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
  941. if err != nil {
  942. ut.Rollback()
  943. return
  944. }
  945. ut.Commit()
  946. return err
  947. }
  948. func BatchModifyExceOldDoctorAdvice(m *models.DoctorAdvice, ids []string) (err error) {
  949. ut := writeDb.Begin()
  950. 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
  951. if err != nil {
  952. ut.Rollback()
  953. return
  954. }
  955. 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
  956. if err != nil {
  957. ut.Rollback()
  958. return
  959. }
  960. ut.Commit()
  961. return err
  962. }
  963. func UpdateOrderCount(good_type_id int64, good_id int64, number string, org_id int64) {
  964. 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))
  965. }
  966. func UpdateOrderInfoDetails(good_type_id int64, good_id int64, time int64, org_id int64, patient_id int64, newNiprocart int64, out *models.WarehouseOutInfo) {
  967. 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})
  968. }
  969. func FindOrderInfoByGoodId(good_type_id int64, good_id int64, number string, org_id int64) (err error, out models.WarehouseOutInfo) {
  970. 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
  971. return
  972. }
  973. func FindGoodUserById(good_type_id int64, good_id int64, id int64, org_id int64, record_time int64) (user models.AutomaticReduceDetail, err error) {
  974. err = readDb.Model(&models.AutomaticReduceDetail{}).Where("good_id = ? AND good_type_id = ? AND patient_id = ? AND org_id = ? AND record_time = ? ", good_id, good_type_id, id, org_id, record_time).First(&user).Error
  975. return
  976. }
  977. func FindOldOrderInfoByGoodId(time int64, org_id int64) (err error, out models.WarehouseOutInfo) {
  978. err = readDb.Model(&models.WarehouseOutInfo{}).Where(" org_id = ? AND status = 1 AND sys_record_time = ? ", org_id, time).First(&out).Error
  979. return
  980. }
  981. func FindStockOutOrderNumberByTime(time int64, org_id int64) (err error, out models.WarehouseOut) {
  982. err = readDb.Model(&models.WarehouseOut{}).Where(" org_id = ? AND status = 1 AND warehouse_out_time = ? ", org_id, time).First(&out).Error
  983. return
  984. }
  985. func UpdateDetails(good_type_id int64, good_id int64, time int64, org_id int64, patient_id int64, newNiprocart int64, out *models.WarehouseOutInfo) {
  986. 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})
  987. }
  988. func FindPatientIsOpenRemindById(patient_id int64, org_id int64) (patients models.Patients, err error) {
  989. err = readDb.Model(&models.Patients{}).Where("status = 1 AND id = ? AND user_org_id = ?", patient_id, org_id).First(&patients).Error
  990. return
  991. }
  992. func GetMaxLongAdviceGroupID(orgId int64, patient_id int64) (group int64) {
  993. var advice models.DoctorAdvice
  994. 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
  995. if err != nil {
  996. fmt.Println(err)
  997. group = 0
  998. }
  999. group = advice.GroupNo
  1000. return
  1001. }
  1002. func GetLastLongAdviceByGroupNo(orgId int64, patient_id int64) (advice []*models.DoctorAdvice, err error) {
  1003. 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 AND frequency_type <> 0", orgId, patient_id).Preload("ChildDoctorAdvice", "status = 1").Find(&advice).Error
  1004. return
  1005. }
  1006. func GetLastLongAdviceByGroupNoOne(orgId int64, patient_id int64) (advice []*models.DoctorAdvice, err error) {
  1007. redis := RedisClient()
  1008. defer redis.Close()
  1009. key := strconv.FormatInt(orgId, 10) + ":" + strconv.FormatInt(patient_id, 10) + ":long_advice"
  1010. long_advice_str, _ := redis.Get(key).Result()
  1011. if len(long_advice_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1012. 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 AND frequency_type <> 0", orgId, patient_id).Preload("ChildDoctorAdvice", "status = 1").Find(&advice).Error
  1013. if err != nil {
  1014. if err == gorm.ErrRecordNotFound {
  1015. return nil, nil
  1016. } else {
  1017. return nil, err
  1018. }
  1019. } else {
  1020. if len(advice) > 0 {
  1021. //缓存数据
  1022. starfes_str, err := json.Marshal(advice)
  1023. if err == nil {
  1024. redis.Set(key, starfes_str, time.Second*60*60*18)
  1025. return advice, nil
  1026. }
  1027. } else {
  1028. redis.Set(key, "null", time.Second*60*60*18)
  1029. return advice, nil
  1030. }
  1031. return advice, nil
  1032. }
  1033. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  1034. if long_advice_str == "null" {
  1035. return advice, nil
  1036. } else {
  1037. json.Unmarshal([]byte(long_advice_str), &advice)
  1038. return advice, nil
  1039. }
  1040. }
  1041. 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 AND frequency_type <> 0", orgId, patient_id).Preload("ChildDoctorAdvice", "status = 1").Find(&advice).Error
  1042. return
  1043. }
  1044. func GetLastLongAdviceByGroupNoThree(orgId int64, patient_id int64) (advice []*models.DoctorAdvice, err error) {
  1045. 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
  1046. return
  1047. }
  1048. func GetLastLongAdviceByGroupNoFour(orgId int64, patient_id int64) (advice []*models.DoctorAdvice, err error) {
  1049. //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
  1050. //return
  1051. redis := RedisClient()
  1052. defer redis.Close()
  1053. key := strconv.FormatInt(orgId, 10) + ":" + strconv.FormatInt(patient_id, 10) + ":long_advice"
  1054. long_advice_str, _ := redis.Get(key).Result()
  1055. if len(long_advice_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1056. 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
  1057. if err != nil {
  1058. if err == gorm.ErrRecordNotFound {
  1059. return nil, nil
  1060. } else {
  1061. return nil, err
  1062. }
  1063. } else {
  1064. if len(advice) > 0 {
  1065. //缓存数据
  1066. starfes_str, err := json.Marshal(advice)
  1067. if err == nil {
  1068. redis.Set(key, starfes_str, time.Second*60*60*18)
  1069. return advice, nil
  1070. }
  1071. } else {
  1072. redis.Set(key, "null", time.Second*60*60*18)
  1073. return advice, nil
  1074. }
  1075. return advice, nil
  1076. }
  1077. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  1078. if long_advice_str == "null" {
  1079. return advice, nil
  1080. } else {
  1081. json.Unmarshal([]byte(long_advice_str), &advice)
  1082. return advice, nil
  1083. }
  1084. }
  1085. 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 AND frequency_type <> 0", orgId, patient_id).Preload("ChildDoctorAdvice", "status = 1").Find(&advice).Error
  1086. return
  1087. }
  1088. func FindAllDoctorAdviceByTime(now int64, end int64, patient_id int64, orgId int64, temp_id string) (advice []*models.DoctorAdvice, err error) {
  1089. 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
  1090. return
  1091. }
  1092. func UpdateScheduleModeId(patient_id int64, org_id int64, date int64, mode_id int64) (err error) {
  1093. 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
  1094. return
  1095. }
  1096. func UpdateAssessmentAfterDate(patient_id int64, org_id int64, date int64, actual_treatment_hour int64, actual_treatment_minute int64) (err error) {
  1097. 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
  1098. return
  1099. }
  1100. func UpdateAssessmentAfterDateOne(patient_id int64, org_id int64, date int64, actual_treatment_hour int64, actual_treatment_minute int64, BreathingRate float64) (err error) {
  1101. 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, "breathing_rate": BreathingRate}).Error
  1102. return
  1103. }
  1104. func CreateDialysisBeforePrepare(beforePrepares []*models.DialysisBeforePrepare) (err error) {
  1105. if len(beforePrepares) > 0 {
  1106. utx := writeDb.Begin()
  1107. if len(beforePrepares) > 0 {
  1108. thisSQL := "INSERT INTO dialysis_before_prepare (user_org_id, patient_id, record_date, good_id, good_type_id,count,ctime,mtime,creater,modifier,status,commdity_code) VALUES "
  1109. insertParams := make([]string, 0)
  1110. insertData := make([]interface{}, 0)
  1111. for _, prepare := range beforePrepares {
  1112. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?)")
  1113. insertData = append(insertData, prepare.UserOrgId)
  1114. insertData = append(insertData, prepare.PatientId)
  1115. insertData = append(insertData, prepare.RecordDate)
  1116. insertData = append(insertData, prepare.GoodId)
  1117. insertData = append(insertData, prepare.GoodTypeId)
  1118. insertData = append(insertData, prepare.Count)
  1119. insertData = append(insertData, prepare.Ctime)
  1120. insertData = append(insertData, prepare.Mtime)
  1121. insertData = append(insertData, prepare.Creater)
  1122. insertData = append(insertData, prepare.Modifier)
  1123. insertData = append(insertData, 1)
  1124. insertData = append(insertData, prepare.CommdityCode)
  1125. }
  1126. thisSQL += strings.Join(insertParams, ", ")
  1127. err = utx.Exec(thisSQL, insertData...).Error
  1128. if err != nil {
  1129. utx.Rollback()
  1130. return
  1131. }
  1132. }
  1133. utx.Commit()
  1134. }
  1135. return
  1136. }
  1137. func UpdateDialysisBeforePrepare(info *models.DialysisBeforePrepare) (err error) {
  1138. err = writeDb.Save(&info).Error
  1139. return err
  1140. }
  1141. func UpdateDialysisBeforePrepareTwo(info *models.DialysisBeforePrepare) (err error) {
  1142. err = writeDb.Model(&models.DialysisBeforePrepare{}).
  1143. Where("status = 1 and good_id = ? AND good_type_id = ? AND patient_id = ? AND user_org_id = ? AND record_date = ?", info.GoodId, info.GoodTypeId, info.PatientId, info.UserOrgId, info.RecordDate).
  1144. Updates(map[string]interface{}{"count": info.Count, "updated_time": time.Now().Unix(), "modifier": info.Modifier, "commdity_code": info.CommdityCode, "status": 1}).Error
  1145. return err
  1146. }
  1147. func UpdateAutoReduceDetailSix(detail *models.AutomaticReduceDetail) (err error) {
  1148. err = writeDb.Model(&models.AutomaticReduceDetail{}).Where("good_id = ? and good_type_id = ? And patient_id = ? and org_id =? and record_time = ?", detail.GoodId, detail.GoodTypeId, detail.PatientId, detail.OrgId, detail.RecordTime).Updates(map[string]interface{}{"count": detail.Count, "mtime": detail.Mtime, "status": 1}).Error
  1149. return err
  1150. }
  1151. func UpdateOrderCountSub(good_type_id int64, good_id int64, number string, org_id int64, count int64) {
  1152. 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))
  1153. }
  1154. func UpdateOrderCountAdd(good_type_id int64, good_id int64, number string, org_id int64, count int64) {
  1155. 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))
  1156. }
  1157. func UpdateUserInfoDetails(good_type_id int64, good_id int64, time int64, org_id int64, patient_id int64, count int64, out *models.WarehouseOutInfo) {
  1158. 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, "status": 1})
  1159. //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)
  1160. }
  1161. func UpdateDrugUserInfoDetails(drug_id int64, time int64, org_id int64, patient_id int64, count int64, out *models.DrugWarehouseOutInfo) {
  1162. writeDb.Model(&models.DrugAutomaticReduceDetail{}).Where("drug_id = ? AND org_id = ? AND status = 1 AND warehouse_out_order_number = ? AND patient_id = ?", drug_id, org_id, out.WarehouseOutOrderNumber, patient_id).Updates(map[string]interface{}{"count": count})
  1163. //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)
  1164. }
  1165. func FindDialysisBeforePrepare(patient_id int64, good_id int64, good_type_id int64, user_org_id int64, record_time int64) (count int64) {
  1166. readDb.Model(&models.DialysisBeforePrepare{}).Where("user_org_id = ? AND patient_id = ? AND good_id = ? AND good_type_id = ? AND record_date = ? AND status = 1 AND count<> 0", user_org_id, patient_id, good_id, good_type_id, record_time).Count(&count)
  1167. return
  1168. }
  1169. func GetExecutionDoctors(orgid int64, patientid int64, id int64) (doctorAdvice []*models.DoctorAdvice, err error) {
  1170. err = readDb.Model(&doctorAdvice).Where("user_org_id = ? and patient_id = ? AND (id = ? Or parent_id=?) and status = 1", orgid, patientid, id, id).Find(&doctorAdvice).Error
  1171. return doctorAdvice, err
  1172. }
  1173. func GetHisExecutionDoctors(orgid int64, patientid int64, id int64) (doctorAdvice []*models.HisDoctorAdviceInfo, err error) {
  1174. err = readDb.Model(&models.HisDoctorAdviceInfo{}).Where("user_org_id = ? and patient_id = ? AND (id = ? Or parent_id=?) and status = 1", orgid, patientid, id, id).Find(&doctorAdvice).Error
  1175. return doctorAdvice, err
  1176. }
  1177. func FindHisDoctorAdviceById(orgID int64, id int64) (advice models.HisDoctorAdviceInfo, err error) {
  1178. err = readDb.Model(&models.HisDoctorAdviceInfo{}).Where("user_org_id=? and status=1 and id = ?", orgID, id).First(&advice).Error
  1179. return
  1180. }
  1181. func FindHisDoctorAdviceByIds(orgID int64, ids []string) (advice []*models.HisDoctorAdviceInfo, err error) {
  1182. err = readDb.Model(&models.HisDoctorAdviceInfo{}).Where("user_org_id=? and status=1 and id in (?)", orgID, ids).Find(&advice).Error
  1183. return
  1184. }
  1185. func SaveHisDoctorAdvice(advice *models.HisDoctorAdviceInfo) (err error) {
  1186. err = writeDb.Save(&advice).Error
  1187. return
  1188. }
  1189. func GetGoodInfoMation(orgid int64) (goodinfo []*models.GoodInfo, err error) {
  1190. err = XTReadDB().Model(&goodinfo).Where("org_id = ? and status = 1 AND find_in_set('停用',good_status) = 0", orgid).Preload("GoodSotckInfo", "stock_count > 0 and status = 1").Find(&goodinfo).Error
  1191. //err = XTReadDB().Model(&goodinfo).Where("org_id = ? and status = 1 AND find_in_set('停用',good_status) = 0", orgid).Find(&goodinfo).Error
  1192. return goodinfo, err
  1193. }
  1194. func GetDialysisBeforePrepare(goodTypeId int64, goodId int64, orgid int64, patientId int64) (*models.DialysisBeforePrepare, error) {
  1195. prepare := models.DialysisBeforePrepare{}
  1196. err := XTReadDB().Where("good_type_id = ? and good_id = ? and user_org_id = ? and status =1 and patient_id = ?", goodTypeId, goodId, orgid, patientId).Last(&prepare).Error
  1197. if err == gorm.ErrRecordNotFound {
  1198. return nil, err
  1199. }
  1200. if err != nil {
  1201. return nil, err
  1202. }
  1203. return &prepare, nil
  1204. }
  1205. func CreateDialysisBeforePrepareOne(prepare *models.DialysisBeforePrepare) error {
  1206. err := XTReadDB().Create(&prepare).Error
  1207. return err
  1208. }
  1209. func ModifyExceDoctorAdviceById(m *models.HisDoctorAdviceInfo, ids []string) (err error) {
  1210. ut := writeDb.Begin()
  1211. err = ut.Model(&models.HisDoctorAdviceInfo{}).Where("status = 1 AND id IN (?) AND execution_state = 1", ids).Updates(map[string]interface{}{"execution_time": m.ExecutionTime}).Error
  1212. if err != nil {
  1213. ut.Rollback()
  1214. return
  1215. }
  1216. err = ut.Model(&models.HisDoctorAdviceInfo{}).Where("status = 1 AND parent_id IN (?) AND execution_state = 1 ", ids).Updates(map[string]interface{}{"execution_time": m.ExecutionTime}).Error
  1217. if err != nil {
  1218. ut.Rollback()
  1219. return
  1220. }
  1221. ut.Commit()
  1222. return err
  1223. }
  1224. func FindHisDoctorAdviceByIdOne(orgID int64, ids []string) (advice []models.HisDoctorAdviceInfo, err error) {
  1225. err = readDb.Model(&models.HisDoctorAdviceInfo{}).Where("id IN (?) AND user_org_id = ? AND status = 1", ids, orgID).Find(&advice).Error
  1226. return
  1227. }
  1228. func ModifyHisPrescriptionProject(m *models.HisPrescriptionProject, ids []string) (err error) {
  1229. ut := writeDb.Begin()
  1230. err = ut.Model(&models.HisPrescriptionProject{}).Where("status = 1 AND id IN (?) AND execution_state = 1", ids).Updates(map[string]interface{}{"execution_time": m.ExecutionTime}).Error
  1231. if err != nil {
  1232. ut.Rollback()
  1233. return
  1234. }
  1235. ut.Commit()
  1236. return err
  1237. }
  1238. func FindHisProjectById(orgID int64, id int64) (advice models.HisPrescriptionProject, err error) {
  1239. err = readDb.Model(&models.HisPrescriptionProject{}).Where("user_org_id=? and status=1 and id = ?", orgID, id).First(&advice).Error
  1240. return
  1241. }
  1242. func SaveHisProject(advice *models.HisPrescriptionProject) (err error) {
  1243. err = writeDb.Save(&advice).Error
  1244. return
  1245. }
  1246. func FindAllHisProjectById(orgID int64, patient_id int64, record_time int64) (advice []*models.HisPrescriptionProject, err error) {
  1247. err = readDb.Model(&models.HisPrescriptionProject{}).Preload("HisProject", "status = 1").Preload("GoodInfo", "status = 1").Where("user_org_id=? and status=1 and patient_id = ? AND record_date = ?", orgID, patient_id, record_time).Find(&advice).Error
  1248. return
  1249. }
  1250. func GetAllPcPatientListByListSeven(orgID int64) (patients []*MSchedualPatientVMList, err error) {
  1251. err = readDb.Where("user_org_id=? and status=1 and lapseto = 1", orgID).Find(&patients).Error
  1252. return patients, err
  1253. }
  1254. func GetAllPcPatientListByListEight(orgID int64) (patients []*MSchedualPatientVMList, err error) {
  1255. err = readDb.Where("user_org_id=? and status=1", orgID).Find(&patients).Error
  1256. return patients, err
  1257. }
  1258. func GetAllPcPatientListByList(orgID int64) (patients []*MSchedualPatientVMList, err error) {
  1259. redis := RedisClient()
  1260. defer redis.Close()
  1261. key := strconv.FormatInt(orgID, 10) + ":" + ":patient_list_all"
  1262. patient_info_str, _ := redis.Get(key).Result()
  1263. if len(patient_info_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1264. err = readDb.Where("user_org_id=? and status=1 and lapseto = 1", orgID).Find(&patients).Error
  1265. if err != nil {
  1266. if err == gorm.ErrRecordNotFound {
  1267. if len(patients) <= 0 {
  1268. redis.Set(key, "null", time.Second*60*60*18)
  1269. }
  1270. return nil, nil
  1271. } else {
  1272. return nil, err
  1273. }
  1274. } else {
  1275. if len(patients) > 0 {
  1276. //缓存数据
  1277. patient_info_json, err := json.Marshal(&patients)
  1278. if err == nil {
  1279. redis.Set(key, patient_info_json, time.Second*60*60*18)
  1280. }
  1281. } else {
  1282. redis.Set(key, "null", time.Second*60*60*18)
  1283. }
  1284. return patients, nil
  1285. }
  1286. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  1287. if patient_info_str == "null" {
  1288. return nil, nil
  1289. } else {
  1290. json.Unmarshal([]byte(patient_info_str), &patients)
  1291. return patients, nil
  1292. }
  1293. }
  1294. }
  1295. func GetAllPcAdvicestByList(orgID int64, scheduleDate int64) (advices []*VMDoctorAdvice, err error) {
  1296. redis := RedisClient()
  1297. defer redis.Close()
  1298. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":advice_list_all"
  1299. advice_list_str, _ := redis.Get(key).Result()
  1300. if len(advice_list_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1301. err = readDb.Where("status = 1 AND user_org_id = ? AND advice_type = 2 AND advice_date = ? ", orgID, scheduleDate).Find(&advices).Error
  1302. if err != nil {
  1303. if err == gorm.ErrRecordNotFound {
  1304. if len(advices) <= 0 {
  1305. redis.Set(key, "null", time.Second*60*60*18)
  1306. }
  1307. return nil, nil
  1308. } else {
  1309. return nil, err
  1310. }
  1311. } else {
  1312. if len(advices) > 0 {
  1313. //缓存数据
  1314. advice_list_str, err := json.Marshal(&advices)
  1315. if err == nil {
  1316. redis.Set(key, advice_list_str, time.Second*60*60*18)
  1317. }
  1318. } else {
  1319. redis.Set(key, "null", time.Second*60*60*18)
  1320. }
  1321. return advices, nil
  1322. }
  1323. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  1324. if advice_list_str == "null" {
  1325. return nil, nil
  1326. } else {
  1327. json.Unmarshal([]byte(advice_list_str), &advices)
  1328. return advices, nil
  1329. }
  1330. }
  1331. }
  1332. func GetAllPcDialysisOrdersByList(orgID int64, scheduleDate int64) (dialysisOrders []*MDialysisOrderVMList, err error) {
  1333. redis := RedisClient()
  1334. defer redis.Close()
  1335. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":dialysis_orders_list_all"
  1336. dialysis_orders_list_all, _ := redis.Get(key).Result()
  1337. if len(dialysis_orders_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1338. err = readDb.Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).Where("status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, scheduleDate).Find(&dialysisOrders).Error
  1339. if err != nil {
  1340. if err == gorm.ErrRecordNotFound {
  1341. return nil, nil
  1342. } else {
  1343. return nil, err
  1344. }
  1345. } else {
  1346. if len(dialysisOrders) > 0 {
  1347. //缓存数据
  1348. dialysis_orders_list_all, err := json.Marshal(&dialysisOrders)
  1349. if err == nil {
  1350. redis.Set(key, dialysis_orders_list_all, time.Second*60*60*18)
  1351. }
  1352. } else {
  1353. redis.Set(key, "null", time.Second*60*60*18)
  1354. }
  1355. return dialysisOrders, nil
  1356. }
  1357. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  1358. if dialysis_orders_list_all == "null" {
  1359. return nil, nil
  1360. } else {
  1361. json.Unmarshal([]byte(dialysis_orders_list_all), &dialysisOrders)
  1362. return dialysisOrders, nil
  1363. }
  1364. }
  1365. }
  1366. func GetAllPcAssessmentAfterDislysisByList(orgID int64, scheduleDate int64) (assessmentAfterDislysis []*AssessmentAfterDislysis, err error) {
  1367. redis := RedisClient()
  1368. defer redis.Close()
  1369. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":assessment_after_dislysis_list_all"
  1370. assessment_after_dislysis__all, _ := redis.Get(key).Result()
  1371. if len(assessment_after_dislysis__all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1372. err = readDb.Where("status = 1 AND user_org_id = ? AND assessment_date = ?", orgID, scheduleDate).Find(&assessmentAfterDislysis).Error
  1373. if err != nil {
  1374. if err == gorm.ErrRecordNotFound {
  1375. if len(assessmentAfterDislysis) <= 0 {
  1376. redis.Set(key, "null", time.Second*60*60*18)
  1377. }
  1378. return nil, nil
  1379. } else {
  1380. return nil, err
  1381. }
  1382. } else {
  1383. if len(assessmentAfterDislysis) > 0 {
  1384. //缓存数据
  1385. assessment_after_dislysis__all, err := json.Marshal(&assessmentAfterDislysis)
  1386. if err == nil {
  1387. redis.Set(key, assessment_after_dislysis__all, time.Second*60*60*18)
  1388. }
  1389. } else {
  1390. redis.Set(key, "null", time.Second*60*60*18)
  1391. }
  1392. return assessmentAfterDislysis, nil
  1393. }
  1394. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  1395. if assessment_after_dislysis__all == "null" {
  1396. return nil, nil
  1397. } else {
  1398. json.Unmarshal([]byte(assessment_after_dislysis__all), &assessmentAfterDislysis)
  1399. return assessmentAfterDislysis, nil
  1400. }
  1401. }
  1402. }
  1403. func GetAllPcTreatmentSummarysByList(orgID int64, scheduleDate int64) (treatmentSummarys []*VMTreatmentSummary, err error) {
  1404. redis := RedisClient()
  1405. defer redis.Close()
  1406. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":treatment_summarys_list_all"
  1407. treatment_summarys_all, _ := redis.Get(key).Result()
  1408. if len(treatment_summarys_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1409. err = readDb.Where("status = 1 AND user_org_id = ? AND assessment_date = ?", orgID, scheduleDate).Find(&treatmentSummarys).Error
  1410. if err != nil {
  1411. if err == gorm.ErrRecordNotFound {
  1412. if len(treatment_summarys_all) <= 0 {
  1413. redis.Set(key, "null", time.Second*60*60*18)
  1414. }
  1415. return nil, nil
  1416. } else {
  1417. return nil, err
  1418. }
  1419. } else {
  1420. if len(treatmentSummarys) > 0 {
  1421. //缓存数据
  1422. treatment_summarys_all, err := json.Marshal(&treatmentSummarys)
  1423. if err == nil {
  1424. redis.Set(key, treatment_summarys_all, time.Second*60*60*18)
  1425. }
  1426. } else {
  1427. redis.Set(key, "null", time.Second*60*60*18)
  1428. }
  1429. return treatmentSummarys, nil
  1430. }
  1431. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  1432. if treatment_summarys_all == "null" {
  1433. return nil, nil
  1434. } else {
  1435. json.Unmarshal([]byte(treatment_summarys_all), &treatmentSummarys)
  1436. return treatmentSummarys, nil
  1437. }
  1438. }
  1439. }
  1440. func UpdateTeamProjectExceStatus(ids []int64, admin_user_id int64, execution_time int64) (err error) {
  1441. ut := writeDb.Begin()
  1442. db := ut.Model(&models.HisPrescriptionProject{})
  1443. err = db.Where("status = 1 AND id IN (?)", ids).Updates(map[string]interface{}{"execution_time": execution_time, "execution_state": 1, "execution_staff": admin_user_id}).Error
  1444. if err != nil {
  1445. ut.Rollback()
  1446. return
  1447. }
  1448. ut.Commit()
  1449. return err
  1450. }
  1451. func UpdateTeamProjectCheckStatus(ids []int64, patient_id int64, record_date int64, user_org_id int64, admin_user_id int64, check_time int64) (err error) {
  1452. ut := writeDb.Begin()
  1453. err = ut.Model(&models.HisPrescriptionProject{}).Where("status = 1 AND id IN (?) AND patient_id = ? AND user_org_id = ? AND record_date = ?", ids, patient_id, user_org_id, record_date).Updates(map[string]interface{}{"check_time": check_time, "check_state": 1, "checker": admin_user_id}).Error
  1454. if err != nil {
  1455. ut.Rollback()
  1456. return
  1457. }
  1458. ut.Commit()
  1459. return err
  1460. }
  1461. func GetCheckTeamProject(team_id int64, patient_id int64, record_date int64, user_org_id int64) (ps []*models.HisPrescriptionProject, err error) {
  1462. err = readDb.Model(&models.HisPrescriptionProject{}).Joins("JOIN xt_his_project as p On p.id = his_prescription_project.project_id AND p.cost_classify = 3").Where("his_prescription_project.team_id IN (?) AND his_prescription_project.patient_id = ? AND his_prescription_project.user_org_id = ? AND his_prescription_project.record_date = ? AND his_prescription_project.status = 1", team_id, patient_id, user_org_id, record_date).Find(&ps).Error
  1463. return
  1464. }
  1465. func GetAdminUserRoleInformation(orgid int64) (models.SgjOrgFollow, error) {
  1466. follow := models.SgjOrgFollow{}
  1467. db := UserReadDB().Table("sgj_org_follow as f").Where("f.status = 1")
  1468. table := UserReadDB().Table("sgj_admin as a").Where("a.status = 1")
  1469. fmt.Println(table)
  1470. if orgid > 0 {
  1471. db = db.Where("org_id = ?", orgid)
  1472. }
  1473. if orgid == 0 {
  1474. db = db.Where("org_id = ?", 0)
  1475. }
  1476. err := db.Select("f.org_id,f.admin_user_id,a.name,a.phone,a.camera,a.wechat,a.qqchat,a.email,a.wechat_link").Joins("left join sgj_admin as a on a.id = f.admin_user_id").Scan(&follow).Error
  1477. return follow, err
  1478. }
  1479. func GetOrgFollowIsExist(orgid int64) (*models.SgjOrgFollowOne, error) {
  1480. follow := models.SgjOrgFollowOne{}
  1481. err := UserReadDB().Where("org_id = ? and status = 1", orgid).Find(&follow).Error
  1482. if err == gorm.ErrRecordNotFound {
  1483. return nil, err
  1484. }
  1485. if err != nil {
  1486. return nil, err
  1487. }
  1488. return &follow, nil
  1489. }