dialysis_service.go 85KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801
  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 advice_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.DoubleCheck{}).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. func UpdatePredialysisEvaluationFour(predialysisevaluation *models.PredialysisEvaluation, id int64) error {
  281. err := writeDb.Model(&predialysisevaluation).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"temperature": predialysisevaluation.Temperature}).Error
  282. return err
  283. }
  284. //修改
  285. func UpdateSummeRecord(treatmentSummary *models.TreatmentSummary) error {
  286. err := writeDb.Save(&treatmentSummary).Error
  287. return err
  288. }
  289. // func UpdateDoctorAdviceById(m *models.DoctorAdvice, id int64, patientId int64) error {
  290. // tx := writeDb.Begin()
  291. // 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
  292. // if err != nil {
  293. // tx.Rollback()
  294. // }
  295. // tx.Commit()
  296. // return err
  297. // }
  298. func ExceDoctorAdviceById(m *models.DoctorAdvice, id int64, patientId int64) error {
  299. 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
  300. return err
  301. }
  302. func ExecBatchDoctorAdvice(patient_id int64, orgid int64, ids []string, creater int64) error {
  303. err := writeDb.Model(&models.DoctorAdvice{}).Where("patient_id = ? AND user_org_id = ? and id in(?) AND status = 1 ", patient_id, orgid, ids).Updates(map[string]interface{}{"execution_time": time.Now().Unix(), "execution_staff": creater, "execution_state": 1}).Error
  304. return err
  305. }
  306. func ExceHidDoctorAdviceById(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{}{"execution_time": m.ExecutionTime, "execution_staff": m.ExecutionStaff, "execution_state": 1}).Error
  308. return err
  309. }
  310. func ExceHisBatchDotorAdvice(patientid int64, orgid int64, ids []string, creater int64) error {
  311. err := writeDb.Model(&models.HisDoctorAdviceInfo{}).Where("patient_id = ? AND user_org_id = ? and id in(?) AND status = 1 ", patientid, orgid, ids).Updates(map[string]interface{}{"execution_time": time.Now().Unix(), "execution_staff": creater, "execution_state": 1}).Error
  312. return err
  313. }
  314. func CheckDoctorAdviceById(m *models.DoctorAdvice, id int64, patientId int64) error {
  315. 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
  316. return err
  317. }
  318. func CheckHisBathDoctorAdviceById(patientId int64, orgid int64, creater int64, ids []string) error {
  319. err := writeDb.Model(&models.HisDoctorAdvice{}).Where("patient_id = ? and user_org_id = ? and id in(?)", patientId, orgid, ids).Updates(map[string]interface{}{"check_time": time.Now().Unix(), "checker": creater, "check_state": 1}).Error
  320. return err
  321. }
  322. func CheckBatchDoctorAdviceById(patientId int64, ids []string, orgid int64, creater int64) error {
  323. err := writeDb.Model(&models.DoctorAdvice{}).Where("patient_id = ? and user_org_id = ? and id in(?)", patientId, orgid, ids).Updates(map[string]interface{}{"check_time": time.Now().Unix(), "checker": creater, "check_state": 1}).Error
  324. return err
  325. }
  326. func CheckHisDoctorAdviceById(m *models.HisDoctorAdviceInfo, id int64, patientId int64) error {
  327. 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
  328. return err
  329. }
  330. func CreateMonitor(m *models.MonitoringRecord) (err error) {
  331. return writeDb.Create(m).Error
  332. }
  333. func UpdateMonitor(m *models.MonitoringRecord) (err error) {
  334. return writeDb.Save(m).Error
  335. }
  336. func DelMonitor(patientId int64, recordId int64) error {
  337. tx := writeDb.Begin()
  338. updateTime := time.Now().Unix()
  339. 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
  340. if err != nil {
  341. tx.Rollback()
  342. }
  343. tx.Commit()
  344. return err
  345. }
  346. //修改长期处方
  347. func UpdateSolution(solution *models.DialysisSolution) {
  348. writeDb.Save(&solution)
  349. }
  350. //创建长期处方
  351. func CreateSolution(solution *models.DialysisSolution) {
  352. writeDb.Create(&solution)
  353. }
  354. func SavePrescriptionAndCreateSolution(solution *models.DialysisSolution, prescription *models.DialysisPrescription) (err error) {
  355. tx := writeDb.Begin()
  356. if prescription.ID > 0 {
  357. err = tx.Save(&prescription).Error
  358. } else {
  359. err = tx.Model(&models.DialysisPrescription{}).Create(&prescription).Error
  360. }
  361. if err != nil {
  362. tx.Rollback()
  363. return
  364. }
  365. err = tx.Create(&solution).Error
  366. if err != nil {
  367. tx.Rollback()
  368. return
  369. }
  370. tx.Commit()
  371. return
  372. }
  373. //透析处方
  374. func FindDialysisPrescriptionByReordDate(id int64, recordDate int64, orgId int64) (error, *models.DialysisPrescription) {
  375. dialysisPrescription := models.DialysisPrescription{}
  376. err := readDb.Model(&dialysisPrescription).Where("patient_id = ? AND record_date = ? AND status = 1 AND user_org_id = ? ", id, recordDate, orgId).Find(&dialysisPrescription).Error
  377. return err, &dialysisPrescription
  378. }
  379. func FindReceiveTreatmentAssesByReordDate(id int64, recordDate int64, orgId int64) (error, *models.ReceiveTreatmentAsses) {
  380. receiveTreatmentAsses := models.ReceiveTreatmentAsses{}
  381. err := readDb.Model(&receiveTreatmentAsses).Where("patient_id = ? AND record_date = ? AND status = 1 AND user_org_id = ?", id, recordDate, orgId).Find(&receiveTreatmentAsses).Error
  382. return err, &receiveTreatmentAsses
  383. }
  384. //透前评估
  385. func FindPredialysisEvaluationByReordDate(id int64, recordDate int64, orgId int64) (error, *models.PredialysisEvaluation) {
  386. dialysisEvaluation := models.PredialysisEvaluation{}
  387. err := readDb.Model(&models.PredialysisEvaluation{}).Where("patient_id = ? AND assessment_date = ? AND status = 1 AND user_org_id = ?", id, recordDate, orgId).First(&dialysisEvaluation).Error
  388. return err, &dialysisEvaluation
  389. }
  390. //双人核对
  391. func FindDoubleCheckByReordDate(id int64, recordDate int64, orgId int64) (error, *models.DoubleCheck) {
  392. check := models.DoubleCheck{}
  393. err := readDb.Model(&models.DoubleCheck{}).Where("patient_id = ? AND check_date = ? AND status = 1 AND user_org_id = ?", id, recordDate, orgId).First(&check).Error
  394. return err, &check
  395. }
  396. func FindAssessmentAfterDislysisByReordDate(id int64, recordDate int64, orgId int64) (error, *models.AssessmentAfterDislysis) {
  397. assessmentAfterDislysis := models.AssessmentAfterDislysis{}
  398. err := readDb.Model(&models.AssessmentAfterDislysis{}).Where("patient_id = ? AND assessment_date = ? AND status = 1 AND user_org_id = ?", id, recordDate, orgId).First(&assessmentAfterDislysis).Error
  399. return err, &assessmentAfterDislysis
  400. }
  401. func FindTreatmentSummaryByReordDate(id int64, recordDate int64, orgId int64) (error, *models.TreatmentSummary) {
  402. summary := models.TreatmentSummary{}
  403. err := readDb.Model(&models.TreatmentSummary{}).Where("patient_id = ? AND assessment_date = ? AND status = 1 AND user_org_id = ?", id, recordDate, orgId).First(&summary).Error
  404. return err, &summary
  405. }
  406. func FindDoctorAdviceByReordDate(id int64, recordDate int64, orgId int64) (error, *models.DoctorAdvice) {
  407. advice := models.DoctorAdvice{}
  408. 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
  409. return err, &advice
  410. }
  411. func FindDialysisRecordById(orgId int64, id int64, recordDate int64) (error, *models.DialysisOrder) {
  412. var count int64
  413. dialysisRecord := models.DialysisOrder{}
  414. err := readDb.Model(&models.DialysisOrder{}).Where("user_org_id = ? AND patient_id = ? AND dialysis_date = ?", orgId, id, recordDate).Find(&dialysisRecord).Count(&count).Error
  415. if count <= 0 {
  416. return err, nil
  417. }
  418. return err, &dialysisRecord
  419. }
  420. func FindDialysisSchedualById(orgId int64, id int64, recordDate int64) (error, *models.Schedule) {
  421. var count int64
  422. schedule := models.Schedule{}
  423. err := readDb.Model(&models.Schedule{}).Where("user_org_id = ? AND patient_id = ? AND schedule_date = ?", orgId, id, recordDate).Find(&schedule).Count(&count).Error
  424. if count <= 0 {
  425. return err, nil
  426. }
  427. return err, &schedule
  428. }
  429. func CreateDialysisRecord(id int64, orgId int64, record *models.DialysisOrder) error {
  430. err := writeDb.Model(&models.DialysisOrder{}).Where("user_org_id = ? AND patient_id = ? ", orgId, id).Create(&record).Error
  431. if err != nil {
  432. return err
  433. }
  434. dialysisPrescriptionErr := UpDateRecordIdToDialysisPrescription(id, orgId, record.DialysisDate, record.ID)
  435. if dialysisPrescriptionErr != nil {
  436. return dialysisPrescriptionErr
  437. }
  438. receiveTreatmentAssesErr := UpDateRecordIdToReceiveTreatmentAsses(id, orgId, record.DialysisDate, record.ID)
  439. if receiveTreatmentAssesErr != nil {
  440. return receiveTreatmentAssesErr
  441. }
  442. predialysisEvaluationErr := UpDateRecordIdToPredialysisEvaluation(id, orgId, record.DialysisDate, record.ID)
  443. if predialysisEvaluationErr != nil {
  444. return predialysisEvaluationErr
  445. }
  446. treatmentSummaryErr := UpDateRecordIdToTreatmentSummary(id, orgId, record.DialysisDate, record.ID)
  447. if treatmentSummaryErr != nil {
  448. return treatmentSummaryErr
  449. }
  450. doubleCheckErr := UpDateRecordIdToDoubleCheck(id, orgId, record.DialysisDate, record.ID)
  451. if doubleCheckErr != nil {
  452. return doubleCheckErr
  453. }
  454. adviceErrr := UpDateRecordIdToDoctorAdvice(id, orgId, record.DialysisDate, record.ID)
  455. if adviceErrr != nil {
  456. return adviceErrr
  457. }
  458. //透析监测
  459. records, err := FindAllMonitorRecord(orgId, id, record.DialysisDate)
  460. if err != nil {
  461. return err
  462. }
  463. var Ids []int64
  464. for _, item := range records {
  465. Ids = append(Ids, item.ID)
  466. }
  467. monitorErr := UpDateRecordIdToMonitor(Ids, record.ID)
  468. if monitorErr != nil {
  469. return monitorErr
  470. }
  471. return nil
  472. }
  473. func ModifyDialysisRecord(id int64, finishNurse int64, end_time int64, admin_user_id int64, puncture_point_haematoma int64, internal_fistula string, catheter string, cruor string) error {
  474. 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, "puncture_point_haematoma": puncture_point_haematoma, "blood_access_internal_fistula": internal_fistula, "catheter": catheter, "cruor": cruor}).Error
  475. return err
  476. }
  477. //func ModifyDialysisRecord(id int64, finishNurse int64, end_time int64, admin_user_id int64) error {
  478. // 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
  479. // return err
  480. //
  481. //}
  482. func UpdateSolutionByPatientId(id int64) error {
  483. err := writeDb.Model(&models.Patients{}).Where("id = ? AND status = 1 ", id).UpdateColumn("total_dialysis", gorm.Expr("total_dialysis + ?", 1)).Error
  484. return err
  485. }
  486. //更新透析处方记录ID
  487. func UpDateRecordIdToDialysisPrescription(patientId int64, orgId int64, recordTime int64, recordId int64) error {
  488. 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
  489. return errors
  490. }
  491. func UpDateRecordIdToReceiveTreatmentAsses(patientId int64, orgId int64, recordTime int64, recordId int64) error {
  492. 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
  493. return errors
  494. }
  495. func UpDateRecordIdToPredialysisEvaluation(patientId int64, orgId int64, recordTime int64, recordId int64) error {
  496. 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
  497. return errors
  498. }
  499. func UpDateRecordIdToAssessmentAfterDislysis(patientId int64, orgId int64, recordTime int64, recordId int64) error {
  500. 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
  501. return errors
  502. }
  503. func UpDateRecordIdToTreatmentSummary(patientId int64, orgId int64, recordTime int64, recordId int64) error {
  504. 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
  505. return errors
  506. }
  507. func UpDateRecordIdToMonitor(Ids []int64, recordId int64) error {
  508. errors := writeDb.Model(&models.MonitoringRecord{}).Where("id IN (?)", Ids).Update(map[string]interface{}{"dialysis_order_id": recordId}).Error
  509. return errors
  510. }
  511. func UpDateRecordIdToDoubleCheck(patientId int64, orgId int64, recordTime int64, recordId int64) error {
  512. 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
  513. return errors
  514. }
  515. func UpDateRecordIdToDoctorAdvice(patientId int64, orgId int64, recordTime int64, recordId int64) error {
  516. 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
  517. return errors
  518. }
  519. //透析处方
  520. func FindPatientPrescribeWidyDevideById(orgID int64, patientsId int64, recordDate int64) (patient models.DialysisPrescriptionAddtional, err error) {
  521. err = readDb.Preload("Device", "status = 1 AND org_id = ?", orgID).
  522. Model(&models.DialysisPrescriptionAddtional{}).
  523. Where("patient_id = ? and user_org_id=? and status=1 and record_date = ? ", patientsId, orgID, recordDate).
  524. First(&patient).Error
  525. return
  526. }
  527. func FindDialysisMode(orgID int64, patientId int64, scheduleDate int64) (models.XtSchedule, error) {
  528. schedule := models.XtSchedule{}
  529. err = readDb.Where("user_org_id = ? and patient_id = ? and schedule_date = ? ", orgID, patientId, scheduleDate).Find(&schedule).Error
  530. return schedule, err
  531. }
  532. func FindDialysisMacher(orgid int64, patientid int64, dialysisDate int64) (models.XtDialysisOrders, error) {
  533. orders := models.XtDialysisOrders{}
  534. db := readDb.Table("xt_dialysis_order as o").Where("o.status = 1")
  535. table := readDb.Table("xt_device_number as n")
  536. fmt.Print("table", table)
  537. 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
  538. return orders, err
  539. }
  540. func FindPunctureNurse(adminid int64, appid int64, orgid int64) (models.App_Roles, error) {
  541. role := models.App_Roles{}
  542. //err := UserReadDB().Where("admin_user_id = ? and app_id = ? and org_id = ?", adminid, appid, orgid).Find(&role).Error
  543. //return role, err
  544. db := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status =1")
  545. 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
  546. return role, err
  547. }
  548. func FindeStartNuse(adminid int64, appid int64, orgid int64) (models.App_Roles, error) {
  549. role := models.App_Roles{}
  550. db := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  551. 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
  552. //err := UserReadDB().Where("admin_user_id = ? and app_id = ? and org_id = ?", adminid, appid, orgid).Find(&role).Error
  553. return role, err
  554. }
  555. func FindDialysisWay(orgid int64, patientid int64, recorddate int64) (models.DialysisPrescription, error) {
  556. prescription := models.DialysisPrescription{}
  557. err := readDb.Where("user_org_id = ? and patient_id = ? and record_date = ? and status =1", orgid, patientid, recorddate).Find(&prescription).Error
  558. return prescription, err
  559. }
  560. func FindDoctorOrder(orgid int64, patientid int64, adviceDate int64) (models.XtDoctorAdvice, error) {
  561. advices := models.XtDoctorAdvice{}
  562. db := readDb.Table("xt_doctor_advice as a").Where("a.status = ?", 1)
  563. 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
  564. return advices, err
  565. }
  566. func FindDoctor(adminuserid int64, appid int64, orgid int64) (models.App_Role, error) {
  567. role := models.App_Role{}
  568. err := UserReadDB().Where("admin_user_id = ? and app_id = ? and org_id = ?", adminuserid, appid, orgid).Find(&role).Error
  569. return role, err
  570. }
  571. func FindDoctorName(adminid int64, appid int64, orgid int64) (models.App_Roles, error) {
  572. role := models.App_Roles{}
  573. db := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  574. 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
  575. //err := UserReadDB().Where("admin_user_id = ? and app_id = ? and org_id = ?", adminid, appid, orgid).Find(&role).Error
  576. return role, err
  577. }
  578. /////////////// 新 18-10-22
  579. func GetDialysisScheduals(orgID int64, scheduleDate int64) ([]*MDialysisScheduleVM, error) {
  580. var vms []*MDialysisScheduleVM
  581. db := readDb.
  582. Table("xt_schedule").
  583. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  584. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  585. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  586. Preload("DialysisOrder", "status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, scheduleDate).
  587. Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ? AND assessment_date = ?", orgID, scheduleDate).
  588. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  589. Preload("TreatmentSummary", "status = 1 AND user_org_id = ? AND assessment_date = ? ", orgID, scheduleDate).
  590. Preload("Advices", "status = 1 AND user_org_id = ? AND advice_date = ? ", orgID, scheduleDate).
  591. Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ? and assessment_date = ?", orgID, scheduleDate).
  592. Where("status = 1 AND user_org_id = ?", orgID)
  593. if scheduleDate != 0 {
  594. db = db.Where("schedule_date = ?", scheduleDate)
  595. }
  596. err := db.Find(&vms).Error
  597. return vms, err
  598. }
  599. func GetDialysisSchedualsOne(orgID int64, scheduleDate int64) ([]*MDialysisScheduleVM, error) {
  600. var vms []*MDialysisScheduleVM
  601. db := readDb.
  602. Table("xt_schedule").
  603. Preload("DeviceNumber", "org_id = ?", orgID).
  604. Preload("DeviceNumber.Zone", "org_id = ?", orgID).
  605. //Preload("DialysisOrder", "status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, scheduleDate).
  606. //Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ? AND assessment_date = ?", orgID, scheduleDate).
  607. //Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  608. //Preload("TreatmentSummary", "status = 1 AND user_org_id = ? AND assessment_date = ? ", orgID, scheduleDate).
  609. //Preload("Advices", "status = 1 AND user_org_id = ? AND advice_date = ? ", orgID, scheduleDate).
  610. //Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ? and assessment_date = ?", orgID, scheduleDate).
  611. Where("status = 1 AND user_org_id = ?", orgID)
  612. if scheduleDate != 0 {
  613. db = db.Where("schedule_date = ?", scheduleDate)
  614. }
  615. err := db.Find(&vms).Error
  616. return vms, err
  617. }
  618. func GetLastAcceptsAssessment(patient int64, orgId int64) (error, models.ReceiveTreatmentAsses) {
  619. var receiveTreatmentAsses models.ReceiveTreatmentAsses
  620. err := readDb.Model(&models.ReceiveTreatmentAsses{}).Where("patient_id = ? AND status = 1 AND user_org_id = ?", patient, orgId).Last(&receiveTreatmentAsses).Error
  621. return err, receiveTreatmentAsses
  622. }
  623. 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) {
  624. db := readDb.Model(&models.MonitorDialysisSchedule{})
  625. if start > 0 && end > 0 {
  626. db = db.Preload("DeviceNumber", "org_id = ?", orgID).
  627. Preload("TreatmentMode", "status = 1").
  628. Preload("Prescription", "status = 1 AND user_org_id = ? and record_date>=? and record_date <=?", orgID, start, end).
  629. Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ? and assessment_date >=? and assessment_date <=?", orgID, start, end).
  630. Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ? and assessment_date >=? and assessment_date <=?", orgID, start, end).
  631. Preload("MonitoringRecord", func(db *gorm.DB) *gorm.DB {
  632. return db.Where("status = 1 AND user_org_id = ? and monitoring_date >=? and monitoring_date<=? ", orgID, start, end).Order("operate_time asc")
  633. }).
  634. Preload("DialysisOrder", "status = 1 AND user_org_id = ? and dialysis_date>=? and dialysis_date<=?", orgID, start, end).
  635. Preload("MonitorPatients", "status = 1 AND user_org_id = ?", orgID)
  636. db = db.Where("xt_schedule.status = 1")
  637. }
  638. if schedulDate > 0 {
  639. db = db.Preload("DeviceNumber", "org_id = ?", orgID).
  640. Preload("TreatmentMode", "status = 1").
  641. Preload("Prescription", "status = 1 AND user_org_id = ? and record_date>=? and record_date <=?", orgID, schedulDate, schedulDate).
  642. Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ? and assessment_date >=? and assessment_date <=?", orgID, schedulDate, schedulDate).
  643. Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ? and assessment_date >=? and assessment_date <=?", orgID, schedulDate, schedulDate).
  644. Preload("MonitoringRecord", func(db *gorm.DB) *gorm.DB {
  645. return db.Where("status = 1 AND user_org_id = ? and monitoring_date >=? and monitoring_date<=? ", orgID, schedulDate, schedulDate).Order("operate_time asc")
  646. }).
  647. Preload("DialysisOrder", "status = 1 AND user_org_id = ? and dialysis_date>=? and dialysis_date<=?", orgID, schedulDate, schedulDate).
  648. Preload("MonitorPatients", "status = 1 AND user_org_id = ?", orgID)
  649. db = db.Where("xt_schedule.status = 1")
  650. db = db.Where("schedule_date = ?", schedulDate)
  651. }
  652. if schedulType > 0 {
  653. db = db.Where("schedule_type = ?", schedulType)
  654. }
  655. if start > 0 {
  656. db = db.Where("schedule_date >= ?", start)
  657. }
  658. if end > 0 {
  659. db = db.Where("schedule_date <= ?", end)
  660. }
  661. if orgID > 0 {
  662. db = db.Where("user_org_id = ?", orgID)
  663. }
  664. if partitionType > 0 {
  665. db = db.Joins("inner join xt_device_number on xt_device_number.id = xt_schedule.bed_id and xt_device_number.zone_id = ?", partitionType)
  666. // db = db.Where("partition_id = ?", partitionType)
  667. }
  668. offset := (page - 1) * limit
  669. err = db.Count(&total).Offset(offset).Limit(limit).Order("bed_id desc").Find(&schedule).Error
  670. return schedule, err, total
  671. }
  672. //func GetDialysisWatchByKeyword(orgID int64, keyword string, schedulType int64, partitionType int64, page int64, limit int64) (schedules []*models.DialysisSchedule, err error, total int64) {
  673. // db := readDb.Model(&models.DialysisSchedule{}).Where("status = 1")
  674. //
  675. // if schedulType > 0 {
  676. // db = db.Where("scheudle_type = ?", schedulType)
  677. // }
  678. //
  679. // if partitionType > 0 {
  680. // db = db.Where("partition_id = ?", partitionType)
  681. // }
  682. //
  683. // err = db.Count(&total).Offset(limit * (page - 1)).Limit(limit).Order("schedule_date desc").Find(&schedules).Error
  684. // return schedules, err, total
  685. //}
  686. func GetDialysisWatchByKeyword(orgID int64, keyword string, schedulType int64, partitionType int64, page int64, limit int64, start int64, end int64) ([]*models.DialysisSchedule, error, int64) {
  687. var patients []*models.Patients
  688. 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
  689. if getPatientErr != nil {
  690. return nil, getPatientErr, 0
  691. }
  692. patientIDs := make([]int64, len(patients))
  693. for index, patient := range patients {
  694. patientIDs[index] = patient.ID
  695. }
  696. db := readDb.Model(&models.DialysisSchedule{})
  697. if start > 0 && end > 0 {
  698. db = db.Preload("DeviceNumber", "org_id = ?", orgID).
  699. // Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  700. Preload("TreatmentMode", "status = 1").
  701. Preload("Prescription", "status = 1 AND user_org_id = ? and record_date>=? and record_date<=?", orgID, start, end).
  702. Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ? and assessment_date>=? and assessment_date<=?", orgID, start, end).
  703. Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ? and assessment_date>=? and assessment_date<=?", orgID, start, end).
  704. Preload("MonitoringRecord", "status = 1 AND user_org_id = ? and monitoring_date>=? and monitoring_date<=?", orgID, start, end).
  705. Preload("DialysisOrder", "status = 1 AND user_org_id = ? and dialysis_date >= ? and dialysis_date<=?", orgID, start, end).
  706. Preload("MonitorPatients", "status = 1 AND user_org_id = ?", orgID)
  707. } else {
  708. db = db.Preload("DeviceNumber", "org_id = ?", orgID).
  709. // Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  710. Preload("TreatmentMode", "status = 1").
  711. Preload("Prescription", "status = 1 AND user_org_id = ?", orgID).
  712. Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ?", orgID).
  713. Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ?", orgID).
  714. Preload("MonitoringRecord", "status = 1 AND user_org_id = ?", orgID).
  715. Preload("DialysisOrder", "status = 1 AND user_org_id = ?", orgID).
  716. Preload("MonitorPatients", "status = 1 AND user_org_id = ?", orgID)
  717. }
  718. db = db.Where("xt_schedule.status = 1 AND patient_id in (?)", patientIDs)
  719. if schedulType > 0 {
  720. db = db.Where("schedule_type = ?", schedulType)
  721. }
  722. if start > 0 {
  723. db = db.Where("schedule_date >= ?", start)
  724. }
  725. if end > 0 {
  726. db = db.Where("schedule_date<=?", end)
  727. }
  728. if partitionType > 0 {
  729. db = db.Joins("inner join xt_device_number as d_n on d_n.id = xt_schedule.bed_id and d_n.zone_id = ?", partitionType)
  730. // db = db.Where("partition_id = ?", partitionType)
  731. }
  732. var schedules []*models.DialysisSchedule
  733. total := int64(0)
  734. err := db.Count(&total).Offset(limit * (page - 1)).Limit(limit).Order("schedule_date desc").Find(&schedules).Error
  735. return schedules, err, total
  736. }
  737. func GetAdminUserES(orgID int64, appID int64, ids []int64) (es []*models.AdminUserElectronicSignature, err error) {
  738. err = readUserDb.Model(&models.AdminUserElectronicSignature{}).Where("org_id=? and app_id=? and creator in (?) and status=1", orgID, appID, ids).Find(&es).Error
  739. return
  740. }
  741. //
  742. //func GetAdminUserEsOne(orgid int64) (admin []*models.UserAdminRoles, err error) {
  743. //
  744. // db := readUserDb.Table("sgj_user_admin_role as x").Where("x.status = 1")
  745. // table := readUserDb.Table("sgj_user_admin_electronic_signature as s")
  746. // fmt.Println("table", table)
  747. // 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
  748. // return admin, err
  749. //}
  750. func GetAdminUserEsOne(orgid int64) (admin []*models.UserAdminRoles, err error) {
  751. db := readUserDb.Table("sgj_user_admin_role as x").Where("x.status =1")
  752. table := readUserDb.Table("sgj_user_admin_electronic_signature as s")
  753. fmt.Println("table", table)
  754. 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
  755. return admin, err
  756. }
  757. func UpDateDialysisPrescriptionDoctorSign(patient int64, date int64, orgId int64, doctor_id int64) (err error) {
  758. 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
  759. return
  760. }
  761. func GetAllAdminUserES(orgID int64, appId int64) (es []*models.AdminUserElectronicSignature, err error) {
  762. err = readUserDb.Model(&models.AdminUserElectronicSignature{}).Where("org_id=? and app_id = ? and status=1", orgID, appId).Find(&es).Error
  763. return
  764. }
  765. func GetAllStarfEs(orgid int64) (es []*models.SgjUserAdminRoles, err error) {
  766. redis := RedisClient()
  767. defer redis.Close()
  768. // cur_date := time.Now().Format("2006-01-02")
  769. key := strconv.FormatInt(orgid, 10) + ":starfes"
  770. starfes_str, _ := redis.Get(key).Result()
  771. if len(starfes_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  772. db := readUserDb.Table("sgj_user_admin_role as x").Where("x.status = 1")
  773. 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
  774. if err != nil {
  775. if err == gorm.ErrRecordNotFound {
  776. if len(es) <= 0 {
  777. redis.Set(key, "starfes_str_null", time.Second*60*60*18)
  778. }
  779. return nil, nil
  780. } else {
  781. return nil, err
  782. }
  783. } else {
  784. if len(es) > 0 {
  785. //缓存数据
  786. starfes_str, err := json.Marshal(es)
  787. if err == nil {
  788. redis.Set(key, starfes_str, time.Second*60*60*18)
  789. return es, nil
  790. }
  791. } else {
  792. redis.Set(key, "starfes_str_null", time.Second*60*60*18)
  793. return es, nil
  794. }
  795. return es, nil
  796. }
  797. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  798. if starfes_str == "starfes_str_null" {
  799. return es, nil
  800. } else {
  801. json.Unmarshal([]byte(starfes_str), &es)
  802. return es, nil
  803. }
  804. }
  805. }
  806. func FindLastMonitorRecord(patient_id int64, org_id int64) (record models.MonitoringRecord, err error) {
  807. err = readDb.Model(&models.MonitoringRecord{}).Where("patient_id = ? AND status = 1 AND user_org_id = ?", patient_id, org_id).Last(&record).Error
  808. return
  809. }
  810. func FindLastMonitorRecordToday(patient_id int64, org_id int64, monitoring_date int64) (record models.MonitoringRecord, err error) {
  811. 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
  812. return
  813. }
  814. func FindFirstMonitorRecordToday(patient_id int64, org_id int64, monitoring_date int64) (record models.MonitoringRecord, err error) {
  815. 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
  816. return
  817. }
  818. func ExceDoctorAdviceByGroupNo(m *models.DoctorAdvice, groupNo int64, org_id int64) (err error) {
  819. ut := writeDb.Begin()
  820. 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
  821. if err != nil {
  822. ut.Rollback()
  823. return
  824. }
  825. ut.Commit()
  826. return err
  827. }
  828. func GetExceDoctorAdviceGroupNo(groupNo int64, org_id int64) (advice []*models.DoctorAdvice, err error) {
  829. err = XTReadDB().Where("groupno = ? AND user_org_id = ? AND status = 1", groupNo, org_id).Find(&advice).Error
  830. return advice, err
  831. }
  832. func ExceOldDoctorAdvice(m *models.DoctorAdvice, advice_id int64) (err error) {
  833. ut := writeDb.Begin()
  834. 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
  835. if err != nil {
  836. ut.Rollback()
  837. return
  838. }
  839. 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
  840. if err != nil {
  841. ut.Rollback()
  842. return
  843. }
  844. ut.Commit()
  845. return err
  846. }
  847. func CheckDoctorAdviceByGroupNo(m *models.DoctorAdvice, groupNo int64, org_id int64) (err error) {
  848. ut := writeDb.Begin()
  849. 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
  850. if err != nil {
  851. ut.Rollback()
  852. return
  853. }
  854. ut.Commit()
  855. return err
  856. }
  857. func CheckOldDoctorAdvice(m *models.DoctorAdvice, advice_id int64) (err error) {
  858. ut := writeDb.Begin()
  859. 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
  860. if err != nil {
  861. ut.Rollback()
  862. return
  863. }
  864. 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
  865. if err != nil {
  866. ut.Rollback()
  867. return
  868. }
  869. ut.Commit()
  870. return err
  871. }
  872. func DeleteDoctorAdviceByGroupNo(m *models.DoctorAdvice) (err error) {
  873. ut := writeDb.Begin()
  874. 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
  875. if err != nil {
  876. ut.Rollback()
  877. return
  878. }
  879. ut.Commit()
  880. return
  881. }
  882. func GetDialysisBoard(orgID int64, scheduleDate int64, offset, limit int64) ([]*models.DialysisBoardVM, error) {
  883. var boards []*models.DialysisBoardVM
  884. db := readDb.
  885. Table("xt_schedule").
  886. Preload("Patient", "status = 1 AND user_org_id = ?", orgID).
  887. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  888. Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  889. Preload("DialysisOrder", "status = 1 AND user_org_id = ?", orgID).
  890. Preload("SigninAndWeigh", "status = 1 AND user_org_id = ?", orgID).
  891. Preload("NextSchedules", func(db *gorm.DB) *gorm.DB {
  892. return db.Where("user_org_id = ? AND schedule_date > ? AND status = 1", orgID, scheduleDate).
  893. Order("schedule_date asc, id asc")
  894. }).
  895. Where("status = 1 AND user_org_id = ?", orgID)
  896. if scheduleDate != 0 {
  897. db = db.Where("schedule_date = ?", scheduleDate)
  898. }
  899. err := db.Offset(offset).Limit(limit).Find(&boards).Error
  900. return boards, err
  901. }
  902. func GetOneDialysisOrder(orgID, orderData int64, patient_id int64) (*models.DialysisOrder, error) {
  903. var order models.DialysisOrder
  904. var err error
  905. 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
  906. if err == gorm.ErrRecordNotFound {
  907. return nil, nil
  908. }
  909. if err != nil {
  910. return nil, err
  911. }
  912. return &order, nil
  913. }
  914. func GetOrgInfoTemplate(orgID int64) (models.GobalTemplate, error) {
  915. var templateInfo models.GobalTemplate
  916. var err error
  917. err = readDb.Model(&models.GobalTemplate{}).Where("org_id=? and status=1", orgID).First(&templateInfo).Error
  918. return templateInfo, err
  919. }
  920. func FindAdminRoleTypeById(orgId int64, adminUserId int64, appId int64) (models.App_Role, error) {
  921. var appRole models.App_Role
  922. var err error
  923. 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
  924. return appRole, err
  925. }
  926. func BatchExceOldDoctorAdvice(m *models.DoctorAdvice, ids []string) (err error) {
  927. ut := writeDb.Begin()
  928. 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
  929. if err != nil {
  930. ut.Rollback()
  931. return
  932. }
  933. 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
  934. if err != nil {
  935. ut.Rollback()
  936. return
  937. }
  938. ut.Commit()
  939. return err
  940. }
  941. func BatchCheckOldDoctorAdvice(m *models.DoctorAdvice, ids []string, org_id int64, date int64) (err error) {
  942. ut := writeDb.Begin()
  943. 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
  944. if err != nil {
  945. ut.Rollback()
  946. return
  947. }
  948. 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
  949. if err != nil {
  950. ut.Rollback()
  951. return
  952. }
  953. ut.Commit()
  954. return err
  955. }
  956. func ModifyScheduleMode(mode_id int64, patient_id int64, recordDate int64, org_id int64) {
  957. ut := writeDb.Begin()
  958. 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
  959. if err != nil {
  960. ut.Rollback()
  961. return
  962. }
  963. ut.Commit()
  964. }
  965. func FindDialysisOrderById(id int64) (error, *models.DialysisOrder) {
  966. dialysisRecord := models.DialysisOrder{}
  967. err := readDb.Model(&models.DialysisOrder{}).Where("id = ? AND status = 1", id).Find(&dialysisRecord).Error
  968. return err, &dialysisRecord
  969. }
  970. func ModifyExceDoctorAdviceByGroupNo(m *models.DoctorAdvice, groupNo int64, org_id int64) (err error) {
  971. ut := writeDb.Begin()
  972. 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
  973. if err != nil {
  974. ut.Rollback()
  975. return
  976. }
  977. ut.Commit()
  978. return err
  979. }
  980. func BatchModifyExceOldDoctorAdvice(m *models.DoctorAdvice, ids []string) (err error) {
  981. ut := writeDb.Begin()
  982. 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
  983. if err != nil {
  984. ut.Rollback()
  985. return
  986. }
  987. 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
  988. if err != nil {
  989. ut.Rollback()
  990. return
  991. }
  992. ut.Commit()
  993. return err
  994. }
  995. func UpdateOrderCount(good_type_id int64, good_id int64, number string, org_id int64) {
  996. 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))
  997. }
  998. func UpdateOrderInfoDetails(good_type_id int64, good_id int64, time int64, org_id int64, patient_id int64, newNiprocart int64, out *models.WarehouseOutInfo) {
  999. 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})
  1000. }
  1001. func FindOrderInfoByGoodId(good_type_id int64, good_id int64, number string, org_id int64) (err error, out models.WarehouseOutInfo) {
  1002. 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
  1003. return
  1004. }
  1005. func FindGoodUserById(good_type_id int64, good_id int64, id int64, org_id int64, record_time int64) (user models.AutomaticReduceDetail, err error) {
  1006. 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
  1007. return
  1008. }
  1009. func FindOldOrderInfoByGoodId(time int64, org_id int64) (err error, out models.WarehouseOutInfo) {
  1010. err = readDb.Model(&models.WarehouseOutInfo{}).Where(" org_id = ? AND status = 1 AND sys_record_time = ? ", org_id, time).First(&out).Error
  1011. return
  1012. }
  1013. func FindStockOutOrderNumberByTime(time int64, org_id int64) (err error, out models.WarehouseOut) {
  1014. err = readDb.Model(&models.WarehouseOut{}).Where(" org_id = ? AND status = 1 AND warehouse_out_time = ? ", org_id, time).First(&out).Error
  1015. return
  1016. }
  1017. func UpdateDetails(good_type_id int64, good_id int64, time int64, org_id int64, patient_id int64, newNiprocart int64, out *models.WarehouseOutInfo) {
  1018. 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})
  1019. }
  1020. func FindPatientIsOpenRemindById(patient_id int64, org_id int64) (patients models.Patients, err error) {
  1021. err = readDb.Model(&models.Patients{}).Where("status = 1 AND id = ? AND user_org_id = ?", patient_id, org_id).First(&patients).Error
  1022. return
  1023. }
  1024. func GetMaxLongAdviceGroupID(orgId int64, patient_id int64) (group int64) {
  1025. var advice models.DoctorAdvice
  1026. 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
  1027. if err != nil {
  1028. fmt.Println(err)
  1029. group = 0
  1030. }
  1031. group = advice.GroupNo
  1032. return
  1033. }
  1034. func GetLastLongAdviceByGroupNo(orgId int64, patient_id int64, advice_date int64) (advice []*models.DoctorAdvice, err error) {
  1035. 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 and (push_start_time >=? or push_start_time = 0) ", orgId, patient_id, advice_date).Preload("ChildDoctorAdvice", "status = 1 and (push_start_time >=? or push_start_time = 0)", advice_date).Find(&advice).Error
  1036. return
  1037. }
  1038. func GetLastLongAdviceByGroupNoOne(orgId int64, patient_id int64, advice_date int64) (advice []*models.DoctorAdvice, err error) {
  1039. redis := RedisClient()
  1040. defer redis.Close()
  1041. key := strconv.FormatInt(orgId, 10) + ":" + strconv.FormatInt(patient_id, 10) + ":long_advice"
  1042. long_advice_str, _ := redis.Get(key).Result()
  1043. if len(long_advice_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1044. 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 and (push_start_time >=? or push_start_time = 0) ", orgId, patient_id, advice_date).Preload("ChildDoctorAdvice", "status = 1 and (push_start_time >=? or push_start_time = 0) ", advice_date).Find(&advice).Error
  1045. if err != nil {
  1046. if err == gorm.ErrRecordNotFound {
  1047. return nil, nil
  1048. } else {
  1049. return nil, err
  1050. }
  1051. } else {
  1052. if len(advice) > 0 {
  1053. //缓存数据
  1054. starfes_str, err := json.Marshal(advice)
  1055. if err == nil {
  1056. redis.Set(key, starfes_str, time.Second*60*60*18)
  1057. return advice, nil
  1058. }
  1059. } else {
  1060. redis.Set(key, "null", time.Second*60*60*18)
  1061. return advice, nil
  1062. }
  1063. return advice, nil
  1064. }
  1065. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  1066. if long_advice_str == "null" {
  1067. return advice, nil
  1068. } else {
  1069. json.Unmarshal([]byte(long_advice_str), &advice)
  1070. return advice, nil
  1071. }
  1072. }
  1073. 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 and (push_start_time >=? or push_start_time = 0) ", orgId, patient_id, advice_date).Preload("ChildDoctorAdvice", "status = 1 and (push_start_time >=? or push_start_time = 0) ", advice_date).Find(&advice).Error
  1074. return
  1075. }
  1076. func GetLastLongAdviceByGroupNoThree(orgId int64, patient_id int64, advice_date int64) (advice []*models.DoctorAdvice, err error) {
  1077. 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 and (push_start_time>=? or push_start_time = 0) ", orgId, patient_id, advice_date).Preload("ChildDoctorAdvice", "status = 1 and (push_start_time>=? or push_start_time = 0) ", advice_date).Find(&advice).Error
  1078. return
  1079. }
  1080. func GetLastLongAdviceByGroupNoFour(orgId int64, patient_id int64, advice_date int64) (advice []*models.DoctorAdvice, err error) {
  1081. //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
  1082. //return
  1083. redis := RedisClient()
  1084. defer redis.Close()
  1085. key := strconv.FormatInt(orgId, 10) + ":" + strconv.FormatInt(patient_id, 10) + ":long_advice"
  1086. long_advice_str, _ := redis.Get(key).Result()
  1087. if len(long_advice_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1088. 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 and (push_start_time >=? or push_start_time = 0)", orgId, patient_id, advice_date).Preload("ChildDoctorAdvice", "status = 1 and (push_start_time >=? or push_start_time = 0)", advice_date).Find(&advice).Error
  1089. if err != nil {
  1090. if err == gorm.ErrRecordNotFound {
  1091. return nil, nil
  1092. } else {
  1093. return nil, err
  1094. }
  1095. } else {
  1096. if len(advice) > 0 {
  1097. //缓存数据
  1098. starfes_str, err := json.Marshal(advice)
  1099. if err == nil {
  1100. redis.Set(key, starfes_str, time.Second*60*60*18)
  1101. return advice, nil
  1102. }
  1103. } else {
  1104. redis.Set(key, "null", time.Second*60*60*18)
  1105. return advice, nil
  1106. }
  1107. return advice, nil
  1108. }
  1109. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  1110. if long_advice_str == "null" {
  1111. return advice, nil
  1112. } else {
  1113. json.Unmarshal([]byte(long_advice_str), &advice)
  1114. return advice, nil
  1115. }
  1116. }
  1117. 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 and (push_start_time >=? or push_start_time = 0)", orgId, patient_id, advice_date).Preload("ChildDoctorAdvice", "status = 1 and (push_start_time >=? or push_start_time = 0)", advice_date).Find(&advice).Error
  1118. return
  1119. }
  1120. func FindAllDoctorAdviceByTime(now int64, end int64, patient_id int64, orgId int64, temp_id string) (advice []*models.DoctorAdvice, err error) {
  1121. 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
  1122. return
  1123. }
  1124. func UpdateScheduleModeId(patient_id int64, org_id int64, date int64, mode_id int64) (err error) {
  1125. 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
  1126. return
  1127. }
  1128. func UpdateAssessmentAfterDate(patient_id int64, org_id int64, date int64, actual_treatment_hour int64, actual_treatment_minute int64) (err error) {
  1129. 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
  1130. return
  1131. }
  1132. func UpdateAssessmentAfterDateOne(patient_id int64, org_id int64, date int64, actual_treatment_hour int64, actual_treatment_minute int64, BreathingRate string) (err error) {
  1133. 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
  1134. return
  1135. }
  1136. func CreateDialysisBeforePrepare(beforePrepares []*models.DialysisBeforePrepare) (err error) {
  1137. if len(beforePrepares) > 0 {
  1138. utx := writeDb.Begin()
  1139. if len(beforePrepares) > 0 {
  1140. 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 "
  1141. insertParams := make([]string, 0)
  1142. insertData := make([]interface{}, 0)
  1143. for _, prepare := range beforePrepares {
  1144. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?)")
  1145. insertData = append(insertData, prepare.UserOrgId)
  1146. insertData = append(insertData, prepare.PatientId)
  1147. insertData = append(insertData, prepare.RecordDate)
  1148. insertData = append(insertData, prepare.GoodId)
  1149. insertData = append(insertData, prepare.GoodTypeId)
  1150. insertData = append(insertData, prepare.Count)
  1151. insertData = append(insertData, prepare.Ctime)
  1152. insertData = append(insertData, prepare.Mtime)
  1153. insertData = append(insertData, prepare.Creater)
  1154. insertData = append(insertData, prepare.Modifier)
  1155. insertData = append(insertData, 1)
  1156. insertData = append(insertData, prepare.CommdityCode)
  1157. }
  1158. thisSQL += strings.Join(insertParams, ", ")
  1159. err = utx.Exec(thisSQL, insertData...).Error
  1160. if err != nil {
  1161. utx.Rollback()
  1162. return
  1163. }
  1164. }
  1165. utx.Commit()
  1166. }
  1167. return
  1168. }
  1169. func UpdateDialysisBeforePrepare(info *models.DialysisBeforePrepare) (err error) {
  1170. err = writeDb.Save(&info).Error
  1171. return err
  1172. }
  1173. func UpdateDialysisBeforePrepareTwo(info *models.DialysisBeforePrepare) (err error) {
  1174. err = writeDb.Model(&models.DialysisBeforePrepare{}).
  1175. 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).
  1176. Updates(map[string]interface{}{"count": info.Count, "updated_time": time.Now().Unix(), "modifier": info.Modifier, "commdity_code": info.CommdityCode, "status": 1}).Error
  1177. return err
  1178. }
  1179. func UpdateAutoReduceDetailSix(detail *models.AutomaticReduceDetail) (err error) {
  1180. 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
  1181. return err
  1182. }
  1183. func UpdateOrderCountSub(good_type_id int64, good_id int64, number string, org_id int64, count int64) {
  1184. 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))
  1185. }
  1186. func UpdateOrderCountAdd(good_type_id int64, good_id int64, number string, org_id int64, count int64) {
  1187. 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))
  1188. }
  1189. func UpdateUserInfoDetails(good_type_id int64, good_id int64, time int64, org_id int64, patient_id int64, count int64, out *models.WarehouseOutInfo) {
  1190. 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})
  1191. //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)
  1192. }
  1193. func UpdateDrugUserInfoDetails(drug_id int64, time int64, org_id int64, patient_id int64, count int64, out *models.DrugWarehouseOutInfo) {
  1194. 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})
  1195. //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)
  1196. }
  1197. func FindDialysisBeforePrepare(patient_id int64, good_id int64, good_type_id int64, user_org_id int64, record_time int64) (count int64) {
  1198. 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)
  1199. return
  1200. }
  1201. func GetExecutionDoctors(orgid int64, patientid int64, id int64) (doctorAdvice []*models.DoctorAdvice, err error) {
  1202. 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
  1203. return doctorAdvice, err
  1204. }
  1205. func GetHisExecutionDoctors(orgid int64, patientid int64, id int64) (doctorAdvice []*models.HisDoctorAdviceInfo, err error) {
  1206. 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
  1207. return doctorAdvice, err
  1208. }
  1209. func GetHisExecutionDoctorsTwo(orgid int64, patientid int64, id int64) (doctorAdvice []*models.HisDoctorAdviceInfo, err error) {
  1210. err = readDb.Model(&models.HisDoctorAdviceInfo{}).Where("user_org_id = ? and patient_id = ? AND id = ? and status = 1 and execution_state = 1", orgid, patientid, id).Find(&doctorAdvice).Error
  1211. return doctorAdvice, err
  1212. }
  1213. func FindHisDoctorAdviceById(orgID int64, id int64) (advice models.HisDoctorAdviceInfo, err error) {
  1214. err = readDb.Model(&models.HisDoctorAdviceInfo{}).Where("user_org_id=? and status=1 and id = ?", orgID, id).First(&advice).Error
  1215. return
  1216. }
  1217. func FindHisDoctorAdviceByIds(orgID int64, ids []string) (advice []*models.HisDoctorAdviceInfo, err error) {
  1218. err = readDb.Model(&models.HisDoctorAdviceInfo{}).Where("user_org_id=? and status=1 and id in (?)", orgID, ids).Find(&advice).Error
  1219. return
  1220. }
  1221. func SaveHisDoctorAdvice(advice *models.HisDoctorAdviceInfo) (err error) {
  1222. err = writeDb.Save(&advice).Error
  1223. return
  1224. }
  1225. func GetGoodInfoMation(orgid int64) (goodinfo []*models.GoodInfo, err error) {
  1226. 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
  1227. //err = XTReadDB().Model(&goodinfo).Where("org_id = ? and status = 1 AND find_in_set('停用',good_status) = 0", orgid).Find(&goodinfo).Error
  1228. return goodinfo, err
  1229. }
  1230. func GetGoodInfoMationSix(orgid int64, storehouse_id int64) (goodinfo []*models.GoodInfo, err error) {
  1231. 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 and storehouse_id = ?", storehouse_id).Find(&goodinfo).Error
  1232. return goodinfo, err
  1233. }
  1234. func GetGoodInfoMationSeven(orgid int64, storehouse_id int64) (goodinfo []*models.GoodInfo, err error) {
  1235. err = XTReadDB().Model(&goodinfo).Where("org_id = ? and status = 1 AND find_in_set('停用',good_status) = 0", orgid).Preload("GoodSotckInfo", "status = 1 and storehouse_id = ?", storehouse_id).Find(&goodinfo).Error
  1236. return goodinfo, err
  1237. }
  1238. func GetDialysisBeforePrepare(goodTypeId int64, goodId int64, orgid int64, patientId int64) (*models.DialysisBeforePrepare, error) {
  1239. prepare := models.DialysisBeforePrepare{}
  1240. 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
  1241. if err == gorm.ErrRecordNotFound {
  1242. return nil, err
  1243. }
  1244. if err != nil {
  1245. return nil, err
  1246. }
  1247. return &prepare, nil
  1248. }
  1249. func CreateDialysisBeforePrepareOne(prepare *models.DialysisBeforePrepare) error {
  1250. err := XTReadDB().Create(&prepare).Error
  1251. return err
  1252. }
  1253. func ModifyExceDoctorAdviceById(m *models.HisDoctorAdviceInfo, ids []string) (err error) {
  1254. ut := writeDb.Begin()
  1255. 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
  1256. if err != nil {
  1257. ut.Rollback()
  1258. return
  1259. }
  1260. 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
  1261. if err != nil {
  1262. ut.Rollback()
  1263. return
  1264. }
  1265. ut.Commit()
  1266. return err
  1267. }
  1268. func FindHisDoctorAdviceByIdOne(orgID int64, ids []string) (advice []models.HisDoctorAdviceInfo, err error) {
  1269. err = readDb.Model(&models.HisDoctorAdviceInfo{}).Where("id IN (?) AND user_org_id = ? AND status = 1", ids, orgID).Find(&advice).Error
  1270. return
  1271. }
  1272. func ModifyHisPrescriptionProject(m *models.HisPrescriptionProject, ids []string) (err error) {
  1273. ut := writeDb.Begin()
  1274. 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
  1275. if err != nil {
  1276. ut.Rollback()
  1277. return
  1278. }
  1279. ut.Commit()
  1280. return err
  1281. }
  1282. func FindHisProjectById(orgID int64, id int64) (advice models.HisPrescriptionProject, err error) {
  1283. err = readDb.Model(&models.HisPrescriptionProject{}).Where("user_org_id=? and status=1 and id = ?", orgID, id).First(&advice).Error
  1284. return
  1285. }
  1286. func SaveHisProject(advice *models.HisPrescriptionProject) (err error) {
  1287. err = writeDb.Save(&advice).Error
  1288. return
  1289. }
  1290. func FindAllHisProjectById(orgID int64, patient_id int64, record_time int64) (advice []*models.HisPrescriptionProject, err error) {
  1291. 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
  1292. return
  1293. }
  1294. func GetAllPcPatientListByListSeven(orgID int64) (patients []*MSchedualPatientVMList, err error) {
  1295. err = readDb.Where("user_org_id=? and status=1 and lapseto = 1", orgID).Find(&patients).Error
  1296. return patients, err
  1297. }
  1298. func GetAllPcPatientListByListEight(orgID int64) (patients []*MSchedualPatientVMList, err error) {
  1299. err = readDb.Where("user_org_id=? and status=1", orgID).Find(&patients).Error
  1300. return patients, err
  1301. }
  1302. func GetAllPcPatientListByList(orgID int64) (patients []*MSchedualPatientVMList, err error) {
  1303. redis := RedisClient()
  1304. defer redis.Close()
  1305. key := strconv.FormatInt(orgID, 10) + ":" + ":patient_list_all"
  1306. patient_info_str, _ := redis.Get(key).Result()
  1307. if len(patient_info_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1308. err = readDb.Where("user_org_id=? and status=1 and lapseto = 1", orgID).Find(&patients).Error
  1309. if err != nil {
  1310. if err == gorm.ErrRecordNotFound {
  1311. if len(patients) <= 0 {
  1312. redis.Set(key, "null", time.Second*60*60*18)
  1313. }
  1314. return nil, nil
  1315. } else {
  1316. return nil, err
  1317. }
  1318. } else {
  1319. if len(patients) > 0 {
  1320. //缓存数据
  1321. patient_info_json, err := json.Marshal(&patients)
  1322. if err == nil {
  1323. redis.Set(key, patient_info_json, time.Second*60*60*18)
  1324. }
  1325. } else {
  1326. redis.Set(key, "null", time.Second*60*60*18)
  1327. }
  1328. return patients, nil
  1329. }
  1330. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  1331. if patient_info_str == "null" {
  1332. return nil, nil
  1333. } else {
  1334. json.Unmarshal([]byte(patient_info_str), &patients)
  1335. return patients, nil
  1336. }
  1337. }
  1338. }
  1339. func GetAllPcAdvicestByList(orgID int64, scheduleDate int64) (advices []*VMDoctorAdvice, err error) {
  1340. redis := RedisClient()
  1341. defer redis.Close()
  1342. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":advice_list_all"
  1343. advice_list_str, _ := redis.Get(key).Result()
  1344. if len(advice_list_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1345. err = readDb.Where("status = 1 AND user_org_id = ? AND advice_type = 2 AND advice_date = ? ", orgID, scheduleDate).Find(&advices).Error
  1346. if err != nil {
  1347. if err == gorm.ErrRecordNotFound {
  1348. if len(advices) <= 0 {
  1349. redis.Set(key, "null", time.Second*60*60*18)
  1350. }
  1351. return nil, nil
  1352. } else {
  1353. return nil, err
  1354. }
  1355. } else {
  1356. if len(advices) > 0 {
  1357. //缓存数据
  1358. advice_list_str, err := json.Marshal(&advices)
  1359. if err == nil {
  1360. redis.Set(key, advice_list_str, time.Second*60*60*18)
  1361. }
  1362. } else {
  1363. redis.Set(key, "null", time.Second*60*60*18)
  1364. }
  1365. return advices, nil
  1366. }
  1367. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  1368. if advice_list_str == "null" {
  1369. return nil, nil
  1370. } else {
  1371. json.Unmarshal([]byte(advice_list_str), &advices)
  1372. return advices, nil
  1373. }
  1374. }
  1375. }
  1376. func GetAllPcDialysisOrdersByList(orgID int64, scheduleDate int64) (dialysisOrders []*MDialysisOrderVMList, err error) {
  1377. redis := RedisClient()
  1378. defer redis.Close()
  1379. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":dialysis_orders_list_all"
  1380. dialysis_orders_list_all, _ := redis.Get(key).Result()
  1381. if len(dialysis_orders_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1382. 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
  1383. if err != nil {
  1384. if err == gorm.ErrRecordNotFound {
  1385. return nil, nil
  1386. } else {
  1387. return nil, err
  1388. }
  1389. } else {
  1390. if len(dialysisOrders) > 0 {
  1391. //缓存数据
  1392. dialysis_orders_list_all, err := json.Marshal(&dialysisOrders)
  1393. if err == nil {
  1394. redis.Set(key, dialysis_orders_list_all, time.Second*60*60*18)
  1395. }
  1396. } else {
  1397. redis.Set(key, "null", time.Second*60*60*18)
  1398. }
  1399. return dialysisOrders, nil
  1400. }
  1401. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  1402. if dialysis_orders_list_all == "null" {
  1403. return nil, nil
  1404. } else {
  1405. json.Unmarshal([]byte(dialysis_orders_list_all), &dialysisOrders)
  1406. return dialysisOrders, nil
  1407. }
  1408. }
  1409. }
  1410. func GetAllPcAssessmentAfterDislysisByList(orgID int64, scheduleDate int64) (assessmentAfterDislysis []*AssessmentAfterDislysis, err error) {
  1411. redis := RedisClient()
  1412. defer redis.Close()
  1413. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":assessment_after_dislysis_list_all"
  1414. assessment_after_dislysis__all, _ := redis.Get(key).Result()
  1415. if len(assessment_after_dislysis__all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1416. err = readDb.Where("status = 1 AND user_org_id = ? AND assessment_date = ?", orgID, scheduleDate).Find(&assessmentAfterDislysis).Error
  1417. if err != nil {
  1418. if err == gorm.ErrRecordNotFound {
  1419. if len(assessmentAfterDislysis) <= 0 {
  1420. redis.Set(key, "null", time.Second*60*60*18)
  1421. }
  1422. return nil, nil
  1423. } else {
  1424. return nil, err
  1425. }
  1426. } else {
  1427. if len(assessmentAfterDislysis) > 0 {
  1428. //缓存数据
  1429. assessment_after_dislysis__all, err := json.Marshal(&assessmentAfterDislysis)
  1430. if err == nil {
  1431. redis.Set(key, assessment_after_dislysis__all, time.Second*60*60*18)
  1432. }
  1433. } else {
  1434. redis.Set(key, "null", time.Second*60*60*18)
  1435. }
  1436. return assessmentAfterDislysis, nil
  1437. }
  1438. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  1439. if assessment_after_dislysis__all == "null" {
  1440. return nil, nil
  1441. } else {
  1442. json.Unmarshal([]byte(assessment_after_dislysis__all), &assessmentAfterDislysis)
  1443. return assessmentAfterDislysis, nil
  1444. }
  1445. }
  1446. }
  1447. func GetAllPcTreatmentSummarysByList(orgID int64, scheduleDate int64) (treatmentSummarys []*VMTreatmentSummary, err error) {
  1448. redis := RedisClient()
  1449. defer redis.Close()
  1450. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":treatment_summarys_list_all"
  1451. treatment_summarys_all, _ := redis.Get(key).Result()
  1452. if len(treatment_summarys_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1453. err = readDb.Where("status = 1 AND user_org_id = ? AND assessment_date = ?", orgID, scheduleDate).Find(&treatmentSummarys).Error
  1454. if err != nil {
  1455. if err == gorm.ErrRecordNotFound {
  1456. if len(treatment_summarys_all) <= 0 {
  1457. redis.Set(key, "null", time.Second*60*60*18)
  1458. }
  1459. return nil, nil
  1460. } else {
  1461. return nil, err
  1462. }
  1463. } else {
  1464. if len(treatmentSummarys) > 0 {
  1465. //缓存数据
  1466. treatment_summarys_all, err := json.Marshal(&treatmentSummarys)
  1467. if err == nil {
  1468. redis.Set(key, treatment_summarys_all, time.Second*60*60*18)
  1469. }
  1470. } else {
  1471. redis.Set(key, "null", time.Second*60*60*18)
  1472. }
  1473. return treatmentSummarys, nil
  1474. }
  1475. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  1476. if treatment_summarys_all == "null" {
  1477. return nil, nil
  1478. } else {
  1479. json.Unmarshal([]byte(treatment_summarys_all), &treatmentSummarys)
  1480. return treatmentSummarys, nil
  1481. }
  1482. }
  1483. }
  1484. func UpdateTeamProjectExceStatus(ids []int64, admin_user_id int64, execution_time int64) (err error) {
  1485. ut := writeDb.Begin()
  1486. db := ut.Model(&models.HisPrescriptionProject{})
  1487. 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
  1488. if err != nil {
  1489. ut.Rollback()
  1490. return
  1491. }
  1492. ut.Commit()
  1493. return err
  1494. }
  1495. func UpdateTeamProjectCheckStatus(ids []int64, patient_id int64, record_date int64, user_org_id int64, admin_user_id int64, check_time int64) (err error) {
  1496. ut := writeDb.Begin()
  1497. 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
  1498. if err != nil {
  1499. ut.Rollback()
  1500. return
  1501. }
  1502. ut.Commit()
  1503. return err
  1504. }
  1505. func GetCheckTeamProject(team_id int64, patient_id int64, record_date int64, user_org_id int64) (ps []*models.HisPrescriptionProject, err error) {
  1506. 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
  1507. return
  1508. }
  1509. func GetAdminUserRoleInformation(orgid int64) (models.SgjOrgFollow, error) {
  1510. follow := models.SgjOrgFollow{}
  1511. db := UserReadDB().Table("sgj_org_follow as f").Where("f.status = 1")
  1512. table := UserReadDB().Table("sgj_admin as a").Where("a.status = 1")
  1513. fmt.Println(table)
  1514. if orgid > 0 {
  1515. db = db.Where("org_id = ?", orgid)
  1516. }
  1517. if orgid == 0 {
  1518. db = db.Where("org_id = ?", 0)
  1519. }
  1520. 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
  1521. return follow, err
  1522. }
  1523. func GetOrgFollowIsExist(orgid int64) (*models.SgjOrgFollowOne, error) {
  1524. follow := models.SgjOrgFollowOne{}
  1525. err := UserReadDB().Where("org_id = ? and status = 1", orgid).Find(&follow).Error
  1526. if err == gorm.ErrRecordNotFound {
  1527. return nil, err
  1528. }
  1529. if err != nil {
  1530. return nil, err
  1531. }
  1532. return &follow, nil
  1533. }
  1534. func UpdatePatientSign(patient_id int64, dialysis_date int64, order models.DialysisOrder, orgid int64) error {
  1535. err := XTWriteDB().Model(&order).Where("patient_id = ? and dialysis_date= ? and status = 1 and user_org_id = ?", patient_id, dialysis_date, orgid).Update(map[string]interface{}{"url": order.Url, "hash": order.Hash}).Error
  1536. return err
  1537. }
  1538. func GetPatientSign(patient_id int64, dialysis_date int64, orgid int64) (models.DialysisOrder, error) {
  1539. order := models.DialysisOrder{}
  1540. err := XTReadDB().Where("patient_id=? and dialysis_date = ? and user_org_id = ? and status = 1", patient_id, dialysis_date, orgid).Find(&order).Error
  1541. return order, err
  1542. }