package com.sinata.rest.modular.mall.job;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.sinata.common.enums.mall.EnumMallOrderState;
|
import com.sinata.rest.core.util.DateUtils;
|
import com.sinata.rest.modular.mall.model.MallOrder;
|
import com.sinata.rest.modular.mall.model.MallOrderMain;
|
import com.sinata.rest.modular.mall.service.IMallGoodsService;
|
import com.sinata.rest.modular.mall.service.IMallOrderMainService;
|
import com.sinata.rest.modular.mall.service.IMallOrderService;
|
import com.sinata.rest.modular.member.model.MyUserCouponOrder;
|
import com.sinata.rest.modular.member.service.IMyCouponService;
|
import com.sinata.rest.modular.member.service.IMyUserCouponOrderService;
|
import com.sinata.rest.modular.system.service.ISystemSetService;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.scheduling.annotation.EnableScheduling;
|
import org.springframework.scheduling.annotation.Scheduled;
|
import org.springframework.stereotype.Component;
|
|
import java.util.*;
|
|
/**
|
* 定时任务-订单超时取消
|
*/
|
@Slf4j
|
@Component
|
@EnableScheduling
|
public class OrderOutTimeCancelWork {
|
|
@Autowired
|
ISystemSetService systemSetService;
|
|
@Autowired
|
IMallGoodsService goodsService;
|
@Autowired
|
IMallOrderService orderService;
|
@Autowired
|
IMallOrderMainService orderMainService;
|
@Autowired
|
IMyUserCouponOrderService couponOrderService;
|
@Autowired
|
IMyCouponService myCouponService;
|
|
/**
|
* 定时任务(30秒执行一次)
|
* 固定等待时间 @Scheduled(fixedDelay = 时间间隔(毫秒) )
|
* @Scheduled(fixedRate = 1000 * 60 * 30)
|
* Corn表达式 @Scheduled(cron = Corn表达式)
|
* @Scheduled(cron = "0 0/30 * * * *")
|
*/
|
@Scheduled(cron = "30 * * * * *")
|
public void run() {
|
try {
|
Date outTime = DateUtils.getForwardOrAfterMinutesDate(new Date(), -30);
|
log.debug("==> 下单后X分钟不支付订单,订单自动取消:{}", outTime);
|
|
// 超时订单未支付,自动取消
|
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<MallOrder>()
|
.eq(MallOrder::getState, EnumMallOrderState.WAIT_PAY.index)
|
.le(MallOrder::getCreateTime, outTime);
|
// 待取消订单列表
|
List<MallOrder> waitCancelOrderList = orderService.list(wrapper);
|
MallOrder order = new MallOrder();
|
order.setState(EnumMallOrderState.CANCEL.index);
|
order.setCancelType(2);
|
order.setCancelTime(new Date());
|
order.setCause("订单未支付超时取消");
|
boolean update = orderService.update(order, wrapper);
|
if (update) {
|
for (MallOrder oo : waitCancelOrderList) {
|
if (oo.getCouponId() != null && oo.getCouponId() != 0) {
|
// 退还优惠券
|
myCouponService.updateUseCoupon(oo.getUserId(), oo.getCouponId(), 0);
|
}
|
|
// 减少销量
|
Map<String, Integer> goodsBuyCountMap = new HashMap();
|
goodsBuyCountMap.put("goodsId", oo.getGoodsId());
|
goodsBuyCountMap.put("stock", -1 * oo.getNumber());
|
goodsService.addGoodsBuyCount(Arrays.asList(goodsBuyCountMap));
|
}
|
}
|
|
// 主订单-超时订单未支付,自动取消
|
LambdaQueryWrapper orderMainWrapper = new LambdaQueryWrapper<MallOrderMain>()
|
.eq(MallOrderMain::getState, EnumMallOrderState.WAIT_PAY.index)
|
.le(MallOrderMain::getCreateTime, outTime);
|
|
MallOrderMain orderMain = new MallOrderMain();
|
orderMain.setState(EnumMallOrderState.CANCEL.index);
|
orderMainService.update(orderMain, orderMainWrapper);
|
} catch (Exception e) {
|
e.printStackTrace();
|
log.error("操作异常", e.getMessage());
|
}
|
}
|
|
@Scheduled(fixedDelay = 1000 * 30)
|
public void couponOrderRun() {
|
try {
|
// 【订单设置】下单后X分钟不支付订单,订单自动取消
|
Date outTime = DateUtils.getForwardOrAfterMinutesDate(new Date(), -5);
|
log.debug("==> 下单后X分钟不支付订单,有价优惠券订单自动取消:{}", outTime);
|
// 超时订单未支付,自动取消
|
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<MyUserCouponOrder>()
|
.eq(MyUserCouponOrder::getPayStatus, 1)
|
.le(MyUserCouponOrder::getCreateTime, outTime);
|
MyUserCouponOrder order = new MyUserCouponOrder();
|
order.setPayStatus(3);
|
couponOrderService.update(order, wrapper);
|
} catch (Exception e) {
|
e.printStackTrace();
|
log.error("操作异常", e.getMessage());
|
}
|
}
|
|
}
|