From cd969de1f50903ca87deda67bff2d6fcc35b4107 Mon Sep 17 00:00:00 2001
From: zj <1772600164@qq.com>
Date: Sun, 07 Jun 2026 02:42:32 +0800
Subject: [PATCH] 1

---
 src/main/java/com/nq/service/impl/UserPositionServiceImpl.java | 1287 +++++++++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 1,003 insertions(+), 284 deletions(-)

diff --git a/src/main/java/com/nq/service/impl/UserPositionServiceImpl.java b/src/main/java/com/nq/service/impl/UserPositionServiceImpl.java
index c6f7764..c37f859 100644
--- a/src/main/java/com/nq/service/impl/UserPositionServiceImpl.java
+++ b/src/main/java/com/nq/service/impl/UserPositionServiceImpl.java
@@ -1,6 +1,7 @@
 package com.nq.service.impl;
 
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
 import com.google.gson.Gson;
 import com.nq.dao.*;
 import com.nq.pojo.*;
@@ -10,6 +11,8 @@
 import com.google.common.collect.Lists;
 import com.nq.common.ServerResponse;
 import com.nq.utils.*;
+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;
@@ -28,13 +31,18 @@
 import net.sf.json.JSONObject;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.beans.BeanUtils;
 import org.springframework.stereotype.Service;
 
 
 import java.math.BigDecimal;
 import java.math.RoundingMode;
 import java.sql.Timestamp;
+import java.time.LocalDate;
+import java.time.YearMonth;
+import java.time.format.DateTimeFormatter;
 import java.util.*;
+import java.util.stream.Collectors;
 import javax.servlet.http.HttpServletRequest;
 
 import org.apache.commons.lang3.StringUtils;
@@ -105,6 +113,7 @@
     StockDzMapper stockDzMapper;
 
 
+    @Override
     @Transactional
     public ServerResponse buy(Integer stockId, Integer buyNum, Integer buyType, Integer lever, BigDecimal profitTarget, BigDecimal stopTarget, HttpServletRequest request) throws Exception {
 
@@ -290,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())) {
@@ -379,11 +388,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);
+        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) {
@@ -439,7 +449,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);
 
@@ -470,34 +479,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("用户交易下单】保存持仓记录出错");
@@ -505,6 +493,424 @@
         }
 
         return ServerResponse.createBySuccess("下单成功");
+    }
+
+
+    @Override
+    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.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);
+    }
+
+    /** 下单从两融可用资金扣除:保证金 + 买入手续费 */
+    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 {
+
+        // 判断周末不能买
+        Date today = new Date();
+        Calendar c = Calendar.getInstance();
+        c.setTime(today);
+        /*实名认证开关开启*/
+        SiteProduct siteProduct = iSiteProductService.getProductSetting();
+        User user = this.iUserService.getCurrentRefreshUser(request);
+
+        if (siteProduct.getRealNameDisplay() && (StringUtils.isBlank(user.getRealName()) || StringUtils.isBlank(user.getIdCard()))) {
+            return ServerResponse.createByErrorMsg("挂单失败,请先实名认证");
+        }
+        BigDecimal user_enable_amt = user.getEnableAmt();
+        BigDecimal user_enable_withdraw_amt = user.getEnaleWithdrawAmt();
+        log.info("用户 {} 下单,股票id = {} ,数量 = {} , 方向 = {} , 杠杆 = {}", new Object[]{user
+                .getId(), stockId, buyNum, buyType, lever});
+        if (siteProduct.getRealNameDisplay() && user.getIsLock().intValue() == 1) {
+            return ServerResponse.createByErrorMsg("挂单失败,账户已被锁定");
+        }
+
+        SiteSetting siteSetting = this.iSiteSettingService.getSiteSetting();
+        if (siteSetting == null) {
+            log.error("下单出错,网站设置表不存在");
+            return ServerResponse.createByErrorMsg("挂单失败,系统设置错误");
+        }
+        Stock stock = null;
+        ServerResponse stock_res = this.iStockService.findStockById(stockId);
+        if (!stock_res.isSuccess()) {
+            return ServerResponse.createByErrorMsg("挂单失败,股票代码错误");
+        }
+        stock = (Stock) stock_res.getData();
+        log.info("--------------购买逻辑股票数据 buy  stock------" + new Gson().toJson(stock));
+
+        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));
+        //TODO
+//        if (!siteProduct.getTranWithdrawDisplay()) {
+//            return ServerResponse.createByErrorMsg("当前交易关闭");
+//        }
+        if (!am_flag && !pm_flag && siteProduct.getTranWithdrawDisplay()) {
+            return ServerResponse.createByErrorMsg("挂单失败,不在交易时段内");
+        }
+        if (siteProduct.getHolidayDisplay() && siteProduct.getTranWithdrawDisplay()) {
+            return ServerResponse.createByErrorMsg("周末或节假日不能交易!");
+        }
+
+
+        if (stock.getIsLock().intValue() != 0) {
+            return ServerResponse.createByErrorMsg("挂单失败,当前股票不能交易");
+        }
+
+        List dbPosition = findPositionByStockCodeAndTimes(siteSetting.getBuySameTimes().intValue(), stock
+                .getStockCode(), user.getId());
+        if (dbPosition.size() >= siteSetting.getBuySameNums().intValue() && siteProduct.getTranWithdrawDisplay()) {
+            return ServerResponse.createByErrorMsg("频繁交易," + siteSetting.getBuySameTimes() + "分钟内同一股票持仓不得超过" + siteSetting
+                    .getBuySameNums() + "条");
+        }
+
+
+        if (buyNum.intValue() < siteSetting.getBuyMinNum().intValue()) {
+            return ServerResponse.createByErrorMsg("挂单失败,购买数量小于" + siteSetting
+                    .getBuyMinNum() + "股");
+        }
+        if (buyNum.intValue() > siteSetting.getBuyMaxNum().intValue()) {
+            return ServerResponse.createByErrorMsg("挂单失败,购买数量大于" + siteSetting
+                    .getBuyMaxNum() + "股");
+        }
+        BigDecimal now_price;
+        StockListVO stockListVO = new StockListVO();
+        //股票类型 现价 数据源的处理
+        stockListVO = SinaStockApi.assembleLideStockListVO(LiDeDataUtils.getStock(stock.getStockCode()));
+        if (ObjectUtils.isEmpty(stockListVO)) {
+            stockListVO = SinaStockApi.assembleStockListVO(SinaStockApi.getSinaStock(stock.getStockGid()));
+        }
+        now_price = new BigDecimal(stockListVO.getNowPrice());
+        if (now_price.compareTo(new BigDecimal("0")) == 0) {
+            return ServerResponse.createByErrorMsg("报价0,请稍后再试");
+        }
+        double stock_crease = stockListVO.getHcrate().doubleValue();
+        BigDecimal maxRisePercent = new BigDecimal("0");
+        if (stock.getStockPlate() != null) {
+
+            maxRisePercent = new BigDecimal("0.2");
+            log.info("【科创股票】");
+        } else {
+            maxRisePercent = new BigDecimal("0.1");
+            log.info("【普通A股】");
+        }
+
+//        if (stockListVO.getName().startsWith("ST") || stockListVO.getName().endsWith("退")) {
+//            return ServerResponse.createByErrorMsg("ST和已退市的股票不能入仓");
+//        }
+
+        BigDecimal zsPrice = new BigDecimal(stockListVO.getPreclose_px());
+
+        BigDecimal ztPrice = zsPrice.multiply(maxRisePercent).add(zsPrice);
+        ztPrice = ztPrice.setScale(2, 4);
+        BigDecimal chaPrice = ztPrice.subtract(zsPrice);
+
+        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);
+//            }
+//        }
+        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() + "不能交易");
+//        }
+
+        BigDecimal buy_amt = now_price.multiply(new BigDecimal(buyNum.intValue()));
+
+        BigDecimal buy_amt_autual = buy_amt.divide(new BigDecimal(lever.intValue()), 2, 4);
+
+
+        int compareInt = buy_amt_autual.compareTo(new BigDecimal(siteSetting.getBuyMinAmt().intValue()));
+        if (compareInt == -1) {
+            return ServerResponse.createByErrorMsg("挂单失败,购买金额小于" + siteSetting
+                    .getBuyMinAmt() + "元");
+        }
+
+        BigDecimal max_buy_amt = user_enable_amt.multiply(siteSetting.getBuyMaxAmtPercent());
+        int compareCwInt = buy_amt_autual.compareTo(max_buy_amt);
+        if (compareCwInt == 1) {
+            return ServerResponse.createByErrorMsg("挂单失败,不能超过可用资金的" + siteSetting
+                    .getBuyMaxAmtPercent().multiply(new BigDecimal("100")) + "%");
+        }
+
+        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 + "元(含保证金及手续费)");
+        }
+        if (user.getUserIndexAmt().compareTo(new BigDecimal("0")) == -1) {
+            return ServerResponse.createByErrorMsg("失败,指数总资金小于0");
+        }
+        UserPosition userPosition = new UserPosition();
+        //挂单
+        userPosition.setStatus(0);
+        if (profitTarget != null && profitTarget.compareTo(new BigDecimal("0")) > 0) {
+            userPosition.setProfitTargetPrice(profitTarget);
+        }
+        if (stopTarget != null && stopTarget.compareTo(new BigDecimal("0")) > 0) {
+            userPosition.setStopTargetPrice(stopTarget);
+        }
+        userPosition.setPositionType(user.getAccountType());
+        userPosition.setPositionSn(KeyUtils.getUniqueKey());
+        userPosition.setUserId(user.getId());
+        userPosition.setNickName(user.getRealName());
+        userPosition.setAgentId(user.getAgentId());
+        userPosition.setStockCode(stock.getStockCode());
+        userPosition.setStockName(stock.getStockName());
+        userPosition.setStockGid(stock.getStockGid());
+        userPosition.setStockSpell(stock.getStockSpell());
+        userPosition.setBuyOrderId(GeneratePosition.getPositionId());
+        userPosition.setBuyOrderTime(new Date());
+        userPosition.setBuyOrderPrice(now_price);
+        userPosition.setOrderDirection((buyType.intValue() == 0) ? "买涨" : "买跌");
+        userPosition.setOrderNum(buyNum);
+        if (stock.getStockPlate() != null) {
+            userPosition.setStockPlate(stock.getStockPlate());
+        }
+        userPosition.setIsLock(Integer.valueOf(0));
+        userPosition.setOrderLever(lever);
+        userPosition.setOrderTotalPrice(buy_amt);
+        //递延费特殊处理
+        BigDecimal stayFee = userPosition.getOrderTotalPrice().multiply(siteSetting.getStayFee());
+        BigDecimal allStayFee = stayFee.multiply(new BigDecimal(1));
+        userPosition.setOrderStayFee(allStayFee);
+        userPosition.setOrderStayDays(1);
+        log.info("用户购买手续费(配资后总资金 * 百分比) = {}", buy_fee_amt);
+        userPosition.setOrderFee(buy_fee_amt);
+        BigDecimal buy_yhs_amt = buy_amt.multiply(siteSetting.getDutyFee()).setScale(2, 4);
+        log.info("用户购买印花税(配资后总资金 * 百分比) = {}", buy_yhs_amt);
+        userPosition.setOrderSpread(buy_yhs_amt);
+        SiteSpread siteSpread = iSiteSpreadService.findSpreadRateOne(new BigDecimal(stock_crease), buy_amt, stock.getStockCode(), now_price);
+        BigDecimal spread_rate_amt = new BigDecimal("0");
+        if (siteSpread != null) {
+            spread_rate_amt = buy_amt.multiply(siteSpread.getSpreadRate()).setScale(2, 4);
+            log.info("用户购买点差费(配资后总资金 * 百分比{}) = {}", siteSpread.getSpreadRate(), spread_rate_amt);
+        } else {
+            log.info("用户购买点差费(配资后总资金 * 百分比{}) = {}", "设置异常", spread_rate_amt);
+        }
+        userPosition.setSpreadRatePrice(spread_rate_amt);
+        BigDecimal profit_and_lose = new BigDecimal("0");
+        userPosition.setProfitAndLose(profit_and_lose);
+        BigDecimal all_profit_and_lose = profit_and_lose.subtract(buy_fee_amt).subtract(buy_yhs_amt).subtract(spread_rate_amt);
+        userPosition.setAllProfitAndLose(all_profit_and_lose);
+        userPosition.setOrderStayDays(Integer.valueOf(0));
+        userPosition.setOrderStayFee(new BigDecimal("0"));
+        this.userPositionMapper.insert(userPosition);
+        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("撤单成功");
     }
 
 
@@ -551,92 +957,105 @@
     }
 
 
+    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));
 
         SiteSetting siteSetting = this.iSiteSettingService.getSiteSetting();
         if (siteSetting == null) {
             log.error("平倉出錯,網站設置表不存在");
-            return ServerResponse.createByErrorMsg("下單失敗,系統設置錯誤");
+            return ServerResponse.createByErrorMsg("下单失败,系统设置错误");
         }
+
         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) {
-                    return ServerResponse.createByErrorMsg("平倉失敗,不在交易時段內");
-                }
-            }
-            if (siteProduct.getHolidayDisplay()) {
-                return ServerResponse.createByErrorMsg("周末或節假日不能交易!");
-            }
-
-        }
-
-
         if (userPosition == null) {
-            return ServerResponse.createByErrorMsg("平倉失敗,訂單不存在");
+            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) {
-            return ServerResponse.createByErrorMsg("平倉失敗,用戶不存在");
+            return ServerResponse.createByErrorMsg("平仓失败,用户不存在");
         }
 
         /*實名認證開關開啟*/
         if (siteProduct.getRealNameDisplay() && user.getIsLock().intValue() == 1) {
 
-            return ServerResponse.createByErrorMsg("平倉失敗,用戶已被鎖定");
+            return ServerResponse.createByErrorMsg("平仓失败,用户已被锁定");
 
         }
 
 
         if (userPosition.getSellOrderId() != null) {
-            return ServerResponse.createByErrorMsg("平倉失敗,此訂單已平倉");
+            return ServerResponse.createByErrorMsg("平仓失败,此订单已平仓");
+        }
+
+        ServerResponse t1Check = validateT1BeforeSell(userPosition, doType);
+        if (t1Check != null) {
+            return t1Check;
         }
 
         if (1 == userPosition.getIsLock().intValue()) {
-            return ServerResponse.createByErrorMsg("平倉失敗 " + userPosition.getLockMsg());
+            if (DateTimeUtil.isCanSellOneday(userPosition.getBuyOrderTime(), userPosition.getLockDays())) {
+                return ServerResponse.createByErrorMsg("平仓失败 " + userPosition.getLockMsg());
+            }
         }
 
-        if (!DateTimeUtil.isCanSell(userPosition.getBuyOrderTime(), siteSetting.getCantSellTimes().intValue())) {
-            // return ServerResponse.createByErrorMsg(siteSetting.getCantSellTimes() + "分鐘內不能平倉");
-            return ServerResponse.createByErrorMsg("當日成交不可平倉");
-        }
+
 
 //        if (DateTimeUtil.sameDate(DateTimeUtil.getCurrentDate(),userPosition.getBuyOrderTime())) {
-//            return ServerResponse.createByErrorMsg("當天入倉的股票需要隔天才能出倉");
+//            return ServerResponse.createByErrorMsg("当天入仓的股票需要隔天才能出仓");
 //        }
         BigDecimal now_price;
         StockListVO stockListVO = new StockListVO();
@@ -680,7 +1099,7 @@
 //        }
 
         if (stockListVO.getNowPrice() == null) {
-            return ServerResponse.createByErrorMsg("平倉失敗,獲取股票信息失敗");
+            return ServerResponse.createByErrorMsg("平仓失败,获取股票信息失败");
         }
 
         now_price = new BigDecimal(stockListVO.getNowPrice());
@@ -688,7 +1107,7 @@
 //        BigDecimal now_price = new BigDecimal(stockListVO.getNowPrice());
         if (now_price.compareTo(new BigDecimal("0")) != 1) {
             log.error("股票 = {} 收到報價 = {}", userPosition.getStockName(), now_price);
-            return ServerResponse.createByErrorMsg("報價0,平倉失敗,請稍後再試");
+            return ServerResponse.createByErrorMsg("报价0,平仓失败,请稍后再试");
         }
 
         double stock_crease = stockListVO.getHcrate().doubleValue();
@@ -703,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();
 
@@ -744,11 +1163,11 @@
         log.info("點差費 = {}", spreadRatePrice);
 
         BigDecimal sell_fee_amt = all_sell_amt.multiply(siteSetting.getSellFee()).setScale(2, 4);
-        log.info("賣出手續費 = {}", sell_fee_amt);
+        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 all_fee_amt = sell_fee_amt.add(orderSpread).add(orderStayFee).add(spreadRatePrice);
+        log.info("总的手续费费用 = {}", all_fee_amt);
 
         userPosition.setSellOrderId(GeneratePosition.getPositionId());
         userPosition.setSellOrderPrice(now_price);
@@ -761,13 +1180,14 @@
 
         BigDecimal all_profit = profitLoss.subtract(all_fee_amt);
         userPosition.setAllProfitAndLose(all_profit);
+        userPosition.setStatus(2);
 
         int updatePositionCount = this.userPositionMapper.updateByPrimaryKeySelective(userPosition);
         if (updatePositionCount > 0) {
-            log.info("【用戶平倉】修改浮動盈虧記錄成功");
+            log.info("【用户平仓】修改浮动盈亏记录成功");
         } else {
-            log.error("用戶平倉】修改浮動盈虧記錄出錯");
-            throw new Exception("用戶平倉】修改浮動盈虧記錄出錯");
+            log.error("用户平仓】修改浮动盈亏记录出错");
+            throw new Exception("用户平仓】修改浮动盈亏记录出错");
         }
 
         BigDecimal freez_amt = all_buy_amt.divide(new BigDecimal(userPosition.getOrderLever().intValue()), 2, 4);
@@ -779,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("【用戶平倉】修改用戶金額成功");
@@ -794,9 +1214,9 @@
         ucd.setAgentName(user.getAgentName());
         ucd.setUserId(user.getId());
         ucd.setUserName(user.getRealName());
-        ucd.setDeType("總盈虧");
+        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));
@@ -813,7 +1233,243 @@
             throw new Exception("用戶平倉】保存明細記錄出錯");
         }
 
-        return ServerResponse.createBySuccessMsg("平倉成功!");
+        return ServerResponse.createBySuccessMsg("平仓成功!");
+    }
+
+    @Override
+    public ServerResponse calendar(String yearMonth,HttpServletRequest request) throws Exception {
+
+        // 获取指定月份的所有日期
+        List<String> allDays = MarketUtils.getAllDaysInMonth(yearMonth);
+
+        User user = this.iUserService.getCurrentRefreshUser(request);
+
+        List<Map<String, String>> dailyProfitMap = userPositionMapper.getDailyProfitByMonth(yearMonth, user.getId());
+        List<DailyRecord> dailyProfits = new ArrayList<>();
+        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
+        for (String day : allDays) {
+            if(null == dailyProfitMap){
+                LocalDate date = LocalDate.parse(day, formatter);
+                dailyProfits.add(new DailyRecord(day, BigDecimal.ZERO, MarketUtils.isMarketClosed(date)));
+            }else {
+                BigDecimal profit = BigDecimal.ZERO;
+                for (Map<String, String> record : dailyProfitMap) {
+                    if(record.get("recordDate").equals(day)){
+                        profit = new BigDecimal(record.get("profit"));
+                    }
+                }
+                LocalDate date = LocalDate.parse(day, formatter);
+                dailyProfits.add(new DailyRecord(day, profit, MarketUtils.isMarketClosed(date)));
+            }
+        }
+        return ServerResponse.createBySuccess(dailyProfits);
+    }
+
+
+
+    public ServerResponse sellbf(String positionSn, Integer quantity) throws Exception {
+        log.info("【用戶交易部分平倉】 positionSn = {} , dotype = {}", positionSn, Integer.valueOf(quantity));
+
+        SiteSetting siteSetting = this.iSiteSettingService.getSiteSetting();
+        if (siteSetting == null) {
+            log.error("平仓出错,网站设置表不存在");
+            return ServerResponse.createByErrorMsg("下单失败,系统设置错误");
+        }
+        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);
+        }
+        ServerResponse timeCheck = validateTransTimeBeforeSell(userPosition, siteSetting);
+        if (timeCheck != null) {
+            return timeCheck;
+        }
+        User user = this.userMapper.selectByPrimaryKey(userPosition.getUserId());
+        if (user == null) {
+            return ServerResponse.createByErrorMsg("平仓失败,用户不存在");
+        }
+        /*實名認證開關開啟*/
+        if (siteProduct.getRealNameDisplay() && user.getIsLock().intValue() == 1) {
+
+            return ServerResponse.createByErrorMsg("平仓失败,用户已被锁定");
+
+        }
+        if (userPosition.getSellOrderId() != null) {
+            return ServerResponse.createByErrorMsg("平仓失败,此订单已平仓");
+        }
+        if (1 == userPosition.getIsLock().intValue()) {
+            return ServerResponse.createByErrorMsg("平仓失败 " + userPosition.getLockMsg());
+        }
+        ServerResponse t1Check = validateT1BeforeSell(userPosition, 1);
+        if (t1Check != null) {
+            return t1Check;
+        }
+
+        BigDecimal now_price;
+        StockListVO stockListVO = new StockListVO();
+        //股票賣出的 價格 數據源
+        stockListVO = SinaStockApi.assembleLideStockListVO(LiDeDataUtils.getStock(userPosition.getStockCode()));
+        if (ObjectUtils.isEmpty(stockListVO)) {
+            stockListVO = SinaStockApi.assembleStockListVO(SinaStockApi.getSinaStock(userPosition.getStockGid()));
+        }
+        if (stockListVO.getNowPrice() == null) {
+            return ServerResponse.createByErrorMsg("平仓失败,获取股票信息失败");
+        }
+        now_price = new BigDecimal(stockListVO.getNowPrice());
+
+        if (now_price.compareTo(new BigDecimal("0")) != 1) {
+            log.error("股票 = {} 收到報價 = {}", userPosition.getStockName(), now_price);
+            return ServerResponse.createByErrorMsg("报价0,平仓失败,请稍后再试");
+        }
+
+        double stock_crease = stockListVO.getHcrate().doubleValue();
+
+        BigDecimal zsPrice = new BigDecimal(stockListVO.getPreclose_px());
+
+        BigDecimal ztPrice = zsPrice.multiply(new BigDecimal("0.1")).add(zsPrice);
+        ztPrice = ztPrice.setScale(2, 4);
+        BigDecimal chaPrice = ztPrice.subtract(zsPrice);
+
+        BigDecimal ztRate = chaPrice.multiply(new BigDecimal("100")).divide(zsPrice, 2, 4);
+
+        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 (quantity <= 0) {
+            return ServerResponse.createByErrorMsg("平仓失败, 平仓数量不可小于0");
+        }
+        if (quantity > userPosition.getOrderNum()) {
+            return ServerResponse.createByErrorMsg("平仓失败,平仓数量不可小于"+userPosition.getOrderNum());
+        }
+
+//        Integer buy_num = userPosition.getOrderNum();
+        Integer buy_num = quantity;
+
+//        BigDecimal all_buy_amt = userPosition.getOrderTotalPrice();
+        BigDecimal all_buy_amt = userPosition.getBuyOrderPrice().multiply(new BigDecimal(buy_num)).setScale(2,4);
+        BigDecimal all_sell_amt = now_price.multiply(new BigDecimal(buy_num.intValue()));
+
+        BigDecimal profitLoss = new BigDecimal("0");
+        if ("买涨".equals(userPosition.getOrderDirection())) {
+            log.info("買賣方向:{}", "漲");
+            profitLoss = all_sell_amt.subtract(all_buy_amt).setScale(2,4);
+        } else {
+            log.info("買賣方向:{}", "跌");
+            profitLoss = all_buy_amt.subtract(all_sell_amt).setScale(2,4);
+        }
+        log.info("買入總金額 = {} , 賣出總金額 = {} , 盈虧 = {}", new Object[]{all_buy_amt, all_sell_amt, profitLoss});
+
+        BigDecimal user_all_amt = user.getUserAmt();
+        BigDecimal user_enable_amt = user.getEnableAmt();
+        log.info("用戶原本總資金 = {} , 可用 = {}", user_all_amt, user_enable_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);
+        log.info("印花稅 = {}", orderSpread);
+
+//        BigDecimal orderStayFee = all_buy_amt.multiply(siteSetting.getStayFee()).setScale(2, 4);
+//        log.info("遞延費 = {}", orderStayFee);
+//
+//        BigDecimal spreadRatePrice = userPosition.getSpreadRatePrice();
+//        log.info("點差費 = {}", spreadRatePrice);
+
+        BigDecimal sell_fee_amt = all_sell_amt.multiply(siteSetting.getSellFee()).setScale(2, 4);
+        log.info("賣出手續費 = {}", sell_fee_amt);
+
+        // 买入手续费已在下单时扣除
+        BigDecimal all_fee_amt = sell_fee_amt.add(orderSpread);
+        log.info("總的手續費費用 = {}", all_fee_amt);
+        //复制一条新订单
+        UserPosition userPositionNew = new UserPosition();
+        BeanUtils.copyProperties(userPosition, userPositionNew);
+        userPositionNew.setId(null);
+        userPositionNew.setBuyOrderId(GeneratePosition.getPositionId());
+        userPositionNew.setPositionSn(KeyUtils.getUniqueKey());
+        userPositionNew.setOrderNum(buy_num);
+        userPositionNew.setOrderTotalPrice(all_buy_amt);
+
+        userPositionNew.setSellOrderId(GeneratePosition.getPositionId());
+        userPositionNew.setSellOrderPrice(now_price);
+        userPositionNew.setSellOrderTime(new Date());
+
+        BigDecimal order_fee_all = buy_fee_amt.add(sell_fee_amt);
+        userPositionNew.setOrderFee(order_fee_all);
+        userPositionNew.setOrderSpread(orderSpread);
+
+        userPositionNew.setProfitAndLose(profitLoss);
+
+        BigDecimal all_profit = profitLoss.subtract(all_fee_amt);
+        userPositionNew.setAllProfitAndLose(all_profit);
+        userPositionNew.setStatus(2);
+        userPositionMapper.insert(userPositionNew);
+        //修改原订单
+        userPosition.setOrderNum(userPosition.getOrderNum() - userPositionNew.getOrderNum());
+        userPosition.setOrderTotalPrice(userPosition.getOrderTotalPrice().subtract(userPositionNew.getOrderTotalPrice()));
+        userPosition.setOrderFee(userPosition.getOrderFee().subtract(buy_fee_amt));
+        userPosition.setOrderSpread(userPosition.getOrderSpread().subtract(orderSpread));
+
+        int updatePositionCount = this.userPositionMapper.updateByPrimaryKeySelective(userPosition);
+        if (updatePositionCount > 0) {
+            log.info("【用戶平倉】修改浮動盈虧記錄成功");
+        } else {
+            log.error("用戶平倉】修改浮動盈虧記錄出錯");
+            throw new Exception("用户平仓】修改浮动盈亏记录出错");
+        }
+
+        BigDecimal freez_amt = all_buy_amt.divide(new BigDecimal(userPositionNew.getOrderLever().intValue()), 2, 4);
+
+        BigDecimal reckon_all = user_all_amt.add(all_profit);
+        //修改用戶可用余額=當前可用余額+總盈虧+買入總金額+追加保證金
+        BigDecimal reckon_enable = user_enable_amt.add(all_profit).add(freez_amt).add(userPositionNew.getMarginAdd());
+
+        log.info("用戶平倉後的總資金  = {} , 可用資金 = {}", reckon_all, reckon_enable);
+        user.setUserAmt(reckon_all);
+        applyEnableAndWithdrawChange(user, user_enable_amt, reckon_enable);
+        int updateUserCount = this.userMapper.updateByPrimaryKeySelective(user);
+        if (updateUserCount > 0) {
+            log.info("【用戶平倉】修改用戶金額成功");
+        } else {
+            log.error("用戶平倉】修改用戶金額出錯");
+            throw new Exception("【用户平仓】修改用户金额出错");
+        }
+
+        UserCashDetail ucd = new UserCashDetail();
+        ucd.setPositionId(userPosition.getId());
+        ucd.setAgentId(user.getAgentId());
+        ucd.setAgentName(user.getAgentName());
+        ucd.setUserId(user.getId());
+        ucd.setUserName(user.getRealName());
+        ucd.setDeType("总盈亏");
+        ucd.setDeAmt(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));
+
+        int insertSxfCount = this.userCashDetailMapper.insert(ucd);
+        if (insertSxfCount > 0) {
+            //核算代理收入-平倉手續費
+            iAgentAgencyFeeService.AgencyFeeIncome(2, userPositionNew.getPositionSn());
+            //核算代理收入-分紅
+            iAgentAgencyFeeService.AgencyFeeIncome(4, userPositionNew.getPositionSn());
+            log.info("【用戶平倉】保存明細記錄成功");
+        } else {
+            log.error("用戶平倉】保存明細記錄出錯");
+            throw new Exception("【用户平仓】保存明细记录出错");
+        }
+
+        return ServerResponse.createBySuccessMsg("平仓成功!");
     }
 
 
@@ -874,7 +1530,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) {
@@ -888,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("【用户平仓】修改用户金额成功");
@@ -922,7 +1579,7 @@
     }
 
 
-    public ServerResponse lock(Integer positionId, Integer state, String lockMsg) {
+    public ServerResponse lock(Integer positionId, Integer state, String lockMsg,Integer lockDays) {
         if (positionId == null || state == null) {
             return ServerResponse.createByErrorMsg("参数不能为空");
         }
@@ -945,6 +1602,7 @@
         if (state.intValue() == 1) {
             position.setIsLock(Integer.valueOf(1));
             position.setLockMsg(lockMsg);
+            position.setLockDays(lockDays);
         } else {
             position.setIsLock(Integer.valueOf(0));
         }
@@ -1013,9 +1671,13 @@
             if (ObjectUtils.isEmpty(stockListVO)) {
                 stockListVO = SinaStockApi.assembleStockListVO(SinaStockApi.getSinaStock(position.getStockGid()));
             }
-            if (stockListVO.getNowPrice() == null) {
-                stockListVO.setNowPrice("0");
+
+            if (stockListVO.getNowPrice() == null || new BigDecimal(stockListVO.getNowPrice()).compareTo(BigDecimal.ZERO) <= 0) {
+//                stockListVO.setNowPrice("0");
+                stockListVO.setNowPrice(String.valueOf(position.getBuyOrderIdIndex()));
             }
+
+
             BigDecimal nowPrice = new BigDecimal(stockListVO.getNowPrice());
 
 
@@ -1164,6 +1826,7 @@
         return ServerResponse.createBySuccess(agentIncomeVO);
     }
 
+
     public ServerResponse listByAdmin(Integer agentId, Integer positionType, Integer state, Integer userId, String positionSn, String beginTime, String endTime, int pageNum, int pageSize) {
         PageHelper.startPage(pageNum, pageSize);
 
@@ -1190,6 +1853,24 @@
         pageInfo.setList(adminPositionVOS);
 
         return ServerResponse.createBySuccess(pageInfo);
+    }
+
+    @Override
+    public ServerResponse transferPositions(String positionId) {
+        if(StringUtils.isBlank(positionId)){
+            return ServerResponse.createByErrorMsg("参数错误");
+        }
+        List<String> positionIds = Arrays.asList(positionId.split(","));
+        UpdateWrapper<UserPosition> updateWrapper = new UpdateWrapper<>();
+        updateWrapper.in("id", positionIds);
+        updateWrapper.eq("status", 0);
+        updateWrapper.set("status", 1);
+        int updateCount = userPositionMapper.update(null, updateWrapper);
+        if (updateCount > 0) {
+            return ServerResponse.createBySuccessMsg("转持仓成功");
+        } else {
+            return ServerResponse.createByErrorMsg("转持仓失败");
+        }
     }
 
     public int CountPositionNum(Integer state, Integer accountType) {
@@ -1248,13 +1929,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);
+        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) {
@@ -1311,8 +1990,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).setScale(2, 4);
+        log.info("创建模拟持仓 手续费(保证金 * 百分比) = {}", buy_fee_amt);
         userPosition.setOrderFee(buy_fee_amt);
 
 
@@ -1355,21 +2034,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("生成持仓成功");
@@ -1543,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;
@@ -1585,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;
@@ -1624,6 +2319,7 @@
         userPositionVO.setOrderStayFee(position.getOrderStayFee());
         userPositionVO.setOrderStayDays(position.getOrderStayDays());
         userPositionVO.setMarginAdd(position.getMarginAdd());
+        userPositionVO.setStatus(position.getStatus());
 
         userPositionVO.setStockPlate(position.getStockPlate());
         userPositionVO.setSpreadRatePrice(position.getSpreadRatePrice());
@@ -1633,7 +2329,15 @@
         userPositionVO.setAllProfitAndLose(positionProfitVO.getAllProfitAndLose());
         userPositionVO.setNow_price(positionProfitVO.getNowPrice());
         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;
     }
@@ -1643,18 +2347,23 @@
         BigDecimal allProfitAndLose = new BigDecimal("0");
         String allProfitAndLoseStr = "";
         String nowPrice = "";
+        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();
@@ -1664,9 +2373,15 @@
             }
             //    stockListVO = SinaStockApi.assembleStockListVO(SinaStockApi.getSinaStock(position.getStockGid()));
             nowPrice = stockListVO.getNowPrice();
-            if (nowPrice == null) {
-                nowPrice = String.valueOf(0);
+            if (nowPrice == null || new BigDecimal(stockListVO.getNowPrice()).compareTo(BigDecimal.ZERO) <= 0) {
+                if(null != position.getBuyOrderIdIndex() && position.getBuyOrderIdIndex().compareTo(BigDecimal.ZERO) > 0){
+                    nowPrice = String.valueOf(position.getBuyOrderIdIndex());
+                }else {
+                    nowPrice = String.valueOf(position.getBuyOrderPrice());
+                }
             }
+
+            orderTotalPrice = new BigDecimal(nowPrice).multiply(new BigDecimal(position.getOrderNum()));
 
             BigDecimal subPrice = (new BigDecimal(nowPrice)).subtract(position.getBuyOrderPrice());
 //            profitAndLose = subPrice.multiply(new BigDecimal(position.getOrderNum().intValue())).multiply(new BigDecimal(position.getOrderLever())).setScale(2,4);
@@ -1674,7 +2389,6 @@
             if ("买跌".equals(position.getOrderDirection())) {
                 profitAndLose = profitAndLose.negate();
             }
-
             //总盈亏= 浮动盈亏 – 手续费 – 印花税 – 留仓费 – 点差费
             allProfitAndLose = profitAndLose.subtract(position.getOrderFee()).subtract(position.getOrderSpread()).subtract(position.getOrderStayFee()).subtract(position.getSpreadRatePrice());
             //改成盈亏百分比
@@ -1683,6 +2397,7 @@
         }
         PositionProfitVO positionProfitVO = new PositionProfitVO();
         positionProfitVO.setProfitAndLose(profitAndLose);
+        positionProfitVO.setOrderTotalPrice(orderTotalPrice);
         positionProfitVO.setAllProfitAndLose(allProfitAndLose);
         positionProfitVO.setAllProfitAndLoseStr(allProfitAndLoseStr);
         positionProfitVO.setNowPrice(nowPrice);
@@ -1726,6 +2441,13 @@
         pageInfo.setList(userPositionVOS);
 
         return ServerResponse.createBySuccess(pageInfo);
+    }
+
+    @Override
+    public ServerResponse findPositionBySn(String positionSn) {
+        UserPosition userPosition = userPositionMapper.findPositionBySn(positionSn);
+        UserPositionVO userPositionVO = assembleUserPositionVO(userPosition);
+        return ServerResponse.createBySuccess(userPositionVO);
     }
 
     /**
@@ -1784,7 +2506,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);
@@ -1855,6 +2577,12 @@
 
     }
 
+    @Override
+    public ServerResponse stockDzDisplay(HttpServletRequest request) {
+        SiteProduct siteProduct = iSiteProductService.getProductSetting();
+        return ServerResponse.createBySuccess(siteProduct.getStockDzDisplay());
+    }
+
     /**
      * 大宗下單
      *
@@ -1871,20 +2599,20 @@
         SiteProduct siteProduct = iSiteProductService.getProductSetting();
         User user = this.iUserService.getCurrentRefreshUser(request);
         if (siteProduct.getRealNameDisplay() && (StringUtils.isBlank(user.getRealName()) || StringUtils.isBlank(user.getIdCard()))) {
-            return ServerResponse.createByErrorMsg("下單失敗,請先實名認證");
+            return ServerResponse.createByErrorMsg("下单失败,请先实名认证");
         }
         BigDecimal user_enable_amt = user.getEnableAmt();
         BigDecimal user_enable_withdraw_amt = user.getEnaleWithdrawAmt();
         log.info("用戶 {} 下單,股票code = {} ,數量 = {}", new Object[]{user
                 .getId(), stockCode, num});
         if (siteProduct.getRealNameDisplay() && user.getIsLock().intValue() == 1) {
-            return ServerResponse.createByErrorMsg("下單失敗,賬戶已被鎖定");
+            return ServerResponse.createByErrorMsg("下单失败,账户已被锁定");
         }
 
         SiteSetting siteSetting = this.iSiteSettingService.getSiteSetting();
         if (siteSetting == null) {
             log.error("下單出錯,網站設置表不存在");
-            return ServerResponse.createByErrorMsg("下單失敗,系統設置錯誤");
+            return ServerResponse.createByErrorMsg("下单失败,系统设置错误");
         }
         StockDz stockDz = null;
         stockDz = this.stockDzMapper.selectOne(new QueryWrapper<StockDz>().eq("stock_code", stockCode));
@@ -1895,38 +2623,42 @@
         boolean am_flag = BuyAndSellUtils.isTransTime(am_begin, am_end);
         boolean pm_flag = BuyAndSellUtils.isTransTime(pm_begin, pm_end);
         log.info("是否在大宗交易時間 = {}", Boolean.valueOf(pm_flag));
+        //TODO
         //15-15:30
 //        if (!pm_flag) {
 //            return ServerResponse.createByErrorMsg("下單失敗,不在交易時段內");
 //        }
         if (siteProduct.getHolidayDisplay()) {
-            return ServerResponse.createByErrorMsg("周末或節假日不能交易!");
+            return ServerResponse.createByErrorMsg("周末或节假日不能交易!");
         }
-        if (!Objects.equals(stockDz.getPassword(), password)) {
-            return ServerResponse.createByErrorMsg("下單失敗,密鑰錯誤");
-        }
+//        if (!Objects.equals(stockDz.getPassword(), password)) {
+//            return ServerResponse.createByErrorMsg("下单失败,密钥错误");
+//        }
 
         if (stockDz.getIsLock().intValue() != 0) {
-            return ServerResponse.createByErrorMsg("下單失敗,當前股票不能交易");
+            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() + "股");
+            return ServerResponse.createByErrorMsg("下单失败,购买数量最小为" + stockDz.getStockNum() + "股");
+        }
+        if (num > stockDz.getStockShare().intValue()) {
+            return ServerResponse.createByErrorMsg("下单失败,购买数量大于" + stockDz.getStockNum() + "股");
         }
         if (num > siteSetting.getBuyMaxNum()) {
-            return ServerResponse.createByErrorMsg("下單失敗,購買數量大於" + siteSetting.getBuyMaxNum() + "股");
+            return ServerResponse.createByErrorMsg("下单失败,购买数量大于" + siteSetting.getBuyMaxNum() + "股");
         }
         BigDecimal now_price;
         StockListVO stockListVO = new StockListVO();
@@ -1938,7 +2670,7 @@
         now_price = new BigDecimal(stockListVO.getNowPrice()).multiply(stockDz.getDiscount());
 
         if (now_price.compareTo(new BigDecimal("0")) == 0) {
-            return ServerResponse.createByErrorMsg("報價0,請稍後再試");
+            return ServerResponse.createByErrorMsg("报价0,请稍后再试");
         }
 
 
@@ -1956,7 +2688,7 @@
         }
 
         if (stockListVO.getName().startsWith("ST") || stockListVO.getName().endsWith("退")) {
-            return ServerResponse.createByErrorMsg("ST和已退市的股票不能入倉");
+            return ServerResponse.createByErrorMsg("ST和已退市的股票不能入仓");
         }
 
         BigDecimal zsPrice = new BigDecimal(stockListVO.getPreclose_px());
@@ -1970,45 +2702,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());
@@ -2019,10 +2751,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);
@@ -2035,7 +2767,7 @@
 
         int compareInt = buy_amt_autual.compareTo(new BigDecimal(siteSetting.getBuyMinAmt().intValue()));
         if (compareInt == -1) {
-            return ServerResponse.createByErrorMsg("下單失敗,購買金額小於" + siteSetting
+            return ServerResponse.createByErrorMsg("下单失败,购买金额小于" + siteSetting
                     .getBuyMinAmt() + "元");
         }
 
@@ -2043,16 +2775,15 @@
         BigDecimal max_buy_amt = user_enable_amt.multiply(siteSetting.getBuyMaxAmtPercent());
         int compareCwInt = buy_amt_autual.compareTo(max_buy_amt);
         if (compareCwInt == 1) {
-            return ServerResponse.createByErrorMsg("下單失敗,不能超過可用資金的" + siteSetting
+            return ServerResponse.createByErrorMsg("下单失败,不能超过可用资金的" + siteSetting
                     .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_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 + "元(含保证金及手续费)");
         }
 
 //        if (user.getUserIndexAmt().compareTo(new BigDecimal("0")) == -1) {
@@ -2060,6 +2791,7 @@
 //        }
 //
         UserPosition userPosition = new UserPosition();
+        userPosition.setStatus(0);
         userPosition.setPositionType(3);
         userPosition.setPositionSn(KeyUtils.getUniqueKey());
         userPosition.setUserId(user.getId());
@@ -2072,7 +2804,7 @@
         userPosition.setBuyOrderId(GeneratePosition.getPositionId());
         userPosition.setBuyOrderTime(new Date());
         userPosition.setBuyOrderPrice(now_price);
-        userPosition.setOrderDirection("買漲");
+        userPosition.setOrderDirection("买涨");
         userPosition.setOrderNum(num);
         if (stockDz.getStockPlate() != null) {
             userPosition.setStockPlate(stockDz.getStockPlate());
@@ -2088,7 +2820,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);
 
@@ -2121,32 +2853,14 @@
 
         log.info("--------------购买逻辑股票数据 buyDz  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 == -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) {
+            //修改大宗剩余
+            stockDz.setStockShare(stockDz.getStockShare() - num);
+            stockDz.setStockSurplus(stockDz.getStockSurplus() + num);
+            stockDzMapper.updateById(stockDz);
+            deductUserEnableOnBuy(user, buy_debit_dz, buy_amt_autual, buy_fee_amt_dz, userPosition);
+            syncUserCacheAfterTrade(request);
             iAgentAgencyFeeService.AgencyFeeIncome(1, userPosition.getPositionSn());
             log.info("【用戶交易下單】保存持倉記錄成功");
         } else {
@@ -2154,13 +2868,13 @@
             throw new Exception("用戶交易下單】保存持倉記錄出錯");
         }
 
-        return ServerResponse.createBySuccess("大宗交易下單成功");
+        return ServerResponse.createBySuccess("大宗交易下单成功");
     }
 
     /**
      * vip抢筹
      *
-     * @param stockId
+     * @param stockCode
      * @param buyNum
      * @param buyType
      * @param lever
@@ -2374,11 +3088,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);
+        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) {
@@ -2432,7 +3145,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);
 
@@ -2466,31 +3178,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("【用戶交易下單】保存持倉記錄成功");
@@ -2501,6 +3192,34 @@
 
         return ServerResponse.createBySuccess("下單成功");
     }
+
+    @Override
+    public void synchronizePrice(){
+        Map<String,BigDecimal> priceMap = new HashMap<>();
+        List<UserPosition> userPositions = this.userPositionMapper.synchronizePrice();
+        log.info("-------------------同步收盘价,查询数据总量 {}个--------------",userPositions.size());
+        int count = 0;
+        for (UserPosition position : userPositions) {
+            if(priceMap.containsKey(position.getStockGid())){
+                BigDecimal newPrice = priceMap.get(position.getStockGid());
+                position.setBuyOrderIdIndex(newPrice);
+                userPositionMapper.updateByPrimaryKey(position);
+            }else {
+                StockListVO stockListVO = new StockListVO();
+                stockListVO = SinaStockApi.assembleLideStockListVO(LiDeDataUtils.getStock(position.getStockCode()));
+                if (ObjectUtils.isEmpty(stockListVO)) {
+                    stockListVO = SinaStockApi.assembleStockListVO(SinaStockApi.getSinaStock(position.getStockGid()));
+                }
+                if(stockListVO.getNowPrice() != null && new BigDecimal(stockListVO.getNowPrice()).compareTo(BigDecimal.ZERO) > 0){
+                    position.setBuyOrderIdIndex(new BigDecimal(stockListVO.getNowPrice()));
+                    priceMap.put(position.getStockGid(),new BigDecimal(stockListVO.getNowPrice()));
+                    userPositionMapper.updateByPrimaryKey(position);
+                }
+            }
+            count++;
+        }
+        log.info("-------------------同步收盘价 {}个--------------",count);
+    }
 }
 
 

--
Gitblit v1.9.3