patient_service.go 168KB

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