dialysis_service.go 110KB

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