优惠券项目---------------第十四章

16 篇文章 0 订阅

-------------------14-1-----14-2----14-3---------------------------------------------------------

redis安装。

------------------------14-4----------------------

zadd:注意是分数放在前面,值放在后面

zrange:索引分为

zrangebyscore:指定分数范围

zrem:移除值

zrank:值的排名

------------------------14-5----------------------

如何引用redis:

第一步:配置yml文件

第二步:配置config序列化

第三步:些测试方法

------------------------14-6----------------------

公告栏:

------------------------14-7----------------------

数据展示:

数据维护:

  public List<String> queryCouponList(){
        Set<String> couponSet = redisTemplate.opsForZSet().reverseRange(COUPON,0,-1);
        //获取set里面前N条数据
        return couponSet.stream().limit(COUPON_NUM).collect(Collectors.toList());
    }

------------------------14-8----------------------

/**
     * 接收coupon优惠券核销mq的时候被调用,以时间窗口展示前N条数据,userCouponStr代表userId_couponId
     * 拿掉时间最早的
     */
    public void updateCoupon(String userCouponStr){
        redisTemplate.opsForZSet().add(COUPON,userCouponStr,System.currentTimeMillis());
        Set<String> couponSet = redisTemplate.opsForZSet().range(COUPON,0,-1);
        if(couponSet.size()>COUPON_NUM){
            String remUserCouponStr = couponSet.stream().findFirst().get();
            redisTemplate.opsForZSet().remove(COUPON,remUserCouponStr);
        }
    }

------------------------14-9------------------------

dubbo接口。

   /**
     * 查询coupon公告栏,前10条数据  userId_couponId  ==>  1_1==> string[]  string[0] =1,string[1] =1
     */
    @Override
    public List<CouponNoticeDto> queryCouponNotice(){
        Set<String> couponSet = redisTemplate.opsForZSet().reverseRange(COUPON,0,-1);
        //获取set里面前N条数据
        List<String> userCouponStrs = couponSet.stream().limit(COUPON_NUM).collect(Collectors.toList());
        Map<String,String> couponUserMap =userCouponStrs.stream().collect(Collectors.toMap(o -> o.split("_")[1],o -> o.split("_")[0]));
        List<String> couponIds = userCouponStrs.stream().map(s -> s.split("_")[1]).collect(Collectors.toList());
        //[1,2] ==>1,2   StringUtils.join
        String couponIdStrs = StringUtils.join(couponIds,",");
        //通过couponIdStrs批量获取coupon缓存数据
        List<TCoupon> tCoupons = getCouponListByIds(couponIdStrs);
        List<CouponNoticeDto> dtos = tCoupons.stream().map(tCoupon -> {
            CouponNoticeDto dto = new CouponNoticeDto();
            BeanUtils.copyProperties(tCoupon,dto);
            dto.setUserId(Integer.parseInt(couponUserMap.get(dto.getId()+"")));
            return dto;
        }).collect(Collectors.toList());
        return dtos;
    }

------------------------14-10----------------------

debug:

   @Test
    public void queryCouponNotice() {
        String ret = JSON.toJSONString(couponService.queryCouponNotice());
        System.err.println(ret);
    }   //本地maven ==》 nexus
private List<News> queryNotice() {
        List<CouponNoticeDto> dtos = iCouponService.queryCouponNotice();
        List<News> newsList = dtos.stream().map(dto -> {
            News news = new News();
            int userId = dto.getUserId();
            int reduce = dto.getReduceAmount();
            String title = dto.getTitle();
            String title1 = "恭喜" + userId + "使用" + title + "优惠券,获得减免金额" + reduce;
            news.setTitle(title1);
            news.setCreateTime(new Date());
            return news;
        }).collect(Collectors.toList());
        return newsList;
    }

------------------------14-11----------------------

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值