修改pc端查询推荐岗位逻辑

1.添加过期时间
2.限制条数
This commit is contained in:
sh
2025-12-03 17:55:21 +08:00
parent 6adda93d5d
commit 50a5c5e128

View File

@@ -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<JobMapper,Job> implements IJobSe
@Override
public List<ESJobDocument> sysRecommend(ESJobSearch esJobSearch) {
Long userId=SecurityUtils.isLogin()?SecurityUtils.getUserId():null;
List<Long> 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<Long> longs = new ArrayList<>();
jobList =longs;
}else {
jobList = cacheObject.toList(Long.class);
Long userId = SecurityUtils.isLogin() ? SecurityUtils.getUserId() : null;
List<Long> 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<ESJobDocument> 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<ESJobDocument> 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<Long> newJobIds = jobListResult.stream().map(ESJobDocument::getJobId).distinct().collect(Collectors.toList());
List<Long> 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<ESJobDocument> esJobDocuments = sysUserCollection(jobListResult);
return esJobDocuments;
}