From 10a06cf876b3dfce68d36ee71cba0cb4fa393b94 Mon Sep 17 00:00:00 2001
From: zyy <zyy@email.com>
Date: Thu, 04 Sep 2025 16:11:21 +0800
Subject: [PATCH] 计算金额(待补金额)

---
 trading-order-service/src/main/java/com/yami/trading/service/ico/IcoService.java |  175 +++++++++++++++++++++++++++-------------------------------
 1 files changed, 82 insertions(+), 93 deletions(-)

diff --git a/trading-order-service/src/main/java/com/yami/trading/service/ico/IcoService.java b/trading-order-service/src/main/java/com/yami/trading/service/ico/IcoService.java
index 6fd54b0..91ffe36 100644
--- a/trading-order-service/src/main/java/com/yami/trading/service/ico/IcoService.java
+++ b/trading-order-service/src/main/java/com/yami/trading/service/ico/IcoService.java
@@ -53,78 +53,75 @@
      * 新币申购
      */
     public Result<String> subscribe(UserSubscription model) {
-        try {
-            if (model == null || model.getIcoProjectId() == null) {
-                throw new YamiShopBindException("参数异常");
-            }
-            String partyId = model.getUserId();
-            Ico ico = this.getById(model.getIcoProjectId());
-            if (ico == null) {
-                throw new YamiShopBindException("新币不存在");
-            }
-
-            User party = userService.getById(partyId);
-            if (!party.isEnabled()) {
-                return Result.succeed("User is locked");
-            }
-            if (Constants.SECURITY_ROLE_TEST.equals(party.getRoleName())) {
-                throw new YamiShopBindException("无权限");
-            }
-            if (!C2cLock.add(partyId)) {
-                throw new YamiShopBindException("Please try again later");
-            }
-
-            if (model.getSubscribeNums() == null || model.getSubscribeNums() == 0) {
-                throw new YamiShopBindException("申请数量不能为空");
-            }
-            //购买金额
-            BigDecimal amount = ico.getUnitAmount().multiply(new BigDecimal(model.getSubscribeNums()));
-            if(amount.compareTo(ico.getMinContribution()) < 0 ){
-                throw new YamiShopBindException("最低投资额: " + ico.getMinContribution());
-            }
-            if(amount.compareTo(ico.getMaxContribution()) > 0 ){
-                throw new YamiShopBindException("最高投资额: " + ico.getMaxContribution());
-            }
-            Date currentDate = new Date();
-            if(currentDate.before(ico.getStartDate())){
-                throw new YamiShopBindException("未开售");
-            }
-            if(currentDate.after(ico.getEndDate())){
-                throw new YamiShopBindException("已结束");
-            }
-
-            model.setStatus(1);
-            model.setUserId(partyId);
-            if (model.getSubscriptionType() == null) { //默认自主申购
-                model.setSubscriptionType(1);
-            }
-
-            //需要先支付
-            if (ico.getIsPayDown() !=null && ico.getIsPayDown() == 1) {
-                Wallet wallet = walletService.saveWalletByPartyId(partyId);
-                if (amount.compareTo(wallet.getMoney()) > 0) {
-                    throw new YamiShopBindException("余额不足");
-                }
-                walletService.update(partyId, Arith.sub(0, amount.doubleValue()));
-
-                // 保存 资金日志
-                MoneyLog moneylog = new MoneyLog();
-                moneylog.setCategory(Constants.MONEYLOG_CONTENT_NEW_COIN);
-                moneylog.setAmountBefore(wallet.getMoney());
-                moneylog.setAmount(amount);
-                moneylog.setAmountAfter(BigDecimal.valueOf(Arith.sub(wallet.getMoney().doubleValue(), amount.doubleValue())));
-                moneylog.setLog("新币购买,申购金额[" + amount + "]");
-                moneylog.setUserId(partyId);
-                moneylog.setSymbol(ico.getSymbol());
-                moneylog.setWalletType(ico.getSymbol());
-                moneylog.setContentType(Constants.MONEYLOG_CONTENT_NEW_COIN_BUY);
-                moneyLogService.save(moneylog);
-            }
-            userSubscriptionService.save(model);
-            return Result.ok("申请成功" );
-        } catch (Exception e) {
-            throw new RuntimeException(e);
+        if (model == null || model.getIcoProjectId() == null) {
+            throw new YamiShopBindException("参数异常");
         }
+        String partyId = model.getUserId();
+        Ico ico = this.getById(model.getIcoProjectId());
+        if (ico == null) {
+            throw new YamiShopBindException("新币不存在");
+        }
+
+        User party = userService.getById(partyId);
+        if (!party.isEnabled()) {
+            throw new YamiShopBindException("User is locked");
+        }
+        if (Constants.SECURITY_ROLE_TEST.equals(party.getRoleName())) {
+            throw new YamiShopBindException("无权限");
+        }
+
+        if (model.getSubscribeNums() == null || model.getSubscribeNums() == 0) {
+            throw new YamiShopBindException("申请数量不能为空");
+        }
+        //购买金额
+        BigDecimal amount = ico.getUnitAmount().multiply(new BigDecimal(model.getSubscribeNums()));
+        if(amount.compareTo(ico.getMinContribution()) < 0 ){
+            throw new YamiShopBindException("最低投资额: " + ico.getMinContribution());
+        }
+        if(amount.compareTo(ico.getMaxContribution()) > 0 ){
+            throw new YamiShopBindException("最高投资额: " + ico.getMaxContribution());
+        }
+        Date currentDate = new Date();
+        if(currentDate.before(ico.getStartDate())){
+            throw new YamiShopBindException("未开售");
+        }
+        if(currentDate.after(ico.getEndDate())){
+            throw new YamiShopBindException("已结束");
+        }
+
+        model.setStatus(1);
+        model.setUserId(partyId);
+        if (model.getSubscriptionType() == null) { //默认自主申购
+            model.setSubscriptionType(1);
+        }
+
+        Wallet wallet = walletService.saveWalletByPartyId(partyId);
+        if(wallet.getAmountToBeCovered().compareTo(BigDecimal.ZERO) > 0){
+            throw new YamiShopBindException("请先缴清待补资金");
+        }
+
+        //需要先支付
+        if (ico.getIsPayDown() !=null && ico.getIsPayDown() == 1) {
+            if (amount.compareTo(wallet.getMoney()) > 0) {
+                throw new YamiShopBindException("余额不足");
+            }
+            walletService.update(partyId, Arith.sub(0, amount.doubleValue()));
+
+            // 保存 资金日志
+            MoneyLog moneylog = new MoneyLog();
+            moneylog.setCategory(Constants.MONEYLOG_CONTENT_NEW_COIN);
+            moneylog.setAmountBefore(wallet.getMoney());
+            moneylog.setAmount(amount);
+            moneylog.setAmountAfter(BigDecimal.valueOf(Arith.sub(wallet.getMoney().doubleValue(), amount.doubleValue())));
+            moneylog.setLog("新币购买,申购金额[" + amount + "]");
+            moneylog.setUserId(partyId);
+            moneylog.setSymbol(ico.getSymbol());
+            moneylog.setWalletType(ico.getSymbol());
+            moneylog.setContentType(Constants.MONEYLOG_CONTENT_NEW_COIN_BUY);
+            moneyLogService.save(moneylog);
+        }
+        userSubscriptionService.save(model);
+        return Result.ok("申请成功" );
     }
 
 
@@ -146,7 +143,7 @@
             if(userSubscription.getStatus() == 3){
                 throw new YamiShopBindException("不能更改申购状态");
             }
-            Ico ico = this.getById(model.getIcoProjectId());
+            Ico ico = this.getById(userSubscription.getIcoProjectId());
             String userId = userSubscription.getUserId();
             if (model.getStatus() == 3 || model.getStatus() == 5) {
                 if(model.getBallotNumber() == null || model.getBallotNumber() == 0){
@@ -161,21 +158,22 @@
             if (model.getStatus() == 3) { //中签
                 //资金账户
                 Wallet wallet = walletService.saveWalletByPartyId(userId);
-                int applyNumber = userSubscription.getSubscribeNums() - model.getBallotNumber();
-                BigDecimal amount = ico.getUnitAmount().multiply(new BigDecimal(applyNumber));
+                BigDecimal before = wallet.getMoney();
                 //已经预支付
                 if(ico.getIsPayDown() !=null && ico.getIsPayDown() == 1) {
+                    int applyNumber = userSubscription.getSubscribeNums() - model.getBallotNumber();
+                    BigDecimal amount = ico.getUnitAmount().multiply(new BigDecimal(applyNumber));
                     if(applyNumber > 0) {
                         //退回资金
                         BigDecimal refundPrice = ico.getUnitAmount().multiply(new BigDecimal(applyNumber));
-                        walletService.update(userId, refundPrice.doubleValue());
+                        wallet = walletService.updateToBeCovered(wallet, refundPrice, 1);
 
                         //保存 资金日志
                         MoneyLog moneylog = new MoneyLog();
                         moneylog.setCategory(Constants.MONEYLOG_CONTENT_NEW_COIN);
-                        moneylog.setAmountBefore(wallet.getMoney());
+                        moneylog.setAmountBefore(before);
                         moneylog.setAmount(amount);
-                        moneylog.setAmountAfter(wallet.getMoney().add(refundPrice));
+                        moneylog.setAmountAfter(wallet.getMoney());
                         moneylog.setLog("新币购买,退回购买金额[" + refundPrice + "]");
                         moneylog.setUserId(userId);
                         moneylog.setSymbol(ico.getSymbol());
@@ -184,24 +182,15 @@
                         moneyLogService.save(moneylog);
                     }
                 } else {
-                    BigDecimal subtract = amount.subtract(wallet.getMoney());
-                    if (subtract.compareTo(BigDecimal.ZERO) > 0) {
-                        //放入待补
-                        wallet.setMoney(BigDecimal.ZERO);
-                        wallet.setAmountToBeCovered(subtract);
-                        if (!walletService.updateById(wallet)) {
-                            throw new YamiShopBindException("操作钱包失败!");
-                        }
-                    } else {
-                        //扣除资金
-                        walletService.update(userId, amount.negate().doubleValue());
-                    }
+                    BigDecimal amount = ico.getUnitAmount().multiply(new BigDecimal(model.getBallotNumber()));
+                    wallet = walletService.updateToBeCovered(wallet, amount, 2);
+
                     //保存 资金日志
                     MoneyLog moneylog = new MoneyLog();
                     moneylog.setCategory(Constants.MONEYLOG_CONTENT_NEW_COIN);
-                    moneylog.setAmountBefore(wallet.getMoney());
+                    moneylog.setAmountBefore(before);
                     moneylog.setAmount(amount);
-                    moneylog.setAmountAfter(subtract.compareTo(BigDecimal.ZERO) > 0 ? BigDecimal.ZERO : subtract.negate());
+                    moneylog.setAmountAfter(wallet.getMoney());
                     moneylog.setLog("新币购买,申购金额[" + amount + "]");
                     moneylog.setUserId(userId);
                     moneylog.setSymbol(ico.getSymbol());
@@ -216,7 +205,7 @@
                 WalletExtend walletExtend = walletService.saveExtendByPara(userId, ico.getSymbol());
                 walletService.updateExtend(userId, walletExtend.getWallettype(), model.getBallotNumber());
             }
-            userSubscriptionService.save(userSubscription);
+            userSubscriptionService.saveOrUpdate(userSubscription);
             return Result.ok ( "操作成功" );
         } catch (Exception e) {
             throw new RuntimeException(e);
@@ -242,7 +231,7 @@
         item.setShowStatus("1");
         item.setTradeStatus("1");
         item.setQuoteCurrency(ico.getCurrency());
-        item.setCurrencyType(0);
+        item.setCurrencyType(1);  //新币
         item.setStatus(0);
         item.setTradeType(ico.getIsContractTrading().toString());
         return item;

--
Gitblit v1.9.3