|
|
@@ -7,7 +7,6 @@ import com.alipay.api.AlipayClient;
|
|
|
import com.alipay.api.request.AlipayTradeWapPayRequest;
|
|
|
import com.github.binarywang.wxpay.bean.order.WxPayMpOrderResult;
|
|
|
import com.github.binarywang.wxpay.bean.order.WxPayMwebOrderResult;
|
|
|
-import com.github.binarywang.wxpay.bean.request.WxPayRefundRequest;
|
|
|
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
|
|
|
import com.github.binarywang.wxpay.constant.WxPayConstants;
|
|
|
import com.github.binarywang.wxpay.exception.WxPayException;
|
|
|
@@ -57,6 +56,7 @@ import org.springframework.ui.Model;
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
import java.math.RoundingMode;
|
|
|
+import java.time.Duration;
|
|
|
import java.time.LocalDateTime;
|
|
|
import java.time.format.DateTimeFormatter;
|
|
|
import java.time.temporal.ChronoUnit;
|
|
|
@@ -110,10 +110,11 @@ public class OrderService {
|
|
|
throw new BusinessException("签名已过期");
|
|
|
}
|
|
|
|
|
|
- Integer stock = collectionService.getStock(collectionId);
|
|
|
- if (stock == null || stock <= 0) {
|
|
|
- throw new BusinessException("藏品已售罄", ErrorCode.SOLD_OUT);
|
|
|
- }
|
|
|
+// Integer stock = collectionService.getStock(collectionId);
|
|
|
+// if (stock == null || stock <= 0) {
|
|
|
+// throw new BusinessException("藏品已售罄", ErrorCode.SOLD_OUT);
|
|
|
+// }
|
|
|
+ limitReq(collectionId);
|
|
|
|
|
|
Long id = snowflakeIdWorker.nextId();
|
|
|
SendResult result = rocketMQTemplate.syncSend(generalProperties.getCreateOrderTopic(),
|
|
|
@@ -123,6 +124,15 @@ public class OrderService {
|
|
|
return String.valueOf(id);
|
|
|
}
|
|
|
|
|
|
+ public void limitReq(Long collectionId) {
|
|
|
+ BoundValueOperations<String, Object> ops = redisTemplate.boundValueOps(RedisKeys.LIMIT_REQ + collectionId);
|
|
|
+ ops.setIfAbsent(3000, Duration.ofSeconds(30));
|
|
|
+ Long val = ops.decrement();
|
|
|
+ if (val == null || val < 0) {
|
|
|
+ throw new BusinessException("很遗憾,藏品已售罄");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
public Order create(Long userId, Long collectionId, int qty, Long addressId, Long userCouponId, Long invitor,
|
|
|
Long id, boolean vip) {
|
|
|
long t = System.currentTimeMillis();
|
|
|
@@ -208,11 +218,6 @@ public class OrderService {
|
|
|
collectionService.decreaseQuota(collectionId, 1);
|
|
|
}
|
|
|
} else {
|
|
|
-// long count = userRepo.countAllByCollectionIdAndCollectionInvitor(collectionId, userId);
|
|
|
-// int sub = collection.getAssignment() - (int) count;
|
|
|
-// if (sub > 0) {
|
|
|
-// throw new BusinessException("再拉新" + sub + "人即可购买");
|
|
|
-// }
|
|
|
if (user.getVipPoint() < 1) {
|
|
|
throw new BusinessException("没有购买名额");
|
|
|
}
|
|
|
@@ -284,7 +289,7 @@ public class OrderService {
|
|
|
|
|
|
if (usePoint > 0) {
|
|
|
// 扣除积分
|
|
|
- userRepo.updateVipPoint(userId, -usePoint);
|
|
|
+ userRepo.addVipPoint(userId, -usePoint);
|
|
|
}
|
|
|
rocketMQTemplate.syncSend(generalProperties.getUpdateStockTopic(), collectionId, 10000);
|
|
|
log.info("订单创建完成, id={}, {}ms", order.getId(), System.currentTimeMillis() - t);
|
|
|
@@ -293,7 +298,7 @@ public class OrderService {
|
|
|
collectionService.increaseStock(collectionId, qty);
|
|
|
if (usePoint > 0) {
|
|
|
// 扣除积分
|
|
|
- userRepo.updateVipPoint(userId, usePoint);
|
|
|
+ userRepo.addVipPoint(userId, usePoint);
|
|
|
log.info("取消加积分用户ID:{}, 积分:{}", userId, usePoint);
|
|
|
}
|
|
|
throw e;
|
|
|
@@ -818,19 +823,6 @@ public class OrderService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public void refund(Long id) throws WxPayException {
|
|
|
- Order order = orderRepo.findById(id).orElseThrow(new BusinessException("无记录"));
|
|
|
- if (order.getStatus() != OrderStatus.FINISH) {
|
|
|
- throw new BusinessException("订单未付款");
|
|
|
- }
|
|
|
- WxPayRefundRequest request = new WxPayRefundRequest();
|
|
|
- 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(snowflakeIdWorker.nextId()));
|
|
|
- wxPayService.refund(request);
|
|
|
- }
|
|
|
-
|
|
|
public Object queryCreateOrder(String id) {
|
|
|
Object res = redisTemplate.opsForValue().get(RedisKeys.CREATE_ORDER + id);
|
|
|
if (res != null) {
|