mobile_dialysis_service.go 167KB

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