|
@@ -31,6 +31,7 @@ import org.apache.commons.lang3.Range;
|
|
|
import org.springframework.context.event.EventListener;
|
|
import org.springframework.context.event.EventListener;
|
|
|
import org.springframework.core.env.Environment;
|
|
import org.springframework.core.env.Environment;
|
|
|
import org.springframework.data.domain.Page;
|
|
import org.springframework.data.domain.Page;
|
|
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.ui.Model;
|
|
import org.springframework.ui.Model;
|
|
@@ -45,21 +46,22 @@ import java.util.*;
|
|
|
@Slf4j
|
|
@Slf4j
|
|
|
public class OrderService {
|
|
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 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 RedisTemplate<String, Object> redisTemplate;
|
|
|
|
|
|
|
|
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));
|
|
@@ -234,87 +236,6 @@ public class OrderService {
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public void notifyOrder1(Long orderId, PayMethod payMethod, String transactionId) {
|
|
|
|
|
- Order order = orderRepo.findById(orderId).orElseThrow(new BusinessException("订单不存在"));
|
|
|
|
|
- Collection collection = collectionRepo.findById(order.getCollectionId())
|
|
|
|
|
- .orElseThrow(new BusinessException("藏品不存在"));
|
|
|
|
|
- User user = userRepo.findById(order.getUserId()).orElseThrow(new BusinessException("用户不存在"));
|
|
|
|
|
- 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) {
|
|
|
|
|
- randomRange.put(item, Range.between(c, c + item.getStock()));
|
|
|
|
|
- c += item.getStock();
|
|
|
|
|
- sum += item.getStock();
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- 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()) {
|
|
|
|
|
- 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.2 || retry > 1 || nRate == 0) {
|
|
|
|
|
- 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(transactionId);
|
|
|
|
|
- order.setPayMethod(payMethod);
|
|
|
|
|
- orderRepo.save(order);
|
|
|
|
|
- assetService.createAsset(winItem, user, order.getId(), order.getPrice(), "出售");
|
|
|
|
|
- } else {
|
|
|
|
|
- if (collection.getSource() == CollectionSource.TRANSFER) {
|
|
|
|
|
- Asset asset = assetRepo.findById(collection.getAssetId()).orElse(null);
|
|
|
|
|
- assetService.transfer(asset, order.getPrice(), user, "转让", order.getId());
|
|
|
|
|
- collectionRepo.delete(collection);
|
|
|
|
|
- } else {
|
|
|
|
|
- order.setStatus(OrderStatus.PROCESSING);
|
|
|
|
|
- order.setPayTime(LocalDateTime.now());
|
|
|
|
|
- order.setTransactionId(transactionId);
|
|
|
|
|
- order.setPayMethod(payMethod);
|
|
|
|
|
- orderRepo.save(order);
|
|
|
|
|
- assetService.createAsset(collection, user, order.getId(), order.getPrice(), "出售");
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- } else if (order.getStatus() == OrderStatus.CANCELLED) {
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
@Transactional
|
|
@Transactional
|
|
|
public void notifyOrder(Long orderId, PayMethod payMethod, String transactionId) {
|
|
public void notifyOrder(Long orderId, PayMethod payMethod, String transactionId) {
|
|
|
Order order = orderRepo.findById(orderId).orElseThrow(new BusinessException("订单不存在"));
|
|
Order order = orderRepo.findById(orderId).orElseThrow(new BusinessException("订单不存在"));
|
|
@@ -329,7 +250,8 @@ public class OrderService {
|
|
|
if (order.getType() == CollectionType.BLIND_BOX) {
|
|
if (order.getType() == CollectionType.BLIND_BOX) {
|
|
|
BlindBoxItem winItem = collectionService.draw(collection.getId());
|
|
BlindBoxItem winItem = collectionService.draw(collection.getId());
|
|
|
orderRepo.save(order);
|
|
orderRepo.save(order);
|
|
|
- assetService.createAsset(winItem, user, order.getId(), order.getPrice(), "出售");
|
|
|
|
|
|
|
+ assetService.createAsset(winItem, user, order.getId(), order.getPrice(), "出售",
|
|
|
|
|
+ collectionService.getNextNumber(winItem.getCollectionId()));
|
|
|
} else {
|
|
} else {
|
|
|
if (collection.getSource() == CollectionSource.TRANSFER) {
|
|
if (collection.getSource() == CollectionSource.TRANSFER) {
|
|
|
Asset asset = assetRepo.findById(collection.getAssetId()).orElse(null);
|
|
Asset asset = assetRepo.findById(collection.getAssetId()).orElse(null);
|
|
@@ -337,7 +259,8 @@ public class OrderService {
|
|
|
collectionRepo.delete(collection);
|
|
collectionRepo.delete(collection);
|
|
|
} else {
|
|
} else {
|
|
|
orderRepo.save(order);
|
|
orderRepo.save(order);
|
|
|
- assetService.createAsset(collection, user, order.getId(), order.getPrice(), "出售");
|
|
|
|
|
|
|
+ assetService.createAsset(collection, user, order.getId(), order.getPrice(), "出售",
|
|
|
|
|
+ collectionService.getNextNumber(order.getCollectionId()));
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
} else if (order.getStatus() == OrderStatus.CANCELLED) {
|
|
} else if (order.getStatus() == OrderStatus.CANCELLED) {
|
|
@@ -423,4 +346,18 @@ public class OrderService {
|
|
|
|
|
|
|
|
public void refundCancelled(Order order) {
|
|
public void refundCancelled(Order order) {
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ public void setNumber() {
|
|
|
|
|
+ for (Collection collection : collectionRepo.findAll()) {
|
|
|
|
|
+ if (collection.getSource() != CollectionSource.OFFICIAL) continue;
|
|
|
|
|
+ String key = "collectionNumber::" + collection.getId();
|
|
|
|
|
+ redisTemplate.opsForValue().set(key, 0);
|
|
|
|
|
+ for (Asset asset : assetRepo.findByCollectionId(collection.getId())) {
|
|
|
|
|
+ redisTemplate.opsForValue().increment(key);
|
|
|
|
|
+ asset.setNumber((Integer) redisTemplate.opsForValue().get(key));
|
|
|
|
|
+ assetRepo.save(asset);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|