patient_service.go 167KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387
  1. package service
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. "time"
  8. "XT_New/models"
  9. "XT_New/utils"
  10. "github.com/jinzhu/gorm"
  11. )
  12. // GetPatientList 返回患者的列表
  13. func GetPatientList(orgID int64, keywords string, page, limit, schedulType, bindingState, lapseto, source, startTime, endTime, contagion, reimbursementWay, isscheduling, isprescription int64, isStartTime, isEndTime bool, patientSoureType int64, nurse int64, patient_type int64) (patients []*models.Patients, total int64, err error) {
  14. db := readDb.Table("xt_patients as p").Where("p.status=1")
  15. if orgID > 0 {
  16. db = db.Where("p.user_org_id=?", orgID)
  17. }
  18. if len(keywords) > 0 {
  19. likeKey := "%" + keywords + "%"
  20. db = db.Where("p.name LIKE ? OR p.dialysis_no LIKE ? OR p.first_letter LIKE ?", likeKey, likeKey, likeKey)
  21. }
  22. if schedulType > 0 {
  23. db = db.Joins("JOIN xt_schedule as s ON s.patient_id=p.id")
  24. db = db.Where("s.status=1 and s.schedule_type=?", schedulType)
  25. }
  26. if contagion > 0 {
  27. db = db.Joins("JOIN xt_patients_infectious_diseases as xpid ON xpid.patient_id=p.id")
  28. db = db.Where("xpid.disease_id=? and xpid.status=1", contagion)
  29. }
  30. if isscheduling == 1 {
  31. db = db.Where("EXISTS (?)", readDb.Table("xt_schedule as iss").Where("iss.patient_id=p.id and iss.status=1").QueryExpr())
  32. } else if isscheduling == 2 {
  33. db = db.Where("NOT EXISTS (?)", readDb.Table("xt_schedule as iss").Where("iss.patient_id=p.id and iss.status=1").QueryExpr())
  34. }
  35. if isprescription == 1 {
  36. db = db.Where("EXISTS (?)", readDb.Table("xt_dialysis_prescription as xdp").Where("xdp.patient_id=p.id and xdp.status=1").QueryExpr())
  37. } else if isprescription == 2 {
  38. db = db.Where("NOT EXISTS (?)", readDb.Table("xt_dialysis_prescription as xdp").Where("xdp.patient_id=p.id and xdp.status=1").QueryExpr())
  39. }
  40. if bindingState > 0 {
  41. db = db.Where("p.binding_state=?", bindingState)
  42. }
  43. if lapseto > 0 {
  44. db = db.Where("p.lapseto=?", lapseto)
  45. }
  46. if source > 0 {
  47. db = db.Where("p.source=?", source)
  48. }
  49. if reimbursementWay > 0 {
  50. db = db.Where("p.reimbursement_way_id = ?", reimbursementWay)
  51. }
  52. if isStartTime {
  53. db = db.Where("p.created_time>=?", startTime)
  54. }
  55. if isEndTime {
  56. db = db.Where("p.created_time<=?", endTime)
  57. }
  58. if patientSoureType > 0 {
  59. db = db.Where("p.patient_source =?", patientSoureType)
  60. }
  61. if nurse > 0 {
  62. db = db.Where("p.head_nurse_id = ?", nurse)
  63. }
  64. if patient_type > 0 {
  65. db = db.Where("p.patient_type = ?", patient_type)
  66. }
  67. offset := (page - 1) * limit
  68. if lapseto == 2 || lapseto == 3 {
  69. err = db.Order("p.death_time desc").Select(" p.id, p.user_org_id, p.user_id, p.patient_type, p.dialysis_no, p.admission_number, p.source, p.lapseto, p.partition_id, p.bed_id, p.name, p.alias, p.gender, p.marital_status, p.id_card_no, p.birthday, p.reimbursement_way_id, p.health_care_type, p.health_care_no, p.health_care_due_date, p.height, p.blood_type, p.rh, p.health_care_due_alert_date, p.education_level, p.profession, p.phone, p.home_telephone, p.relative_phone, p.relative_relations, p.home_address, p.work_unit, p.unit_address, p.children, p.receiving_date, p.is_hospital_first_dialysis, p.first_dialysis_date, p.first_dialysis_hospital, p.induction_period, p.initial_dialysis, p.total_dialysis, p.attending_doctor_id, p.head_nurse_id, p.evaluate, p.diagnose, p.remark, p.registrars_id, p.registrars, p.qr_code, p.binding_state, p.status, p.created_time, p.updated_time,p.user_sys_before_count,p.out_reason,p.death_time,p.is_infectious,p.patient_type").Group("p.id").Count(&total).Offset(offset).Limit(limit).Find(&patients).Error
  70. }
  71. if lapseto != 2 && lapseto != 3 {
  72. if orgID != 10579 && orgID != 10600 && orgID != 10587 {
  73. err = db.Order("p.id desc").Select(" p.id, p.user_org_id, p.user_id, p.patient_type, p.dialysis_no, p.admission_number, p.source, p.lapseto, p.partition_id, p.bed_id, p.name, p.alias, p.gender, p.marital_status, p.id_card_no, p.birthday, p.reimbursement_way_id, p.health_care_type, p.health_care_no, p.health_care_due_date, p.height, p.blood_type, p.rh, p.health_care_due_alert_date, p.education_level, p.profession, p.phone, p.home_telephone, p.relative_phone, p.relative_relations, p.home_address, p.work_unit, p.unit_address, p.children, p.receiving_date, p.is_hospital_first_dialysis, p.first_dialysis_date, p.first_dialysis_hospital, p.induction_period, p.initial_dialysis, p.total_dialysis, p.attending_doctor_id, p.head_nurse_id, p.evaluate, p.diagnose, p.remark, p.registrars_id, p.registrars, p.qr_code, p.binding_state, p.status, p.created_time, p.updated_time,p.user_sys_before_count,p.out_reason,p.death_time,p.is_infectious,p.patient_type").Group("p.id").Count(&total).Offset(offset).Limit(limit).Find(&patients).Error
  74. }
  75. if orgID == 10579 || orgID == 10600 {
  76. err = db.Order("p.dialysis_no asc").Select(" p.id, p.user_org_id, p.user_id, p.patient_type, p.dialysis_no, p.admission_number, p.source, p.lapseto, p.partition_id, p.bed_id, p.name, p.alias, p.gender, p.marital_status, p.id_card_no, p.birthday, p.reimbursement_way_id, p.health_care_type, p.health_care_no, p.health_care_due_date, p.height, p.blood_type, p.rh, p.health_care_due_alert_date, p.education_level, p.profession, p.phone, p.home_telephone, p.relative_phone, p.relative_relations, p.home_address, p.work_unit, p.unit_address, p.children, p.receiving_date, p.is_hospital_first_dialysis, p.first_dialysis_date, p.first_dialysis_hospital, p.induction_period, p.initial_dialysis, p.total_dialysis, p.attending_doctor_id, p.head_nurse_id, p.evaluate, p.diagnose, p.remark, p.registrars_id, p.registrars, p.qr_code, p.binding_state, p.status, p.created_time, p.updated_time,p.user_sys_before_count,p.out_reason,p.death_time,p.is_infectious,p.patient_type").Group("p.id").Count(&total).Offset(offset).Limit(limit).Find(&patients).Error
  77. }
  78. if orgID == 10587 {
  79. err = db.Order("p.created_time desc").Select(" p.id, p.user_org_id, p.user_id, p.patient_type, p.dialysis_no, p.admission_number, p.source, p.lapseto, p.partition_id, p.bed_id, p.name, p.alias, p.gender, p.marital_status, p.id_card_no, p.birthday, p.reimbursement_way_id, p.health_care_type, p.health_care_no, p.health_care_due_date, p.height, p.blood_type, p.rh, p.health_care_due_alert_date, p.education_level, p.profession, p.phone, p.home_telephone, p.relative_phone, p.relative_relations, p.home_address, p.work_unit, p.unit_address, p.children, p.receiving_date, p.is_hospital_first_dialysis, p.first_dialysis_date, p.first_dialysis_hospital, p.induction_period, p.initial_dialysis, p.total_dialysis, p.attending_doctor_id, p.head_nurse_id, p.evaluate, p.diagnose, p.remark, p.registrars_id, p.registrars, p.qr_code, p.binding_state, p.status, p.created_time, p.updated_time,p.user_sys_before_count,p.out_reason,p.death_time,p.is_infectious,p.patient_type").Group("p.id").Count(&total).Offset(offset).Limit(limit).Find(&patients).Error
  80. }
  81. }
  82. return
  83. }
  84. // GetAllPatientList 返回全部患者的列表
  85. func GetAllPatientList(orgID int64) (patients []*models.Patients, total int64, err error) {
  86. db := readDb.Table("xt_patients as p").Where("p.user_org_id=? and p.status=1", orgID)
  87. err = db.Select(" p.id, p.user_org_id, p.user_id, p.patient_type, p.dialysis_no, p.admission_number, p.source, p.lapseto, p.partition_id, p.bed_id, p.name, p.alias, p.gender, p.marital_status, p.id_card_no, p.birthday, p.reimbursement_way_id, p.health_care_type, p.health_care_no, p.health_care_due_date, p.height, p.blood_type, p.rh, p.health_care_due_alert_date, p.education_level, p.profession, p.phone, p.home_telephone, p.relative_phone, p.relative_relations, p.home_address, p.work_unit, p.unit_address, p.children, p.receiving_date, p.is_hospital_first_dialysis, p.first_dialysis_date, p.first_dialysis_hospital, p.induction_period, p.initial_dialysis, p.total_dialysis, p.attending_doctor_id, p.head_nurse_id, p.evaluate, p.diagnose, p.remark, p.registrars_id, p.registrars, p.qr_code, p.binding_state, p.status, p.created_time, p.updated_time,p.age,p.id_card_no").Count(&total).Find(&patients).Error
  88. return
  89. }
  90. func GetAllPatientListTwenty(orgID int64) (patients []*models.Patients, total int64, err error) {
  91. db := readDb.Table("xt_patients as p").Where("p.user_org_id=? and p.status=1 and p.lapseto = 1", orgID)
  92. err = db.Select(" p.id, p.user_org_id, p.user_id, p.patient_type, p.dialysis_no, p.admission_number, p.source, p.lapseto, p.partition_id, p.bed_id, p.name, p.alias, p.gender, p.marital_status, p.id_card_no, p.birthday, p.reimbursement_way_id, p.health_care_type, p.health_care_no, p.health_care_due_date, p.height, p.blood_type, p.rh, p.health_care_due_alert_date, p.education_level, p.profession, p.phone, p.home_telephone, p.relative_phone, p.relative_relations, p.home_address, p.work_unit, p.unit_address, p.children, p.receiving_date, p.is_hospital_first_dialysis, p.first_dialysis_date, p.first_dialysis_hospital, p.induction_period, p.initial_dialysis, p.total_dialysis, p.attending_doctor_id, p.head_nurse_id, p.evaluate, p.diagnose, p.remark, p.registrars_id, p.registrars, p.qr_code, p.binding_state, p.status, p.created_time, p.updated_time").Count(&total).Find(&patients).Error
  93. return
  94. }
  95. func GetAllPatientListSix(orgID int64) (patients []*models.MSchedualPatientList, err error) {
  96. err = XTReadDB().Model(&patients).Where("user_org_id = ? and status = 1", orgID).Find(&patients).Error
  97. return patients, err
  98. }
  99. // GetAllPatientList 返回全部患者的列表
  100. func GetAllPatientListByList(orgID int64) (patients []*models.MSchedualPatientList, err error) {
  101. redis := RedisClient()
  102. defer redis.Close()
  103. key := strconv.FormatInt(orgID, 10) + ":" + ":patient_list_all"
  104. patient_info_str, _ := redis.Get(key).Result()
  105. if len(patient_info_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  106. err = readDb.Model(&models.MSchedualPatientList{}).Where("user_org_id=? and status=1 and lapseto = 1", orgID).Find(&patients).Error
  107. if err != nil {
  108. if err == gorm.ErrRecordNotFound {
  109. if len(patients) <= 0 {
  110. redis.Set(key, "null", time.Second*60*60*18)
  111. }
  112. return nil, nil
  113. } else {
  114. return nil, err
  115. }
  116. } else {
  117. if len(patients) > 0 {
  118. //缓存数据
  119. patient_info_json, err := json.Marshal(&patients)
  120. if err == nil {
  121. redis.Set(key, patient_info_json, time.Second*60*60*18)
  122. }
  123. } else {
  124. redis.Set(key, "null", time.Second*60*60*18)
  125. }
  126. return patients, nil
  127. }
  128. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  129. if patient_info_str == "null" {
  130. return nil, nil
  131. } else {
  132. json.Unmarshal([]byte(patient_info_str), &patients)
  133. return patients, nil
  134. }
  135. }
  136. }
  137. func GetAllDevicetByListSix(orgID int64) (devices []*models.MDeviceNumberForList, err error) {
  138. err = readDb.Model(&models.MDeviceNumberForList{}).Preload("Zone", "status = 1 AND org_id = ?", orgID).Where("org_id=?", orgID).Find(&devices).Error
  139. return devices, err
  140. }
  141. func GetMonitorByListSix(orgID int64, monitor_date int64) (monitor []models.VMMonitoringRecord, err error) {
  142. err = readDb.Where("user_org_id = ? and monitor_date = ? and status =1", orgID, monitor_date).Find(&monitor).Error
  143. return monitor, err
  144. }
  145. func GetAllDevicetByList(orgID int64) (devices []*models.MDeviceNumberForList, err error) {
  146. redis := RedisClient()
  147. defer redis.Close()
  148. key := strconv.FormatInt(orgID, 10) + ":" + ":device_list_all"
  149. device_list_str, _ := redis.Get(key).Result()
  150. if len(device_list_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  151. err = readDb.Model(&models.MDeviceNumberForList{}).Preload("Zone", "status = 1 AND org_id = ?", orgID).Where("org_id=? and status=1 ", orgID).Find(&devices).Error
  152. if err != nil {
  153. if err == gorm.ErrRecordNotFound {
  154. if len(devices) <= 0 {
  155. redis.Set(key, "null", time.Second*60*60*18)
  156. }
  157. return nil, nil
  158. } else {
  159. return nil, err
  160. }
  161. } else {
  162. if len(devices) > 0 {
  163. //缓存数据
  164. device_list_str, err := json.Marshal(&devices)
  165. if err == nil {
  166. redis.Set(key, device_list_str, time.Second*60*60*18)
  167. }
  168. } else {
  169. redis.Set(key, "null", time.Second*60*60*18)
  170. }
  171. return devices, nil
  172. }
  173. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  174. if device_list_str == "null" {
  175. return nil, nil
  176. } else {
  177. json.Unmarshal([]byte(device_list_str), &devices)
  178. return devices, nil
  179. }
  180. }
  181. }
  182. func GetAllAdvicestByList(orgID int64, scheduleDate int64) (advices []models.VMDoctorAdviceForList, err error) {
  183. redis := RedisClient()
  184. defer redis.Close()
  185. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":advice_list_all"
  186. advice_list_str, _ := redis.Get(key).Result()
  187. if len(advice_list_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  188. err = readDb.Model(&models.VMDoctorAdviceForList{}).Where("status = 1 AND user_org_id = ? AND advice_type = 2 AND advice_date = ? ", orgID, scheduleDate).Find(&advices).Error
  189. if err != nil {
  190. if err == gorm.ErrRecordNotFound {
  191. if len(advices) <= 0 {
  192. redis.Set(key, "null", time.Second*60*60*18)
  193. }
  194. return nil, nil
  195. } else {
  196. return nil, err
  197. }
  198. } else {
  199. if len(advices) > 0 {
  200. //缓存数据
  201. advice_list_str, err := json.Marshal(&advices)
  202. if err == nil {
  203. redis.Set(key, advice_list_str, time.Second*60*60*18)
  204. }
  205. } else {
  206. redis.Set(key, "null", time.Second*60*60*18)
  207. }
  208. return advices, nil
  209. }
  210. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  211. if advice_list_str == "null" {
  212. return nil, nil
  213. } else {
  214. json.Unmarshal([]byte(advice_list_str), &advices)
  215. return advices, nil
  216. }
  217. }
  218. }
  219. func GetAllPrescriptionsByList(orgID int64, scheduleDate int64) (prescriptions []*models.DialysisPrescriptionList, err error) {
  220. err = readDb.Where("status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).Find(&prescriptions).Error
  221. return prescriptions, err
  222. }
  223. //func GetAllPrescriptionsByList(orgID int64, scheduleDate int64) (prescriptions []*models.DialysisPrescriptionList, err error) {
  224. // redis := RedisClient()
  225. // defer redis.Close()
  226. // key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":prescriptions_list_all"
  227. // prescriptions_list_all, _ := redis.Get(key).Result()
  228. // redis.Set(prescriptions_list_all, "", time.Second)
  229. // if len(prescriptions_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  230. // err = readDb.Where("status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).Find(&prescriptions).Error
  231. // if err != nil {
  232. // if err == gorm.ErrRecordNotFound {
  233. // if len(prescriptions) <= 0 {
  234. // redis.Set(key, "null", time.Second*60*60*18)
  235. // }
  236. // return nil, nil
  237. // } else {
  238. // return nil, err
  239. // }
  240. // } else {
  241. // if len(prescriptions) > 0 {
  242. // //缓存数据
  243. // prescriptions_list_all, err := json.Marshal(&prescriptions)
  244. // if err == nil {
  245. // redis.Set(key, prescriptions_list_all, time.Second*60*60*18)
  246. // }
  247. // } else {
  248. // redis.Set(key, "null", time.Second*60*60*18)
  249. // }
  250. // return prescriptions, nil
  251. // }
  252. // } else { //缓存数据了数据,将redis缓存的json字符串转为map
  253. // if prescriptions_list_all == "null" {
  254. // return nil, nil
  255. // } else {
  256. // json.Unmarshal([]byte(prescriptions_list_all), &prescriptions)
  257. // return prescriptions, nil
  258. // }
  259. //
  260. // }
  261. //}
  262. func GetAllAssessmentBeforesByList(orgID int64, scheduleDate int64) (assessmentBefores []*models.PredialysisEvaluationList, err error) {
  263. redis := RedisClient()
  264. defer redis.Close()
  265. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":assessment_befores_list_all"
  266. assessment_befores_list_all, _ := redis.Get(key).Result()
  267. if len(assessment_befores_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  268. err = readDb.Where("status = 1 AND user_org_id = ? AND assessment_date = ? ", orgID, scheduleDate).Find(&assessmentBefores).Error
  269. if err != nil {
  270. if err == gorm.ErrRecordNotFound {
  271. if len(assessmentBefores) <= 0 {
  272. redis.Set(key, "null", time.Second*60*60*18)
  273. }
  274. return nil, nil
  275. } else {
  276. return nil, err
  277. }
  278. } else {
  279. if len(assessmentBefores) > 0 {
  280. //缓存数据
  281. assessment_befores_list_all, err := json.Marshal(&assessmentBefores)
  282. if err == nil {
  283. redis.Set(key, assessment_befores_list_all, time.Second*60*60*18)
  284. }
  285. } else {
  286. redis.Set(key, "null", time.Second*60*60*18)
  287. }
  288. return assessmentBefores, nil
  289. }
  290. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  291. if assessment_befores_list_all == "null" {
  292. return nil, nil
  293. } else {
  294. json.Unmarshal([]byte(assessment_befores_list_all), &assessmentBefores)
  295. return assessmentBefores, nil
  296. }
  297. }
  298. }
  299. func GetAllDialysisOrdersByList(orgID int64, scheduleDate int64) (dialysisOrders []*models.MDialysisOrderForList, err error) {
  300. err = readDb.Model(&models.MDialysisOrderForList{}).Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).Where("status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, scheduleDate).Find(&dialysisOrders).Error
  301. return dialysisOrders, err
  302. }
  303. //func GetAllDialysisOrdersByList(orgID int64, scheduleDate int64) (dialysisOrders []*models.MDialysisOrderForList, err error) {
  304. // redis := RedisClient()
  305. // defer redis.Close()
  306. // key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":dialysis_orders_list_all"
  307. // dialysis_orders_list_all, _ := redis.Get(key).Result()
  308. // redis.Set(dialysis_orders_list_all, "", time.Second)
  309. // if len(dialysis_orders_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  310. // err = readDb.Model(&models.MDialysisOrderForList{}).Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).Where("status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, scheduleDate).Find(&dialysisOrders).Error
  311. //
  312. // if err != nil {
  313. // if err == gorm.ErrRecordNotFound {
  314. // return nil, nil
  315. // } else {
  316. // return nil, err
  317. // }
  318. // } else {
  319. // if len(dialysisOrders) > 0 {
  320. // //缓存数据
  321. // dialysis_orders_list_all, err := json.Marshal(&dialysisOrders)
  322. // if err == nil {
  323. // redis.Set(key, dialysis_orders_list_all, time.Second*60*60*18)
  324. // }
  325. // } else {
  326. // redis.Set(key, "null", time.Second*60*60*18)
  327. // }
  328. // return dialysisOrders, nil
  329. // }
  330. // } else { //缓存数据了数据,将redis缓存的json字符串转为map
  331. // if dialysis_orders_list_all == "null" {
  332. // return nil, nil
  333. // } else {
  334. // json.Unmarshal([]byte(dialysis_orders_list_all), &dialysisOrders)
  335. // return dialysisOrders, nil
  336. // }
  337. //
  338. // }
  339. //}
  340. func GetAllTreatmentSummarysByList(orgID int64, scheduleDate int64) (treatmentSummarys []*models.VMTreatmentSummaryForList, err error) {
  341. redis := RedisClient()
  342. defer redis.Close()
  343. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":treatment_summarys_list_all"
  344. treatment_summarys_all, _ := redis.Get(key).Result()
  345. if len(treatment_summarys_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  346. err = readDb.Model(&models.VMTreatmentSummaryForList{}).Where("status = 1 AND user_org_id = ? AND assessment_date = ?", orgID, scheduleDate).Find(&treatmentSummarys).Error
  347. if err != nil {
  348. if err == gorm.ErrRecordNotFound {
  349. if len(treatment_summarys_all) <= 0 {
  350. redis.Set(key, "null", time.Second*60*60*18)
  351. }
  352. return nil, nil
  353. } else {
  354. return nil, err
  355. }
  356. } else {
  357. if len(treatmentSummarys) > 0 {
  358. //缓存数据
  359. treatment_summarys_all, err := json.Marshal(&treatmentSummarys)
  360. if err == nil {
  361. redis.Set(key, treatment_summarys_all, time.Second*60*60*18)
  362. }
  363. } else {
  364. redis.Set(key, "null", time.Second*60*60*18)
  365. }
  366. return treatmentSummarys, nil
  367. }
  368. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  369. if treatment_summarys_all == "null" {
  370. return nil, nil
  371. } else {
  372. json.Unmarshal([]byte(treatment_summarys_all), &treatmentSummarys)
  373. return treatmentSummarys, nil
  374. }
  375. }
  376. }
  377. func GetAllAssessmentAfterDislysisByList(orgID int64, scheduleDate int64) (assessmentAfterDislysis []*models.VMAssessmentAfterDislysis, err error) {
  378. redis := RedisClient()
  379. defer redis.Close()
  380. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":assessment_after_dislysis_list_all"
  381. assessment_after_dislysis__all, _ := redis.Get(key).Result()
  382. if len(assessment_after_dislysis__all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  383. err = readDb.Model(&models.VMAssessmentAfterDislysis{}).Where("status = 1 AND user_org_id = ? AND assessment_date = ?", orgID, scheduleDate).Find(&assessmentAfterDislysis).Error
  384. if err != nil {
  385. if err == gorm.ErrRecordNotFound {
  386. if len(assessmentAfterDislysis) <= 0 {
  387. redis.Set(key, "null", time.Second*60*60*18)
  388. }
  389. return nil, nil
  390. } else {
  391. return nil, err
  392. }
  393. } else {
  394. if len(assessmentAfterDislysis) > 0 {
  395. //缓存数据
  396. assessment_after_dislysis__all, err := json.Marshal(&assessmentAfterDislysis)
  397. if err == nil {
  398. redis.Set(key, assessment_after_dislysis__all, time.Second*60*60*18)
  399. }
  400. } else {
  401. redis.Set(key, "null", time.Second*60*60*18)
  402. }
  403. return assessmentAfterDislysis, nil
  404. }
  405. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  406. if assessment_after_dislysis__all == "null" {
  407. return nil, nil
  408. } else {
  409. json.Unmarshal([]byte(assessment_after_dislysis__all), &assessmentAfterDislysis)
  410. return assessmentAfterDislysis, nil
  411. }
  412. }
  413. }
  414. func GetAllDobuleCheckList(orgID int64, scheduleDate int64) (dobulecheck []*models.DoubleCheck, err error) {
  415. err = XTReadDB().Where("check_date = ? and user_org_id = ? and status = 1", scheduleDate, orgID).Find(&dobulecheck).Error
  416. return dobulecheck, err
  417. }
  418. func GetAllHisAdvicesByList(orgID int64, scheduleDate int64) (hisAdvices []VMHisDoctorAdviceInfo, err error) {
  419. redis := RedisClient()
  420. defer redis.Close()
  421. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":his_advices_list_all"
  422. his_advices_all, _ := redis.Get(key).Result()
  423. redis.Set(key, "", time.Second)
  424. if orgID != 10721 {
  425. if len(his_advices_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  426. err = readDb.Model(&VMHisDoctorAdviceInfo{}).Where("status = 1 AND user_org_id = ? AND advice_date = ?", orgID, scheduleDate).Find(&hisAdvices).Error
  427. if err != nil {
  428. if err == gorm.ErrRecordNotFound {
  429. if len(hisAdvices) <= 0 {
  430. redis.Set(key, "null", time.Second*60*60*18)
  431. }
  432. return nil, nil
  433. } else {
  434. return nil, err
  435. }
  436. } else {
  437. if len(hisAdvices) > 0 {
  438. //缓存数据
  439. his_advices_all, err := json.Marshal(&hisAdvices)
  440. if err == nil {
  441. redis.Set(key, his_advices_all, time.Second*60*60*18)
  442. }
  443. } else {
  444. redis.Set(key, "null", time.Second*60*60*18)
  445. }
  446. return hisAdvices, nil
  447. }
  448. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  449. if his_advices_all == "null" {
  450. return nil, nil
  451. } else {
  452. json.Unmarshal([]byte(his_advices_all), &hisAdvices)
  453. return hisAdvices, nil
  454. }
  455. }
  456. }
  457. if orgID == 10721 {
  458. var keyword = "胰岛素"
  459. keyword = "%" + keyword + "%"
  460. if len(his_advices_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  461. err = readDb.Model(&VMHisDoctorAdviceInfo{}).Where("status = 1 AND user_org_id = ? AND advice_date = ? and execution_frequency !='上机前' and delivery_way!='口服' and advice_name not like ? ", orgID, scheduleDate, keyword).Find(&hisAdvices).Error
  462. if err != nil {
  463. if err == gorm.ErrRecordNotFound {
  464. if len(hisAdvices) <= 0 {
  465. redis.Set(key, "null", time.Second*60*60*18)
  466. }
  467. return nil, nil
  468. } else {
  469. return nil, err
  470. }
  471. } else {
  472. if len(hisAdvices) > 0 {
  473. //缓存数据
  474. his_advices_all, err := json.Marshal(&hisAdvices)
  475. if err == nil {
  476. redis.Set(key, his_advices_all, time.Second*60*60*18)
  477. }
  478. } else {
  479. redis.Set(key, "null", time.Second*60*60*18)
  480. }
  481. return hisAdvices, nil
  482. }
  483. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  484. if his_advices_all == "null" {
  485. return nil, nil
  486. } else {
  487. json.Unmarshal([]byte(his_advices_all), &hisAdvices)
  488. return hisAdvices, nil
  489. }
  490. }
  491. }
  492. return hisAdvices, nil
  493. }
  494. func GetPatientListByUpdateTime(orgID int64, syncTime int64) (patients []*models.PatientListForFaceList, total int64, err error) {
  495. db := readDb.Model(&models.PatientListForFaceList{}).Where("user_org_id=? AND status=1 AND avatar <> 'https://images.shengws.com/201809182128222.png' AND avatar <> 'https://images.shengws.com/201809182128111.png' AND avatar <> ''", orgID)
  496. db = db.Where("updated_time >= ?", syncTime)
  497. err = db.Count(&total).Find(&patients).Error
  498. return
  499. }
  500. func GetPatientListBySchedules(orgID int64, syncTime int64) (patients []*models.SchedulePatients, total int64, err error) {
  501. db := readDb.Model(&models.SchedulePatients{}).Where("user_org_id=? AND status=1 AND schedule_date = ?", orgID, syncTime).Preload("Patient", "status = 1 AND user_org_id = ? ", orgID)
  502. err = db.Count(&total).Find(&patients).Error
  503. return
  504. }
  505. func GetPatientListBySchedulesFind(orgID int64, syncTime int64, patient_name string) (patients []*models.SchedulePatients, total int64, err error) {
  506. db := readDb.Model(&models.SchedulePatients{}).Where("user_org_id=? AND status=1 AND schedule_date = ?", orgID, syncTime).Preload("Patient", "status = 1 AND user_org_id = ? and name like ? ", orgID, "%"+patient_name+"%")
  507. err = db.Count(&total).Find(&patients).Error
  508. return
  509. }
  510. func GetPatientListById(orgID int64, patientId int64) (patients models.PatientListForFace, err error) {
  511. err = readDb.Model(&models.PatientListForFaceList{}).Where("user_org_id=? and id = ? and status=1", orgID, patientId).First(&patients).Error
  512. return
  513. }
  514. func GetPatientCount(orgID int64) (total int64) {
  515. readDb.Model(&models.Patients{}).Where("user_org_id=? and status=1 and lapseto = 1", orgID).Count(&total)
  516. return
  517. }
  518. func GetLapsetoPatientCount(orgID int64, lapseto int64) (total int64) {
  519. readDb.Model(&models.Patients{}).Where("user_org_id=? and lapseto=? and status=1", orgID, lapseto).Count(&total)
  520. return
  521. }
  522. func ChechLastDialysisNo(orgID int64) (dialysisNo int64) {
  523. var patient models.Patients
  524. if orgID == 10278 {
  525. err := readDb.Model(&models.Patients{}).Where("status=1 and user_org_id=?", orgID).Order("id desc").First(&patient).Error
  526. if err != nil {
  527. return
  528. }
  529. } else {
  530. err := readDb.Model(&models.Patients{}).Where("status=1 and user_org_id=?", orgID).Order("dialysis_no desc").First(&patient).Error
  531. if err != nil {
  532. return
  533. }
  534. }
  535. if patient.ID == 0 {
  536. return
  537. }
  538. dialysisNo, _ = strconv.ParseInt(patient.DialysisNo, 10, 64)
  539. return
  540. }
  541. func FindPatientByDialysisNo(orgID int64, dialysisNo string) (patient models.Patients, err error) {
  542. err = readDb.Model(&models.Patients{}).Where("status=1 and user_org_id=? and dialysis_no=? and lapseto = 1", orgID, dialysisNo).First(&patient).Error
  543. return
  544. }
  545. func FindPatientByIdCardNo(orgID int64, idCardNo string) (patient models.Patients, err error) {
  546. err = readDb.Model(&models.Patients{}).Where("status=1 and user_org_id=? and id_card_no=?", orgID, idCardNo).First(&patient).Error
  547. return
  548. }
  549. func FindPatientByMobile(orgID int64, mobile string) (patient models.Patients, err error) {
  550. err = readDb.Model(&models.Patients{}).Where("phone=? and user_org_id=? and status=1", mobile, orgID).First(&patient).Error
  551. return
  552. }
  553. func FindPatientById(orgID int64, id int64) (patient models.Patients, err error) {
  554. err = readDb.Model(&models.Patients{}).Where("id = ? and user_org_id=? and status=1", id, orgID).First(&patient).Error
  555. return
  556. }
  557. func FindPatientByIdTwo(orgID int64, id int64) (patient models.XtPatientsNew, err error) {
  558. err = readDb.Model(&models.XtPatientsNew{}).Where("blood_id = ? and user_org_id = ? and status =1", id, orgID).First(&patient).Error
  559. return
  560. }
  561. // func FindPatientByIdWithDiseases(orgID int64, id int64) (patient models.Patients, err error) {
  562. // err = readDb.Model(&models.Patients{}).Preload("Contagions", "status = 1").Preload("Diseases", "status = 1").Where("id = ? and user_org_id=? and status=1", id, orgID).First(&patient).Error
  563. // return
  564. // }
  565. func FindPatientByIdWithDiseases(orgID int64, id int64) (patient models.Patients, err error) {
  566. redis := RedisClient()
  567. defer redis.Close()
  568. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(id, 10) + ":patient_info"
  569. patient_info_str, _ := redis.Get(key).Result()
  570. redis.Set(key, "", time.Second)
  571. if len(patient_info_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  572. err = readDb.Model(&models.Patients{}).Preload("Contagions", "status = 1").Preload("Diseases", "status = 1").Where("id = ? and user_org_id=? and status=1", id, orgID).First(&patient).Error
  573. if err != nil {
  574. if err == gorm.ErrRecordNotFound {
  575. return patient, nil
  576. } else {
  577. return patient, err
  578. }
  579. } else {
  580. if patient.ID > 0 {
  581. //缓存数据
  582. patient_info_json, err := json.Marshal(&patient)
  583. if err == nil {
  584. redis.Set(key, patient_info_json, time.Second*60*60*18)
  585. }
  586. } else {
  587. redis.Set(key, "null", time.Second*60*60*18)
  588. }
  589. return patient, nil
  590. }
  591. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  592. if patient_info_str == "null" {
  593. return patient, nil
  594. } else {
  595. json.Unmarshal([]byte(patient_info_str), &patient)
  596. return patient, nil
  597. }
  598. }
  599. }
  600. func FindPatientWithDeviceByNo(orgID int64, no string, time int64) (patient models.SchedualPatient2, err error) {
  601. err = readDb.Preload("DialysisSchedule", func(db *gorm.DB) *gorm.DB {
  602. return db.Preload("DeviceNumber", "org_id = ?", orgID).
  603. Preload("DeviceZone", "org_id = ?", orgID).
  604. Where("user_org_id = ? AND schedule_date = ? ", orgID, time)
  605. }).Preload("InfectiousDiseases", "status = 1").Where("user_org_id=? and dialysis_no = ? and status = 1 ", orgID, no).First(&patient).Error
  606. return
  607. }
  608. func FindPatientWithDeviceByPatientId(orgID int64, id int64, time int64) (patient models.SchedualPatient2, err error) {
  609. err = readDb.Preload("DialysisSchedule", func(db *gorm.DB) *gorm.DB {
  610. return db.Preload("DeviceNumber", "org_id = ?", orgID).
  611. Preload("DeviceZone", "org_id = ?", orgID).
  612. Where("user_org_id = ? AND schedule_date = ? ", orgID, time)
  613. }).Preload("InfectiousDiseases", "status = 1").Where("user_org_id=? and id = ? and status = 1 ", orgID, id).First(&patient).Error
  614. return
  615. }
  616. func CreatePatient(patient *models.Patients, contagions []int64, diseases []int64) (err error) {
  617. user, _ := GetSgjUserByMobild(patient.Phone)
  618. customer, _ := GetSgjCoustomerByMobile(patient.UserOrgId, patient.Phone)
  619. utx := writeDb.Begin()
  620. btx := writeUserDb.Begin()
  621. if user.ID == 0 {
  622. user.Mobile = patient.Phone
  623. user.Avatar = patient.Avatar
  624. user.AvatarThumb = patient.Avatar
  625. user.Birthday = patient.Birthday
  626. user.Username = patient.Name
  627. user.Gender = patient.Gender
  628. user.Sources = 11
  629. user.Introduce = patient.Remark
  630. user.Status = 1
  631. user.UpdatedTime = patient.UpdatedTime
  632. user.CreatedTime = patient.CreatedTime
  633. err = btx.Create(&user).Error
  634. if err != nil {
  635. utx.Rollback()
  636. btx.Rollback()
  637. return
  638. }
  639. }
  640. patient.UserId = user.ID
  641. if customer == nil {
  642. err = btx.Create(&models.SgjCustomer{
  643. UserOrgId: patient.UserOrgId,
  644. UserId: user.ID,
  645. Mobile: patient.Phone,
  646. Name: patient.Name,
  647. Gender: patient.Gender,
  648. Birthday: patient.Birthday,
  649. Sources: 11,
  650. Status: 1,
  651. CreatedTime: patient.CreatedTime,
  652. UpdatedTime: patient.UpdatedTime,
  653. Avatar: patient.Avatar,
  654. Remark: patient.Remark,
  655. }).Error
  656. if err != nil {
  657. utx.Rollback()
  658. btx.Rollback()
  659. return
  660. }
  661. }
  662. err = utx.Create(patient).Error
  663. if err != nil {
  664. utx.Rollback()
  665. btx.Rollback()
  666. return
  667. }
  668. var lapseto models.PatientLapseto
  669. lapseto.PatientId = patient.ID
  670. lapseto.LapsetoType = patient.Lapseto
  671. lapseto.CreatedTime = patient.CreatedTime
  672. lapseto.UpdatedTime = patient.CreatedTime
  673. lapseto.Status = 1
  674. lapseto.LapsetoTime = patient.CreatedTime
  675. err = utx.Create(&lapseto).Error
  676. if err != nil {
  677. utx.Rollback()
  678. btx.Rollback()
  679. return
  680. }
  681. if len(contagions) > 0 {
  682. thisSQL := "INSERT INTO xt_patients_infectious_diseases (patient_id, disease_id, status, created_time, updated_time) VALUES "
  683. insertParams := make([]string, 0)
  684. insertData := make([]interface{}, 0)
  685. for _, contagion := range contagions {
  686. insertParams = append(insertParams, "(?, ?, ?, ?, ?)")
  687. insertData = append(insertData, patient.ID)
  688. insertData = append(insertData, contagion)
  689. insertData = append(insertData, 1)
  690. insertData = append(insertData, patient.CreatedTime)
  691. insertData = append(insertData, patient.UpdatedTime)
  692. }
  693. thisSQL += strings.Join(insertParams, ", ")
  694. err = utx.Exec(thisSQL, insertData...).Error
  695. if err != nil {
  696. utx.Rollback()
  697. btx.Rollback()
  698. return
  699. }
  700. }
  701. if len(diseases) > 0 {
  702. thisSQL := "INSERT INTO xt_patients_chronic_diseases (patient_id, disease_id, status, created_time, updated_time) VALUES "
  703. insertParams := make([]string, 0)
  704. insertData := make([]interface{}, 0)
  705. for _, disease := range diseases {
  706. insertParams = append(insertParams, "(?, ?, ?, ?, ?)")
  707. insertData = append(insertData, patient.ID)
  708. insertData = append(insertData, disease)
  709. insertData = append(insertData, 1)
  710. insertData = append(insertData, patient.CreatedTime)
  711. insertData = append(insertData, patient.UpdatedTime)
  712. }
  713. thisSQL += strings.Join(insertParams, ", ")
  714. err = utx.Exec(thisSQL, insertData...).Error
  715. if err != nil {
  716. utx.Rollback()
  717. btx.Rollback()
  718. return
  719. }
  720. }
  721. utx.Commit()
  722. btx.Commit()
  723. return
  724. }
  725. func GetLastPatientData(orgid int64) (models.Patients, error) {
  726. patients := models.Patients{}
  727. err := XTReadDB().Model(&patients).Where("user_org_id = ? and status = 1", orgid).Last(&patients).Error
  728. return patients, err
  729. }
  730. func CreatePatientsNew(patientsNew *models.XtPatientsNew) error {
  731. err := XTWriteDB().Model(&patientsNew).Create(&patientsNew).Error
  732. return err
  733. }
  734. func CreateAllergic(allergic models.XtPatientAllergic) error {
  735. err := XTWriteDB().Create(&allergic).Error
  736. return err
  737. }
  738. func EditPatientLapseto(patient *models.Patients, lapseto *models.PatientLapseto) (err error) {
  739. utx := writeDb.Begin()
  740. err = utx.Model(&models.Patients{}).Where("id=?", patient.ID).Update(map[string]interface{}{"lapseto": patient.Lapseto, "out_reason": patient.OutReason, "death_time": patient.DeathTime, "patient_start_time": patient.PatientStartTime, "patient_end_time": patient.PatientEndTime, "patient_address": patient.PatientAddress}).Error
  741. //err = utx.Model(&models.PatientLapseto{}).Where("id=?", patient.ID).Update(map[string]interface{}{"lapseto_type": patient.Lapseto, "lapseto_time": time.Now().Unix(), "updated_time": time.Now().Unix()}).Error
  742. if err != nil {
  743. utx.Rollback()
  744. return
  745. }
  746. err = utx.Create(lapseto).Error
  747. if err != nil {
  748. utx.Rollback()
  749. return
  750. }
  751. // 删除排班和排班模板信息
  752. if lapseto.LapsetoType == 2 || lapseto.LapsetoType == 3 {
  753. now := time.Now()
  754. deleteScheduleErr := utx.Model(&models.PatientSchedule{}).Where("patient_id = ? AND schedule_date >= ? AND status = 1", patient.ID, lapseto.LapsetoTime).Updates(map[string]interface{}{
  755. "status": 0,
  756. "updated_time": now.Unix(),
  757. }).Error
  758. if deleteScheduleErr != nil {
  759. utx.Rollback()
  760. err = deleteScheduleErr
  761. return
  762. }
  763. deleteSchTempItemErr := utx.Model(&models.PatientScheduleTemplateItem{}).Where("patient_id = ? AND status = 1", patient.ID).Updates(map[string]interface{}{
  764. "status": 0,
  765. "mtime": now.Unix(),
  766. }).Error
  767. if deleteSchTempItemErr != nil {
  768. utx.Rollback()
  769. err = deleteSchTempItemErr
  770. return
  771. }
  772. }
  773. utx.Commit()
  774. return
  775. }
  776. func UpdatePatient(patient *models.Patients, contagions []int64, diseases []int64) (err error) {
  777. // if len(contagions) > 0 || len(diseases) > 0 {
  778. utx := writeDb.Begin()
  779. err = utx.Save(patient).Error
  780. if err != nil {
  781. utx.Rollback()
  782. return
  783. }
  784. err = utx.Model(&models.InfectiousDiseases{}).Where("patient_id=?", patient.ID).Update(map[string]interface{}{"Status": 2, "UpdatedTime": patient.UpdatedTime}).Error
  785. fmt.Println("err", err)
  786. if err != nil {
  787. utx.Rollback()
  788. return
  789. }
  790. if len(contagions) > 0 && patient.IsInfectious == 2 {
  791. thisSQL := "INSERT INTO xt_patients_infectious_diseases (patient_id, disease_id, status, created_time, updated_time) VALUES "
  792. insertParams := make([]string, 0)
  793. insertData := make([]interface{}, 0)
  794. for _, contagion := range contagions {
  795. insertParams = append(insertParams, "(?, ?, ?, ?, ?)")
  796. insertData = append(insertData, patient.ID)
  797. insertData = append(insertData, contagion)
  798. insertData = append(insertData, 1)
  799. insertData = append(insertData, time.Now().Unix())
  800. insertData = append(insertData, time.Now().Unix())
  801. }
  802. thisSQL += strings.Join(insertParams, ", ")
  803. err = utx.Exec(thisSQL, insertData...).Error
  804. if err != nil {
  805. utx.Rollback()
  806. return
  807. }
  808. }
  809. err = utx.Model(&models.ChronicDiseases{}).Where("patient_id=?", patient.ID).Update(map[string]interface{}{"Status": 2, "UpdatedTime": patient.UpdatedTime}).Error
  810. if err != nil {
  811. utx.Rollback()
  812. return
  813. }
  814. if len(diseases) > 0 {
  815. thisSQL := "INSERT INTO xt_patients_chronic_diseases (patient_id, disease_id, status, created_time, updated_time) VALUES "
  816. insertParams := make([]string, 0)
  817. insertData := make([]interface{}, 0)
  818. for _, disease := range diseases {
  819. insertParams = append(insertParams, "(?, ?, ?, ?, ?)")
  820. insertData = append(insertData, patient.ID)
  821. insertData = append(insertData, disease)
  822. insertData = append(insertData, 1)
  823. insertData = append(insertData, time.Now().Unix())
  824. insertData = append(insertData, time.Now().Unix())
  825. }
  826. thisSQL += strings.Join(insertParams, ", ")
  827. err = utx.Exec(thisSQL, insertData...).Error
  828. if err != nil {
  829. utx.Rollback()
  830. return
  831. }
  832. }
  833. utx.Commit()
  834. // } else {
  835. // err = writeDb.Save(patient).Error
  836. // }
  837. return
  838. }
  839. func UpdatePatientLapseto(patientid int64, lapseto models.PatientLapseto) error {
  840. err := XTWriteDB().Model(&lapseto).Where("patient_id = ?", patientid).Updates(map[string]interface{}{"lapseto_type": lapseto.LapsetoType, "lapseto_time": time.Now().Unix(), "updated_time": time.Now().Unix()}).Error
  841. return err
  842. }
  843. func UpdatepatientTwo(patientsNew *models.XtPatientsNew, id int64) error {
  844. err := XTWriteDB().Model(&patientsNew).Where("blood_id = ?", id).Update(map[string]interface{}{"user_org_id": patientsNew.UserOrgId, "user_id": patientsNew.UserId, "avatar": patientsNew.Avatar, "patient_type": patientsNew.Avatar, "dialysis_no": patientsNew.DialysisNo, "admission_number": patientsNew.AdmissionNumber, "source": patientsNew.Source, "lapseto": patientsNew.Lapseto, "partition_id": patientsNew.PartitionId, "bed_id": patientsNew.BedId, "name": patientsNew.Name, "alias": patientsNew.Alias, "gender": patientsNew.Gender, "marital_status": patientsNew.MaritalStatus, "id_card_no": patientsNew.IdCardNo, "birthday": patientsNew.Birthday, "reimbursement_way_id": patientsNew.ReimbursementWayId, "health_care_type": patientsNew.HealthCareType, "health_care_no": patientsNew.HealthCareType, "health_care_due_date": patientsNew.HealthCareType, "height": patientsNew.Height, "blood_type": patientsNew.BloodType, "rh": patientsNew.Rh, "health_care_due_alert_date": patientsNew.HealthCareDueAlertDate, "education_level": patientsNew.EducationLevel, "profession": patientsNew.Profession, "phone": patientsNew.Phone, "home_telephone": patientsNew.HomeTelephone, "relative_phone": patientsNew.RelativePhone, "relative_relations": patientsNew.RelativeRelations, "home_address": patientsNew.HomeAddress, "work_unit": patientsNew.WorkUnit, "unit_address": patientsNew.UnitAddress, "children": patientsNew.Children, "receiving_date": patientsNew.ReceivingDate, "is_hospital_first_dialysis": patientsNew.IsHospitalFirstDialysis, "first_dialysis_date": patientsNew.FirstDialysisDate, "first_dialysis_hospital": patientsNew.FirstDialysisHospital, "predialysis_condition": patientsNew.PredialysisCondition, "pre_hospital_dialysis_frequency": patientsNew.PreHospitalDialysisFrequency, "pre_hospital_dialysis_times": patientsNew.PreHospitalDialysisFrequency, "hospital_first_dialysis_date": patientsNew.HospitalFirstDialysisDate, "induction_period": patientsNew.InductionPeriod, "initial_dialysis": patientsNew.InitialDialysis, "total_dialysis": patientsNew.TotalDialysis, "attending_doctor_id": patientsNew.AttendingDoctorId, "head_nurse_id": patientsNew.HeadNurseId, "evaluate": patientsNew.Evaluate, "diagnose": patientsNew.Diagnose, "remark": patientsNew.Remark, "registrars_id": patientsNew.RegistrarsId, "registrars": patientsNew.Registrars, "qr_code": patientsNew.QrCode, "binding_state": patientsNew.BindingState, "patient_complains": patientsNew.PatientComplains, "present_history": patientsNew.PresentHistory, "past_history": patientsNew.PastHistory, "temperature": patientsNew.Temperature,
  845. "pulse": patientsNew.Pulse, "respiratory": patientsNew.Respiratory, "sbp": patientsNew.Sbp, "dbp": patientsNew.Dbp, "nation": patientsNew.Nation, "native_place": patientsNew.NativePlace, "age": patientsNew.Age, "infectious_next_record_time": patientsNew.InfectiousNextRecordTime, "is_infectious": patientsNew.IsInfectious, "remind_cycle": patientsNew.RemindCycle, "response_result": patientsNew.ResponseResult, "is_open_remind": patientsNew.IsOpenRemind, "first_treatment_date": patientsNew.FirstTreatmentDate, "dialysis_age": patientsNew.DialysisAge, "expense_kind": patientsNew.ExpenseKind, "tell_phone": patientsNew.ExpenseKind, "contact_name": patientsNew.ContactName, "blood_patients": patientsNew.BloodPatients, "slow_patients": patientsNew.SlowPatients, "member_patients": patientsNew.MemberPatients, "ecommer_patients": patientsNew.EcommerPatients, "troble_shoot": patientsNew.TrobleShoot, "sch_remark": patientsNew.SchRemark, "treatment_plan": patientsNew.TreatmentPlan, "record_number": patientsNew.RecordNumber, "patient_start_time": patientsNew.PatientStartTime, "patient_end_time": patientsNew.PatientEndTime, "infectious_remark": patientsNew.InfectiousRemark, "allergic_history": patientsNew.AllergicHistory, "patient_address": patientsNew.PatientAddress, "print_date": patientsNew.PrintDate, "patient_type:": patientsNew.PatientType}).Error
  846. return err
  847. }
  848. func GetLastInfectionRecord(id int64, org_id int64, project_id int64, date int64) (inspection models.Inspection, err error) {
  849. err = readDb.Model(&models.Inspection{}).Where("patient_id=? and status=1 and org_id = ? and project_id = ? AND inspect_date = ? ", id, org_id, project_id, date).Last(&inspection).Error
  850. return
  851. }
  852. func GetAllInfectionRecord(date int64, org_id int64, patient_id int64, project_id int64) (inspection []*models.Inspection, err error) {
  853. err = readDb.Model(&models.Inspection{}).Where("patient_id=? and status=1 and org_id = ? and project_id = ? and inspect_date = ?", patient_id, org_id, project_id, date).Find(&inspection).Error
  854. return
  855. }
  856. func GetPatientDiseases(id int64) []int64 {
  857. var dis []models.ChronicDiseases
  858. ids := make([]int64, 0)
  859. err := readDb.Model(&models.ChronicDiseases{}).Where("patient_id=? and status=1", id).Find(&dis).Error
  860. if err != nil || len(dis) == 0 {
  861. return ids
  862. }
  863. for _, item := range dis {
  864. ids = append(ids, item.DiseaseId)
  865. }
  866. return ids
  867. }
  868. func GetPatientContagions(id int64) []int64 {
  869. var cis []models.InfectiousDiseases
  870. ids := make([]int64, 0)
  871. err := readDb.Model(&models.InfectiousDiseases{}).Where("patient_id=? and status=1", id).Find(&cis).Error
  872. if err != nil || len(cis) == 0 {
  873. return ids
  874. }
  875. for _, item := range cis {
  876. ids = append(ids, item.DiseaseId)
  877. }
  878. return ids
  879. }
  880. func FindPatientDialysisSolutionByMode(orgID int64, patientID, modeId int64) (solution models.DialysisSolution, err error) {
  881. err = readDb.Model(&models.DialysisSolution{}).Where("user_org_id=? and patient_id=? and mode_id=? and parent_id=0 and status=1", orgID, patientID, modeId).First(&solution).Error
  882. return
  883. }
  884. func FindPatientDialysisSolution(orgID int64, id int64) (solution models.DialysisSolution, err error) {
  885. err = readDb.Model(&models.DialysisSolution{}).Where("id = ? and status=1 and user_org_id=?", id, orgID).First(&solution).Error
  886. return
  887. }
  888. func FindPatientDialysisSolutionChild(orgID int64, id int64) (solution models.DialysisSolution, err error) {
  889. err = readDb.Model(&models.DialysisSolution{}).Where("parent_id = ? and status=1 and user_org_id=?", id, orgID).First(&solution).Error
  890. return
  891. }
  892. func CreatePatientDialysisSolution(solution *models.DialysisSolution) (err error) {
  893. err = writeDb.Create(solution).Error
  894. return
  895. }
  896. func UpdatePatientDialysisSolution(solution *models.DialysisSolution) (err error) {
  897. err = writeDb.Save(solution).Error
  898. return
  899. }
  900. // GetPatientDialysisSolutionList 返回患者透析方案的列表
  901. func GetPatientDialysisSolutionList(orgID int64, patientID int64, page, limit int64) (solutions []*models.DialysisSolution, total int64, err error) {
  902. offset := (page - 1) * limit
  903. db := readDb.Table("xt_dialysis_solution as ds").Where("ds.status=1")
  904. if orgID > 0 {
  905. db = db.Where("ds.user_org_id=?", orgID)
  906. }
  907. if patientID > 0 {
  908. db = db.Where("ds.patient_id=?", patientID)
  909. }
  910. db = db.Count(&total).Offset(offset).Limit(limit)
  911. err = db.Order("id desc").Find(&solutions).Error
  912. if err != nil {
  913. return
  914. }
  915. if len(solutions) > 0 {
  916. nilNameIds := make([]int64, 0)
  917. for _, solution := range solutions {
  918. if len(solution.ModeName) == 0 {
  919. nilNameIds = append(nilNameIds, solution.ModeId)
  920. }
  921. }
  922. if len(nilNameIds) > 0 {
  923. var modes []*models.TreatmentMode
  924. err = readDb.Model(&models.TreatmentMode{}).Where("id IN (?)", nilNameIds).Find(&modes).Error
  925. if err != nil {
  926. return
  927. }
  928. modesMap := make(map[int64]models.TreatmentMode, 0)
  929. for _, mode := range modes {
  930. modesMap[mode.ID] = *mode
  931. }
  932. for index, solution := range solutions {
  933. if _, exixt := modesMap[solution.ModeId]; exixt && len(solution.ModeName) == 0 {
  934. solutions[index].ModeName = modesMap[solution.ModeId].Name
  935. }
  936. }
  937. }
  938. }
  939. return
  940. }
  941. // GetPatientDryWeightAdjustList 返回患者调整干体重的列表
  942. func GetPatientDryWeightAdjustList(orgID int64, patientID int64, page int64, limit int64) (weights []*models.DryWeightAdjust, total int64, err error) {
  943. db := readDb.Table("xt_dry_weight_adjust as dwa").Where("dwa.status=1")
  944. if orgID > 0 {
  945. db = db.Where("dwa.user_org_id=?", orgID)
  946. }
  947. if patientID > 0 {
  948. db = db.Where("dwa.patient_id=?", patientID)
  949. }
  950. db = db.Select("dwa.id, dwa.user_org_id, dwa.patient_id, dwa.weight, dwa.adjusted_value, dwa.doctor, dwa.registrars_id, dwa.remark, dwa.status, dwa.created_time, dwa.updated_time").Count(&total).Order("id desc")
  951. if page > 0 && limit > 0 {
  952. offset := (page - 1) * limit
  953. db = db.Offset(offset).Limit(limit)
  954. }
  955. err = db.Find(&weights).Error
  956. return
  957. }
  958. func CreateDryWeightAdjust(m *models.DryWeightAdjust) (err error) {
  959. err = writeDb.Create(m).Error
  960. return
  961. }
  962. func FindPatientLastDryWeightAdjust(orgID int64, id int64) (weight models.DryWeightAdjust, err error) {
  963. err = readDb.Model(&models.DryWeightAdjust{}).Where("user_org_id=? and patient_id=? and status=1", orgID, id).Order("id desc").First(&weight).Error
  964. return
  965. }
  966. func UpdateDoctorAdviceById(id int64, prescribing_number float64, single_dose float64) error {
  967. err := XTWriteDB().Model(&models.DoctorAdvice{}).Where("id=? and status =1", id).Updates(map[string]interface{}{"prescribing_number": prescribing_number, "single_dose": single_dose}).Error
  968. return err
  969. }
  970. func CreateDoctorAdvice(m *models.DoctorAdvice) (err error) {
  971. return writeDb.Create(m).Error
  972. }
  973. func GetMaxAdviceGroupID(orgId int64) (group int64) {
  974. var advice models.DoctorAdvice
  975. err := readDb.Table("xt_doctor_advice").Where("user_org_id=?", orgId).Select("max(groupno) as groupno").First(&advice).Error
  976. if err != nil {
  977. fmt.Println(err)
  978. group = 0
  979. }
  980. group = advice.GroupNo
  981. return
  982. }
  983. func CreateGroupAdvice(orgId int64, group int64, advices []*models.GroupAdvice) (err error) {
  984. if group == 0 {
  985. group = GetMaxAdviceGroupID(orgId) + 1
  986. }
  987. tx := writeDb.Begin()
  988. defer func() {
  989. if r := recover(); r != nil {
  990. tx.Rollback()
  991. }
  992. }()
  993. for _, advice := range advices {
  994. advice.GroupNo = group
  995. if err = tx.Create(advice).Error; err != nil {
  996. tx.Rollback()
  997. return
  998. }
  999. }
  1000. tx.Commit()
  1001. return
  1002. }
  1003. func CreateMGroupAdvice(orgId int64, advices []*models.GroupAdvice, groupNo int64) (list []*models.GroupAdvice, err error) {
  1004. if groupNo <= 0 {
  1005. group := GetMaxAdviceGroupID(orgId)
  1006. groupNo = group + 1
  1007. }
  1008. tx := writeDb.Begin()
  1009. defer func() {
  1010. if r := recover(); r != nil {
  1011. tx.Rollback()
  1012. }
  1013. }()
  1014. for _, advice := range advices {
  1015. advice.GroupNo = groupNo
  1016. if err = tx.Create(advice).Error; err != nil {
  1017. tx.Rollback()
  1018. return
  1019. }
  1020. list = append(list, advice)
  1021. if len(advice.Children) > 0 {
  1022. for _, child := range advice.Children {
  1023. child.GroupNo = groupNo
  1024. child.ParentId = advice.ID
  1025. fmt.Println(child)
  1026. if err = tx.Create(&child).Error; err != nil {
  1027. tx.Rollback()
  1028. return
  1029. }
  1030. list = append(list, child)
  1031. }
  1032. }
  1033. }
  1034. tx.Commit()
  1035. return
  1036. }
  1037. func CreateMGroupAdviceOne(orgId int64, advices []*models.GroupAdvice, groupNo int64) (list []*models.GroupAdvice, err error) {
  1038. if groupNo <= 0 {
  1039. group := GetMaxAdviceGroupID(orgId)
  1040. groupNo = group + 1
  1041. }
  1042. tx := writeDb.Begin()
  1043. defer func() {
  1044. if r := recover(); r != nil {
  1045. tx.Rollback()
  1046. }
  1047. }()
  1048. for _, advice := range advices {
  1049. advice.GroupNo = advice.GroupNo
  1050. if err = tx.Create(advice).Error; err != nil {
  1051. tx.Rollback()
  1052. return
  1053. }
  1054. list = append(list, advice)
  1055. if len(advice.Children) > 0 {
  1056. for _, child := range advice.Children {
  1057. child.GroupNo = advice.GroupNo
  1058. child.ParentId = advice.ID
  1059. fmt.Println(child)
  1060. if err = tx.Create(&child).Error; err != nil {
  1061. tx.Rollback()
  1062. return
  1063. }
  1064. list = append(list, child)
  1065. }
  1066. }
  1067. }
  1068. tx.Commit()
  1069. return
  1070. }
  1071. func UpdateDoctorAdvice(m *models.DoctorAdvice) (err error) {
  1072. return writeDb.Save(m).Error
  1073. }
  1074. func StopGroupAdvice(orgId int64, groupNo int64, m *models.DoctorAdvice) (err error) {
  1075. err = writeDb.Model(&models.DoctorAdvice{}).Where("user_org_id=? and groupno=?", orgId, groupNo).Update(map[string]interface{}{"UpdatedTime": m.UpdatedTime, "StopState": 1, "StopReason": m.StopReason, "StopDoctor": m.StopDoctor, "StopTime": m.StopTime}).Error
  1076. if err != nil {
  1077. return
  1078. }
  1079. return
  1080. }
  1081. func StopDoctorAdvice(m *models.DoctorAdvice) (err error) {
  1082. ut := writeDb.Begin()
  1083. err = ut.Save(m).Error
  1084. if err != nil {
  1085. ut.Rollback()
  1086. return
  1087. }
  1088. err = ut.Model(&models.DoctorAdvice{}).Where("parent_id=?", m.ID).Update(map[string]interface{}{"UpdatedTime": m.UpdatedTime, "StopState": 1, "StopReason": m.StopReason, "StopDoctor": m.StopDoctor, "StopTime": m.StopTime, "Modifier": m.Modifier}).Error
  1089. if err != nil {
  1090. ut.Rollback()
  1091. return
  1092. }
  1093. ut.Commit()
  1094. return
  1095. }
  1096. func DeleteSolution(m *models.DialysisSolution) (err error) {
  1097. if m.ParentId > 0 {
  1098. return writeDb.Save(m).Error
  1099. } else {
  1100. ut := writeDb.Begin()
  1101. err = ut.Save(m).Error
  1102. if err != nil {
  1103. ut.Rollback()
  1104. return
  1105. }
  1106. err = ut.Model(&models.DialysisSolution{}).Where("parent_id=?", m.ID).Update(map[string]interface{}{"UpdatedTime": m.UpdatedTime, "Status": 0}).Error
  1107. if err != nil {
  1108. ut.Rollback()
  1109. return
  1110. }
  1111. ut.Commit()
  1112. }
  1113. return
  1114. }
  1115. func DeleteDoctorAdvice(m *models.DoctorAdvice) (err error) {
  1116. if m.ParentId > 0 {
  1117. return writeDb.Save(m).Error
  1118. } else {
  1119. ut := writeDb.Begin()
  1120. err = ut.Save(m).Error
  1121. if err != nil {
  1122. ut.Rollback()
  1123. return
  1124. }
  1125. err = ut.Model(&models.DoctorAdvice{}).Where("parent_id=? AND user_org_id = ?", m.ID, m.UserOrgId).Update(map[string]interface{}{"UpdatedTime": m.UpdatedTime, "Status": 0, "Modifier": m.Modifier}).Error
  1126. if err != nil {
  1127. ut.Rollback()
  1128. return
  1129. }
  1130. ut.Commit()
  1131. }
  1132. return
  1133. }
  1134. func DeleteGroupAdvice(orgId int64, groupNo int64, admin_user_id int64) (err error) {
  1135. err = writeDb.Model(&models.DoctorAdvice{}).Where("user_org_id = ? and groupno = ?", orgId, groupNo).Update(map[string]interface{}{"UpdatedTime": time.Now().Unix(), "Status": 0, "Modifier": admin_user_id}).Error
  1136. if err != nil {
  1137. return
  1138. }
  1139. return
  1140. }
  1141. func DeleteGroupAdviceOne(orgId int64, groupNo int64, admin_user_id int64) (err error) {
  1142. err = writeDb.Model(&models.DoctorAdvice{}).Where("user_org_id = ? and groupno = ? and status =1 and advice_type=1", orgId, groupNo).Update(map[string]interface{}{"UpdatedTime": time.Now().Unix(), "Status": 0, "Modifier": admin_user_id}).Error
  1143. if err != nil {
  1144. return
  1145. }
  1146. return
  1147. }
  1148. func FindDoctorAdvice(orgID, id int64) (advice models.DoctorAdvice, err error) {
  1149. err = readDb.Model(&models.DoctorAdvice{}).Where("id = ? and user_org_id=? and status = 1", id, orgID).First(&advice).Error
  1150. return
  1151. }
  1152. func FindDoctorAdviceOne(orgID, patient_id int64) (advice models.DoctorAdvice, err error) {
  1153. err = readDb.Model(&models.DoctorAdvice{}).Where("patient_id = ? and user_org_id=? and status = 1", patient_id, orgID).First(&advice).Error
  1154. return
  1155. }
  1156. func FindDoctorAdviceTwo(ids []string, orgID int64) (advice []*models.DoctorAdvice, err error) {
  1157. err = readDb.Model(&models.DoctorAdvice{}).Where("id in(?) and user_org_id=? and status = 1", ids, orgID).First(&advice).Error
  1158. return
  1159. }
  1160. func FindHisDoctorAdvice(orgID, id int64) (advice models.HisDoctorAdviceInfo, err error) {
  1161. err = readDb.Model(&models.HisDoctorAdviceInfo{}).Where("id = ? and user_org_id=? and status = 1", id, orgID).First(&advice).Error
  1162. return
  1163. }
  1164. func FindHisDoctorAdviceTwo(ids []string, orgID int64) (advice []*models.HisDoctorAdviceInfo, err error) {
  1165. err = readDb.Model(&models.HisDoctorAdviceInfo{}).Where("id in(?) and user_org_id=? and status = 1", ids, orgID).First(&advice).Error
  1166. return
  1167. }
  1168. func FindHisDoctorAdviceOne(orgID, patient_id int64) (advice models.HisDoctorAdviceInfo, err error) {
  1169. err = readDb.Model(&models.HisDoctorAdviceInfo{}).Where("patient_id = ? and user_org_id=? and status = 1", patient_id, orgID).First(&advice).Error
  1170. return
  1171. }
  1172. func FindDoctorAdviceByGroupNo(orgID, groupNo int64) (advice models.DoctorAdvice, err error) {
  1173. err = readDb.Model(&models.DoctorAdvice{}).Where("groupno = ? and user_org_id=? and status = 1", groupNo, orgID).First(&advice).Error
  1174. return
  1175. }
  1176. func GetDoctorAdviceList(orgID, patientID, advice_type, stop, start, end int64, keywords string) (advices []*models.DoctorAdvices, total int64, err error) {
  1177. db := readDb.Table("xt_doctor_advice as x").Where("x.status = 1")
  1178. table := UserReadDB().Table("sgj_user_admin_role as r")
  1179. fmt.Print("table", table)
  1180. if orgID > 0 {
  1181. db = db.Where("x.user_org_id=?", orgID)
  1182. }
  1183. if patientID > 0 {
  1184. db = db.Where("x.patient_id = ?", patientID)
  1185. }
  1186. if advice_type > 0 {
  1187. db = db.Where("x.advice_type = ?", advice_type)
  1188. } else if advice_type == 0 {
  1189. db = db.Where("x.advice_type in (?)", []int{1, 3})
  1190. }
  1191. if stop == 1 {
  1192. db = db.Where("(x.stop_state=? or x.execution_state=?)", stop, stop)
  1193. } else if stop == 2 {
  1194. db = db.Where("x.stop_state=? and x.execution_state=?", stop, stop)
  1195. }
  1196. if start != 0 {
  1197. db = db.Where("x.start_time>=?", start)
  1198. }
  1199. if end != 0 {
  1200. db = db.Where("start_time<=?", end)
  1201. }
  1202. if len(keywords) > 0 {
  1203. likeKey := "%" + keywords + "%"
  1204. db = db.Where("x.advice_name LIKE ?", likeKey)
  1205. }
  1206. err = db.Group("x.id").Count(&total).Select("x.id, x.user_org_id, x.patient_id, x.advice_type, x.advice_date, x.record_date, x.start_time, x.advice_name,x.advice_desc, x.reminder_date, x.drug_spec, x.drug_spec_unit, x.single_dose, x.single_dose_unit, x.prescribing_number, x.prescribing_number_unit, x.delivery_way, x.execution_frequency, x.advice_doctor, x.status, x.created_time,x.updated_time, 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.execution_state, x.checker, x.check_state, x.check_time, x.groupno,x.remind_type,x.frequency_type,x.day_count,x.week_day,x.parent_id,r.user_name, IF(x.parent_id > 0, x.parent_id, x.id) as advice_order").Joins("Left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.advice_doctor").Order("start_time desc, groupno desc, advice_order desc, id asc").Scan(&advices).Error
  1207. fmt.Print("err", err)
  1208. return
  1209. }
  1210. func GetLongDoctorAdviceList(orgID, patientID, advice_type, stop, start, end int64, keywords string, page int64, limit int64) (advices []*models.DoctorAdvices, total int64, err error) {
  1211. db := readDb.Table("xt_doctor_advice as x").Where("x.status = 1")
  1212. table := UserReadDB().Table("sgj_user_admin_role as r")
  1213. fmt.Print("table", table)
  1214. if orgID > 0 {
  1215. db = db.Where("x.user_org_id=?", orgID)
  1216. }
  1217. if patientID > 0 {
  1218. db = db.Where("x.patient_id = ?", patientID)
  1219. }
  1220. if advice_type == 1 && advice_type > 0 {
  1221. db = db.Where("x.advice_type = ?", advice_type)
  1222. }
  1223. if advice_type == 3 && advice_type > 0 {
  1224. db = db.Where("x.advice_type = 2 or x.advice_type = 3")
  1225. }
  1226. if advice_type == 4 && advice_type > 0 {
  1227. db = db.Where("x.advice_type = 4")
  1228. }
  1229. if stop == 1 {
  1230. db = db.Where("(x.stop_state=? or x.execution_state=?)", stop, stop)
  1231. } else if stop == 2 {
  1232. db = db.Where("x.stop_state=? and x.execution_state=?", stop, stop)
  1233. }
  1234. if start != 0 {
  1235. db = db.Where("x.start_time>=?", start)
  1236. }
  1237. if end != 0 {
  1238. db = db.Where("x.start_time<=?", end)
  1239. }
  1240. if len(keywords) > 0 {
  1241. likeKey := "%" + keywords + "%"
  1242. db = db.Where("x.advice_name LIKE ?", likeKey)
  1243. }
  1244. offset := (page - 1) * limit
  1245. err = db.Group("x.id").Count(&total).Offset(offset).Limit(limit).Select("x.id, x.user_org_id, x.patient_id, x.advice_type, x.advice_date, x.record_date, x.start_time, x.advice_name,x.advice_desc, x.reminder_date, x.drug_spec, x.drug_spec_unit, x.single_dose, x.single_dose_unit, x.prescribing_number, x.prescribing_number_unit, x.delivery_way, x.execution_frequency, x.advice_doctor, x.status, x.created_time,x.updated_time, 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.execution_state, x.checker, x.check_state, x.check_time, x.groupno,x.remind_type,x.frequency_type,x.day_count,x.week_day,x.parent_id,x.push_start_time,r.user_name, IF(x.parent_id > 0, x.parent_id, x.id) as advice_order").Joins("Left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.advice_doctor").Order("start_time desc, groupno desc, advice_order desc, id asc").Scan(&advices).Error
  1246. fmt.Print("err", err)
  1247. return
  1248. }
  1249. func GetDoctorAdviceListOne(orgID, patientID, advice_type, stop, start, end int64, keywords string, page int64, limit int64, delive_way_one string) (advices []*models.DoctorAdvices, total int64, err error) {
  1250. db := readDb.Table("xt_doctor_advice as x").Where("x.status = 1")
  1251. table := UserReadDB().Table("sgj_user_admin_role as r")
  1252. fmt.Print("table", table)
  1253. if len(delive_way_one) > 0 {
  1254. db = db.Where("delivery_way=?", delive_way_one)
  1255. }
  1256. if orgID > 0 {
  1257. db = db.Where("x.user_org_id=?", orgID)
  1258. }
  1259. if patientID > 0 {
  1260. db = db.Where("x.patient_id = ?", patientID)
  1261. }
  1262. if advice_type == 1 && advice_type > 0 {
  1263. db = db.Where("x.advice_type = ?", advice_type)
  1264. }
  1265. if advice_type == 2 && advice_type > 0 {
  1266. db = db.Where("x.advice_type = ?", advice_type)
  1267. }
  1268. if advice_type == 3 && advice_type > 0 {
  1269. db = db.Where("x.advice_type = 2 or x.advice_type = 3")
  1270. }
  1271. if advice_type == 4 && advice_type > 0 {
  1272. db = db.Where("x.advice_type = 4")
  1273. }
  1274. if stop == 1 {
  1275. db = db.Where("(x.stop_state=? or x.execution_state=?)", stop, stop)
  1276. } else if stop == 2 {
  1277. db = db.Where("x.stop_state=? and x.execution_state=?", stop, stop)
  1278. }
  1279. if start != 0 {
  1280. db = db.Where("x.start_time>=?", start)
  1281. }
  1282. if end != 0 {
  1283. db = db.Where("x.start_time<=?", end)
  1284. }
  1285. if len(keywords) > 0 {
  1286. likeKey := "%" + keywords + "%"
  1287. db = db.Where("x.advice_name LIKE ?", likeKey)
  1288. }
  1289. offset := (page - 1) * limit
  1290. err = db.Group("x.id").Count(&total).Offset(offset).Limit(limit).Select("x.id, x.user_org_id, x.patient_id, x.advice_type, x.advice_date, x.record_date, x.start_time, x.advice_name,x.advice_desc, x.reminder_date, x.drug_spec, x.drug_spec_unit, x.single_dose, x.single_dose_unit, x.prescribing_number, x.prescribing_number_unit, x.delivery_way, x.execution_frequency, x.advice_doctor, x.status, x.created_time,x.updated_time, 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.execution_state, x.checker, x.check_state, x.check_time, x.groupno,x.remind_type,x.frequency_type,x.day_count,x.week_day,x.parent_id,x.push_start_time,r.user_name, IF(x.parent_id > 0, x.parent_id, x.id) as advice_order").Joins("Left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.advice_doctor").Order("start_time desc, groupno desc, advice_order desc, id asc").Scan(&advices).Error
  1291. fmt.Print("err", err)
  1292. return
  1293. }
  1294. func GetDoctorAdviceListTwo(orgID int64, patientID int64, advice_type int64, stop int64, start int64, end int64, keywords string, limit int64, page int64, delive_way_one string) (advices []*models.DoctorAdvices, total int64, err error) {
  1295. db := readDb.Table("xt_doctor_advice as x").Where("x.status = 1")
  1296. table := UserReadDB().Table("sgj_user_admin_role as r")
  1297. fmt.Println(table)
  1298. if len(delive_way_one) > 0 {
  1299. db = db.Where("delivery_way=?", delive_way_one)
  1300. }
  1301. if orgID > 0 {
  1302. db = db.Where("x.user_org_id=?", orgID)
  1303. }
  1304. if patientID > 0 {
  1305. db = db.Where("x.patient_id = ?", patientID)
  1306. }
  1307. if advice_type == 1 {
  1308. db = db.Where("x.advice_type = ?", advice_type)
  1309. }
  1310. if advice_type == 3 && advice_type > 0 {
  1311. db = db.Where("x.advice_type = 2 or x.advice_type = 3")
  1312. }
  1313. if advice_type == 4 && advice_type > 0 {
  1314. db = db.Where("x.advice_type = 4")
  1315. }
  1316. if stop == 1 {
  1317. db = db.Where("(x.stop_state=? or x.execution_state=?)", stop, stop)
  1318. } else if stop == 2 {
  1319. db = db.Where("x.stop_state=? and x.execution_state=?", stop, stop)
  1320. }
  1321. if start != 0 {
  1322. db = db.Where("x.start_time>=?", start)
  1323. }
  1324. if end != 0 {
  1325. db = db.Where("x.start_time<=?", end)
  1326. }
  1327. if len(keywords) > 0 {
  1328. likeKey := "%" + keywords + "%"
  1329. db = db.Where("x.advice_name LIKE ?", likeKey)
  1330. }
  1331. offset := (page - 1) * limit
  1332. err = db.Order("x.start_time desc").Group("x.start_time").Count(&total).Offset(offset).Limit(limit).Select("x.id, x.user_org_id, x.patient_id, x.advice_type, x.advice_date, x.record_date, x.start_time, x.advice_name,x.advice_desc, x.reminder_date, x.drug_spec, x.drug_spec_unit, x.single_dose, x.single_dose_unit, x.prescribing_number, x.prescribing_number_unit, x.delivery_way, x.execution_frequency, x.advice_doctor, x.status, x.created_time,x.updated_time, 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.execution_state, x.checker, x.check_state, x.check_time, x.groupno,x.remind_type,x.frequency_type,x.day_count,x.week_day,x.parent_id,x.push_start_time,r.user_name, IF(x.parent_id > 0, x.parent_id, x.id) as advice_order").Joins("Left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.advice_doctor").Scan(&advices).Error
  1333. fmt.Print("错误是什么", err)
  1334. return
  1335. }
  1336. func GetDoctorAdvicePageList(orgID, patientID, advice_type, stop, start, end int64, keywords string, page, limit int64) (advices []*models.DoctorAdvice, total int64, err error) {
  1337. offset := (page - 1) * limit
  1338. db := readDb.Model(&models.DoctorAdvice{}).Where("status=1")
  1339. if orgID > 0 {
  1340. db = db.Where("user_org_id=?", orgID)
  1341. }
  1342. if patientID > 0 {
  1343. db = db.Where("patient_id = ?", patientID)
  1344. }
  1345. if advice_type > 0 {
  1346. db = db.Where("advice_type = ?", advice_type)
  1347. }
  1348. if stop == 1 {
  1349. db = db.Where("(stop_state=? or execution_state=?) and parent_id=0", stop, stop)
  1350. } else if stop == 2 {
  1351. db = db.Where("stop_state=? and execution_state=?", stop, stop)
  1352. }
  1353. if start != 0 {
  1354. db = db.Where("start_time>=?", start)
  1355. }
  1356. if end != 0 {
  1357. db = db.Where("start_time<=?", end)
  1358. }
  1359. if len(keywords) > 0 {
  1360. likeKey := "%" + keywords + "%"
  1361. db = db.Where("advice_name LIKE ?", likeKey)
  1362. }
  1363. err = db.Count(&total).Select("id,user_org_id,patient_id,advice_type,advice_date,start_time,advice_name,advice_desc,reminder_date,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 IF(parent_id>0, parent_id, id) as advice_order").Order("advice_order desc, id").Offset(offset).Limit(limit).Scan(&advices).Error
  1364. return
  1365. }
  1366. func CreateSubDoctorAdvice(advices []*models.DoctorAdvice) (err error) {
  1367. if len(advices) > 0 {
  1368. utx := writeDb.Begin()
  1369. if len(advices) > 0 {
  1370. thisSQL := "INSERT INTO xt_doctor_advice (single_dose_unit, prescribing_number, prescribing_number_unit, advice_name, advice_desc,single_dose,created_time,updated_time,patient_id,parent_id,user_org_id,record_date,advice_type,drug_spec_unit) VALUES "
  1371. insertParams := make([]string, 0)
  1372. insertData := make([]interface{}, 0)
  1373. for _, advice := range advices {
  1374. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1375. insertData = append(insertData, advice.SingleDoseUnit)
  1376. insertData = append(insertData, advice.PrescribingNumber)
  1377. insertData = append(insertData, advice.PrescribingNumberUnit)
  1378. insertData = append(insertData, advice.AdviceName)
  1379. insertData = append(insertData, advice.AdviceDesc)
  1380. insertData = append(insertData, advice.SingleDose)
  1381. insertData = append(insertData, advice.CreatedTime)
  1382. insertData = append(insertData, advice.UpdatedTime)
  1383. insertData = append(insertData, advice.PatientId)
  1384. insertData = append(insertData, advice.ParentId)
  1385. insertData = append(insertData, advice.UserOrgId)
  1386. insertData = append(insertData, advice.RecordDate)
  1387. insertData = append(insertData, 2)
  1388. insertData = append(insertData, advice.DrugSpecUnit)
  1389. }
  1390. thisSQL += strings.Join(insertParams, ", ")
  1391. err = utx.Exec(thisSQL, insertData...).Error
  1392. if err != nil {
  1393. utx.Rollback()
  1394. return
  1395. }
  1396. }
  1397. utx.Commit()
  1398. }
  1399. return
  1400. }
  1401. func GetPatientDialysisRecord(orgID, patientID int64, page, limit, start, end, mode_id int64) ([]*models.PatientDialysisRecord, int64, error) {
  1402. offset := (page - 1) * limit
  1403. var total int64
  1404. var err error
  1405. var orders []*models.PatientDialysisRecord
  1406. // err = readDb.Table("xt_dialysis_order as do").
  1407. // Preload("DialysisPrescription", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  1408. // Preload("PredialysisEvaluation", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  1409. // Preload("AssessmentAfterDislysis", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  1410. // Preload("TreatmentSummary", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  1411. // Joins("JOIN xt_schedule as s ON s.patient_id=? and FROM_UNIXTIME(s.schedule_date, '%Y-%m-%d')=FROM_UNIXTIME(do.dialysis_date, '%Y-%m-%d')", patientID).
  1412. // Joins("JOIN xt_device_zone as dz ON dz.org_id = ? and dz.id=s.partition_id", orgID).
  1413. // Where("do.patient_id=? and do.user_org_id=? and do.stage = 2 and do.status=1", patientID, orgID).Count(&total).Offset(offset).Limit(limit).Order("do.dialysis_date desc").Select(" do.id, do.dialysis_date, do.user_org_id, do.patient_id, do.prescription_id, do.stage, do.remark, do.status, do.created_time, do.updated_time, s.schedule_type, s.partition_id, dz.name as partition_name").Find(&orders).Error
  1414. db := readDb.Table("xt_dialysis_order as do").
  1415. Preload("DoctorAdvice", "patient_id =? and user_org_id = ? and status =1 and advice_type =2", patientID, orgID).
  1416. Preload("DialysisPrescription", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  1417. Preload("PredialysisEvaluation", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  1418. Preload("DialysisPrescription", func(db *gorm.DB) *gorm.DB {
  1419. return readDb.Where("patient_id=? and user_org_id=? and status=1", patientID, orgID).Preload("UserAdminRole", func(db *gorm.DB) *gorm.DB {
  1420. return readUserDb.Where("status = 1 and org_id=?", orgID)
  1421. })
  1422. }).
  1423. Preload("AssessmentAfterDislysis", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  1424. Preload("TreatmentSummary", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  1425. Preload("Device", "org_id=? and status=1", orgID).
  1426. Preload("UserAdminRole", func(db *gorm.DB) *gorm.DB {
  1427. return readUserDb.Where("org_id=?", orgID)
  1428. }).
  1429. Joins("JOIN xt_schedule as s ON s.patient_id=? and FROM_UNIXTIME(s.schedule_date, '%Y-%m-%d')=FROM_UNIXTIME(do.dialysis_date, '%Y-%m-%d')", patientID).
  1430. Joins("JOIN xt_device_zone as dz ON dz.org_id = ? and dz.id=s.partition_id", orgID).
  1431. Where("do.patient_id=? and do.user_org_id=? and do.status=1 and s.status=1", patientID, orgID).Group("s.schedule_date")
  1432. if start != 0 {
  1433. db = db.Where("do.dialysis_date>=?", start)
  1434. }
  1435. if end != 0 {
  1436. db = db.Where("do.dialysis_date<=?", end)
  1437. }
  1438. if mode_id > 0 {
  1439. db = db.Joins("JOIN xt_dialysis_prescription as dp ON dp.patient_id=do.patient_id and dp.user_org_id = do.user_org_id and dp.record_date = do.dialysis_date")
  1440. db = db.Where("dp.mode_id=?", mode_id)
  1441. }
  1442. err = db.Count(&total).Offset(offset).Limit(limit).Order("do.dialysis_date desc").Select("do.bed_id, do.id, do.dialysis_date, do.user_org_id, do.patient_id, do.prescription_id, do.stage, do.remark, do.status, do.created_time, do.updated_time,do.start_nurse,do.finish_nurse ,do.start_time,do.end_time,do.url,s.schedule_type, s.partition_id, dz.name as partition_name").Find(&orders).Error
  1443. if len(orders) > 0 {
  1444. ids := make([]int64, 0)
  1445. for _, order := range orders {
  1446. dialyzer := order.DialysisPrescription.Dialyzer
  1447. ids = append(ids, dialyzer)
  1448. }
  1449. if len(ids) > 0 {
  1450. var dialyzers []*models.DeviceNumber
  1451. err = readDb.Model(&models.DeviceNumber{}).Where("id IN (?) and org_id=? and status=1", ids, orgID).Find(&dialyzers).Error
  1452. if err != nil {
  1453. return nil, 0, err
  1454. }
  1455. dialyzerMap := make(map[int64]models.DeviceNumber, 0)
  1456. for _, item := range dialyzers {
  1457. dialyzerMap[item.ID] = *item
  1458. }
  1459. for orderIndex, order := range orders {
  1460. if _, exist := dialyzerMap[order.DialysisPrescription.Dialyzer]; exist {
  1461. orders[orderIndex].DeviceNumber = dialyzerMap[order.DialysisPrescription.Dialyzer].Number
  1462. }
  1463. }
  1464. }
  1465. }
  1466. return orders, total, err
  1467. }
  1468. func GetPatientTreatmentSummaryList(orgID, patientID, page, limit, start, end int64) (list []*models.TreatmentSummary, total int, err error) {
  1469. offset := (page - 1) * limit
  1470. fmt.Println(offset)
  1471. fmt.Println(limit)
  1472. db := readDb.Model(&models.TreatmentSummary{}).Where("user_org_id = ? and patient_id=?", orgID, patientID)
  1473. if start != 0 {
  1474. db = db.Where("assessment_date >= ?", start)
  1475. }
  1476. if end != 0 {
  1477. db = db.Where("assessment_date <= ?", end)
  1478. }
  1479. db = db.Where("status=1")
  1480. err = db.Count(&total).Offset(offset).Limit(limit).Order("assessment_date desc, id desc").Find(&list).Error
  1481. return
  1482. }
  1483. func GetPatientScheduleList(orgID int64, patientID int64, page int64, limit int64, start int64) (schedules []*models.PatientSchedule, err error) {
  1484. offset := (page - 1) * limit
  1485. err = readDb.Table("xt_schedule as s").
  1486. Preload("DeviceZone", "org_id=? and status=1", orgID).
  1487. Preload("DeviceNumber", "org_id=? and status=1", orgID).
  1488. Preload("TreatmentMode", "status=1").
  1489. Where("s.patient_id =? and s.user_org_id=? and s.schedule_date>=? and s.status=1", patientID, orgID, start).
  1490. Limit(limit).Offset(offset).Find(&schedules).Error
  1491. return
  1492. }
  1493. func GetMonitorRecord(orgID int64, date int64, partition int64, patientId int64) ([]*models.NewVMMonitorDialysisSchedule, error) {
  1494. var mds []*models.NewVMMonitorDialysisSchedule
  1495. db := readDb.Model(&models.NewVMMonitorDialysisSchedule{})
  1496. if date > 0 {
  1497. db = db.Where("schedule_date = ?", date)
  1498. }
  1499. if partition > 0 {
  1500. db = db.Where("partition_id = ?", partition)
  1501. }
  1502. if patientId > 0 {
  1503. db = db.Where("patient_id = ?", patientId)
  1504. }
  1505. err = db.Where("status = 1 and user_org_id = ?", orgID).Find(&mds).Error
  1506. return mds, err
  1507. }
  1508. func MobileGetMonitorsWithPatient(orgID int64, keyword string, page int) ([]*models.MonitorDialysisSchedule, error) {
  1509. var patients []*models.Patients
  1510. getPatientErr := readDb.Model(&models.Patients{}).Where("status = 1 AND user_org_id = ? AND (name like ? OR dialysis_no like ?)", orgID, "%"+keyword+"%", "%"+keyword+"%").Find(&patients).Error
  1511. if getPatientErr != nil {
  1512. return nil, getPatientErr
  1513. }
  1514. patientIDs := make([]int64, len(patients))
  1515. for index, patient := range patients {
  1516. patientIDs[index] = patient.ID
  1517. }
  1518. db := readDb.
  1519. Model(&models.MonitorDialysisSchedule{}).
  1520. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  1521. Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  1522. Preload("TreatmentMode", "status = 1").
  1523. Preload("Prescription", "status = 1 AND user_org_id = ?", orgID).
  1524. Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ?", orgID).
  1525. Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ?", orgID).
  1526. Preload("MonitoringRecord", "status = 1 AND user_org_id = ?", orgID).
  1527. Preload("DialysisOrder", "status = 1 AND user_org_id = ?", orgID).
  1528. Preload("MonitorPatients", "status = 1 AND user_org_id = ?", orgID).
  1529. Where("status = 1 AND user_org_id = ? AND patient_id in (?)", orgID, patientIDs)
  1530. var schedules []*models.MonitorDialysisSchedule
  1531. err := db.Offset(20 * (page - 1)).Limit(20).Order("schedule_date desc").Find(&schedules).Error
  1532. return schedules, err
  1533. }
  1534. func GetPatientByKeyWord(orgID int64, keywords string) (patient []*models.Patients, err error) {
  1535. db := readDb.Model(&models.Patients{}).Where("user_org_id=? and status=1 and lapseto = 1", orgID)
  1536. if len(keywords) > 0 {
  1537. likekey := "%" + keywords + "%"
  1538. err = db.Where("name LIKE ? OR dialysis_no LIKE ? OR first_letter LIKE ?", likekey, likekey, likekey).Find(&patient).Error
  1539. } else {
  1540. err = db.Find(&patient).Error
  1541. }
  1542. return
  1543. }
  1544. func FindDoctorAdviceByGoroupNo(orgID int64, groupno int64) (advice models.DoctorAdvice, err error) {
  1545. err = readDb.Model(&models.DoctorAdvice{}).Where("user_org_id=? AND groupno = ? AND status = 1", orgID, groupno).First(&advice).Error
  1546. return
  1547. }
  1548. func FindOldDoctorAdvice(orgID int64, advice_id int64) (advice models.DoctorAdvice, err error) {
  1549. err = readDb.Model(&models.DoctorAdvice{}).Where("id = ? AND user_org_id=? AND status = 1", advice_id, orgID).First(&advice).Error
  1550. return
  1551. }
  1552. func UpdateAdviceGroupStartTime(orgID int64, groupNO int64, startTime int64, admin_user_id int64) error {
  1553. now := time.Now().Unix()
  1554. err := writeDb.Model(&models.DoctorAdvice{}).Where("user_org_id = ? AND status = 1 AND execution_state <> 1 AND check_state <> 1 AND groupno = ? AND admin_user_id = ?", orgID, groupNO, admin_user_id).Updates(map[string]interface{}{
  1555. "start_time": startTime,
  1556. "updated_time": now,
  1557. "modifier": admin_user_id,
  1558. }).Error
  1559. return err
  1560. }
  1561. func QueryPatientById(id int64) *models.Patients {
  1562. var pat models.Patients
  1563. err := readDb.Model(&models.Patients{}).Where("id=?", id).First(&pat).Error
  1564. if err != nil {
  1565. }
  1566. return &pat
  1567. }
  1568. func FindAllDoctorAdviceByGoroupNo(orgID int64, groupno int64) (advice []models.DoctorAdvice, err error) {
  1569. err = readDb.Model(&models.DoctorAdvice{}).Where("user_org_id=? AND groupno = ? AND status = 1", orgID, groupno).Find(&advice).Error
  1570. return
  1571. }
  1572. func FindAllDoctorAdviceByGoroupNoOne(orgID int64, groupno int64) (advice []models.DoctorAdvice, err error) {
  1573. err = readDb.Model(&models.DoctorAdvice{}).Where("user_org_id=? AND groupno = ? AND status = 1 and execution_state =2", orgID, groupno).Find(&advice).Error
  1574. return
  1575. }
  1576. func UpdateDoctorAdviceByGoroupNo(id int64, drug_id int64) error {
  1577. err := XTWriteDB().Model(&models.DoctorAdvice{}).Where("id = ? and status =1", id).Updates(map[string]interface{}{"drug_id": drug_id, "drug_name_id": drug_id, "way": 1}).Error
  1578. return err
  1579. }
  1580. func FindAllDoctorAdviceByGoroupNoTwety(orgID int64, groupno int64) (advice []models.DoctorAdvice, err error) {
  1581. err = readDb.Model(&models.DoctorAdvice{}).Where("user_org_id=? AND groupno = ? AND status = 1 and (drug_id >0 or drug_name_id >0)", orgID, groupno).Find(&advice).Error
  1582. return
  1583. }
  1584. func FindDoctorAdviceByIds(orgID int64, ids []string) (advice []models.DoctorAdvice, err error) {
  1585. err = readDb.Model(&models.DoctorAdvice{}).Where("id IN (?) AND user_org_id = ? AND status = 1", ids, orgID).Find(&advice).Error
  1586. return
  1587. }
  1588. func FindDoctorAdviceByIdsOne(orgID int64, ids []string) (advice []models.DoctorAdvice, err error) {
  1589. err = readDb.Model(&models.DoctorAdvice{}).Where("id IN (?) AND user_org_id = ? AND status = 1 and execution_state =2", ids, orgID).Find(&advice).Error
  1590. return
  1591. }
  1592. func FindDoctorAdviceByIdsTwety(orgID int64, ids []string) (advice []models.DoctorAdvice, err error) {
  1593. err = readDb.Model(&models.DoctorAdvice{}).Where("id IN (?) AND user_org_id = ? AND status = 1 and (drug_id >0 or drug_name_id >0)", ids, orgID).Find(&advice).Error
  1594. return
  1595. }
  1596. func GetBatchDoctorAdviceList(ids []string, user_org_id int64) (advice []*models.DoctorAdvice, err error) {
  1597. err = XTReadDB().Where("id in(?) and user_org_id = ? and status =1", ids, user_org_id).Find(&advice).Error
  1598. return advice, err
  1599. }
  1600. func BatchDeleteDoctorAdvice(ids []string, user_id int64) (err error) {
  1601. ut := writeDb.Begin()
  1602. err = ut.Model(&models.DoctorAdvice{}).Where("status = 1 AND id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "updated_time": time.Now().Unix(), "modifier": user_id}).Error
  1603. if err != nil {
  1604. ut.Rollback()
  1605. return
  1606. }
  1607. err = ut.Model(&models.DoctorAdvice{}).Where("status = 1 AND parent_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "updated_time": time.Now().Unix(), "modifier": user_id}).Error
  1608. if err != nil {
  1609. ut.Rollback()
  1610. return
  1611. }
  1612. ut.Commit()
  1613. return err
  1614. }
  1615. func FindAdviceByGoroupNo(orgID int64, groupno int64) (advice []models.DoctorAdvice, err error) {
  1616. err = readDb.Model(&models.DoctorAdvice{}).Where("user_org_id=? AND groupno = ? AND status = 1 AND parent_id = 0", orgID, groupno).Find(&advice).Error
  1617. return
  1618. }
  1619. func UpdateDoctorAdviceAndSubAdvice(m *models.DoctorAdvice) (err error) {
  1620. ut := writeDb.Begin()
  1621. err = ut.Save(m).Error
  1622. if err != nil {
  1623. ut.Rollback()
  1624. return
  1625. }
  1626. err = ut.Model(&models.DoctorAdvice{}).Where("status = 1 AND parent_id IN (?)", m.ID).Updates(map[string]interface{}{"start_time": m.StartTime, "groupno": m.GroupNo, "updated_time": time.Now().Unix()}).Error
  1627. if err != nil {
  1628. ut.Rollback()
  1629. return
  1630. }
  1631. ut.Commit()
  1632. return err
  1633. }
  1634. func GetAllWaitRemindPatient(org_id int64, page int64, limit int64) (total int64, patient []*models.Patients, err error) {
  1635. type Total struct {
  1636. Count int64
  1637. }
  1638. var totals Total
  1639. offset := (page - 1) * limit
  1640. err = readDb.Raw("select * from xt_patients where user_org_id = ? AND lapseto = 1 AND infectious_next_record_time > 0 AND status = 1 AND date_sub(DATE_FORMAT(date(from_unixtime(`xt_patients`.`infectious_next_record_time`)),'%Y-%m-%d'), interval 7 day) <= now() Order by infectious_next_record_time", org_id).Offset(offset).Limit(limit).Scan(&patient).Error
  1641. readDb.Raw("select Count(id) as count from xt_patients where user_org_id = ? AND lapseto = 1 AND infectious_next_record_time > 0 AND status = 1 AND date_sub(DATE_FORMAT(date(from_unixtime(`xt_patients`.`infectious_next_record_time`)),'%Y-%m-%d'), interval 7 day) <= now() Order by infectious_next_record_time", org_id).Scan(&totals)
  1642. return totals.Count, patient, err
  1643. }
  1644. func UpdatePatientRemindStatus(patient_id int64, remind int64, org_id int64) (err error) {
  1645. err = writeDb.Model(&models.Patients{}).Where("status = 1 AND id = ? AND user_org_id = ?", patient_id, org_id).Updates(map[string]interface{}{"is_open_remind": remind}).Error
  1646. return
  1647. }
  1648. func CreatePatientWeightAdjust(m *models.SgjPatientDryweight) (err error) {
  1649. err = writeDb.Create(m).Error
  1650. return
  1651. }
  1652. func GetPatientAssesmentBefor(patient_id int64, assessment_date int64, org_id int64) (models.XtAssessmentBeforeDislysis, error) {
  1653. beforeDislysis := models.XtAssessmentBeforeDislysis{}
  1654. err := XTReadDB().Where("patient_id = ? and assessment_date = ? and user_org_id = ? and status=1", patient_id, assessment_date, org_id).Find(&beforeDislysis).Error
  1655. return beforeDislysis, err
  1656. }
  1657. func GetPatientDialysisPrescription(patient_id int64, record_date int64, org_id int64) (models.XtDialysisPrescription, error) {
  1658. prescription := models.XtDialysisPrescription{}
  1659. err := XTReadDB().Where("patient_id = ? and record_date = ? and user_org_id = ? and status=1", patient_id, record_date, org_id).Find(&prescription).Error
  1660. return prescription, err
  1661. }
  1662. func UpdatePatientAssesmentBefor(id int64, dry_weight float64) (models.XtAssessmentBeforeDislysis, error) {
  1663. beforeDislysis := models.XtAssessmentBeforeDislysis{}
  1664. err := XTWriteDB().Model(&beforeDislysis).Where("id = ? and status=1", id).Update(map[string]interface{}{"dry_weight": dry_weight}).Error
  1665. return beforeDislysis, err
  1666. }
  1667. func UpdatePatientDialysisPrscription(id int64, dewater_amount float64) (models.XtDialysisPrescription, error) {
  1668. prescription := models.XtDialysisPrescription{}
  1669. err := XTWriteDB().Model(&prescription).Where("id = ? and status =1", id).Update(map[string]interface{}{"target_ultrafiltration": dewater_amount, "prescription_water": dewater_amount}).Error
  1670. return prescription, err
  1671. }
  1672. func FindLastDryWeightAdjust(orgID int64, id int64) (weight models.SgjPatientDryweight, err error) {
  1673. err = readDb.Model(&models.SgjPatientDryweight{}).Where("user_org_id=? and patient_id=? and status=1", orgID, id).Order("id desc").First(&weight).Error
  1674. return
  1675. }
  1676. func GetSchedualPatientByKeyWord(orgID int64, keywords string, date int64) (patient []*models.Patients, err error) {
  1677. db := readDb.Model(&models.Patients{}).Where("user_org_id=? and status=1 and lapseto = 1 ", orgID)
  1678. if len(keywords) > 0 {
  1679. likekey := "%" + keywords + "%"
  1680. err = db.Where("(name LIKE ? OR dialysis_no LIKE ?) AND NOT EXISTS (Select * FROM `xt_dialysis_order` as d Where d.`dialysis_date` = ? AND d.`status` = 1 AND d.`patient_id` = xt_patients.id AND d.user_org_id = xt_patients.user_org_id)", likekey, likekey, date).Find(&patient).Error
  1681. } else {
  1682. err = db.Find(&patient).Error
  1683. }
  1684. return
  1685. }
  1686. func GetPatientScheduleOne(patientid int64, nowdate int64, orgid int64) (models.XtSchedule, error) {
  1687. schedule := models.XtSchedule{}
  1688. err := XTReadDB().Model(&schedule).Where("patient_id = ? and schedule_date = ? and user_org_id = ? and status =1", patientid, nowdate, orgid).Find(&schedule).Error
  1689. return schedule, err
  1690. }
  1691. func CreateExportPatient(patient *models.Patients, contagions []int64, org_creator int64) (err error) {
  1692. user, _ := GetSgjUserByMobild(patient.Phone)
  1693. customer, _ := GetSgjCoustomerByMobile(patient.UserOrgId, patient.Phone)
  1694. utx := writeDb.Begin()
  1695. btx := writeUserDb.Begin()
  1696. if user.ID == 0 {
  1697. user.Mobile = patient.Phone
  1698. user.Avatar = patient.Avatar
  1699. user.AvatarThumb = patient.Avatar
  1700. user.Birthday = patient.Birthday
  1701. user.Username = patient.Name
  1702. user.Gender = patient.Gender
  1703. user.Sources = 11
  1704. user.Introduce = patient.Remark
  1705. user.Status = 1
  1706. user.UpdatedTime = patient.UpdatedTime
  1707. user.CreatedTime = patient.CreatedTime
  1708. err = btx.Create(&user).Error
  1709. if err != nil {
  1710. utx.Rollback()
  1711. btx.Rollback()
  1712. return
  1713. }
  1714. }
  1715. patient.UserId = user.ID
  1716. if customer == nil {
  1717. err = btx.Create(&models.SgjCustomer{
  1718. UserOrgId: patient.UserOrgId,
  1719. UserId: user.ID,
  1720. Mobile: patient.Phone,
  1721. Name: patient.Name,
  1722. Gender: patient.Gender,
  1723. Birthday: patient.Birthday,
  1724. Sources: 11,
  1725. Status: 1,
  1726. CreatedTime: patient.CreatedTime,
  1727. UpdatedTime: patient.UpdatedTime,
  1728. Avatar: patient.Avatar,
  1729. Remark: patient.Remark,
  1730. }).Error
  1731. if err != nil {
  1732. utx.Rollback()
  1733. btx.Rollback()
  1734. return
  1735. }
  1736. }
  1737. err = utx.Create(patient).Error
  1738. if err != nil {
  1739. utx.Rollback()
  1740. btx.Rollback()
  1741. return
  1742. }
  1743. if patient.DryWeight > 0 {
  1744. var dryWeight models.SgjPatientDryweight
  1745. dryWeight.PatientId = patient.ID
  1746. dryWeight.UserOrgId = patient.UserOrgId
  1747. dryWeight.Status = 1
  1748. dryWeight.AdjustedValue = "/"
  1749. dryWeight.Creator = org_creator
  1750. dryWeight.UserId = org_creator
  1751. dryWeight.Ctime = time.Now().Unix()
  1752. dryWeight.Mtime = time.Now().Unix()
  1753. dryWeight.DryWeight = patient.DryWeight
  1754. dryWeight.Remakes = ""
  1755. err = utx.Create(&dryWeight).Error
  1756. }
  1757. var lapseto models.PatientLapseto
  1758. lapseto.PatientId = patient.ID
  1759. lapseto.LapsetoType = patient.Lapseto
  1760. lapseto.CreatedTime = patient.CreatedTime
  1761. lapseto.UpdatedTime = patient.CreatedTime
  1762. lapseto.Status = 1
  1763. lapseto.LapsetoTime = patient.CreatedTime
  1764. err = utx.Create(&lapseto).Error
  1765. if err != nil {
  1766. utx.Rollback()
  1767. btx.Rollback()
  1768. return
  1769. }
  1770. if len(contagions) > 0 {
  1771. thisSQL := "INSERT INTO xt_patients_infectious_diseases (patient_id, disease_id, status, created_time, updated_time) VALUES "
  1772. insertParams := make([]string, 0)
  1773. insertData := make([]interface{}, 0)
  1774. for _, contagion := range contagions {
  1775. insertParams = append(insertParams, "(?, ?, ?, ?, ?)")
  1776. insertData = append(insertData, patient.ID)
  1777. insertData = append(insertData, contagion)
  1778. insertData = append(insertData, 1)
  1779. insertData = append(insertData, patient.CreatedTime)
  1780. insertData = append(insertData, patient.UpdatedTime)
  1781. }
  1782. thisSQL += strings.Join(insertParams, ", ")
  1783. err = utx.Exec(thisSQL, insertData...).Error
  1784. if err != nil {
  1785. utx.Rollback()
  1786. btx.Rollback()
  1787. return
  1788. }
  1789. }
  1790. patientsNew := models.XtPatientsNew{
  1791. Name: patient.Name,
  1792. Gender: patient.Gender,
  1793. Phone: patient.Phone,
  1794. IdCardNo: patient.IdCardNo,
  1795. FirstDialysisDate: patient.FirstDialysisDate,
  1796. Source: patient.Source,
  1797. Lapseto: patient.Lapseto,
  1798. IsInfectious: patient.IsInfectious,
  1799. DialysisNo: patient.DialysisNo,
  1800. Height: patient.Height,
  1801. HomeAddress: patient.HomeAddress,
  1802. IsExcelExport: 1,
  1803. BloodPatients: 1,
  1804. Status: 1,
  1805. CreatedTime: time.Now().Unix(),
  1806. UserOrgId: patient.UserOrgId,
  1807. BloodId: patient.ID,
  1808. Avatar: "https://images.shengws.com/201809182128111.png",
  1809. }
  1810. err = utx.Create(&patientsNew).Error
  1811. record := models.XtPatientLaspseRecord{
  1812. PatientId: patient.ID,
  1813. RecordDate: time.Now().Unix(),
  1814. LapseDate: time.Now().Unix(),
  1815. LapseType: 1,
  1816. LapseClass: 0,
  1817. LapseReason: "",
  1818. AdminUserId: org_creator,
  1819. Remark: "",
  1820. UserOrgId: patient.UserOrgId,
  1821. Ctime: time.Now().Unix(),
  1822. Mtime: time.Now().Unix(),
  1823. Status: 1,
  1824. LapseTypeName: "转入",
  1825. HisType: 0,
  1826. HisZone: "",
  1827. HisBed: "",
  1828. }
  1829. err = utx.Create(&record).Error
  1830. if err != nil {
  1831. utx.Rollback()
  1832. btx.Rollback()
  1833. return
  1834. }
  1835. utx.Commit()
  1836. btx.Commit()
  1837. return
  1838. }
  1839. func FindPatientPhoneIsExist(phone string, org_id int64) (count int64) {
  1840. readDb.Model(&models.Patients{}).Where("user_org_id = ? AND phone = ? AND status = 1", org_id, phone).Count(&count)
  1841. return
  1842. }
  1843. func FindPatientIdCardNoIsExist(id_card_no string, org_id int64) (count int64) {
  1844. readDb.Model(&models.Patients{}).Where("user_org_id = ? AND id_card_no = ? AND status = 1", org_id, id_card_no).Count(&count)
  1845. return
  1846. }
  1847. func CreateExportErrLog(log *models.ExportErrLog) {
  1848. writeDb.Create(&log)
  1849. return
  1850. }
  1851. func FindPatientExportLog(org_id int64, export_time int64) (errLogs []*models.ExportErrLog, err error) {
  1852. err = readDb.Model(&models.ExportErrLog{}).Where("user_org_id = ? AND export_time = ? AND log_type = 1", org_id, export_time).Find(&errLogs).Error
  1853. return
  1854. }
  1855. func FindPatientExportLogOne(org_id int64, export_time int64) (errLogs []*models.ExportErrLog, err error) {
  1856. err = readDb.Model(&models.ExportErrLog{}).Where("user_org_id = ? AND export_time = ? AND log_type = 4", org_id, export_time).Find(&errLogs).Error
  1857. return
  1858. }
  1859. func FindPatientExportLogTwo(org_id int64, export_time int64) (errLogs []*models.ExportErrLog, err error) {
  1860. err = readDb.Model(&models.ExportErrLog{}).Where("user_org_id = ? AND export_time = ? AND log_type = 5", org_id, export_time).Find(&errLogs).Error
  1861. return
  1862. }
  1863. func FindPatientExportLogThree(org_id int64, export_time int64) (errLogs []*models.ExportErrLog, err error) {
  1864. err = readDb.Model(&models.ExportErrLog{}).Where("user_org_id = ? AND export_time = ? AND log_type = 6", org_id, export_time).Find(&errLogs).Error
  1865. return
  1866. }
  1867. func CreateExportLog(log *models.ExportLog) {
  1868. writeDb.Create(&log)
  1869. }
  1870. func UpdateDoctorEditAdvice(advice models.XtDoctorAdvice, orgid int64, groupno int64, date int64, patientid int64) error {
  1871. err := XTWriteDB().Model(&advice).Where("user_org_id = ? and groupno = ? and advice_date = ? and patient_id = ?", orgid, groupno, date, patientid).Update(map[string]interface{}{"start_time": advice.StartTime, "updated_time": advice.UpdatedTime}).Error
  1872. return err
  1873. }
  1874. func GetPatientsByKey(orgID int64, keywords string) (patient []*models.Patients, err error) {
  1875. db := readDb.Model(&models.Patients{}).Where("user_org_id=? and status=1 and lapseto = 1 ", orgID)
  1876. if len(keywords) > 0 {
  1877. likekey := "%" + keywords + "%"
  1878. err = db.Where("(name LIKE ? OR dialysis_no LIKE ? OR first_letter LIKE ?)", likekey, likekey, likekey).Find(&patient).Error
  1879. } else {
  1880. err = db.Find(&patient).Error
  1881. }
  1882. return
  1883. }
  1884. func FindAllDoctorAdviceByGoroupNoTwo(orgID int64, groupno int64) (advice []models.DoctorAdvice, err error) {
  1885. err = readDb.Model(&models.DoctorAdvice{}).Where("user_org_id=? AND groupno = ? AND status = 1 AND execution_state = 2", orgID, groupno).Find(&advice).Error
  1886. return
  1887. }
  1888. func FindDoctorAdviceByIdsTwo(orgID int64, ids []string) (advice []models.DoctorAdvice, err error) {
  1889. err = readDb.Model(&models.DoctorAdvice{}).Where("user_org_id = ? AND status = 1 AND execution_state = 2 AND (id IN (?) OR parent_id IN (?))", orgID, ids, ids).Find(&advice).Error
  1890. return
  1891. }
  1892. func CreateHisGroupAdvice(orgId int64, advices []*models.HisGroupAdvice, groupNo int64) (list []*models.HisGroupAdvice, err error) {
  1893. if groupNo <= 0 {
  1894. group := GetMaxAdviceGroupID(orgId)
  1895. groupNo = group + 1
  1896. }
  1897. tx := writeDb.Begin()
  1898. defer func() {
  1899. if r := recover(); r != nil {
  1900. tx.Rollback()
  1901. }
  1902. }()
  1903. for _, advice := range advices {
  1904. advice.Groupno = groupNo
  1905. if err = tx.Create(advice).Error; err != nil {
  1906. tx.Rollback()
  1907. return
  1908. }
  1909. list = append(list, advice)
  1910. if len(advice.Children) > 0 {
  1911. for _, child := range advice.Children {
  1912. child.Groupno = groupNo
  1913. child.ParentId = advice.ID
  1914. fmt.Println(child)
  1915. if err = tx.Create(&child).Error; err != nil {
  1916. tx.Rollback()
  1917. return
  1918. }
  1919. list = append(list, child)
  1920. }
  1921. }
  1922. }
  1923. tx.Commit()
  1924. return
  1925. }
  1926. func GetDialysisCount(orgid int64, partitionid int64) (order []*models.BloodDialysisOrderCount, err error) {
  1927. db := XTReadDB().Table("xt_dialysis_order as o")
  1928. if partitionid == 0 {
  1929. 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.user_org_id = ? and x.schedule_date = o.dialysis_date and x.status = 1", orgid).Group("o.patient_id").Scan(&order)
  1930. }
  1931. if partitionid > 0 {
  1932. 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.user_org_id = ? and x.partition_id = ? and x.schedule_date = o.dialysis_date and x.status = 1", orgid, partitionid).Group("o.patient_id").Scan(&order)
  1933. }
  1934. return order, err
  1935. }
  1936. func UpdatePatientDialysisSolutionOne(patientid int64, orgid int64, prescription *models.DialysisPrescription, timenow int64) error {
  1937. err = XTWriteDB().Model(&prescription).Where("patient_id = ? and user_org_id = ? and record_date = ? ", patientid, orgid, timenow).Updates(map[string]interface{}{"dialyzer": prescription.Dialyzer, "anticoagulant": prescription.Anticoagulant, "mode_id": prescription.ModeId, "dialysis_duration_hour": prescription.DialysisDurationHour, "anticoagulant_shouji": prescription.AnticoagulantShouji, "anticoagulant_weichi": prescription.AnticoagulantWeichi, "anticoagulant_zongliang": prescription.AnticoagulantZongliang, "kalium": prescription.Kalium, "sodium": prescription.Sodium, "calcium": prescription.Calcium, "dialyzer_perfusion_apparatus": prescription.DialyzerPerfusionApparatus, "blood_access": prescription.BloodAccess, "dialysate_flow": prescription.DialysateFlow, "dialysate_temperature": prescription.DialysateTemperature, "dialysis_dialyszers": prescription.DialysisDialyszers, "dialysis_irrigation": prescription.DialysisIrrigation, "plasma_separator": prescription.PlasmaSeparator, "bilirubin_adsorption_column": prescription.BilirubinAdsorptionColumn, "oxygen_uptake": prescription.OxygenUptake, "oxygen_flow": prescription.OxygenFlow, "oxygen_time": prescription.OxygenTime, "dialysis_strainer": prescription.DialysisStrainer, "chaptalization": prescription.Chaptalization, "blood_access_part_id": prescription.BloodAccessPartId}).Error
  1938. return err
  1939. }
  1940. //func FindRemindAdvice(user_org_id int64, advice_name string, advice_desc string, template_id string, fre_type int64, patient_id int64, record_date int64) (advice models.DoctorAdvice, err error) {
  1941. // err = XTWriteDB().Model(&models.DoctorAdvice{}).Where("patient_id = ? and user_org_id = ? and record_date = ? and advice_name = ? and advice_desc = ? and template_id = ? and frequency_type = ? ", patient_id, user_org_id, record_date, advice_name, advice_desc, template_id, fre_type).First(&advice).Error
  1942. // return
  1943. //}
  1944. func FindRemindAdvice(user_org_id int64, advice_name string, advice_desc string, template_id string, fre_type int64, patient_id int64, record_date int64) (advice models.DoctorAdvice, err error) {
  1945. err = XTWriteDB().Model(&models.DoctorAdvice{}).Where("patient_id = ? and user_org_id = ? and record_date = ? and advice_name = ? and advice_desc = ? and template_id = ? and frequency_type = ? AND advice_type = 2 ", patient_id, user_org_id, record_date, advice_name, advice_desc, template_id, fre_type).First(&advice).Error
  1946. return
  1947. }
  1948. func UpdateScheduleByDeathTime(patientid int64, shcheduledate int64) error {
  1949. var err error
  1950. if shcheduledate > 0 {
  1951. err = XTWriteDB().Model(models.XtSchedule{}).Where("patient_id = ? and schedule_date > ? and status=1", patientid, shcheduledate).Updates(map[string]interface{}{"status": 0, "remark": "转出删除排班"}).Error
  1952. }
  1953. return err
  1954. }
  1955. func UpdatePatientLapsetoByPatientId(patient_id int64, record_date int64, lapse_reason string) error {
  1956. err := XTWriteDB().Model(&models.Patients{}).Where("id = ? and status= 1", patient_id).Updates(map[string]interface{}{"death_time": record_date, "lapseto": 2, "out_reason": lapse_reason}).Error
  1957. err = XTWriteDB().Model(&models.XtPatientsNew{}).Where("blood_id = ? and status=1", patient_id).Updates(map[string]interface{}{"death_time": record_date, "lapseto": 2, "out_reason": lapse_reason}).Error
  1958. return err
  1959. }
  1960. func UpdatePatientLapsetoByPatientIdOne(patient_id int64, record_date int64, lapse_reason string) error {
  1961. err := XTWriteDB().Model(&models.Patients{}).Where("id = ? and status= 1", patient_id).Updates(map[string]interface{}{"death_time": record_date, "lapseto": 3, "out_reason": lapse_reason}).Error
  1962. err = XTWriteDB().Model(&models.XtPatientsNew{}).Where("blood_id = ? and status=1", patient_id).Updates(map[string]interface{}{"death_time": record_date, "lapseto": 3, "out_reason": lapse_reason}).Error
  1963. return err
  1964. }
  1965. func UpdatePatientLasetoType(patient_id int64, lapse_type int64) error {
  1966. err := XTWriteDB().Model(&models.Patients{}).Where("id = ? and status=1", patient_id).Updates(map[string]interface{}{"lapseto": 11}).Error
  1967. err = XTWriteDB().Model(&models.XtPatientsNew{}).Where("id = ? and status=1", patient_id).Updates(map[string]interface{}{"lapseto": 11}).Error
  1968. return err
  1969. }
  1970. func UpdatePatientLapsetoByPatientIdTwo(patient_id int64, record_date int64) error {
  1971. err := XTWriteDB().Model(&models.Patients{}).Where("id = ? and status= 1", patient_id).Updates(map[string]interface{}{"death_time": record_date, "lapseto": 1}).Error
  1972. err = XTWriteDB().Model(&models.XtPatientsNew{}).Where("blood_id = ? and status=1", patient_id).Updates(map[string]interface{}{"death_time": record_date, "lapseto": 1}).Error
  1973. return err
  1974. }
  1975. func UpdateScheduleItemByPatientId(id int64) error {
  1976. err := XTWriteDB().Model(models.PatientScheduleTemplateItem{}).Where("patient_id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  1977. return err
  1978. }
  1979. func GetDoctorAdviceListThree(orgID, patientID, advice_type, stop, start, end int64, keywords string, page int64, limit int64, delivery_way_one string) (advices []*models.HisDoctorAdvice, total int64, err error) {
  1980. db := readDb.Table("his_doctor_advice_info as x").Where("x.status = 1")
  1981. table := UserReadDB().Table("sgj_user_admin_role as r")
  1982. fmt.Print("table", table)
  1983. if len(delivery_way_one) > 0 {
  1984. db = db.Where("delivery_way = ?", delivery_way_one)
  1985. }
  1986. if orgID > 0 {
  1987. db = db.Where("x.user_org_id=?", orgID)
  1988. }
  1989. if patientID > 0 {
  1990. db = db.Where("x.patient_id = ?", patientID)
  1991. }
  1992. if advice_type == 1 && advice_type > 0 {
  1993. db = db.Where("x.advice_type = ?", advice_type)
  1994. }
  1995. if advice_type == 2 && advice_type > 0 {
  1996. db = db.Where("x.advice_type = ?", advice_type)
  1997. }
  1998. if advice_type == 3 && advice_type > 0 {
  1999. db = db.Where("x.advice_type = 2 or x.advice_type = 3")
  2000. }
  2001. if advice_type == 4 && advice_type > 0 {
  2002. db = db.Where("x.advice_type = 4")
  2003. }
  2004. if stop == 1 {
  2005. db = db.Where("(x.stop_state=? or x.execution_state=?)", stop, stop)
  2006. } else if stop == 2 {
  2007. db = db.Where("x.stop_state=? and x.execution_state=?", stop, stop)
  2008. }
  2009. if start != 0 {
  2010. db = db.Where("x.start_time>=?", start)
  2011. }
  2012. if end != 0 {
  2013. db = db.Where("x.start_time<=?", end)
  2014. }
  2015. if len(keywords) > 0 {
  2016. likeKey := "%" + keywords + "%"
  2017. db = db.Where("x.advice_name LIKE ?", likeKey)
  2018. }
  2019. offset := (page - 1) * limit
  2020. err = db.Group("x.id").Count(&total).Offset(offset).Limit(limit).Select("x.id, x.user_org_id, x.patient_id, x.advice_type, x.advice_date, x.record_date, x.start_time, x.advice_name,x.advice_desc, x.reminder_date, x.drug_spec, x.drug_spec_unit, x.single_dose, x.single_dose_unit, x.prescribing_number, x.prescribing_number_unit, x.delivery_way, x.execution_frequency, x.advice_doctor, x.status, x.created_time,x.updated_time, 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.execution_state, x.checker, x.check_state, x.check_time, x.groupno,x.remind_type,x.frequency_type,x.day_count,x.week_day,x.parent_id,r.user_name, IF(x.parent_id > 0, x.parent_id, x.id) as advice_order").Joins("Left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.advice_doctor").Order("start_time desc, groupno desc, advice_order desc, id asc").Scan(&advices).Error
  2021. fmt.Print("err", err)
  2022. return
  2023. }
  2024. func GetDoctorAdviceListFour(orgID int64, patientID int64, advice_type int64, stop int64, start int64, end int64, keywords string, limit int64, page int64, delive_way_one string) (advices []*models.HisDoctorAdvice, total int64, err error) {
  2025. db := readDb.Table("his_doctor_advice_info as x").Where("x.status = 1")
  2026. table := UserReadDB().Table("sgj_user_admin_role as r")
  2027. fmt.Println(table)
  2028. if len(delive_way_one) > 0 {
  2029. db = db.Where("delivery_way = ?", delive_way_one)
  2030. }
  2031. if orgID > 0 {
  2032. db = db.Where("x.user_org_id=?", orgID)
  2033. }
  2034. if patientID > 0 {
  2035. db = db.Where("x.patient_id = ?", patientID)
  2036. }
  2037. if advice_type == 1 {
  2038. db = db.Where("x.advice_type = ?", advice_type)
  2039. }
  2040. if advice_type == 3 && advice_type > 0 {
  2041. db = db.Where("x.advice_type = 2 or x.advice_type = 3")
  2042. }
  2043. if advice_type == 4 && advice_type > 0 {
  2044. db = db.Where("x.advice_type = 4")
  2045. }
  2046. if stop == 1 {
  2047. db = db.Where("(x.stop_state=? or x.execution_state=?)", stop, stop)
  2048. } else if stop == 2 {
  2049. db = db.Where("x.stop_state=? and x.execution_state=?", stop, stop)
  2050. }
  2051. if start != 0 {
  2052. db = db.Where("x.start_time>=?", start)
  2053. }
  2054. if end != 0 {
  2055. db = db.Where("x.start_time<=?", end)
  2056. }
  2057. if len(keywords) > 0 {
  2058. likeKey := "%" + keywords + "%"
  2059. db = db.Where("x.advice_name LIKE ?", likeKey)
  2060. }
  2061. offset := (page - 1) * limit
  2062. err = db.Order("x.start_time desc").Group("x.start_time").Count(&total).Offset(offset).Limit(limit).Select("x.id, x.user_org_id, x.patient_id, x.advice_type, x.advice_date, x.record_date, x.start_time, x.advice_name,x.advice_desc, x.reminder_date, x.drug_spec, x.drug_spec_unit, x.single_dose, x.single_dose_unit, x.prescribing_number, x.prescribing_number_unit, x.delivery_way, x.execution_frequency, x.advice_doctor, x.status, x.created_time,x.updated_time, 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.execution_state, x.checker, x.check_state, x.check_time, x.groupno,x.remind_type,x.frequency_type,x.day_count,x.week_day,x.parent_id,r.user_name, IF(x.parent_id > 0, x.parent_id, x.id) as advice_order").Joins("Left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.advice_doctor").Scan(&advices).Error
  2063. fmt.Print("错误是什么", err)
  2064. return
  2065. }
  2066. func GetAllDeviceNumberByListSix(orgID int64) (number []*models.DeviceNumber, err error) {
  2067. err = readDb.Model(&models.DeviceNumber{}).Where("org_id=? and status=1", orgID).Find(&number).Error
  2068. return number, err
  2069. }
  2070. func GetAllDeviceNumberByList(orgID int64) (number []*models.DeviceNumber, err error) {
  2071. redis := RedisClient()
  2072. defer redis.Close()
  2073. key := strconv.FormatInt(orgID, 10) + ":" + ":device_number_all"
  2074. number_info_str, _ := redis.Get(key).Result()
  2075. if len(number_info_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2076. err = readDb.Model(&models.DeviceNumber{}).Where("org_id=? and status=1", orgID).Find(&number).Error
  2077. if err != nil {
  2078. if err == gorm.ErrRecordNotFound {
  2079. return nil, nil
  2080. } else {
  2081. return nil, err
  2082. }
  2083. } else {
  2084. if len(number) > 0 {
  2085. //缓存数据
  2086. device_number_json, err := json.Marshal(&number)
  2087. if err == nil {
  2088. redis.Set(key, device_number_json, time.Second*60*60*18)
  2089. }
  2090. }
  2091. return number, nil
  2092. }
  2093. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2094. json.Unmarshal([]byte(number_info_str), &number)
  2095. return number, nil
  2096. }
  2097. }
  2098. func GetAllPrescriptionByListTwo(orgID int64, scheduleDate int64) (prescriptions []*models.DialysisPrescription, err error) {
  2099. redis := RedisClient()
  2100. defer redis.Close()
  2101. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":prescriptions_list_all"
  2102. prescriptions_list_all, _ := redis.Get(key).Result()
  2103. if len(prescriptions_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2104. if scheduleDate > 0 {
  2105. err = readDb.Model(&models.DialysisPrescription{}).Where("status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).Find(&prescriptions).Error
  2106. } else {
  2107. err = readDb.Model(&models.DialysisPrescription{}).Where("status = 1 AND user_org_id = ?", orgID).Find(&prescriptions).Error
  2108. }
  2109. if err != nil {
  2110. if err == gorm.ErrRecordNotFound {
  2111. return nil, nil
  2112. } else {
  2113. return nil, err
  2114. }
  2115. } else {
  2116. if len(prescriptions) > 0 {
  2117. //缓存数据
  2118. prescriptions_list_all, err := json.Marshal(&prescriptions)
  2119. if err == nil {
  2120. redis.Set(key, prescriptions_list_all, time.Second*60*60*18)
  2121. }
  2122. }
  2123. return prescriptions, nil
  2124. }
  2125. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2126. json.Unmarshal([]byte(prescriptions_list_all), &prescriptions)
  2127. return prescriptions, nil
  2128. }
  2129. }
  2130. func GetAllAssessmentBeforesByListThree(orgID int64, scheduleDate int64) (assessmentBefores []*models.PredialysisEvaluation, err error) {
  2131. redis := RedisClient()
  2132. defer redis.Close()
  2133. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":assessment_befores_list_all"
  2134. assessment_befores_list_all, _ := redis.Get(key).Result()
  2135. if len(assessment_befores_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2136. if scheduleDate > 0 {
  2137. err = readDb.Model(&models.PredialysisEvaluation{}).Where("status = 1 AND user_org_id = ? AND assessment_date = ? ", orgID, scheduleDate).Find(&assessmentBefores).Error
  2138. } else {
  2139. err = readDb.Model(&models.PredialysisEvaluation{}).Where("status = 1 AND user_org_id = ?", orgID).Find(&assessmentBefores).Error
  2140. }
  2141. if err != nil {
  2142. if err == gorm.ErrRecordNotFound {
  2143. return nil, nil
  2144. } else {
  2145. return nil, err
  2146. }
  2147. } else {
  2148. if len(assessmentBefores) > 0 {
  2149. //缓存数据
  2150. assessment_befores_list_all, err := json.Marshal(&assessmentBefores)
  2151. if err == nil {
  2152. redis.Set(key, assessment_befores_list_all, time.Second*60*60*18)
  2153. }
  2154. }
  2155. return assessmentBefores, nil
  2156. }
  2157. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2158. json.Unmarshal([]byte(assessment_befores_list_all), &assessmentBefores)
  2159. return assessmentBefores, nil
  2160. }
  2161. }
  2162. func GetAllDialysisOrdersByListThree(orgID int64, scheduleDate int64) (dialysisOrders []*models.MonitorDialysisOrder, err error) {
  2163. redis := RedisClient()
  2164. defer redis.Close()
  2165. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":dialysis_orders_list_all"
  2166. dialysis_orders_list_all, _ := redis.Get(key).Result()
  2167. if len(dialysis_orders_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2168. if scheduleDate > 0 {
  2169. err = readDb.Model(&models.MonitorDialysisOrder{}).Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).Where("status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, scheduleDate).Find(&dialysisOrders).Error
  2170. } else {
  2171. err = readDb.Model(&models.MonitorDialysisOrder{}).Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).Where("status = 1 AND user_org_id = ?", orgID).Find(&dialysisOrders).Error
  2172. }
  2173. if err != nil {
  2174. if err == gorm.ErrRecordNotFound {
  2175. return nil, nil
  2176. } else {
  2177. return nil, err
  2178. }
  2179. } else {
  2180. if len(dialysisOrders) > 0 {
  2181. //缓存数据
  2182. dialysis_orders_list_all, err := json.Marshal(&dialysisOrders)
  2183. if err == nil {
  2184. redis.Set(key, dialysis_orders_list_all, time.Second*60*60*18)
  2185. }
  2186. }
  2187. return dialysisOrders, nil
  2188. }
  2189. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2190. json.Unmarshal([]byte(dialysis_orders_list_all), &dialysisOrders)
  2191. return dialysisOrders, nil
  2192. }
  2193. }
  2194. func GetAllAssessmentAfterDislysisByListThree(orgID int64, scheduleDate int64) (assessmentAfterDislysis []*models.AssessmentAfterDislysis, err error) {
  2195. redis := RedisClient()
  2196. defer redis.Close()
  2197. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":assessment_after_dislysis_list_all"
  2198. assessment_after_dislysis__all, _ := redis.Get(key).Result()
  2199. if len(assessment_after_dislysis__all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2200. if scheduleDate > 0 {
  2201. err = readDb.Model(&models.AssessmentAfterDislysis{}).Where("status = 1 AND user_org_id = ? AND assessment_date = ?", orgID, scheduleDate).Find(&assessmentAfterDislysis).Error
  2202. } else {
  2203. err = readDb.Model(&models.AssessmentAfterDislysis{}).Where("status = 1 AND user_org_id = ?", orgID).Find(&assessmentAfterDislysis).Error
  2204. }
  2205. if err != nil {
  2206. if err == gorm.ErrRecordNotFound {
  2207. return nil, nil
  2208. } else {
  2209. return nil, err
  2210. }
  2211. } else {
  2212. if len(assessmentAfterDislysis) > 0 {
  2213. //缓存数据
  2214. assessment_after_dislysis__all, err := json.Marshal(&assessmentAfterDislysis)
  2215. if err == nil {
  2216. redis.Set(key, assessment_after_dislysis__all, time.Second*60*60*18)
  2217. }
  2218. }
  2219. return assessmentAfterDislysis, nil
  2220. }
  2221. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2222. json.Unmarshal([]byte(assessment_after_dislysis__all), &assessmentAfterDislysis)
  2223. return assessmentAfterDislysis, nil
  2224. }
  2225. }
  2226. func GetAllMonitorListThree(orgID int64, scheduleDate int64) (monitor []*models.MonitoringRecord, err error) {
  2227. redis := RedisClient()
  2228. defer redis.Close()
  2229. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":monitor_record_list_all"
  2230. monitor_record_list_all, _ := redis.Get(key).Result()
  2231. if len(monitor_record_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2232. if scheduleDate > 0 {
  2233. err = readDb.Model(&models.MonitoringRecord{}).Where("status = 1 AND user_org_id = ? AND monitoring_date= ?", orgID, scheduleDate).Find(&monitor).Error
  2234. } else {
  2235. err = readDb.Model(&models.MonitoringRecord{}).Where("status = 1 AND user_org_id = ?", orgID).Find(&monitor).Error
  2236. }
  2237. if err != nil {
  2238. if err == gorm.ErrRecordNotFound {
  2239. return nil, nil
  2240. } else {
  2241. return nil, err
  2242. }
  2243. } else {
  2244. if len(monitor) > 0 {
  2245. //缓存数据
  2246. assessment_after_dislysis__all, err := json.Marshal(&monitor)
  2247. if err == nil {
  2248. redis.Set(key, assessment_after_dislysis__all, time.Second*60*60*18)
  2249. }
  2250. }
  2251. return monitor, nil
  2252. }
  2253. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2254. json.Unmarshal([]byte(monitor_record_list_all), &monitor)
  2255. return monitor, nil
  2256. }
  2257. }
  2258. func GetAllPatientListByListThree(orgID int64) (patients []*models.MonitorPatients, err error) {
  2259. redis := RedisClient()
  2260. defer redis.Close()
  2261. key := strconv.FormatInt(orgID, 10) + ":" + ":patient_list_all"
  2262. patient_info_str, _ := redis.Get(key).Result()
  2263. if len(patient_info_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2264. err = readDb.Model(&models.MonitorPatients{}).Where("user_org_id=? and status=1 and lapseto = 1", orgID).Find(&patients).Error
  2265. if err != nil {
  2266. if err == gorm.ErrRecordNotFound {
  2267. return nil, nil
  2268. } else {
  2269. return nil, err
  2270. }
  2271. } else {
  2272. if len(patients) > 0 {
  2273. //缓存数据
  2274. patient_info_json, err := json.Marshal(&patients)
  2275. if err == nil {
  2276. redis.Set(key, patient_info_json, time.Second*60*60*18)
  2277. }
  2278. }
  2279. return patients, nil
  2280. }
  2281. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2282. json.Unmarshal([]byte(patient_info_str), &patients)
  2283. return patients, nil
  2284. }
  2285. }
  2286. func GetAllPatientListByListSix(orgID int64, keywords string) (patients []*models.MonitorPatients, err error) {
  2287. db := readDb.Model(&models.MonitorPatients{}).Where("status = 1 and lapseto = 1")
  2288. likeKey := "%" + keywords + "%"
  2289. if orgID > 0 {
  2290. db = db.Where("user_org_id = ?", orgID)
  2291. }
  2292. if len(keywords) > 0 {
  2293. db = db.Where("name like ?", likeKey)
  2294. }
  2295. err = db.Find(&patients).Error
  2296. return patients, err
  2297. }
  2298. func GetAllDialysisOrdersByListTwo(orgID int64, scheduleDate int64) (dialysisOrders []*MDialysisOrderVM, err error) {
  2299. redis := RedisClient()
  2300. defer redis.Close()
  2301. key := strconv.FormatInt(orgID, 10) + ":" + ":dialysis_orders_list_all"
  2302. dialysis_orders_list_all, _ := redis.Get(key).Result()
  2303. if scheduleDate < 1669132800 {
  2304. fmt.Println("key233232232332323232", key)
  2305. redis.Set(key, "", time.Second)
  2306. }
  2307. fmt.Println("dialysis_orders_list_all", dialysis_orders_list_all)
  2308. if len(dialysis_orders_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2309. err = readDb.Model(&MDialysisOrderVM{}).Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).Where("status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, scheduleDate).Find(&dialysisOrders).Error
  2310. if err != nil {
  2311. if err == gorm.ErrRecordNotFound {
  2312. return nil, nil
  2313. } else {
  2314. return nil, err
  2315. }
  2316. } else {
  2317. if len(dialysisOrders) > 0 {
  2318. //缓存数据
  2319. dialysis_orders_list_all, err := json.Marshal(&dialysisOrders)
  2320. if err == nil {
  2321. redis.Set(key, dialysis_orders_list_all, time.Second*60*60*18)
  2322. }
  2323. }
  2324. return dialysisOrders, nil
  2325. }
  2326. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2327. json.Unmarshal([]byte(dialysis_orders_list_all), &dialysisOrders)
  2328. return dialysisOrders, nil
  2329. }
  2330. }
  2331. func GetAllZoneByList(orgID int64) (zone []*models.DeviceZone, err error) {
  2332. redis := RedisClient()
  2333. defer redis.Close()
  2334. key := strconv.FormatInt(orgID, 10) + ":" + ":device_zone_all"
  2335. zone_info_str, _ := redis.Get(key).Result()
  2336. if len(zone_info_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2337. err = readDb.Model(&models.DeviceZone{}).Where("org_id=? and status=1", orgID).Find(&zone).Error
  2338. if err != nil {
  2339. if err == gorm.ErrRecordNotFound {
  2340. return nil, nil
  2341. } else {
  2342. return nil, err
  2343. }
  2344. } else {
  2345. if len(zone) > 0 {
  2346. //缓存数据
  2347. zone_number_json, err := json.Marshal(&zone)
  2348. if err == nil {
  2349. redis.Set(key, zone_number_json, time.Second*60*60*18)
  2350. }
  2351. }
  2352. return zone, nil
  2353. }
  2354. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2355. json.Unmarshal([]byte(zone_info_str), &zone)
  2356. return zone, nil
  2357. }
  2358. }
  2359. func GetAllDialysisOrdersByListNight(orgID int64, scheduleDate int64, pay_tyep int64) (dialysisOrders []*models.VMMonitorDialysisOrder, err error) {
  2360. if pay_tyep == 0 {
  2361. err = readDb.Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).Where("status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, scheduleDate).Find(&dialysisOrders).Error
  2362. }
  2363. //待下机
  2364. if pay_tyep == 3 {
  2365. err = readDb.Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).Where("status = 1 AND user_org_id = ? AND dialysis_date = ? and end_time = 0", orgID, scheduleDate).Find(&dialysisOrders).Error
  2366. }
  2367. fmt.Println("pay_typeowowowoowwo", pay_tyep)
  2368. if pay_tyep == 7 {
  2369. err = readDb.Preload("DeviceNumber", "status = 1 AND org_id = ?'", orgID).Where("status = 1 AND user_org_id = ? AND dialysis_date = ? and url = ''", orgID, scheduleDate).Find(&dialysisOrders).Error
  2370. }
  2371. return dialysisOrders, err
  2372. }
  2373. func GetAllPrescriptionByListSix(orgID int64, scheduleDate int64) (prescriptions []*models.VMDialysisPrescription, err error) {
  2374. err = readDb.Model(&models.VMDialysisPrescription{}).Where("status= 1 and user_org_id = ? and record_date = ?", orgID, scheduleDate).Find(&prescriptions).Error
  2375. return prescriptions, err
  2376. //redis := RedisClient()
  2377. //defer redis.Close()
  2378. //key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":prescriptions_list_all"
  2379. //redis.Set(key, "", time.Second)
  2380. //prescriptions_list_all, _ := redis.Get(key).Result()
  2381. //
  2382. //if len(prescriptions_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2383. // err = readDb.Model(&models.VMDialysisPrescription{}).Where("status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).Find(&prescriptions).Error
  2384. // if err != nil {
  2385. // if err == gorm.ErrRecordNotFound {
  2386. // return nil, nil
  2387. // } else {
  2388. // return nil, err
  2389. // }
  2390. // } else {
  2391. // if len(prescriptions) > 0 {
  2392. // //缓存数据
  2393. // prescriptions_list_all, err := json.Marshal(&prescriptions)
  2394. // if err == nil {
  2395. // redis.Set(key, prescriptions_list_all, time.Second*60*60*18)
  2396. // }
  2397. // }
  2398. // return prescriptions, nil
  2399. // }
  2400. //} else { //缓存数据了数据,将redis缓存的json字符串转为map
  2401. // json.Unmarshal([]byte(prescriptions_list_all), &prescriptions)
  2402. // return prescriptions, nil
  2403. //}
  2404. }
  2405. func GetAllPrescriptionByList(orgID int64, scheduleDate int64) (prescriptions []*models.VMDialysisPrescription, err error) {
  2406. redis := RedisClient()
  2407. defer redis.Close()
  2408. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":prescriptions_list_all"
  2409. redis.Set(key, "", time.Second)
  2410. prescriptions_list_all, _ := redis.Get(key).Result()
  2411. if len(prescriptions_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2412. err = readDb.Model(&models.VMDialysisPrescription{}).Where("status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).Find(&prescriptions).Error
  2413. if err != nil {
  2414. if err == gorm.ErrRecordNotFound {
  2415. return nil, nil
  2416. } else {
  2417. return nil, err
  2418. }
  2419. } else {
  2420. if len(prescriptions) > 0 {
  2421. //缓存数据
  2422. prescriptions_list_all, err := json.Marshal(&prescriptions)
  2423. if err == nil {
  2424. redis.Set(key, prescriptions_list_all, time.Second*60*60*18)
  2425. }
  2426. }
  2427. return prescriptions, nil
  2428. }
  2429. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2430. json.Unmarshal([]byte(prescriptions_list_all), &prescriptions)
  2431. return prescriptions, nil
  2432. }
  2433. }
  2434. func GetAllAssessmentBeforesByListOne(orgID int64, scheduleDate int64) (assessmentBefores []*models.VMPredialysisEvaluation, err error) {
  2435. redis := RedisClient()
  2436. defer redis.Close()
  2437. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":assessment_befores_list_all"
  2438. assessment_befores_list_all, _ := redis.Get(key).Result()
  2439. if len(assessment_befores_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2440. err = readDb.Model(&models.VMPredialysisEvaluation{}).Where("status = 1 AND user_org_id = ? AND assessment_date = ? ", orgID, scheduleDate).Find(&assessmentBefores).Error
  2441. if err != nil {
  2442. if err == gorm.ErrRecordNotFound {
  2443. return nil, nil
  2444. } else {
  2445. return nil, err
  2446. }
  2447. } else {
  2448. if len(assessmentBefores) > 0 {
  2449. //缓存数据
  2450. assessment_befores_list_all, err := json.Marshal(&assessmentBefores)
  2451. if err == nil {
  2452. redis.Set(key, assessment_befores_list_all, time.Second*60*60*18)
  2453. }
  2454. }
  2455. return assessmentBefores, nil
  2456. }
  2457. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2458. json.Unmarshal([]byte(assessment_befores_list_all), &assessmentBefores)
  2459. return assessmentBefores, nil
  2460. }
  2461. }
  2462. func GetAllDialysisOrdersByListSix(orgID int64, scheduleDate int64) (dialysisOrders []*models.VMMonitorDialysisOrder, err error) {
  2463. err = readDb.Model(&models.VMMonitorDialysisOrder{}).Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).Where("status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, scheduleDate).Find(&dialysisOrders).Error
  2464. return dialysisOrders, err
  2465. }
  2466. func GetAllDialysisOrdersByListSeven(orgID int64, scheduleDate int64) (dialysisOrders []*MDialysisOrderVM, err error) {
  2467. err = readDb.Model(&MDialysisOrderVM{}).Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).Where("status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, scheduleDate).Find(&dialysisOrders).Error
  2468. return dialysisOrders, err
  2469. }
  2470. func GetAllDialysisOrdersByListOne(orgID int64, scheduleDate int64) (dialysisOrders []*models.VMMonitorDialysisOrder, err error) {
  2471. redis := RedisClient()
  2472. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":dialysis_orders_list_all"
  2473. fmt.Println("key22332323232323223", key)
  2474. dialysis_orders_list_all, _ := redis.Get(key).Result()
  2475. defer redis.Close()
  2476. if len(dialysis_orders_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2477. err = readDb.Model(&models.VMMonitorDialysisOrder{}).Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).Where("status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, scheduleDate).Find(&dialysisOrders).Error
  2478. if err != nil {
  2479. if err == gorm.ErrRecordNotFound {
  2480. return nil, nil
  2481. } else {
  2482. return nil, err
  2483. }
  2484. } else {
  2485. if len(dialysisOrders) > 0 {
  2486. //缓存数据
  2487. dialysis_orders_list_all, err := json.Marshal(&dialysisOrders)
  2488. if err == nil {
  2489. redis.Set(key, dialysis_orders_list_all, time.Second*60*60*18)
  2490. }
  2491. }
  2492. return dialysisOrders, nil
  2493. }
  2494. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2495. json.Unmarshal([]byte(dialysis_orders_list_all), &dialysisOrders)
  2496. return dialysisOrders, nil
  2497. }
  2498. }
  2499. func GetAllAssessmentAfterDislysisByListSix(orgID int64, scheduleDate int64) (assessmentAfterDislysis []*models.VMAssessmentAfterDislysis, err error) {
  2500. err = readDb.Model(&models.VMAssessmentAfterDislysis{}).Where("status=1 and user_org_id = ? and assessment_date = ?", orgID, scheduleDate).Find(&assessmentAfterDislysis).Error
  2501. return assessmentAfterDislysis, err
  2502. //redis := RedisClient()
  2503. //defer redis.Close()
  2504. //key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":assessment_after_dislysis_list_all"
  2505. //redis.Set(key, "", time.Second)
  2506. //assessment_after_dislysis__all, _ := redis.Get(key).Result()
  2507. //
  2508. //if len(assessment_after_dislysis__all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2509. // err = readDb.Model(&models.VMAssessmentAfterDislysis{}).Where("status = 1 AND user_org_id = ? AND assessment_date = ?", orgID, scheduleDate).Find(&assessmentAfterDislysis).Error
  2510. // if err != nil {
  2511. // if err == gorm.ErrRecordNotFound {
  2512. // return nil, nil
  2513. // } else {
  2514. // return nil, err
  2515. // }
  2516. // } else {
  2517. // if len(assessmentAfterDislysis) > 0 {
  2518. // //缓存数据
  2519. // assessment_after_dislysis__all, err := json.Marshal(&assessmentAfterDislysis)
  2520. // if err == nil {
  2521. // redis.Set(key, assessment_after_dislysis__all, time.Second*60*60*18)
  2522. // }
  2523. // }
  2524. // return assessmentAfterDislysis, nil
  2525. // }
  2526. //} else { //缓存数据了数据,将redis缓存的json字符串转为map
  2527. // json.Unmarshal([]byte(assessment_after_dislysis__all), &assessmentAfterDislysis)
  2528. // return assessmentAfterDislysis, nil
  2529. //}
  2530. }
  2531. func GetAllAssessmentAfterDislysisByListOne(orgID int64, scheduleDate int64) (assessmentAfterDislysis []*models.VMAssessmentAfterDislysis, err error) {
  2532. redis := RedisClient()
  2533. defer redis.Close()
  2534. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":assessment_after_dislysis_list_all"
  2535. redis.Set(key, "", time.Second)
  2536. assessment_after_dislysis__all, _ := redis.Get(key).Result()
  2537. if len(assessment_after_dislysis__all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2538. err = readDb.Model(&models.VMAssessmentAfterDislysis{}).Where("status = 1 AND user_org_id = ? AND assessment_date = ?", orgID, scheduleDate).Find(&assessmentAfterDislysis).Error
  2539. if err != nil {
  2540. if err == gorm.ErrRecordNotFound {
  2541. return nil, nil
  2542. } else {
  2543. return nil, err
  2544. }
  2545. } else {
  2546. if len(assessmentAfterDislysis) > 0 {
  2547. //缓存数据
  2548. assessment_after_dislysis__all, err := json.Marshal(&assessmentAfterDislysis)
  2549. if err == nil {
  2550. redis.Set(key, assessment_after_dislysis__all, time.Second*60*60*18)
  2551. }
  2552. }
  2553. return assessmentAfterDislysis, nil
  2554. }
  2555. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2556. json.Unmarshal([]byte(assessment_after_dislysis__all), &assessmentAfterDislysis)
  2557. return assessmentAfterDislysis, nil
  2558. }
  2559. }
  2560. func GetAllMonitorList(orgID int64, scheduleDate int64) (monitor []*models.VMMonitoringRecord, err error) {
  2561. redis := RedisClient()
  2562. defer redis.Close()
  2563. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":monitor_record_list_all"
  2564. monitor_record_list_all, _ := redis.Get(key).Result()
  2565. redis.Set(key, "", time.Second)
  2566. if len(monitor_record_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2567. err = readDb.Model(&models.VMMonitoringRecord{}).Where("status = 1 AND user_org_id = ? AND monitoring_date= ?", orgID, scheduleDate).Order("monitoring_date asc").Find(&monitor).Error
  2568. if err != nil {
  2569. if err == gorm.ErrRecordNotFound {
  2570. return nil, nil
  2571. } else {
  2572. return nil, err
  2573. }
  2574. } else {
  2575. if len(monitor) > 0 {
  2576. //缓存数据
  2577. assessment_after_dislysis__all, err := json.Marshal(&monitor)
  2578. if err == nil {
  2579. redis.Set(key, assessment_after_dislysis__all, time.Second*60*60*18)
  2580. }
  2581. }
  2582. return monitor, nil
  2583. }
  2584. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2585. json.Unmarshal([]byte(monitor_record_list_all), &monitor)
  2586. return monitor, nil
  2587. }
  2588. }
  2589. func GetAllPatientListByListOne(orgID int64) (patients []*models.VMMonitorPatients, err error) {
  2590. redis := RedisClient()
  2591. defer redis.Close()
  2592. key := strconv.FormatInt(orgID, 10) + ":" + ":patient_list_all"
  2593. patient_info_str, _ := redis.Get(key).Result()
  2594. redis.Set(key, "", time.Second)
  2595. if len(patient_info_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2596. err = readDb.Model(&models.VMMonitorPatients{}).Where("user_org_id=? and status=1", orgID).Find(&patients).Error
  2597. if err != nil {
  2598. if err == gorm.ErrRecordNotFound {
  2599. return nil, nil
  2600. } else {
  2601. return nil, err
  2602. }
  2603. } else {
  2604. if len(patients) > 0 {
  2605. //缓存数据
  2606. patient_info_json, err := json.Marshal(&patients)
  2607. if err == nil {
  2608. redis.Set(key, patient_info_json, time.Second*60*60*18)
  2609. }
  2610. }
  2611. return patients, nil
  2612. }
  2613. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2614. json.Unmarshal([]byte(patient_info_str), &patients)
  2615. return patients, nil
  2616. }
  2617. }
  2618. func GetAllTreatModeByList(orgID int64) (treatement []*models.TreatmentMode, err error) {
  2619. redis := RedisClient()
  2620. defer redis.Close()
  2621. key := strconv.FormatInt(orgID, 10) + ":" + ":treatement_mode_all"
  2622. treatment_info_str, _ := redis.Get(key).Result()
  2623. if len(treatment_info_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2624. err = readDb.Model(&models.TreatmentMode{}).Where("status=1").Find(&treatement).Error
  2625. if err != nil {
  2626. if err == gorm.ErrRecordNotFound {
  2627. return nil, nil
  2628. } else {
  2629. return nil, err
  2630. }
  2631. } else {
  2632. if len(treatement) > 0 {
  2633. //缓存数据
  2634. patient_info_json, err := json.Marshal(&treatement)
  2635. if err == nil {
  2636. redis.Set(key, patient_info_json, time.Second*60*60*18)
  2637. }
  2638. }
  2639. return treatement, nil
  2640. }
  2641. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2642. json.Unmarshal([]byte(treatment_info_str), &treatement)
  2643. return treatement, nil
  2644. }
  2645. }
  2646. func GetAllPatientListByListSeven(orgID int64) (patients []*MSchedualPatientVM, err error) {
  2647. err = readDb.Model(&MSchedualPatientVM{}).Where("user_org_id=? and status=1", orgID).Find(&patients).Error
  2648. return patients, nil
  2649. }
  2650. func GetAllPatientListByListTwo(orgID int64) (patients []*MSchedualPatientVM, err error) {
  2651. redis := RedisClient()
  2652. defer redis.Close()
  2653. key := strconv.FormatInt(orgID, 10) + ":" + ":patient_list_all"
  2654. patient_info_str, _ := redis.Get(key).Result()
  2655. if len(patient_info_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2656. err = readDb.Model(&MSchedualPatientVM{}).Where("user_org_id=? and status=1", orgID).Find(&patients).Error
  2657. if err != nil {
  2658. if err == gorm.ErrRecordNotFound {
  2659. return nil, nil
  2660. } else {
  2661. return nil, err
  2662. }
  2663. } else {
  2664. if len(patients) > 0 {
  2665. //缓存数据
  2666. patient_info_json, err := json.Marshal(&patients)
  2667. if err == nil {
  2668. redis.Set(key, patient_info_json, time.Second*60*60*18)
  2669. }
  2670. }
  2671. return patients, nil
  2672. }
  2673. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2674. json.Unmarshal([]byte(patient_info_str), &patients)
  2675. return patients, nil
  2676. }
  2677. }
  2678. func GetAllDeviceNumberByListOne(orgID int64) (number []*MDeviceNumberVM, err error) {
  2679. redis := RedisClient()
  2680. defer redis.Close()
  2681. key := strconv.FormatInt(orgID, 10) + ":" + ":device_number_all"
  2682. number_info_str, _ := redis.Get(key).Result()
  2683. if len(number_info_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2684. err = readDb.Model(&MDeviceNumberVM{}).Where("org_id=? and status=1", orgID).Find(&number).Error
  2685. if err != nil {
  2686. if err == gorm.ErrRecordNotFound {
  2687. return nil, nil
  2688. } else {
  2689. return nil, err
  2690. }
  2691. } else {
  2692. if len(number) > 0 {
  2693. //缓存数据
  2694. device_number_json, err := json.Marshal(&number)
  2695. if err == nil {
  2696. redis.Set(key, device_number_json, time.Second*60*60*18)
  2697. }
  2698. }
  2699. return number, nil
  2700. }
  2701. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2702. json.Unmarshal([]byte(number_info_str), &number)
  2703. return number, nil
  2704. }
  2705. }
  2706. func GetAllPrescriptionByListOne(orgID int64, scheduleDate int64) (prescriptions []*models.DialysisPrescriptionTwenty, err error) {
  2707. redis := RedisClient()
  2708. defer redis.Close()
  2709. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":prescriptions_list_all"
  2710. prescriptions_list_all, _ := redis.Get(key).Result()
  2711. if len(prescriptions_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2712. err = readDb.Model(&models.DialysisPrescriptionTwenty{}).Where("status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).Find(&prescriptions).Error
  2713. if err != nil {
  2714. if err == gorm.ErrRecordNotFound {
  2715. return nil, nil
  2716. } else {
  2717. return nil, err
  2718. }
  2719. } else {
  2720. if len(prescriptions) > 0 {
  2721. //缓存数据
  2722. prescriptions_list_all, err := json.Marshal(&prescriptions)
  2723. if err == nil {
  2724. redis.Set(key, prescriptions_list_all, time.Second*60*60*18)
  2725. }
  2726. }
  2727. return prescriptions, nil
  2728. }
  2729. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2730. json.Unmarshal([]byte(prescriptions_list_all), &prescriptions)
  2731. return prescriptions, nil
  2732. }
  2733. }
  2734. func GetAllHisProjectPrescription(orgid int64, recorddate int64) (project []*models.HisPrescriptionProjectTwo, err error) {
  2735. err = XTReadDB().Where("user_org_id = ? and record_date = ? and status = 1", orgid, recorddate).Find(&project).Error
  2736. return project, err
  2737. }
  2738. func GetZoneCountList(orgid int64, schedule_date int64) (list []*models.XtScheduleSeven, err error) {
  2739. db := XTReadDB().Model(&list).Where("status = 1")
  2740. if orgid > 0 {
  2741. db = db.Where("user_org_id = ?", orgid)
  2742. }
  2743. if schedule_date > 0 {
  2744. db = db.Where("schedule_date =?", schedule_date)
  2745. }
  2746. err = db.Select("partition_id,count(id) as count").Group("partition_id").Scan(&list).Error
  2747. return list, err
  2748. }
  2749. func GetPatientDialysisLongSolution(patient_id int64, page int64, limit int64, orgid int64) (solution []*models.DialysisSolution, total int64, err error) {
  2750. db := XTReadDB().Model(&solution).Where("status = 1 and solution_status = 1")
  2751. if patient_id > 0 {
  2752. db = db.Where("patient_id = ?", patient_id)
  2753. }
  2754. if orgid > 0 {
  2755. db = db.Where("user_org_id = ?", orgid)
  2756. }
  2757. offset := (page - 1) * limit
  2758. err = db.Group("mode_id").Count(&total).Offset(offset).Limit(limit).Order("created_time desc").Find(&solution).Error
  2759. return solution, total, err
  2760. }
  2761. func GetDialysisSolutionDetailList(orgid int64, patient_id int64, mode_id int64, page int64, limit int64) (solution []*models.DialysisSolution, total int64, err error) {
  2762. offset := (page - 1) * limit
  2763. db := XTReadDB().Model(&solution).Where("status = 1")
  2764. if orgid > 0 {
  2765. db = db.Where("user_org_id = ?", orgid)
  2766. }
  2767. if patient_id > 0 {
  2768. db = db.Where("patient_id = ?", patient_id)
  2769. }
  2770. if mode_id > 0 {
  2771. db = db.Where("mode_id = ?", mode_id)
  2772. }
  2773. err = db.Count(&total).Offset(offset).Limit(limit).Find(&solution).Order("created_time desc").Error
  2774. return solution, total, err
  2775. }
  2776. func UpdateDialysisBefor(dislysis models.XtAssessmentBeforeDislysis, id int64) error {
  2777. err := XTWriteDB().Model(&models.XtAssessmentBeforeDislysis{}).Where("id = ? and status= 1", id).Update(map[string]interface{}{"systolic_blood_pressure": dislysis.SystolicBloodPressure, "diastolic_blood_pressure": dislysis.DiastolicBloodPressure}).Error
  2778. return err
  2779. }
  2780. func GetAllSchedulesByPatientInfo(org_id int64, patient_id int64, mode_id int64) ([]Schedule, error) {
  2781. var record []Schedule
  2782. if org_id == 10579 {
  2783. timeNowStr := time.Now().Format("2006-01-02")
  2784. timeNewDate, _ := utils.ParseTimeStringToTime("2006-01-02", timeNowStr)
  2785. err := readDb.Model(&Schedule{}).Where("status = 1 AND schedule_date >? AND patient_id = ? AND user_org_id = ? AND mode_id= ?", timeNewDate.Unix(), patient_id, org_id, mode_id).Find(&record).Error
  2786. if err != nil {
  2787. if err == gorm.ErrRecordNotFound {
  2788. return nil, nil
  2789. } else {
  2790. return nil, err
  2791. }
  2792. }
  2793. } else {
  2794. timeNowStr := time.Now().Format("2006-01-02")
  2795. timeNewDate, _ := utils.ParseTimeStringToTime("2006-01-02", timeNowStr)
  2796. err := readDb.Model(&Schedule{}).Where("status = 1 AND schedule_date >= ? AND patient_id = ? AND user_org_id = ? AND mode_id= ?", timeNewDate.Unix(), patient_id, org_id, mode_id).Find(&record).Error
  2797. if err != nil {
  2798. if err == gorm.ErrRecordNotFound {
  2799. return nil, nil
  2800. } else {
  2801. return nil, err
  2802. }
  2803. }
  2804. }
  2805. return record, nil
  2806. }
  2807. func UpdateScheduleByExport(org_id int64, shcheduledate int64) error {
  2808. err := XTWriteDB().Model(models.XtSchedule{}).Where("user_org_id = ? and schedule_date > ?", org_id, shcheduledate).Updates(map[string]interface{}{"status": 0}).Error
  2809. return err
  2810. }
  2811. func GetContextScheduleListGroupPatientId(limit int64, page int64, startTime int64, endTime int64, org_id int64) (schedule []*models.ConScheduleList, total int64, err error) {
  2812. db := XTReadDB().Model(&schedule).Where("status=1")
  2813. offset := (page - 1) * limit
  2814. if startTime > 0 {
  2815. db = db.Where("schedule_date >=?", startTime)
  2816. }
  2817. if endTime > 0 {
  2818. db = db.Where("schedule_date<=?", endTime)
  2819. }
  2820. if org_id > 0 {
  2821. db = db.Where("user_org_id = ?", org_id)
  2822. }
  2823. err = db.Count(&total).Offset(offset).Limit(limit).Group("patient_id").Find(&schedule).Error
  2824. return schedule, total, err
  2825. }
  2826. func GetContextScheduleListPatientId(startTime int64, endTime int64, org_id int64) (schedule []*models.ConScheduleList, total int64, err error) {
  2827. db := XTReadDB().Model(&schedule).Where("status=1")
  2828. if startTime > 0 {
  2829. db = db.Where("schedule_date >=?", startTime)
  2830. }
  2831. if endTime > 0 {
  2832. db = db.Where("schedule_date<=?", endTime)
  2833. }
  2834. if org_id > 0 {
  2835. db = db.Where("user_org_id = ?", org_id)
  2836. }
  2837. err = db.Find(&schedule).Error
  2838. return schedule, total, err
  2839. }
  2840. func GetPatientAllagicList(patient_id int64, user_org_id int64) (list []*models.XtPatientAllergic, err error) {
  2841. err = XTReadDB().Where("patient_id = ? and user_org_id =? and status= 1", patient_id, user_org_id).Find(&list).Error
  2842. return list, err
  2843. }
  2844. func GetPatientAllagicListOne(user_org_id int64) (list []*models.XtPatientAllergic, err error) {
  2845. err = XTReadDB().Where("user_org_id =? and status= 1", user_org_id).Find(&list).Error
  2846. return list, err
  2847. }
  2848. func GetNewMonitorRecord(orgID int64, date int64, partition int64, patientId int64) ([]*models.NewMonitorDialysisScheduleList, error) {
  2849. var mds []*models.NewMonitorDialysisScheduleList
  2850. db := readDb.Model(&models.NewMonitorDialysisScheduleList{})
  2851. if date > 0 {
  2852. db = db.Where("schedule_date = ?", date)
  2853. }
  2854. if partition > 0 {
  2855. db = db.Where("partition_id = ?", partition)
  2856. }
  2857. if patientId > 0 {
  2858. db = db.Where("patient_id = ?", patientId)
  2859. }
  2860. err = db.Where("status = 1 and user_org_id = ?", orgID).Find(&mds).Error
  2861. return mds, err
  2862. }
  2863. func GetAdviceList(user_org_id int64, advice_date int64, pat_type int64) (advice []*models.VMDoctorAdviceForList, err error) {
  2864. if pat_type == 0 {
  2865. err = XTReadDB().Where("user_org_id = ? and advice_date = ? and status =7 and check_time = 0", user_org_id, advice_date).Find(&advice).Error
  2866. }
  2867. //待医嘱核对
  2868. if pat_type == 1 {
  2869. err = XTReadDB().Where("user_org_id = ? and advice_date = ? and status =1 and check_time = 0", user_org_id, advice_date).Find(&advice).Error
  2870. }
  2871. //待医嘱执行
  2872. if pat_type == 6 {
  2873. err = XTReadDB().Where("user_org_id = ? and advice_date = ? and status =1 and execution_state = 2", user_org_id, advice_date).Find(&advice).Error
  2874. }
  2875. return advice, err
  2876. }
  2877. func GetDobuleCheck(user_org_id int64, advice_date int64) (check []*models.VmDoubleCheck, err error) {
  2878. err = XTReadDB().Where("user_org_id = ? and check_date = ? and status = 1", user_org_id, advice_date).Find(&check).Error
  2879. return check, err
  2880. }
  2881. func GetDobuleCheckOne(user_org_id int64, advice_date int64, patient_id int64) (check models.VmDoubleCheck, err error) {
  2882. err = XTReadDB().Where("user_org_id = ? and check_date = ? and status = 1 and patient_id = ?", user_org_id, advice_date, patient_id).Find(&check).Error
  2883. return check, err
  2884. }
  2885. func GetTreatmentSummaryForList(user_org_id int64, advice_date int64) (summary []*models.VMTreatmentSummaryForList, err error) {
  2886. err = XTReadDB().Where("user_org_id =? and assessment_date = ? and status =1", user_org_id, advice_date).Find(&summary).Error
  2887. return summary, err
  2888. }
  2889. func GetDialysisInformationList(user_org_id int64, advice_date int64) (information []*models.NewDeviceInformation, err error) {
  2890. err = UserReadDB().Where("user_org_id =? and date = ? and status =1", user_org_id, advice_date).Find(&information).Error
  2891. return information, err
  2892. }
  2893. func GetNewAllMonitorList(orgID int64, scheduleDate int64, pat_type int64) (monitor []*models.VMMonitoringRecord, err error) {
  2894. if pat_type == 0 || pat_type == 9 {
  2895. err = readDb.Model(&models.VMMonitoringRecord{}).Where("status = 1 AND user_org_id = ? AND monitoring_date= ?", orgID, scheduleDate).Order("monitoring_date asc").Find(&monitor).Error
  2896. }
  2897. if pat_type == 10 {
  2898. err = readDb.Model(&models.VMMonitoringRecord{}).Where("status = 1 AND user_org_id = ? AND monitoring_date= ? AND accumulated_blood_volume = 0", orgID, scheduleDate).Order("monitoring_date asc").Find(&monitor).Error
  2899. }
  2900. if pat_type == 11 {
  2901. err = readDb.Model(&models.VMMonitoringRecord{}).Where("status = 1 AND user_org_id = ? AND monitoring_date= ? AND symptom <> ''", orgID, scheduleDate).Order("monitoring_date asc").Find(&monitor).Error
  2902. }
  2903. if pat_type == 12 {
  2904. err = readDb.Model(&models.VMMonitoringRecord{}).Where("status = 1 AND user_org_id = ? AND monitoring_date= ? AND dispose <> ''", orgID, scheduleDate).Order("monitoring_date asc").Find(&monitor).Error
  2905. }
  2906. return monitor, err
  2907. }
  2908. func GetBaseDrugByName(drug_name string, org_id int64) (models.BaseDrugLibSeven, error) {
  2909. libSeven := models.BaseDrugLibSeven{}
  2910. err := XTReadDB().Where("drug_name= ? and org_id = ? and status=1", drug_name, org_id).First(&libSeven).Error
  2911. return libSeven, err
  2912. }
  2913. func CreateDeviceValsualMap(valsualMap models.XtDeviceValsualMap) error {
  2914. err := XTWriteDB().Save(&valsualMap).Error
  2915. return err
  2916. }
  2917. func GetPatientSitemap(patient_id int64, user_org_id int64) (models.XtDeviceValsualMap, error) {
  2918. valsualMap := models.XtDeviceValsualMap{}
  2919. err := XTReadDB().Where("patient_id = ? and status =1 and user_org_id = ?", patient_id, user_org_id).Preload("XtDeviceValsualALeft", "status=1 and user_org_id =?", user_org_id).Preload("XtDeviceValsualVLeft", "status=1 and user_org_id =?", user_org_id).Preload("XtDeviceValsualMapFangxiangA", "status=1 and user_org_id =?", user_org_id).Preload("XtDeviceValsualMapFangxiangV", "status=1 and user_org_id =?", user_org_id).Preload("XtDeviceValsualFuzhuA", "status=1 and user_org_id =?", user_org_id).Preload("XtDeviceValsualFuzhuV", "status=1 and user_org_id =?", user_org_id).Preload("XtDeviceValsualTextArr", "status=1 and user_org_id =?", user_org_id).Preload("DeviceValusalMapArr", "status=1 and user_org_id =?", user_org_id).Preload("XtDeviceValusalMapVrr", "status=1 and user_org_id =?", user_org_id).Find(&valsualMap).Error
  2920. return valsualMap, err
  2921. }
  2922. func UpdateSiteMap(valsualMap models.XtDeviceValsualMap) error {
  2923. err := XTWriteDB().Save(&valsualMap).Error
  2924. return err
  2925. }
  2926. func CreateValsualAleft(left models.XtDeviceValsualALeft) error {
  2927. err := XTWriteDB().Create(&left).Error
  2928. return err
  2929. }
  2930. func UpdateValsualAleft(patient_id int64, user_org_id int64) error {
  2931. err := XTWriteDB().Model(&models.XtDeviceValsualALeft{}).Where("patient_id = ? and user_org_id = ? and status =1", patient_id, user_org_id).Updates(map[string]interface{}{"status": 0}).Error
  2932. return err
  2933. }
  2934. func CreateValsualMapFangxiangA(a models.XtDeviceValsualMapFangxiangA) error {
  2935. err := XTWriteDB().Create(&a).Error
  2936. return err
  2937. }
  2938. func UpdateValsualMapFangxiangA(patient_id int64, user_org_id int64) error {
  2939. err := XTWriteDB().Model(&models.XtDeviceValsualMapFangxiangA{}).Where("patient_id = ? and user_org_id = ? and status =1", patient_id, user_org_id).Updates(map[string]interface{}{"status": 0}).Error
  2940. return err
  2941. }
  2942. func CreateValsualMapFangxiangV(v models.XtDeviceValsualMapFangxiangV) error {
  2943. err := XTWriteDB().Create(&v).Error
  2944. return err
  2945. }
  2946. func UpdateValsualMapFangxiangV(patient_id int64, user_org_id int64) error {
  2947. err := XTWriteDB().Model(&models.XtDeviceValsualMapFangxiangV{}).Where("patient_id = ? and user_org_id = ? and status=1", patient_id, user_org_id).Updates(map[string]interface{}{"status": 0}).Error
  2948. return err
  2949. }
  2950. func ValsualMapFangxiangA(patient_id int64, user_org_id int64) error {
  2951. err := XTWriteDB().Model(models.XtDeviceValsualMapFangxiangA{}).Where("patient_id = ? and user_org_id = ? and status=1", patient_id, user_org_id).Updates(map[string]interface{}{"status": 0}).Error
  2952. return err
  2953. }
  2954. func CreateValSualFuzhA(a models.XtDeviceValsualFuzhuA) error {
  2955. err := XTReadDB().Create(&a).Error
  2956. return err
  2957. }
  2958. func UpdateValSualFuzhA(patient_id int64, user_org_id int64) error {
  2959. err := XTWriteDB().Model(&models.XtDeviceValsualFuzhuA{}).Where("patient_id =? and user_org_id = ? and status=1", patient_id, user_org_id).Updates(map[string]interface{}{"status": 0}).Error
  2960. return err
  2961. }
  2962. func CreateVasuaLfuzhuV(v models.XtDeviceValsualFuzhuV) error {
  2963. err := XTReadDB().Create(&v).Error
  2964. return err
  2965. }
  2966. func UpdateVasuaLfuzhuV(patient_id int64, user_org_id int64) error {
  2967. err := XTWriteDB().Model(&models.XtDeviceValsualFuzhuV{}).Where("patient_id = ? and user_org_id =? and status=1", patient_id, user_org_id).Updates(map[string]interface{}{"status": 0}).Error
  2968. return err
  2969. }
  2970. func CreateTextArr(arr models.XtDeviceValsualTextArr) error {
  2971. err := XTReadDB().Create(&arr).Error
  2972. return err
  2973. }
  2974. func UpdateTextArr(patient_id int64, user_org_id int64) error {
  2975. err := XTWriteDB().Model(&models.XtDeviceValsualTextArr{}).Where("patient_id = ? and user_org_id = ? and status=1", patient_id, user_org_id).Updates(map[string]interface{}{"status": 0}).Error
  2976. return err
  2977. }
  2978. func CreateValsualVleft(left models.XtDeviceValsualVLeft) error {
  2979. err = XTReadDB().Create(&left).Error
  2980. return err
  2981. }
  2982. func UpdateValsualVleft(patient_id int64, user_org_id int64) error {
  2983. err := XTWriteDB().Model(&models.XtDeviceValsualVLeft{}).Where("patient_id = ? and user_org_id =? and status=1", patient_id, user_org_id).Updates(map[string]interface{}{"status": 0}).Error
  2984. return err
  2985. }
  2986. func UpdateValusalMapArr(patient_id int64, user_org_id int64) error {
  2987. err := XTWriteDB().Model(&models.XtDeviceValusalMapArr{}).Where("patient_id =? and user_org_id =? and status=1", patient_id, user_org_id).Updates(map[string]interface{}{"status": 0}).Error
  2988. return err
  2989. }
  2990. func CreateDeviceValusalMapArr(vrr models.XtDeviceValusalMapArr) error {
  2991. err := XTWriteDB().Create(&vrr).Error
  2992. return err
  2993. }
  2994. func UpdateDeviceValusalMapVrr(patient_id int64, user_org_id int64) error {
  2995. err := XTWriteDB().Model(&models.XtDeviceValusalMapVrr{}).Where("patient_id = ? and user_org_id =? and status=1", patient_id, user_org_id).Updates(map[string]interface{}{"status": 0}).Error
  2996. return err
  2997. }
  2998. func CreateDeviceValusalMapVrr(vrr models.XtDeviceValusalMapVrr) error {
  2999. err := XTWriteDB().Create(&vrr).Error
  3000. return err
  3001. }
  3002. func GetDialysisOrderNoExecution(start_nurse int64, user_org_id int64, timenow int64) (order []*models.XtDialysisOrder, err error) {
  3003. err = XTReadDB().Where("start_nurse = ? and user_org_id =? and status =1 and dialysis_date =?", start_nurse, user_org_id, timenow).Find(&order).Error
  3004. return order, err
  3005. }
  3006. func GetAdviceListNoExecution(advice_date int64, user_org_id int64, patientIds []int64) (advice []*models.NoDoctorAdvice, err error) {
  3007. db := XTReadDB().Model(advice).Where("status=1 and execution_state = 2 and advice_type = 2")
  3008. if len(patientIds) > 0 {
  3009. if advice_date > 0 {
  3010. db = db.Where("advice_date= ?", advice_date)
  3011. }
  3012. if user_org_id > 0 {
  3013. db = db.Where("user_org_id =?", user_org_id)
  3014. }
  3015. if len(patientIds) > 0 {
  3016. db = db.Where("patient_id in(?)", patientIds)
  3017. }
  3018. err = db.Select("patient_id,Count(id) as count,advice_date").Group("patient_id").Find(&advice).Error
  3019. return advice, err
  3020. } else {
  3021. return advice, err
  3022. }
  3023. }
  3024. func GetHisAdviceListNoExecution(advice_date int64, user_org_id int64, patientIds []int64) (advice []*models.HisNoDoctorAdvice, err error) {
  3025. db := XTReadDB().Model(advice).Where("status=1 and execution_state = 2")
  3026. if advice_date > 0 {
  3027. db = db.Where("advice_date= ?", advice_date)
  3028. }
  3029. if user_org_id > 0 {
  3030. db = db.Where("user_org_id =?", user_org_id)
  3031. }
  3032. if len(patientIds) > 0 {
  3033. db = db.Where("patient_id in(?)", patientIds)
  3034. }
  3035. err = db.Select("advice_date,patient_id,Count(id) as count").Group("patient_id").Find(&advice).Error
  3036. return advice, err
  3037. }
  3038. func GetProjectListNoExecution(record_date int64, user_org_id int64, patientIds []int64) (project []*models.NoHisPrescriptionProject, err error) {
  3039. db := XTReadDB().Model(&project).Where("status=1 and execution_state =0")
  3040. if record_date > 0 {
  3041. db = db.Where("record_date= ?", record_date)
  3042. }
  3043. if user_org_id > 0 {
  3044. db = db.Where("user_org_id =?", user_org_id)
  3045. }
  3046. if len(patientIds) > 0 {
  3047. db = db.Where("patient_id in(?)", patientIds)
  3048. }
  3049. err = db.Select("patient_id,Count(id) as count,record_date as advice_date").Group("patient_id").Find(&project).Error
  3050. return project, err
  3051. }
  3052. func GetPatientNoExecutionList(org_id int64, patientsIds []int64) (patient []*models.XtPatients, err error) {
  3053. err = XTReadDB().Where("user_org_id = ? and id in(?)", org_id, patientsIds).Find(&patient).Error
  3054. return patient, err
  3055. }
  3056. func FindHisConfig(org_id int64) (err error, config models.XtHisConfig) {
  3057. err = readDb.Model(&models.XtHisConfig{}).Where("status = 1 AND user_org_id = ?", org_id).First(&config).Error
  3058. return
  3059. }
  3060. func GetPatientDetailInformedConsent(patient_id int64, org_id int64) (models.XtPatients, error) {
  3061. patients := models.XtPatients{}
  3062. err := XTReadDB().Where("id = ? and status=1 and user_org_id =?", patient_id, org_id).Find(&patients).Error
  3063. return patients, err
  3064. }
  3065. func GetAllPrescriptionList(org_id int64) (prescription []*models.DialysisPrescription, err error) {
  3066. err = XTReadDB().Where("user_org_id = ? and status =1", org_id).Find(&prescription).Error
  3067. return prescription, err
  3068. }
  3069. func GetPatientScheudleListTwenty(org_id int64, scheduel_type int64, startime int64, endtime int64, schedule_date int64) (schedule []*models.ScheduleTen, err error) {
  3070. db := XTReadDB().Model(&schedule).Where("status= 1")
  3071. if org_id > 0 {
  3072. db = db.Where("user_org_id = ?", org_id)
  3073. }
  3074. if scheduel_type > 0 {
  3075. db = db.Where("schedule_type =?", scheduel_type)
  3076. }
  3077. if startime > 0 {
  3078. db = db.Where("schedule_date >=?", startime)
  3079. }
  3080. if endtime > 0 {
  3081. db = db.Where("schedule_date <=?", endtime)
  3082. }
  3083. if startime == 0 && endtime == 0 {
  3084. db = db.Where("schedule_date = ?", schedule_date)
  3085. }
  3086. err = db.Find(&schedule).Error
  3087. fmt.Println("err2330230320202023", err)
  3088. return schedule, err
  3089. }
  3090. func GetDialysisOrderCountTwentyEight(orgID int64, patient_id int64, recordDate int64) (models.VmDialysisOrder, error) {
  3091. order := models.VmDialysisOrder{}
  3092. err := XTReadDB().Raw("SELECT Count(id) as count,Max(dialysis_date) as dialysis_date from xt_dialysis_order where dialysis_date>=1672502400 and dialysis_date <= ? AND status = 1 AND stage = 2 AND patient_id = ? and user_org_id = ?", recordDate, patient_id, orgID).Scan(&order).Error
  3093. return order, err
  3094. }
  3095. func GetDialysisOrderCountTenEight(patient_id int64, recordDate int64) (models.VmDialysisOrder, error) {
  3096. order := models.VmDialysisOrder{}
  3097. err := XTReadDB().Where("patient_id = ? and dialysis_date = ? and status = 1", patient_id, recordDate).Last(&order).Error
  3098. return order, err
  3099. }
  3100. func GetLastAcceptRecrods(patient_id int64) (models.ReceiveTreatmentAsses, error) {
  3101. treatmentAsses := models.ReceiveTreatmentAsses{}
  3102. err := XTReadDB().Where("patient_id = ? and status= 1 and admission_number!=''", patient_id).Last(&treatmentAsses).Error
  3103. return treatmentAsses, err
  3104. }
  3105. func GetOutPrintList(user_org_id int64) (models.XtFallOutPrint, error) {
  3106. outPrint := models.XtFallOutPrint{}
  3107. err := XTReadDB().Where("user_org_id = ? and status =1", user_org_id).Find(&outPrint).Error
  3108. return outPrint, err
  3109. }
  3110. func CreateFallOutBedPrint(outPrint models.XtFallOutPrint) error {
  3111. err := XTWriteDB().Create(&outPrint).Error
  3112. return err
  3113. }
  3114. func UpdateFallOutBedPrint(id int64, content string) error {
  3115. err := XTWriteDB().Model(&models.XtFallOutPrint{}).Where("id = ? and status=1", id).Updates(map[string]interface{}{"content": content}).Error
  3116. return err
  3117. }
  3118. func GetFallOutBedPrintList(user_org_id int64) (models.XtFallOutPrint, error) {
  3119. outPrint := models.XtFallOutPrint{}
  3120. err := XTReadDB().Where("user_org_id = ? and status=1", user_org_id).Find(&outPrint).Error
  3121. return outPrint, err
  3122. }
  3123. func GetFilterInformed(user_org_id int64) (models.XtFilterInformed, error) {
  3124. informed := models.XtFilterInformed{}
  3125. err := XTReadDB().Where("user_org_id = ? and status =1", user_org_id).Find(&informed).Error
  3126. return informed, err
  3127. }
  3128. func CreateFilterInformed(informed models.XtFilterInformed) error {
  3129. err := XTWriteDB().Create(&informed).Error
  3130. return err
  3131. }
  3132. func UpdateFilterInformed(id int64, content string) error {
  3133. err := XTWriteDB().Model(&models.XtFilterInformed{}).Where("id = ? and status =1", id).Updates(map[string]interface{}{"content": content}).Error
  3134. return err
  3135. }
  3136. func GetProfundaInformed(user_org_id int64) (models.XtProfundaInformed, error) {
  3137. informed := models.XtProfundaInformed{}
  3138. err := XTReadDB().Where("user_org_id = ? and status =1", user_org_id).Find(&informed).Error
  3139. return informed, err
  3140. }
  3141. func CreateProfundaInformed(informed models.XtProfundaInformed) error {
  3142. err := XTWriteDB().Create(&informed).Error
  3143. return err
  3144. }
  3145. func UpdateProfundaInformed(id int64, content string) error {
  3146. profundaInformed := models.XtProfundaInformed{}
  3147. err := XTWriteDB().Model(&profundaInformed).Where("id = ? and status=1", id).Updates(map[string]interface{}{"content": content}).Error
  3148. return err
  3149. }
  3150. func GetArterOvenousList(user_org_id int64) (models.XtArterOvenous, error) {
  3151. ovenous := models.XtArterOvenous{}
  3152. err := XTReadDB().Where("user_org_id = ? and status=1", user_org_id).Find(&ovenous).Error
  3153. return ovenous, err
  3154. }
  3155. func CreateArterOvernous(ovenous models.XtArterOvenous) error {
  3156. err := XTWriteDB().Create(&ovenous).Error
  3157. return err
  3158. }
  3159. func UpdateArterOvernous(id int64, content string) error {
  3160. err := XTWriteDB().Model(&models.XtArterOvenous{}).Where("id = ? and status =1", id).Updates(map[string]interface{}{"content": content}).Error
  3161. return err
  3162. }
  3163. func GetHightRikInformed(user_org_id int64) (models.XtHightRisk, error) {
  3164. risk := models.XtHightRisk{}
  3165. err := XTReadDB().Where("user_org_id = ? and status =1", user_org_id).Find(&risk).Error
  3166. return risk, err
  3167. }
  3168. func CreateHighRik(risk models.XtHightRisk) error {
  3169. err := XTWriteDB().Create(&risk).Error
  3170. return err
  3171. }
  3172. func UpdateHighRik(id int64, content string) error {
  3173. err := XTWriteDB().Model(&models.XtHightRisk{}).Where("id = ? and status =1", id).Updates(map[string]interface{}{"content": content}).Error
  3174. return err
  3175. }
  3176. func GetAnticoagulation(user_org_id int64) (models.XtAuticoagutionPrint, error) {
  3177. auticoagutionPrint := models.XtAuticoagutionPrint{}
  3178. err := XTReadDB().Where("user_org_id = ? and status =1", user_org_id).Find(&auticoagutionPrint).Error
  3179. return auticoagutionPrint, err
  3180. }
  3181. func CreateAnticoagulation(auticoagutionPrint models.XtAuticoagutionPrint) error {
  3182. err := XTWriteDB().Create(&auticoagutionPrint).Error
  3183. return err
  3184. }
  3185. func UpdateAnticoagulation(id int64, content string) error {
  3186. err := XTWriteDB().Model(&models.XtAuticoagutionPrint{}).Where("id = ? and status =1", id).Updates(map[string]interface{}{"content": content}).Error
  3187. return err
  3188. }
  3189. func GetCriticalInfomed(org_id int64) (models.XtCriticalinfomedPrint, error) {
  3190. criticalinfomedPrint := models.XtCriticalinfomedPrint{}
  3191. err := XTReadDB().Where("user_org_id = ? and status=1", org_id).Find(&criticalinfomedPrint).Error
  3192. return criticalinfomedPrint, err
  3193. }
  3194. func CreatedCriticalInfomed(criticalinfomedPrint models.XtCriticalinfomedPrint) error {
  3195. err := XTWriteDB().Create(&criticalinfomedPrint).Error
  3196. return err
  3197. }
  3198. func UpdateCriticalInfomed(id int64, content string) error {
  3199. err := XTWriteDB().Model(&models.XtCriticalinfomedPrint{}).Where("id = ? and status=1", id).Updates(map[string]interface{}{"content": content}).Error
  3200. return err
  3201. }
  3202. func GetHemoperfusionInformed(user_org_id int64) (models.XtHemoperfusionmedPrint, error) {
  3203. hemoperfusionmedPrint := models.XtHemoperfusionmedPrint{}
  3204. err := XTReadDB().Where("user_org_id = ? and status =1", user_org_id).Find(&hemoperfusionmedPrint).Error
  3205. return hemoperfusionmedPrint, err
  3206. }
  3207. func CreateHemoperfusionInformed(hemoperfusionmedPrint models.XtHemoperfusionmedPrint) error {
  3208. err := XTWriteDB().Create(&hemoperfusionmedPrint).Error
  3209. return err
  3210. }
  3211. func UpdateHemoperfusionInformed(id int64, content string) error {
  3212. err := XTWriteDB().Model(&models.XtHemoperfusionmedPrint{}).Where("id = ? and status=1", id).Updates(map[string]interface{}{"content": content}).Error
  3213. return err
  3214. }
  3215. func GetBringInformed(org_id int64) (models.XtBringinformedPrint, error) {
  3216. bringinformedPrint := models.XtBringinformedPrint{}
  3217. err := XTReadDB().Where("user_org_id = ? and status=1", org_id).Find(&bringinformedPrint).Error
  3218. return bringinformedPrint, err
  3219. }
  3220. func CreateBringInformed(bringinformedPrint models.XtBringinformedPrint) error {
  3221. err := XTWriteDB().Create(&bringinformedPrint).Error
  3222. return err
  3223. }
  3224. func UpdateBringInformed(id int64, content string) error {
  3225. err := XTWriteDB().Model(&models.XtBringinformedPrint{}).Where("id =? and status= 1", id).Updates(map[string]interface{}{"content": content}).Error
  3226. return err
  3227. }
  3228. func GetCatheterization(user_org_id int64) (models.XtCathetercationPrint, error) {
  3229. cathetercationPrint := models.XtCathetercationPrint{}
  3230. err := XTReadDB().Where("user_org_id = ? and status= 1", user_org_id).Find(&cathetercationPrint).Error
  3231. return cathetercationPrint, err
  3232. }
  3233. func CreateCatheteriztion(cathetercationPrint models.XtCathetercationPrint) error {
  3234. err := XTWriteDB().Create(&cathetercationPrint).Error
  3235. return err
  3236. }
  3237. func UpdateCatheterization(id int64, content string) error {
  3238. err := XTWriteDB().Model(models.XtCathetercationPrint{}).Where("id=? and status =1", id).Updates(map[string]interface{}{"content": content}).Error
  3239. return err
  3240. }
  3241. func GetDialyzerInformed(org_id int64) (models.XtDialyszerinformedPrint, error) {
  3242. dialyszerinformedPrint := models.XtDialyszerinformedPrint{}
  3243. err := XTReadDB().Where("user_org_id = ? and status=1", org_id).Find(&dialyszerinformedPrint).Error
  3244. return dialyszerinformedPrint, err
  3245. }
  3246. func CreateDialyzerInformed(dialyszerinformedPrint models.XtDialyszerinformedPrint) error {
  3247. err := XTWriteDB().Create(&dialyszerinformedPrint).Error
  3248. return err
  3249. }
  3250. func UpdateDialyszerInformed(id int64, content string) error {
  3251. err := XTWriteDB().Model(&models.XtDialyszerinformedPrint{}).Where("id = ? and status=1", id).Updates(map[string]interface{}{"content": content}).Error
  3252. return err
  3253. }
  3254. func GetTheapyInformed(org_id int64) (models.XtTheapyinformedPrint, error) {
  3255. theapyinformedPrint := models.XtTheapyinformedPrint{}
  3256. err := XTReadDB().Where("user_org_id = ? and status=1", org_id).Find(&theapyinformedPrint).Error
  3257. return theapyinformedPrint, err
  3258. }
  3259. func CreateTheapyInformed(theapyinformedPrint models.XtTheapyinformedPrint) error {
  3260. err := XTWriteDB().Create(&theapyinformedPrint).Error
  3261. return err
  3262. }
  3263. func UpdateTheaphInformed(id int64, content string) error {
  3264. err := XTWriteDB().Model(&models.XtTheapyinformedPrint{}).Where("id = ? and status =1", id).Updates(map[string]interface{}{"content": content}).Error
  3265. return err
  3266. }
  3267. func GetPatientMedicalHistory(patient_id int64, user_org_id int64) (models.XtMedicalHistory, error) {
  3268. history := models.XtMedicalHistory{}
  3269. err := XTReadDB().Model(&history).Where("patient_id = ? and user_org_id = ? and status=1", patient_id, user_org_id).Find(&history).Error
  3270. return history, err
  3271. }
  3272. func UpdatePatientMedicalHistory(id int64, content string) error {
  3273. history := models.XtMedicalHistory{}
  3274. err := XTWriteDB().Model(&history).Where("id = ? and status=1", id).Updates(map[string]interface{}{"content": content}).Error
  3275. return err
  3276. }
  3277. func CreatePatientMedicalHistory(history models.XtMedicalHistory) error {
  3278. err := XTWriteDB().Create(&history).Error
  3279. return err
  3280. }
  3281. func GetPatientMedicalList(patient_id int64, user_org_id int64) (models.XtMedicalHistory, error) {
  3282. history := models.XtMedicalHistory{}
  3283. err := XTReadDB().Where("patient_id = ? and user_org_id = ? and status=1", patient_id, user_org_id).Find(&history).Error
  3284. return history, err
  3285. }
  3286. func CreatePatientFallassessment(tx *gorm.DB, patientFallassessment models.XtPatientFallassessment) (err error) {
  3287. err = tx.Create(&patientFallassessment).Error
  3288. if err == gorm.ErrRecordNotFound {
  3289. tx.Rollback()
  3290. return
  3291. }
  3292. return
  3293. }
  3294. func SavePatientFallassessment(tx *gorm.DB, patientFallassessment models.XtPatientFallassessment) (err error) {
  3295. err = tx.Save(&patientFallassessment).Error
  3296. if err == gorm.ErrRecordNotFound {
  3297. tx.Rollback()
  3298. return
  3299. }
  3300. return
  3301. }
  3302. func GetPatientFallsessMentList(patient_id int64, user_org_id int64, limit int64, page int64) (list []*models.XtPatientFallassessment, total int64, err error) {
  3303. offset := (page - 1) * limit
  3304. db := XTReadDB().Model(&list).Where("status =1")
  3305. if patient_id > 0 {
  3306. db = db.Where("patient_id = ?", patient_id)
  3307. }
  3308. if user_org_id > 0 {
  3309. db = db.Where("user_org_id = ?", user_org_id)
  3310. }
  3311. err = db.Count(&total).Offset(offset).Limit(limit).Find(&list).Error
  3312. return list, total, err
  3313. }
  3314. func GetPatientFallsessMentListOne(ids []string, user_org_id int64) (list []*models.XtPatientFallassessment, err error) {
  3315. db := XTReadDB().Model(&list).Where("status =1")
  3316. if len(ids) > 0 {
  3317. db = db.Where("id in(?)", ids)
  3318. }
  3319. if user_org_id > 0 {
  3320. db = db.Where("user_org_id = ?", user_org_id)
  3321. }
  3322. err = db.Find(&list).Error
  3323. return list, err
  3324. }
  3325. func GetPatientFallsessmentById(id int64, user_org_id int64) (models.XtPatientFallassessment, error) {
  3326. fallassessment := models.XtPatientFallassessment{}
  3327. err := XTReadDB().Where("id = ? and user_org_id = ?", id, user_org_id).Find(&fallassessment).Error
  3328. return fallassessment, err
  3329. }
  3330. func DeletePatientFallsessment(id int64, user_org_id int64) (models.XtPatientFallassessment, error) {
  3331. fallassessment := models.XtPatientFallassessment{}
  3332. err := XTWriteDB().Model(&fallassessment).Where("id =? and status=1", id).Updates(map[string]interface{}{"status": 0}).Error
  3333. return fallassessment, err
  3334. }
  3335. func CreatePadFallssessment(fallssessment models.XtPatientPedFallssessment) error {
  3336. err := XTWriteDB().Create(&fallssessment).Error
  3337. return err
  3338. }
  3339. func UpdatePedFallssessment(fallssessment models.XtPatientPedFallssessment) error {
  3340. err := XTWriteDB().Save(&fallssessment).Error
  3341. return err
  3342. }
  3343. func GetBedDiatricFallassessmentList(patient_id int64, is_type int64, user_org_id int64, limit int64, page int64) (list []*models.XtPatientPedFallssessment, total int64, err error) {
  3344. offset := (page - 1) * limit
  3345. db := XTReadDB().Model(&list).Where("status =1")
  3346. if patient_id > 0 {
  3347. db = db.Where("patient_id = ?", patient_id)
  3348. }
  3349. if user_org_id > 0 {
  3350. db = db.Where("user_org_id = ?", user_org_id)
  3351. }
  3352. if is_type > 0 {
  3353. db = db.Where("type = ?", is_type)
  3354. }
  3355. err = db.Count(&total).Offset(offset).Limit(limit).Find(&list).Error
  3356. return list, total, err
  3357. }
  3358. func GetPedPatientFallassessmentById(id int64) (models.XtPatientPedFallssessment, error) {
  3359. fallassessment := models.XtPatientPedFallssessment{}
  3360. err := XTReadDB().Where("id = ? and status=1", id).Find(&fallassessment).Error
  3361. return fallassessment, err
  3362. }
  3363. func DeletePadPatientFallassment(id int64) (err error) {
  3364. err = XTWriteDB().Model(&models.XtPatientPedFallssessment{}).Where("id=? and status=1", id).Updates(map[string]interface{}{"status": 0}).Error
  3365. return err
  3366. }
  3367. func GetPatientCritaicalInformed(user_org_id int64) (models.XtPatientCritical, error) {
  3368. patientCritical := models.XtPatientCritical{}
  3369. err := XTReadDB().Where("user_org_id = ? and status=1", user_org_id).Find(&patientCritical).Error
  3370. return patientCritical, err
  3371. }
  3372. func CreatePatientCritaicalinformed(critical models.XtPatientCritical) error {
  3373. err = XTWriteDB().Create(&critical).Error
  3374. return err
  3375. }
  3376. func UpdatePatientCritaicalInformed(id int64, content string) error {
  3377. err := XTWriteDB().Model(&models.XtPatientCritical{}).Where("id = ? and status=1", id).Updates(map[string]interface{}{"content": content}).Error
  3378. return err
  3379. }
  3380. func GetPatientCriticalByUserOrgId(user_org_id int64) (models.XtPatientCriticalOne, error) {
  3381. criticalOne := models.XtPatientCriticalOne{}
  3382. err := XTReadDB().Where("user_org_id = ? and status=1", user_org_id).Find(&criticalOne).Error
  3383. return criticalOne, err
  3384. }
  3385. func CreatePatientCritical(patient models.XtPatientCriticalOne) error {
  3386. err := XTWriteDB().Create(&patient).Error
  3387. return err
  3388. }
  3389. func UpdatePatientCriticalOne(user_org_id int64, content string) error {
  3390. err := XTWriteDB().Model(&models.XtPatientCriticalOne{}).Where("user_org_id = ? and status=1", user_org_id).Updates(map[string]interface{}{"content": content}).Error
  3391. return err
  3392. }
  3393. func GetPatientCriticalOneList(user_org_id int64) (models.XtPatientCriticalOne, error) {
  3394. criticalOne := models.XtPatientCriticalOne{}
  3395. err := XTReadDB().Where("user_org_id = ? and status =1", user_org_id).Find(&criticalOne).Error
  3396. return criticalOne, err
  3397. }
  3398. func CreatePatientPedList(fallssessment models.XtPatientPedFallssessment) error {
  3399. err := XTWriteDB().Create(&fallssessment).Error
  3400. return err
  3401. }
  3402. func UpdatePatientPedList(fallssessment models.XtPatientPedFallssessment) error {
  3403. err := XTWriteDB().Save(&fallssessment).Error
  3404. return err
  3405. }
  3406. func GetPatientFallsessMentListTwo(ids []string, user_org_id int64) (list []*models.XtPatientFallassessment, err error) {
  3407. db := XTReadDB().Model(&list).Where("status =1")
  3408. if len(ids) > 0 {
  3409. db = db.Where("id in(?)", ids)
  3410. }
  3411. if user_org_id > 0 {
  3412. db = db.Where("user_org_id = ?", user_org_id)
  3413. }
  3414. err = db.Find(&list).Error
  3415. return list, err
  3416. }
  3417. func CeatePatientSign(braden models.XtPatientBraden) error {
  3418. err := XTWriteDB().Create(&braden).Error
  3419. return err
  3420. }
  3421. func GetPatientBradenList(patient_id int64, limit int64, page int64, user_org_id int64) (list []*models.XtPatientBraden, total int64, err error) {
  3422. offset := (page - 1) * limit
  3423. db := XTReadDB().Model(&list).Where("status =1")
  3424. if patient_id > 0 {
  3425. db = db.Where("patient_id = ?", patient_id)
  3426. }
  3427. if user_org_id > 0 {
  3428. db = db.Where("user_org_id = ?", user_org_id)
  3429. }
  3430. err = db.Count(&total).Offset(offset).Limit(limit).Find(&list).Error
  3431. return list, total, err
  3432. }
  3433. func GetPatientBradenById(id int64) (models.XtPatientBraden, error) {
  3434. patientBraden := models.XtPatientBraden{}
  3435. err := XTReadDB().Where("id = ? and status=1", id).Find(&patientBraden).Error
  3436. return patientBraden, err
  3437. }
  3438. func SavePatientBraden(braden models.XtPatientBraden) error {
  3439. err := XTWriteDB().Save(&braden).Error
  3440. return err
  3441. }
  3442. func DeletePatientBraden(id int64) error {
  3443. patientBraden := models.XtPatientBraden{}
  3444. err := XTWriteDB().Model(&patientBraden).Where("id = ? and status=1", id).Updates(map[string]interface{}{"status": 0}).Error
  3445. return err
  3446. }
  3447. func GetBatchPatientBraden(ids []string, user_org_id int64) (list []*models.XtPatientBraden, err error) {
  3448. db := XTReadDB().Model(&list).Where("status=1")
  3449. if len(ids) > 0 {
  3450. db = db.Where("id in(?)", ids)
  3451. }
  3452. if user_org_id > 0 {
  3453. db = db.Where("user_org_id =?", user_org_id)
  3454. }
  3455. err = db.Find(&list).Error
  3456. return list, err
  3457. }
  3458. func GetAllPatientListMap(user_org_id int64) (list []*models.XtAgePatients, err error) {
  3459. err = XTReadDB().Where("user_org_id =? and status=1 and lapseto = 1", user_org_id).Find(&list).Error
  3460. return list, err
  3461. }
  3462. func GetLastBefor(patient_id int64) (models.XtAssessmentBeforeDislysis, error) {
  3463. beforeDislysis := models.XtAssessmentBeforeDislysis{}
  3464. err := XTReadDB().Where("patient_id = ? and status=1 AND assessment_date>=1704038400 AND assessment_date<=1711814400", patient_id).Order("id asc").Last(&beforeDislysis).Limit(1).Error
  3465. return beforeDislysis, err
  3466. }
  3467. func GetLastRecordAfter(patient_id int64) (models.XtAssessmentAfterDislysis, error) {
  3468. afterDislysis := models.XtAssessmentAfterDislysis{}
  3469. err := XTReadDB().Where("patient_id = ? and status=1 AND assessment_date>=1704038400 AND assessment_date<=1711814400", patient_id).Order("id asc").Last(&afterDislysis).Limit(1).Error
  3470. return afterDislysis, err
  3471. }
  3472. func GetInspectionByPatientId(patient_id int64, item_id int64) (models.XtInspection, error) {
  3473. inspection := models.XtInspection{}
  3474. err := XTReadDB().Where("patient_id =? and status =1 and inspect_date>=1704038400 and inspect_date<=1711814400 and item_id =?", patient_id, item_id).Order("id asc").Last(&inspection).Limit(1).Error
  3475. return inspection, err
  3476. }
  3477. func DeleteSolutionById(id int64) (models.DialysisSolution, error) {
  3478. solution := models.DialysisSolution{}
  3479. err := XTWriteDB().Model(&solution).Where("id=? and status=1", id).Updates(map[string]interface{}{"status": 12}).Error
  3480. return solution, err
  3481. }
  3482. func GetPatientBeforRecordList(patient_id int64, user_org_id int64) (befor []*models.XtAssessmentBeforeDislysis, err error) {
  3483. err = XTReadDB().Where("patient_id = ? and user_org_id =? and status=1", patient_id, user_org_id).Order("id desc").Limit(5).Find(&befor).Error
  3484. return befor, err
  3485. }
  3486. func GetPatientAfterRecordList(patient_id int64, user_org_id int64) (after []*models.XtAssessmentAfterDislysis, err error) {
  3487. err = XTReadDB().Where("patient_id = ? and user_org_id =? and status=1", patient_id, user_org_id).Order("id desc").Limit(5).Find(&after).Error
  3488. return after, err
  3489. }
  3490. func GetMonitorRecordByGroup(patient_id int64, user_org_id int64) (monitor []*models.MonitoringRecordOne, err error) {
  3491. err = XTReadDB().Where("patient_id = ? and user_org_id =? and status=1 and monitoring_date>=1711900800", patient_id, user_org_id).Order("id desc").Preload("MonitoringRecord", "status=1 and user_org_id =? and monitoring_date>=1711900800", user_org_id).Group("monitoring_date").Limit(5).Find(&monitor).Error
  3492. return monitor, err
  3493. }
  3494. func GetMonitorRecordList(patient_id int64, monitor_date int64, user_org_id int64) (monitor []*models.MonitoringRecord, err error) {
  3495. err = XTReadDB().Where("patient_id = ? and user_org_id =? and monitoring_date = ? and status =1", patient_id, user_org_id, monitor_date).Find(&monitor).Error
  3496. return monitor, err
  3497. }
  3498. func GetDialysisTotalCountByTime(user_org_id int64, start_time int64, end_time int64, patient_id int64) (order []*models.XtDialysisOrder, err error) {
  3499. db := XTReadDB().Table("xt_dialysis_order").Where("status =1")
  3500. if user_org_id > 0 {
  3501. db = db.Where("user_org_id =?", user_org_id)
  3502. }
  3503. if start_time > 0 {
  3504. db = db.Where("dialysis_date >=?", start_time)
  3505. }
  3506. if end_time > 0 {
  3507. db = db.Where("dialysis_date <=?", end_time)
  3508. }
  3509. if patient_id > 0 {
  3510. db = db.Where("patient_id = ?", patient_id)
  3511. }
  3512. err = db.Find(&order).Error
  3513. return order, err
  3514. }
  3515. func GetTreamentPland(user_org_id int64) (models.XtTreatmentPlan, error) {
  3516. treatmentPlan := models.XtTreatmentPlan{}
  3517. err := XTReadDB().Where("user_org_id = ? and status =1", user_org_id).Find(&treatmentPlan).Error
  3518. return treatmentPlan, err
  3519. }
  3520. func CreateTreatqmentPland(plan models.XtTreatmentPlan) error {
  3521. err := XTWriteDB().Create(&plan).Error
  3522. return err
  3523. }
  3524. func UpdateTheatmentPland(id int64, content string) error {
  3525. err := XTWriteDB().Model(&models.XtTreatmentPlan{}).Where("id = ? and status =1", id).Updates(map[string]interface{}{"content": content}).Error
  3526. return err
  3527. }
  3528. func GetEditAdviceActionList(id int64, org_id int64) (models.XtDoctorAdviceOne, error) {
  3529. doctorAdviceOne := models.XtDoctorAdviceOne{}
  3530. err := XTReadDB().Where("id = ? and status=1 and user_org_id = ?", id, org_id).Find(&doctorAdviceOne).Error
  3531. return doctorAdviceOne, err
  3532. }
  3533. func CreatePatientRecord(record models.XtPatientLaspseRecord) error {
  3534. err := XTWriteDB().Create(&record).Error
  3535. return err
  3536. }
  3537. func GetPatientLapseRecord(patient_id int64, limit int64, page int64, user_org_id int64) (record []*models.XtPatientLaspseRecord, total int64, err error) {
  3538. db := XTReadDB().Model(&record).Where("status=1")
  3539. offset := (page - 1) * limit
  3540. if patient_id > 0 {
  3541. db = db.Where("patient_id = ?", patient_id)
  3542. }
  3543. if user_org_id > 0 {
  3544. db = db.Where("user_org_id = ?", user_org_id)
  3545. }
  3546. err = db.Count(&total).Offset(offset).Limit(limit).Order("record_date desc").Find(&record).Error
  3547. return record, total, err
  3548. }
  3549. func GetPatientLapsorecord(id int64) (models.XtPatientLaspseRecord, error) {
  3550. laspseRecord := models.XtPatientLaspseRecord{}
  3551. err := XTReadDB().Where("id = ? and status= 1", id).Find(&laspseRecord).Error
  3552. return laspseRecord, err
  3553. }
  3554. func SavePatientLapseRecord(record models.XtPatientLaspseRecord) error {
  3555. err := XTWriteDB().Save(&record).Error
  3556. return err
  3557. }
  3558. func DeletePatientLapseRecord(id int64) error {
  3559. record := models.XtPatientLaspseRecord{}
  3560. err := XTWriteDB().Model(&record).Where("id = ? and status=1", id).Updates(map[string]interface{}{"status": 0}).Error
  3561. return err
  3562. }
  3563. func GetPatientLapsorecordListOne() (lapseto []*models.PatientLapseto, err error) {
  3564. err = XTReadDB().Where("status=1 and (lapseto_type=2 or lapseto_type =3)").Find(&lapseto).Error
  3565. return lapseto, err
  3566. }
  3567. func GetPatientBefor(patient_id int64, record_date int64, user_org_id int64) (models.XtAssessmentBeforeDislysis, error) {
  3568. beforeDislysis := models.XtAssessmentBeforeDislysis{}
  3569. err := XTReadDB().Where("patient_id = ? and assessment_date= ? and user_org_id = ? and status=1", patient_id, record_date, user_org_id).Find(&beforeDislysis).Error
  3570. return beforeDislysis, err
  3571. }
  3572. func GetDialysisPrescribeOneList(patient_id int64, record_date int64, user_org_id int64) (models.DialysisPrescription, error) {
  3573. prescription := models.DialysisPrescription{}
  3574. err := XTReadDB().Where("patient_id = ? and record_date= ? and user_org_id = ? and status=1", patient_id, record_date, user_org_id).Find(&prescription).Error
  3575. return prescription, err
  3576. }
  3577. func CreateDeathSummary(summary models.XtDeathSummary) error {
  3578. err := XTWriteDB().Save(&summary).Error
  3579. return err
  3580. }
  3581. func GetPatientDeathSummary(patient_id int64, user_org_id int64) (models.XtDeathSummary, error) {
  3582. deathSummary := models.XtDeathSummary{}
  3583. err := XTReadDB().Where("patient_id = ? and user_org_id = ? and status=1", patient_id, user_org_id).Find(&deathSummary).Error
  3584. return deathSummary, err
  3585. }
  3586. func DeletePatientDeathSummary(id int64) error {
  3587. summary := models.XtDeathSummary{}
  3588. err := XTWriteDB().Model(&summary).Where("id = ? and status=1", id).Updates(map[string]interface{}{"status": 0}).Error
  3589. return err
  3590. }