mobile_dialysis_service.go 196KB

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