patient_service.go 124KB

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