|
|
@@ -23,6 +23,7 @@ import com.yami.shop.bean.model.*;
|
|
|
import com.yami.shop.bean.param.OrderDto;
|
|
|
import com.yami.shop.common.enums.PayType;
|
|
|
import com.yami.shop.common.exception.YamiShopBindException;
|
|
|
+import com.yami.shop.common.util.RedisUtil;
|
|
|
import com.yami.shop.dao.DeviceFeyMapper;
|
|
|
import com.yami.shop.delivery.comment.api.paotui.PaoTuiApi;
|
|
|
import com.yami.shop.delivery.comment.param.DeliveryOrderItemParam;
|
|
|
@@ -35,6 +36,7 @@ import org.apache.logging.log4j.core.util.JsonUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
|
import org.springframework.http.ResponseEntity;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
@@ -66,34 +68,50 @@ public class OrderTask {
|
|
|
private IHBOrderService ihbOrderService;
|
|
|
|
|
|
|
|
|
- public void cancelOrder(){
|
|
|
+ public void cancelOrder() {
|
|
|
Date now = new Date();
|
|
|
logger.info("取消超时未支付订单。。。");
|
|
|
- // 获取15分钟之前未支付的订单
|
|
|
- List<Order> orders = orderService.selectCancelOrders(OrderStatus.UNPAY.value(),DateUtil.offsetMinute(now, -15));
|
|
|
- if (CollectionUtil.isEmpty(orders)) {
|
|
|
- return;
|
|
|
- }
|
|
|
+ String lockKey = "cancel_timeout_orders_lock";
|
|
|
+ // 使用分布式锁或数据库行锁
|
|
|
+ try {
|
|
|
+ if (!RedisUtil.set(lockKey, "locked", 30)) {
|
|
|
+ logger.info("任务正在执行中,跳过本次执行");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // 获取15分钟之前未支付的订单
|
|
|
+ try {
|
|
|
+ List<Order> orders = orderService.selectCancelOrders(OrderStatus.UNPAY.value(), DateUtil.offsetMinute(now, -15));
|
|
|
+ if (CollectionUtil.isEmpty(orders)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
|
|
|
// List<Order> cancelOrderList = this.chackOrders(orders);
|
|
|
|
|
|
|
|
|
- //TODO zx定时任务加回积分待完善
|
|
|
- orderService.cancelOrders(orders,"未按时支付");
|
|
|
+ //TODO zx定时任务加回积分待完善
|
|
|
+ orderService.cancelOrders(orders, "未按时支付");
|
|
|
|
|
|
- // 移除缓存
|
|
|
- this.removeCache(orders);
|
|
|
+ // 移除缓存
|
|
|
+ this.removeCache(orders);
|
|
|
+ } finally {
|
|
|
+ // 释放锁
|
|
|
+ RedisUtil.del(lockKey);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ // 释放锁
|
|
|
+ RedisUtil.del(lockKey);
|
|
|
+ }
|
|
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 确认收货
|
|
|
*/
|
|
|
- public void confirmOrder(){
|
|
|
+ public void confirmOrder() {
|
|
|
Date now = new Date();
|
|
|
logger.info("系统自动确认收货订单。。。");
|
|
|
// 获取10天之前等待确认收货的订单
|
|
|
- List<Order> orders = orderService.listUnRefundOrderAndOrderItems(OrderStatus.RECEIVED.value(),DateUtil.offsetDay(now, -10));
|
|
|
+ List<Order> orders = orderService.listUnRefundOrderAndOrderItems(OrderStatus.RECEIVED.value(), DateUtil.offsetDay(now, -10));
|
|
|
if (CollectionUtil.isEmpty(orders)) {
|
|
|
return;
|
|
|
}
|
|
|
@@ -181,10 +199,11 @@ public class OrderTask {
|
|
|
|
|
|
/**
|
|
|
* 查询订单,去除已支付的订单
|
|
|
+ *
|
|
|
* @param orders
|
|
|
*/
|
|
|
- private List<Order> chackOrders(List<Order> orders){
|
|
|
- List<String> orderNumbers=orders.stream().map(Order::getOrderNumber).collect(Collectors.toList());
|
|
|
+ private List<Order> chackOrders(List<Order> orders) {
|
|
|
+ List<String> orderNumbers = orders.stream().map(Order::getOrderNumber).collect(Collectors.toList());
|
|
|
// 收集未支付的订单
|
|
|
List<Order> orderList = new ArrayList<>();
|
|
|
//获取订单对应的订单结算数据
|
|
|
@@ -194,17 +213,17 @@ public class OrderTask {
|
|
|
.collect(Collectors.toMap(OrderSettlement::getOrderNumber, orderSettlement -> orderSettlement));
|
|
|
for (Order order : orders) {
|
|
|
OrderSettlement orderSettlement = orderSettlementMap.get(order.getOrderNumber());
|
|
|
- if (Objects.isNull(orderSettlement) || Objects.isNull(orderSettlement.getPayType()) || Objects.isNull(orderSettlement.getPayNo())){
|
|
|
+ if (Objects.isNull(orderSettlement) || Objects.isNull(orderSettlement.getPayType()) || Objects.isNull(orderSettlement.getPayNo())) {
|
|
|
orderList.add(order);
|
|
|
continue;
|
|
|
}
|
|
|
PayInfoBo payInfo = payManagerService.getPayInfo(PayType.instance(orderSettlement.getPayType()), orderSettlement.getPayNo());
|
|
|
- if(payInfo.getIsPaySuccess()){
|
|
|
+ if (payInfo.getIsPaySuccess()) {
|
|
|
// 根据内部订单号更新order settlement
|
|
|
- List<Order> successOrderList = payService.paySuccess(payInfo.getPayNo(), payInfo.getBizPayNo(),order.getPayType());
|
|
|
+ List<Order> successOrderList = payService.paySuccess(payInfo.getPayNo(), payInfo.getBizPayNo(), order.getPayType());
|
|
|
// 移除缓存
|
|
|
this.removeCache(successOrderList);
|
|
|
- }else {
|
|
|
+ } else {
|
|
|
orderList.add(order);
|
|
|
}
|
|
|
}
|
|
|
@@ -214,7 +233,7 @@ public class OrderTask {
|
|
|
/**
|
|
|
* 移除缓存
|
|
|
*/
|
|
|
- private void removeCache(List<Order> orders){
|
|
|
+ private void removeCache(List<Order> orders) {
|
|
|
for (Order order : orders) {
|
|
|
List<OrderItem> orderItems = order.getOrderItems();
|
|
|
for (OrderItem orderItem : orderItems) {
|