|
|
@@ -38,6 +38,7 @@ import org.apache.http.client.utils.URLEncodedUtils;
|
|
|
import org.springframework.context.event.EventListener;
|
|
|
import org.springframework.core.env.Environment;
|
|
|
import org.springframework.data.domain.Page;
|
|
|
+import org.springframework.scheduling.annotation.Scheduled;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.ui.Model;
|
|
|
|
|
|
@@ -120,6 +121,7 @@ public class OrderService {
|
|
|
public void payOrderAlipay(Long id, Model model) {
|
|
|
try {
|
|
|
Order order = orderRepo.findByIdAndDelFalse(id).orElseThrow(new BusinessException("订单不存在"));
|
|
|
+
|
|
|
if (order.getStatus() != OrderStatus.NOT_PAID) {
|
|
|
throw new BusinessException("订单状态错误");
|
|
|
}
|
|
|
@@ -218,6 +220,8 @@ public class OrderService {
|
|
|
if (order.getStatus() == OrderStatus.NOT_PAID) {
|
|
|
if (order.getType() == CollectionType.BLIND_BOX) {
|
|
|
List<BlindBoxItem> items = blindBoxItemRepo.findByBlindBoxId(order.getCollectionId());
|
|
|
+
|
|
|
+
|
|
|
Map<BlindBoxItem, Range<Integer>> randomRange = new HashMap<>();
|
|
|
int c = 0, sum = 0;
|
|
|
for (BlindBoxItem item : items) {
|
|
|
@@ -226,35 +230,52 @@ public class OrderService {
|
|
|
sum += item.getStock();
|
|
|
}
|
|
|
|
|
|
- boolean win = false;
|
|
|
int retry = 0;
|
|
|
BlindBoxItem winItem = null;
|
|
|
while (winItem == null) {
|
|
|
+ retry++;
|
|
|
int rand = RandomUtils.nextInt(0, sum + 1);
|
|
|
for (Map.Entry<BlindBoxItem, Range<Integer>> entry : randomRange.entrySet()) {
|
|
|
BlindBoxItem item = entry.getKey();
|
|
|
Range<Integer> range = entry.getValue();
|
|
|
if (rand >= range.getMinimum() && rand < range.getMaximum()) {
|
|
|
+ int total = items.stream().filter(i -> !i.isRare())
|
|
|
+ .mapToInt(BlindBoxItem::getTotal).sum();
|
|
|
+ int stock = items.stream().filter(i -> !i.isRare())
|
|
|
+ .mapToInt(BlindBoxItem::getStock).sum();
|
|
|
if (item.isRare()) {
|
|
|
- int total = items.stream().filter(i -> !i.isRare())
|
|
|
- .mapToInt(BlindBoxItem::getTotal).sum();
|
|
|
- int stock = items.stream().filter(i -> !i.isRare())
|
|
|
- .mapToInt(BlindBoxItem::getStock).sum();
|
|
|
-
|
|
|
double nRate = stock / (double) total;
|
|
|
+ double rRate = (item.getStock() - 1) / (double) item.getTotal();
|
|
|
+
|
|
|
+ if (Math.abs(nRate - rRate) < (1 / (double) item.getTotal()) || retry > 1 || rRate == 0) {
|
|
|
+ if (!(nRate > 0.1 && item.getStock() == 1)) {
|
|
|
+ winItem = item;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ double nRate = (stock - 1) / (double) total;
|
|
|
double rRate = item.getStock() / (double) item.getTotal();
|
|
|
|
|
|
- if (Math.abs(nRate - rRate) < 0.05 || retry > 1) {
|
|
|
+ if (Math.abs(nRate - rRate) < 0.2 || retry > 1 || nRate == 0) {
|
|
|
winItem = item;
|
|
|
- } else {
|
|
|
- retry++;
|
|
|
}
|
|
|
- } else {
|
|
|
- winItem = item;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ if (retry > 100 && winItem == null) {
|
|
|
+ throw new BusinessException("盲盒抽卡失败");
|
|
|
+ }
|
|
|
}
|
|
|
+ winItem.setStock(winItem.getStock() - 1);
|
|
|
+ winItem.setSale(winItem.getSale() + 1);
|
|
|
+
|
|
|
+ order.setStatus(OrderStatus.PROCESSING);
|
|
|
+ order.setPayTime(LocalDateTime.now());
|
|
|
+ order.setTransactionId(MapUtils.getString(params, "trade_no"));
|
|
|
+ order.setPayMethod(PayMethod.ALIPAY);
|
|
|
+ orderRepo.save(order);
|
|
|
+ assetService.createAsset(order, winItem);
|
|
|
|
|
|
|
|
|
} else {
|
|
|
@@ -265,6 +286,8 @@ public class OrderService {
|
|
|
orderRepo.save(order);
|
|
|
assetService.createAsset(order);
|
|
|
}
|
|
|
+ } else if (order.getStatus() == OrderStatus.CANCELLED) {
|
|
|
+
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -283,4 +306,39 @@ public class OrderService {
|
|
|
log.error("创建asset失败");
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ public void cancel(Long id) {
|
|
|
+ Order order = orderRepo.findById(id).orElseThrow(new BusinessException("订单不存在"));
|
|
|
+ cancel(order);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void cancel(Order order) {
|
|
|
+ if (order.getStatus() != OrderStatus.NOT_PAID) {
|
|
|
+ throw new BusinessException("已支付订单无法取消");
|
|
|
+ }
|
|
|
+ Collection collection = collectionRepo.findById(order.getCollectionId())
|
|
|
+ .orElseThrow(new BusinessException("藏品不存在"));
|
|
|
+ User minter = userRepo.findById(collection.getMinterId()).orElseThrow(new BusinessException("铸造者不存在"));
|
|
|
+
|
|
|
+ collection.setSale(collection.getSale() - 1);
|
|
|
+ collection.setStock(collection.getStock() + 1);
|
|
|
+ collectionRepo.save(collection);
|
|
|
+
|
|
|
+ minter.setSales(minter.getSales() - 1);
|
|
|
+ userRepo.save(minter);
|
|
|
+
|
|
|
+ order.setStatus(OrderStatus.CANCELLED);
|
|
|
+ order.setCancelTime(LocalDateTime.now());
|
|
|
+ orderRepo.save(order);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Scheduled(fixedRate = 60000)
|
|
|
+ public void batchCancel() {
|
|
|
+ List<Order> orders = orderRepo.findByStatusAndCreatedAtBeforeAndDelFalse(OrderStatus.NOT_PAID,
|
|
|
+ LocalDateTime.now().minusMinutes(5));
|
|
|
+ orders.stream().parallel().forEach(this::cancel);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void refundCancelled(Order order) {
|
|
|
+ }
|
|
|
}
|