From 68819657fbc6c3c701620037135b6633b888a886 Mon Sep 17 00:00:00 2001
From: xiaoyong931011 <15274802129@163.com>
Date: Fri, 23 Sep 2022 14:29:15 +0800
Subject: [PATCH] 20220902

---
 src/main/java/cc/mrbird/febs/mall/quartz/OrderOvertimeJob.java |   62 ++++++++++++++++++++++++-------
 1 files changed, 48 insertions(+), 14 deletions(-)

diff --git a/src/main/java/cc/mrbird/febs/mall/quartz/OrderOvertimeJob.java b/src/main/java/cc/mrbird/febs/mall/quartz/OrderOvertimeJob.java
index 12beb38..5a3b9cc 100644
--- a/src/main/java/cc/mrbird/febs/mall/quartz/OrderOvertimeJob.java
+++ b/src/main/java/cc/mrbird/febs/mall/quartz/OrderOvertimeJob.java
@@ -1,9 +1,12 @@
 package cc.mrbird.febs.mall.quartz;
 
 import cc.mrbird.febs.common.enumerates.OrderStatusEnum;
+import cc.mrbird.febs.common.exception.FebsException;
+import cc.mrbird.febs.mall.entity.MallGoods;
+import cc.mrbird.febs.mall.entity.MallGoodsSku;
 import cc.mrbird.febs.mall.entity.MallOrderInfo;
-import cc.mrbird.febs.mall.mapper.MallMemberMapper;
-import cc.mrbird.febs.mall.mapper.MallOrderInfoMapper;
+import cc.mrbird.febs.mall.entity.MallOrderItem;
+import cc.mrbird.febs.mall.mapper.*;
 import cn.hutool.core.collection.CollUtil;
 import cn.hutool.core.date.DateUnit;
 import cn.hutool.core.date.DateUtil;
@@ -29,28 +32,59 @@
     @Autowired
     private MallMemberMapper mallMemberMapper;
 
+    @Autowired
+    private MallOrderItemMapper mallOrderItemMapper;
+
+    @Autowired
+    private MallGoodsMapper mallGoodsMapper;
+
+    @Autowired
+    private MallGoodsSkuMapper mallGoodsSkuMapper;
+
     @Scheduled(cron = "0 0/5 * * * ? ")
     public void overtimeJob() {
         log.info("订单超时任务执行");
-        List<MallOrderInfo> orderList = orderInfoMapper.selectOrderInfoByStatus(OrderStatusEnum.WAIT_PAY.getValue());
+        List<MallOrderInfo> orderList = orderInfoMapper.selectOrderInfoUpTime(OrderStatusEnum.WAIT_PAY.getValue());
         if (CollUtil.isNotEmpty(orderList)) {
             for (MallOrderInfo orderInfo : orderList) {
-                long subTime = DateUtil.between(orderInfo.getOrderTime(), new Date(), DateUnit.MINUTE, false);
+                //更新订单状态
+                orderInfo.setStatus(OrderStatusEnum.CANCEL.getValue());
+                orderInfo.setCancelType(MallOrderInfo.CANCEL_OVERTIME_NO_PAY);
+                orderInfoMapper.updateById(orderInfo);
 
-                if (subTime > 15) {
-                    orderInfo.setStatus(OrderStatusEnum.CANCEL.getValue());
-                    orderInfo.setCancelType(MallOrderInfo.CANCEL_OVERTIME_NO_PAY);
-                    orderInfoMapper.updateById(orderInfo);
+                List<MallOrderItem> mallOrderItemList = mallOrderItemMapper.selectListByOrderId(orderInfo.getId());
+                if(CollUtil.isNotEmpty(mallOrderItemList)){
+                    for(MallOrderItem mallOrderItem : mallOrderItemList){
+                        MallGoodsSku sku = mallGoodsSkuMapper.selectSkuInfoById(mallOrderItem.getSkuId());
+                        if (sku == null) {
+                            throw new FebsException("购买商品或sku不存在");
+                        }
+
+                        if (sku.getStock() < mallOrderItem.getCnt()) {
+                            throw new FebsException(sku.getSkuName() + "库存不足");
+                        }
+
+                        MallGoods mallGoods = mallGoodsMapper.selectById(sku.getGoodsId());
+                        Integer goodsResult = mallGoodsMapper.upDateStockAndVolumeByGoodsId(mallGoods.getId(), mallOrderItem.getCnt());
+                        if(1 != goodsResult){
+                            throw new FebsException(mallGoods.getGoodsName() + "库存不足");
+                        }
+
+                        Integer skuResult = mallGoodsSkuMapper.upDateStockAndVolumeBySkuId(sku.getId(),mallOrderItem.getCnt());
+                        if(1 != skuResult){
+                            throw new FebsException(sku.getSkuName() + "库存不足");
+                        }
+                    }
                 }
             }
         }
 
     }
 
-    @Scheduled(cron = "0/5 * * * * ? ")
-    public void wakeup() {
-        log.info("本地保持唤醒状态");
-        Long id = 5L;
-        mallMemberMapper.selectById(id);
-    }
+//    @Scheduled(cron = "0/5 * * * * ? ")
+//    public void wakeup() {
+//        log.info("本地保持唤醒状态");
+//        Long id = 5L;
+//        mallMemberMapper.selectById(id);
+//    }
 }

--
Gitblit v1.9.1