patient_service.go 134KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318
  1. package service
  2. import (
  3. "XT_New/models"
  4. "XT_New/utils"
  5. "encoding/json"
  6. "fmt"
  7. "strconv"
  8. "strings"
  9. "time"
  10. "github.com/jinzhu/gorm"
  11. )
  12. // GetPatientList 返回患者的列表
  13. func GetPatientList(orgID int64, keywords string, page, limit, schedulType, bindingState, lapseto, source, startTime, endTime, contagion, reimbursementWay, isscheduling, isprescription int64, isStartTime, isEndTime bool, patientSoureType int64, nurse int64) (patients []*models.Patients, total int64, err error) {
  14. db := readDb.Table("xt_patients as p").Where("p.status=1")
  15. if orgID > 0 {
  16. db = db.Where("p.user_org_id=?", orgID)
  17. }
  18. if len(keywords) > 0 {
  19. likeKey := "%" + keywords + "%"
  20. db = db.Where("p.name LIKE ? OR p.dialysis_no LIKE ? OR p.first_letter LIKE ?", likeKey, likeKey, likeKey)
  21. }
  22. if schedulType > 0 {
  23. db = db.Joins("JOIN xt_schedule as s ON s.patient_id=p.id")
  24. db = db.Where("s.status=1 and s.schedule_type=?", schedulType)
  25. }
  26. if contagion > 0 {
  27. db = db.Joins("JOIN xt_patients_infectious_diseases as xpid ON xpid.patient_id=p.id")
  28. db = db.Where("xpid.disease_id=? and xpid.status=1", contagion)
  29. }
  30. if isscheduling == 1 {
  31. db = db.Where("EXISTS (?)", readDb.Table("xt_schedule as iss").Where("iss.patient_id=p.id and iss.status=1").QueryExpr())
  32. } else if isscheduling == 2 {
  33. db = db.Where("NOT EXISTS (?)", readDb.Table("xt_schedule as iss").Where("iss.patient_id=p.id and iss.status=1").QueryExpr())
  34. }
  35. if isprescription == 1 {
  36. db = db.Where("EXISTS (?)", readDb.Table("xt_dialysis_prescription as xdp").Where("xdp.patient_id=p.id and xdp.status=1").QueryExpr())
  37. } else if isprescription == 2 {
  38. db = db.Where("NOT EXISTS (?)", readDb.Table("xt_dialysis_prescription as xdp").Where("xdp.patient_id=p.id and xdp.status=1").QueryExpr())
  39. }
  40. if bindingState > 0 {
  41. db = db.Where("p.binding_state=?", bindingState)
  42. }
  43. if lapseto > 0 {
  44. db = db.Where("p.lapseto=?", lapseto)
  45. }
  46. if source > 0 {
  47. db = db.Where("p.source=?", source)
  48. }
  49. if reimbursementWay > 0 {
  50. db = db.Where("p.reimbursement_way_id = ?", reimbursementWay)
  51. }
  52. if isStartTime {
  53. db = db.Where("p.created_time>=?", startTime)
  54. }
  55. if isEndTime {
  56. db = db.Where("p.created_time<=?", endTime)
  57. }
  58. if patientSoureType > 0 {
  59. db = db.Where("p.patient_source =?", patientSoureType)
  60. }
  61. if nurse > 0 {
  62. db = db.Where("p.head_nurse_id = ?", nurse)
  63. }
  64. offset := (page - 1) * limit
  65. if lapseto == 2 || lapseto == 3 {
  66. err = db.Order("p.death_time desc").Select(" p.id, p.user_org_id, p.user_id, p.patient_type, p.dialysis_no, p.admission_number, p.source, p.lapseto, p.partition_id, p.bed_id, p.name, p.alias, p.gender, p.marital_status, p.id_card_no, p.birthday, p.reimbursement_way_id, p.health_care_type, p.health_care_no, p.health_care_due_date, p.height, p.blood_type, p.rh, p.health_care_due_alert_date, p.education_level, p.profession, p.phone, p.home_telephone, p.relative_phone, p.relative_relations, p.home_address, p.work_unit, p.unit_address, p.children, p.receiving_date, p.is_hospital_first_dialysis, p.first_dialysis_date, p.first_dialysis_hospital, p.induction_period, p.initial_dialysis, p.total_dialysis, p.attending_doctor_id, p.head_nurse_id, p.evaluate, p.diagnose, p.remark, p.registrars_id, p.registrars, p.qr_code, p.binding_state, p.status, p.created_time, p.updated_time,p.user_sys_before_count,p.out_reason,p.death_time,p.is_infectious").Group("p.id").Count(&total).Offset(offset).Limit(limit).Find(&patients).Error
  67. }
  68. if lapseto != 2 && lapseto != 3 {
  69. if orgID != 10579 {
  70. err = db.Order("p.id desc").Select(" p.id, p.user_org_id, p.user_id, p.patient_type, p.dialysis_no, p.admission_number, p.source, p.lapseto, p.partition_id, p.bed_id, p.name, p.alias, p.gender, p.marital_status, p.id_card_no, p.birthday, p.reimbursement_way_id, p.health_care_type, p.health_care_no, p.health_care_due_date, p.height, p.blood_type, p.rh, p.health_care_due_alert_date, p.education_level, p.profession, p.phone, p.home_telephone, p.relative_phone, p.relative_relations, p.home_address, p.work_unit, p.unit_address, p.children, p.receiving_date, p.is_hospital_first_dialysis, p.first_dialysis_date, p.first_dialysis_hospital, p.induction_period, p.initial_dialysis, p.total_dialysis, p.attending_doctor_id, p.head_nurse_id, p.evaluate, p.diagnose, p.remark, p.registrars_id, p.registrars, p.qr_code, p.binding_state, p.status, p.created_time, p.updated_time,p.user_sys_before_count,p.out_reason,p.death_time,p.is_infectious").Group("p.id").Count(&total).Offset(offset).Limit(limit).Find(&patients).Error
  71. }
  72. if orgID == 10579 {
  73. err = db.Order("p.dialysis_no asc").Select(" p.id, p.user_org_id, p.user_id, p.patient_type, p.dialysis_no, p.admission_number, p.source, p.lapseto, p.partition_id, p.bed_id, p.name, p.alias, p.gender, p.marital_status, p.id_card_no, p.birthday, p.reimbursement_way_id, p.health_care_type, p.health_care_no, p.health_care_due_date, p.height, p.blood_type, p.rh, p.health_care_due_alert_date, p.education_level, p.profession, p.phone, p.home_telephone, p.relative_phone, p.relative_relations, p.home_address, p.work_unit, p.unit_address, p.children, p.receiving_date, p.is_hospital_first_dialysis, p.first_dialysis_date, p.first_dialysis_hospital, p.induction_period, p.initial_dialysis, p.total_dialysis, p.attending_doctor_id, p.head_nurse_id, p.evaluate, p.diagnose, p.remark, p.registrars_id, p.registrars, p.qr_code, p.binding_state, p.status, p.created_time, p.updated_time,p.user_sys_before_count,p.out_reason,p.death_time,p.is_infectious").Group("p.id").Count(&total).Offset(offset).Limit(limit).Find(&patients).Error
  74. }
  75. }
  76. return
  77. }
  78. // GetAllPatientList 返回全部患者的列表
  79. func GetAllPatientList(orgID int64) (patients []*models.Patients, total int64, err error) {
  80. db := readDb.Table("xt_patients as p").Where("p.user_org_id=? and p.status=1", orgID)
  81. err = db.Select(" p.id, p.user_org_id, p.user_id, p.patient_type, p.dialysis_no, p.admission_number, p.source, p.lapseto, p.partition_id, p.bed_id, p.name, p.alias, p.gender, p.marital_status, p.id_card_no, p.birthday, p.reimbursement_way_id, p.health_care_type, p.health_care_no, p.health_care_due_date, p.height, p.blood_type, p.rh, p.health_care_due_alert_date, p.education_level, p.profession, p.phone, p.home_telephone, p.relative_phone, p.relative_relations, p.home_address, p.work_unit, p.unit_address, p.children, p.receiving_date, p.is_hospital_first_dialysis, p.first_dialysis_date, p.first_dialysis_hospital, p.induction_period, p.initial_dialysis, p.total_dialysis, p.attending_doctor_id, p.head_nurse_id, p.evaluate, p.diagnose, p.remark, p.registrars_id, p.registrars, p.qr_code, p.binding_state, p.status, p.created_time, p.updated_time").Count(&total).Find(&patients).Error
  82. return
  83. }
  84. func GetAllPatientListTwenty(orgID int64) (patients []*models.Patients, total int64, err error) {
  85. db := readDb.Table("xt_patients as p").Where("p.user_org_id=? and p.status=1 and p.lapseto = 1", orgID)
  86. err = db.Select(" p.id, p.user_org_id, p.user_id, p.patient_type, p.dialysis_no, p.admission_number, p.source, p.lapseto, p.partition_id, p.bed_id, p.name, p.alias, p.gender, p.marital_status, p.id_card_no, p.birthday, p.reimbursement_way_id, p.health_care_type, p.health_care_no, p.health_care_due_date, p.height, p.blood_type, p.rh, p.health_care_due_alert_date, p.education_level, p.profession, p.phone, p.home_telephone, p.relative_phone, p.relative_relations, p.home_address, p.work_unit, p.unit_address, p.children, p.receiving_date, p.is_hospital_first_dialysis, p.first_dialysis_date, p.first_dialysis_hospital, p.induction_period, p.initial_dialysis, p.total_dialysis, p.attending_doctor_id, p.head_nurse_id, p.evaluate, p.diagnose, p.remark, p.registrars_id, p.registrars, p.qr_code, p.binding_state, p.status, p.created_time, p.updated_time").Count(&total).Find(&patients).Error
  87. return
  88. }
  89. func GetAllPatientListSix(orgID int64) (patients []*models.MSchedualPatientList, err error) {
  90. err = XTReadDB().Model(&patients).Where("user_org_id = ? and status = 1", orgID).Find(&patients).Error
  91. return patients, err
  92. }
  93. // GetAllPatientList 返回全部患者的列表
  94. func GetAllPatientListByList(orgID int64) (patients []*models.MSchedualPatientList, err error) {
  95. redis := RedisClient()
  96. defer redis.Close()
  97. key := strconv.FormatInt(orgID, 10) + ":" + ":patient_list_all"
  98. patient_info_str, _ := redis.Get(key).Result()
  99. if len(patient_info_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  100. err = readDb.Model(&models.MSchedualPatientList{}).Where("user_org_id=? and status=1 and lapseto = 1", orgID).Find(&patients).Error
  101. if err != nil {
  102. if err == gorm.ErrRecordNotFound {
  103. if len(patients) <= 0 {
  104. redis.Set(key, "null", time.Second*60*60*18)
  105. }
  106. return nil, nil
  107. } else {
  108. return nil, err
  109. }
  110. } else {
  111. if len(patients) > 0 {
  112. //缓存数据
  113. patient_info_json, err := json.Marshal(&patients)
  114. if err == nil {
  115. redis.Set(key, patient_info_json, time.Second*60*60*18)
  116. }
  117. } else {
  118. redis.Set(key, "null", time.Second*60*60*18)
  119. }
  120. return patients, nil
  121. }
  122. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  123. if patient_info_str == "null" {
  124. return nil, nil
  125. } else {
  126. json.Unmarshal([]byte(patient_info_str), &patients)
  127. return patients, nil
  128. }
  129. }
  130. }
  131. func GetAllDevicetByListSix(orgID int64) (devices []*models.MDeviceNumberForList, err error) {
  132. err = readDb.Model(&models.MDeviceNumberForList{}).Preload("Zone", "status = 1 AND org_id = ?", orgID).Where("org_id=?", orgID).Find(&devices).Error
  133. return devices, err
  134. }
  135. func GetMonitorByListSix(orgID int64, monitor_date int64) (monitor []models.VMMonitoringRecord, err error) {
  136. err = readDb.Where("user_org_id = ? and monitor_date = ? and status =1", orgID, monitor_date).Find(&monitor).Error
  137. return monitor, err
  138. }
  139. func GetAllDevicetByList(orgID int64) (devices []*models.MDeviceNumberForList, err error) {
  140. redis := RedisClient()
  141. defer redis.Close()
  142. key := strconv.FormatInt(orgID, 10) + ":" + ":device_list_all"
  143. device_list_str, _ := redis.Get(key).Result()
  144. if len(device_list_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  145. err = readDb.Model(&models.MDeviceNumberForList{}).Preload("Zone", "status = 1 AND org_id = ?", orgID).Where("org_id=? and status=1 ", orgID).Find(&devices).Error
  146. if err != nil {
  147. if err == gorm.ErrRecordNotFound {
  148. if len(devices) <= 0 {
  149. redis.Set(key, "null", time.Second*60*60*18)
  150. }
  151. return nil, nil
  152. } else {
  153. return nil, err
  154. }
  155. } else {
  156. if len(devices) > 0 {
  157. //缓存数据
  158. device_list_str, err := json.Marshal(&devices)
  159. if err == nil {
  160. redis.Set(key, device_list_str, time.Second*60*60*18)
  161. }
  162. } else {
  163. redis.Set(key, "null", time.Second*60*60*18)
  164. }
  165. return devices, nil
  166. }
  167. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  168. if device_list_str == "null" {
  169. return nil, nil
  170. } else {
  171. json.Unmarshal([]byte(device_list_str), &devices)
  172. return devices, nil
  173. }
  174. }
  175. }
  176. func GetAllAdvicestByList(orgID int64, scheduleDate int64) (advices []models.VMDoctorAdviceForList, err error) {
  177. redis := RedisClient()
  178. defer redis.Close()
  179. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":advice_list_all"
  180. advice_list_str, _ := redis.Get(key).Result()
  181. if len(advice_list_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  182. err = readDb.Model(&models.VMDoctorAdviceForList{}).Where("status = 1 AND user_org_id = ? AND advice_type = 2 AND advice_date = ? ", orgID, scheduleDate).Find(&advices).Error
  183. if err != nil {
  184. if err == gorm.ErrRecordNotFound {
  185. if len(advices) <= 0 {
  186. redis.Set(key, "null", time.Second*60*60*18)
  187. }
  188. return nil, nil
  189. } else {
  190. return nil, err
  191. }
  192. } else {
  193. if len(advices) > 0 {
  194. //缓存数据
  195. advice_list_str, err := json.Marshal(&advices)
  196. if err == nil {
  197. redis.Set(key, advice_list_str, time.Second*60*60*18)
  198. }
  199. } else {
  200. redis.Set(key, "null", time.Second*60*60*18)
  201. }
  202. return advices, nil
  203. }
  204. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  205. if advice_list_str == "null" {
  206. return nil, nil
  207. } else {
  208. json.Unmarshal([]byte(advice_list_str), &advices)
  209. return advices, nil
  210. }
  211. }
  212. }
  213. func GetAllPrescriptionsByList(orgID int64, scheduleDate int64) (prescriptions []*models.DialysisPrescriptionList, err error) {
  214. err = readDb.Where("status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).Find(&prescriptions).Error
  215. return prescriptions, err
  216. }
  217. //func GetAllPrescriptionsByList(orgID int64, scheduleDate int64) (prescriptions []*models.DialysisPrescriptionList, err error) {
  218. // redis := RedisClient()
  219. // defer redis.Close()
  220. // key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":prescriptions_list_all"
  221. // prescriptions_list_all, _ := redis.Get(key).Result()
  222. // redis.Set(prescriptions_list_all, "", time.Second)
  223. // if len(prescriptions_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  224. // err = readDb.Where("status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).Find(&prescriptions).Error
  225. // if err != nil {
  226. // if err == gorm.ErrRecordNotFound {
  227. // if len(prescriptions) <= 0 {
  228. // redis.Set(key, "null", time.Second*60*60*18)
  229. // }
  230. // return nil, nil
  231. // } else {
  232. // return nil, err
  233. // }
  234. // } else {
  235. // if len(prescriptions) > 0 {
  236. // //缓存数据
  237. // prescriptions_list_all, err := json.Marshal(&prescriptions)
  238. // if err == nil {
  239. // redis.Set(key, prescriptions_list_all, time.Second*60*60*18)
  240. // }
  241. // } else {
  242. // redis.Set(key, "null", time.Second*60*60*18)
  243. // }
  244. // return prescriptions, nil
  245. // }
  246. // } else { //缓存数据了数据,将redis缓存的json字符串转为map
  247. // if prescriptions_list_all == "null" {
  248. // return nil, nil
  249. // } else {
  250. // json.Unmarshal([]byte(prescriptions_list_all), &prescriptions)
  251. // return prescriptions, nil
  252. // }
  253. //
  254. // }
  255. //}
  256. func GetAllAssessmentBeforesByList(orgID int64, scheduleDate int64) (assessmentBefores []*models.PredialysisEvaluationList, err error) {
  257. redis := RedisClient()
  258. defer redis.Close()
  259. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":assessment_befores_list_all"
  260. assessment_befores_list_all, _ := redis.Get(key).Result()
  261. if len(assessment_befores_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  262. err = readDb.Where("status = 1 AND user_org_id = ? AND assessment_date = ? ", orgID, scheduleDate).Find(&assessmentBefores).Error
  263. if err != nil {
  264. if err == gorm.ErrRecordNotFound {
  265. if len(assessmentBefores) <= 0 {
  266. redis.Set(key, "null", time.Second*60*60*18)
  267. }
  268. return nil, nil
  269. } else {
  270. return nil, err
  271. }
  272. } else {
  273. if len(assessmentBefores) > 0 {
  274. //缓存数据
  275. assessment_befores_list_all, err := json.Marshal(&assessmentBefores)
  276. if err == nil {
  277. redis.Set(key, assessment_befores_list_all, time.Second*60*60*18)
  278. }
  279. } else {
  280. redis.Set(key, "null", time.Second*60*60*18)
  281. }
  282. return assessmentBefores, nil
  283. }
  284. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  285. if assessment_befores_list_all == "null" {
  286. return nil, nil
  287. } else {
  288. json.Unmarshal([]byte(assessment_befores_list_all), &assessmentBefores)
  289. return assessmentBefores, nil
  290. }
  291. }
  292. }
  293. func GetAllDialysisOrdersByList(orgID int64, scheduleDate int64) (dialysisOrders []*models.MDialysisOrderForList, err error) {
  294. err = readDb.Model(&models.MDialysisOrderForList{}).Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).Where("status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, scheduleDate).Find(&dialysisOrders).Error
  295. return dialysisOrders, err
  296. }
  297. //func GetAllDialysisOrdersByList(orgID int64, scheduleDate int64) (dialysisOrders []*models.MDialysisOrderForList, err error) {
  298. // redis := RedisClient()
  299. // defer redis.Close()
  300. // key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":dialysis_orders_list_all"
  301. // dialysis_orders_list_all, _ := redis.Get(key).Result()
  302. // redis.Set(dialysis_orders_list_all, "", time.Second)
  303. // if len(dialysis_orders_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  304. // err = readDb.Model(&models.MDialysisOrderForList{}).Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).Where("status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, scheduleDate).Find(&dialysisOrders).Error
  305. //
  306. // if err != nil {
  307. // if err == gorm.ErrRecordNotFound {
  308. // return nil, nil
  309. // } else {
  310. // return nil, err
  311. // }
  312. // } else {
  313. // if len(dialysisOrders) > 0 {
  314. // //缓存数据
  315. // dialysis_orders_list_all, err := json.Marshal(&dialysisOrders)
  316. // if err == nil {
  317. // redis.Set(key, dialysis_orders_list_all, time.Second*60*60*18)
  318. // }
  319. // } else {
  320. // redis.Set(key, "null", time.Second*60*60*18)
  321. // }
  322. // return dialysisOrders, nil
  323. // }
  324. // } else { //缓存数据了数据,将redis缓存的json字符串转为map
  325. // if dialysis_orders_list_all == "null" {
  326. // return nil, nil
  327. // } else {
  328. // json.Unmarshal([]byte(dialysis_orders_list_all), &dialysisOrders)
  329. // return dialysisOrders, nil
  330. // }
  331. //
  332. // }
  333. //}
  334. func GetAllTreatmentSummarysByList(orgID int64, scheduleDate int64) (treatmentSummarys []*models.VMTreatmentSummaryForList, err error) {
  335. redis := RedisClient()
  336. defer redis.Close()
  337. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":treatment_summarys_list_all"
  338. treatment_summarys_all, _ := redis.Get(key).Result()
  339. if len(treatment_summarys_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  340. err = readDb.Model(&models.VMTreatmentSummaryForList{}).Where("status = 1 AND user_org_id = ? AND assessment_date = ?", orgID, scheduleDate).Find(&treatmentSummarys).Error
  341. if err != nil {
  342. if err == gorm.ErrRecordNotFound {
  343. if len(treatment_summarys_all) <= 0 {
  344. redis.Set(key, "null", time.Second*60*60*18)
  345. }
  346. return nil, nil
  347. } else {
  348. return nil, err
  349. }
  350. } else {
  351. if len(treatmentSummarys) > 0 {
  352. //缓存数据
  353. treatment_summarys_all, err := json.Marshal(&treatmentSummarys)
  354. if err == nil {
  355. redis.Set(key, treatment_summarys_all, time.Second*60*60*18)
  356. }
  357. } else {
  358. redis.Set(key, "null", time.Second*60*60*18)
  359. }
  360. return treatmentSummarys, nil
  361. }
  362. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  363. if treatment_summarys_all == "null" {
  364. return nil, nil
  365. } else {
  366. json.Unmarshal([]byte(treatment_summarys_all), &treatmentSummarys)
  367. return treatmentSummarys, nil
  368. }
  369. }
  370. }
  371. func GetAllAssessmentAfterDislysisByList(orgID int64, scheduleDate int64) (assessmentAfterDislysis []*models.VMAssessmentAfterDislysis, err error) {
  372. redis := RedisClient()
  373. defer redis.Close()
  374. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":assessment_after_dislysis_list_all"
  375. assessment_after_dislysis__all, _ := redis.Get(key).Result()
  376. if len(assessment_after_dislysis__all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  377. err = readDb.Model(&models.VMAssessmentAfterDislysis{}).Where("status = 1 AND user_org_id = ? AND assessment_date = ?", orgID, scheduleDate).Find(&assessmentAfterDislysis).Error
  378. if err != nil {
  379. if err == gorm.ErrRecordNotFound {
  380. if len(assessmentAfterDislysis) <= 0 {
  381. redis.Set(key, "null", time.Second*60*60*18)
  382. }
  383. return nil, nil
  384. } else {
  385. return nil, err
  386. }
  387. } else {
  388. if len(assessmentAfterDislysis) > 0 {
  389. //缓存数据
  390. assessment_after_dislysis__all, err := json.Marshal(&assessmentAfterDislysis)
  391. if err == nil {
  392. redis.Set(key, assessment_after_dislysis__all, time.Second*60*60*18)
  393. }
  394. } else {
  395. redis.Set(key, "null", time.Second*60*60*18)
  396. }
  397. return assessmentAfterDislysis, nil
  398. }
  399. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  400. if assessment_after_dislysis__all == "null" {
  401. return nil, nil
  402. } else {
  403. json.Unmarshal([]byte(assessment_after_dislysis__all), &assessmentAfterDislysis)
  404. return assessmentAfterDislysis, nil
  405. }
  406. }
  407. }
  408. func GetAllDobuleCheckList(orgID int64, scheduleDate int64) (dobulecheck []*models.DoubleCheck, err error) {
  409. err = XTReadDB().Where("check_date = ? and user_org_id = ? and status = 1", scheduleDate, orgID).Find(&dobulecheck).Error
  410. return dobulecheck, err
  411. }
  412. func GetAllHisAdvicesByList(orgID int64, scheduleDate int64) (hisAdvices []VMHisDoctorAdviceInfo, err error) {
  413. redis := RedisClient()
  414. defer redis.Close()
  415. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":his_advices_list_all"
  416. his_advices_all, _ := redis.Get(key).Result()
  417. if len(his_advices_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  418. err = readDb.Model(&VMHisDoctorAdviceInfo{}).Where("status = 1 AND user_org_id = ? AND advice_date = ?", orgID, scheduleDate).Find(&hisAdvices).Error
  419. if err != nil {
  420. if err == gorm.ErrRecordNotFound {
  421. if len(hisAdvices) <= 0 {
  422. redis.Set(key, "null", time.Second*60*60*18)
  423. }
  424. return nil, nil
  425. } else {
  426. return nil, err
  427. }
  428. } else {
  429. if len(hisAdvices) > 0 {
  430. //缓存数据
  431. his_advices_all, err := json.Marshal(&hisAdvices)
  432. if err == nil {
  433. redis.Set(key, his_advices_all, time.Second*60*60*18)
  434. }
  435. } else {
  436. redis.Set(key, "null", time.Second*60*60*18)
  437. }
  438. return hisAdvices, nil
  439. }
  440. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  441. if his_advices_all == "null" {
  442. return nil, nil
  443. } else {
  444. json.Unmarshal([]byte(his_advices_all), &hisAdvices)
  445. return hisAdvices, nil
  446. }
  447. }
  448. }
  449. func GetPatientListByUpdateTime(orgID int64, syncTime int64) (patients []*models.PatientListForFaceList, total int64, err error) {
  450. db := readDb.Model(&models.PatientListForFaceList{}).Where("user_org_id=? AND status=1 AND avatar <> 'https://images.shengws.com/201809182128222.png' AND avatar <> 'https://images.shengws.com/201809182128111.png' AND avatar <> ''", orgID)
  451. db = db.Where("updated_time >= ?", syncTime)
  452. err = db.Count(&total).Find(&patients).Error
  453. return
  454. }
  455. func GetPatientListBySchedules(orgID int64, syncTime int64) (patients []*models.SchedulePatients, total int64, err error) {
  456. db := readDb.Model(&models.SchedulePatients{}).Where("user_org_id=? AND status=1 AND schedule_date = ?", orgID, syncTime).Preload("Patient", "status = 1 AND user_org_id = ? ", orgID)
  457. err = db.Count(&total).Find(&patients).Error
  458. return
  459. }
  460. func GetPatientListBySchedulesFind(orgID int64, syncTime int64, patient_name string) (patients []*models.SchedulePatients, total int64, err error) {
  461. db := readDb.Model(&models.SchedulePatients{}).Where("user_org_id=? AND status=1 AND schedule_date = ?", orgID, syncTime).Preload("Patient", "status = 1 AND user_org_id = ? and name like ? ", orgID, "%"+patient_name+"%")
  462. err = db.Count(&total).Find(&patients).Error
  463. return
  464. }
  465. func GetPatientListById(orgID int64, patientId int64) (patients models.PatientListForFace, err error) {
  466. err = readDb.Model(&models.PatientListForFaceList{}).Where("user_org_id=? and id = ? and status=1", orgID, patientId).First(&patients).Error
  467. return
  468. }
  469. func GetPatientCount(orgID int64) (total int64) {
  470. readDb.Model(&models.Patients{}).Where("user_org_id=? and status=1 and lapseto = 1", orgID).Count(&total)
  471. return
  472. }
  473. func GetLapsetoPatientCount(orgID int64, lapseto int64) (total int64) {
  474. readDb.Model(&models.Patients{}).Where("user_org_id=? and lapseto=? and status=1", orgID, lapseto).Count(&total)
  475. return
  476. }
  477. func ChechLastDialysisNo(orgID int64) (dialysisNo int64) {
  478. var patient models.Patients
  479. if orgID == 10278 {
  480. err := readDb.Model(&models.Patients{}).Where("status=1 and user_org_id=?", orgID).Order("id desc").First(&patient).Error
  481. if err != nil {
  482. return
  483. }
  484. } else {
  485. err := readDb.Model(&models.Patients{}).Where("status=1 and user_org_id=?", orgID).Order("dialysis_no desc").First(&patient).Error
  486. if err != nil {
  487. return
  488. }
  489. }
  490. if patient.ID == 0 {
  491. return
  492. }
  493. dialysisNo, _ = strconv.ParseInt(patient.DialysisNo, 10, 64)
  494. return
  495. }
  496. func FindPatientByDialysisNo(orgID int64, dialysisNo string) (patient models.Patients, err error) {
  497. err = readDb.Model(&models.Patients{}).Where("status=1 and user_org_id=? and dialysis_no=? and lapseto = 1", orgID, dialysisNo).First(&patient).Error
  498. return
  499. }
  500. func FindPatientByIdCardNo(orgID int64, idCardNo string) (patient models.Patients, err error) {
  501. err = readDb.Model(&models.Patients{}).Where("status=1 and user_org_id=? and id_card_no=?", orgID, idCardNo).First(&patient).Error
  502. return
  503. }
  504. func FindPatientByMobile(orgID int64, mobile string) (patient models.Patients, err error) {
  505. err = readDb.Model(&models.Patients{}).Where("phone=? and user_org_id=? and status=1", mobile, orgID).First(&patient).Error
  506. return
  507. }
  508. func FindPatientById(orgID int64, id int64) (patient models.Patients, err error) {
  509. err = readDb.Model(&models.Patients{}).Where("id = ? and user_org_id=? and status=1", id, orgID).First(&patient).Error
  510. return
  511. }
  512. func FindPatientByIdTwo(orgID int64, id int64) (patient models.XtPatientsNew, err error) {
  513. err = readDb.Model(&models.XtPatientsNew{}).Where("blood_id = ? and user_org_id = ? and status =1", id, orgID).First(&patient).Error
  514. return
  515. }
  516. // func FindPatientByIdWithDiseases(orgID int64, id int64) (patient models.Patients, err error) {
  517. // err = readDb.Model(&models.Patients{}).Preload("Contagions", "status = 1").Preload("Diseases", "status = 1").Where("id = ? and user_org_id=? and status=1", id, orgID).First(&patient).Error
  518. // return
  519. // }
  520. func FindPatientByIdWithDiseases(orgID int64, id int64) (patient models.Patients, err error) {
  521. redis := RedisClient()
  522. defer redis.Close()
  523. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(id, 10) + ":patient_info"
  524. patient_info_str, _ := redis.Get(key).Result()
  525. if len(patient_info_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  526. err = readDb.Model(&models.Patients{}).Preload("Contagions", "status = 1").Preload("Diseases", "status = 1").Where("id = ? and user_org_id=? and status=1", id, orgID).First(&patient).Error
  527. if err != nil {
  528. if err == gorm.ErrRecordNotFound {
  529. return patient, nil
  530. } else {
  531. return patient, err
  532. }
  533. } else {
  534. if patient.ID > 0 {
  535. //缓存数据
  536. patient_info_json, err := json.Marshal(&patient)
  537. if err == nil {
  538. redis.Set(key, patient_info_json, time.Second*60*60*18)
  539. }
  540. } else {
  541. redis.Set(key, "null", time.Second*60*60*18)
  542. }
  543. return patient, nil
  544. }
  545. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  546. if patient_info_str == "null" {
  547. return patient, nil
  548. } else {
  549. json.Unmarshal([]byte(patient_info_str), &patient)
  550. return patient, nil
  551. }
  552. }
  553. }
  554. func FindPatientWithDeviceByNo(orgID int64, no string, time int64) (patient models.SchedualPatient2, err error) {
  555. err = readDb.Preload("DialysisSchedule", func(db *gorm.DB) *gorm.DB {
  556. return db.Preload("DeviceNumber", "org_id = ?", orgID).
  557. Preload("DeviceZone", "org_id = ?", orgID).
  558. Where("user_org_id = ? AND schedule_date = ? ", orgID, time)
  559. }).Preload("InfectiousDiseases", "status = 1").Where("user_org_id=? and dialysis_no = ? and status = 1 ", orgID, no).First(&patient).Error
  560. return
  561. }
  562. func FindPatientWithDeviceByPatientId(orgID int64, id int64, time int64) (patient models.SchedualPatient2, err error) {
  563. err = readDb.Preload("DialysisSchedule", func(db *gorm.DB) *gorm.DB {
  564. return db.Preload("DeviceNumber", "org_id = ?", orgID).
  565. Preload("DeviceZone", "org_id = ?", orgID).
  566. Where("user_org_id = ? AND schedule_date = ? ", orgID, time)
  567. }).Preload("InfectiousDiseases", "status = 1").Where("user_org_id=? and id = ? and status = 1 ", orgID, id).First(&patient).Error
  568. return
  569. }
  570. func CreatePatient(patient *models.Patients, contagions []int64, diseases []int64) (err error) {
  571. user, _ := GetSgjUserByMobild(patient.Phone)
  572. customer, _ := GetSgjCoustomerByMobile(patient.UserOrgId, patient.Phone)
  573. utx := writeDb.Begin()
  574. btx := writeUserDb.Begin()
  575. if user.ID == 0 {
  576. user.Mobile = patient.Phone
  577. user.Avatar = patient.Avatar
  578. user.AvatarThumb = patient.Avatar
  579. user.Birthday = patient.Birthday
  580. user.Username = patient.Name
  581. user.Gender = patient.Gender
  582. user.Sources = 11
  583. user.Introduce = patient.Remark
  584. user.Status = 1
  585. user.UpdatedTime = patient.UpdatedTime
  586. user.CreatedTime = patient.CreatedTime
  587. err = btx.Create(&user).Error
  588. if err != nil {
  589. utx.Rollback()
  590. btx.Rollback()
  591. return
  592. }
  593. }
  594. patient.UserId = user.ID
  595. if customer == nil {
  596. err = btx.Create(&models.SgjCustomer{
  597. UserOrgId: patient.UserOrgId,
  598. UserId: user.ID,
  599. Mobile: patient.Phone,
  600. Name: patient.Name,
  601. Gender: patient.Gender,
  602. Birthday: patient.Birthday,
  603. Sources: 11,
  604. Status: 1,
  605. CreatedTime: patient.CreatedTime,
  606. UpdatedTime: patient.UpdatedTime,
  607. Avatar: patient.Avatar,
  608. Remark: patient.Remark,
  609. }).Error
  610. if err != nil {
  611. utx.Rollback()
  612. btx.Rollback()
  613. return
  614. }
  615. }
  616. err = utx.Create(patient).Error
  617. if err != nil {
  618. utx.Rollback()
  619. btx.Rollback()
  620. return
  621. }
  622. var lapseto models.PatientLapseto
  623. lapseto.PatientId = patient.ID
  624. lapseto.LapsetoType = patient.Lapseto
  625. lapseto.CreatedTime = patient.CreatedTime
  626. lapseto.UpdatedTime = patient.CreatedTime
  627. lapseto.Status = 1
  628. lapseto.LapsetoTime = patient.CreatedTime
  629. err = utx.Create(&lapseto).Error
  630. if err != nil {
  631. utx.Rollback()
  632. btx.Rollback()
  633. return
  634. }
  635. if len(contagions) > 0 {
  636. thisSQL := "INSERT INTO xt_patients_infectious_diseases (patient_id, disease_id, status, created_time, updated_time) VALUES "
  637. insertParams := make([]string, 0)
  638. insertData := make([]interface{}, 0)
  639. for _, contagion := range contagions {
  640. insertParams = append(insertParams, "(?, ?, ?, ?, ?)")
  641. insertData = append(insertData, patient.ID)
  642. insertData = append(insertData, contagion)
  643. insertData = append(insertData, 1)
  644. insertData = append(insertData, patient.CreatedTime)
  645. insertData = append(insertData, patient.UpdatedTime)
  646. }
  647. thisSQL += strings.Join(insertParams, ", ")
  648. err = utx.Exec(thisSQL, insertData...).Error
  649. if err != nil {
  650. utx.Rollback()
  651. btx.Rollback()
  652. return
  653. }
  654. }
  655. if len(diseases) > 0 {
  656. thisSQL := "INSERT INTO xt_patients_chronic_diseases (patient_id, disease_id, status, created_time, updated_time) VALUES "
  657. insertParams := make([]string, 0)
  658. insertData := make([]interface{}, 0)
  659. for _, disease := range diseases {
  660. insertParams = append(insertParams, "(?, ?, ?, ?, ?)")
  661. insertData = append(insertData, patient.ID)
  662. insertData = append(insertData, disease)
  663. insertData = append(insertData, 1)
  664. insertData = append(insertData, patient.CreatedTime)
  665. insertData = append(insertData, patient.UpdatedTime)
  666. }
  667. thisSQL += strings.Join(insertParams, ", ")
  668. err = utx.Exec(thisSQL, insertData...).Error
  669. if err != nil {
  670. utx.Rollback()
  671. btx.Rollback()
  672. return
  673. }
  674. }
  675. utx.Commit()
  676. btx.Commit()
  677. return
  678. }
  679. func GetLastPatientData(orgid int64) (models.Patients, error) {
  680. patients := models.Patients{}
  681. err := XTReadDB().Model(&patients).Where("user_org_id = ? and status = 1", orgid).Last(&patients).Error
  682. return patients, err
  683. }
  684. func CreatePatientsNew(patientsNew *models.XtPatientsNew) error {
  685. err := XTWriteDB().Model(&patientsNew).Create(&patientsNew).Error
  686. return err
  687. }
  688. func CreateAllergic(allergic models.XtPatientAllergic) error {
  689. err := XTWriteDB().Create(&allergic).Error
  690. return err
  691. }
  692. func EditPatientLapseto(patient *models.Patients, lapseto *models.PatientLapseto) (err error) {
  693. utx := writeDb.Begin()
  694. err = utx.Model(&models.Patients{}).Where("id=?", patient.ID).Update(map[string]interface{}{"lapseto": patient.Lapseto, "out_reason": patient.OutReason, "death_time": patient.DeathTime, "patient_start_time": patient.PatientStartTime, "patient_end_time": patient.PatientEndTime, "patient_address": patient.PatientAddress}).Error
  695. //err = utx.Model(&models.PatientLapseto{}).Where("id=?", patient.ID).Update(map[string]interface{}{"lapseto_type": patient.Lapseto, "lapseto_time": time.Now().Unix(), "updated_time": time.Now().Unix()}).Error
  696. if err != nil {
  697. utx.Rollback()
  698. return
  699. }
  700. err = utx.Create(lapseto).Error
  701. if err != nil {
  702. utx.Rollback()
  703. return
  704. }
  705. // 删除排班和排班模板信息
  706. if lapseto.LapsetoType == 2 || lapseto.LapsetoType == 3 {
  707. now := time.Now()
  708. deleteScheduleErr := utx.Model(&models.PatientSchedule{}).Where("patient_id = ? AND schedule_date >= ? AND status = 1", patient.ID, lapseto.LapsetoTime).Updates(map[string]interface{}{
  709. "status": 0,
  710. "updated_time": now.Unix(),
  711. }).Error
  712. if deleteScheduleErr != nil {
  713. utx.Rollback()
  714. err = deleteScheduleErr
  715. return
  716. }
  717. deleteSchTempItemErr := utx.Model(&models.PatientScheduleTemplateItem{}).Where("patient_id = ? AND status = 1", patient.ID).Updates(map[string]interface{}{
  718. "status": 0,
  719. "mtime": now.Unix(),
  720. }).Error
  721. if deleteSchTempItemErr != nil {
  722. utx.Rollback()
  723. err = deleteSchTempItemErr
  724. return
  725. }
  726. }
  727. utx.Commit()
  728. return
  729. }
  730. func UpdatePatient(patient *models.Patients, contagions []int64, diseases []int64) (err error) {
  731. // if len(contagions) > 0 || len(diseases) > 0 {
  732. utx := writeDb.Begin()
  733. err = utx.Save(patient).Error
  734. if err != nil {
  735. utx.Rollback()
  736. return
  737. }
  738. err = utx.Model(&models.InfectiousDiseases{}).Where("patient_id=?", patient.ID).Update(map[string]interface{}{"Status": 2, "UpdatedTime": patient.UpdatedTime}).Error
  739. fmt.Println("err", err)
  740. if err != nil {
  741. utx.Rollback()
  742. return
  743. }
  744. if len(contagions) > 0 && patient.IsInfectious == 2 {
  745. thisSQL := "INSERT INTO xt_patients_infectious_diseases (patient_id, disease_id, status, created_time, updated_time) VALUES "
  746. insertParams := make([]string, 0)
  747. insertData := make([]interface{}, 0)
  748. for _, contagion := range contagions {
  749. insertParams = append(insertParams, "(?, ?, ?, ?, ?)")
  750. insertData = append(insertData, patient.ID)
  751. insertData = append(insertData, contagion)
  752. insertData = append(insertData, 1)
  753. insertData = append(insertData, time.Now().Unix())
  754. insertData = append(insertData, time.Now().Unix())
  755. }
  756. thisSQL += strings.Join(insertParams, ", ")
  757. err = utx.Exec(thisSQL, insertData...).Error
  758. if err != nil {
  759. utx.Rollback()
  760. return
  761. }
  762. }
  763. err = utx.Model(&models.ChronicDiseases{}).Where("patient_id=?", patient.ID).Update(map[string]interface{}{"Status": 2, "UpdatedTime": patient.UpdatedTime}).Error
  764. if err != nil {
  765. utx.Rollback()
  766. return
  767. }
  768. if len(diseases) > 0 {
  769. thisSQL := "INSERT INTO xt_patients_chronic_diseases (patient_id, disease_id, status, created_time, updated_time) VALUES "
  770. insertParams := make([]string, 0)
  771. insertData := make([]interface{}, 0)
  772. for _, disease := range diseases {
  773. insertParams = append(insertParams, "(?, ?, ?, ?, ?)")
  774. insertData = append(insertData, patient.ID)
  775. insertData = append(insertData, disease)
  776. insertData = append(insertData, 1)
  777. insertData = append(insertData, time.Now().Unix())
  778. insertData = append(insertData, time.Now().Unix())
  779. }
  780. thisSQL += strings.Join(insertParams, ", ")
  781. err = utx.Exec(thisSQL, insertData...).Error
  782. if err != nil {
  783. utx.Rollback()
  784. return
  785. }
  786. }
  787. utx.Commit()
  788. // } else {
  789. // err = writeDb.Save(patient).Error
  790. // }
  791. return
  792. }
  793. func UpdatePatientLapseto(patientid int64, lapseto models.PatientLapseto) error {
  794. err := XTWriteDB().Model(&lapseto).Where("patient_id = ?", patientid).Updates(map[string]interface{}{"lapseto_type": lapseto.LapsetoType, "lapseto_time": time.Now().Unix(), "updated_time": time.Now().Unix()}).Error
  795. return err
  796. }
  797. func UpdatepatientTwo(patientsNew *models.XtPatientsNew, id int64) error {
  798. err := XTWriteDB().Model(&patientsNew).Where("blood_id = ?", id).Update(map[string]interface{}{"user_org_id": patientsNew.UserOrgId, "user_id": patientsNew.UserId, "avatar": patientsNew.Avatar, "patient_type": patientsNew.Avatar, "dialysis_no": patientsNew.DialysisNo, "admission_number": patientsNew.AdmissionNumber, "source": patientsNew.Source, "lapseto": patientsNew.Lapseto, "partition_id": patientsNew.PartitionId, "bed_id": patientsNew.BedId, "name": patientsNew.Name, "alias": patientsNew.Alias, "gender": patientsNew.Gender, "marital_status": patientsNew.MaritalStatus, "id_card_no": patientsNew.IdCardNo, "birthday": patientsNew.Birthday, "reimbursement_way_id": patientsNew.ReimbursementWayId, "health_care_type": patientsNew.HealthCareType, "health_care_no": patientsNew.HealthCareType, "health_care_due_date": patientsNew.HealthCareType, "height": patientsNew.Height, "blood_type": patientsNew.BloodType, "rh": patientsNew.Rh, "health_care_due_alert_date": patientsNew.HealthCareDueAlertDate, "education_level": patientsNew.EducationLevel, "profession": patientsNew.Profession, "phone": patientsNew.Phone, "home_telephone": patientsNew.HomeTelephone, "relative_phone": patientsNew.RelativePhone, "relative_relations": patientsNew.RelativeRelations, "home_address": patientsNew.HomeAddress, "work_unit": patientsNew.WorkUnit, "unit_address": patientsNew.UnitAddress, "children": patientsNew.Children, "receiving_date": patientsNew.ReceivingDate, "is_hospital_first_dialysis": patientsNew.IsHospitalFirstDialysis, "first_dialysis_date": patientsNew.FirstDialysisDate, "first_dialysis_hospital": patientsNew.FirstDialysisHospital, "predialysis_condition": patientsNew.PredialysisCondition, "pre_hospital_dialysis_frequency": patientsNew.PreHospitalDialysisFrequency, "pre_hospital_dialysis_times": patientsNew.PreHospitalDialysisFrequency, "hospital_first_dialysis_date": patientsNew.HospitalFirstDialysisDate, "induction_period": patientsNew.InductionPeriod, "initial_dialysis": patientsNew.InitialDialysis, "total_dialysis": patientsNew.TotalDialysis, "attending_doctor_id": patientsNew.AttendingDoctorId, "head_nurse_id": patientsNew.HeadNurseId, "evaluate": patientsNew.Evaluate, "diagnose": patientsNew.Diagnose, "remark": patientsNew.Remark, "registrars_id": patientsNew.RegistrarsId, "registrars": patientsNew.Registrars, "qr_code": patientsNew.QrCode, "binding_state": patientsNew.BindingState, "patient_complains": patientsNew.PatientComplains, "present_history": patientsNew.PresentHistory, "past_history": patientsNew.PastHistory, "temperature": patientsNew.Temperature,
  799. "pulse": patientsNew.Pulse, "respiratory": patientsNew.Respiratory, "sbp": patientsNew.Sbp, "dbp": patientsNew.Dbp, "nation": patientsNew.Nation, "native_place": patientsNew.NativePlace, "age": patientsNew.Age, "infectious_next_record_time": patientsNew.InfectiousNextRecordTime, "is_infectious": patientsNew.IsInfectious, "remind_cycle": patientsNew.RemindCycle, "response_result": patientsNew.ResponseResult, "is_open_remind": patientsNew.IsOpenRemind, "first_treatment_date": patientsNew.FirstTreatmentDate, "dialysis_age": patientsNew.DialysisAge, "expense_kind": patientsNew.ExpenseKind, "tell_phone": patientsNew.ExpenseKind, "contact_name": patientsNew.ContactName, "blood_patients": patientsNew.BloodPatients, "slow_patients": patientsNew.SlowPatients, "member_patients": patientsNew.MemberPatients, "ecommer_patients": patientsNew.EcommerPatients, "troble_shoot": patientsNew.TrobleShoot, "sch_remark": patientsNew.SchRemark, "treatment_plan": patientsNew.TreatmentPlan, "record_number": patientsNew.RecordNumber, "patient_start_time": patientsNew.PatientStartTime, "patient_end_time": patientsNew.PatientEndTime, "infectious_remark": patientsNew.InfectiousRemark, "allergic_history": patientsNew.AllergicHistory, "patient_address": patientsNew.PatientAddress}).Error
  800. return err
  801. }
  802. func GetLastInfectionRecord(id int64, org_id int64, project_id int64, date int64) (inspection models.Inspection, err error) {
  803. err = readDb.Model(&models.Inspection{}).Where("patient_id=? and status=1 and org_id = ? and project_id = ? AND inspect_date = ? ", id, org_id, project_id, date).Last(&inspection).Error
  804. return
  805. }
  806. func GetAllInfectionRecord(date int64, org_id int64, patient_id int64, project_id int64) (inspection []*models.Inspection, err error) {
  807. err = readDb.Model(&models.Inspection{}).Where("patient_id=? and status=1 and org_id = ? and project_id = ? and inspect_date = ?", patient_id, org_id, project_id, date).Find(&inspection).Error
  808. return
  809. }
  810. func GetPatientDiseases(id int64) []int64 {
  811. var dis []models.ChronicDiseases
  812. ids := make([]int64, 0)
  813. err := readDb.Model(&models.ChronicDiseases{}).Where("patient_id=? and status=1", id).Find(&dis).Error
  814. if err != nil || len(dis) == 0 {
  815. return ids
  816. }
  817. for _, item := range dis {
  818. ids = append(ids, item.DiseaseId)
  819. }
  820. return ids
  821. }
  822. func GetPatientContagions(id int64) []int64 {
  823. var cis []models.InfectiousDiseases
  824. ids := make([]int64, 0)
  825. err := readDb.Model(&models.InfectiousDiseases{}).Where("patient_id=? and status=1", id).Find(&cis).Error
  826. if err != nil || len(cis) == 0 {
  827. return ids
  828. }
  829. for _, item := range cis {
  830. ids = append(ids, item.DiseaseId)
  831. }
  832. return ids
  833. }
  834. func FindPatientDialysisSolutionByMode(orgID int64, patientID, modeId int64) (solution models.DialysisSolution, err error) {
  835. err = readDb.Model(&models.DialysisSolution{}).Where("user_org_id=? and patient_id=? and mode_id=? and parent_id=0 and status=1", orgID, patientID, modeId).First(&solution).Error
  836. return
  837. }
  838. func FindPatientDialysisSolution(orgID int64, id int64) (solution models.DialysisSolution, err error) {
  839. err = readDb.Model(&models.DialysisSolution{}).Where("id = ? and status=1 and user_org_id=?", id, orgID).First(&solution).Error
  840. return
  841. }
  842. func FindPatientDialysisSolutionChild(orgID int64, id int64) (solution models.DialysisSolution, err error) {
  843. err = readDb.Model(&models.DialysisSolution{}).Where("parent_id = ? and status=1 and user_org_id=?", id, orgID).First(&solution).Error
  844. return
  845. }
  846. func CreatePatientDialysisSolution(solution *models.DialysisSolution) (err error) {
  847. err = writeDb.Create(solution).Error
  848. return
  849. }
  850. func UpdatePatientDialysisSolution(solution *models.DialysisSolution) (err error) {
  851. err = writeDb.Save(solution).Error
  852. return
  853. }
  854. // GetPatientDialysisSolutionList 返回患者透析方案的列表
  855. func GetPatientDialysisSolutionList(orgID int64, patientID int64, page, limit int64) (solutions []*models.DialysisSolution, total int64, err error) {
  856. offset := (page - 1) * limit
  857. db := readDb.Table("xt_dialysis_solution as ds").Where("ds.status=1")
  858. if orgID > 0 {
  859. db = db.Where("ds.user_org_id=?", orgID)
  860. }
  861. if patientID > 0 {
  862. db = db.Where("ds.patient_id=?", patientID)
  863. }
  864. db = db.Count(&total).Offset(offset).Limit(limit)
  865. err = db.Order("id desc").Find(&solutions).Error
  866. if err != nil {
  867. return
  868. }
  869. if len(solutions) > 0 {
  870. nilNameIds := make([]int64, 0)
  871. for _, solution := range solutions {
  872. if len(solution.ModeName) == 0 {
  873. nilNameIds = append(nilNameIds, solution.ModeId)
  874. }
  875. }
  876. if len(nilNameIds) > 0 {
  877. var modes []*models.TreatmentMode
  878. err = readDb.Model(&models.TreatmentMode{}).Where("id IN (?)", nilNameIds).Find(&modes).Error
  879. if err != nil {
  880. return
  881. }
  882. modesMap := make(map[int64]models.TreatmentMode, 0)
  883. for _, mode := range modes {
  884. modesMap[mode.ID] = *mode
  885. }
  886. for index, solution := range solutions {
  887. if _, exixt := modesMap[solution.ModeId]; exixt && len(solution.ModeName) == 0 {
  888. solutions[index].ModeName = modesMap[solution.ModeId].Name
  889. }
  890. }
  891. }
  892. }
  893. return
  894. }
  895. // GetPatientDryWeightAdjustList 返回患者调整干体重的列表
  896. func GetPatientDryWeightAdjustList(orgID int64, patientID int64, page int64, limit int64) (weights []*models.DryWeightAdjust, total int64, err error) {
  897. db := readDb.Table("xt_dry_weight_adjust as dwa").Where("dwa.status=1")
  898. if orgID > 0 {
  899. db = db.Where("dwa.user_org_id=?", orgID)
  900. }
  901. if patientID > 0 {
  902. db = db.Where("dwa.patient_id=?", patientID)
  903. }
  904. db = db.Select("dwa.id, dwa.user_org_id, dwa.patient_id, dwa.weight, dwa.adjusted_value, dwa.doctor, dwa.registrars_id, dwa.remark, dwa.status, dwa.created_time, dwa.updated_time").Count(&total).Order("id desc")
  905. if page > 0 && limit > 0 {
  906. offset := (page - 1) * limit
  907. db = db.Offset(offset).Limit(limit)
  908. }
  909. err = db.Find(&weights).Error
  910. return
  911. }
  912. func CreateDryWeightAdjust(m *models.DryWeightAdjust) (err error) {
  913. err = writeDb.Create(m).Error
  914. return
  915. }
  916. func FindPatientLastDryWeightAdjust(orgID int64, id int64) (weight models.DryWeightAdjust, err error) {
  917. err = readDb.Model(&models.DryWeightAdjust{}).Where("user_org_id=? and patient_id=? and status=1", orgID, id).Order("id desc").First(&weight).Error
  918. return
  919. }
  920. func UpdateDoctorAdviceById(id int64, prescribing_number float64, single_dose float64) error {
  921. err := XTWriteDB().Model(&models.DoctorAdvice{}).Where("id=? and status =1", id).Updates(map[string]interface{}{"prescribing_number": prescribing_number, "single_dose": single_dose}).Error
  922. return err
  923. }
  924. func CreateDoctorAdvice(m *models.DoctorAdvice) (err error) {
  925. return writeDb.Create(m).Error
  926. }
  927. func GetMaxAdviceGroupID(orgId int64) (group int64) {
  928. var advice models.DoctorAdvice
  929. err := readDb.Table("xt_doctor_advice").Where("user_org_id=?", orgId).Select("max(groupno) as groupno").First(&advice).Error
  930. if err != nil {
  931. fmt.Println(err)
  932. group = 0
  933. }
  934. group = advice.GroupNo
  935. return
  936. }
  937. func CreateGroupAdvice(orgId int64, group int64, advices []*models.GroupAdvice) (err error) {
  938. if group == 0 {
  939. group = GetMaxAdviceGroupID(orgId) + 1
  940. }
  941. tx := writeDb.Begin()
  942. defer func() {
  943. if r := recover(); r != nil {
  944. tx.Rollback()
  945. }
  946. }()
  947. for _, advice := range advices {
  948. advice.GroupNo = group
  949. if err = tx.Create(advice).Error; err != nil {
  950. tx.Rollback()
  951. return
  952. }
  953. }
  954. tx.Commit()
  955. return
  956. }
  957. func CreateMGroupAdvice(orgId int64, advices []*models.GroupAdvice, groupNo int64) (list []*models.GroupAdvice, err error) {
  958. if groupNo <= 0 {
  959. group := GetMaxAdviceGroupID(orgId)
  960. groupNo = group + 1
  961. }
  962. tx := writeDb.Begin()
  963. defer func() {
  964. if r := recover(); r != nil {
  965. tx.Rollback()
  966. }
  967. }()
  968. for _, advice := range advices {
  969. advice.GroupNo = groupNo
  970. if err = tx.Create(advice).Error; err != nil {
  971. tx.Rollback()
  972. return
  973. }
  974. list = append(list, advice)
  975. if len(advice.Children) > 0 {
  976. for _, child := range advice.Children {
  977. child.GroupNo = groupNo
  978. child.ParentId = advice.ID
  979. fmt.Println(child)
  980. if err = tx.Create(&child).Error; err != nil {
  981. tx.Rollback()
  982. return
  983. }
  984. list = append(list, child)
  985. }
  986. }
  987. }
  988. tx.Commit()
  989. return
  990. }
  991. func UpdateDoctorAdvice(m *models.DoctorAdvice) (err error) {
  992. return writeDb.Save(m).Error
  993. }
  994. func StopGroupAdvice(orgId int64, groupNo int64, m *models.DoctorAdvice) (err error) {
  995. err = writeDb.Model(&models.DoctorAdvice{}).Where("user_org_id=? and groupno=?", orgId, groupNo).Update(map[string]interface{}{"UpdatedTime": m.UpdatedTime, "StopState": 1, "StopReason": m.StopReason, "StopDoctor": m.StopDoctor, "StopTime": m.StopTime}).Error
  996. if err != nil {
  997. return
  998. }
  999. return
  1000. }
  1001. func StopDoctorAdvice(m *models.DoctorAdvice) (err error) {
  1002. ut := writeDb.Begin()
  1003. err = ut.Save(m).Error
  1004. if err != nil {
  1005. ut.Rollback()
  1006. return
  1007. }
  1008. err = ut.Model(&models.DoctorAdvice{}).Where("parent_id=?", m.ID).Update(map[string]interface{}{"UpdatedTime": m.UpdatedTime, "StopState": 1, "StopReason": m.StopReason, "StopDoctor": m.StopDoctor, "StopTime": m.StopTime, "Modifier": m.Modifier}).Error
  1009. if err != nil {
  1010. ut.Rollback()
  1011. return
  1012. }
  1013. ut.Commit()
  1014. return
  1015. }
  1016. func DeleteSolution(m *models.DialysisSolution) (err error) {
  1017. if m.ParentId > 0 {
  1018. return writeDb.Save(m).Error
  1019. } else {
  1020. ut := writeDb.Begin()
  1021. err = ut.Save(m).Error
  1022. if err != nil {
  1023. ut.Rollback()
  1024. return
  1025. }
  1026. err = ut.Model(&models.DialysisSolution{}).Where("parent_id=?", m.ID).Update(map[string]interface{}{"UpdatedTime": m.UpdatedTime, "Status": 0}).Error
  1027. if err != nil {
  1028. ut.Rollback()
  1029. return
  1030. }
  1031. ut.Commit()
  1032. }
  1033. return
  1034. }
  1035. func DeleteDoctorAdvice(m *models.DoctorAdvice) (err error) {
  1036. if m.ParentId > 0 {
  1037. return writeDb.Save(m).Error
  1038. } else {
  1039. ut := writeDb.Begin()
  1040. err = ut.Save(m).Error
  1041. if err != nil {
  1042. ut.Rollback()
  1043. return
  1044. }
  1045. err = ut.Model(&models.DoctorAdvice{}).Where("parent_id=? AND user_org_id = ?", m.ID, m.UserOrgId).Update(map[string]interface{}{"UpdatedTime": m.UpdatedTime, "Status": 0, "Modifier": m.Modifier}).Error
  1046. if err != nil {
  1047. ut.Rollback()
  1048. return
  1049. }
  1050. ut.Commit()
  1051. }
  1052. return
  1053. }
  1054. func DeleteGroupAdvice(orgId int64, groupNo int64, admin_user_id int64) (err error) {
  1055. err = writeDb.Model(&models.DoctorAdvice{}).Where("user_org_id = ? and groupno = ?", orgId, groupNo).Update(map[string]interface{}{"UpdatedTime": time.Now().Unix(), "Status": 0, "Modifier": admin_user_id}).Error
  1056. if err != nil {
  1057. return
  1058. }
  1059. return
  1060. }
  1061. func FindDoctorAdvice(orgID, id int64) (advice models.DoctorAdvice, err error) {
  1062. err = readDb.Model(&models.DoctorAdvice{}).Where("id = ? and user_org_id=? and status = 1", id, orgID).First(&advice).Error
  1063. return
  1064. }
  1065. func FindDoctorAdviceOne(orgID, patient_id int64) (advice models.DoctorAdvice, err error) {
  1066. err = readDb.Model(&models.DoctorAdvice{}).Where("patient_id = ? and user_org_id=? and status = 1", patient_id, orgID).First(&advice).Error
  1067. return
  1068. }
  1069. func FindDoctorAdviceTwo(ids []string, orgID int64) (advice []*models.DoctorAdvice, err error) {
  1070. err = readDb.Model(&models.DoctorAdvice{}).Where("id in(?) and user_org_id=? and status = 1", ids, orgID).First(&advice).Error
  1071. return
  1072. }
  1073. func FindHisDoctorAdvice(orgID, id int64) (advice models.HisDoctorAdviceInfo, err error) {
  1074. err = readDb.Model(&models.HisDoctorAdviceInfo{}).Where("id = ? and user_org_id=? and status = 1", id, orgID).First(&advice).Error
  1075. return
  1076. }
  1077. func FindHisDoctorAdviceTwo(ids []string, orgID int64) (advice []*models.HisDoctorAdviceInfo, err error) {
  1078. err = readDb.Model(&models.HisDoctorAdviceInfo{}).Where("id in(?) and user_org_id=? and status = 1", ids, orgID).First(&advice).Error
  1079. return
  1080. }
  1081. func FindHisDoctorAdviceOne(orgID, patient_id int64) (advice models.HisDoctorAdviceInfo, err error) {
  1082. err = readDb.Model(&models.HisDoctorAdviceInfo{}).Where("patient_id = ? and user_org_id=? and status = 1", patient_id, orgID).First(&advice).Error
  1083. return
  1084. }
  1085. func FindDoctorAdviceByGroupNo(orgID, groupNo int64) (advice models.DoctorAdvice, err error) {
  1086. err = readDb.Model(&models.DoctorAdvice{}).Where("groupno = ? and user_org_id=? and status = 1", groupNo, orgID).First(&advice).Error
  1087. return
  1088. }
  1089. func GetDoctorAdviceList(orgID, patientID, advice_type, stop, start, end int64, keywords string) (advices []*models.DoctorAdvices, total int64, err error) {
  1090. db := readDb.Table("xt_doctor_advice as x").Where("x.status = 1")
  1091. table := UserReadDB().Table("sgj_user_admin_role as r")
  1092. fmt.Print("table", table)
  1093. if orgID > 0 {
  1094. db = db.Where("x.user_org_id=?", orgID)
  1095. }
  1096. if patientID > 0 {
  1097. db = db.Where("x.patient_id = ?", patientID)
  1098. }
  1099. if advice_type > 0 {
  1100. db = db.Where("x.advice_type = ?", advice_type)
  1101. } else if advice_type == 0 {
  1102. db = db.Where("x.advice_type in (?)", []int{1, 3})
  1103. }
  1104. if stop == 1 {
  1105. db = db.Where("(x.stop_state=? or x.execution_state=?)", stop, stop)
  1106. } else if stop == 2 {
  1107. db = db.Where("x.stop_state=? and x.execution_state=?", stop, stop)
  1108. }
  1109. if start != 0 {
  1110. db = db.Where("x.start_time>=?", start)
  1111. }
  1112. if end != 0 {
  1113. db = db.Where("start_time<=?", end)
  1114. }
  1115. if len(keywords) > 0 {
  1116. likeKey := "%" + keywords + "%"
  1117. db = db.Where("x.advice_name LIKE ?", likeKey)
  1118. }
  1119. err = db.Group("x.id").Count(&total).Select("x.id, x.user_org_id, x.patient_id, x.advice_type, x.advice_date, x.record_date, x.start_time, x.advice_name,x.advice_desc, x.reminder_date, x.drug_spec, x.drug_spec_unit, x.single_dose, x.single_dose_unit, x.prescribing_number, x.prescribing_number_unit, x.delivery_way, x.execution_frequency, x.advice_doctor, x.status, x.created_time,x.updated_time, x.advice_affirm, x.remark, x.stop_time, x.stop_reason, x.stop_doctor, x.stop_state, x.parent_id, x.execution_time, x.execution_staff, x.execution_state, x.checker, x.check_state, x.check_time, x.groupno,x.remind_type,x.frequency_type,x.day_count,x.week_day,x.parent_id,r.user_name, IF(x.parent_id > 0, x.parent_id, x.id) as advice_order").Joins("Left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.advice_doctor").Order("start_time desc, groupno desc, advice_order desc, id asc").Scan(&advices).Error
  1120. fmt.Print("err", err)
  1121. return
  1122. }
  1123. func GetLongDoctorAdviceList(orgID, patientID, advice_type, stop, start, end int64, keywords string, page int64, limit int64) (advices []*models.DoctorAdvices, total int64, err error) {
  1124. db := readDb.Table("xt_doctor_advice as x").Where("x.status = 1")
  1125. table := UserReadDB().Table("sgj_user_admin_role as r")
  1126. fmt.Print("table", table)
  1127. if orgID > 0 {
  1128. db = db.Where("x.user_org_id=?", orgID)
  1129. }
  1130. if patientID > 0 {
  1131. db = db.Where("x.patient_id = ?", patientID)
  1132. }
  1133. if advice_type == 1 && advice_type > 0 {
  1134. db = db.Where("x.advice_type = ?", advice_type)
  1135. }
  1136. if advice_type == 3 && advice_type > 0 {
  1137. db = db.Where("x.advice_type = 2 or x.advice_type = 3")
  1138. }
  1139. if advice_type == 4 && advice_type > 0 {
  1140. db = db.Where("x.advice_type = 4")
  1141. }
  1142. if stop == 1 {
  1143. db = db.Where("(x.stop_state=? or x.execution_state=?)", stop, stop)
  1144. } else if stop == 2 {
  1145. db = db.Where("x.stop_state=? and x.execution_state=?", stop, stop)
  1146. }
  1147. if start != 0 {
  1148. db = db.Where("x.start_time>=?", start)
  1149. }
  1150. if end != 0 {
  1151. db = db.Where("x.start_time<=?", end)
  1152. }
  1153. if len(keywords) > 0 {
  1154. likeKey := "%" + keywords + "%"
  1155. db = db.Where("x.advice_name LIKE ?", likeKey)
  1156. }
  1157. offset := (page - 1) * limit
  1158. err = db.Group("x.id").Count(&total).Offset(offset).Limit(limit).Select("x.id, x.user_org_id, x.patient_id, x.advice_type, x.advice_date, x.record_date, x.start_time, x.advice_name,x.advice_desc, x.reminder_date, x.drug_spec, x.drug_spec_unit, x.single_dose, x.single_dose_unit, x.prescribing_number, x.prescribing_number_unit, x.delivery_way, x.execution_frequency, x.advice_doctor, x.status, x.created_time,x.updated_time, x.advice_affirm, x.remark, x.stop_time, x.stop_reason, x.stop_doctor, x.stop_state, x.parent_id, x.execution_time, x.execution_staff, x.execution_state, x.checker, x.check_state, x.check_time, x.groupno,x.remind_type,x.frequency_type,x.day_count,x.week_day,x.parent_id,x.push_start_time,r.user_name, IF(x.parent_id > 0, x.parent_id, x.id) as advice_order").Joins("Left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.advice_doctor").Order("start_time desc, groupno desc, advice_order desc, id asc").Scan(&advices).Error
  1159. fmt.Print("err", err)
  1160. return
  1161. }
  1162. func GetDoctorAdviceListOne(orgID, patientID, advice_type, stop, start, end int64, keywords string, page int64, limit int64) (advices []*models.DoctorAdvices, total int64, err error) {
  1163. db := readDb.Table("xt_doctor_advice as x").Where("x.status = 1")
  1164. table := UserReadDB().Table("sgj_user_admin_role as r")
  1165. fmt.Print("table", table)
  1166. if orgID > 0 {
  1167. db = db.Where("x.user_org_id=?", orgID)
  1168. }
  1169. if patientID > 0 {
  1170. db = db.Where("x.patient_id = ?", patientID)
  1171. }
  1172. if advice_type == 1 && advice_type > 0 {
  1173. db = db.Where("x.advice_type = ?", advice_type)
  1174. }
  1175. if advice_type == 3 && advice_type > 0 {
  1176. db = db.Where("x.advice_type = 2 or x.advice_type = 3")
  1177. }
  1178. if advice_type == 4 && advice_type > 0 {
  1179. db = db.Where("x.advice_type = 4")
  1180. }
  1181. if stop == 1 {
  1182. db = db.Where("(x.stop_state=? or x.execution_state=?)", stop, stop)
  1183. } else if stop == 2 {
  1184. db = db.Where("x.stop_state=? and x.execution_state=?", stop, stop)
  1185. }
  1186. if start != 0 {
  1187. db = db.Where("x.start_time>=?", start)
  1188. }
  1189. if end != 0 {
  1190. db = db.Where("x.start_time<=?", end)
  1191. }
  1192. if len(keywords) > 0 {
  1193. likeKey := "%" + keywords + "%"
  1194. db = db.Where("x.advice_name LIKE ?", likeKey)
  1195. }
  1196. offset := (page - 1) * limit
  1197. err = db.Group("x.id").Count(&total).Offset(offset).Limit(limit).Select("x.id, x.user_org_id, x.patient_id, x.advice_type, x.advice_date, x.record_date, x.start_time, x.advice_name,x.advice_desc, x.reminder_date, x.drug_spec, x.drug_spec_unit, x.single_dose, x.single_dose_unit, x.prescribing_number, x.prescribing_number_unit, x.delivery_way, x.execution_frequency, x.advice_doctor, x.status, x.created_time,x.updated_time, x.advice_affirm, x.remark, x.stop_time, x.stop_reason, x.stop_doctor, x.stop_state, x.parent_id, x.execution_time, x.execution_staff, x.execution_state, x.checker, x.check_state, x.check_time, x.groupno,x.remind_type,x.frequency_type,x.day_count,x.week_day,x.parent_id,x.push_start_time,r.user_name, IF(x.parent_id > 0, x.parent_id, x.id) as advice_order").Joins("Left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.advice_doctor").Order("start_time desc, groupno desc, advice_order desc, id asc").Scan(&advices).Error
  1198. fmt.Print("err", err)
  1199. return
  1200. }
  1201. func GetDoctorAdviceListTwo(orgID int64, patientID int64, advice_type int64, stop int64, start int64, end int64, keywords string, limit int64, page int64) (advices []*models.DoctorAdvices, total int64, err error) {
  1202. db := readDb.Table("xt_doctor_advice as x").Where("x.status = 1")
  1203. table := UserReadDB().Table("sgj_user_admin_role as r")
  1204. fmt.Println(table)
  1205. if orgID > 0 {
  1206. db = db.Where("x.user_org_id=?", orgID)
  1207. }
  1208. if patientID > 0 {
  1209. db = db.Where("x.patient_id = ?", patientID)
  1210. }
  1211. if advice_type == 1 {
  1212. db = db.Where("x.advice_type = ?", advice_type)
  1213. }
  1214. if advice_type == 3 && advice_type > 0 {
  1215. db = db.Where("x.advice_type = 2 or x.advice_type = 3")
  1216. }
  1217. if advice_type == 4 && advice_type > 0 {
  1218. db = db.Where("x.advice_type = 4")
  1219. }
  1220. if stop == 1 {
  1221. db = db.Where("(x.stop_state=? or x.execution_state=?)", stop, stop)
  1222. } else if stop == 2 {
  1223. db = db.Where("x.stop_state=? and x.execution_state=?", stop, stop)
  1224. }
  1225. if start != 0 {
  1226. db = db.Where("x.start_time>=?", start)
  1227. }
  1228. if end != 0 {
  1229. db = db.Where("x.start_time<=?", end)
  1230. }
  1231. if len(keywords) > 0 {
  1232. likeKey := "%" + keywords + "%"
  1233. db = db.Where("x.advice_name LIKE ?", likeKey)
  1234. }
  1235. offset := (page - 1) * limit
  1236. err = db.Order("x.start_time desc").Group("x.start_time").Count(&total).Offset(offset).Limit(limit).Select("x.id, x.user_org_id, x.patient_id, x.advice_type, x.advice_date, x.record_date, x.start_time, x.advice_name,x.advice_desc, x.reminder_date, x.drug_spec, x.drug_spec_unit, x.single_dose, x.single_dose_unit, x.prescribing_number, x.prescribing_number_unit, x.delivery_way, x.execution_frequency, x.advice_doctor, x.status, x.created_time,x.updated_time, x.advice_affirm, x.remark, x.stop_time, x.stop_reason, x.stop_doctor, x.stop_state, x.parent_id, x.execution_time, x.execution_staff, x.execution_state, x.checker, x.check_state, x.check_time, x.groupno,x.remind_type,x.frequency_type,x.day_count,x.week_day,x.parent_id,x.push_start_time,r.user_name, IF(x.parent_id > 0, x.parent_id, x.id) as advice_order").Joins("Left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.advice_doctor").Scan(&advices).Error
  1237. fmt.Print("错误是什么", err)
  1238. return
  1239. }
  1240. func GetDoctorAdvicePageList(orgID, patientID, advice_type, stop, start, end int64, keywords string, page, limit int64) (advices []*models.DoctorAdvice, total int64, err error) {
  1241. offset := (page - 1) * limit
  1242. db := readDb.Model(&models.DoctorAdvice{}).Where("status=1")
  1243. if orgID > 0 {
  1244. db = db.Where("user_org_id=?", orgID)
  1245. }
  1246. if patientID > 0 {
  1247. db = db.Where("patient_id = ?", patientID)
  1248. }
  1249. if advice_type > 0 {
  1250. db = db.Where("advice_type = ?", advice_type)
  1251. }
  1252. if stop == 1 {
  1253. db = db.Where("(stop_state=? or execution_state=?) and parent_id=0", stop, stop)
  1254. } else if stop == 2 {
  1255. db = db.Where("stop_state=? and execution_state=?", stop, stop)
  1256. }
  1257. if start != 0 {
  1258. db = db.Where("start_time>=?", start)
  1259. }
  1260. if end != 0 {
  1261. db = db.Where("start_time<=?", end)
  1262. }
  1263. if len(keywords) > 0 {
  1264. likeKey := "%" + keywords + "%"
  1265. db = db.Where("advice_name LIKE ?", likeKey)
  1266. }
  1267. err = db.Count(&total).Select("id,user_org_id,patient_id,advice_type,advice_date,start_time,advice_name,advice_desc,reminder_date,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 IF(parent_id>0, parent_id, id) as advice_order").Order("advice_order desc, id").Offset(offset).Limit(limit).Scan(&advices).Error
  1268. return
  1269. }
  1270. func CreateSubDoctorAdvice(advices []*models.DoctorAdvice) (err error) {
  1271. if len(advices) > 0 {
  1272. utx := writeDb.Begin()
  1273. if len(advices) > 0 {
  1274. thisSQL := "INSERT INTO xt_doctor_advice (single_dose_unit, prescribing_number, prescribing_number_unit, advice_name, advice_desc,single_dose,created_time,updated_time,patient_id,parent_id,user_org_id,record_date,advice_type,drug_spec_unit) VALUES "
  1275. insertParams := make([]string, 0)
  1276. insertData := make([]interface{}, 0)
  1277. for _, advice := range advices {
  1278. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1279. insertData = append(insertData, advice.SingleDoseUnit)
  1280. insertData = append(insertData, advice.PrescribingNumber)
  1281. insertData = append(insertData, advice.PrescribingNumberUnit)
  1282. insertData = append(insertData, advice.AdviceName)
  1283. insertData = append(insertData, advice.AdviceDesc)
  1284. insertData = append(insertData, advice.SingleDose)
  1285. insertData = append(insertData, advice.CreatedTime)
  1286. insertData = append(insertData, advice.UpdatedTime)
  1287. insertData = append(insertData, advice.PatientId)
  1288. insertData = append(insertData, advice.ParentId)
  1289. insertData = append(insertData, advice.UserOrgId)
  1290. insertData = append(insertData, advice.RecordDate)
  1291. insertData = append(insertData, 2)
  1292. insertData = append(insertData, advice.DrugSpecUnit)
  1293. }
  1294. thisSQL += strings.Join(insertParams, ", ")
  1295. err = utx.Exec(thisSQL, insertData...).Error
  1296. if err != nil {
  1297. utx.Rollback()
  1298. return
  1299. }
  1300. }
  1301. utx.Commit()
  1302. }
  1303. return
  1304. }
  1305. func GetPatientDialysisRecord(orgID, patientID int64, page, limit, start, end, mode_id int64) ([]*models.PatientDialysisRecord, int64, error) {
  1306. offset := (page - 1) * limit
  1307. var total int64
  1308. var err error
  1309. var orders []*models.PatientDialysisRecord
  1310. // err = readDb.Table("xt_dialysis_order as do").
  1311. // Preload("DialysisPrescription", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  1312. // Preload("PredialysisEvaluation", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  1313. // Preload("AssessmentAfterDislysis", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  1314. // Preload("TreatmentSummary", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  1315. // Joins("JOIN xt_schedule as s ON s.patient_id=? and FROM_UNIXTIME(s.schedule_date, '%Y-%m-%d')=FROM_UNIXTIME(do.dialysis_date, '%Y-%m-%d')", patientID).
  1316. // Joins("JOIN xt_device_zone as dz ON dz.org_id = ? and dz.id=s.partition_id", orgID).
  1317. // Where("do.patient_id=? and do.user_org_id=? and do.stage = 2 and do.status=1", patientID, orgID).Count(&total).Offset(offset).Limit(limit).Order("do.dialysis_date desc").Select(" do.id, do.dialysis_date, do.user_org_id, do.patient_id, do.prescription_id, do.stage, do.remark, do.status, do.created_time, do.updated_time, s.schedule_type, s.partition_id, dz.name as partition_name").Find(&orders).Error
  1318. db := readDb.Table("xt_dialysis_order as do").
  1319. Preload("DialysisPrescription", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  1320. Preload("PredialysisEvaluation", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  1321. Preload("DialysisPrescription", func(db *gorm.DB) *gorm.DB {
  1322. return readDb.Where("patient_id=? and user_org_id=? and status=1", patientID, orgID).Preload("UserAdminRole", func(db *gorm.DB) *gorm.DB {
  1323. return readUserDb.Where("status = 1")
  1324. })
  1325. }).
  1326. Preload("AssessmentAfterDislysis", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  1327. Preload("TreatmentSummary", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  1328. Preload("Device", "org_id=? and status=1", orgID).
  1329. Preload("UserAdminRole", func(db *gorm.DB) *gorm.DB {
  1330. return readUserDb.Where("org_id=?", orgID)
  1331. }).
  1332. Joins("JOIN xt_schedule as s ON s.patient_id=? and FROM_UNIXTIME(s.schedule_date, '%Y-%m-%d')=FROM_UNIXTIME(do.dialysis_date, '%Y-%m-%d')", patientID).
  1333. Joins("JOIN xt_device_zone as dz ON dz.org_id = ? and dz.id=s.partition_id", orgID).
  1334. Where("do.patient_id=? and do.user_org_id=? and do.status=1", patientID, orgID).Group("s.schedule_date")
  1335. if start != 0 {
  1336. db = db.Where("do.dialysis_date>=?", start)
  1337. }
  1338. if end != 0 {
  1339. db = db.Where("do.dialysis_date<=?", end)
  1340. }
  1341. if mode_id > 0 {
  1342. db = db.Joins("JOIN xt_dialysis_prescription as dp ON dp.patient_id=do.patient_id and dp.user_org_id = do.user_org_id and dp.record_date = do.dialysis_date")
  1343. db = db.Where("dp.mode_id=?", mode_id)
  1344. }
  1345. err = db.Count(&total).Offset(offset).Limit(limit).Order("do.dialysis_date desc").Select("do.bed_id, do.id, do.dialysis_date, do.user_org_id, do.patient_id, do.prescription_id, do.stage, do.remark, do.status, do.created_time, do.updated_time,do.start_nurse,do.finish_nurse ,s.schedule_type, s.partition_id, dz.name as partition_name").Find(&orders).Error
  1346. if len(orders) > 0 {
  1347. ids := make([]int64, 0)
  1348. for _, order := range orders {
  1349. dialyzer := order.DialysisPrescription.Dialyzer
  1350. ids = append(ids, dialyzer)
  1351. }
  1352. if len(ids) > 0 {
  1353. var dialyzers []*models.DeviceNumber
  1354. err = readDb.Model(&models.DeviceNumber{}).Where("id IN (?) and org_id=? and status=1", ids, orgID).Find(&dialyzers).Error
  1355. if err != nil {
  1356. return nil, 0, err
  1357. }
  1358. dialyzerMap := make(map[int64]models.DeviceNumber, 0)
  1359. for _, item := range dialyzers {
  1360. dialyzerMap[item.ID] = *item
  1361. }
  1362. for orderIndex, order := range orders {
  1363. if _, exist := dialyzerMap[order.DialysisPrescription.Dialyzer]; exist {
  1364. orders[orderIndex].DeviceNumber = dialyzerMap[order.DialysisPrescription.Dialyzer].Number
  1365. }
  1366. }
  1367. }
  1368. }
  1369. return orders, total, err
  1370. }
  1371. func GetPatientTreatmentSummaryList(orgID, patientID, page, limit, start, end int64) (list []*models.TreatmentSummary, total int, err error) {
  1372. offset := (page - 1) * limit
  1373. fmt.Println(offset)
  1374. fmt.Println(limit)
  1375. db := readDb.Model(&models.TreatmentSummary{}).Where("user_org_id = ? and patient_id=?", orgID, patientID)
  1376. if start != 0 {
  1377. db = db.Where("assessment_date >= ?", start)
  1378. }
  1379. if end != 0 {
  1380. db = db.Where("assessment_date <= ?", end)
  1381. }
  1382. db = db.Where("status=1")
  1383. err = db.Count(&total).Offset(offset).Limit(limit).Order("assessment_date desc, id desc").Find(&list).Error
  1384. return
  1385. }
  1386. func GetPatientScheduleList(orgID int64, patientID int64, page int64, limit int64, start int64) (schedules []*models.PatientSchedule, err error) {
  1387. offset := (page - 1) * limit
  1388. err = readDb.Table("xt_schedule as s").
  1389. Preload("DeviceZone", "org_id=? and status=1", orgID).
  1390. Preload("DeviceNumber", "org_id=? and status=1", orgID).
  1391. Preload("TreatmentMode", "status=1").
  1392. Where("s.patient_id =? and s.user_org_id=? and s.schedule_date>=? and s.status=1", patientID, orgID, start).
  1393. Limit(limit).Offset(offset).Find(&schedules).Error
  1394. return
  1395. }
  1396. func GetMonitorRecord(orgID int64, date int64, partition int64, patientId int64) ([]*models.NewVMMonitorDialysisSchedule, error) {
  1397. var mds []*models.NewVMMonitorDialysisSchedule
  1398. db := readDb.Model(&models.NewVMMonitorDialysisSchedule{})
  1399. if date > 0 {
  1400. db = db.Where("schedule_date = ?", date)
  1401. }
  1402. if partition > 0 {
  1403. db = db.Where("partition_id = ?", partition)
  1404. }
  1405. if patientId > 0 {
  1406. db = db.Where("patient_id = ?", patientId)
  1407. }
  1408. err = db.Where("status = 1 and user_org_id = ?", orgID).Find(&mds).Error
  1409. return mds, err
  1410. }
  1411. func MobileGetMonitorsWithPatient(orgID int64, keyword string, page int) ([]*models.MonitorDialysisSchedule, error) {
  1412. var patients []*models.Patients
  1413. 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
  1414. if getPatientErr != nil {
  1415. return nil, getPatientErr
  1416. }
  1417. patientIDs := make([]int64, len(patients))
  1418. for index, patient := range patients {
  1419. patientIDs[index] = patient.ID
  1420. }
  1421. db := readDb.
  1422. Model(&models.MonitorDialysisSchedule{}).
  1423. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  1424. Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  1425. Preload("TreatmentMode", "status = 1").
  1426. Preload("Prescription", "status = 1 AND user_org_id = ?", orgID).
  1427. Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ?", orgID).
  1428. Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ?", orgID).
  1429. Preload("MonitoringRecord", "status = 1 AND user_org_id = ?", orgID).
  1430. Preload("DialysisOrder", "status = 1 AND user_org_id = ?", orgID).
  1431. Preload("MonitorPatients", "status = 1 AND user_org_id = ?", orgID).
  1432. Where("status = 1 AND user_org_id = ? AND patient_id in (?)", orgID, patientIDs)
  1433. var schedules []*models.MonitorDialysisSchedule
  1434. err := db.Offset(20 * (page - 1)).Limit(20).Order("schedule_date desc").Find(&schedules).Error
  1435. return schedules, err
  1436. }
  1437. func GetPatientByKeyWord(orgID int64, keywords string) (patient []*models.Patients, err error) {
  1438. db := readDb.Model(&models.Patients{}).Where("user_org_id=? and status=1", orgID)
  1439. if len(keywords) > 0 {
  1440. likekey := "%" + keywords + "%"
  1441. err = db.Where("name LIKE ? OR dialysis_no LIKE ? OR first_letter LIKE ?", likekey, likekey, likekey).Find(&patient).Error
  1442. } else {
  1443. err = db.Find(&patient).Error
  1444. }
  1445. return
  1446. }
  1447. func FindDoctorAdviceByGoroupNo(orgID int64, groupno int64) (advice models.DoctorAdvice, err error) {
  1448. err = readDb.Model(&models.DoctorAdvice{}).Where("user_org_id=? AND groupno = ? AND status = 1", orgID, groupno).First(&advice).Error
  1449. return
  1450. }
  1451. func FindOldDoctorAdvice(orgID int64, advice_id int64) (advice models.DoctorAdvice, err error) {
  1452. err = readDb.Model(&models.DoctorAdvice{}).Where("id = ? AND user_org_id=? AND status = 1", advice_id, orgID).First(&advice).Error
  1453. return
  1454. }
  1455. func UpdateAdviceGroupStartTime(orgID int64, groupNO int64, startTime int64, admin_user_id int64) error {
  1456. now := time.Now().Unix()
  1457. err := writeDb.Model(&models.DoctorAdvice{}).Where("user_org_id = ? AND status = 1 AND execution_state <> 1 AND check_state <> 1 AND groupno = ? AND admin_user_id = ?", orgID, groupNO, admin_user_id).Updates(map[string]interface{}{
  1458. "start_time": startTime,
  1459. "updated_time": now,
  1460. "modifier": admin_user_id,
  1461. }).Error
  1462. return err
  1463. }
  1464. func QueryPatientById(id int64) *models.Patients {
  1465. var pat models.Patients
  1466. err := readDb.Model(&models.Patients{}).Where("id=?", id).First(&pat).Error
  1467. if err != nil {
  1468. }
  1469. return &pat
  1470. }
  1471. func FindAllDoctorAdviceByGoroupNo(orgID int64, groupno int64) (advice []models.DoctorAdvice, err error) {
  1472. err = readDb.Model(&models.DoctorAdvice{}).Where("user_org_id=? AND groupno = ? AND status = 1", orgID, groupno).Find(&advice).Error
  1473. return
  1474. }
  1475. func UpdateDoctorAdviceByGoroupNo(id int64, drug_id int64) error {
  1476. err := XTWriteDB().Model(&models.DoctorAdvice{}).Where("id = ? and status =1", id).Updates(map[string]interface{}{"drug_id": drug_id, "drug_name_id": drug_id, "way": 1}).Error
  1477. return err
  1478. }
  1479. func FindAllDoctorAdviceByGoroupNoTwety(orgID int64, groupno int64) (advice []models.DoctorAdvice, err error) {
  1480. err = readDb.Model(&models.DoctorAdvice{}).Where("user_org_id=? AND groupno = ? AND status = 1 and (drug_id >0 or drug_name_id >0)", orgID, groupno).Find(&advice).Error
  1481. return
  1482. }
  1483. func FindDoctorAdviceByIds(orgID int64, ids []string) (advice []models.DoctorAdvice, err error) {
  1484. err = readDb.Model(&models.DoctorAdvice{}).Where("id IN (?) AND user_org_id = ? AND status = 1", ids, orgID).Find(&advice).Error
  1485. return
  1486. }
  1487. func FindDoctorAdviceByIdsTwety(orgID int64, ids []string) (advice []models.DoctorAdvice, err error) {
  1488. err = readDb.Model(&models.DoctorAdvice{}).Where("id IN (?) AND user_org_id = ? AND status = 1 and (drug_id >0 or drug_name_id >0)", ids, orgID).Find(&advice).Error
  1489. return
  1490. }
  1491. func GetBatchDoctorAdviceList(ids []string, user_org_id int64) (advice []*models.DoctorAdvice, err error) {
  1492. err = XTReadDB().Where("id in(?) and user_org_id = ? and status =1", ids, user_org_id).Find(&advice).Error
  1493. return advice, err
  1494. }
  1495. func BatchDeleteDoctorAdvice(ids []string, user_id int64) (err error) {
  1496. ut := writeDb.Begin()
  1497. err = ut.Model(&models.DoctorAdvice{}).Where("status = 1 AND id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "updated_time": time.Now().Unix(), "modifier": user_id}).Error
  1498. if err != nil {
  1499. ut.Rollback()
  1500. return
  1501. }
  1502. err = ut.Model(&models.DoctorAdvice{}).Where("status = 1 AND parent_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "updated_time": time.Now().Unix(), "modifier": user_id}).Error
  1503. if err != nil {
  1504. ut.Rollback()
  1505. return
  1506. }
  1507. ut.Commit()
  1508. return err
  1509. }
  1510. func FindAdviceByGoroupNo(orgID int64, groupno int64) (advice []models.DoctorAdvice, err error) {
  1511. err = readDb.Model(&models.DoctorAdvice{}).Where("user_org_id=? AND groupno = ? AND status = 1 AND parent_id = 0", orgID, groupno).Find(&advice).Error
  1512. return
  1513. }
  1514. func UpdateDoctorAdviceAndSubAdvice(m *models.DoctorAdvice) (err error) {
  1515. ut := writeDb.Begin()
  1516. err = ut.Save(m).Error
  1517. if err != nil {
  1518. ut.Rollback()
  1519. return
  1520. }
  1521. err = ut.Model(&models.DoctorAdvice{}).Where("status = 1 AND parent_id IN (?)", m.ID).Updates(map[string]interface{}{"start_time": m.StartTime, "groupno": m.GroupNo, "updated_time": time.Now().Unix()}).Error
  1522. if err != nil {
  1523. ut.Rollback()
  1524. return
  1525. }
  1526. ut.Commit()
  1527. return err
  1528. }
  1529. func GetAllWaitRemindPatient(org_id int64, page int64, limit int64) (total int64, patient []*models.Patients, err error) {
  1530. type Total struct {
  1531. Count int64
  1532. }
  1533. var totals Total
  1534. offset := (page - 1) * limit
  1535. err = readDb.Raw("select * from xt_patients where user_org_id = ? AND lapseto = 1 AND infectious_next_record_time > 0 AND status = 1 AND date_sub(DATE_FORMAT(date(from_unixtime(`xt_patients`.`infectious_next_record_time`)),'%Y-%m-%d'), interval 7 day) <= now() Order by infectious_next_record_time", org_id).Offset(offset).Limit(limit).Scan(&patient).Error
  1536. readDb.Raw("select Count(id) as count from xt_patients where user_org_id = ? AND lapseto = 1 AND infectious_next_record_time > 0 AND status = 1 AND date_sub(DATE_FORMAT(date(from_unixtime(`xt_patients`.`infectious_next_record_time`)),'%Y-%m-%d'), interval 7 day) <= now() Order by infectious_next_record_time", org_id).Scan(&totals)
  1537. return totals.Count, patient, err
  1538. }
  1539. func UpdatePatientRemindStatus(patient_id int64, remind int64, org_id int64) (err error) {
  1540. err = writeDb.Model(&models.Patients{}).Where("status = 1 AND id = ? AND user_org_id = ?", patient_id, org_id).Updates(map[string]interface{}{"is_open_remind": remind}).Error
  1541. return
  1542. }
  1543. func CreatePatientWeightAdjust(m *models.SgjPatientDryweight) (err error) {
  1544. err = writeDb.Create(m).Error
  1545. return
  1546. }
  1547. func FindLastDryWeightAdjust(orgID int64, id int64) (weight models.SgjPatientDryweight, err error) {
  1548. err = readDb.Model(&models.SgjPatientDryweight{}).Where("user_org_id=? and patient_id=? and status=1", orgID, id).Order("id desc").First(&weight).Error
  1549. return
  1550. }
  1551. func GetSchedualPatientByKeyWord(orgID int64, keywords string, date int64) (patient []*models.Patients, err error) {
  1552. db := readDb.Model(&models.Patients{}).Where("user_org_id=? and status=1 and lapseto = 1 ", orgID)
  1553. if len(keywords) > 0 {
  1554. likekey := "%" + keywords + "%"
  1555. err = db.Where("(name LIKE ? OR dialysis_no LIKE ?) AND NOT EXISTS (Select * FROM `xt_dialysis_order` as d Where d.`dialysis_date` = ? AND d.`status` = 1 AND d.`patient_id` = xt_patients.id AND d.user_org_id = xt_patients.user_org_id)", likekey, likekey, date).Find(&patient).Error
  1556. } else {
  1557. err = db.Find(&patient).Error
  1558. }
  1559. return
  1560. }
  1561. func GetPatientScheduleOne(patientid int64, nowdate int64, orgid int64) (models.XtSchedule, error) {
  1562. schedule := models.XtSchedule{}
  1563. err := XTReadDB().Model(&schedule).Where("patient_id = ? and schedule_date = ? and user_org_id = ? and status =1", patientid, nowdate, orgid).Find(&schedule).Error
  1564. return schedule, err
  1565. }
  1566. func CreateExportPatient(patient *models.Patients, contagions []int64, org_creator int64) (err error) {
  1567. user, _ := GetSgjUserByMobild(patient.Phone)
  1568. customer, _ := GetSgjCoustomerByMobile(patient.UserOrgId, patient.Phone)
  1569. utx := writeDb.Begin()
  1570. btx := writeUserDb.Begin()
  1571. if user.ID == 0 {
  1572. user.Mobile = patient.Phone
  1573. user.Avatar = patient.Avatar
  1574. user.AvatarThumb = patient.Avatar
  1575. user.Birthday = patient.Birthday
  1576. user.Username = patient.Name
  1577. user.Gender = patient.Gender
  1578. user.Sources = 11
  1579. user.Introduce = patient.Remark
  1580. user.Status = 1
  1581. user.UpdatedTime = patient.UpdatedTime
  1582. user.CreatedTime = patient.CreatedTime
  1583. err = btx.Create(&user).Error
  1584. if err != nil {
  1585. utx.Rollback()
  1586. btx.Rollback()
  1587. return
  1588. }
  1589. }
  1590. patient.UserId = user.ID
  1591. if customer == nil {
  1592. err = btx.Create(&models.SgjCustomer{
  1593. UserOrgId: patient.UserOrgId,
  1594. UserId: user.ID,
  1595. Mobile: patient.Phone,
  1596. Name: patient.Name,
  1597. Gender: patient.Gender,
  1598. Birthday: patient.Birthday,
  1599. Sources: 11,
  1600. Status: 1,
  1601. CreatedTime: patient.CreatedTime,
  1602. UpdatedTime: patient.UpdatedTime,
  1603. Avatar: patient.Avatar,
  1604. Remark: patient.Remark,
  1605. }).Error
  1606. if err != nil {
  1607. utx.Rollback()
  1608. btx.Rollback()
  1609. return
  1610. }
  1611. }
  1612. err = utx.Create(patient).Error
  1613. if err != nil {
  1614. utx.Rollback()
  1615. btx.Rollback()
  1616. return
  1617. }
  1618. if patient.DryWeight > 0 {
  1619. var dryWeight models.SgjPatientDryweight
  1620. dryWeight.PatientId = patient.ID
  1621. dryWeight.UserOrgId = patient.UserOrgId
  1622. dryWeight.Status = 1
  1623. dryWeight.AdjustedValue = "/"
  1624. dryWeight.Creator = org_creator
  1625. dryWeight.UserId = org_creator
  1626. dryWeight.Ctime = time.Now().Unix()
  1627. dryWeight.Mtime = time.Now().Unix()
  1628. dryWeight.DryWeight = patient.DryWeight
  1629. dryWeight.Remakes = ""
  1630. err = utx.Create(&dryWeight).Error
  1631. }
  1632. var lapseto models.PatientLapseto
  1633. lapseto.PatientId = patient.ID
  1634. lapseto.LapsetoType = patient.Lapseto
  1635. lapseto.CreatedTime = patient.CreatedTime
  1636. lapseto.UpdatedTime = patient.CreatedTime
  1637. lapseto.Status = 1
  1638. lapseto.LapsetoTime = patient.CreatedTime
  1639. err = utx.Create(&lapseto).Error
  1640. if err != nil {
  1641. utx.Rollback()
  1642. btx.Rollback()
  1643. return
  1644. }
  1645. if len(contagions) > 0 {
  1646. thisSQL := "INSERT INTO xt_patients_infectious_diseases (patient_id, disease_id, status, created_time, updated_time) VALUES "
  1647. insertParams := make([]string, 0)
  1648. insertData := make([]interface{}, 0)
  1649. for _, contagion := range contagions {
  1650. insertParams = append(insertParams, "(?, ?, ?, ?, ?)")
  1651. insertData = append(insertData, patient.ID)
  1652. insertData = append(insertData, contagion)
  1653. insertData = append(insertData, 1)
  1654. insertData = append(insertData, patient.CreatedTime)
  1655. insertData = append(insertData, patient.UpdatedTime)
  1656. }
  1657. thisSQL += strings.Join(insertParams, ", ")
  1658. err = utx.Exec(thisSQL, insertData...).Error
  1659. if err != nil {
  1660. utx.Rollback()
  1661. btx.Rollback()
  1662. return
  1663. }
  1664. }
  1665. patientsNew := models.XtPatientsNew{
  1666. Name: patient.Name,
  1667. Gender: patient.Gender,
  1668. Phone: patient.Phone,
  1669. IdCardNo: patient.IdCardNo,
  1670. FirstDialysisDate: patient.FirstDialysisDate,
  1671. Source: patient.Source,
  1672. Lapseto: patient.Lapseto,
  1673. IsInfectious: patient.IsInfectious,
  1674. DialysisNo: patient.DialysisNo,
  1675. Height: patient.Height,
  1676. HomeAddress: patient.HomeAddress,
  1677. IsExcelExport: 1,
  1678. BloodPatients: 1,
  1679. Status: 1,
  1680. CreatedTime: time.Now().Unix(),
  1681. UserOrgId: patient.UserOrgId,
  1682. BloodId: patient.ID,
  1683. Avatar: "https://images.shengws.com/201809182128111.png",
  1684. }
  1685. err = utx.Create(&patientsNew).Error
  1686. if err != nil {
  1687. utx.Rollback()
  1688. btx.Rollback()
  1689. return
  1690. }
  1691. utx.Commit()
  1692. btx.Commit()
  1693. return
  1694. }
  1695. func FindPatientPhoneIsExist(phone string, org_id int64) (count int64) {
  1696. readDb.Model(&models.Patients{}).Where("user_org_id = ? AND phone = ? AND status = 1", org_id, phone).Count(&count)
  1697. return
  1698. }
  1699. func FindPatientIdCardNoIsExist(id_card_no string, org_id int64) (count int64) {
  1700. readDb.Model(&models.Patients{}).Where("user_org_id = ? AND id_card_no = ? AND status = 1", org_id, id_card_no).Count(&count)
  1701. return
  1702. }
  1703. func CreateExportErrLog(log *models.ExportErrLog) {
  1704. writeDb.Create(&log)
  1705. return
  1706. }
  1707. func FindPatientExportLog(org_id int64, export_time int64) (errLogs []*models.ExportErrLog, err error) {
  1708. err = readDb.Model(&models.ExportErrLog{}).Where("user_org_id = ? AND export_time = ? AND log_type = 1", org_id, export_time).Find(&errLogs).Error
  1709. return
  1710. }
  1711. func FindPatientExportLogOne(org_id int64, export_time int64) (errLogs []*models.ExportErrLog, err error) {
  1712. err = readDb.Model(&models.ExportErrLog{}).Where("user_org_id = ? AND export_time = ? AND log_type = 4", org_id, export_time).Find(&errLogs).Error
  1713. return
  1714. }
  1715. func FindPatientExportLogTwo(org_id int64, export_time int64) (errLogs []*models.ExportErrLog, err error) {
  1716. err = readDb.Model(&models.ExportErrLog{}).Where("user_org_id = ? AND export_time = ? AND log_type = 5", org_id, export_time).Find(&errLogs).Error
  1717. return
  1718. }
  1719. func FindPatientExportLogThree(org_id int64, export_time int64) (errLogs []*models.ExportErrLog, err error) {
  1720. err = readDb.Model(&models.ExportErrLog{}).Where("user_org_id = ? AND export_time = ? AND log_type = 6", org_id, export_time).Find(&errLogs).Error
  1721. return
  1722. }
  1723. func CreateExportLog(log *models.ExportLog) {
  1724. writeDb.Create(&log)
  1725. }
  1726. func UpdateDoctorEditAdvice(advice models.XtDoctorAdvice, orgid int64, groupno int64, date int64, patientid int64) error {
  1727. err := XTWriteDB().Model(&advice).Where("user_org_id = ? and groupno = ? and advice_date = ? and patient_id = ?", orgid, groupno, date, patientid).Update(map[string]interface{}{"start_time": advice.StartTime, "updated_time": advice.UpdatedTime}).Error
  1728. return err
  1729. }
  1730. func GetPatientsByKey(orgID int64, keywords string) (patient []*models.Patients, err error) {
  1731. db := readDb.Model(&models.Patients{}).Where("user_org_id=? and status=1 and lapseto = 1 ", orgID)
  1732. if len(keywords) > 0 {
  1733. likekey := "%" + keywords + "%"
  1734. err = db.Where("(name LIKE ? OR dialysis_no LIKE ? OR first_letter LIKE ?)", likekey, likekey, likekey).Find(&patient).Error
  1735. } else {
  1736. err = db.Find(&patient).Error
  1737. }
  1738. return
  1739. }
  1740. func FindAllDoctorAdviceByGoroupNoTwo(orgID int64, groupno int64) (advice []models.DoctorAdvice, err error) {
  1741. err = readDb.Model(&models.DoctorAdvice{}).Where("user_org_id=? AND groupno = ? AND status = 1 AND execution_state = 2", orgID, groupno).Find(&advice).Error
  1742. return
  1743. }
  1744. func FindDoctorAdviceByIdsTwo(orgID int64, ids []string) (advice []models.DoctorAdvice, err error) {
  1745. err = readDb.Model(&models.DoctorAdvice{}).Where("user_org_id = ? AND status = 1 AND execution_state = 2 AND (id IN (?) OR parent_id IN (?))", orgID, ids, ids).Find(&advice).Error
  1746. return
  1747. }
  1748. func CreateHisGroupAdvice(orgId int64, advices []*models.HisGroupAdvice, groupNo int64) (list []*models.HisGroupAdvice, err error) {
  1749. if groupNo <= 0 {
  1750. group := GetMaxAdviceGroupID(orgId)
  1751. groupNo = group + 1
  1752. }
  1753. tx := writeDb.Begin()
  1754. defer func() {
  1755. if r := recover(); r != nil {
  1756. tx.Rollback()
  1757. }
  1758. }()
  1759. for _, advice := range advices {
  1760. advice.Groupno = groupNo
  1761. if err = tx.Create(advice).Error; err != nil {
  1762. tx.Rollback()
  1763. return
  1764. }
  1765. list = append(list, advice)
  1766. if len(advice.Children) > 0 {
  1767. for _, child := range advice.Children {
  1768. child.Groupno = groupNo
  1769. child.ParentId = advice.ID
  1770. fmt.Println(child)
  1771. if err = tx.Create(&child).Error; err != nil {
  1772. tx.Rollback()
  1773. return
  1774. }
  1775. list = append(list, child)
  1776. }
  1777. }
  1778. }
  1779. tx.Commit()
  1780. return
  1781. }
  1782. func GetDialysisCount(orgid int64, partitionid int64) (order []*models.BloodDialysisOrderCount, err error) {
  1783. db := XTReadDB().Table("xt_dialysis_order as o")
  1784. if partitionid == 0 {
  1785. db.Raw("select count(o.id) as count,o.patient_id from xt_dialysis_order as o left join xt_schedule as x on x.patient_id = o.patient_id where o.status =1 and o.user_org_id = ? and x.schedule_date = o.dialysis_date and x.status = 1", orgid).Group("o.patient_id").Scan(&order)
  1786. }
  1787. if partitionid > 0 {
  1788. db.Raw("select count(o.id) as count,o.patient_id from xt_dialysis_order as o left join xt_schedule as x on x.patient_id = o.patient_id where o.status =1 and o.user_org_id = ? and x.partition_id = ? and x.schedule_date = o.dialysis_date and x.status = 1", orgid, partitionid).Group("o.patient_id").Scan(&order)
  1789. }
  1790. return order, err
  1791. }
  1792. func UpdatePatientDialysisSolutionOne(patientid int64, orgid int64, prescription *models.DialysisPrescription, timenow int64) error {
  1793. err = XTWriteDB().Model(&prescription).Where("patient_id = ? and user_org_id = ? and record_date = ? ", patientid, orgid, timenow).Updates(map[string]interface{}{"dialyzer": prescription.Dialyzer, "anticoagulant": prescription.Anticoagulant, "mode_id": prescription.ModeId, "dialysis_duration_hour": prescription.DialysisDurationHour, "anticoagulant_shouji": prescription.AnticoagulantShouji, "anticoagulant_weichi": prescription.AnticoagulantWeichi, "anticoagulant_zongliang": prescription.AnticoagulantZongliang, "kalium": prescription.Kalium, "sodium": prescription.Sodium, "calcium": prescription.Calcium, "dialyzer_perfusion_apparatus": prescription.DialyzerPerfusionApparatus, "blood_access": prescription.BloodAccess, "dialysate_flow": prescription.DialysateFlow, "dialysate_temperature": prescription.DialysateTemperature, "dialysis_dialyszers": prescription.DialysisDialyszers, "dialysis_irrigation": prescription.DialysisIrrigation, "plasma_separator": prescription.PlasmaSeparator, "bilirubin_adsorption_column": prescription.BilirubinAdsorptionColumn, "oxygen_uptake": prescription.OxygenUptake, "oxygen_flow": prescription.OxygenFlow, "oxygen_time": prescription.OxygenTime, "dialysis_strainer": prescription.DialysisStrainer, "chaptalization": prescription.Chaptalization, "blood_access_part_id": prescription.BloodAccessPartId}).Error
  1794. return err
  1795. }
  1796. //func FindRemindAdvice(user_org_id int64, advice_name string, advice_desc string, template_id string, fre_type int64, patient_id int64, record_date int64) (advice models.DoctorAdvice, err error) {
  1797. // err = XTWriteDB().Model(&models.DoctorAdvice{}).Where("patient_id = ? and user_org_id = ? and record_date = ? and advice_name = ? and advice_desc = ? and template_id = ? and frequency_type = ? ", patient_id, user_org_id, record_date, advice_name, advice_desc, template_id, fre_type).First(&advice).Error
  1798. // return
  1799. //}
  1800. func FindRemindAdvice(user_org_id int64, advice_name string, advice_desc string, template_id string, fre_type int64, patient_id int64, record_date int64) (advice models.DoctorAdvice, err error) {
  1801. err = XTWriteDB().Model(&models.DoctorAdvice{}).Where("patient_id = ? and user_org_id = ? and record_date = ? and advice_name = ? and advice_desc = ? and template_id = ? and frequency_type = ? AND advice_type = 2 ", patient_id, user_org_id, record_date, advice_name, advice_desc, template_id, fre_type).First(&advice).Error
  1802. return
  1803. }
  1804. func UpdateScheduleByDeathTime(patientid int64, shcheduledate int64) error {
  1805. err := XTWriteDB().Model(models.XtSchedule{}).Where("patient_id = ? and schedule_date > ?", patientid, shcheduledate).Updates(map[string]interface{}{"status": 0}).Error
  1806. return err
  1807. }
  1808. func UpdateScheduleItemByPatientId(id int64) error {
  1809. err := XTWriteDB().Model(models.PatientScheduleTemplateItem{}).Where("patient_id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  1810. return err
  1811. }
  1812. func GetDoctorAdviceListThree(orgID, patientID, advice_type, stop, start, end int64, keywords string, page int64, limit int64) (advices []*models.HisDoctorAdvice, total int64, err error) {
  1813. db := readDb.Table("his_doctor_advice_info as x").Where("x.status = 1")
  1814. table := UserReadDB().Table("sgj_user_admin_role as r")
  1815. fmt.Print("table", table)
  1816. if orgID > 0 {
  1817. db = db.Where("x.user_org_id=?", orgID)
  1818. }
  1819. if patientID > 0 {
  1820. db = db.Where("x.patient_id = ?", patientID)
  1821. }
  1822. if advice_type == 1 && advice_type > 0 {
  1823. db = db.Where("x.advice_type = ?", advice_type)
  1824. }
  1825. if advice_type == 3 && advice_type > 0 {
  1826. db = db.Where("x.advice_type = 2 or x.advice_type = 3")
  1827. }
  1828. if advice_type == 4 && advice_type > 0 {
  1829. db = db.Where("x.advice_type = 4")
  1830. }
  1831. if stop == 1 {
  1832. db = db.Where("(x.stop_state=? or x.execution_state=?)", stop, stop)
  1833. } else if stop == 2 {
  1834. db = db.Where("x.stop_state=? and x.execution_state=?", stop, stop)
  1835. }
  1836. if start != 0 {
  1837. db = db.Where("x.start_time>=?", start)
  1838. }
  1839. if end != 0 {
  1840. db = db.Where("x.start_time<=?", end)
  1841. }
  1842. if len(keywords) > 0 {
  1843. likeKey := "%" + keywords + "%"
  1844. db = db.Where("x.advice_name LIKE ?", likeKey)
  1845. }
  1846. offset := (page - 1) * limit
  1847. err = db.Group("x.id").Count(&total).Offset(offset).Limit(limit).Select("x.id, x.user_org_id, x.patient_id, x.advice_type, x.advice_date, x.record_date, x.start_time, x.advice_name,x.advice_desc, x.reminder_date, x.drug_spec, x.drug_spec_unit, x.single_dose, x.single_dose_unit, x.prescribing_number, x.prescribing_number_unit, x.delivery_way, x.execution_frequency, x.advice_doctor, x.status, x.created_time,x.updated_time, x.advice_affirm, x.remark, x.stop_time, x.stop_reason, x.stop_doctor, x.stop_state, x.parent_id, x.execution_time, x.execution_staff, x.execution_state, x.checker, x.check_state, x.check_time, x.groupno,x.remind_type,x.frequency_type,x.day_count,x.week_day,x.parent_id,r.user_name, IF(x.parent_id > 0, x.parent_id, x.id) as advice_order").Joins("Left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.advice_doctor").Order("start_time desc, groupno desc, advice_order desc, id asc").Scan(&advices).Error
  1848. fmt.Print("err", err)
  1849. return
  1850. }
  1851. func GetDoctorAdviceListFour(orgID int64, patientID int64, advice_type int64, stop int64, start int64, end int64, keywords string, limit int64, page int64) (advices []*models.HisDoctorAdvice, total int64, err error) {
  1852. db := readDb.Table("his_doctor_advice_info as x").Where("x.status = 1")
  1853. table := UserReadDB().Table("sgj_user_admin_role as r")
  1854. fmt.Println(table)
  1855. if orgID > 0 {
  1856. db = db.Where("x.user_org_id=?", orgID)
  1857. }
  1858. if patientID > 0 {
  1859. db = db.Where("x.patient_id = ?", patientID)
  1860. }
  1861. if advice_type == 1 {
  1862. db = db.Where("x.advice_type = ?", advice_type)
  1863. }
  1864. if advice_type == 3 && advice_type > 0 {
  1865. db = db.Where("x.advice_type = 2 or x.advice_type = 3")
  1866. }
  1867. if advice_type == 4 && advice_type > 0 {
  1868. db = db.Where("x.advice_type = 4")
  1869. }
  1870. if stop == 1 {
  1871. db = db.Where("(x.stop_state=? or x.execution_state=?)", stop, stop)
  1872. } else if stop == 2 {
  1873. db = db.Where("x.stop_state=? and x.execution_state=?", stop, stop)
  1874. }
  1875. if start != 0 {
  1876. db = db.Where("x.start_time>=?", start)
  1877. }
  1878. if end != 0 {
  1879. db = db.Where("x.start_time<=?", end)
  1880. }
  1881. if len(keywords) > 0 {
  1882. likeKey := "%" + keywords + "%"
  1883. db = db.Where("x.advice_name LIKE ?", likeKey)
  1884. }
  1885. offset := (page - 1) * limit
  1886. err = db.Order("x.start_time desc").Group("x.start_time").Count(&total).Offset(offset).Limit(limit).Select("x.id, x.user_org_id, x.patient_id, x.advice_type, x.advice_date, x.record_date, x.start_time, x.advice_name,x.advice_desc, x.reminder_date, x.drug_spec, x.drug_spec_unit, x.single_dose, x.single_dose_unit, x.prescribing_number, x.prescribing_number_unit, x.delivery_way, x.execution_frequency, x.advice_doctor, x.status, x.created_time,x.updated_time, x.advice_affirm, x.remark, x.stop_time, x.stop_reason, x.stop_doctor, x.stop_state, x.parent_id, x.execution_time, x.execution_staff, x.execution_state, x.checker, x.check_state, x.check_time, x.groupno,x.remind_type,x.frequency_type,x.day_count,x.week_day,x.parent_id,r.user_name, IF(x.parent_id > 0, x.parent_id, x.id) as advice_order").Joins("Left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.advice_doctor").Scan(&advices).Error
  1887. fmt.Print("错误是什么", err)
  1888. return
  1889. }
  1890. func GetAllDeviceNumberByListSix(orgID int64) (number []*models.DeviceNumber, err error) {
  1891. err = readDb.Model(&models.DeviceNumber{}).Where("org_id=? and status=1", orgID).Find(&number).Error
  1892. return number, err
  1893. }
  1894. func GetAllDeviceNumberByList(orgID int64) (number []*models.DeviceNumber, err error) {
  1895. redis := RedisClient()
  1896. defer redis.Close()
  1897. key := strconv.FormatInt(orgID, 10) + ":" + ":device_number_all"
  1898. number_info_str, _ := redis.Get(key).Result()
  1899. if len(number_info_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1900. err = readDb.Model(&models.DeviceNumber{}).Where("org_id=? and status=1", orgID).Find(&number).Error
  1901. if err != nil {
  1902. if err == gorm.ErrRecordNotFound {
  1903. return nil, nil
  1904. } else {
  1905. return nil, err
  1906. }
  1907. } else {
  1908. if len(number) > 0 {
  1909. //缓存数据
  1910. device_number_json, err := json.Marshal(&number)
  1911. if err == nil {
  1912. redis.Set(key, device_number_json, time.Second*60*60*18)
  1913. }
  1914. }
  1915. return number, nil
  1916. }
  1917. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  1918. json.Unmarshal([]byte(number_info_str), &number)
  1919. return number, nil
  1920. }
  1921. }
  1922. func GetAllPrescriptionByListTwo(orgID int64, scheduleDate int64) (prescriptions []*models.DialysisPrescription, err error) {
  1923. redis := RedisClient()
  1924. defer redis.Close()
  1925. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":prescriptions_list_all"
  1926. prescriptions_list_all, _ := redis.Get(key).Result()
  1927. if len(prescriptions_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1928. if scheduleDate > 0 {
  1929. err = readDb.Model(&models.DialysisPrescription{}).Where("status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).Find(&prescriptions).Error
  1930. } else {
  1931. err = readDb.Model(&models.DialysisPrescription{}).Where("status = 1 AND user_org_id = ?", orgID).Find(&prescriptions).Error
  1932. }
  1933. if err != nil {
  1934. if err == gorm.ErrRecordNotFound {
  1935. return nil, nil
  1936. } else {
  1937. return nil, err
  1938. }
  1939. } else {
  1940. if len(prescriptions) > 0 {
  1941. //缓存数据
  1942. prescriptions_list_all, err := json.Marshal(&prescriptions)
  1943. if err == nil {
  1944. redis.Set(key, prescriptions_list_all, time.Second*60*60*18)
  1945. }
  1946. }
  1947. return prescriptions, nil
  1948. }
  1949. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  1950. json.Unmarshal([]byte(prescriptions_list_all), &prescriptions)
  1951. return prescriptions, nil
  1952. }
  1953. }
  1954. func GetAllAssessmentBeforesByListThree(orgID int64, scheduleDate int64) (assessmentBefores []*models.PredialysisEvaluation, err error) {
  1955. redis := RedisClient()
  1956. defer redis.Close()
  1957. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":assessment_befores_list_all"
  1958. assessment_befores_list_all, _ := redis.Get(key).Result()
  1959. if len(assessment_befores_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1960. if scheduleDate > 0 {
  1961. err = readDb.Model(&models.PredialysisEvaluation{}).Where("status = 1 AND user_org_id = ? AND assessment_date = ? ", orgID, scheduleDate).Find(&assessmentBefores).Error
  1962. } else {
  1963. err = readDb.Model(&models.PredialysisEvaluation{}).Where("status = 1 AND user_org_id = ?", orgID).Find(&assessmentBefores).Error
  1964. }
  1965. if err != nil {
  1966. if err == gorm.ErrRecordNotFound {
  1967. return nil, nil
  1968. } else {
  1969. return nil, err
  1970. }
  1971. } else {
  1972. if len(assessmentBefores) > 0 {
  1973. //缓存数据
  1974. assessment_befores_list_all, err := json.Marshal(&assessmentBefores)
  1975. if err == nil {
  1976. redis.Set(key, assessment_befores_list_all, time.Second*60*60*18)
  1977. }
  1978. }
  1979. return assessmentBefores, nil
  1980. }
  1981. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  1982. json.Unmarshal([]byte(assessment_befores_list_all), &assessmentBefores)
  1983. return assessmentBefores, nil
  1984. }
  1985. }
  1986. func GetAllDialysisOrdersByListThree(orgID int64, scheduleDate int64) (dialysisOrders []*models.MonitorDialysisOrder, err error) {
  1987. redis := RedisClient()
  1988. defer redis.Close()
  1989. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":dialysis_orders_list_all"
  1990. dialysis_orders_list_all, _ := redis.Get(key).Result()
  1991. if len(dialysis_orders_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1992. if scheduleDate > 0 {
  1993. err = readDb.Model(&models.MonitorDialysisOrder{}).Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).Where("status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, scheduleDate).Find(&dialysisOrders).Error
  1994. } else {
  1995. err = readDb.Model(&models.MonitorDialysisOrder{}).Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).Where("status = 1 AND user_org_id = ?", orgID).Find(&dialysisOrders).Error
  1996. }
  1997. if err != nil {
  1998. if err == gorm.ErrRecordNotFound {
  1999. return nil, nil
  2000. } else {
  2001. return nil, err
  2002. }
  2003. } else {
  2004. if len(dialysisOrders) > 0 {
  2005. //缓存数据
  2006. dialysis_orders_list_all, err := json.Marshal(&dialysisOrders)
  2007. if err == nil {
  2008. redis.Set(key, dialysis_orders_list_all, time.Second*60*60*18)
  2009. }
  2010. }
  2011. return dialysisOrders, nil
  2012. }
  2013. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2014. json.Unmarshal([]byte(dialysis_orders_list_all), &dialysisOrders)
  2015. return dialysisOrders, nil
  2016. }
  2017. }
  2018. func GetAllAssessmentAfterDislysisByListThree(orgID int64, scheduleDate int64) (assessmentAfterDislysis []*models.AssessmentAfterDislysis, err error) {
  2019. redis := RedisClient()
  2020. defer redis.Close()
  2021. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":assessment_after_dislysis_list_all"
  2022. assessment_after_dislysis__all, _ := redis.Get(key).Result()
  2023. if len(assessment_after_dislysis__all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2024. if scheduleDate > 0 {
  2025. err = readDb.Model(&models.AssessmentAfterDislysis{}).Where("status = 1 AND user_org_id = ? AND assessment_date = ?", orgID, scheduleDate).Find(&assessmentAfterDislysis).Error
  2026. } else {
  2027. err = readDb.Model(&models.AssessmentAfterDislysis{}).Where("status = 1 AND user_org_id = ?", orgID).Find(&assessmentAfterDislysis).Error
  2028. }
  2029. if err != nil {
  2030. if err == gorm.ErrRecordNotFound {
  2031. return nil, nil
  2032. } else {
  2033. return nil, err
  2034. }
  2035. } else {
  2036. if len(assessmentAfterDislysis) > 0 {
  2037. //缓存数据
  2038. assessment_after_dislysis__all, err := json.Marshal(&assessmentAfterDislysis)
  2039. if err == nil {
  2040. redis.Set(key, assessment_after_dislysis__all, time.Second*60*60*18)
  2041. }
  2042. }
  2043. return assessmentAfterDislysis, nil
  2044. }
  2045. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2046. json.Unmarshal([]byte(assessment_after_dislysis__all), &assessmentAfterDislysis)
  2047. return assessmentAfterDislysis, nil
  2048. }
  2049. }
  2050. func GetAllMonitorListThree(orgID int64, scheduleDate int64) (monitor []*models.MonitoringRecord, err error) {
  2051. redis := RedisClient()
  2052. defer redis.Close()
  2053. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":monitor_record_list_all"
  2054. monitor_record_list_all, _ := redis.Get(key).Result()
  2055. if len(monitor_record_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2056. if scheduleDate > 0 {
  2057. err = readDb.Model(&models.MonitoringRecord{}).Where("status = 1 AND user_org_id = ? AND monitoring_date= ?", orgID, scheduleDate).Find(&monitor).Error
  2058. } else {
  2059. err = readDb.Model(&models.MonitoringRecord{}).Where("status = 1 AND user_org_id = ?", orgID).Find(&monitor).Error
  2060. }
  2061. if err != nil {
  2062. if err == gorm.ErrRecordNotFound {
  2063. return nil, nil
  2064. } else {
  2065. return nil, err
  2066. }
  2067. } else {
  2068. if len(monitor) > 0 {
  2069. //缓存数据
  2070. assessment_after_dislysis__all, err := json.Marshal(&monitor)
  2071. if err == nil {
  2072. redis.Set(key, assessment_after_dislysis__all, time.Second*60*60*18)
  2073. }
  2074. }
  2075. return monitor, nil
  2076. }
  2077. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2078. json.Unmarshal([]byte(monitor_record_list_all), &monitor)
  2079. return monitor, nil
  2080. }
  2081. }
  2082. func GetAllPatientListByListThree(orgID int64) (patients []*models.MonitorPatients, err error) {
  2083. redis := RedisClient()
  2084. defer redis.Close()
  2085. key := strconv.FormatInt(orgID, 10) + ":" + ":patient_list_all"
  2086. patient_info_str, _ := redis.Get(key).Result()
  2087. if len(patient_info_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2088. err = readDb.Model(&models.MonitorPatients{}).Where("user_org_id=? and status=1 and lapseto = 1", orgID).Find(&patients).Error
  2089. if err != nil {
  2090. if err == gorm.ErrRecordNotFound {
  2091. return nil, nil
  2092. } else {
  2093. return nil, err
  2094. }
  2095. } else {
  2096. if len(patients) > 0 {
  2097. //缓存数据
  2098. patient_info_json, err := json.Marshal(&patients)
  2099. if err == nil {
  2100. redis.Set(key, patient_info_json, time.Second*60*60*18)
  2101. }
  2102. }
  2103. return patients, nil
  2104. }
  2105. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2106. json.Unmarshal([]byte(patient_info_str), &patients)
  2107. return patients, nil
  2108. }
  2109. }
  2110. func GetAllPatientListByListSix(orgID int64, keywords string) (patients []*models.MonitorPatients, err error) {
  2111. db := readDb.Model(&models.MonitorPatients{}).Where("status = 1 and lapseto = 1")
  2112. likeKey := "%" + keywords + "%"
  2113. if orgID > 0 {
  2114. db = db.Where("user_org_id = ?", orgID)
  2115. }
  2116. if len(keywords) > 0 {
  2117. db = db.Where("name like ?", likeKey)
  2118. }
  2119. err = db.Find(&patients).Error
  2120. return patients, err
  2121. }
  2122. func GetAllDialysisOrdersByListTwo(orgID int64, scheduleDate int64) (dialysisOrders []*MDialysisOrderVM, err error) {
  2123. redis := RedisClient()
  2124. defer redis.Close()
  2125. key := strconv.FormatInt(orgID, 10) + ":" + ":dialysis_orders_list_all"
  2126. dialysis_orders_list_all, _ := redis.Get(key).Result()
  2127. if scheduleDate < 1669132800 {
  2128. fmt.Println("key233232232332323232", key)
  2129. redis.Set(key, "", time.Second)
  2130. }
  2131. fmt.Println("dialysis_orders_list_all", dialysis_orders_list_all)
  2132. if len(dialysis_orders_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2133. err = readDb.Model(&MDialysisOrderVM{}).Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).Where("status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, scheduleDate).Find(&dialysisOrders).Error
  2134. if err != nil {
  2135. if err == gorm.ErrRecordNotFound {
  2136. return nil, nil
  2137. } else {
  2138. return nil, err
  2139. }
  2140. } else {
  2141. if len(dialysisOrders) > 0 {
  2142. //缓存数据
  2143. dialysis_orders_list_all, err := json.Marshal(&dialysisOrders)
  2144. if err == nil {
  2145. redis.Set(key, dialysis_orders_list_all, time.Second*60*60*18)
  2146. }
  2147. }
  2148. return dialysisOrders, nil
  2149. }
  2150. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2151. json.Unmarshal([]byte(dialysis_orders_list_all), &dialysisOrders)
  2152. return dialysisOrders, nil
  2153. }
  2154. }
  2155. func GetAllZoneByList(orgID int64) (zone []*models.DeviceZone, err error) {
  2156. redis := RedisClient()
  2157. defer redis.Close()
  2158. key := strconv.FormatInt(orgID, 10) + ":" + ":device_zone_all"
  2159. zone_info_str, _ := redis.Get(key).Result()
  2160. if len(zone_info_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2161. err = readDb.Model(&models.DeviceZone{}).Where("org_id=? and status=1", orgID).Find(&zone).Error
  2162. if err != nil {
  2163. if err == gorm.ErrRecordNotFound {
  2164. return nil, nil
  2165. } else {
  2166. return nil, err
  2167. }
  2168. } else {
  2169. if len(zone) > 0 {
  2170. //缓存数据
  2171. zone_number_json, err := json.Marshal(&zone)
  2172. if err == nil {
  2173. redis.Set(key, zone_number_json, time.Second*60*60*18)
  2174. }
  2175. }
  2176. return zone, nil
  2177. }
  2178. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2179. json.Unmarshal([]byte(zone_info_str), &zone)
  2180. return zone, nil
  2181. }
  2182. }
  2183. func GetAllDialysisOrdersByListNight(orgID int64, scheduleDate int64, pay_tyep int64) (dialysisOrders []*models.VMMonitorDialysisOrder, err error) {
  2184. if pay_tyep == 0 {
  2185. 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
  2186. }
  2187. //待下机
  2188. if pay_tyep == 3 {
  2189. err = readDb.Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).Where("status = 1 AND user_org_id = ? AND dialysis_date = ? and end_time = 0", orgID, scheduleDate).Find(&dialysisOrders).Error
  2190. }
  2191. fmt.Println("pay_typeowowowoowwo", pay_tyep)
  2192. if pay_tyep == 7 {
  2193. err = readDb.Preload("DeviceNumber", "status = 1 AND org_id = ?'", orgID).Where("status = 1 AND user_org_id = ? AND dialysis_date = ? and url = ''", orgID, scheduleDate).Find(&dialysisOrders).Error
  2194. }
  2195. return dialysisOrders, err
  2196. }
  2197. func GetAllPrescriptionByListSix(orgID int64, scheduleDate int64) (prescriptions []*models.VMDialysisPrescription, err error) {
  2198. err = readDb.Model(&models.VMDialysisPrescription{}).Where("status= 1 and user_org_id = ? and record_date = ?", orgID, scheduleDate).Find(&prescriptions).Error
  2199. return prescriptions, err
  2200. //redis := RedisClient()
  2201. //defer redis.Close()
  2202. //key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":prescriptions_list_all"
  2203. //redis.Set(key, "", time.Second)
  2204. //prescriptions_list_all, _ := redis.Get(key).Result()
  2205. //
  2206. //if len(prescriptions_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2207. // err = readDb.Model(&models.VMDialysisPrescription{}).Where("status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).Find(&prescriptions).Error
  2208. // if err != nil {
  2209. // if err == gorm.ErrRecordNotFound {
  2210. // return nil, nil
  2211. // } else {
  2212. // return nil, err
  2213. // }
  2214. // } else {
  2215. // if len(prescriptions) > 0 {
  2216. // //缓存数据
  2217. // prescriptions_list_all, err := json.Marshal(&prescriptions)
  2218. // if err == nil {
  2219. // redis.Set(key, prescriptions_list_all, time.Second*60*60*18)
  2220. // }
  2221. // }
  2222. // return prescriptions, nil
  2223. // }
  2224. //} else { //缓存数据了数据,将redis缓存的json字符串转为map
  2225. // json.Unmarshal([]byte(prescriptions_list_all), &prescriptions)
  2226. // return prescriptions, nil
  2227. //}
  2228. }
  2229. func GetAllPrescriptionByList(orgID int64, scheduleDate int64) (prescriptions []*models.VMDialysisPrescription, err error) {
  2230. redis := RedisClient()
  2231. defer redis.Close()
  2232. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":prescriptions_list_all"
  2233. redis.Set(key, "", time.Second)
  2234. prescriptions_list_all, _ := redis.Get(key).Result()
  2235. if len(prescriptions_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2236. err = readDb.Model(&models.VMDialysisPrescription{}).Where("status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).Find(&prescriptions).Error
  2237. if err != nil {
  2238. if err == gorm.ErrRecordNotFound {
  2239. return nil, nil
  2240. } else {
  2241. return nil, err
  2242. }
  2243. } else {
  2244. if len(prescriptions) > 0 {
  2245. //缓存数据
  2246. prescriptions_list_all, err := json.Marshal(&prescriptions)
  2247. if err == nil {
  2248. redis.Set(key, prescriptions_list_all, time.Second*60*60*18)
  2249. }
  2250. }
  2251. return prescriptions, nil
  2252. }
  2253. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2254. json.Unmarshal([]byte(prescriptions_list_all), &prescriptions)
  2255. return prescriptions, nil
  2256. }
  2257. }
  2258. func GetAllAssessmentBeforesByListOne(orgID int64, scheduleDate int64) (assessmentBefores []*models.VMPredialysisEvaluation, err error) {
  2259. redis := RedisClient()
  2260. defer redis.Close()
  2261. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":assessment_befores_list_all"
  2262. assessment_befores_list_all, _ := redis.Get(key).Result()
  2263. if len(assessment_befores_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2264. err = readDb.Model(&models.VMPredialysisEvaluation{}).Where("status = 1 AND user_org_id = ? AND assessment_date = ? ", orgID, scheduleDate).Find(&assessmentBefores).Error
  2265. if err != nil {
  2266. if err == gorm.ErrRecordNotFound {
  2267. return nil, nil
  2268. } else {
  2269. return nil, err
  2270. }
  2271. } else {
  2272. if len(assessmentBefores) > 0 {
  2273. //缓存数据
  2274. assessment_befores_list_all, err := json.Marshal(&assessmentBefores)
  2275. if err == nil {
  2276. redis.Set(key, assessment_befores_list_all, time.Second*60*60*18)
  2277. }
  2278. }
  2279. return assessmentBefores, nil
  2280. }
  2281. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2282. json.Unmarshal([]byte(assessment_befores_list_all), &assessmentBefores)
  2283. return assessmentBefores, nil
  2284. }
  2285. }
  2286. func GetAllDialysisOrdersByListSix(orgID int64, scheduleDate int64) (dialysisOrders []*models.VMMonitorDialysisOrder, err error) {
  2287. err = readDb.Model(&models.VMMonitorDialysisOrder{}).Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).Where("status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, scheduleDate).Find(&dialysisOrders).Error
  2288. return dialysisOrders, err
  2289. }
  2290. func GetAllDialysisOrdersByListSeven(orgID int64, scheduleDate int64) (dialysisOrders []*MDialysisOrderVM, err error) {
  2291. err = readDb.Model(&MDialysisOrderVM{}).Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).Where("status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, scheduleDate).Find(&dialysisOrders).Error
  2292. return dialysisOrders, err
  2293. }
  2294. func GetAllDialysisOrdersByListOne(orgID int64, scheduleDate int64) (dialysisOrders []*models.VMMonitorDialysisOrder, err error) {
  2295. redis := RedisClient()
  2296. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":dialysis_orders_list_all"
  2297. fmt.Println("key22332323232323223", key)
  2298. dialysis_orders_list_all, _ := redis.Get(key).Result()
  2299. defer redis.Close()
  2300. if len(dialysis_orders_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2301. err = readDb.Model(&models.VMMonitorDialysisOrder{}).Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).Where("status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, scheduleDate).Find(&dialysisOrders).Error
  2302. if err != nil {
  2303. if err == gorm.ErrRecordNotFound {
  2304. return nil, nil
  2305. } else {
  2306. return nil, err
  2307. }
  2308. } else {
  2309. if len(dialysisOrders) > 0 {
  2310. //缓存数据
  2311. dialysis_orders_list_all, err := json.Marshal(&dialysisOrders)
  2312. if err == nil {
  2313. redis.Set(key, dialysis_orders_list_all, time.Second*60*60*18)
  2314. }
  2315. }
  2316. return dialysisOrders, nil
  2317. }
  2318. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2319. json.Unmarshal([]byte(dialysis_orders_list_all), &dialysisOrders)
  2320. return dialysisOrders, nil
  2321. }
  2322. }
  2323. func GetAllAssessmentAfterDislysisByListSix(orgID int64, scheduleDate int64) (assessmentAfterDislysis []*models.VMAssessmentAfterDislysis, err error) {
  2324. err = readDb.Model(&models.VMAssessmentAfterDislysis{}).Where("status=1 and user_org_id = ? and assessment_date = ?", orgID, scheduleDate).Find(&assessmentAfterDislysis).Error
  2325. return assessmentAfterDislysis, err
  2326. //redis := RedisClient()
  2327. //defer redis.Close()
  2328. //key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":assessment_after_dislysis_list_all"
  2329. //redis.Set(key, "", time.Second)
  2330. //assessment_after_dislysis__all, _ := redis.Get(key).Result()
  2331. //
  2332. //if len(assessment_after_dislysis__all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2333. // err = readDb.Model(&models.VMAssessmentAfterDislysis{}).Where("status = 1 AND user_org_id = ? AND assessment_date = ?", orgID, scheduleDate).Find(&assessmentAfterDislysis).Error
  2334. // if err != nil {
  2335. // if err == gorm.ErrRecordNotFound {
  2336. // return nil, nil
  2337. // } else {
  2338. // return nil, err
  2339. // }
  2340. // } else {
  2341. // if len(assessmentAfterDislysis) > 0 {
  2342. // //缓存数据
  2343. // assessment_after_dislysis__all, err := json.Marshal(&assessmentAfterDislysis)
  2344. // if err == nil {
  2345. // redis.Set(key, assessment_after_dislysis__all, time.Second*60*60*18)
  2346. // }
  2347. // }
  2348. // return assessmentAfterDislysis, nil
  2349. // }
  2350. //} else { //缓存数据了数据,将redis缓存的json字符串转为map
  2351. // json.Unmarshal([]byte(assessment_after_dislysis__all), &assessmentAfterDislysis)
  2352. // return assessmentAfterDislysis, nil
  2353. //}
  2354. }
  2355. func GetAllAssessmentAfterDislysisByListOne(orgID int64, scheduleDate int64) (assessmentAfterDislysis []*models.VMAssessmentAfterDislysis, err error) {
  2356. redis := RedisClient()
  2357. defer redis.Close()
  2358. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":assessment_after_dislysis_list_all"
  2359. redis.Set(key, "", time.Second)
  2360. assessment_after_dislysis__all, _ := redis.Get(key).Result()
  2361. if len(assessment_after_dislysis__all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2362. err = readDb.Model(&models.VMAssessmentAfterDislysis{}).Where("status = 1 AND user_org_id = ? AND assessment_date = ?", orgID, scheduleDate).Find(&assessmentAfterDislysis).Error
  2363. if err != nil {
  2364. if err == gorm.ErrRecordNotFound {
  2365. return nil, nil
  2366. } else {
  2367. return nil, err
  2368. }
  2369. } else {
  2370. if len(assessmentAfterDislysis) > 0 {
  2371. //缓存数据
  2372. assessment_after_dislysis__all, err := json.Marshal(&assessmentAfterDislysis)
  2373. if err == nil {
  2374. redis.Set(key, assessment_after_dislysis__all, time.Second*60*60*18)
  2375. }
  2376. }
  2377. return assessmentAfterDislysis, nil
  2378. }
  2379. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2380. json.Unmarshal([]byte(assessment_after_dislysis__all), &assessmentAfterDislysis)
  2381. return assessmentAfterDislysis, nil
  2382. }
  2383. }
  2384. func GetAllMonitorList(orgID int64, scheduleDate int64) (monitor []*models.VMMonitoringRecord, err error) {
  2385. redis := RedisClient()
  2386. defer redis.Close()
  2387. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":monitor_record_list_all"
  2388. monitor_record_list_all, _ := redis.Get(key).Result()
  2389. fmt.Println("monitor_recorde_list", len(monitor_record_list_all))
  2390. if len(monitor_record_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2391. err = readDb.Model(&models.VMMonitoringRecord{}).Where("status = 1 AND user_org_id = ? AND monitoring_date= ?", orgID, scheduleDate).Order("monitoring_date asc").Find(&monitor).Error
  2392. if err != nil {
  2393. if err == gorm.ErrRecordNotFound {
  2394. return nil, nil
  2395. } else {
  2396. return nil, err
  2397. }
  2398. } else {
  2399. if len(monitor) > 0 {
  2400. //缓存数据
  2401. assessment_after_dislysis__all, err := json.Marshal(&monitor)
  2402. if err == nil {
  2403. redis.Set(key, assessment_after_dislysis__all, time.Second*60*60*18)
  2404. }
  2405. }
  2406. return monitor, nil
  2407. }
  2408. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2409. json.Unmarshal([]byte(monitor_record_list_all), &monitor)
  2410. return monitor, nil
  2411. }
  2412. }
  2413. func GetAllPatientListByListOne(orgID int64) (patients []*models.VMMonitorPatients, err error) {
  2414. redis := RedisClient()
  2415. defer redis.Close()
  2416. key := strconv.FormatInt(orgID, 10) + ":" + ":patient_list_all"
  2417. patient_info_str, _ := redis.Get(key).Result()
  2418. redis.Set(key, "", time.Second)
  2419. if len(patient_info_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2420. err = readDb.Model(&models.VMMonitorPatients{}).Where("user_org_id=? and status=1", orgID).Find(&patients).Error
  2421. if err != nil {
  2422. if err == gorm.ErrRecordNotFound {
  2423. return nil, nil
  2424. } else {
  2425. return nil, err
  2426. }
  2427. } else {
  2428. if len(patients) > 0 {
  2429. //缓存数据
  2430. patient_info_json, err := json.Marshal(&patients)
  2431. if err == nil {
  2432. redis.Set(key, patient_info_json, time.Second*60*60*18)
  2433. }
  2434. }
  2435. return patients, nil
  2436. }
  2437. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2438. json.Unmarshal([]byte(patient_info_str), &patients)
  2439. return patients, nil
  2440. }
  2441. }
  2442. func GetAllTreatModeByList(orgID int64) (treatement []*models.TreatmentMode, err error) {
  2443. redis := RedisClient()
  2444. defer redis.Close()
  2445. key := strconv.FormatInt(orgID, 10) + ":" + ":treatement_mode_all"
  2446. treatment_info_str, _ := redis.Get(key).Result()
  2447. if len(treatment_info_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2448. err = readDb.Model(&models.TreatmentMode{}).Where("status=1").Find(&treatement).Error
  2449. if err != nil {
  2450. if err == gorm.ErrRecordNotFound {
  2451. return nil, nil
  2452. } else {
  2453. return nil, err
  2454. }
  2455. } else {
  2456. if len(treatement) > 0 {
  2457. //缓存数据
  2458. patient_info_json, err := json.Marshal(&treatement)
  2459. if err == nil {
  2460. redis.Set(key, patient_info_json, time.Second*60*60*18)
  2461. }
  2462. }
  2463. return treatement, nil
  2464. }
  2465. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2466. json.Unmarshal([]byte(treatment_info_str), &treatement)
  2467. return treatement, nil
  2468. }
  2469. }
  2470. func GetAllPatientListByListSeven(orgID int64) (patients []*MSchedualPatientVM, err error) {
  2471. err = readDb.Model(&MSchedualPatientVM{}).Where("user_org_id=? and status=1 and lapseto = 1", orgID).Find(&patients).Error
  2472. return patients, nil
  2473. }
  2474. func GetAllPatientListByListTwo(orgID int64) (patients []*MSchedualPatientVM, err error) {
  2475. redis := RedisClient()
  2476. defer redis.Close()
  2477. key := strconv.FormatInt(orgID, 10) + ":" + ":patient_list_all"
  2478. patient_info_str, _ := redis.Get(key).Result()
  2479. if len(patient_info_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2480. err = readDb.Model(&MSchedualPatientVM{}).Where("user_org_id=? and status=1 and lapseto = 1", orgID).Find(&patients).Error
  2481. if err != nil {
  2482. if err == gorm.ErrRecordNotFound {
  2483. return nil, nil
  2484. } else {
  2485. return nil, err
  2486. }
  2487. } else {
  2488. if len(patients) > 0 {
  2489. //缓存数据
  2490. patient_info_json, err := json.Marshal(&patients)
  2491. if err == nil {
  2492. redis.Set(key, patient_info_json, time.Second*60*60*18)
  2493. }
  2494. }
  2495. return patients, nil
  2496. }
  2497. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2498. json.Unmarshal([]byte(patient_info_str), &patients)
  2499. return patients, nil
  2500. }
  2501. }
  2502. func GetAllDeviceNumberByListOne(orgID int64) (number []*MDeviceNumberVM, err error) {
  2503. redis := RedisClient()
  2504. defer redis.Close()
  2505. key := strconv.FormatInt(orgID, 10) + ":" + ":device_number_all"
  2506. number_info_str, _ := redis.Get(key).Result()
  2507. if len(number_info_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2508. err = readDb.Model(&MDeviceNumberVM{}).Where("org_id=? and status=1", orgID).Find(&number).Error
  2509. if err != nil {
  2510. if err == gorm.ErrRecordNotFound {
  2511. return nil, nil
  2512. } else {
  2513. return nil, err
  2514. }
  2515. } else {
  2516. if len(number) > 0 {
  2517. //缓存数据
  2518. device_number_json, err := json.Marshal(&number)
  2519. if err == nil {
  2520. redis.Set(key, device_number_json, time.Second*60*60*18)
  2521. }
  2522. }
  2523. return number, nil
  2524. }
  2525. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2526. json.Unmarshal([]byte(number_info_str), &number)
  2527. return number, nil
  2528. }
  2529. }
  2530. func GetAllPrescriptionByListOne(orgID int64, scheduleDate int64) (prescriptions []*models.DialysisPrescriptionTwenty, err error) {
  2531. redis := RedisClient()
  2532. defer redis.Close()
  2533. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":prescriptions_list_all"
  2534. prescriptions_list_all, _ := redis.Get(key).Result()
  2535. if len(prescriptions_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2536. err = readDb.Model(&models.DialysisPrescriptionTwenty{}).Where("status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).Find(&prescriptions).Error
  2537. if err != nil {
  2538. if err == gorm.ErrRecordNotFound {
  2539. return nil, nil
  2540. } else {
  2541. return nil, err
  2542. }
  2543. } else {
  2544. if len(prescriptions) > 0 {
  2545. //缓存数据
  2546. prescriptions_list_all, err := json.Marshal(&prescriptions)
  2547. if err == nil {
  2548. redis.Set(key, prescriptions_list_all, time.Second*60*60*18)
  2549. }
  2550. }
  2551. return prescriptions, nil
  2552. }
  2553. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2554. json.Unmarshal([]byte(prescriptions_list_all), &prescriptions)
  2555. return prescriptions, nil
  2556. }
  2557. }
  2558. func GetAllHisProjectPrescription(orgid int64, recorddate int64) (project []*models.HisPrescriptionProjectTwo, err error) {
  2559. err = XTReadDB().Where("user_org_id = ? and record_date = ? and status = 1", orgid, recorddate).Find(&project).Error
  2560. return project, err
  2561. }
  2562. func GetZoneCountList(orgid int64, schedule_date int64) (list []*models.XtScheduleSeven, err error) {
  2563. db := XTReadDB().Model(&list).Where("status = 1")
  2564. if orgid > 0 {
  2565. db = db.Where("user_org_id = ?", orgid)
  2566. }
  2567. if schedule_date > 0 {
  2568. db = db.Where("schedule_date =?", schedule_date)
  2569. }
  2570. err = db.Select("partition_id,count(id) as count").Group("partition_id").Scan(&list).Error
  2571. return list, err
  2572. }
  2573. func GetPatientDialysisLongSolution(patient_id int64, page int64, limit int64, orgid int64) (solution []*models.DialysisSolution, total int64, err error) {
  2574. db := XTReadDB().Model(&solution).Where("status = 1 and solution_status = 1")
  2575. if patient_id > 0 {
  2576. db = db.Where("patient_id = ?", patient_id)
  2577. }
  2578. if orgid > 0 {
  2579. db = db.Where("user_org_id = ?", orgid)
  2580. }
  2581. offset := (page - 1) * limit
  2582. err = db.Group("mode_id").Count(&total).Offset(offset).Limit(limit).Order("created_time desc").Find(&solution).Error
  2583. return solution, total, err
  2584. }
  2585. func GetDialysisSolutionDetailList(orgid int64, patient_id int64, mode_id int64, page int64, limit int64) (solution []*models.DialysisSolution, total int64, err error) {
  2586. offset := (page - 1) * limit
  2587. db := XTReadDB().Model(&solution).Where("status = 1")
  2588. if orgid > 0 {
  2589. db = db.Where("user_org_id = ?", orgid)
  2590. }
  2591. if patient_id > 0 {
  2592. db = db.Where("patient_id = ?", patient_id)
  2593. }
  2594. if mode_id > 0 {
  2595. db = db.Where("mode_id = ?", mode_id)
  2596. }
  2597. err = db.Count(&total).Offset(offset).Limit(limit).Find(&solution).Order("created_time desc").Error
  2598. return solution, total, err
  2599. }
  2600. func UpdateDialysisBefor(dislysis models.XtAssessmentBeforeDislysis, id int64) error {
  2601. err := XTWriteDB().Model(&models.XtAssessmentBeforeDislysis{}).Where("id = ? and status= 1", id).Update(map[string]interface{}{"systolic_blood_pressure": dislysis.SystolicBloodPressure, "diastolic_blood_pressure": dislysis.DiastolicBloodPressure}).Error
  2602. return err
  2603. }
  2604. func GetAllSchedulesByPatientInfo(org_id int64, patient_id int64, mode_id int64) ([]Schedule, error) {
  2605. var record []Schedule
  2606. if org_id == 10597 {
  2607. timeNowStr := time.Now().Format("2006-01-02")
  2608. timeNewDate, _ := utils.ParseTimeStringToTime("2006-01-02", timeNowStr)
  2609. err := readDb.Model(&Schedule{}).Where("status = 1 AND schedule_date >? AND patient_id = ? AND user_org_id = ? AND mode_id= ?", timeNewDate.Unix(), patient_id, org_id, mode_id).Find(&record).Error
  2610. if err != nil {
  2611. if err == gorm.ErrRecordNotFound {
  2612. return nil, nil
  2613. } else {
  2614. return nil, err
  2615. }
  2616. }
  2617. } else {
  2618. timeNowStr := time.Now().Format("2006-01-02")
  2619. timeNewDate, _ := utils.ParseTimeStringToTime("2006-01-02", timeNowStr)
  2620. err := readDb.Model(&Schedule{}).Where("status = 1 AND schedule_date >= ? AND patient_id = ? AND user_org_id = ? AND mode_id= ?", timeNewDate.Unix(), patient_id, org_id, mode_id).Find(&record).Error
  2621. if err != nil {
  2622. if err == gorm.ErrRecordNotFound {
  2623. return nil, nil
  2624. } else {
  2625. return nil, err
  2626. }
  2627. }
  2628. }
  2629. return record, nil
  2630. }
  2631. func UpdateScheduleByExport(org_id int64, shcheduledate int64) error {
  2632. err := XTWriteDB().Model(models.XtSchedule{}).Where("user_org_id = ? and schedule_date > ?", org_id, shcheduledate).Updates(map[string]interface{}{"status": 0}).Error
  2633. return err
  2634. }
  2635. func GetContextScheduleListGroupPatientId(limit int64, page int64, startTime int64, endTime int64, org_id int64) (schedule []*models.ConScheduleList, total int64, err error) {
  2636. db := XTReadDB().Model(&schedule).Where("status=1")
  2637. offset := (page - 1) * limit
  2638. if startTime > 0 {
  2639. db = db.Where("schedule_date >=?", startTime)
  2640. }
  2641. if endTime > 0 {
  2642. db = db.Where("schedule_date<=?", endTime)
  2643. }
  2644. if org_id > 0 {
  2645. db = db.Where("user_org_id = ?", org_id)
  2646. }
  2647. err = db.Count(&total).Offset(offset).Limit(limit).Group("patient_id").Find(&schedule).Error
  2648. return schedule, total, err
  2649. }
  2650. func GetContextScheduleListPatientId(startTime int64, endTime int64, org_id int64) (schedule []*models.ConScheduleList, total int64, err error) {
  2651. db := XTReadDB().Model(&schedule).Where("status=1")
  2652. if startTime > 0 {
  2653. db = db.Where("schedule_date >=?", startTime)
  2654. }
  2655. if endTime > 0 {
  2656. db = db.Where("schedule_date<=?", endTime)
  2657. }
  2658. if org_id > 0 {
  2659. db = db.Where("user_org_id = ?", org_id)
  2660. }
  2661. err = db.Find(&schedule).Error
  2662. return schedule, total, err
  2663. }
  2664. func GetPatientAllagicList(patient_id int64, user_org_id int64) (list []*models.XtPatientAllergic, err error) {
  2665. err = XTReadDB().Where("patient_id = ? and user_org_id =? and status= 1", patient_id, user_org_id).Find(&list).Error
  2666. return list, err
  2667. }
  2668. func GetNewMonitorRecord(orgID int64, date int64, partition int64, patientId int64) ([]*models.NewMonitorDialysisScheduleList, error) {
  2669. var mds []*models.NewMonitorDialysisScheduleList
  2670. db := readDb.Model(&models.NewMonitorDialysisScheduleList{})
  2671. if date > 0 {
  2672. db = db.Where("schedule_date = ?", date)
  2673. }
  2674. if partition > 0 {
  2675. db = db.Where("partition_id = ?", partition)
  2676. }
  2677. if patientId > 0 {
  2678. db = db.Where("patient_id = ?", patientId)
  2679. }
  2680. err = db.Where("status = 1 and user_org_id = ?", orgID).Find(&mds).Error
  2681. return mds, err
  2682. }
  2683. func GetAdviceList(user_org_id int64, advice_date int64, pat_type int64) (advice []*models.VMDoctorAdviceForList, err error) {
  2684. if pat_type == 0 {
  2685. err = XTReadDB().Where("user_org_id = ? and advice_date = ? and status =7 and check_time = 0", user_org_id, advice_date).Find(&advice).Error
  2686. }
  2687. //待医嘱核对
  2688. if pat_type == 1 {
  2689. err = XTReadDB().Where("user_org_id = ? and advice_date = ? and status =1 and check_time = 0", user_org_id, advice_date).Find(&advice).Error
  2690. }
  2691. //待医嘱执行
  2692. if pat_type == 6 {
  2693. err = XTReadDB().Where("user_org_id = ? and advice_date = ? and status =1 and execution_state = 2", user_org_id, advice_date).Find(&advice).Error
  2694. }
  2695. return advice, err
  2696. }
  2697. func GetDobuleCheck(user_org_id int64, advice_date int64) (check []*models.VmDoubleCheck, err error) {
  2698. err = XTReadDB().Where("user_org_id = ? and check_date = ? and status = 1", user_org_id, advice_date).Find(&check).Error
  2699. return check, err
  2700. }
  2701. func GetTreatmentSummaryForList(user_org_id int64, advice_date int64) (summary []*models.VMTreatmentSummaryForList, err error) {
  2702. err = XTReadDB().Where("user_org_id =? and assessment_date = ? and status =1", user_org_id, advice_date).Find(&summary).Error
  2703. return summary, err
  2704. }
  2705. func GetDialysisInformationList(user_org_id int64, advice_date int64) (information []*models.NewDeviceInformation, err error) {
  2706. err = UserReadDB().Where("user_org_id =? and date = ? and status =1", user_org_id, advice_date).Find(&information).Error
  2707. return information, err
  2708. }
  2709. func GetNewAllMonitorList(orgID int64, scheduleDate int64, pat_type int64) (monitor []*models.VMMonitoringRecord, err error) {
  2710. if pat_type == 0 || pat_type == 9 {
  2711. err = readDb.Model(&models.VMMonitoringRecord{}).Where("status = 1 AND user_org_id = ? AND monitoring_date= ?", orgID, scheduleDate).Order("monitoring_date asc").Find(&monitor).Error
  2712. }
  2713. if pat_type == 10 {
  2714. err = readDb.Model(&models.VMMonitoringRecord{}).Where("status = 1 AND user_org_id = ? AND monitoring_date= ? AND accumulated_blood_volume = 0", orgID, scheduleDate).Order("monitoring_date asc").Find(&monitor).Error
  2715. }
  2716. if pat_type == 11 {
  2717. err = readDb.Model(&models.VMMonitoringRecord{}).Where("status = 1 AND user_org_id = ? AND monitoring_date= ? AND symptom <> ''", orgID, scheduleDate).Order("monitoring_date asc").Find(&monitor).Error
  2718. }
  2719. if pat_type == 12 {
  2720. err = readDb.Model(&models.VMMonitoringRecord{}).Where("status = 1 AND user_org_id = ? AND monitoring_date= ? AND dispose <> ''", orgID, scheduleDate).Order("monitoring_date asc").Find(&monitor).Error
  2721. }
  2722. return monitor, err
  2723. }
  2724. func GetBaseDrugByName(drug_name string, org_id int64) (models.BaseDrugLibSeven, error) {
  2725. libSeven := models.BaseDrugLibSeven{}
  2726. err := XTReadDB().Where("drug_name= ? and org_id = ? and status=1", drug_name, org_id).First(&libSeven).Error
  2727. return libSeven, err
  2728. }
  2729. func CreateDeviceValsualMap(valsualMap models.XtDeviceValsualMap) error {
  2730. err := XTWriteDB().Save(&valsualMap).Error
  2731. return err
  2732. }
  2733. func GetPatientSitemap(patient_id int64, user_org_id int64) (models.XtDeviceValsualMap, error) {
  2734. valsualMap := models.XtDeviceValsualMap{}
  2735. err := XTReadDB().Where("patient_id = ? and status =1 and user_org_id = ?", patient_id, user_org_id).Preload("XtDeviceValsualALeft", "status=1 and user_org_id =?", user_org_id).Preload("XtDeviceValsualVLeft", "status=1 and user_org_id =?", user_org_id).Preload("XtDeviceValsualMapFangxiangA", "status=1 and user_org_id =?", user_org_id).Preload("XtDeviceValsualMapFangxiangV", "status=1 and user_org_id =?", user_org_id).Preload("XtDeviceValsualFuzhuA", "status=1 and user_org_id =?", user_org_id).Preload("XtDeviceValsualFuzhuV", "status=1 and user_org_id =?", user_org_id).Preload("XtDeviceValsualTextArr", "status=1 and user_org_id =?", user_org_id).Preload("DeviceValusalMapArr", "status=1 and user_org_id =?", user_org_id).Preload("XtDeviceValusalMapVrr", "status=1 and user_org_id =?", user_org_id).Find(&valsualMap).Error
  2736. return valsualMap, err
  2737. }
  2738. func UpdateSiteMap(valsualMap models.XtDeviceValsualMap) error {
  2739. err := XTWriteDB().Save(&valsualMap).Error
  2740. return err
  2741. }
  2742. func CreateValsualAleft(left models.XtDeviceValsualALeft) error {
  2743. err := XTWriteDB().Create(&left).Error
  2744. return err
  2745. }
  2746. func UpdateValsualAleft(patient_id int64, user_org_id int64) error {
  2747. err := XTWriteDB().Model(&models.XtDeviceValsualALeft{}).Where("patient_id = ? and user_org_id = ? and status =1", patient_id, user_org_id).Updates(map[string]interface{}{"status": 0}).Error
  2748. return err
  2749. }
  2750. func CreateValsualMapFangxiangA(a models.XtDeviceValsualMapFangxiangA) error {
  2751. err := XTWriteDB().Create(&a).Error
  2752. return err
  2753. }
  2754. func UpdateValsualMapFangxiangA(patient_id int64, user_org_id int64) error {
  2755. err := XTWriteDB().Model(&models.XtDeviceValsualMapFangxiangA{}).Where("patient_id = ? and user_org_id = ? and status =1", patient_id, user_org_id).Updates(map[string]interface{}{"status": 0}).Error
  2756. return err
  2757. }
  2758. func CreateValsualMapFangxiangV(v models.XtDeviceValsualMapFangxiangV) error {
  2759. err := XTWriteDB().Create(&v).Error
  2760. return err
  2761. }
  2762. func UpdateValsualMapFangxiangV(patient_id int64, user_org_id int64) error {
  2763. err := XTWriteDB().Model(&models.XtDeviceValsualMapFangxiangV{}).Where("patient_id = ? and user_org_id = ? and status=1", patient_id, user_org_id).Updates(map[string]interface{}{"status": 0}).Error
  2764. return err
  2765. }
  2766. func ValsualMapFangxiangA(patient_id int64, user_org_id int64) error {
  2767. err := XTWriteDB().Model(models.XtDeviceValsualMapFangxiangA{}).Where("patient_id = ? and user_org_id = ? and status=1", patient_id, user_org_id).Updates(map[string]interface{}{"status": 0}).Error
  2768. return err
  2769. }
  2770. func CreateValSualFuzhA(a models.XtDeviceValsualFuzhuA) error {
  2771. err := XTReadDB().Create(&a).Error
  2772. return err
  2773. }
  2774. func UpdateValSualFuzhA(patient_id int64, user_org_id int64) error {
  2775. err := XTWriteDB().Model(&models.XtDeviceValsualFuzhuA{}).Where("patient_id =? and user_org_id = ? and status=1", patient_id, user_org_id).Updates(map[string]interface{}{"status": 0}).Error
  2776. return err
  2777. }
  2778. func CreateVasuaLfuzhuV(v models.XtDeviceValsualFuzhuV) error {
  2779. err := XTReadDB().Create(&v).Error
  2780. return err
  2781. }
  2782. func UpdateVasuaLfuzhuV(patient_id int64, user_org_id int64) error {
  2783. err := XTWriteDB().Model(&models.XtDeviceValsualFuzhuV{}).Where("patient_id = ? and user_org_id =? and status=1", patient_id, user_org_id).Updates(map[string]interface{}{"status": 0}).Error
  2784. return err
  2785. }
  2786. func CreateTextArr(arr models.XtDeviceValsualTextArr) error {
  2787. err := XTReadDB().Create(&arr).Error
  2788. return err
  2789. }
  2790. func UpdateTextArr(patient_id int64, user_org_id int64) error {
  2791. err := XTWriteDB().Model(&models.XtDeviceValsualTextArr{}).Where("patient_id = ? and user_org_id = ? and status=1", patient_id, user_org_id).Updates(map[string]interface{}{"status": 0}).Error
  2792. return err
  2793. }
  2794. func CreateValsualVleft(left models.XtDeviceValsualVLeft) error {
  2795. err = XTReadDB().Create(&left).Error
  2796. return err
  2797. }
  2798. func UpdateValsualVleft(patient_id int64, user_org_id int64) error {
  2799. err := XTWriteDB().Model(&models.XtDeviceValsualVLeft{}).Where("patient_id = ? and user_org_id =? and status=1", patient_id, user_org_id).Updates(map[string]interface{}{"status": 0}).Error
  2800. return err
  2801. }
  2802. func UpdateValusalMapArr(patient_id int64, user_org_id int64) error {
  2803. err := XTWriteDB().Model(&models.XtDeviceValusalMapArr{}).Where("patient_id =? and user_org_id =? and status=1", patient_id, user_org_id).Updates(map[string]interface{}{"status": 0}).Error
  2804. return err
  2805. }
  2806. func CreateDeviceValusalMapArr(vrr models.XtDeviceValusalMapArr) error {
  2807. err := XTWriteDB().Create(&vrr).Error
  2808. return err
  2809. }
  2810. func UpdateDeviceValusalMapVrr(patient_id int64, user_org_id int64) error {
  2811. err := XTWriteDB().Model(&models.XtDeviceValusalMapVrr{}).Where("patient_id = ? and user_org_id =? and status=1", patient_id, user_org_id).Updates(map[string]interface{}{"status": 0}).Error
  2812. return err
  2813. }
  2814. func CreateDeviceValusalMapVrr(vrr models.XtDeviceValusalMapVrr) error {
  2815. err := XTWriteDB().Create(&vrr).Error
  2816. return err
  2817. }
  2818. func GetDialysisOrderNoExecution(start_nurse int64, user_org_id int64, timenow int64) (order []*models.XtDialysisOrder, err error) {
  2819. err = XTReadDB().Where("start_nurse = ? and user_org_id =? and status =1 and dialysis_date =?", start_nurse, user_org_id, timenow).Find(&order).Error
  2820. return order, err
  2821. }
  2822. func GetAdviceListNoExecution(advice_date int64, user_org_id int64, patientIds []int64) (advice []*models.NoDoctorAdvice, err error) {
  2823. db := XTReadDB().Model(advice).Where("status=1 and execution_state = 2 and advice_type = 2")
  2824. if advice_date > 0 {
  2825. db = db.Where("advice_date= ?", advice_date)
  2826. }
  2827. if user_org_id > 0 {
  2828. db = db.Where("user_org_id =?", user_org_id)
  2829. }
  2830. if len(patientIds) > 0 {
  2831. db = db.Where("patient_id in(?)", patientIds)
  2832. }
  2833. err = db.Select("patient_id,Count(id) as count,advice_date").Group("patient_id").Find(&advice).Error
  2834. return advice, err
  2835. }
  2836. func GetHisAdviceListNoExecution(advice_date int64, user_org_id int64, patientIds []int64) (advice []*models.HisNoDoctorAdvice, err error) {
  2837. db := XTReadDB().Model(advice).Where("status=1 and execution_state = 2")
  2838. if advice_date > 0 {
  2839. db = db.Where("advice_date= ?", advice_date)
  2840. }
  2841. if user_org_id > 0 {
  2842. db = db.Where("user_org_id =?", user_org_id)
  2843. }
  2844. if len(patientIds) > 0 {
  2845. db = db.Where("patient_id in(?)", patientIds)
  2846. }
  2847. err = db.Select("advice_date,patient_id,Count(id) as count").Group("patient_id").Find(&advice).Error
  2848. return advice, err
  2849. }
  2850. func GetProjectListNoExecution(record_date int64, user_org_id int64, patientIds []int64) (project []*models.NoHisPrescriptionProject, err error) {
  2851. db := XTReadDB().Model(&project).Where("status=1 and execution_state =0")
  2852. if record_date > 0 {
  2853. db = db.Where("record_date= ?", record_date)
  2854. }
  2855. if user_org_id > 0 {
  2856. db = db.Where("user_org_id =?", user_org_id)
  2857. }
  2858. if len(patientIds) > 0 {
  2859. db = db.Where("patient_id in(?)", patientIds)
  2860. }
  2861. err = db.Select("patient_id,Count(id) as count,record_date as advice_date").Group("patient_id").Find(&project).Error
  2862. return project, err
  2863. }
  2864. func GetPatientNoExecutionList(org_id int64, patientsIds []int64) (patient []*models.XtPatients, err error) {
  2865. err = XTReadDB().Where("user_org_id = ? and id in(?)", org_id, patientsIds).Find(&patient).Error
  2866. return patient, err
  2867. }
  2868. func FindHisConfig(org_id int64) (err error, config models.XtHisConfig) {
  2869. err = readDb.Model(&models.XtHisConfig{}).Where("status = 1 AND user_org_id = ?", org_id).First(&config).Error
  2870. return
  2871. }
  2872. func GetPatientDetailInformedConsent(patient_id int64, org_id int64) (models.XtPatients, error) {
  2873. patients := models.XtPatients{}
  2874. err := XTReadDB().Where("id = ? and status=1 and user_org_id =?", patient_id, org_id).Find(&patients).Error
  2875. return patients, err
  2876. }
  2877. func GetAllPrescriptionList(org_id int64) (prescription []*models.DialysisPrescription, err error) {
  2878. err = XTReadDB().Where("user_org_id = ? and status =1", org_id).Find(&prescription).Error
  2879. return prescription, err
  2880. }