patient_service.go 157KB

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