patient_service.go 166KB

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