patient_service.go 155KB

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