1
zj
2 days ago d615fc515fc52d6ed970c11d59a017e48de4be32
src/main/java/com/nq/service/impl/UserPositionServiceImpl.java
@@ -11,6 +11,9 @@
import com.google.common.collect.Lists;
import com.nq.common.ServerResponse;
import com.nq.utils.*;
import com.nq.utils.StayFeeUtil;
import com.nq.utils.TradeFeeUtil;
import com.nq.utils.redis.JsonUtil;
import com.nq.utils.redis.RedisShardedPoolUtils;
import com.nq.utils.stock.BuyAndSellUtils;
import com.nq.utils.stock.GeneratePosition;
@@ -111,6 +114,7 @@
    StockDzMapper stockDzMapper;
    @Override
    @Transactional
    public ServerResponse buy(Integer stockId, Integer buyNum, Integer buyType, Integer lever, BigDecimal profitTarget, BigDecimal stopTarget, HttpServletRequest request) throws Exception {
@@ -178,7 +182,7 @@
            if (!am_flag && !pm_flag) {
                return ServerResponse.createByErrorMsg("下单失败,不在港股股交易时段内");
            }
            if (siteProduct.getHolidayDisplay()) {
            if (TradingDayUtil.shouldBlockTradingToday(siteProduct)) {
                return ServerResponse.createByErrorMsg("周末或节假日不能交易!");
            }
        } else {
@@ -193,7 +197,7 @@
            if (!am_flag && !pm_flag) {
                return ServerResponse.createByErrorMsg("下单失败,不在交易时段内");
            }
            if (siteProduct.getHolidayDisplay()) {
            if (TradingDayUtil.shouldBlockTradingToday(siteProduct)) {
                return ServerResponse.createByErrorMsg("周末或节假日不能交易!");
            }
        }
@@ -296,10 +300,10 @@
        BigDecimal ztRate = chaPrice.multiply(new BigDecimal("100")).divide(zsPrice, 2, 4);
        log.info("当前涨跌幅 = {} % , 涨停幅度 = {} %", Double.valueOf(stock_crease), ztRate);
        if ((new BigDecimal(String.valueOf(stock_crease))).compareTo(ztRate) == 0 && buyType
                .intValue() == 0) {
            return ServerResponse.createByErrorMsg("当前股票已涨停不能买涨");
        }
//        if ((new BigDecimal(String.valueOf(stock_crease))).compareTo(ztRate) == 0 && buyType
//                .intValue() == 0) {
//            return ServerResponse.createByErrorMsg("当前股票已涨停不能买涨");
//        }
        if (stock.getStockPlate() == null || StringUtils.isEmpty(stock.getStockPlate())) {
@@ -385,11 +389,12 @@
        }
        int compareUserAmtInt = user_enable_amt.compareTo(buy_amt_autual);
        log.info("用户可用金额 = {}  实际购买金额 =  {}", user_enable_amt, buy_amt_autual);
        log.info("比较 用户金额 和 实际 购买金额 =  {}", Integer.valueOf(compareUserAmtInt));
        BigDecimal buy_fee_amt = TradeFeeUtil.calcBuyFee(buy_amt_autual, siteSetting.getBuyFee());
        BigDecimal buy_debit = TradeFeeUtil.calcBuyDebit(buy_amt_autual, buy_fee_amt);
        int compareUserAmtInt = user_enable_amt.compareTo(buy_debit);
        log.info("用户可用金额 = {}  下单扣款(保证金+手续费) =  {}", user_enable_amt, buy_debit);
        if (compareUserAmtInt == -1) {
            return ServerResponse.createByErrorMsg("下单失败,融资可用金额小于" + buy_amt_autual + "元");
            return ServerResponse.createByErrorMsg("下单失败,融资可用金额小于" + buy_debit + "元(含保证金及手续费)");
        }
        if (user.getUserIndexAmt().compareTo(new BigDecimal("0")) == -1) {
@@ -445,7 +450,6 @@
        userPosition.setOrderStayDays(1);
        BigDecimal buy_fee_amt = buy_amt.multiply(siteSetting.getBuyFee()).setScale(2, 4);
        log.info("用户购买手续费(配资后总资金 * 百分比) = {}", buy_fee_amt);
        userPosition.setOrderFee(buy_fee_amt);
@@ -476,34 +480,13 @@
        userPosition.setOrderStayDays(Integer.valueOf(0));
        userPosition.setOrderStayFee(new BigDecimal("0"));
        userPosition.setStatus(1);
        int insertPositionCount = 0;
        this.userPositionMapper.insert(userPosition);
        insertPositionCount = userPosition.getId();
        if (insertPositionCount > 0) {
            //修改用户可用余额= 当前余额-下单金额-买入手续费-印花税-点差费
            //BigDecimal reckon_enable = user_enable_amt.subtract(buy_amt_autual).subtract(buy_fee_amt).subtract(buy_yhs_amt).subtract(spread_rate_amt);
            //修改用户可用余额= 当前余额-下单总金额
            BigDecimal reckon_enable = user_enable_amt.subtract(buy_amt_autual);
            //修改用户可取余额=当前可取余额-下单总金额
            int compareUserWithdrawAmtInt = user_enable_withdraw_amt.compareTo(buy_amt_autual);
            if (compareUserWithdrawAmtInt == -1) {
                //若可取余额小于下单总额,但是可用余额充足,令可取余额为0
                user.setEnaleWithdrawAmt(BigDecimal.ZERO);
            } else {
                user_enable_withdraw_amt = user_enable_withdraw_amt.subtract(buy_amt_autual);
                user.setEnaleWithdrawAmt(user_enable_withdraw_amt);
            }
            user.setEnableAmt(reckon_enable);
            int updateUserCount = this.userMapper.updateByPrimaryKeySelective(user);
            if (updateUserCount > 0) {
                log.info("【用户交易下单】修改用户金额成功");
            } else {
                log.error("用户交易下单】修改用户金额出错");
                throw new Exception("用户交易下单】修改用户金额出错");
            }
            //核算代理收入-入仓手续费
        if (userPosition.getId() != null && userPosition.getId() > 0) {
            deductUserEnableOnBuy(user, buy_debit, buy_amt_autual, buy_fee_amt, userPosition);
            iAgentAgencyFeeService.AgencyFeeIncome(1, userPosition.getPositionSn());
            syncUserCacheAfterTrade(request);
            log.info("【用户交易下单】保存持仓记录成功");
        } else {
            log.error("用户交易下单】保存持仓记录出错");
@@ -514,12 +497,119 @@
    }
    public ServerResponse fee(Integer buyNum,BigDecimal nowPrice){
        BigDecimal buy_amt = nowPrice.multiply(new BigDecimal(buyNum.intValue()));
    @Override
    public ServerResponse fee(Integer buyNum, BigDecimal nowPrice, Integer lever) {
        int leverValue = (lever == null || lever <= 0) ? 1 : lever;
        SiteSetting siteSetting = this.iSiteSettingService.getSiteSetting();
        BigDecimal buy_fee_amt = buy_amt.multiply(siteSetting.getBuyFee()).setScale(2, 4);
        BigDecimal buy_amt = nowPrice.multiply(new BigDecimal(buyNum.intValue()));
        BigDecimal buy_fee_amt = TradeFeeUtil.calcBuyFeeByNotional(buy_amt, leverValue,
                siteSetting != null ? siteSetting.getBuyFee() : null).setScale(2, RoundingMode.HALF_UP);
        return ServerResponse.createBySuccess(buy_fee_amt);
    }
    /** T+0:可用资金变动时同步调整可取资金 */
    private void applyEnableAndWithdrawChange(User user, BigDecimal enableBefore, BigDecimal enableAfter) {
        UserFundUtil.applyEnableDelta(user, enableBefore, enableAfter);
    }
    /** 下单从两融可用资金扣除:保证金 + 买入手续费 */
    private void deductUserEnableOnBuy(User user, BigDecimal buyDebit, BigDecimal margin, BigDecimal fee,
                                       UserPosition position) throws Exception {
        User fresh = this.userMapper.selectByPrimaryKey(user.getId());
        if (fresh == null) {
            throw new Exception("用户不存在");
        }
        BigDecimal enableAmt = fresh.getEnableAmt() == null ? BigDecimal.ZERO : fresh.getEnableAmt();
        if (enableAmt.compareTo(buyDebit) < 0) {
            throw new Exception("扣除可用资金失败(保证金+手续费=" + buyDebit + "元),余额不足");
        }
        fresh.setEnableAmt(enableAmt.subtract(buyDebit));
        BigDecimal withdrawAmt = fresh.getEnaleWithdrawAmt() == null ? BigDecimal.ZERO : fresh.getEnaleWithdrawAmt();
        if (withdrawAmt.compareTo(buyDebit) < 0) {
            fresh.setEnaleWithdrawAmt(BigDecimal.ZERO);
        } else {
            fresh.setEnaleWithdrawAmt(withdrawAmt.subtract(buyDebit));
        }
        BigDecimal userAmt = fresh.getUserAmt() == null ? BigDecimal.ZERO : fresh.getUserAmt();
        fresh.setUserAmt(userAmt.subtract(buyDebit));
        int rows = this.userMapper.updateByPrimaryKeySelective(fresh);
        if (rows <= 0) {
            throw new Exception("扣除可用资金失败(保证金+手续费=" + buyDebit + "元)");
        }
        saveBuyDebitCashDetail(user, position, margin, fee, buyDebit);
        log.info("【用户交易下单】扣款成功,用户={},保证金={},手续费={},合计={}", user.getId(), margin, fee, buyDebit);
    }
    private void refundUserEnableOnCancel(User user, BigDecimal refundAmt, UserPosition position) throws Exception {
        User fresh = this.userMapper.selectByPrimaryKey(user.getId());
        if (fresh == null) {
            throw new Exception("用户不存在");
        }
        BigDecimal enableAmt = fresh.getEnableAmt() == null ? BigDecimal.ZERO : fresh.getEnableAmt();
        BigDecimal withdrawAmt = fresh.getEnaleWithdrawAmt() == null ? BigDecimal.ZERO : fresh.getEnaleWithdrawAmt();
        BigDecimal userAmt = fresh.getUserAmt() == null ? BigDecimal.ZERO : fresh.getUserAmt();
        fresh.setEnableAmt(enableAmt.add(refundAmt));
        fresh.setEnaleWithdrawAmt(withdrawAmt.add(refundAmt));
        fresh.setUserAmt(userAmt.add(refundAmt));
        int rows = this.userMapper.updateByPrimaryKeySelective(fresh);
        if (rows <= 0) {
            throw new Exception("撤单退款失败");
        }
        UserCashDetail ucd = new UserCashDetail();
        ucd.setPositionId(position.getId());
        ucd.setAgentId(user.getAgentId());
        ucd.setAgentName(user.getAgentName());
        ucd.setUserId(user.getId());
        ucd.setUserName(user.getRealName());
        ucd.setDeType("撤单退款");
        ucd.setDeAmt(refundAmt);
        ucd.setDeSummary("撤销委托," + position.getStockCode() + "/" + position.getStockName()
                + ",退还保证金+手续费:" + refundAmt);
        ucd.setAddTime(new Date());
        ucd.setIsRead(Integer.valueOf(0));
        this.userCashDetailMapper.insert(ucd);
        log.info("【用户撤单】退款成功,用户={},金额={}", user.getId(), refundAmt);
    }
    private void saveBuyDebitCashDetail(User user, UserPosition position, BigDecimal margin, BigDecimal fee,
                                        BigDecimal buyDebit) {
        UserCashDetail ucd = new UserCashDetail();
        ucd.setPositionId(position.getId());
        ucd.setAgentId(user.getAgentId());
        ucd.setAgentName(user.getAgentName());
        ucd.setUserId(user.getId());
        ucd.setUserName(user.getRealName());
        ucd.setDeType("买入扣款");
        ucd.setDeAmt(buyDebit.negate());
        ucd.setDeSummary("委托买入," + position.getStockCode() + "/" + position.getStockName()
                + ",保证金:" + margin + ",手续费:" + fee + ",合计扣款:" + buyDebit);
        ucd.setAddTime(new Date());
        ucd.setIsRead(Integer.valueOf(0));
        this.userCashDetailMapper.insert(ucd);
    }
    /** 下单/撤单后刷新 Redis 中的用户资金,避免页面仍显示旧可用余额 */
    private void syncUserCacheAfterTrade(HttpServletRequest request) {
        if (request == null) {
            return;
        }
        String cookieName = PropertiesUtil.getProperty("user.cookie.name");
        String loginToken = request.getHeader(cookieName);
        if (StringUtils.isBlank(loginToken)) {
            return;
        }
        String userJson = RedisShardedPoolUtils.get(loginToken);
        User cached = (User) JsonUtil.string2Obj(userJson, User.class);
        if (cached == null || cached.getId() == null) {
            return;
        }
        User dbUser = this.userMapper.selectByPrimaryKey(cached.getId());
        if (dbUser != null) {
            RedisShardedPoolUtils.setEx(loginToken, JsonUtil.obj2String(dbUser), 9999);
        }
    }
    @Override
    @Transactional
    public ServerResponse pending(Integer stockId, Integer buyNum, Integer buyType, Integer lever, BigDecimal profitTarget, BigDecimal stopTarget, HttpServletRequest request) throws Exception {
@@ -569,7 +659,7 @@
        if (!am_flag && !pm_flag && siteProduct.getTranWithdrawDisplay()) {
            return ServerResponse.createByErrorMsg("挂单失败,不在交易时段内");
        }
        if (siteProduct.getHolidayDisplay()) {
        if (siteProduct.getTranWithdrawDisplay() && TradingDayUtil.shouldBlockTradingToday(siteProduct)) {
            return ServerResponse.createByErrorMsg("周末或节假日不能交易!");
        }
@@ -585,11 +675,6 @@
                    .getBuySameNums() + "条");
        }
        Integer transNum = findPositionNumByTimes(siteSetting.getBuyNumTimes().intValue(), user.getId());
        if (transNum.intValue() / 100 >= siteSetting.getBuyNumLots().intValue() && siteProduct.getTranWithdrawDisplay()) {
            return ServerResponse.createByErrorMsg("频繁交易," + siteSetting
                    .getBuyNumTimes() + "分钟内不能超过" + siteSetting.getBuyNumLots() + "手");
        }
        if (buyNum.intValue() < siteSetting.getBuyMinNum().intValue()) {
            return ServerResponse.createByErrorMsg("挂单失败,购买数量小于" + siteSetting
@@ -621,9 +706,9 @@
            log.info("【普通A股】");
        }
        if (stockListVO.getName().startsWith("ST") || stockListVO.getName().endsWith("退")) {
            return ServerResponse.createByErrorMsg("ST和已退市的股票不能入仓");
        }
//        if (stockListVO.getName().startsWith("ST") || stockListVO.getName().endsWith("退")) {
//            return ServerResponse.createByErrorMsg("ST和已退市的股票不能入仓");
//        }
        BigDecimal zsPrice = new BigDecimal(stockListVO.getPreclose_px());
@@ -633,67 +718,67 @@
        BigDecimal ztRate = chaPrice.multiply(new BigDecimal("100")).divide(zsPrice, 2, 4);
        log.info("当前涨跌幅 = {} % , 涨停幅度 = {} %", Double.valueOf(stock_crease), ztRate);
        if ((new BigDecimal(String.valueOf(stock_crease))).compareTo(ztRate) == 0 && buyType
                .intValue() == 0 && siteProduct.getTranWithdrawDisplay()) {
            return ServerResponse.createByErrorMsg("当前股票已涨停不能买涨");
        }
        if (stock.getStockPlate() == null || StringUtils.isEmpty(stock.getStockPlate())) {
            int maxcrease = siteSetting.getCreaseMaxPercent().intValue();
            if (stock_crease > 0.0D &&
                    stock_crease >= maxcrease && siteProduct.getTranWithdrawDisplay()) {
                return ServerResponse.createByErrorMsg("挂单失败,股票当前涨幅:" + stock_crease + ",大于最大涨幅:" + maxcrease);
            }
            if (stock_crease < 0.0D &&
                    -stock_crease > maxcrease && siteProduct.getTranWithdrawDisplay()) {
                return ServerResponse.createByErrorMsg("挂单失败,股票当前跌幅:" + stock_crease + ",大于最大跌幅:" + maxcrease);
            }
        } else if ("创业".equals(stock.getStockPlate())) {
            int maxcrease = siteSetting.getCyCreaseMaxPercent().intValue();
            if (stock_crease > 0.0D &&
                    stock_crease >= maxcrease && siteProduct.getTranWithdrawDisplay()) {
                return ServerResponse.createByErrorMsg("挂单失败,创业股当前涨幅:" + stock_crease + ",大于最大涨幅:" + maxcrease);
            }
            if (stock_crease < 0.0D &&
                    -stock_crease > maxcrease && siteProduct.getTranWithdrawDisplay()) {
                return ServerResponse.createByErrorMsg("挂单失败,创业股当前跌幅:" + stock_crease + ",大于最大跌幅:" + maxcrease);
            }
        } else {
            int maxcrease = siteSetting.getKcCreaseMaxPercent().intValue();
            if (stock_crease > 0.0D &&
                    stock_crease >= maxcrease && siteProduct.getTranWithdrawDisplay()) {
                return ServerResponse.createByErrorMsg("挂单失败,科创股当前涨幅:" + stock_crease + ",大于最大涨幅:" + maxcrease);
            }
            if (stock_crease < 0.0D &&
                    -stock_crease > maxcrease && siteProduct.getTranWithdrawDisplay()) {
                return ServerResponse.createByErrorMsg("挂单失败,科创股当前跌幅:" + stock_crease + ",大于最大跌幅:" + maxcrease);
            }
        }
//        log.info("当前涨跌幅 = {} % , 涨停幅度 = {} %", Double.valueOf(stock_crease), ztRate);
//        if ((new BigDecimal(String.valueOf(stock_crease))).compareTo(ztRate) == 0 && buyType
//                .intValue() == 0 && siteProduct.getTranWithdrawDisplay()) {
//            return ServerResponse.createByErrorMsg("当前股票已涨停不能买涨");
//        }
//        if (stock.getStockPlate() == null || StringUtils.isEmpty(stock.getStockPlate())) {
//
//            int maxcrease = siteSetting.getCreaseMaxPercent().intValue();
//            if (stock_crease > 0.0D &&
//                    stock_crease >= maxcrease && siteProduct.getTranWithdrawDisplay()) {
//                return ServerResponse.createByErrorMsg("挂单失败,股票当前涨幅:" + stock_crease + ",大于最大涨幅:" + maxcrease);
//            }
//
//
//            if (stock_crease < 0.0D &&
//                    -stock_crease > maxcrease && siteProduct.getTranWithdrawDisplay()) {
//                return ServerResponse.createByErrorMsg("挂单失败,股票当前跌幅:" + stock_crease + ",大于最大跌幅:" + maxcrease);
//
//            }
//
//        } else if ("创业".equals(stock.getStockPlate())) {
//
//            int maxcrease = siteSetting.getCyCreaseMaxPercent().intValue();
//            if (stock_crease > 0.0D &&
//                    stock_crease >= maxcrease && siteProduct.getTranWithdrawDisplay()) {
//                return ServerResponse.createByErrorMsg("挂单失败,创业股当前涨幅:" + stock_crease + ",大于最大涨幅:" + maxcrease);
//            }
//
//
//            if (stock_crease < 0.0D &&
//                    -stock_crease > maxcrease && siteProduct.getTranWithdrawDisplay()) {
//                return ServerResponse.createByErrorMsg("挂单失败,创业股当前跌幅:" + stock_crease + ",大于最大跌幅:" + maxcrease);
//            }
//        } else {
//
//            int maxcrease = siteSetting.getKcCreaseMaxPercent().intValue();
//            if (stock_crease > 0.0D &&
//                    stock_crease >= maxcrease && siteProduct.getTranWithdrawDisplay()) {
//                return ServerResponse.createByErrorMsg("挂单失败,科创股当前涨幅:" + stock_crease + ",大于最大涨幅:" + maxcrease);
//            }
//
//
//            if (stock_crease < 0.0D &&
//                    -stock_crease > maxcrease && siteProduct.getTranWithdrawDisplay()) {
//                return ServerResponse.createByErrorMsg("挂单失败,科创股当前跌幅:" + stock_crease + ",大于最大跌幅:" + maxcrease);
//            }
//        }
        ServerResponse serverResponse = this.iStockService.selectRateByDaysAndStockCode(stock
                .getStockCode(), siteSetting.getStockDays().intValue());
        if (!serverResponse.isSuccess()) {
            return serverResponse;
        }
        BigDecimal daysRate = (BigDecimal) serverResponse.getData();
        log.info("股票 {} , {} 天内 涨幅 {} , 设置的涨幅 = {}", new Object[]{stock.getStockCode(), siteSetting
                .getStockDays(), daysRate, siteSetting.getStockRate()});
        if (daysRate != null &&
                siteSetting.getStockRate().compareTo(daysRate) == -1 && siteProduct.getTranWithdrawDisplay()) {
            return serverResponse.createByErrorMsg(siteSetting.getStockDays() + "天内涨幅超过 " + siteSetting
                    .getStockRate() + "不能交易");
        }
//        log.info("股票 {} , {} 天内 涨幅 {} , 设置的涨幅 = {}", new Object[]{stock.getStockCode(), siteSetting
//                .getStockDays(), daysRate, siteSetting.getStockRate()});
//
//        if (daysRate != null &&
//                siteSetting.getStockRate().compareTo(daysRate) == -1 && siteProduct.getTranWithdrawDisplay()) {
//            return serverResponse.createByErrorMsg(siteSetting.getStockDays() + "天内涨幅超过 " + siteSetting
//                    .getStockRate() + "不能交易");
//        }
        BigDecimal buy_amt = now_price.multiply(new BigDecimal(buyNum.intValue()));
@@ -713,11 +798,10 @@
                    .getBuyMaxAmtPercent().multiply(new BigDecimal("100")) + "%");
        }
        int compareUserAmtInt = user_enable_amt.compareTo(buy_amt_autual);
        log.info("用户可用金额 = {}  实际购买金额 =  {}", user_enable_amt, buy_amt_autual);
        log.info("比较 用户金额 和 实际 购买金额 =  {}", Integer.valueOf(compareUserAmtInt));
        if (compareUserAmtInt == -1) {
            return ServerResponse.createByErrorMsg("挂单失败,融资可用金额小于" + buy_amt_autual + "元");
        BigDecimal buy_fee_amt = TradeFeeUtil.calcBuyFee(buy_amt_autual, siteSetting.getBuyFee());
        BigDecimal buy_debit = TradeFeeUtil.calcBuyDebit(buy_amt_autual, buy_fee_amt);
        if (user_enable_amt.compareTo(buy_debit) == -1) {
            return ServerResponse.createByErrorMsg("挂单失败,融资可用金额小于" + buy_debit + "元(含保证金及手续费)");
        }
        if (user.getUserIndexAmt().compareTo(new BigDecimal("0")) == -1) {
            return ServerResponse.createByErrorMsg("失败,指数总资金小于0");
@@ -756,7 +840,6 @@
        BigDecimal allStayFee = stayFee.multiply(new BigDecimal(1));
        userPosition.setOrderStayFee(allStayFee);
        userPosition.setOrderStayDays(1);
        BigDecimal buy_fee_amt = buy_amt.multiply(siteSetting.getBuyFee()).setScale(2, 4);
        log.info("用户购买手续费(配资后总资金 * 百分比) = {}", buy_fee_amt);
        userPosition.setOrderFee(buy_fee_amt);
        BigDecimal buy_yhs_amt = buy_amt.multiply(siteSetting.getDutyFee()).setScale(2, 4);
@@ -777,40 +860,60 @@
        userPosition.setAllProfitAndLose(all_profit_and_lose);
        userPosition.setOrderStayDays(Integer.valueOf(0));
        userPosition.setOrderStayFee(new BigDecimal("0"));
        int insertPositionCount = 0;
        this.userPositionMapper.insert(userPosition);
        insertPositionCount = userPosition.getId();
        if (insertPositionCount > 0) {
            //修改用户可用余额= 当前余额-下单金额-买入手续费-印花税-点差费
            //BigDecimal reckon_enable = user_enable_amt.subtract(buy_amt_autual).subtract(buy_fee_amt).subtract(buy_yhs_amt).subtract(spread_rate_amt);
            //修改用户可用余额= 当前余额-下单总金额
            BigDecimal reckon_enable = user_enable_amt.subtract(buy_amt_autual);
            //修改用户可取余额=当前可取余额-下单总金额
            int compareUserWithdrawAmtInt = user_enable_withdraw_amt.compareTo(buy_amt_autual);
            if (compareUserWithdrawAmtInt == -1) {
                //若可取余额小于下单总额,但是可用余额充足,令可取余额为0
                user.setEnaleWithdrawAmt(BigDecimal.ZERO);
            } else {
                user_enable_withdraw_amt = user_enable_withdraw_amt.subtract(buy_amt_autual);
                user.setEnaleWithdrawAmt(user_enable_withdraw_amt);
            }
            user.setEnableAmt(reckon_enable);
//            user.setDjzj(user.getDjzj().subtract(buy_amt_autual));
            int updateUserCount = this.userMapper.updateByPrimaryKeySelective(user);
            if (updateUserCount > 0) {
                log.info("【用户交易下单】修改用户金额成功");
            } else {
                log.error("用户交易下单】修改用户金额出错");
                throw new Exception("用户交易下单】修改用户金额出错");
            }
            //核算代理收入-入仓手续费
//            iAgentAgencyFeeService.AgencyFeeIncome(1, userPosition.getPositionSn());
        if (userPosition.getId() != null && userPosition.getId() > 0) {
            deductUserEnableOnBuy(user, buy_debit, buy_amt_autual, buy_fee_amt, userPosition);
            syncUserCacheAfterTrade(request);
            log.info("【用户交易下单】保存持仓记录成功");
        } else {
            log.error("用户交易下单】保存持仓记录出错");
            throw new Exception("用户交易下单】保存持仓记录出错");
        }
        return ServerResponse.createBySuccess("挂单成功");
    }
    /**
     * 撤销委托单(status=0),退还保证金+买入手续费
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ServerResponse cancelOrder(String positionSn, HttpServletRequest request) throws Exception {
        if (StringUtils.isBlank(positionSn)) {
            return ServerResponse.createByErrorMsg("参数错误");
        }
        User user = this.iUserService.getCurrentRefreshUser(request);
        if (user == null) {
            return ServerResponse.createByErrorMsg("请先登录");
        }
        UserPosition userPosition = this.userPositionMapper.findPositionBySn(positionSn);
        if (userPosition == null) {
            return ServerResponse.createByErrorMsg("委托不存在");
        }
        if (!user.getId().equals(userPosition.getUserId())) {
            return ServerResponse.createByErrorMsg("无权操作该委托");
        }
        if (userPosition.getStatus() == null || userPosition.getStatus().intValue() != 0) {
            return ServerResponse.createByErrorMsg("当前订单不可撤单");
        }
        if (userPosition.getSellOrderId() != null) {
            return ServerResponse.createByErrorMsg("当前订单不可撤单");
        }
        BigDecimal buyAmtActual = userPosition.getOrderTotalPrice()
                .divide(new BigDecimal(userPosition.getOrderLever()), 2, 4);
        BigDecimal buyFee = userPosition.getOrderFee() != null ? userPosition.getOrderFee() : BigDecimal.ZERO;
        BigDecimal refundAmt = TradeFeeUtil.calcBuyDebit(buyAmtActual, buyFee);
        User freshUser = this.userMapper.selectByPrimaryKey(user.getId());
        if (freshUser == null) {
            throw new Exception("用户不存在");
        }
        refundUserEnableOnCancel(freshUser, refundAmt, userPosition);
        int delCount = this.userPositionMapper.deleteByPrimaryKey(userPosition.getId());
        if (delCount <= 0) {
            throw new Exception("撤单失败,删除委托记录失败");
        }
        syncUserCacheAfterTrade(request);
        log.info("【用户撤单】positionSn={} 退还保证金+手续费={}", positionSn, refundAmt);
        return ServerResponse.createBySuccessMsg("撤单成功");
    }
@@ -857,6 +960,49 @@
    }
    private ServerResponse validateT1BeforeSell(UserPosition userPosition, int doType) {
        if (doType == 0 || userPosition == null || userPosition.getBuyOrderTime() == null) {
            return null;
        }
        String stockGid = userPosition.getStockGid();
        if (stockGid != null && (stockGid.contains("us") || stockGid.contains("hk"))) {
            return null;
        }
        if (!DateTimeUtil.canSellByT1(userPosition.getBuyOrderTime())) {
            return ServerResponse.createByErrorMsg("T+1交易制度,当日买入的股票需下一交易日才能平仓");
        }
        return null;
    }
    private ServerResponse validateTransTimeBeforeSell(UserPosition userPosition, SiteSetting siteSetting) throws Exception {
        if (userPosition == null || siteSetting == null) {
            return null;
        }
        String stockGid = userPosition.getStockGid();
        boolean inSession;
        if (stockGid != null && stockGid.contains("us")) {
            inSession = BuyAndSellUtils.isInTransSession(
                    siteSetting.getTransAmBeginUs(), siteSetting.getTransAmEndUs(),
                    siteSetting.getTransPmBeginUs(), siteSetting.getTransPmEndUs());
        } else if (stockGid != null && stockGid.contains("hk")) {
            inSession = BuyAndSellUtils.isInTransSession(
                    siteSetting.getTransAmBeginhk(), siteSetting.getTransAmEndhk(),
                    siteSetting.getTransPmBeginhk(), siteSetting.getTransPmEndhk());
        } else {
            inSession = BuyAndSellUtils.isInTransSession(
                    siteSetting.getTransAmBegin(), siteSetting.getTransAmEnd(),
                    siteSetting.getTransPmBegin(), siteSetting.getTransPmEnd());
        }
        if (!inSession) {
            return ServerResponse.createByErrorMsg("平仓失败,不在交易时段内");
        }
        SiteProduct siteProduct = iSiteProductService.getProductSetting();
        if (TradingDayUtil.shouldBlockTradingToday(siteProduct)) {
            return ServerResponse.createByErrorMsg("周末或节假日不能交易!");
        }
        return null;
    }
    public ServerResponse sell(String positionSn, int doType) throws Exception {
        log.info("【用戶交易平倉】 positionSn = {} , dotype = {}", positionSn, Integer.valueOf(doType));
@@ -869,54 +1015,17 @@
        SiteProduct siteProduct = iSiteProductService.getProductSetting();
        UserPosition userPosition = this.userPositionMapper.findPositionBySn(positionSn);
        if (doType != 0) {
            if (userPosition.getStockGid().contains("us")) {
                String am_begin = siteSetting.getTransAmBeginUs();
                String am_end = siteSetting.getTransAmEndUs();
                String pm_begin = siteSetting.getTransPmBeginUs();
                String pm_end = siteSetting.getTransPmEndUs();
                boolean am_flag = BuyAndSellUtils.isTransTime(am_begin, am_end);
                boolean pm_flag = BuyAndSellUtils.isTransTime(pm_begin, pm_end);
                log.info("是否在上午交易時間 = {} 是否在下午交易時間 = {}", Boolean.valueOf(am_flag), Boolean.valueOf(pm_flag));
                if (!am_flag && !pm_flag) {
                    return ServerResponse.createByErrorMsg("下单失败,系统设置错误");
                }
            } else if (userPosition.getStockGid().contains("hk")) {
                String am_begin = siteSetting.getTransAmBeginhk();
                String am_end = siteSetting.getTransAmEndhk();
                String pm_begin = siteSetting.getTransPmBeginhk();
                String pm_end = siteSetting.getTransPmEndhk();
                boolean am_flag = BuyAndSellUtils.isTransTime(am_begin, am_end);
                boolean pm_flag = BuyAndSellUtils.isTransTime(pm_begin, pm_end);
                log.info("是否在上午交易時間 = {} 是否在下午交易時間 = {}", Boolean.valueOf(am_flag), Boolean.valueOf(pm_flag));
                if (!am_flag && !pm_flag) {
                    return ServerResponse.createByErrorMsg("下單失敗,不在港股股交易時段內");
                }
            } else {
                String am_begin = siteSetting.getTransAmBegin();
                String am_end = siteSetting.getTransAmEnd();
                String pm_begin = siteSetting.getTransPmBegin();
                String pm_end = siteSetting.getTransPmEnd();
                boolean am_flag = BuyAndSellUtils.isTransTime(am_begin, am_end);
                boolean pm_flag = BuyAndSellUtils.isTransTime(pm_begin, pm_end);
                log.info("是否在上午交易時間 = {} 是否在下午交易時間 = {}", Boolean.valueOf(am_flag), Boolean.valueOf(pm_flag));
                if (!am_flag && !pm_flag && siteProduct.getTranWithdrawDisplay()) {
                    return ServerResponse.createByErrorMsg("平仓失败,不在交易时段内");
                }
            }
            if (siteProduct.getHolidayDisplay()) {
                return ServerResponse.createByErrorMsg("周末或节假日不能交易!");
            }
        }
        if (userPosition == null) {
            return ServerResponse.createByErrorMsg("平仓失败,订单不存在");
        }
        if (doType != 0) {
            ServerResponse timeCheck = validateTransTimeBeforeSell(userPosition, siteSetting);
            if (timeCheck != null) {
                return timeCheck;
            }
        }
        User user = this.userMapper.selectByPrimaryKey(userPosition.getUserId());
        if (user == null) {
@@ -935,10 +1044,9 @@
            return ServerResponse.createByErrorMsg("平仓失败,此订单已平仓");
        }
        if (DateTimeUtil.isCanSellOneday(userPosition.getBuyOrderTime(), siteSetting.getCantSellTimes().intValue()) && siteProduct.getTranWithdrawDisplay()) {
            // return ServerResponse.createByErrorMsg(siteSetting.getCantSellTimes() + "分鐘內不能平倉");
            return ServerResponse.createByErrorMsg("当日成交不可平仓");
        ServerResponse t1Check = validateT1BeforeSell(userPosition, doType);
        if (t1Check != null) {
            return t1Check;
        }
        if (1 == userPosition.getIsLock().intValue()) {
@@ -1017,10 +1125,10 @@
        ztRate = ztRate.negate();
        log.info("股票當前漲跌幅 = {} 跌停幅度 = {}", Double.valueOf(stock_crease), ztRate);
        if ((new BigDecimal(String.valueOf(stock_crease))).compareTo(ztRate) == 0 && "買漲"
                .equals(userPosition.getOrderDirection())) {
            return ServerResponse.createByErrorMsg("当前股票已跌停不能卖出");
        }
//        if ((new BigDecimal(String.valueOf(stock_crease))).compareTo(ztRate) == 0 && "買漲"
//                .equals(userPosition.getOrderDirection())) {
//            return ServerResponse.createByErrorMsg("当前股票已跌停不能卖出");
//        }
        Integer buy_num = userPosition.getOrderNum();
@@ -1060,9 +1168,9 @@
        BigDecimal sell_fee_amt = all_sell_amt.multiply(siteSetting.getSellFee()).setScale(2, 4);
        log.info("卖出手续费 = {}", sell_fee_amt);
        //總手續費= 買入手續費+賣出手續費+印花稅+遞延費+點差費
        BigDecimal all_fee_amt = buy_fee_amt.add(sell_fee_amt).add(orderSpread).add(orderStayFee).add(spreadRatePrice);
        log.info("总的手续费费用 = {}", all_fee_amt);
        // 买入手续费已在下单时扣除;递延费已在持仓期间从余额实时扣除,平仓不再重复扣款
        BigDecimal close_fee_amt = sell_fee_amt.add(orderSpread).add(spreadRatePrice);
        log.info("平仓结算费用(不含递延费) = {}", close_fee_amt);
        userPosition.setSellOrderId(GeneratePosition.getPositionId());
        userPosition.setSellOrderPrice(now_price);
@@ -1073,8 +1181,10 @@
        userPosition.setProfitAndLose(profitLoss);
        BigDecimal all_profit = profitLoss.subtract(all_fee_amt);
        userPosition.setAllProfitAndLose(all_profit);
        BigDecimal all_profit_balance = profitLoss.subtract(close_fee_amt);
        BigDecimal all_profit_display = all_profit_balance.subtract(
                orderStayFee == null ? BigDecimal.ZERO : orderStayFee);
        userPosition.setAllProfitAndLose(all_profit_display);
        userPosition.setStatus(2);
        int updatePositionCount = this.userPositionMapper.updateByPrimaryKeySelective(userPosition);
@@ -1088,13 +1198,13 @@
        BigDecimal freez_amt = all_buy_amt.divide(new BigDecimal(userPosition.getOrderLever().intValue()), 2, 4);
        //BigDecimal freez_amt = all_buy_amt;
        BigDecimal reckon_all = user_all_amt.add(all_profit);
        BigDecimal reckon_all = user_all_amt.add(all_profit_balance);
        //修改用戶可用余額=當前可用余額+總盈虧+買入總金額+追加保證金
        BigDecimal reckon_enable = user_enable_amt.add(all_profit).add(freez_amt).add(userPosition.getMarginAdd());
        BigDecimal reckon_enable = user_enable_amt.add(all_profit_balance).add(freez_amt).add(userPosition.getMarginAdd());
        log.info("用戶平倉後的總資金  = {} , 可用資金 = {}", reckon_all, reckon_enable);
        user.setUserAmt(reckon_all);
        user.setEnableAmt(reckon_enable);
        applyEnableAndWithdrawChange(user, user_enable_amt, reckon_enable);
        int updateUserCount = this.userMapper.updateByPrimaryKeySelective(user);
        if (updateUserCount > 0) {
            log.info("【用戶平倉】修改用戶金額成功");
@@ -1110,8 +1220,10 @@
        ucd.setUserId(user.getId());
        ucd.setUserName(user.getRealName());
        ucd.setDeType("总盈亏");
        ucd.setDeAmt(all_profit);
        ucd.setDeSummary("卖出股票," + userPosition.getStockCode() + "/" + userPosition.getStockName() + ",占用本金:" + freez_amt + ",总手续费:" + all_fee_amt + ",递延费:" + orderStayFee + ",印花稅:" + orderSpread + ",盈亏:" + profitLoss + ",总盈亏:" + all_profit);
        ucd.setDeAmt(all_profit_balance);
        ucd.setDeSummary("卖出股票," + userPosition.getStockCode() + "/" + userPosition.getStockName() + ",占用本金:" + freez_amt
                + ",总手续费:" + order_fee_all + ",递延费:" + (orderStayFee == null ? BigDecimal.ZERO : orderStayFee)
                + ",盈亏:" + profitLoss + ",总盈亏:" + all_profit_display);
        ucd.setAddTime(new Date());
        ucd.setIsRead(Integer.valueOf(0));
@@ -1172,23 +1284,16 @@
        }
        SiteProduct siteProduct = iSiteProductService.getProductSetting();
        UserPosition userPosition = this.userPositionMapper.findPositionBySn(positionSn);
        if (userPosition == null) {
            return ServerResponse.createByErrorMsg("平仓失败,订单不存在");
        }
        //部分平仓数量等于订单总数量,则调用全平接口
        if(quantity.equals(userPosition.getOrderNum())){
            return sell(positionSn,1);
        }
        //校验时间
        String am_begin = siteSetting.getTransAmBegin();
        String am_end = siteSetting.getTransAmEnd();
        String pm_begin = siteSetting.getTransPmBegin();
        String pm_end = siteSetting.getTransPmEnd();
        boolean am_flag = BuyAndSellUtils.isTransTime(am_begin, am_end);
        boolean pm_flag = BuyAndSellUtils.isTransTime(pm_begin, pm_end);
        log.info("是否在上午交易時間 = {} 是否在下午交易時間 = {}", Boolean.valueOf(am_flag), Boolean.valueOf(pm_flag));
        if (!am_flag && !pm_flag) {
            return ServerResponse.createByErrorMsg("平仓失败,不在交易时段内");
        }
        if (userPosition == null) {
            return ServerResponse.createByErrorMsg("平仓失败,订单不存在");
        ServerResponse timeCheck = validateTransTimeBeforeSell(userPosition, siteSetting);
        if (timeCheck != null) {
            return timeCheck;
        }
        User user = this.userMapper.selectByPrimaryKey(userPosition.getUserId());
        if (user == null) {
@@ -1206,8 +1311,9 @@
        if (1 == userPosition.getIsLock().intValue()) {
            return ServerResponse.createByErrorMsg("平仓失败 " + userPosition.getLockMsg());
        }
        if (!DateTimeUtil.isCanSell(userPosition.getBuyOrderTime(), siteSetting.getCantSellTimes().intValue())) {
            return ServerResponse.createByErrorMsg("当日成交不可平仓");
        ServerResponse t1Check = validateT1BeforeSell(userPosition, 1);
        if (t1Check != null) {
            return t1Check;
        }
        BigDecimal now_price;
@@ -1271,7 +1377,9 @@
        BigDecimal user_enable_amt = user.getEnableAmt();
        log.info("用戶原本總資金 = {} , 可用 = {}", user_all_amt, user_enable_amt);
        BigDecimal buy_fee_amt = all_buy_amt.multiply(siteSetting.getBuyFee()).setScale(2,4);
        BigDecimal partialMargin = all_buy_amt.divide(
                new BigDecimal(userPosition.getOrderLever()), 2, RoundingMode.HALF_UP);
        BigDecimal buy_fee_amt = TradeFeeUtil.calcBuyFee(partialMargin, siteSetting.getBuyFee());
        log.info("買入手續費 = {}", buy_fee_amt);
        BigDecimal orderSpread = all_buy_amt.multiply(siteSetting.getDutyFee()).setScale(2, 4);
@@ -1286,9 +1394,8 @@
        BigDecimal sell_fee_amt = all_sell_amt.multiply(siteSetting.getSellFee()).setScale(2, 4);
        log.info("賣出手續費 = {}", sell_fee_amt);
        //總手續費= 買入手續費+賣出手續費+印花稅+遞延費+點差費
//        BigDecimal all_fee_amt = buy_fee_amt.add(sell_fee_amt).add(orderSpread).add(orderStayFee).add(spreadRatePrice);
        BigDecimal all_fee_amt = buy_fee_amt.add(sell_fee_amt).add(orderSpread);
        // 买入手续费已在下单时扣除
        BigDecimal all_fee_amt = sell_fee_amt.add(orderSpread);
        log.info("總的手續費費用 = {}", all_fee_amt);
        //复制一条新订单
        UserPosition userPositionNew = new UserPosition();
@@ -1335,7 +1442,7 @@
        log.info("用戶平倉後的總資金  = {} , 可用資金 = {}", reckon_all, reckon_enable);
        user.setUserAmt(reckon_all);
        user.setEnableAmt(reckon_enable);
        applyEnableAndWithdrawChange(user, user_enable_amt, reckon_enable);
        int updateUserCount = this.userMapper.updateByPrimaryKeySelective(user);
        if (updateUserCount > 0) {
            log.info("【用戶平倉】修改用戶金額成功");
@@ -1352,7 +1459,7 @@
        ucd.setUserName(user.getRealName());
        ucd.setDeType("总盈亏");
        ucd.setDeAmt(all_profit);
        ucd.setDeSummary("卖出股票," + userPositionNew.getStockCode() + "/" + userPositionNew.getStockName() + ",占用本金:" + freez_amt + ",总手续费:" + all_fee_amt + ",递延费:" + 0 + ",印花稅:" + orderSpread + ",盈亏:" + profitLoss + ",总盈亏:" + all_profit);
        ucd.setDeSummary("卖出股票," + userPositionNew.getStockCode() + "/" + userPositionNew.getStockName() + ",占用本金:" + freez_amt + ",总手续费:" + order_fee_all + ",递延费:" + 0 + ",印花稅:" + orderSpread + ",盈亏:" + profitLoss + ",总盈亏:" + all_profit);
        ucd.setAddTime(new Date());
        ucd.setIsRead(Integer.valueOf(0));
@@ -1408,7 +1515,7 @@
            return ServerResponse.createByErrorMsg("不允许追加,请联系管理员");
        }
        if (siteProduct.getHolidayDisplay()) {
        if (TradingDayUtil.shouldBlockTradingToday(siteProduct)) {
            return ServerResponse.createByErrorMsg("周末或节假日不能交易!");
        }
@@ -1430,7 +1537,8 @@
        }
        userPosition.setMarginAdd(userPosition.getMarginAdd().add(marginAdd));
        BigDecimal existMarginAdd = userPosition.getMarginAdd() == null ? BigDecimal.ZERO : userPosition.getMarginAdd();
        userPosition.setMarginAdd(existMarginAdd.add(marginAdd));
        int updatePositionCount = this.userPositionMapper.updateByPrimaryKeySelective(userPosition);
        if (updatePositionCount > 0) {
@@ -1444,7 +1552,7 @@
        BigDecimal reckon_enable = user_enable_amt.subtract(marginAdd);
        log.info("用户追加保证金后的总资金  = {} , 可用资金 = {}", user_all_amt, reckon_enable);
        user.setEnableAmt(reckon_enable);
        applyEnableAndWithdrawChange(user, user_enable_amt, reckon_enable);
        int updateUserCount = this.userMapper.updateByPrimaryKeySelective(user);
        if (updateUserCount > 0) {
            log.info("【用户平仓】修改用户金额成功");
@@ -1828,13 +1936,11 @@
        BigDecimal buy_amt_autual = buy_amt.divide(new BigDecimal(lever.intValue()), 2, 4);
        int compareUserAmtInt = user_enable_amt.compareTo(buy_amt_autual);
        log.info("用户可用金额 = {}  实际购买金额 =  {}", user_enable_amt, buy_amt_autual);
        log.info("比较 用户金额 和 实际 购买金额 =  {}", Integer.valueOf(compareUserAmtInt));
        if (compareUserAmtInt == -1) {
            log.info("下单失败,用户可用金额小于" + buy_amt_autual + "元");
            return ServerResponse.createByErrorMsg("下单失败,用户可用金额小于" + buy_amt_autual + "元");
        BigDecimal buy_fee_amt_check = TradeFeeUtil.calcBuyFee(buy_amt_autual, siteSetting.getBuyFee());
        BigDecimal buy_debit_check = TradeFeeUtil.calcBuyDebit(buy_amt_autual, buy_fee_amt_check);
        if (user_enable_amt.compareTo(buy_debit_check) < 0) {
            log.info("下单失败,用户可用金额小于{}元(含保证金及手续费)", buy_debit_check);
            return ServerResponse.createByErrorMsg("下单失败,用户可用金额小于" + buy_debit_check + "元(含保证金及手续费)");
        }
        if (user.getUserIndexAmt().compareTo(new BigDecimal("0")) == -1) {
@@ -1891,8 +1997,8 @@
        userPosition.setOrderStayDays(1);
        BigDecimal buy_fee_amt = buy_amt.multiply(siteSetting.getBuyFee()).setScale(2, 4);
        log.info("创建模拟持仓 手续费(配资后总资金 * 百分比) = {}", buy_fee_amt);
        BigDecimal buy_fee_amt = TradeFeeUtil.calcBuyFee(buy_amt_autual, siteSetting.getBuyFee()).setScale(2, 4);
        log.info("创建模拟持仓 手续费(保证金 * 百分比) = {}", buy_fee_amt);
        userPosition.setOrderFee(buy_fee_amt);
@@ -1935,21 +2041,21 @@
        userPosition.setOrderStayDays(Integer.valueOf(0));
        userPosition.setOrderStayFee(new BigDecimal("0"));
        userPosition.setSpreadRatePrice(new BigDecimal("0"));
        userPosition.setStatus(1);
        int insertPositionCount = this.userPositionMapper.insert(userPosition);
        if (insertPositionCount > 0) {
            log.info("【创建持仓】保存记录成功");
        } else {
        this.userPositionMapper.insert(userPosition);
        if (userPosition.getId() == null || userPosition.getId() <= 0) {
            log.error("【创建持仓】保存记录出错");
            return ServerResponse.createByErrorMsg("生成持仓失败");
        }
        BigDecimal reckon_enable = user_enable_amt.subtract(buy_amt_autual);
        user.setEnableAmt(reckon_enable);
        int updateUserCount = this.userMapper.updateByPrimaryKeySelective(user);
        if (updateUserCount > 0) {
            log.info("【用户交易下单】修改用户金额成功");
        } else {
            log.error("用户交易下单】修改用户金额出错");
        log.info("【创建持仓】保存记录成功");
        BigDecimal buy_debit = TradeFeeUtil.calcBuyDebit(buy_amt_autual, buy_fee_amt);
        try {
            deductUserEnableOnBuy(user, buy_debit, buy_amt_autual, buy_fee_amt, userPosition);
        } catch (Exception e) {
            this.userPositionMapper.deleteByPrimaryKey(userPosition.getId());
            log.error("【创建持仓】扣款失败,已回滚持仓记录", e);
            return ServerResponse.createByErrorMsg("生成持仓失败:" + e.getMessage());
        }
        iAgentAgencyFeeService.AgencyFeeIncome(1, userPosition.getPositionSn());
        return ServerResponse.createBySuccess("生成持仓成功");
@@ -1962,127 +2068,165 @@
    public void doClosingStayTask() {
        List<UserPosition> userPositions = this.userPositionMapper.findAllStayPosition();
        if (userPositions.size() > 0) {
            log.info("查询到正在持仓的订单数量 = {}", Integer.valueOf(userPositions.size()));
            SiteProduct siteProduct = iSiteProductService.getProductSetting();
            if (!siteProduct.getHolidayDisplay()) {
                for (UserPosition position : userPositions) {
                    int stayDays = GetStayDays.getDays(GetStayDays.getBeginDate(position.getBuyOrderTime()));
                    //递延费特殊处理
                    stayDays = stayDays + 1;
                    log.info("");
                    log.info("开始处理 持仓订单id = {} 订单号 = {} 用户id = {} realName = {} 留仓天数 = {}", new Object[]{position
                            .getId(), position.getPositionSn(), position.getUserId(), position
                            .getNickName(), Integer.valueOf(stayDays)});
                    if (stayDays != 0) {
                        log.info(" 开始收取 {} 天 留仓费", Integer.valueOf(stayDays));
                        try {
                            closingStayTask(position, Integer.valueOf(stayDays));
                        } catch (Exception e) {
                            log.error("doClosingStayTask = ", e);
                        }
                    } else {
                        log.info("持仓订单 = {} ,持仓天数0天,不需要处理...", position.getId());
                    }
                    log.info("修改留仓费 处理结束。");
                    log.info("");
                }
                SiteTaskLog stl = new SiteTaskLog();
                stl.setTaskType("扣除留仓费");
                stl.setAddTime(new Date());
                stl.setIsSuccess(Integer.valueOf(0));
                stl.setTaskTarget("扣除留仓费,订单数量为" + userPositions.size());
                this.siteTaskLogMapper.insert(stl);
            }
        } else {
        if (userPositions.isEmpty()) {
            log.info("doClosingStayTask没有正在持仓的订单");
            return;
        }
        log.info("查询到正在持仓的订单数量 = {}", userPositions.size());
        SiteProduct siteProduct = iSiteProductService.getProductSetting();
        boolean respectHoliday = TradingDayUtil.isHolidayRestrictionEnabled(siteProduct);
        if (StayFeeUtil.shouldSkipStayFeeTaskToday(respectHoliday)) {
            log.info("节假日休市开关已开启且今日休市,跳过递延费扣款");
            return;
        }
        SiteSetting siteSetting = this.iSiteSettingService.getSiteSetting();
        if (siteSetting == null) {
            log.error("递延费扣款失败,网站设置表不存在");
            return;
        }
        int processed = 0;
        for (UserPosition position : userPositions) {
            log.info("开始处理递延费 持仓id={} 订单号={} 用户={}", position.getId(), position.getPositionSn(), position.getUserId());
            try {
                int billedDays = position.getOrderStayDays() == null ? 0 : position.getOrderStayDays();
                if (siteSetting.getStayMaxDays() != null && billedDays >= siteSetting.getStayMaxDays().intValue()) {
                    log.info("递延费已达最大天数{},强制平仓 positionSn={}", siteSetting.getStayMaxDays(), position.getPositionSn());
                    this.sell(position.getPositionSn(), 0);
                    continue;
                }
                if (!StayFeeUtil.isEligibleForCharge(position.getBuyOrderTime(), position.getOrderStayDays(), respectHoliday)) {
                    log.info("持仓订单={} 未到T+1递延费扣款日或今日已扣,跳过", position.getId());
                    continue;
                }
                closingStayTask(position, null);
                processed++;
            } catch (Exception e) {
                log.error("doClosingStayTask positionSn=" + position.getPositionSn(), e);
            }
        }
        SiteTaskLog stl = new SiteTaskLog();
        stl.setTaskType("扣除递延费");
        stl.setAddTime(new Date());
        stl.setIsSuccess(Integer.valueOf(0));
        stl.setTaskTarget("扣除递延费,持仓数量" + userPositions.size() + ",实际扣款" + processed);
        this.siteTaskLogMapper.insert(stl);
    }
    /*留仓到期强制平仓,每天15点执行*/
    /*递延费达到最大天数强制平仓*/
    public void expireStayUnwindTask() {
        List<UserPosition> userPositions = this.userPositionMapper.findAllStayPosition();
        if (userPositions.isEmpty()) {
            log.info("expireStayUnwindTask没有正在持仓的订单");
            return;
        }
        SiteProduct siteProduct = iSiteProductService.getProductSetting();
        boolean respectHoliday = TradingDayUtil.isHolidayRestrictionEnabled(siteProduct);
        if (StayFeeUtil.shouldSkipStayFeeTaskToday(respectHoliday)) {
            log.info("节假日休市开关已开启且今日休市,跳过递延费到期强平");
            return;
        }
        if (userPositions.size() > 0) {
            log.info("查询到正在持仓的订单数量 = {}", Integer.valueOf(userPositions.size()));
        SiteSetting siteSetting = this.iSiteSettingService.getSiteSetting();
        if (siteSetting == null || siteSetting.getStayMaxDays() == null) {
            return;
        }
        int maxDays = siteSetting.getStayMaxDays().intValue();
            SiteSetting siteSetting = this.iSiteSettingService.getSiteSetting();
            for (UserPosition position : userPositions) {
                int stayDays = GetStayDays.getDays(GetStayDays.getBeginDate(position.getBuyOrderTime()));
                log.info("");
                log.info("开始处理 持仓订单id = {} 订单号 = {} 用户id = {} realName = {} 留仓天数 = {}", new Object[]{position
                        .getId(), position.getPositionSn(), position.getUserId(), position
                        .getNickName(), Integer.valueOf(stayDays)});
                //留仓达到最大天数
                if (stayDays >= siteSetting.getStayMaxDays().intValue()) {
                    log.info(" 开始强平 {} 天", Integer.valueOf(stayDays));
                    try {
                        this.sell(position.getPositionSn(), 0);
                    } catch (Exception e) {
                        log.error("expireStayUnwindTask = ", e);
                    }
                } else {
                    log.info("持仓订单 = {} ,持仓天数0天,不需要处理...", position.getId());
        for (UserPosition position : userPositions) {
            int billedDays = position.getOrderStayDays() == null ? 0 : position.getOrderStayDays();
            if (billedDays >= maxDays) {
                log.info("递延费天数{}达上限{},强制平仓 positionSn={}", billedDays, maxDays, position.getPositionSn());
                try {
                    this.sell(position.getPositionSn(), 0);
                } catch (Exception e) {
                    log.error("expireStayUnwindTask positionSn=" + position.getPositionSn(), e);
                }
            }
        } else {
            log.info("doClosingStayTask没有正在持仓的订单");
        }
    }
    @Transactional
    public ServerResponse closingStayTask(UserPosition position, Integer stayDays) throws Exception {
        log.info("=================closingStayTask====================");
        log.info("修改留仓费,持仓id={},持仓天数={}", position.getId(), stayDays);
        log.info("=================closingStayTask 递延费扣款====================");
        SiteSetting siteSetting = this.iSiteSettingService.getSiteSetting();
        if (siteSetting == null) {
            log.error("修改留仓费出错,网站设置表不存在");
            return ServerResponse.createByErrorMsg("修改留仓费出错,网站设置表不存在");
            log.error("递延费扣款失败,网站设置表不存在");
            return ServerResponse.createByErrorMsg("递延费扣款失败,网站设置表不存在");
        }
        SiteProduct siteProduct = iSiteProductService.getProductSetting();
        boolean respectHoliday = TradingDayUtil.isHolidayRestrictionEnabled(siteProduct);
        int daysToCharge = StayFeeUtil.getDaysToChargeThisRun(
                position.getBuyOrderTime(), position.getOrderStayDays(), respectHoliday);
        if (daysToCharge <= 0) {
            return ServerResponse.createBySuccess();
        }
        BigDecimal stayFee = position.getOrderTotalPrice().multiply(siteSetting.getStayFee());
        PositionProfitVO profitVO = getPositionProfitVO(position);
        BigDecimal marketValue = profitVO.getOrderTotalPrice();
        BigDecimal billingMarketValue = StayFeeUtil.calcBillingMarketValue(marketValue);
        BigDecimal dailyFee = StayFeeUtil.calcDailyStayFee(marketValue, siteSetting.getStayFee());
        BigDecimal totalFee = dailyFee.multiply(new BigDecimal(daysToCharge)).setScale(2, RoundingMode.HALF_UP);
        log.info("持仓id={} 市值={} 计费基数={} 单日递延费={} 本次扣费天数={} 合计={}",
                position.getId(), marketValue, billingMarketValue, dailyFee, daysToCharge, totalFee);
        BigDecimal allStayFee = stayFee.multiply(new BigDecimal(stayDays.intValue()));
        if (totalFee.compareTo(BigDecimal.ZERO) <= 0) {
            return ServerResponse.createBySuccess();
        }
        log.info("总留仓费 = {}", allStayFee);
        User user = this.userMapper.selectByPrimaryKey(position.getUserId());
        if (user == null) {
            throw new Exception("递延费扣款失败,用户不存在");
        }
        BigDecimal enableAmt = user.getEnableAmt() == null ? BigDecimal.ZERO : user.getEnableAmt();
        if (enableAmt.compareTo(totalFee) < 0) {
            log.info("余额不足支付递延费{},可用{},强制平仓 positionSn={}", totalFee, enableAmt, position.getPositionSn());
            this.sell(position.getPositionSn(), 0);
            return ServerResponse.createBySuccess();
        }
        position.setOrderStayFee(allStayFee);
        position.setOrderStayDays(stayDays);
        BigDecimal userAmt = user.getUserAmt() == null ? BigDecimal.ZERO : user.getUserAmt();
        user.setUserAmt(userAmt.subtract(totalFee));
        UserFundUtil.debitUserBalance(user, totalFee);
        int userRows = this.userMapper.updateByPrimaryKeySelective(user);
        if (userRows <= 0) {
            throw new Exception("递延费扣款失败,更新用户余额出错");
        }
        BigDecimal all_profit = position.getAllProfitAndLose().subtract(allStayFee);
        position.setAllProfitAndLose(all_profit);
        BigDecimal accumulated = (position.getOrderStayFee() == null ? BigDecimal.ZERO : position.getOrderStayFee()).add(totalFee);
        int billedDays = (position.getOrderStayDays() == null ? 0 : position.getOrderStayDays()) + daysToCharge;
        position.setOrderStayFee(accumulated);
        position.setOrderStayDays(billedDays);
        int updateCount = this.userPositionMapper.updateByPrimaryKeySelective(position);
        if (updateCount > 0) {
            //核算代理收入-延递费
            iAgentAgencyFeeService.AgencyFeeIncome(3, position.getPositionSn());
            log.info("【closingStayTask收持仓费】修改持仓记录成功");
        } else {
            log.error("【closingStayTask收持仓费】修改持仓记录出错");
            throw new Exception("【closingStayTask收持仓费】修改持仓记录出错");
        if (updateCount <= 0) {
            throw new Exception("递延费扣款失败,更新持仓记录出错");
        }
        UserCashDetail ucd = new UserCashDetail();
        ucd.setPositionId(position.getId());
        ucd.setAgentId(user.getAgentId());
        ucd.setAgentName(user.getAgentName());
        ucd.setUserId(user.getId());
        ucd.setUserName(user.getRealName());
        ucd.setDeType("递延费");
        ucd.setDeAmt(totalFee.negate());
        ucd.setDeSummary("递延费扣款," + position.getStockCode() + "/" + position.getStockName()
                + ",持仓市值:" + marketValue + ",计费基数:" + billingMarketValue + ",单日:" + dailyFee
                + ",扣费天数:" + daysToCharge + ",本次扣费:" + totalFee + ",累计递延费:" + accumulated
                + ",递延天数:" + billedDays);
        ucd.setAddTime(new Date());
        ucd.setIsRead(Integer.valueOf(0));
        this.userCashDetailMapper.insert(ucd);
        log.info("=======================================================");
        iAgentAgencyFeeService.AgencyFeeIncome(3, position.getPositionSn());
        log.info("【递延费扣款成功】positionSn={} 天数={} 本次={} 累计={}", position.getPositionSn(), daysToCharge, totalFee, accumulated);
        return ServerResponse.createBySuccess();
    }
@@ -2123,10 +2267,18 @@
        adminPositionVO.setStockPlate(position.getStockPlate());
        PositionProfitVO positionProfitVO = getPositionProfitVO(position);
        adminPositionVO.setProfitAndLose(positionProfitVO.getProfitAndLose());
        adminPositionVO.setAllProfitAndLose(positionProfitVO.getAllProfitAndLose());
        adminPositionVO.setNow_price(positionProfitVO.getNowPrice());
        if (position.getStatus() != null && position.getStatus().intValue() == 2) {
            adminPositionVO.setProfitAndLose(position.getProfitAndLose());
            adminPositionVO.setAllProfitAndLose(position.getAllProfitAndLose());
            if (position.getSellOrderPrice() != null) {
                adminPositionVO.setNow_price(position.getSellOrderPrice().toPlainString());
            }
        } else {
            PositionProfitVO positionProfitVO = getPositionProfitVO(position);
            adminPositionVO.setProfitAndLose(positionProfitVO.getProfitAndLose());
            adminPositionVO.setAllProfitAndLose(positionProfitVO.getAllProfitAndLose());
            adminPositionVO.setNow_price(positionProfitVO.getNowPrice());
        }
        return adminPositionVO;
@@ -2165,10 +2317,18 @@
        agentPositionVO.setStockPlate(position.getStockPlate());
        PositionProfitVO positionProfitVO = getPositionProfitVO(position);
        agentPositionVO.setProfitAndLose(positionProfitVO.getProfitAndLose());
        agentPositionVO.setAllProfitAndLose(positionProfitVO.getAllProfitAndLose());
        agentPositionVO.setNow_price(positionProfitVO.getNowPrice());
        if (position.getStatus() != null && position.getStatus().intValue() == 2) {
            agentPositionVO.setProfitAndLose(position.getProfitAndLose());
            agentPositionVO.setAllProfitAndLose(position.getAllProfitAndLose());
            if (position.getSellOrderPrice() != null) {
                agentPositionVO.setNow_price(position.getSellOrderPrice().toPlainString());
            }
        } else {
            PositionProfitVO positionProfitVO = getPositionProfitVO(position);
            agentPositionVO.setProfitAndLose(positionProfitVO.getProfitAndLose());
            agentPositionVO.setAllProfitAndLose(positionProfitVO.getAllProfitAndLose());
            agentPositionVO.setNow_price(positionProfitVO.getNowPrice());
        }
        return agentPositionVO;
@@ -2216,6 +2376,22 @@
        userPositionVO.setAllProfitAndLoseStr(positionProfitVO.getAllProfitAndLoseStr());
        userPositionVO.setOrderTotalPrice(positionProfitVO.getOrderTotalPrice());
        if (position.getOrderLever() != null && position.getOrderLever() > 0
                && position.getBuyOrderPrice() != null && position.getOrderNum() != null) {
            userPositionVO.setBuyAmtAutual(
                    position.getBuyOrderPrice()
                            .multiply(new BigDecimal(position.getOrderNum()))
                            .divide(new BigDecimal(position.getOrderLever()), 2, RoundingMode.HALF_UP));
        }
        if (position.getSellOrderId() == null && Integer.valueOf(1).equals(position.getStatus())) {
            SiteProduct siteProduct = iSiteProductService.getProductSetting();
            boolean respectHoliday = TradingDayUtil.isHolidayRestrictionEnabled(siteProduct);
            long nextTs = StayFeeUtil.getNextStayFeeTimestamp(position.getBuyOrderTime(), position.getOrderStayDays(), respectHoliday);
            userPositionVO.setNextStayFeeTime(nextTs);
            userPositionVO.setStayFeeCountdownSec(StayFeeUtil.getStayFeeCountdownSec(nextTs));
            userPositionVO.setRespectHoliday(respectHoliday);
        }
        return userPositionVO;
    }
@@ -2228,16 +2404,20 @@
        BigDecimal orderTotalPrice = position.getOrderTotalPrice();
        if (position.getSellOrderId() != null) {
            BigDecimal subPrice = position.getSellOrderPrice().subtract(position.getBuyOrderPrice());
//            profitAndLose = subPrice.multiply(new BigDecimal(position.getOrderNum().intValue())).multiply(new BigDecimal(position.getOrderLever())).setScale(2,4);
            profitAndLose = subPrice.multiply(new BigDecimal(position.getOrderNum().intValue())).setScale(2,4);
            if ("买跌".equals(position.getOrderDirection())) {
                profitAndLose = profitAndLose.negate();
            if (Integer.valueOf(2).equals(position.getStatus())
                    && position.getProfitAndLose() != null
                    && position.getAllProfitAndLose() != null) {
                profitAndLose = position.getProfitAndLose();
                allProfitAndLose = position.getAllProfitAndLose();
            } else {
                BigDecimal subPrice = position.getSellOrderPrice().subtract(position.getBuyOrderPrice());
                profitAndLose = subPrice.multiply(new BigDecimal(position.getOrderNum().intValue())).setScale(2, 4);
                if ("买跌".equals(position.getOrderDirection())) {
                    profitAndLose = profitAndLose.negate();
                }
                allProfitAndLose = profitAndLose.subtract(position.getOrderFee()).subtract(position.getOrderSpread())
                        .subtract(position.getOrderStayFee()).subtract(position.getSpreadRatePrice());
            }
            allProfitAndLose = profitAndLose.subtract(position.getOrderFee()).subtract(position.getOrderSpread()).subtract(position.getOrderStayFee()).subtract(position.getSpreadRatePrice());
        } else {
            StockListVO stockListVO = new StockListVO();
            StockCoin stockCoin = new StockCoin();
@@ -2317,6 +2497,13 @@
        return ServerResponse.createBySuccess(pageInfo);
    }
    @Override
    public ServerResponse findPositionBySn(String positionSn) {
        UserPosition userPosition = userPositionMapper.findPositionBySn(positionSn);
        UserPositionVO userPositionVO = assembleUserPositionVO(userPosition);
        return ServerResponse.createBySuccess(userPositionVO);
    }
    /**
     * @Description: 新股轉持倉
     * @Param:
@@ -2373,7 +2560,7 @@
            userPosition.setOrderStayDays(1);
            userPosition.setOrderTotalPrice(userStockSubscribe.getBond());
            //            BigDecimal buy_fee_amt = buy_amt.multiply(siteSetting.getBuyFee()).setScale(2, 4);
            //            BigDecimal buy_fee_amt = TradeFeeUtil.calcBuyFee(buy_amt).setScale(2, 4);
            BigDecimal buy_fee_amt = new BigDecimal(0);
            log.info("用戶購買手續費(配資後總資金 * 百分比) = {}", buy_fee_amt);
            userPosition.setOrderFee(buy_fee_amt);
@@ -2495,7 +2682,7 @@
//        if (!pm_flag) {
//            return ServerResponse.createByErrorMsg("下單失敗,不在交易時段內");
//        }
        if (siteProduct.getHolidayDisplay()) {
        if (TradingDayUtil.shouldBlockTradingToday(siteProduct)) {
            return ServerResponse.createByErrorMsg("周末或节假日不能交易!");
        }
//        if (!Objects.equals(stockDz.getPassword(), password)) {
@@ -2506,17 +2693,17 @@
            return ServerResponse.createByErrorMsg("下单失败,当前股票不能交易");
        }
        List dbPosition = findPositionByStockCodeAndTimes(siteSetting.getBuySameTimes().intValue(), stockDz.getStockCode(), user.getId());
        if (dbPosition.size() >= siteSetting.getBuySameNums().intValue()) {
            return ServerResponse.createByErrorMsg("频繁交易," + siteSetting.getBuySameTimes() + "分钟内同一股票持仓不得超过" + siteSetting
                    .getBuySameNums() + "条");
        }
//        List dbPosition = findPositionByStockCodeAndTimes(siteSetting.getBuySameTimes().intValue(), stockDz.getStockCode(), user.getId());
//        if (dbPosition.size() >= siteSetting.getBuySameNums().intValue()) {
//            return ServerResponse.createByErrorMsg("频繁交易," + siteSetting.getBuySameTimes() + "分钟内同一股票持仓不得超过" + siteSetting
//                    .getBuySameNums() + "条");
//        }
        Integer transNum = findPositionNumByTimes(siteSetting.getBuyNumTimes().intValue(), user.getId());
        if (transNum.intValue() / 100 >= siteSetting.getBuyNumLots().intValue()) {
            return ServerResponse.createByErrorMsg("频繁交易," + siteSetting
                    .getBuyNumTimes() + "分钟内不能超过" + siteSetting.getBuyNumLots() + "手");
        }
//        Integer transNum = findPositionNumByTimes(siteSetting.getBuyNumTimes().intValue(), user.getId());
//        if (transNum.intValue() / 100 >= siteSetting.getBuyNumLots().intValue()) {
//            return ServerResponse.createByErrorMsg("频繁交易," + siteSetting
//                    .getBuyNumTimes() + "分钟内不能超过" + siteSetting.getBuyNumLots() + "手");
//        }
        if (num < stockDz.getStockNum().intValue()) {
            return ServerResponse.createByErrorMsg("下单失败,购买数量最小为" + stockDz.getStockNum() + "股");
@@ -2569,45 +2756,45 @@
        log.info("當前漲跌幅 = {} % , 漲停幅度 = {} %", Double.valueOf(stock_crease), ztRate);
        if (stockDz.getStockPlate() == null || StringUtils.isEmpty(stockDz.getStockPlate())) {
            int maxcrease = siteSetting.getCreaseMaxPercent().intValue();
            if (stock_crease > 0.0D &&
                    stock_crease >= maxcrease) {
                return ServerResponse.createByErrorMsg("下单失败,股票当前涨幅:" + stock_crease + ",大于最大涨幅:" + maxcrease);
            }
            if (stock_crease < 0.0D &&
                    -stock_crease > maxcrease) {
                return ServerResponse.createByErrorMsg("下单失败,股票当前跌幅:" + stock_crease + ",大于最大跌幅:" + maxcrease);
            }
        } else if ("创业".equals(stockDz.getStockPlate())) {
            int maxcrease = siteSetting.getCyCreaseMaxPercent().intValue();
            if (stock_crease > 0.0D &&
                    stock_crease >= maxcrease) {
                return ServerResponse.createByErrorMsg("下单失败,创业股当前涨幅:" + stock_crease + ",大于最大涨幅:" + maxcrease);
            }
            if (stock_crease < 0.0D &&
                    -stock_crease > maxcrease) {
                return ServerResponse.createByErrorMsg("下单失败,创业股当前跌幅:" + stock_crease + ",大于最大跌幅:" + maxcrease);
            }
        } else {
            int maxcrease = siteSetting.getKcCreaseMaxPercent().intValue();
            if (stock_crease > 0.0D &&
                    stock_crease >= maxcrease) {
                return ServerResponse.createByErrorMsg("下单失败,科创股当前涨幅:" + stock_crease + ",大于最大涨幅:" + maxcrease);
            }
            if (stock_crease < 0.0D &&
                    -stock_crease > maxcrease) {
                return ServerResponse.createByErrorMsg("下单失败,科创股当前跌幅:" + stock_crease + ",大于最大跌幅:" + maxcrease);
            }
        }
//        if (stockDz.getStockPlate() == null || StringUtils.isEmpty(stockDz.getStockPlate())) {
//
//            int maxcrease = siteSetting.getCreaseMaxPercent().intValue();
//            if (stock_crease > 0.0D &&
//                    stock_crease >= maxcrease) {
//                return ServerResponse.createByErrorMsg("下单失败,股票当前涨幅:" + stock_crease + ",大于最大涨幅:" + maxcrease);
//            }
//
//            if (stock_crease < 0.0D &&
//                    -stock_crease > maxcrease) {
//                return ServerResponse.createByErrorMsg("下单失败,股票当前跌幅:" + stock_crease + ",大于最大跌幅:" + maxcrease);
//
//            }
//
//        } else if ("创业".equals(stockDz.getStockPlate())) {
//
//            int maxcrease = siteSetting.getCyCreaseMaxPercent().intValue();
//            if (stock_crease > 0.0D &&
//                    stock_crease >= maxcrease) {
//                return ServerResponse.createByErrorMsg("下单失败,创业股当前涨幅:" + stock_crease + ",大于最大涨幅:" + maxcrease);
//            }
//
//            if (stock_crease < 0.0D &&
//                    -stock_crease > maxcrease) {
//                return ServerResponse.createByErrorMsg("下单失败,创业股当前跌幅:" + stock_crease + ",大于最大跌幅:" + maxcrease);
//            }
//        } else {
//
//            int maxcrease = siteSetting.getKcCreaseMaxPercent().intValue();
//            if (stock_crease > 0.0D &&
//                    stock_crease >= maxcrease) {
//                return ServerResponse.createByErrorMsg("下单失败,科创股当前涨幅:" + stock_crease + ",大于最大涨幅:" + maxcrease);
//            }
//
//            if (stock_crease < 0.0D &&
//                    -stock_crease > maxcrease) {
//                return ServerResponse.createByErrorMsg("下单失败,科创股当前跌幅:" + stock_crease + ",大于最大跌幅:" + maxcrease);
//            }
//        }
        ServerResponse serverResponse = this.iStockService.selectRateByDaysAndStockCode(stockDz.getStockCode(), siteSetting.getStockDays().intValue());
@@ -2618,10 +2805,10 @@
        log.info("股票 {} , {} 天內 漲幅 {} , 設置的漲幅 = {}", new Object[]{stockDz.getStockCode(), siteSetting
                .getStockDays(), daysRate, siteSetting.getStockRate()});
        if (daysRate != null && siteSetting.getStockRate().compareTo(daysRate) == -1) {
            return serverResponse.createByErrorMsg(siteSetting.getStockDays() + "天内涨幅超过 " + siteSetting
                    .getStockRate() + "不能交易");
        }
//        if (daysRate != null && siteSetting.getStockRate().compareTo(daysRate) == -1) {
//            return serverResponse.createByErrorMsg(siteSetting.getStockDays() + "天内涨幅超过 " + siteSetting
//                    .getStockRate() + "不能交易");
//        }
        //BigDecimal buy_amt = now_price.multiply(new BigDecimal(buyNum.intValue())).divide(new BigDecimal(lever.intValue())).setScale(2, 4);
@@ -2647,11 +2834,10 @@
        }
        int compareUserAmtInt = user_enable_amt.compareTo(buy_amt_autual);
        log.info("用戶可用金額 = {}  實際購買金額 =  {}", user_enable_amt, buy_amt_autual);
        log.info("比較 用戶金額 和 實際 購買金額 =  {}", Integer.valueOf(compareUserAmtInt));
        if (compareUserAmtInt == -1) {
            return ServerResponse.createByErrorMsg("下单失败,融资可用金额小于" + buy_amt_autual + "元");
        BigDecimal buy_fee_amt_dz = TradeFeeUtil.calcBuyFee(buy_amt_autual, siteSetting.getBuyFee());
        BigDecimal buy_debit_dz = TradeFeeUtil.calcBuyDebit(buy_amt_autual, buy_fee_amt_dz);
        if (user_enable_amt.compareTo(buy_debit_dz) == -1) {
            return ServerResponse.createByErrorMsg("下单失败,融资可用金额小于" + buy_debit_dz + "元(含保证金及手续费)");
        }
//        if (user.getUserIndexAmt().compareTo(new BigDecimal("0")) == -1) {
@@ -2688,7 +2874,7 @@
        userPosition.setOrderStayDays(1);
        BigDecimal buy_fee_amt = buy_amt.multiply(siteSetting.getBuyFee()).setScale(2, 4);
        BigDecimal buy_fee_amt = buy_fee_amt_dz;
        log.info("用戶購買手續費(配資後總資金 * 百分比) = {}", buy_fee_amt);
        userPosition.setOrderFee(buy_fee_amt);
@@ -2721,36 +2907,14 @@
        log.info("--------------购买逻辑股票数据 buyDz  stock------" + new Gson().toJson(userPosition));
        int insertPositionCount = 0;
        this.userPositionMapper.insert(userPosition);
        insertPositionCount = userPosition.getId();
        if (insertPositionCount > 0) {
        if (userPosition.getId() != null && userPosition.getId() > 0) {
            //修改大宗剩余
            stockDz.setStockShare(stockDz.getStockShare() - num);
            stockDz.setStockSurplus(stockDz.getStockSurplus() + num);
            stockDzMapper.updateById(stockDz);
            //修改用戶可用余額= 當前余額-下單金額-買入手續費-印花稅-點差費
            //BigDecimal reckon_enable = user_enable_amt.subtract(buy_amt_autual).subtract(buy_fee_amt).subtract(buy_yhs_amt).subtract(spread_rate_amt);
            //修改用戶可用余額= 當前余額-下單總金額
            BigDecimal reckon_enable = user_enable_amt.subtract(buy_amt_autual);
            //修改用戶可取余額=當前可取余額-下單總金額
            int compareUserWithdrawAmtInt = user_enable_withdraw_amt.compareTo(buy_amt_autual);
            if (compareUserWithdrawAmtInt == -1) {
                //若可取余額小於下單總額,但是可用余額充足,令可取余額為0
                user.setEnaleWithdrawAmt(BigDecimal.ZERO);
            } else {
                user_enable_withdraw_amt = user_enable_withdraw_amt.subtract(buy_amt_autual);
                user.setEnaleWithdrawAmt(user_enable_withdraw_amt);
            }
            user.setEnableAmt(reckon_enable);
            int updateUserCount = this.userMapper.updateByPrimaryKeySelective(user);
            if (updateUserCount > 0) {
                log.info("【用戶交易下單】修改用戶金額成功");
            } else {
                log.error("用戶交易下單】修改用戶金額出錯");
                throw new Exception("用戶交易下單】修改用戶金額出錯");
            }
            //核算代理收入-入倉手續費
            deductUserEnableOnBuy(user, buy_debit_dz, buy_amt_autual, buy_fee_amt_dz, userPosition);
            syncUserCacheAfterTrade(request);
            iAgentAgencyFeeService.AgencyFeeIncome(1, userPosition.getPositionSn());
            log.info("【用戶交易下單】保存持倉記錄成功");
        } else {
@@ -2819,7 +2983,7 @@
        if (!am_flag && !pm_flag) {
            return ServerResponse.createByErrorMsg("下單失敗,不在交易時段內");
        }
        if (siteProduct.getHolidayDisplay()) {
        if (TradingDayUtil.shouldBlockTradingToday(siteProduct)) {
            return ServerResponse.createByErrorMsg("周末或節假日不能交易!");
        }
@@ -2978,11 +3142,10 @@
        }
        int compareUserAmtInt = user_enable_amt.compareTo(buy_amt_autual);
        log.info("用戶可用金額 = {}  實際購買金額 =  {}", user_enable_amt, buy_amt_autual);
        log.info("比較 用戶金額 和 實際 購買金額 =  {}", Integer.valueOf(compareUserAmtInt));
        if (compareUserAmtInt == -1) {
            return ServerResponse.createByErrorMsg("下單失敗,可用金額小於" + buy_amt_autual + "元");
        BigDecimal buy_fee_amt = TradeFeeUtil.calcBuyFee(buy_amt_autual, siteSetting.getBuyFee());
        BigDecimal buy_debit = TradeFeeUtil.calcBuyDebit(buy_amt_autual, buy_fee_amt);
        if (user_enable_amt.compareTo(buy_debit) == -1) {
            return ServerResponse.createByErrorMsg("下單失敗,可用金額小於" + buy_debit + "元(含保证金及手续费)");
        }
//        if (user.getUserIndexAmt().compareTo(new BigDecimal("0")) == -1) {
@@ -3036,7 +3199,6 @@
        userPosition.setOrderStayDays(1);
        BigDecimal buy_fee_amt = buy_amt.multiply(siteSetting.getBuyFee()).setScale(2, 4);
        log.info("用戶購買手續費(配資後總資金 * 百分比) = {}", buy_fee_amt);
        userPosition.setOrderFee(buy_fee_amt);
@@ -3070,31 +3232,10 @@
        log.info("--------------购买逻辑股票数据 buyVipQc  stock------" + new Gson().toJson(userPosition));
        int insertPositionCount = 0;
        this.userPositionMapper.insert(userPosition);
        insertPositionCount = userPosition.getId();
        if (insertPositionCount > 0) {
            //修改用戶可用余額= 當前余額-下單金額-買入手續費-印花稅-點差費
            //BigDecimal reckon_enable = user_enable_amt.subtract(buy_amt_autual).subtract(buy_fee_amt).subtract(buy_yhs_amt).subtract(spread_rate_amt);
            //修改用戶可用余額= 當前余額-下單總金額
            BigDecimal reckon_enable = user_enable_amt.subtract(buy_amt_autual);
            //修改用戶可取余額=當前可取余額-下單總金額
            int compareUserWithdrawAmtInt = user_enable_withdraw_amt.compareTo(buy_amt_autual);
            if (compareUserWithdrawAmtInt < 0) {
                //若可取余額小於下單總額,但是可用余額充足,令可取余額為0
                user.setEnaleWithdrawAmt(BigDecimal.ZERO);
            } else {
                user_enable_withdraw_amt = user_enable_withdraw_amt.subtract(buy_amt_autual);
                user.setEnaleWithdrawAmt(user_enable_withdraw_amt);
            }
            user.setEnableAmt(reckon_enable);
            int updateUserCount = this.userMapper.updateByPrimaryKeySelective(user);
            if (updateUserCount > 0) {
                log.info("【用戶交易下單】修改用戶金額成功");
            } else {
                log.error("用戶交易下單】修改用戶金額出錯");
                throw new Exception("用戶交易下單】修改用戶金額出錯");
            }
        if (userPosition.getId() != null && userPosition.getId() > 0) {
            deductUserEnableOnBuy(user, buy_debit, buy_amt_autual, buy_fee_amt, userPosition);
            syncUserCacheAfterTrade(request);
            //核算代理收入-入倉手續費
            //iAgentAgencyFeeService.AgencyFeeIncome(1, userPosition.getPositionSn());
            log.info("【用戶交易下單】保存持倉記錄成功");