patient_service.go 159KB

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