patient_service.go 190KB

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