dialysis_service.go 120KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671
  1. package service
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. "time"
  8. "XT_New/models"
  9. "github.com/jinzhu/gorm"
  10. )
  11. // GetPatientList 返回患者的列表
  12. func GetSchedualPatientList(orgID int64, schedulDate int64, schedulType int64, partitionType int64, keywords string, page int64, limit int64) (schedule []*models.DialysisSchedule, err error) {
  13. var total int64
  14. //if len(keywords) > 0 {
  15. // likeKey := "%" + keywords + "%"
  16. // //db = db.Preload("MonitorPatients", "status = 1 AND user_org_id = ? AND `name` LIKE ? ", orgID, likeKey).Preload("DeviceNumber", "status = 1 AND org_id = ? ", orgID).Preload("PredialysisEvaluation", "status = 1 AND user_org_id = ? AND assessment_date = ? ", orgID, schedulDate).Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ? AND assessment_date = ? ", orgID, schedulDate).Preload("MonitoringRecord", "status = 1 AND user_org_id = ? AND monitoring_date = ?", orgID, schedulDate).Where("status = 1 AND user_org_id = ? ", orgID)
  17. // db = readDb.
  18. // Model(&models.MonitorDialysisSchedule{}).
  19. // Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  20. // Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  21. // Preload("TreatmentMode", "status = 1").
  22. // Preload("Prescription", "status = 1 AND user_org_id = ?", orgID).
  23. // Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ?", orgID).
  24. // Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ?", orgID).
  25. // Preload("MonitoringRecord", "status = 1 AND user_org_id = ?", orgID).
  26. // Preload("DialysisOrder", "status = 1 AND user_org_id = ?", orgID).
  27. // Preload("MonitorPatients", "status = 1 AND user_org_id = ? AND `name` LIKE ?", orgID,likeKey)
  28. //
  29. //
  30. //} else {
  31. //}
  32. db := readDb.
  33. Model(&models.MonitorDialysisSchedule{}).
  34. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  35. // Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  36. Preload("TreatmentMode", "status = 1").
  37. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, schedulDate).
  38. Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ? AND assessment_date = ?", orgID, schedulDate).
  39. Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ? AND assessment_date = ?", orgID, schedulDate).
  40. Preload("MonitoringRecord", "status = 1 AND user_org_id = ? AND monitoring_date = ?", orgID, schedulDate).
  41. Preload("DialysisOrder", "status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, schedulDate).
  42. Preload("MonitorPatients", "status = 1 AND user_org_id = ?", orgID)
  43. db = db.Where("status = 1")
  44. if schedulDate > 0 {
  45. db = db.Where("schedule_date = ?", schedulDate)
  46. }
  47. db = db.Count(&total)
  48. fmt.Println(total)
  49. if schedulType > 0 {
  50. db = db.Where("schedule_type = ?", schedulType)
  51. }
  52. if partitionType > 0 {
  53. db = db.Where("partition_id = ?", partitionType)
  54. }
  55. offset := (page - 1) * limit
  56. errors := db.Offset(offset).Limit(limit).Find(&schedule).Error
  57. return schedule, errors
  58. }
  59. // 用户基本信息
  60. func FindPatientInfoById(orgID int64, id int64, time int64) (patient models.SchedualPatient2, err error) {
  61. err = readDb.Preload("DialysisSchedule", func(db *gorm.DB) *gorm.DB {
  62. return db.Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).Where("patient_id = ? AND user_org_id = ? AND schedule_date = ? ", id, orgID, time)
  63. }).Where("id = ? and user_org_id=? and status=1", id, orgID).First(&patient).Error
  64. return
  65. }
  66. // 透析处方
  67. func FindPatientPrescribeById(orgID int64, patientsId int64, recordDate int64) (patient models.DialysisPrescription, err error) {
  68. err = readDb.Model(&models.DialysisPrescription{}).Where("patient_id = ? and user_org_id=? and status=1 and record_date = ? ", patientsId, orgID, recordDate).First(&patient).Error
  69. return
  70. }
  71. // 透析方案中的透析处方
  72. func FindDialysisSolution(orgID int64, patientsId int64) (patient models.DialysisSolution, err error) {
  73. err = readDb.Model(&models.DialysisSolution{}).Where("patient_id = ? and user_org_id=? and status=1", patientsId, orgID).Last(&patient).Error
  74. return
  75. }
  76. // 透析方案中的透析模式
  77. func FindTreatmentModeById(modeId int64) (mode models.TreatmentMode, err error) {
  78. err = readDb.Model(&models.TreatmentMode{}).Where("id = ? AND status=1", modeId).First(&mode).Error
  79. return
  80. }
  81. // 接诊评估
  82. func FindReceiverTreatmentAccessRecordById(orgID int64, patientsId int64, recordDate int64) (receiveTreatmentAsses models.ReceiveTreatmentAsses, err error) {
  83. err = readDb.Model(&models.ReceiveTreatmentAsses{}).Where("patient_id = ? and user_org_id=? and status=1 and record_date = ?", patientsId, orgID, recordDate).First(&receiveTreatmentAsses).Error
  84. return
  85. }
  86. // 透前评估
  87. func FindPredialysisEvaluationById(orgID int64, patientsId int64, recordDate int64) (patient models.PredialysisEvaluation, err error) {
  88. err = readDb.Model(&models.PredialysisEvaluation{}).Where("patient_id = ? and user_org_id=? and status=1 and assessment_date = ?", patientsId, orgID, recordDate).First(&patient).Error
  89. return
  90. }
  91. func FindPredialysisName(id int64, orgid int64) (models.Dataconfig, error) {
  92. dataconfig := models.Dataconfig{}
  93. err := readDb.Where("value = ? and org_id =? and status = 1", id, orgid).Find(&dataconfig).Error
  94. return dataconfig, err
  95. }
  96. // 临时医嘱
  97. func FindDoctorAdviceById(orgID int64, patientsId int64, recordDate int64) (patient []*models.DoctorAdvice, err error) {
  98. err = readDb.Model(&models.DoctorAdvice{}).Where("patient_id = ? and user_org_id=? and status=1 and record_date = ? AND advice_type = 2", patientsId, orgID, recordDate).Find(&patient).Error
  99. return
  100. }
  101. func FindDoctorAdviceByInfo(orgID int64, patientsId int64, recordDate int64) (advices []*models.XtDoctorAdvice, err error) {
  102. //db := readDb.Table("xt_doctor_advice as a").Where("a.status = ?", 1)
  103. //err = db.Group("a.advice_name").Select("a.id,a.user_org_id,a.patient_id,a.advice_type,a.advice_date,a.start_time,a.advice_name,a.advice_desc,a.reminder_date,a.single_dose,a.single_dose_unit,a.prescribing_number,a.prescribing_number_unit,a.delivery_way,a.execution_frequency,a.advice_doctor,a.status,a.created_time,a.updated_time,a.advice_affirm,a.remark,a.stop_time,a.stop_reason,a.stop_doctor,a.stop_state,a.parent_id,a.execution_time,a.execution_staff,a.execution_state,a.checker,a.record_date,a.dialysis_order_id,a.check_time,a.check_state,a.drug_spec,a.drug_spec_unit,a.groupno,a.remind_type,a.frequency_type,a.day_count,a.week_day,a.template_id,r.user_name,s.user_name").Joins(" left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = a.execution_staff").Joins("left join sgj_users.sgj_user_admin_role as s on s.admin_user_id = a.checker").Where("a.user_org_id = ? and a.patient_id = ? and a.advice_date = ?", orgID, patientsId, recordDate).Find(&advices).Error
  104. //return
  105. err = readDb.Model(&models.DoctorAdvice{}).
  106. Where("patient_id = ? and user_org_id=? and status=1 and record_date = ? and advice_type = 2", patientsId, orgID, recordDate).
  107. Select("id, user_org_id, patient_id, advice_type, advice_date, record_date, start_time, advice_name,advice_desc, reminder_date, drug_spec, drug_spec_unit, single_dose, single_dose_unit, prescribing_number, prescribing_number_unit, delivery_way, execution_frequency, advice_doctor, status, created_time,updated_time, advice_affirm, remark, stop_time, stop_reason, stop_doctor, stop_state, parent_id, execution_time, execution_staff, execution_state, checker, check_state, check_time, groupno, IF(parent_id > 0, parent_id, id) as advice_order").Order("start_time asc, groupno desc, advice_order desc, id asc").
  108. Find(&advices).Error
  109. return
  110. }
  111. func GetTotalDialysisCout(orgid int64, patientid int64) (order []*models.DialysisOrder, total int64, err error) {
  112. err = readDb.Model(&order).Where("user_org_id = ? and patient_id = ? and status = 1", orgid, patientid).Find(&order).Count(&total).Error
  113. return order, total, err
  114. }
  115. // 临时医嘱
  116. func FindDoctorAdviceOrderById(orgID int64, patientsId int64, recordDate int64) (patient []*models.DoctorAdvice, err error) {
  117. err = readDb.Model(&models.DoctorAdvice{}).
  118. Where("patient_id = ? and user_org_id=? and status=1 and record_date = ? and (advice_type = 2 or advice_type = 3)", 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 UpdateAddSigleRecord(id int64, target_ultrafiltration float64) error {
  194. err := XTWriteDB().Model(&models.DialysisPrescription{}).Where("id = ? and status =1", id).Updates(map[string]interface{}{"target_ultrafiltration": target_ultrafiltration, "prescription_water": target_ultrafiltration}).Error
  195. return err
  196. }
  197. func AddSigleSummaryRecord(summary *models.TreatmentSummary) (err error) {
  198. tx := writeDb.Begin()
  199. errors := tx.Model(&models.TreatmentSummary{}).Create(&summary).Error
  200. if err != nil {
  201. tx.Rollback()
  202. }
  203. tx.Commit()
  204. return errors
  205. }
  206. func AddSigleReceiveTreatmentAssesRecord(receiveTreatmentAsses *models.ReceiveTreatmentAsses) (err error) {
  207. tx := writeDb.Begin()
  208. errors := tx.Model(&models.ReceiveTreatmentAsses{}).Create(&receiveTreatmentAsses).Error
  209. if err != nil {
  210. tx.Rollback()
  211. }
  212. tx.Commit()
  213. return errors
  214. }
  215. func AddSigleAssessmentAfterDislysisRecord(assessmentAfterDislysis *models.AssessmentAfterDislysis) (err error) {
  216. tx := writeDb.Begin()
  217. errors := tx.Model(&models.AssessmentAfterDislysis{}).Create(&assessmentAfterDislysis).Error
  218. if err != nil {
  219. tx.Rollback()
  220. }
  221. tx.Commit()
  222. return errors
  223. }
  224. func GetSigleAssessmentAfterDislysisRecord(patient_id int64, assesment_date int64, user_org_id int64) (models.AssessmentAfterDislysis, error) {
  225. afterDislysis := models.AssessmentAfterDislysis{}
  226. err := XTReadDB().Where("patient_id=? and assessment_date = ? and user_org_id = ? and status=1", patient_id, assesment_date, user_org_id).Find(&afterDislysis).Error
  227. return afterDislysis, err
  228. }
  229. func AddSigleAssessmentBeforeDislysisRecord(assessmentBeforeDislysis *models.PredialysisEvaluation) (err error) {
  230. tx := writeDb.Begin()
  231. errors := tx.Model(&models.PredialysisEvaluation{}).Create(&assessmentBeforeDislysis).Error
  232. if err != nil {
  233. tx.Rollback()
  234. }
  235. tx.Commit()
  236. return errors
  237. }
  238. func AddSigleDoubleCheck(check *models.DoubleCheck) (err error) {
  239. tx := writeDb.Begin()
  240. errors := tx.Model(&models.DoubleCheck{}).Create(&check).Error
  241. if err != nil {
  242. tx.Rollback()
  243. }
  244. tx.Commit()
  245. return errors
  246. }
  247. // 修改透析处方
  248. func UpDateDialysisPrescription(dialysisPrescription *models.DialysisPrescription) error {
  249. tx := writeDb.Begin()
  250. err := writeDb.Save(&dialysisPrescription).Error
  251. if err != nil {
  252. tx.Rollback()
  253. }
  254. tx.Commit()
  255. return err
  256. }
  257. // 修改接诊评估
  258. func UpadateReceiveTreatmentAsses(receiveTreatmentAsses *models.ReceiveTreatmentAsses) error {
  259. err := writeDb.Save(receiveTreatmentAsses).Error
  260. return err
  261. }
  262. func GetPredialysisEvaluation(patient_id int64, org_id int64) (models.PredialysisEvaluation, error) {
  263. evaluation := models.PredialysisEvaluation{}
  264. err := XTReadDB().Where("patient_id = ? and user_org_id = ? and status =1 and symptom_before_dialysis!=''", patient_id, org_id).Last(&evaluation).Error
  265. return evaluation, err
  266. }
  267. // 修改透前评估
  268. func UpadatePredialysisEvaluation(predialysisEvaluation *models.PredialysisEvaluation) error {
  269. err := writeDb.Save(&predialysisEvaluation).Error
  270. return err
  271. }
  272. // 修改双人核对
  273. func UpdateDoubleCheck(check *models.DoubleCheck) error {
  274. err := writeDb.Save(&check).Error
  275. return err
  276. }
  277. // 修改透后评估
  278. func UpdateAssessmentAfterDislysisRecord(assessmentAfterDislysis *models.AssessmentAfterDislysis) error {
  279. err := writeDb.Save(&assessmentAfterDislysis).Error
  280. return err
  281. }
  282. func UpdatePredialysisEvaluation(predialysisevaluation *models.PredialysisEvaluation, id int64) error {
  283. err := writeDb.Model(&predialysisevaluation).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"systolic_blood_pressure": predialysisevaluation.SystolicBloodPressure}).Error
  284. return err
  285. }
  286. func UpdatePredialysisEvaluationTwo(predialysisevaluation *models.PredialysisEvaluation, id int64) error {
  287. err := writeDb.Model(&predialysisevaluation).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"diastolic_blood_pressure": predialysisevaluation.DiastolicBloodPressure}).Error
  288. return err
  289. }
  290. func UpdatePredialysisEvaluationThree(predialysisevaluation *models.PredialysisEvaluation, id int64) error {
  291. err := writeDb.Model(&predialysisevaluation).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"pulse_frequency": predialysisevaluation.PulseFrequency}).Error
  292. return err
  293. }
  294. func UpdatePredialysisEvaluationFour(predialysisevaluation *models.PredialysisEvaluation, id int64) error {
  295. err := writeDb.Model(&predialysisevaluation).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"temperature": predialysisevaluation.Temperature}).Error
  296. return err
  297. }
  298. // 修改
  299. func UpdateSummeRecord(treatmentSummary *models.TreatmentSummary) error {
  300. err := writeDb.Save(&treatmentSummary).Error
  301. return err
  302. }
  303. // func UpdateDoctorAdviceById(m *models.DoctorAdvice, id int64, patientId int64) error {
  304. // tx := writeDb.Begin()
  305. // 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
  306. // if err != nil {
  307. // tx.Rollback()
  308. // }
  309. // tx.Commit()
  310. // return err
  311. // }
  312. func ExceDoctorAdviceById(m *models.DoctorAdvice, id int64, patientId int64) error {
  313. 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
  314. return err
  315. }
  316. func ExecBatchDoctorAdvice(patient_id int64, orgid int64, ids []string, creater int64) error {
  317. 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
  318. return err
  319. }
  320. func ExceHidDoctorAdviceById(m *models.HisDoctorAdviceInfo, id int64, patientId int64) error {
  321. 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, "is_mobile": 2}).Error
  322. return err
  323. }
  324. func ExceHisBatchDotorAdvice(patientid int64, orgid int64, ids []string, creater int64) error {
  325. 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
  326. return err
  327. }
  328. func CheckDoctorAdviceById(m *models.DoctorAdvice, id int64, patientId int64) error {
  329. 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
  330. return err
  331. }
  332. func CheckHisBathDoctorAdviceById(patientId int64, orgid int64, creater int64, ids []string) error {
  333. 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
  334. return err
  335. }
  336. func CheckBatchDoctorAdviceById(patientId int64, ids []string, orgid int64, creater int64) error {
  337. 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
  338. return err
  339. }
  340. func CheckHisDoctorAdviceById(m *models.HisDoctorAdviceInfo, id int64, patientId int64) error {
  341. 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
  342. return err
  343. }
  344. func CreateMonitor(m *models.MonitoringRecord) (err error) {
  345. return writeDb.Create(m).Error
  346. }
  347. func UpdateMonitor(m *models.MonitoringRecord) (err error) {
  348. return writeDb.Save(m).Error
  349. }
  350. func DelMonitor(patientId int64, recordId int64) error {
  351. tx := writeDb.Begin()
  352. updateTime := time.Now().Unix()
  353. 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
  354. if err != nil {
  355. tx.Rollback()
  356. }
  357. tx.Commit()
  358. return err
  359. }
  360. //修改长期处方
  361. func UpdateSolution(solution *models.DialysisSolution) {
  362. writeDb.Save(&solution)
  363. }
  364. // 创建长期处方
  365. func CreateSolution(solution *models.DialysisSolution) {
  366. writeDb.Create(&solution)
  367. }
  368. func SavePrescriptionAndCreateSolution(solution *models.DialysisSolution, prescription *models.DialysisPrescription) (err error) {
  369. tx := writeDb.Begin()
  370. if prescription.ID > 0 {
  371. err = tx.Save(&prescription).Error
  372. } else {
  373. err = tx.Model(&models.DialysisPrescription{}).Create(&prescription).Error
  374. }
  375. if err != nil {
  376. tx.Rollback()
  377. return
  378. }
  379. err = tx.Create(&solution).Error
  380. if err != nil {
  381. tx.Rollback()
  382. return
  383. }
  384. tx.Commit()
  385. return
  386. }
  387. // 透析处方
  388. func FindDialysisPrescriptionByReordDate(id int64, recordDate int64, orgId int64) (error, *models.DialysisPrescription) {
  389. dialysisPrescription := models.DialysisPrescription{}
  390. err := readDb.Model(&dialysisPrescription).Where("patient_id = ? AND record_date = ? AND status = 1 AND user_org_id = ? ", id, recordDate, orgId).Find(&dialysisPrescription).Error
  391. return err, &dialysisPrescription
  392. }
  393. func FindReceiveTreatmentAssesByReordDate(id int64, recordDate int64, orgId int64) (error, *models.ReceiveTreatmentAsses) {
  394. receiveTreatmentAsses := models.ReceiveTreatmentAsses{}
  395. err := readDb.Model(&receiveTreatmentAsses).Where("patient_id = ? AND record_date = ? AND status = 1 AND user_org_id = ?", id, recordDate, orgId).Find(&receiveTreatmentAsses).Error
  396. return err, &receiveTreatmentAsses
  397. }
  398. // 透前评估
  399. func FindPredialysisEvaluationByReordDate(id int64, recordDate int64, orgId int64) (error, *models.PredialysisEvaluation) {
  400. dialysisEvaluation := models.PredialysisEvaluation{}
  401. err := readDb.Model(&models.PredialysisEvaluation{}).Where("patient_id = ? AND assessment_date = ? AND status = 1 AND user_org_id = ?", id, recordDate, orgId).First(&dialysisEvaluation).Error
  402. return err, &dialysisEvaluation
  403. }
  404. // 双人核对
  405. func FindDoubleCheckByReordDate(id int64, recordDate int64, orgId int64) (error, *models.DoubleCheck) {
  406. check := models.DoubleCheck{}
  407. err := readDb.Model(&models.DoubleCheck{}).Where("patient_id = ? AND check_date = ? AND status = 1 AND user_org_id = ?", id, recordDate, orgId).First(&check).Error
  408. return err, &check
  409. }
  410. func FindAssessmentAfterDislysisByReordDate(id int64, recordDate int64, orgId int64) (error, *models.AssessmentAfterDislysis) {
  411. assessmentAfterDislysis := models.AssessmentAfterDislysis{}
  412. err := readDb.Model(&models.AssessmentAfterDislysis{}).Where("patient_id = ? AND assessment_date = ? AND status = 1 AND user_org_id = ?", id, recordDate, orgId).First(&assessmentAfterDislysis).Error
  413. return err, &assessmentAfterDislysis
  414. }
  415. func FindTreatmentSummaryByReordDate(id int64, recordDate int64, orgId int64) (error, *models.TreatmentSummary) {
  416. summary := models.TreatmentSummary{}
  417. err := readDb.Model(&models.TreatmentSummary{}).Where("patient_id = ? AND assessment_date = ? AND status = 1 AND user_org_id = ?", id, recordDate, orgId).First(&summary).Error
  418. return err, &summary
  419. }
  420. func FindDoctorAdviceByReordDate(id int64, recordDate int64, orgId int64) (error, *models.DoctorAdvice) {
  421. advice := models.DoctorAdvice{}
  422. 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
  423. return err, &advice
  424. }
  425. func FindDialysisRecordById(orgId int64, id int64, recordDate int64) (error, *models.DialysisOrder) {
  426. var count int64
  427. dialysisRecord := models.DialysisOrder{}
  428. err := readDb.Model(&models.DialysisOrder{}).Where("user_org_id = ? AND patient_id = ? AND dialysis_date = ?", orgId, id, recordDate).Find(&dialysisRecord).Count(&count).Error
  429. if count <= 0 {
  430. return err, nil
  431. }
  432. return err, &dialysisRecord
  433. }
  434. func FindDialysisSchedualById(orgId int64, id int64, recordDate int64) (error, *models.Schedule) {
  435. var count int64
  436. schedule := models.Schedule{}
  437. err := readDb.Model(&models.Schedule{}).Where("user_org_id = ? AND patient_id = ? AND schedule_date = ?", orgId, id, recordDate).Find(&schedule).Count(&count).Error
  438. if count <= 0 {
  439. return err, nil
  440. }
  441. return err, &schedule
  442. }
  443. func CreateDialysisRecord(id int64, orgId int64, record *models.DialysisOrder) error {
  444. err := writeDb.Model(&models.DialysisOrder{}).Where("user_org_id = ? AND patient_id = ? ", orgId, id).Create(&record).Error
  445. if err != nil {
  446. return err
  447. }
  448. dialysisPrescriptionErr := UpDateRecordIdToDialysisPrescription(id, orgId, record.DialysisDate, record.ID)
  449. if dialysisPrescriptionErr != nil {
  450. return dialysisPrescriptionErr
  451. }
  452. receiveTreatmentAssesErr := UpDateRecordIdToReceiveTreatmentAsses(id, orgId, record.DialysisDate, record.ID)
  453. if receiveTreatmentAssesErr != nil {
  454. return receiveTreatmentAssesErr
  455. }
  456. predialysisEvaluationErr := UpDateRecordIdToPredialysisEvaluation(id, orgId, record.DialysisDate, record.ID)
  457. if predialysisEvaluationErr != nil {
  458. return predialysisEvaluationErr
  459. }
  460. treatmentSummaryErr := UpDateRecordIdToTreatmentSummary(id, orgId, record.DialysisDate, record.ID)
  461. if treatmentSummaryErr != nil {
  462. return treatmentSummaryErr
  463. }
  464. doubleCheckErr := UpDateRecordIdToDoubleCheck(id, orgId, record.DialysisDate, record.ID)
  465. if doubleCheckErr != nil {
  466. return doubleCheckErr
  467. }
  468. adviceErrr := UpDateRecordIdToDoctorAdvice(id, orgId, record.DialysisDate, record.ID)
  469. if adviceErrr != nil {
  470. return adviceErrr
  471. }
  472. //透析监测
  473. records, err := FindAllMonitorRecord(orgId, id, record.DialysisDate)
  474. if err != nil {
  475. return err
  476. }
  477. var Ids []int64
  478. for _, item := range records {
  479. Ids = append(Ids, item.ID)
  480. }
  481. monitorErr := UpDateRecordIdToMonitor(Ids, record.ID)
  482. if monitorErr != nil {
  483. return monitorErr
  484. }
  485. return nil
  486. }
  487. func ModifyDialysisRecord(id int64, finishNurse int64, end_time int64, admin_user_id int64, puncture_point_haematoma int64, internal_fistula string, catheter string, cruor string, mission string, condenser string) error {
  488. 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, "mission": mission, "condenser": condenser}).Error
  489. return err
  490. }
  491. //func ModifyDialysisRecord(id int64, finishNurse int64, end_time int64, admin_user_id int64) error {
  492. // 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
  493. // return err
  494. //
  495. //}
  496. func UpdateSolutionByPatientId(id int64) error {
  497. err := writeDb.Model(&models.Patients{}).Where("id = ? AND status = 1 ", id).UpdateColumn("total_dialysis", gorm.Expr("total_dialysis + ?", 1)).Error
  498. return err
  499. }
  500. // 更新透析处方记录ID
  501. func UpDateRecordIdToDialysisPrescription(patientId int64, orgId int64, recordTime int64, recordId int64) error {
  502. 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
  503. return errors
  504. }
  505. func UpDateRecordIdToReceiveTreatmentAsses(patientId int64, orgId int64, recordTime int64, recordId int64) error {
  506. 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
  507. return errors
  508. }
  509. func UpDateRecordIdToPredialysisEvaluation(patientId int64, orgId int64, recordTime int64, recordId int64) error {
  510. 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
  511. return errors
  512. }
  513. func UpDateRecordIdToAssessmentAfterDislysis(patientId int64, orgId int64, recordTime int64, recordId int64) error {
  514. 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
  515. return errors
  516. }
  517. func UpDateRecordIdToTreatmentSummary(patientId int64, orgId int64, recordTime int64, recordId int64) error {
  518. 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
  519. return errors
  520. }
  521. func UpDateRecordIdToMonitor(Ids []int64, recordId int64) error {
  522. errors := writeDb.Model(&models.MonitoringRecord{}).Where("id IN (?)", Ids).Update(map[string]interface{}{"dialysis_order_id": recordId}).Error
  523. return errors
  524. }
  525. func UpDateRecordIdToDoubleCheck(patientId int64, orgId int64, recordTime int64, recordId int64) error {
  526. 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
  527. return errors
  528. }
  529. func UpDateRecordIdToDoctorAdvice(patientId int64, orgId int64, recordTime int64, recordId int64) error {
  530. 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
  531. return errors
  532. }
  533. // 透析处方
  534. func FindPatientPrescribeWidyDevideById(orgID int64, patientsId int64, recordDate int64) (patient models.DialysisPrescriptionAddtional, err error) {
  535. err = readDb.Preload("Device", "status = 1 AND org_id = ?", orgID).
  536. Model(&models.DialysisPrescriptionAddtional{}).
  537. Where("patient_id = ? and user_org_id=? and status=1 and record_date = ? ", patientsId, orgID, recordDate).
  538. First(&patient).Error
  539. return
  540. }
  541. func FindDialysisMode(orgID int64, patientId int64, scheduleDate int64) (models.XtSchedule, error) {
  542. schedule := models.XtSchedule{}
  543. err = readDb.Where("user_org_id = ? and patient_id = ? and schedule_date = ? ", orgID, patientId, scheduleDate).Find(&schedule).Error
  544. return schedule, err
  545. }
  546. func FindDialysisMacher(orgid int64, patientid int64, dialysisDate int64) (models.XtDialysisOrders, error) {
  547. orders := models.XtDialysisOrders{}
  548. db := readDb.Table("xt_dialysis_order as o").Where("o.status = 1")
  549. table := readDb.Table("xt_device_number as n")
  550. fmt.Print("table", table)
  551. err := db.Select("o.id,o.dialysis_date,o.user_org_id,o.patient_id,o.prescription_id,o.stage,o.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,o.quality_nurse_id,o.blood_drawing,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
  552. return orders, err
  553. }
  554. func FindPunctureNurse(adminid int64, appid int64, orgid int64) (models.App_Roles, error) {
  555. role := models.App_Roles{}
  556. //err := UserReadDB().Where("admin_user_id = ? and app_id = ? and org_id = ?", adminid, appid, orgid).Find(&role).Error
  557. //return role, err
  558. db := UserReadDB().Table("sgj_user_admin_role as r")
  559. 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
  560. return role, err
  561. }
  562. func FindeStartNuse(adminid int64, appid int64, orgid int64) (models.App_Roles, error) {
  563. role := models.App_Roles{}
  564. db := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  565. 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
  566. //err := UserReadDB().Where("admin_user_id = ? and app_id = ? and org_id = ?", adminid, appid, orgid).Find(&role).Error
  567. return role, err
  568. }
  569. func FindeQualityNurse(adminid int64, appid int64, orgid int64) (models.App_Roles, error) {
  570. role := models.App_Roles{}
  571. db := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  572. 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
  573. return role, err
  574. }
  575. func FindDialysisWay(orgid int64, patientid int64, recorddate int64) (models.DialysisPrescription, error) {
  576. prescription := models.DialysisPrescription{}
  577. err := readDb.Where("user_org_id = ? and patient_id = ? and record_date = ? and status =1", orgid, patientid, recorddate).Find(&prescription).Error
  578. return prescription, err
  579. }
  580. func FindDoctorOrder(orgid int64, patientid int64, adviceDate int64) (models.XtDoctorAdvice, error) {
  581. advices := models.XtDoctorAdvice{}
  582. db := readDb.Table("xt_doctor_advice as a").Where("a.status = ?", 1)
  583. 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
  584. return advices, err
  585. }
  586. func FindDoctor(adminuserid int64, appid int64, orgid int64) (models.App_Role, error) {
  587. role := models.App_Role{}
  588. err := UserReadDB().Where("admin_user_id = ? and app_id = ? and org_id = ?", adminuserid, appid, orgid).Find(&role).Error
  589. return role, err
  590. }
  591. func FindDoctorName(adminid int64, appid int64, orgid int64) (models.App_Roles, error) {
  592. role := models.App_Roles{}
  593. db := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  594. 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
  595. //err := UserReadDB().Where("admin_user_id = ? and app_id = ? and org_id = ?", adminid, appid, orgid).Find(&role).Error
  596. return role, err
  597. }
  598. // ///////////// 新 18-10-22
  599. func GetDialysisScheduals(orgID int64, scheduleDate int64) ([]*MDialysisScheduleVM, error) {
  600. var vms []*MDialysisScheduleVM
  601. db := readDb.
  602. Table("xt_schedule").
  603. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  604. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  605. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  606. Preload("DialysisOrder", "status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, scheduleDate).
  607. Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ? AND assessment_date = ?", orgID, scheduleDate).
  608. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  609. Preload("TreatmentSummary", "status = 1 AND user_org_id = ? AND assessment_date = ? ", orgID, scheduleDate).
  610. Preload("Advices", "status = 1 AND user_org_id = ? AND advice_date = ? ", orgID, scheduleDate).
  611. Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ? and assessment_date = ?", orgID, scheduleDate).
  612. Where("status = 1 AND user_org_id = ?", orgID)
  613. if scheduleDate != 0 {
  614. db = db.Where("schedule_date = ?", scheduleDate)
  615. }
  616. err := db.Find(&vms).Error
  617. return vms, err
  618. }
  619. func GetDialysisSchedualsOne(orgID int64, scheduleDate int64) ([]*MDialysisScheduleVM, error) {
  620. var vms []*MDialysisScheduleVM
  621. db := readDb.
  622. Table("xt_schedule").
  623. Preload("DeviceNumber", "org_id = ?", orgID).
  624. Preload("DeviceNumber.Zone", "org_id = ?", orgID).
  625. //Preload("DialysisOrder", "status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, scheduleDate).
  626. //Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ? AND assessment_date = ?", orgID, scheduleDate).
  627. //Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  628. //Preload("TreatmentSummary", "status = 1 AND user_org_id = ? AND assessment_date = ? ", orgID, scheduleDate).
  629. //Preload("Advices", "status = 1 AND user_org_id = ? AND advice_date = ? ", orgID, scheduleDate).
  630. //Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ? and assessment_date = ?", orgID, scheduleDate).
  631. Where("status = 1 AND user_org_id = ?", orgID)
  632. if scheduleDate != 0 {
  633. db = db.Where("schedule_date = ?", scheduleDate)
  634. }
  635. err := db.Find(&vms).Error
  636. return vms, err
  637. }
  638. func GetLastAcceptsAssessment(patient int64, orgId int64) (error, models.ReceiveTreatmentAsses) {
  639. var receiveTreatmentAsses models.ReceiveTreatmentAsses
  640. err := readDb.Model(&models.ReceiveTreatmentAsses{}).Where("patient_id = ? AND status = 1 AND user_org_id = ?", patient, orgId).Last(&receiveTreatmentAsses).Error
  641. return err, receiveTreatmentAsses
  642. }
  643. 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) {
  644. db := readDb.Model(&models.MonitorDialysisSchedule{})
  645. if start > 0 && end > 0 {
  646. db = db.Preload("DeviceNumber", "org_id = ?", orgID).
  647. Preload("TreatmentMode", "status = 1").
  648. Preload("MonitoringRecord", func(db *gorm.DB) *gorm.DB {
  649. return db.Where("status = 1 AND user_org_id = ? and monitoring_date >=? and monitoring_date<=? ", orgID, start, end).Order("operate_time asc")
  650. })
  651. db = db.Where("xt_schedule.status = 1")
  652. }
  653. if schedulDate > 0 {
  654. db = db.Preload("DeviceNumber", "org_id = ?", orgID).
  655. Preload("TreatmentMode", "status = 1").
  656. Preload("MonitoringRecord", func(db *gorm.DB) *gorm.DB {
  657. return db.Where("status = 1 AND user_org_id = ? and monitoring_date >=? and monitoring_date<=? ", orgID, schedulDate, schedulDate).Order("operate_time asc")
  658. })
  659. db = db.Where("xt_schedule.status = 1")
  660. db = db.Where("schedule_date = ?", schedulDate)
  661. }
  662. if schedulType > 0 {
  663. db = db.Where("schedule_type = ?", schedulType)
  664. }
  665. if start > 0 {
  666. db = db.Where("schedule_date >= ?", start)
  667. }
  668. if end > 0 {
  669. db = db.Where("schedule_date <= ?", end)
  670. }
  671. if orgID > 0 {
  672. db = db.Where("user_org_id = ?", orgID)
  673. }
  674. if partitionType > 0 {
  675. db = db.Joins("inner join xt_device_number on xt_device_number.id = xt_schedule.bed_id and xt_device_number.zone_id = ?", partitionType)
  676. }
  677. offset := (page - 1) * limit
  678. err = db.Count(&total).Offset(offset).Limit(limit).Order("bed_id desc").Find(&schedule).Error
  679. return schedule, err, total
  680. }
  681. //func GetDialysisWatchByKeyword(orgID int64, keyword string, schedulType int64, partitionType int64, page int64, limit int64) (schedules []*models.DialysisSchedule, err error, total int64) {
  682. // db := readDb.Model(&models.DialysisSchedule{}).Where("status = 1")
  683. //
  684. // if schedulType > 0 {
  685. // db = db.Where("scheudle_type = ?", schedulType)
  686. // }
  687. //
  688. // if partitionType > 0 {
  689. // db = db.Where("partition_id = ?", partitionType)
  690. // }
  691. //
  692. // err = db.Count(&total).Offset(limit * (page - 1)).Limit(limit).Order("schedule_date desc").Find(&schedules).Error
  693. // return schedules, err, total
  694. //}
  695. func GetDialysisWatchByKeyword(orgID int64, keyword string, schedulType int64, partitionType int64, page int64, limit int64, start int64, end int64) ([]*models.DialysisSchedule, error, int64) {
  696. var patients []*models.Patients
  697. 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
  698. if getPatientErr != nil {
  699. return nil, getPatientErr, 0
  700. }
  701. patientIDs := make([]int64, len(patients))
  702. for index, patient := range patients {
  703. patientIDs[index] = patient.ID
  704. }
  705. db := readDb.Model(&models.DialysisSchedule{})
  706. if start > 0 && end > 0 {
  707. db = db.Preload("DeviceNumber", "org_id = ?", orgID).
  708. // Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  709. Preload("TreatmentMode", "status = 1").
  710. Preload("Prescription", "status = 1 AND user_org_id = ? and record_date>=? and record_date<=?", orgID, start, end).
  711. Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ? and assessment_date>=? and assessment_date<=?", orgID, start, end).
  712. Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ? and assessment_date>=? and assessment_date<=?", orgID, start, end).
  713. Preload("MonitoringRecord", "status = 1 AND user_org_id = ? and monitoring_date>=? and monitoring_date<=?", orgID, start, end).
  714. Preload("DialysisOrder", "status = 1 AND user_org_id = ? and dialysis_date >= ? and dialysis_date<=?", orgID, start, end).
  715. Preload("MonitorPatients", "status = 1 AND user_org_id = ?", orgID)
  716. } else {
  717. db = db.Preload("DeviceNumber", "org_id = ?", orgID).
  718. // Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  719. Preload("TreatmentMode", "status = 1").
  720. Preload("Prescription", "status = 1 AND user_org_id = ?", orgID).
  721. Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ?", orgID).
  722. Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ?", orgID).
  723. Preload("MonitoringRecord", "status = 1 AND user_org_id = ?", orgID).
  724. Preload("DialysisOrder", "status = 1 AND user_org_id = ?", orgID).
  725. Preload("MonitorPatients", "status = 1 AND user_org_id = ?", orgID)
  726. }
  727. db = db.Where("xt_schedule.status = 1 AND patient_id in (?)", patientIDs)
  728. if schedulType > 0 {
  729. db = db.Where("schedule_type = ?", schedulType)
  730. }
  731. if start > 0 {
  732. db = db.Where("schedule_date >= ?", start)
  733. }
  734. if end > 0 {
  735. db = db.Where("schedule_date<=?", end)
  736. }
  737. if partitionType > 0 {
  738. 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)
  739. // db = db.Where("partition_id = ?", partitionType)
  740. }
  741. var schedules []*models.DialysisSchedule
  742. total := int64(0)
  743. err := db.Count(&total).Offset(limit * (page - 1)).Limit(limit).Order("schedule_date desc").Find(&schedules).Error
  744. return schedules, err, total
  745. }
  746. func GetAdminUserES(orgID int64, appID int64, ids []int64) (es []*models.AdminUserElectronicSignature, err error) {
  747. err = readUserDb.Model(&models.AdminUserElectronicSignature{}).Where("org_id=? and app_id=? and creator in (?) and status=1", orgID, appID, ids).Find(&es).Error
  748. return
  749. }
  750. //
  751. //func GetAdminUserEsOne(orgid int64) (admin []*models.UserAdminRoles, err error) {
  752. //
  753. // db := readUserDb.Table("sgj_user_admin_role as x").Where("x.status = 1")
  754. // table := readUserDb.Table("sgj_user_admin_electronic_signature as s")
  755. // fmt.Println("table", table)
  756. // 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
  757. // return admin, err
  758. //}
  759. func GetAdminUserEsOne(orgid int64) (admin []*models.UserAdminRoles, err error) {
  760. db := readUserDb.Table("sgj_user_admin_role as x")
  761. table := readUserDb.Table("sgj_user_admin_electronic_signature as s")
  762. fmt.Println("table", table)
  763. 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
  764. return admin, err
  765. }
  766. func GetAdminUserEsTwo(orgid int64, aapid int64) (admin []*models.UserAdminRoles, err error) {
  767. db := readUserDb.Table("sgj_user_admin_role as x")
  768. table := readUserDb.Table("sgj_user_admin_electronic_signature as s")
  769. fmt.Println("table", table)
  770. 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 = ? and x.app_id =? and x.status=1", orgid, aapid).Group("user_name").Scan(&admin).Error
  771. return admin, err
  772. }
  773. func UpDateDialysisPrescriptionDoctorSign(patient int64, date int64, orgId int64, doctor_id int64) (err error) {
  774. 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
  775. return
  776. }
  777. func GetAllAdminUserES(orgID int64, appId int64) (es []*models.AdminUserElectronicSignature, err error) {
  778. err = readUserDb.Model(&models.AdminUserElectronicSignature{}).Where("org_id=? and app_id = ? and status=1", orgID, appId).Find(&es).Error
  779. return
  780. }
  781. func GetAllStarfEs(orgid int64) (es []*models.SgjUserAdminRoles, err error) {
  782. redis := RedisClient()
  783. defer redis.Close()
  784. // cur_date := time.Now().Format("2006-01-02")
  785. key := strconv.FormatInt(orgid, 10) + ":starfes"
  786. starfes_str, _ := redis.Get(key).Result()
  787. redis.Set(key, "", time.Second)
  788. if len(starfes_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  789. db := readUserDb.Table("sgj_user_admin_role as x").Where("x.status = 1")
  790. 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
  791. if err != nil {
  792. if err == gorm.ErrRecordNotFound {
  793. if len(es) <= 0 {
  794. redis.Set(key, "starfes_str_null", time.Second*60*60*18)
  795. }
  796. return nil, nil
  797. } else {
  798. return nil, err
  799. }
  800. } else {
  801. if len(es) > 0 {
  802. //缓存数据
  803. starfes_str, err := json.Marshal(es)
  804. if err == nil {
  805. redis.Set(key, starfes_str, time.Second*60*60*18)
  806. return es, nil
  807. }
  808. } else {
  809. redis.Set(key, "starfes_str_null", time.Second*60*60*18)
  810. return es, nil
  811. }
  812. return es, nil
  813. }
  814. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  815. if starfes_str == "starfes_str_null" {
  816. return es, nil
  817. } else {
  818. json.Unmarshal([]byte(starfes_str), &es)
  819. return es, nil
  820. }
  821. }
  822. }
  823. func FindLastMonitorRecord(patient_id int64, org_id int64) (record models.MonitoringRecord, err error) {
  824. err = readDb.Model(&models.MonitoringRecord{}).Where("patient_id = ? AND status = 1 AND user_org_id = ?", patient_id, org_id).Last(&record).Error
  825. return
  826. }
  827. func FindLastMonitorRecordToday(patient_id int64, org_id int64, monitoring_date int64) (record models.MonitoringRecord, err error) {
  828. 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
  829. return
  830. }
  831. func FindFirstMonitorRecordToday(patient_id int64, org_id int64, monitoring_date int64) (record models.MonitoringRecord, err error) {
  832. 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
  833. return
  834. }
  835. func ExceDoctorAdviceByGroupNo(m *models.DoctorAdvice, groupNo int64, org_id int64) (err error) {
  836. err = writeDb.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
  837. return err
  838. }
  839. func GetExceDoctorAdviceGroupNo(groupNo int64, org_id int64) (advice []*models.DoctorAdvice, err error) {
  840. err = XTReadDB().Where("groupno = ? AND user_org_id = ? AND status = 1", groupNo, org_id).Find(&advice).Error
  841. return advice, err
  842. }
  843. func ExceOldDoctorAdvice(m *models.DoctorAdvice, advice_id int64) (err error) {
  844. ut := writeDb.Begin()
  845. 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
  846. if err != nil {
  847. ut.Rollback()
  848. return
  849. }
  850. 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
  851. if err != nil {
  852. ut.Rollback()
  853. return
  854. }
  855. ut.Commit()
  856. return err
  857. }
  858. func CheckDoctorAdviceByGroupNo(m *models.DoctorAdvice, groupNo int64, org_id int64) (err error) {
  859. ut := writeDb.Begin()
  860. 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
  861. if err != nil {
  862. ut.Rollback()
  863. return
  864. }
  865. ut.Commit()
  866. return err
  867. }
  868. func CheckDoctorAdviceByGroupNoOne(m *models.DoctorAdvice, groupNo int64, org_id int64) (err error) {
  869. ut := writeDb.Begin()
  870. err = ut.Model(&models.DoctorAdvice{}).Where("groupno = ? AND user_org_id = ? AND status = 1", groupNo, org_id).Updates(map[string]interface{}{"check_time": m.CheckTime, "checker": m.Checker, "check_state": 1}).Error
  871. if err != nil {
  872. ut.Rollback()
  873. return
  874. }
  875. ut.Commit()
  876. return err
  877. }
  878. func CheckOldDoctorAdvice(m *models.DoctorAdvice, advice_id int64) (err error) {
  879. ut := writeDb.Begin()
  880. 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
  881. if err != nil {
  882. ut.Rollback()
  883. return
  884. }
  885. 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
  886. if err != nil {
  887. ut.Rollback()
  888. return
  889. }
  890. ut.Commit()
  891. return err
  892. }
  893. func DeleteDoctorAdviceByGroupNo(m *models.DoctorAdvice) (err error) {
  894. ut := writeDb.Begin()
  895. 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
  896. if err != nil {
  897. ut.Rollback()
  898. return
  899. }
  900. ut.Commit()
  901. return
  902. }
  903. func GetDialysisBoard(orgID int64, scheduleDate int64, offset, limit int64) ([]*models.DialysisBoardVM, error) {
  904. var boards []*models.DialysisBoardVM
  905. db := readDb.
  906. Table("xt_schedule").
  907. Preload("Patient", "status = 1 AND user_org_id = ?", orgID).
  908. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  909. Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  910. Preload("DialysisOrder", "status = 1 AND user_org_id = ?", orgID).
  911. Preload("SigninAndWeigh", "status = 1 AND user_org_id = ?", orgID).
  912. Preload("NextSchedules", func(db *gorm.DB) *gorm.DB {
  913. return db.Where("user_org_id = ? AND schedule_date > ? AND status = 1", orgID, scheduleDate).
  914. Order("schedule_date asc, id asc")
  915. }).
  916. Where("status = 1 AND user_org_id = ?", orgID)
  917. if scheduleDate != 0 {
  918. db = db.Where("schedule_date = ?", scheduleDate)
  919. }
  920. err := db.Offset(offset).Limit(limit).Find(&boards).Error
  921. return boards, err
  922. }
  923. func GetOneDialysisOrder(orgID, orderData int64, patient_id int64) (*models.DialysisOrder, error) {
  924. var order models.DialysisOrder
  925. var err error
  926. 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
  927. if err == gorm.ErrRecordNotFound {
  928. return nil, nil
  929. }
  930. if err != nil {
  931. return nil, err
  932. }
  933. return &order, nil
  934. }
  935. func GetOneDialysisOrderOne(orgID, orderData int64, patient_id int64) (models.DialysisOrder, error) {
  936. order := models.DialysisOrder{}
  937. err := readDb.Where("user_org_id=? and dialysis_date=? and status=1 and patient_id = ?", orgID, orderData, patient_id).Find(&order).Error
  938. return order, err
  939. }
  940. func GetPscheduleDate(schedule_date int64, patient_id int64, user_org_id int64) (models.XtSchedule, error) {
  941. schedule := models.XtSchedule{}
  942. err := XTReadDB().Where("schedule_date = ? and patient_id = ? and user_org_id =? and status =1").Find(&schedule).Error
  943. return schedule, err
  944. }
  945. func UpdateDialysiOrderByPatientId(id int64, schedule_type int64, partion_id int64, bed_id int64) error {
  946. err := XTWriteDB().Model(&models.DialysisOrder{}).Where("id = ? and status =1", id).Updates(map[string]interface{}{"schedual_type": schedule_type, "zone_id": partion_id, "bed_id": bed_id}).Error
  947. return err
  948. }
  949. func GetOrgInfoTemplate(orgID int64) (models.GobalTemplate, error) {
  950. var templateInfo models.GobalTemplate
  951. var err error
  952. err = readDb.Model(&models.GobalTemplate{}).Where("org_id=? and status=1", orgID).First(&templateInfo).Error
  953. return templateInfo, err
  954. }
  955. func FindAdminRoleTypeById(orgId int64, adminUserId int64, appId int64) (models.App_Role, error) {
  956. var appRole models.App_Role
  957. var err error
  958. 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
  959. return appRole, err
  960. }
  961. func BatchExceOldDoctorAdvice(m *models.DoctorAdvice, ids []string) (err error) {
  962. err = writeDb.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
  963. err = writeDb.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
  964. return err
  965. }
  966. func BatchCheckOldDoctorAdvice(m *models.DoctorAdvice, ids []string, org_id int64, date int64) (err error) {
  967. 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
  968. 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
  969. return err
  970. }
  971. func ModifyScheduleMode(mode_id int64, patient_id int64, recordDate int64, org_id int64, dialysis_machine_name string) {
  972. ut := writeDb.Begin()
  973. 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, "dialysis_machine_name": dialysis_machine_name}).Error
  974. if err != nil {
  975. ut.Rollback()
  976. return
  977. }
  978. ut.Commit()
  979. }
  980. func ModifyScheduleModeOne(mode_id int64, patient_id int64, recordDate int64, org_id int64, dialysis_machine_name string) {
  981. ut := writeDb.Begin()
  982. err = writeDb.Model(&models.Schedule{}).Where("status = 1 AND user_org_id = ? AND patient_id = ? AND schedule_date >= ? mode_id= ?", org_id, patient_id, recordDate, mode_id).Updates(map[string]interface{}{"updated_time": time.Now().Unix(), "dialysis_machine_name": dialysis_machine_name}).Error
  983. if err != nil {
  984. ut.Rollback()
  985. return
  986. }
  987. ut.Commit()
  988. }
  989. func FindDialysisOrderById(id int64) (error, *models.DialysisOrder) {
  990. dialysisRecord := models.DialysisOrder{}
  991. err := readDb.Model(&models.DialysisOrder{}).Where("id = ? AND status = 1", id).Find(&dialysisRecord).Error
  992. return err, &dialysisRecord
  993. }
  994. func ModifyExceDoctorAdviceByGroupNo(m *models.DoctorAdvice, groupNo int64, org_id int64) (err error) {
  995. ut := writeDb.Begin()
  996. 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
  997. if err != nil {
  998. ut.Rollback()
  999. return
  1000. }
  1001. ut.Commit()
  1002. return err
  1003. }
  1004. func ModifyExceDoctorAdviceByGroupNoOne(m *models.DoctorAdvice, ids []string) (err error) {
  1005. ut := writeDb.Begin()
  1006. err = ut.Model(&models.DoctorAdvice{}).Where("id in(?) and status =1", ids).Updates(map[string]interface{}{"execution_time": m.ExecutionTime}).Error
  1007. if err != nil {
  1008. ut.Rollback()
  1009. return
  1010. }
  1011. ut.Commit()
  1012. return err
  1013. }
  1014. func BatchModifyExceOldDoctorAdvice(m *models.DoctorAdvice, ids []string) (err error) {
  1015. ut := writeDb.Begin()
  1016. 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
  1017. if err != nil {
  1018. ut.Rollback()
  1019. return
  1020. }
  1021. 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
  1022. if err != nil {
  1023. ut.Rollback()
  1024. return
  1025. }
  1026. ut.Commit()
  1027. return err
  1028. }
  1029. func UpdateOrderCount(good_type_id int64, good_id int64, number string, org_id int64) {
  1030. 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))
  1031. }
  1032. func UpdateOrderInfoDetails(good_type_id int64, good_id int64, time int64, org_id int64, patient_id int64, newNiprocart int64, out *models.WarehouseOutInfo) {
  1033. 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})
  1034. }
  1035. func FindOrderInfoByGoodId(good_type_id int64, good_id int64, number string, org_id int64) (err error, out models.WarehouseOutInfo) {
  1036. 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
  1037. return
  1038. }
  1039. func FindGoodUserById(good_type_id int64, good_id int64, id int64, org_id int64, record_time int64) (user models.AutomaticReduceDetail, err error) {
  1040. 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
  1041. return
  1042. }
  1043. func FindOldOrderInfoByGoodId(time int64, org_id int64) (err error, out models.WarehouseOutInfo) {
  1044. err = readDb.Model(&models.WarehouseOutInfo{}).Where(" org_id = ? AND status = 1 AND sys_record_time = ? ", org_id, time).First(&out).Error
  1045. return
  1046. }
  1047. func FindStockOutOrderNumberByTime(time int64, org_id int64) (err error, out models.WarehouseOut) {
  1048. err = readDb.Model(&models.WarehouseOut{}).Where(" org_id = ? AND status = 1 AND warehouse_out_time = ? ", org_id, time).First(&out).Error
  1049. return
  1050. }
  1051. func UpdateDetails(good_type_id int64, good_id int64, time int64, org_id int64, patient_id int64, newNiprocart int64, out *models.WarehouseOutInfo) {
  1052. 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})
  1053. }
  1054. func FindPatientIsOpenRemindById(patient_id int64, org_id int64) (patients models.Patients, err error) {
  1055. err = readDb.Model(&models.Patients{}).Where("status = 1 AND id = ? AND user_org_id = ?", patient_id, org_id).First(&patients).Error
  1056. return
  1057. }
  1058. func GetMaxLongAdviceGroupID(orgId int64, patient_id int64) (group int64) {
  1059. var advice models.DoctorAdvice
  1060. 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
  1061. if err != nil {
  1062. fmt.Println(err)
  1063. group = 0
  1064. }
  1065. group = advice.GroupNo
  1066. return
  1067. }
  1068. func GetLastLongAdviceByGroupNo(orgId int64, patient_id int64, advice_date int64) (advice []*models.DoctorAdvice, err error) {
  1069. 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
  1070. return
  1071. }
  1072. func GetLastLongAdviceByGroupNoOne(orgId int64, patient_id int64, advice_date int64) (advice []*models.DoctorAdvice, err error) {
  1073. redis := RedisClient()
  1074. defer redis.Close()
  1075. key := strconv.FormatInt(orgId, 10) + ":" + strconv.FormatInt(patient_id, 10) + ":long_advice"
  1076. long_advice_str, _ := redis.Get(key).Result()
  1077. if len(long_advice_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1078. 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
  1079. if err != nil {
  1080. if err == gorm.ErrRecordNotFound {
  1081. return nil, nil
  1082. } else {
  1083. return nil, err
  1084. }
  1085. } else {
  1086. if len(advice) > 0 {
  1087. //缓存数据
  1088. starfes_str, err := json.Marshal(advice)
  1089. if err == nil {
  1090. redis.Set(key, starfes_str, time.Second*60*60*18)
  1091. return advice, nil
  1092. }
  1093. } else {
  1094. redis.Set(key, "null", time.Second*60*60*18)
  1095. return advice, nil
  1096. }
  1097. return advice, nil
  1098. }
  1099. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  1100. if long_advice_str == "null" {
  1101. return advice, nil
  1102. } else {
  1103. json.Unmarshal([]byte(long_advice_str), &advice)
  1104. return advice, nil
  1105. }
  1106. }
  1107. 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
  1108. return
  1109. }
  1110. func GetLastLongAdviceByGroupNoThree(orgId int64, patient_id int64, advice_date int64) (advice []*models.DoctorAdvice, err error) {
  1111. 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
  1112. return
  1113. }
  1114. func GetLastLongAdviceByGroupNoFour(orgId int64, patient_id int64, advice_date int64) (advice []*models.DoctorAdvice, err error) {
  1115. //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
  1116. //return
  1117. redis := RedisClient()
  1118. defer redis.Close()
  1119. key := strconv.FormatInt(orgId, 10) + ":" + strconv.FormatInt(patient_id, 10) + ":long_advice"
  1120. long_advice_str, _ := redis.Get(key).Result()
  1121. if len(long_advice_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1122. 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
  1123. if err != nil {
  1124. if err == gorm.ErrRecordNotFound {
  1125. return nil, nil
  1126. } else {
  1127. return nil, err
  1128. }
  1129. } else {
  1130. if len(advice) > 0 {
  1131. //缓存数据
  1132. starfes_str, err := json.Marshal(advice)
  1133. if err == nil {
  1134. redis.Set(key, starfes_str, time.Second*60*60*18)
  1135. return advice, nil
  1136. }
  1137. } else {
  1138. redis.Set(key, "null", time.Second*60*60*18)
  1139. return advice, nil
  1140. }
  1141. return advice, nil
  1142. }
  1143. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  1144. if long_advice_str == "null" {
  1145. return advice, nil
  1146. } else {
  1147. json.Unmarshal([]byte(long_advice_str), &advice)
  1148. return advice, nil
  1149. }
  1150. }
  1151. 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
  1152. return
  1153. }
  1154. func FindAllDoctorAdviceByTime(now int64, end int64, patient_id int64, orgId int64, temp_id string) (advice []*models.DoctorAdvice, err error) {
  1155. 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
  1156. return
  1157. }
  1158. func UpdateScheduleModeId(patient_id int64, org_id int64, date int64, mode_id int64) (err error) {
  1159. 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
  1160. return
  1161. }
  1162. func UpdateAssessmentAfterDate(patient_id int64, org_id int64, date int64, actual_treatment_hour int64, actual_treatment_minute int64) (err error) {
  1163. 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
  1164. return
  1165. }
  1166. func UpdateAssessmentAfterDateOne(patient_id int64, org_id int64, date int64, actual_treatment_hour int64, actual_treatment_minute int64, BreathingRate string) (err error) {
  1167. 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
  1168. return
  1169. }
  1170. func CreateDialysisBeforePrepare(beforePrepares []*models.DialysisBeforePrepare) (err error) {
  1171. if len(beforePrepares) > 0 {
  1172. utx := writeDb.Begin()
  1173. if len(beforePrepares) > 0 {
  1174. 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 "
  1175. insertParams := make([]string, 0)
  1176. insertData := make([]interface{}, 0)
  1177. for _, prepare := range beforePrepares {
  1178. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?)")
  1179. insertData = append(insertData, prepare.UserOrgId)
  1180. insertData = append(insertData, prepare.PatientId)
  1181. insertData = append(insertData, prepare.RecordDate)
  1182. insertData = append(insertData, prepare.GoodId)
  1183. insertData = append(insertData, prepare.GoodTypeId)
  1184. insertData = append(insertData, prepare.Count)
  1185. insertData = append(insertData, prepare.Ctime)
  1186. insertData = append(insertData, prepare.Mtime)
  1187. insertData = append(insertData, prepare.Creater)
  1188. insertData = append(insertData, prepare.Modifier)
  1189. insertData = append(insertData, 1)
  1190. insertData = append(insertData, prepare.CommdityCode)
  1191. }
  1192. thisSQL += strings.Join(insertParams, ", ")
  1193. err = utx.Exec(thisSQL, insertData...).Error
  1194. if err != nil {
  1195. utx.Rollback()
  1196. return
  1197. }
  1198. }
  1199. utx.Commit()
  1200. }
  1201. return
  1202. }
  1203. func UpdateDialysisBeforePrepare(info *models.DialysisBeforePrepare) (err error) {
  1204. err = writeDb.Save(&info).Error
  1205. return err
  1206. }
  1207. func UpdateDialysisBeforePrepareTwo(info *models.DialysisBeforePrepare) (err error) {
  1208. err = writeDb.Model(&models.DialysisBeforePrepare{}).
  1209. 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).
  1210. Updates(map[string]interface{}{"count": info.Count, "updated_time": time.Now().Unix(), "modifier": info.Modifier, "commdity_code": info.CommdityCode, "status": 1}).Error
  1211. return err
  1212. }
  1213. func UpdateAutoReduceDetailSix(detail *models.AutomaticReduceDetail) (err error) {
  1214. 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
  1215. return err
  1216. }
  1217. func UpdateOrderCountSub(good_type_id int64, good_id int64, number string, org_id int64, count int64) {
  1218. 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))
  1219. }
  1220. func UpdateOrderCountAdd(good_type_id int64, good_id int64, number string, org_id int64, count int64) {
  1221. 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))
  1222. }
  1223. func UpdateUserInfoDetails(good_type_id int64, good_id int64, time int64, org_id int64, patient_id int64, count int64, out *models.WarehouseOutInfo) {
  1224. 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})
  1225. //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)
  1226. }
  1227. func UpdateDrugUserInfoDetails(drug_id int64, time int64, org_id int64, patient_id int64, count int64, out *models.DrugWarehouseOutInfo) {
  1228. 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})
  1229. //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)
  1230. }
  1231. func FindDialysisBeforePrepare(patient_id int64, good_id int64, good_type_id int64, user_org_id int64, record_time int64) (count int64) {
  1232. 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)
  1233. return
  1234. }
  1235. func GetExecutionDoctors(orgid int64, patientid int64, id int64) (doctorAdvice []*models.DoctorAdvice, err error) {
  1236. 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
  1237. return doctorAdvice, err
  1238. }
  1239. func GetHisExecutionDoctors(orgid int64, patientid int64, id int64) (doctorAdvice []*models.HisDoctorAdviceInfo, err error) {
  1240. 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
  1241. return doctorAdvice, err
  1242. }
  1243. func GetHisExecutionDoctorsTwo(orgid int64, patientid int64, id int64) (doctorAdvice []*models.HisDoctorAdviceInfo, err error) {
  1244. 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
  1245. return doctorAdvice, err
  1246. }
  1247. func FindHisDoctorAdviceById(orgID int64, id int64) (advice models.HisDoctorAdviceInfo, err error) {
  1248. err = readDb.Model(&models.HisDoctorAdviceInfo{}).Where("user_org_id=? and status=1 and id = ?", orgID, id).First(&advice).Error
  1249. return
  1250. }
  1251. func FindHisDoctorAdviceByIdTwo(orgID int64, id int64, tx *gorm.DB) (advice models.HisDoctorAdviceInfo, err error) {
  1252. err = tx.Model(&models.HisDoctorAdviceInfo{}).Where("user_org_id=? and status=1 and id = ?", orgID, id).First(&advice).Error
  1253. if err != gorm.ErrRecordNotFound {
  1254. if err != nil {
  1255. tx.Rollback()
  1256. return
  1257. }
  1258. }
  1259. return
  1260. }
  1261. func FindHisDoctorAdviceByIds(orgID int64, ids []string) (advice []*models.HisDoctorAdviceInfo, err error) {
  1262. err = readDb.Model(&models.HisDoctorAdviceInfo{}).Where("user_org_id=? and status=1 and id in (?)", orgID, ids).Find(&advice).Error
  1263. return
  1264. }
  1265. func SaveHisDoctorAdvice(advice *models.HisDoctorAdviceInfo) (err error) {
  1266. err = writeDb.Save(&advice).Error
  1267. return
  1268. }
  1269. func SaveHisDoctorAdviceOne(advice *models.HisDoctorAdviceInfo, tx *gorm.DB) (err error) {
  1270. err = tx.Save(&advice).Error
  1271. if err != nil {
  1272. tx.Rollback()
  1273. return
  1274. }
  1275. return
  1276. }
  1277. func GetGoodInfoMation(orgid int64) (goodinfo []*models.GoodInfo, err error) {
  1278. 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
  1279. //err = XTReadDB().Model(&goodinfo).Where("org_id = ? and status = 1 AND find_in_set('停用',good_status) = 0", orgid).Find(&goodinfo).Error
  1280. return goodinfo, err
  1281. }
  1282. func GetGoodInfoMationSix(orgid int64, storehouse_id int64) (goodinfo []*models.GoodInfo, err error) {
  1283. 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).Order("sort asc").Find(&goodinfo).Error
  1284. return goodinfo, err
  1285. }
  1286. func GetGoodInfoMationSeven(orgid int64, storehouse_id int64) (goodinfo []*models.GoodInfo, err error) {
  1287. 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 = ? and stock_count>0", storehouse_id).Find(&goodinfo).Error
  1288. return goodinfo, err
  1289. }
  1290. func GetDialysisBeforePrepare(goodTypeId int64, goodId int64, orgid int64, patientId int64) (*models.DialysisBeforePrepare, error) {
  1291. prepare := models.DialysisBeforePrepare{}
  1292. 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
  1293. if err == gorm.ErrRecordNotFound {
  1294. return nil, err
  1295. }
  1296. if err != nil {
  1297. return nil, err
  1298. }
  1299. return &prepare, nil
  1300. }
  1301. func CreateDialysisBeforePrepareOne(prepare *models.DialysisBeforePrepare) error {
  1302. err := XTWriteDB().Create(&prepare).Error
  1303. return err
  1304. }
  1305. func ModifyExceDoctorAdviceById(m *models.HisDoctorAdviceInfo, ids []string) (err error) {
  1306. ut := writeDb.Begin()
  1307. 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
  1308. if err != nil {
  1309. ut.Rollback()
  1310. return
  1311. }
  1312. 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
  1313. if err != nil {
  1314. ut.Rollback()
  1315. return
  1316. }
  1317. ut.Commit()
  1318. return err
  1319. }
  1320. func FindHisDoctorAdviceByIdOne(orgID int64, ids []string) (advice []models.HisDoctorAdviceInfo, err error) {
  1321. err = readDb.Model(&models.HisDoctorAdviceInfo{}).Where("id IN (?) AND user_org_id = ? AND status = 1", ids, orgID).Find(&advice).Error
  1322. return
  1323. }
  1324. func ModifyHisPrescriptionProject(m *models.HisPrescriptionProject, ids []string) (err error) {
  1325. ut := writeDb.Begin()
  1326. 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
  1327. if err != nil {
  1328. ut.Rollback()
  1329. return
  1330. }
  1331. ut.Commit()
  1332. return err
  1333. }
  1334. func FindHisProjectById(orgID int64, id int64) (advice models.HisPrescriptionProject, err error) {
  1335. err = readDb.Model(&models.HisPrescriptionProject{}).Where("user_org_id=? and status=1 and id = ?", orgID, id).First(&advice).Error
  1336. return
  1337. }
  1338. func SaveHisProject(advice *models.HisPrescriptionProject) (err error) {
  1339. err = writeDb.Save(&advice).Error
  1340. return
  1341. }
  1342. func FindAllHisProjectById(orgID int64, patient_id int64, record_time int64) (advice []*models.HisPrescriptionProject, err error) {
  1343. err = readDb.Model(&models.HisPrescriptionProject{}).Preload("HisProject", "status = 1 and user_org_id = ?", orgID).Preload("XtHisProjectTeam", "user_org_id=? and status=1", orgID).Preload("GoodInfo", "status = 1 and org_id = ?", orgID).Where("user_org_id=? and status=1 and patient_id = ? AND record_date = ?", orgID, patient_id, record_time).Find(&advice).Error
  1344. return
  1345. }
  1346. func GetAllPcPatientListByListSeven(orgID int64) (patients []*MSchedualPatientVMList, err error) {
  1347. err = readDb.Where("user_org_id=? and status=1 and lapseto = 1", orgID).Find(&patients).Error
  1348. return patients, err
  1349. }
  1350. func GetAllPcPatientListByListEight(orgID int64) (patients []*MSchedualPatientVMList, err error) {
  1351. err = readDb.Where("user_org_id=? and status=1", orgID).Find(&patients).Error
  1352. return patients, err
  1353. }
  1354. func GetAllPcPatientListByList(orgID int64) (patients []*MSchedualPatientVMList, err error) {
  1355. redis := RedisClient()
  1356. defer redis.Close()
  1357. key := strconv.FormatInt(orgID, 10) + ":" + ":patient_list_all"
  1358. patient_info_str, _ := redis.Get(key).Result()
  1359. if len(patient_info_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1360. err = readDb.Where("user_org_id=? and status=1 and lapseto = 1", orgID).Find(&patients).Error
  1361. if err != nil {
  1362. if err == gorm.ErrRecordNotFound {
  1363. if len(patients) <= 0 {
  1364. redis.Set(key, "null", time.Second*60*60*18)
  1365. }
  1366. return nil, nil
  1367. } else {
  1368. return nil, err
  1369. }
  1370. } else {
  1371. if len(patients) > 0 {
  1372. //缓存数据
  1373. patient_info_json, err := json.Marshal(&patients)
  1374. if err == nil {
  1375. redis.Set(key, patient_info_json, time.Second*60*60*18)
  1376. }
  1377. } else {
  1378. redis.Set(key, "null", time.Second*60*60*18)
  1379. }
  1380. return patients, nil
  1381. }
  1382. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  1383. if patient_info_str == "null" {
  1384. return nil, nil
  1385. } else {
  1386. json.Unmarshal([]byte(patient_info_str), &patients)
  1387. return patients, nil
  1388. }
  1389. }
  1390. }
  1391. func GetAllPcAdvicestByList(orgID int64, scheduleDate int64) (advices []*VMDoctorAdvice, err error) {
  1392. redis := RedisClient()
  1393. defer redis.Close()
  1394. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":advice_list_all"
  1395. advice_list_str, _ := redis.Get(key).Result()
  1396. if len(advice_list_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1397. err = readDb.Where("status = 1 AND user_org_id = ? AND advice_type = 2 AND advice_date = ?", orgID, scheduleDate).Find(&advices).Error
  1398. if err != nil {
  1399. if err == gorm.ErrRecordNotFound {
  1400. if len(advices) <= 0 {
  1401. redis.Set(key, "null", time.Second*60*60*18)
  1402. }
  1403. return nil, nil
  1404. } else {
  1405. return nil, err
  1406. }
  1407. } else {
  1408. if len(advices) > 0 {
  1409. //缓存数据
  1410. advice_list_str, err := json.Marshal(&advices)
  1411. if err == nil {
  1412. redis.Set(key, advice_list_str, time.Second*60*60*18)
  1413. }
  1414. } else {
  1415. redis.Set(key, "null", time.Second*60*60*18)
  1416. }
  1417. return advices, nil
  1418. }
  1419. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  1420. if advice_list_str == "null" {
  1421. return nil, nil
  1422. } else {
  1423. json.Unmarshal([]byte(advice_list_str), &advices)
  1424. return advices, nil
  1425. }
  1426. }
  1427. }
  1428. func GetAllPcDialysisOrdersByList(orgID int64, scheduleDate int64) (dialysisOrders []*MDialysisOrderVMList, err error) {
  1429. redis := RedisClient()
  1430. defer redis.Close()
  1431. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":dialysis_orders_list_all"
  1432. dialysis_orders_list_all, _ := redis.Get(key).Result()
  1433. if len(dialysis_orders_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1434. 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
  1435. if err != nil {
  1436. if err == gorm.ErrRecordNotFound {
  1437. return nil, nil
  1438. } else {
  1439. return nil, err
  1440. }
  1441. } else {
  1442. if len(dialysisOrders) > 0 {
  1443. //缓存数据
  1444. dialysis_orders_list_all, err := json.Marshal(&dialysisOrders)
  1445. if err == nil {
  1446. redis.Set(key, dialysis_orders_list_all, time.Second*60*60*18)
  1447. }
  1448. } else {
  1449. redis.Set(key, "null", time.Second*60*60*18)
  1450. }
  1451. return dialysisOrders, nil
  1452. }
  1453. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  1454. if dialysis_orders_list_all == "null" {
  1455. return nil, nil
  1456. } else {
  1457. json.Unmarshal([]byte(dialysis_orders_list_all), &dialysisOrders)
  1458. return dialysisOrders, nil
  1459. }
  1460. }
  1461. }
  1462. func GetAllPcAssessmentAfterDislysisByList(orgID int64, scheduleDate int64) (assessmentAfterDislysis []*AssessmentAfterDislysis, err error) {
  1463. redis := RedisClient()
  1464. defer redis.Close()
  1465. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":assessment_after_dislysis_list_all"
  1466. assessment_after_dislysis__all, _ := redis.Get(key).Result()
  1467. if len(assessment_after_dislysis__all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1468. err = readDb.Where("status = 1 AND user_org_id = ? AND assessment_date = ?", orgID, scheduleDate).Find(&assessmentAfterDislysis).Error
  1469. if err != nil {
  1470. if err == gorm.ErrRecordNotFound {
  1471. if len(assessmentAfterDislysis) <= 0 {
  1472. redis.Set(key, "null", time.Second*60*60*18)
  1473. }
  1474. return nil, nil
  1475. } else {
  1476. return nil, err
  1477. }
  1478. } else {
  1479. if len(assessmentAfterDislysis) > 0 {
  1480. //缓存数据
  1481. assessment_after_dislysis__all, err := json.Marshal(&assessmentAfterDislysis)
  1482. if err == nil {
  1483. redis.Set(key, assessment_after_dislysis__all, time.Second*60*60*18)
  1484. }
  1485. } else {
  1486. redis.Set(key, "null", time.Second*60*60*18)
  1487. }
  1488. return assessmentAfterDislysis, nil
  1489. }
  1490. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  1491. if assessment_after_dislysis__all == "null" {
  1492. return nil, nil
  1493. } else {
  1494. json.Unmarshal([]byte(assessment_after_dislysis__all), &assessmentAfterDislysis)
  1495. return assessmentAfterDislysis, nil
  1496. }
  1497. }
  1498. }
  1499. func GetAllPcTreatmentSummarysByList(orgID int64, scheduleDate int64) (treatmentSummarys []*VMTreatmentSummary, err error) {
  1500. redis := RedisClient()
  1501. defer redis.Close()
  1502. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":treatment_summarys_list_all"
  1503. treatment_summarys_all, _ := redis.Get(key).Result()
  1504. if len(treatment_summarys_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1505. err = readDb.Where("status = 1 AND user_org_id = ? AND assessment_date = ?", orgID, scheduleDate).Find(&treatmentSummarys).Error
  1506. if err != nil {
  1507. if err == gorm.ErrRecordNotFound {
  1508. if len(treatment_summarys_all) <= 0 {
  1509. redis.Set(key, "null", time.Second*60*60*18)
  1510. }
  1511. return nil, nil
  1512. } else {
  1513. return nil, err
  1514. }
  1515. } else {
  1516. if len(treatmentSummarys) > 0 {
  1517. //缓存数据
  1518. treatment_summarys_all, err := json.Marshal(&treatmentSummarys)
  1519. if err == nil {
  1520. redis.Set(key, treatment_summarys_all, time.Second*60*60*18)
  1521. }
  1522. } else {
  1523. redis.Set(key, "null", time.Second*60*60*18)
  1524. }
  1525. return treatmentSummarys, nil
  1526. }
  1527. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  1528. if treatment_summarys_all == "null" {
  1529. return nil, nil
  1530. } else {
  1531. json.Unmarshal([]byte(treatment_summarys_all), &treatmentSummarys)
  1532. return treatmentSummarys, nil
  1533. }
  1534. }
  1535. }
  1536. func UpdateTeamProjectExceStatus(ids []int64, admin_user_id int64, execution_time int64) (err error) {
  1537. ut := writeDb.Begin()
  1538. db := ut.Model(&models.HisPrescriptionProject{})
  1539. 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
  1540. if err != nil {
  1541. ut.Rollback()
  1542. return
  1543. }
  1544. ut.Commit()
  1545. return err
  1546. }
  1547. func UpdateTeamProjectCheckStatus(ids []int64, patient_id int64, record_date int64, user_org_id int64, admin_user_id int64, check_time int64) (err error) {
  1548. ut := writeDb.Begin()
  1549. 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
  1550. if err != nil {
  1551. ut.Rollback()
  1552. return
  1553. }
  1554. ut.Commit()
  1555. return err
  1556. }
  1557. func GetCheckTeamProject(team_id int64, patient_id int64, record_date int64, user_org_id int64) (ps []*models.HisPrescriptionProject, err error) {
  1558. 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
  1559. return
  1560. }
  1561. func GetAdminUserRoleInformation(orgid int64) (models.SgjOrgFollow, error) {
  1562. follow := models.SgjOrgFollow{}
  1563. db := UserReadDB().Table("sgj_org_follow as f").Where("f.status = 1")
  1564. table := UserReadDB().Table("sgj_admin as a").Where("a.status = 1")
  1565. fmt.Println(table)
  1566. if orgid > 0 {
  1567. db = db.Where("org_id = ?", orgid)
  1568. }
  1569. if orgid == 0 {
  1570. db = db.Where("org_id = ?", 0)
  1571. }
  1572. 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
  1573. return follow, err
  1574. }
  1575. func GetOrgFollowIsExist(orgid int64) (*models.SgjOrgFollowOne, error) {
  1576. follow := models.SgjOrgFollowOne{}
  1577. err := UserReadDB().Where("org_id = ? and status = 1", orgid).Find(&follow).Error
  1578. if err == gorm.ErrRecordNotFound {
  1579. return nil, err
  1580. }
  1581. if err != nil {
  1582. return nil, err
  1583. }
  1584. return &follow, nil
  1585. }
  1586. func UpdatePatientSign(patient_id int64, dialysis_date int64, order models.DialysisOrder, orgid int64) error {
  1587. 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
  1588. return err
  1589. }
  1590. func GetPatientSign(patient_id int64, dialysis_date int64, orgid int64) (models.DialysisOrder, error) {
  1591. order := models.DialysisOrder{}
  1592. err := XTReadDB().Where("patient_id=? and dialysis_date = ? and user_org_id = ? and status = 1", patient_id, dialysis_date, orgid).Find(&order).Error
  1593. return order, err
  1594. }
  1595. func GetScheduleByPatient(patient_id int64, schedule_date int64, orgid int64) (models.XtScheduleSix, error) {
  1596. schedule := models.XtScheduleSix{}
  1597. err := XTReadDB().Where("patient_id = ? and schedule_date = ? and user_org_id = ? and status = 1", patient_id, schedule_date, orgid).Find(&schedule).Error
  1598. return schedule, err
  1599. }
  1600. func GetSchedulePrintList(page int64, limit int64, schedulType int64, partitionType int64, keywords string, schedule_date int64, orgID int64) (list []*models.DialysisScheduleSix, total int64, err error) {
  1601. db := XTReadDB().Table("xt_schedule as x").Where("x.status = 1")
  1602. table := XTReadDB().Table("xt_patients as p").Where("p.status = 1")
  1603. fmt.Println("table", table)
  1604. if schedulType > 0 {
  1605. db = db.Where("x.schedule_type = ?", schedulType)
  1606. }
  1607. if partitionType > 0 {
  1608. db = db.Where("x.partition_id = ?", partitionType)
  1609. }
  1610. if orgID > 0 {
  1611. db = db.Where("x.user_org_id = ?", orgID)
  1612. }
  1613. if schedule_date > 0 {
  1614. db = db.Where("x.schedule_date = ?", schedule_date)
  1615. }
  1616. db = db.Joins("left join xt_patients as p on p.id = x.patient_id")
  1617. likeKey := "%" + keywords + "%"
  1618. if len(keywords) > 0 {
  1619. db = db.Where("p.name like ?", likeKey)
  1620. }
  1621. err = db.Count(&total).Select("x.id,x.user_org_id,x.partition_id,x.bed_id,x.patient_id,x.schedule_date,x.schedule_type,x.schedule_week,x.mode_id,x.status,p.name").Offset(limit * (page - 1)).Limit(limit).Order("x.schedule_date desc").Find(&list).Error
  1622. return list, total, err
  1623. }
  1624. func GetSchedulePrintListOne(page int64, limit int64, schedulType int64, partitionType int64, keywords string, schedule_date int64, orgID int64) (list []*models.DialysisScheduleSeven, err error) {
  1625. db := XTReadDB().Model(&list).Where("status = 1")
  1626. if orgID > 0 {
  1627. db = db.Where("user_org_id = ?", orgID)
  1628. }
  1629. if schedulType > 0 {
  1630. db = db.Where("schedule_type = ?", schedulType)
  1631. }
  1632. if partitionType > 0 {
  1633. db = db.Where("partition_id = ?", partitionType)
  1634. }
  1635. err = db.Group("schedule_type,partition_id").Find(&list).Error
  1636. return list, err
  1637. }
  1638. func GetDialysisRecordOrder(org_id int64, patient_id int64, dialysis_date int64) (models.MDialysisOrderForListSix, error) {
  1639. order := models.MDialysisOrderForListSix{}
  1640. err := XTReadDB().Where("user_org_id = ? and patient_id = ? and dialysis_date = ? and status =1 ", org_id, patient_id, dialysis_date).Find(&order).Error
  1641. return order, err
  1642. }
  1643. func GetSolutionListByOrgId(orgid int64) (list []*models.DialysisSolution, err error) {
  1644. err = XTReadDB().Where("user_org_id = ? and status = 1", orgid).Group("patient_id,mode_id").Order("id desc").Find(&list).Error
  1645. return list, err
  1646. }
  1647. func GetScheduleTypeById(org_id int64, schedule_date int64, schedule_type int64) (list []*models.XtScheduleSeven, err error) {
  1648. db := XTReadDB().Model(&list).Where("status = 1")
  1649. if org_id > 0 {
  1650. db = db.Where("user_org_id = ?", org_id)
  1651. }
  1652. if schedule_date > 0 {
  1653. db = db.Where("schedule_date = ?", schedule_date)
  1654. }
  1655. if schedule_type > 0 {
  1656. db = db.Where("schedule_type = ?", schedule_type)
  1657. }
  1658. err = db.Select("partition_id,count(id) as count").Group("partition_id").Scan(&list).Error
  1659. return list, err
  1660. }
  1661. func GetHisExcutionDoctorAdviceListGroupList(patients []string, advice_date int64, user_org_id int64) (advice []*models.HisDoctorAdvice, err error) {
  1662. err = XTReadDB().Where("patient_id in(?) and advice_date = ? and user_org_id = ? and status = 1 and execution_state = 2", patients, advice_date, user_org_id).Group("drug_id").Find(&advice).Error
  1663. return advice, err
  1664. }
  1665. func GetHisExcutionDoctorAdviceList(patients []string, advice_date int64, user_org_id int64) (advice []*models.HisDoctorAdvice, err error) {
  1666. err = XTReadDB().Where("patient_id in(?) and advice_date = ? and user_org_id = ? and status = 1 and execution_state = 2", patients, advice_date, user_org_id).Find(&advice).Error
  1667. return advice, err
  1668. }
  1669. func UpdateHisDoctorAdviceExecution(info models.HisDoctorAdviceInfo, id int64) error {
  1670. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Update(map[string]interface{}{"execution_staff": info.ExecutionStaff, "execution_state": info.ExecutionState, "execution_time": info.ExecutionTime, "updated_time": info.ExecutionTime}).Error
  1671. return err
  1672. }
  1673. func UpdateDoctorAdviceExecution(info models.DoctorAdvice, id int64) error {
  1674. err := XTWriteDB().Model(&models.DoctorAdvice{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"execution_staff": info.ExecutionStaff, "execution_state": info.ExecutionState, "execution_time": info.ExecutionTime, "updated_time": info.ExecutionTime}).Error
  1675. return err
  1676. }
  1677. func GetExcutionDoctorAdviceGroupList(patients []string, advice_date int64, user_org_id int64) (advice []*models.DoctorAdviceThrity, err error) {
  1678. err = XTReadDB().Where("patient_id in(?) and advice_date = ? and user_org_id = ? and status = 1 and execution_state = 2", patients, advice_date, user_org_id).Group("drug_id").Find(&advice).Error
  1679. return advice, err
  1680. }
  1681. func GetExcutionDoctorAdviceList(patients []string, advice_date int64, user_org_id int64) (advice []*models.DoctorAdviceThrity, err error) {
  1682. err = XTReadDB().Where("patient_id in(?) and advice_date = ? and user_org_id = ? and status = 1 and execution_state = 2", patients, advice_date, user_org_id).Find(&advice).Error
  1683. return advice, err
  1684. }
  1685. func GetHisCheckDoctorAdvice(orgid int64, patinentids []string, advice_date int64) (advice []*models.HisDoctorAdviceThirty, err error) {
  1686. err = XTReadDB().Where("user_org_id = ? and patient_id in(?) and advice_date = ? and status =1", orgid, patinentids, advice_date).Find(&advice).Error
  1687. return advice, err
  1688. }
  1689. func CheckDoctorAdvice(id int64, creater int64) (models.DoctorAdviceThrity, error) {
  1690. doctoradvice := models.DoctorAdviceThrity{}
  1691. err := XTWriteDB().Model(&doctoradvice).Where("id = ? and status = 1", id).Update(map[string]interface{}{"checker": creater, "check_time": time.Now().Unix()}).Error
  1692. return doctoradvice, err
  1693. }
  1694. func CheckHisDoctorAdvice(id int64, creater int64) (models.HisDoctorAdviceThirty, error) {
  1695. doctoradvice := models.HisDoctorAdviceThirty{}
  1696. err := XTWriteDB().Model(&doctoradvice).Where("id = ? and status = 1", id).Update(map[string]interface{}{"checker": creater, "check_time": time.Now().Unix()}).Error
  1697. return doctoradvice, err
  1698. }
  1699. func GetCheckDoctorAdvice(orgid int64, patinentids []string, advice_date int64) (advice []*models.DoctorAdviceThrity, err error) {
  1700. err = XTReadDB().Where("user_org_id = ? and patient_id in(?) and advice_date = ? and status=1", orgid, patinentids, advice_date).Find(&advice).Error
  1701. return advice, err
  1702. }
  1703. func SettleHisNewDoctorAdvice(patinentids []string, advice_date int64, orgid int64) (advice []*models.HisDoctorAdviceThirty, err error) {
  1704. err = XTWriteDB().Model(&advice).Where("patient_id in(?) and advice_date = ? and user_org_id = ? and status = 1", patinentids, advice_date, orgid).Update(map[string]interface{}{"is_settle": 1}).Error
  1705. return advice, err
  1706. }
  1707. func SettleNewDoctorAdvice(patinentids []string, advice_date int64, orgid int64) (advice []*models.DoctorAdviceThrity, err error) {
  1708. err = XTWriteDB().Model(&advice).Where("patient_id in(?) and advice_date = ? and user_org_id = ? and status = 1", patinentids, advice_date, orgid).Update(map[string]interface{}{"is_settle": 1}).Error
  1709. return advice, err
  1710. }
  1711. func GetHisMobileAdviceGroupList(orgid int64, ids []string) (advice []*models.HisDoctorAdvice, err error) {
  1712. err = XTReadDB().Where("user_org_id = ? and id in(?) and status = 1", orgid, ids).Group("drug_id").Find(&advice).Error
  1713. return advice, err
  1714. }
  1715. func GetHisMobileAdviceList(orgid int64, ids []string) (advice []*models.HisDoctorAdvice, err error) {
  1716. err = XTReadDB().Where("user_org_id = ? and id in(?) and status = 1", orgid, ids).Find(&advice).Error
  1717. return advice, err
  1718. }
  1719. func ExectionMobileAdvice(orgid int64, ids []string, execution_time int64, execution_staff int64) error {
  1720. advice := models.HisDoctorAdvice{}
  1721. err := XTWriteDB().Model(&advice).Where("user_org_id = ? and id in(?) and status = 1", orgid, ids).Updates(map[string]interface{}{"execution_time": execution_time, "execution_staff": execution_staff, "execution_state": 1, "is_mobile": 3}).Error
  1722. return err
  1723. }
  1724. func GetBloodMobileAdviceGroupList(orgid int64, ids []string) (advice []*models.DoctorAdviceThrity, err error) {
  1725. err = XTReadDB().Model(&advice).Where("user_org_id = ? and id in(?) and status = 1", orgid, ids).Group("drug_id").Find(&advice).Error
  1726. return advice, err
  1727. }
  1728. func GetBloodMobileAdviceList(orgid int64, ids []string) (advice []*models.DoctorAdviceThrity, err error) {
  1729. err = XTReadDB().Model(&advice).Where("user_org_id = ? and id in(?) and status = 1", orgid, ids).Find(&advice).Error
  1730. return advice, err
  1731. }
  1732. func FindAllDoctorAdviceByIds(orgID int64, ids []string) (advice []models.DoctorAdvice, err error) {
  1733. err = readDb.Model(&models.DoctorAdvice{}).Where("user_org_id=? AND id in(?) AND status = 1", orgID, ids).Find(&advice).Error
  1734. return
  1735. }
  1736. func ExectionBloodMobileAdvice(orgid int64, ids []string, execution_time int64, execution_staff int64) error {
  1737. advice := models.DoctorAdviceThrity{}
  1738. err := XTWriteDB().Model(&advice).Where("user_org_id = ? and id in(?) and status = 1", orgid, ids).Updates(map[string]interface{}{"execution_time": execution_time, "execution_staff": execution_staff, "execution_state": 1}).Error
  1739. return err
  1740. }
  1741. func UpdateSettleMobileHisAdvice(orgid int64, ids []string) error {
  1742. advice := models.HisDoctorAdviceThirty{}
  1743. err := XTWriteDB().Model(&advice).Where("user_org_id = ? and id in(?) and status =1", orgid, ids).Updates(map[string]interface{}{"is_settle": 1}).Error
  1744. return err
  1745. }
  1746. func UpdateSettleMobileBloodAdvice(orgid int64, ids []string) error {
  1747. advice := models.DoctorAdvice{}
  1748. err := XTWriteDB().Model(&advice).Where("user_org_id = ? and id in(?) and status =1", orgid, ids).Updates(map[string]interface{}{"is_settle": 1}).Error
  1749. return err
  1750. }
  1751. func CheckBloodDoctorAdvice(orgid int64, ids []string, advice models.DoctorAdvice) error {
  1752. err := XTWriteDB().Model(&advice).Where("user_org_id = ? and id in(?) and status = 1", orgid, ids).Updates(map[string]interface{}{"check_time": advice.CheckTime, "checker": advice.Checker, "check_state": 1}).Error
  1753. return err
  1754. }
  1755. func CheckHisMobileDoctorAdvice(orgid int64, ids []string, advice models.HisDoctorAdviceThirty) error {
  1756. err := XTWriteDB().Model(&advice).Where("user_org_id = ? and id in(?) and status = 1", orgid, ids).Updates(map[string]interface{}{"check_time": advice.CheckTime, "checker": advice.Checker, "check_state": 1}).Error
  1757. return err
  1758. }
  1759. func GetLastOrder(user_org_id int64, patient_id int64, dialysis_date int64) (models.DialysisOrderTwenty, error) {
  1760. twenty := models.DialysisOrderTwenty{}
  1761. err := XTReadDB().Where("user_org_id = ? and patient_id =? and dialysis_date < ? and status = 1", user_org_id, patient_id, dialysis_date).Preload("DeviceNumber", func(db *gorm.DB) *gorm.DB {
  1762. return db.Preload("DeviceZone", "status =1").Where("org_id = ? AND status = 1", user_org_id)
  1763. }).Order("id desc").First(&twenty).Error
  1764. return twenty, err
  1765. }
  1766. func UpdateDoctorSix(start_time int64, patient_id int64, dialysis_date int64, user_org_id int64) (models.DoctorAdvice, error) {
  1767. advice := models.DoctorAdvice{}
  1768. err := XTWriteDB().Model(&advice).Where("patient_id = ? and advice_date=? and user_org_id =? and status=1 and advice_name ='低分子肝素钠注射液'", patient_id, dialysis_date, user_org_id).Updates(map[string]interface{}{"execution_time": start_time}).Error
  1769. return advice, err
  1770. }
  1771. func GetTodayInforMation(orgid int64, record_date int64) (list []*NewDeviceInformation, err error) {
  1772. err = UserReadDB().Where("user_org_id = ? and date = ? and status = 1", orgid, record_date).Find(&list).Error
  1773. return list, err
  1774. }
  1775. func GetFiledConfig(orgid int64) (models.FiledConfig, error) {
  1776. config := models.FiledConfig{}
  1777. err := XTReadDB().Where("filed_name = 'dialyzer_perfusion_apparatus' and org_id = ? and module =1 and is_show = 1", orgid).First(&config).Error
  1778. return config, err
  1779. }
  1780. func GetDoctorAdviceCheckList(patient_id int64, advice_date int64, user_org_id int64) (doctor []*models.DoctorAdvice, err error) {
  1781. err = XTReadDB().Where("patient_id = ? and advice_date = ? and user_org_id =? and status= 1 and checker= 0", patient_id, advice_date, user_org_id).Find(&doctor).Error
  1782. return doctor, err
  1783. }
  1784. func UpdateDoctorAdviceList(id int64, checker int64) error {
  1785. advice := models.DoctorAdvice{}
  1786. err := XTWriteDB().Model(&advice).Where("id = ? and status =1", id).Updates(map[string]interface{}{"checker": checker, "check_state": 1, "check_time": time.Now().Unix()}).Error
  1787. return err
  1788. }
  1789. func UpdateLastMonitorRecordToday(accumulated_blood_volume float64, id int64) error {
  1790. err := XTWriteDB().Model(&models.MonitoringRecord{}).Where(" id = ? and status= 1", id).Update(map[string]interface{}{"accumulated_blood_volume": accumulated_blood_volume}).Error
  1791. return err
  1792. }
  1793. func GetDialysisFinish(user_org_id int64, record_date int64, module int64, patient_id int64) (models.XtDialysisFinish, error) {
  1794. finish := models.XtDialysisFinish{}
  1795. err := XTReadDB().Where("user_org_id = ? and record_date = ? and status =1 and module = ? and patient_id = ?", user_org_id, record_date, module, patient_id).Find(&finish).Error
  1796. return finish, err
  1797. }
  1798. func CreateDialysisFinish(finish models.XtDialysisFinish) error {
  1799. err := XTWriteDB().Create(&finish).Error
  1800. return err
  1801. }
  1802. func GetDialysisInformationSetting(user_org_id int64) (models.XtDialysisInformaitonSetting, error) {
  1803. setting := models.XtDialysisInformaitonSetting{}
  1804. err := XTReadDB().Where("user_org_id =? and status = 1", user_org_id).Find(&setting).Error
  1805. return setting, err
  1806. }
  1807. func CreatedDialysisInformation(information models.XtDialysisInformation) error {
  1808. err := XTWriteDB().Create(&information).Error
  1809. return err
  1810. }
  1811. func GetDialysisInoformationById(patient_id int64, record_date int64, user_org_id int64, module int64) (models.XtDialysisInformation, error) {
  1812. information := models.XtDialysisInformation{}
  1813. err := XTReadDB().Where("patient_id = ? and record_date = ? and user_org_id = ? and module = ? and status = 1", patient_id, record_date, user_org_id, module).Find(&information).Error
  1814. return information, err
  1815. }
  1816. func UpdateDialysisInformationById(patient_id int64, record_date int64, user_org_id int64, module int64, advice_date int64, remark string) error {
  1817. information := models.XtDialysisInformation{}
  1818. err := XTWriteDB().Model(&information).Where("patient_id = ? and record_date = ? and user_org_id =? and module = ? and status=1", patient_id, record_date, user_org_id, module).Update(map[string]interface{}{"application_date": advice_date, "remark": remark}).Error
  1819. return err
  1820. }
  1821. func UpdateDoctorAdviceByExcecute(id int64, user_org_id int64) error {
  1822. err := XTWriteDB().Model(models.DoctorAdvice{}).Where("id = ? and user_org_id = ? and status =1", id, user_org_id).Updates(map[string]interface{}{"execution_time": 0, "execution_staff": 0, "execution_state": 2}).Error
  1823. return err
  1824. }
  1825. func UpdateInformationSettingById(orgid int64, week_day int64) error {
  1826. err := XTWriteDB().Model(&models.XtDialysisInformaitonSetting{}).Where("user_org_id = ? and status =1", orgid).Updates(map[string]interface{}{"week_day": week_day}).Error
  1827. return err
  1828. }
  1829. func CreateInformationSetting(setting models.XtDialysisInformaitonSetting) error {
  1830. err := XTWriteDB().Create(&setting).Error
  1831. return err
  1832. }
  1833. func GetInformationSettingByOrgId(user_org_id int64) (models.XtDialysisInformaitonSetting, error) {
  1834. setting := models.XtDialysisInformaitonSetting{}
  1835. err := XTReadDB().Where("user_org_id = ? and status =1", user_org_id).Find(&setting).Error
  1836. return setting, err
  1837. }
  1838. func GetDialysisInformationSettingList(user_org_id int64) (infor []*models.XtDialysisInformaitonSetting, err error) {
  1839. err = XTReadDB().Where("user_org_id = ? and status =1", user_org_id).Find(&infor).Error
  1840. return infor, err
  1841. }
  1842. func GetDialysisInformationIsNoCheck(user_org_id int64, limit int64, page int64, application_status int64) (infor []*models.DialysisInformation, total int64, err error) {
  1843. db := XTReadDB().Model(&infor).Where("status= 1")
  1844. if application_status > 0 {
  1845. db = db.Where("application_status = ?", application_status)
  1846. }
  1847. if user_org_id > 0 {
  1848. db = db.Where("user_org_id = ?", user_org_id)
  1849. }
  1850. err = db.Count(&total).Offset(limit * (page - 1)).Limit(limit).Find(&infor).Error
  1851. return infor, total, err
  1852. }
  1853. func GetDialysisInformationIsNoCheckOne(user_org_id int64, limit int64, page int64, application_status int64) (infor []*models.DialysisInformation, total int64, err error) {
  1854. db := XTReadDB().Model(&infor).Where("status= 1")
  1855. if application_status > 0 {
  1856. db = db.Where("application_status = ? or application_status= 3", application_status)
  1857. }
  1858. if user_org_id > 0 {
  1859. db = db.Where("user_org_id = ?", user_org_id)
  1860. }
  1861. err = db.Count(&total).Offset(limit * (page - 1)).Limit(limit).Find(&infor).Error
  1862. return infor, total, err
  1863. }
  1864. func CheckDialysisInformation(id int64, application_status int64, timenow int64, checker int64) (models.DialysisInformation, error) {
  1865. information := models.DialysisInformation{}
  1866. err := XTWriteDB().Model(&information).Where("id=? and status = 1", id).Updates(map[string]interface{}{"application_status": application_status, "check_time": timenow, "checker": checker}).Error
  1867. return information, err
  1868. }
  1869. func GetDialysisWatchByKeywordFlow(orgID int64, keyword string, schedulType int64, partitionType int64, page int64, limit int64, start int64, end int64) ([]*models.DialysisScheduleFlow, error, int64) {
  1870. var patients []*models.Patients
  1871. 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
  1872. if getPatientErr != nil {
  1873. return nil, getPatientErr, 0
  1874. }
  1875. patientIDs := make([]int64, len(patients))
  1876. for index, patient := range patients {
  1877. patientIDs[index] = patient.ID
  1878. }
  1879. db := readDb.Model(&models.DialysisScheduleFlow{})
  1880. if start > 0 && end > 0 {
  1881. db = db.Preload("DeviceNumber", "org_id = ?", orgID).
  1882. // Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  1883. Preload("TreatmentMode", "status = 1").
  1884. Preload("DialysisFinish", "status = 1 AND user_org_id = ? and record_date >= ? and record_date<=?", orgID, start, end).
  1885. Preload("MonitorPatients", "status = 1 AND user_org_id = ?", orgID)
  1886. } else {
  1887. db = db.Preload("DeviceNumber", "org_id = ?", orgID).
  1888. Preload("TreatmentMode", "status = 1").
  1889. Preload("DialysisFinish", "status = 1 AND user_org_id = ?", orgID).
  1890. Preload("MonitorPatients", "status = 1 AND user_org_id = ?", orgID)
  1891. }
  1892. db = db.Where("xt_schedule.status = 1 AND patient_id in (?)", patientIDs)
  1893. if schedulType > 0 {
  1894. db = db.Where("schedule_type = ?", schedulType)
  1895. }
  1896. if start > 0 {
  1897. db = db.Where("schedule_date >= ?", start)
  1898. }
  1899. if end > 0 {
  1900. db = db.Where("schedule_date<=?", end)
  1901. }
  1902. if partitionType > 0 {
  1903. 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)
  1904. }
  1905. var schedules []*models.DialysisScheduleFlow
  1906. total := int64(0)
  1907. err := db.Count(&total).Offset(limit * (page - 1)).Limit(limit).Order("schedule_date desc").Find(&schedules).Error
  1908. return schedules, err, total
  1909. }
  1910. func GetDialysisWatchFlow(orgID int64, schedulDate int64, schedulType int64, partitionType int64, page int64, limit int64, start int64, end int64) (schedule []*models.DialysisScheduleFlow, err error, total int64) {
  1911. db := readDb.Model(&models.DialysisScheduleFlow{})
  1912. if schedulDate > 0 {
  1913. db = db.Preload("DeviceNumber", "org_id = ?", orgID).
  1914. Preload("TreatmentMode", "status = 1").
  1915. Preload("DialysisFinish", "status = 1 AND user_org_id = ? and record_date =?", orgID, schedulDate).
  1916. Preload("MonitorPatients", "status = 1 AND user_org_id = ?", orgID)
  1917. db = db.Where("xt_schedule.status = 1")
  1918. db = db.Where("schedule_date = ?", schedulDate)
  1919. }
  1920. if schedulType > 0 {
  1921. db = db.Where("schedule_type = ?", schedulType)
  1922. }
  1923. if start > 0 {
  1924. db = db.Where("schedule_date >= ?", start)
  1925. }
  1926. if end > 0 {
  1927. db = db.Where("schedule_date <= ?", end)
  1928. }
  1929. if orgID > 0 {
  1930. db = db.Where("user_org_id = ?", orgID)
  1931. }
  1932. if partitionType > 0 {
  1933. db = db.Joins("inner join xt_device_number on xt_device_number.id = xt_schedule.bed_id and xt_device_number.zone_id = ?", partitionType)
  1934. }
  1935. offset := (page - 1) * limit
  1936. err = db.Count(&total).Offset(offset).Limit(limit).Order("bed_id desc").Find(&schedule).Error
  1937. return schedule, err, total
  1938. }
  1939. func GetPrescriptionInfo(user_org_id int64, patient_id int64, record_date int64) (models.HisPrescriptionInfo, error) {
  1940. info := models.HisPrescriptionInfo{}
  1941. err := XTReadDB().Where("user_org_id = ? and patient_id =? and record_date = ? and status=1", user_org_id, patient_id, record_date).Find(&info).Error
  1942. return info, err
  1943. }
  1944. func FindLastSchedule(user_org_id int64, patient_id int64, schedule_date int64) (models.XtSchedules, error) {
  1945. schedules := models.XtSchedules{}
  1946. err := XTReadDB().Where("user_org_id = ? and patient_id = ? and schedule_date < ? and status =1", user_org_id, patient_id, schedule_date).Order("schedule_date desc").First(&schedules).Error
  1947. return schedules, err
  1948. }
  1949. func GetMonitorPatients(patient_id int64) (*models.MonitorPatients, error) {
  1950. patients := models.MonitorPatients{}
  1951. err := XTReadDB().Where("id = ? and status = 1", patient_id).Find(&patients).Error
  1952. return &patients, err
  1953. }
  1954. func GetMonitorDialysisOrderObj(patient_id int64, schedule_date int64) (*models.MonitorDialysisOrder, error) {
  1955. order := models.MonitorDialysisOrder{}
  1956. err := XTReadDB().Where("patient_id = ? and dialysis_date = ? and status = 1", patient_id, schedule_date).Find(&order).Error
  1957. return &order, err
  1958. }
  1959. func GetMonitorDialysisPrescription(patient_id int64, record_date int64) (*models.DialysisPrescription, error) {
  1960. prescription := models.DialysisPrescription{}
  1961. err := XTReadDB().Where("patient_id = ? and status =1 and record_date = ?", patient_id, record_date).Find(&prescription).Error
  1962. return &prescription, err
  1963. }
  1964. func GetMonitorPredialysisEvaluation(patient_id int64, record_date int64) (*models.PredialysisEvaluation, error) {
  1965. evaluation := models.PredialysisEvaluation{}
  1966. err := XTReadDB().Where("patient_id = ? and status =1 and assessment_date = ?", patient_id, record_date).Find(&evaluation).Error
  1967. return &evaluation, err
  1968. }
  1969. func GetMonitorAssessmentAfterDislysis(patient_id int64, record_date int64) (*models.AssessmentAfterDislysis, error) {
  1970. dislysis := models.AssessmentAfterDislysis{}
  1971. err = XTReadDB().Where("patient_id = ? and status =1 and assessment_date = ?", patient_id, record_date).Find(&dislysis).Error
  1972. return &dislysis, err
  1973. }
  1974. func GetMonitoringRecordList(patient_id int64, record_date int64) (record []*models.MonitoringRecord, err error) {
  1975. err = XTReadDB().Where("patient_id = ? and monitoring_date=? and status =1", patient_id, record_date).Find(&record).Error
  1976. return record, err
  1977. }
  1978. func GetDoctorAdviceListNoExecution(patient_id int64, dialysis_date int64, user_org_id int64) (doctor []*models.XtDoctorAdvice, err error) {
  1979. err = XTReadDB().Where("patient_id = ? and advice_date = ? and user_org_id = ? and execution_state = 2 and status =1", patient_id, dialysis_date, user_org_id).Find(&doctor).Error
  1980. return doctor, err
  1981. }
  1982. func UpdateDoctorAdviceNoExecution(id int64, creator int64, end_time int64) error {
  1983. err := XTWriteDB().Model(&models.XtDoctorAdvice{}).Where("id = ? and status =1", id).Updates(map[string]interface{}{"execution_state": 1, "execution_staff": creator, "execution_time": end_time}).Error
  1984. return err
  1985. }
  1986. func CreatePrescriptionLog(log models.XtDialysisPrescriptionLog) error {
  1987. ut := writeDb.Begin()
  1988. err := ut.Create(&log).Error
  1989. if err != nil {
  1990. ut.Rollback()
  1991. return err
  1992. }
  1993. ut.Commit()
  1994. return err
  1995. }
  1996. func CreateBeforLog(log models.XtAssessmentBeforeDislysisLog) error {
  1997. err := XTWriteDB().Create(&log).Error
  1998. return err
  1999. }
  2000. func CreateAfterDialysisLog(log models.XtAssessmentAfterDialysisLog) error {
  2001. err := XTWriteDB().Create(&log).Error
  2002. return err
  2003. }
  2004. func CreateDoctorAdviceLog(log models.XtDoctorAdviceLog) error {
  2005. err := XTWriteDB().Create(&log).Error
  2006. return err
  2007. }
  2008. func CreateMonitorRecordLog(log models.XtMonitorRecordLog) error {
  2009. err := XTWriteDB().Create(&log).Error
  2010. return err
  2011. }
  2012. func GetAllHisAdviceList(patient_id int64, advice_date int64, user_org_id int64) (advice []*models.HisDoctorAdvice, err error) {
  2013. err = XTReadDB().Where("patient_id = ? and advice_date =? and user_org_id = ? and status =1", patient_id, advice_date, user_org_id).Find(&advice).Error
  2014. return advice, err
  2015. }
  2016. func BatchCheckHisAdvice(id int64, creater int64) error {
  2017. err := XTWriteDB().Model(&models.HisDoctorAdvice{}).Where("id = ? and status=1", id).Updates(map[string]interface{}{"checker": creater, "check_state": 1, "check_time": time.Now().Unix()}).Error
  2018. return err
  2019. }
  2020. func BatchAdviceList(id int64, creater int64) error {
  2021. err := XTWriteDB().Model(&models.DoctorAdvice{}).Where("id = ? and status =1", id).Updates(map[string]interface{}{"checker": creater, "check_state": 1, "check_time": time.Now().Unix()}).Error
  2022. return err
  2023. }
  2024. func GetMobileProjectList(patient_id int64, advice_date int64, user_org_id int64) (project []*models.HisPrescriptionProject, err error) {
  2025. err = XTReadDB().Where("patient_id = ? and status=1 and record_date = ? and user_org_id = ?", patient_id, advice_date, user_org_id).Find(&project).Error
  2026. return project, err
  2027. }
  2028. func BatchCheckProject(id int64, creater int64) error {
  2029. err = XTWriteDB().Model(&models.HisPrescriptionProject{}).Where("id = ? and status =1", id).Updates(map[string]interface{}{"checker": creater, "check_state": 1, "check_time": time.Now().Unix()}).Error
  2030. return err
  2031. }
  2032. func GetAllAdviceList(patient_id int64, advice_date int64, user_org_id int64) (advice []*models.DoctorAdvice, err error) {
  2033. err = XTReadDB().Where("patient_id = ? and advice_date = ? and user_org_id = ? and status=1", patient_id, advice_date, user_org_id).Find(&advice).Error
  2034. return advice, err
  2035. }
  2036. func GetHistoryWarehouseOutInfo(patient_id int64, user_org_id int64, record_time int64, good_id int64, tx *gorm.DB) (outInfo []*models.WarehouseOutInfo, err error) {
  2037. err = tx.Where("patient_id = ? and org_id = ? and sys_record_time = ? and status =1 and good_id = ?", patient_id, user_org_id, record_time, good_id).Find(&outInfo).Error
  2038. if err != gorm.ErrRecordNotFound {
  2039. if err != nil {
  2040. tx.Rollback()
  2041. return
  2042. }
  2043. }
  2044. return outInfo, err
  2045. }
  2046. func GetDialysisAutoGoodInfo(patient_id int64, record_time int64, org_id int64, tx *gorm.DB, good_id int64) (detail []*models.AutomaticReduceDetail, err error) {
  2047. err = tx.Where("patient_id = ? and record_time=? and org_id=? and status= 1 and good_id = ?", patient_id, record_time, org_id, good_id).Find(&detail).Error
  2048. if err != gorm.ErrRecordNotFound {
  2049. if err != nil {
  2050. tx.Commit()
  2051. return
  2052. }
  2053. }
  2054. return detail, err
  2055. }
  2056. func CreateDialysisPrepare(prepare models.DialysisBeforePrepare, tx *gorm.DB) error {
  2057. err := tx.Create(&prepare).Error
  2058. if err != nil {
  2059. tx.Commit()
  2060. return err
  2061. }
  2062. return err
  2063. }
  2064. func ModfiyAddDialysisAuto(patient_id int64, record_date int64, count int64, good_id int64, tx *gorm.DB) error {
  2065. err = tx.Model(&models.DialysisBeforePrepare{}).Where("patient_id = ? and record_date = ? and good_id = ? and status = 1", patient_id, record_date, good_id).UpdateColumn("count", gorm.Expr("count + ?", count)).Error
  2066. if err != nil {
  2067. tx.Rollback()
  2068. return err
  2069. }
  2070. return err
  2071. }
  2072. func ModfiyReduceDialysisAuto(patient_id int64, record_date int64, count int64, good_id int64, tx *gorm.DB) error {
  2073. err = tx.Model(&models.DialysisBeforePrepare{}).Where("patient_id = ? and record_date = ? and good_id = ? and status = 1", patient_id, record_date, good_id).UpdateColumn("count", gorm.Expr("count - ?", count)).Error
  2074. if err != nil {
  2075. tx.Rollback()
  2076. return err
  2077. }
  2078. return err
  2079. }
  2080. func GetHisExecutionDoctorsThree(orgid int64, patientid int64, id int64, tx *gorm.DB) (doctorAdvice []*models.HisDoctorAdviceInfo, err error) {
  2081. err = tx.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
  2082. if err != gorm.ErrRecordNotFound {
  2083. if err != nil {
  2084. tx.Rollback()
  2085. return doctorAdvice, err
  2086. }
  2087. }
  2088. return doctorAdvice, err
  2089. }
  2090. func CreateMobileAdivce(advice models.DoctorAdvice) error {
  2091. err := XTWriteDB().Create(&advice).Error
  2092. return err
  2093. }
  2094. func CreateMobileAdivceOne(advice models.XtDoctorAdviceOne) error {
  2095. err := XTWriteDB().Create(&advice).Error
  2096. return err
  2097. }
  2098. func GetLongDoctorAdviceByPatient(patient_id int64, org_id int64) (advice []*models.XtDoctorAdvice, err error) {
  2099. err = XTReadDB().Where("patient_id = ? and user_org_id = ? and status=1 and advice_type =1", patient_id, org_id).Order("start_time asc").Find(&advice).Error
  2100. return advice, err
  2101. }
  2102. func GetLastLongAdviceList(user_org_id int64) (models.XtDoctorAdvice, error) {
  2103. advice := models.XtDoctorAdvice{}
  2104. err := XTReadDB().Where("user_org_id =? and status=1 and advice_type =1", user_org_id).Last(&advice).Error
  2105. return advice, err
  2106. }
  2107. func DeleteSelfAdviceSubAdvice(id int64) (models.XtDoctorAdviceOne, error) {
  2108. adviceOne := models.XtDoctorAdviceOne{}
  2109. err2 := XTWriteDB().Model(&adviceOne).Where("id = ? and status=1", id).Updates(map[string]interface{}{"status": 0}).Error
  2110. return adviceOne, err2
  2111. }
  2112. func UpdateMobileDoctorAdviceById(id int64, advice models.XtDoctorAdviceOne) error {
  2113. err := XTWriteDB().Model(&models.XtDoctorAdviceOne{}).Where("id = ? and status=1", id).Updates(map[string]interface{}{"advice_name": advice.AdviceName, "advice_date": advice.AdviceDate, "start_time": advice.StartTime, "advice_desc": advice.AdviceDesc, "single_dose": advice.SingleDose, "single_dose_unit": advice.SingleDoseUnit, "prescribing_number": advice.PrescribingNumber, "prescribing_number_unit": advice.PrescribingNumberUnit, "delivery_way": advice.DeliveryWay, "execution_frequency": advice.ExecutionFrequency, "remark": advice.Remark}).Error
  2114. return err
  2115. }
  2116. func UpdateMobileDoctorAdviceByIdOne(id int64, advice models.XtDoctorAdviceOne) error {
  2117. err := XTWriteDB().Model(&models.XtDoctorAdviceOne{}).Where("id = ? and status=1", id).Updates(map[string]interface{}{"advice_name": advice.AdviceName, "advice_date": advice.AdviceDate, "start_time": advice.StartTime, "advice_desc": advice.AdviceDesc, "single_dose": advice.SingleDose, "single_dose_unit": advice.SingleDoseUnit, "prescribing_number": advice.PrescribingNumber, "prescribing_number_unit": advice.PrescribingNumberUnit, "delivery_way": advice.DeliveryWay, "execution_frequency": advice.ExecutionFrequency, "remark": advice.Remark, "frequency_type": advice.FrequencyType, "week_day": advice.WeekDay, "day_count": advice.DayCount}).Error
  2118. return err
  2119. }
  2120. func CreateMobileMGroupAdvice(orgId int64, advices []*models.GroupAdvice, groupNo int64) (list []*models.GroupAdvice, err error) {
  2121. if groupNo <= 0 {
  2122. group := GetMaxAdviceGroupID(orgId)
  2123. groupNo = group + 1
  2124. }
  2125. tx := writeDb.Begin()
  2126. defer func() {
  2127. if r := recover(); r != nil {
  2128. tx.Rollback()
  2129. }
  2130. }()
  2131. for _, advice := range advices {
  2132. advice.GroupNo = groupNo
  2133. advice.Status = 1
  2134. template_id := strconv.FormatInt(groupNo, 10)
  2135. advice.TemplateId = "M" + template_id
  2136. if err = tx.Create(advice).Error; err != nil {
  2137. tx.Rollback()
  2138. return
  2139. }
  2140. list = append(list, advice)
  2141. if len(advice.Children) > 0 {
  2142. for _, child := range advice.Children {
  2143. child.GroupNo = groupNo
  2144. child.ParentId = advice.ID
  2145. fmt.Println(child)
  2146. if err = tx.Create(&child).Error; err != nil {
  2147. tx.Rollback()
  2148. return
  2149. }
  2150. list = append(list, child)
  2151. }
  2152. }
  2153. }
  2154. tx.Commit()
  2155. return
  2156. }