|
@@ -57,17 +57,28 @@ public class OrderService {
|
|
|
private SysConfigService sysConfigService;
|
|
private SysConfigService sysConfigService;
|
|
|
private BlindBoxItemRepo blindBoxItemRepo;
|
|
private BlindBoxItemRepo blindBoxItemRepo;
|
|
|
private AssetRepo assetRepo;
|
|
private AssetRepo assetRepo;
|
|
|
|
|
+ private UserCouponRepo userCouponRepo;
|
|
|
|
|
|
|
|
public Page<Order> all(PageQuery pageQuery) {
|
|
public Page<Order> all(PageQuery pageQuery) {
|
|
|
return orderRepo.findAll(JpaUtils.toSpecification(pageQuery, Order.class), JpaUtils.toPageRequest(pageQuery));
|
|
return orderRepo.findAll(JpaUtils.toSpecification(pageQuery, Order.class), JpaUtils.toPageRequest(pageQuery));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Transactional
|
|
@Transactional
|
|
|
- public Order create(Long userId, Long collectionId, int qty, Long addressId) {
|
|
|
|
|
|
|
+ public Order create(Long userId, Long collectionId, int qty, Long addressId, Long userCouponId) {
|
|
|
if (qty <= 0) throw new BusinessException("数量必须大于0");
|
|
if (qty <= 0) throw new BusinessException("数量必须大于0");
|
|
|
User user = userRepo.findByIdAndDelFalse(userId).orElseThrow(new BusinessException("用户不存在"));
|
|
User user = userRepo.findByIdAndDelFalse(userId).orElseThrow(new BusinessException("用户不存在"));
|
|
|
Collection collection = collectionRepo.findById(collectionId).orElseThrow(new BusinessException("藏品不存在"));
|
|
Collection collection = collectionRepo.findById(collectionId).orElseThrow(new BusinessException("藏品不存在"));
|
|
|
User minter = userRepo.findById(collection.getMinterId()).orElseThrow(new BusinessException("铸造者不存在"));
|
|
User minter = userRepo.findById(collection.getMinterId()).orElseThrow(new BusinessException("铸造者不存在"));
|
|
|
|
|
+ UserCoupon coupon = null;
|
|
|
|
|
+ if (userCouponId != null) {
|
|
|
|
|
+ coupon = userCouponRepo.findById(userCouponId).orElseThrow(new BusinessException("兑换券不存在"));
|
|
|
|
|
+ if (coupon.isUsed()) {
|
|
|
|
|
+ throw new BusinessException("该兑换券已使用");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (coupon.isLimited() && !coupon.getCollectionIds().contains(collectionId)) {
|
|
|
|
|
+ throw new BusinessException("该兑换券不可用");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
if (!collection.isOnShelf()) {
|
|
if (!collection.isOnShelf()) {
|
|
|
throw new BusinessException("藏品已下架");
|
|
throw new BusinessException("藏品已下架");
|
|
@@ -121,7 +132,17 @@ public class OrderService {
|
|
|
.orElse(null))
|
|
.orElse(null))
|
|
|
.status(OrderStatus.NOT_PAID)
|
|
.status(OrderStatus.NOT_PAID)
|
|
|
.assetId(collection.getAssetId())
|
|
.assetId(collection.getAssetId())
|
|
|
|
|
+ .couponId(userCouponId)
|
|
|
.build();
|
|
.build();
|
|
|
|
|
+ if (coupon != null) {
|
|
|
|
|
+ coupon.setUsed(true);
|
|
|
|
|
+ coupon.setUseTime(LocalDateTime.now());
|
|
|
|
|
+ if (coupon.isNeedGas()) {
|
|
|
|
|
+ order.setTotalPrice(order.getGasPrice());
|
|
|
|
|
+ } else {
|
|
|
|
|
+ order.setTotalPrice(BigDecimal.ZERO);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
if (collection.getSource() == CollectionSource.TRANSFER) {
|
|
if (collection.getSource() == CollectionSource.TRANSFER) {
|
|
|
Asset asset = assetRepo.findById(collection.getAssetId()).orElseThrow(new BusinessException("资产不存在"));
|
|
Asset asset = assetRepo.findById(collection.getAssetId()).orElseThrow(new BusinessException("资产不存在"));
|
|
@@ -130,7 +151,11 @@ public class OrderService {
|
|
|
collection.setOnShelf(false);
|
|
collection.setOnShelf(false);
|
|
|
collectionRepo.save(collection);
|
|
collectionRepo.save(collection);
|
|
|
}
|
|
}
|
|
|
- return orderRepo.save(order);
|
|
|
|
|
|
|
+ order = orderRepo.save(order);
|
|
|
|
|
+ if (order.getTotalPrice().equals(BigDecimal.ZERO)) {
|
|
|
|
|
+ notifyOrder(order.getId(), PayMethod.WEIXIN, null);
|
|
|
|
|
+ }
|
|
|
|
|
+ return order;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public void payOrderAlipay(Long id, Model model) {
|
|
public void payOrderAlipay(Long id, Model model) {
|
|
@@ -344,13 +369,21 @@ public class OrderService {
|
|
|
order.setStatus(OrderStatus.CANCELLED);
|
|
order.setStatus(OrderStatus.CANCELLED);
|
|
|
order.setCancelTime(LocalDateTime.now());
|
|
order.setCancelTime(LocalDateTime.now());
|
|
|
orderRepo.save(order);
|
|
orderRepo.save(order);
|
|
|
|
|
+
|
|
|
|
|
+ if (order.getCouponId() != null) {
|
|
|
|
|
+ userCouponRepo.findById(order.getCouponId()).ifPresent(coupon -> {
|
|
|
|
|
+ coupon.setUsed(false);
|
|
|
|
|
+ coupon.setUseTime(null);
|
|
|
|
|
+ userCouponRepo.save(coupon);
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Scheduled(fixedRate = 60000)
|
|
@Scheduled(fixedRate = 60000)
|
|
|
public void batchCancel() {
|
|
public void batchCancel() {
|
|
|
List<Order> orders = orderRepo.findByStatusAndCreatedAtBeforeAndDelFalse(OrderStatus.NOT_PAID,
|
|
List<Order> orders = orderRepo.findByStatusAndCreatedAtBeforeAndDelFalse(OrderStatus.NOT_PAID,
|
|
|
LocalDateTime.now().minusMinutes(5));
|
|
LocalDateTime.now().minusMinutes(5));
|
|
|
- orders.stream().forEach(this::cancel);
|
|
|
|
|
|
|
+ orders.forEach(this::cancel);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public void refundCancelled(Order order) {
|
|
public void refundCancelled(Order order) {
|