|
|
@@ -42,6 +42,7 @@ import org.apache.rocketmq.spring.core.RocketMQTemplate;
|
|
|
import org.springframework.context.event.EventListener;
|
|
|
import org.springframework.core.env.Environment;
|
|
|
import org.springframework.data.domain.Page;
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.ui.Model;
|
|
|
@@ -58,37 +59,43 @@ import java.util.*;
|
|
|
@Slf4j
|
|
|
public class OrderService {
|
|
|
|
|
|
- private OrderRepo orderRepo;
|
|
|
- private CollectionRepo collectionRepo;
|
|
|
- private UserAddressRepo userAddressRepo;
|
|
|
- private UserRepo userRepo;
|
|
|
- private Environment env;
|
|
|
- private AlipayClient alipayClient;
|
|
|
- private AlipayProperties alipayProperties;
|
|
|
- private WxPayService wxPayService;
|
|
|
- private WxPayProperties wxPayProperties;
|
|
|
- private AssetService assetService;
|
|
|
- private SysConfigService sysConfigService;
|
|
|
- private BlindBoxItemRepo blindBoxItemRepo;
|
|
|
- private AssetRepo assetRepo;
|
|
|
- private UserCouponRepo userCouponRepo;
|
|
|
- private CollectionService collectionService;
|
|
|
- private CommissionRecordRepo commissionRecordRepo;
|
|
|
- private AdapayProperties adapayProperties;
|
|
|
- private GeneralProperties generalProperties;
|
|
|
- private RocketMQTemplate rocketMQTemplate;
|
|
|
+ private OrderRepo orderRepo;
|
|
|
+ private CollectionRepo collectionRepo;
|
|
|
+ private UserAddressRepo userAddressRepo;
|
|
|
+ private UserRepo userRepo;
|
|
|
+ private Environment env;
|
|
|
+ private AlipayClient alipayClient;
|
|
|
+ private AlipayProperties alipayProperties;
|
|
|
+ private WxPayService wxPayService;
|
|
|
+ private WxPayProperties wxPayProperties;
|
|
|
+ private AssetService assetService;
|
|
|
+ private SysConfigService sysConfigService;
|
|
|
+ private BlindBoxItemRepo blindBoxItemRepo;
|
|
|
+ private AssetRepo assetRepo;
|
|
|
+ private UserCouponRepo userCouponRepo;
|
|
|
+ private CollectionService collectionService;
|
|
|
+ private CommissionRecordRepo commissionRecordRepo;
|
|
|
+ private AdapayProperties adapayProperties;
|
|
|
+ private GeneralProperties generalProperties;
|
|
|
+ private RocketMQTemplate rocketMQTemplate;
|
|
|
+ private RedisTemplate<String, Object> redisTemplate;
|
|
|
+ private SnowflakeIdWorker snowflakeIdWorker;
|
|
|
|
|
|
public Page<Order> all(PageQuery pageQuery) {
|
|
|
return orderRepo.findAll(JpaUtils.toSpecification(pageQuery, Order.class), JpaUtils.toPageRequest(pageQuery));
|
|
|
}
|
|
|
|
|
|
- public Object mqCreate(Long userId, Long collectionId, int qty, Long addressId, Long userCouponId, Long invitor) {
|
|
|
- SendResult result = rocketMQTemplate.syncSend("create-order", new CreateOrderEvent(userId, collectionId, qty, addressId, userCouponId, invitor));
|
|
|
- return result.getMsgId();
|
|
|
+ public String mqCreate(Long userId, Long collectionId, int qty, Long addressId, Long userCouponId, Long invitor) {
|
|
|
+ Long id = snowflakeIdWorker.nextId();
|
|
|
+ SendResult result = rocketMQTemplate.syncSend(generalProperties.getCreateOrderTopic(),
|
|
|
+ new CreateOrderEvent(id, userId, collectionId, qty, addressId, userCouponId, invitor), 100000);
|
|
|
+ log.info("发送订单到队列: {}, result={}", id, result);
|
|
|
+ return String.valueOf(id);
|
|
|
}
|
|
|
|
|
|
@Transactional
|
|
|
- public Order create(Long userId, Long collectionId, int qty, Long addressId, Long userCouponId, Long invitor) {
|
|
|
+ public Order create(Long userId, Long collectionId, int qty, Long addressId, Long userCouponId, Long invitor, Long id) {
|
|
|
+ long t = System.currentTimeMillis();
|
|
|
qty = 1;
|
|
|
int stock = Optional.ofNullable(collectionService.decreaseStock(collectionId, qty))
|
|
|
.map(Math::toIntExact)
|
|
|
@@ -143,6 +150,7 @@ public class OrderService {
|
|
|
|
|
|
BigDecimal gasFee = sysConfigService.getBigDecimal("gas_fee");
|
|
|
Order order = Order.builder()
|
|
|
+ .id(Optional.ofNullable(id).orElse(snowflakeIdWorker.nextId()))
|
|
|
.userId(userId)
|
|
|
.collectionId(collectionId)
|
|
|
.name(collection.getName())
|
|
|
@@ -193,6 +201,8 @@ public class OrderService {
|
|
|
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);
|
|
|
@@ -211,7 +221,7 @@ public class OrderService {
|
|
|
JSONObject bizContent = new JSONObject();
|
|
|
bizContent.put("notifyUrl", alipayProperties.getNotifyUrl());
|
|
|
bizContent.put("returnUrl", alipayProperties.getReturnUrl());
|
|
|
- bizContent.put("out_trade_no", String.valueOf(new SnowflakeIdWorker(0, 0).nextId()));
|
|
|
+ bizContent.put("out_trade_no", String.valueOf(snowflakeIdWorker.nextId()));
|
|
|
bizContent.put("total_amount", order.getTotalPrice().stripTrailingZeros().toPlainString());
|
|
|
bizContent.put("disable_pay_channels", "pcredit,creditCard");
|
|
|
if (Arrays.stream(env.getActiveProfiles()).noneMatch(s -> s.equals("prod"))) {
|
|
|
@@ -295,7 +305,7 @@ public class OrderService {
|
|
|
}
|
|
|
|
|
|
Map<String, Object> paymentParams = new HashMap<>();
|
|
|
- paymentParams.put("order_no", String.valueOf(new SnowflakeIdWorker(0, 0).nextId()));
|
|
|
+ paymentParams.put("order_no", String.valueOf(snowflakeIdWorker.nextId()));
|
|
|
paymentParams.put("pay_amt", order.getTotalPrice().setScale(2, RoundingMode.HALF_UP).toPlainString());
|
|
|
paymentParams.put("app_id", adapayProperties.getAppId());
|
|
|
paymentParams.put("pay_channel", payChannel);
|
|
|
@@ -417,7 +427,6 @@ public class OrderService {
|
|
|
orderRepo.save(order);
|
|
|
assetService.createAsset(winItem, user, order.getId(), order.getPrice(), "出售",
|
|
|
winItem.getTotal() > 1 ? collectionService.getNextNumber(winItem.getCollectionId()) : null);
|
|
|
- addSales(winItem.getMinterId(), order.getQty());
|
|
|
} else {
|
|
|
if (collection.getSource() == CollectionSource.TRANSFER) {
|
|
|
Asset asset = assetRepo.findById(collection.getAssetId()).orElse(null);
|
|
|
@@ -428,11 +437,12 @@ public class OrderService {
|
|
|
assetService.createAsset(collection, user, order.getId(), order.getPrice(), "出售",
|
|
|
collection.getTotal() > 1 ? collectionService.getNextNumber(order.getCollectionId()) : null);
|
|
|
}
|
|
|
- addSales(collection.getMinterId(), order.getQty());
|
|
|
}
|
|
|
commission(order);
|
|
|
+ collectionService.increaseSale(order.getCollectionId(), order.getQty());
|
|
|
} else if (order.getStatus() == OrderStatus.CANCELLED) {
|
|
|
}
|
|
|
+
|
|
|
}
|
|
|
|
|
|
@EventListener
|
|
|
@@ -486,8 +496,7 @@ public class OrderService {
|
|
|
}
|
|
|
collectionRepo.setOnShelf(order.getCollectionId(), true);
|
|
|
}
|
|
|
- collectionService.decreaseStock(order.getCollectionId(), order.getQty());
|
|
|
- collectionService.increaseSale(order.getCollectionId(), order.getQty());
|
|
|
+ collectionService.increaseStock(order.getCollectionId(), order.getQty());
|
|
|
|
|
|
order.setStatus(OrderStatus.CANCELLED);
|
|
|
order.setCancelTime(LocalDateTime.now());
|
|
|
@@ -500,6 +509,8 @@ public class OrderService {
|
|
|
userCouponRepo.save(coupon);
|
|
|
});
|
|
|
}
|
|
|
+ rocketMQTemplate.syncSend(generalProperties.getUpdateStockTopic(), order.getCollectionId(), 10000);
|
|
|
+ log.info("取消订单{}", order.getId());
|
|
|
}
|
|
|
|
|
|
@Scheduled(fixedRate = 30000)
|
|
|
@@ -509,7 +520,7 @@ public class OrderService {
|
|
|
}
|
|
|
List<Order> orders = orderRepo.findByStatusAndCreatedAtBeforeAndDelFalse(OrderStatus.NOT_PAID,
|
|
|
LocalDateTime.now().minusSeconds(210));
|
|
|
- orders.forEach(o -> {
|
|
|
+ orders.parallelStream().forEach(o -> {
|
|
|
try {
|
|
|
cancel(o);
|
|
|
} catch (Exception ignored) {
|
|
|
@@ -520,12 +531,6 @@ public class OrderService {
|
|
|
public void refundCancelled(Order order) {
|
|
|
}
|
|
|
|
|
|
- public synchronized void addSales(Long userId, int num) {
|
|
|
- if (userId != null) {
|
|
|
- userRepo.increaseSales(userId, num);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
public void setNumber() {
|
|
|
for (Collection collection : collectionRepo.findAll()) {
|
|
|
if (collection.getSource() != CollectionSource.OFFICIAL) continue;
|
|
|
@@ -585,7 +590,11 @@ public class OrderService {
|
|
|
request.setTransactionId(order.getTransactionId());
|
|
|
request.setTotalFee(order.getTotalPrice().multiply(BigDecimal.valueOf(100)).intValue());
|
|
|
request.setRefundFee(order.getTotalPrice().multiply(BigDecimal.valueOf(100)).intValue());
|
|
|
- request.setOutRefundNo(String.valueOf(new SnowflakeIdWorker(0, 0).nextId()));
|
|
|
+ request.setOutRefundNo(String.valueOf(snowflakeIdWorker.nextId()));
|
|
|
wxPayService.refund(request);
|
|
|
}
|
|
|
+
|
|
|
+ public Object queryCreateOrder(String id) {
|
|
|
+ return redisTemplate.opsForValue().get("createOrder::" + id);
|
|
|
+ }
|
|
|
}
|