Skip to content

Commit fcdbb44

Browse files
committed
chore[litemall-admin, litemall-admin-api]: 管理后台的定时任务迁移到统一的job子包
1 parent f6cd92a commit fcdbb44

File tree

2 files changed

+158
-126
lines changed

2 files changed

+158
-126
lines changed
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
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+
}

litemall-admin-api/src/main/java/org/linlinjava/litemall/admin/web/AdminOrderController.java

Lines changed: 0 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -295,130 +295,4 @@ public Object reply(@LoginAdmin Integer adminId, @RequestBody String body) {
295295
return ResponseUtil.ok();
296296
}
297297

298-
/**
299-
* 自动取消订单
300-
* <p>
301-
* 定时检查订单未付款情况,如果超时半个小时则自动取消订单
302-
* 定时时间是每次相隔半个小时。
303-
* <p>
304-
* 注意,因为是相隔半小时检查,因此导致有订单是超时一个小时以后才设置取消状态。
305-
* TODO
306-
* 这里可以进一步地配合用户订单查询时订单未付款检查,如果订单超时半小时则取消。
307-
*/
308-
@Scheduled(fixedDelay = 30 * 60 * 1000)
309-
public void checkOrderUnpaid() {
310-
logger.info("系统开启任务检查订单是否已经超期自动取消订单");
311-
312-
List<LitemallOrder> orderList = orderService.queryUnpaid();
313-
for (LitemallOrder order : orderList) {
314-
LocalDateTime add = order.getAddTime();
315-
LocalDateTime now = LocalDateTime.now();
316-
LocalDateTime expired = add.plusMinutes(30);
317-
if (expired.isAfter(now)) {
318-
continue;
319-
}
320-
321-
// 开启事务管理
322-
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
323-
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
324-
TransactionStatus status = txManager.getTransaction(def);
325-
try {
326-
// 设置订单已取消状态
327-
order.setOrderStatus(OrderUtil.STATUS_AUTO_CANCEL);
328-
order.setEndTime(LocalDateTime.now());
329-
if (orderService.updateWithOptimisticLocker(order) == 0) {
330-
throw new Exception("更新数据已失效");
331-
}
332-
333-
// 商品货品数量增加
334-
Integer orderId = order.getId();
335-
List<LitemallOrderGoods> orderGoodsList = orderGoodsService.queryByOid(orderId);
336-
for (LitemallOrderGoods orderGoods : orderGoodsList) {
337-
Integer productId = orderGoods.getProductId();
338-
LitemallGoodsProduct product = productService.findById(productId);
339-
Short number = orderGoods.getNumber();
340-
if (productService.addStock(productId, number) == 0) {
341-
throw new Exception("商品货品库存增加失败");
342-
}
343-
}
344-
} catch (Exception ex) {
345-
txManager.rollback(status);
346-
logger.info("订单 ID=" + order.getId() + " 数据更新失败,放弃自动确认收货");
347-
return;
348-
}
349-
txManager.commit(status);
350-
logger.info("订单 ID=" + order.getId() + " 已经超期自动取消订单");
351-
}
352-
}
353-
354-
/**
355-
* 自动确认订单
356-
* <p>
357-
* 定时检查订单未确认情况,如果超时七天则自动确认订单
358-
* 定时时间是每天凌晨3点。
359-
* <p>
360-
* 注意,因为是相隔一天检查,因此导致有订单是超时八天以后才设置自动确认。
361-
* 这里可以进一步地配合用户订单查询时订单未确认检查,如果订单超时7天则自动确认。
362-
* 但是,这里可能不是非常必要。相比订单未付款检查中存在商品资源有限所以应该
363-
* 早点清理未付款情况,这里八天再确认是可以的。
364-
* <p>
365-
* TODO
366-
* 目前自动确认是基于管理后台管理员所设置的商品快递时间,见orderService.queryUnconfirm。
367-
* 那么在实际业务上有可能存在商品寄出以后商品因为一些原因快递最终没有到达,
368-
* 也就是商品快递失败而shipEndTime一直是空的情况,因此这里业务可能需要扩展,以防止订单一直
369-
* 处于发货状态。
370-
*/
371-
@Scheduled(cron = "0 0 3 * * ?")
372-
public void checkOrderUnconfirm() {
373-
logger.info("系统开启任务检查订单是否已经超期自动确认收货");
374-
375-
List<LitemallOrder> orderList = orderService.queryUnconfirm();
376-
for (LitemallOrder order : orderList) {
377-
LocalDateTime ship = order.getShipTime();
378-
LocalDateTime now = LocalDateTime.now();
379-
LocalDateTime expired = ship.plusDays(7);
380-
if (expired.isAfter(now)) {
381-
continue;
382-
}
383-
384-
// 设置订单已取消状态
385-
order.setOrderStatus(OrderUtil.STATUS_AUTO_CONFIRM);
386-
order.setConfirmTime(now);
387-
if (orderService.updateWithOptimisticLocker(order) == 0) {
388-
logger.info("订单 ID=" + order.getId() + " 数据已经更新,放弃自动确认收货");
389-
} else {
390-
logger.info("订单 ID=" + order.getId() + " 已经超期自动确认收货");
391-
}
392-
}
393-
}
394-
395-
/**
396-
* 可评价订单商品超期
397-
* <p>
398-
* 定时检查订单商品评价情况,如果确认商品超时七天则取消可评价状态
399-
* 定时时间是每天凌晨4点。
400-
*/
401-
@Scheduled(cron = "0 0 4 * * ?")
402-
public void checkOrderComment() {
403-
logger.info("系统开启任务检查订单是否已经超期未评价");
404-
405-
LocalDateTime now = LocalDateTime.now();
406-
List<LitemallOrder> orderList = orderService.queryComment();
407-
for (LitemallOrder order : orderList) {
408-
LocalDateTime confirm = order.getConfirmTime();
409-
LocalDateTime expired = confirm.plusDays(7);
410-
if (expired.isAfter(now)) {
411-
continue;
412-
}
413-
414-
order.setComments((short) 0);
415-
orderService.updateWithOptimisticLocker(order);
416-
417-
List<LitemallOrderGoods> orderGoodsList = orderGoodsService.queryByOid(order.getId());
418-
for (LitemallOrderGoods orderGoods : orderGoodsList) {
419-
orderGoods.setComment(-1);
420-
orderGoodsService.updateById(orderGoods);
421-
}
422-
}
423-
}
424298
}

0 commit comments

Comments
 (0)