From cef8129dd595e2d881592cb83dd302971665a085 Mon Sep 17 00:00:00 2001 From: jianjun Date: Sat, 19 Mar 2022 11:53:32 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=84=E7=BB=87=E6=A0=91=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E4=B8=B4=E6=97=B6=E7=BC=93=E5=AD=98=20=E8=A6=81=E4=B8=8D?= =?UTF-8?q?=E7=84=B6=E5=89=8D=E7=AB=AF=E5=AE=B9=E6=98=93=E6=8C=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../epmet/commons/tools/redis/RedisUtils.java | 4 +++ .../govorg/impl/GovOrgServiceImpl.java | 36 +++++++++++++++++-- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/redis/RedisUtils.java b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/redis/RedisUtils.java index 32cba063c5..27636e435a 100644 --- a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/redis/RedisUtils.java +++ b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/redis/RedisUtils.java @@ -440,4 +440,8 @@ public class RedisUtils { public Long bitCount(String key, int start, int end) { return redisTemplate.execute((RedisCallback) con -> con.bitCount(key.getBytes(), start, end)); } + + public Long getTTL(String treeCacheKey) { + return redisTemplate.getExpire(treeCacheKey); + } } diff --git a/epmet-module/data-aggregator/data-aggregator-server/src/main/java/com/epmet/dataaggre/service/govorg/impl/GovOrgServiceImpl.java b/epmet-module/data-aggregator/data-aggregator-server/src/main/java/com/epmet/dataaggre/service/govorg/impl/GovOrgServiceImpl.java index 00997c59cd..e943dd5894 100644 --- a/epmet-module/data-aggregator/data-aggregator-server/src/main/java/com/epmet/dataaggre/service/govorg/impl/GovOrgServiceImpl.java +++ b/epmet-module/data-aggregator/data-aggregator-server/src/main/java/com/epmet/dataaggre/service/govorg/impl/GovOrgServiceImpl.java @@ -12,8 +12,11 @@ import com.epmet.commons.tools.constant.StrConstant; import com.epmet.commons.tools.dto.form.DingTextBriefNessFormDTO; import com.epmet.commons.tools.dto.result.CustomerStaffInfoCacheResult; import com.epmet.commons.tools.exception.EpmetErrorCode; +import com.epmet.commons.tools.exception.EpmetException; import com.epmet.commons.tools.exception.ExceptionUtils; import com.epmet.commons.tools.exception.RenException; +import com.epmet.commons.tools.redis.RedisKeys; +import com.epmet.commons.tools.redis.RedisUtils; import com.epmet.commons.tools.redis.common.CustomerOrgRedis; import com.epmet.commons.tools.redis.common.CustomerStaffRedis; import com.epmet.commons.tools.redis.common.bean.AgencyInfoCache; @@ -60,6 +63,7 @@ import java.io.OutputStream; import java.text.NumberFormat; import java.text.SimpleDateFormat; import java.util.*; +import java.util.concurrent.ExecutorService; import java.util.stream.Collectors; /** @@ -95,6 +99,10 @@ public class GovOrgServiceImpl implements GovOrgService { private EvaluationIndexService evaluationIndexService; @Autowired private IcBuildingDao icBuildingDao; + @Autowired + private RedisUtils redisUtils; + @Autowired + private ExecutorService executorService; /** * @param staffId @@ -583,12 +591,34 @@ public class GovOrgServiceImpl implements GovOrgService { @Override public List getAgencyTree(TokenDto tokenDto, SubOrgFormDTO formDTO) { CustomerStaffInfoCacheResult staffInfo = CustomerStaffRedis.getStaffInfo(tokenDto.getCustomerId(), tokenDto.getUserId()); + if (staffInfo == null || StringUtils.isBlank(staffInfo.getAgencyId())){ + throw new EpmetException(EpmetErrorCode.EPMET_COMMON_OPERATION_FAIL.getCode(),"工作人员所属组织不存在"); + } + //组织缓存key + String treeCacheKey = RedisKeys.getOrgTreeCacheKey(formDTO.getAgencyId()).concat(StrConstant.COLON).concat(formDTO.getClient()); + Long expiryTime = redisUtils.getTTL(treeCacheKey); + List orgTreeResultDTOS = (List) redisUtils.get(treeCacheKey); + String agencyId = staffInfo.getAgencyId(); + String client = formDTO.getClient(); + //如果接近过期或已经过期且缓存数据不为空 则异步查询 + if ((expiryTime == null || expiryTime <= NumConstant.ONE_THOUSAND) && CollectionUtils.isNotEmpty(orgTreeResultDTOS)) { + executorService.submit(() -> { + List list = buildTempOrgTree(agencyId, client); + redisUtils.set(treeCacheKey, list, RedisUtils.HOUR_FOUR_EXPIRE); + }); + } else { + orgTreeResultDTOS = buildTempOrgTree(staffInfo.getAgencyId(), formDTO.getClient()); + redisUtils.set(treeCacheKey, orgTreeResultDTOS, RedisUtils.HOUR_FOUR_EXPIRE); + } + return orgTreeResultDTOS; + } + private List buildTempOrgTree(String agencyId, String client){ List list = new ArrayList<>(); - if ("resi".equals(formDTO.getClient())) { - list.add(customerAgencyDao.getResiOrgTree(staffInfo.getAgencyId())); + if ("resi".equals(client)) { + list.add(customerAgencyDao.getResiOrgTree(agencyId)); return list; } - list.add(customerAgencyDao.getOrgTree(staffInfo.getAgencyId())); + list.add(customerAgencyDao.getOrgTree(agencyId)); return list; }