redis中如何进行分页查询以及redis简历关注后的操作

redis中如何进行分页查询以及redis简历关注后的操作

针对于较多数据存储于redis中,我们需要进行分页查询相关数据应用场景时,下面我们介绍一下如何进行redis的分页查询。
lastId就是图中对应的value值,zrevrank是个人简历库中简历排名名次。在这里插入图片描述

redis中如何进行分页查询
 @ResponseBody
    @RequestMapping(value = "/myResumeRepository", method = RequestMethod.GET)
    @CheckParam("userId,lastId")
    public ResultMessage myResumeRepository(
            @RequestParam(value = "userId") Integer userId,
            @RequestParam(value = "lastId") Integer lastId,
            @RequestParam(value = "size", required = false, defaultValue = "10") int size) {
        Map<String, Object> resultData = new HashMap<>();
        List<Map<String, Object>> resumes = new ArrayList<>();
        Integer end = 1;
        try {
            Set<ZSetOperations.TypedTuple<Object>> typedTuples = null;
            /**实现分页查询列表数据*/
            if (lastId == 0) {
            //该方法三个参数分别是相应的key值,查询最小排名0,需要查询的排名size
                typedTuples = redisUtil.reverseRangeWithScores(RedisKey_ResumeConstants.BASE_RESUME_USER + userId + ":repository", 0, size);
            } else {
                /**根据末尾简历id获取索引*/
                //改方法是根据相关的key值,value值 lastid查询出该简历的排名名次
                Long zrevrank = redisUtil.reverseRank(RedisKey_ResumeConstants.BASE_RESUME_USER + userId + ":repository", lastId);
                if (zrevrank != null) {
                    zrevrank++;
                     //该方法三个参数分别是相应的key值,查询最小排名zrevrank,需要查询的排名zrevrank + size
                    typedTuples = redisUtil.reverseRangeWithScores(RedisKey_ResumeConstants.BASE_RESUME_USER + userId + ":repository", zrevrank, zrevrank + size);
                } else {
                    logger.error(String.format("未在用户简历库里查询到简历信息,userId:%d   lastId:%d", userId, lastId));
                    return new ResultMessage(ResponseCode.DATA_NOT_FOUND.getCode(), "未找到相关信息", "");
                }
            }
            /**判断是否为最后一条*/
            if (typedTuples != null && typedTuples.size() < (size + 1))
                end = -1;
            /**
             * 获取简历详细信息,进行过滤处理
             */
            for (ZSetOperations.TypedTuple<Object> typedTuple : typedTuples) {
                /**
                 * 根据简历id获取简历信息
                 */
                Map<Object, Object> resumeInfo = redisUtil.hmget(RedisKey_ResumeConstants.BASE_RESUME_INFO + typedTuple.getValue());
                if (resumeInfo == null || resumeInfo.size() == 0) {
                    logger.error(RedisKey_ResumeConstants.BASE_RESUME_INFO + typedTuple.getValue() + "\t\t\t简历信息不存在。");
                    continue;
                }
                /**
                 * 根据简历id获取职位信息
                 */
                Object hgetDemand = redisUtil.hget(RedisKey_ApplyConstants.BASE_APPLY_NEWEST + userId, typedTuple.getValue().toString());
                String jobType = "";    //职位类型
                if (hgetDemand != null) {
                    String[] split = hgetDemand.toString().split(",");
                    if (split.length > 1)
                        jobType = split[1];
                }
                /**意向城市*/
                JSONArray intentionCityArray = new JSONArray();
                if (resumeInfo.get("intentionalCity") != null) {
                    List intentionalCityList = (List) resumeInfo.get("intentionalCity");
                    if (intentionalCityList != null && intentionalCityList.size() > 0) {
                        HashSet hashSet = new HashSet(intentionalCityList);
                        intentionalCityList.clear();
                        intentionalCityList.addAll(hashSet);
                        intentionCityArray = JSONArray.parseArray(intentionalCityList.toString());
                    }
                }
                /**工作经历*/
                JSONArray resumeExperienceArray = new JSONArray();
                if (resumeInfo.get("resumeExperienceList") != null) {
                    List resumeExperienceList = (List) resumeInfo.get("resumeExperienceList");
                    if (resumeExperienceList != null && resumeExperienceList.size() > 0) {
                        HashSet hashSet = new HashSet(resumeExperienceList);
                        resumeExperienceList.clear();
                        resumeExperienceList.addAll(hashSet);
                        resumeExperienceArray = JSONArray.parseArray(resumeExperienceList.toString());
                    }
                }
                /**期望岗位*/
                JSONArray intentionPositionArray = new JSONArray();
                if (resumeInfo.get("intentionPosition") != null) {
                    List intentionPositionList = (List) resumeInfo.get("intentionPosition");
                    if (intentionPositionList != null && intentionPositionList.size() > 0) {
                        HashSet hashSet = new HashSet(intentionPositionList);
                        intentionPositionList.clear();
                        intentionPositionList.addAll(hashSet);
                        intentionPositionArray = JSONArray.parseArray(intentionPositionList.toString());
                    }
                }
                /**学历*/
                Object educationObject = resumeInfo.get("education");
                String education = "";
                if (educationObject != null)
                    education = EducationUtil.matchEducation(Integer.valueOf(educationObject.toString()));
                /** 技能标签
                 *  暂无该字段
                 */
                /**计算年龄(根据出生日期)*/
                Integer bornInteger = null;
                if (resumeInfo.get("bornString") != null && !resumeInfo.get("bornString").toString().trim().equals("")) {
                    Date bornDate = DateConvertUtils.parse(resumeInfo.get("bornString").toString(), DateConvertUtils.allTime);
                    bornInteger = DateConvertUtils.getAge(bornDate);
                }
                /**入库时间*/
                Double score = typedTuple.getScore();
                long time = score.longValue();
                Date putTime = new Date(time);
                /**
                 * 过滤返回字段
                 */
                Map<String, Object> filterMap = new HashMap<>();
                /**返回页面视图字段*/
                filterMap.put("id", resumeInfo.get("resumeId"));    //简历id
                filterMap.put("name", resumeInfo.get("name"));                //姓名
                filterMap.put("genderString", resumeInfo.get("genderString"));    //性别
                filterMap.put("bornInteger", bornInteger);   //年龄
                filterMap.put("jobTypeString", jobType);  //关联职位类型
                filterMap.put("intentionalCity", intentionCityArray);   //意向城市
                filterMap.put("mobile", resumeInfo.get("mobile"));  //手机号码
                filterMap.put("updateTime", resumeInfo.get("updateTime")); //简历更新时间
                filterMap.put("putTime", DateConvertUtils.format(putTime, DateConvertUtils.allTime)); //简历入库时间
                filterMap.put("resumeExperience", resumeExperienceArray);//工作经历
                filterMap.put("nativePlace", resumeInfo.get("nativePlace"));  //籍贯
                filterMap.put("education", education);  //学历
                filterMap.put("intentionPosition", intentionPositionArray);  //期望岗位
                filterMap.put("born", resumeInfo.get("bornString") != null ? DateConvertUtils.format(DateConvertUtils.parse(resumeInfo.get("bornString").toString()),DateConvertUtils.allDate): "");  //出生年月
                filterMap.put("workYearInteger", resumeInfo.get("workYearInteger"));  //工作年限
                filterMap.put("skillTags", new JSONArray());  //技能标签
                /**
                 * 区分简历来源,对数据进行处理
                 * tag   001:正常投递   002:智能推荐   003:主动下载
                 */
                Boolean isSeenMobile = redisUtil.sHasKey(RedisKey_ResumeConstants.BASE_RESUME_USER + userId + ":seenMobile", typedTuple.getValue());
                Boolean isApply = redisUtil.hHasKey(RedisKey_ApplyConstants.BASE_APPLY_NEWEST + userId, typedTuple.getValue().toString());
                if (isApply)
                    filterMap.put("tag", "001");
                else if(isSeenMobile)
                    filterMap.put("tag", "003");
                /**获取简历浏览量*/
                Object looked = redisUtil.hget(RedisKey_StatisticsConstants.STATISTICS_RESUME_LOOKED, typedTuple.getValue().toString());
                filterMap.put("looked", looked != null ? looked : 0);
                /**获取简历下载量*/
                Object downloads = redisUtil.hget(RedisKey_StatisticsConstants.STATISTICS_RESUME_DOWNLOADS, typedTuple.getValue().toString());
                filterMap.put("downloads", downloads != null ? downloads : 0);
                /**投递当前渠道商职位数*/
                Integer applyUserDemandCount = applyBaseService.applyUserDemandCount(Long.valueOf(userId), Long.valueOf(typedTuple.getValue().toString()));
                filterMap.put("applyUserDemandCount", applyUserDemandCount != null ? applyUserDemandCount : 0);
                //添加简历数据到返回结果
                resumes.add(filterMap);
                if (resumes.size() >= size)
                    break;
            }
            resultData.put("resumes", resumes);
            resultData.put("end", end);
            return new ResultMessage(ResponseCode.SUCCESS.getCode(), "获取成功", resultData);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultMessage(ResponseCode.SYSTEM_INNER_ERROR.getCode(), "操作异常", "");
        }
    }
redis简历关注后的操作
  /**
     * showdoc
     *
     * @param userId   必选 integer 用户id
     * @param resumeId 必选 integer 简历id
     * @param source   必选 string  来源(search:共享搜索,apply:主动投递)
     * @return {"code":200,"data":{"mobile":"18375894653","name":"万林"},"message":"操作成功"}
     * @catalog 渠道商管理/简历
     * @title 关注简历
     * @description 用户关注简历
     * @method post
     * @url http://ip:port/api/channel/resume/followResume
     * @return_param mobile string 手机号
     * @return_param name string 姓名
     * @remark 渠道商用户对简历进行关注(添加到个人简历库)
     * @number 100
     */
    @ResponseBody
    @RequestMapping(value = "/followResume", method = RequestMethod.POST)
    @CheckParam("userId,resumeId")
    public ResultMessage followResume(@RequestParam(value = "userId") Integer userId,
                                      @RequestParam(value = "resumeId") Integer resumeId,
                                      @RequestParam(value = "source") String source) {
        try {
            Map<String, Object> result = new HashMap<>();
            /**验证用户信息*/
            CorpUser user = corpUserService.getById(userId.longValue());
            /**验证简历是否存在*/
            Boolean exists = redisUtil.hasKey(RedisKey_ResumeConstants.BASE_RESUME_INFO + resumeId);
            if (!exists) {
                logger.error(RedisKey_ResumeConstants.BASE_RESUME_INFO + resumeId + "\t\t\t简历不存在");
                return new ResultMessage(ResponseCode.DATA_NOT_FOUND.getCode(), "简历不存在", "");
            }
            //获取该简历联系方式
            Map<Object, Object> resumeInfo = redisUtil.hmget(RedisKey_ResumeConstants.BASE_RESUME_INFO + resumeId);
            result.put("mobile", resumeInfo.get("mobile") != null ? resumeInfo.get("mobile") : "");
            result.put("name", resumeInfo.get("name") != null ? resumeInfo.get("name") : "");
            /**验证简历是否已被关注*/
            if (redisUtil.sHasKey(RedisKey_ResumeConstants.BASE_RESUME_USER + userId + ":seenMobile", resumeId)) {
                //已下载过的简历,直接返回信息
                return new ResultMessage(ResponseCode.SUCCESS.getCode(), "操作成功", result);
            }
            /**判断简历来源
             * 1.如果为共享简历库(搜索),验证积分是否充足
             */
            if (source.trim().equals("search")) {
                /**判断积分是否充足(调用服务)*/
                Boolean reducePointtByResume = userFeign.isReducePointtByResume(userId.longValue());
                if(!reducePointtByResume) {
                    return new ResultMessage(ResponseCode.BUSINESS_DATA_ZERO.getCode(), "积分不足", "");
                }
            }
            /**添加到关注记录里(关注即为下载)*/
            redisUtil.sSet(RedisKey_ResumeConstants.BASE_RESUME_USER + userId + ":seenMobile", resumeId);
            /**下载后,添加到统计数据,供BI使用*/
            String countKey = RedisKey_ResumeConstants.BASE_RESUME_DOWNLOADCOUNT + DateConvertUtils.format(DateConvertUtils.getNow(), DateConvertUtils.allDate);
            String countField = resumeId.toString();
            boolean hasKey = redisUtil.hHasKey(countKey, countField);
            if (!hasKey) {
                redisUtil.hset(countKey, countField, 1);
                redisUtil.expire(countKey, 30 * 24 * 60 * 60); // 一个月有效期
            } else {
                redisUtil.hincr(countKey, countField, 1);
            }
            /**关注后添加到用户简历库(关注即为下载)*/
            long time = new Date().getTime();
            redisUtil.add(RedisKey_ResumeConstants.BASE_RESUME_USER + userId + ":repository", resumeId, time);
            /**增加简历关注(下载)量*/
            redisUtil.hincr(RedisKey_StatisticsConstants.STATISTICS_RESUME_DOWNLOADS, resumeId.toString(), 1);
            /**判断简历来源
             * 2.如果为共享简历库(搜索),扣除积分
             */
            if (source.trim().equals("search")) {
                /**消耗积分(调用服务)*/
                ResultEntity resultEntity = userFeign.reducePointbyResume(userId.longValue());
                if(!resultEntity.isSuccess()) {
                    return new ResultMessage(Integer.parseInt(resultEntity.getCode()), resultEntity.getMessage(), "");
                }
                logger.info("===成功扣除积分===");
            }
            /**给求职者(简历)发送短信*/
            logger.info(String.format("给简历id:%s 发送完善信息短信",resumeId));
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    YlwTianyanchaCorpBase corpInfoByEhrId = tycCorpService.getByEhrId(user.getCorpId());
                    if (resumeInfo.get("mobile") != null && RegexUtil.isMobile_11num(resumeInfo.get("mobile").toString())) {
                        ResultMessage resultMessage = codeFeign.sendSmsByCodeOrCustom("509",resumeInfo.get("mobile").toString(), null,Long.valueOf(userId), Long.valueOf(resumeId), null, null,null, null, corpInfoByEhrId != null ? corpInfoByEhrId.getName() : "", null,false);
                        logger.info("短信接口返回结果:" + JSONObject.fromObject(resultMessage));
                    }
                }
            });
            thread.start();
            return new ResultMessage(com.qh.util.base.ResponseCode.SUCCESS.getCode(), "操作成功", result);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return new ResultMessage(com.qh.util.base.ResponseCode.SYSTEM_INNER_ERROR.getCode(), "操作异常", "");
        }
    }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值