patient_service.go 162KB

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