|
|
@@ -858,4 +858,114 @@ public class OrderService {
|
|
|
public void releaseOrderLock(Long orderId) {
|
|
|
redisTemplate.delete(RedisKeys.ORDER_LOCK + orderId);
|
|
|
}
|
|
|
+
|
|
|
+ public Order create1(Long userId, Long collectionId, Long userCouponId, Long invitor, Long id) {
|
|
|
+ long t = System.currentTimeMillis();
|
|
|
+ int qty = 1;
|
|
|
+ int stock = Optional.ofNullable(collectionService.decreaseStock(collectionId, qty))
|
|
|
+ .map(Math::toIntExact)
|
|
|
+ .orElseThrow(new BusinessException("很遗憾,藏品已售罄", ErrorCode.SOLD_OUT));
|
|
|
+
|
|
|
+
|
|
|
+ // 创建订单出错后需要回滚库存,所以需要try-catch
|
|
|
+ try {
|
|
|
+ if (stock < 0) {
|
|
|
+ throw new BusinessException("很遗憾,藏品已售罄", ErrorCode.SOLD_OUT);
|
|
|
+ }
|
|
|
+ Collection collection = collectionRepo.findById(collectionId).orElseThrow(new BusinessException("藏品不存在"));
|
|
|
+ if (collection.getAssetId() != null && collection.getAssetId().equals(778359L)) {
|
|
|
+ throw new BusinessException("很遗憾,藏品已售罄", ErrorCode.SOLD_OUT);
|
|
|
+ }
|
|
|
+ if (collection.getAssetId() != null) {
|
|
|
+ Asset asset = assetRepo.findById(collection.getAssetId()).orElseThrow(new BusinessException("藏品不存在"));
|
|
|
+ if (asset.getStatus() != AssetStatus.NORMAL) {
|
|
|
+ throw new BusinessException("藏品已下架");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ User minter = userRepo.findById(collection.getMinterId()).orElseThrow(new BusinessException("铸造者不存在"));
|
|
|
+
|
|
|
+ if (collection.isScheduleSale()) {
|
|
|
+ if (collection.getStartTime().isAfter(LocalDateTime.now())) {
|
|
|
+ throw new BusinessException("当前还未开售");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (!collection.isOnShelf()) {
|
|
|
+ if (!collection.isScanCode()) {
|
|
|
+ throw new BusinessException("藏品已下架");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!collection.isSalable()) {
|
|
|
+ throw new BusinessException("该藏品当前不可购买");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (collection.getMaxCount() > 0) {
|
|
|
+ int count;
|
|
|
+ if (StringUtils.isNotBlank(collection.getCountId())) {
|
|
|
+ count = orderRepo.countByUserIdAndCountIdAndStatusIn(userId, collection.getCountId(), Arrays.asList(OrderStatus.FINISH, OrderStatus.NOT_PAID, OrderStatus.PROCESSING));
|
|
|
+ } else {
|
|
|
+ count = orderRepo.countByUserIdAndCollectionIdAndStatusIn(userId, collectionId, Arrays.asList(OrderStatus.FINISH, OrderStatus.NOT_PAID, OrderStatus.PROCESSING));
|
|
|
+ }
|
|
|
+ if (count >= collection.getMaxCount()) {
|
|
|
+ throw new BusinessException("限购" + collection.getMaxCount() + "件");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (StringUtils.isNotBlank(collection.getName())) {
|
|
|
+ User user = userRepo.findById(userId).orElseThrow(new BusinessException("用户不存在"));
|
|
|
+ if (!user.getInviteCode().equals(collection.getName())) {
|
|
|
+ throw new BusinessException("非活动用户");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ Order order = Order.builder()
|
|
|
+ .id(Optional.ofNullable(id).orElse(snowflakeIdWorker.nextId()))
|
|
|
+ .userId(userId)
|
|
|
+ .collectionId(collectionId)
|
|
|
+ .name(collection.getName())
|
|
|
+ .pic(collection.getPic())
|
|
|
+ .detail(collection.getDetail())
|
|
|
+ .properties(collection.getProperties())
|
|
|
+ .category(collection.getCategory())
|
|
|
+ .canResale(collection.isCanResale())
|
|
|
+ .royalties(collection.getRoyalties())
|
|
|
+ .serviceCharge(collection.getServiceCharge())
|
|
|
+ .type(collection.getType())
|
|
|
+ .source(collection.getSource())
|
|
|
+ .minterId(collection.getMinterId())
|
|
|
+ .minter(minter.getNickname())
|
|
|
+ .minterAvatar(minter.getAvatar())
|
|
|
+ .qty(qty)
|
|
|
+ .price(collection.getPrice())
|
|
|
+ .gasPrice(BigDecimal.ZERO)
|
|
|
+ .totalPrice(BigDecimal.ZERO)
|
|
|
+ .status(OrderStatus.NOT_PAID)
|
|
|
+ .assetId(collection.getAssetId())
|
|
|
+ .couponId(userCouponId)
|
|
|
+ .invitor(invitor)
|
|
|
+ .countId(collection.getCountId())
|
|
|
+ .vip(false)
|
|
|
+ .vipPoint(0)
|
|
|
+ .build();
|
|
|
+
|
|
|
+ if (collection.getSource() == CollectionSource.TRANSFER) {
|
|
|
+ Asset asset = assetRepo.findById(collection.getAssetId()).orElseThrow(new BusinessException("资产不存在"));
|
|
|
+ asset.setStatus(AssetStatus.TRADING);
|
|
|
+ assetRepo.save(asset);
|
|
|
+ collectionRepo.setOnShelf(collectionId, false);
|
|
|
+ }
|
|
|
+ order = orderRepo.save(order);
|
|
|
+ if (order.getTotalPrice().equals(BigDecimal.ZERO)) {
|
|
|
+ notifyOrder(order.getId(), PayMethod.WEIXIN, null);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ rocketMQTemplate.syncSend(generalProperties.getUpdateStockTopic(), collectionId, 10000);
|
|
|
+ log.info("订单创建完成, id={}, {}ms", order.getId(), System.currentTimeMillis() - t);
|
|
|
+ return order;
|
|
|
+ } catch (Exception e) {
|
|
|
+ collectionService.increaseStock(collectionId, qty);
|
|
|
+ throw e;
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|