Browse Source

新拍卖流程

licailing 3 years ago
parent
commit
e873ca2d3b

+ 2 - 0
src/main/java/com/izouma/nineth/enums/BalanceType.java

@@ -4,6 +4,8 @@ public enum BalanceType {
 
     WITHDRAW("提现"),
     SELL("藏品出售"),
+    AUCTION("藏品拍卖"),
+    REWARD("拍卖奖励"),
     RETURN("失败退回"),
     PAY("支付"),
     RECHARGE("充值"),

+ 9 - 5
src/main/java/com/izouma/nineth/repo/AuctionRecordRepo.java

@@ -1,7 +1,6 @@
 package com.izouma.nineth.repo;
 
 import com.izouma.nineth.domain.AuctionRecord;
-import com.izouma.nineth.enums.AuctionType;
 import org.springframework.data.jpa.repository.JpaRepository;
 import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
 import org.springframework.data.jpa.repository.Modifying;
@@ -24,8 +23,13 @@ public interface AuctionRecordRepo extends JpaRepository<AuctionRecord, Long>, J
 
     @Query(nativeQuery = true, value = "WITH ranked_messages AS (" +
             " SELECT m.*, ROW_NUMBER() OVER (PARTITION BY auction_id ORDER BY id DESC) AS rn" +
-            "    FROM auction_record AS m where user_id = ?1 and auction_type = ?2" +
-            ")" +
+            "    FROM auction_record AS m where user_id = ?1 and auction_type = ?2)" +
             " SELECT * FROM ranked_messages WHERE rn = 1;")
-    List<AuctionRecord> findByUserId(Long userId, String type);
-}
+    List<AuctionRecord> findByUserId(Long userId, String auctionType);
+
+    @Query(nativeQuery = true, value = "WITH ranked_messages AS (" +
+            " SELECT m.user_id, ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY id DESC) AS rn" +
+            "    FROM auction_record AS m where auction_id = ?1 and type = 'DEPOSIT' and pay_deposit = true)" +
+            " SELECT * FROM ranked_messages WHERE rn = 1 limit ?2;")
+    List<Long> findByAuctionId(Long auctionId, int size);
+}

+ 40 - 38
src/main/java/com/izouma/nineth/service/AuctionOrderService.java

@@ -65,9 +65,11 @@ public class AuctionOrderService {
     @Autowired
     private SmsService                    smsService;
     @Autowired
-    private UserBalanceRepo               userBalanceRepo;
+    private UserBalanceService            userBalanceService;
     @Autowired
-    private BalanceRecordRepo             balanceRecordRepo;
+    private ShowroomService               showroomService;
+    @Autowired
+    private CollectionRepo                collectionRepo;
     @Autowired
     private ShowroomRepo                  showroomRepo;
 
@@ -285,32 +287,11 @@ public class AuctionOrderService {
             }
 
             //用户冲余额
-            UserBalance userBalance = userBalanceRepo.findByUserId(asset.getOwnerId())
-                    .orElse(UserBalance.builder()
-                            .balance(BigDecimal.ZERO)
-                            .lastBalance(BigDecimal.ZERO)
-                            .userId(asset.getOwnerId())
-                            .build());
-
             BigDecimal amount = order.getTotalPrice()
                     .multiply(BigDecimal.valueOf(100 - order.getRoyalties() - order.getServiceCharge()))
                     .divide(new BigDecimal("100"), 2, RoundingMode.HALF_UP);
 
-            userBalance.setLastBalance(userBalance.getBalance());
-            userBalance.setBalance(userBalance.getBalance().add(amount));
-            userBalanceRepo.save(userBalance);
-            log.info("拍卖冲用户余额{},¥{}", asset.getOwnerId(), amount);
-
-            balanceRecordRepo.save(BalanceRecord.builder()
-                    .time(LocalDateTime.now())
-                    .userId(asset.getOwnerId())
-                    .orderId(order.getId())
-                    .amount(amount)
-                    .balance(userBalance.getBalance())
-                    .lastBalance(userBalance.getLastBalance())
-                    .type(BalanceType.SELL)
-                    .build());
-
+            userBalanceService.addBalance(asset.getOwnerId(), amount, id, BalanceType.AUCTION);
 
         }
 
@@ -519,35 +500,56 @@ public class AuctionOrderService {
         auctionOrderRepo.save(auctionOrder);
     }
 
-    public void privilege(AuctionOrder order) {
-        int maxCollection = sysConfigService.getInt("max_collection");
-
-        //Bider特殊拍卖展厅服务
-        Showroom.builder()
-                .userId(order.getUserId())
-                .nickname(order.getNickname())
-                .status(AuthStatus.SUCCESS)
-                .type("AUCTION")
-                .maxCollection(maxCollection)
-                .build();
+    public void privilege(AuctionOrder order, User user) {
+        if (showroomRepo.findByUserIdAndType(order.getUserId(), "AUCTION").isEmpty()) {
+            //Bidder特殊拍卖展厅服务 创建一个bidder展厅藏品
+            Long collectionId = (long) sysConfigService.getInt("bidder_collection_id");
+            List<Asset> assets = assetRepo.findAllByUserIdAndCollectionIdAndStatus(order.getUserId(), collectionId, AssetStatus.NORMAL);
+            Asset asset;
+            if (assets.isEmpty()) {
+                Collection collection = collectionRepo.findById(collectionId).orElseThrow(new BusinessException("无藏品"));
+                if (!CollectionType.SHOWROOM.equals(collection.getType())) {
+                    throw new BusinessException("不是展厅藏品");
+                }
+                //创建资产
+                asset = assetService.createAsset(collection, user, order.getId(), BigDecimal.ZERO, "拍卖赠送", null);
+            } else {
+                asset = assets.get(0);
+            }
+            //创建展厅
+            showroomService.save(asset, "AUCTION");
+        }
 
         //一个月的优先拍卖权
         PurchaserPrivilege.builder()
                 .userId(order.getUserId())
-                .title("元宇宙Bider")
+                .title("元宇宙Bidder")
                 .priority(true)
                 .priorityExpireAt(LocalDateTime.now().plusMonths(1))
                 .build();
 
+
+        //前20名分钱
         BigDecimal totalPrice = order.getTotalPrice();
         //手续费
-        BigDecimal serviceCharge = totalPrice.multiply(new BigDecimal("20"))
+        int auctionServiceCharge = sysConfigService.getInt("auction_service_charge");
+        BigDecimal serviceCharge = totalPrice.multiply(new BigDecimal(auctionServiceCharge))
                 .divide(new BigDecimal("100"), 2, RoundingMode.HALF_UP);
 
         //奖励费用
         BigDecimal subtract = totalPrice.subtract(serviceCharge);
-        subtract.multiply(new BigDecimal("50")).divide(new BigDecimal("100"), 2, RoundingMode.HALF_UP);
+        int auctionReward = sysConfigService.getInt("auction_reward");
+        BigDecimal reward = subtract.multiply(new BigDecimal(auctionReward)).divide(new BigDecimal("100"), 2, RoundingMode.HALF_UP);
+        List<Long> records = auctionRecordRepo.findByAuctionId(order.getAuctionId(), 20);
+        BigDecimal everyReward = reward.divide(new BigDecimal(records.size()), 2, RoundingMode.HALF_UP);
+        //分奖励
+        records.forEach(userId -> userBalanceService.addBalance(userId, everyReward, order.getId(), BalanceType.REWARD));
+
 
+        //拍卖者所得
+        BigDecimal amount = totalPrice.subtract(serviceCharge).subtract(reward);
+        userBalanceService.addBalance(order.getUserId(), amount, order.getId(), BalanceType.AUCTION);
 
     }
+
 }

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

@@ -192,7 +192,7 @@ public class AuctionRecordService {
         save.setId(null);
         save.setUser(SecurityUtils.getAuthenticatedUser().getNickname());
         save.setBidderPrice(amount);
-        save.setType(AuctionRecordType.BIDDER);
+        save.setType(AuctionRecordType.DEPOSIT);
         save = auctionRecordRepo.save(save);
 
         if (record.isPayDeposit()) {

+ 7 - 3
src/main/java/com/izouma/nineth/service/ShowroomService.java

@@ -1,8 +1,8 @@
 package com.izouma.nineth.service;
 
 import cn.hutool.core.collection.CollUtil;
-import com.izouma.nineth.domain.*;
 import com.izouma.nineth.domain.Collection;
+import com.izouma.nineth.domain.*;
 import com.izouma.nineth.dto.PageQuery;
 import com.izouma.nineth.enums.AssetStatus;
 import com.izouma.nineth.enums.AuthStatus;
@@ -108,7 +108,7 @@ public class ShowroomService {
         return show;
     }
 
-    public Showroom save(Asset asset) {
+    public Showroom save(Asset asset, String type) {
         if (!AssetStatus.NORMAL.equals(asset.getStatus())) {
             throw new BusinessException("该状态不可创建展厅");
         }
@@ -128,7 +128,7 @@ public class ShowroomService {
                 .userId(asset.getUserId())
                 .assetId(asset.getId())
                 .nickname(asset.getOwner())
-                .type("USER")
+                .type(type)
                 .status(AuthStatus.SUCCESS)
                 .build();
         showroom = showroomRepo.save(showroom);
@@ -137,6 +137,10 @@ public class ShowroomService {
         return showroom;
     }
 
+    public Showroom save(Asset asset) {
+        return this.save(asset, "USER");
+    }
+
     public Showroom save(Long userId) {
         User user = userRepo.findByIdAndDelFalse(userId).orElseThrow(new BusinessException("无用户"));
         if (!user.isCompany()) {

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

@@ -39,7 +39,6 @@ import java.math.BigDecimal;
 import java.math.RoundingMode;
 import java.time.LocalDate;
 import java.time.LocalDateTime;
-import java.time.LocalTime;
 import java.time.temporal.ChronoUnit;
 import java.util.*;
 import java.util.concurrent.ExecutionException;
@@ -555,4 +554,29 @@ public class UserBalanceService {
                 .type(BalanceType.SELL)
                 .build());
     }
-}
+
+    public void addBalance(Long userId, BigDecimal amount, Long orderId, BalanceType type) {
+        //用户冲余额
+        UserBalance userBalance = userBalanceRepo.findByUserId(userId)
+                .orElse(UserBalance.builder()
+                        .balance(BigDecimal.ZERO)
+                        .lastBalance(BigDecimal.ZERO)
+                        .userId(userId)
+                        .build());
+
+        userBalance.setLastBalance(userBalance.getBalance());
+        userBalance.setBalance(userBalance.getBalance().add(amount));
+        userBalanceRepo.save(userBalance);
+        log.info("拍卖冲用户余额{},¥{}", userId, amount);
+
+        balanceRecordRepo.save(BalanceRecord.builder()
+                .time(LocalDateTime.now())
+                .userId(userId)
+                .orderId(orderId)
+                .amount(amount)
+                .balance(userBalance.getBalance())
+                .lastBalance(userBalance.getLastBalance())
+                .type(type)
+                .build());
+    }
+}