dialysis_service.go 124KB

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