patient_service.go 166KB

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