|  |  | 
 |  |  | 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 cn.hutool.core.date.DateUtil; | 
 |  |  | import lombok.extern.slf4j.Slf4j; | 
 |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
 |  |  | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | 
 |  |  | import org.springframework.scheduling.annotation.Scheduled; | 
 |  |  | import org.springframework.stereotype.Component; | 
 |  |  |  | 
 |  |  | 
 |  |  |  **/ | 
 |  |  | @Slf4j | 
 |  |  | @Component | 
 |  |  | @ConditionalOnProperty(prefix = "system", name = "job", havingValue = "true") | 
 |  |  | public class OrderOvertimeJob { | 
 |  |  |  | 
 |  |  |     @Autowired | 
 |  |  | 
 |  |  |     @Autowired | 
 |  |  |     private MallGoodsSkuMapper mallGoodsSkuMapper; | 
 |  |  |  | 
 |  |  |     @Scheduled(cron = "0 0/5 * * * ? ") | 
 |  |  |     public void overtimeJob() { | 
 |  |  |         log.info("订单超时任务执行"); | 
 |  |  |         List<MallOrderInfo> orderList = orderInfoMapper.selectOrderInfoByStatus(OrderStatusEnum.WAIT_PAY.getValue()); | 
 |  |  |         if (CollUtil.isNotEmpty(orderList)) { | 
 |  |  |             for (MallOrderInfo orderInfo : orderList) { | 
 |  |  |                 long subTime = DateUtil.between(orderInfo.getOrderTime(), new Date(), DateUnit.MINUTE, false); | 
 |  |  |  | 
 |  |  |                 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) { | 
 |  |  |                             Long goodsId = mallOrderItem.getGoodsId(); | 
 |  |  |                             Long skuId = mallOrderItem.getSkuId(); | 
 |  |  |                             Integer cnt = mallOrderItem.getCnt(); | 
 |  |  |                             MallGoods mallGoods = mallGoodsMapper.selectById(goodsId); | 
 |  |  |                             mallGoods.setVolume(mallGoods.getVolume() - cnt); | 
 |  |  |                             mallGoods.setStock(mallGoods.getStock() + cnt); | 
 |  |  |                             mallGoodsMapper.updateById(mallGoods); | 
 |  |  |  | 
 |  |  |                             MallGoodsSku mallGoodsSku = mallGoodsSkuMapper.selectById(skuId); | 
 |  |  |                             mallGoodsSku.setSkuVolume(mallGoodsSku.getSkuVolume() - cnt); | 
 |  |  |                             mallGoodsSku.setStock(mallGoodsSku.getStock() + cnt); | 
 |  |  |                             mallGoodsSkuMapper.updateById(mallGoodsSku); | 
 |  |  |                         } | 
 |  |  |                     } | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |     } | 
 |  |  | //    @Scheduled(cron = "0 0/5 * * * ? ") | 
 |  |  | //    public void overtimeJob() { | 
 |  |  | //        log.info("订单超时任务执行"); | 
 |  |  | //        List<MallOrderInfo> orderList = orderInfoMapper.selectOrderInfoUpTime(OrderStatusEnum.WAIT_PAY.getValue()); | 
 |  |  | //        if (CollUtil.isNotEmpty(orderList)) { | 
 |  |  | //            for (MallOrderInfo orderInfo : orderList) { | 
 |  |  | //                //更新订单状态 | 
 |  |  | //                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() { |