解决实际项⽬中优惠券到期后⾃动过期问题
解决实际项⽬中优惠券到期后⾃动过期问题
前⾔
对于实际项⽬中,当前时间超过了⽤户领取的优惠券的可使⽤时间,如何使得⽤户领取的优惠券⾃动过期的问题?
我们的思路是设置⼀个定时操作,异步执⾏⼀个⽅法,该⽅法是循环拿到优惠券,检查当前时间是否在使⽤时间范围内,若不在,则设置优惠券的状态属性为已过期。
提⽰:以下是本篇⽂章正⽂内容,下⾯案例可供参考
⼀、DateUtil类
代码如下(⽰例):
/**
* 判断当前时间是否在[startTime, endTime]区间,注意时间格式要⼀致
moves like a jagger
*
* @param nowTime 当前时间
orientation翻译
* @param startTime 开始时间
* @param endTime 结束时间
* @return
afc* @author jqlin
*/
public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime){
Time()== Time()
|| Time()== Time()){
return true;
}
Calendar date = Instance();
date.tTime(nowTime);
Calendar begin = Instance();
begin.tTime(startTime);
Calendar end = Instance();
end.tTime(endTime);
if(date.after(begin)&& date.before(end)){
return true;
}el{
return fal;
}
}
monte⼆、rvice层
代码如下(⽰例):
/**
* 执⾏更新⽤户已领取券超过有效期后设置状态为过期
*/
void runCouponsUrStatusTimeOutToExpired();
@Override
@Async
public void runCouponsUrStatusTimeOutToExpired(){
log.info("time={}",System.currentTimeMillis());
chris hemsworth
Long startTime01 = System.currentTimeMillis();
//查找到所有⽤户已领取,但未使⽤的⽤户券列表
List<CouponsUr> couponsUrList =findListByUStatus(CouponsUrConstant.RECEIVE_STATE);
log.info("runCouponsUrStatusTimeOutToExpired couponsUrList.size={}",couponsUrList.size());
if(couponsUrList.size()>0){
List<CouponsUr> needCouponsUrList =new ArrayList<>();
Date currentDate =new Date();
for(int i =0; i < couponsUrList.size(); i++){
CouponsUr couponsUr = (i);偶然的意思
Date effDate = EffDate();
Date expDate = ExpDate();
boolean isEffective = DateUtil.isEffectiveDate(currentDate, effDate, expDate);
metlife//券已失效则修改⽤户券的状态为已过期
if(!isEffective){
couponsUr.tCancelTime(currentDate);
couponsUr.tUStatus(CouponsUrConstant.EXPIRED_STATE);
needCouponsUrList.add(couponsUr);//需要更新状态的券
}职场英语培训班
}
if(needCouponsUrList.size()>0){
log.info("runCouponsUrStatusTimeOutToExpired update needCouponsUrList.size={}",needCouponsUrList.size());
saveOrUpdateBatch(needCouponsUrList);
}
}
Long endTime02 = System.currentTimeMillis();
log.info("runCouponsUrStatusTimeOutToExpired end cond={},mills={}",(endTime02-startTime01)/1000,(endTime02-startTime01)%1000);
}
三、task层
代码如下(⽰例):
@Configuration
wilsonart
@EnableScheduling
public class CouponsUrScheduleTask {
@Autowired按摩培训
private CouponsUrService couponsUrService;
private static Logger log = Logger(CouponsUrScheduleTask.class);
@Scheduled(cron ="0 0/1 * * * ?")//每3分 //crontab -e 动态代替应⽤命令
public void handlerCouponsUrStatusTimeOutToExpired(){
log.info("time={}",System.currentTimeMillis());
Long startTime01 = System.currentTimeMillis();
couponsUrService.runCouponsUrStatusTimeOutToExpired();
Long endTime02 = System.currentTimeMillis();
log.info("handlerCouponsUrStatusTimeOutToExpired end cond={},mills={}",(endTime02-startTime01)/1000,(endTime02-startTime01)%1000); }
}