xiongzhu 4 سال پیش
والد
کامیت
dcaeb612a4

+ 34 - 0
src/main/java/com/izouma/nineth/repo/CollectionRepo.java

@@ -31,6 +31,7 @@ public interface CollectionRepo extends JpaRepository<Collection, Long>, JpaSpec
     @Query("update Collection t set t.likes = t.likes + ?2 where t.id = ?1")
     @Modifying
     @Transactional
+    @CacheEvict(value = "collection", key = "#id")
     void addLike(Long id, int num);
 
     @Query(value = "select distinct c from Collection c join Like l on l.collectionId = c.id " +
@@ -46,4 +47,37 @@ public interface CollectionRepo extends JpaRepository<Collection, Long>, JpaSpec
     @Query("select new com.izouma.nineth.dto.RecommendCollection(c,r) from Collection c join Recommend r on c.id = r.collectionId " +
             "where c.del = false and c.onShelf = true and r.type = ?1 order by r.sort desc")
     List<RecommendCollection> recommend(String type);
+
+    @Transactional
+    @Modifying
+    @Query("update Collection c set c.currentNumber = COALESCE(c.currentNumber, 0) + ?2 where c.id = ?1")
+    @CacheEvict(value = "collection", key = "#id")
+    void increaseNumber(Long id, int d);
+
+    @Transactional
+    @Modifying
+    @Query("update Collection c set c.sale = COALESCE(c.sale, 0) + ?2 where c.id = ?1")
+    @CacheEvict(value = "collection", key = "#id")
+    void increaseSale(Long id, int d);
+
+    @Transactional
+    @Modifying
+    @Query("update Collection c set c.stock = COALESCE(c.stock, 0) + ?2 where c.id = ?1")
+    @CacheEvict(value = "collection", key = "#id")
+    void increaseStock(Long id, int d);
+
+    @Transactional
+    @Modifying
+    @Query("update Collection c set c.total = COALESCE(c.total, 0) + ?2 where c.id = ?1")
+    @CacheEvict(value = "collection", key = "#id")
+    void increaseTotal(Long id, int d);
+
+    @Transactional
+    @Modifying
+    @Query("update Collection c set c.onShelf = ?2 where c.id = ?1")
+    @CacheEvict(value = "collection", key = "#id")
+    void setOnShelf(Long id, boolean onShelf);
+
+    @Query("select c.currentNumber from Collection c where c.id = ?1")
+    Optional<Integer> getCurrentNumber(Long id);
 }

+ 5 - 0
src/main/java/com/izouma/nineth/repo/UserRepo.java

@@ -158,4 +158,9 @@ public interface UserRepo extends JpaRepository<User, Long>, JpaSpecificationExe
     void setSales(Long userId, int sales);
 
     List<User> findByAuthoritiesContains(Authority authority);
+
+    @Transactional
+    @Modifying
+    @Query("update User u set u.sales = COALESCE(u.sales, 0) + ?2 where u.id = ?1")
+    public void increaseSales(Long id, int num);
 }

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

@@ -58,11 +58,10 @@ public class AirDropService {
                         BlindBoxItem winItem = collectionService.draw(collection.getId());
                         assetService.createAsset(winItem, user, null, null, "空投", collectionService.getNextNumber(winItem.getCollectionId()));
                     } else {
-                        assetService.createAsset(collection, user, null, null, "空投", collectionService.getNextNumber(collection));
+                        assetService.createAsset(collection, user, null, null, "空投", collectionService.getNextNumber(collection.getId()));
                     }
-                    collection.setStock(collection.getStock() - 1);
-                    collection.setSale(collection.getSale() + 1);
-                    collectionRepo.save(collection);
+                    collectionRepo.increaseStock(collection.getId(), -1);
+                    collectionRepo.increaseSale(collection.getId(), 1);
                 } catch (Exception e) {
                     log.error("空投出错", e);
                 }

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

@@ -10,6 +10,7 @@ import com.izouma.nineth.exception.BusinessException;
 import com.izouma.nineth.repo.AssetRepo;
 import com.izouma.nineth.repo.TokenHistoryRepo;
 import com.izouma.nineth.repo.UserRepo;
+import com.izouma.nineth.utils.SnowflakeIdWorker;
 import io.ipfs.api.IPFS;
 import io.ipfs.api.MerkleNode;
 import io.ipfs.api.NamedStreamable;
@@ -22,7 +23,8 @@ import org.springframework.core.env.Environment;
 import org.springframework.stereotype.Service;
 
 import java.io.File;
-import java.util.concurrent.LinkedBlockingQueue;
+import java.math.BigInteger;
+import java.util.Arrays;
 
 @Service
 @Slf4j
@@ -36,6 +38,19 @@ public class AssetMintService {
     private Environment        env;
 
     public void mint(Asset asset, Long historyId) {
+        if (Arrays.stream(env.getActiveProfiles()).anyMatch(s -> s.equals("dev"))) {
+            asset.setTokenId(new SnowflakeIdWorker(0, 1).nextId() + "");
+            asset.setBlockNumber(new BigInteger("1"));
+            asset.setTxHash("1");
+            asset.setGasUsed(new BigInteger("1"));
+            if (asset.getIpfsUrl() == null) {
+                asset.setIpfsUrl(ipfsUpload(asset.getPic().get(0).getUrl()));
+            }
+            assetRepo.save(asset);
+            applicationContext.publishEvent(new CreateAssetEvent(this, true, asset));
+            return;
+        }
+
         User user = userRepo.findById(asset.getUserId()).orElseThrow(new BusinessException("用户不存在"));
         if (StringUtils.isEmpty(user.getPublicKey())) {
             NFTAccount account = nftService.createAccount(user.getUsername() + "_");

+ 6 - 24
src/main/java/com/izouma/nineth/service/CollectionService.java

@@ -173,8 +173,7 @@ public class CollectionService {
         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);
+            collectionRepo.increaseStock(collection.getId(), -item.getTotal());
             BlindBoxItem blindBoxItem = new BlindBoxItem();
             BeanUtils.copyProperties(collection, blindBoxItem);
             blindBoxItem.setId(null);
@@ -210,9 +209,8 @@ public class CollectionService {
     public void scheduleOnShelf() {
         List<Collection> collections = collectionRepo.findByScheduleSaleTrueAndOnShelfFalseAndStartTimeBeforeAndDelFalse(LocalDateTime.now());
         for (Collection collection : collections) {
-            collection.setOnShelf(true);
+            collectionRepo.setOnShelf(collection.getId(), true);
         }
-        collectionRepo.saveAll(collections);
     }
 
 
@@ -271,24 +269,8 @@ public class CollectionService {
     }
 
     public synchronized Integer getNextNumber(Long collectionId) {
-        Collection collection = collectionRepo.findById(collectionId).orElse(null);
-        if (collection == null) return 0;
-        if (collection.getCurrentNumber() == null) {
-            collection.setCurrentNumber(0);
-        }
-        collection.setCurrentNumber(collection.getCurrentNumber() + 1);
-        collectionRepo.save(collection);
-        return collection.getCurrentNumber();
-    }
-
-    public synchronized Integer getNextNumber(Collection collection) {
-        if (collection == null) return 0;
-        if (collection.getCurrentNumber() == null) {
-            collection.setCurrentNumber(0);
-        }
-        collection.setCurrentNumber(collection.getCurrentNumber() + 1);
-        collectionRepo.save(collection);
-        return collection.getCurrentNumber();
+        collectionRepo.increaseNumber(collectionId, 1);
+        return collectionRepo.getCurrentNumber(collectionId).orElse(0);
     }
 
     public void addStock(Long id, int number) {
@@ -299,7 +281,7 @@ public class CollectionService {
         if (collection.getType() == CollectionType.BLIND_BOX) {
             throw new BusinessException("盲盒无法增发");
         }
-        collection.setStock(collection.getStock() + number);
-        collection.setTotal(collection.getTotal() + number);
+        collectionRepo.increaseStock(id, number);
+        collectionRepo.increaseTotal(id, number);
     }
 }

+ 10 - 16
src/main/java/com/izouma/nineth/service/OrderService.java

@@ -116,9 +116,8 @@ public class OrderService {
             userAddress = userAddressRepo.findById(addressId).orElseThrow(new BusinessException("地址信息不存在"));
         }
 
-        collection.setStock(collection.getStock() - qty);
-        collection.setSale(collection.getSale() + qty);
-        collectionRepo.save(collection);
+        collectionRepo.increaseStock(collectionId, -qty);
+        collectionRepo.increaseSale(collectionId, qty);
 
         BigDecimal gasFee = sysConfigService.getBigDecimal("gas_fee");
         Order order = Order.builder()
@@ -164,8 +163,7 @@ public class OrderService {
             Asset asset = assetRepo.findById(collection.getAssetId()).orElseThrow(new BusinessException("资产不存在"));
             asset.setStatus(AssetStatus.TRADING);
             assetRepo.save(asset);
-            collection.setOnShelf(false);
-            collectionRepo.save(collection);
+            collectionRepo.setOnShelf(collectionId, false);
         }
         order = orderRepo.save(order);
         if (order.getTotalPrice().equals(BigDecimal.ZERO)) {
@@ -388,7 +386,7 @@ public class OrderService {
                 orderRepo.save(order);
                 assetService.createAsset(winItem, user, order.getId(), order.getPrice(), "出售",
                         collectionService.getNextNumber(winItem.getCollectionId()));
-                addSales(winItem.getMinterId());
+                addSales(winItem.getMinterId(), order.getQty());
             } else {
                 if (collection.getSource() == CollectionSource.TRANSFER) {
                     Asset asset = assetRepo.findById(collection.getAssetId()).orElse(null);
@@ -399,7 +397,7 @@ public class OrderService {
                     assetService.createAsset(collection, user, order.getId(), order.getPrice(), "出售",
                             collectionService.getNextNumber(order.getCollectionId()));
                 }
-                addSales(collection.getMinterId());
+                addSales(collection.getMinterId(), order.getQty());
             }
             commission(order);
         } else if (order.getStatus() == OrderStatus.CANCELLED) {
@@ -455,11 +453,10 @@ public class OrderService {
                 asset.setStatus(AssetStatus.NORMAL);
                 assetRepo.save(asset);
             }
-            collection.setOnShelf(true);
+            collectionRepo.setOnShelf(collection.getId(), true);
         }
-        collection.setSale(collection.getSale() - 1);
-        collection.setStock(collection.getStock() + 1);
-        collectionRepo.save(collection);
+        collectionRepo.increaseSale(collection.getId(), -order.getQty());
+        collectionRepo.increaseStock(collection.getId(), order.getQty());
 
         order.setStatus(OrderStatus.CANCELLED);
         order.setCancelTime(LocalDateTime.now());
@@ -489,12 +486,9 @@ public class OrderService {
     public void refundCancelled(Order order) {
     }
 
-    public synchronized void addSales(Long userId) {
+    public synchronized void addSales(Long userId, int num) {
         if (userId != null) {
-            userRepo.findById(userId).ifPresent(user -> {
-                user.setSales(user.getSales() + 1);
-                userRepo.save(user);
-            });
+            userRepo.increaseSales(userId, num);
         }
     }
 

+ 3 - 4
src/test/java/com/izouma/nineth/service/OrderServiceTest.java

@@ -113,9 +113,8 @@ public class OrderServiceTest extends ApplicationTests {
                 }
             }
             orderRepo.delete(errOrder);
-            collection.setStock(collection.getStock() + 1);
-            collection.setSale(collection.getSale() - 1);
-            collectionRepo.save(collection);
+            collectionRepo.increaseStock(collection.getId(), 1);
+            collectionRepo.increaseSale(collection.getId(), -1);
         }
     }
 
@@ -167,7 +166,7 @@ public class OrderServiceTest extends ApplicationTests {
 
     @Test
     public void consignmentFix() throws IOException {
-        String s = FileUtils.readFile("/Users/drew/Downloads/app.log").replaceAll("</p>\n","</p>");
+        String s = FileUtils.readFile("/Users/drew/Downloads/app.log").replaceAll("</p>\n", "</p>");
         String[] arr = s.split("\n");
         for (int i = 0; i < arr.length; i++) {
             if (arr[i].contains("insert into collection_info")) {