patient_service.go 186KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863
  1. package service
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. "time"
  8. "XT_New/models"
  9. "XT_New/utils"
  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, patient_type 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. if patient_type > 0 {
  65. db = db.Where("p.patient_type = ?", patient_type)
  66. }
  67. offset := (page - 1) * limit
  68. if lapseto == 2 || lapseto == 3 {
  69. 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,p.patient_type").Group("p.id").Count(&total).Offset(offset).Limit(limit).Find(&patients).Error
  70. }
  71. if lapseto != 2 && lapseto != 3 {
  72. if orgID != 10579 && orgID != 10600 && orgID != 10587 {
  73. 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,p.patient_type").Group("p.id").Count(&total).Offset(offset).Limit(limit).Find(&patients).Error
  74. }
  75. if orgID == 10579 || orgID == 10600 {
  76. 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,p.patient_type").Group("p.id").Count(&total).Offset(offset).Limit(limit).Find(&patients).Error
  77. }
  78. if orgID == 10587 {
  79. err = db.Order("p.created_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,p.patient_type").Group("p.id").Count(&total).Offset(offset).Limit(limit).Find(&patients).Error
  80. }
  81. }
  82. return
  83. }
  84. // GetAllPatientList 返回全部患者的列表
  85. func GetAllPatientList(orgID int64) (patients []*models.Patients, total int64, err error) {
  86. db := readDb.Table("xt_patients as p").Where("p.user_org_id=? and p.status=1", orgID)
  87. 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,p.age,p.id_card_no").Count(&total).Find(&patients).Error
  88. return
  89. }
  90. func GetAllPatientListOne(orgID int64) (patients []*models.Patients, total int64, err error) {
  91. db := readDb.Table("xt_patients as p").Where("p.user_org_id=? and p.status=1 and p.lapseto =1", orgID)
  92. 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,p.age,p.id_card_no").Count(&total).Find(&patients).Error
  93. return
  94. }
  95. func GetAllPatientListTwenty(orgID int64) (patients []*models.Patients, total int64, err error) {
  96. db := readDb.Table("xt_patients as p").Where("p.user_org_id=? and p.status=1 and p.lapseto = 1", orgID)
  97. 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
  98. return
  99. }
  100. func GetAllPatientListSix(orgID int64) (patients []*models.MSchedualPatientList, err error) {
  101. err = XTReadDB().Model(&patients).Where("user_org_id = ? and status = 1", orgID).Find(&patients).Error
  102. return patients, err
  103. }
  104. // GetAllPatientList 返回全部患者的列表
  105. func GetAllPatientListByList(orgID int64) (patients []*models.MSchedualPatientList, err error) {
  106. redis := RedisClient()
  107. defer redis.Close()
  108. key := strconv.FormatInt(orgID, 10) + ":" + ":patient_list_all"
  109. patient_info_str, _ := redis.Get(key).Result()
  110. if len(patient_info_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  111. err = readDb.Model(&models.MSchedualPatientList{}).Where("user_org_id=? and status=1 and lapseto = 1", orgID).Find(&patients).Error
  112. if err != nil {
  113. if err == gorm.ErrRecordNotFound {
  114. if len(patients) <= 0 {
  115. redis.Set(key, "null", time.Second*60*60*18)
  116. }
  117. return nil, nil
  118. } else {
  119. return nil, err
  120. }
  121. } else {
  122. if len(patients) > 0 {
  123. //缓存数据
  124. patient_info_json, err := json.Marshal(&patients)
  125. if err == nil {
  126. redis.Set(key, patient_info_json, time.Second*60*60*18)
  127. }
  128. } else {
  129. redis.Set(key, "null", time.Second*60*60*18)
  130. }
  131. return patients, nil
  132. }
  133. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  134. if patient_info_str == "null" {
  135. return nil, nil
  136. } else {
  137. json.Unmarshal([]byte(patient_info_str), &patients)
  138. return patients, nil
  139. }
  140. }
  141. }
  142. func GetAllDevicetByListSix(orgID int64) (devices []*models.MDeviceNumberForList, err error) {
  143. err = readDb.Model(&models.MDeviceNumberForList{}).Preload("Zone", "status = 1 AND org_id = ?", orgID).Where("org_id=?", orgID).Find(&devices).Error
  144. return devices, err
  145. }
  146. func GetMonitorByListSix(orgID int64, monitor_date int64) (monitor []models.VMMonitoringRecord, err error) {
  147. err = readDb.Where("user_org_id = ? and monitor_date = ? and status =1", orgID, monitor_date).Find(&monitor).Error
  148. return monitor, err
  149. }
  150. func GetAllDevicetByList(orgID int64) (devices []*models.MDeviceNumberForList, err error) {
  151. redis := RedisClient()
  152. defer redis.Close()
  153. key := strconv.FormatInt(orgID, 10) + ":" + ":device_list_all"
  154. device_list_str, _ := redis.Get(key).Result()
  155. if len(device_list_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  156. err = readDb.Model(&models.MDeviceNumberForList{}).Preload("Zone", "status = 1 AND org_id = ?", orgID).Where("org_id=? and status=1 ", orgID).Find(&devices).Error
  157. if err != nil {
  158. if err == gorm.ErrRecordNotFound {
  159. if len(devices) <= 0 {
  160. redis.Set(key, "null", time.Second*60*60*18)
  161. }
  162. return nil, nil
  163. } else {
  164. return nil, err
  165. }
  166. } else {
  167. if len(devices) > 0 {
  168. //缓存数据
  169. device_list_str, err := json.Marshal(&devices)
  170. if err == nil {
  171. redis.Set(key, device_list_str, time.Second*60*60*18)
  172. }
  173. } else {
  174. redis.Set(key, "null", time.Second*60*60*18)
  175. }
  176. return devices, nil
  177. }
  178. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  179. if device_list_str == "null" {
  180. return nil, nil
  181. } else {
  182. json.Unmarshal([]byte(device_list_str), &devices)
  183. return devices, nil
  184. }
  185. }
  186. }
  187. func GetAllAdvicestByList(orgID int64, scheduleDate int64) (advices []models.VMDoctorAdviceForList, err error) {
  188. redis := RedisClient()
  189. defer redis.Close()
  190. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":advice_list_all"
  191. advice_list_str, _ := redis.Get(key).Result()
  192. if len(advice_list_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  193. err = readDb.Model(&models.VMDoctorAdviceForList{}).Where("status = 1 AND user_org_id = ? AND advice_type = 2 AND advice_date = ? ", orgID, scheduleDate).Find(&advices).Error
  194. if err != nil {
  195. if err == gorm.ErrRecordNotFound {
  196. if len(advices) <= 0 {
  197. redis.Set(key, "null", time.Second*60*60*18)
  198. }
  199. return nil, nil
  200. } else {
  201. return nil, err
  202. }
  203. } else {
  204. if len(advices) > 0 {
  205. //缓存数据
  206. advice_list_str, err := json.Marshal(&advices)
  207. if err == nil {
  208. redis.Set(key, advice_list_str, time.Second*60*60*18)
  209. }
  210. } else {
  211. redis.Set(key, "null", time.Second*60*60*18)
  212. }
  213. return advices, nil
  214. }
  215. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  216. if advice_list_str == "null" {
  217. return nil, nil
  218. } else {
  219. json.Unmarshal([]byte(advice_list_str), &advices)
  220. return advices, nil
  221. }
  222. }
  223. }
  224. func GetAllPrescriptionsByList(orgID int64, scheduleDate int64) (prescriptions []*models.DialysisPrescriptionList, err error) {
  225. err = readDb.Where("status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).Find(&prescriptions).Error
  226. return prescriptions, err
  227. }
  228. //func GetAllPrescriptionsByList(orgID int64, scheduleDate int64) (prescriptions []*models.DialysisPrescriptionList, err error) {
  229. // redis := RedisClient()
  230. // defer redis.Close()
  231. // key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":prescriptions_list_all"
  232. // prescriptions_list_all, _ := redis.Get(key).Result()
  233. // redis.Set(prescriptions_list_all, "", time.Second)
  234. // if len(prescriptions_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  235. // err = readDb.Where("status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).Find(&prescriptions).Error
  236. // if err != nil {
  237. // if err == gorm.ErrRecordNotFound {
  238. // if len(prescriptions) <= 0 {
  239. // redis.Set(key, "null", time.Second*60*60*18)
  240. // }
  241. // return nil, nil
  242. // } else {
  243. // return nil, err
  244. // }
  245. // } else {
  246. // if len(prescriptions) > 0 {
  247. // //缓存数据
  248. // prescriptions_list_all, err := json.Marshal(&prescriptions)
  249. // if err == nil {
  250. // redis.Set(key, prescriptions_list_all, time.Second*60*60*18)
  251. // }
  252. // } else {
  253. // redis.Set(key, "null", time.Second*60*60*18)
  254. // }
  255. // return prescriptions, nil
  256. // }
  257. // } else { //缓存数据了数据,将redis缓存的json字符串转为map
  258. // if prescriptions_list_all == "null" {
  259. // return nil, nil
  260. // } else {
  261. // json.Unmarshal([]byte(prescriptions_list_all), &prescriptions)
  262. // return prescriptions, nil
  263. // }
  264. //
  265. // }
  266. //}
  267. func GetAllAssessmentBeforesByList(orgID int64, scheduleDate int64) (assessmentBefores []*models.PredialysisEvaluationList, err error) {
  268. redis := RedisClient()
  269. defer redis.Close()
  270. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":assessment_befores_list_all"
  271. assessment_befores_list_all, _ := redis.Get(key).Result()
  272. if len(assessment_befores_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  273. err = readDb.Where("status = 1 AND user_org_id = ? AND assessment_date = ? ", orgID, scheduleDate).Find(&assessmentBefores).Error
  274. if err != nil {
  275. if err == gorm.ErrRecordNotFound {
  276. if len(assessmentBefores) <= 0 {
  277. redis.Set(key, "null", time.Second*60*60*18)
  278. }
  279. return nil, nil
  280. } else {
  281. return nil, err
  282. }
  283. } else {
  284. if len(assessmentBefores) > 0 {
  285. //缓存数据
  286. assessment_befores_list_all, err := json.Marshal(&assessmentBefores)
  287. if err == nil {
  288. redis.Set(key, assessment_befores_list_all, time.Second*60*60*18)
  289. }
  290. } else {
  291. redis.Set(key, "null", time.Second*60*60*18)
  292. }
  293. return assessmentBefores, nil
  294. }
  295. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  296. if assessment_befores_list_all == "null" {
  297. return nil, nil
  298. } else {
  299. json.Unmarshal([]byte(assessment_befores_list_all), &assessmentBefores)
  300. return assessmentBefores, nil
  301. }
  302. }
  303. }
  304. func GetAllDialysisOrdersByList(orgID int64, scheduleDate int64) (dialysisOrders []*models.MDialysisOrderForList, err error) {
  305. 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
  306. return dialysisOrders, err
  307. }
  308. //func GetAllDialysisOrdersByList(orgID int64, scheduleDate int64) (dialysisOrders []*models.MDialysisOrderForList, err error) {
  309. // redis := RedisClient()
  310. // defer redis.Close()
  311. // key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":dialysis_orders_list_all"
  312. // dialysis_orders_list_all, _ := redis.Get(key).Result()
  313. // redis.Set(dialysis_orders_list_all, "", time.Second)
  314. // if len(dialysis_orders_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  315. // 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
  316. //
  317. // if err != nil {
  318. // if err == gorm.ErrRecordNotFound {
  319. // return nil, nil
  320. // } else {
  321. // return nil, err
  322. // }
  323. // } else {
  324. // if len(dialysisOrders) > 0 {
  325. // //缓存数据
  326. // dialysis_orders_list_all, err := json.Marshal(&dialysisOrders)
  327. // if err == nil {
  328. // redis.Set(key, dialysis_orders_list_all, time.Second*60*60*18)
  329. // }
  330. // } else {
  331. // redis.Set(key, "null", time.Second*60*60*18)
  332. // }
  333. // return dialysisOrders, nil
  334. // }
  335. // } else { //缓存数据了数据,将redis缓存的json字符串转为map
  336. // if dialysis_orders_list_all == "null" {
  337. // return nil, nil
  338. // } else {
  339. // json.Unmarshal([]byte(dialysis_orders_list_all), &dialysisOrders)
  340. // return dialysisOrders, nil
  341. // }
  342. //
  343. // }
  344. //}
  345. func GetAllTreatmentSummarysByList(orgID int64, scheduleDate int64) (treatmentSummarys []*models.VMTreatmentSummaryForList, err error) {
  346. redis := RedisClient()
  347. defer redis.Close()
  348. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":treatment_summarys_list_all"
  349. treatment_summarys_all, _ := redis.Get(key).Result()
  350. if len(treatment_summarys_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  351. err = readDb.Model(&models.VMTreatmentSummaryForList{}).Where("status = 1 AND user_org_id = ? AND assessment_date = ?", orgID, scheduleDate).Find(&treatmentSummarys).Error
  352. if err != nil {
  353. if err == gorm.ErrRecordNotFound {
  354. if len(treatment_summarys_all) <= 0 {
  355. redis.Set(key, "null", time.Second*60*60*18)
  356. }
  357. return nil, nil
  358. } else {
  359. return nil, err
  360. }
  361. } else {
  362. if len(treatmentSummarys) > 0 {
  363. //缓存数据
  364. treatment_summarys_all, err := json.Marshal(&treatmentSummarys)
  365. if err == nil {
  366. redis.Set(key, treatment_summarys_all, time.Second*60*60*18)
  367. }
  368. } else {
  369. redis.Set(key, "null", time.Second*60*60*18)
  370. }
  371. return treatmentSummarys, nil
  372. }
  373. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  374. if treatment_summarys_all == "null" {
  375. return nil, nil
  376. } else {
  377. json.Unmarshal([]byte(treatment_summarys_all), &treatmentSummarys)
  378. return treatmentSummarys, nil
  379. }
  380. }
  381. }
  382. func GetAllAssessmentAfterDislysisByList(orgID int64, scheduleDate int64) (assessmentAfterDislysis []*models.VMAssessmentAfterDislysis, err error) {
  383. redis := RedisClient()
  384. defer redis.Close()
  385. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":assessment_after_dislysis_list_all"
  386. assessment_after_dislysis__all, _ := redis.Get(key).Result()
  387. if len(assessment_after_dislysis__all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  388. err = readDb.Model(&models.VMAssessmentAfterDislysis{}).Where("status = 1 AND user_org_id = ? AND assessment_date = ?", orgID, scheduleDate).Find(&assessmentAfterDislysis).Error
  389. if err != nil {
  390. if err == gorm.ErrRecordNotFound {
  391. if len(assessmentAfterDislysis) <= 0 {
  392. redis.Set(key, "null", time.Second*60*60*18)
  393. }
  394. return nil, nil
  395. } else {
  396. return nil, err
  397. }
  398. } else {
  399. if len(assessmentAfterDislysis) > 0 {
  400. //缓存数据
  401. assessment_after_dislysis__all, err := json.Marshal(&assessmentAfterDislysis)
  402. if err == nil {
  403. redis.Set(key, assessment_after_dislysis__all, time.Second*60*60*18)
  404. }
  405. } else {
  406. redis.Set(key, "null", time.Second*60*60*18)
  407. }
  408. return assessmentAfterDislysis, nil
  409. }
  410. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  411. if assessment_after_dislysis__all == "null" {
  412. return nil, nil
  413. } else {
  414. json.Unmarshal([]byte(assessment_after_dislysis__all), &assessmentAfterDislysis)
  415. return assessmentAfterDislysis, nil
  416. }
  417. }
  418. }
  419. func GetAllDobuleCheckList(orgID int64, scheduleDate int64) (dobulecheck []*models.DoubleCheck, err error) {
  420. err = XTReadDB().Where("check_date = ? and user_org_id = ? and status = 1", scheduleDate, orgID).Find(&dobulecheck).Error
  421. return dobulecheck, err
  422. }
  423. func GetAllHisAdvicesByList(orgID int64, scheduleDate int64) (hisAdvices []VMHisDoctorAdviceInfo, err error) {
  424. redis := RedisClient()
  425. defer redis.Close()
  426. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":his_advices_list_all"
  427. his_advices_all, _ := redis.Get(key).Result()
  428. redis.Set(key, "", time.Second)
  429. if orgID != 10721 {
  430. if len(his_advices_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  431. err = readDb.Model(&VMHisDoctorAdviceInfo{}).Where("status = 1 AND user_org_id = ? AND advice_date = ?", orgID, scheduleDate).Find(&hisAdvices).Error
  432. if err != nil {
  433. if err == gorm.ErrRecordNotFound {
  434. if len(hisAdvices) <= 0 {
  435. redis.Set(key, "null", time.Second*60*60*18)
  436. }
  437. return nil, nil
  438. } else {
  439. return nil, err
  440. }
  441. } else {
  442. if len(hisAdvices) > 0 {
  443. //缓存数据
  444. his_advices_all, err := json.Marshal(&hisAdvices)
  445. if err == nil {
  446. redis.Set(key, his_advices_all, time.Second*60*60*18)
  447. }
  448. } else {
  449. redis.Set(key, "null", time.Second*60*60*18)
  450. }
  451. return hisAdvices, nil
  452. }
  453. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  454. if his_advices_all == "null" {
  455. return nil, nil
  456. } else {
  457. json.Unmarshal([]byte(his_advices_all), &hisAdvices)
  458. return hisAdvices, nil
  459. }
  460. }
  461. }
  462. if orgID == 10721 {
  463. var keyword = "胰岛素"
  464. keyword = "%" + keyword + "%"
  465. if len(his_advices_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  466. err = readDb.Model(&VMHisDoctorAdviceInfo{}).Where("status = 1 AND user_org_id = ? AND advice_date = ? and execution_frequency !='上机前' and delivery_way!='口服' and advice_name not like ? ", orgID, scheduleDate, keyword).Find(&hisAdvices).Error
  467. if err != nil {
  468. if err == gorm.ErrRecordNotFound {
  469. if len(hisAdvices) <= 0 {
  470. redis.Set(key, "null", time.Second*60*60*18)
  471. }
  472. return nil, nil
  473. } else {
  474. return nil, err
  475. }
  476. } else {
  477. if len(hisAdvices) > 0 {
  478. //缓存数据
  479. his_advices_all, err := json.Marshal(&hisAdvices)
  480. if err == nil {
  481. redis.Set(key, his_advices_all, time.Second*60*60*18)
  482. }
  483. } else {
  484. redis.Set(key, "null", time.Second*60*60*18)
  485. }
  486. return hisAdvices, nil
  487. }
  488. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  489. if his_advices_all == "null" {
  490. return nil, nil
  491. } else {
  492. json.Unmarshal([]byte(his_advices_all), &hisAdvices)
  493. return hisAdvices, nil
  494. }
  495. }
  496. }
  497. return hisAdvices, nil
  498. }
  499. func GetPatientListByUpdateTime(orgID int64, syncTime int64) (patients []*models.PatientListForFaceList, total int64, err error) {
  500. 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)
  501. db = db.Where("updated_time >= ?", syncTime)
  502. err = db.Count(&total).Find(&patients).Error
  503. return
  504. }
  505. func GetPatientListBySchedules(orgID int64, syncTime int64) (patients []*models.SchedulePatients, total int64, err error) {
  506. 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)
  507. err = db.Count(&total).Find(&patients).Error
  508. return
  509. }
  510. func GetPatientListBySchedulesFind(orgID int64, syncTime int64, patient_name string) (patients []*models.SchedulePatients, total int64, err error) {
  511. 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+"%")
  512. err = db.Count(&total).Find(&patients).Error
  513. return
  514. }
  515. func GetPatientListById(orgID int64, patientId int64) (patients models.PatientListForFace, err error) {
  516. err = readDb.Model(&models.PatientListForFaceList{}).Where("user_org_id=? and id = ? and status=1", orgID, patientId).First(&patients).Error
  517. return
  518. }
  519. func GetPatientCount(orgID int64) (total int64) {
  520. readDb.Model(&models.Patients{}).Where("user_org_id=? and status=1 and lapseto = 1", orgID).Count(&total)
  521. return
  522. }
  523. func GetLapsetoPatientCount(orgID int64, lapseto int64) (total int64) {
  524. readDb.Model(&models.Patients{}).Where("user_org_id=? and lapseto=? and status=1", orgID, lapseto).Count(&total)
  525. return
  526. }
  527. func ChechLastDialysisNo(orgID int64) (dialysisNo int64) {
  528. var patient models.Patients
  529. if orgID == 10278 {
  530. err := readDb.Model(&models.Patients{}).Where("status=1 and user_org_id=?", orgID).Order("id desc").First(&patient).Error
  531. if err != nil {
  532. return
  533. }
  534. } else {
  535. err := readDb.Model(&models.Patients{}).Where("status=1 and user_org_id=?", orgID).Order("dialysis_no desc").First(&patient).Error
  536. if err != nil {
  537. return
  538. }
  539. }
  540. if patient.ID == 0 {
  541. return
  542. }
  543. dialysisNo, _ = strconv.ParseInt(patient.DialysisNo, 10, 64)
  544. return
  545. }
  546. func FindPatientByDialysisNo(orgID int64, dialysisNo string) (patient models.Patients, err error) {
  547. err = readDb.Model(&models.Patients{}).Where("status=1 and user_org_id=? and dialysis_no=? and lapseto = 1", orgID, dialysisNo).First(&patient).Error
  548. return
  549. }
  550. func FindPatientByIdCardNo(orgID int64, idCardNo string) (patient models.Patients, err error) {
  551. err = readDb.Model(&models.Patients{}).Where("status=1 and user_org_id=? and id_card_no=?", orgID, idCardNo).First(&patient).Error
  552. return
  553. }
  554. func FindPatientByMobile(orgID int64, mobile string) (patient models.Patients, err error) {
  555. err = readDb.Model(&models.Patients{}).Where("phone=? and user_org_id=? and status=1", mobile, orgID).First(&patient).Error
  556. return
  557. }
  558. func FindPatientById(orgID int64, id int64) (patient models.Patients, err error) {
  559. err = readDb.Model(&models.Patients{}).Where("id = ? and user_org_id=? and status=1", id, orgID).First(&patient).Error
  560. return
  561. }
  562. func FindPatientByIdTwo(orgID int64, id int64) (patient models.XtPatientsNew, err error) {
  563. err = readDb.Model(&models.XtPatientsNew{}).Where("blood_id = ? and user_org_id = ? and status =1", id, orgID).First(&patient).Error
  564. return
  565. }
  566. // func FindPatientByIdWithDiseases(orgID int64, id int64) (patient models.Patients, err error) {
  567. // 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
  568. // return
  569. // }
  570. func FindPatientByIdWithDiseases(orgID int64, id int64) (patient models.Patients, err error) {
  571. redis := RedisClient()
  572. defer redis.Close()
  573. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(id, 10) + ":patient_info"
  574. patient_info_str, _ := redis.Get(key).Result()
  575. redis.Set(key, "", time.Second)
  576. if len(patient_info_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  577. 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
  578. if err != nil {
  579. if err == gorm.ErrRecordNotFound {
  580. return patient, nil
  581. } else {
  582. return patient, err
  583. }
  584. } else {
  585. if patient.ID > 0 {
  586. //缓存数据
  587. patient_info_json, err := json.Marshal(&patient)
  588. if err == nil {
  589. redis.Set(key, patient_info_json, time.Second*60*60*18)
  590. }
  591. } else {
  592. redis.Set(key, "null", time.Second*60*60*18)
  593. }
  594. return patient, nil
  595. }
  596. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  597. if patient_info_str == "null" {
  598. return patient, nil
  599. } else {
  600. json.Unmarshal([]byte(patient_info_str), &patient)
  601. return patient, nil
  602. }
  603. }
  604. }
  605. func FindPatientWithDeviceByNo(orgID int64, no string, time int64) (patient models.SchedualPatient2, err error) {
  606. err = readDb.Preload("DialysisSchedule", func(db *gorm.DB) *gorm.DB {
  607. return db.Preload("DeviceNumber", "org_id = ?", orgID).
  608. Preload("DeviceZone", "org_id = ?", orgID).
  609. Where("user_org_id = ? AND schedule_date = ? ", orgID, time)
  610. }).Preload("InfectiousDiseases", "status = 1").Where("user_org_id=? and dialysis_no = ? and status = 1 ", orgID, no).First(&patient).Error
  611. return
  612. }
  613. func FindPatientWithDeviceByPatientId(orgID int64, id int64, time int64) (patient models.SchedualPatient2, err error) {
  614. err = readDb.Preload("DialysisSchedule", func(db *gorm.DB) *gorm.DB {
  615. return db.Preload("DeviceNumber", "org_id = ?", orgID).
  616. Preload("DeviceZone", "org_id = ?", orgID).
  617. Where("user_org_id = ? AND schedule_date = ? ", orgID, time)
  618. }).Preload("InfectiousDiseases", "status = 1").Where("user_org_id=? and id = ? and status = 1 ", orgID, id).First(&patient).Error
  619. return
  620. }
  621. func CreatePatient(patient *models.Patients, contagions []int64, diseases []int64) (err error) {
  622. user, _ := GetSgjUserByMobild(patient.Phone)
  623. customer, _ := GetSgjCoustomerByMobile(patient.UserOrgId, patient.Phone)
  624. utx := writeDb.Begin()
  625. btx := writeUserDb.Begin()
  626. if user.ID == 0 {
  627. user.Mobile = patient.Phone
  628. user.Avatar = patient.Avatar
  629. user.AvatarThumb = patient.Avatar
  630. user.Birthday = patient.Birthday
  631. user.Username = patient.Name
  632. user.Gender = patient.Gender
  633. user.Sources = 11
  634. user.Introduce = patient.Remark
  635. user.Status = 1
  636. user.UpdatedTime = patient.UpdatedTime
  637. user.CreatedTime = patient.CreatedTime
  638. err = btx.Create(&user).Error
  639. if err != nil {
  640. utx.Rollback()
  641. btx.Rollback()
  642. return
  643. }
  644. }
  645. patient.UserId = user.ID
  646. if customer == nil {
  647. err = btx.Create(&models.SgjCustomer{
  648. UserOrgId: patient.UserOrgId,
  649. UserId: user.ID,
  650. Mobile: patient.Phone,
  651. Name: patient.Name,
  652. Gender: patient.Gender,
  653. Birthday: patient.Birthday,
  654. Sources: 11,
  655. Status: 1,
  656. CreatedTime: patient.CreatedTime,
  657. UpdatedTime: patient.UpdatedTime,
  658. Avatar: patient.Avatar,
  659. Remark: patient.Remark,
  660. }).Error
  661. if err != nil {
  662. utx.Rollback()
  663. btx.Rollback()
  664. return
  665. }
  666. }
  667. err = utx.Create(patient).Error
  668. if err != nil {
  669. utx.Rollback()
  670. btx.Rollback()
  671. return
  672. }
  673. var lapseto models.PatientLapseto
  674. lapseto.PatientId = patient.ID
  675. lapseto.LapsetoType = patient.Lapseto
  676. lapseto.CreatedTime = patient.CreatedTime
  677. lapseto.UpdatedTime = patient.CreatedTime
  678. lapseto.Status = 1
  679. lapseto.LapsetoTime = patient.CreatedTime
  680. err = utx.Create(&lapseto).Error
  681. if err != nil {
  682. utx.Rollback()
  683. btx.Rollback()
  684. return
  685. }
  686. if len(contagions) > 0 {
  687. thisSQL := "INSERT INTO xt_patients_infectious_diseases (patient_id, disease_id, status, created_time, updated_time) VALUES "
  688. insertParams := make([]string, 0)
  689. insertData := make([]interface{}, 0)
  690. for _, contagion := range contagions {
  691. insertParams = append(insertParams, "(?, ?, ?, ?, ?)")
  692. insertData = append(insertData, patient.ID)
  693. insertData = append(insertData, contagion)
  694. insertData = append(insertData, 1)
  695. insertData = append(insertData, patient.CreatedTime)
  696. insertData = append(insertData, patient.UpdatedTime)
  697. }
  698. thisSQL += strings.Join(insertParams, ", ")
  699. err = utx.Exec(thisSQL, insertData...).Error
  700. if err != nil {
  701. utx.Rollback()
  702. btx.Rollback()
  703. return
  704. }
  705. }
  706. if len(diseases) > 0 {
  707. thisSQL := "INSERT INTO xt_patients_chronic_diseases (patient_id, disease_id, status, created_time, updated_time) VALUES "
  708. insertParams := make([]string, 0)
  709. insertData := make([]interface{}, 0)
  710. for _, disease := range diseases {
  711. insertParams = append(insertParams, "(?, ?, ?, ?, ?)")
  712. insertData = append(insertData, patient.ID)
  713. insertData = append(insertData, disease)
  714. insertData = append(insertData, 1)
  715. insertData = append(insertData, patient.CreatedTime)
  716. insertData = append(insertData, patient.UpdatedTime)
  717. }
  718. thisSQL += strings.Join(insertParams, ", ")
  719. err = utx.Exec(thisSQL, insertData...).Error
  720. if err != nil {
  721. utx.Rollback()
  722. btx.Rollback()
  723. return
  724. }
  725. }
  726. utx.Commit()
  727. btx.Commit()
  728. return
  729. }
  730. func GetLastPatientData(orgid int64) (models.Patients, error) {
  731. patients := models.Patients{}
  732. err := XTReadDB().Model(&patients).Where("user_org_id = ? and status = 1", orgid).Last(&patients).Error
  733. return patients, err
  734. }
  735. func CreatePatientsNew(patientsNew *models.XtPatientsNew) error {
  736. err := XTWriteDB().Model(&patientsNew).Create(&patientsNew).Error
  737. return err
  738. }
  739. func CreateAllergic(allergic models.XtPatientAllergic) error {
  740. err := XTWriteDB().Create(&allergic).Error
  741. return err
  742. }
  743. func EditPatientLapseto(patient *models.Patients, lapseto *models.PatientLapseto) (err error) {
  744. utx := writeDb.Begin()
  745. 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
  746. //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
  747. if err != nil {
  748. utx.Rollback()
  749. return
  750. }
  751. err = utx.Create(lapseto).Error
  752. if err != nil {
  753. utx.Rollback()
  754. return
  755. }
  756. // 删除排班和排班模板信息
  757. if lapseto.LapsetoType == 2 || lapseto.LapsetoType == 3 {
  758. now := time.Now()
  759. deleteScheduleErr := utx.Model(&models.PatientSchedule{}).Where("patient_id = ? AND schedule_date >= ? AND status = 1", patient.ID, lapseto.LapsetoTime).Updates(map[string]interface{}{
  760. "status": 0,
  761. "updated_time": now.Unix(),
  762. }).Error
  763. if deleteScheduleErr != nil {
  764. utx.Rollback()
  765. err = deleteScheduleErr
  766. return
  767. }
  768. deleteSchTempItemErr := utx.Model(&models.PatientScheduleTemplateItem{}).Where("patient_id = ? AND status = 1", patient.ID).Updates(map[string]interface{}{
  769. "status": 0,
  770. "mtime": now.Unix(),
  771. }).Error
  772. if deleteSchTempItemErr != nil {
  773. utx.Rollback()
  774. err = deleteSchTempItemErr
  775. return
  776. }
  777. }
  778. utx.Commit()
  779. return
  780. }
  781. func UpdatePatient(patient *models.Patients, contagions []int64, diseases []int64) (err error) {
  782. // if len(contagions) > 0 || len(diseases) > 0 {
  783. utx := writeDb.Begin()
  784. err = utx.Save(patient).Error
  785. if err != nil {
  786. utx.Rollback()
  787. return
  788. }
  789. err = utx.Model(&models.InfectiousDiseases{}).Where("patient_id=?", patient.ID).Update(map[string]interface{}{"Status": 2, "UpdatedTime": patient.UpdatedTime}).Error
  790. fmt.Println("err", err)
  791. if err != nil {
  792. utx.Rollback()
  793. return
  794. }
  795. if len(contagions) > 0 && patient.IsInfectious == 2 {
  796. thisSQL := "INSERT INTO xt_patients_infectious_diseases (patient_id, disease_id, status, created_time, updated_time) VALUES "
  797. insertParams := make([]string, 0)
  798. insertData := make([]interface{}, 0)
  799. for _, contagion := range contagions {
  800. insertParams = append(insertParams, "(?, ?, ?, ?, ?)")
  801. insertData = append(insertData, patient.ID)
  802. insertData = append(insertData, contagion)
  803. insertData = append(insertData, 1)
  804. insertData = append(insertData, time.Now().Unix())
  805. insertData = append(insertData, time.Now().Unix())
  806. }
  807. thisSQL += strings.Join(insertParams, ", ")
  808. err = utx.Exec(thisSQL, insertData...).Error
  809. if err != nil {
  810. utx.Rollback()
  811. return
  812. }
  813. }
  814. err = utx.Model(&models.ChronicDiseases{}).Where("patient_id=?", patient.ID).Update(map[string]interface{}{"Status": 2, "UpdatedTime": patient.UpdatedTime}).Error
  815. if err != nil {
  816. utx.Rollback()
  817. return
  818. }
  819. if len(diseases) > 0 {
  820. thisSQL := "INSERT INTO xt_patients_chronic_diseases (patient_id, disease_id, status, created_time, updated_time) VALUES "
  821. insertParams := make([]string, 0)
  822. insertData := make([]interface{}, 0)
  823. for _, disease := range diseases {
  824. insertParams = append(insertParams, "(?, ?, ?, ?, ?)")
  825. insertData = append(insertData, patient.ID)
  826. insertData = append(insertData, disease)
  827. insertData = append(insertData, 1)
  828. insertData = append(insertData, time.Now().Unix())
  829. insertData = append(insertData, time.Now().Unix())
  830. }
  831. thisSQL += strings.Join(insertParams, ", ")
  832. err = utx.Exec(thisSQL, insertData...).Error
  833. if err != nil {
  834. utx.Rollback()
  835. return
  836. }
  837. }
  838. utx.Commit()
  839. // } else {
  840. // err = writeDb.Save(patient).Error
  841. // }
  842. return
  843. }
  844. func UpdatePatientLapseto(patientid int64, lapseto models.PatientLapseto) error {
  845. 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
  846. return err
  847. }
  848. func UpdatepatientTwo(patientsNew *models.XtPatientsNew, id int64) error {
  849. 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,
  850. "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, "print_date": patientsNew.PrintDate, "patient_type:": patientsNew.PatientType}).Error
  851. return err
  852. }
  853. func GetLastInfectionRecord(id int64, org_id int64, project_id int64, date int64) (inspection models.Inspection, err error) {
  854. 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
  855. return
  856. }
  857. func GetAllInfectionRecord(date int64, org_id int64, patient_id int64, project_id int64) (inspection []*models.Inspection, err error) {
  858. 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
  859. return
  860. }
  861. func GetPatientDiseases(id int64) []int64 {
  862. var dis []models.ChronicDiseases
  863. ids := make([]int64, 0)
  864. err := readDb.Model(&models.ChronicDiseases{}).Where("patient_id=? and status=1", id).Find(&dis).Error
  865. if err != nil || len(dis) == 0 {
  866. return ids
  867. }
  868. for _, item := range dis {
  869. ids = append(ids, item.DiseaseId)
  870. }
  871. return ids
  872. }
  873. func GetPatientContagions(id int64) []int64 {
  874. var cis []models.InfectiousDiseases
  875. ids := make([]int64, 0)
  876. err := readDb.Model(&models.InfectiousDiseases{}).Where("patient_id=? and status=1", id).Find(&cis).Error
  877. if err != nil || len(cis) == 0 {
  878. return ids
  879. }
  880. for _, item := range cis {
  881. ids = append(ids, item.DiseaseId)
  882. }
  883. return ids
  884. }
  885. func FindPatientDialysisSolutionByMode(orgID int64, patientID, modeId int64) (solution models.DialysisSolution, err error) {
  886. 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
  887. return
  888. }
  889. func FindPatientDialysisSolution(orgID int64, id int64) (solution models.DialysisSolution, err error) {
  890. err = readDb.Model(&models.DialysisSolution{}).Where("id = ? and status=1 and user_org_id=?", id, orgID).First(&solution).Error
  891. return
  892. }
  893. func FindPatientDialysisSolutionChild(orgID int64, id int64) (solution models.DialysisSolution, err error) {
  894. err = readDb.Model(&models.DialysisSolution{}).Where("parent_id = ? and status=1 and user_org_id=?", id, orgID).First(&solution).Error
  895. return
  896. }
  897. func CreatePatientDialysisSolution(solution *models.DialysisSolution) (err error) {
  898. err = writeDb.Create(solution).Error
  899. return
  900. }
  901. func UpdatePatientDialysisSolution(solution *models.DialysisSolution) (err error) {
  902. err = writeDb.Save(solution).Error
  903. return
  904. }
  905. // GetPatientDialysisSolutionList 返回患者透析方案的列表
  906. func GetPatientDialysisSolutionList(orgID int64, patientID int64, page, limit int64) (solutions []*models.DialysisSolution, total int64, err error) {
  907. offset := (page - 1) * limit
  908. db := readDb.Table("xt_dialysis_solution as ds").Where("ds.status=1")
  909. if orgID > 0 {
  910. db = db.Where("ds.user_org_id=?", orgID)
  911. }
  912. if patientID > 0 {
  913. db = db.Where("ds.patient_id=?", patientID)
  914. }
  915. db = db.Count(&total).Offset(offset).Limit(limit)
  916. err = db.Order("id desc").Find(&solutions).Error
  917. if err != nil {
  918. return
  919. }
  920. if len(solutions) > 0 {
  921. nilNameIds := make([]int64, 0)
  922. for _, solution := range solutions {
  923. if len(solution.ModeName) == 0 {
  924. nilNameIds = append(nilNameIds, solution.ModeId)
  925. }
  926. }
  927. if len(nilNameIds) > 0 {
  928. var modes []*models.TreatmentMode
  929. err = readDb.Model(&models.TreatmentMode{}).Where("id IN (?)", nilNameIds).Find(&modes).Error
  930. if err != nil {
  931. return
  932. }
  933. modesMap := make(map[int64]models.TreatmentMode, 0)
  934. for _, mode := range modes {
  935. modesMap[mode.ID] = *mode
  936. }
  937. for index, solution := range solutions {
  938. if _, exixt := modesMap[solution.ModeId]; exixt && len(solution.ModeName) == 0 {
  939. solutions[index].ModeName = modesMap[solution.ModeId].Name
  940. }
  941. }
  942. }
  943. }
  944. return
  945. }
  946. // GetPatientDryWeightAdjustList 返回患者调整干体重的列表
  947. func GetPatientDryWeightAdjustList(orgID int64, patientID int64, page int64, limit int64) (weights []*models.DryWeightAdjust, total int64, err error) {
  948. db := readDb.Table("xt_dry_weight_adjust as dwa").Where("dwa.status=1")
  949. if orgID > 0 {
  950. db = db.Where("dwa.user_org_id=?", orgID)
  951. }
  952. if patientID > 0 {
  953. db = db.Where("dwa.patient_id=?", patientID)
  954. }
  955. 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")
  956. if page > 0 && limit > 0 {
  957. offset := (page - 1) * limit
  958. db = db.Offset(offset).Limit(limit)
  959. }
  960. err = db.Find(&weights).Error
  961. return
  962. }
  963. func CreateDryWeightAdjust(m *models.DryWeightAdjust) (err error) {
  964. err = writeDb.Create(m).Error
  965. return
  966. }
  967. func FindPatientLastDryWeightAdjust(orgID int64, id int64) (weight models.DryWeightAdjust, err error) {
  968. err = readDb.Model(&models.DryWeightAdjust{}).Where("user_org_id=? and patient_id=? and status=1", orgID, id).Order("id desc").First(&weight).Error
  969. return
  970. }
  971. func UpdateDoctorAdviceById(id int64, prescribing_number float64, single_dose float64) error {
  972. err := XTWriteDB().Model(&models.DoctorAdvice{}).Where("id=? and status =1", id).Updates(map[string]interface{}{"prescribing_number": prescribing_number, "single_dose": single_dose}).Error
  973. return err
  974. }
  975. func CreateDoctorAdvice(m *models.DoctorAdvice) (err error) {
  976. return writeDb.Create(m).Error
  977. }
  978. func GetMaxAdviceGroupID(orgId int64) (group int64) {
  979. var advice models.DoctorAdvice
  980. err := readDb.Table("xt_doctor_advice").Where("user_org_id=?", orgId).Select("max(groupno) as groupno").First(&advice).Error
  981. if err != nil {
  982. fmt.Println(err)
  983. group = 0
  984. }
  985. group = advice.GroupNo
  986. return
  987. }
  988. func CreateGroupAdvice(orgId int64, group int64, advices []*models.GroupAdvice) (err error) {
  989. if group == 0 {
  990. group = GetMaxAdviceGroupID(orgId) + 1
  991. }
  992. tx := writeDb.Begin()
  993. defer func() {
  994. if r := recover(); r != nil {
  995. tx.Rollback()
  996. }
  997. }()
  998. for _, advice := range advices {
  999. advice.GroupNo = group
  1000. if err = tx.Create(advice).Error; err != nil {
  1001. tx.Rollback()
  1002. return
  1003. }
  1004. }
  1005. tx.Commit()
  1006. return
  1007. }
  1008. func CreateMGroupAdvice(orgId int64, advices []*models.GroupAdvice, groupNo int64) (list []*models.GroupAdvice, err error) {
  1009. if groupNo <= 0 {
  1010. group := GetMaxAdviceGroupID(orgId)
  1011. groupNo = group + 1
  1012. }
  1013. tx := writeDb.Begin()
  1014. defer func() {
  1015. if r := recover(); r != nil {
  1016. tx.Rollback()
  1017. }
  1018. }()
  1019. for _, advice := range advices {
  1020. advice.GroupNo = groupNo
  1021. if err = tx.Create(advice).Error; err != nil {
  1022. tx.Rollback()
  1023. return
  1024. }
  1025. list = append(list, advice)
  1026. if len(advice.Children) > 0 {
  1027. for _, child := range advice.Children {
  1028. child.GroupNo = groupNo
  1029. child.ParentId = advice.ID
  1030. fmt.Println(child)
  1031. if err = tx.Create(&child).Error; err != nil {
  1032. tx.Rollback()
  1033. return
  1034. }
  1035. list = append(list, child)
  1036. }
  1037. }
  1038. }
  1039. tx.Commit()
  1040. return
  1041. }
  1042. func CreateMGroupAdviceOne(orgId int64, advices []*models.GroupAdvice, groupNo int64) (list []*models.GroupAdvice, err error) {
  1043. if groupNo <= 0 {
  1044. group := GetMaxAdviceGroupID(orgId)
  1045. groupNo = group + 1
  1046. }
  1047. tx := writeDb.Begin()
  1048. defer func() {
  1049. if r := recover(); r != nil {
  1050. tx.Rollback()
  1051. }
  1052. }()
  1053. for _, advice := range advices {
  1054. advice.GroupNo = advice.GroupNo
  1055. if err = tx.Create(advice).Error; err != nil {
  1056. tx.Rollback()
  1057. return
  1058. }
  1059. list = append(list, advice)
  1060. if len(advice.Children) > 0 {
  1061. for _, child := range advice.Children {
  1062. child.GroupNo = advice.GroupNo
  1063. child.ParentId = advice.ID
  1064. fmt.Println(child)
  1065. if err = tx.Create(&child).Error; err != nil {
  1066. tx.Rollback()
  1067. return
  1068. }
  1069. list = append(list, child)
  1070. }
  1071. }
  1072. }
  1073. tx.Commit()
  1074. return
  1075. }
  1076. func UpdateDoctorAdvice(m *models.DoctorAdvice) (err error) {
  1077. return writeDb.Save(m).Error
  1078. }
  1079. func StopGroupAdvice(orgId int64, groupNo int64, m *models.DoctorAdvice) (err error) {
  1080. 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
  1081. if err != nil {
  1082. return
  1083. }
  1084. return
  1085. }
  1086. func StopDoctorAdvice(m *models.DoctorAdvice) (err error) {
  1087. ut := writeDb.Begin()
  1088. err = ut.Save(m).Error
  1089. if err != nil {
  1090. ut.Rollback()
  1091. return
  1092. }
  1093. 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
  1094. if err != nil {
  1095. ut.Rollback()
  1096. return
  1097. }
  1098. ut.Commit()
  1099. return
  1100. }
  1101. func DeleteSolution(m *models.DialysisSolution) (err error) {
  1102. if m.ParentId > 0 {
  1103. return writeDb.Save(m).Error
  1104. } else {
  1105. ut := writeDb.Begin()
  1106. err = ut.Save(m).Error
  1107. if err != nil {
  1108. ut.Rollback()
  1109. return
  1110. }
  1111. err = ut.Model(&models.DialysisSolution{}).Where("parent_id=?", m.ID).Update(map[string]interface{}{"UpdatedTime": m.UpdatedTime, "Status": 0}).Error
  1112. if err != nil {
  1113. ut.Rollback()
  1114. return
  1115. }
  1116. ut.Commit()
  1117. }
  1118. return
  1119. }
  1120. func DeleteDoctorAdvice(m *models.DoctorAdvice) (err error) {
  1121. if m.ParentId > 0 {
  1122. return writeDb.Save(m).Error
  1123. } else {
  1124. ut := writeDb.Begin()
  1125. err = ut.Save(m).Error
  1126. if err != nil {
  1127. ut.Rollback()
  1128. return
  1129. }
  1130. 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
  1131. if err != nil {
  1132. ut.Rollback()
  1133. return
  1134. }
  1135. ut.Commit()
  1136. }
  1137. return
  1138. }
  1139. func DeleteGroupAdvice(orgId int64, groupNo int64, admin_user_id int64) (err error) {
  1140. 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
  1141. if err != nil {
  1142. return
  1143. }
  1144. return
  1145. }
  1146. func DeleteGroupAdviceOne(orgId int64, groupNo int64, admin_user_id int64) (err error) {
  1147. err = writeDb.Model(&models.DoctorAdvice{}).Where("user_org_id = ? and groupno = ? and status =1 and advice_type=1", orgId, groupNo).Update(map[string]interface{}{"UpdatedTime": time.Now().Unix(), "Status": 0, "Modifier": admin_user_id}).Error
  1148. if err != nil {
  1149. return
  1150. }
  1151. return
  1152. }
  1153. func FindDoctorAdvice(orgID, id int64) (advice models.DoctorAdvice, err error) {
  1154. err = readDb.Model(&models.DoctorAdvice{}).Where("id = ? and user_org_id=? and status = 1", id, orgID).First(&advice).Error
  1155. return
  1156. }
  1157. func FindDoctorAdviceOne(orgID, patient_id int64) (advice models.DoctorAdvice, err error) {
  1158. err = readDb.Model(&models.DoctorAdvice{}).Where("patient_id = ? and user_org_id=? and status = 1", patient_id, orgID).First(&advice).Error
  1159. return
  1160. }
  1161. func FindDoctorAdviceTwo(ids []string, orgID int64) (advice []*models.DoctorAdvice, err error) {
  1162. err = readDb.Model(&models.DoctorAdvice{}).Where("id in(?) and user_org_id=? and status = 1", ids, orgID).First(&advice).Error
  1163. return
  1164. }
  1165. func FindHisDoctorAdvice(orgID, id int64) (advice models.HisDoctorAdviceInfo, err error) {
  1166. err = readDb.Model(&models.HisDoctorAdviceInfo{}).Where("id = ? and user_org_id=? and status = 1", id, orgID).First(&advice).Error
  1167. return
  1168. }
  1169. func FindHisDoctorAdviceTwo(ids []string, orgID int64) (advice []*models.HisDoctorAdviceInfo, err error) {
  1170. err = readDb.Model(&models.HisDoctorAdviceInfo{}).Where("id in(?) and user_org_id=? and status = 1", ids, orgID).First(&advice).Error
  1171. return
  1172. }
  1173. func FindHisDoctorAdviceOne(orgID, patient_id int64) (advice models.HisDoctorAdviceInfo, err error) {
  1174. err = readDb.Model(&models.HisDoctorAdviceInfo{}).Where("patient_id = ? and user_org_id=? and status = 1", patient_id, orgID).First(&advice).Error
  1175. return
  1176. }
  1177. func FindDoctorAdviceByGroupNo(orgID, groupNo int64) (advice models.DoctorAdvice, err error) {
  1178. err = readDb.Model(&models.DoctorAdvice{}).Where("groupno = ? and user_org_id=? and status = 1", groupNo, orgID).First(&advice).Error
  1179. return
  1180. }
  1181. func GetDoctorAdviceList(orgID, patientID, advice_type, stop, start, end int64, keywords string) (advices []*models.DoctorAdvices, total int64, err error) {
  1182. db := readDb.Table("xt_doctor_advice as x").Where("x.status = 1")
  1183. table := UserReadDB().Table("sgj_user_admin_role as r")
  1184. fmt.Print("table", table)
  1185. if orgID > 0 {
  1186. db = db.Where("x.user_org_id=?", orgID)
  1187. }
  1188. if patientID > 0 {
  1189. db = db.Where("x.patient_id = ?", patientID)
  1190. }
  1191. if advice_type > 0 {
  1192. db = db.Where("x.advice_type = ?", advice_type)
  1193. } else if advice_type == 0 {
  1194. db = db.Where("x.advice_type in (?)", []int{1, 3})
  1195. }
  1196. if stop == 1 {
  1197. db = db.Where("(x.stop_state=? or x.execution_state=?)", stop, stop)
  1198. } else if stop == 2 {
  1199. db = db.Where("x.stop_state=? and x.execution_state=?", stop, stop)
  1200. }
  1201. if start != 0 {
  1202. db = db.Where("x.start_time>=?", start)
  1203. }
  1204. if end != 0 {
  1205. db = db.Where("start_time<=?", end)
  1206. }
  1207. if len(keywords) > 0 {
  1208. likeKey := "%" + keywords + "%"
  1209. db = db.Where("x.advice_name LIKE ?", likeKey)
  1210. }
  1211. 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
  1212. fmt.Print("err", err)
  1213. return
  1214. }
  1215. func GetLongDoctorAdviceList(orgID, patientID, advice_type, stop, start, end int64, keywords string, page int64, limit int64) (advices []*models.DoctorAdvices, total int64, err error) {
  1216. db := readDb.Table("xt_doctor_advice as x").Where("x.status = 1")
  1217. table := UserReadDB().Table("sgj_user_admin_role as r")
  1218. fmt.Print("table", table)
  1219. if orgID > 0 {
  1220. db = db.Where("x.user_org_id=?", orgID)
  1221. }
  1222. if patientID > 0 {
  1223. db = db.Where("x.patient_id = ?", patientID)
  1224. }
  1225. if advice_type == 1 && advice_type > 0 {
  1226. db = db.Where("x.advice_type = ?", advice_type)
  1227. }
  1228. if advice_type == 3 && advice_type > 0 {
  1229. db = db.Where("x.advice_type = 2 or x.advice_type = 3")
  1230. }
  1231. if advice_type == 4 && advice_type > 0 {
  1232. db = db.Where("x.advice_type = 4")
  1233. }
  1234. if stop == 1 {
  1235. db = db.Where("(x.stop_state=? or x.execution_state=?)", stop, stop)
  1236. } else if stop == 2 {
  1237. db = db.Where("x.stop_state=? and x.execution_state=?", stop, stop)
  1238. }
  1239. if start != 0 {
  1240. db = db.Where("x.start_time>=?", start)
  1241. }
  1242. if end != 0 {
  1243. db = db.Where("x.start_time<=?", end)
  1244. }
  1245. if len(keywords) > 0 {
  1246. likeKey := "%" + keywords + "%"
  1247. db = db.Where("x.advice_name LIKE ?", likeKey)
  1248. }
  1249. offset := (page - 1) * limit
  1250. 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
  1251. fmt.Print("err", err)
  1252. return
  1253. }
  1254. func GetDoctorAdviceListOne(orgID, patientID, advice_type, stop, start, end int64, keywords string, page int64, limit int64, delive_way_one string) (advices []*models.DoctorAdvices, total int64, err error) {
  1255. db := readDb.Table("xt_doctor_advice as x").Where("x.status = 1")
  1256. table := UserReadDB().Table("sgj_user_admin_role as r")
  1257. fmt.Print("table", table)
  1258. if len(delive_way_one) > 0 {
  1259. db = db.Where("delivery_way=?", delive_way_one)
  1260. }
  1261. if orgID > 0 {
  1262. db = db.Where("x.user_org_id=?", orgID)
  1263. }
  1264. if patientID > 0 {
  1265. db = db.Where("x.patient_id = ?", patientID)
  1266. }
  1267. if advice_type == 1 && advice_type > 0 {
  1268. db = db.Where("x.advice_type = ?", advice_type)
  1269. }
  1270. if advice_type == 2 && advice_type > 0 {
  1271. db = db.Where("x.advice_type = ?", advice_type)
  1272. }
  1273. if advice_type == 3 && advice_type > 0 {
  1274. db = db.Where("x.advice_type = 2 or x.advice_type = 3")
  1275. }
  1276. if advice_type == 4 && advice_type > 0 {
  1277. db = db.Where("x.advice_type = 4")
  1278. }
  1279. if stop == 1 {
  1280. db = db.Where("(x.stop_state=? or x.execution_state=?)", stop, stop)
  1281. } else if stop == 2 {
  1282. db = db.Where("x.stop_state=? and x.execution_state=?", stop, stop)
  1283. }
  1284. if start != 0 {
  1285. db = db.Where("x.start_time>=?", start)
  1286. }
  1287. if end != 0 {
  1288. db = db.Where("x.start_time<=?", end)
  1289. }
  1290. if len(keywords) > 0 {
  1291. likeKey := "%" + keywords + "%"
  1292. db = db.Where("x.advice_name LIKE ?", likeKey)
  1293. }
  1294. offset := (page - 1) * limit
  1295. 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
  1296. fmt.Print("err", err)
  1297. return
  1298. }
  1299. func GetDoctorAdviceListTwo(orgID int64, patientID int64, advice_type int64, stop int64, start int64, end int64, keywords string, limit int64, page int64, delive_way_one string) (advices []*models.DoctorAdvices, total int64, err error) {
  1300. db := readDb.Table("xt_doctor_advice as x").Where("x.status = 1")
  1301. table := UserReadDB().Table("sgj_user_admin_role as r")
  1302. fmt.Println(table)
  1303. if len(delive_way_one) > 0 {
  1304. db = db.Where("delivery_way=?", delive_way_one)
  1305. }
  1306. if orgID > 0 {
  1307. db = db.Where("x.user_org_id=?", orgID)
  1308. }
  1309. if patientID > 0 {
  1310. db = db.Where("x.patient_id = ?", patientID)
  1311. }
  1312. if advice_type == 1 {
  1313. db = db.Where("x.advice_type = ?", advice_type)
  1314. }
  1315. if advice_type == 3 && advice_type > 0 {
  1316. db = db.Where("x.advice_type = 2 or x.advice_type = 3")
  1317. }
  1318. if advice_type == 4 && advice_type > 0 {
  1319. db = db.Where("x.advice_type = 4")
  1320. }
  1321. if stop == 1 {
  1322. db = db.Where("(x.stop_state=? or x.execution_state=?)", stop, stop)
  1323. } else if stop == 2 {
  1324. db = db.Where("x.stop_state=? and x.execution_state=?", stop, stop)
  1325. }
  1326. if start != 0 {
  1327. db = db.Where("x.start_time>=?", start)
  1328. }
  1329. if end != 0 {
  1330. db = db.Where("x.start_time<=?", end)
  1331. }
  1332. if len(keywords) > 0 {
  1333. likeKey := "%" + keywords + "%"
  1334. db = db.Where("x.advice_name LIKE ?", likeKey)
  1335. }
  1336. offset := (page - 1) * limit
  1337. 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
  1338. fmt.Print("错误是什么", err)
  1339. return
  1340. }
  1341. func GetDoctorAdvicePageList(orgID, patientID, advice_type, stop, start, end int64, keywords string, page, limit int64) (advices []*models.DoctorAdvice, total int64, err error) {
  1342. offset := (page - 1) * limit
  1343. db := readDb.Model(&models.DoctorAdvice{}).Where("status=1")
  1344. if orgID > 0 {
  1345. db = db.Where("user_org_id=?", orgID)
  1346. }
  1347. if patientID > 0 {
  1348. db = db.Where("patient_id = ?", patientID)
  1349. }
  1350. if advice_type > 0 {
  1351. db = db.Where("advice_type = ?", advice_type)
  1352. }
  1353. if stop == 1 {
  1354. db = db.Where("(stop_state=? or execution_state=?) and parent_id=0", stop, stop)
  1355. } else if stop == 2 {
  1356. db = db.Where("stop_state=? and execution_state=?", stop, stop)
  1357. }
  1358. if start != 0 {
  1359. db = db.Where("start_time>=?", start)
  1360. }
  1361. if end != 0 {
  1362. db = db.Where("start_time<=?", end)
  1363. }
  1364. if len(keywords) > 0 {
  1365. likeKey := "%" + keywords + "%"
  1366. db = db.Where("advice_name LIKE ?", likeKey)
  1367. }
  1368. 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
  1369. return
  1370. }
  1371. func CreateSubDoctorAdvice(advices []*models.DoctorAdvice) (err error) {
  1372. if len(advices) > 0 {
  1373. utx := writeDb.Begin()
  1374. if len(advices) > 0 {
  1375. 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 "
  1376. insertParams := make([]string, 0)
  1377. insertData := make([]interface{}, 0)
  1378. for _, advice := range advices {
  1379. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1380. insertData = append(insertData, advice.SingleDoseUnit)
  1381. insertData = append(insertData, advice.PrescribingNumber)
  1382. insertData = append(insertData, advice.PrescribingNumberUnit)
  1383. insertData = append(insertData, advice.AdviceName)
  1384. insertData = append(insertData, advice.AdviceDesc)
  1385. insertData = append(insertData, advice.SingleDose)
  1386. insertData = append(insertData, advice.CreatedTime)
  1387. insertData = append(insertData, advice.UpdatedTime)
  1388. insertData = append(insertData, advice.PatientId)
  1389. insertData = append(insertData, advice.ParentId)
  1390. insertData = append(insertData, advice.UserOrgId)
  1391. insertData = append(insertData, advice.RecordDate)
  1392. insertData = append(insertData, 2)
  1393. insertData = append(insertData, advice.DrugSpecUnit)
  1394. }
  1395. thisSQL += strings.Join(insertParams, ", ")
  1396. err = utx.Exec(thisSQL, insertData...).Error
  1397. if err != nil {
  1398. utx.Rollback()
  1399. return
  1400. }
  1401. }
  1402. utx.Commit()
  1403. }
  1404. return
  1405. }
  1406. func GetPatientDialysisRecord(orgID, patientID int64, page, limit, start, end, mode_id int64) ([]*models.PatientDialysisRecord, int64, error) {
  1407. offset := (page - 1) * limit
  1408. var total int64
  1409. var err error
  1410. var orders []*models.PatientDialysisRecord
  1411. // err = readDb.Table("xt_dialysis_order as do").
  1412. // Preload("DialysisPrescription", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  1413. // Preload("PredialysisEvaluation", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  1414. // Preload("AssessmentAfterDislysis", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  1415. // Preload("TreatmentSummary", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  1416. // 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).
  1417. // Joins("JOIN xt_device_zone as dz ON dz.org_id = ? and dz.id=s.partition_id", orgID).
  1418. // 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
  1419. db := readDb.Table("xt_dialysis_order as do").
  1420. Preload("DoctorAdvice", "patient_id =? and user_org_id = ? and status =1 and advice_type =2", patientID, orgID).
  1421. Preload("DialysisPrescription", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  1422. Preload("PredialysisEvaluation", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  1423. Preload("DialysisPrescription", func(db *gorm.DB) *gorm.DB {
  1424. return readDb.Where("patient_id=? and user_org_id=? and status=1", patientID, orgID).Preload("UserAdminRole", func(db *gorm.DB) *gorm.DB {
  1425. return readUserDb.Where("status = 1 and org_id=?", orgID)
  1426. })
  1427. }).
  1428. Preload("AssessmentAfterDislysis", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  1429. Preload("TreatmentSummary", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  1430. Preload("Device", "org_id=? and status=1", orgID).
  1431. Preload("UserAdminRole", func(db *gorm.DB) *gorm.DB {
  1432. return readUserDb.Where("org_id=?", orgID)
  1433. }).
  1434. 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).
  1435. Joins("JOIN xt_device_zone as dz ON dz.org_id = ? and dz.id=s.partition_id", orgID).
  1436. Where("do.patient_id=? and do.user_org_id=? and do.status=1 and s.status=1", patientID, orgID).Group("s.schedule_date")
  1437. if start != 0 {
  1438. db = db.Where("do.dialysis_date>=?", start)
  1439. }
  1440. if end != 0 {
  1441. db = db.Where("do.dialysis_date<=?", end)
  1442. }
  1443. if mode_id > 0 {
  1444. 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")
  1445. db = db.Where("dp.mode_id=?", mode_id)
  1446. }
  1447. 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 ,do.start_time,do.end_time,do.url,s.schedule_type, s.partition_id, dz.name as partition_name").Find(&orders).Error
  1448. if len(orders) > 0 {
  1449. ids := make([]int64, 0)
  1450. for _, order := range orders {
  1451. dialyzer := order.DialysisPrescription.Dialyzer
  1452. ids = append(ids, dialyzer)
  1453. }
  1454. if len(ids) > 0 {
  1455. var dialyzers []*models.DeviceNumber
  1456. err = readDb.Model(&models.DeviceNumber{}).Where("id IN (?) and org_id=? and status=1", ids, orgID).Find(&dialyzers).Error
  1457. if err != nil {
  1458. return nil, 0, err
  1459. }
  1460. dialyzerMap := make(map[int64]models.DeviceNumber, 0)
  1461. for _, item := range dialyzers {
  1462. dialyzerMap[item.ID] = *item
  1463. }
  1464. for orderIndex, order := range orders {
  1465. if _, exist := dialyzerMap[order.DialysisPrescription.Dialyzer]; exist {
  1466. orders[orderIndex].DeviceNumber = dialyzerMap[order.DialysisPrescription.Dialyzer].Number
  1467. }
  1468. }
  1469. }
  1470. }
  1471. return orders, total, err
  1472. }
  1473. func GetPatientTreatmentSummaryList(orgID, patientID, page, limit, start, end int64) (list []*models.TreatmentSummary, total int, err error) {
  1474. offset := (page - 1) * limit
  1475. fmt.Println(offset)
  1476. fmt.Println(limit)
  1477. db := readDb.Model(&models.TreatmentSummary{}).Where("user_org_id = ? and patient_id=?", orgID, patientID)
  1478. if start != 0 {
  1479. db = db.Where("assessment_date >= ?", start)
  1480. }
  1481. if end != 0 {
  1482. db = db.Where("assessment_date <= ?", end)
  1483. }
  1484. db = db.Where("status=1")
  1485. err = db.Count(&total).Offset(offset).Limit(limit).Order("assessment_date desc, id desc").Find(&list).Error
  1486. return
  1487. }
  1488. func GetPatientScheduleList(orgID int64, patientID int64, page int64, limit int64, start int64) (schedules []*models.PatientSchedule, err error) {
  1489. offset := (page - 1) * limit
  1490. err = readDb.Table("xt_schedule as s").
  1491. Preload("DeviceZone", "org_id=? and status=1", orgID).
  1492. Preload("DeviceNumber", "org_id=? and status=1", orgID).
  1493. Preload("TreatmentMode", "status=1").
  1494. Where("s.patient_id =? and s.user_org_id=? and s.schedule_date>=? and s.status=1", patientID, orgID, start).
  1495. Limit(limit).Offset(offset).Find(&schedules).Error
  1496. return
  1497. }
  1498. func GetMonitorRecord(orgID int64, date int64, partition int64, patientId int64) ([]*models.NewVMMonitorDialysisSchedule, error) {
  1499. var mds []*models.NewVMMonitorDialysisSchedule
  1500. db := readDb.Model(&models.NewVMMonitorDialysisSchedule{})
  1501. if date > 0 {
  1502. db = db.Where("schedule_date = ?", date)
  1503. }
  1504. if partition > 0 {
  1505. db = db.Where("partition_id = ?", partition)
  1506. }
  1507. if patientId > 0 {
  1508. db = db.Where("patient_id = ?", patientId)
  1509. }
  1510. err = db.Where("status = 1 and user_org_id = ?", orgID).Find(&mds).Error
  1511. return mds, err
  1512. }
  1513. func MobileGetMonitorsWithPatient(orgID int64, keyword string, page int) ([]*models.MonitorDialysisSchedule, error) {
  1514. var patients []*models.Patients
  1515. 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
  1516. if getPatientErr != nil {
  1517. return nil, getPatientErr
  1518. }
  1519. patientIDs := make([]int64, len(patients))
  1520. for index, patient := range patients {
  1521. patientIDs[index] = patient.ID
  1522. }
  1523. db := readDb.
  1524. Model(&models.MonitorDialysisSchedule{}).
  1525. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  1526. Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  1527. Preload("TreatmentMode", "status = 1").
  1528. Preload("Prescription", "status = 1 AND user_org_id = ?", orgID).
  1529. Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ?", orgID).
  1530. Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ?", orgID).
  1531. Preload("MonitoringRecord", "status = 1 AND user_org_id = ?", orgID).
  1532. Preload("DialysisOrder", "status = 1 AND user_org_id = ?", orgID).
  1533. Preload("MonitorPatients", "status = 1 AND user_org_id = ?", orgID).
  1534. Where("status = 1 AND user_org_id = ? AND patient_id in (?)", orgID, patientIDs)
  1535. var schedules []*models.MonitorDialysisSchedule
  1536. err := db.Offset(20 * (page - 1)).Limit(20).Order("schedule_date desc").Find(&schedules).Error
  1537. return schedules, err
  1538. }
  1539. func GetPatientByKeyWord(orgID int64, keywords string) (patient []*models.Patients, err error) {
  1540. db := readDb.Model(&models.Patients{}).Where("user_org_id=? and status=1 and lapseto = 1", orgID)
  1541. if len(keywords) > 0 {
  1542. likekey := "%" + keywords + "%"
  1543. err = db.Where("name LIKE ? OR dialysis_no LIKE ? OR first_letter LIKE ?", likekey, likekey, likekey).Find(&patient).Error
  1544. } else {
  1545. err = db.Find(&patient).Error
  1546. }
  1547. return
  1548. }
  1549. func FindDoctorAdviceByGoroupNo(orgID int64, groupno int64) (advice models.DoctorAdvice, err error) {
  1550. err = readDb.Model(&models.DoctorAdvice{}).Where("user_org_id=? AND groupno = ? AND status = 1", orgID, groupno).First(&advice).Error
  1551. return
  1552. }
  1553. func FindOldDoctorAdvice(orgID int64, advice_id int64) (advice models.DoctorAdvice, err error) {
  1554. err = readDb.Model(&models.DoctorAdvice{}).Where("id = ? AND user_org_id=? AND status = 1", advice_id, orgID).First(&advice).Error
  1555. return
  1556. }
  1557. func UpdateAdviceGroupStartTime(orgID int64, groupNO int64, startTime int64, admin_user_id int64) error {
  1558. now := time.Now().Unix()
  1559. 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{}{
  1560. "start_time": startTime,
  1561. "updated_time": now,
  1562. "modifier": admin_user_id,
  1563. }).Error
  1564. return err
  1565. }
  1566. func QueryPatientById(id int64) *models.Patients {
  1567. var pat models.Patients
  1568. err := readDb.Model(&models.Patients{}).Where("id=?", id).First(&pat).Error
  1569. if err != nil {
  1570. }
  1571. return &pat
  1572. }
  1573. func FindAllDoctorAdviceByGoroupNo(orgID int64, groupno int64) (advice []models.DoctorAdvice, err error) {
  1574. err = readDb.Model(&models.DoctorAdvice{}).Where("user_org_id=? AND groupno = ? AND status = 1", orgID, groupno).Find(&advice).Error
  1575. return
  1576. }
  1577. func FindAllDoctorAdviceByGoroupNoOne(orgID int64, groupno int64) (advice []models.DoctorAdvice, err error) {
  1578. err = readDb.Model(&models.DoctorAdvice{}).Where("user_org_id=? AND groupno = ? AND status = 1 and execution_state =2", orgID, groupno).Find(&advice).Error
  1579. return
  1580. }
  1581. func FindAllDoctorAdviceByGoroupNoThree(orgID int64, groupno int64, patient_id int64, advice_date int64) (advice []models.DoctorAdvice, err error) {
  1582. err = readDb.Model(&models.DoctorAdvice{}).Where("user_org_id=? AND groupno = ? AND status = 1 and patient_id =? and advice_date =?", orgID, groupno, patient_id, advice_date).Find(&advice).Error
  1583. return
  1584. }
  1585. func UpdateDoctorAdviceByGoroupNo(id int64, drug_id int64) error {
  1586. 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
  1587. return err
  1588. }
  1589. func FindAllDoctorAdviceByGoroupNoTwety(orgID int64, groupno int64) (advice []models.DoctorAdvice, err error) {
  1590. 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
  1591. return
  1592. }
  1593. func FindDoctorAdviceByIds(orgID int64, ids []string) (advice []models.DoctorAdvice, err error) {
  1594. err = readDb.Model(&models.DoctorAdvice{}).Where("id IN (?) AND user_org_id = ? AND status = 1", ids, orgID).Find(&advice).Error
  1595. return
  1596. }
  1597. func FindDoctorAdviceByIdsOne(orgID int64, ids []string) (advice []models.DoctorAdvice, err error) {
  1598. err = readDb.Model(&models.DoctorAdvice{}).Where("id IN (?) AND user_org_id = ? AND status = 1 and execution_state =2", ids, orgID).Find(&advice).Error
  1599. return
  1600. }
  1601. func FindDoctorAdviceByIdsTwety(orgID int64, ids []string) (advice []models.DoctorAdvice, err error) {
  1602. 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
  1603. return
  1604. }
  1605. func GetBatchDoctorAdviceList(ids []string, user_org_id int64) (advice []*models.DoctorAdvice, err error) {
  1606. err = XTReadDB().Where("id in(?) and user_org_id = ? and status =1", ids, user_org_id).Find(&advice).Error
  1607. return advice, err
  1608. }
  1609. func BatchDeleteDoctorAdvice(ids []string, user_id int64) (err error) {
  1610. ut := writeDb.Begin()
  1611. 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
  1612. if err != nil {
  1613. ut.Rollback()
  1614. return
  1615. }
  1616. 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
  1617. if err != nil {
  1618. ut.Rollback()
  1619. return
  1620. }
  1621. ut.Commit()
  1622. return err
  1623. }
  1624. func FindAdviceByGoroupNo(orgID int64, groupno int64) (advice []models.DoctorAdvice, err error) {
  1625. err = readDb.Model(&models.DoctorAdvice{}).Where("user_org_id=? AND groupno = ? AND status = 1 AND parent_id = 0", orgID, groupno).Find(&advice).Error
  1626. return
  1627. }
  1628. func UpdateDoctorAdviceAndSubAdvice(m *models.DoctorAdvice) (err error) {
  1629. ut := writeDb.Begin()
  1630. err = ut.Save(m).Error
  1631. if err != nil {
  1632. ut.Rollback()
  1633. return
  1634. }
  1635. 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
  1636. if err != nil {
  1637. ut.Rollback()
  1638. return
  1639. }
  1640. ut.Commit()
  1641. return err
  1642. }
  1643. func GetAllWaitRemindPatient(org_id int64, page int64, limit int64) (total int64, patient []*models.Patients, err error) {
  1644. type Total struct {
  1645. Count int64
  1646. }
  1647. var totals Total
  1648. offset := (page - 1) * limit
  1649. 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
  1650. 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)
  1651. return totals.Count, patient, err
  1652. }
  1653. func UpdatePatientRemindStatus(patient_id int64, remind int64, org_id int64) (err error) {
  1654. 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
  1655. return
  1656. }
  1657. func CreatePatientWeightAdjust(m *models.SgjPatientDryweight) (err error) {
  1658. err = writeDb.Create(m).Error
  1659. return
  1660. }
  1661. func GetPatientAssesmentBefor(patient_id int64, assessment_date int64, org_id int64) (models.XtAssessmentBeforeDislysis, error) {
  1662. beforeDislysis := models.XtAssessmentBeforeDislysis{}
  1663. err := XTReadDB().Where("patient_id = ? and assessment_date = ? and user_org_id = ? and status=1", patient_id, assessment_date, org_id).Find(&beforeDislysis).Error
  1664. return beforeDislysis, err
  1665. }
  1666. func GetPatientDialysisPrescription(patient_id int64, record_date int64, org_id int64) (models.XtDialysisPrescription, error) {
  1667. prescription := models.XtDialysisPrescription{}
  1668. err := XTReadDB().Where("patient_id = ? and record_date = ? and user_org_id = ? and status=1", patient_id, record_date, org_id).Find(&prescription).Error
  1669. return prescription, err
  1670. }
  1671. func UpdatePatientAssesmentBefor(id int64, dry_weight float64) (models.XtAssessmentBeforeDislysis, error) {
  1672. beforeDislysis := models.XtAssessmentBeforeDislysis{}
  1673. err := XTWriteDB().Model(&beforeDislysis).Where("id = ? and status=1", id).Update(map[string]interface{}{"dry_weight": dry_weight}).Error
  1674. return beforeDislysis, err
  1675. }
  1676. func UpdatePatientDialysisPrscription(id int64, dewater_amount float64) (models.XtDialysisPrescription, error) {
  1677. prescription := models.XtDialysisPrescription{}
  1678. err := XTWriteDB().Model(&prescription).Where("id = ? and status =1", id).Update(map[string]interface{}{"target_ultrafiltration": dewater_amount, "prescription_water": dewater_amount}).Error
  1679. return prescription, err
  1680. }
  1681. func FindLastDryWeightAdjust(orgID int64, id int64) (weight models.SgjPatientDryweight, err error) {
  1682. err = readDb.Model(&models.SgjPatientDryweight{}).Where("user_org_id=? and patient_id=? and status=1", orgID, id).Order("id desc").First(&weight).Error
  1683. return
  1684. }
  1685. func GetSchedualPatientByKeyWord(orgID int64, keywords string, date int64) (patient []*models.Patients, err error) {
  1686. db := readDb.Model(&models.Patients{}).Where("user_org_id=? and status=1 and lapseto = 1 ", orgID)
  1687. if len(keywords) > 0 {
  1688. likekey := "%" + keywords + "%"
  1689. 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
  1690. } else {
  1691. err = db.Find(&patient).Error
  1692. }
  1693. return
  1694. }
  1695. func GetPatientScheduleOne(patientid int64, nowdate int64, orgid int64) (models.XtSchedule, error) {
  1696. schedule := models.XtSchedule{}
  1697. err := XTReadDB().Model(&schedule).Where("patient_id = ? and schedule_date = ? and user_org_id = ? and status =1", patientid, nowdate, orgid).Find(&schedule).Error
  1698. return schedule, err
  1699. }
  1700. func CreateExportPatient(patient *models.Patients, contagions []int64, org_creator int64) (err error) {
  1701. user, _ := GetSgjUserByMobild(patient.Phone)
  1702. customer, _ := GetSgjCoustomerByMobile(patient.UserOrgId, patient.Phone)
  1703. utx := writeDb.Begin()
  1704. btx := writeUserDb.Begin()
  1705. if user.ID == 0 {
  1706. user.Mobile = patient.Phone
  1707. user.Avatar = patient.Avatar
  1708. user.AvatarThumb = patient.Avatar
  1709. user.Birthday = patient.Birthday
  1710. user.Username = patient.Name
  1711. user.Gender = patient.Gender
  1712. user.Sources = 11
  1713. user.Introduce = patient.Remark
  1714. user.Status = 1
  1715. user.UpdatedTime = patient.UpdatedTime
  1716. user.CreatedTime = patient.CreatedTime
  1717. err = btx.Create(&user).Error
  1718. if err != nil {
  1719. utx.Rollback()
  1720. btx.Rollback()
  1721. return
  1722. }
  1723. }
  1724. patient.UserId = user.ID
  1725. if customer == nil {
  1726. err = btx.Create(&models.SgjCustomer{
  1727. UserOrgId: patient.UserOrgId,
  1728. UserId: user.ID,
  1729. Mobile: patient.Phone,
  1730. Name: patient.Name,
  1731. Gender: patient.Gender,
  1732. Birthday: patient.Birthday,
  1733. Sources: 11,
  1734. Status: 1,
  1735. CreatedTime: patient.CreatedTime,
  1736. UpdatedTime: patient.UpdatedTime,
  1737. Avatar: patient.Avatar,
  1738. Remark: patient.Remark,
  1739. }).Error
  1740. if err != nil {
  1741. utx.Rollback()
  1742. btx.Rollback()
  1743. return
  1744. }
  1745. }
  1746. err = utx.Create(patient).Error
  1747. if err != nil {
  1748. utx.Rollback()
  1749. btx.Rollback()
  1750. return
  1751. }
  1752. if patient.DryWeight > 0 {
  1753. var dryWeight models.SgjPatientDryweight
  1754. dryWeight.PatientId = patient.ID
  1755. dryWeight.UserOrgId = patient.UserOrgId
  1756. dryWeight.Status = 1
  1757. dryWeight.AdjustedValue = "/"
  1758. dryWeight.Creator = org_creator
  1759. dryWeight.UserId = org_creator
  1760. dryWeight.Ctime = time.Now().Unix()
  1761. dryWeight.Mtime = time.Now().Unix()
  1762. dryWeight.DryWeight = patient.DryWeight
  1763. dryWeight.Remakes = ""
  1764. err = utx.Create(&dryWeight).Error
  1765. }
  1766. var lapseto models.PatientLapseto
  1767. lapseto.PatientId = patient.ID
  1768. lapseto.LapsetoType = patient.Lapseto
  1769. lapseto.CreatedTime = patient.CreatedTime
  1770. lapseto.UpdatedTime = patient.CreatedTime
  1771. lapseto.Status = 1
  1772. lapseto.LapsetoTime = patient.CreatedTime
  1773. err = utx.Create(&lapseto).Error
  1774. if err != nil {
  1775. utx.Rollback()
  1776. btx.Rollback()
  1777. return
  1778. }
  1779. if len(contagions) > 0 {
  1780. thisSQL := "INSERT INTO xt_patients_infectious_diseases (patient_id, disease_id, status, created_time, updated_time) VALUES "
  1781. insertParams := make([]string, 0)
  1782. insertData := make([]interface{}, 0)
  1783. for _, contagion := range contagions {
  1784. insertParams = append(insertParams, "(?, ?, ?, ?, ?)")
  1785. insertData = append(insertData, patient.ID)
  1786. insertData = append(insertData, contagion)
  1787. insertData = append(insertData, 1)
  1788. insertData = append(insertData, patient.CreatedTime)
  1789. insertData = append(insertData, patient.UpdatedTime)
  1790. }
  1791. thisSQL += strings.Join(insertParams, ", ")
  1792. err = utx.Exec(thisSQL, insertData...).Error
  1793. if err != nil {
  1794. utx.Rollback()
  1795. btx.Rollback()
  1796. return
  1797. }
  1798. }
  1799. patientsNew := models.XtPatientsNew{
  1800. Name: patient.Name,
  1801. Gender: patient.Gender,
  1802. Phone: patient.Phone,
  1803. IdCardNo: patient.IdCardNo,
  1804. FirstDialysisDate: patient.FirstDialysisDate,
  1805. Source: patient.Source,
  1806. Lapseto: patient.Lapseto,
  1807. IsInfectious: patient.IsInfectious,
  1808. DialysisNo: patient.DialysisNo,
  1809. Height: patient.Height,
  1810. HomeAddress: patient.HomeAddress,
  1811. IsExcelExport: 1,
  1812. BloodPatients: 1,
  1813. Status: 1,
  1814. CreatedTime: time.Now().Unix(),
  1815. UserOrgId: patient.UserOrgId,
  1816. BloodId: patient.ID,
  1817. Avatar: "https://images.shengws.com/201809182128111.png",
  1818. }
  1819. err = utx.Create(&patientsNew).Error
  1820. record := models.XtPatientLaspseRecord{
  1821. PatientId: patient.ID,
  1822. RecordDate: time.Now().Unix(),
  1823. LapseDate: time.Now().Unix(),
  1824. LapseType: 1,
  1825. LapseClass: 0,
  1826. LapseReason: "",
  1827. AdminUserId: org_creator,
  1828. Remark: "",
  1829. UserOrgId: patient.UserOrgId,
  1830. Ctime: time.Now().Unix(),
  1831. Mtime: time.Now().Unix(),
  1832. Status: 1,
  1833. LapseTypeName: "转入",
  1834. HisType: 0,
  1835. HisZone: "",
  1836. HisBed: "",
  1837. }
  1838. err = utx.Create(&record).Error
  1839. if err != nil {
  1840. utx.Rollback()
  1841. btx.Rollback()
  1842. return
  1843. }
  1844. utx.Commit()
  1845. btx.Commit()
  1846. return
  1847. }
  1848. func FindPatientPhoneIsExist(phone string, org_id int64) (count int64) {
  1849. readDb.Model(&models.Patients{}).Where("user_org_id = ? AND phone = ? AND status = 1", org_id, phone).Count(&count)
  1850. return
  1851. }
  1852. func FindPatientIdCardNoIsExist(id_card_no string, org_id int64) (count int64) {
  1853. readDb.Model(&models.Patients{}).Where("user_org_id = ? AND id_card_no = ? AND status = 1", org_id, id_card_no).Count(&count)
  1854. return
  1855. }
  1856. func CreateExportErrLog(log *models.ExportErrLog) {
  1857. writeDb.Create(&log)
  1858. return
  1859. }
  1860. func FindPatientExportLog(org_id int64, export_time int64) (errLogs []*models.ExportErrLog, err error) {
  1861. err = readDb.Model(&models.ExportErrLog{}).Where("user_org_id = ? AND export_time = ? AND log_type = 1", org_id, export_time).Find(&errLogs).Error
  1862. return
  1863. }
  1864. func FindPatientExportLogOne(org_id int64, export_time int64) (errLogs []*models.ExportErrLog, err error) {
  1865. err = readDb.Model(&models.ExportErrLog{}).Where("user_org_id = ? AND export_time = ? AND log_type = 4", org_id, export_time).Find(&errLogs).Error
  1866. return
  1867. }
  1868. func FindPatientExportLogTwo(org_id int64, export_time int64) (errLogs []*models.ExportErrLog, err error) {
  1869. err = readDb.Model(&models.ExportErrLog{}).Where("user_org_id = ? AND export_time = ? AND log_type = 5", org_id, export_time).Find(&errLogs).Error
  1870. return
  1871. }
  1872. func FindPatientExportLogThree(org_id int64, export_time int64) (errLogs []*models.ExportErrLog, err error) {
  1873. err = readDb.Model(&models.ExportErrLog{}).Where("user_org_id = ? AND export_time = ? AND log_type = 6", org_id, export_time).Find(&errLogs).Error
  1874. return
  1875. }
  1876. func CreateExportLog(log *models.ExportLog) {
  1877. writeDb.Create(&log)
  1878. }
  1879. func UpdateDoctorEditAdvice(advice models.XtDoctorAdvice, orgid int64, groupno int64, date int64, patientid int64) error {
  1880. 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
  1881. return err
  1882. }
  1883. func GetPatientsByKey(orgID int64, keywords string) (patient []*models.Patients, err error) {
  1884. db := readDb.Model(&models.Patients{}).Where("user_org_id=? and status=1 and lapseto = 1 ", orgID)
  1885. if len(keywords) > 0 {
  1886. likekey := "%" + keywords + "%"
  1887. err = db.Where("(name LIKE ? OR dialysis_no LIKE ? OR first_letter LIKE ?)", likekey, likekey, likekey).Find(&patient).Error
  1888. } else {
  1889. err = db.Find(&patient).Error
  1890. }
  1891. return
  1892. }
  1893. func FindAllDoctorAdviceByGoroupNoTwo(orgID int64, groupno int64) (advice []models.DoctorAdvice, err error) {
  1894. err = readDb.Model(&models.DoctorAdvice{}).Where("user_org_id=? AND groupno = ? AND status = 1 AND execution_state = 2", orgID, groupno).Find(&advice).Error
  1895. return
  1896. }
  1897. func FindDoctorAdviceByIdsTwo(orgID int64, ids []string) (advice []models.DoctorAdvice, err error) {
  1898. 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
  1899. return
  1900. }
  1901. func CreateHisGroupAdvice(orgId int64, advices []*models.HisGroupAdvice, groupNo int64) (list []*models.HisGroupAdvice, err error) {
  1902. if groupNo <= 0 {
  1903. group := GetMaxAdviceGroupID(orgId)
  1904. groupNo = group + 1
  1905. }
  1906. tx := writeDb.Begin()
  1907. defer func() {
  1908. if r := recover(); r != nil {
  1909. tx.Rollback()
  1910. }
  1911. }()
  1912. for _, advice := range advices {
  1913. advice.Groupno = groupNo
  1914. if err = tx.Create(advice).Error; err != nil {
  1915. tx.Rollback()
  1916. return
  1917. }
  1918. list = append(list, advice)
  1919. if len(advice.Children) > 0 {
  1920. for _, child := range advice.Children {
  1921. child.Groupno = groupNo
  1922. child.ParentId = advice.ID
  1923. fmt.Println(child)
  1924. if err = tx.Create(&child).Error; err != nil {
  1925. tx.Rollback()
  1926. return
  1927. }
  1928. list = append(list, child)
  1929. }
  1930. }
  1931. }
  1932. tx.Commit()
  1933. return
  1934. }
  1935. func GetDialysisCount(orgid int64, partitionid int64) (order []*models.BloodDialysisOrderCount, err error) {
  1936. db := XTReadDB().Table("xt_dialysis_order as o")
  1937. if partitionid == 0 {
  1938. 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)
  1939. }
  1940. if partitionid > 0 {
  1941. 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)
  1942. }
  1943. return order, err
  1944. }
  1945. func UpdatePatientDialysisSolutionOne(patientid int64, orgid int64, prescription *models.DialysisPrescription, timenow int64) error {
  1946. 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
  1947. return err
  1948. }
  1949. //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) {
  1950. // 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
  1951. // return
  1952. //}
  1953. 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) {
  1954. 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
  1955. return
  1956. }
  1957. func UpdateScheduleByDeathTime(patientid int64, shcheduledate int64) error {
  1958. var err error
  1959. if shcheduledate > 0 {
  1960. err = XTWriteDB().Model(models.XtSchedule{}).Where("patient_id = ? and schedule_date > ? and status=1", patientid, shcheduledate).Updates(map[string]interface{}{"status": 0, "remark": "转出删除排班"}).Error
  1961. }
  1962. return err
  1963. }
  1964. func UpdatePatientLapsetoByPatientId(patient_id int64, record_date int64, lapse_reason string) error {
  1965. err := XTWriteDB().Model(&models.Patients{}).Where("id = ? and status= 1", patient_id).Updates(map[string]interface{}{"death_time": record_date, "lapseto": 2, "out_reason": lapse_reason}).Error
  1966. err = XTWriteDB().Model(&models.XtPatientsNew{}).Where("blood_id = ? and status=1", patient_id).Updates(map[string]interface{}{"death_time": record_date, "lapseto": 2, "out_reason": lapse_reason}).Error
  1967. return err
  1968. }
  1969. func UpdatePatientLapsetoByPatientIdOne(patient_id int64, record_date int64, lapse_reason string) error {
  1970. err := XTWriteDB().Model(&models.Patients{}).Where("id = ? and status= 1", patient_id).Updates(map[string]interface{}{"death_time": record_date, "lapseto": 3, "out_reason": lapse_reason}).Error
  1971. err = XTWriteDB().Model(&models.XtPatientsNew{}).Where("blood_id = ? and status=1", patient_id).Updates(map[string]interface{}{"death_time": record_date, "lapseto": 3, "out_reason": lapse_reason}).Error
  1972. return err
  1973. }
  1974. func UpdatePatientLasetoType(patient_id int64, lapse_type int64) error {
  1975. err := XTWriteDB().Model(&models.Patients{}).Where("id = ? and status=1", patient_id).Updates(map[string]interface{}{"lapseto": 11}).Error
  1976. err = XTWriteDB().Model(&models.XtPatientsNew{}).Where("id = ? and status=1", patient_id).Updates(map[string]interface{}{"lapseto": 11}).Error
  1977. return err
  1978. }
  1979. func UpdatePatientLapsetoByPatientIdTwo(patient_id int64, record_date int64) error {
  1980. err := XTWriteDB().Model(&models.Patients{}).Where("id = ? and status= 1", patient_id).Updates(map[string]interface{}{"death_time": record_date, "lapseto": 1}).Error
  1981. err = XTWriteDB().Model(&models.XtPatientsNew{}).Where("blood_id = ? and status=1", patient_id).Updates(map[string]interface{}{"death_time": record_date, "lapseto": 1}).Error
  1982. return err
  1983. }
  1984. func UpdateScheduleItemByPatientId(id int64) error {
  1985. err := XTWriteDB().Model(models.PatientScheduleTemplateItem{}).Where("patient_id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  1986. return err
  1987. }
  1988. func GetDoctorAdviceListThree(orgID, patientID, advice_type, stop, start, end int64, keywords string, page int64, limit int64, delivery_way_one string) (advices []*models.HisDoctorAdvice, total int64, err error) {
  1989. db := readDb.Table("his_doctor_advice_info as x").Where("x.status = 1")
  1990. table := UserReadDB().Table("sgj_user_admin_role as r")
  1991. fmt.Print("table", table)
  1992. if len(delivery_way_one) > 0 {
  1993. db = db.Where("delivery_way = ?", delivery_way_one)
  1994. }
  1995. if orgID > 0 {
  1996. db = db.Where("x.user_org_id=?", orgID)
  1997. }
  1998. if patientID > 0 {
  1999. db = db.Where("x.patient_id = ?", patientID)
  2000. }
  2001. if advice_type == 1 && advice_type > 0 {
  2002. db = db.Where("x.advice_type = ?", advice_type)
  2003. }
  2004. if advice_type == 2 && advice_type > 0 {
  2005. db = db.Where("x.advice_type = ?", advice_type)
  2006. }
  2007. if advice_type == 3 && advice_type > 0 {
  2008. db = db.Where("x.advice_type = 2 or x.advice_type = 3")
  2009. }
  2010. if advice_type == 4 && advice_type > 0 {
  2011. db = db.Where("x.advice_type = 4")
  2012. }
  2013. if stop == 1 {
  2014. db = db.Where("(x.stop_state=? or x.execution_state=?)", stop, stop)
  2015. } else if stop == 2 {
  2016. db = db.Where("x.stop_state=? and x.execution_state=?", stop, stop)
  2017. }
  2018. if start != 0 {
  2019. db = db.Where("x.start_time>=?", start)
  2020. }
  2021. if end != 0 {
  2022. db = db.Where("x.start_time<=?", end)
  2023. }
  2024. if len(keywords) > 0 {
  2025. likeKey := "%" + keywords + "%"
  2026. db = db.Where("x.advice_name LIKE ?", likeKey)
  2027. }
  2028. offset := (page - 1) * limit
  2029. 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
  2030. fmt.Print("err", err)
  2031. return
  2032. }
  2033. func GetDoctorAdviceListFour(orgID int64, patientID int64, advice_type int64, stop int64, start int64, end int64, keywords string, limit int64, page int64, delive_way_one string) (advices []*models.HisDoctorAdvice, total int64, err error) {
  2034. db := readDb.Table("his_doctor_advice_info as x").Where("x.status = 1")
  2035. table := UserReadDB().Table("sgj_user_admin_role as r")
  2036. fmt.Println(table)
  2037. if len(delive_way_one) > 0 {
  2038. db = db.Where("delivery_way = ?", delive_way_one)
  2039. }
  2040. if orgID > 0 {
  2041. db = db.Where("x.user_org_id=?", orgID)
  2042. }
  2043. if patientID > 0 {
  2044. db = db.Where("x.patient_id = ?", patientID)
  2045. }
  2046. if advice_type == 1 {
  2047. db = db.Where("x.advice_type = ?", advice_type)
  2048. }
  2049. if advice_type == 3 && advice_type > 0 {
  2050. db = db.Where("x.advice_type = 2 or x.advice_type = 3")
  2051. }
  2052. if advice_type == 4 && advice_type > 0 {
  2053. db = db.Where("x.advice_type = 4")
  2054. }
  2055. if stop == 1 {
  2056. db = db.Where("(x.stop_state=? or x.execution_state=?)", stop, stop)
  2057. } else if stop == 2 {
  2058. db = db.Where("x.stop_state=? and x.execution_state=?", stop, stop)
  2059. }
  2060. if start != 0 {
  2061. db = db.Where("x.start_time>=?", start)
  2062. }
  2063. if end != 0 {
  2064. db = db.Where("x.start_time<=?", end)
  2065. }
  2066. if len(keywords) > 0 {
  2067. likeKey := "%" + keywords + "%"
  2068. db = db.Where("x.advice_name LIKE ?", likeKey)
  2069. }
  2070. offset := (page - 1) * limit
  2071. 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
  2072. fmt.Print("错误是什么", err)
  2073. return
  2074. }
  2075. func GetAllDeviceNumberByListSix(orgID int64) (number []*models.DeviceNumber, err error) {
  2076. err = readDb.Model(&models.DeviceNumber{}).Where("org_id=? and status=1", orgID).Find(&number).Error
  2077. return number, err
  2078. }
  2079. func GetAllDeviceNumberByList(orgID int64) (number []*models.DeviceNumber, err error) {
  2080. redis := RedisClient()
  2081. defer redis.Close()
  2082. key := strconv.FormatInt(orgID, 10) + ":" + ":device_number_all"
  2083. number_info_str, _ := redis.Get(key).Result()
  2084. if len(number_info_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2085. err = readDb.Model(&models.DeviceNumber{}).Where("org_id=? and status=1", orgID).Find(&number).Error
  2086. if err != nil {
  2087. if err == gorm.ErrRecordNotFound {
  2088. return nil, nil
  2089. } else {
  2090. return nil, err
  2091. }
  2092. } else {
  2093. if len(number) > 0 {
  2094. //缓存数据
  2095. device_number_json, err := json.Marshal(&number)
  2096. if err == nil {
  2097. redis.Set(key, device_number_json, time.Second*60*60*18)
  2098. }
  2099. }
  2100. return number, nil
  2101. }
  2102. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2103. json.Unmarshal([]byte(number_info_str), &number)
  2104. return number, nil
  2105. }
  2106. }
  2107. func GetAllPrescriptionByListTwo(orgID int64, scheduleDate int64) (prescriptions []*models.DialysisPrescription, err error) {
  2108. redis := RedisClient()
  2109. defer redis.Close()
  2110. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":prescriptions_list_all"
  2111. prescriptions_list_all, _ := redis.Get(key).Result()
  2112. if len(prescriptions_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2113. if scheduleDate > 0 {
  2114. err = readDb.Model(&models.DialysisPrescription{}).Where("status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).Find(&prescriptions).Error
  2115. } else {
  2116. err = readDb.Model(&models.DialysisPrescription{}).Where("status = 1 AND user_org_id = ?", orgID).Find(&prescriptions).Error
  2117. }
  2118. if err != nil {
  2119. if err == gorm.ErrRecordNotFound {
  2120. return nil, nil
  2121. } else {
  2122. return nil, err
  2123. }
  2124. } else {
  2125. if len(prescriptions) > 0 {
  2126. //缓存数据
  2127. prescriptions_list_all, err := json.Marshal(&prescriptions)
  2128. if err == nil {
  2129. redis.Set(key, prescriptions_list_all, time.Second*60*60*18)
  2130. }
  2131. }
  2132. return prescriptions, nil
  2133. }
  2134. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2135. json.Unmarshal([]byte(prescriptions_list_all), &prescriptions)
  2136. return prescriptions, nil
  2137. }
  2138. }
  2139. func GetAllAssessmentBeforesByListThree(orgID int64, scheduleDate int64) (assessmentBefores []*models.PredialysisEvaluation, err error) {
  2140. redis := RedisClient()
  2141. defer redis.Close()
  2142. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":assessment_befores_list_all"
  2143. assessment_befores_list_all, _ := redis.Get(key).Result()
  2144. if len(assessment_befores_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2145. if scheduleDate > 0 {
  2146. err = readDb.Model(&models.PredialysisEvaluation{}).Where("status = 1 AND user_org_id = ? AND assessment_date = ? ", orgID, scheduleDate).Find(&assessmentBefores).Error
  2147. } else {
  2148. err = readDb.Model(&models.PredialysisEvaluation{}).Where("status = 1 AND user_org_id = ?", orgID).Find(&assessmentBefores).Error
  2149. }
  2150. if err != nil {
  2151. if err == gorm.ErrRecordNotFound {
  2152. return nil, nil
  2153. } else {
  2154. return nil, err
  2155. }
  2156. } else {
  2157. if len(assessmentBefores) > 0 {
  2158. //缓存数据
  2159. assessment_befores_list_all, err := json.Marshal(&assessmentBefores)
  2160. if err == nil {
  2161. redis.Set(key, assessment_befores_list_all, time.Second*60*60*18)
  2162. }
  2163. }
  2164. return assessmentBefores, nil
  2165. }
  2166. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2167. json.Unmarshal([]byte(assessment_befores_list_all), &assessmentBefores)
  2168. return assessmentBefores, nil
  2169. }
  2170. }
  2171. func GetAllDialysisOrdersByListThree(orgID int64, scheduleDate int64) (dialysisOrders []*models.MonitorDialysisOrder, err error) {
  2172. redis := RedisClient()
  2173. defer redis.Close()
  2174. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":dialysis_orders_list_all"
  2175. dialysis_orders_list_all, _ := redis.Get(key).Result()
  2176. if len(dialysis_orders_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2177. if scheduleDate > 0 {
  2178. 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
  2179. } else {
  2180. err = readDb.Model(&models.MonitorDialysisOrder{}).Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).Where("status = 1 AND user_org_id = ?", orgID).Find(&dialysisOrders).Error
  2181. }
  2182. if err != nil {
  2183. if err == gorm.ErrRecordNotFound {
  2184. return nil, nil
  2185. } else {
  2186. return nil, err
  2187. }
  2188. } else {
  2189. if len(dialysisOrders) > 0 {
  2190. //缓存数据
  2191. dialysis_orders_list_all, err := json.Marshal(&dialysisOrders)
  2192. if err == nil {
  2193. redis.Set(key, dialysis_orders_list_all, time.Second*60*60*18)
  2194. }
  2195. }
  2196. return dialysisOrders, nil
  2197. }
  2198. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2199. json.Unmarshal([]byte(dialysis_orders_list_all), &dialysisOrders)
  2200. return dialysisOrders, nil
  2201. }
  2202. }
  2203. func GetAllAssessmentAfterDislysisByListThree(orgID int64, scheduleDate int64) (assessmentAfterDislysis []*models.AssessmentAfterDislysis, err error) {
  2204. redis := RedisClient()
  2205. defer redis.Close()
  2206. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":assessment_after_dislysis_list_all"
  2207. assessment_after_dislysis__all, _ := redis.Get(key).Result()
  2208. if len(assessment_after_dislysis__all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2209. if scheduleDate > 0 {
  2210. err = readDb.Model(&models.AssessmentAfterDislysis{}).Where("status = 1 AND user_org_id = ? AND assessment_date = ?", orgID, scheduleDate).Find(&assessmentAfterDislysis).Error
  2211. } else {
  2212. err = readDb.Model(&models.AssessmentAfterDislysis{}).Where("status = 1 AND user_org_id = ?", orgID).Find(&assessmentAfterDislysis).Error
  2213. }
  2214. if err != nil {
  2215. if err == gorm.ErrRecordNotFound {
  2216. return nil, nil
  2217. } else {
  2218. return nil, err
  2219. }
  2220. } else {
  2221. if len(assessmentAfterDislysis) > 0 {
  2222. //缓存数据
  2223. assessment_after_dislysis__all, err := json.Marshal(&assessmentAfterDislysis)
  2224. if err == nil {
  2225. redis.Set(key, assessment_after_dislysis__all, time.Second*60*60*18)
  2226. }
  2227. }
  2228. return assessmentAfterDislysis, nil
  2229. }
  2230. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2231. json.Unmarshal([]byte(assessment_after_dislysis__all), &assessmentAfterDislysis)
  2232. return assessmentAfterDislysis, nil
  2233. }
  2234. }
  2235. func GetAllMonitorListThree(orgID int64, scheduleDate int64) (monitor []*models.MonitoringRecord, err error) {
  2236. redis := RedisClient()
  2237. defer redis.Close()
  2238. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":monitor_record_list_all"
  2239. monitor_record_list_all, _ := redis.Get(key).Result()
  2240. if len(monitor_record_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2241. if scheduleDate > 0 {
  2242. err = readDb.Model(&models.MonitoringRecord{}).Where("status = 1 AND user_org_id = ? AND monitoring_date= ?", orgID, scheduleDate).Find(&monitor).Error
  2243. } else {
  2244. err = readDb.Model(&models.MonitoringRecord{}).Where("status = 1 AND user_org_id = ?", orgID).Find(&monitor).Error
  2245. }
  2246. if err != nil {
  2247. if err == gorm.ErrRecordNotFound {
  2248. return nil, nil
  2249. } else {
  2250. return nil, err
  2251. }
  2252. } else {
  2253. if len(monitor) > 0 {
  2254. //缓存数据
  2255. assessment_after_dislysis__all, err := json.Marshal(&monitor)
  2256. if err == nil {
  2257. redis.Set(key, assessment_after_dislysis__all, time.Second*60*60*18)
  2258. }
  2259. }
  2260. return monitor, nil
  2261. }
  2262. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2263. json.Unmarshal([]byte(monitor_record_list_all), &monitor)
  2264. return monitor, nil
  2265. }
  2266. }
  2267. func GetAllPatientListByListThree(orgID int64) (patients []*models.MonitorPatients, err error) {
  2268. redis := RedisClient()
  2269. defer redis.Close()
  2270. key := strconv.FormatInt(orgID, 10) + ":" + ":patient_list_all"
  2271. patient_info_str, _ := redis.Get(key).Result()
  2272. if len(patient_info_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2273. err = readDb.Model(&models.MonitorPatients{}).Where("user_org_id=? and status=1 and lapseto = 1", orgID).Find(&patients).Error
  2274. if err != nil {
  2275. if err == gorm.ErrRecordNotFound {
  2276. return nil, nil
  2277. } else {
  2278. return nil, err
  2279. }
  2280. } else {
  2281. if len(patients) > 0 {
  2282. //缓存数据
  2283. patient_info_json, err := json.Marshal(&patients)
  2284. if err == nil {
  2285. redis.Set(key, patient_info_json, time.Second*60*60*18)
  2286. }
  2287. }
  2288. return patients, nil
  2289. }
  2290. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2291. json.Unmarshal([]byte(patient_info_str), &patients)
  2292. return patients, nil
  2293. }
  2294. }
  2295. func GetAllPatientListByListSix(orgID int64, keywords string) (patients []*models.MonitorPatients, err error) {
  2296. db := readDb.Model(&models.MonitorPatients{}).Where("status = 1 and lapseto = 1")
  2297. likeKey := "%" + keywords + "%"
  2298. if orgID > 0 {
  2299. db = db.Where("user_org_id = ?", orgID)
  2300. }
  2301. if len(keywords) > 0 {
  2302. db = db.Where("name like ?", likeKey)
  2303. }
  2304. err = db.Find(&patients).Error
  2305. return patients, err
  2306. }
  2307. func GetAllDialysisOrdersByListTwo(orgID int64, scheduleDate int64) (dialysisOrders []*MDialysisOrderVM, err error) {
  2308. redis := RedisClient()
  2309. defer redis.Close()
  2310. key := strconv.FormatInt(orgID, 10) + ":" + ":dialysis_orders_list_all"
  2311. dialysis_orders_list_all, _ := redis.Get(key).Result()
  2312. if scheduleDate < 1669132800 {
  2313. fmt.Println("key233232232332323232", key)
  2314. redis.Set(key, "", time.Second)
  2315. }
  2316. fmt.Println("dialysis_orders_list_all", dialysis_orders_list_all)
  2317. if len(dialysis_orders_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2318. 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
  2319. if err != nil {
  2320. if err == gorm.ErrRecordNotFound {
  2321. return nil, nil
  2322. } else {
  2323. return nil, err
  2324. }
  2325. } else {
  2326. if len(dialysisOrders) > 0 {
  2327. //缓存数据
  2328. dialysis_orders_list_all, err := json.Marshal(&dialysisOrders)
  2329. if err == nil {
  2330. redis.Set(key, dialysis_orders_list_all, time.Second*60*60*18)
  2331. }
  2332. }
  2333. return dialysisOrders, nil
  2334. }
  2335. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2336. json.Unmarshal([]byte(dialysis_orders_list_all), &dialysisOrders)
  2337. return dialysisOrders, nil
  2338. }
  2339. }
  2340. func GetAllZoneByList(orgID int64) (zone []*models.DeviceZone, err error) {
  2341. redis := RedisClient()
  2342. defer redis.Close()
  2343. key := strconv.FormatInt(orgID, 10) + ":" + ":device_zone_all"
  2344. zone_info_str, _ := redis.Get(key).Result()
  2345. if len(zone_info_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2346. err = readDb.Model(&models.DeviceZone{}).Where("org_id=? and status=1", orgID).Find(&zone).Error
  2347. if err != nil {
  2348. if err == gorm.ErrRecordNotFound {
  2349. return nil, nil
  2350. } else {
  2351. return nil, err
  2352. }
  2353. } else {
  2354. if len(zone) > 0 {
  2355. //缓存数据
  2356. zone_number_json, err := json.Marshal(&zone)
  2357. if err == nil {
  2358. redis.Set(key, zone_number_json, time.Second*60*60*18)
  2359. }
  2360. }
  2361. return zone, nil
  2362. }
  2363. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2364. json.Unmarshal([]byte(zone_info_str), &zone)
  2365. return zone, nil
  2366. }
  2367. }
  2368. func GetAllDialysisOrdersByListNight(orgID int64, scheduleDate int64, pay_tyep int64) (dialysisOrders []*models.VMMonitorDialysisOrder, err error) {
  2369. if pay_tyep == 0 {
  2370. 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
  2371. }
  2372. //待下机
  2373. if pay_tyep == 3 {
  2374. 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
  2375. }
  2376. fmt.Println("pay_typeowowowoowwo", pay_tyep)
  2377. if pay_tyep == 7 {
  2378. 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
  2379. }
  2380. return dialysisOrders, err
  2381. }
  2382. func GetAllPrescriptionByListSix(orgID int64, scheduleDate int64) (prescriptions []*models.VMDialysisPrescription, err error) {
  2383. err = readDb.Model(&models.VMDialysisPrescription{}).Where("status= 1 and user_org_id = ? and record_date = ?", orgID, scheduleDate).Find(&prescriptions).Error
  2384. return prescriptions, err
  2385. //redis := RedisClient()
  2386. //defer redis.Close()
  2387. //key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":prescriptions_list_all"
  2388. //redis.Set(key, "", time.Second)
  2389. //prescriptions_list_all, _ := redis.Get(key).Result()
  2390. //
  2391. //if len(prescriptions_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2392. // err = readDb.Model(&models.VMDialysisPrescription{}).Where("status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).Find(&prescriptions).Error
  2393. // if err != nil {
  2394. // if err == gorm.ErrRecordNotFound {
  2395. // return nil, nil
  2396. // } else {
  2397. // return nil, err
  2398. // }
  2399. // } else {
  2400. // if len(prescriptions) > 0 {
  2401. // //缓存数据
  2402. // prescriptions_list_all, err := json.Marshal(&prescriptions)
  2403. // if err == nil {
  2404. // redis.Set(key, prescriptions_list_all, time.Second*60*60*18)
  2405. // }
  2406. // }
  2407. // return prescriptions, nil
  2408. // }
  2409. //} else { //缓存数据了数据,将redis缓存的json字符串转为map
  2410. // json.Unmarshal([]byte(prescriptions_list_all), &prescriptions)
  2411. // return prescriptions, nil
  2412. //}
  2413. }
  2414. func GetAllPrescriptionByList(orgID int64, scheduleDate int64) (prescriptions []*models.VMDialysisPrescription, err error) {
  2415. redis := RedisClient()
  2416. defer redis.Close()
  2417. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":prescriptions_list_all"
  2418. redis.Set(key, "", time.Second)
  2419. prescriptions_list_all, _ := redis.Get(key).Result()
  2420. if len(prescriptions_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2421. err = readDb.Model(&models.VMDialysisPrescription{}).Where("status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).Find(&prescriptions).Error
  2422. if err != nil {
  2423. if err == gorm.ErrRecordNotFound {
  2424. return nil, nil
  2425. } else {
  2426. return nil, err
  2427. }
  2428. } else {
  2429. if len(prescriptions) > 0 {
  2430. //缓存数据
  2431. prescriptions_list_all, err := json.Marshal(&prescriptions)
  2432. if err == nil {
  2433. redis.Set(key, prescriptions_list_all, time.Second*60*60*18)
  2434. }
  2435. }
  2436. return prescriptions, nil
  2437. }
  2438. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2439. json.Unmarshal([]byte(prescriptions_list_all), &prescriptions)
  2440. return prescriptions, nil
  2441. }
  2442. }
  2443. func GetAllAssessmentBeforesByListOne(orgID int64, scheduleDate int64) (assessmentBefores []*models.VMPredialysisEvaluation, err error) {
  2444. redis := RedisClient()
  2445. defer redis.Close()
  2446. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":assessment_befores_list_all"
  2447. assessment_befores_list_all, _ := redis.Get(key).Result()
  2448. if len(assessment_befores_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2449. err = readDb.Model(&models.VMPredialysisEvaluation{}).Where("status = 1 AND user_org_id = ? AND assessment_date = ? ", orgID, scheduleDate).Find(&assessmentBefores).Error
  2450. if err != nil {
  2451. if err == gorm.ErrRecordNotFound {
  2452. return nil, nil
  2453. } else {
  2454. return nil, err
  2455. }
  2456. } else {
  2457. if len(assessmentBefores) > 0 {
  2458. //缓存数据
  2459. assessment_befores_list_all, err := json.Marshal(&assessmentBefores)
  2460. if err == nil {
  2461. redis.Set(key, assessment_befores_list_all, time.Second*60*60*18)
  2462. }
  2463. }
  2464. return assessmentBefores, nil
  2465. }
  2466. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2467. json.Unmarshal([]byte(assessment_befores_list_all), &assessmentBefores)
  2468. return assessmentBefores, nil
  2469. }
  2470. }
  2471. func GetAllDialysisOrdersByListSix(orgID int64, scheduleDate int64) (dialysisOrders []*models.VMMonitorDialysisOrder, err error) {
  2472. 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
  2473. return dialysisOrders, err
  2474. }
  2475. func GetAllDialysisOrdersByListSeven(orgID int64, scheduleDate int64) (dialysisOrders []*MDialysisOrderVM, err error) {
  2476. 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
  2477. return dialysisOrders, err
  2478. }
  2479. func GetAllDialysisOrdersByListOne(orgID int64, scheduleDate int64) (dialysisOrders []*models.VMMonitorDialysisOrder, err error) {
  2480. redis := RedisClient()
  2481. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":dialysis_orders_list_all"
  2482. fmt.Println("key22332323232323223", key)
  2483. dialysis_orders_list_all, _ := redis.Get(key).Result()
  2484. defer redis.Close()
  2485. if len(dialysis_orders_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2486. 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
  2487. if err != nil {
  2488. if err == gorm.ErrRecordNotFound {
  2489. return nil, nil
  2490. } else {
  2491. return nil, err
  2492. }
  2493. } else {
  2494. if len(dialysisOrders) > 0 {
  2495. //缓存数据
  2496. dialysis_orders_list_all, err := json.Marshal(&dialysisOrders)
  2497. if err == nil {
  2498. redis.Set(key, dialysis_orders_list_all, time.Second*60*60*18)
  2499. }
  2500. }
  2501. return dialysisOrders, nil
  2502. }
  2503. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2504. json.Unmarshal([]byte(dialysis_orders_list_all), &dialysisOrders)
  2505. return dialysisOrders, nil
  2506. }
  2507. }
  2508. func GetAllAssessmentAfterDislysisByListSix(orgID int64, scheduleDate int64) (assessmentAfterDislysis []*models.VMAssessmentAfterDislysis, err error) {
  2509. err = readDb.Model(&models.VMAssessmentAfterDislysis{}).Where("status=1 and user_org_id = ? and assessment_date = ?", orgID, scheduleDate).Find(&assessmentAfterDislysis).Error
  2510. return assessmentAfterDislysis, err
  2511. //redis := RedisClient()
  2512. //defer redis.Close()
  2513. //key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":assessment_after_dislysis_list_all"
  2514. //redis.Set(key, "", time.Second)
  2515. //assessment_after_dislysis__all, _ := redis.Get(key).Result()
  2516. //
  2517. //if len(assessment_after_dislysis__all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2518. // err = readDb.Model(&models.VMAssessmentAfterDislysis{}).Where("status = 1 AND user_org_id = ? AND assessment_date = ?", orgID, scheduleDate).Find(&assessmentAfterDislysis).Error
  2519. // if err != nil {
  2520. // if err == gorm.ErrRecordNotFound {
  2521. // return nil, nil
  2522. // } else {
  2523. // return nil, err
  2524. // }
  2525. // } else {
  2526. // if len(assessmentAfterDislysis) > 0 {
  2527. // //缓存数据
  2528. // assessment_after_dislysis__all, err := json.Marshal(&assessmentAfterDislysis)
  2529. // if err == nil {
  2530. // redis.Set(key, assessment_after_dislysis__all, time.Second*60*60*18)
  2531. // }
  2532. // }
  2533. // return assessmentAfterDislysis, nil
  2534. // }
  2535. //} else { //缓存数据了数据,将redis缓存的json字符串转为map
  2536. // json.Unmarshal([]byte(assessment_after_dislysis__all), &assessmentAfterDislysis)
  2537. // return assessmentAfterDislysis, nil
  2538. //}
  2539. }
  2540. func GetAllAssessmentAfterDislysisByListOne(orgID int64, scheduleDate int64) (assessmentAfterDislysis []*models.VMAssessmentAfterDislysis, err error) {
  2541. redis := RedisClient()
  2542. defer redis.Close()
  2543. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":assessment_after_dislysis_list_all"
  2544. redis.Set(key, "", time.Second)
  2545. assessment_after_dislysis__all, _ := redis.Get(key).Result()
  2546. if len(assessment_after_dislysis__all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2547. err = readDb.Model(&models.VMAssessmentAfterDislysis{}).Where("status = 1 AND user_org_id = ? AND assessment_date = ?", orgID, scheduleDate).Find(&assessmentAfterDislysis).Error
  2548. if err != nil {
  2549. if err == gorm.ErrRecordNotFound {
  2550. return nil, nil
  2551. } else {
  2552. return nil, err
  2553. }
  2554. } else {
  2555. if len(assessmentAfterDislysis) > 0 {
  2556. //缓存数据
  2557. assessment_after_dislysis__all, err := json.Marshal(&assessmentAfterDislysis)
  2558. if err == nil {
  2559. redis.Set(key, assessment_after_dislysis__all, time.Second*60*60*18)
  2560. }
  2561. }
  2562. return assessmentAfterDislysis, nil
  2563. }
  2564. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2565. json.Unmarshal([]byte(assessment_after_dislysis__all), &assessmentAfterDislysis)
  2566. return assessmentAfterDislysis, nil
  2567. }
  2568. }
  2569. func GetAllMonitorList(orgID int64, scheduleDate int64) (monitor []*models.VMMonitoringRecord, err error) {
  2570. redis := RedisClient()
  2571. defer redis.Close()
  2572. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":monitor_record_list_all"
  2573. monitor_record_list_all, _ := redis.Get(key).Result()
  2574. redis.Set(key, "", time.Second)
  2575. if len(monitor_record_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2576. err = readDb.Model(&models.VMMonitoringRecord{}).Where("status = 1 AND user_org_id = ? AND monitoring_date= ?", orgID, scheduleDate).Order("monitoring_date asc").Find(&monitor).Error
  2577. if err != nil {
  2578. if err == gorm.ErrRecordNotFound {
  2579. return nil, nil
  2580. } else {
  2581. return nil, err
  2582. }
  2583. } else {
  2584. if len(monitor) > 0 {
  2585. //缓存数据
  2586. assessment_after_dislysis__all, err := json.Marshal(&monitor)
  2587. if err == nil {
  2588. redis.Set(key, assessment_after_dislysis__all, time.Second*60*60*18)
  2589. }
  2590. }
  2591. return monitor, nil
  2592. }
  2593. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2594. json.Unmarshal([]byte(monitor_record_list_all), &monitor)
  2595. return monitor, nil
  2596. }
  2597. }
  2598. func GetAllPatientListByListOne(orgID int64) (patients []*models.VMMonitorPatients, err error) {
  2599. redis := RedisClient()
  2600. defer redis.Close()
  2601. key := strconv.FormatInt(orgID, 10) + ":" + ":patient_list_all"
  2602. patient_info_str, _ := redis.Get(key).Result()
  2603. redis.Set(key, "", time.Second)
  2604. if len(patient_info_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2605. err = readDb.Model(&models.VMMonitorPatients{}).Where("user_org_id=? and status=1", orgID).Find(&patients).Error
  2606. if err != nil {
  2607. if err == gorm.ErrRecordNotFound {
  2608. return nil, nil
  2609. } else {
  2610. return nil, err
  2611. }
  2612. } else {
  2613. if len(patients) > 0 {
  2614. //缓存数据
  2615. patient_info_json, err := json.Marshal(&patients)
  2616. if err == nil {
  2617. redis.Set(key, patient_info_json, time.Second*60*60*18)
  2618. }
  2619. }
  2620. return patients, nil
  2621. }
  2622. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2623. json.Unmarshal([]byte(patient_info_str), &patients)
  2624. return patients, nil
  2625. }
  2626. }
  2627. func GetAllTreatModeByList(orgID int64) (treatement []*models.TreatmentMode, err error) {
  2628. redis := RedisClient()
  2629. defer redis.Close()
  2630. key := strconv.FormatInt(orgID, 10) + ":" + ":treatement_mode_all"
  2631. treatment_info_str, _ := redis.Get(key).Result()
  2632. if len(treatment_info_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2633. err = readDb.Model(&models.TreatmentMode{}).Where("status=1").Find(&treatement).Error
  2634. if err != nil {
  2635. if err == gorm.ErrRecordNotFound {
  2636. return nil, nil
  2637. } else {
  2638. return nil, err
  2639. }
  2640. } else {
  2641. if len(treatement) > 0 {
  2642. //缓存数据
  2643. patient_info_json, err := json.Marshal(&treatement)
  2644. if err == nil {
  2645. redis.Set(key, patient_info_json, time.Second*60*60*18)
  2646. }
  2647. }
  2648. return treatement, nil
  2649. }
  2650. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2651. json.Unmarshal([]byte(treatment_info_str), &treatement)
  2652. return treatement, nil
  2653. }
  2654. }
  2655. func GetAllPatientListByListSeven(orgID int64) (patients []*MSchedualPatientVM, err error) {
  2656. err = readDb.Model(&MSchedualPatientVM{}).Where("user_org_id=? and status=1", orgID).Find(&patients).Error
  2657. return patients, nil
  2658. }
  2659. func GetAllPatientListByListTwo(orgID int64) (patients []*MSchedualPatientVM, err error) {
  2660. redis := RedisClient()
  2661. defer redis.Close()
  2662. key := strconv.FormatInt(orgID, 10) + ":" + ":patient_list_all"
  2663. patient_info_str, _ := redis.Get(key).Result()
  2664. if len(patient_info_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2665. err = readDb.Model(&MSchedualPatientVM{}).Where("user_org_id=? and status=1", orgID).Find(&patients).Error
  2666. if err != nil {
  2667. if err == gorm.ErrRecordNotFound {
  2668. return nil, nil
  2669. } else {
  2670. return nil, err
  2671. }
  2672. } else {
  2673. if len(patients) > 0 {
  2674. //缓存数据
  2675. patient_info_json, err := json.Marshal(&patients)
  2676. if err == nil {
  2677. redis.Set(key, patient_info_json, time.Second*60*60*18)
  2678. }
  2679. }
  2680. return patients, nil
  2681. }
  2682. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2683. json.Unmarshal([]byte(patient_info_str), &patients)
  2684. return patients, nil
  2685. }
  2686. }
  2687. func GetAllDeviceNumberByListOne(orgID int64) (number []*MDeviceNumberVM, err error) {
  2688. redis := RedisClient()
  2689. defer redis.Close()
  2690. key := strconv.FormatInt(orgID, 10) + ":" + ":device_number_all"
  2691. number_info_str, _ := redis.Get(key).Result()
  2692. if len(number_info_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2693. err = readDb.Model(&MDeviceNumberVM{}).Where("org_id=? and status=1", orgID).Find(&number).Error
  2694. if err != nil {
  2695. if err == gorm.ErrRecordNotFound {
  2696. return nil, nil
  2697. } else {
  2698. return nil, err
  2699. }
  2700. } else {
  2701. if len(number) > 0 {
  2702. //缓存数据
  2703. device_number_json, err := json.Marshal(&number)
  2704. if err == nil {
  2705. redis.Set(key, device_number_json, time.Second*60*60*18)
  2706. }
  2707. }
  2708. return number, nil
  2709. }
  2710. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2711. json.Unmarshal([]byte(number_info_str), &number)
  2712. return number, nil
  2713. }
  2714. }
  2715. func GetAllPrescriptionByListOne(orgID int64, scheduleDate int64) (prescriptions []*models.DialysisPrescriptionTwenty, err error) {
  2716. redis := RedisClient()
  2717. defer redis.Close()
  2718. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":prescriptions_list_all"
  2719. prescriptions_list_all, _ := redis.Get(key).Result()
  2720. if len(prescriptions_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2721. err = readDb.Model(&models.DialysisPrescriptionTwenty{}).Where("status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).Find(&prescriptions).Error
  2722. if err != nil {
  2723. if err == gorm.ErrRecordNotFound {
  2724. return nil, nil
  2725. } else {
  2726. return nil, err
  2727. }
  2728. } else {
  2729. if len(prescriptions) > 0 {
  2730. //缓存数据
  2731. prescriptions_list_all, err := json.Marshal(&prescriptions)
  2732. if err == nil {
  2733. redis.Set(key, prescriptions_list_all, time.Second*60*60*18)
  2734. }
  2735. }
  2736. return prescriptions, nil
  2737. }
  2738. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2739. json.Unmarshal([]byte(prescriptions_list_all), &prescriptions)
  2740. return prescriptions, nil
  2741. }
  2742. }
  2743. func GetAllHisProjectPrescription(orgid int64, recorddate int64) (project []*models.HisPrescriptionProjectTwo, err error) {
  2744. err = XTReadDB().Where("user_org_id = ? and record_date = ? and status = 1", orgid, recorddate).Find(&project).Error
  2745. return project, err
  2746. }
  2747. func GetZoneCountList(orgid int64, schedule_date int64) (list []*models.XtScheduleSeven, err error) {
  2748. db := XTReadDB().Model(&list).Where("status = 1")
  2749. if orgid > 0 {
  2750. db = db.Where("user_org_id = ?", orgid)
  2751. }
  2752. if schedule_date > 0 {
  2753. db = db.Where("schedule_date =?", schedule_date)
  2754. }
  2755. err = db.Select("partition_id,count(id) as count").Group("partition_id").Scan(&list).Error
  2756. return list, err
  2757. }
  2758. func GetPatientDialysisLongSolution(patient_id int64, page int64, limit int64, orgid int64) (solution []*models.DialysisSolution, total int64, err error) {
  2759. db := XTReadDB().Model(&solution).Where("status = 1 and solution_status = 1")
  2760. if patient_id > 0 {
  2761. db = db.Where("patient_id = ?", patient_id)
  2762. }
  2763. if orgid > 0 {
  2764. db = db.Where("user_org_id = ?", orgid)
  2765. }
  2766. offset := (page - 1) * limit
  2767. err = db.Group("mode_id").Count(&total).Offset(offset).Limit(limit).Order("created_time desc").Find(&solution).Error
  2768. return solution, total, err
  2769. }
  2770. func GetDialysisSolutionDetailList(orgid int64, patient_id int64, mode_id int64, page int64, limit int64) (solution []*models.DialysisSolution, total int64, err error) {
  2771. offset := (page - 1) * limit
  2772. db := XTReadDB().Model(&solution).Where("status = 1")
  2773. if orgid > 0 {
  2774. db = db.Where("user_org_id = ?", orgid)
  2775. }
  2776. if patient_id > 0 {
  2777. db = db.Where("patient_id = ?", patient_id)
  2778. }
  2779. if mode_id > 0 {
  2780. db = db.Where("mode_id = ?", mode_id)
  2781. }
  2782. err = db.Count(&total).Offset(offset).Limit(limit).Find(&solution).Order("created_time desc").Error
  2783. return solution, total, err
  2784. }
  2785. func UpdateDialysisBefor(dislysis models.XtAssessmentBeforeDislysis, id int64) error {
  2786. 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
  2787. return err
  2788. }
  2789. func GetAllSchedulesByPatientInfo(org_id int64, patient_id int64, mode_id int64) ([]Schedule, error) {
  2790. var record []Schedule
  2791. if org_id == 10579 {
  2792. timeNowStr := time.Now().Format("2006-01-02")
  2793. timeNewDate, _ := utils.ParseTimeStringToTime("2006-01-02", timeNowStr)
  2794. 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
  2795. if err != nil {
  2796. if err == gorm.ErrRecordNotFound {
  2797. return nil, nil
  2798. } else {
  2799. return nil, err
  2800. }
  2801. }
  2802. } else {
  2803. timeNowStr := time.Now().Format("2006-01-02")
  2804. timeNewDate, _ := utils.ParseTimeStringToTime("2006-01-02", timeNowStr)
  2805. 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
  2806. if err != nil {
  2807. if err == gorm.ErrRecordNotFound {
  2808. return nil, nil
  2809. } else {
  2810. return nil, err
  2811. }
  2812. }
  2813. }
  2814. return record, nil
  2815. }
  2816. func UpdateScheduleByExport(org_id int64, shcheduledate int64) error {
  2817. err := XTWriteDB().Model(models.XtSchedule{}).Where("user_org_id = ? and schedule_date > ?", org_id, shcheduledate).Updates(map[string]interface{}{"status": 0}).Error
  2818. return err
  2819. }
  2820. func GetContextScheduleListGroupPatientId(limit int64, page int64, startTime int64, endTime int64, org_id int64) (schedule []*models.ConScheduleList, total int64, err error) {
  2821. db := XTReadDB().Model(&schedule).Where("status=1")
  2822. offset := (page - 1) * limit
  2823. if startTime > 0 {
  2824. db = db.Where("schedule_date >=?", startTime)
  2825. }
  2826. if endTime > 0 {
  2827. db = db.Where("schedule_date<=?", endTime)
  2828. }
  2829. if org_id > 0 {
  2830. db = db.Where("user_org_id = ?", org_id)
  2831. }
  2832. err = db.Count(&total).Offset(offset).Limit(limit).Group("patient_id").Find(&schedule).Error
  2833. return schedule, total, err
  2834. }
  2835. func GetContextScheduleListPatientId(startTime int64, endTime int64, org_id int64) (schedule []*models.ConScheduleList, total int64, err error) {
  2836. db := XTReadDB().Model(&schedule).Where("status=1")
  2837. if startTime > 0 {
  2838. db = db.Where("schedule_date >=?", startTime)
  2839. }
  2840. if endTime > 0 {
  2841. db = db.Where("schedule_date<=?", endTime)
  2842. }
  2843. if org_id > 0 {
  2844. db = db.Where("user_org_id = ?", org_id)
  2845. }
  2846. err = db.Find(&schedule).Error
  2847. return schedule, total, err
  2848. }
  2849. func GetPatientAllagicList(patient_id int64, user_org_id int64) (list []*models.XtPatientAllergic, err error) {
  2850. err = XTReadDB().Where("patient_id = ? and user_org_id =? and status= 1", patient_id, user_org_id).Find(&list).Error
  2851. return list, err
  2852. }
  2853. func GetPatientAllagicListOne(user_org_id int64) (list []*models.XtPatientAllergic, err error) {
  2854. err = XTReadDB().Where("user_org_id =? and status= 1", user_org_id).Find(&list).Error
  2855. return list, err
  2856. }
  2857. func GetNewMonitorRecord(orgID int64, date int64, partition int64, patientId int64) ([]*models.NewMonitorDialysisScheduleList, error) {
  2858. var mds []*models.NewMonitorDialysisScheduleList
  2859. db := readDb.Model(&models.NewMonitorDialysisScheduleList{})
  2860. if date > 0 {
  2861. db = db.Where("schedule_date = ?", date)
  2862. }
  2863. if partition > 0 {
  2864. db = db.Where("partition_id = ?", partition)
  2865. }
  2866. if patientId > 0 {
  2867. db = db.Where("patient_id = ?", patientId)
  2868. }
  2869. err = db.Where("status = 1 and user_org_id = ?", orgID).Find(&mds).Error
  2870. return mds, err
  2871. }
  2872. func GetAdviceList(user_org_id int64, advice_date int64, pat_type int64) (advice []*models.VMDoctorAdviceForList, err error) {
  2873. if pat_type == 0 {
  2874. err = XTReadDB().Where("user_org_id = ? and advice_date = ? and status =7 and check_time = 0", user_org_id, advice_date).Find(&advice).Error
  2875. }
  2876. //待医嘱核对
  2877. if pat_type == 1 {
  2878. err = XTReadDB().Where("user_org_id = ? and advice_date = ? and status =1 and check_time = 0", user_org_id, advice_date).Find(&advice).Error
  2879. }
  2880. //待医嘱执行
  2881. if pat_type == 6 {
  2882. err = XTReadDB().Where("user_org_id = ? and advice_date = ? and status =1 and execution_state = 2", user_org_id, advice_date).Find(&advice).Error
  2883. }
  2884. return advice, err
  2885. }
  2886. func GetDobuleCheck(user_org_id int64, advice_date int64) (check []*models.VmDoubleCheck, err error) {
  2887. err = XTReadDB().Where("user_org_id = ? and check_date = ? and status = 1", user_org_id, advice_date).Find(&check).Error
  2888. return check, err
  2889. }
  2890. func GetDobuleCheckOne(user_org_id int64, advice_date int64, patient_id int64) (check models.VmDoubleCheck, err error) {
  2891. err = XTReadDB().Where("user_org_id = ? and check_date = ? and status = 1 and patient_id = ?", user_org_id, advice_date, patient_id).Find(&check).Error
  2892. return check, err
  2893. }
  2894. func GetTreatmentSummaryForList(user_org_id int64, advice_date int64) (summary []*models.VMTreatmentSummaryForList, err error) {
  2895. err = XTReadDB().Where("user_org_id =? and assessment_date = ? and status =1", user_org_id, advice_date).Find(&summary).Error
  2896. return summary, err
  2897. }
  2898. func GetDialysisInformationList(user_org_id int64, advice_date int64) (information []*models.NewDeviceInformation, err error) {
  2899. err = UserReadDB().Where("user_org_id =? and date = ? and status =1", user_org_id, advice_date).Find(&information).Error
  2900. return information, err
  2901. }
  2902. func GetNewAllMonitorList(orgID int64, scheduleDate int64, pat_type int64) (monitor []*models.VMMonitoringRecord, err error) {
  2903. if pat_type == 0 || pat_type == 9 {
  2904. err = readDb.Model(&models.VMMonitoringRecord{}).Where("status = 1 AND user_org_id = ? AND monitoring_date= ?", orgID, scheduleDate).Order("monitoring_date asc").Find(&monitor).Error
  2905. }
  2906. if pat_type == 10 {
  2907. 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
  2908. }
  2909. if pat_type == 11 {
  2910. 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
  2911. }
  2912. if pat_type == 12 {
  2913. 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
  2914. }
  2915. return monitor, err
  2916. }
  2917. func GetBaseDrugByName(drug_name string, org_id int64) (models.BaseDrugLibSeven, error) {
  2918. libSeven := models.BaseDrugLibSeven{}
  2919. err := XTReadDB().Where("drug_name= ? and org_id = ? and status=1", drug_name, org_id).First(&libSeven).Error
  2920. return libSeven, err
  2921. }
  2922. func CreateDeviceValsualMap(valsualMap models.XtDeviceValsualMap) error {
  2923. err := XTWriteDB().Save(&valsualMap).Error
  2924. return err
  2925. }
  2926. func GetPatientSitemap(patient_id int64, user_org_id int64) (models.XtDeviceValsualMap, error) {
  2927. valsualMap := models.XtDeviceValsualMap{}
  2928. 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
  2929. return valsualMap, err
  2930. }
  2931. func UpdateSiteMap(valsualMap models.XtDeviceValsualMap) error {
  2932. err := XTWriteDB().Save(&valsualMap).Error
  2933. return err
  2934. }
  2935. func CreateValsualAleft(left models.XtDeviceValsualALeft) error {
  2936. err := XTWriteDB().Create(&left).Error
  2937. return err
  2938. }
  2939. func UpdateValsualAleft(patient_id int64, user_org_id int64) error {
  2940. 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
  2941. return err
  2942. }
  2943. func CreateValsualMapFangxiangA(a models.XtDeviceValsualMapFangxiangA) error {
  2944. err := XTWriteDB().Create(&a).Error
  2945. return err
  2946. }
  2947. func UpdateValsualMapFangxiangA(patient_id int64, user_org_id int64) error {
  2948. 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
  2949. return err
  2950. }
  2951. func CreateValsualMapFangxiangV(v models.XtDeviceValsualMapFangxiangV) error {
  2952. err := XTWriteDB().Create(&v).Error
  2953. return err
  2954. }
  2955. func UpdateValsualMapFangxiangV(patient_id int64, user_org_id int64) error {
  2956. 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
  2957. return err
  2958. }
  2959. func ValsualMapFangxiangA(patient_id int64, user_org_id int64) error {
  2960. 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
  2961. return err
  2962. }
  2963. func CreateValSualFuzhA(a models.XtDeviceValsualFuzhuA) error {
  2964. err := XTReadDB().Create(&a).Error
  2965. return err
  2966. }
  2967. func UpdateValSualFuzhA(patient_id int64, user_org_id int64) error {
  2968. 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
  2969. return err
  2970. }
  2971. func CreateVasuaLfuzhuV(v models.XtDeviceValsualFuzhuV) error {
  2972. err := XTReadDB().Create(&v).Error
  2973. return err
  2974. }
  2975. func UpdateVasuaLfuzhuV(patient_id int64, user_org_id int64) error {
  2976. 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
  2977. return err
  2978. }
  2979. func CreateTextArr(arr models.XtDeviceValsualTextArr) error {
  2980. err := XTReadDB().Create(&arr).Error
  2981. return err
  2982. }
  2983. func UpdateTextArr(patient_id int64, user_org_id int64) error {
  2984. 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
  2985. return err
  2986. }
  2987. func CreateValsualVleft(left models.XtDeviceValsualVLeft) error {
  2988. err = XTReadDB().Create(&left).Error
  2989. return err
  2990. }
  2991. func UpdateValsualVleft(patient_id int64, user_org_id int64) error {
  2992. 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
  2993. return err
  2994. }
  2995. func UpdateValusalMapArr(patient_id int64, user_org_id int64) error {
  2996. 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
  2997. return err
  2998. }
  2999. func CreateDeviceValusalMapArr(vrr models.XtDeviceValusalMapArr) error {
  3000. err := XTWriteDB().Create(&vrr).Error
  3001. return err
  3002. }
  3003. func UpdateDeviceValusalMapVrr(patient_id int64, user_org_id int64) error {
  3004. 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
  3005. return err
  3006. }
  3007. func CreateDeviceValusalMapVrr(vrr models.XtDeviceValusalMapVrr) error {
  3008. err := XTWriteDB().Create(&vrr).Error
  3009. return err
  3010. }
  3011. func GetDialysisOrderNoExecution(start_nurse int64, user_org_id int64, timenow int64) (order []*models.XtDialysisOrder, err error) {
  3012. err = XTReadDB().Where("start_nurse = ? and user_org_id =? and status =1 and dialysis_date =?", start_nurse, user_org_id, timenow).Find(&order).Error
  3013. return order, err
  3014. }
  3015. func GetAdviceListNoExecution(advice_date int64, user_org_id int64, patientIds []int64) (advice []*models.NoDoctorAdvice, err error) {
  3016. db := XTReadDB().Model(advice).Where("status=1 and execution_state = 2 and advice_type = 2")
  3017. if len(patientIds) > 0 {
  3018. if advice_date > 0 {
  3019. db = db.Where("advice_date= ?", advice_date)
  3020. }
  3021. if user_org_id > 0 {
  3022. db = db.Where("user_org_id =?", user_org_id)
  3023. }
  3024. if len(patientIds) > 0 {
  3025. db = db.Where("patient_id in(?)", patientIds)
  3026. }
  3027. err = db.Select("patient_id,Count(id) as count,advice_date").Group("patient_id").Find(&advice).Error
  3028. return advice, err
  3029. } else {
  3030. return advice, err
  3031. }
  3032. }
  3033. func GetHisAdviceListNoExecution(advice_date int64, user_org_id int64, patientIds []int64) (advice []*models.HisNoDoctorAdvice, err error) {
  3034. db := XTReadDB().Model(advice).Where("status=1 and execution_state = 2")
  3035. if advice_date > 0 {
  3036. db = db.Where("advice_date= ?", advice_date)
  3037. }
  3038. if user_org_id > 0 {
  3039. db = db.Where("user_org_id =?", user_org_id)
  3040. }
  3041. if len(patientIds) > 0 {
  3042. db = db.Where("patient_id in(?)", patientIds)
  3043. }
  3044. err = db.Select("advice_date,patient_id,Count(id) as count").Group("patient_id").Find(&advice).Error
  3045. return advice, err
  3046. }
  3047. func GetProjectListNoExecution(record_date int64, user_org_id int64, patientIds []int64) (project []*models.NoHisPrescriptionProject, err error) {
  3048. db := XTReadDB().Model(&project).Where("status=1 and execution_state =0")
  3049. if record_date > 0 {
  3050. db = db.Where("record_date= ?", record_date)
  3051. }
  3052. if user_org_id > 0 {
  3053. db = db.Where("user_org_id =?", user_org_id)
  3054. }
  3055. if len(patientIds) > 0 {
  3056. db = db.Where("patient_id in(?)", patientIds)
  3057. }
  3058. err = db.Select("patient_id,Count(id) as count,record_date as advice_date").Group("patient_id").Find(&project).Error
  3059. return project, err
  3060. }
  3061. func GetPatientNoExecutionList(org_id int64, patientsIds []int64) (patient []*models.XtPatients, err error) {
  3062. err = XTReadDB().Where("user_org_id = ? and id in(?)", org_id, patientsIds).Find(&patient).Error
  3063. return patient, err
  3064. }
  3065. func FindHisConfig(org_id int64) (err error, config models.XtHisConfig) {
  3066. err = readDb.Model(&models.XtHisConfig{}).Where("status = 1 AND user_org_id = ?", org_id).First(&config).Error
  3067. return
  3068. }
  3069. func GetPatientDetailInformedConsent(patient_id int64, org_id int64) (models.XtPatients, error) {
  3070. patients := models.XtPatients{}
  3071. err := XTReadDB().Where("id = ? and status=1 and user_org_id =?", patient_id, org_id).Find(&patients).Error
  3072. return patients, err
  3073. }
  3074. func GetAllPrescriptionList(org_id int64) (prescription []*models.DialysisPrescription, err error) {
  3075. err = XTReadDB().Where("user_org_id = ? and status =1", org_id).Find(&prescription).Error
  3076. return prescription, err
  3077. }
  3078. func GetPatientScheudleListTwenty(org_id int64, scheduel_type int64, startime int64, endtime int64, schedule_date int64) (schedule []*models.ScheduleTen, err error) {
  3079. db := XTReadDB().Model(&schedule).Where("status= 1")
  3080. if org_id > 0 {
  3081. db = db.Where("user_org_id = ?", org_id)
  3082. }
  3083. if scheduel_type > 0 {
  3084. db = db.Where("schedule_type =?", scheduel_type)
  3085. }
  3086. if startime > 0 {
  3087. db = db.Where("schedule_date >=?", startime)
  3088. }
  3089. if endtime > 0 {
  3090. db = db.Where("schedule_date <=?", endtime)
  3091. }
  3092. if startime == 0 && endtime == 0 {
  3093. db = db.Where("schedule_date = ?", schedule_date)
  3094. }
  3095. err = db.Find(&schedule).Error
  3096. fmt.Println("err2330230320202023", err)
  3097. return schedule, err
  3098. }
  3099. func GetDialysisOrderCountTwentyEight(orgID int64, patient_id int64, recordDate int64) (models.VmDialysisOrder, error) {
  3100. order := models.VmDialysisOrder{}
  3101. err := XTReadDB().Raw("SELECT Count(id) as count,Max(dialysis_date) as dialysis_date from xt_dialysis_order where dialysis_date>=1672502400 and dialysis_date <= ? AND status = 1 AND stage = 2 AND patient_id = ? and user_org_id = ?", recordDate, patient_id, orgID).Scan(&order).Error
  3102. return order, err
  3103. }
  3104. func GetDialysisOrderCountTenEight(patient_id int64, recordDate int64) (models.VmDialysisOrder, error) {
  3105. order := models.VmDialysisOrder{}
  3106. err := XTReadDB().Where("patient_id = ? and dialysis_date = ? and status = 1", patient_id, recordDate).Last(&order).Error
  3107. return order, err
  3108. }
  3109. func GetLastAcceptRecrods(patient_id int64) (models.ReceiveTreatmentAsses, error) {
  3110. treatmentAsses := models.ReceiveTreatmentAsses{}
  3111. err := XTReadDB().Where("patient_id = ? and status= 1 and admission_number!=''", patient_id).Last(&treatmentAsses).Error
  3112. return treatmentAsses, err
  3113. }
  3114. func GetOutPrintList(user_org_id int64) (models.XtFallOutPrint, error) {
  3115. outPrint := models.XtFallOutPrint{}
  3116. err := XTReadDB().Where("user_org_id = ? and status =1", user_org_id).Find(&outPrint).Error
  3117. return outPrint, err
  3118. }
  3119. func CreateFallOutBedPrint(outPrint models.XtFallOutPrint) error {
  3120. err := XTWriteDB().Create(&outPrint).Error
  3121. return err
  3122. }
  3123. func UpdateFallOutBedPrint(id int64, content string) error {
  3124. err := XTWriteDB().Model(&models.XtFallOutPrint{}).Where("id = ? and status=1", id).Updates(map[string]interface{}{"content": content}).Error
  3125. return err
  3126. }
  3127. func GetFallOutBedPrintList(user_org_id int64) (models.XtFallOutPrint, error) {
  3128. outPrint := models.XtFallOutPrint{}
  3129. err := XTReadDB().Where("user_org_id = ? and status=1", user_org_id).Find(&outPrint).Error
  3130. return outPrint, err
  3131. }
  3132. func GetFilterInformed(user_org_id int64) (models.XtFilterInformed, error) {
  3133. informed := models.XtFilterInformed{}
  3134. err := XTReadDB().Where("user_org_id = ? and status =1", user_org_id).Find(&informed).Error
  3135. return informed, err
  3136. }
  3137. func CreateFilterInformed(informed models.XtFilterInformed) error {
  3138. err := XTWriteDB().Create(&informed).Error
  3139. return err
  3140. }
  3141. func UpdateFilterInformed(id int64, content string) error {
  3142. err := XTWriteDB().Model(&models.XtFilterInformed{}).Where("id = ? and status =1", id).Updates(map[string]interface{}{"content": content}).Error
  3143. return err
  3144. }
  3145. func GetProfundaInformed(user_org_id int64) (models.XtProfundaInformed, error) {
  3146. informed := models.XtProfundaInformed{}
  3147. err := XTReadDB().Where("user_org_id = ? and status =1", user_org_id).Find(&informed).Error
  3148. return informed, err
  3149. }
  3150. func CreateProfundaInformed(informed models.XtProfundaInformed) error {
  3151. err := XTWriteDB().Create(&informed).Error
  3152. return err
  3153. }
  3154. func UpdateProfundaInformed(id int64, content string) error {
  3155. profundaInformed := models.XtProfundaInformed{}
  3156. err := XTWriteDB().Model(&profundaInformed).Where("id = ? and status=1", id).Updates(map[string]interface{}{"content": content}).Error
  3157. return err
  3158. }
  3159. func GetArterOvenousList(user_org_id int64) (models.XtArterOvenous, error) {
  3160. ovenous := models.XtArterOvenous{}
  3161. err := XTReadDB().Where("user_org_id = ? and status=1", user_org_id).Find(&ovenous).Error
  3162. return ovenous, err
  3163. }
  3164. func CreateArterOvernous(ovenous models.XtArterOvenous) error {
  3165. err := XTWriteDB().Create(&ovenous).Error
  3166. return err
  3167. }
  3168. func UpdateArterOvernous(id int64, content string) error {
  3169. err := XTWriteDB().Model(&models.XtArterOvenous{}).Where("id = ? and status =1", id).Updates(map[string]interface{}{"content": content}).Error
  3170. return err
  3171. }
  3172. func GetHightRikInformed(user_org_id int64) (models.XtHightRisk, error) {
  3173. risk := models.XtHightRisk{}
  3174. err := XTReadDB().Where("user_org_id = ? and status =1", user_org_id).Find(&risk).Error
  3175. return risk, err
  3176. }
  3177. func CreateHighRik(risk models.XtHightRisk) error {
  3178. err := XTWriteDB().Create(&risk).Error
  3179. return err
  3180. }
  3181. func UpdateHighRik(id int64, content string) error {
  3182. err := XTWriteDB().Model(&models.XtHightRisk{}).Where("id = ? and status =1", id).Updates(map[string]interface{}{"content": content}).Error
  3183. return err
  3184. }
  3185. func GetAnticoagulation(user_org_id int64) (models.XtAuticoagutionPrint, error) {
  3186. auticoagutionPrint := models.XtAuticoagutionPrint{}
  3187. err := XTReadDB().Where("user_org_id = ? and status =1", user_org_id).Find(&auticoagutionPrint).Error
  3188. return auticoagutionPrint, err
  3189. }
  3190. func CreateAnticoagulation(auticoagutionPrint models.XtAuticoagutionPrint) error {
  3191. err := XTWriteDB().Create(&auticoagutionPrint).Error
  3192. return err
  3193. }
  3194. func UpdateAnticoagulation(id int64, content string) error {
  3195. err := XTWriteDB().Model(&models.XtAuticoagutionPrint{}).Where("id = ? and status =1", id).Updates(map[string]interface{}{"content": content}).Error
  3196. return err
  3197. }
  3198. func GetCriticalInfomed(org_id int64) (models.XtCriticalinfomedPrint, error) {
  3199. criticalinfomedPrint := models.XtCriticalinfomedPrint{}
  3200. err := XTReadDB().Where("user_org_id = ? and status=1", org_id).Find(&criticalinfomedPrint).Error
  3201. return criticalinfomedPrint, err
  3202. }
  3203. func CreatedCriticalInfomed(criticalinfomedPrint models.XtCriticalinfomedPrint) error {
  3204. err := XTWriteDB().Create(&criticalinfomedPrint).Error
  3205. return err
  3206. }
  3207. func UpdateCriticalInfomed(id int64, content string) error {
  3208. err := XTWriteDB().Model(&models.XtCriticalinfomedPrint{}).Where("id = ? and status=1", id).Updates(map[string]interface{}{"content": content}).Error
  3209. return err
  3210. }
  3211. func GetHemoperfusionInformed(user_org_id int64) (models.XtHemoperfusionmedPrint, error) {
  3212. hemoperfusionmedPrint := models.XtHemoperfusionmedPrint{}
  3213. err := XTReadDB().Where("user_org_id = ? and status =1", user_org_id).Find(&hemoperfusionmedPrint).Error
  3214. return hemoperfusionmedPrint, err
  3215. }
  3216. func CreateHemoperfusionInformed(hemoperfusionmedPrint models.XtHemoperfusionmedPrint) error {
  3217. err := XTWriteDB().Create(&hemoperfusionmedPrint).Error
  3218. return err
  3219. }
  3220. func UpdateHemoperfusionInformed(id int64, content string) error {
  3221. err := XTWriteDB().Model(&models.XtHemoperfusionmedPrint{}).Where("id = ? and status=1", id).Updates(map[string]interface{}{"content": content}).Error
  3222. return err
  3223. }
  3224. func GetBringInformed(org_id int64) (models.XtBringinformedPrint, error) {
  3225. bringinformedPrint := models.XtBringinformedPrint{}
  3226. err := XTReadDB().Where("user_org_id = ? and status=1", org_id).Find(&bringinformedPrint).Error
  3227. return bringinformedPrint, err
  3228. }
  3229. func CreateBringInformed(bringinformedPrint models.XtBringinformedPrint) error {
  3230. err := XTWriteDB().Create(&bringinformedPrint).Error
  3231. return err
  3232. }
  3233. func UpdateBringInformed(id int64, content string) error {
  3234. err := XTWriteDB().Model(&models.XtBringinformedPrint{}).Where("id =? and status= 1", id).Updates(map[string]interface{}{"content": content}).Error
  3235. return err
  3236. }
  3237. func GetCatheterization(user_org_id int64) (models.XtCathetercationPrint, error) {
  3238. cathetercationPrint := models.XtCathetercationPrint{}
  3239. err := XTReadDB().Where("user_org_id = ? and status= 1", user_org_id).Find(&cathetercationPrint).Error
  3240. return cathetercationPrint, err
  3241. }
  3242. func CreateCatheteriztion(cathetercationPrint models.XtCathetercationPrint) error {
  3243. err := XTWriteDB().Create(&cathetercationPrint).Error
  3244. return err
  3245. }
  3246. func UpdateCatheterization(id int64, content string) error {
  3247. err := XTWriteDB().Model(models.XtCathetercationPrint{}).Where("id=? and status =1", id).Updates(map[string]interface{}{"content": content}).Error
  3248. return err
  3249. }
  3250. func GetDialyzerInformed(org_id int64) (models.XtDialyszerinformedPrint, error) {
  3251. dialyszerinformedPrint := models.XtDialyszerinformedPrint{}
  3252. err := XTReadDB().Where("user_org_id = ? and status=1", org_id).Find(&dialyszerinformedPrint).Error
  3253. return dialyszerinformedPrint, err
  3254. }
  3255. func CreateDialyzerInformed(dialyszerinformedPrint models.XtDialyszerinformedPrint) error {
  3256. err := XTWriteDB().Create(&dialyszerinformedPrint).Error
  3257. return err
  3258. }
  3259. func UpdateDialyszerInformed(id int64, content string) error {
  3260. err := XTWriteDB().Model(&models.XtDialyszerinformedPrint{}).Where("id = ? and status=1", id).Updates(map[string]interface{}{"content": content}).Error
  3261. return err
  3262. }
  3263. func GetTheapyInformed(org_id int64) (models.XtTheapyinformedPrint, error) {
  3264. theapyinformedPrint := models.XtTheapyinformedPrint{}
  3265. err := XTReadDB().Where("user_org_id = ? and status=1", org_id).Find(&theapyinformedPrint).Error
  3266. return theapyinformedPrint, err
  3267. }
  3268. func CreateTheapyInformed(theapyinformedPrint models.XtTheapyinformedPrint) error {
  3269. err := XTWriteDB().Create(&theapyinformedPrint).Error
  3270. return err
  3271. }
  3272. func UpdateTheaphInformed(id int64, content string) error {
  3273. err := XTWriteDB().Model(&models.XtTheapyinformedPrint{}).Where("id = ? and status =1", id).Updates(map[string]interface{}{"content": content}).Error
  3274. return err
  3275. }
  3276. func GetPatientMedicalHistory(patient_id int64, user_org_id int64) (models.XtMedicalHistory, error) {
  3277. history := models.XtMedicalHistory{}
  3278. err := XTReadDB().Model(&history).Where("patient_id = ? and user_org_id = ? and status=1", patient_id, user_org_id).Find(&history).Error
  3279. return history, err
  3280. }
  3281. func UpdatePatientMedicalHistory(id int64, content string) error {
  3282. history := models.XtMedicalHistory{}
  3283. err := XTWriteDB().Model(&history).Where("id = ? and status=1", id).Updates(map[string]interface{}{"content": content}).Error
  3284. return err
  3285. }
  3286. func CreatePatientMedicalHistory(history models.XtMedicalHistory) error {
  3287. err := XTWriteDB().Create(&history).Error
  3288. return err
  3289. }
  3290. func GetPatientMedicalList(patient_id int64, user_org_id int64) (models.XtMedicalHistory, error) {
  3291. history := models.XtMedicalHistory{}
  3292. err := XTReadDB().Where("patient_id = ? and user_org_id = ? and status=1", patient_id, user_org_id).Find(&history).Error
  3293. return history, err
  3294. }
  3295. func CreatePatientFallassessment(tx *gorm.DB, patientFallassessment models.XtPatientFallassessment) (err error) {
  3296. err = tx.Create(&patientFallassessment).Error
  3297. if err == gorm.ErrRecordNotFound {
  3298. tx.Rollback()
  3299. return
  3300. }
  3301. return
  3302. }
  3303. func SavePatientFallassessment(tx *gorm.DB, patientFallassessment models.XtPatientFallassessment) (err error) {
  3304. err = tx.Save(&patientFallassessment).Error
  3305. if err == gorm.ErrRecordNotFound {
  3306. tx.Rollback()
  3307. return
  3308. }
  3309. return
  3310. }
  3311. func GetPatientFallsessMentList(patient_id int64, user_org_id int64, limit int64, page int64) (list []*models.XtPatientFallassessment, total int64, err error) {
  3312. offset := (page - 1) * limit
  3313. db := XTReadDB().Model(&list).Where("status =1")
  3314. if patient_id > 0 {
  3315. db = db.Where("patient_id = ?", patient_id)
  3316. }
  3317. if user_org_id > 0 {
  3318. db = db.Where("user_org_id = ?", user_org_id)
  3319. }
  3320. err = db.Count(&total).Offset(offset).Limit(limit).Find(&list).Error
  3321. return list, total, err
  3322. }
  3323. func GetPatientFallsessMentListOne(ids []string, user_org_id int64) (list []*models.XtPatientFallassessment, err error) {
  3324. db := XTReadDB().Model(&list).Where("status =1")
  3325. if len(ids) > 0 {
  3326. db = db.Where("id in(?)", ids)
  3327. }
  3328. if user_org_id > 0 {
  3329. db = db.Where("user_org_id = ?", user_org_id)
  3330. }
  3331. err = db.Find(&list).Error
  3332. return list, err
  3333. }
  3334. func GetPatientFallsessmentById(id int64, user_org_id int64) (models.XtPatientFallassessment, error) {
  3335. fallassessment := models.XtPatientFallassessment{}
  3336. err := XTReadDB().Where("id = ? and user_org_id = ?", id, user_org_id).Find(&fallassessment).Error
  3337. return fallassessment, err
  3338. }
  3339. func DeletePatientFallsessment(id int64, user_org_id int64) (models.XtPatientFallassessment, error) {
  3340. fallassessment := models.XtPatientFallassessment{}
  3341. err := XTWriteDB().Model(&fallassessment).Where("id =? and status=1", id).Updates(map[string]interface{}{"status": 0}).Error
  3342. return fallassessment, err
  3343. }
  3344. func CreatePadFallssessment(fallssessment models.XtPatientPedFallssessment) error {
  3345. err := XTWriteDB().Create(&fallssessment).Error
  3346. return err
  3347. }
  3348. func UpdatePedFallssessment(fallssessment models.XtPatientPedFallssessment) error {
  3349. err := XTWriteDB().Save(&fallssessment).Error
  3350. return err
  3351. }
  3352. func GetBedDiatricFallassessmentList(patient_id int64, is_type int64, user_org_id int64, limit int64, page int64) (list []*models.XtPatientPedFallssessment, total int64, err error) {
  3353. offset := (page - 1) * limit
  3354. db := XTReadDB().Model(&list).Where("status =1")
  3355. if patient_id > 0 {
  3356. db = db.Where("patient_id = ?", patient_id)
  3357. }
  3358. if user_org_id > 0 {
  3359. db = db.Where("user_org_id = ?", user_org_id)
  3360. }
  3361. if is_type > 0 {
  3362. db = db.Where("type = ?", is_type)
  3363. }
  3364. err = db.Count(&total).Offset(offset).Limit(limit).Find(&list).Error
  3365. return list, total, err
  3366. }
  3367. func GetPedPatientFallassessmentById(id int64) (models.XtPatientPedFallssessment, error) {
  3368. fallassessment := models.XtPatientPedFallssessment{}
  3369. err := XTReadDB().Where("id = ? and status=1", id).Find(&fallassessment).Error
  3370. return fallassessment, err
  3371. }
  3372. func DeletePadPatientFallassment(id int64) (err error) {
  3373. err = XTWriteDB().Model(&models.XtPatientPedFallssessment{}).Where("id=? and status=1", id).Updates(map[string]interface{}{"status": 0}).Error
  3374. return err
  3375. }
  3376. func GetPatientCritaicalInformed(user_org_id int64) (models.XtPatientCritical, error) {
  3377. patientCritical := models.XtPatientCritical{}
  3378. err := XTReadDB().Where("user_org_id = ? and status=1", user_org_id).Find(&patientCritical).Error
  3379. return patientCritical, err
  3380. }
  3381. func CreatePatientCritaicalinformed(critical models.XtPatientCritical) error {
  3382. err = XTWriteDB().Create(&critical).Error
  3383. return err
  3384. }
  3385. func UpdatePatientCritaicalInformed(id int64, content string) error {
  3386. err := XTWriteDB().Model(&models.XtPatientCritical{}).Where("id = ? and status=1", id).Updates(map[string]interface{}{"content": content}).Error
  3387. return err
  3388. }
  3389. func GetPatientCriticalByUserOrgId(user_org_id int64) (models.XtPatientCriticalOne, error) {
  3390. criticalOne := models.XtPatientCriticalOne{}
  3391. err := XTReadDB().Where("user_org_id = ? and status=1", user_org_id).Find(&criticalOne).Error
  3392. return criticalOne, err
  3393. }
  3394. func CreatePatientCritical(patient models.XtPatientCriticalOne) error {
  3395. err := XTWriteDB().Create(&patient).Error
  3396. return err
  3397. }
  3398. func UpdatePatientCriticalOne(user_org_id int64, content string) error {
  3399. err := XTWriteDB().Model(&models.XtPatientCriticalOne{}).Where("user_org_id = ? and status=1", user_org_id).Updates(map[string]interface{}{"content": content}).Error
  3400. return err
  3401. }
  3402. func GetPatientCriticalOneList(user_org_id int64) (models.XtPatientCriticalOne, error) {
  3403. criticalOne := models.XtPatientCriticalOne{}
  3404. err := XTReadDB().Where("user_org_id = ? and status =1", user_org_id).Find(&criticalOne).Error
  3405. return criticalOne, err
  3406. }
  3407. func CreatePatientPedList(fallssessment models.XtPatientPedFallssessment) error {
  3408. err := XTWriteDB().Create(&fallssessment).Error
  3409. return err
  3410. }
  3411. func UpdatePatientPedList(fallssessment models.XtPatientPedFallssessment) error {
  3412. err := XTWriteDB().Save(&fallssessment).Error
  3413. return err
  3414. }
  3415. func GetPatientFallsessMentListTwo(ids []string, user_org_id int64) (list []*models.XtPatientFallassessment, err error) {
  3416. db := XTReadDB().Model(&list).Where("status =1")
  3417. if len(ids) > 0 {
  3418. db = db.Where("id in(?)", ids)
  3419. }
  3420. if user_org_id > 0 {
  3421. db = db.Where("user_org_id = ?", user_org_id)
  3422. }
  3423. err = db.Find(&list).Error
  3424. return list, err
  3425. }
  3426. func CeatePatientSign(braden models.XtPatientBraden) error {
  3427. err := XTWriteDB().Create(&braden).Error
  3428. return err
  3429. }
  3430. func GetPatientBradenList(patient_id int64, limit int64, page int64, user_org_id int64) (list []*models.XtPatientBraden, total int64, err error) {
  3431. offset := (page - 1) * limit
  3432. db := XTReadDB().Model(&list).Where("status =1")
  3433. if patient_id > 0 {
  3434. db = db.Where("patient_id = ?", patient_id)
  3435. }
  3436. if user_org_id > 0 {
  3437. db = db.Where("user_org_id = ?", user_org_id)
  3438. }
  3439. err = db.Count(&total).Offset(offset).Limit(limit).Find(&list).Error
  3440. return list, total, err
  3441. }
  3442. func GetPatientBradenById(id int64) (models.XtPatientBraden, error) {
  3443. patientBraden := models.XtPatientBraden{}
  3444. err := XTReadDB().Where("id = ? and status=1", id).Find(&patientBraden).Error
  3445. return patientBraden, err
  3446. }
  3447. func SavePatientBraden(braden models.XtPatientBraden) error {
  3448. err := XTWriteDB().Save(&braden).Error
  3449. return err
  3450. }
  3451. func DeletePatientBraden(id int64) error {
  3452. patientBraden := models.XtPatientBraden{}
  3453. err := XTWriteDB().Model(&patientBraden).Where("id = ? and status=1", id).Updates(map[string]interface{}{"status": 0}).Error
  3454. return err
  3455. }
  3456. func GetBatchPatientBraden(ids []string, user_org_id int64) (list []*models.XtPatientBraden, err error) {
  3457. db := XTReadDB().Model(&list).Where("status=1")
  3458. if len(ids) > 0 {
  3459. db = db.Where("id in(?)", ids)
  3460. }
  3461. if user_org_id > 0 {
  3462. db = db.Where("user_org_id =?", user_org_id)
  3463. }
  3464. err = db.Find(&list).Error
  3465. return list, err
  3466. }
  3467. func GetAllPatientListMap(user_org_id int64) (list []*models.XtAgePatients, err error) {
  3468. err = XTReadDB().Where("user_org_id =? and status=1 and lapseto = 1", user_org_id).Find(&list).Error
  3469. return list, err
  3470. }
  3471. func GetLastBefor(patient_id int64) (models.XtAssessmentBeforeDislysis, error) {
  3472. beforeDislysis := models.XtAssessmentBeforeDislysis{}
  3473. err := XTReadDB().Where("patient_id = ? and status=1 AND assessment_date>=1704038400 AND assessment_date<=1711814400", patient_id).Order("id asc").Last(&beforeDislysis).Limit(1).Error
  3474. return beforeDislysis, err
  3475. }
  3476. func GetLastRecordAfter(patient_id int64) (models.XtAssessmentAfterDislysis, error) {
  3477. afterDislysis := models.XtAssessmentAfterDislysis{}
  3478. err := XTReadDB().Where("patient_id = ? and status=1 AND assessment_date>=1704038400 AND assessment_date<=1711814400", patient_id).Order("id asc").Last(&afterDislysis).Limit(1).Error
  3479. return afterDislysis, err
  3480. }
  3481. func GetInspectionByPatientId(patient_id int64, item_id int64) (models.XtInspection, error) {
  3482. inspection := models.XtInspection{}
  3483. err := XTReadDB().Where("patient_id =? and status =1 and inspect_date>=1704038400 and inspect_date<=1711814400 and item_id =?", patient_id, item_id).Order("id asc").Last(&inspection).Limit(1).Error
  3484. return inspection, err
  3485. }
  3486. func DeleteSolutionById(id int64) (models.DialysisSolution, error) {
  3487. solution := models.DialysisSolution{}
  3488. err := XTWriteDB().Model(&solution).Where("id=? and status=1", id).Updates(map[string]interface{}{"status": 12}).Error
  3489. return solution, err
  3490. }
  3491. func GetPatientBeforRecordList(patient_id int64, user_org_id int64) (befor []*models.XtAssessmentBeforeDislysis, err error) {
  3492. err = XTReadDB().Where("patient_id = ? and user_org_id =? and status=1", patient_id, user_org_id).Order("id desc").Limit(5).Find(&befor).Error
  3493. return befor, err
  3494. }
  3495. func GetPatientAfterRecordList(patient_id int64, user_org_id int64) (after []*models.XtAssessmentAfterDislysis, err error) {
  3496. err = XTReadDB().Where("patient_id = ? and user_org_id =? and status=1", patient_id, user_org_id).Order("id desc").Limit(5).Find(&after).Error
  3497. return after, err
  3498. }
  3499. func GetMonitorRecordByGroup(patient_id int64, user_org_id int64) (monitor []*models.MonitoringRecordOne, err error) {
  3500. err = XTReadDB().Where("patient_id = ? and user_org_id =? and status=1 and monitoring_date>=1711900800", patient_id, user_org_id).Order("id desc").Preload("MonitoringRecord", "status=1 and user_org_id =? and monitoring_date>=1711900800", user_org_id).Group("monitoring_date").Limit(5).Find(&monitor).Error
  3501. return monitor, err
  3502. }
  3503. func GetMonitorRecordList(patient_id int64, monitor_date int64, user_org_id int64) (monitor []*models.MonitoringRecord, err error) {
  3504. err = XTReadDB().Where("patient_id = ? and user_org_id =? and monitoring_date = ? and status =1", patient_id, user_org_id, monitor_date).Find(&monitor).Error
  3505. return monitor, err
  3506. }
  3507. func GetDialysisTotalCountByTime(user_org_id int64, start_time int64, end_time int64, patient_id int64) (order []*models.XtDialysisOrder, err error) {
  3508. db := XTReadDB().Table("xt_dialysis_order").Where("status =1")
  3509. if user_org_id > 0 {
  3510. db = db.Where("user_org_id =?", user_org_id)
  3511. }
  3512. if start_time > 0 {
  3513. db = db.Where("dialysis_date >=?", start_time)
  3514. }
  3515. if end_time > 0 {
  3516. db = db.Where("dialysis_date <=?", end_time)
  3517. }
  3518. if patient_id > 0 {
  3519. db = db.Where("patient_id = ?", patient_id)
  3520. }
  3521. err = db.Find(&order).Error
  3522. return order, err
  3523. }
  3524. func GetTreamentPland(user_org_id int64) (models.XtTreatmentPlan, error) {
  3525. treatmentPlan := models.XtTreatmentPlan{}
  3526. err := XTReadDB().Where("user_org_id = ? and status =1", user_org_id).Find(&treatmentPlan).Error
  3527. return treatmentPlan, err
  3528. }
  3529. func CreateTreatqmentPland(plan models.XtTreatmentPlan) error {
  3530. err := XTWriteDB().Create(&plan).Error
  3531. return err
  3532. }
  3533. func UpdateTheatmentPland(id int64, content string) error {
  3534. err := XTWriteDB().Model(&models.XtTreatmentPlan{}).Where("id = ? and status =1", id).Updates(map[string]interface{}{"content": content}).Error
  3535. return err
  3536. }
  3537. func GetEditAdviceActionList(id int64, org_id int64) (models.XtDoctorAdviceOne, error) {
  3538. doctorAdviceOne := models.XtDoctorAdviceOne{}
  3539. err := XTReadDB().Where("id = ? and status=1 and user_org_id = ?", id, org_id).Find(&doctorAdviceOne).Error
  3540. return doctorAdviceOne, err
  3541. }
  3542. func CreatePatientRecord(record models.XtPatientLaspseRecord) error {
  3543. err := XTWriteDB().Create(&record).Error
  3544. return err
  3545. }
  3546. func GetPatientLapseRecord(patient_id int64, limit int64, page int64, user_org_id int64) (record []*models.XtPatientLaspseRecord, total int64, err error) {
  3547. db := XTReadDB().Model(&record).Where("status=1")
  3548. offset := (page - 1) * limit
  3549. if patient_id > 0 {
  3550. db = db.Where("patient_id = ?", patient_id)
  3551. }
  3552. if user_org_id > 0 {
  3553. db = db.Where("user_org_id = ?", user_org_id)
  3554. }
  3555. err = db.Count(&total).Offset(offset).Limit(limit).Order("record_date desc").Find(&record).Error
  3556. return record, total, err
  3557. }
  3558. func GetPatientLapsorecord(id int64) (models.XtPatientLaspseRecord, error) {
  3559. laspseRecord := models.XtPatientLaspseRecord{}
  3560. err := XTReadDB().Where("id = ? and status= 1", id).Find(&laspseRecord).Error
  3561. return laspseRecord, err
  3562. }
  3563. func SavePatientLapseRecord(record models.XtPatientLaspseRecord) error {
  3564. err := XTWriteDB().Save(&record).Error
  3565. return err
  3566. }
  3567. func DeletePatientLapseRecord(id int64) error {
  3568. record := models.XtPatientLaspseRecord{}
  3569. err := XTWriteDB().Model(&record).Where("id = ? and status=1", id).Updates(map[string]interface{}{"status": 0}).Error
  3570. return err
  3571. }
  3572. func GetPatientLapsorecordListOne() (lapseto []*models.PatientLapseto, err error) {
  3573. err = XTReadDB().Where("status=1 and (lapseto_type=2 or lapseto_type =3)").Find(&lapseto).Error
  3574. return lapseto, err
  3575. }
  3576. func GetPatientBefor(patient_id int64, record_date int64, user_org_id int64) (models.XtAssessmentBeforeDislysis, error) {
  3577. beforeDislysis := models.XtAssessmentBeforeDislysis{}
  3578. err := XTReadDB().Where("patient_id = ? and assessment_date= ? and user_org_id = ? and status=1", patient_id, record_date, user_org_id).Find(&beforeDislysis).Error
  3579. return beforeDislysis, err
  3580. }
  3581. func GetDialysisPrescribeOneList(patient_id int64, record_date int64, user_org_id int64) (models.DialysisPrescription, error) {
  3582. prescription := models.DialysisPrescription{}
  3583. err := XTReadDB().Where("patient_id = ? and record_date= ? and user_org_id = ? and status=1", patient_id, record_date, user_org_id).Find(&prescription).Error
  3584. return prescription, err
  3585. }
  3586. func CreateDeathSummary(summary models.XtDeathSummary) error {
  3587. err := XTWriteDB().Save(&summary).Error
  3588. return err
  3589. }
  3590. func GetPatientDeathSummary(patient_id int64, user_org_id int64) (models.XtDeathSummary, error) {
  3591. deathSummary := models.XtDeathSummary{}
  3592. err := XTReadDB().Where("patient_id = ? and user_org_id = ? and status=1", patient_id, user_org_id).Find(&deathSummary).Error
  3593. return deathSummary, err
  3594. }
  3595. func DeletePatientDeathSummary(id int64) error {
  3596. summary := models.XtDeathSummary{}
  3597. err := XTWriteDB().Model(&summary).Where("id = ? and status=1", id).Updates(map[string]interface{}{"status": 0}).Error
  3598. return err
  3599. }
  3600. func CreateNewSickHistoryRecord(history models.XtPatientSickHistory) error {
  3601. err := XTWriteDB().Save(&history).Error
  3602. return err
  3603. }
  3604. func GetNewSickHistory(patient_id int64, org_id int64) (sick []*models.XtPatientSickHistory, err error) {
  3605. err = XTReadDB().Where("patient_id = ? and org_id = ? and status =1", patient_id, org_id).Order("record_time desc").Find(&sick).Error
  3606. return sick, err
  3607. }
  3608. func DeleteNewSickHistory(id int64) error {
  3609. err := XTWriteDB().Model(&models.XtPatientSickHistory{}).Where("id = ? and status=1", id).Updates(map[string]interface{}{"status": 0}).Error
  3610. return err
  3611. }
  3612. func CreateSickHistoryTemplate(template models.XtSickHistoryTemplate) error {
  3613. err := XTWriteDB().Create(&template).Error
  3614. return err
  3615. }
  3616. func GetSickHistoryTemplate(user_org_id int64) (template []*models.XtSickHistoryTemplate, err error) {
  3617. err = XTReadDB().Where("user_org_id = ? and status=1", user_org_id).Find(&template).Error
  3618. return template, err
  3619. }
  3620. func ModifySickHistoryTemplate(id int64, content string) error {
  3621. err := XTReadDB().Model(&models.XtSickHistoryTemplate{}).Where("id = ? and status=1", id).Updates(map[string]interface{}{"content": content}).Error
  3622. return err
  3623. }
  3624. func DeleteSickHistoryTemplate(id int64) error {
  3625. err := XTWriteDB().Model(&models.XtSickHistoryTemplate{}).Where("id = ? and status=1", id).Updates(map[string]interface{}{"status": 0}).Error
  3626. return err
  3627. }
  3628. func CreateNewCourseOfDiaseaseRecord(course models.PatientDiseaseCourse) error {
  3629. err := XTWriteDB().Save(&course).Error
  3630. return err
  3631. }
  3632. func GetPatientCoureseOfDiseasesList(patient_id int64, org_id int64) (list []*models.PatientDiseaseCourse, err error) {
  3633. err = XTReadDB().Where("patient_id = ? and org_id = ? and status=1", patient_id, org_id).Find(&list).Error
  3634. return list, err
  3635. }
  3636. func DeleteNewCourseOfDisease(id int64) error {
  3637. patientCourse := models.PatientDiseaseCourse{}
  3638. err := XTWriteDB().Model(&patientCourse).Where("id = ? and status=1", id).Updates(map[string]interface{}{"status": 0}).Error
  3639. return err
  3640. }
  3641. func CreateCourseTemplate(course models.XtPatientCourseTemplate) error {
  3642. err := XTWriteDB().Create(&course).Error
  3643. return err
  3644. }
  3645. func GetPatientDeathRecordList(org_id int64) (models.XtDeathRecord, error) {
  3646. deathRecord := models.XtDeathRecord{}
  3647. err := XTReadDB().Where("user_org_id = ? and status=1", org_id).Find(&deathRecord).Error
  3648. return deathRecord, err
  3649. }
  3650. func CreatePatientDeathRecord(record models.XtDeathRecord) error {
  3651. err := XTWriteDB().Create(&record).Error
  3652. return err
  3653. }
  3654. func UpdatePatientDeathRecord(id int64, content string) error {
  3655. err := XTWriteDB().Model(&models.XtDeathRecord{}).Where("id =? and status=1", id).Updates(map[string]interface{}{"content": content}).Error
  3656. return err
  3657. }
  3658. func GetPatientDeathRecord(user_org_id int64) (models.XtDeathRecord, error) {
  3659. record := models.XtDeathRecord{}
  3660. err := XTReadDB().Where("user_org_id = ? and status=1", user_org_id).Find(&record).Error
  3661. return record, err
  3662. }
  3663. func SaveCoureseTemplateTitle(courseTemplate models.XtPatientCourseTemplate) error {
  3664. err := XTWriteDB().Create(&courseTemplate).Error
  3665. return err
  3666. }
  3667. func GetPatientCourseOfTemplate(user_org_id int64) (list []*models.XtPatientCourseTemplate, err error) {
  3668. err = XTReadDB().Where("user_org_id = ? and status=1", user_org_id).Find(&list).Error
  3669. return list, err
  3670. }
  3671. func SaveCourseOfNewTemplate(id int64, content string) (models.XtPatientCourseTemplate, error) {
  3672. template := models.XtPatientCourseTemplate{}
  3673. err := XTWriteDB().Model(&template).Where("id = ? and status=1", id).Updates(map[string]interface{}{"content": content}).Error
  3674. return template, err
  3675. }
  3676. func DeleteCouseTemplate(id int64) error {
  3677. err := XTWriteDB().Model(&models.XtPatientCourseTemplate{}).Where("id = ? and status=1", id).Updates(map[string]interface{}{"status": 0}).Error
  3678. return err
  3679. }
  3680. func GetAutoDialysisPrescription(patient_id int64, start_time int64, end_time int64, org_id int64) (prescription []*models.XtAutoDialysisPrescription, err error) {
  3681. db := XTReadDB().Model(&prescription).Where("status=1")
  3682. if patient_id > 0 {
  3683. db = db.Where("patient_id = ?", patient_id)
  3684. }
  3685. if start_time > 0 {
  3686. db = db.Where("record_date>=?", start_time)
  3687. }
  3688. if end_time > 0 {
  3689. db = db.Where("record_date<=?", end_time)
  3690. }
  3691. if org_id > 0 {
  3692. db = db.Where("user_org_id = ?", org_id)
  3693. }
  3694. err = db.Find(&prescription).Error
  3695. return prescription, err
  3696. }
  3697. func GetDialysisPrescriptionGroup(patient_id int64, start_time int64, end_time int64, org_id int64) (list []*models.XtDialysisPrescriptionList, err error) {
  3698. err = XTReadDB().Where("patient_id = ? and record_date >=? and record_date <=? and user_org_id = ? and status=1", patient_id, start_time, end_time, org_id).Group("anticoagulant").Find(&list).Error
  3699. return list, err
  3700. }
  3701. func GetDialysisPrescriptionMaxAnticoagulant() {
  3702. }
  3703. func GetAutoMaxDialysisPrescription(patient_id int64, start_time int64, end_time int64, org_id int64) (models.XtDialysisPrescriptionListOne, error) {
  3704. prescription := models.XtDialysisPrescriptionListOne{}
  3705. err := XTReadDB().Raw("select MAX(anticoagulant_zongliang) as anticoagulant_zongliang from xt_dialysis_prescription where patient_id = ? and record_date>=? and record_date<=? and user_org_id = ?", patient_id, start_time, end_time, org_id).Scan(&prescription).Error
  3706. return prescription, err
  3707. }
  3708. func GetAutoMinDialysisPrescription(patient_id int64, start_time int64, end_time int64, org_id int64) (models.XtDialysisPrescriptionListOne, error) {
  3709. prescription := models.XtDialysisPrescriptionListOne{}
  3710. err := XTReadDB().Raw("select Min(anticoagulant_zongliang) as anticoagulant_zongliang from xt_dialysis_prescription where patient_id = ? and record_date>=? and record_date<=? and user_org_id = ?", patient_id, start_time, end_time, org_id).Scan(&prescription).Error
  3711. return prescription, err
  3712. }
  3713. func GetAutoAssessmentBefor(patient_id int64, start_time int64, end_time int64, org_id int64) (berfor []*models.BtAssessmentBeforeDislysis, err error) {
  3714. db := XTReadDB().Model(&berfor).Where("status=1")
  3715. if patient_id > 0 {
  3716. db = db.Where("patient_id = ?", patient_id)
  3717. }
  3718. if start_time > 0 {
  3719. db = db.Where("assessment_date>=?", start_time)
  3720. }
  3721. if end_time > 0 {
  3722. db = db.Where("assessment_date<=?", end_time)
  3723. }
  3724. if org_id > 0 {
  3725. db = db.Where("user_org_id = ?", org_id)
  3726. }
  3727. err = db.Find(&berfor).Error
  3728. return berfor, err
  3729. }
  3730. func GetAutoAfterWeight(patient_id int64, start_time int64, end_time int64, org_id int64) (after []*models.XtAssessmentAfterDislysis, err error) {
  3731. db := XTReadDB().Model(&after).Where("status=1")
  3732. if patient_id > 0 {
  3733. db = db.Where("patient_id = ?", patient_id)
  3734. }
  3735. if start_time > 0 {
  3736. db = db.Where("assessment_date>=?", start_time)
  3737. }
  3738. if end_time > 0 {
  3739. db = db.Where("assessment_date<=?", end_time)
  3740. }
  3741. if org_id > 0 {
  3742. db = db.Where("user_org_id = ?", org_id)
  3743. }
  3744. err = db.Find(&after).Error
  3745. return after, err
  3746. }
  3747. func GetLastAfterWeight(patient_id int64, assessment_date int64, user_org_id int64) (models.XtAssessmentAfterDislysis, error) {
  3748. afterDislysis := models.XtAssessmentAfterDislysis{}
  3749. err := XTReadDB().Where("patient_id = ? and assessment_date <? and user_org_id =? and status=1", patient_id, assessment_date, user_org_id).Order("assessment_date asc").Last(&afterDislysis).Error
  3750. return afterDislysis, err
  3751. }
  3752. func GetAutoMonitorList(patient_id int64, start_time int64, end_time int64, org_id int64) (monitor []*models.XTMonitoringRecord, err error) {
  3753. db := XTReadDB().Model(&monitor).Where("status=1")
  3754. if patient_id > 0 {
  3755. db = db.Where("patient_id = ?", patient_id)
  3756. }
  3757. if start_time > 0 {
  3758. db = db.Where("monitoring_date>=?", start_time)
  3759. }
  3760. if end_time > 0 {
  3761. db = db.Where("monitoring_date<=?", end_time)
  3762. }
  3763. if org_id > 0 {
  3764. db = db.Where("user_org_id = ?", org_id)
  3765. }
  3766. err = db.Find(&monitor).Error
  3767. return monitor, err
  3768. }
  3769. func MaxMonitorByBloodFlow(patient_id int64, start_time int64, end_time int64, org_id int64) (models.MonitoringRecord, error) {
  3770. monitoringRecord := models.MonitoringRecord{}
  3771. err := XTReadDB().Raw("select MAX(blood_flow_volume) as blood_flow_volume from xt_monitoring_record where patient_id = ? and monitoring_date>=? and monitoring_date<=? and user_org_id = ?", patient_id, start_time, end_time, org_id).Scan(&monitoringRecord).Error
  3772. return monitoringRecord, err
  3773. }
  3774. func GetAutoPatientDryWeight(patient_id int64, start_time int64, end_time int64, org_id int64) (dryweight []*models.SgjPatientDryweight, err error) {
  3775. db := XTReadDB().Model(&dryweight).Where("status=1")
  3776. if patient_id > 0 {
  3777. db = db.Where("patient_id = ?", patient_id)
  3778. }
  3779. if org_id > 0 {
  3780. db = db.Where("user_org_id = ?", org_id)
  3781. }
  3782. err = db.Order("id asc").Find(&dryweight).Error
  3783. return dryweight, err
  3784. }
  3785. func GetPatientLastDryWeightObj(patient_id int64, start_time int64, end_time int64, org_id int64) (models.SgjPatientDryweight, error) {
  3786. dryweight := models.SgjPatientDryweight{}
  3787. err := XTReadDB().Where("patient_id =? and user_org_id = ? and status=1", patient_id, org_id).Order("id asc").Last(&dryweight).Error
  3788. return dryweight, err
  3789. }
  3790. func GetAutoInspectionList(patient_id int64, start_time int64, end_time int64, org_id int64) (inpection []*models.XtInspection, err error) {
  3791. db := XTReadDB().Model(&inpection).Where("status=1")
  3792. if patient_id > 0 {
  3793. db = db.Where("patient_id = ?", patient_id)
  3794. }
  3795. if start_time > 0 {
  3796. db = db.Where("inspect_date>=?", start_time)
  3797. }
  3798. if end_time > 0 {
  3799. db = db.Where("inspect_date<=?", end_time)
  3800. }
  3801. if org_id > 0 {
  3802. db = db.Where("org_id = ?", org_id)
  3803. }
  3804. err = db.Find(&inpection).Error
  3805. return inpection, err
  3806. }
  3807. func GetPatientDialysisOrder(org_id int64) (order []*models.XtDialysisOrder, err error) {
  3808. err = XTReadDB().Where("user_org_id = ? and status=1 and dialysis_date<1733155200", org_id).Find(&order).Error
  3809. return order, err
  3810. }
  3811. func UpdateAssesmentAfeter(patient_id int64, dialysis_date int64, end_time int64) (models.XtAssessmentAfterDislysis, error) {
  3812. dialysisOrder := models.XtAssessmentAfterDislysis{}
  3813. err := XTWriteDB().Model(&dialysisOrder).Where("patient_id = ? and assessment_date =? and status=1", patient_id, dialysis_date).Updates(map[string]interface{}{"record_time": end_time}).Error
  3814. return dialysisOrder, err
  3815. }
  3816. func GetPatientRescueRecord(id int64) (models.XtPatientRescueRecord, error) {
  3817. rescueRecords := models.XtPatientRescueRecord{}
  3818. err := XTReadDB().Where("id = ? and status=1", id).Find(&rescueRecords).Error
  3819. return rescueRecords, err
  3820. }
  3821. func ModifyCoursePrint(id int64, content string) (models.XtPatientRescueRecord, error) {
  3822. rescueRecord := models.XtPatientRescueRecord{}
  3823. err := XTWriteDB().Model(&rescueRecord).Where("id = ? and status=1", id).Updates(map[string]interface{}{"content": content}).Error
  3824. return rescueRecord, err
  3825. }
  3826. func GetPatientRescureRecordList(id int64) (rescueRecord []*models.XtPatientRescueRecord, err error) {
  3827. err = XTWriteDB().Model(&rescueRecord).Where("id = ? and status=1", id).Find(&rescueRecord).Error
  3828. return rescueRecord, err
  3829. }
  3830. func GetAutoDialysisOrder(patient_id int64, start_time int64, end_time int64, user_org_id int64) (order []*models.XtDialysisOrder, err error) {
  3831. err = XTReadDB().Where("patient_id = ? and dialysis_date>=? and dialysis_date<=? and user_org_id = ? and status=1", patient_id, start_time, end_time, user_org_id).Find(&order).Error
  3832. return order, err
  3833. }
  3834. func GetMaxAssessmentBforSystolicBloodPressure(patient_id int64, start_time int64, end_time int64, org_id int64) (models.XtAssessmentBeforeDislysis, error) {
  3835. befor := models.XtAssessmentBeforeDislysis{}
  3836. err := XTReadDB().Raw("select MAX(systolic_blood_pressure) as systolic_blood_pressure from xt_assessment_before_dislysis where patient_id = ? and assessment_date>=? and assessment_date<=? and user_org_id = ?", patient_id, start_time, end_time, org_id).Scan(&befor).Error
  3837. return befor, err
  3838. }
  3839. func GetMminAssessmentBforSystolicBloodPressure(patient_id int64, start_time int64, end_time int64, org_id int64) (models.XtAssessmentBeforeDislysis, error) {
  3840. befor := models.XtAssessmentBeforeDislysis{}
  3841. err := XTReadDB().Raw("select Min(systolic_blood_pressure) as systolic_blood_pressure from xt_assessment_before_dislysis where patient_id = ? and assessment_date>=? and assessment_date<=? and user_org_id = ?", patient_id, start_time, end_time, org_id).Scan(&befor).Error
  3842. return befor, err
  3843. }
  3844. func GetMaxAssessmentBforDiastolicBloodPressure(patient_id int64, start_time int64, end_time int64, org_id int64) (models.XtAssessmentBeforeDislysis, error) {
  3845. befor := models.XtAssessmentBeforeDislysis{}
  3846. err := XTReadDB().Raw("select MAX(diastolic_blood_pressure) as diastolic_blood_pressure from xt_assessment_before_dislysis where patient_id = ? and assessment_date>=? and assessment_date<=? and user_org_id = ?", patient_id, start_time, end_time, org_id).Scan(&befor).Error
  3847. return befor, err
  3848. }
  3849. func GetMinAssessmentBforDiastolicBloodPressure(patient_id int64, start_time int64, end_time int64, org_id int64) (models.XtAssessmentBeforeDislysis, error) {
  3850. befor := models.XtAssessmentBeforeDislysis{}
  3851. err := XTReadDB().Raw("select Min(diastolic_blood_pressure) as diastolic_blood_pressure from xt_assessment_before_dislysis where patient_id = ? and assessment_date>=? and assessment_date<=? and user_org_id = ?", patient_id, start_time, end_time, org_id).Scan(&befor).Error
  3852. return befor, err
  3853. }
  3854. func GetMaxAssessmentAfterSystolicBloodPressure(patient_id int64, start_time int64, end_time int64, org_id int64) (models.XtAssessmentAfterDislysis, error) {
  3855. befor := models.XtAssessmentAfterDislysis{}
  3856. err := XTReadDB().Raw("select MAX(systolic_blood_pressure) as systolic_blood_pressure from xt_assessment_after_dislysis where patient_id = ? and assessment_date>=? and assessment_date<=? and user_org_id = ?", patient_id, start_time, end_time, org_id).Scan(&befor).Error
  3857. return befor, err
  3858. }
  3859. func GetMinAssessmentAfterSystolicBloodPressure(patient_id int64, start_time int64, end_time int64, org_id int64) (models.XtAssessmentAfterDislysis, error) {
  3860. befor := models.XtAssessmentAfterDislysis{}
  3861. err := XTReadDB().Raw("select Min(systolic_blood_pressure) as systolic_blood_pressure from xt_assessment_after_dislysis where patient_id = ? and assessment_date>=? and assessment_date<=? and user_org_id = ?", patient_id, start_time, end_time, org_id).Scan(&befor).Error
  3862. return befor, err
  3863. }
  3864. func GetMaxAssessmentAfterDiastolicBloodPressureList(patient_id int64, start_time int64, end_time int64, org_id int64) (models.XtAssessmentAfterDislysis, error) {
  3865. befor := models.XtAssessmentAfterDislysis{}
  3866. err := XTReadDB().Raw("select Max(diastolic_blood_pressure) as diastolic_blood_pressure from xt_assessment_after_dislysis where patient_id = ? and assessment_date>=? and assessment_date<=? and user_org_id = ?", patient_id, start_time, end_time, org_id).Scan(&befor).Error
  3867. return befor, err
  3868. }
  3869. func GetMinAssessmentAfterDiastolicBloodPressureList(patient_id int64, start_time int64, end_time int64, org_id int64) (models.XtAssessmentAfterDislysis, error) {
  3870. befor := models.XtAssessmentAfterDislysis{}
  3871. err := XTReadDB().Raw("select Min(diastolic_blood_pressure) as diastolic_blood_pressure from xt_assessment_after_dislysis where patient_id = ? and assessment_date>=? and assessment_date<=? and user_org_id = ?", patient_id, start_time, end_time, org_id).Scan(&befor).Error
  3872. return befor, err
  3873. }
  3874. func GetMaxMonitorSystolicBloodPressure(patient_id int64, start_time int64, end_time int64, org_id int64) (models.XTMonitoringRecord, error) {
  3875. monitor := models.XTMonitoringRecord{}
  3876. err := XTReadDB().Raw("select Min(diastolic_blood_pressure) as diastolic_blood_pressure from xt_monitoring_record where patient_id = ? and monitoring_date>=? and monitoring_date<=? and user_org_id = ?", patient_id, start_time, end_time, org_id).Scan(&monitor).Error
  3877. return monitor, err
  3878. }
  3879. func GetMinMonitorSystolicBloodPressure(patient_id int64, start_time int64, end_time int64, org_id int64) (models.XTMonitoringRecord, error) {
  3880. monitor := models.XTMonitoringRecord{}
  3881. err := XTReadDB().Raw("select Min(systolic_blood_pressure) as systolic_blood_pressure from xt_monitoring_record where patient_id = ? and monitoring_date>=? and monitoring_date<=? and user_org_id = ?", patient_id, start_time, end_time, org_id).Scan(&monitor).Error
  3882. return monitor, err
  3883. }
  3884. func GetMaxMonitorDiastolicBloodPressure(patient_id int64, start_time int64, end_time int64, org_id int64) (models.XTMonitoringRecord, error) {
  3885. monitor := models.XTMonitoringRecord{}
  3886. err := XTReadDB().Raw("select Max(diastolic_blood_pressure) as diastolic_blood_pressure from xt_monitoring_record where patient_id = ? and monitoring_date>=? and monitoring_date<=? and user_org_id = ?", patient_id, start_time, end_time, org_id).Scan(&monitor).Error
  3887. return monitor, err
  3888. }
  3889. func GetMinMonitorDiastolicBloodPressure(patient_id int64, start_time int64, end_time int64, org_id int64) (models.XTMonitoringRecord, error) {
  3890. monitor := models.XTMonitoringRecord{}
  3891. err := XTReadDB().Raw("select Min(diastolic_blood_pressure) as diastolic_blood_pressure from xt_monitoring_record where patient_id = ? and monitoring_date>=? and monitoring_date<=? and user_org_id = ?", patient_id, start_time, end_time, org_id).Scan(&monitor).Error
  3892. return monitor, err
  3893. }
  3894. func GetPatientVascularAccessList(patient_id int64, org_id int64) (models.XtPatientVascularAccess, error) {
  3895. vascularAccess := models.XtPatientVascularAccess{}
  3896. err := XTReadDB().Where("patient_id = ? and user_org_id = ? and status=1", patient_id, org_id).Last(&vascularAccess).Error
  3897. return vascularAccess, err
  3898. }
  3899. func GetPatientLastBefor(patient_id int64, start_time int64, end_time int64, org_id int64) (models.XtAssessmentBeforeDislysis, error) {
  3900. beforeDislysis := models.XtAssessmentBeforeDislysis{}
  3901. err := XTReadDB().Where("patient_id =? and user_org_id = ? and assessment_date>=? and assessment_date<=? and status=1 and complication!=''", patient_id, org_id, start_time, end_time).Order("id asc").Last(&beforeDislysis).Error
  3902. return beforeDislysis, err
  3903. }