mobile_dialysis_service.go 198KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112
  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. fmt.Println("key23233232323323wi", key)
  697. doctor_advices_str, _ := redis.Get(key).Result()
  698. redis.Set(doctor_advices_str, "", time.Second)
  699. redis.Set(doctor_advices_str, "", time.Second)
  700. if len(doctor_advices_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  701. err := readDb.Model(&models.DoctorAdvice{}).
  702. Where("patient_id = ? and user_org_id = ? and status = 1 and record_date = ? and (advice_type = 2 || advice_type = 3)", patientID, orgID, recordDate).
  703. 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").
  704. Order("start_time asc, groupno desc, advice_order desc, id asc").
  705. Scan(&records).Error
  706. if err != nil {
  707. if err == gorm.ErrRecordNotFound {
  708. if len(records) <= 0 {
  709. redis.Set(key, "", time.Second*60*60*18)
  710. }
  711. return nil, nil
  712. } else {
  713. return nil, err
  714. }
  715. } else {
  716. if len(records) > 0 {
  717. //缓存数据
  718. doctor_advices_str, err := json.Marshal(records)
  719. if err == nil {
  720. redis.Set(key, doctor_advices_str, time.Second*60*60*18)
  721. return records, nil
  722. }
  723. } else {
  724. redis.Set(key, "null", time.Second*60*60*18)
  725. return records, nil
  726. }
  727. return records, nil
  728. }
  729. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  730. if doctor_advices_str == "null" {
  731. return records, nil
  732. } else {
  733. json.Unmarshal([]byte(doctor_advices_str), &records)
  734. return records, nil
  735. }
  736. }
  737. }
  738. // 透析记录
  739. func MobileGetSchedualDialysisRecordTen(orgID int64, patientID int64, recordDate int64) (*models.DialysisOrder, error) {
  740. var record models.DialysisOrder
  741. 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
  742. if err != nil {
  743. if err == gorm.ErrRecordNotFound {
  744. return nil, nil
  745. } else {
  746. return nil, err
  747. }
  748. }
  749. return &record, nil
  750. }
  751. // 透析记录
  752. func MobileGetSchedualDialysisRecord(orgID int64, patientID int64, recordDate int64) (*models.DialysisOrder, error) {
  753. var record models.DialysisOrder
  754. redis := RedisClient()
  755. defer redis.Close()
  756. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(patientID, 10) + ":" + strconv.FormatInt(recordDate, 10) + ":dialysis_order"
  757. redis.Set(key, "", time.Second)
  758. dialysis_order_str, _ := redis.Get(key).Result()
  759. if len(dialysis_order_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  760. 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
  761. if err != nil {
  762. if err == gorm.ErrRecordNotFound {
  763. return nil, nil
  764. } else {
  765. return nil, err
  766. }
  767. } else {
  768. if record.ID > 0 {
  769. //缓存数据
  770. dialysis_order_str, err := json.Marshal(record)
  771. if err == nil {
  772. redis.Set(key, dialysis_order_str, time.Second*60*60*18)
  773. }
  774. } else {
  775. redis.Set(key, "null", time.Second*60*60*18)
  776. }
  777. return &record, nil
  778. }
  779. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  780. if dialysis_order_str == "null" {
  781. 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
  782. return &record, nil
  783. } else {
  784. json.Unmarshal([]byte(dialysis_order_str), &record)
  785. return &record, nil
  786. }
  787. }
  788. }
  789. // 双人核对
  790. func MobileGetDoubleCheck(orgID int64, patientID int64, recordDate int64) (*models.DoubleCheck, error) {
  791. var record models.DoubleCheck
  792. redis := RedisClient()
  793. defer redis.Close()
  794. // cur_date := time.Now().Format("2006-01-02")
  795. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(patientID, 10) + ":" + strconv.FormatInt(recordDate, 10) + ":double_check"
  796. double_check_str, _ := redis.Get(key).Result()
  797. if len(double_check_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  798. err := readDb.Model(&models.DoubleCheck{}).Where("patient_id = ? and user_org_id = ? and status = 1 and check_date = ?", patientID, orgID, recordDate).First(&record).Error
  799. if err != nil {
  800. if err == gorm.ErrRecordNotFound {
  801. if record.ID <= 0 {
  802. redis.Set(key, "null", time.Second*60*60*18)
  803. }
  804. return nil, nil
  805. } else {
  806. return nil, err
  807. }
  808. } else {
  809. if record.ID > 0 {
  810. //缓存数据
  811. double_check_str, err := json.Marshal(record)
  812. if err == nil {
  813. redis.Set(key, double_check_str, time.Second*60*60*18)
  814. }
  815. } else {
  816. redis.Set(key, "null", time.Second*60*60*18)
  817. }
  818. return &record, nil
  819. }
  820. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  821. if double_check_str == "null" {
  822. return &record, nil
  823. } else {
  824. json.Unmarshal([]byte(double_check_str), &record)
  825. return &record, nil
  826. }
  827. }
  828. }
  829. // 透析监测记录
  830. func MobileGetMonitorRecords(orgID int64, patientID int64, recordDate int64) ([]*models.MonitoringRecord, error) {
  831. var records []*models.MonitoringRecord
  832. redis := RedisClient()
  833. defer redis.Close()
  834. // cur_date := time.Now().Format("2006-01-02")
  835. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(patientID, 10) + ":" + strconv.FormatInt(recordDate, 10) + ":monitor_records"
  836. redis.Set(key, "", time.Second)
  837. monitor_records_str, _ := redis.Get(key).Result()
  838. if len(monitor_records_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  839. 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
  840. if err != nil {
  841. if err == gorm.ErrRecordNotFound {
  842. //if len(records) <= 0 {
  843. // redis.Set(key, "null", time.Second*60*60*18)
  844. //}
  845. return nil, nil
  846. } else {
  847. return nil, err
  848. }
  849. } else {
  850. if len(records) > 0 {
  851. //缓存数据
  852. monitor_records_str, err := json.Marshal(records)
  853. if err == nil {
  854. redis.Set(key, monitor_records_str, time.Second*60*60*18)
  855. }
  856. } else {
  857. redis.Set(key, "null", time.Second*60*60*18)
  858. }
  859. return records, nil
  860. }
  861. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  862. if monitor_records_str == "null" {
  863. return records, nil
  864. } else {
  865. json.Unmarshal([]byte(monitor_records_str), &records)
  866. return records, nil
  867. }
  868. }
  869. }
  870. func MobileGetMonitorRecordFirst(orgID int64, patientID int64, recordDate int64) (*models.MonitoringRecord, error) {
  871. var records models.MonitoringRecord
  872. 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
  873. if err != nil {
  874. return nil, err
  875. }
  876. return &records, nil
  877. }
  878. func MobileGetLastMonitorRecordOne(orgID int64, patientID int64, beforeDate int64) (*models.MonitoringRecord, error) {
  879. var record models.MonitoringRecord
  880. 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
  881. if err != nil {
  882. if err == gorm.ErrRecordNotFound {
  883. return nil, nil
  884. } else {
  885. return nil, err
  886. }
  887. }
  888. return &record, nil
  889. }
  890. func MobileGetLastMonitorRecord(orgID int64, patientID int64, beforeDate int64) (*models.MonitoringRecord, error) {
  891. var record models.MonitoringRecord
  892. redis := RedisClient()
  893. defer redis.Close()
  894. // cur_date := time.Now().Format("2006-01-02")
  895. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(patientID, 10) + ":" + strconv.FormatInt(beforeDate, 10) + ":monitor_record_last"
  896. monitor_record_last_str, _ := redis.Get(key).Result()
  897. if len(monitor_record_last_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  898. 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
  899. if err != nil {
  900. if err == gorm.ErrRecordNotFound {
  901. if record.ID <= 0 {
  902. redis.Set(key, "null", time.Second*60*60*18)
  903. }
  904. return nil, nil
  905. } else {
  906. return nil, err
  907. }
  908. } else {
  909. if record.ID > 0 {
  910. //缓存数据
  911. monitor_record_last_str, err := json.Marshal(record)
  912. if err == nil {
  913. redis.Set(key, monitor_record_last_str, time.Second*60*60*18)
  914. }
  915. } else {
  916. redis.Set(key, "null", time.Second*60*60*18)
  917. }
  918. return &record, nil
  919. }
  920. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  921. if monitor_record_last_str == "null" {
  922. return &record, nil
  923. } else {
  924. json.Unmarshal([]byte(monitor_record_last_str), &record)
  925. return &record, nil
  926. }
  927. }
  928. }
  929. func MobileGetAssessmentAfterDislysisOne(orgID int64, patientID int64, recordDate int64) (*models.AssessmentAfterDislysis, error) {
  930. var record models.AssessmentAfterDislysis
  931. err := readDb.Model(&models.AssessmentAfterDislysis{}).Where("patient_id = ? and user_org_id = ? and status = 1 and assessment_date = ?", patientID, orgID, recordDate).First(&record).Error
  932. if err != nil {
  933. if err == gorm.ErrRecordNotFound {
  934. return nil, nil
  935. } else {
  936. return nil, err
  937. }
  938. }
  939. return &record, nil
  940. }
  941. // 透后评估
  942. func MobileGetAssessmentAfterDislysis(orgID int64, patientID int64, recordDate int64) (*models.AssessmentAfterDislysis, error) {
  943. var record models.AssessmentAfterDislysis
  944. redis := RedisClient()
  945. defer redis.Close()
  946. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(patientID, 10) + ":" + strconv.FormatInt(recordDate, 10) + ":assessment_after_dislysis"
  947. assessment_after_dislysis_str, _ := redis.Get(key).Result()
  948. if len(assessment_after_dislysis_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  949. err := readDb.Model(&models.AssessmentAfterDislysis{}).Where("patient_id = ? and user_org_id = ? and status = 1 and assessment_date = ?", patientID, orgID, recordDate).First(&record).Error
  950. if err != nil {
  951. if err == gorm.ErrRecordNotFound {
  952. if record.ID <= 0 {
  953. redis.Set(key, "null", time.Second*60*60*18)
  954. }
  955. return nil, nil
  956. } else {
  957. return nil, err
  958. }
  959. } else {
  960. if record.ID > 0 {
  961. //缓存数据
  962. assessment_after_dislysis_str, err := json.Marshal(record)
  963. if err == nil {
  964. redis.Set(key, assessment_after_dislysis_str, time.Second*60*60*18)
  965. }
  966. } else {
  967. redis.Set(key, "null", time.Second*60*60*18)
  968. }
  969. return &record, nil
  970. }
  971. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  972. if assessment_after_dislysis_str == "null" {
  973. return &record, nil
  974. } else {
  975. json.Unmarshal([]byte(assessment_after_dislysis_str), &record)
  976. return &record, nil
  977. }
  978. }
  979. }
  980. // 获取 maxDate 之前一次的透后评估记录
  981. func MobileGetLastTimeAssessmentAfterDislysis(orgID int64, patientID int64, maxDate int64) (*models.AssessmentAfterDislysis, error) {
  982. var record models.AssessmentAfterDislysis
  983. redis := RedisClient()
  984. defer redis.Close()
  985. // cur_date := time.Now().Format("2006-01-02")
  986. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(patientID, 10) + ":" + strconv.FormatInt(maxDate, 10) + ":assessment_after_dislysis_last"
  987. assessment_after_dislysis_last_str, _ := redis.Get(key).Result()
  988. if len(assessment_after_dislysis_last_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  989. 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
  990. if err != nil {
  991. if err == gorm.ErrRecordNotFound {
  992. if record.ID <= 0 {
  993. redis.Set(key, "null", time.Second*60*60*18)
  994. }
  995. return nil, nil
  996. } else {
  997. return nil, err
  998. }
  999. } else {
  1000. if record.ID > 0 {
  1001. //缓存数据
  1002. assessment_after_dislysis_last_str, err := json.Marshal(record)
  1003. if err == nil {
  1004. redis.Set(key, assessment_after_dislysis_last_str, time.Second*60*60*18)
  1005. }
  1006. } else {
  1007. redis.Set(key, "null", time.Second*60*60*18)
  1008. }
  1009. return &record, nil
  1010. }
  1011. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  1012. if assessment_after_dislysis_last_str == "null" {
  1013. return &record, nil
  1014. } else {
  1015. json.Unmarshal([]byte(assessment_after_dislysis_last_str), &record)
  1016. return &record, nil
  1017. }
  1018. }
  1019. }
  1020. func MobileGetLastTimeAssessmentAfterDislysisOne(orgID int64, patientID int64, maxDate int64) (*models.AssessmentAfterDislysis, error) {
  1021. var record models.AssessmentAfterDislysis
  1022. 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
  1023. if err != nil {
  1024. if err == gorm.ErrRecordNotFound {
  1025. return nil, nil
  1026. } else {
  1027. return nil, err
  1028. }
  1029. }
  1030. return &record, nil
  1031. }
  1032. func MobileGetLast(orgID int64, patientID int64, maxDate int64) (models.AssessmentAfterDislysis, error) {
  1033. dislysis := models.AssessmentAfterDislysis{}
  1034. 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
  1035. return dislysis, err
  1036. }
  1037. // 治疗小结
  1038. func MobileGetTreatmentSummary(orgID int64, patientID int64, recordDate int64) (*models.TreatmentSummary, error) {
  1039. var record models.TreatmentSummary
  1040. redis := RedisClient()
  1041. defer redis.Close()
  1042. // cur_date := time.Now().Format("2006-01-02")
  1043. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(patientID, 10) + ":" + strconv.FormatInt(recordDate, 10) + ":treatment_summary"
  1044. treatment_summary_str, _ := redis.Get(key).Result()
  1045. if len(treatment_summary_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1046. err := readDb.Model(&models.TreatmentSummary{}).Where("patient_id = ? and user_org_id = ? and status = 1 and assessment_date = ?", patientID, orgID, recordDate).First(&record).Error
  1047. if err != nil {
  1048. if err == gorm.ErrRecordNotFound {
  1049. if record.ID <= 0 {
  1050. redis.Set(key, "null", time.Second*60*60*18)
  1051. }
  1052. return nil, nil
  1053. } else {
  1054. return nil, err
  1055. }
  1056. } else {
  1057. if record.ID > 0 {
  1058. //缓存数据
  1059. treatment_summary_str, err := json.Marshal(record)
  1060. if err == nil {
  1061. redis.Set(key, treatment_summary_str, time.Second*60*60*18)
  1062. }
  1063. } else {
  1064. redis.Set(key, "null", time.Second*60*60*18)
  1065. }
  1066. return &record, nil
  1067. }
  1068. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  1069. if treatment_summary_str == "null" {
  1070. return &record, nil
  1071. } else {
  1072. json.Unmarshal([]byte(treatment_summary_str), &record)
  1073. return &record, nil
  1074. }
  1075. }
  1076. }
  1077. // 透析处方
  1078. func MobileGetDialysisPrescribe(orgID int64, patientID int64, recordDate int64) (*models.DialysisPrescription, error) {
  1079. var record models.DialysisPrescription
  1080. err := readDb.Model(&models.DialysisPrescription{}).Where("patient_id = ? and user_org_id = ? and status = 1 and record_date = ?", patientID, orgID, recordDate).Find(&record).Error
  1081. if err != nil {
  1082. if err == gorm.ErrRecordNotFound {
  1083. return nil, nil
  1084. } else {
  1085. return nil, err
  1086. }
  1087. }
  1088. return &record, nil
  1089. }
  1090. func MobileGetDialysisSchedual(orgID int64, patientID int64, scheduleDate int64) (*models.XtSchedule, error) {
  1091. var schedule models.XtSchedule
  1092. err := readDb.Model(&models.XtSchedule{}).Where("patient_id = ? and user_org_id = ? and status = 1 and schedule_date = ?", patientID, orgID, scheduleDate).First(&schedule).Error
  1093. if err != nil {
  1094. if err == gorm.ErrRecordNotFound {
  1095. return nil, nil
  1096. } else {
  1097. return nil, err
  1098. }
  1099. }
  1100. return &schedule, nil
  1101. }
  1102. // 透析方案
  1103. func MobileGetDialysisSolution(orgID int64, patientID int64) (*models.DialysisSolution, error) {
  1104. var record models.DialysisSolution
  1105. err := readDb.Model(&models.DialysisSolution{}).Where("patient_id = ? and user_org_id = ? and status = 1", patientID, orgID).Last(&record).Error
  1106. if err != nil {
  1107. if err == gorm.ErrRecordNotFound {
  1108. return nil, nil
  1109. } else {
  1110. return nil, err
  1111. }
  1112. }
  1113. return &record, nil
  1114. }
  1115. func MobileGetPatientById(orgID int64, patientID int64) (*models.Patients, error) {
  1116. var patient models.Patients
  1117. err := readDb.Model(&models.Patients{}).Where("id = ? and user_org_id = ? and status = 1", patientID, orgID).First(&patient).Error
  1118. if err != nil {
  1119. if err == gorm.ErrRecordNotFound {
  1120. return nil, nil
  1121. } else {
  1122. return nil, err
  1123. }
  1124. }
  1125. return &patient, nil
  1126. }
  1127. func DisableMonitor(orgID int64, patientID int64, recordID int64, admin_user_id int64) error {
  1128. fmt.Println()
  1129. tx := writeDb.Begin()
  1130. updateTime := time.Now().Unix()
  1131. 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
  1132. if err != nil {
  1133. tx.Rollback()
  1134. return err
  1135. }
  1136. tx.Commit()
  1137. return nil
  1138. }
  1139. func GetMonitor(orgID int64, patientID int64, id int64) (*models.MonitoringRecord, error) {
  1140. var monitor models.MonitoringRecord
  1141. var err error
  1142. err = readDb.Model(&models.MonitoringRecord{}).Where("id = ? AND user_org_id = ? AND patient_id = ? AND status = 1 ", id, orgID, patientID).Find(&monitor).Error
  1143. if err == gorm.ErrRecordNotFound {
  1144. return nil, nil
  1145. }
  1146. if err != nil {
  1147. return nil, err
  1148. }
  1149. return &monitor, nil
  1150. }
  1151. type MScheduleDoctorAdviceVM struct {
  1152. ID int64 `gorm:"column:id" json:"id"`
  1153. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id"`
  1154. PartitionId int64 `gorm:"column:partition_id" json:"partition_id"`
  1155. BedId int64 `gorm:"column:bed_id" json:"bed_id"`
  1156. PatientId int64 `gorm:"column:patient_id" json:"patient_id"`
  1157. ScheduleDate int64 `gorm:"column:schedule_date" json:"schedule_date"`
  1158. ScheduleType int64 `gorm:"column:schedule_type" json:"schedule_type"`
  1159. ModeId int64 `gorm:"column:mode_id" json:"mode_id"`
  1160. Status int64 `gorm:"column:status" json:"status"`
  1161. DialysisOrder *MDialysisOrderVM `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"dialysis_order"`
  1162. SchedualPatient *MSchedualPatientVM `gorm:"ForeignKey:PatientId;AssociationForeignKey:ID" json:"patient"`
  1163. DeviceNumber *MDeviceNumberVM `gorm:"ForeignKey:BedId" json:"device_number"`
  1164. DoctorAdvices []*MDoctorAdviceVM `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"doctor_advice"`
  1165. Prescription *models.DialysisPrescriptionList `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"prescription"`
  1166. DialysisAssesmentBefor *models.DialysisAssesmentBefor `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"dialysisassesmentbefor"`
  1167. }
  1168. func (MScheduleDoctorAdviceVM) TableName() string {
  1169. return "xt_schedule"
  1170. }
  1171. type MScheduleDoctorAdviceVMOne struct {
  1172. ID int64 `gorm:"column:id" json:"id"`
  1173. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id"`
  1174. PartitionId int64 `gorm:"column:partition_id" json:"partition_id"`
  1175. BedId int64 `gorm:"column:bed_id" json:"bed_id"`
  1176. PatientId int64 `gorm:"column:patient_id" json:"patient_id"`
  1177. ScheduleDate int64 `gorm:"column:schedule_date" json:"schedule_date"`
  1178. ScheduleType int64 `gorm:"column:schedule_type" json:"schedule_type"`
  1179. ModeId int64 `gorm:"column:mode_id" json:"mode_id"`
  1180. Status int64 `gorm:"column:status" json:"status"`
  1181. DialysisOrder *MDialysisOrderVM `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"dialysis_order"`
  1182. SchedualPatient *MSchedualPatientVM `gorm:"ForeignKey:PatientId;AssociationForeignKey:ID" json:"patient"`
  1183. DeviceNumber *MDeviceNumberVM `gorm:"ForeignKey:BedId" json:"device_number"`
  1184. DoctorAdvices []*MDoctorAdviceVM `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"doctor_advice"`
  1185. Prescription *models.DialysisPrescriptionList `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"prescription"`
  1186. //DialysisAssesmentBefor *models.DialysisAssesmentBefor `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"dialysisassesmentbefor"`
  1187. }
  1188. func (MScheduleDoctorAdviceVMOne) TableName() string {
  1189. return "xt_schedule"
  1190. }
  1191. type MDoctorAdviceVM struct {
  1192. ID int64 `gorm:"column:id" json:"id"`
  1193. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id"`
  1194. PatientId int64 `gorm:"column:patient_id" json:"patient_id"`
  1195. AdviceType int64 `gorm:"column:advice_type" json:"advice_type"`
  1196. AdviceDate int64 `gorm:"column:advice_date" json:"advice_date"`
  1197. StartTime int64 `gorm:"column:start_time" json:"start_time"`
  1198. AdviceName string `gorm:"column:advice_name" json:"advice_name"`
  1199. AdviceDesc string `gorm:"column:advice_desc" json:"advice_desc"`
  1200. ReminderDate int64 `gorm:"column:reminder_date" json:"reminder_date"`
  1201. SingleDose float64 `gorm:"column:single_dose" json:"single_dose"`
  1202. SingleDoseUnit string `gorm:"column:single_dose_unit" json:"single_dose_unit"`
  1203. PrescribingNumber float64 `gorm:"column:prescribing_number" json:"prescribing_number"`
  1204. PrescribingNumberUnit string `gorm:"column:prescribing_number_unit" json:"prescribing_number_unit"`
  1205. DeliveryWay string `gorm:"column:delivery_way" json:"delivery_way"`
  1206. ExecutionFrequency string `gorm:"column:execution_frequency" json:"execution_frequency"`
  1207. AdviceDoctor int64 `gorm:"column:advice_doctor" json:"advice_doctor"`
  1208. Status int64 `gorm:"column:status" json:"status"`
  1209. CreatedTime int64 `gorm:"column:created_time" json:"created_time"`
  1210. UpdatedTime int64 `gorm:"column:updated_time" json:"updated_time"`
  1211. AdviceAffirm string `gorm:"column:advice_affirm" json:"advice_affirm"`
  1212. Remark string `gorm:"column:remark" json:"remark"`
  1213. StopTime int64 `gorm:"column:stop_time" json:"stop_time"`
  1214. StopReason string `gorm:"column:stop_reason" json:"stop_reason"`
  1215. StopDoctor int64 `gorm:"column:stop_doctor" json:"stop_doctor"`
  1216. StopState int64 `gorm:"column:stop_state" json:"stop_state"`
  1217. ParentId int64 `gorm:"column:parent_id" json:"parent_id"`
  1218. ExecutionTime int64 `gorm:"column:execution_time" json:"execution_time"`
  1219. ExecutionStaff int64 `gorm:"column:execution_staff" json:"execution_staff"`
  1220. ExecutionState int64 `gorm:"column:execution_state" json:"execution_state"`
  1221. Checker int64 `gorm:"column:checker" json:"checker"`
  1222. RecordDate int64 `gorm:"column:record_date" json:"record_date"`
  1223. DialysisOrderId int64 `gorm:"column:dialysis_order_id" json:"dialysis_order_id"`
  1224. CheckTime int64 `gorm:"column:check_time" json:"check_time"`
  1225. CheckState int64 `gorm:"column:check_state" json:"check_state"`
  1226. DrugSpec float64 `gorm:"column:drug_spec" json:"drug_spec"`
  1227. DrugSpecUnit string `gorm:"column:drug_spec_unit" json:"drug_spec_unit"`
  1228. Groupno int64 `gorm:"column:groupno" json:"groupno"`
  1229. RemindType int64 `gorm:"column:remind_type" json:"remind_type"`
  1230. FrequencyType int64 `gorm:"column:frequency_type" json:"frequency_type"`
  1231. DayCount int64 `gorm:"column:day_count" json:"day_count"`
  1232. WeekDay string `gorm:"column:week_day" json:"week_day"`
  1233. TemplateId string `gorm:"column:template_id" json:"template_id"`
  1234. Modifier int64 `gorm:"column:modifier" json:"modifier" form:"modifier"`
  1235. IsMedicine int64 `gorm:"column:is_medicine" json:"is_medicine" form:"is_medicine"`
  1236. }
  1237. func (MDoctorAdviceVM) TableName() string {
  1238. return "xt_doctor_advice"
  1239. }
  1240. func MobileGetScheduleDoctorAdvices(orgID int64, scheduleDate int64, adviceType int, patientType int, adminUserId int64, deliverWay string) ([]*MScheduleDoctorAdviceVM, error) {
  1241. var vms []*MScheduleDoctorAdviceVM
  1242. adviceWhere := ""
  1243. adviceCondition := []interface{}{}
  1244. if adviceType == 0 {
  1245. if patientType == 0 {
  1246. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND (advice_type = 3 OR advice_type = 1)"
  1247. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  1248. } else if patientType == 1 {
  1249. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_doctor = ? AND(advice_type = 3 OR advice_type = 1)"
  1250. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId)
  1251. } else if patientType == 2 {
  1252. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND execution_staff = 0 AND(advice_type = 3 OR advice_type = 1)"
  1253. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  1254. }
  1255. } else if adviceType == 1 {
  1256. if patientType == 0 {
  1257. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 1 AND record_date = ? "
  1258. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  1259. } else if patientType == 1 {
  1260. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 1 AND record_date = ? AND advice_doctor = ? "
  1261. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId)
  1262. } else if patientType == 2 {
  1263. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 1 AND record_date = ? AND execution_staff = 0"
  1264. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  1265. }
  1266. } else if adviceType == 3 {
  1267. if patientType == 0 {
  1268. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 3 AND record_date = ? "
  1269. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  1270. } else if patientType == 1 {
  1271. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 3 AND advice_doctor = ? "
  1272. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId)
  1273. } else if patientType == 2 {
  1274. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 3 AND execution_staff = 0"
  1275. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  1276. }
  1277. } else if adviceType == 2 && len(deliverWay) > 0 {
  1278. if patientType == 0 {
  1279. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 2 AND record_date = ? and delivery_way = ?"
  1280. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, deliverWay)
  1281. } else if patientType == 1 {
  1282. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 2 AND advice_doctor = ? and delivery_way = ? "
  1283. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId, deliverWay)
  1284. } else if patientType == 2 {
  1285. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 2 AND execution_staff = 0 and delivery_way = ?"
  1286. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, deliverWay)
  1287. }
  1288. } else if adviceType == 2 && len(deliverWay) <= 0 {
  1289. if patientType == 0 {
  1290. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 2 AND record_date = ?"
  1291. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  1292. } else if patientType == 1 {
  1293. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 2 AND (advice_doctor = ? or execution_staff = ?)"
  1294. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId, adminUserId)
  1295. } else if patientType == 2 {
  1296. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 2 AND execution_staff = 0"
  1297. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  1298. }
  1299. }
  1300. db := readDb.
  1301. Table("xt_schedule").
  1302. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  1303. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  1304. return db.Where("status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, scheduleDate).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  1305. }).
  1306. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  1307. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  1308. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  1309. Preload("DialysisAssesmentBefor", "status =1 AND user_org_id = ? and assessment_date =?", orgID, scheduleDate).
  1310. Preload("DoctorAdvices", adviceCondition...).
  1311. Where("status = 1 AND user_org_id = ?", orgID)
  1312. if scheduleDate != 0 {
  1313. db = db.Where("schedule_date = ?", scheduleDate)
  1314. }
  1315. err := db.Find(&vms).Error
  1316. return vms, err
  1317. }
  1318. func MobileGetScheduleDoctorAdvicesTwo(orgID int64, scheduleDate int64, adviceType int, patientType int, adminUserId int64, deliverWay string) ([]*MScheduleDoctorAdviceVMOne, error) {
  1319. var vms []*MScheduleDoctorAdviceVMOne
  1320. adviceWhere := ""
  1321. adviceCondition := []interface{}{}
  1322. if adviceType == 0 {
  1323. if patientType == 0 {
  1324. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND (advice_type = 3 OR advice_type = 1)"
  1325. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  1326. } else if patientType == 1 {
  1327. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_doctor = ? AND(advice_type = 3 OR advice_type = 1)"
  1328. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId)
  1329. } else if patientType == 2 {
  1330. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND execution_staff = 0 AND(advice_type = 3 OR advice_type = 1)"
  1331. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  1332. }
  1333. } else if adviceType == 1 {
  1334. if patientType == 0 {
  1335. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 1 AND record_date = ? "
  1336. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  1337. } else if patientType == 1 {
  1338. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 1 AND record_date = ? AND advice_doctor = ? "
  1339. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId)
  1340. } else if patientType == 2 {
  1341. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 1 AND record_date = ? AND execution_staff = 0"
  1342. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  1343. }
  1344. } else if adviceType == 3 {
  1345. if patientType == 0 {
  1346. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 3 AND record_date = ? "
  1347. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  1348. } else if patientType == 1 {
  1349. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 3 AND advice_doctor = ? "
  1350. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId)
  1351. } else if patientType == 2 {
  1352. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 3 AND execution_staff = 0"
  1353. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  1354. }
  1355. } else if adviceType == 2 && len(deliverWay) > 0 {
  1356. if patientType == 0 {
  1357. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 2 AND record_date = ? and delivery_way = ?"
  1358. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, deliverWay)
  1359. } else if patientType == 1 {
  1360. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 2 AND advice_doctor = ? and delivery_way = ? "
  1361. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId, deliverWay)
  1362. } else if patientType == 2 {
  1363. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 2 AND execution_staff = 0 and delivery_way = ?"
  1364. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, deliverWay)
  1365. }
  1366. } else if adviceType == 2 && len(deliverWay) <= 0 {
  1367. if patientType == 0 {
  1368. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 2 AND record_date = ?"
  1369. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  1370. } else if patientType == 1 {
  1371. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 2 AND (advice_doctor = ? or execution_staff = ?)"
  1372. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId, adminUserId)
  1373. } else if patientType == 2 {
  1374. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 2 AND execution_staff = 0"
  1375. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  1376. }
  1377. }
  1378. db := readDb.
  1379. Table("xt_schedule").
  1380. //Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  1381. //Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  1382. // return db.Where("status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, scheduleDate).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  1383. //}).
  1384. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  1385. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  1386. //Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  1387. Preload("DoctorAdvices", adviceCondition...).
  1388. Where("status = 1 AND user_org_id = ?", orgID)
  1389. if scheduleDate != 0 {
  1390. db = db.Where("schedule_date = ?", scheduleDate)
  1391. }
  1392. err := db.Find(&vms).Error
  1393. return vms, err
  1394. }
  1395. func GetHisDoctorAdvices(orgID int64, scheduleDate int64, deliverWay string, patientType int, adminUserId int64) ([]*HisMScheduleDoctorAdviceVM, error) {
  1396. var vms []*HisMScheduleDoctorAdviceVM
  1397. if len(deliverWay) > 0 {
  1398. if patientType == 0 {
  1399. db := readDb.
  1400. Table("xt_schedule").
  1401. Preload("HisDoctorAdviceInfo", "status = 1 AND user_org_id = ? AND advice_date = ? and delivery_way = ?", orgID, scheduleDate, deliverWay).
  1402. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  1403. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  1404. Preload("HisPrescriptionProject", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  1405. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  1406. return db.Where("status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, scheduleDate).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  1407. }).
  1408. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  1409. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  1410. Where("status = 1 AND user_org_id = ?", orgID)
  1411. if scheduleDate != 0 {
  1412. db = db.Where("schedule_date = ?", scheduleDate)
  1413. }
  1414. err = db.Find(&vms).Error
  1415. }
  1416. if patientType > 0 {
  1417. db := readDb.
  1418. Table("xt_schedule").
  1419. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  1420. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  1421. 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).
  1422. Preload("HisPrescriptionProject", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  1423. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  1424. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  1425. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  1426. return db.Where("status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, scheduleDate).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  1427. }).Where("status = 1 AND user_org_id = ?", orgID)
  1428. if scheduleDate != 0 {
  1429. db = db.Where("schedule_date = ?", scheduleDate)
  1430. }
  1431. err = db.Find(&vms).Error
  1432. }
  1433. } else {
  1434. if patientType == 0 {
  1435. db := readDb.
  1436. Table("xt_schedule").
  1437. Preload("HisDoctorAdviceInfo", "status = 1 AND user_org_id = ? AND advice_date = ? ", orgID, scheduleDate).
  1438. Preload("HisPrescriptionProject", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  1439. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  1440. return db.Where("status = 1 AND user_org_id = ? and dialysis_date = ?", orgID, scheduleDate).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  1441. }).
  1442. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  1443. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  1444. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  1445. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  1446. Where("status = 1 AND user_org_id = ?", orgID)
  1447. if scheduleDate != 0 {
  1448. db = db.Where("schedule_date = ?", scheduleDate)
  1449. }
  1450. err = db.Find(&vms).Error
  1451. }
  1452. if patientType > 0 {
  1453. db := readDb.
  1454. Table("xt_schedule").
  1455. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  1456. Preload("HisDoctorAdviceInfo", "status = 1 AND user_org_id = ? AND advice_date = ? and (advice_doctor = ? or execution_staff = ?) ", orgID, scheduleDate, adminUserId, adminUserId).
  1457. Preload("HisPrescriptionProject", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  1458. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  1459. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  1460. return db.Where("status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, scheduleDate).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  1461. }).Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  1462. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  1463. Where("status = 1 AND user_org_id = ?", orgID)
  1464. if scheduleDate != 0 {
  1465. db = db.Where("schedule_date = ?", scheduleDate)
  1466. }
  1467. err = db.Find(&vms).Error
  1468. }
  1469. }
  1470. return vms, err
  1471. }
  1472. func GetHisDoctorAdvicesTwo(orgID int64, scheduleDate int64, deliverWay string, patientType int, adminUserId int64) ([]*HisMScheduleDoctorAdviceVMOne, error) {
  1473. var vms []*HisMScheduleDoctorAdviceVMOne
  1474. if len(deliverWay) > 0 {
  1475. if patientType == 0 {
  1476. db := readDb.
  1477. Table("xt_schedule").
  1478. Preload("HisDoctorAdviceInfo", "status = 1 AND user_org_id = ? AND advice_date = ? and delivery_way = ?", orgID, scheduleDate, deliverWay).
  1479. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  1480. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  1481. Preload("HisPrescriptionProject", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  1482. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  1483. return db.Where("status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, scheduleDate).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  1484. }).
  1485. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  1486. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  1487. Where("status = 1 AND user_org_id = ?", orgID)
  1488. if scheduleDate != 0 {
  1489. db = db.Where("schedule_date = ?", scheduleDate)
  1490. }
  1491. err = db.Find(&vms).Error
  1492. }
  1493. if patientType > 0 {
  1494. db := readDb.
  1495. Table("xt_schedule").
  1496. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  1497. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  1498. 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).
  1499. Preload("HisPrescriptionProject", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  1500. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  1501. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  1502. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  1503. return db.Where("status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, scheduleDate).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  1504. }).Where("status = 1 AND user_org_id = ?", orgID)
  1505. if scheduleDate != 0 {
  1506. db = db.Where("schedule_date = ?", scheduleDate)
  1507. }
  1508. err = db.Find(&vms).Error
  1509. }
  1510. } else {
  1511. if patientType == 0 {
  1512. db := readDb.
  1513. Table("xt_schedule").
  1514. Preload("HisDoctorAdviceInfo", "status = 1 AND user_org_id = ? AND advice_date = ? ", orgID, scheduleDate).
  1515. Preload("HisPrescriptionProject", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  1516. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  1517. return db.Where("status = 1 AND user_org_id = ? and dialysis_date = ?", orgID, scheduleDate).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  1518. }).
  1519. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  1520. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  1521. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  1522. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  1523. Where("status = 1 AND user_org_id = ?", orgID)
  1524. if scheduleDate != 0 {
  1525. db = db.Where("schedule_date = ?", scheduleDate)
  1526. }
  1527. err = db.Find(&vms).Error
  1528. }
  1529. if patientType > 0 {
  1530. db := readDb.
  1531. Table("xt_schedule").
  1532. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  1533. Preload("HisDoctorAdviceInfo", "status = 1 AND user_org_id = ? AND advice_date = ? and (advice_doctor = ? or execution_staff = ?) ", orgID, scheduleDate, adminUserId, adminUserId).
  1534. Preload("HisPrescriptionProject", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  1535. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  1536. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  1537. return db.Where("status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, scheduleDate).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  1538. }).Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  1539. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  1540. Where("status = 1 AND user_org_id = ?", orgID)
  1541. if scheduleDate != 0 {
  1542. db = db.Where("schedule_date = ?", scheduleDate)
  1543. }
  1544. err = db.Find(&vms).Error
  1545. }
  1546. }
  1547. return vms, err
  1548. }
  1549. func GetMobileHisPrescriptionProject(orgID int64, scheduleDate int64, deliverWay string, patientType int, adminUserId int64) ([]*HisMScheduleProjectVM, error) {
  1550. var vms []*HisMScheduleProjectVM
  1551. //if patientType == 0 {
  1552. // db := readDb.
  1553. // Table("xt_schedule").
  1554. // Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  1555. // Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  1556. // return db.Where("status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, scheduleDate).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  1557. // }).
  1558. // Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  1559. // Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  1560. // Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  1561. // Preload("HisPrescriptionProject", func(db *gorm.DB) *gorm.DB {
  1562. // return db.Where("status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).Preload("HisProject").Preload("GoodInfo", "status=1")
  1563. // }).
  1564. // Where("status = 1 AND user_org_id = ?", orgID)
  1565. // if scheduleDate != 0 {
  1566. // db = db.Where("schedule_date = ?", scheduleDate)
  1567. // }
  1568. // err = db.Find(&vms).Error
  1569. //}
  1570. //if patientType > 0 {
  1571. // db := readDb.
  1572. // Table("xt_schedule").
  1573. // Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  1574. // Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  1575. // return db.Where("status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, scheduleDate).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  1576. // }).
  1577. // Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  1578. // Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  1579. // Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  1580. // Preload("HisDoctorAdviceInfo", "status = 1 AND user_org_id = ? AND advice_date = ? and (advice_doctor = ? or execution_staff = ?) ", orgID, scheduleDate, adminUserId, adminUserId).
  1581. // Preload("HisPrescriptionProject", func(db *gorm.DB) *gorm.DB {
  1582. // return db.Where("status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).Preload("HisProject").Preload("GoodInfo", "status=1")
  1583. // }).
  1584. // Where("status = 1 AND user_org_id = ?", orgID)
  1585. // if scheduleDate != 0 {
  1586. // db = db.Where("schedule_date = ?", scheduleDate)
  1587. // }
  1588. // err = db.Find(&vms).Error
  1589. //}
  1590. db := readDb.Table("xt_schedule").Where("status = 1")
  1591. if orgID > 0 {
  1592. db = db.Where("user_org_id = ?", orgID)
  1593. }
  1594. if scheduleDate > 0 {
  1595. db = db.Where("schedule_date =?", scheduleDate)
  1596. }
  1597. err = db.Find(&vms).Error
  1598. if patientType == 0 {
  1599. db := readDb.
  1600. Table("xt_schedule").
  1601. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  1602. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  1603. return db.Where("status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, scheduleDate).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  1604. }).
  1605. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  1606. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  1607. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  1608. Preload("HisPrescriptionProject", 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("HisProject").Preload("GoodInfo", "status=1")
  1610. }).
  1611. Preload("HisPrescriptionTeamProject", func(db *gorm.DB) *gorm.DB {
  1612. 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")
  1613. }).
  1614. Where("status = 1 AND user_org_id = ?", orgID)
  1615. if scheduleDate != 0 {
  1616. db = db.Where("schedule_date = ?", scheduleDate)
  1617. }
  1618. err = db.Find(&vms).Error
  1619. }
  1620. if patientType > 0 {
  1621. db := readDb.
  1622. Table("xt_schedule").
  1623. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  1624. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  1625. return db.Where("status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, scheduleDate).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  1626. }).
  1627. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  1628. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  1629. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  1630. Preload("HisDoctorAdviceInfo", "status = 1 AND user_org_id = ? AND advice_date = ? and (advice_doctor = ? or execution_staff = ?) ", orgID, scheduleDate, adminUserId, adminUserId).
  1631. Preload("HisPrescriptionProject", 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("HisProject").Preload("GoodInfo", "status=1")
  1633. }).
  1634. Preload("HisPrescriptionTeamProject", func(db *gorm.DB) *gorm.DB {
  1635. 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")
  1636. }).
  1637. Where("status = 1 AND user_org_id = ?", orgID)
  1638. if scheduleDate != 0 {
  1639. db = db.Where("schedule_date = ?", scheduleDate)
  1640. }
  1641. err = db.Find(&vms).Error
  1642. }
  1643. return vms, err
  1644. }
  1645. func MobileCreateDialysisOrder(orgID int64, patientID int64, order *models.DialysisOrder) error {
  1646. now := time.Now()
  1647. tx := writeDb.Begin()
  1648. if createOrderErr := tx.Model(&models.DialysisOrder{}).Create(order).Error; createOrderErr != nil {
  1649. tx.Rollback()
  1650. return createOrderErr
  1651. }
  1652. // 更新透析记录 ID
  1653. // 透析处方
  1654. 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 {
  1655. tx.Rollback()
  1656. return err
  1657. }
  1658. // 接诊评估
  1659. 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 {
  1660. tx.Rollback()
  1661. return err
  1662. }
  1663. // 透前评估
  1664. 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 {
  1665. tx.Rollback()
  1666. return err
  1667. }
  1668. // 临时医嘱
  1669. 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 {
  1670. tx.Rollback()
  1671. return err
  1672. }
  1673. // 双人核对
  1674. 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 {
  1675. tx.Rollback()
  1676. return err
  1677. }
  1678. // 透后评估
  1679. 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 {
  1680. tx.Rollback()
  1681. return err
  1682. }
  1683. // 治疗小结
  1684. 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 {
  1685. tx.Rollback()
  1686. return err
  1687. }
  1688. // 透析监测
  1689. 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 {
  1690. tx.Rollback()
  1691. return err
  1692. }
  1693. tx.Commit()
  1694. return nil
  1695. }
  1696. type MobileUrgentSchedulePatientVM struct {
  1697. ID int64 `gorm:"column:id" json:"id"`
  1698. Name string `gorm:"column:name" json:"name"`
  1699. DialysisNo string `gorm:"column:dialysis_no" json:"dialysis_no"`
  1700. }
  1701. func (MobileUrgentSchedulePatientVM) TableName() string {
  1702. return "xt_patients"
  1703. }
  1704. type MobileUrgentScheduleTreatmentModeVM struct {
  1705. ID int64 `gorm:"column:id" json:"id"`
  1706. Name string `gorm:"column:name" json:"name"`
  1707. }
  1708. func (MobileUrgentScheduleTreatmentModeVM) TableName() string {
  1709. return "xt_treatment_mode"
  1710. }
  1711. func MobileGetAllPatientsForUrgentSchedule(orgID int64, record_date int64) ([]*MobileUrgentSchedulePatientVM, error) {
  1712. var vms []*MobileUrgentSchedulePatientVM = make([]*MobileUrgentSchedulePatientVM, 0)
  1713. 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()
  1714. defer rows.Close()
  1715. if err != nil {
  1716. return nil, err
  1717. }
  1718. for rows.Next() {
  1719. var vm MobileUrgentSchedulePatientVM
  1720. readDb.ScanRows(rows, &vm)
  1721. vms = append(vms, &vm)
  1722. }
  1723. return vms, nil
  1724. }
  1725. func MobileGetAllTrearmentModesForUrgentSchedule() ([]*MobileUrgentScheduleTreatmentModeVM, error) {
  1726. var modes []*MobileUrgentScheduleTreatmentModeVM
  1727. err := readDb.Model(&MobileUrgentScheduleTreatmentModeVM{}).Where("status = 1").Find(&modes).Error
  1728. if err != nil {
  1729. return nil, err
  1730. }
  1731. return modes, nil
  1732. }
  1733. type MobileUrgentScheduleScheduleListVM struct {
  1734. ID int64 `gorm:"column:id" json:"id"`
  1735. // ZoneID int64 `gorm:"column:partition_id" json:"zone_id"`
  1736. DeviceNumberID int64 `gorm:"column:bed_id" json:"bed_id"`
  1737. PatientID int64 `gorm:"column:patient_id" json:"patient_id"`
  1738. ScheduleType int `gorm:"column:schedule_type" json:"schedule_type"`
  1739. DeviceNumber *models.DeviceNumber `gorm:"ForeignKey:DeviceNumberID" json:"device_number"`
  1740. // DeviceZone *models.DeviceZone `gorm:"ForeignKey:ZoneID" json:"device_zone"`
  1741. }
  1742. func (MobileUrgentScheduleScheduleListVM) TableName() string {
  1743. return "xt_schedule"
  1744. }
  1745. func MobileGetSchedulesForUrgentSchedule(orgID int64, scheduleDate int64, schedule_type int) (schedules []*MobileUrgentScheduleScheduleListVM, err error) {
  1746. db := readDb.
  1747. Model(&MobileUrgentScheduleScheduleListVM{}).
  1748. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  1749. Where("status = 1 AND user_org_id = ? AND schedule_date = ? AND schedule_type = ? ", orgID, scheduleDate, schedule_type)
  1750. err = db.Find(&schedules).Error
  1751. if err != nil {
  1752. return nil, err
  1753. }
  1754. return schedules, nil
  1755. }
  1756. func MobileGetOtherSchedulesForUrgentSchedule(orgID int64, scheduleDate int64, schedule_type int) ([]*MobileUrgentScheduleScheduleListVM, error) {
  1757. var vms []*MobileUrgentScheduleScheduleListVM = make([]*MobileUrgentScheduleScheduleListVM, 0)
  1758. 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()
  1759. defer rows.Close()
  1760. if err != nil {
  1761. return nil, err
  1762. }
  1763. for rows.Next() {
  1764. var vm MobileUrgentScheduleScheduleListVM
  1765. readDb.ScanRows(rows, &vm)
  1766. vms = append(vms, &vm)
  1767. }
  1768. return vms, nil
  1769. }
  1770. type MobileUrgentScheduleDeviceNumberVM struct {
  1771. ID int64 `gorm:"column:id" json:"id"`
  1772. Number string `gorm:"column:number" json:"number"`
  1773. ZoneID int64 `gorm:"column:zone_id" json:"zone_id"`
  1774. Zone *models.DeviceZone `gorm:"ForeignKey:ZoneID" json:"zone"`
  1775. }
  1776. func (MobileUrgentScheduleDeviceNumberVM) TableName() string {
  1777. return "xt_device_number"
  1778. }
  1779. func MobileGetAllDeviceNumbersForUrgentSchedule(orgID int64, record_date int64, schedule_type int) ([]*DeviceNumberViewModel, error) {
  1780. var vms []*DeviceNumberViewModel = make([]*DeviceNumberViewModel, 0)
  1781. 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()
  1782. defer rows.Close()
  1783. if err != nil {
  1784. return nil, err
  1785. }
  1786. for rows.Next() {
  1787. var vm DeviceNumberViewModel
  1788. readDb.ScanRows(rows, &vm)
  1789. vms = append(vms, &vm)
  1790. }
  1791. return vms, nil
  1792. //var deviceNumbers []*MobileUrgentScheduleDeviceNumberVM
  1793. //db := readDb.
  1794. // Model(&MobileUrgentScheduleDeviceNumberVM{}).
  1795. // Preload("Zone", "status = 1 AND org_id = ?", orgID).
  1796. // Where("status = 1 AND org_id = ?", orgID)
  1797. //err := db.Order("zone_id asc").Find(&deviceNumbers).Error
  1798. //if err != nil {
  1799. // return nil, err
  1800. //}
  1801. //return deviceNumbers, nil
  1802. }
  1803. func GetValidScheduleMonitorRecordCount() (int64, error) {
  1804. var total int64
  1805. err := readDb.Model(&models.MonitoringRecord{}).Where("status = 1 AND operate_time = 0 AND monitoring_date <> 0").Count(&total).Error
  1806. return total, err
  1807. }
  1808. func GetTop1000ValidScheduleMonitorRecord() ([]*models.MonitoringRecord, error) {
  1809. var monitors []*models.MonitoringRecord
  1810. err := readDb.Model(&models.MonitoringRecord{}).Where("status = 1 AND operate_time = 0 AND monitoring_date <> 0").Order("id asc").Limit(100).Find(&monitors).Error
  1811. if err != nil {
  1812. return nil, err
  1813. }
  1814. return monitors, nil
  1815. }
  1816. func BatchUpdateMonitors(monitors []*models.MonitoringRecord) error {
  1817. tx := writeDb.Begin()
  1818. for index := 0; index < len(monitors); index++ {
  1819. tx.Save(monitors[index])
  1820. }
  1821. return tx.Commit().Error
  1822. }
  1823. func ModifyStartDialysisOrder(order *models.DialysisOrder) error {
  1824. tx := writeDb.Begin()
  1825. updateTime := time.Now().Unix()
  1826. 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
  1827. if err != nil {
  1828. tx.Rollback()
  1829. return err
  1830. }
  1831. tx.Commit()
  1832. return err
  1833. }
  1834. func ModifyFinishDialysisOrder(order *models.DialysisOrder) error {
  1835. tx := writeDb.Begin()
  1836. updateTime := time.Now().Unix()
  1837. 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
  1838. if err != nil {
  1839. tx.Rollback()
  1840. return err
  1841. }
  1842. tx.Commit()
  1843. return err
  1844. }
  1845. func MobileGetLastDryWeight(orgID int64, patientID int64) (*models.SgjPatientDryweight, error) {
  1846. var record models.SgjPatientDryweight
  1847. err := readDb.Model(&models.SgjPatientDryweight{}).Where("patient_id = ? and user_org_id = ? and status = 1", patientID, orgID).Last(&record).Error
  1848. if err != nil {
  1849. if err == gorm.ErrRecordNotFound {
  1850. return nil, nil
  1851. } else {
  1852. return nil, err
  1853. }
  1854. }
  1855. return &record, nil
  1856. }
  1857. //func MobileGetLastDryWeight(orgID int64, patientID int64) (*models.SgjPatientDryweight, error) {
  1858. // var record models.SgjPatientDryweight
  1859. // redis := RedisClient()
  1860. // defer redis.Close()
  1861. //
  1862. // key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(patientID, 10) + ":last_dry_weight"
  1863. // last_dry_weight_str, _ := redis.Get(key).Result()
  1864. //
  1865. // if len(last_dry_weight_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1866. // err := readDb.Model(&models.SgjPatientDryweight{}).Where("patient_id = ? and user_org_id = ? and status = 1", patientID, orgID).Last(&record).Error
  1867. // if err != nil {
  1868. // if err == gorm.ErrRecordNotFound {
  1869. // if record.ID <= 0 {
  1870. // redis.Set(key, "null", time.Second*60*60*18)
  1871. // }
  1872. // return nil, nil
  1873. // } else {
  1874. // return nil, err
  1875. // }
  1876. // } else {
  1877. // if record.ID > 0 {
  1878. // //缓存数据
  1879. // last_dry_weight_str, err := json.Marshal(record)
  1880. // if err == nil {
  1881. // redis.Set(key, last_dry_weight_str, time.Second*60*60*18)
  1882. // return nil, err
  1883. // }
  1884. // } else {
  1885. // redis.Set(key, "null", time.Second*60*60*18)
  1886. // return nil, err
  1887. // }
  1888. // return &record, nil
  1889. // }
  1890. // } else { //缓存数据了数据,将redis缓存的json字符串转为map
  1891. // if last_dry_weight_str == "null" {
  1892. // return &record, nil
  1893. // } else {
  1894. // json.Unmarshal([]byte(last_dry_weight_str), &record)
  1895. // return &record, nil
  1896. // }
  1897. //
  1898. // }
  1899. //}
  1900. // 透析方案
  1901. func MobileGetDialysisSolutionByModeIdSix(orgID int64, patientID int64, mode_id int64) (*models.DialysisSolution, error) {
  1902. var record models.DialysisSolution
  1903. 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
  1904. if err != nil {
  1905. if err == gorm.ErrRecordNotFound {
  1906. return nil, nil
  1907. } else {
  1908. return nil, err
  1909. }
  1910. }
  1911. return &record, nil
  1912. }
  1913. // 透析方案
  1914. func MobileGetDialysisSolutionByModeId(orgID int64, patientID int64, mode_id int64) (*models.DialysisSolution, error) {
  1915. var record models.DialysisSolution
  1916. redis := RedisClient()
  1917. defer redis.Close()
  1918. // cur_date := time.Now().Format("2006-01-02")
  1919. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(patientID, 10) + ":" + strconv.FormatInt(mode_id, 10) + ":dialysis_solution"
  1920. dialysis_solution_str, _ := redis.Get(key).Result()
  1921. if len(dialysis_solution_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1922. 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
  1923. if err != nil {
  1924. if err == gorm.ErrRecordNotFound {
  1925. if record.ID <= 0 {
  1926. redis.Set(key, "null", time.Second*60*60*18)
  1927. }
  1928. return nil, nil
  1929. } else {
  1930. return nil, err
  1931. }
  1932. } else {
  1933. if record.ID > 0 {
  1934. //缓存数据
  1935. dialysis_solution_str, err := json.Marshal(record)
  1936. if err == nil {
  1937. redis.Set(key, dialysis_solution_str, time.Second*60*60*18)
  1938. }
  1939. } else {
  1940. redis.Set(key, "null", time.Second*60*60*18)
  1941. }
  1942. return &record, nil
  1943. }
  1944. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  1945. if dialysis_solution_str == "null" {
  1946. return &record, nil
  1947. } else {
  1948. json.Unmarshal([]byte(dialysis_solution_str), &record)
  1949. return &record, nil
  1950. }
  1951. }
  1952. }
  1953. // 透析处方
  1954. func MobileGetDialysisPrescribeByModeId(orgID int64, patientID int64, recordDate int64, mode_id int64) (*models.DialysisPrescription, error) {
  1955. var record models.DialysisPrescription
  1956. redis := RedisClient()
  1957. defer redis.Close()
  1958. // cur_date := time.Now().Format("2006-01-02")
  1959. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(patientID, 10) + ":" + strconv.FormatInt(recordDate, 10) + ":" + strconv.FormatInt(mode_id, 10) + ":dialysis_prescribe"
  1960. fmt.Println("key23322323232323", key)
  1961. dialysis_prescribe_str, _ := redis.Get(key).Result()
  1962. if len(dialysis_prescribe_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1963. 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
  1964. if err != nil {
  1965. if err == gorm.ErrRecordNotFound {
  1966. if record.ID <= 0 {
  1967. redis.Set(key, "null", time.Second*60*60*18)
  1968. }
  1969. return nil, nil
  1970. } else {
  1971. return nil, err
  1972. }
  1973. } else {
  1974. if record.ID > 0 {
  1975. //缓存数据
  1976. dialysis_prescribe_str, err := json.Marshal(record)
  1977. if err == nil {
  1978. redis.Set(key, dialysis_prescribe_str, time.Second*60*60*18)
  1979. }
  1980. } else {
  1981. redis.Set(key, "null", time.Second*60*60*18)
  1982. }
  1983. return &record, nil
  1984. }
  1985. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  1986. if dialysis_prescribe_str == "null" {
  1987. return &record, nil
  1988. } else {
  1989. json.Unmarshal([]byte(dialysis_prescribe_str), &record)
  1990. return &record, nil
  1991. }
  1992. }
  1993. }
  1994. func MobileGetDialysisPrescribeByModeIdSix(orgID int64, patientID int64, recordDate int64, mode_id int64) (*models.DialysisPrescription, error) {
  1995. var record models.DialysisPrescription
  1996. 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
  1997. if err != nil {
  1998. if err == gorm.ErrRecordNotFound {
  1999. return nil, nil
  2000. } else {
  2001. return nil, err
  2002. }
  2003. }
  2004. return &record, nil
  2005. }
  2006. func MobileGetDialysisPrescribeByModeIdOne(orgID int64, patientID int64, recordDate int64) (*models.DialysisPrescription, error) {
  2007. var record models.DialysisPrescription
  2008. err := readDb.Model(&models.DialysisPrescription{}).Where("patient_id = ? and user_org_id = ? and status = 1 and record_date = ?", patientID, orgID, recordDate).First(&record).Error
  2009. if err != nil {
  2010. if err == gorm.ErrRecordNotFound {
  2011. return nil, nil
  2012. } else {
  2013. return nil, err
  2014. }
  2015. }
  2016. return &record, nil
  2017. }
  2018. //func MobileGetDialysisPrescribeByModeIdOne(orgID int64, patientID int64, recordDate int64) (*models.DialysisPrescription, error) {
  2019. // var record models.DialysisPrescription
  2020. // redis := RedisClient()
  2021. // defer redis.Close()
  2022. //
  2023. // // cur_date := time.Now().Format("2006-01-02")
  2024. // key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(patientID, 10) + ":" + strconv.FormatInt(recordDate, 10) + ":dialysis_prescribe_by_mode"
  2025. // dialysis_prescribe_by_mode_str, _ := redis.Get(key).Result()
  2026. //
  2027. // if len(dialysis_prescribe_by_mode_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2028. // err := readDb.Model(&models.DialysisPrescription{}).Where("patient_id = ? and user_org_id = ? and status = 1 and record_date = ?", patientID, orgID, recordDate).First(&record).Error
  2029. // if err != nil {
  2030. // if err == gorm.ErrRecordNotFound {
  2031. // if record.ID <= 0 {
  2032. // redis.Set(key, "null", time.Second*60*60*18)
  2033. // }
  2034. // return nil, nil
  2035. // } else {
  2036. // return nil, err
  2037. // }
  2038. // } else {
  2039. //
  2040. // if record.ID > 0 {
  2041. // //缓存数据
  2042. // dialysis_prescribe_by_mode_str, err := json.Marshal(record)
  2043. //
  2044. // if err == nil {
  2045. // redis.Set(key, dialysis_prescribe_by_mode_str, time.Second*60*60*18)
  2046. // return nil, err
  2047. // }
  2048. // } else {
  2049. // redis.Set(key, "null", time.Second*60*60*18)
  2050. // return nil, err
  2051. // }
  2052. // return &record, nil
  2053. // }
  2054. // } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2055. // if dialysis_prescribe_by_mode_str == "null" {
  2056. // json.Unmarshal([]byte(dialysis_prescribe_by_mode_str), &record)
  2057. // return &record, nil
  2058. // } else {
  2059. // json.Unmarshal([]byte(dialysis_prescribe_by_mode_str), &record)
  2060. // return &record, nil
  2061. // }
  2062. //
  2063. // }
  2064. //}
  2065. func MobileGetLastDialysisPrescribe(orgID int64, patientID int64) (*models.DialysisPrescription, error) {
  2066. var record models.DialysisPrescription
  2067. err := readDb.Model(&models.DialysisPrescription{}).Where("patient_id = ? and user_org_id = ? and status = 1 ", patientID, orgID).Last(&record).Error
  2068. if err != nil {
  2069. if err == gorm.ErrRecordNotFound {
  2070. return nil, nil
  2071. } else {
  2072. return nil, err
  2073. }
  2074. }
  2075. return &record, nil
  2076. }
  2077. func MobileGetLastDialysisPrescribeByModeIdSix(orgID int64, patientID int64, mode_id int64) (*models.DialysisPrescription, error) {
  2078. var record models.DialysisPrescription
  2079. 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
  2080. if err != nil {
  2081. if err == gorm.ErrRecordNotFound {
  2082. return nil, nil
  2083. } else {
  2084. return nil, err
  2085. }
  2086. }
  2087. return &record, nil
  2088. }
  2089. func MobileGetLastDialysisPrescribeByModeId(orgID int64, patientID int64, mode_id int64) (*models.DialysisPrescription, error) {
  2090. var record models.DialysisPrescription
  2091. redis := RedisClient()
  2092. defer redis.Close()
  2093. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(patientID, 10) + ":" + strconv.FormatInt(mode_id, 10) + ":dialysis_prescribe_by_mode"
  2094. dialysis_prescribe_by_mode_id, _ := redis.Get(key).Result()
  2095. if len(dialysis_prescribe_by_mode_id) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2096. 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
  2097. if err != nil {
  2098. if err == gorm.ErrRecordNotFound {
  2099. if record.ID <= 0 {
  2100. redis.Set(key, "null", time.Second*60*60*18)
  2101. }
  2102. return nil, nil
  2103. } else {
  2104. return nil, err
  2105. }
  2106. } else {
  2107. if record.ID > 0 {
  2108. //缓存数据
  2109. dialysis_prescribe_by_mode_str, err := json.Marshal(record)
  2110. if err == nil {
  2111. redis.Set(key, dialysis_prescribe_by_mode_str, time.Second*60*60*18)
  2112. return nil, err
  2113. }
  2114. } else {
  2115. redis.Set(key, "null", time.Second*60*60*18)
  2116. return nil, err
  2117. }
  2118. return &record, nil
  2119. }
  2120. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2121. if dialysis_prescribe_by_mode_id == "null" {
  2122. return &record, nil
  2123. } else {
  2124. json.Unmarshal([]byte(dialysis_prescribe_by_mode_id), &record)
  2125. return &record, nil
  2126. }
  2127. }
  2128. }
  2129. func MobileGetLastDialysisPrescribeByModeIdOne(orgID int64, patientID int64, mode_id int64) (models.DialysisPrescription, error) {
  2130. var record models.DialysisPrescription
  2131. 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
  2132. return record, err
  2133. }
  2134. func GetAllAvaildDeviceNumbers(orgID int64, record_date int64, schedule_type int) ([]*DeviceNumberViewModel, error) {
  2135. var vms []*DeviceNumberViewModel = make([]*DeviceNumberViewModel, 0)
  2136. 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()
  2137. defer rows.Close()
  2138. if err != nil {
  2139. return nil, err
  2140. }
  2141. for rows.Next() {
  2142. var vm DeviceNumberViewModel
  2143. readDb.ScanRows(rows, &vm)
  2144. vms = append(vms, &vm)
  2145. }
  2146. return vms, nil
  2147. }
  2148. // 获取 maxDate 之前一次的透前评估记录
  2149. func GetLastTimePredialysisEvaluation(orgID int64, patientID int64, maxDate int64) (*models.PredialysisEvaluation, error) {
  2150. var record models.PredialysisEvaluation
  2151. 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
  2152. if err != nil {
  2153. if err == gorm.ErrRecordNotFound {
  2154. return nil, nil
  2155. } else {
  2156. return nil, err
  2157. }
  2158. }
  2159. return &record, nil
  2160. }
  2161. func GetLastTimeOrder(orgID int64, patientID int64, maxDate int64) (*models.DialysisOrder, error) {
  2162. var record models.DialysisOrder
  2163. 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
  2164. if err != nil {
  2165. if err == gorm.ErrRecordNotFound {
  2166. return nil, nil
  2167. } else {
  2168. return nil, err
  2169. }
  2170. }
  2171. return &record, nil
  2172. }
  2173. func GetLastMonitorRecord(orgID int64, patientID int64, beforeDate int64) (*models.MonitoringRecord, error) {
  2174. var record models.MonitoringRecord
  2175. 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
  2176. if err != nil {
  2177. if err == gorm.ErrRecordNotFound {
  2178. return nil, nil
  2179. } else {
  2180. return nil, err
  2181. }
  2182. }
  2183. return &record, nil
  2184. }
  2185. // 获取 maxDate 之前一次的透后评估记录
  2186. func GetLastTimeAssessmentAfterDislysis(orgID int64, patientID int64, maxDate int64) (*models.AssessmentAfterDislysis, error) {
  2187. var record models.AssessmentAfterDislysis
  2188. 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
  2189. if err != nil {
  2190. if err == gorm.ErrRecordNotFound {
  2191. return nil, nil
  2192. } else {
  2193. return nil, err
  2194. }
  2195. }
  2196. return &record, nil
  2197. }
  2198. // 透析处方
  2199. func GetDialysisPrescribe(orgID int64, patientID int64, recordDate int64) (*models.DialysisPrescription, error) {
  2200. var record models.DialysisPrescription
  2201. err := readDb.Model(&models.DialysisPrescription{}).Where("patient_id = ? and user_org_id = ? and status = 1 and record_date = ?", patientID, orgID, recordDate).First(&record).Error
  2202. if err != nil {
  2203. if err == gorm.ErrRecordNotFound {
  2204. return nil, nil
  2205. } else {
  2206. return nil, err
  2207. }
  2208. }
  2209. return &record, nil
  2210. }
  2211. // 透析方案
  2212. func GetDialysisSolution(orgID int64, patientID int64, mode_id int64) (*models.DialysisSolution, error) {
  2213. var record models.DialysisSolution
  2214. 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
  2215. if err != nil {
  2216. if err == gorm.ErrRecordNotFound {
  2217. return nil, nil
  2218. } else {
  2219. return nil, err
  2220. }
  2221. }
  2222. return &record, nil
  2223. }
  2224. func GetDialysisSolutionOne(orgID int64, mode_id int64) (*models.DialysisSolution, error) {
  2225. var record models.DialysisSolution
  2226. err := readDb.Model(&models.DialysisSolution{}).Where("user_org_id = ? and status = 1 AND mode_id = ?", orgID, mode_id).Last(&record).Error
  2227. if err != nil {
  2228. if err == gorm.ErrRecordNotFound {
  2229. return nil, nil
  2230. } else {
  2231. return nil, err
  2232. }
  2233. }
  2234. return &record, nil
  2235. }
  2236. func GetLastDialysisPrescribeByModeId(orgID int64, patientID int64, mode_id int64) (*models.DialysisPrescription, error) {
  2237. var record models.DialysisPrescription
  2238. 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
  2239. if err != nil {
  2240. if err == gorm.ErrRecordNotFound {
  2241. return nil, nil
  2242. } else {
  2243. return nil, err
  2244. }
  2245. }
  2246. return &record, nil
  2247. }
  2248. func GetLastDryWeight(orgID int64, patientID int64) (*models.SgjPatientDryweight, error) {
  2249. var record models.SgjPatientDryweight
  2250. err := readDb.Model(&models.SgjPatientDryweight{}).Where("patient_id = ? and user_org_id = ? and status = 1", patientID, orgID).Last(&record).Error
  2251. if err != nil {
  2252. if err == gorm.ErrRecordNotFound {
  2253. return nil, nil
  2254. } else {
  2255. return nil, err
  2256. }
  2257. }
  2258. return &record, nil
  2259. }
  2260. func MobileGetSystemDialysisPrescribeByModeIdSix(orgID int64, mode_id int64) (*models.SystemPrescription, error) {
  2261. var record models.SystemPrescription
  2262. err := readDb.Model(&models.SystemPrescription{}).Where("user_org_id = ? and status = 1 AND mode_id = ?", orgID, mode_id).First(&record).Error
  2263. if err != nil {
  2264. if err == gorm.ErrRecordNotFound {
  2265. return nil, nil
  2266. } else {
  2267. return nil, err
  2268. }
  2269. }
  2270. return &record, nil
  2271. }
  2272. func MobileGetSystemDialysisPrescribeByModeId(orgID int64, mode_id int64) (*models.SystemPrescription, error) {
  2273. var record models.SystemPrescription
  2274. redis := RedisClient()
  2275. defer redis.Close()
  2276. // cur_date := time.Now().Format("2006-01-02")
  2277. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(mode_id, 10) + ":system_dialysis_prescribe"
  2278. system_dialysis_prescribe_str, _ := redis.Get(key).Result()
  2279. if len(system_dialysis_prescribe_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2280. err := readDb.Model(&models.SystemPrescription{}).Where("user_org_id = ? and status = 1 AND mode_id = ?", orgID, mode_id).Last(&record).Error
  2281. if err != nil {
  2282. if err == gorm.ErrRecordNotFound {
  2283. if record.ID <= 0 {
  2284. redis.Set(key, "null", time.Second*60*60*18)
  2285. }
  2286. return nil, nil
  2287. } else {
  2288. return nil, err
  2289. }
  2290. } else {
  2291. if record.ID > 0 {
  2292. //缓存数据
  2293. system_dialysis_prescribe_str, err := json.Marshal(record)
  2294. if err == nil {
  2295. redis.Set(key, system_dialysis_prescribe_str, time.Second*60*60*18)
  2296. }
  2297. } else {
  2298. redis.Set(key, "null", time.Second*60*60*18)
  2299. }
  2300. return &record, nil
  2301. }
  2302. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2303. if system_dialysis_prescribe_str == "null" {
  2304. return &record, nil
  2305. } else {
  2306. json.Unmarshal([]byte(system_dialysis_prescribe_str), &record)
  2307. return &record, nil
  2308. }
  2309. }
  2310. }
  2311. func MobileGetSystemDialysisPrescribeByModeIdOne(orgID int64, mode_id int64) (models.SystemPrescription, error) {
  2312. var record models.SystemPrescription
  2313. err := readDb.Model(&models.SystemPrescription{}).Where("user_org_id = ? and status = 1 AND mode_id = ?", orgID, mode_id).Last(&record).Error
  2314. return record, err
  2315. }
  2316. func GetSystemDialysisPrescribeByModeId(orgID int64, mode_id int64) (*models.SystemPrescription, error) {
  2317. var record models.SystemPrescription
  2318. err := readDb.Model(&models.SystemPrescription{}).Where("user_org_id = ? and status = 1 AND mode_id = ?", orgID, mode_id).Last(&record).Error
  2319. if err != nil {
  2320. if err == gorm.ErrRecordNotFound {
  2321. return nil, nil
  2322. } else {
  2323. return nil, err
  2324. }
  2325. }
  2326. return &record, nil
  2327. }
  2328. func GetDialysisOrderCount(orgID int64, patient_id int64, recordDate int64) (count int64, err error) {
  2329. 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
  2330. return
  2331. }
  2332. func GetDialysisOrderCountOne(orgID int64, patient_id int64, recordDate int64) (count int64, err error) {
  2333. 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
  2334. return
  2335. }
  2336. func GetFirstDateOfMonth(d time.Time) time.Time {
  2337. d = d.AddDate(0, 0, -d.Day()+1)
  2338. return GetZeroTime(d)
  2339. }
  2340. func GetZeroTime(d time.Time) time.Time {
  2341. return time.Date(d.Year(), d.Month(), d.Day(), 0, 0, 0, 0, d.Location())
  2342. }
  2343. func GetDialysisCountByPatientId(startime int64, endtime int64, patientid int64, orgid int64) (order []*models.BloodDialysisOrderCount, err error) {
  2344. db := XTReadDB().Table("xt_dialysis_order as o")
  2345. 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
  2346. return order, err
  2347. }
  2348. func FindConsumablesByDate(orgID int64, patient_id int64, recordDate int64) (consumables []*models.DialysisBeforePrepare, err error) {
  2349. 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
  2350. return
  2351. }
  2352. func FindConsumablesByDateTwo(orgID int64, patient_id int64, recordDate int64) (consumables []*models.DialysisBeforePrepare, err error) {
  2353. 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
  2354. return
  2355. }
  2356. func FindConsumablesByDateThree(orgID int64, patient_id int64, recordDate int64) (consumables []*models.BloodAutomaticReduceDetail, err error) {
  2357. 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
  2358. return
  2359. }
  2360. //func FindLastConsumables(orgID int64, patient_id int64, recordDate int64) (consumables []*models.DialysisBeforePrepare, err error){
  2361. // 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
  2362. // return
  2363. //}
  2364. //
  2365. func GetLastTimeConsumables(orgID int64, patientID int64, maxDate int64) (prepare []*models.DialysisBeforePrepare, err error) {
  2366. 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
  2367. return
  2368. }
  2369. type GoodsType struct {
  2370. ID int64 `gorm:"column:id" json:"id"`
  2371. TypeName string `gorm:"column:type_name" json:"type_name"`
  2372. Remark string `gorm:"column:remark" json:"remark"`
  2373. OrgId int64 `gorm:"column:org_id" json:"org_id"`
  2374. Status int64 `gorm:"column:status" json:"status"`
  2375. Type int64 `gorm:"column:type" json:"type"`
  2376. }
  2377. func (GoodsType) TableName() string {
  2378. return "xt_goods_type"
  2379. }
  2380. type VMGoodInfo struct {
  2381. ID int64 `gorm:"column:id" json:"id"`
  2382. SpecificationName string `gorm:"column:specification_name" json:"specification_name"`
  2383. GoodTypeId int64 `gorm:"column:good_type_id" json:"good_type_id"`
  2384. OrgId int64 `gorm:"column:org_id" json:"org_id"`
  2385. GoodName string `gorm:"column:good_name" json:"good_name" form:"good_name"`
  2386. GoodUnit int64 `gorm:"column:good_unit" json:"good_unit" form:"good_unit"`
  2387. Total float64 `gorm:"column:total" json:"total" form:"total"`
  2388. PackingUnit string `gorm:"column:packing_unit" json:"packing_unit" form:"packing_unit"`
  2389. }
  2390. func (VMGoodInfo) TableName() string {
  2391. return "xt_good_information"
  2392. }
  2393. type AutomaticReduceDetail struct {
  2394. ID int64 `gorm:"column:id" json:"id"`
  2395. PatientId int64 `gorm:"column:patient_id" json:"patient_id"`
  2396. Status int64 `gorm:"column:status" json:"status"`
  2397. RecordTime int64 `gorm:"column:record_time" json:"record_time"`
  2398. OrgId int64 `gorm:"column:org_id" json:"org_id"`
  2399. GoodId int64 `gorm:"column:good_id" json:"good_id"`
  2400. GoodTypeId int64 `gorm:"column:good_type_id" json:"good_type_id"`
  2401. VMGoodInfo VMGoodInfo `gorm:"ForeignKey:GoodId;AssociationForeignKey:ID" json:"info"`
  2402. GoodsType GoodsType `gorm:"ForeignKey:GoodTypeId;AssociationForeignKey:ID" json:"type"`
  2403. Count int64 `gorm:"column:count" json:"count"`
  2404. }
  2405. func (AutomaticReduceDetail) TableName() string {
  2406. return "xt_automatic_reduce_detail"
  2407. }
  2408. type DialysisBeforePrepare struct {
  2409. ID int64 `gorm:"column:id" json:"id" form:"id"`
  2410. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id" form:"user_org_id"`
  2411. PatientId int64 `gorm:"column:patient_id" json:"patient_id" form:"patient_id"`
  2412. RecordDate int64 `gorm:"column:record_date" json:"record_date" form:"record_date"`
  2413. GoodId int64 `gorm:"column:good_id" json:"good_id" form:"good_id"`
  2414. GoodTypeId int64 `gorm:"column:good_type_id" json:"good_type_id" form:"good_type_id"`
  2415. Count int64 `gorm:"column:count" json:"count" form:"count"`
  2416. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  2417. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  2418. Creater int64 `gorm:"column:creater" json:"creater" form:"creater"`
  2419. Modifier int64 `gorm:"column:modifier" json:"modifier" form:"modifier"`
  2420. VMGoodInfo VMGoodInfo `gorm:"ForeignKey:GoodId;AssociationForeignKey:ID" json:"info"`
  2421. GoodsType GoodsType `gorm:"ForeignKey:GoodTypeId;AssociationForeignKey:ID" json:"type"`
  2422. }
  2423. func (DialysisBeforePrepare) TableName() string {
  2424. return "dialysis_before_prepare"
  2425. }
  2426. type MDialysisGoodsVM struct {
  2427. ID int64 `gorm:"column:id" json:"id"`
  2428. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id"`
  2429. PartitionId int64 `gorm:"column:partition_id" json:"partition_id"`
  2430. BedId int64 `gorm:"column:bed_id" json:"bed_id"`
  2431. PatientId int64 `gorm:"column:patient_id" json:"patient_id"`
  2432. ScheduleDate int64 `gorm:"column:schedule_date" json:"schedule_date"`
  2433. ScheduleType int64 `gorm:"column:schedule_type" json:"schedule_type"`
  2434. ScheduleWeek int64 `gorm:"column:schedule_week" json:"schedule_week"`
  2435. ModeId int64 `gorm:"column:mode_id" json:"mode_id"`
  2436. Status int64 `gorm:"column:status" json:"status"`
  2437. DeviceNumber *MDeviceNumberVM `gorm:"ForeignKey:BedId" json:"device_number"`
  2438. SchedualPatient *MSchedualPatientVMList `gorm:"ForeignKey:PatientId" json:"patient"`
  2439. AutomaticReduceDetail []*AutomaticReduceDetail `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"good_user"`
  2440. LastAutomaticReduceDetail []*AutomaticReduceDetail `gorm:"-" json:"last_good_user"`
  2441. DialysisBeforePrepare []*DialysisBeforePrepare `gorm:"ForeignKey:PatientId,ScheduleDate;AssociationForeignKey:PatientId,RecordDate" json:"good_user_detail"`
  2442. LastDialysisBeforePrepare []*DialysisBeforePrepare `gorm:"-" json:"last_good_user_detail"`
  2443. Project []*models.HisPrescriptionProject `gorm:"-" json:"project"`
  2444. //WarehouseOutInfo []*models.WarehouseOutInfoSeven `gorm:"ForeignKey:PatientId,ScheduleDate;AssociationForeignKey:PatientId,SysRecordTime" json:"ware_house_out_info"`
  2445. }
  2446. func (MDialysisGoodsVM) TableName() string {
  2447. return "xt_schedule"
  2448. }
  2449. type VMWarehouseOutInfo struct {
  2450. ID int64 `gorm:"column:id" json:"id"`
  2451. WarehouseOutId int64 `gorm:"column:warehouse_out_id" json:"warehouse_out_id"`
  2452. GoodId int64 `gorm:"column:good_id" json:"good_id"`
  2453. GoodTypeId int64 `gorm:"column:good_type_id" json:"good_type_id"`
  2454. Count int64 `gorm:"column:count" json:"count"`
  2455. Price float64 `gorm:"column:price" json:"price"`
  2456. TotalPrice float64 `gorm:"column:total_price" json:"total_price"`
  2457. Status int64 `gorm:"column:status" json:"status"`
  2458. OrgId int64 `gorm:"column:org_id" json:"org_id"`
  2459. WarehouseOutOrderNumber string `gorm:"column:warehouse_out_order_number" json:"warehouse_out_order_number"`
  2460. GoodInfo VMGoodInfo `gorm:"ForeignKey:GoodId;AssociationForeignKey:ID" json:"good_info"`
  2461. GoodsType GoodsType `gorm:"ForeignKey:GoodTypeId;AssociationForeignKey:ID" json:"good_type"`
  2462. IsSys int64 `gorm:"column:is_sys" json:"is_sys"`
  2463. SysRecordTime int64 `gorm:"column:sys_record_time" json:"sys_record_time"`
  2464. Mtime int64 `gorm:"column:mtime" json:"mtime"`
  2465. Ctime int64 `gorm:"column:ctime" json:"ctime"`
  2466. }
  2467. func (VMWarehouseOutInfo) TableName() string {
  2468. return "xt_warehouse_out_info"
  2469. }
  2470. type DialysisGoodsDate struct {
  2471. RecordDate int64
  2472. }
  2473. type DialysisGoodsDetailDate struct {
  2474. RecordTime int64
  2475. }
  2476. 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) {
  2477. var vms []*MDialysisGoodsVM
  2478. var total int64
  2479. db := readDb.
  2480. Table("xt_schedule as sch").
  2481. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  2482. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  2483. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  2484. Preload("DialysisBeforePrepare", func(db *gorm.DB) *gorm.DB {
  2485. 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)
  2486. }).
  2487. Preload("AutomaticReduceDetail", func(db *gorm.DB) *gorm.DB {
  2488. 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")
  2489. }).Where("sch.status = 1 AND sch.user_org_id = ?", orgID)
  2490. if scheduleDate != 0 {
  2491. db = db.Where("schedule_date = ?", scheduleDate)
  2492. }
  2493. if schedule_type != 0 {
  2494. db = db.Where("schedule_type = ?", schedule_type)
  2495. }
  2496. if partition_id != 0 {
  2497. db = db.Where("partition_id = ?", partition_id)
  2498. }
  2499. if patient_id != 0 {
  2500. db = db.Where("patient_id = ?", patient_id)
  2501. }
  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 GetAllWarehouseOutSumList(patient_id int64, orgID int64, record_time int64) (info []*models.WarehouseOutInfoSeven, err error) {
  2522. err = XTReadDB().Where("patient_id = ? and org_id = ? and sys_record_time = ? and status = 1", patient_id, orgID, record_time).Find(&info).Error
  2523. return info, err
  2524. }
  2525. func MobileGetGoodsStatistics(orgID int64, start_time int64, end_time int64) (list []*models.StockInfo, err error) {
  2526. db := readDb.Model(&models.StockInfo{})
  2527. db = db.Where("xt_good_information.org_id = ? AND xt_good_information.status = 1", orgID)
  2528. 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")
  2529. db = db.Preload("QueryWarehouseOutInfo", func(db *gorm.DB) *gorm.DB {
  2530. 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)
  2531. })
  2532. db = db.Preload("GoodsType", "org_id = ? AND status = 1", orgID)
  2533. err = db.Order("ctime desc").Find(&list).Error
  2534. return
  2535. }
  2536. 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) {
  2537. var vms []*MDialysisGoodsVM
  2538. var total int64
  2539. db := readDb.
  2540. Model(&models.Schedule{}).
  2541. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  2542. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  2543. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  2544. Preload("DialysisBeforePrepare", func(db *gorm.DB) *gorm.DB {
  2545. 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)
  2546. }).Preload("AutomaticReduceDetail", func(db *gorm.DB) *gorm.DB {
  2547. 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)
  2548. }).Where("xt_schedule.status = 1 AND xt_schedule.user_org_id = ?", orgID)
  2549. if scheduleDate != 0 {
  2550. db = db.Where("schedule_date = ?", scheduleDate)
  2551. }
  2552. if patient_id != 0 {
  2553. db = db.Where("patient_id = ?", patient_id)
  2554. }
  2555. if len(keywords) != 0 {
  2556. keywords = "%" + keywords + "%"
  2557. 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)
  2558. if schedule_type != 0 {
  2559. db = db.Where("schedule_type = ?", schedule_type)
  2560. }
  2561. if partition_id != 0 {
  2562. db = db.Where("partition_id = ?", partition_id)
  2563. }
  2564. if good_type == 1 {
  2565. db = db.Where("patient_id in(?)", ids)
  2566. }
  2567. if good_type == 2 {
  2568. if len(ids) > 0 {
  2569. db = db.Where("patient_id not in(?)", ids)
  2570. }
  2571. }
  2572. } else {
  2573. if schedule_type != 0 {
  2574. db = db.Where("schedule_type = ?", schedule_type)
  2575. }
  2576. if partition_id != 0 {
  2577. db = db.Where("partition_id = ?", partition_id)
  2578. }
  2579. if good_type == 1 {
  2580. db = db.Where("patient_id in(?)", ids)
  2581. }
  2582. if good_type == 2 {
  2583. if len(ids) > 0 {
  2584. db = db.Where("patient_id not in(?)", ids)
  2585. }
  2586. }
  2587. db = db.Count(&total)
  2588. offset := (page - 1) * limit
  2589. db = db.Offset(offset).Limit(limit)
  2590. }
  2591. err := db.Find(&vms).Error
  2592. return vms, err, total
  2593. }
  2594. func GetLastDialysisGoodsTwo(patient_id int64, orgID int64, record_time int64) (goodUser []*AutomaticReduceDetail, err error) {
  2595. var Id []*DialysisGoodsDetailDate
  2596. 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
  2597. if len(Id) > 0 {
  2598. 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
  2599. }
  2600. return
  2601. }
  2602. func GetLastDialysisBeforePrepareTwo(patient_id int64, orgID int64, record_time int64) (goodUser []*DialysisBeforePrepare, err error) {
  2603. var Id []*DialysisGoodsDate
  2604. 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
  2605. if len(Id) > 0 {
  2606. 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
  2607. }
  2608. return
  2609. }
  2610. func GetAssessmentBefor(orgid int64, patientid int64, recorddate int64) (*models.PredialysisEvaluation, error) {
  2611. evaluation := models.PredialysisEvaluation{}
  2612. err := XTReadDB().Model(&evaluation).Where("user_org_id = ? and patient_id = ? and assessment_date = ? and status = 1", orgid, patientid, recorddate).Find(&evaluation).Error
  2613. if err == gorm.ErrRecordNotFound {
  2614. return nil, err
  2615. }
  2616. if err != nil {
  2617. return nil, err
  2618. }
  2619. return &evaluation, nil
  2620. }
  2621. func GetAllHisDoctorAdvice(orgid int64, patientid int64, recorddate int64) (his []*models.HisDoctorAdviceInfo, err error) {
  2622. redis := RedisClient()
  2623. defer redis.Close()
  2624. key := strconv.FormatInt(orgid, 10) + ":" + strconv.FormatInt(patientid, 10) + ":" + strconv.FormatInt(recorddate, 10) + ":his_doctor_advice"
  2625. his_doctor_advice_str, _ := redis.Get(key).Result()
  2626. redis.Set(key, "", time.Second)
  2627. if len(his_doctor_advice_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2628. err = readDb.Model(&models.HisDoctorAdviceInfo{}).Where("patient_id = ? AND user_org_id = ? AND status=1 AND record_date = ?", patientid, orgid, recorddate).Order("created_time asc").Find(&his).Error
  2629. if err != nil {
  2630. if err == gorm.ErrRecordNotFound {
  2631. if len(his) <= 0 {
  2632. redis.Set(key, "null", time.Second*60*60*18)
  2633. }
  2634. return his, nil
  2635. } else {
  2636. return his, err
  2637. }
  2638. } else {
  2639. if len(his) > 0 {
  2640. //缓存数据
  2641. his_doctor_advice_str, err := json.Marshal(his)
  2642. if err == nil {
  2643. redis.Set(key, his_doctor_advice_str, time.Second*60*60*18)
  2644. }
  2645. } else {
  2646. redis.Set(key, "null", time.Second*60*60*18)
  2647. }
  2648. return his, nil
  2649. }
  2650. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2651. json.Unmarshal([]byte(his_doctor_advice_str), &his)
  2652. return his, nil
  2653. }
  2654. }
  2655. func GetLastDialysisPrescriptionByPatientId(orgid int64, patientid int64, recorddate int64) (*models.DialysisPrescription, error) {
  2656. prescription := models.DialysisPrescription{}
  2657. err := readDb.Model(&models.DialysisPrescription{}).Where("user_org_id = ? and patient_id = ? and record_date = ? and status = 1", orgid, patientid, recorddate).Find(&prescription).Error
  2658. if err == gorm.ErrRecordNotFound {
  2659. return nil, err
  2660. }
  2661. if err != nil {
  2662. return nil, err
  2663. }
  2664. return &prescription, nil
  2665. }
  2666. func GetLastDialysisPrescriptionByPatientIdTwo(orgid int64, patientid int64, recorddate int64) (models.DialysisPrescription, error) {
  2667. prescription := models.DialysisPrescription{}
  2668. err := readDb.Model(&models.DialysisPrescription{}).Where("user_org_id = ? and patient_id = ? and record_date = ? and status = 1", orgid, patientid, recorddate).Find(&prescription).Error
  2669. return prescription, err
  2670. }
  2671. type HisMScheduleDoctorAdviceVM struct {
  2672. ID int64 `gorm:"column:id" json:"id"`
  2673. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id"`
  2674. PartitionId int64 `gorm:"column:partition_id" json:"partition_id"`
  2675. BedId int64 `gorm:"column:bed_id" json:"bed_id"`
  2676. PatientId int64 `gorm:"column:patient_id" json:"patient_id"`
  2677. ScheduleDate int64 `gorm:"column:schedule_date" json:"schedule_date"`
  2678. ScheduleType int64 `gorm:"column:schedule_type" json:"schedule_type"`
  2679. ModeId int64 `gorm:"column:mode_id" json:"mode_id"`
  2680. Status int64 `gorm:"column:status" json:"status"`
  2681. DialysisOrder *MDialysisOrderVM `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"dialysis_order"`
  2682. SchedualPatient *MSchedualPatientVM `gorm:"ForeignKey:PatientId;AssociationForeignKey:ID" json:"patient"`
  2683. DeviceNumber *MDeviceNumberVM `gorm:"ForeignKey:BedId" json:"device_number"`
  2684. Prescription *models.DialysisPrescriptionList `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"prescription"`
  2685. HisDoctorAdviceInfo []*models.HisDoctorAdviceInfo `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"doctor_advice"`
  2686. HisPrescriptionProject []*models.HisPrescriptionProject `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"project"`
  2687. }
  2688. func (HisMScheduleDoctorAdviceVM) TableName() string {
  2689. return "xt_schedule"
  2690. }
  2691. type HisMScheduleDoctorAdviceVMOne struct {
  2692. ID int64 `gorm:"column:id" json:"id"`
  2693. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id"`
  2694. PartitionId int64 `gorm:"column:partition_id" json:"partition_id"`
  2695. BedId int64 `gorm:"column:bed_id" json:"bed_id"`
  2696. PatientId int64 `gorm:"column:patient_id" json:"patient_id"`
  2697. ScheduleDate int64 `gorm:"column:schedule_date" json:"schedule_date"`
  2698. ScheduleType int64 `gorm:"column:schedule_type" json:"schedule_type"`
  2699. ModeId int64 `gorm:"column:mode_id" json:"mode_id"`
  2700. Status int64 `gorm:"column:status" json:"status"`
  2701. DialysisOrder *MDialysisOrderVM `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"dialysis_order"`
  2702. SchedualPatient *MSchedualPatientVM `gorm:"ForeignKey:PatientId;AssociationForeignKey:ID" json:"patient"`
  2703. DeviceNumber *MDeviceNumberVM `gorm:"ForeignKey:BedId" json:"device_number"`
  2704. Prescription *models.DialysisPrescriptionList `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"prescription"`
  2705. HisDoctorAdviceInfo []*models.HisDoctorAdviceInfo `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"doctor_advice"`
  2706. HisPrescriptionProject []*models.HisPrescriptionProject `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"project"`
  2707. }
  2708. func (HisMScheduleDoctorAdviceVMOne) TableName() string {
  2709. return "xt_schedule"
  2710. }
  2711. type HisMScheduleProjectVM struct {
  2712. ID int64 `gorm:"column:id" json:"id"`
  2713. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id"`
  2714. PartitionId int64 `gorm:"column:partition_id" json:"partition_id"`
  2715. BedId int64 `gorm:"column:bed_id" json:"bed_id"`
  2716. PatientId int64 `gorm:"column:patient_id" json:"patient_id"`
  2717. ScheduleDate int64 `gorm:"column:schedule_date" json:"schedule_date"`
  2718. ScheduleType int64 `gorm:"column:schedule_type" json:"schedule_type"`
  2719. ModeId int64 `gorm:"column:mode_id" json:"mode_id"`
  2720. Status int64 `gorm:"column:status" json:"status"`
  2721. DialysisOrder *MDialysisOrderVM `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"dialysis_order"`
  2722. SchedualPatient *MSchedualPatientVM `gorm:"ForeignKey:PatientId;AssociationForeignKey:ID" json:"patient"`
  2723. DeviceNumber *MDeviceNumberVM `gorm:"ForeignKey:BedId" json:"device_number"`
  2724. Prescription *models.DialysisPrescriptionList `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"prescription"`
  2725. HisPrescriptionProject []*models.HisPrescriptionProject `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"project"`
  2726. HisPrescriptionTeamProject []*models.HisPrescriptionProject `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"team_project"`
  2727. }
  2728. func (HisMScheduleProjectVM) TableName() string {
  2729. return "xt_schedule"
  2730. }
  2731. func GetHisDoctorConfig(orgid int64) (models.XtHisConfig, error) {
  2732. config := models.XtHisConfig{}
  2733. err := XTReadDB().Model(&config).Where("user_org_id = ? and status =1", orgid).Find(&config).Error
  2734. return config, err
  2735. }
  2736. func GetHisDoctorPatientById(orgid int64, patientid int64, recordtime int64) (advice []*models.HisDoctorAdviceInfo, err error) {
  2737. db := XTReadDB().Table("his_doctor_advice_info as x").Where("x.status = 1")
  2738. if orgid > 0 {
  2739. db = db.Where("x.user_org_id = ?", orgid)
  2740. }
  2741. if patientid > 0 {
  2742. db = db.Where("x.patient_id =?", patientid)
  2743. }
  2744. if recordtime > 0 {
  2745. db = db.Where("x.advice_date = ?", recordtime)
  2746. }
  2747. 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
  2748. return advice, err
  2749. }
  2750. func GetDoctorAdviceCount(startime int64, endtime int64, deliveway string, orgid int64) (advice []*models.BloodDoctorAdvice, err error) {
  2751. db := XTReadDB().Table("xt_doctor_advice as x").Where("x.status = 1 and x.advice_type =2")
  2752. if startime > 0 {
  2753. db = db.Where("x.advice_date >= ?", startime)
  2754. }
  2755. if endtime > 0 {
  2756. db = db.Where("x.advice_date<=?", endtime)
  2757. }
  2758. if orgid > 0 {
  2759. db = db.Where("x.user_org_id = ?", orgid)
  2760. }
  2761. if len(deliveway) > 0 {
  2762. db = db.Where("x.delivery_way = ?", deliveway)
  2763. }
  2764. 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
  2765. return advice, err
  2766. }
  2767. func GetHisDoctorAdviceCount(startime int64, endtime int64, deliveway string, orgid int64) (advice []*models.BloodDoctorAdvice, err error) {
  2768. db := XTReadDB().Table("his_doctor_advice_info as x").Where("x.status = 1 and x.advice_type =2")
  2769. if startime > 0 {
  2770. db = db.Where("x.advice_date >= ?", startime)
  2771. }
  2772. if endtime > 0 {
  2773. db = db.Where("x.advice_date<=?", endtime)
  2774. }
  2775. if orgid > 0 {
  2776. db = db.Where("x.user_org_id = ?", orgid)
  2777. }
  2778. if len(deliveway) > 0 {
  2779. db = db.Where("x.delivery_way = ?", deliveway)
  2780. }
  2781. 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
  2782. return advice, err
  2783. }
  2784. 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) {
  2785. var vms []*MScheduleDoctorAdviceVM
  2786. adviceWhere := ""
  2787. adviceCondition := []interface{}{}
  2788. if adviceType == 0 {
  2789. if patientType == 0 {
  2790. if patient_id > 0 {
  2791. if execution_state > 0 {
  2792. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND (advice_type = 3 OR advice_type = 1) and patient_id = ? and execution_state= ?"
  2793. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, patient_id, execution_state)
  2794. } else {
  2795. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND (advice_type = 3 OR advice_type = 1) and patient_id = ?"
  2796. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, patient_id)
  2797. }
  2798. } else {
  2799. if execution_state > 0 {
  2800. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND (advice_type = 3 OR advice_type = 1) and execution_state= ?"
  2801. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, execution_state)
  2802. } else {
  2803. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND (advice_type = 3 OR advice_type = 1)"
  2804. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  2805. }
  2806. }
  2807. } else if patientType == 1 {
  2808. if patient_id > 0 {
  2809. if execution_state > 0 {
  2810. 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= ?"
  2811. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId, patient_id, execution_state)
  2812. } else {
  2813. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_doctor = ? AND(advice_type = 3 OR advice_type = 1) and patient_id = ?"
  2814. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId, patient_id)
  2815. }
  2816. } else {
  2817. if execution_state > 0 {
  2818. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_doctor = ? AND(advice_type = 3 OR advice_type = 1) and execution_state = ?"
  2819. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId, execution_state)
  2820. } else {
  2821. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_doctor = ? AND(advice_type = 3 OR advice_type = 1)"
  2822. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId)
  2823. }
  2824. }
  2825. } else if patientType == 2 {
  2826. if patient_id > 0 {
  2827. if execution_state > 0 {
  2828. 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 = ?"
  2829. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, patient_id, execution_state)
  2830. } else {
  2831. 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 = ?)"
  2832. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, patient_id)
  2833. }
  2834. } else {
  2835. if execution_state > 0 {
  2836. 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 = ?"
  2837. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, execution_state)
  2838. } else {
  2839. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND execution_staff = 0 AND(advice_type = 3 OR advice_type = 1)"
  2840. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  2841. }
  2842. }
  2843. }
  2844. } else if adviceType == 1 {
  2845. if patientType == 0 {
  2846. if patient_id > 0 {
  2847. if execution_state > 0 {
  2848. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 1 AND record_date = ? and patient_id = ? and execution_state = ?"
  2849. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, patient_id, execution_state)
  2850. } else {
  2851. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 1 AND record_date = ? and patient_id = ? "
  2852. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, patient_id)
  2853. }
  2854. } else {
  2855. if execution_state > 0 {
  2856. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 1 AND record_date = ? and execution_state = ?"
  2857. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, execution_state)
  2858. } else {
  2859. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 1 AND record_date = ? "
  2860. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  2861. }
  2862. }
  2863. } else if patientType == 1 {
  2864. if patient_id > 0 {
  2865. if execution_state > 0 {
  2866. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 1 AND record_date = ? AND advice_doctor = ? and patient_id = ? and execution_state = ?"
  2867. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId, patient_id, execution_state)
  2868. } else {
  2869. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 1 AND record_date = ? AND advice_doctor = ? and patient_id = ?"
  2870. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId, patient_id)
  2871. }
  2872. } else {
  2873. if execution_state > 0 {
  2874. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 1 AND record_date = ? AND advice_doctor = ? and execution_state=?"
  2875. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId, execution_state)
  2876. } else {
  2877. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 1 AND record_date = ? AND advice_doctor = ? "
  2878. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId)
  2879. }
  2880. }
  2881. } else if patientType == 2 {
  2882. if patient_id > 0 {
  2883. if execution_state > 0 {
  2884. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 1 AND record_date = ? AND execution_staff = 0 and patient_id = ? and execution_state = ?"
  2885. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, patient_id, execution_state)
  2886. } else {
  2887. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 1 AND record_date = ? AND execution_staff = 0 and patient_id = ?"
  2888. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, patient_id)
  2889. }
  2890. } else {
  2891. if execution_state > 0 {
  2892. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 1 AND record_date = ? AND execution_staff = 0 and execution_state = ?"
  2893. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, execution_state)
  2894. } else {
  2895. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 1 AND record_date = ? AND execution_staff = 0"
  2896. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  2897. }
  2898. }
  2899. }
  2900. } else if adviceType == 3 {
  2901. if patientType == 0 {
  2902. if patient_id > 0 {
  2903. if execution_state > 0 {
  2904. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 3 AND record_date = ? and patient_id = ? and execution_state = ?"
  2905. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, patient_id, execution_state)
  2906. } else {
  2907. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 3 AND record_date = ? and patient_id = ? "
  2908. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, patient_id)
  2909. }
  2910. } else {
  2911. if execution_state > 0 {
  2912. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 3 AND record_date = ? and execution_state =? "
  2913. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, execution_state)
  2914. } else {
  2915. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 3 AND record_date = ? "
  2916. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  2917. }
  2918. }
  2919. } else if patientType == 1 {
  2920. if patient_id > 0 {
  2921. if execution_state > 0 {
  2922. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 3 AND advice_doctor = ? and patient_id = ? and execution_state = ?"
  2923. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId, patient_id, execution_state)
  2924. } else {
  2925. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 3 AND advice_doctor = ? and patient_id = ?"
  2926. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId, patient_id)
  2927. }
  2928. } else {
  2929. if execution_state > 0 {
  2930. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 3 AND advice_doctor = ? and execution_state =? "
  2931. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId, execution_state)
  2932. } else {
  2933. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 3 AND advice_doctor = ? "
  2934. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId)
  2935. }
  2936. }
  2937. } else if patientType == 2 {
  2938. if patient_id > 0 {
  2939. if execution_state > 0 {
  2940. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 3 AND execution_staff = 0 and patient_id = ? and execution_state = ?"
  2941. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, patient_id, execution_state)
  2942. } else {
  2943. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 3 AND execution_staff = 0 and patient_id = ?"
  2944. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, patient_id)
  2945. }
  2946. } else {
  2947. if execution_state > 0 {
  2948. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 3 AND execution_staff = 0 and execution_state= ?"
  2949. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, execution_state)
  2950. } else {
  2951. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 3 AND execution_staff = 0"
  2952. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  2953. }
  2954. }
  2955. }
  2956. } else if adviceType == 2 && len(deliverWay) > 0 {
  2957. if patientType == 0 {
  2958. if patient_id > 0 {
  2959. if execution_state > 0 {
  2960. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 2 AND record_date = ? and delivery_way = ? and patient_id = ? and execution_state = ?"
  2961. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, deliverWay, patient_id, execution_state)
  2962. } else {
  2963. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 2 AND record_date = ? and delivery_way = ? and patient_id = ?"
  2964. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, deliverWay, patient_id)
  2965. }
  2966. } else {
  2967. if execution_state > 0 {
  2968. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 2 AND record_date = ? and delivery_way = ? and execution_state = ?"
  2969. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, deliverWay, execution_state)
  2970. } else {
  2971. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 2 AND record_date = ? and delivery_way = ?"
  2972. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, deliverWay)
  2973. }
  2974. }
  2975. } else if patientType == 1 {
  2976. if patient_id > 0 {
  2977. if execution_state > 0 {
  2978. 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 = ?"
  2979. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId, deliverWay, patient_id, execution_state)
  2980. } else {
  2981. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 2 AND advice_doctor = ? and delivery_way = ? and patient_id = ?"
  2982. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId, deliverWay, patient_id)
  2983. }
  2984. } else {
  2985. if execution_state > 0 {
  2986. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 2 AND advice_doctor = ? and delivery_way = ? and execution_state = ?"
  2987. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId, deliverWay, execution_state)
  2988. } else {
  2989. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 2 AND advice_doctor = ? and delivery_way = ? "
  2990. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId, deliverWay)
  2991. }
  2992. }
  2993. } else if patientType == 2 {
  2994. if patient_id > 0 {
  2995. if execution_state > 0 {
  2996. 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 = ?"
  2997. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, deliverWay, patient_id, execution_state)
  2998. } else {
  2999. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 2 AND execution_staff = 0 and delivery_way = ? and patient_id = ?"
  3000. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, deliverWay, patient_id)
  3001. }
  3002. } else {
  3003. if execution_state > 0 {
  3004. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 2 AND execution_staff = 0 and delivery_way = ? and execution_state = ?"
  3005. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, deliverWay, execution_state)
  3006. } else {
  3007. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 2 AND execution_staff = 0 and delivery_way = ?"
  3008. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, deliverWay)
  3009. }
  3010. }
  3011. }
  3012. } else if adviceType == 2 && len(deliverWay) <= 0 {
  3013. if patientType == 0 {
  3014. if patient_id > 0 {
  3015. if execution_state > 0 {
  3016. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 2 AND record_date = ? and patient_id = ? and execution_state = ?"
  3017. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, patient_id, execution_state)
  3018. } else {
  3019. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 2 AND record_date = ? and patient_id = ?"
  3020. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, patient_id)
  3021. }
  3022. } else {
  3023. if execution_state > 0 {
  3024. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 2 AND record_date = ? and execution_state = ?"
  3025. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, execution_state)
  3026. } else {
  3027. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 2 AND record_date = ?"
  3028. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  3029. }
  3030. }
  3031. } else if patientType == 1 {
  3032. if patient_id > 0 {
  3033. if execution_state > 0 {
  3034. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 2 AND advice_doctor = ? and patient_id = ? and execution_state = ?"
  3035. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId, patient_id, execution_state)
  3036. } else {
  3037. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 2 AND advice_doctor = ? and patient_id = ?"
  3038. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId, patient_id)
  3039. }
  3040. } else {
  3041. if execution_state > 0 {
  3042. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 2 AND advice_doctor = ? and execution_state = ?"
  3043. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId, execution_state)
  3044. } else {
  3045. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 2 AND advice_doctor = ?"
  3046. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId)
  3047. }
  3048. }
  3049. } else if patientType == 2 {
  3050. if patient_id > 0 {
  3051. if execution_state > 0 {
  3052. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 2 AND execution_staff = 0 and patient_id = ? and execution_state = ?"
  3053. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, patient_id, execution_state)
  3054. } else {
  3055. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 2 AND execution_staff = 0 and patient_id = ?"
  3056. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, patient_id)
  3057. }
  3058. } else {
  3059. if execution_state > 0 {
  3060. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 2 AND execution_staff = 0 and execution_state = ?"
  3061. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, execution_state)
  3062. } else {
  3063. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 2 AND execution_staff = 0"
  3064. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  3065. }
  3066. }
  3067. }
  3068. }
  3069. db := readDb.Table("xt_schedule")
  3070. if scheduleType > 0 {
  3071. db = db.Where("schedule_type = ?", scheduleType)
  3072. }
  3073. if partitonType > 0 {
  3074. db = db.Where("partition_id = ?", partitonType)
  3075. }
  3076. db = db.Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  3077. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  3078. return db.Where("status = 1 AND user_org_id = ?", orgID).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  3079. }).
  3080. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  3081. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  3082. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  3083. Preload("DialysisAssesmentBefor", "status =1 AND user_org_id = ? and assessment_date =?", orgID, scheduleDate).
  3084. Preload("DoctorAdvices", adviceCondition...).
  3085. Where("status = 1 AND user_org_id = ?", orgID)
  3086. if scheduleDate != 0 {
  3087. db = db.Where("schedule_date = ?", scheduleDate)
  3088. }
  3089. err := db.Find(&vms).Error
  3090. return vms, err
  3091. }
  3092. func GetHisDoctorAdvicesOne(orgID int64, scheduleDate int64, deliverWay string, scheduleType int64, partitionType int64, patient_id int64, execution_state int64) ([]*HisMScheduleDoctorAdviceVM, error) {
  3093. var vms []*HisMScheduleDoctorAdviceVM
  3094. if len(deliverWay) > 0 {
  3095. db := readDb.Table("xt_schedule")
  3096. if scheduleType > 0 {
  3097. db = db.Where("schedule_type = ?", scheduleType)
  3098. }
  3099. if partitionType > 0 {
  3100. db = db.Where("partition_id = ?", partitionType)
  3101. }
  3102. if patient_id > 0 {
  3103. if execution_state > 0 {
  3104. db = db.Preload("SchedualPatient", "status = 1 AND user_org_id = ? and id =?", orgID, patient_id).
  3105. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  3106. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  3107. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ? and patient_id = ?", orgID, scheduleDate, patient_id).
  3108. 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).
  3109. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  3110. return db.Where("status = 1 AND user_org_id = ? and patient_id = ?", orgID, patient_id).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  3111. }).
  3112. Where("status = 1 AND user_org_id = ?", orgID)
  3113. if scheduleDate != 0 {
  3114. db = db.Where("schedule_date = ?", scheduleDate)
  3115. }
  3116. err = db.Find(&vms).Error
  3117. } else {
  3118. db = db.Preload("SchedualPatient", "status = 1 AND user_org_id = ? and id =?", orgID, patient_id).
  3119. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  3120. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  3121. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ? and patient_id = ?", orgID, scheduleDate, patient_id).
  3122. Preload("HisDoctorAdviceInfo", "status = 1 AND user_org_id = ? AND advice_date = ? and delivery_way = ? and patient_id = ?", orgID, scheduleDate, deliverWay, patient_id).
  3123. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  3124. return db.Where("status = 1 AND user_org_id = ? and patient_id = ?", orgID, patient_id).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  3125. }).
  3126. Where("status = 1 AND user_org_id = ?", orgID)
  3127. if scheduleDate != 0 {
  3128. db = db.Where("schedule_date = ?", scheduleDate)
  3129. }
  3130. err = db.Find(&vms).Error
  3131. }
  3132. } else {
  3133. if execution_state > 0 {
  3134. db = db.Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  3135. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  3136. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  3137. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  3138. Preload("HisDoctorAdviceInfo", "status = 1 AND user_org_id = ? AND advice_date = ? and delivery_way = ? and execution_state = ?", orgID, scheduleDate, deliverWay, execution_state).
  3139. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  3140. return db.Where("status = 1 AND user_org_id = ?", orgID).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  3141. }).
  3142. Where("status = 1 AND user_org_id = ?", orgID)
  3143. if scheduleDate != 0 {
  3144. db = db.Where("schedule_date = ?", scheduleDate)
  3145. }
  3146. err = db.Find(&vms).Error
  3147. } else {
  3148. db = db.Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  3149. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  3150. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  3151. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  3152. Preload("HisDoctorAdviceInfo", "status = 1 AND user_org_id = ? AND advice_date = ? and delivery_way = ?", orgID, scheduleDate, deliverWay).
  3153. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  3154. return db.Where("status = 1 AND user_org_id = ?", orgID).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  3155. }).
  3156. Where("status = 1 AND user_org_id = ?", orgID)
  3157. if scheduleDate != 0 {
  3158. db = db.Where("schedule_date = ?", scheduleDate)
  3159. }
  3160. err = db.Find(&vms).Error
  3161. }
  3162. }
  3163. } else {
  3164. db := readDb.Table("xt_schedule")
  3165. if scheduleType > 0 {
  3166. db = db.Where("schedule_type = ?", scheduleType)
  3167. }
  3168. if partitionType > 0 {
  3169. db = db.Where("partition_id = ?", partitionType)
  3170. }
  3171. if patient_id > 0 {
  3172. if execution_state > 0 {
  3173. db = db.Preload("SchedualPatient", "status = 1 AND user_org_id = ? and id = ?", orgID, patient_id).
  3174. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  3175. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  3176. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ? and patient_id = ?", orgID, scheduleDate, patient_id).
  3177. Preload("HisDoctorAdviceInfo", "status = 1 AND user_org_id = ? AND advice_date = ? and patient_id = ? and execution_state = ?", orgID, scheduleDate, patient_id, execution_state).
  3178. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  3179. return db.Where("status = 1 AND user_org_id = ? and patient_id = ?", orgID, patient_id).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  3180. }).
  3181. Where("status = 1 AND user_org_id = ?", orgID)
  3182. if scheduleDate != 0 {
  3183. db = db.Where("schedule_date = ?", scheduleDate)
  3184. }
  3185. err = db.Find(&vms).Error
  3186. } else {
  3187. db = db.Preload("SchedualPatient", "status = 1 AND user_org_id = ? and id = ?", orgID, patient_id).
  3188. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  3189. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  3190. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ? and patient_id = ?", orgID, scheduleDate, patient_id).
  3191. Preload("HisDoctorAdviceInfo", "status = 1 AND user_org_id = ? AND advice_date = ? and patient_id = ?", orgID, scheduleDate, patient_id).
  3192. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  3193. return db.Where("status = 1 AND user_org_id = ? and patient_id = ?", orgID, patient_id).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  3194. }).
  3195. Where("status = 1 AND user_org_id = ?", orgID)
  3196. if scheduleDate != 0 {
  3197. db = db.Where("schedule_date = ?", scheduleDate)
  3198. }
  3199. err = db.Find(&vms).Error
  3200. }
  3201. } else {
  3202. if execution_state > 0 {
  3203. db = db.Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  3204. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  3205. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  3206. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  3207. Preload("HisDoctorAdviceInfo", "status = 1 AND user_org_id = ? AND advice_date = ? and execution_state = ?", orgID, scheduleDate, execution_state).
  3208. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  3209. return db.Where("status = 1 AND user_org_id = ?", orgID).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  3210. }).
  3211. Where("status = 1 AND user_org_id = ?", orgID)
  3212. if scheduleDate != 0 {
  3213. db = db.Where("schedule_date = ?", scheduleDate)
  3214. }
  3215. err = db.Find(&vms).Error
  3216. } else {
  3217. db = db.Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  3218. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  3219. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  3220. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  3221. Preload("HisDoctorAdviceInfo", "status = 1 AND user_org_id = ? AND advice_date = ?", orgID, scheduleDate).
  3222. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  3223. return db.Where("status = 1 AND user_org_id = ?", orgID).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  3224. }).
  3225. Where("status = 1 AND user_org_id = ?", orgID)
  3226. if scheduleDate != 0 {
  3227. db = db.Where("schedule_date = ?", scheduleDate)
  3228. }
  3229. err = db.Find(&vms).Error
  3230. }
  3231. }
  3232. }
  3233. return vms, err
  3234. }
  3235. func BatchDeleteMonitor(ids []string) (err error) {
  3236. if len(ids) == 1 {
  3237. err = XTWriteDB().Model(&models.MonitoringRecord{}).Where("id=?", ids[0]).Updates(map[string]interface{}{"status": 0, "updated_time": time.Now().Unix()}).Error
  3238. } else {
  3239. err = XTWriteDB().Model(models.MonitoringRecord{}).Where("id IN(?)", ids).Updates(map[string]interface{}{"status": 0, "updated_time": time.Now().Unix()}).Error
  3240. }
  3241. return
  3242. }
  3243. func GetPatientDialysisRecordList(patientid int64, startime int64, endtime int64) (order []*models.XtDialysisOrders, err error) {
  3244. db := XTReadDB().Table("xt_dialysis_order as x").Where("x.status = 1")
  3245. //table := XTReadDB().Table("xt_schedule as s")
  3246. //fmt.Println(table)
  3247. if patientid > 0 {
  3248. db = db.Where("x.patient_id = ?", patientid)
  3249. }
  3250. if startime > 0 {
  3251. db = db.Where("x.dialysis_date>=?", startime)
  3252. }
  3253. if endtime > 0 {
  3254. db = db.Where("x.dialysis_date <= ?", endtime)
  3255. }
  3256. 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
  3257. return order, err
  3258. }
  3259. func BatchDeleteAdvice(ids []string) (err error) {
  3260. if len(ids) == 1 {
  3261. err = XTWriteDB().Model(&models.DoctorAdvice{}).Where("id=?", ids[0]).Updates(map[string]interface{}{"status": 0, "updated_time": time.Now().Unix()}).Error
  3262. } else {
  3263. err = XTWriteDB().Model(models.DoctorAdvice{}).Where("id IN(?)", ids).Updates(map[string]interface{}{"status": 0, "updated_time": time.Now().Unix()}).Error
  3264. }
  3265. return
  3266. }
  3267. func BatchDeleteHisAdvice(ids []string) (err error) {
  3268. if len(ids) == 1 {
  3269. err = XTWriteDB().Model(&models.HisDoctorAdviceInfo{}).Where("id = ?", ids[0]).Updates(map[string]interface{}{"status": 0, "updated_time": time.Now().Unix()}).Error
  3270. } else {
  3271. err = XTWriteDB().Model(models.HisDoctorAdviceInfo{}).Where("id IN(?)", ids).Updates(map[string]interface{}{"status": 0, "updated_time": time.Now().Unix()}).Error
  3272. }
  3273. return
  3274. }
  3275. func UpdateAutoReduceDetail(good_id int64, count int64, record_time int64, patient_id int64) (models.AutomaticReduceDetail, error) {
  3276. detail := models.AutomaticReduceDetail{}
  3277. 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
  3278. return detail, err
  3279. }
  3280. func DeleteAutoReduceDetail(good_id int64, record_time int64, patient_id int64) error {
  3281. detail := models.AutomaticReduceDetail{}
  3282. 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
  3283. return err
  3284. }
  3285. func DeleteDialysisBeforOne(good_id int64, record_time int64, patient_id int64) error {
  3286. detail := models.DialysisBeforePrepare{}
  3287. 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
  3288. return err
  3289. }
  3290. func BatchAdviceCheck(ids []string, creator int64) error {
  3291. advice := models.XtDoctorAdvice{}
  3292. 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
  3293. return err
  3294. }
  3295. func BatchHisAdviceCheck(ids []string, creator int64) error {
  3296. advice := models.HisDoctorAdviceInfo{}
  3297. 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
  3298. return err
  3299. }
  3300. func BatchAdviceExecution(ids []string, creator int64, execution_state int64) error {
  3301. advice := models.XtDoctorAdvice{}
  3302. 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
  3303. return err
  3304. }
  3305. func BatchHisAdviceExecution(ids []string, creator int64, execution_state int64) error {
  3306. advice := models.HisDoctorAdviceInfo{}
  3307. 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
  3308. return err
  3309. }
  3310. func GetAdviceExecutionById(ids []string) (doctor []*models.XtDoctorAdvice, err error) {
  3311. err = XTReadDB().Model(&doctor).Where("id IN(?) AND status = 1", ids).Find(&doctor).Error
  3312. return doctor, err
  3313. }
  3314. func GetHisAdviceExecutionById(ids []string) (doctor []*models.HisDoctorAdviceInfo, err error) {
  3315. err = XTReadDB().Model(&doctor).Where("id IN(?) AND status = 1", ids).Find(&doctor).Error
  3316. return doctor, err
  3317. }
  3318. func GetHisProjectConfig(orgid int64) (models.XtHisProjectConfig, error) {
  3319. config := models.XtHisProjectConfig{}
  3320. err := XTReadDB().Model(&config).Where("user_org_id = ? and status =1", orgid).Find(&config).Error
  3321. return config, err
  3322. }
  3323. func GetHisPrescriptionProject(org_id int64, patient_id int64, record_date int64) (project []*models.HisPrescriptionProject, err error) {
  3324. err = readDb.Model(&models.HisPrescriptionProject{}).Preload("GoodInfo", func(db *gorm.DB) *gorm.DB {
  3325. return db.Preload("GoodsType", "status = 1").Where("status = 1 and is_warehouse = 1")
  3326. }).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
  3327. return
  3328. }
  3329. func GetPCHisPrescriptionProject(orgID int64, scheduleDate int64, deliverWay string, patientType int, adminUserId int64, patient_id int64, execution_state int64) ([]*HisMScheduleProjectVM, error) {
  3330. var vms []*HisMScheduleProjectVM
  3331. fmt.Println("patient_id233232332", patient_id)
  3332. fmt.Println("execution_state2323323322332", execution_state)
  3333. fmt.Println("patientType2323323322332", patientType)
  3334. if patientType == 0 {
  3335. if patient_id > 0 {
  3336. if execution_state == 1 {
  3337. db := readDb.
  3338. Table("xt_schedule").
  3339. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  3340. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  3341. return db.Where("status = 1 AND user_org_id = ? and patient_id = ?", orgID, patient_id).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  3342. }).
  3343. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  3344. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  3345. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ? and patient_id = ?", orgID, scheduleDate, patient_id).
  3346. Preload("HisPrescriptionProject", func(db *gorm.DB) *gorm.DB {
  3347. 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")
  3348. }).
  3349. Preload("HisPrescriptionTeamProject", func(db *gorm.DB) *gorm.DB {
  3350. 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")
  3351. }).Where("status = 1 AND user_org_id = ?", orgID)
  3352. if scheduleDate != 0 {
  3353. db = db.Where("schedule_date = ?", scheduleDate)
  3354. }
  3355. err = db.Find(&vms).Error
  3356. } else if execution_state == 2 {
  3357. db := readDb.
  3358. Table("xt_schedule").
  3359. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  3360. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  3361. return db.Where("status = 1 AND user_org_id = ? and patient_id = ?", orgID, patient_id).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  3362. }).
  3363. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  3364. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  3365. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ? and patient_id = ?", orgID, scheduleDate, patient_id).
  3366. Preload("HisPrescriptionProject", func(db *gorm.DB) *gorm.DB {
  3367. 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")
  3368. }).
  3369. Preload("HisPrescriptionTeamProject", func(db *gorm.DB) *gorm.DB {
  3370. 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")
  3371. }).Where("status = 1 AND user_org_id = ?", orgID)
  3372. if scheduleDate != 0 {
  3373. db = db.Where("schedule_date = ?", scheduleDate)
  3374. }
  3375. err = db.Find(&vms).Error
  3376. } else if execution_state == 0 {
  3377. db := readDb.
  3378. Table("xt_schedule").
  3379. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  3380. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  3381. return db.Where("status = 1 AND user_org_id = ? and patient_id = ?", orgID, patient_id).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  3382. }).
  3383. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  3384. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  3385. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ? and patient_id = ?", orgID, scheduleDate, patient_id).
  3386. Preload("HisPrescriptionProject", func(db *gorm.DB) *gorm.DB {
  3387. return db.Where("status = 1 AND user_org_id = ? AND record_date = ? AND team_id = 0", orgID, scheduleDate).Preload("HisProject").Preload("GoodInfo", "status=1")
  3388. }).
  3389. Preload("HisPrescriptionTeamProject", func(db *gorm.DB) *gorm.DB {
  3390. 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")
  3391. }).Where("status = 1 AND user_org_id = ?", orgID)
  3392. if scheduleDate != 0 {
  3393. db = db.Where("schedule_date = ?", scheduleDate)
  3394. }
  3395. err = db.Find(&vms).Error
  3396. }
  3397. } else {
  3398. if patient_id > 0 {
  3399. if execution_state == 1 {
  3400. db := readDb.
  3401. Table("xt_schedule").
  3402. Preload("SchedualPatient", "status = 1 AND user_org_id = ? and id= ?", orgID, patient_id).
  3403. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  3404. return db.Where("status = 1 AND user_org_id = ? and patient_id = ?", orgID, patient_id).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  3405. }).
  3406. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  3407. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  3408. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ? and patient_id = ?", orgID, scheduleDate, patient_id).
  3409. Preload("HisPrescriptionProject", func(db *gorm.DB) *gorm.DB {
  3410. 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")
  3411. }).
  3412. Preload("HisPrescriptionTeamProject", func(db *gorm.DB) *gorm.DB {
  3413. 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")
  3414. }).Where("status = 1 AND user_org_id = ?", orgID)
  3415. if scheduleDate != 0 {
  3416. db = db.Where("schedule_date = ?", scheduleDate)
  3417. }
  3418. err = db.Find(&vms).Error
  3419. } else if execution_state == 2 {
  3420. db := readDb.
  3421. Table("xt_schedule").
  3422. Preload("SchedualPatient", "status = 1 AND user_org_id = ? and id= ?", orgID, patient_id).
  3423. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  3424. return db.Where("status = 1 AND user_org_id = ? and patient_id = ?", orgID, patient_id).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  3425. }).
  3426. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  3427. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  3428. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ? and patient_id = ?", orgID, scheduleDate, patient_id).
  3429. Preload("HisPrescriptionProject", func(db *gorm.DB) *gorm.DB {
  3430. 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")
  3431. }).
  3432. Preload("HisPrescriptionTeamProject", func(db *gorm.DB) *gorm.DB {
  3433. 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")
  3434. }).Where("status = 1 AND user_org_id = ?", orgID)
  3435. if scheduleDate != 0 {
  3436. db = db.Where("schedule_date = ?", scheduleDate)
  3437. }
  3438. err = db.Find(&vms).Error
  3439. } else if execution_state == 0 {
  3440. db := readDb.
  3441. Table("xt_schedule").
  3442. Preload("SchedualPatient", "status = 1 AND user_org_id = ? and id= ?", orgID, patient_id).
  3443. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  3444. return db.Where("status = 1 AND user_org_id = ? and patient_id = ?", orgID, patient_id).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  3445. }).
  3446. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  3447. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  3448. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ? and patient_id = ?", orgID, scheduleDate, patient_id).
  3449. Preload("HisPrescriptionProject", func(db *gorm.DB) *gorm.DB {
  3450. return db.Where("status = 1 AND user_org_id = ? AND record_date = ? AND team_id = 0", orgID, scheduleDate).Preload("HisProject").Preload("GoodInfo", "status=1")
  3451. }).
  3452. Preload("HisPrescriptionTeamProject", func(db *gorm.DB) *gorm.DB {
  3453. 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")
  3454. }).Where("status = 1 AND user_org_id = ?", orgID)
  3455. if scheduleDate != 0 {
  3456. db = db.Where("schedule_date = ?", scheduleDate)
  3457. }
  3458. err = db.Find(&vms).Error
  3459. }
  3460. } else {
  3461. if execution_state == 1 {
  3462. db := readDb.
  3463. Table("xt_schedule").
  3464. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  3465. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  3466. return db.Where("status = 1 AND user_org_id = ?", orgID).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  3467. }).
  3468. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  3469. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  3470. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  3471. Preload("HisPrescriptionProject", func(db *gorm.DB) *gorm.DB {
  3472. 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")
  3473. }).
  3474. Preload("HisPrescriptionTeamProject", func(db *gorm.DB) *gorm.DB {
  3475. 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")
  3476. }).Where("status = 1 AND user_org_id = ?", orgID)
  3477. if scheduleDate != 0 {
  3478. db = db.Where("schedule_date = ?", scheduleDate)
  3479. }
  3480. err = db.Find(&vms).Error
  3481. } else if execution_state == 2 {
  3482. fmt.Println("j氯332n323232n323ℹ️33232323232")
  3483. db := readDb.
  3484. Table("xt_schedule").
  3485. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  3486. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  3487. return db.Where("status = 1 AND user_org_id = ?", orgID).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  3488. }).
  3489. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  3490. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  3491. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  3492. Preload("HisPrescriptionProject", func(db *gorm.DB) *gorm.DB {
  3493. 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")
  3494. }).
  3495. Preload("HisPrescriptionTeamProject", func(db *gorm.DB) *gorm.DB {
  3496. 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")
  3497. }).Where("status = 1 AND user_org_id = ?", orgID)
  3498. if scheduleDate != 0 {
  3499. db = db.Where("schedule_date = ?", scheduleDate)
  3500. }
  3501. err = db.Find(&vms).Error
  3502. } else if execution_state == 0 {
  3503. db := readDb.
  3504. Table("xt_schedule").
  3505. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  3506. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  3507. return db.Where("status = 1 AND user_org_id = ?", orgID).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  3508. }).
  3509. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  3510. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  3511. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  3512. Preload("HisPrescriptionProject", func(db *gorm.DB) *gorm.DB {
  3513. return db.Where("status = 1 AND user_org_id = ? AND record_date = ? AND team_id = 0", orgID, scheduleDate).Preload("HisProject").Preload("GoodInfo", "status=1")
  3514. }).
  3515. Preload("HisPrescriptionTeamProject", func(db *gorm.DB) *gorm.DB {
  3516. 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")
  3517. }).Where("status = 1 AND user_org_id = ?", orgID)
  3518. if scheduleDate != 0 {
  3519. db = db.Where("schedule_date = ?", scheduleDate)
  3520. }
  3521. err = db.Find(&vms).Error
  3522. }
  3523. }
  3524. }
  3525. }
  3526. if patientType > 0 {
  3527. if execution_state == 1 {
  3528. db := readDb.
  3529. Table("xt_schedule").
  3530. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  3531. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  3532. return db.Where("status = 1 AND user_org_id = ?", orgID).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  3533. }).
  3534. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  3535. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  3536. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  3537. Preload("HisDoctorAdviceInfo", "status = 1 AND user_org_id = ? AND advice_date = ? and (advice_doctor = ? or execution_staff = ?) ", orgID, scheduleDate, adminUserId, adminUserId).
  3538. Preload("HisPrescriptionProject", func(db *gorm.DB) *gorm.DB {
  3539. 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")
  3540. }).
  3541. Preload("HisPrescriptionTeamProject", func(db *gorm.DB) *gorm.DB {
  3542. 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")
  3543. }).Where("status = 1 AND user_org_id = ?", orgID)
  3544. if scheduleDate != 0 {
  3545. db = db.Where("schedule_date = ?", scheduleDate)
  3546. }
  3547. err = db.Find(&vms).Error
  3548. } else if execution_state == 2 {
  3549. db := readDb.
  3550. Table("xt_schedule").
  3551. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  3552. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  3553. return db.Where("status = 1 AND user_org_id = ?", orgID).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  3554. }).
  3555. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  3556. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  3557. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  3558. Preload("HisDoctorAdviceInfo", "status = 1 AND user_org_id = ? AND advice_date = ? and (advice_doctor = ? or execution_staff = ?) ", orgID, scheduleDate, adminUserId, adminUserId).
  3559. Preload("HisPrescriptionProject", func(db *gorm.DB) *gorm.DB {
  3560. 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")
  3561. }).
  3562. Preload("HisPrescriptionTeamProject", func(db *gorm.DB) *gorm.DB {
  3563. 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")
  3564. }).Where("status = 1 AND user_org_id = ?", orgID)
  3565. if scheduleDate != 0 {
  3566. db = db.Where("schedule_date = ?", scheduleDate)
  3567. }
  3568. err = db.Find(&vms).Error
  3569. } else if execution_state == 0 {
  3570. db := readDb.
  3571. Table("xt_schedule").
  3572. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  3573. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  3574. return db.Where("status = 1 AND user_org_id = ?", orgID).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  3575. }).
  3576. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  3577. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  3578. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  3579. Preload("HisDoctorAdviceInfo", "status = 1 AND user_org_id = ? AND advice_date = ? and (advice_doctor = ? or execution_staff = ?) ", orgID, scheduleDate, adminUserId, adminUserId).
  3580. Preload("HisPrescriptionProject", func(db *gorm.DB) *gorm.DB {
  3581. return db.Where("status = 1 AND user_org_id = ? AND record_date = ? AND team_id = 0", orgID, scheduleDate).Preload("HisProject").Preload("GoodInfo", "status=1")
  3582. }).
  3583. Preload("HisPrescriptionTeamProject", func(db *gorm.DB) *gorm.DB {
  3584. 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")
  3585. }).Where("status = 1 AND user_org_id = ?", orgID)
  3586. if scheduleDate != 0 {
  3587. db = db.Where("schedule_date = ?", scheduleDate)
  3588. }
  3589. err = db.Find(&vms).Error
  3590. }
  3591. }
  3592. return vms, err
  3593. }
  3594. func UpdateStockGoods(good_id int64, record_time int64, patient_id int64, count int64) error {
  3595. prepare := models.DialysisBeforePrepare{}
  3596. 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
  3597. return err
  3598. }
  3599. func UPdateAutoStockGoods(good_id int64, record_time int64, patient_id int64, count int64) error {
  3600. detail := models.AutomaticReduceDetail{}
  3601. 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
  3602. return err
  3603. }
  3604. func MobileGetDialysisSolutionByModeIdSeven(orgID int64, patientID int64, mode_id int64) (*models.DialysisSolution, error) {
  3605. var record models.DialysisSolution
  3606. 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
  3607. if err != nil {
  3608. if err == gorm.ErrRecordNotFound {
  3609. return nil, nil
  3610. } else {
  3611. return nil, err
  3612. }
  3613. }
  3614. return &record, nil
  3615. }
  3616. func MobileGetLastDialysisPrescribeByModeIdSeven(orgID int64, patientID int64, mode_id int64) (*models.DialysisPrescription, error) {
  3617. var record models.DialysisPrescription
  3618. 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
  3619. if err != nil {
  3620. if err == gorm.ErrRecordNotFound {
  3621. return nil, nil
  3622. } else {
  3623. return nil, err
  3624. }
  3625. }
  3626. return &record, nil
  3627. }
  3628. func MobileGetSystemDialysisPrescribeByModeIdSeven(orgID int64, mode_id int64) (*models.SystemPrescription, error) {
  3629. var record models.SystemPrescription
  3630. err := readDb.Model(&models.SystemPrescription{}).Where("user_org_id = ? and status = 1 AND mode_id = ?", orgID, mode_id).First(&record).Error
  3631. if err != nil {
  3632. if err == gorm.ErrRecordNotFound {
  3633. return nil, nil
  3634. } else {
  3635. return nil, err
  3636. }
  3637. }
  3638. return &record, nil
  3639. }
  3640. // 透前评估
  3641. func MobileGetPredialysisEvaluationSeven(orgID int64, patientID int64, recordDate int64) (*models.PredialysisEvaluation, error) {
  3642. var record models.PredialysisEvaluation
  3643. err := readDb.Model(&models.PredialysisEvaluation{}).Where("patient_id = ? and user_org_id = ? and status = 1 and assessment_date = ?", patientID, orgID, recordDate).First(&record).Error
  3644. if err != nil {
  3645. if err == gorm.ErrRecordNotFound {
  3646. return nil, nil
  3647. } else {
  3648. return nil, err
  3649. }
  3650. }
  3651. return &record, nil
  3652. }
  3653. //
  3654. //func MobileGetDialysisGoodsSix(orgID int64, scheduleDate int64) (prepare []*models.DialysisBeforePrepare, err error) {
  3655. //
  3656. // 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
  3657. // return prepare, err
  3658. //}
  3659. func GetMobileAutoReduce(orgID int64, scheduleDate int64) (auto []*models.AutomaticReduceDetail, err error) {
  3660. err = readDb.Where("org_id = ? and status = 1 and record_time = ?", orgID, scheduleDate).Find(&auto).Error
  3661. return auto, err
  3662. }
  3663. func GetRoleList(orgid int64, admin_user_id int64) (models.SgjUserAdminRole, error) {
  3664. role := models.SgjUserAdminRole{}
  3665. err = readUserDb.Where("org_id = ? and admin_user_id = ? and status =1 ", orgid, admin_user_id).Find(&role).Error
  3666. return role, err
  3667. }
  3668. func GetDialysisGood(orgID int64, scheduleDate int64) (auto []*models.AutomaticReduceDetail, err error) {
  3669. err = readDb.Where("org_id = ? and status = 1 and record_time >= ?", orgID, scheduleDate).Group("patient_id").Find(&auto).Error
  3670. return auto, err
  3671. }
  3672. func GetGoodWarehouseOutInfo(orgId int64, patient_id int64, sys_record_time int64) (outifno []*models.WarehouseOutInfoSeven, err error) {
  3673. err = readDb.Where("org_id = ? and patient_id = ? and sys_record_time = ? and status = 1", orgId, patient_id, sys_record_time).Find(&outifno).Error
  3674. return outifno, err
  3675. }