|
|
@@ -102,35 +102,40 @@ public class CollectionService {
|
|
|
public Collection createBlindBox(CreateBlindBox createBlindBox) {
|
|
|
Collection blindBox = createBlindBox.getBlindBox();
|
|
|
|
|
|
- List<Collection> list = new ArrayList<>();
|
|
|
- createBlindBox.getItems().stream().parallel().forEach(item -> {
|
|
|
- Collection collection = collectionRepo.findById(item.getId()).orElseThrow(new BusinessException("所选藏品不存在"));
|
|
|
- list.add(collection);
|
|
|
+ List<Collection> list =
|
|
|
+ collectionRepo.findAllById(createBlindBox.getItems().stream().map(BlindBoxItem::getCollectionId)
|
|
|
+ .collect(Collectors.toSet()));
|
|
|
+ for (BlindBoxItem item : createBlindBox.getItems()) {
|
|
|
+ Collection collection = list.stream().filter(i -> i.getId().equals(item.getCollectionId())).findAny()
|
|
|
+ .orElseThrow(new BusinessException("所选藏品不存在"));
|
|
|
if (item.getTotal() > collection.getStock()) {
|
|
|
- throw new BusinessException("所选藏品库存不足");
|
|
|
+ throw new BusinessException("所选藏品库存不足:" + collection.getName());
|
|
|
}
|
|
|
- });
|
|
|
+ }
|
|
|
|
|
|
User user = SecurityUtils.getAuthenticatedUser();
|
|
|
blindBox.setMinter(user.getNickname());
|
|
|
blindBox.setMinterId(user.getId());
|
|
|
blindBox.setMinterAvatar(user.getAvatar());
|
|
|
+ blindBox.setStock(blindBox.getTotal());
|
|
|
+ blindBox.setSale(0);
|
|
|
collectionRepo.save(blindBox);
|
|
|
- createBlindBox.getItems().stream().parallel().forEach(item -> {
|
|
|
- Collection collection = list.stream().filter(i -> i.getId().equals(item.getId())).findAny().get();
|
|
|
+ for (BlindBoxItem item : createBlindBox.getItems()) {
|
|
|
+ Collection collection = list.stream().filter(i -> i.getId().equals(item.getCollectionId())).findAny()
|
|
|
+ .orElseThrow(new BusinessException("所选藏品不存在"));
|
|
|
collection.setStock(collection.getStock() - item.getTotal());
|
|
|
collectionRepo.save(collection);
|
|
|
BlindBoxItem blindBoxItem = new BlindBoxItem();
|
|
|
BeanUtils.copyProperties(collection, blindBoxItem);
|
|
|
- blindBoxItem.setCollectionId(collection.getId());
|
|
|
blindBoxItem.setId(null);
|
|
|
+ blindBoxItem.setCollectionId(item.getCollectionId());
|
|
|
blindBoxItem.setSale(0);
|
|
|
blindBoxItem.setTotal(item.getTotal());
|
|
|
blindBoxItem.setStock(item.getTotal());
|
|
|
blindBoxItem.setRare(item.isRare());
|
|
|
blindBoxItem.setBlindBoxId(blindBox.getId());
|
|
|
blindBoxItemRepo.save(blindBoxItem);
|
|
|
- });
|
|
|
+ }
|
|
|
|
|
|
return blindBox;
|
|
|
}
|