patient_service.go 114KB

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