From 6983124000997b16d88b13ee99af339566a69128 Mon Sep 17 00:00:00 2001
From: zyy <zyy@email.com>
Date: Fri, 05 Dec 2025 18:13:13 +0800
Subject: [PATCH] ETF改版

---
 trading-order-service/src/main/java/com/yami/trading/service/dz/impl/StockDzServiceImpl.java |  139 +++++++++++++++++++++++++++++++++++----------
 1 files changed, 107 insertions(+), 32 deletions(-)

diff --git a/trading-order-service/src/main/java/com/yami/trading/service/dz/impl/StockDzServiceImpl.java b/trading-order-service/src/main/java/com/yami/trading/service/dz/impl/StockDzServiceImpl.java
index 87a2721..9c88e38 100644
--- a/trading-order-service/src/main/java/com/yami/trading/service/dz/impl/StockDzServiceImpl.java
+++ b/trading-order-service/src/main/java/com/yami/trading/service/dz/impl/StockDzServiceImpl.java
@@ -24,6 +24,7 @@
 import com.yami.trading.common.domain.Result;
 import com.yami.trading.common.exception.YamiShopBindException;
 import com.yami.trading.common.util.Arith;
+import com.yami.trading.common.util.DateUtils;
 import com.yami.trading.common.util.RandomUtil;
 import com.yami.trading.dao.dz.ExchangeApplyOrderDzMapper;
 import com.yami.trading.dao.dz.StockDzMapper;
@@ -99,9 +100,12 @@
     }
 
     @Override
-    public Result addByAdmin(String stockCode, String stockNum, String password, String startTime, String endTime, String discount,Integer period,String nowPrice,Integer switchType, String stockType) {
-        if (stockCode == null || stockCode.isEmpty() ||stockNum == null || stockNum.isEmpty() || startTime == null || startTime.isEmpty() ||endTime == null || endTime.isEmpty() || nowPrice == null){
+    public Result addByAdmin(String stockCode, String stockNum, String password, String startTime, String endTime, String discount,Integer period,String nowPrice,Integer switchType, String stockType, double dayRate) {
+        if (stockCode == null || stockCode.isEmpty() || startTime == null || startTime.isEmpty() ||endTime == null || endTime.isEmpty() || nowPrice == null){
             return Result.failed("参数不能为空");
+        }
+        if (stockType.equalsIgnoreCase(Item.indices) && dayRate == 0) {
+            return Result.failed("请输入日收益率");
         }
 
         //该类型是否已添加
@@ -125,7 +129,9 @@
         stockDz.setIsLock(0);
         stockDz.setIsShow(1);
         stockDz.setAddTime(new Date());
-        stockDz.setStockNum(Integer.valueOf(stockNum));
+        if (stockNum != null && !stockNum.isEmpty()){
+            stockDz.setStockNum(Integer.valueOf(stockNum));
+        }
         stockDz.setPassword(password);
         stockDz.setStartTime(DateUtil.parseDateTime(startTime));
         stockDz.setEndTime(DateUtil.parseDateTime(endTime));
@@ -133,6 +139,7 @@
         stockDz.setPeriod(period);
         stockDz.setSwitchType(switchType);
         stockDz.setNowPrice(new BigDecimal(nowPrice));
+        stockDz.setDayRate(dayRate);
         if (save(stockDz)) {
             return Result.succeed("添加成功");
         }
@@ -201,15 +208,33 @@
                     } else { //已结算
                         closePrice = dz.getClosePrice();
                     }
-                    //盈亏
-                    double profitLoss = (closePrice - dz.getPrice()) * dz.getSymbolValue();
-                    DecimalFormat df = new DecimalFormat("#.##");
-                    String resultStr = df.format(profitLoss);
-                    dz.setProfitLoss(Double.parseDouble(resultStr));
+                    if (dz.getDayRate() > 0) {
+                        Date startTime = dz.getCreateTime();
+                        // 计算相差天数
+                        int num = com.yami.trading.common.util.DateUtil.dateNum(startTime, new Date());
+                        num = Math.max(1, Math.min(num, dz.getPeriod()));
+                        double dayEarnings = dz.getDayRate() * dz.getVolume();
+                        double profitLoss = dayEarnings * num;
+                        DecimalFormat df = new DecimalFormat("#.##");
+                        String resultStr = df.format(profitLoss);
+                        String resultStr2 = df.format(dayEarnings);
+                        dz.setProfitLoss(Double.parseDouble(resultStr));
+                        dz.setDayEarnings(Double.parseDouble(resultStr2));
 
-                    double profitPercentage = (profitLoss / dz.getVolume()) * 100;
-                    String resultPer = df.format(profitPercentage);
-                    dz.setProfitLossPercentage(Double.parseDouble(resultPer));
+                        double profitPercentage = (profitLoss / dz.getVolume()) * 100;
+                        String resultPer = df.format(profitPercentage);
+                        dz.setProfitLossPercentage(Double.parseDouble(resultPer));
+                    } else {
+                        //盈亏
+                        double profitLoss = (closePrice - dz.getPrice()) * dz.getSymbolValue();
+                        DecimalFormat df = new DecimalFormat("#.##");
+                        String resultStr = df.format(profitLoss);
+                        dz.setProfitLoss(Double.parseDouble(resultStr));
+
+                        double profitPercentage = (profitLoss / dz.getVolume()) * 100;
+                        String resultPer = df.format(profitPercentage);
+                        dz.setProfitLossPercentage(Double.parseDouble(resultPer));
+                    }
                 });
             }
 
@@ -238,7 +263,7 @@
     public Result buyDz(String dzId, String password, double num, String partyId) {
         try {
             if (num <= 0) {
-                throw new YamiShopBindException("请输入正确的货币数量");
+                throw new YamiShopBindException("请输入");
             }
             User party = userService.getById(partyId);
             if (!party.isEnabled()) {
@@ -252,7 +277,6 @@
                     throw new YamiShopBindException("无网络");
                 }
             }
-
 
             StockDz stockDz = this.stockDzMapper.selectOne(new QueryWrapper<StockDz>().eq("uuid", dzId));
             if (stockDz == null) {
@@ -273,8 +297,15 @@
             if (nowPrice.compareTo(new BigDecimal("0")) == 0) {
                 throw new YamiShopBindException("股票价格0,请重试");
             }
-            if (stockDz.getStockNum() > num) {
-                return Result.failed("请购买最小数量");
+            boolean isETF = stockDz.getStockType().equalsIgnoreCase(Item.indices);
+            if (isETF) {
+                if (stockDz.getNowPrice().doubleValue() > num) {
+                    return Result.failed("请输入最小购买金额");
+                }
+            } else {
+                if (stockDz.getStockNum() > num) {
+                    return Result.failed("请购买最小数量");
+                }
             }
             boolean isOpen = MarketOpenChecker.isMarketOpenBuyDz(Item.US_STOCKS);
             if (!isOpen) {
@@ -283,7 +314,12 @@
 
 
             Wallet wallet = this.walletService.saveWalletByPartyId(partyId);
-            BigDecimal buyAmt = nowPrice.multiply(new BigDecimal(num));
+            BigDecimal buyAmt;
+            if (isETF) {
+                buyAmt = new BigDecimal(num);
+            } else {
+                buyAmt = nowPrice.multiply(new BigDecimal(num));
+            }
             //手续费比率
             Double feeRate = sysparaService.find("exchange_apply_order_dz_buy_fee").getDouble();
             BigDecimal orderFree = buyAmt.multiply(BigDecimal.valueOf(feeRate));
@@ -362,16 +398,27 @@
                 exchangeApplyOrderDzMapper.updateById(order);
                 return Result.succeed("操作成功");
             }
-            if (orderNum > order.getSymbolValue()) {
-                throw new YamiShopBindException("输入数量大于用户买入数量");
-            }
 
             StockDz stockDz = this.stockDzMapper.selectOne(new QueryWrapper<StockDz>().eq("uuid", order.getDzId()));
             if (stockDz == null) {
                 throw new YamiShopBindException("大宗不存在");
             }
 
-            BigDecimal nowPrice = stockDz.getNowPrice();
+            BigDecimal nowPrice;
+            boolean isETF = stockDz.getStockType().equalsIgnoreCase(Item.indices);
+            if (isETF) {
+                if (orderNum > order.getPrice()) {
+                    throw new YamiShopBindException("输入金额大于用户买入金额");
+                }
+                nowPrice = new  BigDecimal(orderNum);
+                order.setPrice(nowPrice.doubleValue());
+            } else {
+                if (orderNum > order.getSymbolValue()) {
+                    throw new YamiShopBindException("输入数量大于用户买入数量");
+                }
+                nowPrice = stockDz.getNowPrice();
+            }
+
             if (price > 0) {
                 nowPrice = BigDecimal.valueOf(price);
                 order.setPrice(price);
@@ -381,7 +428,10 @@
             }
 
             Wallet wallet = this.walletService.saveWalletByPartyId(order.getPartyId());
-            BigDecimal buyAmt = nowPrice.multiply(new BigDecimal(orderNum));
+            BigDecimal buyAmt = nowPrice;
+            if (!isETF) {
+                buyAmt = nowPrice.multiply(new BigDecimal(orderNum));
+            }
             //手续费比率
             Double feeRate = sysparaService.find("exchange_apply_order_dz_buy_fee").getDouble();
             BigDecimal orderFree = buyAmt.multiply(BigDecimal.valueOf(feeRate));
@@ -492,7 +542,13 @@
                 throw new YamiShopBindException("大宗不存在");
             }
 
-            BigDecimal nowPrice = stockDz.getNowPrice();
+            BigDecimal nowPrice;
+            boolean isETF = stockDz.getStockType().equalsIgnoreCase(Item.indices);
+            if (isETF) {
+                nowPrice = BigDecimal.valueOf(order.getPrice());
+            } else {
+                nowPrice = stockDz.getNowPrice();
+            }
             if (nowPrice.compareTo(new BigDecimal("0")) == 0) {
                 throw new YamiShopBindException("股票价格0,请重试");
             }
@@ -509,14 +565,24 @@
                     return Result.failed("未到平仓时间");
                 }
             }
+            if (stockDz.getStockName().contains("测试")) {
 
-            boolean isOpen = MarketOpenChecker.isMarketOpenByItemCloseType(Item.US_STOCKS);
-            if (!isOpen) {
-                return Result.failed("当前股市休市");
+            } else {
+                boolean isOpen = MarketOpenChecker.isMarketOpenByItemCloseType(Item.US_STOCKS);
+                if (!isOpen) {
+                    return Result.failed("当前股市休市");
+                }
             }
 
+
+
             Wallet wallet = this.walletService.saveWalletByPartyId(order.getPartyId());
-            BigDecimal sellAmt = nowPrice.multiply(new BigDecimal(num));
+            BigDecimal sellAmt;
+            if (isETF) {
+                sellAmt = nowPrice;
+            } else {
+                sellAmt = nowPrice.multiply(new BigDecimal(num));
+            }
             //手续费比率
             Double feeRate = sysparaService.find("exchange_apply_order_dz_sell_fee").getDouble();
             BigDecimal orderFree = sellAmt.multiply(BigDecimal.valueOf(feeRate));
@@ -524,13 +590,22 @@
                 orderFree = BigDecimal.ONE;
             }
 
-
-            String symbol = stockDz.getStockCode();
-            Realtime realtime = this.dataService.realtime(symbol).get(0);
-            double closePrice = realtime.getClose();
-
             //结算金额
-            double closeAmt = closePrice * num - orderFree.doubleValue();
+            double closeAmt;
+            String symbol = stockDz.getStockCode();
+
+            double closePrice;
+            if (isETF) {
+                //按日收益率结算
+                closeAmt = order.getPrice();
+                closeAmt = closeAmt + stockDz.getDayRate() * closeAmt * stockDz.getPeriod();
+                closePrice = closeAmt;
+                closeAmt = closeAmt - orderFree.doubleValue();
+            } else {
+                Realtime realtime = this.dataService.realtime(symbol).get(0);
+                closePrice = realtime.getClose();
+                closeAmt = closePrice * num - orderFree.doubleValue();
+            }
             if (closeAmt < 0) {
                 return Result.failed("余额不足");
             }

--
Gitblit v1.9.3