Search in sources :

Example 1 with IOrderGoodsService

use of com.wayn.common.core.service.shop.IOrderGoodsService in project waynboot-mall by wayn111.

the class OrderUnpaidTask method run.

@Override
public void run() {
    log.info("系统开始处理延时任务---订单超时未付款---{}", this.orderId);
    IOrderService orderService = SpringContextUtil.getBean(IOrderService.class);
    IOrderGoodsService orderGoodsService = SpringContextUtil.getBean(IOrderGoodsService.class);
    IGoodsProductService productService = SpringContextUtil.getBean(IGoodsProductService.class);
    RedisCache redisCache = SpringContextUtil.getBean(RedisCache.class);
    Set<Long> zSet = redisCache.getCacheZset("order_zset", 0, System.currentTimeMillis());
    if (CollectionUtils.isEmpty(zSet) || !zSet.contains(this.orderId)) {
        return;
    }
    for (Long orderId : zSet) {
        log.info("redis内未付款订单, 编号:{} begin", orderId);
        final Long num = redisCache.deleteZsetObject("order_zset", orderId);
        if (num == null || num <= 0) {
            break;
        }
        Order order = orderService.getOne(Wrappers.lambdaQuery(Order.class).eq(Order::getOrderStatus, OrderUtil.STATUS_CREATE).eq(Order::getId, orderId));
        if (Objects.isNull(order) || !OrderUtil.isCreateStatus(order)) {
            break;
        }
        // 设置订单为已取消状态
        order.setOrderStatus(OrderUtil.STATUS_AUTO_CANCEL);
        order.setOrderEndTime(LocalDateTime.now());
        if (!orderService.updateById(order)) {
            log.info("redis内未付款订单, 编号:{} 更新订单状态失败", orderId);
            throw new RuntimeException("更新订单状态失败");
        }
        // 商品货品数量增加
        List<OrderGoods> orderGoodsList = orderGoodsService.list(Wrappers.lambdaQuery(OrderGoods.class).eq(OrderGoods::getOrderId, orderId));
        for (OrderGoods orderGoods : orderGoodsList) {
            Long productId = orderGoods.getProductId();
            Integer number = orderGoods.getNumber();
            if (!productService.addStock(productId, number)) {
                log.info("redis内未付款订单, 编号:{} 商品货品库存增加失败", orderId);
                throw new RuntimeException("商品货品库存增加失败");
            }
        }
        log.info("redis内未付款订单, 编号:{} end", orderId);
    }
    log.info("系统结束处理延时任务---订单超时未付款---{}", this.orderId);
}
Also used : Order(com.wayn.common.core.domain.shop.Order) RedisCache(com.wayn.data.redis.manager.RedisCache) IOrderService(com.wayn.mobile.api.service.IOrderService) IGoodsProductService(com.wayn.common.core.service.shop.IGoodsProductService) IOrderGoodsService(com.wayn.common.core.service.shop.IOrderGoodsService) OrderGoods(com.wayn.common.core.domain.shop.OrderGoods)

Aggregations

Order (com.wayn.common.core.domain.shop.Order)1 OrderGoods (com.wayn.common.core.domain.shop.OrderGoods)1 IGoodsProductService (com.wayn.common.core.service.shop.IGoodsProductService)1 IOrderGoodsService (com.wayn.common.core.service.shop.IOrderGoodsService)1 RedisCache (com.wayn.data.redis.manager.RedisCache)1 IOrderService (com.wayn.mobile.api.service.IOrderService)1