licailing 3 lat temu
rodzic
commit
4a22e521c6

+ 2 - 0
src/main/java/com/izouma/nineth/domain/ActivityMaterial.java

@@ -5,6 +5,7 @@ import com.fasterxml.jackson.annotation.JsonInclude;
 import com.izouma.nineth.converter.FileObjectListConverter;
 import io.swagger.annotations.ApiModelProperty;
 import lombok.AllArgsConstructor;
+import lombok.Builder;
 import lombok.Data;
 import lombok.NoArgsConstructor;
 
@@ -17,6 +18,7 @@ import java.util.List;
 @Entity
 @AllArgsConstructor
 @NoArgsConstructor
+@Builder
 @JsonInclude(JsonInclude.Include.NON_NULL)
 @JsonIgnoreProperties(value = {"hibernateLazyInitializer"}, ignoreUnknown = true)
 public class ActivityMaterial extends BaseEntity {

+ 94 - 42
src/main/java/com/izouma/nineth/service/ActivityOrderService.java

@@ -28,62 +28,114 @@ public class ActivityOrderService {
     private AssetRepo                 assetRepo;
     private OrderRepo                 orderRepo;
     private CollectionRepo            collectionRepo;
+    private ActivityMaterialRepo      activityMaterialRepo;
+    private CollectionService         collectionService;
+    private AssetService              assetService;
+    private UserRepo                  userRepo;
 
     public Page<ActivityOrder> all(PageQuery pageQuery) {
         return activityOrderRepo.findAll(JpaUtils.toSpecification(pageQuery, ActivityOrder.class), JpaUtils.toPageRequest(pageQuery));
     }
 
 
-    public void created(Long userId, Long mintActivityId) {
-        ActivityCollection activity = activityCollectionRepo.findById(mintActivityId)
-                .orElseThrow(new BusinessException("活动不存在"));
+    public void created(User user, Long mintActivityId) {
+        try {
+            ActivityCollection activity = activityCollectionRepo.findById(mintActivityId)
+                    .orElseThrow(new BusinessException("活动不存在"));
 
-        //库存
-        int stock = Optional.ofNullable(activityCollectionService.increaseStock(mintActivityId, -1))
-                .map(Math::toIntExact)
-                .orElseThrow(new BusinessException("很遗憾,活动已无库存"));
-        if (stock < 0) {
-            throw new BusinessException("活动已无库存");
-        }
+            //库存
+            int stock = Optional.ofNullable(activityCollectionService.increaseStock(mintActivityId, -1))
+                    .map(Math::toIntExact)
+                    .orElseThrow(new BusinessException("很遗憾,活动已无库存"));
+            if (stock < 0) {
+                throw new BusinessException("活动已无库存");
+            }
 
-        //数量
-        List<Asset> assets = assetRepo.findAllByCollectionIdAndUserIdAndStatus(activity.getCollectionId(), userId, AssetStatus.NORMAL);
-        if (assets.size() < activity.getNum()) {
-            throw new BusinessException("数量不足,无法领取");
-        }
+            //数量
+            List<Asset> assets = assetRepo.findAllByCollectionIdAndUserIdAndStatus(activity.getCollectionId(), user.getId(),
+                    AssetStatus.NORMAL);
+            if (assets.size() < activity.getNum()) {
+                throw new BusinessException("数量不足,无法领取");
+            }
 
-        List<Asset> consumption = assets.stream().limit(activity.getNum()).collect(Collectors.toList());
-        //消耗资产
-        consumption.forEach(asset -> {
-            if (asset.isPublicShow()) {
-                if (asset.isConsignment()) {
-                    asset.setConsignment(false);
-                }
+            //兑换后的
+            Collection award = collectionRepo.findById(activity.getAwardCollectionId())
+                    .orElseThrow(new BusinessException("无藏品"));
 
-                if (asset.getPublicCollectionId() != null) {
-                    List<Order> orders = orderRepo.findByCollectionId(asset.getPublicCollectionId());
-                    if (orders.stream().anyMatch(o -> o.getStatus() != OrderStatus.CANCELLED)) {
-                        throw new BusinessException("已有订单不可兑换");
-                    }
-                    Collection collection = collectionRepo.findById(asset.getPublicCollectionId())
-                            .orElseThrow(new BusinessException("无展示记录"));
-                    List<Long> collectionIds = collectionRepo.findAllByAssetId(collection.getAssetId());
-                    if (CollectionUtils.isNotEmpty(collectionIds)) {
-                        log.info("删除collection {}", collectionIds);
-                        collectionRepo.deleteAllByIdIn(collectionIds);
-                    } else {
-                        log.info("删除collection {}", collection.getId());
-                        collectionRepo.delete(collection);
+            int awardStock = Optional.ofNullable(collectionService.decreaseStock(activity.getAwardCollectionId(), 1))
+                    .map(Math::toIntExact)
+                    .orElseThrow(new BusinessException("兑换藏品无库存"));
+            if (awardStock < 0) {
+                throw new BusinessException("兑换藏品无库存");
+            }
+
+            //指定账户
+            User newOwner = userRepo.findByIdAndDelFalse(1590945L).orElseThrow(new BusinessException("无法兑换"));
+
+            List<Asset> consumption = assets.stream().limit(activity.getNum()).collect(Collectors.toList());
+            //消耗资产
+            consumption.forEach(asset -> {
+                if (asset.isPublicShow()) {
+//                    if (asset.isConsignment()) {
+//                        asset.setConsignment(false);
+//                    }
+
+                    if (asset.getPublicCollectionId() != null) {
+                        List<Order> orders = orderRepo.findByCollectionId(asset.getPublicCollectionId());
+                        if (orders.stream().anyMatch(o -> o.getStatus() != OrderStatus.CANCELLED)) {
+                            throw new BusinessException("已有订单不可兑换");
+                        }
+                        Collection collection = collectionRepo.findById(asset.getPublicCollectionId())
+                                .orElseThrow(new BusinessException("无展示记录"));
+                        List<Long> collectionIds = collectionRepo.findAllByAssetId(collection.getAssetId());
+                        if (CollectionUtils.isNotEmpty(collectionIds)) {
+                            log.info("删除collection {}", collectionIds);
+                            collectionRepo.deleteAllByIdIn(collectionIds);
+                        } else {
+                            log.info("删除collection {}", collection.getId());
+                            collectionRepo.delete(collection);
+                        }
                     }
+//
+//                asset.setPublicShow(false);
+//                asset.setPublicCollectionId(null);
                 }
+//            asset.setStatus(AssetStatus.REDEEMED);
+//            assetRepo.save(asset);
+            });
 
-                asset.setPublicShow(false);
-                asset.setPublicCollectionId(null);
-            }
-            asset.setStatus(AssetStatus.REDEEMED);
-            assetRepo.save(asset);
-        });
+            //收集记录
+            ActivityOrder order = activityOrderRepo.save(ActivityOrder.builder()
+                    .userId(user.getId())
+                    .phone(user.getPhone())
+                    .activityCollection(activity.getCollectionName())
+                    .activityCollectionId(mintActivityId)
+                    .build());
+
+            //兑换详情
+            consumption.forEach(asset -> {
+                activityMaterialRepo.save(
+                        ActivityMaterial.builder()
+                                .orderId(order.getId())
+                                .assetId(asset.getId())
+                                .category(asset.getCategory())
+                                .collectionId(asset.getCollectionId())
+                                .name(asset.getName())
+                                .number(asset.getNumber())
+                                .pic(asset.getPic())
+                                .build());
+                // 转赠
+                assetService.transfer(asset, asset.getPrice(), newOwner, "转赠", null);
+            });
 
+            //发放新的
+            assetService.createAsset(award, user, order.getId(), null, "兑换",
+                    award.getTotal() > 1 ? collectionService.getNextNumber(award.getId()) : null);
 
+        } catch (Exception e) {
+            // 错了加库存
+            activityCollectionService.increaseStock(mintActivityId, 1);
+            throw e;
+        }
     }
 }

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

@@ -370,11 +370,16 @@ public class AssetService {
                 case "转让":
                     userHistory.setDescription(tokenHistory.getToUserId().equals(userId) ? "作品交易——买入" : "作品交易——售出");
                     break;
+                case "兑换":
+                    userHistory.setDescription("兑换");
                 case "空投":
                     userHistory.setDescription("空投");
                     break;
                 case "转赠":
                     userHistory.setDescription(tokenHistory.getToUserId().equals(userId) ? "他人赠送" : "作品赠送");
+                    if (userHistory.getToUserId().equals(1590945L)) {
+                        userHistory.setDescription("活动收集销毁");
+                    }
                     break;
             }
             return userHistory;

+ 21 - 0
src/test/java/com/izouma/nineth/service/ActivityOrderServiceTest.java

@@ -0,0 +1,21 @@
+package com.izouma.nineth.service;
+
+import com.izouma.nineth.ApplicationTests;
+import com.izouma.nineth.domain.User;
+import com.izouma.nineth.repo.UserRepo;
+import org.junit.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+
+public class ActivityOrderServiceTest extends ApplicationTests {
+
+    @Autowired
+    private ActivityOrderService activityOrderService;
+    @Autowired
+    private UserRepo             userRepo;
+
+    @Test
+    public void created() {
+        User user = userRepo.findById(7834L).orElse(null);
+        activityOrderService.created(user, 8847L);
+    }
+}