patient_service.go 115KB

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