xiongzhu 4 년 전
부모
커밋
f8d123de2e

+ 3 - 0
src/main/java/com/izouma/nineth/domain/Collection.java

@@ -74,6 +74,9 @@ public class Collection extends BaseEntity {
     @ApiModelProperty("特权")
     private List<Privilege> privileges;
 
+    @ApiModelProperty("编号")
+    private Integer number;
+
     @ApiModelProperty("是否可转售")
     private boolean canResale;
 

+ 3 - 0
src/main/java/com/izouma/nineth/repo/AssetRepo.java

@@ -8,6 +8,7 @@ import org.springframework.data.jpa.repository.Modifying;
 import org.springframework.data.jpa.repository.Query;
 
 import javax.transaction.Transactional;
+import java.util.List;
 
 public interface AssetRepo extends JpaRepository<Asset, Long>, JpaSpecificationExecutor<Asset> {
     @Query("update Asset t set t.del = true where t.id = ?1")
@@ -16,4 +17,6 @@ public interface AssetRepo extends JpaRepository<Asset, Long>, JpaSpecificationE
     void softDelete(Long id);
 
     long countByIpfsUrlAndStatusNot(String ipfsUrl, AssetStatus status);
+
+    List<Asset> findByCollectionId(Long collectionId);
 }

+ 2 - 2
src/main/java/com/izouma/nineth/service/AirDropService.java

@@ -56,9 +56,9 @@ public class AirDropService {
                 try {
                     if (collection.getType() == CollectionType.BLIND_BOX) {
                         BlindBoxItem winItem = collectionService.draw(collection.getId());
-                        assetService.createAsset(winItem, user, null, null, "空投");
+                        assetService.createAsset(winItem, user, null, null, "空投", collectionService.getNextNumber(winItem.getCollectionId()));
                     } else {
-                        assetService.createAsset(collection, user, null, null, "空投");
+                        assetService.createAsset(collection, user, null, null, "空投", collectionService.getNextNumber(collection.getId()));
                     }
                     collection.setStock(collection.getStock() - 1);
                     collection.setSale(collection.getSale() + 1);

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

@@ -46,7 +46,7 @@ public class AssetService {
     }
 
     @Async
-    public Future<Asset> createAsset(Collection collection, User user, Long orderId, BigDecimal price, String type) {
+    public Future<Asset> createAsset(Collection collection, User user, Long orderId, BigDecimal price, String type, Integer number) {
         Asset asset = Asset.create(collection, user);
         asset.setOrderId(orderId);
         asset.setPrice(price);
@@ -67,7 +67,7 @@ public class AssetService {
     }
 
     @Async
-    public Future<Asset> createAsset(BlindBoxItem winItem, User user, Long orderId, BigDecimal price, String type) {
+    public Future<Asset> createAsset(BlindBoxItem winItem, User user, Long orderId, BigDecimal price, String type, Integer number) {
         Asset asset = Asset.create(winItem, user);
         asset.setOrderId(orderId);
         asset.setPrice(price);
@@ -119,6 +119,7 @@ public class AssetService {
                 .royalties(asset.getRoyalties())
                 .serviceCharge(asset.getServiceCharge())
                 .assetId(id)
+                .number(asset.getNumber())
                 .build();
         collectionRepo.save(collection);
         asset.setPublicShow(true);
@@ -161,6 +162,7 @@ public class AssetService {
                 .royalties(asset.getRoyalties())
                 .serviceCharge(asset.getServiceCharge())
                 .assetId(id)
+                .number(asset.getNumber())
                 .build();
         collectionRepo.save(collection);
         asset.setPublicShow(true);

+ 11 - 10
src/main/java/com/izouma/nineth/service/CollectionService.java

@@ -21,6 +21,7 @@ import org.springframework.data.domain.PageImpl;
 import org.springframework.data.domain.PageRequest;
 import org.springframework.data.domain.Sort;
 import org.springframework.data.jpa.domain.Specification;
+import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Service;
 
@@ -34,12 +35,13 @@ import java.util.stream.Collectors;
 @AllArgsConstructor
 public class CollectionService {
 
-    private CollectionRepo   collectionRepo;
-    private LikeRepo         likeRepo;
-    private BlindBoxItemRepo blindBoxItemRepo;
-    private AppointmentRepo  appointmentRepo;
-    private UserRepo         userRepo;
-    private AssetService     assetService;
+    private CollectionRepo                collectionRepo;
+    private LikeRepo                      likeRepo;
+    private BlindBoxItemRepo              blindBoxItemRepo;
+    private AppointmentRepo               appointmentRepo;
+    private UserRepo                      userRepo;
+    private AssetService                  assetService;
+    private RedisTemplate<String, Object> redisTemplate;
 
     public Page<Collection> all(PageQuery pageQuery) {
         pageQuery.getQuery().put("del", false);
@@ -262,9 +264,8 @@ public class CollectionService {
         return winItem;
     }
 
-    public void airDrop(Long collectionId, List<Long> userIds) {
-        Collection collection = collectionRepo.findById(collectionId).orElseThrow(new BusinessException("藏品不存在"));
-        for (Long userId : userIds) {
-        }
+    public synchronized Integer getNextNumber(Long collectionId) {
+        redisTemplate.opsForValue().increment("collectionNumber::" + collectionId);
+        return (Integer) redisTemplate.opsForValue().get("collectionNumber::" + collectionId);
     }
 }

+ 35 - 98
src/main/java/com/izouma/nineth/service/OrderService.java

@@ -31,6 +31,7 @@ import org.apache.commons.lang3.Range;
 import org.springframework.context.event.EventListener;
 import org.springframework.core.env.Environment;
 import org.springframework.data.domain.Page;
+import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Service;
 import org.springframework.ui.Model;
@@ -45,21 +46,22 @@ import java.util.*;
 @Slf4j
 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) {
         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
     public void notifyOrder(Long orderId, PayMethod payMethod, String transactionId) {
         Order order = orderRepo.findById(orderId).orElseThrow(new BusinessException("订单不存在"));
@@ -329,7 +250,8 @@ public class OrderService {
             if (order.getType() == CollectionType.BLIND_BOX) {
                 BlindBoxItem winItem = collectionService.draw(collection.getId());
                 orderRepo.save(order);
-                assetService.createAsset(winItem, user, order.getId(), order.getPrice(), "出售");
+                assetService.createAsset(winItem, user, order.getId(), order.getPrice(), "出售",
+                        collectionService.getNextNumber(winItem.getCollectionId()));
             } else {
                 if (collection.getSource() == CollectionSource.TRANSFER) {
                     Asset asset = assetRepo.findById(collection.getAssetId()).orElse(null);
@@ -337,7 +259,8 @@ public class OrderService {
                     collectionRepo.delete(collection);
                 } else {
                     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) {
@@ -423,4 +346,18 @@ public class OrderService {
 
     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);
+            }
+        }
+    }
 }

+ 17 - 0
src/test/java/com/izouma/nineth/RedisTest.java

@@ -0,0 +1,17 @@
+package com.izouma.nineth;
+
+import org.junit.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.redis.core.BoundValueOperations;
+import org.springframework.data.redis.core.RedisTemplate;
+
+public class RedisTest extends ApplicationTests {
+    @Autowired
+    private RedisTemplate<String, Object> redisTemplate;
+
+    @Test
+    public void testINC() {
+        redisTemplate.opsForValue().increment("collectionNumber::2");
+        System.out.println(redisTemplate.opsForValue().get("collectionNumber::2"));
+    }
+}

+ 5 - 0
src/test/java/com/izouma/nineth/service/OrderServiceTest.java

@@ -17,4 +17,9 @@ public class OrderServiceTest extends ApplicationTests {
         Order order = orderService.create(1110L, 1777L, 1, null, 1896L);
         assert order.getStatus() == OrderStatus.FINISH;
     }
+
+    @Test
+    public void setNumber() {
+        orderService.setNumber();
+    }
 }