patient_service.go 110KB

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