mobile_dialysis_service.go 196KB

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