mobile_dialysis_service.go 197KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101
  1. package service
  2. import (
  3. "XT_New/models"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/jinzhu/gorm"
  7. "strconv"
  8. "time"
  9. )
  10. // func GetSchedualPatients(orgID int64) ([]*MDialysisScheduleVM, error) {
  11. // var vms []*MDialysisScheduleVM
  12. // err := readDb.
  13. // Table("xt_schedule as sch").
  14. // Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  15. // Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  16. // Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  17. // Preload("TreatmentMode", "status = 1").
  18. // Preload("DialysisOrder", "status = 1 AND user_org_id = ?", orgID).
  19. // Preload("DialysisOrder.MonitoringRecords", "status = 1 AND user_org_id = ?", orgID).
  20. // Where("sch.status = 1 AND sch.user_org_id = ? AND schedule_date = 1536768000", orgID).
  21. // Find(&vms).
  22. // Error
  23. // return vms, err
  24. // }
  25. func MobileGetDialysisScheduals(orgID int64, scheduleDate int64, scheduleType int64) ([]*MDialysisScheduleVMForList, error) {
  26. var vms []*MDialysisScheduleVMForList
  27. db := readDb.
  28. Table("xt_schedule as sch").
  29. Where("sch.status = 1 AND sch.user_org_id = ?", orgID)
  30. if scheduleDate != 0 {
  31. db = db.Where("schedule_date = ?", scheduleDate)
  32. }
  33. if scheduleType != 0 {
  34. db = db.Where("schedule_type = ?", scheduleType)
  35. }
  36. err := db.Find(&vms).Error
  37. return vms, err
  38. }
  39. func MobileGetWaitingScheduals(orgID int64, scheduleDate int64) ([]*MDialysisScheduleVM, error) {
  40. var vms []*MDialysisScheduleVM
  41. db := readDb.
  42. Table("xt_schedule as sch").
  43. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  44. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  45. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  46. // Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  47. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  48. Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ? AND assessment_date = ? ", orgID, scheduleDate).
  49. Preload("DialysisOrder", "status = 1 AND user_org_id = ?", orgID).
  50. Preload("Advices", "status = 1 AND user_org_id = ? AND advice_type = 2 ", orgID).
  51. Preload("TreatmentMode", "status = 1").
  52. Preload("TreatmentSummary", "status = 1 AND user_org_id = ?", orgID).
  53. Where("sch.status = 1 AND sch.user_org_id = ?", orgID)
  54. if scheduleDate != 0 {
  55. db = db.Where("schedule_date = ?", scheduleDate)
  56. }
  57. err := db.Find(&vms).Error
  58. return vms, err
  59. }
  60. type VMTreatmentSummary struct {
  61. ID int64 `gorm:"column:id" json:"id"`
  62. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id"`
  63. PatientId int64 `gorm:"column:patient_id" json:"patient_id"`
  64. AssessmentDate int64 `gorm:"column:assessment_date" json:"assessment_date"`
  65. DialysisSummary string `gorm:"column:dialysis_summary" json:"dialysis_summary" form:"dialysis_summary"`
  66. }
  67. func (VMTreatmentSummary) TableName() string {
  68. return "xt_treatment_summary"
  69. }
  70. type AssessmentAfterDislysis struct {
  71. ID int64 `gorm:"column:id" json:"id"`
  72. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id"`
  73. PatientId int64 `gorm:"column:patient_id" json:"patient_id"`
  74. AssessmentDate int64 `gorm:"column:assessment_date" json:"assessment_date"`
  75. WeightAfter float64 `gorm:"column:weight_after" json:"weight_after"`
  76. Status int64 `gorm:"column:status" json:"status"`
  77. }
  78. func (AssessmentAfterDislysis) TableName() string {
  79. return "xt_assessment_after_dislysis"
  80. }
  81. type MDialysisScheduleVM struct {
  82. ID int64 `gorm:"column:id" json:"id"`
  83. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id"`
  84. PartitionId int64 `gorm:"column:partition_id" json:"partition_id"`
  85. BedId int64 `gorm:"column:bed_id" json:"bed_id"`
  86. PatientId int64 `gorm:"column:patient_id" json:"patient_id"`
  87. ScheduleDate int64 `gorm:"column:schedule_date" json:"schedule_date"`
  88. ScheduleType int64 `gorm:"column:schedule_type" json:"schedule_type"`
  89. ScheduleWeek int64 `gorm:"column:schedule_week" json:"schedule_week"`
  90. ModeId int64 `gorm:"column:mode_id" json:"mode_id"`
  91. Status int64 `gorm:"column:status" json:"status"`
  92. SchedualPatient *MSchedualPatientVMList `gorm:"ForeignKey:PatientId" json:"patient"`
  93. DeviceNumber *MDeviceNumberVM `gorm:"ForeignKey:BedId" json:"device_number"`
  94. DialysisOrder *MDialysisOrderVMList `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"dialysis_order"`
  95. Prescription *models.DialysisPrescriptionList `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"prescription"`
  96. AssessmentBeforeDislysis *models.PredialysisEvaluationList `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"assessment_before_dislysis"`
  97. AssessmentAfterDislysis *AssessmentAfterDislysis `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"assessment_after_dislysis"`
  98. HisAdvices []*VMHisDoctorAdviceInfo `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"his_doctor_advice"`
  99. Advices []*VMDoctorAdvice `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"doctor_advice"`
  100. TreatmentSummary *VMTreatmentSummary `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"treatment_summary"`
  101. }
  102. func (MDialysisScheduleVM) TableName() string {
  103. return "xt_schedule"
  104. }
  105. type MDialysisScheduleVMForList struct {
  106. ID int64 `gorm:"column:id" json:"id"`
  107. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id"`
  108. PartitionId int64 `gorm:"column:partition_id" json:"partition_id"`
  109. BedId int64 `gorm:"column:bed_id" json:"bed_id"`
  110. PatientId int64 `gorm:"column:patient_id" json:"patient_id"`
  111. ScheduleDate int64 `gorm:"column:schedule_date" json:"schedule_date"`
  112. ScheduleType int64 `gorm:"column:schedule_type" json:"schedule_type"`
  113. ScheduleWeek int64 `gorm:"column:schedule_week" json:"schedule_week"`
  114. ModeId int64 `gorm:"column:mode_id" json:"mode_id"`
  115. Status int64 `gorm:"column:status" json:"status"`
  116. SchedualPatient *models.MSchedualPatientList `gorm:"ForeignKey:PatientId" json:"patient"`
  117. DeviceNumber *models.MDeviceNumberForList `gorm:"ForeignKey:BedId" json:"device_number"`
  118. DialysisOrder *models.MDialysisOrderForList `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"dialysis_order"`
  119. Prescription *models.DialysisPrescriptionList `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"prescription"`
  120. AssessmentBeforeDislysis *models.PredialysisEvaluationList `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"assessment_before_dislysis"`
  121. AssessmentAfterDislysis *models.VMAssessmentAfterDislysis `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"assessment_after_dislysis"`
  122. HisAdvices []VMHisDoctorAdviceInfo `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"his_doctor_advice"`
  123. Advices []models.VMDoctorAdviceForList `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"doctor_advice"`
  124. TreatmentSummary *models.VMTreatmentSummaryForList `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"treatment_summary"`
  125. }
  126. func (MDialysisScheduleVMForList) TableName() string {
  127. return "xt_schedule"
  128. }
  129. type MDeviceNumberVM struct {
  130. models.DeviceNumber
  131. Zone *models.DeviceZone `gorm:"ForeignKey:ZoneID" json:"zone"`
  132. }
  133. func (MDeviceNumberVM) TableName() string {
  134. return "xt_device_number"
  135. }
  136. type MSchedualPatientVMList struct {
  137. ID int64 `gorm:"column:id" json:"id" form:"id"`
  138. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id" form:"user_org_id"`
  139. UserId int64 `gorm:"column:user_id" json:"user_id" form:"user_id"`
  140. PatientType int64 `gorm:"column:patient_type" json:"patient_type" form:"patient_type"`
  141. DialysisNo string `gorm:"column:dialysis_no" json:"dialysis_no" form:"dialysis_no"`
  142. Avatar string `gorm:"column:avatar" json:"avatar" form:"avatar"`
  143. Gender int64 `gorm:"column:gender" json:"gender" form:"gender"`
  144. Birthday int64 `gorm:"column:birthday" json:"birthday" form:"birthday"`
  145. Age int64 `gorm:"column:age" json:"age"`
  146. Name string `gorm:"column:name" json:"name" form:"name"`
  147. IdCardNo string `gorm:"column:id_card_no" json:"id_card_no" form:"id_card_no"`
  148. UserSysBeforeCount int64 `gorm:"column:user_sys_before_count" json:"user_sys_before_count" form:"user_sys_before_count"`
  149. TrobleShoot int64 `gorm:"column:troble_shoot" json:"troble_shoot" form:"troble_shoot"`
  150. }
  151. func (MSchedualPatientVMList) TableName() string {
  152. return "xt_patients"
  153. }
  154. type MSchedualPatientVM struct {
  155. ID int64 `gorm:"column:id" json:"id" form:"id"`
  156. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id" form:"user_org_id"`
  157. UserId int64 `gorm:"column:user_id" json:"user_id" form:"user_id"`
  158. PatientType int64 `gorm:"column:patient_type" json:"patient_type" form:"patient_type"`
  159. Avatar string `gorm:"column:avatar" json:"avatar" form:"avatar"`
  160. DialysisNo string `gorm:"column:dialysis_no" json:"dialysis_no" form:"dialysis_no"`
  161. AdmissionNumber string `gorm:"column:admission_number" json:"admission_number" form:"admission_number"`
  162. Source int64 `gorm:"column:source" json:"source" form:"source"`
  163. Lapseto int64 `gorm:"column:lapseto" json:"lapseto" form:"lapseto"`
  164. PartitionId int64 `gorm:"column:partition_id" json:"partition_id" form:"partition_id"`
  165. BedId int64 `gorm:"column:bed_id" json:"bed_id" form:"bed_id"`
  166. Name string `gorm:"column:name" json:"name" form:"name"`
  167. Alias string `gorm:"column:alias" json:"alias" form:"alias"`
  168. Gender int64 `gorm:"column:gender" json:"gender" form:"gender"`
  169. Nation string `gorm:"column:nation" json:"nation" form:"nation"`
  170. NativePlace string `gorm:"column:native_place" json:"native_place" form:"native_place"`
  171. MaritalStatus int64 `gorm:"column:marital_status" json:"marital_status" form:"marital_status"`
  172. IdCardNo string `gorm:"column:id_card_no" json:"id_card_no" form:"id_card_no"`
  173. Birthday int64 `gorm:"column:birthday" json:"birthday" form:"birthday"`
  174. ReimbursementWayId int64 `gorm:"column:reimbursement_way_id" json:"reimbursement_way_id" form:"reimbursement_way_id"`
  175. HealthCareType int64 `gorm:"column:health_care_type" json:"health_care_type" form:"health_care_type"`
  176. HealthCareNo string `gorm:"column:health_care_no" json:"health_care_no" form:"health_care_no"`
  177. HealthCareDueDate int64 `gorm:"column:health_care_due_date" json:"health_care_due_date" form:"health_care_due_date"`
  178. Height int64 `gorm:"column:height" json:"height" form:"height"`
  179. BloodType int64 `gorm:"column:blood_type" json:"blood_type" form:"blood_type"`
  180. Rh int64 `gorm:"column:rh" json:"rh" form:"rh"`
  181. HealthCareDueAlertDate int64 `gorm:"column:health_care_due_alert_date" json:"health_care_due_alert_date" form:"health_care_due_alert_date"`
  182. EducationLevel int64 `gorm:"column:education_level" json:"education_level" form:"education_level"`
  183. Profession int64 `gorm:"column:profession" json:"profession" form:"profession"`
  184. Phone string `gorm:"column:phone" json:"phone" form:"phone"`
  185. HomeTelephone string `gorm:"column:home_telephone" json:"home_telephone" form:"home_telephone"`
  186. RelativePhone string `gorm:"column:relative_phone" json:"relative_phone" form:"relative_phone"`
  187. RelativeRelations string `gorm:"column:relative_relations" json:"relative_relations" form:"relative_relations"`
  188. HomeAddress string `gorm:"column:home_address" json:"home_address" form:"home_address"`
  189. WorkUnit string `gorm:"column:work_unit" json:"work_unit" form:"work_unit"`
  190. UnitAddress string `gorm:"column:unit_address" json:"unit_address" form:"unit_address"`
  191. Children int64 `gorm:"column:children" json:"children" form:"children"`
  192. ReceivingDate int64 `gorm:"column:receiving_date" json:"receiving_date" form:"receiving_date"`
  193. IsHospitalFirstDialysis int64 `gorm:"column:is_hospital_first_dialysis" json:"is_hospital_first_dialysis" form:"is_hospital_first_dialysis"`
  194. FirstDialysisDate int64 `gorm:"column:first_dialysis_date" json:"first_dialysis_date" form:"first_dialysis_date"`
  195. FirstDialysisHospital string `gorm:"column:first_dialysis_hospital" json:"first_dialysis_hospital" form:"first_dialysis_hospital"`
  196. InductionPeriod int64 `gorm:"column:induction_period" json:"induction_period" form:"induction_period"`
  197. InitialDialysis int64 `gorm:"column:initial_dialysis" json:"initial_dialysis" form:"initial_dialysis"`
  198. TotalDialysis int64 `gorm:"column:total_dialysis" json:"total_dialysis" form:"total_dialysis"`
  199. AttendingDoctorId int64 `gorm:"column:attending_doctor_id" json:"attending_doctor_id" form:"attending_doctor_id"`
  200. HeadNurseId int64 `gorm:"column:head_nurse_id" json:"head_nurse_id" form:"head_nurse_id"`
  201. Evaluate string `gorm:"column:evaluate" json:"evaluate" form:"evaluate"`
  202. Diagnose string `gorm:"column:diagnose" json:"diagnose" form:"diagnose"`
  203. Remark string `gorm:"column:remark" json:"remark" form:"remark"`
  204. RegistrarsId int64 `gorm:"column:registrars_id" json:"registrars_id" form:"registrars_id"`
  205. Registrars string `gorm:"column:registrars" json:"registrars" form:"registrars"`
  206. QrCode string `gorm:"column:qr_code" json:"qr_code" form:"qr_code"`
  207. BindingState int64 `gorm:"column:binding_state" json:"binding_state" form:"binding_state"`
  208. PatientComplains string `gorm:"column:patient_complains" json:"patient_complains"` // 主诉
  209. PresentHistory string `gorm:"column:present_history" json:"present_history"` // 现病史
  210. PastHistory string `gorm:"column:past_history" json:"past_history"` // 既往史
  211. Temperature float64 `gorm:"column:temperature" json:"temperature"` // 体格检查-体温
  212. Pulse int64 `gorm:"column:pulse" json:"pulse"` // 体格检查-脉搏
  213. Respiratory int64 `gorm:"column:respiratory" json:"respiratory"` // 体格检查-呼吸频率
  214. SBP int64 `gorm:"column:sbp" json:"sbp"` // 体格检查-收缩压
  215. DBP int64 `gorm:"column:dbp" json:"dbp"` // 体格检查-舒张压
  216. Status int64 `gorm:"column:status" json:"status" form:"status"`
  217. Age int64 `gorm:"column:age" json:"age"`
  218. IsOpenRemind int64 `gorm:"column:is_open_remind" json:"is_open_remind"`
  219. DialysisAge int64 `gorm:"column:dialysis_age" json:"dialysis_age" form:"dialysis_age"`
  220. ExpenseKind int64 `gorm:"column:expense_kind" json:"expense_kind" form:"expense_kind"`
  221. TellPhone string `gorm:"column:tell_phone" json:"tell_phone" form:"tell_phone"`
  222. FirstTreatmentDate int64 `gorm:"column:first_treatment_date" json:"first_treatment_date" form:"first_treatment_date"`
  223. ContactName string `gorm:"column:contact_name" json:"contact_name" form:"contact_name"`
  224. UserSysBeforeCount int64 `gorm:"column:user_sys_before_count" json:"user_sys_before_count" form:"user_sys_before_count"`
  225. }
  226. func (MSchedualPatientVM) TableName() string {
  227. return "xt_patients"
  228. }
  229. type MDialysisOrderVM struct {
  230. ID int64 `gorm:"column:id" json:"id"`
  231. DialysisDate int64 `gorm:"column:dialysis_date" json:"dialysis_date"`
  232. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id"`
  233. PatientId int64 `gorm:"column:patient_id" json:"patient_id"`
  234. // PrescriptionId int64 `gorm:"column:prescription_id" json:"prescription_id"`
  235. Stage int64 `gorm:"column:stage" json:"stage"`
  236. // Remark string `gorm:"column:remark" json:"remark"`
  237. BedID int64 `gorm:"column:bed_id" json:"bed_id"`
  238. StartNurse int64 `gorm:"column:start_nurse" json:"start_nurse"`
  239. FinishNurse int64 `gorm:"column:finish_nurse" json:"finish_nurse"`
  240. Status int64 `gorm:"column:status" json:"status"`
  241. PunctureNurse int64 `gorm:"column:puncture_nurse" json:"puncture_nurse"`
  242. DeviceNumber *MDeviceNumberVM `gorm:"ForeignKey:BedID" json:"device_number"`
  243. MonitoringRecords []*models.MonitoringRecord `gorm:"ForeignKey:DialysisOrderId" json:"monitoring_records"`
  244. Creator int64 `gorm:"column:creator" json:"creator"`
  245. Modifier int64 `gorm:"column:modifier" json:"modifier"`
  246. FinishCreator int64 `gorm:"column:finish_creator" json:"finish_creator"`
  247. FinishModifier int64 `gorm:"column:finish_modifier" json:"finish_modifier"`
  248. SchedualType int64 `gorm:"column:schedual_type" json:"schedual_type"`
  249. WashpipeNurse int64 `gorm:"column:washpipe_nurse" json:"washpipe_nurse" form:"washpipe_nurse"`
  250. }
  251. func (MDialysisOrderVM) TableName() string {
  252. return "xt_dialysis_order"
  253. }
  254. type MDialysisOrderVMList struct {
  255. ID int64 `gorm:"column:id" json:"id"`
  256. DialysisDate int64 `gorm:"column:dialysis_date" json:"dialysis_date"`
  257. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id"`
  258. PatientId int64 `gorm:"column:patient_id" json:"patient_id"`
  259. // PrescriptionId int64 `gorm:"column:prescription_id" json:"prescription_id"`
  260. Stage int64 `gorm:"column:stage" json:"stage"`
  261. // Remark string `gorm:"column:remark" json:"remark"`
  262. BedID int64 `gorm:"column:bed_id" json:"bed_id"`
  263. StartNurse int64 `gorm:"column:start_nurse" json:"start_nurse"`
  264. Status int64 `gorm:"column:status" json:"status"`
  265. DeviceNumber *MDeviceNumberVM `gorm:"ForeignKey:BedID" json:"device_number"`
  266. Creator int64 `gorm:"column:creator" json:"creator"`
  267. WashpipeNurse int64 `gorm:"column:washpipe_nurse" json:"washpipe_nurse" form:"washpipe_nurse"`
  268. }
  269. func (MDialysisOrderVMList) TableName() string {
  270. return "xt_dialysis_order"
  271. }
  272. type VMDoctorAdvice struct {
  273. ID int64 `gorm:"column:id" json:"id" form:"id"`
  274. GroupNo int64 `gorm:"column:groupno" json:"groupno" form:"groupno"`
  275. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id" form:"user_org_id"`
  276. PatientId int64 `gorm:"column:patient_id" json:"patient_id" form:"patient_id"`
  277. AdviceDate int64 `gorm:"column:advice_date" json:"advice_date" form:"advice_date"`
  278. Status int64 `gorm:"column:status" json:"status" form:"status"`
  279. ExecutionState int64 `gorm:"column:execution_state" json:"execution_state" form:"execution_state"`
  280. RecordDate int64 `gorm:"column:record_date" json:"record_date"`
  281. CheckTime int64 `gorm:"column:check_time" json:"check_time" form:"check_time"`
  282. CheckState int64 `gorm:"column:check_state" json:"check_state" form:"check_state"`
  283. ParentId int64 `gorm:"column:parent_id" json:"parent_id" form:"parent_id"`
  284. }
  285. func (VMDoctorAdvice) TableName() string {
  286. return "xt_doctor_advice"
  287. }
  288. type VMHisDoctorAdviceInfo struct {
  289. ID int64 `gorm:"column:id" json:"id" form:"id"`
  290. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id" form:"user_org_id"`
  291. PatientId int64 `gorm:"column:patient_id" json:"patient_id" form:"patient_id"`
  292. HisPatientId int64 `gorm:"column:his_patient_id" json:"his_patient_id" form:"his_patient_id"`
  293. ExecutionTime int64 `gorm:"column:execution_time" json:"execution_time" form:"execution_time"`
  294. ExecutionStaff int64 `gorm:"column:execution_staff" json:"execution_staff" form:"execution_staff"`
  295. ExecutionState int64 `gorm:"column:execution_state" json:"execution_state" form:"execution_state"`
  296. IsMedicine int64 `gorm:"column:is_medicine" json:"is_medicine" form:"is_medicine"`
  297. ExecutionFrequencyId int64 `gorm:"column:execution_frequency_id" json:"execution_frequency_id" form:"execution_frequency_id"`
  298. }
  299. func (VMHisDoctorAdviceInfo) TableName() string {
  300. return "his_doctor_advice_info"
  301. }
  302. type VMAssessmentAfterDislysis struct {
  303. ID int64 `gorm:"column:id" json:"id"`
  304. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id"`
  305. PatientId int64 `gorm:"column:patient_id" json:"patient_id"`
  306. AssessmentDate int64 `gorm:"column:assessment_date" json:"assessment_date"`
  307. WeightAfter float64 `gorm:"column:weight_after" json:"weight_after"`
  308. Status int64 `gorm:"column:status" json:"status"`
  309. }
  310. func (VMAssessmentAfterDislysis) TableName() string {
  311. return "xt_assessment_after_dislysis"
  312. }
  313. // 获取透析记录
  314. func MobileGetDialysisRecord(orgID int64, patientID int64, recordDate int64) (*models.DialysisOrder, error) {
  315. var record models.DialysisOrder
  316. err := readDb.Model(&models.DialysisOrder{}).Where("user_org_id = ? AND patient_id = ? AND dialysis_date = ?", orgID, patientID, recordDate).First(&record).Error
  317. if err != nil {
  318. if err == gorm.ErrRecordNotFound {
  319. return nil, nil
  320. } else {
  321. return nil, err
  322. }
  323. }
  324. return &record, nil
  325. }
  326. // 用户基本信息
  327. func MobileGetPatientDetail(orgID int64, patientID int64) (*MPatient, error) {
  328. var patient MPatient
  329. //err := readDb.Model(&MPatient{}).Where("status = 1 AND user_org_id = ? AND id = ?", orgID, patientID).First(&patient).Error
  330. //if err != nil {
  331. // if err == gorm.ErrRecordNotFound {
  332. // return nil, nil
  333. // } else {
  334. // return nil, err
  335. // }
  336. //}
  337. //return &patient, nil
  338. redis := RedisClient()
  339. defer redis.Close()
  340. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(patientID, 10) + ":patient_info"
  341. patient_info_str, _ := redis.Get(key).Result()
  342. if len(patient_info_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  343. err = readDb.Model(&patient).Where("id = ? and user_org_id=? and status=1", patientID, orgID).First(&patient).Error
  344. if err != nil {
  345. if err == gorm.ErrRecordNotFound {
  346. return &patient, nil
  347. } else {
  348. return &patient, err
  349. }
  350. } else {
  351. if patient.ID > 0 {
  352. //缓存数据
  353. patient_info_json, err := json.Marshal(&patient)
  354. if err == nil {
  355. redis.Set(key, patient_info_json, time.Second*60*60*18)
  356. }
  357. }
  358. return &patient, nil
  359. }
  360. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  361. json.Unmarshal([]byte(patient_info_str), &patient)
  362. return &patient, nil
  363. }
  364. }
  365. func MobileGetSchedualDetailOne(orgID int64, patientID int64, schedualDate int64) (*MDialysisScheduleVM, error) {
  366. var vm MDialysisScheduleVM
  367. err := readDb.
  368. Table("xt_schedule").
  369. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  370. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  371. Where("status = 1 AND user_org_id = ? AND schedule_date = ? AND patient_id = ?", orgID, schedualDate, patientID).
  372. First(&vm).Error
  373. return &vm, err
  374. }
  375. // 用户排班信息
  376. func MobileGetSchedualDetailSix(orgID int64, patientID int64, schedualDate int64) (*MDialysisScheduleVM, error) {
  377. var vm MDialysisScheduleVM
  378. err := readDb.
  379. Table("xt_schedule").
  380. // Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  381. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  382. Where("status = 1 AND user_org_id = ? AND schedule_date = ? AND patient_id = ?", orgID, schedualDate, patientID).
  383. First(&vm).Error
  384. if err != nil {
  385. if err == gorm.ErrRecordNotFound {
  386. return nil, nil
  387. } else {
  388. return nil, err
  389. }
  390. }
  391. return &vm, err
  392. }
  393. // 用户排班信息
  394. func MobileGetSchedualDetail(orgID int64, patientID int64, schedualDate int64) (*MDialysisScheduleVM, error) {
  395. redis := RedisClient()
  396. defer redis.Close()
  397. var vm MDialysisScheduleVM
  398. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(patientID, 10) + ":" + strconv.FormatInt(schedualDate, 10) + ":schedual_detail"
  399. schedual_detail_str, _ := redis.Get(key).Result()
  400. if len(schedual_detail_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  401. err := readDb.
  402. Table("xt_schedule").
  403. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  404. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  405. Where("status = 1 AND user_org_id = ? AND schedule_date = ? AND patient_id = ?", orgID, schedualDate, patientID).
  406. First(&vm).Error
  407. if err != nil {
  408. if err == gorm.ErrRecordNotFound {
  409. return nil, nil
  410. } else {
  411. return nil, err
  412. }
  413. } else {
  414. if vm.ID > 0 {
  415. //缓存数据
  416. schedual_detail_str, err := json.Marshal(vm)
  417. if err == nil {
  418. redis.Set(key, schedual_detail_str, time.Second*60*60*18)
  419. }
  420. } else {
  421. redis.Set(key, "null", time.Second*60*60*18)
  422. }
  423. return &vm, nil
  424. }
  425. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  426. if schedual_detail_str == "null" {
  427. return &vm, nil
  428. }
  429. json.Unmarshal([]byte(schedual_detail_str), &vm)
  430. return &vm, nil
  431. }
  432. }
  433. // 用户排班信息
  434. func MobileGetPatientSchedual(orgID int64, patientID int64, schedualDate int64) (*models.Schedule, error) {
  435. var schedule models.Schedule
  436. err := readDb.
  437. Table("xt_schedule").
  438. Where("status = 1 AND user_org_id = ? AND schedule_date = ? AND patient_id = ?", orgID, schedualDate, patientID).
  439. First(&schedule).Error
  440. if err != nil {
  441. if err == gorm.ErrRecordNotFound {
  442. return nil, nil
  443. } else {
  444. return nil, err
  445. }
  446. }
  447. return &schedule, nil
  448. }
  449. type MPatient struct {
  450. ID int64 `gorm:"column:id" json:"id" form:"id"`
  451. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id" form:"user_org_id"`
  452. UserId int64 `gorm:"column:user_id" json:"user_id" form:"user_id"`
  453. PatientType int64 `gorm:"column:patient_type" json:"patient_type" form:"patient_type"`
  454. Avatar string `gorm:"column:avatar" json:"avatar" form:"avatar"`
  455. DialysisNo string `gorm:"column:dialysis_no" json:"dialysis_no" form:"dialysis_no"`
  456. AdmissionNumber string `gorm:"column:admission_number" json:"admission_number" form:"admission_number"`
  457. Source int64 `gorm:"column:source" json:"source" form:"source"`
  458. Lapseto int64 `gorm:"column:lapseto" json:"lapseto" form:"lapseto"`
  459. PartitionId int64 `gorm:"column:partition_id" json:"partition_id" form:"partition_id"`
  460. BedId int64 `gorm:"column:bed_id" json:"bed_id" form:"bed_id"`
  461. Name string `gorm:"column:name" json:"name" form:"name"`
  462. Alias string `gorm:"column:alias" json:"alias" form:"alias"`
  463. Gender int64 `gorm:"column:gender" json:"gender" form:"gender"`
  464. Nation string `gorm:"column:nation" json:"nation" form:"nation"`
  465. NativePlace string `gorm:"column:native_place" json:"native_place" form:"native_place"`
  466. MaritalStatus int64 `gorm:"column:marital_status" json:"marital_status" form:"marital_status"`
  467. IdCardNo string `gorm:"column:id_card_no" json:"id_card_no" form:"id_card_no"`
  468. Birthday int64 `gorm:"column:birthday" json:"birthday" form:"birthday"`
  469. ReimbursementWayId int64 `gorm:"column:reimbursement_way_id" json:"reimbursement_way_id" form:"reimbursement_way_id"`
  470. HealthCareType int64 `gorm:"column:health_care_type" json:"health_care_type" form:"health_care_type"`
  471. HealthCareNo string `gorm:"column:health_care_no" json:"health_care_no" form:"health_care_no"`
  472. HealthCareDueDate int64 `gorm:"column:health_care_due_date" json:"health_care_due_date" form:"health_care_due_date"`
  473. Height int64 `gorm:"column:height" json:"height" form:"height"`
  474. BloodType int64 `gorm:"column:blood_type" json:"blood_type" form:"blood_type"`
  475. Rh int64 `gorm:"column:rh" json:"rh" form:"rh"`
  476. HealthCareDueAlertDate int64 `gorm:"column:health_care_due_alert_date" json:"health_care_due_alert_date" form:"health_care_due_alert_date"`
  477. EducationLevel int64 `gorm:"column:education_level" json:"education_level" form:"education_level"`
  478. Profession int64 `gorm:"column:profession" json:"profession" form:"profession"`
  479. Phone string `gorm:"column:phone" json:"phone" form:"phone"`
  480. HomeTelephone string `gorm:"column:home_telephone" json:"home_telephone" form:"home_telephone"`
  481. RelativePhone string `gorm:"column:relative_phone" json:"relative_phone" form:"relative_phone"`
  482. RelativeRelations string `gorm:"column:relative_relations" json:"relative_relations" form:"relative_relations"`
  483. HomeAddress string `gorm:"column:home_address" json:"home_address" form:"home_address"`
  484. WorkUnit string `gorm:"column:work_unit" json:"work_unit" form:"work_unit"`
  485. UnitAddress string `gorm:"column:unit_address" json:"unit_address" form:"unit_address"`
  486. Children int64 `gorm:"column:children" json:"children" form:"children"`
  487. ReceivingDate int64 `gorm:"column:receiving_date" json:"receiving_date" form:"receiving_date"`
  488. IsHospitalFirstDialysis int64 `gorm:"column:is_hospital_first_dialysis" json:"is_hospital_first_dialysis" form:"is_hospital_first_dialysis"`
  489. FirstDialysisDate int64 `gorm:"column:first_dialysis_date" json:"first_dialysis_date" form:"first_dialysis_date"`
  490. FirstDialysisHospital string `gorm:"column:first_dialysis_hospital" json:"first_dialysis_hospital" form:"first_dialysis_hospital"`
  491. InductionPeriod int64 `gorm:"column:induction_period" json:"induction_period" form:"induction_period"`
  492. InitialDialysis int64 `gorm:"column:initial_dialysis" json:"initial_dialysis" form:"initial_dialysis"`
  493. TotalDialysis int64 `gorm:"column:total_dialysis" json:"total_dialysis" form:"total_dialysis"`
  494. AttendingDoctorId int64 `gorm:"column:attending_doctor_id" json:"attending_doctor_id" form:"attending_doctor_id"`
  495. HeadNurseId int64 `gorm:"column:head_nurse_id" json:"head_nurse_id" form:"head_nurse_id"`
  496. Evaluate string `gorm:"column:evaluate" json:"evaluate" form:"evaluate"`
  497. Diagnose string `gorm:"column:diagnose" json:"diagnose" form:"diagnose"`
  498. Remark string `gorm:"column:remark" json:"remark" form:"remark"`
  499. RegistrarsId int64 `gorm:"column:registrars_id" json:"registrars_id" form:"registrars_id"`
  500. Registrars string `gorm:"column:registrars" json:"registrars" form:"registrars"`
  501. QrCode string `gorm:"column:qr_code" json:"qr_code" form:"qr_code"`
  502. BindingState int64 `gorm:"column:binding_state" json:"binding_state" form:"binding_state"`
  503. PatientComplains string `gorm:"column:patient_complains" json:"patient_complains"` // 主诉
  504. PresentHistory string `gorm:"column:present_history" json:"present_history"` // 现病史
  505. PastHistory string `gorm:"column:past_history" json:"past_history"` // 既往史
  506. Temperature float64 `gorm:"column:temperature" json:"temperature"` // 体格检查-体温
  507. Pulse int64 `gorm:"column:pulse" json:"pulse"` // 体格检查-脉搏
  508. Respiratory int64 `gorm:"column:respiratory" json:"respiratory"` // 体格检查-呼吸频率
  509. SBP int64 `gorm:"column:sbp" json:"sbp"` // 体格检查-收缩压
  510. DBP int64 `gorm:"column:dbp" json:"dbp"` // 体格检查-舒张压
  511. Status int64 `gorm:"column:status" json:"status" form:"status"`
  512. Age int64 `gorm:"column:age" json:"age"`
  513. IsOpenRemind int64 `gorm:"column:is_open_remind" json:"is_open_remind"`
  514. CreatedTime int64 `gorm:"column:created_time" json:"created_time" form:"created_time"`
  515. DialysisAge int64 `gorm:"column:dialysis_age" json:"dialysis_age" form:"dialysis_age"`
  516. ExpenseKind int64 `gorm:"column:expense_kind" json:"expense_kind" form:"expense_kind"`
  517. TellPhone string `gorm:"column:tell_phone" json:"tell_phone" form:"tell_phone"`
  518. FirstTreatmentDate int64 `gorm:"column:first_treatment_date" json:"first_treatment_date" form:"first_treatment_date"`
  519. ContactName string `gorm:"column:contact_name" json:"contact_name" form:"contact_name"`
  520. UserSysBeforeCount int64 `gorm:"column:user_sys_before_count" json:"user_sys_before_count" form:"user_sys_before_count"`
  521. }
  522. func (MPatient) TableName() string {
  523. return "xt_patients"
  524. }
  525. // 接诊评估
  526. func MobileGetReceiverTreatmentAccessRecord(orgID int64, patientID int64, recordDate int64) (*models.ReceiveTreatmentAsses, error) {
  527. var record models.ReceiveTreatmentAsses
  528. redis := RedisClient()
  529. defer redis.Close()
  530. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(patientID, 10) + ":" + strconv.FormatInt(recordDate, 10) + ":receive_treatment_asses"
  531. receive_treatment_asses_str, _ := redis.Get(key).Result()
  532. if len(receive_treatment_asses_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  533. err = readDb.Model(&models.ReceiveTreatmentAsses{}).Where("patient_id = ? and user_org_id = ? and status = 1 and record_date = ?", patientID, orgID, recordDate).First(&record).Error
  534. if err != nil {
  535. if err == gorm.ErrRecordNotFound {
  536. if record.ID <= 0 {
  537. redis.Set(key, "", time.Second*60*60*18)
  538. }
  539. return nil, nil
  540. } else {
  541. return nil, err
  542. }
  543. } else {
  544. if record.ID > 0 {
  545. //缓存数据
  546. receive_treatment_asses_str, err := json.Marshal(record)
  547. if err == nil {
  548. redis.Set(key, receive_treatment_asses_str, time.Second*60*60*18)
  549. }
  550. } else {
  551. redis.Set(key, "null", time.Second*60*60*18)
  552. }
  553. return &record, nil
  554. }
  555. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  556. if receive_treatment_asses_str == "null" {
  557. return &record, nil
  558. }
  559. json.Unmarshal([]byte(receive_treatment_asses_str), &record)
  560. return &record, nil
  561. }
  562. }
  563. // 透前评估
  564. func MobileGetPredialysisEvaluationOne(orgID int64, patientID int64, recordDate int64) (*models.PredialysisEvaluation, error) {
  565. fmt.Println("recordDate", recordDate)
  566. var record models.PredialysisEvaluation
  567. err := readDb.Model(&models.PredialysisEvaluation{}).Where("patient_id = ? and user_org_id = ? and status = 1 and assessment_date = ?", patientID, orgID, recordDate).First(&record).Error
  568. if err != nil {
  569. if err == gorm.ErrRecordNotFound {
  570. return nil, nil
  571. } else {
  572. return nil, err
  573. }
  574. }
  575. return &record, nil
  576. }
  577. func MobileGetPredialysisEvaluationTwo(orgID int64, patientID int64, recordDate int64) (models.PredialysisEvaluation, error) {
  578. var record models.PredialysisEvaluation
  579. err := readDb.Model(&models.PredialysisEvaluation{}).Where("patient_id = ? and user_org_id = ? and status = 1 and assessment_date = ?", patientID, orgID, recordDate).First(&record).Error
  580. return record, err
  581. }
  582. // 透前评估
  583. func MobileGetPredialysisEvaluation(orgID int64, patientID int64, recordDate int64) (*models.PredialysisEvaluation, error) {
  584. var record models.PredialysisEvaluation
  585. redis := RedisClient()
  586. defer redis.Close()
  587. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(patientID, 10) + ":" + strconv.FormatInt(recordDate, 10) + ":assessment_before_dislysis"
  588. assessment_before_dislysis_str, _ := redis.Get(key).Result()
  589. redis.Set(key, "", time.Second)
  590. if len(assessment_before_dislysis_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  591. err := readDb.Model(&models.PredialysisEvaluation{}).Where("patient_id = ? and user_org_id = ? and status = 1 and assessment_date = ?", patientID, orgID, recordDate).First(&record).Error
  592. if err != nil {
  593. if err == gorm.ErrRecordNotFound {
  594. if record.ID <= 0 {
  595. redis.Set(key, "", time.Second*60*60*18)
  596. }
  597. return nil, nil
  598. } else {
  599. return nil, err
  600. }
  601. } else {
  602. if record.ID > 0 {
  603. //缓存数据
  604. assessment_before_dislysis_str, err := json.Marshal(record)
  605. if err == nil {
  606. redis.Set(key, assessment_before_dislysis_str, time.Second*60*60*18)
  607. }
  608. } else {
  609. redis.Set(key, "null", time.Second*60*60*18)
  610. }
  611. return &record, nil
  612. }
  613. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  614. if assessment_before_dislysis_str == "null" {
  615. return &record, nil
  616. } else {
  617. json.Unmarshal([]byte(assessment_before_dislysis_str), &record)
  618. return &record, nil
  619. }
  620. }
  621. }
  622. // 获取 maxDate 之前一次的透前评估记录
  623. func MobileGetLastTimePredialysisEvaluationOne(orgID int64, patientID int64, maxDate int64) (*models.PredialysisEvaluation, error) {
  624. var record models.PredialysisEvaluation
  625. err := readDb.Model(&models.PredialysisEvaluation{}).Where("patient_id = ? and user_org_id = ? and status = 1 and assessment_date < ?", patientID, orgID, maxDate).Order("assessment_date desc").First(&record).Error
  626. if err != nil {
  627. if err == gorm.ErrRecordNotFound {
  628. return nil, nil
  629. } else {
  630. return nil, err
  631. }
  632. }
  633. return &record, nil
  634. }
  635. // 获取 maxDate 之前一次的透前评估记录
  636. func MobileGetLastTimePredialysisEvaluation(orgID int64, patientID int64, maxDate int64) (*models.PredialysisEvaluation, error) {
  637. var record models.PredialysisEvaluation
  638. redis := RedisClient()
  639. defer redis.Close()
  640. // cur_date := time.Now().Format("2006-01-02")
  641. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(patientID, 10) + ":" + strconv.FormatInt(maxDate, 10) + ":assessment_before_dislysis_last"
  642. assessment_before_dislysis_last_str, _ := redis.Get(key).Result()
  643. if len(assessment_before_dislysis_last_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  644. err := readDb.Model(&models.PredialysisEvaluation{}).Where("patient_id = ? and user_org_id = ? and status = 1 and assessment_date < ?", patientID, orgID, maxDate).Order("assessment_date desc").First(&record).Error
  645. if err != nil {
  646. if err == gorm.ErrRecordNotFound {
  647. if record.ID <= 0 {
  648. redis.Set(key, "", time.Second*60*60*18)
  649. }
  650. return nil, nil
  651. } else {
  652. return nil, err
  653. }
  654. } else {
  655. if record.ID > 0 {
  656. //缓存数据
  657. assessment_before_dislysis_last_str, err := json.Marshal(record)
  658. if err == nil {
  659. redis.Set(key, assessment_before_dislysis_last_str, time.Second*60*60*4)
  660. }
  661. } else {
  662. redis.Set(key, "null", time.Second*60*60*18)
  663. }
  664. return &record, nil
  665. }
  666. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  667. if assessment_before_dislysis_last_str == "null" {
  668. return &record, nil
  669. } else {
  670. json.Unmarshal([]byte(assessment_before_dislysis_last_str), &record)
  671. return &record, nil
  672. }
  673. }
  674. }
  675. // 临时医嘱
  676. func MobileGetDoctorAdvices(orgID int64, patientID int64, recordDate int64) ([]*models.DoctorAdvice, error) {
  677. var records []*models.DoctorAdvice
  678. // err := readDb.Model(&models.DoctorAdvice{}).Where("patient_id = ? and user_org_id = ? and status = 1 and record_date = ?", patientID, orgID, recordDate).Find(&records).Error
  679. err := readDb.
  680. Model(&models.DoctorAdvice{}).
  681. Where("patient_id = ? and user_org_id = ? and status = 1 and record_date = ? and (advice_type = 2 or advice_type = 3)", patientID, orgID, recordDate).
  682. Select("id,user_org_id,patient_id,advice_type,advice_date,record_date,start_time,advice_name,advice_desc,reminder_date, drug_spec, drug_spec_unit,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, check_state, check_time,way,drug_id,drug_name_id,IF(parent_id>0, parent_id, id) as advice_order,groupno,is_medicine").
  683. Order("start_time asc, groupno desc, advice_order desc, id").
  684. Scan(&records).Error
  685. if err != nil {
  686. return nil, err
  687. }
  688. return records, nil
  689. }
  690. func MobileGetDoctorAdvicesByGroups(orgID int64, patientID int64, recordDate int64) ([]*models.DoctorAdvice, error) {
  691. var records []*models.DoctorAdvice
  692. redis := RedisClient()
  693. defer redis.Close()
  694. // cur_date := time.Now().Format("2006-01-02")
  695. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(patientID, 10) + ":" + strconv.FormatInt(recordDate, 10) + ":doctor_advices"
  696. doctor_advices_str, _ := redis.Get(key).Result()
  697. if len(doctor_advices_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  698. err := readDb.Model(&models.DoctorAdvice{}).
  699. Where("patient_id = ? and user_org_id = ? and status = 1 and record_date = ? and (advice_type = 2 || advice_type = 3)", patientID, orgID, recordDate).
  700. Select("id, user_org_id, patient_id, advice_type, advice_date, record_date, start_time, advice_name,advice_desc, reminder_date, drug_spec, drug_spec_unit, 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, check_state, check_time, groupno,way,drug_id,is_medicine,drug_name_id, IF(parent_id > 0, parent_id, id) as advice_order").
  701. Order("start_time asc, groupno desc, advice_order desc, id asc").
  702. Scan(&records).Error
  703. if err != nil {
  704. if err == gorm.ErrRecordNotFound {
  705. if len(records) <= 0 {
  706. redis.Set(key, "", time.Second*60*60*18)
  707. }
  708. return nil, nil
  709. } else {
  710. return nil, err
  711. }
  712. } else {
  713. if len(records) > 0 {
  714. //缓存数据
  715. doctor_advices_str, err := json.Marshal(records)
  716. if err == nil {
  717. redis.Set(key, doctor_advices_str, time.Second*60*60*18)
  718. return records, nil
  719. }
  720. } else {
  721. redis.Set(key, "null", time.Second*60*60*18)
  722. return records, nil
  723. }
  724. return records, nil
  725. }
  726. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  727. if doctor_advices_str == "null" {
  728. return records, nil
  729. } else {
  730. json.Unmarshal([]byte(doctor_advices_str), &records)
  731. return records, nil
  732. }
  733. }
  734. }
  735. // 透析记录
  736. func MobileGetSchedualDialysisRecordTen(orgID int64, patientID int64, recordDate int64) (*models.DialysisOrder, error) {
  737. var record models.DialysisOrder
  738. err := readDb.Model(&models.DialysisOrder{}).Preload("DeviceNumber", "org_id = ?", orgID).Where("user_org_id = ? AND patient_id = ? AND dialysis_date = ?", orgID, patientID, recordDate).First(&record).Error
  739. if err != nil {
  740. if err == gorm.ErrRecordNotFound {
  741. return nil, nil
  742. } else {
  743. return nil, err
  744. }
  745. }
  746. return &record, nil
  747. }
  748. // 透析记录
  749. func MobileGetSchedualDialysisRecord(orgID int64, patientID int64, recordDate int64) (*models.DialysisOrder, error) {
  750. var record models.DialysisOrder
  751. redis := RedisClient()
  752. defer redis.Close()
  753. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(patientID, 10) + ":" + strconv.FormatInt(recordDate, 10) + ":dialysis_order"
  754. redis.Set(key, "", time.Second)
  755. dialysis_order_str, _ := redis.Get(key).Result()
  756. if len(dialysis_order_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  757. err := readDb.Model(&models.DialysisOrder{}).Preload("DeviceNumber", "org_id = ? AND status = 1", orgID).Where("user_org_id = ? AND patient_id = ? AND dialysis_date = ?", orgID, patientID, recordDate).First(&record).Error
  758. if err != nil {
  759. if err == gorm.ErrRecordNotFound {
  760. return nil, nil
  761. } else {
  762. return nil, err
  763. }
  764. } else {
  765. if record.ID > 0 {
  766. //缓存数据
  767. dialysis_order_str, err := json.Marshal(record)
  768. if err == nil {
  769. redis.Set(key, dialysis_order_str, time.Second*60*60*18)
  770. }
  771. } else {
  772. redis.Set(key, "null", time.Second*60*60*18)
  773. }
  774. return &record, nil
  775. }
  776. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  777. if dialysis_order_str == "null" {
  778. err = readDb.Model(&models.DialysisOrder{}).Preload("DeviceNumber", "org_id = ? AND status = 1", orgID).Where("user_org_id = ? AND patient_id = ? AND dialysis_date = ?", orgID, patientID, recordDate).First(&record).Error
  779. return &record, nil
  780. } else {
  781. json.Unmarshal([]byte(dialysis_order_str), &record)
  782. return &record, nil
  783. }
  784. }
  785. }
  786. // 双人核对
  787. func MobileGetDoubleCheck(orgID int64, patientID int64, recordDate int64) (*models.DoubleCheck, error) {
  788. var record models.DoubleCheck
  789. redis := RedisClient()
  790. defer redis.Close()
  791. // cur_date := time.Now().Format("2006-01-02")
  792. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(patientID, 10) + ":" + strconv.FormatInt(recordDate, 10) + ":double_check"
  793. double_check_str, _ := redis.Get(key).Result()
  794. if len(double_check_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  795. err := readDb.Model(&models.DoubleCheck{}).Where("patient_id = ? and user_org_id = ? and status = 1 and check_date = ?", patientID, orgID, recordDate).First(&record).Error
  796. if err != nil {
  797. if err == gorm.ErrRecordNotFound {
  798. if record.ID <= 0 {
  799. redis.Set(key, "null", time.Second*60*60*18)
  800. }
  801. return nil, nil
  802. } else {
  803. return nil, err
  804. }
  805. } else {
  806. if record.ID > 0 {
  807. //缓存数据
  808. double_check_str, err := json.Marshal(record)
  809. if err == nil {
  810. redis.Set(key, double_check_str, time.Second*60*60*18)
  811. }
  812. } else {
  813. redis.Set(key, "null", time.Second*60*60*18)
  814. }
  815. return &record, nil
  816. }
  817. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  818. if double_check_str == "null" {
  819. return &record, nil
  820. } else {
  821. json.Unmarshal([]byte(double_check_str), &record)
  822. return &record, nil
  823. }
  824. }
  825. }
  826. // 透析监测记录
  827. func MobileGetMonitorRecords(orgID int64, patientID int64, recordDate int64) ([]*models.MonitoringRecord, error) {
  828. var records []*models.MonitoringRecord
  829. redis := RedisClient()
  830. defer redis.Close()
  831. // cur_date := time.Now().Format("2006-01-02")
  832. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(patientID, 10) + ":" + strconv.FormatInt(recordDate, 10) + ":monitor_records"
  833. redis.Set(key, "", time.Second)
  834. monitor_records_str, _ := redis.Get(key).Result()
  835. if len(monitor_records_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  836. err := readDb.Model(&models.MonitoringRecord{}).Where("patient_id = ? and user_org_id = ? and status = 1 and monitoring_date = ?", patientID, orgID, recordDate).Order("operate_time asc").Find(&records).Error
  837. if err != nil {
  838. if err == gorm.ErrRecordNotFound {
  839. //if len(records) <= 0 {
  840. // redis.Set(key, "null", time.Second*60*60*18)
  841. //}
  842. return nil, nil
  843. } else {
  844. return nil, err
  845. }
  846. } else {
  847. if len(records) > 0 {
  848. //缓存数据
  849. monitor_records_str, err := json.Marshal(records)
  850. if err == nil {
  851. redis.Set(key, monitor_records_str, time.Second*60*60*18)
  852. }
  853. } else {
  854. redis.Set(key, "null", time.Second*60*60*18)
  855. }
  856. return records, nil
  857. }
  858. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  859. if monitor_records_str == "null" {
  860. return records, nil
  861. } else {
  862. json.Unmarshal([]byte(monitor_records_str), &records)
  863. return records, nil
  864. }
  865. }
  866. }
  867. func MobileGetMonitorRecordFirst(orgID int64, patientID int64, recordDate int64) (*models.MonitoringRecord, error) {
  868. var records models.MonitoringRecord
  869. err := readDb.Model(&models.MonitoringRecord{}).Where("patient_id = ? and user_org_id = ? and status = 1 and monitoring_date = ?", patientID, orgID, recordDate).Order("operate_time asc").First(&records).Error
  870. if err != nil {
  871. return nil, err
  872. }
  873. return &records, nil
  874. }
  875. func MobileGetLastMonitorRecordOne(orgID int64, patientID int64, beforeDate int64) (*models.MonitoringRecord, error) {
  876. var record models.MonitoringRecord
  877. err := readDb.Model(&models.MonitoringRecord{}).Where("patient_id = ? and user_org_id = ? and status = 1 and monitoring_date = ?", patientID, orgID, beforeDate).Order("operate_time desc").First(&record).Error
  878. if err != nil {
  879. if err == gorm.ErrRecordNotFound {
  880. return nil, nil
  881. } else {
  882. return nil, err
  883. }
  884. }
  885. return &record, nil
  886. }
  887. func MobileGetLastMonitorRecord(orgID int64, patientID int64, beforeDate int64) (*models.MonitoringRecord, error) {
  888. var record models.MonitoringRecord
  889. redis := RedisClient()
  890. defer redis.Close()
  891. // cur_date := time.Now().Format("2006-01-02")
  892. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(patientID, 10) + ":" + strconv.FormatInt(beforeDate, 10) + ":monitor_record_last"
  893. monitor_record_last_str, _ := redis.Get(key).Result()
  894. if len(monitor_record_last_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  895. err := readDb.Model(&models.MonitoringRecord{}).Where("patient_id = ? and user_org_id = ? and status = 1 and monitoring_date = ?", patientID, orgID, beforeDate).Order("operate_time desc").First(&record).Error
  896. if err != nil {
  897. if err == gorm.ErrRecordNotFound {
  898. if record.ID <= 0 {
  899. redis.Set(key, "null", time.Second*60*60*18)
  900. }
  901. return nil, nil
  902. } else {
  903. return nil, err
  904. }
  905. } else {
  906. if record.ID > 0 {
  907. //缓存数据
  908. monitor_record_last_str, err := json.Marshal(record)
  909. if err == nil {
  910. redis.Set(key, monitor_record_last_str, time.Second*60*60*18)
  911. }
  912. } else {
  913. redis.Set(key, "null", time.Second*60*60*18)
  914. }
  915. return &record, nil
  916. }
  917. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  918. if monitor_record_last_str == "null" {
  919. return &record, nil
  920. } else {
  921. json.Unmarshal([]byte(monitor_record_last_str), &record)
  922. return &record, nil
  923. }
  924. }
  925. }
  926. func MobileGetAssessmentAfterDislysisOne(orgID int64, patientID int64, recordDate int64) (*models.AssessmentAfterDislysis, error) {
  927. var record models.AssessmentAfterDislysis
  928. err := readDb.Model(&models.AssessmentAfterDislysis{}).Where("patient_id = ? and user_org_id = ? and status = 1 and assessment_date = ?", patientID, orgID, recordDate).First(&record).Error
  929. if err != nil {
  930. if err == gorm.ErrRecordNotFound {
  931. return nil, nil
  932. } else {
  933. return nil, err
  934. }
  935. }
  936. return &record, nil
  937. }
  938. // 透后评估
  939. func MobileGetAssessmentAfterDislysis(orgID int64, patientID int64, recordDate int64) (*models.AssessmentAfterDislysis, error) {
  940. var record models.AssessmentAfterDislysis
  941. redis := RedisClient()
  942. defer redis.Close()
  943. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(patientID, 10) + ":" + strconv.FormatInt(recordDate, 10) + ":assessment_after_dislysis"
  944. assessment_after_dislysis_str, _ := redis.Get(key).Result()
  945. if len(assessment_after_dislysis_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  946. err := readDb.Model(&models.AssessmentAfterDislysis{}).Where("patient_id = ? and user_org_id = ? and status = 1 and assessment_date = ?", patientID, orgID, recordDate).First(&record).Error
  947. if err != nil {
  948. if err == gorm.ErrRecordNotFound {
  949. if record.ID <= 0 {
  950. redis.Set(key, "null", time.Second*60*60*18)
  951. }
  952. return nil, nil
  953. } else {
  954. return nil, err
  955. }
  956. } else {
  957. if record.ID > 0 {
  958. //缓存数据
  959. assessment_after_dislysis_str, err := json.Marshal(record)
  960. if err == nil {
  961. redis.Set(key, assessment_after_dislysis_str, time.Second*60*60*18)
  962. }
  963. } else {
  964. redis.Set(key, "null", time.Second*60*60*18)
  965. }
  966. return &record, nil
  967. }
  968. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  969. if assessment_after_dislysis_str == "null" {
  970. return &record, nil
  971. } else {
  972. json.Unmarshal([]byte(assessment_after_dislysis_str), &record)
  973. return &record, nil
  974. }
  975. }
  976. }
  977. // 获取 maxDate 之前一次的透后评估记录
  978. func MobileGetLastTimeAssessmentAfterDislysis(orgID int64, patientID int64, maxDate int64) (*models.AssessmentAfterDislysis, error) {
  979. var record models.AssessmentAfterDislysis
  980. redis := RedisClient()
  981. defer redis.Close()
  982. // cur_date := time.Now().Format("2006-01-02")
  983. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(patientID, 10) + ":" + strconv.FormatInt(maxDate, 10) + ":assessment_after_dislysis_last"
  984. assessment_after_dislysis_last_str, _ := redis.Get(key).Result()
  985. if len(assessment_after_dislysis_last_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  986. err := readDb.Model(&models.AssessmentAfterDislysis{}).Where("patient_id = ? and user_org_id = ? and status = 1 and assessment_date < ?", patientID, orgID, maxDate).Order("assessment_date desc").First(&record).Error
  987. if err != nil {
  988. if err == gorm.ErrRecordNotFound {
  989. if record.ID <= 0 {
  990. redis.Set(key, "null", time.Second*60*60*18)
  991. }
  992. return nil, nil
  993. } else {
  994. return nil, err
  995. }
  996. } else {
  997. if record.ID > 0 {
  998. //缓存数据
  999. assessment_after_dislysis_last_str, err := json.Marshal(record)
  1000. if err == nil {
  1001. redis.Set(key, assessment_after_dislysis_last_str, time.Second*60*60*18)
  1002. }
  1003. } else {
  1004. redis.Set(key, "null", time.Second*60*60*18)
  1005. }
  1006. return &record, nil
  1007. }
  1008. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  1009. if assessment_after_dislysis_last_str == "null" {
  1010. return &record, nil
  1011. } else {
  1012. json.Unmarshal([]byte(assessment_after_dislysis_last_str), &record)
  1013. return &record, nil
  1014. }
  1015. }
  1016. }
  1017. func MobileGetLastTimeAssessmentAfterDislysisOne(orgID int64, patientID int64, maxDate int64) (*models.AssessmentAfterDislysis, error) {
  1018. var record models.AssessmentAfterDislysis
  1019. err := readDb.Model(&models.AssessmentAfterDislysis{}).Where("patient_id = ? and user_org_id = ? and status = 1 and assessment_date < ?", patientID, orgID, maxDate).Order("assessment_date desc").First(&record).Error
  1020. if err != nil {
  1021. if err == gorm.ErrRecordNotFound {
  1022. return nil, nil
  1023. } else {
  1024. return nil, err
  1025. }
  1026. }
  1027. return &record, nil
  1028. }
  1029. func MobileGetLast(orgID int64, patientID int64, maxDate int64) (models.AssessmentAfterDislysis, error) {
  1030. dislysis := models.AssessmentAfterDislysis{}
  1031. err := readDb.Model(&models.AssessmentAfterDislysis{}).Where("patient_id = ? and user_org_id = ? and status = 1 and assessment_date < ?", patientID, orgID, maxDate).Order("assessment_date desc").First(&dislysis).Error
  1032. return dislysis, err
  1033. }
  1034. // 治疗小结
  1035. func MobileGetTreatmentSummary(orgID int64, patientID int64, recordDate int64) (*models.TreatmentSummary, error) {
  1036. var record models.TreatmentSummary
  1037. redis := RedisClient()
  1038. defer redis.Close()
  1039. // cur_date := time.Now().Format("2006-01-02")
  1040. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(patientID, 10) + ":" + strconv.FormatInt(recordDate, 10) + ":treatment_summary"
  1041. treatment_summary_str, _ := redis.Get(key).Result()
  1042. if len(treatment_summary_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1043. err := readDb.Model(&models.TreatmentSummary{}).Where("patient_id = ? and user_org_id = ? and status = 1 and assessment_date = ?", patientID, orgID, recordDate).First(&record).Error
  1044. if err != nil {
  1045. if err == gorm.ErrRecordNotFound {
  1046. if record.ID <= 0 {
  1047. redis.Set(key, "null", time.Second*60*60*18)
  1048. }
  1049. return nil, nil
  1050. } else {
  1051. return nil, err
  1052. }
  1053. } else {
  1054. if record.ID > 0 {
  1055. //缓存数据
  1056. treatment_summary_str, err := json.Marshal(record)
  1057. if err == nil {
  1058. redis.Set(key, treatment_summary_str, time.Second*60*60*18)
  1059. }
  1060. } else {
  1061. redis.Set(key, "null", time.Second*60*60*18)
  1062. }
  1063. return &record, nil
  1064. }
  1065. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  1066. if treatment_summary_str == "null" {
  1067. return &record, nil
  1068. } else {
  1069. json.Unmarshal([]byte(treatment_summary_str), &record)
  1070. return &record, nil
  1071. }
  1072. }
  1073. }
  1074. // 透析处方
  1075. func MobileGetDialysisPrescribe(orgID int64, patientID int64, recordDate int64) (*models.DialysisPrescription, error) {
  1076. var record models.DialysisPrescription
  1077. err := readDb.Model(&models.DialysisPrescription{}).Where("patient_id = ? and user_org_id = ? and status = 1 and record_date = ?", patientID, orgID, recordDate).Find(&record).Error
  1078. if err != nil {
  1079. if err == gorm.ErrRecordNotFound {
  1080. return nil, nil
  1081. } else {
  1082. return nil, err
  1083. }
  1084. }
  1085. return &record, nil
  1086. }
  1087. func MobileGetDialysisSchedual(orgID int64, patientID int64, scheduleDate int64) (*models.XtSchedule, error) {
  1088. var schedule models.XtSchedule
  1089. err := readDb.Model(&models.XtSchedule{}).Where("patient_id = ? and user_org_id = ? and status = 1 and schedule_date = ?", patientID, orgID, scheduleDate).First(&schedule).Error
  1090. if err != nil {
  1091. if err == gorm.ErrRecordNotFound {
  1092. return nil, nil
  1093. } else {
  1094. return nil, err
  1095. }
  1096. }
  1097. return &schedule, nil
  1098. }
  1099. // 透析方案
  1100. func MobileGetDialysisSolution(orgID int64, patientID int64) (*models.DialysisSolution, error) {
  1101. var record models.DialysisSolution
  1102. err := readDb.Model(&models.DialysisSolution{}).Where("patient_id = ? and user_org_id = ? and status = 1", patientID, orgID).Last(&record).Error
  1103. if err != nil {
  1104. if err == gorm.ErrRecordNotFound {
  1105. return nil, nil
  1106. } else {
  1107. return nil, err
  1108. }
  1109. }
  1110. return &record, nil
  1111. }
  1112. func MobileGetPatientById(orgID int64, patientID int64) (*models.Patients, error) {
  1113. var patient models.Patients
  1114. err := readDb.Model(&models.Patients{}).Where("id = ? and user_org_id = ? and status = 1", patientID, orgID).First(&patient).Error
  1115. if err != nil {
  1116. if err == gorm.ErrRecordNotFound {
  1117. return nil, nil
  1118. } else {
  1119. return nil, err
  1120. }
  1121. }
  1122. return &patient, nil
  1123. }
  1124. func DisableMonitor(orgID int64, patientID int64, recordID int64, admin_user_id int64) error {
  1125. fmt.Println()
  1126. tx := writeDb.Begin()
  1127. updateTime := time.Now().Unix()
  1128. err := tx.Model(&models.MonitoringRecord{}).Where("user_org_id = ? AND patient_id = ? AND id = ? AND status = 1 ", orgID, patientID, recordID).Updates(map[string]interface{}{"status": 0, "updated_time": updateTime, "modify": admin_user_id}).Error
  1129. if err != nil {
  1130. tx.Rollback()
  1131. return err
  1132. }
  1133. tx.Commit()
  1134. return nil
  1135. }
  1136. func GetMonitor(orgID int64, patientID int64, id int64) (*models.MonitoringRecord, error) {
  1137. var monitor models.MonitoringRecord
  1138. var err error
  1139. err = readDb.Model(&models.MonitoringRecord{}).Where("id = ? AND user_org_id = ? AND patient_id = ? AND status = 1 ", id, orgID, patientID).Find(&monitor).Error
  1140. if err == gorm.ErrRecordNotFound {
  1141. return nil, nil
  1142. }
  1143. if err != nil {
  1144. return nil, err
  1145. }
  1146. return &monitor, nil
  1147. }
  1148. type MScheduleDoctorAdviceVM struct {
  1149. ID int64 `gorm:"column:id" json:"id"`
  1150. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id"`
  1151. PartitionId int64 `gorm:"column:partition_id" json:"partition_id"`
  1152. BedId int64 `gorm:"column:bed_id" json:"bed_id"`
  1153. PatientId int64 `gorm:"column:patient_id" json:"patient_id"`
  1154. ScheduleDate int64 `gorm:"column:schedule_date" json:"schedule_date"`
  1155. ScheduleType int64 `gorm:"column:schedule_type" json:"schedule_type"`
  1156. ModeId int64 `gorm:"column:mode_id" json:"mode_id"`
  1157. Status int64 `gorm:"column:status" json:"status"`
  1158. DialysisOrder *MDialysisOrderVM `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"dialysis_order"`
  1159. SchedualPatient *MSchedualPatientVM `gorm:"ForeignKey:PatientId;AssociationForeignKey:ID" json:"patient"`
  1160. DeviceNumber *MDeviceNumberVM `gorm:"ForeignKey:BedId" json:"device_number"`
  1161. DoctorAdvices []*MDoctorAdviceVM `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"doctor_advice"`
  1162. Prescription *models.DialysisPrescriptionList `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"prescription"`
  1163. DialysisAssesmentBefor *models.DialysisAssesmentBefor `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"dialysisassesmentbefor"`
  1164. }
  1165. func (MScheduleDoctorAdviceVM) TableName() string {
  1166. return "xt_schedule"
  1167. }
  1168. type MScheduleDoctorAdviceVMOne struct {
  1169. ID int64 `gorm:"column:id" json:"id"`
  1170. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id"`
  1171. PartitionId int64 `gorm:"column:partition_id" json:"partition_id"`
  1172. BedId int64 `gorm:"column:bed_id" json:"bed_id"`
  1173. PatientId int64 `gorm:"column:patient_id" json:"patient_id"`
  1174. ScheduleDate int64 `gorm:"column:schedule_date" json:"schedule_date"`
  1175. ScheduleType int64 `gorm:"column:schedule_type" json:"schedule_type"`
  1176. ModeId int64 `gorm:"column:mode_id" json:"mode_id"`
  1177. Status int64 `gorm:"column:status" json:"status"`
  1178. DialysisOrder *MDialysisOrderVM `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"dialysis_order"`
  1179. SchedualPatient *MSchedualPatientVM `gorm:"ForeignKey:PatientId;AssociationForeignKey:ID" json:"patient"`
  1180. DeviceNumber *MDeviceNumberVM `gorm:"ForeignKey:BedId" json:"device_number"`
  1181. DoctorAdvices []*MDoctorAdviceVM `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"doctor_advice"`
  1182. Prescription *models.DialysisPrescriptionList `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"prescription"`
  1183. //DialysisAssesmentBefor *models.DialysisAssesmentBefor `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"dialysisassesmentbefor"`
  1184. }
  1185. func (MScheduleDoctorAdviceVMOne) TableName() string {
  1186. return "xt_schedule"
  1187. }
  1188. type MDoctorAdviceVM struct {
  1189. ID int64 `gorm:"column:id" json:"id"`
  1190. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id"`
  1191. PatientId int64 `gorm:"column:patient_id" json:"patient_id"`
  1192. AdviceType int64 `gorm:"column:advice_type" json:"advice_type"`
  1193. AdviceDate int64 `gorm:"column:advice_date" json:"advice_date"`
  1194. StartTime int64 `gorm:"column:start_time" json:"start_time"`
  1195. AdviceName string `gorm:"column:advice_name" json:"advice_name"`
  1196. AdviceDesc string `gorm:"column:advice_desc" json:"advice_desc"`
  1197. ReminderDate int64 `gorm:"column:reminder_date" json:"reminder_date"`
  1198. SingleDose float64 `gorm:"column:single_dose" json:"single_dose"`
  1199. SingleDoseUnit string `gorm:"column:single_dose_unit" json:"single_dose_unit"`
  1200. PrescribingNumber float64 `gorm:"column:prescribing_number" json:"prescribing_number"`
  1201. PrescribingNumberUnit string `gorm:"column:prescribing_number_unit" json:"prescribing_number_unit"`
  1202. DeliveryWay string `gorm:"column:delivery_way" json:"delivery_way"`
  1203. ExecutionFrequency string `gorm:"column:execution_frequency" json:"execution_frequency"`
  1204. AdviceDoctor int64 `gorm:"column:advice_doctor" json:"advice_doctor"`
  1205. Status int64 `gorm:"column:status" json:"status"`
  1206. CreatedTime int64 `gorm:"column:created_time" json:"created_time"`
  1207. UpdatedTime int64 `gorm:"column:updated_time" json:"updated_time"`
  1208. AdviceAffirm string `gorm:"column:advice_affirm" json:"advice_affirm"`
  1209. Remark string `gorm:"column:remark" json:"remark"`
  1210. StopTime int64 `gorm:"column:stop_time" json:"stop_time"`
  1211. StopReason string `gorm:"column:stop_reason" json:"stop_reason"`
  1212. StopDoctor int64 `gorm:"column:stop_doctor" json:"stop_doctor"`
  1213. StopState int64 `gorm:"column:stop_state" json:"stop_state"`
  1214. ParentId int64 `gorm:"column:parent_id" json:"parent_id"`
  1215. ExecutionTime int64 `gorm:"column:execution_time" json:"execution_time"`
  1216. ExecutionStaff int64 `gorm:"column:execution_staff" json:"execution_staff"`
  1217. ExecutionState int64 `gorm:"column:execution_state" json:"execution_state"`
  1218. Checker int64 `gorm:"column:checker" json:"checker"`
  1219. RecordDate int64 `gorm:"column:record_date" json:"record_date"`
  1220. DialysisOrderId int64 `gorm:"column:dialysis_order_id" json:"dialysis_order_id"`
  1221. CheckTime int64 `gorm:"column:check_time" json:"check_time"`
  1222. CheckState int64 `gorm:"column:check_state" json:"check_state"`
  1223. DrugSpec float64 `gorm:"column:drug_spec" json:"drug_spec"`
  1224. DrugSpecUnit string `gorm:"column:drug_spec_unit" json:"drug_spec_unit"`
  1225. Groupno int64 `gorm:"column:groupno" json:"groupno"`
  1226. RemindType int64 `gorm:"column:remind_type" json:"remind_type"`
  1227. FrequencyType int64 `gorm:"column:frequency_type" json:"frequency_type"`
  1228. DayCount int64 `gorm:"column:day_count" json:"day_count"`
  1229. WeekDay string `gorm:"column:week_day" json:"week_day"`
  1230. TemplateId string `gorm:"column:template_id" json:"template_id"`
  1231. Modifier int64 `gorm:"column:modifier" json:"modifier" form:"modifier"`
  1232. IsMedicine int64 `gorm:"column:is_medicine" json:"is_medicine" form:"is_medicine"`
  1233. }
  1234. func (MDoctorAdviceVM) TableName() string {
  1235. return "xt_doctor_advice"
  1236. }
  1237. func MobileGetScheduleDoctorAdvices(orgID int64, scheduleDate int64, adviceType int, patientType int, adminUserId int64, deliverWay string) ([]*MScheduleDoctorAdviceVM, error) {
  1238. var vms []*MScheduleDoctorAdviceVM
  1239. adviceWhere := ""
  1240. adviceCondition := []interface{}{}
  1241. if adviceType == 0 {
  1242. if patientType == 0 {
  1243. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND (advice_type = 3 OR advice_type = 1)"
  1244. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  1245. } else if patientType == 1 {
  1246. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_doctor = ? AND(advice_type = 3 OR advice_type = 1)"
  1247. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId)
  1248. } else if patientType == 2 {
  1249. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND execution_staff = 0 AND(advice_type = 3 OR advice_type = 1)"
  1250. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  1251. }
  1252. } else if adviceType == 1 {
  1253. if patientType == 0 {
  1254. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 1 AND record_date = ? "
  1255. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  1256. } else if patientType == 1 {
  1257. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 1 AND record_date = ? AND advice_doctor = ? "
  1258. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId)
  1259. } else if patientType == 2 {
  1260. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 1 AND record_date = ? AND execution_staff = 0"
  1261. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  1262. }
  1263. } else if adviceType == 3 {
  1264. if patientType == 0 {
  1265. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 3 AND record_date = ? "
  1266. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  1267. } else if patientType == 1 {
  1268. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 3 AND advice_doctor = ? "
  1269. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId)
  1270. } else if patientType == 2 {
  1271. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 3 AND execution_staff = 0"
  1272. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  1273. }
  1274. } else if adviceType == 2 && len(deliverWay) > 0 {
  1275. if patientType == 0 {
  1276. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 2 AND record_date = ? and delivery_way = ?"
  1277. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, deliverWay)
  1278. } else if patientType == 1 {
  1279. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 2 AND advice_doctor = ? and delivery_way = ? "
  1280. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId, deliverWay)
  1281. } else if patientType == 2 {
  1282. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 2 AND execution_staff = 0 and delivery_way = ?"
  1283. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, deliverWay)
  1284. }
  1285. } else if adviceType == 2 && len(deliverWay) <= 0 {
  1286. if patientType == 0 {
  1287. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 2 AND record_date = ?"
  1288. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  1289. } else if patientType == 1 {
  1290. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 2 AND (advice_doctor = ? or execution_staff = ?)"
  1291. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId, adminUserId)
  1292. } else if patientType == 2 {
  1293. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 2 AND execution_staff = 0"
  1294. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  1295. }
  1296. }
  1297. db := readDb.
  1298. Table("xt_schedule").
  1299. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  1300. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  1301. return db.Where("status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, scheduleDate).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  1302. }).
  1303. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  1304. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  1305. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  1306. Preload("DialysisAssesmentBefor", "status =1 AND user_org_id = ? and assessment_date =?", orgID, scheduleDate).
  1307. Preload("DoctorAdvices", adviceCondition...).
  1308. Where("status = 1 AND user_org_id = ?", orgID)
  1309. if scheduleDate != 0 {
  1310. db = db.Where("schedule_date = ?", scheduleDate)
  1311. }
  1312. err := db.Find(&vms).Error
  1313. return vms, err
  1314. }
  1315. func MobileGetScheduleDoctorAdvicesTwo(orgID int64, scheduleDate int64, adviceType int, patientType int, adminUserId int64, deliverWay string) ([]*MScheduleDoctorAdviceVMOne, error) {
  1316. var vms []*MScheduleDoctorAdviceVMOne
  1317. adviceWhere := ""
  1318. adviceCondition := []interface{}{}
  1319. if adviceType == 0 {
  1320. if patientType == 0 {
  1321. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND (advice_type = 3 OR advice_type = 1)"
  1322. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  1323. } else if patientType == 1 {
  1324. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_doctor = ? AND(advice_type = 3 OR advice_type = 1)"
  1325. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId)
  1326. } else if patientType == 2 {
  1327. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND execution_staff = 0 AND(advice_type = 3 OR advice_type = 1)"
  1328. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  1329. }
  1330. } else if adviceType == 1 {
  1331. if patientType == 0 {
  1332. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 1 AND record_date = ? "
  1333. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  1334. } else if patientType == 1 {
  1335. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 1 AND record_date = ? AND advice_doctor = ? "
  1336. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId)
  1337. } else if patientType == 2 {
  1338. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 1 AND record_date = ? AND execution_staff = 0"
  1339. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  1340. }
  1341. } else if adviceType == 3 {
  1342. if patientType == 0 {
  1343. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 3 AND record_date = ? "
  1344. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  1345. } else if patientType == 1 {
  1346. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 3 AND advice_doctor = ? "
  1347. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId)
  1348. } else if patientType == 2 {
  1349. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 3 AND execution_staff = 0"
  1350. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  1351. }
  1352. } else if adviceType == 2 && len(deliverWay) > 0 {
  1353. if patientType == 0 {
  1354. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 2 AND record_date = ? and delivery_way = ?"
  1355. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, deliverWay)
  1356. } else if patientType == 1 {
  1357. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 2 AND advice_doctor = ? and delivery_way = ? "
  1358. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId, deliverWay)
  1359. } else if patientType == 2 {
  1360. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 2 AND execution_staff = 0 and delivery_way = ?"
  1361. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, deliverWay)
  1362. }
  1363. } else if adviceType == 2 && len(deliverWay) <= 0 {
  1364. if patientType == 0 {
  1365. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 2 AND record_date = ?"
  1366. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  1367. } else if patientType == 1 {
  1368. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 2 AND (advice_doctor = ? or execution_staff = ?)"
  1369. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId, adminUserId)
  1370. } else if patientType == 2 {
  1371. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 2 AND execution_staff = 0"
  1372. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  1373. }
  1374. }
  1375. db := readDb.
  1376. Table("xt_schedule").
  1377. //Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  1378. //Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  1379. // return db.Where("status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, scheduleDate).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  1380. //}).
  1381. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  1382. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  1383. //Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  1384. Preload("DoctorAdvices", adviceCondition...).
  1385. Where("status = 1 AND user_org_id = ?", orgID)
  1386. if scheduleDate != 0 {
  1387. db = db.Where("schedule_date = ?", scheduleDate)
  1388. }
  1389. err := db.Find(&vms).Error
  1390. return vms, err
  1391. }
  1392. func GetHisDoctorAdvices(orgID int64, scheduleDate int64, deliverWay string, patientType int, adminUserId int64) ([]*HisMScheduleDoctorAdviceVM, error) {
  1393. var vms []*HisMScheduleDoctorAdviceVM
  1394. if len(deliverWay) > 0 {
  1395. if patientType == 0 {
  1396. db := readDb.
  1397. Table("xt_schedule").
  1398. Preload("HisDoctorAdviceInfo", "status = 1 AND user_org_id = ? AND advice_date = ? and delivery_way = ?", orgID, scheduleDate, deliverWay).
  1399. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  1400. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  1401. Preload("HisPrescriptionProject", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  1402. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  1403. return db.Where("status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, scheduleDate).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  1404. }).
  1405. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  1406. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  1407. Where("status = 1 AND user_org_id = ?", orgID)
  1408. if scheduleDate != 0 {
  1409. db = db.Where("schedule_date = ?", scheduleDate)
  1410. }
  1411. err = db.Find(&vms).Error
  1412. }
  1413. if patientType > 0 {
  1414. db := readDb.
  1415. Table("xt_schedule").
  1416. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  1417. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  1418. Preload("HisDoctorAdviceInfo", "status = 1 AND user_org_id = ? AND advice_date = ? and delivery_way = ? and (advice_doctor = ? or execution_staff = ?) ", orgID, scheduleDate, deliverWay, adminUserId, adminUserId).
  1419. Preload("HisPrescriptionProject", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  1420. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  1421. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  1422. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  1423. return db.Where("status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, scheduleDate).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  1424. }).Where("status = 1 AND user_org_id = ?", orgID)
  1425. if scheduleDate != 0 {
  1426. db = db.Where("schedule_date = ?", scheduleDate)
  1427. }
  1428. err = db.Find(&vms).Error
  1429. }
  1430. } else {
  1431. if patientType == 0 {
  1432. db := readDb.
  1433. Table("xt_schedule").
  1434. Preload("HisDoctorAdviceInfo", "status = 1 AND user_org_id = ? AND advice_date = ? ", orgID, scheduleDate).
  1435. Preload("HisPrescriptionProject", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  1436. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  1437. return db.Where("status = 1 AND user_org_id = ? and dialysis_date = ?", orgID, scheduleDate).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  1438. }).
  1439. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  1440. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  1441. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  1442. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  1443. Where("status = 1 AND user_org_id = ?", orgID)
  1444. if scheduleDate != 0 {
  1445. db = db.Where("schedule_date = ?", scheduleDate)
  1446. }
  1447. err = db.Find(&vms).Error
  1448. }
  1449. if patientType > 0 {
  1450. db := readDb.
  1451. Table("xt_schedule").
  1452. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  1453. Preload("HisDoctorAdviceInfo", "status = 1 AND user_org_id = ? AND advice_date = ? and (advice_doctor = ? or execution_staff = ?) ", orgID, scheduleDate, adminUserId, adminUserId).
  1454. Preload("HisPrescriptionProject", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  1455. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  1456. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  1457. return db.Where("status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, scheduleDate).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  1458. }).Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  1459. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  1460. Where("status = 1 AND user_org_id = ?", orgID)
  1461. if scheduleDate != 0 {
  1462. db = db.Where("schedule_date = ?", scheduleDate)
  1463. }
  1464. err = db.Find(&vms).Error
  1465. }
  1466. }
  1467. return vms, err
  1468. }
  1469. func GetHisDoctorAdvicesTwo(orgID int64, scheduleDate int64, deliverWay string, patientType int, adminUserId int64) ([]*HisMScheduleDoctorAdviceVMOne, error) {
  1470. var vms []*HisMScheduleDoctorAdviceVMOne
  1471. if len(deliverWay) > 0 {
  1472. if patientType == 0 {
  1473. db := readDb.
  1474. Table("xt_schedule").
  1475. Preload("HisDoctorAdviceInfo", "status = 1 AND user_org_id = ? AND advice_date = ? and delivery_way = ?", orgID, scheduleDate, deliverWay).
  1476. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  1477. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  1478. Preload("HisPrescriptionProject", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  1479. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  1480. return db.Where("status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, scheduleDate).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  1481. }).
  1482. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  1483. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  1484. Where("status = 1 AND user_org_id = ?", orgID)
  1485. if scheduleDate != 0 {
  1486. db = db.Where("schedule_date = ?", scheduleDate)
  1487. }
  1488. err = db.Find(&vms).Error
  1489. }
  1490. if patientType > 0 {
  1491. db := readDb.
  1492. Table("xt_schedule").
  1493. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  1494. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  1495. Preload("HisDoctorAdviceInfo", "status = 1 AND user_org_id = ? AND advice_date = ? and delivery_way = ? and (advice_doctor = ? or execution_staff = ?) ", orgID, scheduleDate, deliverWay, adminUserId, adminUserId).
  1496. Preload("HisPrescriptionProject", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  1497. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  1498. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  1499. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  1500. return db.Where("status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, scheduleDate).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  1501. }).Where("status = 1 AND user_org_id = ?", orgID)
  1502. if scheduleDate != 0 {
  1503. db = db.Where("schedule_date = ?", scheduleDate)
  1504. }
  1505. err = db.Find(&vms).Error
  1506. }
  1507. } else {
  1508. if patientType == 0 {
  1509. db := readDb.
  1510. Table("xt_schedule").
  1511. Preload("HisDoctorAdviceInfo", "status = 1 AND user_org_id = ? AND advice_date = ? ", orgID, scheduleDate).
  1512. Preload("HisPrescriptionProject", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  1513. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  1514. return db.Where("status = 1 AND user_org_id = ? and dialysis_date = ?", orgID, scheduleDate).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  1515. }).
  1516. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  1517. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  1518. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  1519. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  1520. Where("status = 1 AND user_org_id = ?", orgID)
  1521. if scheduleDate != 0 {
  1522. db = db.Where("schedule_date = ?", scheduleDate)
  1523. }
  1524. err = db.Find(&vms).Error
  1525. }
  1526. if patientType > 0 {
  1527. db := readDb.
  1528. Table("xt_schedule").
  1529. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  1530. Preload("HisDoctorAdviceInfo", "status = 1 AND user_org_id = ? AND advice_date = ? and (advice_doctor = ? or execution_staff = ?) ", orgID, scheduleDate, adminUserId, adminUserId).
  1531. Preload("HisPrescriptionProject", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  1532. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  1533. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  1534. return db.Where("status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, scheduleDate).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  1535. }).Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  1536. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  1537. Where("status = 1 AND user_org_id = ?", orgID)
  1538. if scheduleDate != 0 {
  1539. db = db.Where("schedule_date = ?", scheduleDate)
  1540. }
  1541. err = db.Find(&vms).Error
  1542. }
  1543. }
  1544. return vms, err
  1545. }
  1546. func GetMobileHisPrescriptionProject(orgID int64, scheduleDate int64, deliverWay string, patientType int, adminUserId int64) ([]*HisMScheduleProjectVM, error) {
  1547. var vms []*HisMScheduleProjectVM
  1548. //if patientType == 0 {
  1549. // db := readDb.
  1550. // Table("xt_schedule").
  1551. // Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  1552. // Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  1553. // return db.Where("status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, scheduleDate).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  1554. // }).
  1555. // Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  1556. // Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  1557. // Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  1558. // Preload("HisPrescriptionProject", func(db *gorm.DB) *gorm.DB {
  1559. // return db.Where("status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).Preload("HisProject").Preload("GoodInfo", "status=1")
  1560. // }).
  1561. // Where("status = 1 AND user_org_id = ?", orgID)
  1562. // if scheduleDate != 0 {
  1563. // db = db.Where("schedule_date = ?", scheduleDate)
  1564. // }
  1565. // err = db.Find(&vms).Error
  1566. //}
  1567. //if patientType > 0 {
  1568. // db := readDb.
  1569. // Table("xt_schedule").
  1570. // Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  1571. // Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  1572. // return db.Where("status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, scheduleDate).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  1573. // }).
  1574. // Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  1575. // Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  1576. // Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  1577. // Preload("HisDoctorAdviceInfo", "status = 1 AND user_org_id = ? AND advice_date = ? and (advice_doctor = ? or execution_staff = ?) ", orgID, scheduleDate, adminUserId, adminUserId).
  1578. // Preload("HisPrescriptionProject", func(db *gorm.DB) *gorm.DB {
  1579. // return db.Where("status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).Preload("HisProject").Preload("GoodInfo", "status=1")
  1580. // }).
  1581. // Where("status = 1 AND user_org_id = ?", orgID)
  1582. // if scheduleDate != 0 {
  1583. // db = db.Where("schedule_date = ?", scheduleDate)
  1584. // }
  1585. // err = db.Find(&vms).Error
  1586. //}
  1587. db := readDb.Table("xt_schedule").Where("status = 1")
  1588. if orgID > 0 {
  1589. db = db.Where("user_org_id = ?", orgID)
  1590. }
  1591. if scheduleDate > 0 {
  1592. db = db.Where("schedule_date =?", scheduleDate)
  1593. }
  1594. err = db.Find(&vms).Error
  1595. if patientType == 0 {
  1596. db := readDb.
  1597. Table("xt_schedule").
  1598. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  1599. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  1600. return db.Where("status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, scheduleDate).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  1601. }).
  1602. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  1603. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  1604. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  1605. Preload("HisPrescriptionProject", func(db *gorm.DB) *gorm.DB {
  1606. return db.Where("status = 1 AND user_org_id = ? AND record_date = ? AND team_id = 0", orgID, scheduleDate).Preload("HisProject").Preload("GoodInfo", "status=1")
  1607. }).
  1608. Preload("HisPrescriptionTeamProject", func(db *gorm.DB) *gorm.DB {
  1609. return db.Where("status = 1 AND user_org_id = ? AND record_date = ? AND team_id > 0", orgID, scheduleDate).Preload("XtHisProjectTeam", "status = 1").Preload("HisProject").Preload("GoodInfo", "status=1")
  1610. }).
  1611. Where("status = 1 AND user_org_id = ?", orgID)
  1612. if scheduleDate != 0 {
  1613. db = db.Where("schedule_date = ?", scheduleDate)
  1614. }
  1615. err = db.Find(&vms).Error
  1616. }
  1617. if patientType > 0 {
  1618. db := readDb.
  1619. Table("xt_schedule").
  1620. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  1621. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  1622. return db.Where("status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, scheduleDate).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  1623. }).
  1624. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  1625. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  1626. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  1627. Preload("HisDoctorAdviceInfo", "status = 1 AND user_org_id = ? AND advice_date = ? and (advice_doctor = ? or execution_staff = ?) ", orgID, scheduleDate, adminUserId, adminUserId).
  1628. Preload("HisPrescriptionProject", func(db *gorm.DB) *gorm.DB {
  1629. return db.Where("status = 1 AND user_org_id = ? AND record_date = ? AND team_id = 0", orgID, scheduleDate).Preload("HisProject").Preload("GoodInfo", "status=1")
  1630. }).
  1631. Preload("HisPrescriptionTeamProject", func(db *gorm.DB) *gorm.DB {
  1632. return db.Where("status = 1 AND user_org_id = ? AND record_date = ? AND team_id > 0", orgID, scheduleDate).Preload("XtHisProjectTeam", "status = 1").Preload("HisProject").Preload("GoodInfo", "status=1")
  1633. }).
  1634. Where("status = 1 AND user_org_id = ?", orgID)
  1635. if scheduleDate != 0 {
  1636. db = db.Where("schedule_date = ?", scheduleDate)
  1637. }
  1638. err = db.Find(&vms).Error
  1639. }
  1640. return vms, err
  1641. }
  1642. func MobileCreateDialysisOrder(orgID int64, patientID int64, order *models.DialysisOrder) error {
  1643. now := time.Now()
  1644. tx := writeDb.Begin()
  1645. if createOrderErr := tx.Model(&models.DialysisOrder{}).Create(order).Error; createOrderErr != nil {
  1646. tx.Rollback()
  1647. return createOrderErr
  1648. }
  1649. // 更新透析记录 ID
  1650. // 透析处方
  1651. if err := tx.Model(&models.DialysisPrescription{}).Where("user_org_id = ? AND patient_id = ? AND record_date = ? AND status = 1", orgID, patientID, order.DialysisDate).Updates(map[string]interface{}{"record_id": order.ID, "updated_time": now.Unix()}).Error; err != nil {
  1652. tx.Rollback()
  1653. return err
  1654. }
  1655. // 接诊评估
  1656. if err := tx.Model(&models.ReceiveTreatmentAsses{}).Where("user_org_id = ? AND patient_id = ? AND record_date = ? AND status = 1", orgID, patientID, order.DialysisDate).Updates(map[string]interface{}{"record_id": order.ID, "update_time": now.Unix()}).Error; err != nil {
  1657. tx.Rollback()
  1658. return err
  1659. }
  1660. // 透前评估
  1661. if err := tx.Model(&models.PredialysisEvaluation{}).Where("user_org_id = ? AND patient_id = ? AND assessment_date = ? AND status = 1", orgID, patientID, order.DialysisDate).Updates(map[string]interface{}{"dialysis_order_id": order.ID, "updated_time": now.Unix()}).Error; err != nil {
  1662. tx.Rollback()
  1663. return err
  1664. }
  1665. // 临时医嘱
  1666. if err := tx.Model(&models.DoctorAdvice{}).Where("user_org_id = ? AND patient_id = ? AND record_date = ? AND status = 1 AND advice_type = 2", orgID, patientID, order.DialysisDate).Updates(map[string]interface{}{"dialysis_order_id": order.ID, "updated_time": now.Unix()}).Error; err != nil {
  1667. tx.Rollback()
  1668. return err
  1669. }
  1670. // 双人核对
  1671. if err := tx.Model(&models.DoubleCheck{}).Where("user_org_id = ? AND patient_id = ? AND check_date = ? AND status = 1", orgID, patientID, order.DialysisDate).Updates(map[string]interface{}{"dialysis_order_id": order.ID, "updated_time": now.Unix()}).Error; err != nil {
  1672. tx.Rollback()
  1673. return err
  1674. }
  1675. // 透后评估
  1676. if err := tx.Model(&models.AssessmentAfterDislysis{}).Where("user_org_id = ? AND patient_id = ? AND assessment_date = ? AND status = 1", orgID, patientID, order.DialysisDate).Updates(map[string]interface{}{"dialysis_order_id": order.ID, "updated_time": now.Unix()}).Error; err != nil {
  1677. tx.Rollback()
  1678. return err
  1679. }
  1680. // 治疗小结
  1681. if err := tx.Model(&models.TreatmentSummary{}).Where("user_org_id = ? AND patient_id = ? AND assessment_date = ? AND status = 1", orgID, patientID, order.DialysisDate).Updates(map[string]interface{}{"dialysis_order_id": order.ID, "updated_time": now.Unix()}).Error; err != nil {
  1682. tx.Rollback()
  1683. return err
  1684. }
  1685. // 透析监测
  1686. if err := tx.Model(&models.MonitoringRecord{}).Where("patient_id = ? and user_org_id=? and status=1 and monitoring_date = ?", patientID, orgID, order.DialysisDate).Update(map[string]interface{}{"dialysis_order_id": order.ID, "updated_time": now.Unix()}).Error; err != nil {
  1687. tx.Rollback()
  1688. return err
  1689. }
  1690. tx.Commit()
  1691. return nil
  1692. }
  1693. type MobileUrgentSchedulePatientVM struct {
  1694. ID int64 `gorm:"column:id" json:"id"`
  1695. Name string `gorm:"column:name" json:"name"`
  1696. DialysisNo string `gorm:"column:dialysis_no" json:"dialysis_no"`
  1697. }
  1698. func (MobileUrgentSchedulePatientVM) TableName() string {
  1699. return "xt_patients"
  1700. }
  1701. type MobileUrgentScheduleTreatmentModeVM struct {
  1702. ID int64 `gorm:"column:id" json:"id"`
  1703. Name string `gorm:"column:name" json:"name"`
  1704. }
  1705. func (MobileUrgentScheduleTreatmentModeVM) TableName() string {
  1706. return "xt_treatment_mode"
  1707. }
  1708. func MobileGetAllPatientsForUrgentSchedule(orgID int64, record_date int64) ([]*MobileUrgentSchedulePatientVM, error) {
  1709. var vms []*MobileUrgentSchedulePatientVM = make([]*MobileUrgentSchedulePatientVM, 0)
  1710. rows, err := readDb.Raw("SELECT p.* FROM xt_patients as p WHERE (p.user_org_id = ? AND p.status = 1 AND p.lapseto = 1) AND NOT EXISTS (Select * FROM `xt_dialysis_order` as d Where d.`dialysis_date` = ? AND d.`status` = 1 AND d.`patient_id` = p.id AND d.user_org_id = p.user_org_id )", orgID, record_date).Rows()
  1711. defer rows.Close()
  1712. if err != nil {
  1713. return nil, err
  1714. }
  1715. for rows.Next() {
  1716. var vm MobileUrgentSchedulePatientVM
  1717. readDb.ScanRows(rows, &vm)
  1718. vms = append(vms, &vm)
  1719. }
  1720. return vms, nil
  1721. }
  1722. func MobileGetAllTrearmentModesForUrgentSchedule() ([]*MobileUrgentScheduleTreatmentModeVM, error) {
  1723. var modes []*MobileUrgentScheduleTreatmentModeVM
  1724. err := readDb.Model(&MobileUrgentScheduleTreatmentModeVM{}).Where("status = 1").Find(&modes).Error
  1725. if err != nil {
  1726. return nil, err
  1727. }
  1728. return modes, nil
  1729. }
  1730. type MobileUrgentScheduleScheduleListVM struct {
  1731. ID int64 `gorm:"column:id" json:"id"`
  1732. // ZoneID int64 `gorm:"column:partition_id" json:"zone_id"`
  1733. DeviceNumberID int64 `gorm:"column:bed_id" json:"bed_id"`
  1734. PatientID int64 `gorm:"column:patient_id" json:"patient_id"`
  1735. ScheduleType int `gorm:"column:schedule_type" json:"schedule_type"`
  1736. DeviceNumber *models.DeviceNumber `gorm:"ForeignKey:DeviceNumberID" json:"device_number"`
  1737. // DeviceZone *models.DeviceZone `gorm:"ForeignKey:ZoneID" json:"device_zone"`
  1738. }
  1739. func (MobileUrgentScheduleScheduleListVM) TableName() string {
  1740. return "xt_schedule"
  1741. }
  1742. func MobileGetSchedulesForUrgentSchedule(orgID int64, scheduleDate int64, schedule_type int) (schedules []*MobileUrgentScheduleScheduleListVM, err error) {
  1743. db := readDb.
  1744. Model(&MobileUrgentScheduleScheduleListVM{}).
  1745. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  1746. Where("status = 1 AND user_org_id = ? AND schedule_date = ? AND schedule_type = ? ", orgID, scheduleDate, schedule_type)
  1747. err = db.Find(&schedules).Error
  1748. if err != nil {
  1749. return nil, err
  1750. }
  1751. return schedules, nil
  1752. }
  1753. func MobileGetOtherSchedulesForUrgentSchedule(orgID int64, scheduleDate int64, schedule_type int) ([]*MobileUrgentScheduleScheduleListVM, error) {
  1754. var vms []*MobileUrgentScheduleScheduleListVM = make([]*MobileUrgentScheduleScheduleListVM, 0)
  1755. rows, err := readDb.Raw("SELECT * FROM xt_schedule as s WHERE s.user_org_id = ? AND s.status = 1 AND s.schedule_date = ? ", orgID, scheduleDate).Rows()
  1756. defer rows.Close()
  1757. if err != nil {
  1758. return nil, err
  1759. }
  1760. for rows.Next() {
  1761. var vm MobileUrgentScheduleScheduleListVM
  1762. readDb.ScanRows(rows, &vm)
  1763. vms = append(vms, &vm)
  1764. }
  1765. return vms, nil
  1766. }
  1767. type MobileUrgentScheduleDeviceNumberVM struct {
  1768. ID int64 `gorm:"column:id" json:"id"`
  1769. Number string `gorm:"column:number" json:"number"`
  1770. ZoneID int64 `gorm:"column:zone_id" json:"zone_id"`
  1771. Zone *models.DeviceZone `gorm:"ForeignKey:ZoneID" json:"zone"`
  1772. }
  1773. func (MobileUrgentScheduleDeviceNumberVM) TableName() string {
  1774. return "xt_device_number"
  1775. }
  1776. func MobileGetAllDeviceNumbersForUrgentSchedule(orgID int64, record_date int64, schedule_type int) ([]*DeviceNumberViewModel, error) {
  1777. var vms []*DeviceNumberViewModel = make([]*DeviceNumberViewModel, 0)
  1778. rows, err := readDb.Raw("SELECT n.*, z.name as zone_name, g.name as group_name FROM xt_device_number as n join xt_device_zone as z on z.id = n.zone_id join xt_device_group as g on g.id = n.group_id WHERE (n.org_id = ? AND n.status = 1) AND NOT EXISTS (Select * FROM xt_schedule as s Where s.`schedule_date` = ? AND s.user_org_id = n.org_id AND s.`bed_id` = n.id AND s.`schedule_type` = ? AND s.status = 1 )", orgID, record_date, schedule_type).Rows()
  1779. defer rows.Close()
  1780. if err != nil {
  1781. return nil, err
  1782. }
  1783. for rows.Next() {
  1784. var vm DeviceNumberViewModel
  1785. readDb.ScanRows(rows, &vm)
  1786. vms = append(vms, &vm)
  1787. }
  1788. return vms, nil
  1789. //var deviceNumbers []*MobileUrgentScheduleDeviceNumberVM
  1790. //db := readDb.
  1791. // Model(&MobileUrgentScheduleDeviceNumberVM{}).
  1792. // Preload("Zone", "status = 1 AND org_id = ?", orgID).
  1793. // Where("status = 1 AND org_id = ?", orgID)
  1794. //err := db.Order("zone_id asc").Find(&deviceNumbers).Error
  1795. //if err != nil {
  1796. // return nil, err
  1797. //}
  1798. //return deviceNumbers, nil
  1799. }
  1800. func GetValidScheduleMonitorRecordCount() (int64, error) {
  1801. var total int64
  1802. err := readDb.Model(&models.MonitoringRecord{}).Where("status = 1 AND operate_time = 0 AND monitoring_date <> 0").Count(&total).Error
  1803. return total, err
  1804. }
  1805. func GetTop1000ValidScheduleMonitorRecord() ([]*models.MonitoringRecord, error) {
  1806. var monitors []*models.MonitoringRecord
  1807. err := readDb.Model(&models.MonitoringRecord{}).Where("status = 1 AND operate_time = 0 AND monitoring_date <> 0").Order("id asc").Limit(100).Find(&monitors).Error
  1808. if err != nil {
  1809. return nil, err
  1810. }
  1811. return monitors, nil
  1812. }
  1813. func BatchUpdateMonitors(monitors []*models.MonitoringRecord) error {
  1814. tx := writeDb.Begin()
  1815. for index := 0; index < len(monitors); index++ {
  1816. tx.Save(monitors[index])
  1817. }
  1818. return tx.Commit().Error
  1819. }
  1820. func ModifyStartDialysisOrder(order *models.DialysisOrder) error {
  1821. tx := writeDb.Begin()
  1822. updateTime := time.Now().Unix()
  1823. err := tx.Model(&models.DialysisOrder{}).Where("user_org_id = ? AND id = ? AND status = 1 ", order.UserOrgId, order.ID).Updates(map[string]interface{}{"start_nurse": order.StartNurse, "updated_time": updateTime, "bed_id": order.BedID, "puncture_nurse": order.PunctureNurse, "start_time": order.StartTime, "modifier": order.Modifier, "schedual_type": order.SchedualType, "washpipe_nurse": order.WashpipeNurse, "change_nurse": order.ChangeNurse, "difficult_puncture_nurse": order.DifficultPunctureNurse, "new_fistula_nurse": order.NewFistulaNurse}).Error
  1824. if err != nil {
  1825. tx.Rollback()
  1826. return err
  1827. }
  1828. tx.Commit()
  1829. return err
  1830. }
  1831. func ModifyFinishDialysisOrder(order *models.DialysisOrder) error {
  1832. tx := writeDb.Begin()
  1833. updateTime := time.Now().Unix()
  1834. err := tx.Model(&models.DialysisOrder{}).Where("user_org_id = ? AND id = ? AND status = 1 ", order.UserOrgId, order.ID).Updates(map[string]interface{}{"finish_nurse": order.FinishNurse, "updated_time": updateTime, "end_time": order.EndTime, "finish_modifier": order.FinishModifier, "puncture_point_haematoma": order.PuncturePointHaematoma, "blood_access_internal_fistula": order.BloodAccessInternalFistula, "catheter": order.Catheter, "cruor": order.Cruor}).Error
  1835. if err != nil {
  1836. tx.Rollback()
  1837. return err
  1838. }
  1839. tx.Commit()
  1840. return err
  1841. }
  1842. func MobileGetLastDryWeight(orgID int64, patientID int64) (*models.SgjPatientDryweight, error) {
  1843. var record models.SgjPatientDryweight
  1844. err := readDb.Model(&models.SgjPatientDryweight{}).Where("patient_id = ? and user_org_id = ? and status = 1", patientID, orgID).Last(&record).Error
  1845. if err != nil {
  1846. if err == gorm.ErrRecordNotFound {
  1847. return nil, nil
  1848. } else {
  1849. return nil, err
  1850. }
  1851. }
  1852. return &record, nil
  1853. }
  1854. //func MobileGetLastDryWeight(orgID int64, patientID int64) (*models.SgjPatientDryweight, error) {
  1855. // var record models.SgjPatientDryweight
  1856. // redis := RedisClient()
  1857. // defer redis.Close()
  1858. //
  1859. // key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(patientID, 10) + ":last_dry_weight"
  1860. // last_dry_weight_str, _ := redis.Get(key).Result()
  1861. //
  1862. // if len(last_dry_weight_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1863. // err := readDb.Model(&models.SgjPatientDryweight{}).Where("patient_id = ? and user_org_id = ? and status = 1", patientID, orgID).Last(&record).Error
  1864. // if err != nil {
  1865. // if err == gorm.ErrRecordNotFound {
  1866. // if record.ID <= 0 {
  1867. // redis.Set(key, "null", time.Second*60*60*18)
  1868. // }
  1869. // return nil, nil
  1870. // } else {
  1871. // return nil, err
  1872. // }
  1873. // } else {
  1874. // if record.ID > 0 {
  1875. // //缓存数据
  1876. // last_dry_weight_str, err := json.Marshal(record)
  1877. // if err == nil {
  1878. // redis.Set(key, last_dry_weight_str, time.Second*60*60*18)
  1879. // return nil, err
  1880. // }
  1881. // } else {
  1882. // redis.Set(key, "null", time.Second*60*60*18)
  1883. // return nil, err
  1884. // }
  1885. // return &record, nil
  1886. // }
  1887. // } else { //缓存数据了数据,将redis缓存的json字符串转为map
  1888. // if last_dry_weight_str == "null" {
  1889. // return &record, nil
  1890. // } else {
  1891. // json.Unmarshal([]byte(last_dry_weight_str), &record)
  1892. // return &record, nil
  1893. // }
  1894. //
  1895. // }
  1896. //}
  1897. // 透析方案
  1898. func MobileGetDialysisSolutionByModeIdSix(orgID int64, patientID int64, mode_id int64) (*models.DialysisSolution, error) {
  1899. var record models.DialysisSolution
  1900. err := readDb.Model(&models.DialysisSolution{}).Where("patient_id = ? and user_org_id = ? and status = 1 AND mode_id = ?", patientID, orgID, mode_id).Last(&record).Error
  1901. if err != nil {
  1902. if err == gorm.ErrRecordNotFound {
  1903. return nil, nil
  1904. } else {
  1905. return nil, err
  1906. }
  1907. }
  1908. return &record, nil
  1909. }
  1910. // 透析方案
  1911. func MobileGetDialysisSolutionByModeId(orgID int64, patientID int64, mode_id int64) (*models.DialysisSolution, error) {
  1912. var record models.DialysisSolution
  1913. redis := RedisClient()
  1914. defer redis.Close()
  1915. // cur_date := time.Now().Format("2006-01-02")
  1916. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(patientID, 10) + ":" + strconv.FormatInt(mode_id, 10) + ":dialysis_solution"
  1917. dialysis_solution_str, _ := redis.Get(key).Result()
  1918. if len(dialysis_solution_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1919. err := readDb.Model(&models.DialysisSolution{}).Where("patient_id = ? and user_org_id = ? and status = 1 AND mode_id = ?", patientID, orgID, mode_id).Last(&record).Error
  1920. if err != nil {
  1921. if err == gorm.ErrRecordNotFound {
  1922. if record.ID <= 0 {
  1923. redis.Set(key, "null", time.Second*60*60*18)
  1924. }
  1925. return nil, nil
  1926. } else {
  1927. return nil, err
  1928. }
  1929. } else {
  1930. if record.ID > 0 {
  1931. //缓存数据
  1932. dialysis_solution_str, err := json.Marshal(record)
  1933. if err == nil {
  1934. redis.Set(key, dialysis_solution_str, time.Second*60*60*18)
  1935. }
  1936. } else {
  1937. redis.Set(key, "null", time.Second*60*60*18)
  1938. }
  1939. return &record, nil
  1940. }
  1941. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  1942. if dialysis_solution_str == "null" {
  1943. return &record, nil
  1944. } else {
  1945. json.Unmarshal([]byte(dialysis_solution_str), &record)
  1946. return &record, nil
  1947. }
  1948. }
  1949. }
  1950. // 透析处方
  1951. func MobileGetDialysisPrescribeByModeId(orgID int64, patientID int64, recordDate int64, mode_id int64) (*models.DialysisPrescription, error) {
  1952. var record models.DialysisPrescription
  1953. redis := RedisClient()
  1954. defer redis.Close()
  1955. // cur_date := time.Now().Format("2006-01-02")
  1956. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(patientID, 10) + ":" + strconv.FormatInt(recordDate, 10) + ":" + strconv.FormatInt(mode_id, 10) + ":dialysis_prescribe"
  1957. fmt.Println("key23322323232323", key)
  1958. dialysis_prescribe_str, _ := redis.Get(key).Result()
  1959. if len(dialysis_prescribe_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1960. err := readDb.Model(&models.DialysisPrescription{}).Where("patient_id = ? and user_org_id = ? and status = 1 and record_date = ? AND mode_id = ?", patientID, orgID, recordDate, mode_id).First(&record).Error
  1961. if err != nil {
  1962. if err == gorm.ErrRecordNotFound {
  1963. if record.ID <= 0 {
  1964. redis.Set(key, "null", time.Second*60*60*18)
  1965. }
  1966. return nil, nil
  1967. } else {
  1968. return nil, err
  1969. }
  1970. } else {
  1971. if record.ID > 0 {
  1972. //缓存数据
  1973. dialysis_prescribe_str, err := json.Marshal(record)
  1974. if err == nil {
  1975. redis.Set(key, dialysis_prescribe_str, time.Second*60*60*18)
  1976. }
  1977. } else {
  1978. redis.Set(key, "null", time.Second*60*60*18)
  1979. }
  1980. return &record, nil
  1981. }
  1982. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  1983. if dialysis_prescribe_str == "null" {
  1984. return &record, nil
  1985. } else {
  1986. json.Unmarshal([]byte(dialysis_prescribe_str), &record)
  1987. return &record, nil
  1988. }
  1989. }
  1990. }
  1991. func MobileGetDialysisPrescribeByModeIdSix(orgID int64, patientID int64, recordDate int64, mode_id int64) (*models.DialysisPrescription, error) {
  1992. var record models.DialysisPrescription
  1993. err := readDb.Model(&models.DialysisPrescription{}).Where("patient_id = ? and user_org_id = ? and status = 1 and record_date = ? AND mode_id = ?", patientID, orgID, recordDate, mode_id).First(&record).Error
  1994. if err != nil {
  1995. if err == gorm.ErrRecordNotFound {
  1996. return nil, nil
  1997. } else {
  1998. return nil, err
  1999. }
  2000. }
  2001. return &record, nil
  2002. }
  2003. func MobileGetDialysisPrescribeByModeIdOne(orgID int64, patientID int64, recordDate int64) (*models.DialysisPrescription, error) {
  2004. var record models.DialysisPrescription
  2005. err := readDb.Model(&models.DialysisPrescription{}).Where("patient_id = ? and user_org_id = ? and status = 1 and record_date = ?", patientID, orgID, recordDate).First(&record).Error
  2006. if err != nil {
  2007. if err == gorm.ErrRecordNotFound {
  2008. return nil, nil
  2009. } else {
  2010. return nil, err
  2011. }
  2012. }
  2013. return &record, nil
  2014. }
  2015. //func MobileGetDialysisPrescribeByModeIdOne(orgID int64, patientID int64, recordDate int64) (*models.DialysisPrescription, error) {
  2016. // var record models.DialysisPrescription
  2017. // redis := RedisClient()
  2018. // defer redis.Close()
  2019. //
  2020. // // cur_date := time.Now().Format("2006-01-02")
  2021. // key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(patientID, 10) + ":" + strconv.FormatInt(recordDate, 10) + ":dialysis_prescribe_by_mode"
  2022. // dialysis_prescribe_by_mode_str, _ := redis.Get(key).Result()
  2023. //
  2024. // if len(dialysis_prescribe_by_mode_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2025. // err := readDb.Model(&models.DialysisPrescription{}).Where("patient_id = ? and user_org_id = ? and status = 1 and record_date = ?", patientID, orgID, recordDate).First(&record).Error
  2026. // if err != nil {
  2027. // if err == gorm.ErrRecordNotFound {
  2028. // if record.ID <= 0 {
  2029. // redis.Set(key, "null", time.Second*60*60*18)
  2030. // }
  2031. // return nil, nil
  2032. // } else {
  2033. // return nil, err
  2034. // }
  2035. // } else {
  2036. //
  2037. // if record.ID > 0 {
  2038. // //缓存数据
  2039. // dialysis_prescribe_by_mode_str, err := json.Marshal(record)
  2040. //
  2041. // if err == nil {
  2042. // redis.Set(key, dialysis_prescribe_by_mode_str, time.Second*60*60*18)
  2043. // return nil, err
  2044. // }
  2045. // } else {
  2046. // redis.Set(key, "null", time.Second*60*60*18)
  2047. // return nil, err
  2048. // }
  2049. // return &record, nil
  2050. // }
  2051. // } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2052. // if dialysis_prescribe_by_mode_str == "null" {
  2053. // json.Unmarshal([]byte(dialysis_prescribe_by_mode_str), &record)
  2054. // return &record, nil
  2055. // } else {
  2056. // json.Unmarshal([]byte(dialysis_prescribe_by_mode_str), &record)
  2057. // return &record, nil
  2058. // }
  2059. //
  2060. // }
  2061. //}
  2062. func MobileGetLastDialysisPrescribe(orgID int64, patientID int64) (*models.DialysisPrescription, error) {
  2063. var record models.DialysisPrescription
  2064. err := readDb.Model(&models.DialysisPrescription{}).Where("patient_id = ? and user_org_id = ? and status = 1 ", patientID, orgID).Last(&record).Error
  2065. if err != nil {
  2066. if err == gorm.ErrRecordNotFound {
  2067. return nil, nil
  2068. } else {
  2069. return nil, err
  2070. }
  2071. }
  2072. return &record, nil
  2073. }
  2074. func MobileGetLastDialysisPrescribeByModeIdSix(orgID int64, patientID int64, mode_id int64) (*models.DialysisPrescription, error) {
  2075. var record models.DialysisPrescription
  2076. err := readDb.Model(&models.DialysisPrescription{}).Where("patient_id = ? and user_org_id = ? and status = 1 AND mode_id = ?", patientID, orgID, mode_id).Last(&record).Error
  2077. if err != nil {
  2078. if err == gorm.ErrRecordNotFound {
  2079. return nil, nil
  2080. } else {
  2081. return nil, err
  2082. }
  2083. }
  2084. return &record, nil
  2085. }
  2086. func MobileGetLastDialysisPrescribeByModeId(orgID int64, patientID int64, mode_id int64) (*models.DialysisPrescription, error) {
  2087. var record models.DialysisPrescription
  2088. redis := RedisClient()
  2089. defer redis.Close()
  2090. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(patientID, 10) + ":" + strconv.FormatInt(mode_id, 10) + ":dialysis_prescribe_by_mode"
  2091. dialysis_prescribe_by_mode_id, _ := redis.Get(key).Result()
  2092. if len(dialysis_prescribe_by_mode_id) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2093. err := readDb.Model(&models.DialysisPrescription{}).Where("patient_id = ? and user_org_id = ? and status = 1 and mode_id = ?", patientID, orgID, mode_id).Last(&record).Error
  2094. if err != nil {
  2095. if err == gorm.ErrRecordNotFound {
  2096. if record.ID <= 0 {
  2097. redis.Set(key, "null", time.Second*60*60*18)
  2098. }
  2099. return nil, nil
  2100. } else {
  2101. return nil, err
  2102. }
  2103. } else {
  2104. if record.ID > 0 {
  2105. //缓存数据
  2106. dialysis_prescribe_by_mode_str, err := json.Marshal(record)
  2107. if err == nil {
  2108. redis.Set(key, dialysis_prescribe_by_mode_str, time.Second*60*60*18)
  2109. return nil, err
  2110. }
  2111. } else {
  2112. redis.Set(key, "null", time.Second*60*60*18)
  2113. return nil, err
  2114. }
  2115. return &record, nil
  2116. }
  2117. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2118. if dialysis_prescribe_by_mode_id == "null" {
  2119. return &record, nil
  2120. } else {
  2121. json.Unmarshal([]byte(dialysis_prescribe_by_mode_id), &record)
  2122. return &record, nil
  2123. }
  2124. }
  2125. }
  2126. func MobileGetLastDialysisPrescribeByModeIdOne(orgID int64, patientID int64, mode_id int64) (models.DialysisPrescription, error) {
  2127. var record models.DialysisPrescription
  2128. err := readDb.Model(&models.DialysisPrescription{}).Where("patient_id = ? and user_org_id = ? and status = 1 and mode_id = ?", patientID, orgID, mode_id).Last(&record).Error
  2129. return record, err
  2130. }
  2131. func GetAllAvaildDeviceNumbers(orgID int64, record_date int64, schedule_type int) ([]*DeviceNumberViewModel, error) {
  2132. var vms []*DeviceNumberViewModel = make([]*DeviceNumberViewModel, 0)
  2133. rows, err := readDb.Raw("SELECT n.*, z.name as zone_name, g.name as group_name FROM xt_device_number as n join xt_device_zone as z on z.id = n.zone_id join xt_device_group as g on g.id = n.group_id WHERE (n.org_id = ? AND n.status = 1) AND NOT EXISTS (Select * FROM xt_schedule as s Where s.`schedule_date` = ? AND s.user_org_id = n.org_id AND s.`bed_id` = n.id AND s.`schedule_type` = ? AND s.status = 1 )", orgID, record_date, schedule_type).Rows()
  2134. defer rows.Close()
  2135. if err != nil {
  2136. return nil, err
  2137. }
  2138. for rows.Next() {
  2139. var vm DeviceNumberViewModel
  2140. readDb.ScanRows(rows, &vm)
  2141. vms = append(vms, &vm)
  2142. }
  2143. return vms, nil
  2144. }
  2145. // 获取 maxDate 之前一次的透前评估记录
  2146. func GetLastTimePredialysisEvaluation(orgID int64, patientID int64, maxDate int64) (*models.PredialysisEvaluation, error) {
  2147. var record models.PredialysisEvaluation
  2148. err := readDb.Model(&models.PredialysisEvaluation{}).Where("patient_id = ? and user_org_id = ? and status = 1 and assessment_date < ?", patientID, orgID, maxDate).Order("assessment_date desc").First(&record).Error
  2149. if err != nil {
  2150. if err == gorm.ErrRecordNotFound {
  2151. return nil, nil
  2152. } else {
  2153. return nil, err
  2154. }
  2155. }
  2156. return &record, nil
  2157. }
  2158. func GetLastTimeOrder(orgID int64, patientID int64, maxDate int64) (*models.DialysisOrder, error) {
  2159. var record models.DialysisOrder
  2160. err := readDb.Model(&models.DialysisOrder{}).Where("patient_id = ? and user_org_id = ? and status = 1 and dialysis_date < ?", patientID, orgID, maxDate).Order("dialysis_date desc").First(&record).Error
  2161. if err != nil {
  2162. if err == gorm.ErrRecordNotFound {
  2163. return nil, nil
  2164. } else {
  2165. return nil, err
  2166. }
  2167. }
  2168. return &record, nil
  2169. }
  2170. func GetLastMonitorRecord(orgID int64, patientID int64, beforeDate int64) (*models.MonitoringRecord, error) {
  2171. var record models.MonitoringRecord
  2172. err := readDb.Model(&models.MonitoringRecord{}).Where("patient_id = ? and user_org_id = ? and status = 1 and monitoring_date = ?", patientID, orgID, beforeDate).Order("operate_time desc").First(&record).Error
  2173. if err != nil {
  2174. if err == gorm.ErrRecordNotFound {
  2175. return nil, nil
  2176. } else {
  2177. return nil, err
  2178. }
  2179. }
  2180. return &record, nil
  2181. }
  2182. // 获取 maxDate 之前一次的透后评估记录
  2183. func GetLastTimeAssessmentAfterDislysis(orgID int64, patientID int64, maxDate int64) (*models.AssessmentAfterDislysis, error) {
  2184. var record models.AssessmentAfterDislysis
  2185. err := readDb.Model(&models.AssessmentAfterDislysis{}).Where("patient_id = ? and user_org_id = ? and status = 1 and assessment_date < ?", patientID, orgID, maxDate).Order("assessment_date desc").First(&record).Error
  2186. if err != nil {
  2187. if err == gorm.ErrRecordNotFound {
  2188. return nil, nil
  2189. } else {
  2190. return nil, err
  2191. }
  2192. }
  2193. return &record, nil
  2194. }
  2195. // 透析处方
  2196. func GetDialysisPrescribe(orgID int64, patientID int64, recordDate int64) (*models.DialysisPrescription, error) {
  2197. var record models.DialysisPrescription
  2198. err := readDb.Model(&models.DialysisPrescription{}).Where("patient_id = ? and user_org_id = ? and status = 1 and record_date = ?", patientID, orgID, recordDate).First(&record).Error
  2199. if err != nil {
  2200. if err == gorm.ErrRecordNotFound {
  2201. return nil, nil
  2202. } else {
  2203. return nil, err
  2204. }
  2205. }
  2206. return &record, nil
  2207. }
  2208. // 透析方案
  2209. func GetDialysisSolution(orgID int64, patientID int64, mode_id int64) (*models.DialysisSolution, error) {
  2210. var record models.DialysisSolution
  2211. err := readDb.Model(&models.DialysisSolution{}).Where("patient_id = ? and user_org_id = ? and status = 1 AND mode_id = ?", patientID, orgID, mode_id).Last(&record).Error
  2212. if err != nil {
  2213. if err == gorm.ErrRecordNotFound {
  2214. return nil, nil
  2215. } else {
  2216. return nil, err
  2217. }
  2218. }
  2219. return &record, nil
  2220. }
  2221. func GetDialysisSolutionOne(orgID int64, mode_id int64) (*models.DialysisSolution, error) {
  2222. var record models.DialysisSolution
  2223. err := readDb.Model(&models.DialysisSolution{}).Where("user_org_id = ? and status = 1 AND mode_id = ?", orgID, mode_id).Last(&record).Error
  2224. if err != nil {
  2225. if err == gorm.ErrRecordNotFound {
  2226. return nil, nil
  2227. } else {
  2228. return nil, err
  2229. }
  2230. }
  2231. return &record, nil
  2232. }
  2233. func GetLastDialysisPrescribeByModeId(orgID int64, patientID int64, mode_id int64) (*models.DialysisPrescription, error) {
  2234. var record models.DialysisPrescription
  2235. err := readDb.Model(&models.DialysisPrescription{}).Where("patient_id = ? and user_org_id = ? and status = 1 AND mode_id = ?", patientID, orgID, mode_id).Last(&record).Error
  2236. if err != nil {
  2237. if err == gorm.ErrRecordNotFound {
  2238. return nil, nil
  2239. } else {
  2240. return nil, err
  2241. }
  2242. }
  2243. return &record, nil
  2244. }
  2245. func GetLastDryWeight(orgID int64, patientID int64) (*models.SgjPatientDryweight, error) {
  2246. var record models.SgjPatientDryweight
  2247. err := readDb.Model(&models.SgjPatientDryweight{}).Where("patient_id = ? and user_org_id = ? and status = 1", patientID, orgID).Last(&record).Error
  2248. if err != nil {
  2249. if err == gorm.ErrRecordNotFound {
  2250. return nil, nil
  2251. } else {
  2252. return nil, err
  2253. }
  2254. }
  2255. return &record, nil
  2256. }
  2257. func MobileGetSystemDialysisPrescribeByModeIdSix(orgID int64, mode_id int64) (*models.SystemPrescription, error) {
  2258. var record models.SystemPrescription
  2259. err := readDb.Model(&models.SystemPrescription{}).Where("user_org_id = ? and status = 1 AND mode_id = ?", orgID, mode_id).First(&record).Error
  2260. if err != nil {
  2261. if err == gorm.ErrRecordNotFound {
  2262. return nil, nil
  2263. } else {
  2264. return nil, err
  2265. }
  2266. }
  2267. return &record, nil
  2268. }
  2269. func MobileGetSystemDialysisPrescribeByModeId(orgID int64, mode_id int64) (*models.SystemPrescription, error) {
  2270. var record models.SystemPrescription
  2271. redis := RedisClient()
  2272. defer redis.Close()
  2273. // cur_date := time.Now().Format("2006-01-02")
  2274. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(mode_id, 10) + ":system_dialysis_prescribe"
  2275. system_dialysis_prescribe_str, _ := redis.Get(key).Result()
  2276. if len(system_dialysis_prescribe_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2277. err := readDb.Model(&models.SystemPrescription{}).Where("user_org_id = ? and status = 1 AND mode_id = ?", orgID, mode_id).Last(&record).Error
  2278. if err != nil {
  2279. if err == gorm.ErrRecordNotFound {
  2280. if record.ID <= 0 {
  2281. redis.Set(key, "null", time.Second*60*60*18)
  2282. }
  2283. return nil, nil
  2284. } else {
  2285. return nil, err
  2286. }
  2287. } else {
  2288. if record.ID > 0 {
  2289. //缓存数据
  2290. system_dialysis_prescribe_str, err := json.Marshal(record)
  2291. if err == nil {
  2292. redis.Set(key, system_dialysis_prescribe_str, time.Second*60*60*18)
  2293. }
  2294. } else {
  2295. redis.Set(key, "null", time.Second*60*60*18)
  2296. }
  2297. return &record, nil
  2298. }
  2299. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2300. if system_dialysis_prescribe_str == "null" {
  2301. return &record, nil
  2302. } else {
  2303. json.Unmarshal([]byte(system_dialysis_prescribe_str), &record)
  2304. return &record, nil
  2305. }
  2306. }
  2307. }
  2308. func MobileGetSystemDialysisPrescribeByModeIdOne(orgID int64, mode_id int64) (models.SystemPrescription, error) {
  2309. var record models.SystemPrescription
  2310. err := readDb.Model(&models.SystemPrescription{}).Where("user_org_id = ? and status = 1 AND mode_id = ?", orgID, mode_id).Last(&record).Error
  2311. return record, err
  2312. }
  2313. func GetSystemDialysisPrescribeByModeId(orgID int64, mode_id int64) (*models.SystemPrescription, error) {
  2314. var record models.SystemPrescription
  2315. err := readDb.Model(&models.SystemPrescription{}).Where("user_org_id = ? and status = 1 AND mode_id = ?", orgID, mode_id).Last(&record).Error
  2316. if err != nil {
  2317. if err == gorm.ErrRecordNotFound {
  2318. return nil, nil
  2319. } else {
  2320. return nil, err
  2321. }
  2322. }
  2323. return &record, nil
  2324. }
  2325. func GetDialysisOrderCount(orgID int64, patient_id int64, recordDate int64) (count int64, err error) {
  2326. err = readDb.Model(&models.DialysisOrder{}).Where("dialysis_date <= ? AND status = 1 AND stage = 2 AND user_org_id = ? AND patient_id = ?", recordDate, orgID, patient_id).Count(&count).Error
  2327. return
  2328. }
  2329. func GetDialysisOrderCountOne(orgID int64, patient_id int64, recordDate int64) (count int64, err error) {
  2330. err = readDb.Model(&models.DialysisOrder{}).Where("dialysis_date>=1640966400 and dialysis_date <= ? AND status = 1 AND stage = 2 AND user_org_id = ? AND patient_id = ?", recordDate, orgID, patient_id).Count(&count).Error
  2331. return
  2332. }
  2333. func GetFirstDateOfMonth(d time.Time) time.Time {
  2334. d = d.AddDate(0, 0, -d.Day()+1)
  2335. return GetZeroTime(d)
  2336. }
  2337. func GetZeroTime(d time.Time) time.Time {
  2338. return time.Date(d.Year(), d.Month(), d.Day(), 0, 0, 0, 0, d.Location())
  2339. }
  2340. func GetDialysisCountByPatientId(startime int64, endtime int64, patientid int64, orgid int64) (order []*models.BloodDialysisOrderCount, err error) {
  2341. db := XTReadDB().Table("xt_dialysis_order as o")
  2342. err = 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.dialysis_date>=? and o.dialysis_date<=? and o.user_org_id = ? and x.schedule_date = o.dialysis_date and x.status = 1 and o.patient_id = ?", startime, endtime, orgid, patientid).Scan(&order).Group("o.patient_id").Error
  2343. return order, err
  2344. }
  2345. func FindConsumablesByDate(orgID int64, patient_id int64, recordDate int64) (consumables []*models.DialysisBeforePrepare, err error) {
  2346. err = readDb.Model(&models.DialysisBeforePrepare{}).Where("user_org_id = ? AND patient_id = ? AND record_date = ? AND status = 1 AND count<>0", orgID, patient_id, recordDate).Find(&consumables).Error
  2347. return
  2348. }
  2349. func FindConsumablesByDateTwo(orgID int64, patient_id int64, recordDate int64) (consumables []*models.DialysisBeforePrepare, err error) {
  2350. err = readDb.Model(&models.DialysisBeforePrepare{}).Where("user_org_id = ? AND patient_id = ? AND record_date = ? AND status = 1 AND count > 0", orgID, patient_id, recordDate).Find(&consumables).Error
  2351. return
  2352. }
  2353. func FindConsumablesByDateThree(orgID int64, patient_id int64, recordDate int64) (consumables []*models.BloodAutomaticReduceDetail, err error) {
  2354. err = readDb.Model(&models.BloodAutomaticReduceDetail{}).Where("org_id = ? AND patient_id = ? AND record_time = ? AND status = 1 AND count > 0", orgID, patient_id, recordDate).Find(&consumables).Error
  2355. return
  2356. }
  2357. //func FindLastConsumables(orgID int64, patient_id int64, recordDate int64) (consumables []*models.DialysisBeforePrepare, err error){
  2358. // err = readDb.Model(&models.DialysisBeforePrepare{}).Where("user_org_id = ? AND patient_id = ? AND record_date = ? AND status = 1", orgID, patient_id,recordDate).Find(&consumables).Error
  2359. // return
  2360. //}
  2361. //
  2362. func GetLastTimeConsumables(orgID int64, patientID int64, maxDate int64) (prepare []*models.DialysisBeforePrepare, err error) {
  2363. err = readDb.Model(&models.DialysisBeforePrepare{}).Where("patient_id = ? and user_org_id = ? and status = 1 and record_date < ?", patientID, orgID, maxDate).Order("record_date desc").Find(&prepare).Error
  2364. return
  2365. }
  2366. type GoodsType struct {
  2367. ID int64 `gorm:"column:id" json:"id"`
  2368. TypeName string `gorm:"column:type_name" json:"type_name"`
  2369. Remark string `gorm:"column:remark" json:"remark"`
  2370. OrgId int64 `gorm:"column:org_id" json:"org_id"`
  2371. Status int64 `gorm:"column:status" json:"status"`
  2372. Type int64 `gorm:"column:type" json:"type"`
  2373. }
  2374. func (GoodsType) TableName() string {
  2375. return "xt_goods_type"
  2376. }
  2377. type VMGoodInfo struct {
  2378. ID int64 `gorm:"column:id" json:"id"`
  2379. SpecificationName string `gorm:"column:specification_name" json:"specification_name"`
  2380. GoodTypeId int64 `gorm:"column:good_type_id" json:"good_type_id"`
  2381. OrgId int64 `gorm:"column:org_id" json:"org_id"`
  2382. GoodName string `gorm:"column:good_name" json:"good_name" form:"good_name"`
  2383. GoodUnit int64 `gorm:"column:good_unit" json:"good_unit" form:"good_unit"`
  2384. Total float64 `gorm:"column:total" json:"total" form:"total"`
  2385. PackingUnit string `gorm:"column:packing_unit" json:"packing_unit" form:"packing_unit"`
  2386. }
  2387. func (VMGoodInfo) TableName() string {
  2388. return "xt_good_information"
  2389. }
  2390. type AutomaticReduceDetail struct {
  2391. ID int64 `gorm:"column:id" json:"id"`
  2392. PatientId int64 `gorm:"column:patient_id" json:"patient_id"`
  2393. Status int64 `gorm:"column:status" json:"status"`
  2394. RecordTime int64 `gorm:"column:record_time" json:"record_time"`
  2395. OrgId int64 `gorm:"column:org_id" json:"org_id"`
  2396. GoodId int64 `gorm:"column:good_id" json:"good_id"`
  2397. GoodTypeId int64 `gorm:"column:good_type_id" json:"good_type_id"`
  2398. VMGoodInfo VMGoodInfo `gorm:"ForeignKey:GoodId;AssociationForeignKey:ID" json:"info"`
  2399. GoodsType GoodsType `gorm:"ForeignKey:GoodTypeId;AssociationForeignKey:ID" json:"type"`
  2400. Count int64 `gorm:"column:count" json:"count"`
  2401. }
  2402. func (AutomaticReduceDetail) TableName() string {
  2403. return "xt_automatic_reduce_detail"
  2404. }
  2405. type DialysisBeforePrepare struct {
  2406. ID int64 `gorm:"column:id" json:"id" form:"id"`
  2407. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id" form:"user_org_id"`
  2408. PatientId int64 `gorm:"column:patient_id" json:"patient_id" form:"patient_id"`
  2409. RecordDate int64 `gorm:"column:record_date" json:"record_date" form:"record_date"`
  2410. GoodId int64 `gorm:"column:good_id" json:"good_id" form:"good_id"`
  2411. GoodTypeId int64 `gorm:"column:good_type_id" json:"good_type_id" form:"good_type_id"`
  2412. Count int64 `gorm:"column:count" json:"count" form:"count"`
  2413. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  2414. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  2415. Creater int64 `gorm:"column:creater" json:"creater" form:"creater"`
  2416. Modifier int64 `gorm:"column:modifier" json:"modifier" form:"modifier"`
  2417. VMGoodInfo VMGoodInfo `gorm:"ForeignKey:GoodId;AssociationForeignKey:ID" json:"info"`
  2418. GoodsType GoodsType `gorm:"ForeignKey:GoodTypeId;AssociationForeignKey:ID" json:"type"`
  2419. }
  2420. func (DialysisBeforePrepare) TableName() string {
  2421. return "dialysis_before_prepare"
  2422. }
  2423. type MDialysisGoodsVM struct {
  2424. ID int64 `gorm:"column:id" json:"id"`
  2425. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id"`
  2426. PartitionId int64 `gorm:"column:partition_id" json:"partition_id"`
  2427. BedId int64 `gorm:"column:bed_id" json:"bed_id"`
  2428. PatientId int64 `gorm:"column:patient_id" json:"patient_id"`
  2429. ScheduleDate int64 `gorm:"column:schedule_date" json:"schedule_date"`
  2430. ScheduleType int64 `gorm:"column:schedule_type" json:"schedule_type"`
  2431. ScheduleWeek int64 `gorm:"column:schedule_week" json:"schedule_week"`
  2432. ModeId int64 `gorm:"column:mode_id" json:"mode_id"`
  2433. Status int64 `gorm:"column:status" json:"status"`
  2434. DeviceNumber *MDeviceNumberVM `gorm:"ForeignKey:BedId" json:"device_number"`
  2435. SchedualPatient *MSchedualPatientVMList `gorm:"ForeignKey:PatientId" json:"patient"`
  2436. AutomaticReduceDetail []*AutomaticReduceDetail `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"good_user"`
  2437. LastAutomaticReduceDetail []*AutomaticReduceDetail `gorm:"-" json:"last_good_user"`
  2438. DialysisBeforePrepare []*DialysisBeforePrepare `gorm:"ForeignKey:PatientId,ScheduleDate;AssociationForeignKey:PatientId,RecordDate" json:"good_user_detail"`
  2439. LastDialysisBeforePrepare []*DialysisBeforePrepare `gorm:"-" json:"last_good_user_detail"`
  2440. Project []*models.HisPrescriptionProject `gorm:"-" json:"project"`
  2441. }
  2442. func (MDialysisGoodsVM) TableName() string {
  2443. return "xt_schedule"
  2444. }
  2445. type VMWarehouseOutInfo struct {
  2446. ID int64 `gorm:"column:id" json:"id"`
  2447. WarehouseOutId int64 `gorm:"column:warehouse_out_id" json:"warehouse_out_id"`
  2448. GoodId int64 `gorm:"column:good_id" json:"good_id"`
  2449. GoodTypeId int64 `gorm:"column:good_type_id" json:"good_type_id"`
  2450. Count int64 `gorm:"column:count" json:"count"`
  2451. Price float64 `gorm:"column:price" json:"price"`
  2452. TotalPrice float64 `gorm:"column:total_price" json:"total_price"`
  2453. Status int64 `gorm:"column:status" json:"status"`
  2454. OrgId int64 `gorm:"column:org_id" json:"org_id"`
  2455. WarehouseOutOrderNumber string `gorm:"column:warehouse_out_order_number" json:"warehouse_out_order_number"`
  2456. GoodInfo VMGoodInfo `gorm:"ForeignKey:GoodId;AssociationForeignKey:ID" json:"good_info"`
  2457. GoodsType GoodsType `gorm:"ForeignKey:GoodTypeId;AssociationForeignKey:ID" json:"good_type"`
  2458. IsSys int64 `gorm:"column:is_sys" json:"is_sys"`
  2459. SysRecordTime int64 `gorm:"column:sys_record_time" json:"sys_record_time"`
  2460. Mtime int64 `gorm:"column:mtime" json:"mtime"`
  2461. Ctime int64 `gorm:"column:ctime" json:"ctime"`
  2462. }
  2463. func (VMWarehouseOutInfo) TableName() string {
  2464. return "xt_warehouse_out_info"
  2465. }
  2466. type DialysisGoodsDate struct {
  2467. RecordDate int64
  2468. }
  2469. type DialysisGoodsDetailDate struct {
  2470. RecordTime int64
  2471. }
  2472. func MobileGetDialysisGoods(orgID int64, scheduleDate int64, schedule_type int64, partition_id int64, page int, limit int, patient_id int64, keywords string, end_time int64) ([]*MDialysisGoodsVM, error, int64) {
  2473. var vms []*MDialysisGoodsVM
  2474. var total int64
  2475. db := readDb.
  2476. Table("xt_schedule as sch").
  2477. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  2478. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  2479. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  2480. Preload("DialysisBeforePrepare", func(db *gorm.DB) *gorm.DB {
  2481. return db.Preload("VMGoodInfo", "status = 1 AND org_id = ? and find_in_set('停用',good_status) = 0", orgID).Preload("GoodsType", "status = 1 AND (org_id = ? OR org_id = 0) ", orgID).Where("status = 1 AND user_org_id = ? AND record_date = ? AND count > 0 ", orgID, scheduleDate)
  2482. }).
  2483. Preload("AutomaticReduceDetail", func(db *gorm.DB) *gorm.DB {
  2484. return db.Preload("VMGoodInfo", "status = 1 AND org_id = ? and find_in_set('停用',good_status) = 0", orgID).Preload("GoodsType", "status = 1 AND (org_id = ? OR org_id = 0) ", orgID).Where("status = 1 AND org_id = ? AND count > 0 AND record_time >= ? AND record_time <= ? ", orgID, scheduleDate, end_time).Group("patient_id,good_id")
  2485. }).Where("sch.status = 1 AND sch.user_org_id = ?", orgID)
  2486. if scheduleDate != 0 {
  2487. db = db.Where("schedule_date = ?", scheduleDate)
  2488. }
  2489. if schedule_type != 0 {
  2490. db = db.Where("schedule_type = ?", schedule_type)
  2491. }
  2492. if partition_id != 0 {
  2493. db = db.Where("partition_id = ?", partition_id)
  2494. }
  2495. if patient_id != 0 {
  2496. db = db.Where("patient_id = ?", patient_id)
  2497. }
  2498. //db = db.Count(&total)
  2499. //offset := (page - 1) * limit
  2500. //
  2501. //db = db.Offset(offset).Limit(limit)
  2502. err := db.Find(&vms).Error
  2503. return vms, err, total
  2504. }
  2505. func GetLastDialysisGoods(patient_id int64, orgID int64, record_time int64) (goodUser []*AutomaticReduceDetail, err error) {
  2506. var Id []*DialysisGoodsDetailDate
  2507. err = readDb.Model(&AutomaticReduceDetail{}).Where("patient_id = ? AND org_id = ? AND status=1 AND record_time < ? AND count > 0", patient_id, orgID, record_time).Select("record_time").Group("record_time").Order("record_time asc").Scan(&Id).Error
  2508. if len(Id) > 0 {
  2509. err = readDb.Model(&AutomaticReduceDetail{}).Where("patient_id = ? AND org_id = ? AND status=1 AND record_time = ? AND count > 0", patient_id, orgID, Id[len(Id)-1].RecordTime).Preload("VMGoodInfo", "status = 1 AND org_id = ? and find_in_set('停用',good_status) = 0", orgID).Preload("GoodsType", "status = 1 AND (org_id = ? OR org_id = 0) ", orgID).Find(&goodUser).Error
  2510. }
  2511. return
  2512. }
  2513. func GetLastDialysisBeforePrepare(patient_id int64, orgID int64, record_time int64) (goodUser []*DialysisBeforePrepare, err error) {
  2514. var Id []*DialysisGoodsDate
  2515. err = readDb.Model(&models.DialysisBeforePrepare{}).Where("patient_id = ? AND user_org_id = ? AND status=1 AND record_date < ? AND count > 0", patient_id, orgID, record_time).Select("record_date").Group("record_date").Order("record_date asc").Scan(&Id).Error
  2516. if len(Id) > 0 {
  2517. err = readDb.Model(&models.DialysisBeforePrepare{}).Where("patient_id = ? AND user_org_id = ? AND status=1 AND record_date = ? AND count > 0", patient_id, orgID, Id[len(Id)-1].RecordDate).Preload("VMGoodInfo", "status = 1 AND org_id = ? and find_in_set('停用',good_status) = 0", orgID).Preload("GoodsType", "status = 1 AND (org_id = ? OR org_id = 0) ", orgID).Find(&goodUser).Error
  2518. }
  2519. return
  2520. }
  2521. func MobileGetGoodsStatistics(orgID int64, start_time int64, end_time int64) (list []*models.StockInfo, err error) {
  2522. db := readDb.Model(&models.StockInfo{})
  2523. db = db.Where("xt_good_information.org_id = ? AND xt_good_information.status = 1", orgID)
  2524. db = db.Joins("JOIN xt_warehouse_out_info AS info ON info.good_id=xt_good_information.id AND info.status = 1 AND info.org_id = ?", orgID).Group("xt_good_information.id")
  2525. db = db.Preload("QueryWarehouseOutInfo", func(db *gorm.DB) *gorm.DB {
  2526. return db.Where("xt_warehouse_out_info.org_id = ? AND xt_warehouse_out_info.status = 1 AND xt_warehouse_out_info.sys_record_time >= ? AND xt_warehouse_out_info.sys_record_time <= ?", orgID, start_time, end_time)
  2527. })
  2528. db = db.Preload("GoodsType", "org_id = ? AND status = 1", orgID)
  2529. err = db.Order("ctime desc").Find(&list).Error
  2530. return
  2531. }
  2532. func PCGetDialysisGoods(orgID int64, scheduleDate int64, schedule_type int64, partition_id int64, page int, limit int, patient_id int64, keywords string, end_time int64, good_type int64, ids []int64) ([]*MDialysisGoodsVM, error, int64) {
  2533. var vms []*MDialysisGoodsVM
  2534. var total int64
  2535. db := readDb.
  2536. Model(&models.Schedule{}).
  2537. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  2538. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  2539. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  2540. Preload("DialysisBeforePrepare", func(db *gorm.DB) *gorm.DB {
  2541. return db.Preload("VMGoodInfo", "status = 1 AND org_id = ? ", orgID).Preload("GoodsType", "status = 1 AND (org_id = ? OR org_id = 0) ", orgID).Where("status = 1 AND user_org_id = ? AND record_date = ? AND count > 0 ", orgID, scheduleDate)
  2542. }).Preload("AutomaticReduceDetail", func(db *gorm.DB) *gorm.DB {
  2543. return db.Preload("VMGoodInfo", "status = 1 AND org_id = ? ", orgID).Preload("GoodsType", "status = 1 AND (org_id = ? OR org_id = 0) ", orgID).Where("status = 1 AND org_id = ? AND record_time >= ? AND record_time <=? AND count > 0", orgID, scheduleDate, end_time)
  2544. }).Where("xt_schedule.status = 1 AND xt_schedule.user_org_id = ?", orgID)
  2545. if scheduleDate != 0 {
  2546. db = db.Where("schedule_date = ?", scheduleDate)
  2547. }
  2548. if patient_id != 0 {
  2549. db = db.Where("patient_id = ?", patient_id)
  2550. }
  2551. if len(keywords) != 0 {
  2552. keywords = "%" + keywords + "%"
  2553. db = db.Joins("JOIN xt_patients AS patient ON patient.id=xt_schedule.patient_id AND patient.status = 1 AND patient.user_org_id = ? AND patient.name Like ?", orgID, keywords)
  2554. if schedule_type != 0 {
  2555. db = db.Where("schedule_type = ?", schedule_type)
  2556. }
  2557. if partition_id != 0 {
  2558. db = db.Where("partition_id = ?", partition_id)
  2559. }
  2560. if good_type == 1 {
  2561. db = db.Where("patient_id in(?)", ids)
  2562. }
  2563. if good_type == 2 {
  2564. if len(ids) > 0 {
  2565. db = db.Where("patient_id not in(?)", ids)
  2566. }
  2567. }
  2568. } else {
  2569. if schedule_type != 0 {
  2570. db = db.Where("schedule_type = ?", schedule_type)
  2571. }
  2572. if partition_id != 0 {
  2573. db = db.Where("partition_id = ?", partition_id)
  2574. }
  2575. if good_type == 1 {
  2576. db = db.Where("patient_id in(?)", ids)
  2577. }
  2578. if good_type == 2 {
  2579. if len(ids) > 0 {
  2580. db = db.Where("patient_id not in(?)", ids)
  2581. }
  2582. }
  2583. db = db.Count(&total)
  2584. offset := (page - 1) * limit
  2585. db = db.Offset(offset).Limit(limit)
  2586. }
  2587. err := db.Find(&vms).Error
  2588. return vms, err, total
  2589. }
  2590. func GetLastDialysisGoodsTwo(patient_id int64, orgID int64, record_time int64) (goodUser []*AutomaticReduceDetail, err error) {
  2591. var Id []*DialysisGoodsDetailDate
  2592. err = readDb.Model(&AutomaticReduceDetail{}).Where("patient_id = ? AND org_id = ? AND status=1 AND record_time < ? AND count > 0", patient_id, orgID, record_time).Select("record_time").Group("record_time").Order("record_time asc").Scan(&Id).Error
  2593. if len(Id) > 0 {
  2594. err = readDb.Model(&AutomaticReduceDetail{}).Where("patient_id = ? AND org_id = ? AND status=1 AND record_time = ? AND count > 0", patient_id, orgID, Id[len(Id)-1].RecordTime).Preload("VMGoodInfo", "status = 1 AND org_id = ? ", orgID).Preload("GoodsType", "status = 1 AND (org_id = ? OR org_id = 0) ", orgID).Find(&goodUser).Error
  2595. }
  2596. return
  2597. }
  2598. func GetLastDialysisBeforePrepareTwo(patient_id int64, orgID int64, record_time int64) (goodUser []*DialysisBeforePrepare, err error) {
  2599. var Id []*DialysisGoodsDate
  2600. err = readDb.Model(&models.DialysisBeforePrepare{}).Where("patient_id = ? AND user_org_id = ? AND status=1 AND record_date < ? AND count > 0", patient_id, orgID, record_time).Select("record_date").Group("record_date").Order("record_date asc").Scan(&Id).Error
  2601. if len(Id) > 0 {
  2602. err = readDb.Model(&models.DialysisBeforePrepare{}).Where("patient_id = ? AND user_org_id = ? AND status=1 AND record_date = ? AND count > 0", patient_id, orgID, Id[len(Id)-1].RecordDate).Preload("VMGoodInfo", "status = 1 AND org_id = ? ", orgID).Preload("GoodsType", "status = 1 AND (org_id = ? OR org_id = 0) ", orgID).Find(&goodUser).Error
  2603. }
  2604. return
  2605. }
  2606. func GetAssessmentBefor(orgid int64, patientid int64, recorddate int64) (*models.PredialysisEvaluation, error) {
  2607. evaluation := models.PredialysisEvaluation{}
  2608. err := XTReadDB().Model(&evaluation).Where("user_org_id = ? and patient_id = ? and assessment_date = ? and status = 1", orgid, patientid, recorddate).Find(&evaluation).Error
  2609. if err == gorm.ErrRecordNotFound {
  2610. return nil, err
  2611. }
  2612. if err != nil {
  2613. return nil, err
  2614. }
  2615. return &evaluation, nil
  2616. }
  2617. func GetAllHisDoctorAdvice(orgid int64, patientid int64, recorddate int64) (his []*models.HisDoctorAdviceInfo, err error) {
  2618. redis := RedisClient()
  2619. defer redis.Close()
  2620. key := strconv.FormatInt(orgid, 10) + ":" + strconv.FormatInt(patientid, 10) + ":" + strconv.FormatInt(recorddate, 10) + ":his_doctor_advice"
  2621. his_doctor_advice_str, _ := redis.Get(key).Result()
  2622. redis.Set(key, "", time.Second)
  2623. if len(his_doctor_advice_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2624. err = readDb.Model(&models.HisDoctorAdviceInfo{}).Where("patient_id = ? AND user_org_id = ? AND status=1 AND record_date = ?", patientid, orgid, recorddate).Find(&his).Error
  2625. if err != nil {
  2626. if err == gorm.ErrRecordNotFound {
  2627. if len(his) <= 0 {
  2628. redis.Set(key, "null", time.Second*60*60*18)
  2629. }
  2630. return his, nil
  2631. } else {
  2632. return his, err
  2633. }
  2634. } else {
  2635. if len(his) > 0 {
  2636. //缓存数据
  2637. his_doctor_advice_str, err := json.Marshal(his)
  2638. if err == nil {
  2639. redis.Set(key, his_doctor_advice_str, time.Second*60*60*18)
  2640. }
  2641. } else {
  2642. redis.Set(key, "null", time.Second*60*60*18)
  2643. }
  2644. return his, nil
  2645. }
  2646. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2647. json.Unmarshal([]byte(his_doctor_advice_str), &his)
  2648. return his, nil
  2649. }
  2650. }
  2651. func GetLastDialysisPrescriptionByPatientId(orgid int64, patientid int64, recorddate int64) (*models.DialysisPrescription, error) {
  2652. prescription := models.DialysisPrescription{}
  2653. err := readDb.Model(&models.DialysisPrescription{}).Where("user_org_id = ? and patient_id = ? and record_date = ? and status = 1", orgid, patientid, recorddate).Find(&prescription).Error
  2654. if err == gorm.ErrRecordNotFound {
  2655. return nil, err
  2656. }
  2657. if err != nil {
  2658. return nil, err
  2659. }
  2660. return &prescription, nil
  2661. }
  2662. func GetLastDialysisPrescriptionByPatientIdTwo(orgid int64, patientid int64, recorddate int64) (models.DialysisPrescription, error) {
  2663. prescription := models.DialysisPrescription{}
  2664. err := readDb.Model(&models.DialysisPrescription{}).Where("user_org_id = ? and patient_id = ? and record_date = ? and status = 1", orgid, patientid, recorddate).Find(&prescription).Error
  2665. return prescription, err
  2666. }
  2667. type HisMScheduleDoctorAdviceVM struct {
  2668. ID int64 `gorm:"column:id" json:"id"`
  2669. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id"`
  2670. PartitionId int64 `gorm:"column:partition_id" json:"partition_id"`
  2671. BedId int64 `gorm:"column:bed_id" json:"bed_id"`
  2672. PatientId int64 `gorm:"column:patient_id" json:"patient_id"`
  2673. ScheduleDate int64 `gorm:"column:schedule_date" json:"schedule_date"`
  2674. ScheduleType int64 `gorm:"column:schedule_type" json:"schedule_type"`
  2675. ModeId int64 `gorm:"column:mode_id" json:"mode_id"`
  2676. Status int64 `gorm:"column:status" json:"status"`
  2677. DialysisOrder *MDialysisOrderVM `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"dialysis_order"`
  2678. SchedualPatient *MSchedualPatientVM `gorm:"ForeignKey:PatientId;AssociationForeignKey:ID" json:"patient"`
  2679. DeviceNumber *MDeviceNumberVM `gorm:"ForeignKey:BedId" json:"device_number"`
  2680. Prescription *models.DialysisPrescriptionList `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"prescription"`
  2681. HisDoctorAdviceInfo []*models.HisDoctorAdviceInfo `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"doctor_advice"`
  2682. HisPrescriptionProject []*models.HisPrescriptionProject `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"project"`
  2683. }
  2684. func (HisMScheduleDoctorAdviceVM) TableName() string {
  2685. return "xt_schedule"
  2686. }
  2687. type HisMScheduleDoctorAdviceVMOne struct {
  2688. ID int64 `gorm:"column:id" json:"id"`
  2689. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id"`
  2690. PartitionId int64 `gorm:"column:partition_id" json:"partition_id"`
  2691. BedId int64 `gorm:"column:bed_id" json:"bed_id"`
  2692. PatientId int64 `gorm:"column:patient_id" json:"patient_id"`
  2693. ScheduleDate int64 `gorm:"column:schedule_date" json:"schedule_date"`
  2694. ScheduleType int64 `gorm:"column:schedule_type" json:"schedule_type"`
  2695. ModeId int64 `gorm:"column:mode_id" json:"mode_id"`
  2696. Status int64 `gorm:"column:status" json:"status"`
  2697. DialysisOrder *MDialysisOrderVM `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"dialysis_order"`
  2698. SchedualPatient *MSchedualPatientVM `gorm:"ForeignKey:PatientId;AssociationForeignKey:ID" json:"patient"`
  2699. DeviceNumber *MDeviceNumberVM `gorm:"ForeignKey:BedId" json:"device_number"`
  2700. Prescription *models.DialysisPrescriptionList `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"prescription"`
  2701. HisDoctorAdviceInfo []*models.HisDoctorAdviceInfo `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"doctor_advice"`
  2702. HisPrescriptionProject []*models.HisPrescriptionProject `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"project"`
  2703. }
  2704. func (HisMScheduleDoctorAdviceVMOne) TableName() string {
  2705. return "xt_schedule"
  2706. }
  2707. type HisMScheduleProjectVM struct {
  2708. ID int64 `gorm:"column:id" json:"id"`
  2709. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id"`
  2710. PartitionId int64 `gorm:"column:partition_id" json:"partition_id"`
  2711. BedId int64 `gorm:"column:bed_id" json:"bed_id"`
  2712. PatientId int64 `gorm:"column:patient_id" json:"patient_id"`
  2713. ScheduleDate int64 `gorm:"column:schedule_date" json:"schedule_date"`
  2714. ScheduleType int64 `gorm:"column:schedule_type" json:"schedule_type"`
  2715. ModeId int64 `gorm:"column:mode_id" json:"mode_id"`
  2716. Status int64 `gorm:"column:status" json:"status"`
  2717. DialysisOrder *MDialysisOrderVM `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"dialysis_order"`
  2718. SchedualPatient *MSchedualPatientVM `gorm:"ForeignKey:PatientId;AssociationForeignKey:ID" json:"patient"`
  2719. DeviceNumber *MDeviceNumberVM `gorm:"ForeignKey:BedId" json:"device_number"`
  2720. Prescription *models.DialysisPrescriptionList `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"prescription"`
  2721. HisPrescriptionProject []*models.HisPrescriptionProject `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"project"`
  2722. HisPrescriptionTeamProject []*models.HisPrescriptionProject `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"team_project"`
  2723. }
  2724. func (HisMScheduleProjectVM) TableName() string {
  2725. return "xt_schedule"
  2726. }
  2727. func GetHisDoctorConfig(orgid int64) (models.XtHisConfig, error) {
  2728. config := models.XtHisConfig{}
  2729. err := XTReadDB().Model(&config).Where("user_org_id = ? and status =1", orgid).Find(&config).Error
  2730. return config, err
  2731. }
  2732. func GetHisDoctorPatientById(orgid int64, patientid int64, recordtime int64) (advice []*models.HisDoctorAdviceInfo, err error) {
  2733. db := XTReadDB().Table("his_doctor_advice_info as x").Where("x.status = 1")
  2734. if orgid > 0 {
  2735. db = db.Where("x.user_org_id = ?", orgid)
  2736. }
  2737. if patientid > 0 {
  2738. db = db.Where("x.patient_id =?", patientid)
  2739. }
  2740. if recordtime > 0 {
  2741. db = db.Where("x.advice_date = ?", recordtime)
  2742. }
  2743. err = db.Select("x.id,x.user_org_id,x.patient_id,x.his_patient_id,x.advice_type,x.advice_date,x.start_time,x.advice_name,x.advice_desc,x.reminder_date,x.single_dose,x.single_dose_unit,x.single_dose_unit,x.prescribing_number_unit,x.prescribing_number,x.delivery_way,x.execution_frequency,x.advice_doctor,x.status,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.checker,x.record_date,x.dialysis_order_id,x.check_time,x.check_state,x.drug_spec,x.drug_spec_unit,x.groupno,x.remind_type,x.frequency_type,x.day_count,x.week_day,x.template_id,x.modifier,x.drug_id,x.price,x.prescription_id,x.med_list_codg,x.feedetl_sn,x.day").Find(&advice).Error
  2744. return advice, err
  2745. }
  2746. func GetDoctorAdviceCount(startime int64, endtime int64, deliveway string, orgid int64) (advice []*models.BloodDoctorAdvice, err error) {
  2747. db := XTReadDB().Table("xt_doctor_advice as x").Where("x.status = 1 and x.advice_type =2")
  2748. if startime > 0 {
  2749. db = db.Where("x.advice_date >= ?", startime)
  2750. }
  2751. if endtime > 0 {
  2752. db = db.Where("x.advice_date<=?", endtime)
  2753. }
  2754. if orgid > 0 {
  2755. db = db.Where("x.user_org_id = ?", orgid)
  2756. }
  2757. if len(deliveway) > 0 {
  2758. db = db.Where("x.delivery_way = ?", deliveway)
  2759. }
  2760. err = db.Select("x.advice_name,x.advice_desc,x.delivery_way,count(x.id) as total").Group("x.advice_name,x.advice_desc").Scan(&advice).Error
  2761. return advice, err
  2762. }
  2763. func GetHisDoctorAdviceCount(startime int64, endtime int64, deliveway string, orgid int64) (advice []*models.BloodDoctorAdvice, err error) {
  2764. db := XTReadDB().Table("his_doctor_advice_info as x").Where("x.status = 1 and x.advice_type =2")
  2765. if startime > 0 {
  2766. db = db.Where("x.advice_date >= ?", startime)
  2767. }
  2768. if endtime > 0 {
  2769. db = db.Where("x.advice_date<=?", endtime)
  2770. }
  2771. if orgid > 0 {
  2772. db = db.Where("x.user_org_id = ?", orgid)
  2773. }
  2774. if len(deliveway) > 0 {
  2775. db = db.Where("x.delivery_way = ?", deliveway)
  2776. }
  2777. err = db.Select("x.advice_name,x.advice_desc,x.delivery_way,count(x.id) as total").Group("x.advice_name,x.advice_desc").Scan(&advice).Error
  2778. return advice, err
  2779. }
  2780. func MobileGetScheduleDoctorAdvicesOne(orgID int64, scheduleDate int64, adviceType int, patientType int, adminUserId int64, deliverWay string, scheduleType int64, partitonType int64, patient_id int64, execution_state int64) ([]*MScheduleDoctorAdviceVM, error) {
  2781. var vms []*MScheduleDoctorAdviceVM
  2782. adviceWhere := ""
  2783. adviceCondition := []interface{}{}
  2784. if adviceType == 0 {
  2785. if patientType == 0 {
  2786. if patient_id > 0 {
  2787. if execution_state > 0 {
  2788. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND (advice_type = 3 OR advice_type = 1) and patient_id = ? and execution_state= ?"
  2789. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, patient_id, execution_state)
  2790. } else {
  2791. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND (advice_type = 3 OR advice_type = 1) and patient_id = ?"
  2792. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, patient_id)
  2793. }
  2794. } else {
  2795. if execution_state > 0 {
  2796. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND (advice_type = 3 OR advice_type = 1) and execution_state= ?"
  2797. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, execution_state)
  2798. } else {
  2799. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND (advice_type = 3 OR advice_type = 1)"
  2800. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  2801. }
  2802. }
  2803. } else if patientType == 1 {
  2804. if patient_id > 0 {
  2805. if execution_state > 0 {
  2806. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_doctor = ? AND(advice_type = 3 OR advice_type = 1) and patient_id = ? and execution_state= ?"
  2807. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId, patient_id, execution_state)
  2808. } else {
  2809. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_doctor = ? AND(advice_type = 3 OR advice_type = 1) and patient_id = ?"
  2810. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId, patient_id)
  2811. }
  2812. } else {
  2813. if execution_state > 0 {
  2814. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_doctor = ? AND(advice_type = 3 OR advice_type = 1) and execution_state = ?"
  2815. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId, execution_state)
  2816. } else {
  2817. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_doctor = ? AND(advice_type = 3 OR advice_type = 1)"
  2818. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId)
  2819. }
  2820. }
  2821. } else if patientType == 2 {
  2822. if patient_id > 0 {
  2823. if execution_state > 0 {
  2824. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND execution_staff = 0 AND(advice_type = 3 OR advice_type = 1 and patient_id = ?) and execution_state = ?"
  2825. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, patient_id, execution_state)
  2826. } else {
  2827. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND execution_staff = 0 AND(advice_type = 3 OR advice_type = 1 and patient_id = ?)"
  2828. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, patient_id)
  2829. }
  2830. } else {
  2831. if execution_state > 0 {
  2832. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND execution_staff = 0 AND(advice_type = 3 OR advice_type = 1) and execution_state = ?"
  2833. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, execution_state)
  2834. } else {
  2835. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND execution_staff = 0 AND(advice_type = 3 OR advice_type = 1)"
  2836. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  2837. }
  2838. }
  2839. }
  2840. } else if adviceType == 1 {
  2841. if patientType == 0 {
  2842. if patient_id > 0 {
  2843. if execution_state > 0 {
  2844. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 1 AND record_date = ? and patient_id = ? and execution_state = ?"
  2845. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, patient_id, execution_state)
  2846. } else {
  2847. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 1 AND record_date = ? and patient_id = ? "
  2848. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, patient_id)
  2849. }
  2850. } else {
  2851. if execution_state > 0 {
  2852. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 1 AND record_date = ? and execution_state = ?"
  2853. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, execution_state)
  2854. } else {
  2855. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 1 AND record_date = ? "
  2856. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  2857. }
  2858. }
  2859. } else if patientType == 1 {
  2860. if patient_id > 0 {
  2861. if execution_state > 0 {
  2862. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 1 AND record_date = ? AND advice_doctor = ? and patient_id = ? and execution_state = ?"
  2863. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId, patient_id, execution_state)
  2864. } else {
  2865. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 1 AND record_date = ? AND advice_doctor = ? and patient_id = ?"
  2866. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId, patient_id)
  2867. }
  2868. } else {
  2869. if execution_state > 0 {
  2870. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 1 AND record_date = ? AND advice_doctor = ? and execution_state=?"
  2871. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId, execution_state)
  2872. } else {
  2873. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 1 AND record_date = ? AND advice_doctor = ? "
  2874. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId)
  2875. }
  2876. }
  2877. } else if patientType == 2 {
  2878. if patient_id > 0 {
  2879. if execution_state > 0 {
  2880. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 1 AND record_date = ? AND execution_staff = 0 and patient_id = ? and execution_state = ?"
  2881. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, patient_id, execution_state)
  2882. } else {
  2883. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 1 AND record_date = ? AND execution_staff = 0 and patient_id = ?"
  2884. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, patient_id)
  2885. }
  2886. } else {
  2887. if execution_state > 0 {
  2888. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 1 AND record_date = ? AND execution_staff = 0 and execution_state = ?"
  2889. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, execution_state)
  2890. } else {
  2891. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 1 AND record_date = ? AND execution_staff = 0"
  2892. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  2893. }
  2894. }
  2895. }
  2896. } else if adviceType == 3 {
  2897. if patientType == 0 {
  2898. if patient_id > 0 {
  2899. if execution_state > 0 {
  2900. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 3 AND record_date = ? and patient_id = ? and execution_state = ?"
  2901. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, patient_id, execution_state)
  2902. } else {
  2903. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 3 AND record_date = ? and patient_id = ? "
  2904. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, patient_id)
  2905. }
  2906. } else {
  2907. if execution_state > 0 {
  2908. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 3 AND record_date = ? and execution_state =? "
  2909. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, execution_state)
  2910. } else {
  2911. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 3 AND record_date = ? "
  2912. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  2913. }
  2914. }
  2915. } else if patientType == 1 {
  2916. if patient_id > 0 {
  2917. if execution_state > 0 {
  2918. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 3 AND advice_doctor = ? and patient_id = ? and execution_state = ?"
  2919. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId, patient_id, execution_state)
  2920. } else {
  2921. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 3 AND advice_doctor = ? and patient_id = ?"
  2922. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId, patient_id)
  2923. }
  2924. } else {
  2925. if execution_state > 0 {
  2926. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 3 AND advice_doctor = ? and execution_state =? "
  2927. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId, execution_state)
  2928. } else {
  2929. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 3 AND advice_doctor = ? "
  2930. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId)
  2931. }
  2932. }
  2933. } else if patientType == 2 {
  2934. if patient_id > 0 {
  2935. if execution_state > 0 {
  2936. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 3 AND execution_staff = 0 and patient_id = ? and execution_state = ?"
  2937. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, patient_id, execution_state)
  2938. } else {
  2939. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 3 AND execution_staff = 0 and patient_id = ?"
  2940. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, patient_id)
  2941. }
  2942. } else {
  2943. if execution_state > 0 {
  2944. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 3 AND execution_staff = 0 and execution_state= ?"
  2945. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, execution_state)
  2946. } else {
  2947. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 3 AND execution_staff = 0"
  2948. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  2949. }
  2950. }
  2951. }
  2952. } else if adviceType == 2 && len(deliverWay) > 0 {
  2953. if patientType == 0 {
  2954. if patient_id > 0 {
  2955. if execution_state > 0 {
  2956. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 2 AND record_date = ? and delivery_way = ? and patient_id = ? and execution_state = ?"
  2957. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, deliverWay, patient_id, execution_state)
  2958. } else {
  2959. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 2 AND record_date = ? and delivery_way = ? and patient_id = ?"
  2960. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, deliverWay, patient_id)
  2961. }
  2962. } else {
  2963. if execution_state > 0 {
  2964. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 2 AND record_date = ? and delivery_way = ? and execution_state = ?"
  2965. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, deliverWay, execution_state)
  2966. } else {
  2967. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 2 AND record_date = ? and delivery_way = ?"
  2968. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, deliverWay)
  2969. }
  2970. }
  2971. } else if patientType == 1 {
  2972. if patient_id > 0 {
  2973. if execution_state > 0 {
  2974. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 2 AND advice_doctor = ? and delivery_way = ? and patient_id = ? and execution_state = ?"
  2975. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId, deliverWay, patient_id, execution_state)
  2976. } else {
  2977. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 2 AND advice_doctor = ? and delivery_way = ? and patient_id = ?"
  2978. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId, deliverWay, patient_id)
  2979. }
  2980. } else {
  2981. if execution_state > 0 {
  2982. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 2 AND advice_doctor = ? and delivery_way = ? and execution_state = ?"
  2983. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId, deliverWay, execution_state)
  2984. } else {
  2985. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 2 AND advice_doctor = ? and delivery_way = ? "
  2986. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId, deliverWay)
  2987. }
  2988. }
  2989. } else if patientType == 2 {
  2990. if patient_id > 0 {
  2991. if execution_state > 0 {
  2992. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 2 AND execution_staff = 0 and delivery_way = ? and patient_id = ? and execution_state = ?"
  2993. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, deliverWay, patient_id, execution_state)
  2994. } else {
  2995. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 2 AND execution_staff = 0 and delivery_way = ? and patient_id = ?"
  2996. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, deliverWay, patient_id)
  2997. }
  2998. } else {
  2999. if execution_state > 0 {
  3000. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 2 AND execution_staff = 0 and delivery_way = ? and execution_state = ?"
  3001. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, deliverWay, execution_state)
  3002. } else {
  3003. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 2 AND execution_staff = 0 and delivery_way = ?"
  3004. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, deliverWay)
  3005. }
  3006. }
  3007. }
  3008. } else if adviceType == 2 && len(deliverWay) <= 0 {
  3009. if patientType == 0 {
  3010. if patient_id > 0 {
  3011. if execution_state > 0 {
  3012. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 2 AND record_date = ? and patient_id = ? and execution_state = ?"
  3013. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, patient_id, execution_state)
  3014. } else {
  3015. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 2 AND record_date = ? and patient_id = ?"
  3016. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, patient_id)
  3017. }
  3018. } else {
  3019. if execution_state > 0 {
  3020. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 2 AND record_date = ? and execution_state = ?"
  3021. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, execution_state)
  3022. } else {
  3023. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 2 AND record_date = ?"
  3024. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  3025. }
  3026. }
  3027. } else if patientType == 1 {
  3028. if patient_id > 0 {
  3029. if execution_state > 0 {
  3030. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 2 AND advice_doctor = ? and patient_id = ? and execution_state = ?"
  3031. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId, patient_id, execution_state)
  3032. } else {
  3033. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 2 AND advice_doctor = ? and patient_id = ?"
  3034. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId, patient_id)
  3035. }
  3036. } else {
  3037. if execution_state > 0 {
  3038. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 2 AND advice_doctor = ? and execution_state = ?"
  3039. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId, execution_state)
  3040. } else {
  3041. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 2 AND advice_doctor = ?"
  3042. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId)
  3043. }
  3044. }
  3045. } else if patientType == 2 {
  3046. if patient_id > 0 {
  3047. if execution_state > 0 {
  3048. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 2 AND execution_staff = 0 and patient_id = ? and execution_state = ?"
  3049. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, patient_id, execution_state)
  3050. } else {
  3051. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 2 AND execution_staff = 0 and patient_id = ?"
  3052. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, patient_id)
  3053. }
  3054. } else {
  3055. if execution_state > 0 {
  3056. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 2 AND execution_staff = 0 and execution_state = ?"
  3057. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, execution_state)
  3058. } else {
  3059. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 2 AND execution_staff = 0"
  3060. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  3061. }
  3062. }
  3063. }
  3064. }
  3065. db := readDb.Table("xt_schedule")
  3066. if scheduleType > 0 {
  3067. db = db.Where("schedule_type = ?", scheduleType)
  3068. }
  3069. if partitonType > 0 {
  3070. db = db.Where("partition_id = ?", partitonType)
  3071. }
  3072. db = db.Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  3073. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  3074. return db.Where("status = 1 AND user_org_id = ?", orgID).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  3075. }).
  3076. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  3077. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  3078. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  3079. Preload("DialysisAssesmentBefor", "status =1 AND user_org_id = ? and assessment_date =?", orgID, scheduleDate).
  3080. Preload("DoctorAdvices", adviceCondition...).
  3081. Where("status = 1 AND user_org_id = ?", orgID)
  3082. if scheduleDate != 0 {
  3083. db = db.Where("schedule_date = ?", scheduleDate)
  3084. }
  3085. err := db.Find(&vms).Error
  3086. return vms, err
  3087. }
  3088. func GetHisDoctorAdvicesOne(orgID int64, scheduleDate int64, deliverWay string, scheduleType int64, partitionType int64, patient_id int64, execution_state int64) ([]*HisMScheduleDoctorAdviceVM, error) {
  3089. var vms []*HisMScheduleDoctorAdviceVM
  3090. if len(deliverWay) > 0 {
  3091. db := readDb.Table("xt_schedule")
  3092. if scheduleType > 0 {
  3093. db = db.Where("schedule_type = ?", scheduleType)
  3094. }
  3095. if partitionType > 0 {
  3096. db = db.Where("partition_id = ?", partitionType)
  3097. }
  3098. if patient_id > 0 {
  3099. if execution_state > 0 {
  3100. db = db.Preload("SchedualPatient", "status = 1 AND user_org_id = ? and id =?", orgID, patient_id).
  3101. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  3102. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  3103. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ? and patient_id = ?", orgID, scheduleDate, patient_id).
  3104. Preload("HisDoctorAdviceInfo", "status = 1 AND user_org_id = ? AND advice_date = ? and delivery_way = ? and patient_id = ? and execution_state = ?", orgID, scheduleDate, deliverWay, patient_id, execution_state).
  3105. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  3106. return db.Where("status = 1 AND user_org_id = ? and patient_id = ?", orgID, patient_id).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  3107. }).
  3108. Where("status = 1 AND user_org_id = ?", orgID)
  3109. if scheduleDate != 0 {
  3110. db = db.Where("schedule_date = ?", scheduleDate)
  3111. }
  3112. err = db.Find(&vms).Error
  3113. } else {
  3114. db = db.Preload("SchedualPatient", "status = 1 AND user_org_id = ? and id =?", orgID, patient_id).
  3115. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  3116. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  3117. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ? and patient_id = ?", orgID, scheduleDate, patient_id).
  3118. Preload("HisDoctorAdviceInfo", "status = 1 AND user_org_id = ? AND advice_date = ? and delivery_way = ? and patient_id = ?", orgID, scheduleDate, deliverWay, patient_id).
  3119. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  3120. return db.Where("status = 1 AND user_org_id = ? and patient_id = ?", orgID, patient_id).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  3121. }).
  3122. Where("status = 1 AND user_org_id = ?", orgID)
  3123. if scheduleDate != 0 {
  3124. db = db.Where("schedule_date = ?", scheduleDate)
  3125. }
  3126. err = db.Find(&vms).Error
  3127. }
  3128. } else {
  3129. if execution_state > 0 {
  3130. db = db.Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  3131. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  3132. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  3133. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  3134. Preload("HisDoctorAdviceInfo", "status = 1 AND user_org_id = ? AND advice_date = ? and delivery_way = ? and execution_state = ?", orgID, scheduleDate, deliverWay, execution_state).
  3135. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  3136. return db.Where("status = 1 AND user_org_id = ?", orgID).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  3137. }).
  3138. Where("status = 1 AND user_org_id = ?", orgID)
  3139. if scheduleDate != 0 {
  3140. db = db.Where("schedule_date = ?", scheduleDate)
  3141. }
  3142. err = db.Find(&vms).Error
  3143. } else {
  3144. db = db.Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  3145. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  3146. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  3147. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  3148. Preload("HisDoctorAdviceInfo", "status = 1 AND user_org_id = ? AND advice_date = ? and delivery_way = ?", orgID, scheduleDate, deliverWay).
  3149. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  3150. return db.Where("status = 1 AND user_org_id = ?", orgID).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  3151. }).
  3152. Where("status = 1 AND user_org_id = ?", orgID)
  3153. if scheduleDate != 0 {
  3154. db = db.Where("schedule_date = ?", scheduleDate)
  3155. }
  3156. err = db.Find(&vms).Error
  3157. }
  3158. }
  3159. } else {
  3160. db := readDb.Table("xt_schedule")
  3161. if scheduleType > 0 {
  3162. db = db.Where("schedule_type = ?", scheduleType)
  3163. }
  3164. if partitionType > 0 {
  3165. db = db.Where("partition_id = ?", partitionType)
  3166. }
  3167. if patient_id > 0 {
  3168. if execution_state > 0 {
  3169. db = db.Preload("SchedualPatient", "status = 1 AND user_org_id = ? and id = ?", orgID, patient_id).
  3170. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  3171. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  3172. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ? and patient_id = ?", orgID, scheduleDate, patient_id).
  3173. Preload("HisDoctorAdviceInfo", "status = 1 AND user_org_id = ? AND advice_date = ? and patient_id = ? and execution_state = ?", orgID, scheduleDate, patient_id, execution_state).
  3174. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  3175. return db.Where("status = 1 AND user_org_id = ? and patient_id = ?", orgID, patient_id).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  3176. }).
  3177. Where("status = 1 AND user_org_id = ?", orgID)
  3178. if scheduleDate != 0 {
  3179. db = db.Where("schedule_date = ?", scheduleDate)
  3180. }
  3181. err = db.Find(&vms).Error
  3182. } else {
  3183. db = db.Preload("SchedualPatient", "status = 1 AND user_org_id = ? and id = ?", orgID, patient_id).
  3184. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  3185. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  3186. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ? and patient_id = ?", orgID, scheduleDate, patient_id).
  3187. Preload("HisDoctorAdviceInfo", "status = 1 AND user_org_id = ? AND advice_date = ? and patient_id = ?", orgID, scheduleDate, patient_id).
  3188. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  3189. return db.Where("status = 1 AND user_org_id = ? and patient_id = ?", orgID, patient_id).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  3190. }).
  3191. Where("status = 1 AND user_org_id = ?", orgID)
  3192. if scheduleDate != 0 {
  3193. db = db.Where("schedule_date = ?", scheduleDate)
  3194. }
  3195. err = db.Find(&vms).Error
  3196. }
  3197. } else {
  3198. if execution_state > 0 {
  3199. db = db.Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  3200. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  3201. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  3202. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  3203. Preload("HisDoctorAdviceInfo", "status = 1 AND user_org_id = ? AND advice_date = ? and execution_state = ?", orgID, scheduleDate, execution_state).
  3204. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  3205. return db.Where("status = 1 AND user_org_id = ?", orgID).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  3206. }).
  3207. Where("status = 1 AND user_org_id = ?", orgID)
  3208. if scheduleDate != 0 {
  3209. db = db.Where("schedule_date = ?", scheduleDate)
  3210. }
  3211. err = db.Find(&vms).Error
  3212. } else {
  3213. db = db.Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  3214. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  3215. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  3216. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  3217. Preload("HisDoctorAdviceInfo", "status = 1 AND user_org_id = ? AND advice_date = ?", orgID, scheduleDate).
  3218. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  3219. return db.Where("status = 1 AND user_org_id = ?", orgID).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  3220. }).
  3221. Where("status = 1 AND user_org_id = ?", orgID)
  3222. if scheduleDate != 0 {
  3223. db = db.Where("schedule_date = ?", scheduleDate)
  3224. }
  3225. err = db.Find(&vms).Error
  3226. }
  3227. }
  3228. }
  3229. return vms, err
  3230. }
  3231. func BatchDeleteMonitor(ids []string) (err error) {
  3232. if len(ids) == 1 {
  3233. err = XTWriteDB().Model(&models.MonitoringRecord{}).Where("id=?", ids[0]).Updates(map[string]interface{}{"status": 0, "updated_time": time.Now().Unix()}).Error
  3234. } else {
  3235. err = XTWriteDB().Model(models.MonitoringRecord{}).Where("id IN(?)", ids).Updates(map[string]interface{}{"status": 0, "updated_time": time.Now().Unix()}).Error
  3236. }
  3237. return
  3238. }
  3239. func GetPatientDialysisRecordList(patientid int64, startime int64, endtime int64) (order []*models.XtDialysisOrders, err error) {
  3240. db := XTReadDB().Table("xt_dialysis_order as x").Where("x.status = 1")
  3241. //table := XTReadDB().Table("xt_schedule as s")
  3242. //fmt.Println(table)
  3243. if patientid > 0 {
  3244. db = db.Where("x.patient_id = ?", patientid)
  3245. }
  3246. if startime > 0 {
  3247. db = db.Where("x.dialysis_date>=?", startime)
  3248. }
  3249. if endtime > 0 {
  3250. db = db.Where("x.dialysis_date <= ?", endtime)
  3251. }
  3252. err = db.Select("x.id,x.dialysis_date,x.schedual_type,s.mode_id").Joins("left join xt_schedule as s on x.patient_id = s.patient_id and x.dialysis_date = s.schedule_date ").Scan(&order).Error
  3253. return order, err
  3254. }
  3255. func BatchDeleteAdvice(ids []string) (err error) {
  3256. if len(ids) == 1 {
  3257. err = XTWriteDB().Model(&models.DoctorAdvice{}).Where("id=?", ids[0]).Updates(map[string]interface{}{"status": 0, "updated_time": time.Now().Unix()}).Error
  3258. } else {
  3259. err = XTWriteDB().Model(models.DoctorAdvice{}).Where("id IN(?)", ids).Updates(map[string]interface{}{"status": 0, "updated_time": time.Now().Unix()}).Error
  3260. }
  3261. return
  3262. }
  3263. func BatchDeleteHisAdvice(ids []string) (err error) {
  3264. if len(ids) == 1 {
  3265. err = XTWriteDB().Model(&models.HisDoctorAdviceInfo{}).Where("id = ?", ids[0]).Updates(map[string]interface{}{"status": 0, "updated_time": time.Now().Unix()}).Error
  3266. } else {
  3267. err = XTWriteDB().Model(models.HisDoctorAdviceInfo{}).Where("id IN(?)", ids).Updates(map[string]interface{}{"status": 0, "updated_time": time.Now().Unix()}).Error
  3268. }
  3269. return
  3270. }
  3271. func UpdateAutoReduceDetail(good_id int64, count int64, record_time int64, patient_id int64) (models.AutomaticReduceDetail, error) {
  3272. detail := models.AutomaticReduceDetail{}
  3273. err := XTWriteDB().Model(&detail).Where("good_id = ? and status = 1 and record_time = ? and patient_id = ?", good_id, record_time, patient_id).Updates(map[string]interface{}{"count": count}).Error
  3274. return detail, err
  3275. }
  3276. func DeleteAutoReduceDetail(good_id int64, record_time int64, patient_id int64) error {
  3277. detail := models.AutomaticReduceDetail{}
  3278. err := XTWriteDB().Model(&detail).Where("good_id = ? and status = 1 and record_time = ? and patient_id = ?", good_id, record_time, patient_id).Updates(map[string]interface{}{"status": 0}).Error
  3279. return err
  3280. }
  3281. func DeleteDialysisBeforOne(good_id int64, record_time int64, patient_id int64) error {
  3282. detail := models.DialysisBeforePrepare{}
  3283. err := XTWriteDB().Model(&detail).Where("good_id = ? and status = 1 and record_date = ? and patient_id = ?", good_id, record_time, patient_id).Updates(map[string]interface{}{"status": 0}).Error
  3284. return err
  3285. }
  3286. func BatchAdviceCheck(ids []string, creator int64) error {
  3287. advice := models.XtDoctorAdvice{}
  3288. err := XTWriteDB().Model(&advice).Where("id IN(?) and status = 1", ids).Updates(map[string]interface{}{"check_state": 1, "checker": creator, "check_time": time.Now().Unix()}).Error
  3289. return err
  3290. }
  3291. func BatchHisAdviceCheck(ids []string, creator int64) error {
  3292. advice := models.HisDoctorAdviceInfo{}
  3293. err := XTWriteDB().Model(&advice).Where("id IN(?) and status = 1", ids).Updates(map[string]interface{}{"check_state": 1, "checker": creator, "check_time": time.Now().Unix()}).Error
  3294. return err
  3295. }
  3296. func BatchAdviceExecution(ids []string, creator int64, execution_state int64) error {
  3297. advice := models.XtDoctorAdvice{}
  3298. err := XTWriteDB().Model(&advice).Where("id IN(?) and status = 1", ids).Updates(map[string]interface{}{"execution_state": 1, "execution_staff": creator, "execution_time": execution_state}).Error
  3299. return err
  3300. }
  3301. func BatchHisAdviceExecution(ids []string, creator int64, execution_state int64) error {
  3302. advice := models.HisDoctorAdviceInfo{}
  3303. err := XTWriteDB().Model(&advice).Where("id IN(?) and status = 1", ids).Updates(map[string]interface{}{"execution_state": 1, "execution_staff": creator, "execution_time": execution_state}).Error
  3304. return err
  3305. }
  3306. func GetAdviceExecutionById(ids []string) (doctor []*models.XtDoctorAdvice, err error) {
  3307. err = XTReadDB().Model(&doctor).Where("id IN(?) AND status = 1", ids).Find(&doctor).Error
  3308. return doctor, err
  3309. }
  3310. func GetHisAdviceExecutionById(ids []string) (doctor []*models.HisDoctorAdviceInfo, err error) {
  3311. err = XTReadDB().Model(&doctor).Where("id IN(?) AND status = 1", ids).Find(&doctor).Error
  3312. return doctor, err
  3313. }
  3314. func GetHisProjectConfig(orgid int64) (models.XtHisProjectConfig, error) {
  3315. config := models.XtHisProjectConfig{}
  3316. err := XTReadDB().Model(&config).Where("user_org_id = ? and status =1", orgid).Find(&config).Error
  3317. return config, err
  3318. }
  3319. func GetHisPrescriptionProject(org_id int64, patient_id int64, record_date int64) (project []*models.HisPrescriptionProject, err error) {
  3320. err = readDb.Model(&models.HisPrescriptionProject{}).Preload("GoodInfo", func(db *gorm.DB) *gorm.DB {
  3321. return db.Preload("GoodsType", "status = 1").Where("status = 1 and is_warehouse = 1")
  3322. }).Where("user_org_id = ? AND status = 1 AND record_date = ? AND patient_id = ? AND type = 3", org_id, record_date, patient_id).Find(&project).Error
  3323. return
  3324. }
  3325. func GetPCHisPrescriptionProject(orgID int64, scheduleDate int64, deliverWay string, patientType int, adminUserId int64, patient_id int64, execution_state int64) ([]*HisMScheduleProjectVM, error) {
  3326. var vms []*HisMScheduleProjectVM
  3327. fmt.Println("patient_id233232332", patient_id)
  3328. fmt.Println("execution_state2323323322332", execution_state)
  3329. fmt.Println("patientType2323323322332", patientType)
  3330. if patientType == 0 {
  3331. if patient_id > 0 {
  3332. if execution_state == 1 {
  3333. db := readDb.
  3334. Table("xt_schedule").
  3335. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  3336. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  3337. return db.Where("status = 1 AND user_org_id = ? and patient_id = ?", orgID, patient_id).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  3338. }).
  3339. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  3340. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  3341. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ? and patient_id = ?", orgID, scheduleDate, patient_id).
  3342. Preload("HisPrescriptionProject", func(db *gorm.DB) *gorm.DB {
  3343. return db.Where("status = 1 AND user_org_id = ? AND record_date = ? AND team_id = 0 and execution_state = ?", orgID, scheduleDate, execution_state).Preload("HisProject").Preload("GoodInfo", "status=1")
  3344. }).
  3345. Preload("HisPrescriptionTeamProject", func(db *gorm.DB) *gorm.DB {
  3346. return db.Where("status = 1 AND user_org_id = ? AND record_date = ? AND team_id > 0 and execution_state = ?", orgID, scheduleDate, execution_state).Preload("XtHisProjectTeam", "status = 1").Preload("HisProject").Preload("GoodInfo", "status=1")
  3347. }).Where("status = 1 AND user_org_id = ?", orgID)
  3348. if scheduleDate != 0 {
  3349. db = db.Where("schedule_date = ?", scheduleDate)
  3350. }
  3351. err = db.Find(&vms).Error
  3352. } else if execution_state == 2 {
  3353. db := readDb.
  3354. Table("xt_schedule").
  3355. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  3356. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  3357. return db.Where("status = 1 AND user_org_id = ? and patient_id = ?", orgID, patient_id).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  3358. }).
  3359. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  3360. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  3361. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ? and patient_id = ?", orgID, scheduleDate, patient_id).
  3362. Preload("HisPrescriptionProject", func(db *gorm.DB) *gorm.DB {
  3363. return db.Where("status = 1 AND user_org_id = ? AND record_date = ? AND team_id = 0 and execution_state = 0", orgID, scheduleDate).Preload("HisProject").Preload("GoodInfo", "status=1")
  3364. }).
  3365. Preload("HisPrescriptionTeamProject", func(db *gorm.DB) *gorm.DB {
  3366. return db.Where("status = 1 AND user_org_id = ? AND record_date = ? AND team_id > 0 and execution_state = 0", orgID, scheduleDate).Preload("XtHisProjectTeam", "status = 1").Preload("HisProject").Preload("GoodInfo", "status=1")
  3367. }).Where("status = 1 AND user_org_id = ?", orgID)
  3368. if scheduleDate != 0 {
  3369. db = db.Where("schedule_date = ?", scheduleDate)
  3370. }
  3371. err = db.Find(&vms).Error
  3372. } else if execution_state == 0 {
  3373. db := readDb.
  3374. Table("xt_schedule").
  3375. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  3376. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  3377. return db.Where("status = 1 AND user_org_id = ? and patient_id = ?", orgID, patient_id).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  3378. }).
  3379. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  3380. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  3381. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ? and patient_id = ?", orgID, scheduleDate, patient_id).
  3382. Preload("HisPrescriptionProject", func(db *gorm.DB) *gorm.DB {
  3383. return db.Where("status = 1 AND user_org_id = ? AND record_date = ? AND team_id = 0", orgID, scheduleDate).Preload("HisProject").Preload("GoodInfo", "status=1")
  3384. }).
  3385. Preload("HisPrescriptionTeamProject", func(db *gorm.DB) *gorm.DB {
  3386. return db.Where("status = 1 AND user_org_id = ? AND record_date = ? AND team_id > 0", orgID, scheduleDate).Preload("XtHisProjectTeam", "status = 1").Preload("HisProject").Preload("GoodInfo", "status=1")
  3387. }).Where("status = 1 AND user_org_id = ?", orgID)
  3388. if scheduleDate != 0 {
  3389. db = db.Where("schedule_date = ?", scheduleDate)
  3390. }
  3391. err = db.Find(&vms).Error
  3392. }
  3393. } else {
  3394. if patient_id > 0 {
  3395. if execution_state == 1 {
  3396. db := readDb.
  3397. Table("xt_schedule").
  3398. Preload("SchedualPatient", "status = 1 AND user_org_id = ? and id= ?", orgID, patient_id).
  3399. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  3400. return db.Where("status = 1 AND user_org_id = ? and patient_id = ?", orgID, patient_id).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  3401. }).
  3402. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  3403. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  3404. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ? and patient_id = ?", orgID, scheduleDate, patient_id).
  3405. Preload("HisPrescriptionProject", func(db *gorm.DB) *gorm.DB {
  3406. return db.Where("status = 1 AND user_org_id = ? AND record_date = ? AND team_id = 0 and execution_state = ?", orgID, scheduleDate, execution_state).Preload("HisProject").Preload("GoodInfo", "status=1")
  3407. }).
  3408. Preload("HisPrescriptionTeamProject", func(db *gorm.DB) *gorm.DB {
  3409. return db.Where("status = 1 AND user_org_id = ? AND record_date = ? AND team_id > 0 and execution_state = ?", orgID, scheduleDate, execution_state).Preload("XtHisProjectTeam", "status = 1").Preload("HisProject").Preload("GoodInfo", "status=1")
  3410. }).Where("status = 1 AND user_org_id = ?", orgID)
  3411. if scheduleDate != 0 {
  3412. db = db.Where("schedule_date = ?", scheduleDate)
  3413. }
  3414. err = db.Find(&vms).Error
  3415. } else if execution_state == 2 {
  3416. db := readDb.
  3417. Table("xt_schedule").
  3418. Preload("SchedualPatient", "status = 1 AND user_org_id = ? and id= ?", orgID, patient_id).
  3419. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  3420. return db.Where("status = 1 AND user_org_id = ? and patient_id = ?", orgID, patient_id).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  3421. }).
  3422. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  3423. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  3424. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ? and patient_id = ?", orgID, scheduleDate, patient_id).
  3425. Preload("HisPrescriptionProject", func(db *gorm.DB) *gorm.DB {
  3426. return db.Where("status = 1 AND user_org_id = ? AND record_date = ? AND team_id = 0 and execution_state = 0", orgID, scheduleDate).Preload("HisProject").Preload("GoodInfo", "status=1")
  3427. }).
  3428. Preload("HisPrescriptionTeamProject", func(db *gorm.DB) *gorm.DB {
  3429. return db.Where("status = 1 AND user_org_id = ? AND record_date = ? AND team_id > 0 and execution_state = 0", orgID, scheduleDate).Preload("XtHisProjectTeam", "status = 1").Preload("HisProject").Preload("GoodInfo", "status=1")
  3430. }).Where("status = 1 AND user_org_id = ?", orgID)
  3431. if scheduleDate != 0 {
  3432. db = db.Where("schedule_date = ?", scheduleDate)
  3433. }
  3434. err = db.Find(&vms).Error
  3435. } else if execution_state == 0 {
  3436. db := readDb.
  3437. Table("xt_schedule").
  3438. Preload("SchedualPatient", "status = 1 AND user_org_id = ? and id= ?", orgID, patient_id).
  3439. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  3440. return db.Where("status = 1 AND user_org_id = ? and patient_id = ?", orgID, patient_id).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  3441. }).
  3442. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  3443. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  3444. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ? and patient_id = ?", orgID, scheduleDate, patient_id).
  3445. Preload("HisPrescriptionProject", func(db *gorm.DB) *gorm.DB {
  3446. return db.Where("status = 1 AND user_org_id = ? AND record_date = ? AND team_id = 0", orgID, scheduleDate).Preload("HisProject").Preload("GoodInfo", "status=1")
  3447. }).
  3448. Preload("HisPrescriptionTeamProject", func(db *gorm.DB) *gorm.DB {
  3449. return db.Where("status = 1 AND user_org_id = ? AND record_date = ? AND team_id > 0", orgID, scheduleDate).Preload("XtHisProjectTeam", "status = 1").Preload("HisProject").Preload("GoodInfo", "status=1")
  3450. }).Where("status = 1 AND user_org_id = ?", orgID)
  3451. if scheduleDate != 0 {
  3452. db = db.Where("schedule_date = ?", scheduleDate)
  3453. }
  3454. err = db.Find(&vms).Error
  3455. }
  3456. } else {
  3457. if execution_state == 1 {
  3458. db := readDb.
  3459. Table("xt_schedule").
  3460. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  3461. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  3462. return db.Where("status = 1 AND user_org_id = ?", orgID).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  3463. }).
  3464. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  3465. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  3466. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  3467. Preload("HisPrescriptionProject", func(db *gorm.DB) *gorm.DB {
  3468. return db.Where("status = 1 AND user_org_id = ? AND record_date = ? AND team_id = 0 and execution_state = ?", orgID, scheduleDate, execution_state).Preload("HisProject").Preload("GoodInfo", "status=1")
  3469. }).
  3470. Preload("HisPrescriptionTeamProject", func(db *gorm.DB) *gorm.DB {
  3471. return db.Where("status = 1 AND user_org_id = ? AND record_date = ? AND team_id > 0 and execution_state = ?", orgID, scheduleDate, execution_state).Preload("XtHisProjectTeam", "status = 1").Preload("HisProject").Preload("GoodInfo", "status=1")
  3472. }).Where("status = 1 AND user_org_id = ?", orgID)
  3473. if scheduleDate != 0 {
  3474. db = db.Where("schedule_date = ?", scheduleDate)
  3475. }
  3476. err = db.Find(&vms).Error
  3477. } else if execution_state == 2 {
  3478. fmt.Println("j氯332n323232n323ℹ️33232323232")
  3479. db := readDb.
  3480. Table("xt_schedule").
  3481. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  3482. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  3483. return db.Where("status = 1 AND user_org_id = ?", orgID).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  3484. }).
  3485. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  3486. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  3487. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  3488. Preload("HisPrescriptionProject", func(db *gorm.DB) *gorm.DB {
  3489. return db.Where("status = 1 AND user_org_id = ? AND record_date = ? AND team_id = 0 and execution_state =0 ", orgID, scheduleDate).Preload("HisProject").Preload("GoodInfo", "status=1")
  3490. }).
  3491. Preload("HisPrescriptionTeamProject", func(db *gorm.DB) *gorm.DB {
  3492. return db.Where("status = 1 AND user_org_id = ? AND record_date = ? AND team_id > 0 and execution_state =0 ", orgID, scheduleDate).Preload("XtHisProjectTeam", "status = 1").Preload("HisProject").Preload("GoodInfo", "status=1")
  3493. }).Where("status = 1 AND user_org_id = ?", orgID)
  3494. if scheduleDate != 0 {
  3495. db = db.Where("schedule_date = ?", scheduleDate)
  3496. }
  3497. err = db.Find(&vms).Error
  3498. } else if execution_state == 0 {
  3499. db := readDb.
  3500. Table("xt_schedule").
  3501. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  3502. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  3503. return db.Where("status = 1 AND user_org_id = ?", orgID).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  3504. }).
  3505. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  3506. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  3507. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  3508. Preload("HisPrescriptionProject", func(db *gorm.DB) *gorm.DB {
  3509. return db.Where("status = 1 AND user_org_id = ? AND record_date = ? AND team_id = 0", orgID, scheduleDate).Preload("HisProject").Preload("GoodInfo", "status=1")
  3510. }).
  3511. Preload("HisPrescriptionTeamProject", func(db *gorm.DB) *gorm.DB {
  3512. return db.Where("status = 1 AND user_org_id = ? AND record_date = ? AND team_id > 0", orgID, scheduleDate).Preload("XtHisProjectTeam", "status = 1").Preload("HisProject").Preload("GoodInfo", "status=1")
  3513. }).Where("status = 1 AND user_org_id = ?", orgID)
  3514. if scheduleDate != 0 {
  3515. db = db.Where("schedule_date = ?", scheduleDate)
  3516. }
  3517. err = db.Find(&vms).Error
  3518. }
  3519. }
  3520. }
  3521. }
  3522. if patientType > 0 {
  3523. if execution_state == 1 {
  3524. db := readDb.
  3525. Table("xt_schedule").
  3526. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  3527. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  3528. return db.Where("status = 1 AND user_org_id = ?", orgID).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  3529. }).
  3530. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  3531. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  3532. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  3533. Preload("HisDoctorAdviceInfo", "status = 1 AND user_org_id = ? AND advice_date = ? and (advice_doctor = ? or execution_staff = ?) ", orgID, scheduleDate, adminUserId, adminUserId).
  3534. Preload("HisPrescriptionProject", func(db *gorm.DB) *gorm.DB {
  3535. return db.Where("status = 1 AND user_org_id = ? AND record_date = ? AND team_id = 0 and execution_state = 1", orgID, scheduleDate).Preload("HisProject").Preload("GoodInfo", "status=1")
  3536. }).
  3537. Preload("HisPrescriptionTeamProject", func(db *gorm.DB) *gorm.DB {
  3538. return db.Where("status = 1 AND user_org_id = ? AND record_date = ? AND team_id > 0", orgID, scheduleDate).Preload("XtHisProjectTeam", "status = 1").Preload("HisProject").Preload("GoodInfo", "status=1")
  3539. }).Where("status = 1 AND user_org_id = ?", orgID)
  3540. if scheduleDate != 0 {
  3541. db = db.Where("schedule_date = ?", scheduleDate)
  3542. }
  3543. err = db.Find(&vms).Error
  3544. } else if execution_state == 2 {
  3545. db := readDb.
  3546. Table("xt_schedule").
  3547. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  3548. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  3549. return db.Where("status = 1 AND user_org_id = ?", orgID).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  3550. }).
  3551. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  3552. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  3553. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  3554. Preload("HisDoctorAdviceInfo", "status = 1 AND user_org_id = ? AND advice_date = ? and (advice_doctor = ? or execution_staff = ?) ", orgID, scheduleDate, adminUserId, adminUserId).
  3555. Preload("HisPrescriptionProject", func(db *gorm.DB) *gorm.DB {
  3556. return db.Where("status = 1 AND user_org_id = ? AND record_date = ? AND team_id = 0 and execution_state = 0", orgID, scheduleDate).Preload("HisProject").Preload("GoodInfo", "status=1")
  3557. }).
  3558. Preload("HisPrescriptionTeamProject", func(db *gorm.DB) *gorm.DB {
  3559. return db.Where("status = 1 AND user_org_id = ? AND record_date = ? AND team_id > 0", orgID, scheduleDate).Preload("XtHisProjectTeam", "status = 1").Preload("HisProject").Preload("GoodInfo", "status=1")
  3560. }).Where("status = 1 AND user_org_id = ?", orgID)
  3561. if scheduleDate != 0 {
  3562. db = db.Where("schedule_date = ?", scheduleDate)
  3563. }
  3564. err = db.Find(&vms).Error
  3565. } else if execution_state == 0 {
  3566. db := readDb.
  3567. Table("xt_schedule").
  3568. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  3569. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  3570. return db.Where("status = 1 AND user_org_id = ?", orgID).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  3571. }).
  3572. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  3573. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  3574. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  3575. Preload("HisDoctorAdviceInfo", "status = 1 AND user_org_id = ? AND advice_date = ? and (advice_doctor = ? or execution_staff = ?) ", orgID, scheduleDate, adminUserId, adminUserId).
  3576. Preload("HisPrescriptionProject", func(db *gorm.DB) *gorm.DB {
  3577. return db.Where("status = 1 AND user_org_id = ? AND record_date = ? AND team_id = 0", orgID, scheduleDate).Preload("HisProject").Preload("GoodInfo", "status=1")
  3578. }).
  3579. Preload("HisPrescriptionTeamProject", func(db *gorm.DB) *gorm.DB {
  3580. return db.Where("status = 1 AND user_org_id = ? AND record_date = ? AND team_id > 0", orgID, scheduleDate).Preload("XtHisProjectTeam", "status = 1").Preload("HisProject").Preload("GoodInfo", "status=1")
  3581. }).Where("status = 1 AND user_org_id = ?", orgID)
  3582. if scheduleDate != 0 {
  3583. db = db.Where("schedule_date = ?", scheduleDate)
  3584. }
  3585. err = db.Find(&vms).Error
  3586. }
  3587. }
  3588. return vms, err
  3589. }
  3590. func UpdateStockGoods(good_id int64, record_time int64, patient_id int64, count int64) error {
  3591. prepare := models.DialysisBeforePrepare{}
  3592. err := XTWriteDB().Model(&prepare).Where("good_id= ? and record_date = ? and patient_id = ? and count <> 0 and status = 1", good_id, record_time, patient_id).Updates(map[string]interface{}{"count": count}).Error
  3593. return err
  3594. }
  3595. func UPdateAutoStockGoods(good_id int64, record_time int64, patient_id int64, count int64) error {
  3596. detail := models.AutomaticReduceDetail{}
  3597. err := XTWriteDB().Model(&detail).Where("good_id = ? and record_time = ? and patient_id = ? and count <> 0 and status = 1", good_id, record_time, patient_id).Updates(map[string]interface{}{"count": count}).Error
  3598. return err
  3599. }
  3600. func MobileGetDialysisSolutionByModeIdSeven(orgID int64, patientID int64, mode_id int64) (*models.DialysisSolution, error) {
  3601. var record models.DialysisSolution
  3602. err := readDb.Model(&models.DialysisSolution{}).Where("patient_id = ? and user_org_id = ? and status = 1 AND mode_id = ?", patientID, orgID, mode_id).Last(&record).Error
  3603. if err != nil {
  3604. if err == gorm.ErrRecordNotFound {
  3605. return nil, nil
  3606. } else {
  3607. return nil, err
  3608. }
  3609. }
  3610. return &record, nil
  3611. }
  3612. func MobileGetLastDialysisPrescribeByModeIdSeven(orgID int64, patientID int64, mode_id int64) (*models.DialysisPrescription, error) {
  3613. var record models.DialysisPrescription
  3614. err := readDb.Model(&models.DialysisPrescription{}).Where("patient_id = ? and user_org_id = ? and status = 1 AND mode_id = ?", patientID, orgID, mode_id).Last(&record).Error
  3615. if err != nil {
  3616. if err == gorm.ErrRecordNotFound {
  3617. return nil, nil
  3618. } else {
  3619. return nil, err
  3620. }
  3621. }
  3622. return &record, nil
  3623. }
  3624. func MobileGetSystemDialysisPrescribeByModeIdSeven(orgID int64, mode_id int64) (*models.SystemPrescription, error) {
  3625. var record models.SystemPrescription
  3626. err := readDb.Model(&models.SystemPrescription{}).Where("user_org_id = ? and status = 1 AND mode_id = ?", orgID, mode_id).First(&record).Error
  3627. if err != nil {
  3628. if err == gorm.ErrRecordNotFound {
  3629. return nil, nil
  3630. } else {
  3631. return nil, err
  3632. }
  3633. }
  3634. return &record, nil
  3635. }
  3636. // 透前评估
  3637. func MobileGetPredialysisEvaluationSeven(orgID int64, patientID int64, recordDate int64) (*models.PredialysisEvaluation, error) {
  3638. var record models.PredialysisEvaluation
  3639. err := readDb.Model(&models.PredialysisEvaluation{}).Where("patient_id = ? and user_org_id = ? and status = 1 and assessment_date = ?", patientID, orgID, recordDate).First(&record).Error
  3640. if err != nil {
  3641. if err == gorm.ErrRecordNotFound {
  3642. return nil, nil
  3643. } else {
  3644. return nil, err
  3645. }
  3646. }
  3647. return &record, nil
  3648. }
  3649. //
  3650. //func MobileGetDialysisGoodsSix(orgID int64, scheduleDate int64) (prepare []*models.DialysisBeforePrepare, err error) {
  3651. //
  3652. // err = readDb.Where("user_org_id = ? AND status=1 AND record_date = ? AND count > 0", orgID, scheduleDate).Select("patient_id,good_id,record_date").Find(&prepare).Error
  3653. // return prepare, err
  3654. //}
  3655. func GetMobileAutoReduce(orgID int64, scheduleDate int64) (auto []*models.AutomaticReduceDetail, err error) {
  3656. err = readDb.Where("org_id = ? and status = 1 and record_time = ?", orgID, scheduleDate).Find(&auto).Error
  3657. return auto, err
  3658. }
  3659. func GetRoleList(orgid int64, admin_user_id int64) (models.SgjUserAdminRole, error) {
  3660. role := models.SgjUserAdminRole{}
  3661. err = readUserDb.Where("org_id = ? and admin_user_id = ? and status =1 ", orgid, admin_user_id).Find(&role).Error
  3662. return role, err
  3663. }
  3664. func GetDialysisGood(orgID int64, scheduleDate int64) (auto []*models.AutomaticReduceDetail, err error) {
  3665. err = readDb.Where("org_id = ? and status = 1 and record_time >= ?", orgID, scheduleDate).Group("patient_id").Find(&auto).Error
  3666. return auto, err
  3667. }