lambda根据字段类型分组,根据字段去重

import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.TreeSet;
import java.util.stream.Collectors;

/**
 * @author WU SHENG QIANG
 * @Date 2019/10/23 17:15
 * @Description
 */
@JobHandler(value = "CouponExpireJobHandler")
@Component
@Slf4j
public class CouponExpireJobHandler extends IJobHandler {

    @Autowired
    private CouponTvDao couponTvDao;

    @Override
    public ReturnT<String> execute(String s) throws Exception {
        log.info("/CouponExpireJobHandler [定时任务]判断优惠券是否过期,更改状态为已过期 s:{}", s);
        List<CouponGetPo> couponList = couponTvDao.couponExpire();
        if (couponList.size() > 0) {
            //时间类型是为1的根据CouponId去重
            Map<Integer, List<CouponGetPo>> map = couponList.stream().collect(Collectors.groupingBy(CouponGetPo::getTimelimitType));
            List<CouponGetPo> couponExpireList = null == map.get(2) ? new ArrayList<>() : map.get(2);
            List<CouponGetPo> couponTimeList = map.get(1);
            if (null != couponTimeList && couponTimeList.size() > 0) {
                couponExpireList.addAll(couponTimeList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() ->
                        new TreeSet<>(Comparator.comparing(o -> o.getCouponId()))), ArrayList::new)));
            }
            log.info("/CouponExpireJobHandler 优惠券过期总数量:{},时间范围去重后的数量:{},couponList:{}", couponList.size(), couponExpireList.size(), couponList);
            for (CouponGetPo coupon : couponExpireList) {
                if (1 == coupon.getTimelimitType()) {//时间范围的优惠券,结束时间都是一致的,所以根据优惠券id改变所有优惠券状态为已过期
                    try {
                        couponTvDao.updateCouponStatus(coupon.getCouponId());
                    } catch (Exception e) {
                        log.error("/CouponExpireJobHandler 更新所有过期(时间范围)优惠券状态失败了 CouponId:{} e:{}", coupon.getCouponId(), e.getMessage());
                    }
                } else {
                    coupon.setStatus(0);
                    try {
                        couponTvDao.updateCouponGet(coupon);
                    } catch (Exception e) {
                        log.error("/CouponExpireJobHandler 更新过期的(固定天数)优惠券失败了 领取表Id:{} e:{}", coupon.getId(), e.getMessage());
                    }
                }
            }
        }
        return SUCCESS;
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值