patient_service.go 114KB

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