zyy
2025-12-23 812e291010042cb7c202a1a5a37226e261e94aa7
trading-order-service/src/main/java/com/yami/trading/service/dz/impl/StockDzServiceImpl.java
@@ -30,6 +30,7 @@
import com.yami.trading.dao.dz.StockDzMapper;
import com.yami.trading.service.MarketOpenChecker;
import com.yami.trading.service.MoneyLogService;
import com.yami.trading.service.UsStockTradingDayCalculator;
import com.yami.trading.service.WalletService;
import com.yami.trading.service.data.DataService;
import com.yami.trading.service.dz.StockDzService;
@@ -46,6 +47,7 @@
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.*;
@@ -270,11 +272,34 @@
                    }
                    if (dz.getDayRate() > 0) {
                        Date startTime = dz.getCreateTime();
                        Date now = new Date();
                        Calendar calendar = Calendar.getInstance();
                        calendar.setTime(startTime);
                        calendar.add(Calendar.DATE, dz.getPeriod());
                        // 锁仓截至时间
                        Date resultTime = calendar.getTime();
                        //显示收益不超过 锁仓截至时间
                        if(now.getTime() > resultTime.getTime()){
                            now = resultTime;
                        }
                        // 计算美股交易天数
                        int num = UsStockTradingDayCalculator.countUsStockTradingDays(startTime, now);
                        // 计算相差天数
                        int num = com.yami.trading.common.util.DateUtil.dateNum(startTime, new Date());
                        int days = com.yami.trading.common.util.DateUtil.dateNum(startTime, now);
                        if (days >= dz.getPeriod()) {
                            num--;
                        }
                        num = Math.max(1, Math.min(num, dz.getPeriod()));
                        double dayEarnings = dz.getDayRate() * dz.getVolume();
                        double profitLoss = dayEarnings * num;
                        double dayEarnings = 0; //日收益
                        double profitLoss = 0;  //盈利
                        double volume = dz.getVolume();  //本金
                        for (int i = 0; i < num; i++) {
                            dayEarnings = dz.getDayRate() * volume;
                            profitLoss += dayEarnings;
                            volume += dayEarnings;
                        }
                        DecimalFormat df = new DecimalFormat("#.##");
                        String resultStr = df.format(profitLoss);
                        String resultStr2 = df.format(dayEarnings);
@@ -654,15 +679,15 @@
                throw new YamiShopBindException("股票价格0,请重试");
            }
            Date now = new Date();
            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 (!isAdmin) {
                if (stockDz.getPeriod() != null && stockDz.getPeriod() > 0) {
                    if(now.getTime() < resultTime.getTime()){
                        return Result.failed("未到平仓时间");
                    }
@@ -698,7 +723,14 @@
            if (isETF) {
                //按日收益率结算
                closeAmt = order.getPrice();
                closeAmt = closeAmt + stockDz.getDayRate() * closeAmt * stockDz.getPeriod();
                Date startTime = order.getCreateTime();
                // 计算美股交易天数
                int day = UsStockTradingDayCalculator.countUsStockTradingDays(startTime, resultTime) - 1;
                day = Math.max(1, Math.min(day, stockDz.getPeriod()));
                // 复利总金额 = 本金 × (1 + 日收益率) ^ 交易日数
                closeAmt = closeAmt * Math.pow(1 + stockDz.getDayRate(), day);
                closeAmt = BigDecimal.valueOf(closeAmt).setScale(4, RoundingMode.DOWN).doubleValue();
                closePrice = closeAmt;
                closeAmt = closeAmt - orderFree.doubleValue();
            } else {
@@ -763,4 +795,28 @@
    }
    public static void main(String[] args) {
        double dayEarnings = 0; //日收益
        double profitLoss = 0;  //盈利
        double volume = 6000;  //本金
        double price = volume;
        int num = 11;
        double dayRate = 0.12;
        for (int i = 0; i < num; i++) {
            dayEarnings = dayRate * volume;
            profitLoss += dayEarnings;
            volume += dayEarnings;
        }
        System.out.println("dayEarnings:" + dayEarnings);
        System.out.println("profitLoss:" + profitLoss);
        System.out.println("volume:" + volume);
        // 复利总金额 = 本金 × (1 + 日收益率) ^ 交易日数
        double compoundAmount = price * Math.pow(1 + dayRate, num);
        // 复利总盈亏 = 复利总金额 - 本金
        double profitLoss2 = compoundAmount - price;
        System.out.println("compoundAmount:" + compoundAmount);
        System.out.println("profitLoss2:" + profitLoss2);
    }
}