puzhibing
2023-10-08 22199bbdda579861736420fe26c2873ab0f5d21c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
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());
        }
    }
 
}