patient_service.go 136KB

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