解决实际项目中优惠券到期后 自动过期问题

解决实际项目中优惠券到期后 自动过期问题


前言

对于实际项目中,当前时间超过了用户领取的优惠券的可使用时间,如何使得用户领取的优惠券自动过期的问题?
我们的思路是设置一个定时操作,异步执行一个方法,该方法是循环拿到优惠券,检查当前时间是否在使用时间范围内,若不在,则设置优惠券的状态属性为已过期。


提示:以下是本篇文章正文内容,下面案例可供参考

一、DateUtil类

代码如下(示例):

/**
     * 判断当前时间是否在[startTime, endTime]区间,注意时间格式要一致
     *
     * @param nowTime 当前时间
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return
     * @author jqlin
     */
    public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {
        if (nowTime.getTime() == startTime.getTime()
                || nowTime.getTime() == endTime.getTime()) {
            return true;
        }

        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);

        Calendar begin = Calendar.getInstance();
        begin.setTime(startTime);

        Calendar end = Calendar.getInstance();
        end.setTime(endTime);

        if (date.after(begin) && date.before(end)) {
            return true;
        } else {
            return false;
        }
    }

二、service层

代码如下(示例):

/**
     * 执行更新 用户已领取券超过有效期后设置状态为过期
     */
    void runCouponsUserStatusTimeOutToExpired();
    @Override
    @Async
    public void runCouponsUserStatusTimeOutToExpired() {

        log.info("runCouponsUserStatusTimeOutToExpired start.......time={}",System.currentTimeMillis());
        Long startTime01 = System.currentTimeMillis();
        //查找到所有用户已领取,但未使用的用户券列表
        List<CouponsUser> couponsUserList = findListByUseStatus(CouponsUserConstant.RECEIVE_STATE);
        log.info("runCouponsUserStatusTimeOutToExpired couponsUserList.size={}",couponsUserList.size());
        if (couponsUserList.size() > 0) {
            List<CouponsUser> needCouponsUserList = new ArrayList<>();
            Date currentDate = new Date();
            for (int i = 0; i < couponsUserList.size(); i++) {
                CouponsUser couponsUser = couponsUserList.get(i);
                Date effDate = couponsUser.getEffDate();
                Date expDate = couponsUser.getExpDate();
                boolean isEffective = DateUtil.isEffectiveDate(currentDate, effDate, expDate);
                //券已失效 则修改用户券的状态为已过期
                if (!isEffective) {  
                    couponsUser.setCancelTime(currentDate);
                    couponsUser.setUseStatus(CouponsUserConstant.EXPIRED_STATE);
                    needCouponsUserList.add(couponsUser); //需要更新状态的券
                }
            }

            if (needCouponsUserList.size() > 0) {
                log.info("runCouponsUserStatusTimeOutToExpired update needCouponsUserList.size={}",needCouponsUserList.size());
                saveOrUpdateBatch(needCouponsUserList);
            }

        }

        Long endTime02 = System.currentTimeMillis();
        log.info("runCouponsUserStatusTimeOutToExpired end second={},mills={}",(endTime02-startTime01)/1000,(endTime02-startTime01)%1000);

    }

三、task层

代码如下(示例):

@Configuration
@EnableScheduling
public class CouponsUserScheduleTask {

    @Autowired
    private CouponsUserService couponsUserService;

    private static Logger log = LoggerFactory.getLogger(CouponsUserScheduleTask.class);

    @Scheduled(cron = "0 0/1 * * * ?")//每3分 //crontab -e 动态代替应用命令
    public void handlerCouponsUserStatusTimeOutToExpired(){
        log.info("handlerCouponsUserStatusTimeOutToExpired start.......time={}",System.currentTimeMillis());
        Long startTime01 = System.currentTimeMillis();
        couponsUserService.runCouponsUserStatusTimeOutToExpired();
        Long endTime02 = System.currentTimeMillis();
        log.info("handlerCouponsUserStatusTimeOutToExpired end second={},mills={}",(endTime02-startTime01)/1000,(endTime02-startTime01)%1000);
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值