patient_service.go 168KB

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