|
|
@@ -1,10 +1,18 @@
|
|
|
package com.izouma.nineth.service;
|
|
|
|
|
|
+import com.izouma.nineth.annotations.RedisLock;
|
|
|
import com.izouma.nineth.config.GeneralProperties;
|
|
|
+import com.izouma.nineth.domain.GiftOrder;
|
|
|
+import com.izouma.nineth.domain.MintOrder;
|
|
|
import com.izouma.nineth.domain.Order;
|
|
|
import com.izouma.nineth.domain.SysConfig;
|
|
|
+import com.izouma.nineth.dto.PayQuery;
|
|
|
+import com.izouma.nineth.enums.MintOrderStatus;
|
|
|
import com.izouma.nineth.enums.OrderStatus;
|
|
|
+import com.izouma.nineth.enums.PayStatus;
|
|
|
import com.izouma.nineth.exception.BusinessException;
|
|
|
+import com.izouma.nineth.repo.GiftOrderRepo;
|
|
|
+import com.izouma.nineth.repo.MintOrderRepo;
|
|
|
import com.izouma.nineth.repo.OrderRepo;
|
|
|
import com.izouma.nineth.repo.SysConfigRepo;
|
|
|
import lombok.AllArgsConstructor;
|
|
|
@@ -15,7 +23,9 @@ import org.springframework.stereotype.Service;
|
|
|
|
|
|
import javax.annotation.PostConstruct;
|
|
|
import java.time.LocalDateTime;
|
|
|
+import java.util.Arrays;
|
|
|
import java.util.List;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
@Service
|
|
|
@ConditionalOnProperty(value = "general.notify-server", havingValue = "true")
|
|
|
@@ -26,27 +36,34 @@ public class OrderCancelService {
|
|
|
private final OrderRepo orderRepo;
|
|
|
private final OrderService orderService;
|
|
|
private final SysConfigRepo sysConfigRepo;
|
|
|
+ private final MintOrderRepo mintOrderRepo;
|
|
|
+ private final MintOrderService mintOrderService;
|
|
|
+ private final GiftOrderRepo giftOrderRepo;
|
|
|
+ private final GiftOrderService giftOrderService;
|
|
|
+ private final OrderPayService orderPayService;
|
|
|
|
|
|
private static int orderCancelInterval = 210;
|
|
|
|
|
|
public static void setOrderCancelInterval(int orderCancelInterval) {
|
|
|
+ log.info("设置订单取消时间间隔为 {}S", orderCancelInterval);
|
|
|
OrderCancelService.orderCancelInterval = orderCancelInterval;
|
|
|
}
|
|
|
|
|
|
@PostConstruct
|
|
|
public void init() {
|
|
|
- orderCancelInterval = sysConfigRepo.findByName("order_cancel_interval")
|
|
|
+ orderCancelInterval = sysConfigRepo.findByName("order_cancel_time")
|
|
|
.map(SysConfig::getValue).map(Integer::parseInt).orElse(210);
|
|
|
}
|
|
|
|
|
|
- @Scheduled(fixedRate = 30000)
|
|
|
+ @Scheduled(fixedRate = 30000, initialDelay = 10000)
|
|
|
+ @RedisLock(value = "order_batch_cancel_lock", expire = 3, unit = TimeUnit.MINUTES)
|
|
|
public void batchCancel() {
|
|
|
List<Order> orders = orderRepo.findByStatusAndCreatedAtBeforeAndDelFalse(OrderStatus.NOT_PAID,
|
|
|
LocalDateTime.now().minusSeconds(orderCancelInterval));
|
|
|
orders.parallelStream().forEach(o -> {
|
|
|
try {
|
|
|
Order order = orderRepo.findById(o.getId()).orElseThrow(new BusinessException("订单不存在"));
|
|
|
- if (order.getStatus() == OrderStatus.NOT_PAID) {
|
|
|
+ if (order.getStatus() == OrderStatus.NOT_PAID && canCancel(order.getId().toString())) {
|
|
|
orderService.cancel(order);
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
@@ -54,4 +71,58 @@ public class OrderCancelService {
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
+
|
|
|
+ @Scheduled(fixedRate = 30000, initialDelay = 20000)
|
|
|
+ @RedisLock(value = "gift_order_batch_cancel_lock", expire = 3, unit = TimeUnit.MINUTES)
|
|
|
+ public void batchCancelMintOrder() {
|
|
|
+ List<MintOrder> orders = mintOrderRepo.findByStatusAndCreatedAtBeforeAndDelFalse(MintOrderStatus.NOT_PAID,
|
|
|
+ LocalDateTime.now().minusSeconds(orderCancelInterval));
|
|
|
+ orders.forEach(o -> {
|
|
|
+ try {
|
|
|
+ MintOrder order = mintOrderRepo.findById(o.getId()).orElseThrow(new BusinessException("订单不存在"));
|
|
|
+ if (order.getStatus() == MintOrderStatus.NOT_PAID && canCancel(order.getId().toString())) {
|
|
|
+ mintOrderService.cancel(order, false);
|
|
|
+ }
|
|
|
+ } catch (Exception ignored) {
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ @Scheduled(fixedRate = 30000, initialDelay = 30000)
|
|
|
+ public void batchCancelGiftOrder() {
|
|
|
+ List<GiftOrder> orders = giftOrderRepo.findByStatusAndCreatedAtBeforeAndDelFalse(OrderStatus.NOT_PAID,
|
|
|
+ LocalDateTime.now().minusSeconds(orderCancelInterval));
|
|
|
+ orders.forEach(o -> {
|
|
|
+ try {
|
|
|
+ GiftOrder order = giftOrderRepo.findById(o.getId()).orElseThrow(new BusinessException("订单不存在"));
|
|
|
+ if (order.getStatus() == OrderStatus.NOT_PAID && canCancel(order.getId().toString())) {
|
|
|
+ giftOrderService.cancel(order);
|
|
|
+ }
|
|
|
+ } catch (Exception ignored) {
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean canCancel(String id) {
|
|
|
+ PayQuery query = new PayQuery();
|
|
|
+ try {
|
|
|
+ query = orderPayService.query(id);
|
|
|
+ } catch (Exception e) {
|
|
|
+ query.setExist(false);
|
|
|
+ }
|
|
|
+ if (query.isExist()) {
|
|
|
+ if (query.getStatus() != null) {
|
|
|
+ switch (query.getStatus()) {
|
|
|
+ case SUCCESS:
|
|
|
+ case PENDING:
|
|
|
+ log.info("订单 {}, 状态 {}, 不能取消", id, query.getStatus().name());
|
|
|
+ return false;
|
|
|
+ default:
|
|
|
+ log.info("订单 {}, 状态 {}, 可以取消", id, query.getStatus().name());
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
}
|