|
| 1 | +package org.linlinjava.litemall.admin.job; |
| 2 | + |
| 3 | +import org.apache.commons.logging.Log; |
| 4 | +import org.apache.commons.logging.LogFactory; |
| 5 | +import org.linlinjava.litemall.db.domain.LitemallGoodsProduct; |
| 6 | +import org.linlinjava.litemall.db.domain.LitemallOrder; |
| 7 | +import org.linlinjava.litemall.db.domain.LitemallOrderGoods; |
| 8 | +import org.linlinjava.litemall.db.service.*; |
| 9 | +import org.linlinjava.litemall.db.util.OrderUtil; |
| 10 | +import org.springframework.beans.factory.annotation.Autowired; |
| 11 | +import org.springframework.scheduling.annotation.Scheduled; |
| 12 | +import org.springframework.stereotype.Component; |
| 13 | +import org.springframework.transaction.PlatformTransactionManager; |
| 14 | +import org.springframework.transaction.TransactionDefinition; |
| 15 | +import org.springframework.transaction.TransactionStatus; |
| 16 | +import org.springframework.transaction.support.DefaultTransactionDefinition; |
| 17 | + |
| 18 | +import java.time.LocalDateTime; |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +/** |
| 22 | + * 检测订单状态 |
| 23 | + */ |
| 24 | +@Component |
| 25 | +public class OrderJob { |
| 26 | + private final Log logger = LogFactory.getLog(OrderJob.class); |
| 27 | + |
| 28 | + @Autowired |
| 29 | + private PlatformTransactionManager txManager; |
| 30 | + |
| 31 | + @Autowired |
| 32 | + private LitemallOrderGoodsService orderGoodsService; |
| 33 | + @Autowired |
| 34 | + private LitemallOrderService orderService; |
| 35 | + @Autowired |
| 36 | + private LitemallGoodsProductService productService; |
| 37 | + |
| 38 | + /** |
| 39 | + * 自动取消订单 |
| 40 | + * <p> |
| 41 | + * 定时检查订单未付款情况,如果超时半个小时则自动取消订单 |
| 42 | + * 定时时间是每次相隔半个小时。 |
| 43 | + * <p> |
| 44 | + * 注意,因为是相隔半小时检查,因此导致有订单是超时一个小时以后才设置取消状态。 |
| 45 | + * TODO |
| 46 | + * 这里可以进一步地配合用户订单查询时订单未付款检查,如果订单超时半小时则取消。 |
| 47 | + */ |
| 48 | + @Scheduled(fixedDelay = 30 * 60 * 1000) |
| 49 | + public void checkOrderUnpaid() { |
| 50 | + logger.info("系统开启任务检查订单是否已经超期自动取消订单"); |
| 51 | + |
| 52 | + List<LitemallOrder> orderList = orderService.queryUnpaid(); |
| 53 | + for (LitemallOrder order : orderList) { |
| 54 | + LocalDateTime add = order.getAddTime(); |
| 55 | + LocalDateTime now = LocalDateTime.now(); |
| 56 | + LocalDateTime expired = add.plusMinutes(30); |
| 57 | + if (expired.isAfter(now)) { |
| 58 | + continue; |
| 59 | + } |
| 60 | + |
| 61 | + // 开启事务管理 |
| 62 | + DefaultTransactionDefinition def = new DefaultTransactionDefinition(); |
| 63 | + def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED); |
| 64 | + TransactionStatus status = txManager.getTransaction(def); |
| 65 | + try { |
| 66 | + // 设置订单已取消状态 |
| 67 | + order.setOrderStatus(OrderUtil.STATUS_AUTO_CANCEL); |
| 68 | + order.setEndTime(LocalDateTime.now()); |
| 69 | + if (orderService.updateWithOptimisticLocker(order) == 0) { |
| 70 | + throw new Exception("更新数据已失效"); |
| 71 | + } |
| 72 | + |
| 73 | + // 商品货品数量增加 |
| 74 | + Integer orderId = order.getId(); |
| 75 | + List<LitemallOrderGoods> orderGoodsList = orderGoodsService.queryByOid(orderId); |
| 76 | + for (LitemallOrderGoods orderGoods : orderGoodsList) { |
| 77 | + Integer productId = orderGoods.getProductId(); |
| 78 | + LitemallGoodsProduct product = productService.findById(productId); |
| 79 | + Short number = orderGoods.getNumber(); |
| 80 | + if (productService.addStock(productId, number) == 0) { |
| 81 | + throw new Exception("商品货品库存增加失败"); |
| 82 | + } |
| 83 | + } |
| 84 | + } catch (Exception ex) { |
| 85 | + txManager.rollback(status); |
| 86 | + logger.info("订单 ID=" + order.getId() + " 数据更新失败,放弃自动确认收货"); |
| 87 | + return; |
| 88 | + } |
| 89 | + txManager.commit(status); |
| 90 | + logger.info("订单 ID=" + order.getId() + " 已经超期自动取消订单"); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * 自动确认订单 |
| 96 | + * <p> |
| 97 | + * 定时检查订单未确认情况,如果超时七天则自动确认订单 |
| 98 | + * 定时时间是每天凌晨3点。 |
| 99 | + * <p> |
| 100 | + * 注意,因为是相隔一天检查,因此导致有订单是超时八天以后才设置自动确认。 |
| 101 | + * 这里可以进一步地配合用户订单查询时订单未确认检查,如果订单超时7天则自动确认。 |
| 102 | + * 但是,这里可能不是非常必要。相比订单未付款检查中存在商品资源有限所以应该 |
| 103 | + * 早点清理未付款情况,这里八天再确认是可以的。。 |
| 104 | + */ |
| 105 | + @Scheduled(cron = "0 0 3 * * ?") |
| 106 | + public void checkOrderUnconfirm() { |
| 107 | + logger.info("系统开启任务检查订单是否已经超期自动确认收货"); |
| 108 | + |
| 109 | + List<LitemallOrder> orderList = orderService.queryUnconfirm(); |
| 110 | + for (LitemallOrder order : orderList) { |
| 111 | + LocalDateTime ship = order.getShipTime(); |
| 112 | + LocalDateTime now = LocalDateTime.now(); |
| 113 | + LocalDateTime expired = ship.plusDays(7); |
| 114 | + if (expired.isAfter(now)) { |
| 115 | + continue; |
| 116 | + } |
| 117 | + |
| 118 | + // 设置订单已取消状态 |
| 119 | + order.setOrderStatus(OrderUtil.STATUS_AUTO_CONFIRM); |
| 120 | + order.setConfirmTime(now); |
| 121 | + if (orderService.updateWithOptimisticLocker(order) == 0) { |
| 122 | + logger.info("订单 ID=" + order.getId() + " 数据已经更新,放弃自动确认收货"); |
| 123 | + } else { |
| 124 | + logger.info("订单 ID=" + order.getId() + " 已经超期自动确认收货"); |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * 可评价订单商品超期 |
| 131 | + * <p> |
| 132 | + * 定时检查订单商品评价情况,如果确认商品超时七天则取消可评价状态 |
| 133 | + * 定时时间是每天凌晨4点。 |
| 134 | + */ |
| 135 | + @Scheduled(cron = "0 0 4 * * ?") |
| 136 | + public void checkOrderComment() { |
| 137 | + logger.info("系统开启任务检查订单是否已经超期未评价"); |
| 138 | + |
| 139 | + LocalDateTime now = LocalDateTime.now(); |
| 140 | + List<LitemallOrder> orderList = orderService.queryComment(); |
| 141 | + for (LitemallOrder order : orderList) { |
| 142 | + LocalDateTime confirm = order.getConfirmTime(); |
| 143 | + LocalDateTime expired = confirm.plusDays(7); |
| 144 | + if (expired.isAfter(now)) { |
| 145 | + continue; |
| 146 | + } |
| 147 | + |
| 148 | + order.setComments((short) 0); |
| 149 | + orderService.updateWithOptimisticLocker(order); |
| 150 | + |
| 151 | + List<LitemallOrderGoods> orderGoodsList = orderGoodsService.queryByOid(order.getId()); |
| 152 | + for (LitemallOrderGoods orderGoods : orderGoodsList) { |
| 153 | + orderGoods.setComment(-1); |
| 154 | + orderGoodsService.updateById(orderGoods); |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | +} |
0 commit comments