From 6eef7f43f9ad3d82727fba36f543f268cfb646d2 Mon Sep 17 00:00:00 2001
From: zyy <zyy@email.com>
Date: Tue, 09 Dec 2025 18:39:30 +0800
Subject: [PATCH] 大宗

---
 trading-order-service/src/main/java/com/yami/trading/service/dz/impl/StockDzServiceImpl.java |  222 +++++++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 172 insertions(+), 50 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 3fc3892..bfebf14 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
@@ -12,6 +12,7 @@
 import com.yami.trading.bean.dz.ExchangeApplyOrderDz;
 import com.yami.trading.bean.dz.StockDz;
 import com.yami.trading.bean.dz.dto.ExchangeApplyOrderDzDto;
+import com.yami.trading.bean.dz.dto.StockDzDto;
 import com.yami.trading.bean.exchange.ExchangeApplyOrder;
 import com.yami.trading.bean.item.domain.Item;
 import com.yami.trading.bean.model.MoneyLog;
@@ -23,9 +24,11 @@
 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;
+import com.yami.trading.service.MarketOpenChecker;
 import com.yami.trading.service.MoneyLogService;
 import com.yami.trading.service.WalletService;
 import com.yami.trading.service.data.DataService;
@@ -42,6 +45,7 @@
 import javax.annotation.Resource;
 import javax.servlet.http.HttpServletRequest;
 import java.math.BigDecimal;
+import java.math.RoundingMode;
 import java.text.DecimalFormat;
 import java.util.*;
 
@@ -80,8 +84,14 @@
     public Result getDzList(int pageNum, int pageSize, String orderBy, String keyWords, String stockType) {
         try {
             String formatDate = DateUtil.format(new Date(), DatePattern.NORM_DATETIME_PATTERN);
-            Page page = new Page(pageNum, pageSize);
-            stockDzMapper.findStockTypeDz(page,orderBy,keyWords,formatDate, stockType);
+            Page<StockDzDto> page = new Page<>(pageNum, pageSize);
+            page = stockDzMapper.findStockTypeDz(page,orderBy,keyWords,formatDate, stockType);
+            page.getRecords().forEach(stockDzDto -> {
+                Realtime realtime = this.dataService.realtime(stockDzDto.getStockCode()).get(0);
+                if (realtime != null) {
+                    stockDzDto.setCurrentPrice(realtime.getClose());
+                }
+            });
             return Result.succeed(page);
         } catch (Exception e) {
             log.error(e.getMessage());
@@ -90,13 +100,18 @@
     }
 
     @Override
-    public Result addByAdmin(String stockCode, String stockNum, String password, String startTime, String endTime, String discount,Integer period,String nowPrice,Integer switchType) {
-        if (stockCode == null || stockCode.isEmpty() ||stockNum == null || stockNum.isEmpty() || startTime == null || startTime.isEmpty() ||endTime == null || endTime.isEmpty() || period == null || 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("请输入日收益率");
+        }
 
+        //该类型是否已添加
         Long count = stockDzMapper.selectCount(new LambdaQueryWrapper<StockDz>()
-                .eq(StockDz::getStockCode,stockCode));
+                .eq(StockDz::getStockCode,stockCode)
+                .eq(StockDz::getStockType,stockType));
         if(count > 0){
             return Result.failed("股票代码已存在");
         }
@@ -109,12 +124,14 @@
         StockDz stockDz = new StockDz();
         stockDz.setStockName(item.getSymbolFullName());
         stockDz.setStockCode(item.getSymbol());
-        stockDz.setStockType(item.getType());
+        stockDz.setStockType(stockType);
         stockDz.setStockSpell(item.getSymbolData());
         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));
@@ -122,6 +139,7 @@
         stockDz.setPeriod(period);
         stockDz.setSwitchType(switchType);
         stockDz.setNowPrice(new BigDecimal(nowPrice));
+        stockDz.setDayRate(dayRate);
         if (save(stockDz)) {
             return Result.succeed("添加成功");
         }
@@ -158,9 +176,9 @@
         if(stockDz == null){
             return Result.failed("不存在该大宗信息");
         }
-        model.setStockName(item.getSymbolFullName());
+        //model.setStockName(item.getSymbolFullName());
         model.setStockCode(item.getSymbol());
-        model.setStockType(item.getType());
+        model.setStockType(stockDz.getStockType());
         model.setStockSpell(item.getSymbolData());
         return stockDzMapper.updateById(model) > 0 ? Result.succeed("修改成功") : Result.failed("修改失败");
     }
@@ -190,11 +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));
+                    } 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));
+                    }
                 });
             }
 
@@ -206,10 +246,11 @@
     }
 
     @Override
-    public Result getDzCheckList(int pageNum, int pageSize, String state, String stockCode, String stockType) {
+    public Result getDzCheckList(int pageNum, int pageSize, String state, String stockCode, String stockType, List<String> checkedList) {
         try {
             Page page = new Page(pageNum, pageSize);
-            stockDzMapper.getDzCheckList(page ,state, stockCode, stockType);
+
+            stockDzMapper.getDzCheckList(page ,state, stockCode, stockType, checkedList);
             return Result.succeed(page);
         } catch (Exception e) {
             log.error(e.getMessage());
@@ -219,10 +260,10 @@
 
     @Transactional
     @Override
-    public Result buyDz(String dzId, String password, double num, String partyId) {
+    public Result buyDz(String dzId, String password, double num, String partyId, Boolean isAdmin) {
         try {
             if (num <= 0) {
-                throw new YamiShopBindException("请输入正确的货币数量");
+                throw new YamiShopBindException("请输入");
             }
             User party = userService.getById(partyId);
             if (!party.isEnabled()) {
@@ -237,7 +278,6 @@
                 }
             }
 
-
             StockDz stockDz = this.stockDzMapper.selectOne(new QueryWrapper<StockDz>().eq("uuid", dzId));
             if (stockDz == null) {
                 throw new YamiShopBindException("大宗不存在");
@@ -250,25 +290,47 @@
             }
 
             if(stockDz.getStartTime().getTime() > new Date().getTime() || stockDz.getEndTime().getTime() < new Date().getTime()){
-                throw new YamiShopBindException("不在内幕交易时间之内");
+                return Result.failed("不在内幕交易时间之内");
             }
             BigDecimal nowPrice = stockDz.getNowPrice();
 
             if (nowPrice.compareTo(new BigDecimal("0")) == 0) {
                 throw new YamiShopBindException("股票价格0,请重试");
             }
-            if (stockDz.getStockNum() > num) {
-                throw new YamiShopBindException("最小购买数量" + stockDz.getStockNum());
+            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("请购买最小数量");
+                }
+            }
+
+            if (!isAdmin) { //后台买入不判断
+                boolean isOpen = MarketOpenChecker.isMarketOpenBuyDz(Item.US_STOCKS);
+                if (!isOpen) {
+                    return Result.failed("当前股市休市");
+                }
             }
 
             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_buy_fee").getDouble();
+            Double feeRate = sysparaService.find("exchange_apply_order_dz_buy_fee").getDouble();
             BigDecimal orderFree = buyAmt.multiply(BigDecimal.valueOf(feeRate));
+            if (buyAmt.doubleValue() < 300) {
+                orderFree = BigDecimal.ONE;
+            }
             BigDecimal orderAmt = buyAmt.add(orderFree);
             if (wallet.getMoney().compareTo(orderAmt) < 0) {
-                throw new YamiShopBindException("订单失败,资金不足");
+                return Result.failed("余额不足");
             }
 
             BigDecimal amountBefore = wallet.getMoney();
@@ -317,13 +379,13 @@
             return Result.succeed("购买成功");
         } catch (Exception e) {
             log.error(e.getMessage());
-            return Result.failed("操作失败:" + e.getMessage());
+            return Result.failed("失败");
         }
     }
 
     @Transactional
     @Override
-    public Result dzCheck(String id, Integer checkType, Double orderNum) {
+    public Result dzCheck(String id, Integer checkType, Double orderNum, double price) {
         try {
             ExchangeApplyOrderDz order = exchangeApplyOrderDzMapper.selectById(id);
             if (order == null) {
@@ -338,25 +400,48 @@
                 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);
+            }
             if (nowPrice.compareTo(new BigDecimal("0")) == 0) {
                 throw new YamiShopBindException("股票价格0,请重试");
             }
 
             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));
+            if (buyAmt.doubleValue() < 300) {
+                orderFree = BigDecimal.ONE;
+            }
+
+
             BigDecimal orderAmt = buyAmt.add(orderFree);
             if (wallet.getMoney().compareTo(orderAmt) < 0) {
                 throw new YamiShopBindException("订单失败,资金不足");
@@ -368,6 +453,7 @@
             order.setVolume(buyAmt.doubleValue());
             order.setFee(orderFree.doubleValue());
             order.setState(ExchangeApplyOrderDz.STATE_POSITION);
+            order.setCreateTime(new Date());
             exchangeApplyOrderDzMapper.updateById(order);
             walletService.update(wallet.getUserId(), Arith.sub(0, orderAmt.doubleValue()));
 
@@ -437,7 +523,7 @@
 
     @Transactional
     @Override
-    public Result closeDz(String id, Double num, String partyId) {
+    public Result closeDz(String id, Double num, String partyId, Boolean isAdmin) {
         try {
             ExchangeApplyOrderDz order = exchangeApplyOrderDzMapper.selectById(id);
             if (order == null) {
@@ -452,44 +538,80 @@
             if (num < 0 || num > order.getSymbolValue()) {
                 throw new YamiShopBindException("数量错误");
             }
+            if (partyId == null) {
+                partyId = order.getPartyId();
+            }
 
             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) {
+                nowPrice = BigDecimal.valueOf(order.getPrice());
+            } else {
+                nowPrice = stockDz.getNowPrice();
+            }
             if (nowPrice.compareTo(new BigDecimal("0")) == 0) {
                 throw new YamiShopBindException("股票价格0,请重试");
             }
 
             Date now = new Date();
-            if (stockDz.getPeriod() > 0) {
-                Calendar calendar = Calendar.getInstance();
-                calendar.setTime(order.getCreateTime());
-                calendar.add(Calendar.DATE, stockDz.getPeriod());
-                // 锁仓时间
-                Date resultTime = calendar.getTime();
+            if (!isAdmin) {
+                if (stockDz.getPeriod() != null && stockDz.getPeriod() > 0) {
+                    Calendar calendar = Calendar.getInstance();
+                    calendar.setTime(order.getCreateTime());
+                    calendar.add(Calendar.DATE, stockDz.getPeriod());
+                    // 锁仓时间
+                    Date resultTime = calendar.getTime();
 
-                if(now.getTime() < resultTime.getTime()){
-                    throw new YamiShopBindException("Lock-up period:" + stockDz.getPeriod());
+                    if(now.getTime() < resultTime.getTime()){
+                        return Result.failed("未到平仓时间");
+                    }
+                }
+                if (!stockDz.getStockName().contains("测试")) {
+                    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));
-
-            String symbol = stockDz.getStockCode();
-            Realtime realtime = this.dataService.realtime(symbol).get(0);
-            double closePrice = realtime.getClose();
+            if (sellAmt.doubleValue() < 300) {
+                orderFree = BigDecimal.ONE;
+            }
 
             //结算金额
-            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) {
-                throw new YamiShopBindException("手续费资金不足");
+                return Result.failed("余额不足");
             }
 
             //拆分订单
@@ -537,7 +659,7 @@
             return Result.succeed("平仓成功");
         } catch (Exception e) {
             log.error(e.getMessage());
-            return Result.failed("操作失败:" + e.getMessage());
+            return Result.failed("失败");
         }
     }
 

--
Gitblit v1.9.3