patient_service.go 157KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101
  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) (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 orgID > 0 {
  1212. db = db.Where("x.user_org_id=?", orgID)
  1213. }
  1214. if patientID > 0 {
  1215. db = db.Where("x.patient_id = ?", patientID)
  1216. }
  1217. if advice_type == 1 && advice_type > 0 {
  1218. db = db.Where("x.advice_type = ?", advice_type)
  1219. }
  1220. if advice_type == 2 && advice_type > 0 {
  1221. db = db.Where("x.advice_type = ?", advice_type)
  1222. }
  1223. if advice_type == 3 && advice_type > 0 {
  1224. db = db.Where("x.advice_type = 2 or x.advice_type = 3")
  1225. }
  1226. if advice_type == 4 && advice_type > 0 {
  1227. db = db.Where("x.advice_type = 4")
  1228. }
  1229. if stop == 1 {
  1230. db = db.Where("(x.stop_state=? or x.execution_state=?)", stop, stop)
  1231. } else if stop == 2 {
  1232. db = db.Where("x.stop_state=? and x.execution_state=?", stop, stop)
  1233. }
  1234. if start != 0 {
  1235. db = db.Where("x.start_time>=?", start)
  1236. }
  1237. if end != 0 {
  1238. db = db.Where("x.start_time<=?", end)
  1239. }
  1240. if len(keywords) > 0 {
  1241. likeKey := "%" + keywords + "%"
  1242. db = db.Where("x.advice_name LIKE ?", likeKey)
  1243. }
  1244. offset := (page - 1) * limit
  1245. err = db.Group("x.id").Count(&total).Offset(offset).Limit(limit).Select("x.id, x.user_org_id, x.patient_id, x.advice_type, x.advice_date, x.record_date, x.start_time, x.advice_name,x.advice_desc, x.reminder_date, x.drug_spec, x.drug_spec_unit, x.single_dose, x.single_dose_unit, x.prescribing_number, x.prescribing_number_unit, x.delivery_way, x.execution_frequency, x.advice_doctor, x.status, x.created_time,x.updated_time, x.advice_affirm, x.remark, x.stop_time, x.stop_reason, x.stop_doctor, x.stop_state, x.parent_id, x.execution_time, x.execution_staff, x.execution_state, x.checker, x.check_state, x.check_time, x.groupno,x.remind_type,x.frequency_type,x.day_count,x.week_day,x.parent_id,x.push_start_time,r.user_name, IF(x.parent_id > 0, x.parent_id, x.id) as advice_order").Joins("Left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.advice_doctor").Order("start_time desc, groupno desc, advice_order desc, id asc").Scan(&advices).Error
  1246. fmt.Print("err", err)
  1247. return
  1248. }
  1249. func GetDoctorAdviceListTwo(orgID int64, patientID int64, advice_type int64, stop int64, start int64, end int64, keywords string, limit int64, page int64) (advices []*models.DoctorAdvices, total int64, err error) {
  1250. db := readDb.Table("xt_doctor_advice as x").Where("x.status = 1")
  1251. table := UserReadDB().Table("sgj_user_admin_role as r")
  1252. fmt.Println(table)
  1253. if orgID > 0 {
  1254. db = db.Where("x.user_org_id=?", orgID)
  1255. }
  1256. if patientID > 0 {
  1257. db = db.Where("x.patient_id = ?", patientID)
  1258. }
  1259. if advice_type == 1 {
  1260. db = db.Where("x.advice_type = ?", advice_type)
  1261. }
  1262. if advice_type == 3 && advice_type > 0 {
  1263. db = db.Where("x.advice_type = 2 or x.advice_type = 3")
  1264. }
  1265. if advice_type == 4 && advice_type > 0 {
  1266. db = db.Where("x.advice_type = 4")
  1267. }
  1268. if stop == 1 {
  1269. db = db.Where("(x.stop_state=? or x.execution_state=?)", stop, stop)
  1270. } else if stop == 2 {
  1271. db = db.Where("x.stop_state=? and x.execution_state=?", stop, stop)
  1272. }
  1273. if start != 0 {
  1274. db = db.Where("x.start_time>=?", start)
  1275. }
  1276. if end != 0 {
  1277. db = db.Where("x.start_time<=?", end)
  1278. }
  1279. if len(keywords) > 0 {
  1280. likeKey := "%" + keywords + "%"
  1281. db = db.Where("x.advice_name LIKE ?", likeKey)
  1282. }
  1283. offset := (page - 1) * limit
  1284. 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
  1285. fmt.Print("错误是什么", err)
  1286. return
  1287. }
  1288. func GetDoctorAdvicePageList(orgID, patientID, advice_type, stop, start, end int64, keywords string, page, limit int64) (advices []*models.DoctorAdvice, total int64, err error) {
  1289. offset := (page - 1) * limit
  1290. db := readDb.Model(&models.DoctorAdvice{}).Where("status=1")
  1291. if orgID > 0 {
  1292. db = db.Where("user_org_id=?", orgID)
  1293. }
  1294. if patientID > 0 {
  1295. db = db.Where("patient_id = ?", patientID)
  1296. }
  1297. if advice_type > 0 {
  1298. db = db.Where("advice_type = ?", advice_type)
  1299. }
  1300. if stop == 1 {
  1301. db = db.Where("(stop_state=? or execution_state=?) and parent_id=0", stop, stop)
  1302. } else if stop == 2 {
  1303. db = db.Where("stop_state=? and execution_state=?", stop, stop)
  1304. }
  1305. if start != 0 {
  1306. db = db.Where("start_time>=?", start)
  1307. }
  1308. if end != 0 {
  1309. db = db.Where("start_time<=?", end)
  1310. }
  1311. if len(keywords) > 0 {
  1312. likeKey := "%" + keywords + "%"
  1313. db = db.Where("advice_name LIKE ?", likeKey)
  1314. }
  1315. 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
  1316. return
  1317. }
  1318. func CreateSubDoctorAdvice(advices []*models.DoctorAdvice) (err error) {
  1319. if len(advices) > 0 {
  1320. utx := writeDb.Begin()
  1321. if len(advices) > 0 {
  1322. 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 "
  1323. insertParams := make([]string, 0)
  1324. insertData := make([]interface{}, 0)
  1325. for _, advice := range advices {
  1326. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1327. insertData = append(insertData, advice.SingleDoseUnit)
  1328. insertData = append(insertData, advice.PrescribingNumber)
  1329. insertData = append(insertData, advice.PrescribingNumberUnit)
  1330. insertData = append(insertData, advice.AdviceName)
  1331. insertData = append(insertData, advice.AdviceDesc)
  1332. insertData = append(insertData, advice.SingleDose)
  1333. insertData = append(insertData, advice.CreatedTime)
  1334. insertData = append(insertData, advice.UpdatedTime)
  1335. insertData = append(insertData, advice.PatientId)
  1336. insertData = append(insertData, advice.ParentId)
  1337. insertData = append(insertData, advice.UserOrgId)
  1338. insertData = append(insertData, advice.RecordDate)
  1339. insertData = append(insertData, 2)
  1340. insertData = append(insertData, advice.DrugSpecUnit)
  1341. }
  1342. thisSQL += strings.Join(insertParams, ", ")
  1343. err = utx.Exec(thisSQL, insertData...).Error
  1344. if err != nil {
  1345. utx.Rollback()
  1346. return
  1347. }
  1348. }
  1349. utx.Commit()
  1350. }
  1351. return
  1352. }
  1353. func GetPatientDialysisRecord(orgID, patientID int64, page, limit, start, end, mode_id int64) ([]*models.PatientDialysisRecord, int64, error) {
  1354. offset := (page - 1) * limit
  1355. var total int64
  1356. var err error
  1357. var orders []*models.PatientDialysisRecord
  1358. // err = readDb.Table("xt_dialysis_order as do").
  1359. // Preload("DialysisPrescription", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  1360. // Preload("PredialysisEvaluation", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  1361. // Preload("AssessmentAfterDislysis", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  1362. // Preload("TreatmentSummary", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  1363. // 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).
  1364. // Joins("JOIN xt_device_zone as dz ON dz.org_id = ? and dz.id=s.partition_id", orgID).
  1365. // 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
  1366. db := readDb.Table("xt_dialysis_order as do").
  1367. Preload("DoctorAdvice", "patient_id =? and user_org_id = ? and status =1 and advice_type =2", patientID, orgID).
  1368. Preload("DialysisPrescription", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  1369. Preload("PredialysisEvaluation", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  1370. Preload("DialysisPrescription", func(db *gorm.DB) *gorm.DB {
  1371. return readDb.Where("patient_id=? and user_org_id=? and status=1", patientID, orgID).Preload("UserAdminRole", func(db *gorm.DB) *gorm.DB {
  1372. return readUserDb.Where("status = 1 and org_id=?", orgID)
  1373. })
  1374. }).
  1375. Preload("AssessmentAfterDislysis", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  1376. Preload("TreatmentSummary", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  1377. Preload("Device", "org_id=? and status=1", orgID).
  1378. Preload("UserAdminRole", func(db *gorm.DB) *gorm.DB {
  1379. return readUserDb.Where("org_id=?", orgID)
  1380. }).
  1381. 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).
  1382. Joins("JOIN xt_device_zone as dz ON dz.org_id = ? and dz.id=s.partition_id", orgID).
  1383. Where("do.patient_id=? and do.user_org_id=? and do.status=1 and s.status=1", patientID, orgID).Group("s.schedule_date")
  1384. if start != 0 {
  1385. db = db.Where("do.dialysis_date>=?", start)
  1386. }
  1387. if end != 0 {
  1388. db = db.Where("do.dialysis_date<=?", end)
  1389. }
  1390. if mode_id > 0 {
  1391. 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")
  1392. db = db.Where("dp.mode_id=?", mode_id)
  1393. }
  1394. 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
  1395. if len(orders) > 0 {
  1396. ids := make([]int64, 0)
  1397. for _, order := range orders {
  1398. dialyzer := order.DialysisPrescription.Dialyzer
  1399. ids = append(ids, dialyzer)
  1400. }
  1401. if len(ids) > 0 {
  1402. var dialyzers []*models.DeviceNumber
  1403. err = readDb.Model(&models.DeviceNumber{}).Where("id IN (?) and org_id=? and status=1", ids, orgID).Find(&dialyzers).Error
  1404. if err != nil {
  1405. return nil, 0, err
  1406. }
  1407. dialyzerMap := make(map[int64]models.DeviceNumber, 0)
  1408. for _, item := range dialyzers {
  1409. dialyzerMap[item.ID] = *item
  1410. }
  1411. for orderIndex, order := range orders {
  1412. if _, exist := dialyzerMap[order.DialysisPrescription.Dialyzer]; exist {
  1413. orders[orderIndex].DeviceNumber = dialyzerMap[order.DialysisPrescription.Dialyzer].Number
  1414. }
  1415. }
  1416. }
  1417. }
  1418. return orders, total, err
  1419. }
  1420. func GetPatientTreatmentSummaryList(orgID, patientID, page, limit, start, end int64) (list []*models.TreatmentSummary, total int, err error) {
  1421. offset := (page - 1) * limit
  1422. fmt.Println(offset)
  1423. fmt.Println(limit)
  1424. db := readDb.Model(&models.TreatmentSummary{}).Where("user_org_id = ? and patient_id=?", orgID, patientID)
  1425. if start != 0 {
  1426. db = db.Where("assessment_date >= ?", start)
  1427. }
  1428. if end != 0 {
  1429. db = db.Where("assessment_date <= ?", end)
  1430. }
  1431. db = db.Where("status=1")
  1432. err = db.Count(&total).Offset(offset).Limit(limit).Order("assessment_date desc, id desc").Find(&list).Error
  1433. return
  1434. }
  1435. func GetPatientScheduleList(orgID int64, patientID int64, page int64, limit int64, start int64) (schedules []*models.PatientSchedule, err error) {
  1436. offset := (page - 1) * limit
  1437. err = readDb.Table("xt_schedule as s").
  1438. Preload("DeviceZone", "org_id=? and status=1", orgID).
  1439. Preload("DeviceNumber", "org_id=? and status=1", orgID).
  1440. Preload("TreatmentMode", "status=1").
  1441. Where("s.patient_id =? and s.user_org_id=? and s.schedule_date>=? and s.status=1", patientID, orgID, start).
  1442. Limit(limit).Offset(offset).Find(&schedules).Error
  1443. return
  1444. }
  1445. func GetMonitorRecord(orgID int64, date int64, partition int64, patientId int64) ([]*models.NewVMMonitorDialysisSchedule, error) {
  1446. var mds []*models.NewVMMonitorDialysisSchedule
  1447. db := readDb.Model(&models.NewVMMonitorDialysisSchedule{})
  1448. if date > 0 {
  1449. db = db.Where("schedule_date = ?", date)
  1450. }
  1451. if partition > 0 {
  1452. db = db.Where("partition_id = ?", partition)
  1453. }
  1454. if patientId > 0 {
  1455. db = db.Where("patient_id = ?", patientId)
  1456. }
  1457. err = db.Where("status = 1 and user_org_id = ?", orgID).Find(&mds).Error
  1458. return mds, err
  1459. }
  1460. func MobileGetMonitorsWithPatient(orgID int64, keyword string, page int) ([]*models.MonitorDialysisSchedule, error) {
  1461. var patients []*models.Patients
  1462. 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
  1463. if getPatientErr != nil {
  1464. return nil, getPatientErr
  1465. }
  1466. patientIDs := make([]int64, len(patients))
  1467. for index, patient := range patients {
  1468. patientIDs[index] = patient.ID
  1469. }
  1470. db := readDb.
  1471. Model(&models.MonitorDialysisSchedule{}).
  1472. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  1473. Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  1474. Preload("TreatmentMode", "status = 1").
  1475. Preload("Prescription", "status = 1 AND user_org_id = ?", orgID).
  1476. Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ?", orgID).
  1477. Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ?", orgID).
  1478. Preload("MonitoringRecord", "status = 1 AND user_org_id = ?", orgID).
  1479. Preload("DialysisOrder", "status = 1 AND user_org_id = ?", orgID).
  1480. Preload("MonitorPatients", "status = 1 AND user_org_id = ?", orgID).
  1481. Where("status = 1 AND user_org_id = ? AND patient_id in (?)", orgID, patientIDs)
  1482. var schedules []*models.MonitorDialysisSchedule
  1483. err := db.Offset(20 * (page - 1)).Limit(20).Order("schedule_date desc").Find(&schedules).Error
  1484. return schedules, err
  1485. }
  1486. func GetPatientByKeyWord(orgID int64, keywords string) (patient []*models.Patients, err error) {
  1487. db := readDb.Model(&models.Patients{}).Where("user_org_id=? and status=1 and lapseto = 1", orgID)
  1488. if len(keywords) > 0 {
  1489. likekey := "%" + keywords + "%"
  1490. err = db.Where("name LIKE ? OR dialysis_no LIKE ? OR first_letter LIKE ?", likekey, likekey, likekey).Find(&patient).Error
  1491. } else {
  1492. err = db.Find(&patient).Error
  1493. }
  1494. return
  1495. }
  1496. func FindDoctorAdviceByGoroupNo(orgID int64, groupno int64) (advice models.DoctorAdvice, err error) {
  1497. err = readDb.Model(&models.DoctorAdvice{}).Where("user_org_id=? AND groupno = ? AND status = 1", orgID, groupno).First(&advice).Error
  1498. return
  1499. }
  1500. func FindOldDoctorAdvice(orgID int64, advice_id int64) (advice models.DoctorAdvice, err error) {
  1501. err = readDb.Model(&models.DoctorAdvice{}).Where("id = ? AND user_org_id=? AND status = 1", advice_id, orgID).First(&advice).Error
  1502. return
  1503. }
  1504. func UpdateAdviceGroupStartTime(orgID int64, groupNO int64, startTime int64, admin_user_id int64) error {
  1505. now := time.Now().Unix()
  1506. 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{}{
  1507. "start_time": startTime,
  1508. "updated_time": now,
  1509. "modifier": admin_user_id,
  1510. }).Error
  1511. return err
  1512. }
  1513. func QueryPatientById(id int64) *models.Patients {
  1514. var pat models.Patients
  1515. err := readDb.Model(&models.Patients{}).Where("id=?", id).First(&pat).Error
  1516. if err != nil {
  1517. }
  1518. return &pat
  1519. }
  1520. func FindAllDoctorAdviceByGoroupNo(orgID int64, groupno int64) (advice []models.DoctorAdvice, err error) {
  1521. err = readDb.Model(&models.DoctorAdvice{}).Where("user_org_id=? AND groupno = ? AND status = 1", orgID, groupno).Find(&advice).Error
  1522. return
  1523. }
  1524. func UpdateDoctorAdviceByGoroupNo(id int64, drug_id int64) error {
  1525. 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
  1526. return err
  1527. }
  1528. func FindAllDoctorAdviceByGoroupNoTwety(orgID int64, groupno int64) (advice []models.DoctorAdvice, err error) {
  1529. 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
  1530. return
  1531. }
  1532. func FindDoctorAdviceByIds(orgID int64, ids []string) (advice []models.DoctorAdvice, err error) {
  1533. err = readDb.Model(&models.DoctorAdvice{}).Where("id IN (?) AND user_org_id = ? AND status = 1", ids, orgID).Find(&advice).Error
  1534. return
  1535. }
  1536. func FindDoctorAdviceByIdsTwety(orgID int64, ids []string) (advice []models.DoctorAdvice, err error) {
  1537. 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
  1538. return
  1539. }
  1540. func GetBatchDoctorAdviceList(ids []string, user_org_id int64) (advice []*models.DoctorAdvice, err error) {
  1541. err = XTReadDB().Where("id in(?) and user_org_id = ? and status =1", ids, user_org_id).Find(&advice).Error
  1542. return advice, err
  1543. }
  1544. func BatchDeleteDoctorAdvice(ids []string, user_id int64) (err error) {
  1545. ut := writeDb.Begin()
  1546. 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
  1547. if err != nil {
  1548. ut.Rollback()
  1549. return
  1550. }
  1551. 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
  1552. if err != nil {
  1553. ut.Rollback()
  1554. return
  1555. }
  1556. ut.Commit()
  1557. return err
  1558. }
  1559. func FindAdviceByGoroupNo(orgID int64, groupno int64) (advice []models.DoctorAdvice, err error) {
  1560. err = readDb.Model(&models.DoctorAdvice{}).Where("user_org_id=? AND groupno = ? AND status = 1 AND parent_id = 0", orgID, groupno).Find(&advice).Error
  1561. return
  1562. }
  1563. func UpdateDoctorAdviceAndSubAdvice(m *models.DoctorAdvice) (err error) {
  1564. ut := writeDb.Begin()
  1565. err = ut.Save(m).Error
  1566. if err != nil {
  1567. ut.Rollback()
  1568. return
  1569. }
  1570. 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
  1571. if err != nil {
  1572. ut.Rollback()
  1573. return
  1574. }
  1575. ut.Commit()
  1576. return err
  1577. }
  1578. func GetAllWaitRemindPatient(org_id int64, page int64, limit int64) (total int64, patient []*models.Patients, err error) {
  1579. type Total struct {
  1580. Count int64
  1581. }
  1582. var totals Total
  1583. offset := (page - 1) * limit
  1584. 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
  1585. 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)
  1586. return totals.Count, patient, err
  1587. }
  1588. func UpdatePatientRemindStatus(patient_id int64, remind int64, org_id int64) (err error) {
  1589. 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
  1590. return
  1591. }
  1592. func CreatePatientWeightAdjust(m *models.SgjPatientDryweight) (err error) {
  1593. err = writeDb.Create(m).Error
  1594. return
  1595. }
  1596. func FindLastDryWeightAdjust(orgID int64, id int64) (weight models.SgjPatientDryweight, err error) {
  1597. err = readDb.Model(&models.SgjPatientDryweight{}).Where("user_org_id=? and patient_id=? and status=1", orgID, id).Order("id desc").First(&weight).Error
  1598. return
  1599. }
  1600. func GetSchedualPatientByKeyWord(orgID int64, keywords string, date int64) (patient []*models.Patients, err error) {
  1601. db := readDb.Model(&models.Patients{}).Where("user_org_id=? and status=1 and lapseto = 1 ", orgID)
  1602. if len(keywords) > 0 {
  1603. likekey := "%" + keywords + "%"
  1604. 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
  1605. } else {
  1606. err = db.Find(&patient).Error
  1607. }
  1608. return
  1609. }
  1610. func GetPatientScheduleOne(patientid int64, nowdate int64, orgid int64) (models.XtSchedule, error) {
  1611. schedule := models.XtSchedule{}
  1612. err := XTReadDB().Model(&schedule).Where("patient_id = ? and schedule_date = ? and user_org_id = ? and status =1", patientid, nowdate, orgid).Find(&schedule).Error
  1613. return schedule, err
  1614. }
  1615. func CreateExportPatient(patient *models.Patients, contagions []int64, org_creator int64) (err error) {
  1616. user, _ := GetSgjUserByMobild(patient.Phone)
  1617. customer, _ := GetSgjCoustomerByMobile(patient.UserOrgId, patient.Phone)
  1618. utx := writeDb.Begin()
  1619. btx := writeUserDb.Begin()
  1620. if user.ID == 0 {
  1621. user.Mobile = patient.Phone
  1622. user.Avatar = patient.Avatar
  1623. user.AvatarThumb = patient.Avatar
  1624. user.Birthday = patient.Birthday
  1625. user.Username = patient.Name
  1626. user.Gender = patient.Gender
  1627. user.Sources = 11
  1628. user.Introduce = patient.Remark
  1629. user.Status = 1
  1630. user.UpdatedTime = patient.UpdatedTime
  1631. user.CreatedTime = patient.CreatedTime
  1632. err = btx.Create(&user).Error
  1633. if err != nil {
  1634. utx.Rollback()
  1635. btx.Rollback()
  1636. return
  1637. }
  1638. }
  1639. patient.UserId = user.ID
  1640. if customer == nil {
  1641. err = btx.Create(&models.SgjCustomer{
  1642. UserOrgId: patient.UserOrgId,
  1643. UserId: user.ID,
  1644. Mobile: patient.Phone,
  1645. Name: patient.Name,
  1646. Gender: patient.Gender,
  1647. Birthday: patient.Birthday,
  1648. Sources: 11,
  1649. Status: 1,
  1650. CreatedTime: patient.CreatedTime,
  1651. UpdatedTime: patient.UpdatedTime,
  1652. Avatar: patient.Avatar,
  1653. Remark: patient.Remark,
  1654. }).Error
  1655. if err != nil {
  1656. utx.Rollback()
  1657. btx.Rollback()
  1658. return
  1659. }
  1660. }
  1661. err = utx.Create(patient).Error
  1662. if err != nil {
  1663. utx.Rollback()
  1664. btx.Rollback()
  1665. return
  1666. }
  1667. if patient.DryWeight > 0 {
  1668. var dryWeight models.SgjPatientDryweight
  1669. dryWeight.PatientId = patient.ID
  1670. dryWeight.UserOrgId = patient.UserOrgId
  1671. dryWeight.Status = 1
  1672. dryWeight.AdjustedValue = "/"
  1673. dryWeight.Creator = org_creator
  1674. dryWeight.UserId = org_creator
  1675. dryWeight.Ctime = time.Now().Unix()
  1676. dryWeight.Mtime = time.Now().Unix()
  1677. dryWeight.DryWeight = patient.DryWeight
  1678. dryWeight.Remakes = ""
  1679. err = utx.Create(&dryWeight).Error
  1680. }
  1681. var lapseto models.PatientLapseto
  1682. lapseto.PatientId = patient.ID
  1683. lapseto.LapsetoType = patient.Lapseto
  1684. lapseto.CreatedTime = patient.CreatedTime
  1685. lapseto.UpdatedTime = patient.CreatedTime
  1686. lapseto.Status = 1
  1687. lapseto.LapsetoTime = patient.CreatedTime
  1688. err = utx.Create(&lapseto).Error
  1689. if err != nil {
  1690. utx.Rollback()
  1691. btx.Rollback()
  1692. return
  1693. }
  1694. if len(contagions) > 0 {
  1695. thisSQL := "INSERT INTO xt_patients_infectious_diseases (patient_id, disease_id, status, created_time, updated_time) VALUES "
  1696. insertParams := make([]string, 0)
  1697. insertData := make([]interface{}, 0)
  1698. for _, contagion := range contagions {
  1699. insertParams = append(insertParams, "(?, ?, ?, ?, ?)")
  1700. insertData = append(insertData, patient.ID)
  1701. insertData = append(insertData, contagion)
  1702. insertData = append(insertData, 1)
  1703. insertData = append(insertData, patient.CreatedTime)
  1704. insertData = append(insertData, patient.UpdatedTime)
  1705. }
  1706. thisSQL += strings.Join(insertParams, ", ")
  1707. err = utx.Exec(thisSQL, insertData...).Error
  1708. if err != nil {
  1709. utx.Rollback()
  1710. btx.Rollback()
  1711. return
  1712. }
  1713. }
  1714. patientsNew := models.XtPatientsNew{
  1715. Name: patient.Name,
  1716. Gender: patient.Gender,
  1717. Phone: patient.Phone,
  1718. IdCardNo: patient.IdCardNo,
  1719. FirstDialysisDate: patient.FirstDialysisDate,
  1720. Source: patient.Source,
  1721. Lapseto: patient.Lapseto,
  1722. IsInfectious: patient.IsInfectious,
  1723. DialysisNo: patient.DialysisNo,
  1724. Height: patient.Height,
  1725. HomeAddress: patient.HomeAddress,
  1726. IsExcelExport: 1,
  1727. BloodPatients: 1,
  1728. Status: 1,
  1729. CreatedTime: time.Now().Unix(),
  1730. UserOrgId: patient.UserOrgId,
  1731. BloodId: patient.ID,
  1732. Avatar: "https://images.shengws.com/201809182128111.png",
  1733. }
  1734. err = utx.Create(&patientsNew).Error
  1735. if err != nil {
  1736. utx.Rollback()
  1737. btx.Rollback()
  1738. return
  1739. }
  1740. utx.Commit()
  1741. btx.Commit()
  1742. return
  1743. }
  1744. func FindPatientPhoneIsExist(phone string, org_id int64) (count int64) {
  1745. readDb.Model(&models.Patients{}).Where("user_org_id = ? AND phone = ? AND status = 1", org_id, phone).Count(&count)
  1746. return
  1747. }
  1748. func FindPatientIdCardNoIsExist(id_card_no string, org_id int64) (count int64) {
  1749. readDb.Model(&models.Patients{}).Where("user_org_id = ? AND id_card_no = ? AND status = 1", org_id, id_card_no).Count(&count)
  1750. return
  1751. }
  1752. func CreateExportErrLog(log *models.ExportErrLog) {
  1753. writeDb.Create(&log)
  1754. return
  1755. }
  1756. func FindPatientExportLog(org_id int64, export_time int64) (errLogs []*models.ExportErrLog, err error) {
  1757. err = readDb.Model(&models.ExportErrLog{}).Where("user_org_id = ? AND export_time = ? AND log_type = 1", org_id, export_time).Find(&errLogs).Error
  1758. return
  1759. }
  1760. func FindPatientExportLogOne(org_id int64, export_time int64) (errLogs []*models.ExportErrLog, err error) {
  1761. err = readDb.Model(&models.ExportErrLog{}).Where("user_org_id = ? AND export_time = ? AND log_type = 4", org_id, export_time).Find(&errLogs).Error
  1762. return
  1763. }
  1764. func FindPatientExportLogTwo(org_id int64, export_time int64) (errLogs []*models.ExportErrLog, err error) {
  1765. err = readDb.Model(&models.ExportErrLog{}).Where("user_org_id = ? AND export_time = ? AND log_type = 5", org_id, export_time).Find(&errLogs).Error
  1766. return
  1767. }
  1768. func FindPatientExportLogThree(org_id int64, export_time int64) (errLogs []*models.ExportErrLog, err error) {
  1769. err = readDb.Model(&models.ExportErrLog{}).Where("user_org_id = ? AND export_time = ? AND log_type = 6", org_id, export_time).Find(&errLogs).Error
  1770. return
  1771. }
  1772. func CreateExportLog(log *models.ExportLog) {
  1773. writeDb.Create(&log)
  1774. }
  1775. func UpdateDoctorEditAdvice(advice models.XtDoctorAdvice, orgid int64, groupno int64, date int64, patientid int64) error {
  1776. 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
  1777. return err
  1778. }
  1779. func GetPatientsByKey(orgID int64, keywords string) (patient []*models.Patients, err error) {
  1780. db := readDb.Model(&models.Patients{}).Where("user_org_id=? and status=1 and lapseto = 1 ", orgID)
  1781. if len(keywords) > 0 {
  1782. likekey := "%" + keywords + "%"
  1783. err = db.Where("(name LIKE ? OR dialysis_no LIKE ? OR first_letter LIKE ?)", likekey, likekey, likekey).Find(&patient).Error
  1784. } else {
  1785. err = db.Find(&patient).Error
  1786. }
  1787. return
  1788. }
  1789. func FindAllDoctorAdviceByGoroupNoTwo(orgID int64, groupno int64) (advice []models.DoctorAdvice, err error) {
  1790. err = readDb.Model(&models.DoctorAdvice{}).Where("user_org_id=? AND groupno = ? AND status = 1 AND execution_state = 2", orgID, groupno).Find(&advice).Error
  1791. return
  1792. }
  1793. func FindDoctorAdviceByIdsTwo(orgID int64, ids []string) (advice []models.DoctorAdvice, err error) {
  1794. 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
  1795. return
  1796. }
  1797. func CreateHisGroupAdvice(orgId int64, advices []*models.HisGroupAdvice, groupNo int64) (list []*models.HisGroupAdvice, err error) {
  1798. if groupNo <= 0 {
  1799. group := GetMaxAdviceGroupID(orgId)
  1800. groupNo = group + 1
  1801. }
  1802. tx := writeDb.Begin()
  1803. defer func() {
  1804. if r := recover(); r != nil {
  1805. tx.Rollback()
  1806. }
  1807. }()
  1808. for _, advice := range advices {
  1809. advice.Groupno = groupNo
  1810. if err = tx.Create(advice).Error; err != nil {
  1811. tx.Rollback()
  1812. return
  1813. }
  1814. list = append(list, advice)
  1815. if len(advice.Children) > 0 {
  1816. for _, child := range advice.Children {
  1817. child.Groupno = groupNo
  1818. child.ParentId = advice.ID
  1819. fmt.Println(child)
  1820. if err = tx.Create(&child).Error; err != nil {
  1821. tx.Rollback()
  1822. return
  1823. }
  1824. list = append(list, child)
  1825. }
  1826. }
  1827. }
  1828. tx.Commit()
  1829. return
  1830. }
  1831. func GetDialysisCount(orgid int64, partitionid int64) (order []*models.BloodDialysisOrderCount, err error) {
  1832. db := XTReadDB().Table("xt_dialysis_order as o")
  1833. if partitionid == 0 {
  1834. 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)
  1835. }
  1836. if partitionid > 0 {
  1837. 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)
  1838. }
  1839. return order, err
  1840. }
  1841. func UpdatePatientDialysisSolutionOne(patientid int64, orgid int64, prescription *models.DialysisPrescription, timenow int64) error {
  1842. 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
  1843. return err
  1844. }
  1845. //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) {
  1846. // 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
  1847. // return
  1848. //}
  1849. 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) {
  1850. 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
  1851. return
  1852. }
  1853. func UpdateScheduleByDeathTime(patientid int64, shcheduledate int64) error {
  1854. err := XTWriteDB().Model(models.XtSchedule{}).Where("patient_id = ? and schedule_date > ?", patientid, shcheduledate).Updates(map[string]interface{}{"status": 12}).Error
  1855. return err
  1856. }
  1857. func UpdateScheduleItemByPatientId(id int64) error {
  1858. err := XTWriteDB().Model(models.PatientScheduleTemplateItem{}).Where("patient_id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  1859. return err
  1860. }
  1861. func GetDoctorAdviceListThree(orgID, patientID, advice_type, stop, start, end int64, keywords string, page int64, limit int64) (advices []*models.HisDoctorAdvice, total int64, err error) {
  1862. db := readDb.Table("his_doctor_advice_info as x").Where("x.status = 1")
  1863. table := UserReadDB().Table("sgj_user_admin_role as r")
  1864. fmt.Print("table", table)
  1865. if orgID > 0 {
  1866. db = db.Where("x.user_org_id=?", orgID)
  1867. }
  1868. if patientID > 0 {
  1869. db = db.Where("x.patient_id = ?", patientID)
  1870. }
  1871. if advice_type == 1 && advice_type > 0 {
  1872. db = db.Where("x.advice_type = ?", advice_type)
  1873. }
  1874. if advice_type == 2 && advice_type > 0 {
  1875. db = db.Where("x.advice_type = ?", advice_type)
  1876. }
  1877. if advice_type == 3 && advice_type > 0 {
  1878. db = db.Where("x.advice_type = 2 or x.advice_type = 3")
  1879. }
  1880. if advice_type == 4 && advice_type > 0 {
  1881. db = db.Where("x.advice_type = 4")
  1882. }
  1883. if stop == 1 {
  1884. db = db.Where("(x.stop_state=? or x.execution_state=?)", stop, stop)
  1885. } else if stop == 2 {
  1886. db = db.Where("x.stop_state=? and x.execution_state=?", stop, stop)
  1887. }
  1888. if start != 0 {
  1889. db = db.Where("x.start_time>=?", start)
  1890. }
  1891. if end != 0 {
  1892. db = db.Where("x.start_time<=?", end)
  1893. }
  1894. if len(keywords) > 0 {
  1895. likeKey := "%" + keywords + "%"
  1896. db = db.Where("x.advice_name LIKE ?", likeKey)
  1897. }
  1898. offset := (page - 1) * limit
  1899. 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
  1900. fmt.Print("err", err)
  1901. return
  1902. }
  1903. func GetDoctorAdviceListFour(orgID int64, patientID int64, advice_type int64, stop int64, start int64, end int64, keywords string, limit int64, page int64) (advices []*models.HisDoctorAdvice, total int64, err error) {
  1904. db := readDb.Table("his_doctor_advice_info as x").Where("x.status = 1")
  1905. table := UserReadDB().Table("sgj_user_admin_role as r")
  1906. fmt.Println(table)
  1907. if orgID > 0 {
  1908. db = db.Where("x.user_org_id=?", orgID)
  1909. }
  1910. if patientID > 0 {
  1911. db = db.Where("x.patient_id = ?", patientID)
  1912. }
  1913. if advice_type == 1 {
  1914. db = db.Where("x.advice_type = ?", advice_type)
  1915. }
  1916. if advice_type == 3 && advice_type > 0 {
  1917. db = db.Where("x.advice_type = 2 or x.advice_type = 3")
  1918. }
  1919. if advice_type == 4 && advice_type > 0 {
  1920. db = db.Where("x.advice_type = 4")
  1921. }
  1922. if stop == 1 {
  1923. db = db.Where("(x.stop_state=? or x.execution_state=?)", stop, stop)
  1924. } else if stop == 2 {
  1925. db = db.Where("x.stop_state=? and x.execution_state=?", stop, stop)
  1926. }
  1927. if start != 0 {
  1928. db = db.Where("x.start_time>=?", start)
  1929. }
  1930. if end != 0 {
  1931. db = db.Where("x.start_time<=?", end)
  1932. }
  1933. if len(keywords) > 0 {
  1934. likeKey := "%" + keywords + "%"
  1935. db = db.Where("x.advice_name LIKE ?", likeKey)
  1936. }
  1937. offset := (page - 1) * limit
  1938. 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
  1939. fmt.Print("错误是什么", err)
  1940. return
  1941. }
  1942. func GetAllDeviceNumberByListSix(orgID int64) (number []*models.DeviceNumber, err error) {
  1943. err = readDb.Model(&models.DeviceNumber{}).Where("org_id=? and status=1", orgID).Find(&number).Error
  1944. return number, err
  1945. }
  1946. func GetAllDeviceNumberByList(orgID int64) (number []*models.DeviceNumber, err error) {
  1947. redis := RedisClient()
  1948. defer redis.Close()
  1949. key := strconv.FormatInt(orgID, 10) + ":" + ":device_number_all"
  1950. number_info_str, _ := redis.Get(key).Result()
  1951. if len(number_info_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1952. err = readDb.Model(&models.DeviceNumber{}).Where("org_id=? and status=1", orgID).Find(&number).Error
  1953. if err != nil {
  1954. if err == gorm.ErrRecordNotFound {
  1955. return nil, nil
  1956. } else {
  1957. return nil, err
  1958. }
  1959. } else {
  1960. if len(number) > 0 {
  1961. //缓存数据
  1962. device_number_json, err := json.Marshal(&number)
  1963. if err == nil {
  1964. redis.Set(key, device_number_json, time.Second*60*60*18)
  1965. }
  1966. }
  1967. return number, nil
  1968. }
  1969. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  1970. json.Unmarshal([]byte(number_info_str), &number)
  1971. return number, nil
  1972. }
  1973. }
  1974. func GetAllPrescriptionByListTwo(orgID int64, scheduleDate int64) (prescriptions []*models.DialysisPrescription, err error) {
  1975. redis := RedisClient()
  1976. defer redis.Close()
  1977. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":prescriptions_list_all"
  1978. prescriptions_list_all, _ := redis.Get(key).Result()
  1979. if len(prescriptions_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1980. if scheduleDate > 0 {
  1981. err = readDb.Model(&models.DialysisPrescription{}).Where("status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).Find(&prescriptions).Error
  1982. } else {
  1983. err = readDb.Model(&models.DialysisPrescription{}).Where("status = 1 AND user_org_id = ?", orgID).Find(&prescriptions).Error
  1984. }
  1985. if err != nil {
  1986. if err == gorm.ErrRecordNotFound {
  1987. return nil, nil
  1988. } else {
  1989. return nil, err
  1990. }
  1991. } else {
  1992. if len(prescriptions) > 0 {
  1993. //缓存数据
  1994. prescriptions_list_all, err := json.Marshal(&prescriptions)
  1995. if err == nil {
  1996. redis.Set(key, prescriptions_list_all, time.Second*60*60*18)
  1997. }
  1998. }
  1999. return prescriptions, nil
  2000. }
  2001. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2002. json.Unmarshal([]byte(prescriptions_list_all), &prescriptions)
  2003. return prescriptions, nil
  2004. }
  2005. }
  2006. func GetAllAssessmentBeforesByListThree(orgID int64, scheduleDate int64) (assessmentBefores []*models.PredialysisEvaluation, err error) {
  2007. redis := RedisClient()
  2008. defer redis.Close()
  2009. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":assessment_befores_list_all"
  2010. assessment_befores_list_all, _ := redis.Get(key).Result()
  2011. if len(assessment_befores_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2012. if scheduleDate > 0 {
  2013. err = readDb.Model(&models.PredialysisEvaluation{}).Where("status = 1 AND user_org_id = ? AND assessment_date = ? ", orgID, scheduleDate).Find(&assessmentBefores).Error
  2014. } else {
  2015. err = readDb.Model(&models.PredialysisEvaluation{}).Where("status = 1 AND user_org_id = ?", orgID).Find(&assessmentBefores).Error
  2016. }
  2017. if err != nil {
  2018. if err == gorm.ErrRecordNotFound {
  2019. return nil, nil
  2020. } else {
  2021. return nil, err
  2022. }
  2023. } else {
  2024. if len(assessmentBefores) > 0 {
  2025. //缓存数据
  2026. assessment_befores_list_all, err := json.Marshal(&assessmentBefores)
  2027. if err == nil {
  2028. redis.Set(key, assessment_befores_list_all, time.Second*60*60*18)
  2029. }
  2030. }
  2031. return assessmentBefores, nil
  2032. }
  2033. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2034. json.Unmarshal([]byte(assessment_befores_list_all), &assessmentBefores)
  2035. return assessmentBefores, nil
  2036. }
  2037. }
  2038. func GetAllDialysisOrdersByListThree(orgID int64, scheduleDate int64) (dialysisOrders []*models.MonitorDialysisOrder, err error) {
  2039. redis := RedisClient()
  2040. defer redis.Close()
  2041. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":dialysis_orders_list_all"
  2042. dialysis_orders_list_all, _ := redis.Get(key).Result()
  2043. if len(dialysis_orders_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2044. if scheduleDate > 0 {
  2045. 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
  2046. } else {
  2047. err = readDb.Model(&models.MonitorDialysisOrder{}).Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).Where("status = 1 AND user_org_id = ?", orgID).Find(&dialysisOrders).Error
  2048. }
  2049. if err != nil {
  2050. if err == gorm.ErrRecordNotFound {
  2051. return nil, nil
  2052. } else {
  2053. return nil, err
  2054. }
  2055. } else {
  2056. if len(dialysisOrders) > 0 {
  2057. //缓存数据
  2058. dialysis_orders_list_all, err := json.Marshal(&dialysisOrders)
  2059. if err == nil {
  2060. redis.Set(key, dialysis_orders_list_all, time.Second*60*60*18)
  2061. }
  2062. }
  2063. return dialysisOrders, nil
  2064. }
  2065. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2066. json.Unmarshal([]byte(dialysis_orders_list_all), &dialysisOrders)
  2067. return dialysisOrders, nil
  2068. }
  2069. }
  2070. func GetAllAssessmentAfterDislysisByListThree(orgID int64, scheduleDate int64) (assessmentAfterDislysis []*models.AssessmentAfterDislysis, err error) {
  2071. redis := RedisClient()
  2072. defer redis.Close()
  2073. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":assessment_after_dislysis_list_all"
  2074. assessment_after_dislysis__all, _ := redis.Get(key).Result()
  2075. if len(assessment_after_dislysis__all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2076. if scheduleDate > 0 {
  2077. err = readDb.Model(&models.AssessmentAfterDislysis{}).Where("status = 1 AND user_org_id = ? AND assessment_date = ?", orgID, scheduleDate).Find(&assessmentAfterDislysis).Error
  2078. } else {
  2079. err = readDb.Model(&models.AssessmentAfterDislysis{}).Where("status = 1 AND user_org_id = ?", orgID).Find(&assessmentAfterDislysis).Error
  2080. }
  2081. if err != nil {
  2082. if err == gorm.ErrRecordNotFound {
  2083. return nil, nil
  2084. } else {
  2085. return nil, err
  2086. }
  2087. } else {
  2088. if len(assessmentAfterDislysis) > 0 {
  2089. //缓存数据
  2090. assessment_after_dislysis__all, err := json.Marshal(&assessmentAfterDislysis)
  2091. if err == nil {
  2092. redis.Set(key, assessment_after_dislysis__all, time.Second*60*60*18)
  2093. }
  2094. }
  2095. return assessmentAfterDislysis, nil
  2096. }
  2097. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2098. json.Unmarshal([]byte(assessment_after_dislysis__all), &assessmentAfterDislysis)
  2099. return assessmentAfterDislysis, nil
  2100. }
  2101. }
  2102. func GetAllMonitorListThree(orgID int64, scheduleDate int64) (monitor []*models.MonitoringRecord, err error) {
  2103. redis := RedisClient()
  2104. defer redis.Close()
  2105. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":monitor_record_list_all"
  2106. monitor_record_list_all, _ := redis.Get(key).Result()
  2107. if len(monitor_record_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2108. if scheduleDate > 0 {
  2109. err = readDb.Model(&models.MonitoringRecord{}).Where("status = 1 AND user_org_id = ? AND monitoring_date= ?", orgID, scheduleDate).Find(&monitor).Error
  2110. } else {
  2111. err = readDb.Model(&models.MonitoringRecord{}).Where("status = 1 AND user_org_id = ?", orgID).Find(&monitor).Error
  2112. }
  2113. if err != nil {
  2114. if err == gorm.ErrRecordNotFound {
  2115. return nil, nil
  2116. } else {
  2117. return nil, err
  2118. }
  2119. } else {
  2120. if len(monitor) > 0 {
  2121. //缓存数据
  2122. assessment_after_dislysis__all, err := json.Marshal(&monitor)
  2123. if err == nil {
  2124. redis.Set(key, assessment_after_dislysis__all, time.Second*60*60*18)
  2125. }
  2126. }
  2127. return monitor, nil
  2128. }
  2129. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2130. json.Unmarshal([]byte(monitor_record_list_all), &monitor)
  2131. return monitor, nil
  2132. }
  2133. }
  2134. func GetAllPatientListByListThree(orgID int64) (patients []*models.MonitorPatients, err error) {
  2135. redis := RedisClient()
  2136. defer redis.Close()
  2137. key := strconv.FormatInt(orgID, 10) + ":" + ":patient_list_all"
  2138. patient_info_str, _ := redis.Get(key).Result()
  2139. if len(patient_info_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2140. err = readDb.Model(&models.MonitorPatients{}).Where("user_org_id=? and status=1 and lapseto = 1", orgID).Find(&patients).Error
  2141. if err != nil {
  2142. if err == gorm.ErrRecordNotFound {
  2143. return nil, nil
  2144. } else {
  2145. return nil, err
  2146. }
  2147. } else {
  2148. if len(patients) > 0 {
  2149. //缓存数据
  2150. patient_info_json, err := json.Marshal(&patients)
  2151. if err == nil {
  2152. redis.Set(key, patient_info_json, time.Second*60*60*18)
  2153. }
  2154. }
  2155. return patients, nil
  2156. }
  2157. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2158. json.Unmarshal([]byte(patient_info_str), &patients)
  2159. return patients, nil
  2160. }
  2161. }
  2162. func GetAllPatientListByListSix(orgID int64, keywords string) (patients []*models.MonitorPatients, err error) {
  2163. db := readDb.Model(&models.MonitorPatients{}).Where("status = 1 and lapseto = 1")
  2164. likeKey := "%" + keywords + "%"
  2165. if orgID > 0 {
  2166. db = db.Where("user_org_id = ?", orgID)
  2167. }
  2168. if len(keywords) > 0 {
  2169. db = db.Where("name like ?", likeKey)
  2170. }
  2171. err = db.Find(&patients).Error
  2172. return patients, err
  2173. }
  2174. func GetAllDialysisOrdersByListTwo(orgID int64, scheduleDate int64) (dialysisOrders []*MDialysisOrderVM, err error) {
  2175. redis := RedisClient()
  2176. defer redis.Close()
  2177. key := strconv.FormatInt(orgID, 10) + ":" + ":dialysis_orders_list_all"
  2178. dialysis_orders_list_all, _ := redis.Get(key).Result()
  2179. if scheduleDate < 1669132800 {
  2180. fmt.Println("key233232232332323232", key)
  2181. redis.Set(key, "", time.Second)
  2182. }
  2183. fmt.Println("dialysis_orders_list_all", dialysis_orders_list_all)
  2184. if len(dialysis_orders_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2185. 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
  2186. if err != nil {
  2187. if err == gorm.ErrRecordNotFound {
  2188. return nil, nil
  2189. } else {
  2190. return nil, err
  2191. }
  2192. } else {
  2193. if len(dialysisOrders) > 0 {
  2194. //缓存数据
  2195. dialysis_orders_list_all, err := json.Marshal(&dialysisOrders)
  2196. if err == nil {
  2197. redis.Set(key, dialysis_orders_list_all, time.Second*60*60*18)
  2198. }
  2199. }
  2200. return dialysisOrders, nil
  2201. }
  2202. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2203. json.Unmarshal([]byte(dialysis_orders_list_all), &dialysisOrders)
  2204. return dialysisOrders, nil
  2205. }
  2206. }
  2207. func GetAllZoneByList(orgID int64) (zone []*models.DeviceZone, err error) {
  2208. redis := RedisClient()
  2209. defer redis.Close()
  2210. key := strconv.FormatInt(orgID, 10) + ":" + ":device_zone_all"
  2211. zone_info_str, _ := redis.Get(key).Result()
  2212. if len(zone_info_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2213. err = readDb.Model(&models.DeviceZone{}).Where("org_id=? and status=1", orgID).Find(&zone).Error
  2214. if err != nil {
  2215. if err == gorm.ErrRecordNotFound {
  2216. return nil, nil
  2217. } else {
  2218. return nil, err
  2219. }
  2220. } else {
  2221. if len(zone) > 0 {
  2222. //缓存数据
  2223. zone_number_json, err := json.Marshal(&zone)
  2224. if err == nil {
  2225. redis.Set(key, zone_number_json, time.Second*60*60*18)
  2226. }
  2227. }
  2228. return zone, nil
  2229. }
  2230. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2231. json.Unmarshal([]byte(zone_info_str), &zone)
  2232. return zone, nil
  2233. }
  2234. }
  2235. func GetAllDialysisOrdersByListNight(orgID int64, scheduleDate int64, pay_tyep int64) (dialysisOrders []*models.VMMonitorDialysisOrder, err error) {
  2236. if pay_tyep == 0 {
  2237. 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
  2238. }
  2239. //待下机
  2240. if pay_tyep == 3 {
  2241. 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
  2242. }
  2243. fmt.Println("pay_typeowowowoowwo", pay_tyep)
  2244. if pay_tyep == 7 {
  2245. 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
  2246. }
  2247. return dialysisOrders, err
  2248. }
  2249. func GetAllPrescriptionByListSix(orgID int64, scheduleDate int64) (prescriptions []*models.VMDialysisPrescription, err error) {
  2250. err = readDb.Model(&models.VMDialysisPrescription{}).Where("status= 1 and user_org_id = ? and record_date = ?", orgID, scheduleDate).Find(&prescriptions).Error
  2251. return prescriptions, err
  2252. //redis := RedisClient()
  2253. //defer redis.Close()
  2254. //key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":prescriptions_list_all"
  2255. //redis.Set(key, "", time.Second)
  2256. //prescriptions_list_all, _ := redis.Get(key).Result()
  2257. //
  2258. //if len(prescriptions_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2259. // err = readDb.Model(&models.VMDialysisPrescription{}).Where("status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).Find(&prescriptions).Error
  2260. // if err != nil {
  2261. // if err == gorm.ErrRecordNotFound {
  2262. // return nil, nil
  2263. // } else {
  2264. // return nil, err
  2265. // }
  2266. // } else {
  2267. // if len(prescriptions) > 0 {
  2268. // //缓存数据
  2269. // prescriptions_list_all, err := json.Marshal(&prescriptions)
  2270. // if err == nil {
  2271. // redis.Set(key, prescriptions_list_all, time.Second*60*60*18)
  2272. // }
  2273. // }
  2274. // return prescriptions, nil
  2275. // }
  2276. //} else { //缓存数据了数据,将redis缓存的json字符串转为map
  2277. // json.Unmarshal([]byte(prescriptions_list_all), &prescriptions)
  2278. // return prescriptions, nil
  2279. //}
  2280. }
  2281. func GetAllPrescriptionByList(orgID int64, scheduleDate int64) (prescriptions []*models.VMDialysisPrescription, err error) {
  2282. redis := RedisClient()
  2283. defer redis.Close()
  2284. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":prescriptions_list_all"
  2285. redis.Set(key, "", time.Second)
  2286. prescriptions_list_all, _ := redis.Get(key).Result()
  2287. if len(prescriptions_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2288. err = readDb.Model(&models.VMDialysisPrescription{}).Where("status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).Find(&prescriptions).Error
  2289. if err != nil {
  2290. if err == gorm.ErrRecordNotFound {
  2291. return nil, nil
  2292. } else {
  2293. return nil, err
  2294. }
  2295. } else {
  2296. if len(prescriptions) > 0 {
  2297. //缓存数据
  2298. prescriptions_list_all, err := json.Marshal(&prescriptions)
  2299. if err == nil {
  2300. redis.Set(key, prescriptions_list_all, time.Second*60*60*18)
  2301. }
  2302. }
  2303. return prescriptions, nil
  2304. }
  2305. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2306. json.Unmarshal([]byte(prescriptions_list_all), &prescriptions)
  2307. return prescriptions, nil
  2308. }
  2309. }
  2310. func GetAllAssessmentBeforesByListOne(orgID int64, scheduleDate int64) (assessmentBefores []*models.VMPredialysisEvaluation, err error) {
  2311. redis := RedisClient()
  2312. defer redis.Close()
  2313. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":assessment_befores_list_all"
  2314. assessment_befores_list_all, _ := redis.Get(key).Result()
  2315. if len(assessment_befores_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2316. err = readDb.Model(&models.VMPredialysisEvaluation{}).Where("status = 1 AND user_org_id = ? AND assessment_date = ? ", orgID, scheduleDate).Find(&assessmentBefores).Error
  2317. if err != nil {
  2318. if err == gorm.ErrRecordNotFound {
  2319. return nil, nil
  2320. } else {
  2321. return nil, err
  2322. }
  2323. } else {
  2324. if len(assessmentBefores) > 0 {
  2325. //缓存数据
  2326. assessment_befores_list_all, err := json.Marshal(&assessmentBefores)
  2327. if err == nil {
  2328. redis.Set(key, assessment_befores_list_all, time.Second*60*60*18)
  2329. }
  2330. }
  2331. return assessmentBefores, nil
  2332. }
  2333. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2334. json.Unmarshal([]byte(assessment_befores_list_all), &assessmentBefores)
  2335. return assessmentBefores, nil
  2336. }
  2337. }
  2338. func GetAllDialysisOrdersByListSix(orgID int64, scheduleDate int64) (dialysisOrders []*models.VMMonitorDialysisOrder, err error) {
  2339. 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
  2340. return dialysisOrders, err
  2341. }
  2342. func GetAllDialysisOrdersByListSeven(orgID int64, scheduleDate int64) (dialysisOrders []*MDialysisOrderVM, err error) {
  2343. 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
  2344. return dialysisOrders, err
  2345. }
  2346. func GetAllDialysisOrdersByListOne(orgID int64, scheduleDate int64) (dialysisOrders []*models.VMMonitorDialysisOrder, err error) {
  2347. redis := RedisClient()
  2348. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":dialysis_orders_list_all"
  2349. fmt.Println("key22332323232323223", key)
  2350. dialysis_orders_list_all, _ := redis.Get(key).Result()
  2351. defer redis.Close()
  2352. if len(dialysis_orders_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2353. 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
  2354. if err != nil {
  2355. if err == gorm.ErrRecordNotFound {
  2356. return nil, nil
  2357. } else {
  2358. return nil, err
  2359. }
  2360. } else {
  2361. if len(dialysisOrders) > 0 {
  2362. //缓存数据
  2363. dialysis_orders_list_all, err := json.Marshal(&dialysisOrders)
  2364. if err == nil {
  2365. redis.Set(key, dialysis_orders_list_all, time.Second*60*60*18)
  2366. }
  2367. }
  2368. return dialysisOrders, nil
  2369. }
  2370. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2371. json.Unmarshal([]byte(dialysis_orders_list_all), &dialysisOrders)
  2372. return dialysisOrders, nil
  2373. }
  2374. }
  2375. func GetAllAssessmentAfterDislysisByListSix(orgID int64, scheduleDate int64) (assessmentAfterDislysis []*models.VMAssessmentAfterDislysis, err error) {
  2376. err = readDb.Model(&models.VMAssessmentAfterDislysis{}).Where("status=1 and user_org_id = ? and assessment_date = ?", orgID, scheduleDate).Find(&assessmentAfterDislysis).Error
  2377. return assessmentAfterDislysis, err
  2378. //redis := RedisClient()
  2379. //defer redis.Close()
  2380. //key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":assessment_after_dislysis_list_all"
  2381. //redis.Set(key, "", time.Second)
  2382. //assessment_after_dislysis__all, _ := redis.Get(key).Result()
  2383. //
  2384. //if len(assessment_after_dislysis__all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2385. // err = readDb.Model(&models.VMAssessmentAfterDislysis{}).Where("status = 1 AND user_org_id = ? AND assessment_date = ?", orgID, scheduleDate).Find(&assessmentAfterDislysis).Error
  2386. // if err != nil {
  2387. // if err == gorm.ErrRecordNotFound {
  2388. // return nil, nil
  2389. // } else {
  2390. // return nil, err
  2391. // }
  2392. // } else {
  2393. // if len(assessmentAfterDislysis) > 0 {
  2394. // //缓存数据
  2395. // assessment_after_dislysis__all, err := json.Marshal(&assessmentAfterDislysis)
  2396. // if err == nil {
  2397. // redis.Set(key, assessment_after_dislysis__all, time.Second*60*60*18)
  2398. // }
  2399. // }
  2400. // return assessmentAfterDislysis, nil
  2401. // }
  2402. //} else { //缓存数据了数据,将redis缓存的json字符串转为map
  2403. // json.Unmarshal([]byte(assessment_after_dislysis__all), &assessmentAfterDislysis)
  2404. // return assessmentAfterDislysis, nil
  2405. //}
  2406. }
  2407. func GetAllAssessmentAfterDislysisByListOne(orgID int64, scheduleDate int64) (assessmentAfterDislysis []*models.VMAssessmentAfterDislysis, err error) {
  2408. redis := RedisClient()
  2409. defer redis.Close()
  2410. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":assessment_after_dislysis_list_all"
  2411. redis.Set(key, "", time.Second)
  2412. assessment_after_dislysis__all, _ := redis.Get(key).Result()
  2413. if len(assessment_after_dislysis__all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2414. err = readDb.Model(&models.VMAssessmentAfterDislysis{}).Where("status = 1 AND user_org_id = ? AND assessment_date = ?", orgID, scheduleDate).Find(&assessmentAfterDislysis).Error
  2415. if err != nil {
  2416. if err == gorm.ErrRecordNotFound {
  2417. return nil, nil
  2418. } else {
  2419. return nil, err
  2420. }
  2421. } else {
  2422. if len(assessmentAfterDislysis) > 0 {
  2423. //缓存数据
  2424. assessment_after_dislysis__all, err := json.Marshal(&assessmentAfterDislysis)
  2425. if err == nil {
  2426. redis.Set(key, assessment_after_dislysis__all, time.Second*60*60*18)
  2427. }
  2428. }
  2429. return assessmentAfterDislysis, nil
  2430. }
  2431. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2432. json.Unmarshal([]byte(assessment_after_dislysis__all), &assessmentAfterDislysis)
  2433. return assessmentAfterDislysis, nil
  2434. }
  2435. }
  2436. func GetAllMonitorList(orgID int64, scheduleDate int64) (monitor []*models.VMMonitoringRecord, err error) {
  2437. redis := RedisClient()
  2438. defer redis.Close()
  2439. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":monitor_record_list_all"
  2440. monitor_record_list_all, _ := redis.Get(key).Result()
  2441. redis.Set(key, "", time.Second)
  2442. if len(monitor_record_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2443. err = readDb.Model(&models.VMMonitoringRecord{}).Where("status = 1 AND user_org_id = ? AND monitoring_date= ?", orgID, scheduleDate).Order("monitoring_date asc").Find(&monitor).Error
  2444. if err != nil {
  2445. if err == gorm.ErrRecordNotFound {
  2446. return nil, nil
  2447. } else {
  2448. return nil, err
  2449. }
  2450. } else {
  2451. if len(monitor) > 0 {
  2452. //缓存数据
  2453. assessment_after_dislysis__all, err := json.Marshal(&monitor)
  2454. if err == nil {
  2455. redis.Set(key, assessment_after_dislysis__all, time.Second*60*60*18)
  2456. }
  2457. }
  2458. return monitor, nil
  2459. }
  2460. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2461. json.Unmarshal([]byte(monitor_record_list_all), &monitor)
  2462. return monitor, nil
  2463. }
  2464. }
  2465. func GetAllPatientListByListOne(orgID int64) (patients []*models.VMMonitorPatients, err error) {
  2466. redis := RedisClient()
  2467. defer redis.Close()
  2468. key := strconv.FormatInt(orgID, 10) + ":" + ":patient_list_all"
  2469. patient_info_str, _ := redis.Get(key).Result()
  2470. redis.Set(key, "", time.Second)
  2471. if len(patient_info_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2472. err = readDb.Model(&models.VMMonitorPatients{}).Where("user_org_id=? and status=1", orgID).Find(&patients).Error
  2473. if err != nil {
  2474. if err == gorm.ErrRecordNotFound {
  2475. return nil, nil
  2476. } else {
  2477. return nil, err
  2478. }
  2479. } else {
  2480. if len(patients) > 0 {
  2481. //缓存数据
  2482. patient_info_json, err := json.Marshal(&patients)
  2483. if err == nil {
  2484. redis.Set(key, patient_info_json, time.Second*60*60*18)
  2485. }
  2486. }
  2487. return patients, nil
  2488. }
  2489. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2490. json.Unmarshal([]byte(patient_info_str), &patients)
  2491. return patients, nil
  2492. }
  2493. }
  2494. func GetAllTreatModeByList(orgID int64) (treatement []*models.TreatmentMode, err error) {
  2495. redis := RedisClient()
  2496. defer redis.Close()
  2497. key := strconv.FormatInt(orgID, 10) + ":" + ":treatement_mode_all"
  2498. treatment_info_str, _ := redis.Get(key).Result()
  2499. if len(treatment_info_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2500. err = readDb.Model(&models.TreatmentMode{}).Where("status=1").Find(&treatement).Error
  2501. if err != nil {
  2502. if err == gorm.ErrRecordNotFound {
  2503. return nil, nil
  2504. } else {
  2505. return nil, err
  2506. }
  2507. } else {
  2508. if len(treatement) > 0 {
  2509. //缓存数据
  2510. patient_info_json, err := json.Marshal(&treatement)
  2511. if err == nil {
  2512. redis.Set(key, patient_info_json, time.Second*60*60*18)
  2513. }
  2514. }
  2515. return treatement, nil
  2516. }
  2517. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2518. json.Unmarshal([]byte(treatment_info_str), &treatement)
  2519. return treatement, nil
  2520. }
  2521. }
  2522. func GetAllPatientListByListSeven(orgID int64) (patients []*MSchedualPatientVM, err error) {
  2523. err = readDb.Model(&MSchedualPatientVM{}).Where("user_org_id=? and status=1", orgID).Find(&patients).Error
  2524. return patients, nil
  2525. }
  2526. func GetAllPatientListByListTwo(orgID int64) (patients []*MSchedualPatientVM, err error) {
  2527. redis := RedisClient()
  2528. defer redis.Close()
  2529. key := strconv.FormatInt(orgID, 10) + ":" + ":patient_list_all"
  2530. patient_info_str, _ := redis.Get(key).Result()
  2531. if len(patient_info_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2532. err = readDb.Model(&MSchedualPatientVM{}).Where("user_org_id=? and status=1", orgID).Find(&patients).Error
  2533. if err != nil {
  2534. if err == gorm.ErrRecordNotFound {
  2535. return nil, nil
  2536. } else {
  2537. return nil, err
  2538. }
  2539. } else {
  2540. if len(patients) > 0 {
  2541. //缓存数据
  2542. patient_info_json, err := json.Marshal(&patients)
  2543. if err == nil {
  2544. redis.Set(key, patient_info_json, time.Second*60*60*18)
  2545. }
  2546. }
  2547. return patients, nil
  2548. }
  2549. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2550. json.Unmarshal([]byte(patient_info_str), &patients)
  2551. return patients, nil
  2552. }
  2553. }
  2554. func GetAllDeviceNumberByListOne(orgID int64) (number []*MDeviceNumberVM, err error) {
  2555. redis := RedisClient()
  2556. defer redis.Close()
  2557. key := strconv.FormatInt(orgID, 10) + ":" + ":device_number_all"
  2558. number_info_str, _ := redis.Get(key).Result()
  2559. if len(number_info_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2560. err = readDb.Model(&MDeviceNumberVM{}).Where("org_id=? and status=1", orgID).Find(&number).Error
  2561. if err != nil {
  2562. if err == gorm.ErrRecordNotFound {
  2563. return nil, nil
  2564. } else {
  2565. return nil, err
  2566. }
  2567. } else {
  2568. if len(number) > 0 {
  2569. //缓存数据
  2570. device_number_json, err := json.Marshal(&number)
  2571. if err == nil {
  2572. redis.Set(key, device_number_json, time.Second*60*60*18)
  2573. }
  2574. }
  2575. return number, nil
  2576. }
  2577. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2578. json.Unmarshal([]byte(number_info_str), &number)
  2579. return number, nil
  2580. }
  2581. }
  2582. func GetAllPrescriptionByListOne(orgID int64, scheduleDate int64) (prescriptions []*models.DialysisPrescriptionTwenty, err error) {
  2583. redis := RedisClient()
  2584. defer redis.Close()
  2585. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":prescriptions_list_all"
  2586. prescriptions_list_all, _ := redis.Get(key).Result()
  2587. if len(prescriptions_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2588. err = readDb.Model(&models.DialysisPrescriptionTwenty{}).Where("status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).Find(&prescriptions).Error
  2589. if err != nil {
  2590. if err == gorm.ErrRecordNotFound {
  2591. return nil, nil
  2592. } else {
  2593. return nil, err
  2594. }
  2595. } else {
  2596. if len(prescriptions) > 0 {
  2597. //缓存数据
  2598. prescriptions_list_all, err := json.Marshal(&prescriptions)
  2599. if err == nil {
  2600. redis.Set(key, prescriptions_list_all, time.Second*60*60*18)
  2601. }
  2602. }
  2603. return prescriptions, nil
  2604. }
  2605. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2606. json.Unmarshal([]byte(prescriptions_list_all), &prescriptions)
  2607. return prescriptions, nil
  2608. }
  2609. }
  2610. func GetAllHisProjectPrescription(orgid int64, recorddate int64) (project []*models.HisPrescriptionProjectTwo, err error) {
  2611. err = XTReadDB().Where("user_org_id = ? and record_date = ? and status = 1", orgid, recorddate).Find(&project).Error
  2612. return project, err
  2613. }
  2614. func GetZoneCountList(orgid int64, schedule_date int64) (list []*models.XtScheduleSeven, err error) {
  2615. db := XTReadDB().Model(&list).Where("status = 1")
  2616. if orgid > 0 {
  2617. db = db.Where("user_org_id = ?", orgid)
  2618. }
  2619. if schedule_date > 0 {
  2620. db = db.Where("schedule_date =?", schedule_date)
  2621. }
  2622. err = db.Select("partition_id,count(id) as count").Group("partition_id").Scan(&list).Error
  2623. return list, err
  2624. }
  2625. func GetPatientDialysisLongSolution(patient_id int64, page int64, limit int64, orgid int64) (solution []*models.DialysisSolution, total int64, err error) {
  2626. db := XTReadDB().Model(&solution).Where("status = 1 and solution_status = 1")
  2627. if patient_id > 0 {
  2628. db = db.Where("patient_id = ?", patient_id)
  2629. }
  2630. if orgid > 0 {
  2631. db = db.Where("user_org_id = ?", orgid)
  2632. }
  2633. offset := (page - 1) * limit
  2634. err = db.Group("mode_id").Count(&total).Offset(offset).Limit(limit).Order("created_time desc").Find(&solution).Error
  2635. return solution, total, err
  2636. }
  2637. func GetDialysisSolutionDetailList(orgid int64, patient_id int64, mode_id int64, page int64, limit int64) (solution []*models.DialysisSolution, total int64, err error) {
  2638. offset := (page - 1) * limit
  2639. db := XTReadDB().Model(&solution).Where("status = 1")
  2640. if orgid > 0 {
  2641. db = db.Where("user_org_id = ?", orgid)
  2642. }
  2643. if patient_id > 0 {
  2644. db = db.Where("patient_id = ?", patient_id)
  2645. }
  2646. if mode_id > 0 {
  2647. db = db.Where("mode_id = ?", mode_id)
  2648. }
  2649. err = db.Count(&total).Offset(offset).Limit(limit).Find(&solution).Order("created_time desc").Error
  2650. return solution, total, err
  2651. }
  2652. func UpdateDialysisBefor(dislysis models.XtAssessmentBeforeDislysis, id int64) error {
  2653. 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
  2654. return err
  2655. }
  2656. func GetAllSchedulesByPatientInfo(org_id int64, patient_id int64, mode_id int64) ([]Schedule, error) {
  2657. var record []Schedule
  2658. if org_id == 10579 {
  2659. timeNowStr := time.Now().Format("2006-01-02")
  2660. timeNewDate, _ := utils.ParseTimeStringToTime("2006-01-02", timeNowStr)
  2661. 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
  2662. if err != nil {
  2663. if err == gorm.ErrRecordNotFound {
  2664. return nil, nil
  2665. } else {
  2666. return nil, err
  2667. }
  2668. }
  2669. } else {
  2670. timeNowStr := time.Now().Format("2006-01-02")
  2671. timeNewDate, _ := utils.ParseTimeStringToTime("2006-01-02", timeNowStr)
  2672. 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
  2673. if err != nil {
  2674. if err == gorm.ErrRecordNotFound {
  2675. return nil, nil
  2676. } else {
  2677. return nil, err
  2678. }
  2679. }
  2680. }
  2681. return record, nil
  2682. }
  2683. func UpdateScheduleByExport(org_id int64, shcheduledate int64) error {
  2684. err := XTWriteDB().Model(models.XtSchedule{}).Where("user_org_id = ? and schedule_date > ?", org_id, shcheduledate).Updates(map[string]interface{}{"status": 0}).Error
  2685. return err
  2686. }
  2687. func GetContextScheduleListGroupPatientId(limit int64, page int64, startTime int64, endTime int64, org_id int64) (schedule []*models.ConScheduleList, total int64, err error) {
  2688. db := XTReadDB().Model(&schedule).Where("status=1")
  2689. offset := (page - 1) * limit
  2690. if startTime > 0 {
  2691. db = db.Where("schedule_date >=?", startTime)
  2692. }
  2693. if endTime > 0 {
  2694. db = db.Where("schedule_date<=?", endTime)
  2695. }
  2696. if org_id > 0 {
  2697. db = db.Where("user_org_id = ?", org_id)
  2698. }
  2699. err = db.Count(&total).Offset(offset).Limit(limit).Group("patient_id").Find(&schedule).Error
  2700. return schedule, total, err
  2701. }
  2702. func GetContextScheduleListPatientId(startTime int64, endTime int64, org_id int64) (schedule []*models.ConScheduleList, total int64, err error) {
  2703. db := XTReadDB().Model(&schedule).Where("status=1")
  2704. if startTime > 0 {
  2705. db = db.Where("schedule_date >=?", startTime)
  2706. }
  2707. if endTime > 0 {
  2708. db = db.Where("schedule_date<=?", endTime)
  2709. }
  2710. if org_id > 0 {
  2711. db = db.Where("user_org_id = ?", org_id)
  2712. }
  2713. err = db.Find(&schedule).Error
  2714. return schedule, total, err
  2715. }
  2716. func GetPatientAllagicList(patient_id int64, user_org_id int64) (list []*models.XtPatientAllergic, err error) {
  2717. err = XTReadDB().Where("patient_id = ? and user_org_id =? and status= 1", patient_id, user_org_id).Find(&list).Error
  2718. return list, err
  2719. }
  2720. func GetNewMonitorRecord(orgID int64, date int64, partition int64, patientId int64) ([]*models.NewMonitorDialysisScheduleList, error) {
  2721. var mds []*models.NewMonitorDialysisScheduleList
  2722. db := readDb.Model(&models.NewMonitorDialysisScheduleList{})
  2723. if date > 0 {
  2724. db = db.Where("schedule_date = ?", date)
  2725. }
  2726. if partition > 0 {
  2727. db = db.Where("partition_id = ?", partition)
  2728. }
  2729. if patientId > 0 {
  2730. db = db.Where("patient_id = ?", patientId)
  2731. }
  2732. err = db.Where("status = 1 and user_org_id = ?", orgID).Find(&mds).Error
  2733. return mds, err
  2734. }
  2735. func GetAdviceList(user_org_id int64, advice_date int64, pat_type int64) (advice []*models.VMDoctorAdviceForList, err error) {
  2736. if pat_type == 0 {
  2737. err = XTReadDB().Where("user_org_id = ? and advice_date = ? and status =7 and check_time = 0", user_org_id, advice_date).Find(&advice).Error
  2738. }
  2739. //待医嘱核对
  2740. if pat_type == 1 {
  2741. err = XTReadDB().Where("user_org_id = ? and advice_date = ? and status =1 and check_time = 0", user_org_id, advice_date).Find(&advice).Error
  2742. }
  2743. //待医嘱执行
  2744. if pat_type == 6 {
  2745. err = XTReadDB().Where("user_org_id = ? and advice_date = ? and status =1 and execution_state = 2", user_org_id, advice_date).Find(&advice).Error
  2746. }
  2747. return advice, err
  2748. }
  2749. func GetDobuleCheck(user_org_id int64, advice_date int64) (check []*models.VmDoubleCheck, err error) {
  2750. err = XTReadDB().Where("user_org_id = ? and check_date = ? and status = 1", user_org_id, advice_date).Find(&check).Error
  2751. return check, err
  2752. }
  2753. func GetTreatmentSummaryForList(user_org_id int64, advice_date int64) (summary []*models.VMTreatmentSummaryForList, err error) {
  2754. err = XTReadDB().Where("user_org_id =? and assessment_date = ? and status =1", user_org_id, advice_date).Find(&summary).Error
  2755. return summary, err
  2756. }
  2757. func GetDialysisInformationList(user_org_id int64, advice_date int64) (information []*models.NewDeviceInformation, err error) {
  2758. err = UserReadDB().Where("user_org_id =? and date = ? and status =1", user_org_id, advice_date).Find(&information).Error
  2759. return information, err
  2760. }
  2761. func GetNewAllMonitorList(orgID int64, scheduleDate int64, pat_type int64) (monitor []*models.VMMonitoringRecord, err error) {
  2762. if pat_type == 0 || pat_type == 9 {
  2763. err = readDb.Model(&models.VMMonitoringRecord{}).Where("status = 1 AND user_org_id = ? AND monitoring_date= ?", orgID, scheduleDate).Order("monitoring_date asc").Find(&monitor).Error
  2764. }
  2765. if pat_type == 10 {
  2766. 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
  2767. }
  2768. if pat_type == 11 {
  2769. 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
  2770. }
  2771. if pat_type == 12 {
  2772. 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
  2773. }
  2774. return monitor, err
  2775. }
  2776. func GetBaseDrugByName(drug_name string, org_id int64) (models.BaseDrugLibSeven, error) {
  2777. libSeven := models.BaseDrugLibSeven{}
  2778. err := XTReadDB().Where("drug_name= ? and org_id = ? and status=1", drug_name, org_id).First(&libSeven).Error
  2779. return libSeven, err
  2780. }
  2781. func CreateDeviceValsualMap(valsualMap models.XtDeviceValsualMap) error {
  2782. err := XTWriteDB().Save(&valsualMap).Error
  2783. return err
  2784. }
  2785. func GetPatientSitemap(patient_id int64, user_org_id int64) (models.XtDeviceValsualMap, error) {
  2786. valsualMap := models.XtDeviceValsualMap{}
  2787. 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
  2788. return valsualMap, err
  2789. }
  2790. func UpdateSiteMap(valsualMap models.XtDeviceValsualMap) error {
  2791. err := XTWriteDB().Save(&valsualMap).Error
  2792. return err
  2793. }
  2794. func CreateValsualAleft(left models.XtDeviceValsualALeft) error {
  2795. err := XTWriteDB().Create(&left).Error
  2796. return err
  2797. }
  2798. func UpdateValsualAleft(patient_id int64, user_org_id int64) error {
  2799. 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
  2800. return err
  2801. }
  2802. func CreateValsualMapFangxiangA(a models.XtDeviceValsualMapFangxiangA) error {
  2803. err := XTWriteDB().Create(&a).Error
  2804. return err
  2805. }
  2806. func UpdateValsualMapFangxiangA(patient_id int64, user_org_id int64) error {
  2807. 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
  2808. return err
  2809. }
  2810. func CreateValsualMapFangxiangV(v models.XtDeviceValsualMapFangxiangV) error {
  2811. err := XTWriteDB().Create(&v).Error
  2812. return err
  2813. }
  2814. func UpdateValsualMapFangxiangV(patient_id int64, user_org_id int64) error {
  2815. 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
  2816. return err
  2817. }
  2818. func ValsualMapFangxiangA(patient_id int64, user_org_id int64) error {
  2819. 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
  2820. return err
  2821. }
  2822. func CreateValSualFuzhA(a models.XtDeviceValsualFuzhuA) error {
  2823. err := XTReadDB().Create(&a).Error
  2824. return err
  2825. }
  2826. func UpdateValSualFuzhA(patient_id int64, user_org_id int64) error {
  2827. 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
  2828. return err
  2829. }
  2830. func CreateVasuaLfuzhuV(v models.XtDeviceValsualFuzhuV) error {
  2831. err := XTReadDB().Create(&v).Error
  2832. return err
  2833. }
  2834. func UpdateVasuaLfuzhuV(patient_id int64, user_org_id int64) error {
  2835. 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
  2836. return err
  2837. }
  2838. func CreateTextArr(arr models.XtDeviceValsualTextArr) error {
  2839. err := XTReadDB().Create(&arr).Error
  2840. return err
  2841. }
  2842. func UpdateTextArr(patient_id int64, user_org_id int64) error {
  2843. 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
  2844. return err
  2845. }
  2846. func CreateValsualVleft(left models.XtDeviceValsualVLeft) error {
  2847. err = XTReadDB().Create(&left).Error
  2848. return err
  2849. }
  2850. func UpdateValsualVleft(patient_id int64, user_org_id int64) error {
  2851. 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
  2852. return err
  2853. }
  2854. func UpdateValusalMapArr(patient_id int64, user_org_id int64) error {
  2855. 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
  2856. return err
  2857. }
  2858. func CreateDeviceValusalMapArr(vrr models.XtDeviceValusalMapArr) error {
  2859. err := XTWriteDB().Create(&vrr).Error
  2860. return err
  2861. }
  2862. func UpdateDeviceValusalMapVrr(patient_id int64, user_org_id int64) error {
  2863. 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
  2864. return err
  2865. }
  2866. func CreateDeviceValusalMapVrr(vrr models.XtDeviceValusalMapVrr) error {
  2867. err := XTWriteDB().Create(&vrr).Error
  2868. return err
  2869. }
  2870. func GetDialysisOrderNoExecution(start_nurse int64, user_org_id int64, timenow int64) (order []*models.XtDialysisOrder, err error) {
  2871. err = XTReadDB().Where("start_nurse = ? and user_org_id =? and status =1 and dialysis_date =?", start_nurse, user_org_id, timenow).Find(&order).Error
  2872. return order, err
  2873. }
  2874. func GetAdviceListNoExecution(advice_date int64, user_org_id int64, patientIds []int64) (advice []*models.NoDoctorAdvice, err error) {
  2875. db := XTReadDB().Model(advice).Where("status=1 and execution_state = 2 and advice_type = 2")
  2876. if len(patientIds) > 0 {
  2877. if advice_date > 0 {
  2878. db = db.Where("advice_date= ?", advice_date)
  2879. }
  2880. if user_org_id > 0 {
  2881. db = db.Where("user_org_id =?", user_org_id)
  2882. }
  2883. if len(patientIds) > 0 {
  2884. db = db.Where("patient_id in(?)", patientIds)
  2885. }
  2886. err = db.Select("patient_id,Count(id) as count,advice_date").Group("patient_id").Find(&advice).Error
  2887. return advice, err
  2888. } else {
  2889. return advice, err
  2890. }
  2891. }
  2892. func GetHisAdviceListNoExecution(advice_date int64, user_org_id int64, patientIds []int64) (advice []*models.HisNoDoctorAdvice, err error) {
  2893. db := XTReadDB().Model(advice).Where("status=1 and execution_state = 2")
  2894. if advice_date > 0 {
  2895. db = db.Where("advice_date= ?", advice_date)
  2896. }
  2897. if user_org_id > 0 {
  2898. db = db.Where("user_org_id =?", user_org_id)
  2899. }
  2900. if len(patientIds) > 0 {
  2901. db = db.Where("patient_id in(?)", patientIds)
  2902. }
  2903. err = db.Select("advice_date,patient_id,Count(id) as count").Group("patient_id").Find(&advice).Error
  2904. return advice, err
  2905. }
  2906. func GetProjectListNoExecution(record_date int64, user_org_id int64, patientIds []int64) (project []*models.NoHisPrescriptionProject, err error) {
  2907. db := XTReadDB().Model(&project).Where("status=1 and execution_state =0")
  2908. if record_date > 0 {
  2909. db = db.Where("record_date= ?", record_date)
  2910. }
  2911. if user_org_id > 0 {
  2912. db = db.Where("user_org_id =?", user_org_id)
  2913. }
  2914. if len(patientIds) > 0 {
  2915. db = db.Where("patient_id in(?)", patientIds)
  2916. }
  2917. err = db.Select("patient_id,Count(id) as count,record_date as advice_date").Group("patient_id").Find(&project).Error
  2918. return project, err
  2919. }
  2920. func GetPatientNoExecutionList(org_id int64, patientsIds []int64) (patient []*models.XtPatients, err error) {
  2921. err = XTReadDB().Where("user_org_id = ? and id in(?)", org_id, patientsIds).Find(&patient).Error
  2922. return patient, err
  2923. }
  2924. func FindHisConfig(org_id int64) (err error, config models.XtHisConfig) {
  2925. err = readDb.Model(&models.XtHisConfig{}).Where("status = 1 AND user_org_id = ?", org_id).First(&config).Error
  2926. return
  2927. }
  2928. func GetPatientDetailInformedConsent(patient_id int64, org_id int64) (models.XtPatients, error) {
  2929. patients := models.XtPatients{}
  2930. err := XTReadDB().Where("id = ? and status=1 and user_org_id =?", patient_id, org_id).Find(&patients).Error
  2931. return patients, err
  2932. }
  2933. func GetAllPrescriptionList(org_id int64) (prescription []*models.DialysisPrescription, err error) {
  2934. err = XTReadDB().Where("user_org_id = ? and status =1", org_id).Find(&prescription).Error
  2935. return prescription, err
  2936. }
  2937. func GetPatientScheudleListTwenty(org_id int64, scheduel_type int64, startime int64, endtime int64, schedule_date int64) (schedule []*models.ScheduleTen, err error) {
  2938. db := XTReadDB().Model(&schedule).Where("status= 1")
  2939. if org_id > 0 {
  2940. db = db.Where("user_org_id = ?", org_id)
  2941. }
  2942. if scheduel_type > 0 {
  2943. db = db.Where("schedule_type =?", scheduel_type)
  2944. }
  2945. if startime > 0 {
  2946. db = db.Where("schedule_date >=?", startime)
  2947. }
  2948. if endtime > 0 {
  2949. db = db.Where("schedule_date <=?", endtime)
  2950. }
  2951. if startime == 0 && endtime == 0 {
  2952. db = db.Where("schedule_date = ?", schedule_date)
  2953. }
  2954. err = db.Find(&schedule).Error
  2955. fmt.Println("err2330230320202023", err)
  2956. return schedule, err
  2957. }
  2958. func GetDialysisOrderCountTwentyEight(orgID int64, patient_id int64, recordDate int64) (models.VmDialysisOrder, error) {
  2959. order := models.VmDialysisOrder{}
  2960. 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
  2961. return order, err
  2962. }
  2963. func GetDialysisOrderCountTenEight(patient_id int64, recordDate int64) (models.VmDialysisOrder, error) {
  2964. order := models.VmDialysisOrder{}
  2965. err := XTReadDB().Where("patient_id = ? and dialysis_date = ? and status = 1", patient_id, recordDate).Last(&order).Error
  2966. return order, err
  2967. }
  2968. func GetLastAcceptRecrods(patient_id int64) (models.ReceiveTreatmentAsses, error) {
  2969. treatmentAsses := models.ReceiveTreatmentAsses{}
  2970. err := XTReadDB().Where("patient_id = ? and status= 1 and admission_number!=''", patient_id).Last(&treatmentAsses).Error
  2971. return treatmentAsses, err
  2972. }
  2973. func GetOutPrintList(user_org_id int64) (models.XtFallOutPrint, error) {
  2974. outPrint := models.XtFallOutPrint{}
  2975. err := XTReadDB().Where("user_org_id = ? and status =1", user_org_id).Find(&outPrint).Error
  2976. return outPrint, err
  2977. }
  2978. func CreateFallOutBedPrint(outPrint models.XtFallOutPrint) error {
  2979. err := XTWriteDB().Create(&outPrint).Error
  2980. return err
  2981. }
  2982. func UpdateFallOutBedPrint(id int64, content string) error {
  2983. err := XTWriteDB().Model(&models.XtFallOutPrint{}).Where("id = ? and status=1", id).Updates(map[string]interface{}{"content": content}).Error
  2984. return err
  2985. }
  2986. func GetFallOutBedPrintList(user_org_id int64) (models.XtFallOutPrint, error) {
  2987. outPrint := models.XtFallOutPrint{}
  2988. err := XTReadDB().Where("user_org_id = ? and status=1", user_org_id).Find(&outPrint).Error
  2989. return outPrint, err
  2990. }
  2991. func GetFilterInformed(user_org_id int64) (models.XtFilterInformed, error) {
  2992. informed := models.XtFilterInformed{}
  2993. err := XTReadDB().Where("user_org_id = ? and status =1", user_org_id).Find(&informed).Error
  2994. return informed, err
  2995. }
  2996. func CreateFilterInformed(informed models.XtFilterInformed) error {
  2997. err := XTWriteDB().Create(&informed).Error
  2998. return err
  2999. }
  3000. func UpdateFilterInformed(id int64, content string) error {
  3001. err := XTWriteDB().Model(&models.XtFilterInformed{}).Where("id = ? and status =1", id).Updates(map[string]interface{}{"content": content}).Error
  3002. return err
  3003. }
  3004. func GetProfundaInformed(user_org_id int64) (models.XtProfundaInformed, error) {
  3005. informed := models.XtProfundaInformed{}
  3006. err := XTReadDB().Where("user_org_id = ? and status =1", user_org_id).Find(&informed).Error
  3007. return informed, err
  3008. }
  3009. func CreateProfundaInformed(informed models.XtProfundaInformed) error {
  3010. err := XTWriteDB().Create(&informed).Error
  3011. return err
  3012. }
  3013. func UpdateProfundaInformed(id int64, content string) error {
  3014. profundaInformed := models.XtProfundaInformed{}
  3015. err := XTWriteDB().Model(&profundaInformed).Where("id = ? and status=1", id).Updates(map[string]interface{}{"content": content}).Error
  3016. return err
  3017. }
  3018. func GetArterOvenousList(user_org_id int64) (models.XtArterOvenous, error) {
  3019. ovenous := models.XtArterOvenous{}
  3020. err := XTReadDB().Where("user_org_id = ? and status=1", user_org_id).Find(&ovenous).Error
  3021. return ovenous, err
  3022. }
  3023. func CreateArterOvernous(ovenous models.XtArterOvenous) error {
  3024. err := XTWriteDB().Create(&ovenous).Error
  3025. return err
  3026. }
  3027. func UpdateArterOvernous(id int64, content string) error {
  3028. err := XTWriteDB().Model(&models.XtArterOvenous{}).Where("id = ? and status =1", id).Updates(map[string]interface{}{"content": content}).Error
  3029. return err
  3030. }
  3031. func GetHightRikInformed(user_org_id int64) (models.XtHightRisk, error) {
  3032. risk := models.XtHightRisk{}
  3033. err := XTReadDB().Where("user_org_id = ? and status =1", user_org_id).Find(&risk).Error
  3034. return risk, err
  3035. }
  3036. func CreateHighRik(risk models.XtHightRisk) error {
  3037. err := XTWriteDB().Create(&risk).Error
  3038. return err
  3039. }
  3040. func UpdateHighRik(id int64, content string) error {
  3041. err := XTWriteDB().Model(&models.XtHightRisk{}).Where("id = ? and status =1", id).Updates(map[string]interface{}{"content": content}).Error
  3042. return err
  3043. }
  3044. func GetAnticoagulation(user_org_id int64) (models.XtAuticoagutionPrint, error) {
  3045. auticoagutionPrint := models.XtAuticoagutionPrint{}
  3046. err := XTReadDB().Where("user_org_id = ? and status =1", user_org_id).Find(&auticoagutionPrint).Error
  3047. return auticoagutionPrint, err
  3048. }
  3049. func CreateAnticoagulation(auticoagutionPrint models.XtAuticoagutionPrint) error {
  3050. err := XTWriteDB().Create(&auticoagutionPrint).Error
  3051. return err
  3052. }
  3053. func UpdateAnticoagulation(id int64, content string) error {
  3054. err := XTWriteDB().Model(&models.XtAuticoagutionPrint{}).Where("id = ? and status =1", id).Updates(map[string]interface{}{"content": content}).Error
  3055. return err
  3056. }
  3057. func GetCriticalInfomed(org_id int64) (models.XtCriticalinfomedPrint, error) {
  3058. criticalinfomedPrint := models.XtCriticalinfomedPrint{}
  3059. err := XTReadDB().Where("user_org_id = ? and status=1", org_id).Find(&criticalinfomedPrint).Error
  3060. return criticalinfomedPrint, err
  3061. }
  3062. func CreatedCriticalInfomed(criticalinfomedPrint models.XtCriticalinfomedPrint) error {
  3063. err := XTWriteDB().Create(&criticalinfomedPrint).Error
  3064. return err
  3065. }
  3066. func UpdateCriticalInfomed(id int64, content string) error {
  3067. err := XTWriteDB().Model(&models.XtCriticalinfomedPrint{}).Where("id = ? and status=1", id).Updates(map[string]interface{}{"content": content}).Error
  3068. return err
  3069. }
  3070. func GetHemoperfusionInformed(user_org_id int64) (models.XtHemoperfusionmedPrint, error) {
  3071. hemoperfusionmedPrint := models.XtHemoperfusionmedPrint{}
  3072. err := XTReadDB().Where("user_org_id = ? and status =1", user_org_id).Find(&hemoperfusionmedPrint).Error
  3073. return hemoperfusionmedPrint, err
  3074. }
  3075. func CreateHemoperfusionInformed(hemoperfusionmedPrint models.XtHemoperfusionmedPrint) error {
  3076. err := XTWriteDB().Create(&hemoperfusionmedPrint).Error
  3077. return err
  3078. }
  3079. func UpdateHemoperfusionInformed(id int64, content string) error {
  3080. err := XTWriteDB().Model(&models.XtHemoperfusionmedPrint{}).Where("id = ? and status=1", id).Updates(map[string]interface{}{"content": content}).Error
  3081. return err
  3082. }
  3083. func GetBringInformed(org_id int64) (models.XtBringinformedPrint, error) {
  3084. bringinformedPrint := models.XtBringinformedPrint{}
  3085. err := XTReadDB().Where("user_org_id = ? and status=1", org_id).Find(&bringinformedPrint).Error
  3086. return bringinformedPrint, err
  3087. }
  3088. func CreateBringInformed(bringinformedPrint models.XtBringinformedPrint) error {
  3089. err := XTWriteDB().Create(&bringinformedPrint).Error
  3090. return err
  3091. }
  3092. func UpdateBringInformed(id int64, content string) error {
  3093. err := XTWriteDB().Model(&models.XtBringinformedPrint{}).Where("id =? and status= 1", id).Updates(map[string]interface{}{"content": content}).Error
  3094. return err
  3095. }
  3096. func GetCatheterization(user_org_id int64) (models.XtCathetercationPrint, error) {
  3097. cathetercationPrint := models.XtCathetercationPrint{}
  3098. err := XTReadDB().Where("user_org_id = ? and status= 1", user_org_id).Find(&cathetercationPrint).Error
  3099. return cathetercationPrint, err
  3100. }
  3101. func CreateCatheteriztion(cathetercationPrint models.XtCathetercationPrint) error {
  3102. err := XTWriteDB().Create(&cathetercationPrint).Error
  3103. return err
  3104. }
  3105. func UpdateCatheterization(id int64, content string) error {
  3106. err := XTWriteDB().Model(models.XtCathetercationPrint{}).Where("id=? and status =1", id).Updates(map[string]interface{}{"content": content}).Error
  3107. return err
  3108. }
  3109. func GetDialyzerInformed(org_id int64) (models.XtDialyszerinformedPrint, error) {
  3110. dialyszerinformedPrint := models.XtDialyszerinformedPrint{}
  3111. err := XTReadDB().Where("user_org_id = ? and status=1", org_id).Find(&dialyszerinformedPrint).Error
  3112. return dialyszerinformedPrint, err
  3113. }
  3114. func CreateDialyzerInformed(dialyszerinformedPrint models.XtDialyszerinformedPrint) error {
  3115. err := XTWriteDB().Create(&dialyszerinformedPrint).Error
  3116. return err
  3117. }
  3118. func UpdateDialyszerInformed(id int64, content string) error {
  3119. err := XTWriteDB().Model(&models.XtDialyszerinformedPrint{}).Where("id = ? and status=1", id).Updates(map[string]interface{}{"content": content}).Error
  3120. return err
  3121. }
  3122. func GetTheapyInformed(org_id int64) (models.XtTheapyinformedPrint, error) {
  3123. theapyinformedPrint := models.XtTheapyinformedPrint{}
  3124. err := XTReadDB().Where("user_org_id = ? and status=1", org_id).Find(&theapyinformedPrint).Error
  3125. return theapyinformedPrint, err
  3126. }
  3127. func CreateTheapyInformed(theapyinformedPrint models.XtTheapyinformedPrint) error {
  3128. err := XTWriteDB().Create(&theapyinformedPrint).Error
  3129. return err
  3130. }
  3131. func UpdateTheaphInformed(id int64, content string) error {
  3132. err := XTWriteDB().Model(&models.XtTheapyinformedPrint{}).Where("id = ? and status =1", id).Updates(map[string]interface{}{"content": content}).Error
  3133. return err
  3134. }
  3135. func GetPatientMedicalHistory(patient_id int64, user_org_id int64) (models.XtMedicalHistory, error) {
  3136. history := models.XtMedicalHistory{}
  3137. err := XTReadDB().Model(&history).Where("patient_id = ? and user_org_id = ? and status=1", patient_id, user_org_id).Find(&history).Error
  3138. return history, err
  3139. }
  3140. func UpdatePatientMedicalHistory(id int64, content string) error {
  3141. history := models.XtMedicalHistory{}
  3142. err := XTWriteDB().Model(&history).Where("id = ? and status=1", id).Updates(map[string]interface{}{"content": content}).Error
  3143. return err
  3144. }
  3145. func CreatePatientMedicalHistory(history models.XtMedicalHistory) error {
  3146. err := XTWriteDB().Create(&history).Error
  3147. return err
  3148. }
  3149. func GetPatientMedicalList(patient_id int64, user_org_id int64) (models.XtMedicalHistory, error) {
  3150. history := models.XtMedicalHistory{}
  3151. err := XTReadDB().Where("patient_id = ? and user_org_id = ? and status=1", patient_id, user_org_id).Find(&history).Error
  3152. return history, err
  3153. }
  3154. func CreatePatientFallassessment(tx *gorm.DB, patientFallassessment models.XtPatientFallassessment) (err error) {
  3155. err = tx.Create(&patientFallassessment).Error
  3156. if err == gorm.ErrRecordNotFound {
  3157. tx.Rollback()
  3158. return
  3159. }
  3160. return
  3161. }
  3162. func SavePatientFallassessment(tx *gorm.DB, patientFallassessment models.XtPatientFallassessment) (err error) {
  3163. err = tx.Save(&patientFallassessment).Error
  3164. if err == gorm.ErrRecordNotFound {
  3165. tx.Rollback()
  3166. return
  3167. }
  3168. return
  3169. }
  3170. func GetPatientFallsessMentList(patient_id int64, user_org_id int64, limit int64, page int64) (list []*models.XtPatientFallassessment, total int64, err error) {
  3171. offset := (page - 1) * limit
  3172. db := XTReadDB().Model(&list).Where("status =1")
  3173. if patient_id > 0 {
  3174. db = db.Where("patient_id = ?", patient_id)
  3175. }
  3176. if user_org_id > 0 {
  3177. db = db.Where("user_org_id = ?", user_org_id)
  3178. }
  3179. err = db.Count(&total).Offset(offset).Limit(limit).Find(&list).Error
  3180. return list, total, err
  3181. }
  3182. func GetPatientFallsessMentListOne(ids []string, user_org_id int64) (list []*models.XtPatientFallassessment, err error) {
  3183. db := XTReadDB().Model(&list).Where("status =1")
  3184. if len(ids) > 0 {
  3185. db = db.Where("id in(?)", ids)
  3186. }
  3187. if user_org_id > 0 {
  3188. db = db.Where("user_org_id = ?", user_org_id)
  3189. }
  3190. err = db.Find(&list).Error
  3191. return list, err
  3192. }
  3193. func GetPatientFallsessmentById(id int64, user_org_id int64) (models.XtPatientFallassessment, error) {
  3194. fallassessment := models.XtPatientFallassessment{}
  3195. err := XTReadDB().Where("id = ? and user_org_id = ?", id, user_org_id).Find(&fallassessment).Error
  3196. return fallassessment, err
  3197. }
  3198. func DeletePatientFallsessment(id int64, user_org_id int64) (models.XtPatientFallassessment, error) {
  3199. fallassessment := models.XtPatientFallassessment{}
  3200. err := XTWriteDB().Model(&fallassessment).Where("id =? and status=1", id).Updates(map[string]interface{}{"status": 0}).Error
  3201. return fallassessment, err
  3202. }
  3203. func CreatePadFallssessment(fallssessment models.XtPatientPedFallssessment) error {
  3204. err := XTWriteDB().Create(&fallssessment).Error
  3205. return err
  3206. }
  3207. func UpdatePedFallssessment(fallssessment models.XtPatientPedFallssessment) error {
  3208. err := XTWriteDB().Save(&fallssessment).Error
  3209. return err
  3210. }
  3211. func GetBedDiatricFallassessmentList(patient_id int64, is_type int64, user_org_id int64, limit int64, page int64) (list []*models.XtPatientPedFallssessment, total int64, err error) {
  3212. offset := (page - 1) * limit
  3213. db := XTReadDB().Model(&list).Where("status =1")
  3214. if patient_id > 0 {
  3215. db = db.Where("patient_id = ?", patient_id)
  3216. }
  3217. if user_org_id > 0 {
  3218. db = db.Where("user_org_id = ?", user_org_id)
  3219. }
  3220. if is_type > 0 {
  3221. db = db.Where("type = ?", is_type)
  3222. }
  3223. err = db.Count(&total).Offset(offset).Limit(limit).Find(&list).Error
  3224. return list, total, err
  3225. }
  3226. func GetPedPatientFallassessmentById(id int64) (models.XtPatientPedFallssessment, error) {
  3227. fallassessment := models.XtPatientPedFallssessment{}
  3228. err := XTReadDB().Where("id = ? and status=1", id).Find(&fallassessment).Error
  3229. return fallassessment, err
  3230. }
  3231. func DeletePadPatientFallassment(id int64) (err error) {
  3232. err = XTWriteDB().Model(&models.XtPatientPedFallssessment{}).Where("id=? and status=1", id).Updates(map[string]interface{}{"status": 0}).Error
  3233. return err
  3234. }
  3235. func GetPatientCritaicalInformed(user_org_id int64) (models.XtPatientCritical, error) {
  3236. patientCritical := models.XtPatientCritical{}
  3237. err := XTReadDB().Where("user_org_id = ? and status=1", user_org_id).Find(&patientCritical).Error
  3238. return patientCritical, err
  3239. }
  3240. func CreatePatientCritaicalinformed(critical models.XtPatientCritical) error {
  3241. err = XTWriteDB().Create(&critical).Error
  3242. return err
  3243. }
  3244. func UpdatePatientCritaicalInformed(id int64, content string) error {
  3245. err := XTWriteDB().Model(&models.XtPatientCritical{}).Where("id = ? and status=1", id).Updates(map[string]interface{}{"content": content}).Error
  3246. return err
  3247. }
  3248. func GetPatientCriticalByUserOrgId(user_org_id int64) (models.XtPatientCriticalOne, error) {
  3249. criticalOne := models.XtPatientCriticalOne{}
  3250. err := XTReadDB().Where("user_org_id = ? and status=1", user_org_id).Find(&criticalOne).Error
  3251. return criticalOne, err
  3252. }
  3253. func CreatePatientCritical(patient models.XtPatientCriticalOne) error {
  3254. err := XTWriteDB().Create(&patient).Error
  3255. return err
  3256. }
  3257. func UpdatePatientCriticalOne(user_org_id int64, content string) error {
  3258. err := XTWriteDB().Model(&models.XtPatientCriticalOne{}).Where("user_org_id = ? and status=1", user_org_id).Updates(map[string]interface{}{"content": content}).Error
  3259. return err
  3260. }
  3261. func GetPatientCriticalOneList(user_org_id int64) (models.XtPatientCriticalOne, error) {
  3262. criticalOne := models.XtPatientCriticalOne{}
  3263. err := XTReadDB().Where("user_org_id = ? and status =1", user_org_id).Find(&criticalOne).Error
  3264. return criticalOne, err
  3265. }
  3266. func CreatePatientPedList(fallssessment models.XtPatientPedFallssessment) error {
  3267. err := XTWriteDB().Create(&fallssessment).Error
  3268. return err
  3269. }
  3270. func UpdatePatientPedList(fallssessment models.XtPatientPedFallssessment) error {
  3271. err := XTWriteDB().Save(&fallssessment).Error
  3272. return err
  3273. }
  3274. func GetPatientFallsessMentListTwo(ids []string, user_org_id int64) (list []*models.XtPatientFallassessment, err error) {
  3275. db := XTReadDB().Model(&list).Where("status =1")
  3276. if len(ids) > 0 {
  3277. db = db.Where("id in(?)", ids)
  3278. }
  3279. if user_org_id > 0 {
  3280. db = db.Where("user_org_id = ?", user_org_id)
  3281. }
  3282. err = db.Find(&list).Error
  3283. return list, err
  3284. }
  3285. func CeatePatientSign(braden models.XtPatientBraden) error {
  3286. err := XTWriteDB().Create(&braden).Error
  3287. return err
  3288. }
  3289. func GetPatientBradenList(patient_id int64, limit int64, page int64, user_org_id int64) (list []*models.XtPatientBraden, total int64, err error) {
  3290. offset := (page - 1) * limit
  3291. db := XTReadDB().Model(&list).Where("status =1")
  3292. if patient_id > 0 {
  3293. db = db.Where("patient_id = ?", patient_id)
  3294. }
  3295. if user_org_id > 0 {
  3296. db = db.Where("user_org_id = ?", user_org_id)
  3297. }
  3298. err = db.Count(&total).Offset(offset).Limit(limit).Find(&list).Error
  3299. return list, total, err
  3300. }
  3301. func GetPatientBradenById(id int64) (models.XtPatientBraden, error) {
  3302. patientBraden := models.XtPatientBraden{}
  3303. err := XTReadDB().Where("id = ? and status=1", id).Find(&patientBraden).Error
  3304. return patientBraden, err
  3305. }
  3306. func SavePatientBraden(braden models.XtPatientBraden) error {
  3307. err := XTWriteDB().Save(&braden).Error
  3308. return err
  3309. }
  3310. func DeletePatientBraden(id int64) error {
  3311. patientBraden := models.XtPatientBraden{}
  3312. err := XTWriteDB().Model(&patientBraden).Where("id = ? and status=1", id).Updates(map[string]interface{}{"status": 0}).Error
  3313. return err
  3314. }
  3315. func GetBatchPatientBraden(ids []string, user_org_id int64) (list []*models.XtPatientBraden, err error) {
  3316. db := XTReadDB().Model(&list).Where("status=1")
  3317. if len(ids) > 0 {
  3318. db = db.Where("id in(?)", ids)
  3319. }
  3320. if user_org_id > 0 {
  3321. db = db.Where("user_org_id =?", user_org_id)
  3322. }
  3323. err = db.Find(&list).Error
  3324. return list, err
  3325. }
  3326. func GetAllPatientListMap(user_org_id int64) (list []*models.XtAgePatients, err error) {
  3327. err = XTReadDB().Where("user_org_id =? and status=1 and lapseto = 1", user_org_id).Find(&list).Error
  3328. return list, err
  3329. }
  3330. func GetLastBefor(patient_id int64) (models.XtAssessmentBeforeDislysis, error) {
  3331. beforeDislysis := models.XtAssessmentBeforeDislysis{}
  3332. 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
  3333. return beforeDislysis, err
  3334. }
  3335. func GetLastRecordAfter(patient_id int64) (models.XtAssessmentAfterDislysis, error) {
  3336. afterDislysis := models.XtAssessmentAfterDislysis{}
  3337. 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
  3338. return afterDislysis, err
  3339. }
  3340. func GetInspectionByPatientId(patient_id int64, item_id int64) (models.XtInspection, error) {
  3341. inspection := models.XtInspection{}
  3342. 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
  3343. return inspection, err
  3344. }
  3345. func DeleteSolutionById(id int64) (models.DialysisSolution, error) {
  3346. solution := models.DialysisSolution{}
  3347. err := XTWriteDB().Model(&solution).Where("id=? and status=1", id).Updates(map[string]interface{}{"status": 12}).Error
  3348. return solution, err
  3349. }
  3350. func GetPatientBeforRecordList(patient_id int64, user_org_id int64) (befor []*models.XtAssessmentBeforeDislysis, err error) {
  3351. 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
  3352. return befor, err
  3353. }
  3354. func GetPatientAfterRecordList(patient_id int64, user_org_id int64) (after []*models.XtAssessmentAfterDislysis, err error) {
  3355. 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
  3356. return after, err
  3357. }
  3358. func GetMonitorRecordByGroup(patient_id int64, user_org_id int64) (monitor []*models.MonitoringRecordOne, err error) {
  3359. 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
  3360. return monitor, err
  3361. }
  3362. func GetMonitorRecordList(patient_id int64, monitor_date int64, user_org_id int64) (monitor []*models.MonitoringRecord, err error) {
  3363. 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
  3364. return monitor, err
  3365. }
  3366. func GetDialysisTotalCountByTime(user_org_id int64, start_time int64, end_time int64, patient_id int64) (order []*models.XtDialysisOrder, err error) {
  3367. db := XTReadDB().Table("xt_dialysis_order").Where("status =1")
  3368. if user_org_id > 0 {
  3369. db = db.Where("user_org_id =?", user_org_id)
  3370. }
  3371. if start_time > 0 {
  3372. db = db.Where("dialysis_date >=?", start_time)
  3373. }
  3374. if end_time > 0 {
  3375. db = db.Where("dialysis_date <=?", end_time)
  3376. }
  3377. if patient_id > 0 {
  3378. db = db.Where("patient_id = ?", patient_id)
  3379. }
  3380. err = db.Find(&order).Error
  3381. return order, err
  3382. }
  3383. func GetTreamentPland(user_org_id int64) (models.XtTreatmentPlan, error) {
  3384. treatmentPlan := models.XtTreatmentPlan{}
  3385. err := XTReadDB().Where("user_org_id = ? and status =1", user_org_id).Find(&treatmentPlan).Error
  3386. return treatmentPlan, err
  3387. }
  3388. func CreateTreatqmentPland(plan models.XtTreatmentPlan) error {
  3389. err := XTWriteDB().Create(&plan).Error
  3390. return err
  3391. }
  3392. func UpdateTheatmentPland(id int64, content string) error {
  3393. err := XTWriteDB().Model(&models.XtTreatmentPlan{}).Where("id = ? and status =1", id).Updates(map[string]interface{}{"content": content}).Error
  3394. return err
  3395. }