|
| 1 | +package org.linlinjava.litemall.wx.task; |
| 2 | + |
| 3 | +import org.apache.commons.logging.Log; |
| 4 | +import org.apache.commons.logging.LogFactory; |
| 5 | +import org.linlinjava.litemall.core.system.SystemConfig; |
| 6 | +import org.linlinjava.litemall.core.task.Task; |
| 7 | +import org.linlinjava.litemall.core.util.BeanUtil; |
| 8 | +import org.linlinjava.litemall.db.domain.LitemallOrder; |
| 9 | +import org.linlinjava.litemall.db.domain.LitemallOrderGoods; |
| 10 | +import org.linlinjava.litemall.db.service.LitemallGoodsProductService; |
| 11 | +import org.linlinjava.litemall.db.service.LitemallOrderGoodsService; |
| 12 | +import org.linlinjava.litemall.db.service.LitemallOrderService; |
| 13 | +import org.linlinjava.litemall.db.util.OrderUtil; |
| 14 | + |
| 15 | +import java.time.LocalDateTime; |
| 16 | +import java.util.List; |
| 17 | + |
| 18 | +public class OrderUnpaidTask extends Task { |
| 19 | + private final Log logger = LogFactory.getLog(OrderUnpaidTask.class); |
| 20 | + private int orderId = -1; |
| 21 | + |
| 22 | + public OrderUnpaidTask(Integer orderId){ |
| 23 | + super("OrderUnpaidTask-" + orderId, SystemConfig.getOrderUnpaid() * 60 * 1000); |
| 24 | + this.orderId = orderId; |
| 25 | + } |
| 26 | + |
| 27 | + @Override |
| 28 | + public void run() { |
| 29 | + logger.info("系统开始处理订单超时未付款订单 " + this.orderId); |
| 30 | + |
| 31 | + LitemallOrderService orderService = BeanUtil.getBean(LitemallOrderService.class); |
| 32 | + LitemallOrderGoodsService orderGoodsService = BeanUtil.getBean(LitemallOrderGoodsService.class); |
| 33 | + LitemallGoodsProductService productService = BeanUtil.getBean(LitemallGoodsProductService.class); |
| 34 | + |
| 35 | + LitemallOrder order = orderService.findById(this.orderId); |
| 36 | + if(order == null){ |
| 37 | + return; |
| 38 | + } |
| 39 | + if(!OrderUtil.isCreateStatus(order)){ |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + // 设置订单已取消状态 |
| 44 | + order.setOrderStatus(OrderUtil.STATUS_AUTO_CANCEL); |
| 45 | + order.setEndTime(LocalDateTime.now()); |
| 46 | + if (orderService.updateWithOptimisticLocker(order) == 0) { |
| 47 | + throw new RuntimeException("更新数据已失效"); |
| 48 | + } |
| 49 | + |
| 50 | + // 商品货品数量增加 |
| 51 | + Integer orderId = order.getId(); |
| 52 | + List<LitemallOrderGoods> orderGoodsList = orderGoodsService.queryByOid(orderId); |
| 53 | + for (LitemallOrderGoods orderGoods : orderGoodsList) { |
| 54 | + Integer productId = orderGoods.getProductId(); |
| 55 | + Short number = orderGoods.getNumber(); |
| 56 | + if (productService.addStock(productId, number) == 0) { |
| 57 | + throw new RuntimeException("商品货品库存增加失败"); |
| 58 | + } |
| 59 | + } |
| 60 | + logger.info("系统成功处理订单超时未付款订单 " + this.orderId); |
| 61 | + } |
| 62 | +} |
0 commit comments