From 50a5c5e12861dda42e7216302cacd7dae517bbaa Mon Sep 17 00:00:00 2001 From: sh Date: Wed, 3 Dec 2025 17:55:21 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9pc=E7=AB=AF=E6=9F=A5=E8=AF=A2?= =?UTF-8?q?=E6=8E=A8=E8=8D=90=E5=B2=97=E4=BD=8D=E9=80=BB=E8=BE=91=201.?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=BF=87=E6=9C=9F=E6=97=B6=E9=97=B4=202.?= =?UTF-8?q?=E9=99=90=E5=88=B6=E6=9D=A1=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cms/service/impl/JobServiceImpl.java | 69 +++++++++++++------ 1 file changed, 48 insertions(+), 21 deletions(-) diff --git a/ruoyi-bussiness/src/main/java/com/ruoyi/cms/service/impl/JobServiceImpl.java b/ruoyi-bussiness/src/main/java/com/ruoyi/cms/service/impl/JobServiceImpl.java index 1e6982c..dfb59c2 100644 --- a/ruoyi-bussiness/src/main/java/com/ruoyi/cms/service/impl/JobServiceImpl.java +++ b/ruoyi-bussiness/src/main/java/com/ruoyi/cms/service/impl/JobServiceImpl.java @@ -47,6 +47,8 @@ import java.time.format.DateTimeFormatter; import java.util.*; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; +import java.util.stream.Stream; + /** * 岗位Service业务层处理 * @@ -925,30 +927,55 @@ public class JobServiceImpl extends ServiceImpl implements IJobSe @Override public List sysRecommend(ESJobSearch esJobSearch) { - Long userId=SecurityUtils.isLogin()?SecurityUtils.getUserId():null; - List jobList=new ArrayList<>(); - String jobKey=""; - AppUser appUser=null; - if(userId!=null){ - jobKey=CacheConstants.SYS_JOB_IDS+ userId; - RedisCache redisCache = SpringUtils.getBean(RedisCache.class); - JSONArray cacheObject = redisCache.getCacheObject(jobKey); - jobList = new ArrayList<>(); - if(Objects.isNull(cacheObject)){ - ArrayList longs = new ArrayList<>(); - jobList =longs; - }else { - jobList = cacheObject.toList(Long.class); + Long userId = SecurityUtils.isLogin() ? SecurityUtils.getUserId() : null; + List viewedJobIds = new ArrayList<>(); + String jobCacheKey = ""; + AppUser appUser = null; + + if (userId != null) { + jobCacheKey = CacheConstants.SYS_JOB_IDS + userId; + try { + JSONArray cacheObject = redisCache.getCacheObject(jobCacheKey); + if (Objects.isNull(cacheObject)) { + cacheObject = new JSONArray(); + } + viewedJobIds = cacheObject.stream() + .map(o -> Long.parseLong(o.toString())) + .distinct().limit(100).collect(Collectors.toList()); + + String idCard = RoleUtils.getCurrentUseridCard(); + if (StringUtils.isNotEmpty(idCard)) { + appUser = appUserService.selectAppuserByIdcard(idCard); + } + } catch (Exception e) { + log.error("获取用户已查看岗位缓存失败", e); + viewedJobIds = new ArrayList<>(); } - appUser=appUserService.selectAppuserByIdcard(RoleUtils.getCurrentUseridCard()); } - //从es中查询 - List jobListResult = iesJobSearchService.selectSysTextListExceptJobId(esJobSearch,jobList,appUser); - //存入当前session中查看的岗位 避免重复 todo 定时删除 key上保存用户信息 - jobList.addAll(jobListResult.stream().map(ESJobDocument::getJobId).collect(Collectors.toList())); - if(StringUtils.isNotEmpty(jobKey)){ - redisCache.setCacheObject(jobKey,jobList); + + List jobListResult = new ArrayList<>(); + try { + jobListResult = iesJobSearchService.selectSysTextListExceptJobId(esJobSearch, viewedJobIds, appUser); + //降级策略:如果过滤后无数据,忽略已查看记录重新查询 + if (CollectionUtils.isEmpty(jobListResult) && !viewedJobIds.isEmpty()) { + log.warn("用户{}已查看岗位过多,忽略过滤条件重新查询"); + jobListResult = iesJobSearchService.selectSysTextListExceptJobId(esJobSearch, new ArrayList<>(), appUser); + } + } catch (Exception e) { + log.error("ES推荐岗位查询失败", e); + return new ArrayList<>(); } + + if (userId != null && !CollectionUtils.isEmpty(jobListResult)) { + try { + List newJobIds = jobListResult.stream().map(ESJobDocument::getJobId).distinct().collect(Collectors.toList()); + List updatedJobIds = Stream.concat(viewedJobIds.stream(), newJobIds.stream()).distinct().limit(100).collect(Collectors.toList()); + redisCache.setCacheObject(jobCacheKey, updatedJobIds, 24, TimeUnit.HOURS); + } catch (Exception e) { + log.error("更新用户已查看岗位缓存失败", e); + } + } + List esJobDocuments = sysUserCollection(jobListResult); return esJobDocuments; }