1
zj
7 days ago cd969de1f50903ca87deda67bff2d6fcc35b4107
src/main/java/com/nq/service/impl/UserPositionServiceImpl.java
@@ -299,10 +299,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())) {
@@ -388,7 +388,7 @@
        }
        BigDecimal buy_fee_amt = TradeFeeUtil.calcBuyFee(buy_amt);
        BigDecimal buy_fee_amt = TradeFeeUtil.calcBuyFee(buy_amt_autual);
        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);
@@ -479,6 +479,7 @@
        userPosition.setOrderStayDays(Integer.valueOf(0));
        userPosition.setOrderStayFee(new BigDecimal("0"));
        userPosition.setStatus(1);
        this.userPositionMapper.insert(userPosition);
        if (userPosition.getId() != null && userPosition.getId() > 0) {
@@ -496,10 +497,16 @@
    @Override
    public ServerResponse fee(Integer buyNum, BigDecimal nowPrice) {
    public ServerResponse fee(Integer buyNum, BigDecimal nowPrice, Integer lever) {
        int leverValue = (lever == null || lever <= 0) ? 1 : lever;
        BigDecimal buy_amt = nowPrice.multiply(new BigDecimal(buyNum.intValue()));
        BigDecimal buy_fee_amt = TradeFeeUtil.calcBuyFee(buy_amt).setScale(2, 4);
        BigDecimal buy_fee_amt = TradeFeeUtil.calcBuyFeeByNotional(buy_amt, leverValue).setScale(2, 4);
        return ServerResponse.createBySuccess(buy_fee_amt);
    }
    /** T+0:可用资金变动时同步调整可取资金 */
    private void applyEnableAndWithdrawChange(User user, BigDecimal enableBefore, BigDecimal enableAfter) {
        UserFundUtil.applyEnableDelta(user, enableBefore, enableAfter);
    }
    /** 下单从两融可用资金扣除:保证金 + 买入手续费 */
@@ -788,7 +795,7 @@
                    .getBuyMaxAmtPercent().multiply(new BigDecimal("100")) + "%");
        }
        BigDecimal buy_fee_amt = TradeFeeUtil.calcBuyFee(buy_amt);
        BigDecimal buy_fee_amt = TradeFeeUtil.calcBuyFee(buy_amt_autual);
        BigDecimal buy_debit = TradeFeeUtil.calcBuyDebit(buy_amt_autual, buy_fee_amt);
        if (user_enable_amt.compareTo(buy_debit) == -1) {
            return ServerResponse.createByErrorMsg("挂单失败,融资可用金额小于" + buy_debit + "元(含保证金及手续费)");
@@ -950,6 +957,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 (siteProduct != null && siteProduct.getHolidayDisplay()) {
            return ServerResponse.createByErrorMsg("周末或节假日不能交易!");
        }
        return null;
    }
    public ServerResponse sell(String positionSn, int doType) throws Exception {
        log.info("【用戶交易平倉】 positionSn = {} , dotype = {}", positionSn, Integer.valueOf(doType));
@@ -962,54 +1012,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() && siteProduct.getTranWithdrawDisplay()) {
                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) {
@@ -1028,10 +1041,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()) {
@@ -1110,10 +1122,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();
@@ -1187,7 +1199,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("【用戶平倉】修改用戶金額成功");
@@ -1204,7 +1216,7 @@
        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.setDeSummary("卖出股票," + userPosition.getStockCode() + "/" + userPosition.getStockName() + ",占用本金:" + freez_amt + ",总手续费:" + order_fee_all + ",盈亏:" + profitLoss + ",总盈亏:" + all_profit);
        ucd.setAddTime(new Date());
        ucd.setIsRead(Integer.valueOf(0));
@@ -1265,23 +1277,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) {
@@ -1299,8 +1304,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;
@@ -1364,7 +1370,9 @@
        BigDecimal user_enable_amt = user.getEnableAmt();
        log.info("用戶原本總資金 = {} , 可用 = {}", user_all_amt, user_enable_amt);
        BigDecimal buy_fee_amt = TradeFeeUtil.calcBuyFee(all_buy_amt);
        BigDecimal partialMargin = all_buy_amt.divide(
                new BigDecimal(userPosition.getOrderLever()), 2, RoundingMode.HALF_UP);
        BigDecimal buy_fee_amt = TradeFeeUtil.calcBuyFee(partialMargin);
        log.info("買入手續費 = {}", buy_fee_amt);
        BigDecimal orderSpread = all_buy_amt.multiply(siteSetting.getDutyFee()).setScale(2, 4);
@@ -1427,7 +1435,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("【用戶平倉】修改用戶金額成功");
@@ -1444,7 +1452,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));
@@ -1537,7 +1545,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("【用户平仓】修改用户金额成功");
@@ -1921,7 +1929,7 @@
        BigDecimal buy_amt_autual = buy_amt.divide(new BigDecimal(lever.intValue()), 2, 4);
        BigDecimal buy_fee_amt_check = TradeFeeUtil.calcBuyFee(buy_amt);
        BigDecimal buy_fee_amt_check = TradeFeeUtil.calcBuyFee(buy_amt_autual);
        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);
@@ -1982,8 +1990,8 @@
        userPosition.setOrderStayDays(1);
        BigDecimal buy_fee_amt = TradeFeeUtil.calcBuyFee(buy_amt).setScale(2, 4);
        log.info("创建模拟持仓 手续费(配资后总资金 * 百分比) = {}", buy_fee_amt);
        BigDecimal buy_fee_amt = TradeFeeUtil.calcBuyFee(buy_amt_autual).setScale(2, 4);
        log.info("创建模拟持仓 手续费(保证金 * 百分比) = {}", buy_fee_amt);
        userPosition.setOrderFee(buy_fee_amt);
@@ -2026,6 +2034,7 @@
        userPosition.setOrderStayDays(Integer.valueOf(0));
        userPosition.setOrderStayFee(new BigDecimal("0"));
        userPosition.setSpreadRatePrice(new BigDecimal("0"));
        userPosition.setStatus(1);
        this.userPositionMapper.insert(userPosition);
        if (userPosition.getId() == null || userPosition.getId() <= 0) {
@@ -2213,10 +2222,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;
@@ -2255,10 +2272,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;
@@ -2306,6 +2331,13 @@
        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));
        }
        return userPositionVO;
    }
@@ -2318,16 +2350,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();
@@ -2744,7 +2780,7 @@
        }
        BigDecimal buy_fee_amt_dz = TradeFeeUtil.calcBuyFee(buy_amt);
        BigDecimal buy_fee_amt_dz = TradeFeeUtil.calcBuyFee(buy_amt_autual);
        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 + "元(含保证金及手续费)");
@@ -3052,7 +3088,7 @@
        }
        BigDecimal buy_fee_amt = TradeFeeUtil.calcBuyFee(buy_amt);
        BigDecimal buy_fee_amt = TradeFeeUtil.calcBuyFee(buy_amt_autual);
        BigDecimal buy_debit = TradeFeeUtil.calcBuyDebit(buy_amt_autual, buy_fee_amt);
        if (user_enable_amt.compareTo(buy_debit) == -1) {
            return ServerResponse.createByErrorMsg("下單失敗,可用金額小於" + buy_debit + "元(含保证金及手续费)");