patient_service.go 118KB

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