patient_service.go 131KB

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