1
zj
8 days ago cd969de1f50903ca87deda67bff2d6fcc35b4107
src/main/java/com/nq/service/impl/UserPositionServiceImpl.java
@@ -504,6 +504,11 @@
        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 {
@@ -966,6 +971,35 @@
        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));
@@ -978,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) {
@@ -1202,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("【用戶平倉】修改用戶金額成功");
@@ -1219,7 +1216,7 @@
        ucd.setUserName(user.getRealName());
        ucd.setDeType("总盈亏");
        ucd.setDeAmt(all_profit);
        ucd.setDeSummary("卖出股票," + userPosition.getStockCode() + "/" + userPosition.getStockName() + ",占用本金:" + freez_amt + ",总手续费:" + all_fee_amt + ",盈亏:" + 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));
@@ -1280,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) {
@@ -1445,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("【用戶平倉】修改用戶金額成功");
@@ -1462,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));
@@ -1555,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("【用户平仓】修改用户金额成功");
@@ -2232,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;
@@ -2274,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;
@@ -2325,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;
    }
@@ -2337,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();