xiongzhu 3 years ago
parent
commit
bc0bbc9e83

+ 2 - 0
src/main/java/com/izouma/nineth/config/Constants.java

@@ -19,6 +19,8 @@ public interface Constants {
 
     String PAY_ERR_MSG = "绿洲宇宙冷却系统已启动,请稍后支付";
 
+    Long BLACK_HOLE_USER_ID = 1435297L;
+
     interface PayChannel {
         String SAND = "sandPay";
         String HM   = "hmPay";

+ 1 - 1
src/main/java/com/izouma/nineth/service/AssetService.java

@@ -822,7 +822,7 @@ public class AssetService {
 //            cancelPublic(asset);
         }
 
-        User toUser = userRepo.findById(1435297L).orElseThrow(new BusinessException("无记录"));
+        User toUser = userRepo.findById(Constants.BLACK_HOLE_USER_ID).orElseThrow(new BusinessException("无记录"));
 
         TokenHistory tokenHistory = TokenHistory.builder()
                 .tokenId(asset.getTokenId())

+ 15 - 31
src/main/java/com/izouma/nineth/service/MintOrderService.java

@@ -124,7 +124,7 @@ public class MintOrderService {
     @Transactional
     public void create(Long userId, List<Long> assetIds) {
         User user = userRepo.findByIdAndDelFalse(userId).orElseThrow(new BusinessException("用户不存在"));
-        User blackHole = userRepo.findByIdAndDelFalse(1435297L).orElseThrow(new BusinessException("无法铸造"));
+        User blackHole = userRepo.findByIdAndDelFalse(Constants.BLACK_HOLE_USER_ID).orElseThrow(new BusinessException("无法铸造"));
         if (assetIds.size() != 3) {
             throw new BusinessException("数量不正确,请重新选择");
         }
@@ -239,11 +239,10 @@ public class MintOrderService {
 //            if (assets.stream().anyMatch(a -> a.isPublicShow() || a.isConsignment())) {
 //                throw new BusinessException("请先下架所选藏品");
 //            }
-
+            if (assets.stream().anyMatch(a -> a.getStatus() != AssetStatus.NORMAL)) {
+                throw new BusinessException("所选藏品不符和规则,请重新选择");
+            }
             if (!mintActivity.isAudit()) {
-                if (assets.stream().anyMatch(a -> a.getStatus() != AssetStatus.NORMAL)) {
-                    throw new BusinessException("所选藏品不符和规则,请重新选择");
-                }
                 if (!mintActivityService.matchRule(new ArrayList<>(assets), mintActivity.getRule())) {
                     throw new BusinessException("所选藏品不符和规则,请重新选择");
                 }
@@ -258,38 +257,23 @@ public class MintOrderService {
                 }
             }
 
-
             Map<Long, Long> privilegeIds = new HashMap<>();
             // 铸造特权
             if (!mintActivity.isConsume()) {
-                assets.forEach(asset -> {
-                    List<Privilege> privileges = asset.getPrivileges()
-                            .stream()
-                            .filter(p -> p.getName().equals("铸造"))
-                            .collect(Collectors.toList());
-                    if (privileges.size() == 0) {
-                        throw new BusinessException("无铸造特权");
-                    } else {
-                        boolean flag = false;
-                        for (Privilege privilege : privileges) {
-                            // 打开多次 或者 可打开一次但未使用
-                            if (!privilege.isOnce() || (privilege.isOnce() && !privilege
-                                    .isOpened())) {
-                                flag = true;
-                                privilegeIds.put(asset.getId(), privilege.getId());
-                                break;
-                            }
-                        }
-                        if (!flag) {
-                            throw new BusinessException("铸造特权已使用");
-                        }
+                assets.forEach(a -> {
+                    if (a.getPrivileges().stream().noneMatch(p ->
+                            "铸造".equals(p.getName())
+                                    && (!p.isOnce() || !p.isOpened()))) {
+                        throw new BusinessException(a.getName() + (a.getNumber() == null ? "" : (" #" + a.getNumber().toString())) + " 无铸造特权或特权已使用");
                     }
                 });
                 assets.forEach(asset -> {
                     asset.getPrivileges()
                             .stream()
-                            .filter(p -> p.getId().equals(privilegeIds.get(asset.getId())))
-                            .forEach(p -> {
+                            .filter(p -> !p.isOnce() || !p.isOpened())
+                            .findFirst()
+                            .ifPresent(p -> {
+                                privilegeIds.put(asset.getId(), p.getId());
                                 p.setOpened(true);
                                 p.setOpenTime(LocalDateTime.now());
                                 p.setOpenedBy(SecurityUtils.getAuthenticatedUser().getId());
@@ -298,7 +282,7 @@ public class MintOrderService {
                 });
             } else {
                 // 转让的用户
-                userRepo.findByIdAndDelFalse(1435297L).orElseThrow(new BusinessException("无法铸造"));
+                userRepo.findByIdAndDelFalse(Constants.BLACK_HOLE_USER_ID).orElseThrow(new BusinessException("无法铸造"));
 
                 // 消耗改为转赠
                 assets.forEach(asset -> {
@@ -569,7 +553,7 @@ public class MintOrderService {
             }
 
             if (mintOrder.isConsume()) {
-                User newOwner = userRepo.findByIdAndDelFalse(1435297L).orElseThrow(new BusinessException("无法铸造"));
+                User newOwner = userRepo.findByIdAndDelFalse(Constants.BLACK_HOLE_USER_ID).orElseThrow(new BusinessException("无法铸造"));
                 assets.forEach(asset -> assetService.transfer(asset, asset.getPrice(), newOwner, TransferReason.GIFT, null));
             }
             mintOrderRepo.save(mintOrder);

+ 2 - 2
src/test/java/com/izouma/nineth/service/MintOrderServiceTest.java

@@ -159,7 +159,7 @@ class MintOrderServiceTest extends ApplicationTests {
                                 tokenHistoryRepo.findByTokenIdOrderByCreatedAtDesc(asset.getTokenId()).stream()
                                         .findFirst()
                                         .ifPresent(tokenHistory -> {
-                                            if (tokenHistory.getToUserId().equals(1435297L)) {
+                                            if (tokenHistory.getToUserId().equals(Constants.BLACK_HOLE_USER_ID)) {
                                                 tokenHistoryRepo.delete(tokenHistory);
                                             }
                                         });
@@ -223,7 +223,7 @@ class MintOrderServiceTest extends ApplicationTests {
                     userRepo.updateAssetOwner(asset.getUserId());
                     tokenHistoryRepo.findByTokenIdOrderByCreatedAtDesc(asset.getTokenId()).stream().findFirst()
                             .ifPresent(tokenHistory -> {
-                                if (tokenHistory.getToUserId().equals(1435297L)) {
+                                if (tokenHistory.getToUserId().equals(Constants.BLACK_HOLE_USER_ID)) {
                                     tokenHistoryRepo.delete(tokenHistory);
                                 }
                             });