From a66b2a41f265cc9526781e39c1d6c6d5f5d7c013 Mon Sep 17 00:00:00 2001
From: peter <14100000001@qq.com>
Date: Thu, 01 Jan 2026 02:29:08 +0800
Subject: [PATCH] 新增港股

---
 src/main/java/com/nq/service/impl/UserPositionServiceImpl.java |  532 ++++++++++++++++++++++++++++++++++++++++++----------------
 1 files changed, 382 insertions(+), 150 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..901e9ea 100644
--- a/src/main/java/com/nq/service/impl/UserPositionServiceImpl.java
+++ b/src/main/java/com/nq/service/impl/UserPositionServiceImpl.java
@@ -2,6 +2,7 @@
 
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.google.gson.Gson;
+import com.nq.config.StockType;
 import com.nq.dao.*;
 import com.nq.pojo.*;
 import com.nq.service.*;
@@ -28,6 +29,7 @@
 import net.sf.json.JSONObject;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.beans.BeanUtils;
 import org.springframework.stereotype.Service;
 
 
@@ -127,6 +129,7 @@
             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});
@@ -168,7 +171,7 @@
             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 (!am_flag && !pm_flag) {
                 return ServerResponse.createByErrorMsg("下单失败,不在港股股交易时段内");
             }
@@ -183,7 +186,7 @@
             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 (!am_flag && !pm_flag) {
                 return ServerResponse.createByErrorMsg("下单失败,不在交易时段内");
             }
@@ -220,44 +223,17 @@
         }
         BigDecimal now_price;
         StockListVO stockListVO = new StockListVO();
-        StockCoin stockCoin = new StockCoin();
         //股票类型 现价 数据源的处理
         stockListVO = SinaStockApi.assembleLideStockListVO(LiDeDataUtils.getStock(stock.getStockCode()));
         if (ObjectUtils.isEmpty(stockListVO)) {
-            stockListVO = SinaStockApi.assembleStockListVO(SinaStockApi.getSinaStock(stock.getStockGid()));
+            if(StockType.HK.getCode().equals(stock.getStockType())){
+                stockListVO = SinaStockApi.assembleStockListVOHk(SinaStockApi.getSinaStock(StockType.HK.getCode()+stock.getStockGid()));
+            }else {
+                stockListVO = SinaStockApi.assembleStockListVO(SinaStockApi.getSinaStock(stock.getStockGid()));
+            }
         }
-        now_price = new BigDecimal(stockListVO.getNowPrice());
-//        if (stock.getDataBase()!=0){
-//             String date = getCurrentTimeMiaoZero();
-//            String result = HttpClientRequest.doGet(PropertiesUtil.getProperty("changePrice.url")+"?cat_time="+date+"&stock_gid="+stock.getStockGid()+"&price="+stockListVO.getNowPrice());
-//            JSONObject jsonObject = JSONObject.fromObject(result);
-//            String nowPrice = jsonObject.getJSONObject("data").getString("new_price");
-//            if (nowPrice!=null){
-//                if ("us".equals(stock.getStockType())){
-//                    ExchangeVO exchangeVO = this.iStockFuturesService.queryExchangeVO("USD").getData();
-//                    now_price =new BigDecimal(nowPrice).multiply(new BigDecimal(exchangeVO.getNowPrice()));
-//                } else if ("hk".equals(stock.getStockType())){
-//                    ExchangeVO exchangeVO = this.iStockFuturesService.queryExchangeVO("HKD").getData();
-//                    now_price =new BigDecimal(nowPrice).multiply(new BigDecimal(exchangeVO.getNowPrice()));
-//                } else {
-//                    now_price = new BigDecimal(nowPrice);
-//                }
-//
-//            }else {
-//                stockListVO.setNowPrice(stockListVO.getNowPrice());
-//                if ("us".equals(stock.getStockType())){
-//                    ExchangeVO exchangeVO = this.iStockFuturesService.queryExchangeVO("USD").getData();
-//                    now_price =new BigDecimal(nowPrice).multiply(new BigDecimal(exchangeVO.getNowPrice()));
-//                } else if ("hk".equals(stock.getStockType())){
-//                    ExchangeVO exchangeVO = this.iStockFuturesService.queryExchangeVO("HKD").getData();
-//                    now_price =new BigDecimal(nowPrice).multiply(new BigDecimal(exchangeVO.getNowPrice()));
-//                } else {
-//                    now_price = new BigDecimal(nowPrice);
-//                }
-//            }
-//        }
-//            String stockOther = RedisShardedPoolUtils.get(stock.getStockName(), 8);
 
+        now_price = new BigDecimal(stockListVO.getNowPrice());
 
         if (now_price.compareTo(new BigDecimal("0")) == 0) {
             return ServerResponse.createByErrorMsg("报价0,请稍后再试");
@@ -340,8 +316,14 @@
         }
 
 
-        ServerResponse serverResponse = this.iStockService.selectRateByDaysAndStockCode(stock
-                .getStockCode(), siteSetting.getStockDays().intValue());
+        ServerResponse serverResponse = null;
+        if(StockType.HK.getCode().equals(stock.getStockType())){
+            serverResponse = this.iStockService.selectRateByDaysAndStockCode(stock
+                    .getStockGid(), siteSetting.getStockDays().intValue());
+        }else {
+            serverResponse = this.iStockService.selectRateByDaysAndStockCode(stock
+                    .getStockCode(), siteSetting.getStockDays().intValue());
+        }
         if (!serverResponse.isSuccess()) {
             return serverResponse;
         }
@@ -370,7 +352,9 @@
                     .getBuyMinAmt() + "元");
         }
 
-
+        if(StockType.HK.getCode().equals(stock.getStockType())){
+            user_enable_amt = user.getHkAmt();
+        }
         BigDecimal max_buy_amt = user_enable_amt.multiply(siteSetting.getBuyMaxAmtPercent());
         int compareCwInt = buy_amt_autual.compareTo(max_buy_amt);
         if (compareCwInt == 1) {
@@ -415,7 +399,7 @@
         userPosition.setBuyOrderTime(new Date());
         userPosition.setBuyOrderPrice(now_price);
         userPosition.setOrderDirection((buyType.intValue() == 0) ? "买涨" : "买跌");
-
+        userPosition.setStockType(stock.getStockType());
         userPosition.setOrderNum(buyNum);
 
 
@@ -478,7 +462,7 @@
             //修改用户可用余额= 当前余额-下单金额-买入手续费-印花税-点差费
             //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);
+            BigDecimal reckon_enable = user_enable_amt.subtract(buy_amt_autual);;
             //修改用户可取余额=当前可取余额-下单总金额
             int compareUserWithdrawAmtInt = user_enable_withdraw_amt.compareTo(buy_amt_autual);
             if (compareUserWithdrawAmtInt == -1) {
@@ -488,7 +472,12 @@
                 user_enable_withdraw_amt = user_enable_withdraw_amt.subtract(buy_amt_autual);
                 user.setEnaleWithdrawAmt(user_enable_withdraw_amt);
             }
-            user.setEnableAmt(reckon_enable);
+            if(StockType.HK.getCode().equals(stock.getStockType())){
+                user.setHkAmt(reckon_enable);
+            }else {
+                user.setEnableAmt(reckon_enable);
+            }
+
             int updateUserCount = this.userMapper.updateByPrimaryKeySelective(user);
             if (updateUserCount > 0) {
                 log.info("【用户交易下单】修改用户金额成功");
@@ -557,7 +546,7 @@
         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);
@@ -572,7 +561,7 @@
                 log.info("是否在上午交易時間 = {} 是否在下午交易時間 = {}", Boolean.valueOf(am_flag), Boolean.valueOf(pm_flag));
 
                 if (!am_flag && !pm_flag) {
-                    return ServerResponse.createByErrorMsg("平倉失敗,不在交易時段內");
+                    return ServerResponse.createByErrorMsg("下单失败,系统设置错误");
                 }
             } else if (userPosition.getStockGid().contains("hk")) {
                 String am_begin = siteSetting.getTransAmBeginhk();
@@ -594,45 +583,46 @@
                 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 (!am_flag && !pm_flag) {
-                    return ServerResponse.createByErrorMsg("平倉失敗,不在交易時段內");
+                    return ServerResponse.createByErrorMsg("平仓失败,不在交易时段内");
                 }
             }
             if (siteProduct.getHolidayDisplay()) {
-                return ServerResponse.createByErrorMsg("周末或節假日不能交易!");
+                return ServerResponse.createByErrorMsg("周末或节假日不能交易!");
             }
 
         }
 
 
         if (userPosition == null) {
-            return ServerResponse.createByErrorMsg("平倉失敗,訂單不存在");
+            return ServerResponse.createByErrorMsg("平仓失败,订单不存在");
         }
 
         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("平仓失败,此订单已平仓");
         }
 
         if (1 == userPosition.getIsLock().intValue()) {
-            return ServerResponse.createByErrorMsg("平倉失敗 " + userPosition.getLockMsg());
+            return ServerResponse.createByErrorMsg("平仓失败 " + userPosition.getLockMsg());
         }
 
         if (!DateTimeUtil.isCanSell(userPosition.getBuyOrderTime(), siteSetting.getCantSellTimes().intValue())) {
             // return ServerResponse.createByErrorMsg(siteSetting.getCantSellTimes() + "分鐘內不能平倉");
-            return ServerResponse.createByErrorMsg("當日成交不可平倉");
+            return ServerResponse.createByErrorMsg("当日成交不可平仓");
         }
 
 //        if (DateTimeUtil.sameDate(DateTimeUtil.getCurrentDate(),userPosition.getBuyOrderTime())) {
@@ -645,42 +635,16 @@
         //股票賣出的 價格 數據源
         stockListVO = SinaStockApi.assembleLideStockListVO(LiDeDataUtils.getStock(userPosition.getStockCode()));
         if (ObjectUtils.isEmpty(stockListVO)) {
-            stockListVO = SinaStockApi.assembleStockListVO(SinaStockApi.getSinaStock(userPosition.getStockGid()));
+            if(StockType.HK.getCode().equals(userPosition.getStockType())){
+                stockListVO = SinaStockApi.assembleStockListVOHk(SinaStockApi.getSinaStock(StockType.HK.getCode()+userPosition.getStockGid()));
+            }else {
+                stockListVO = SinaStockApi.assembleStockListVO(SinaStockApi.getSinaStock(userPosition.getStockGid()));
+            }
+//            stockListVO = SinaStockApi.assembleStockListVO(SinaStockApi.getSinaStock(userPosition.getStockGid()));
         }
-        //stockListVO = SinaStockApi.assembleStockListVO(SinaStockApi.getSinaStock(userPosition.getStockGid()));
-//            if (stock.getDataBase() != 0) {
-//                  String date = getCurrentTimeMiaoZero();
-//                String result = HttpClientRequest.doGet(PropertiesUtil.getProperty("changePrice.url")+"?cat_time="+date+"&stock_gid="+stock.getStockGid()+"&price="+stockListVO.getNowPrice());
-//                JSONObject jsonObject = JSONObject.fromObject(result);
-//                String nowPrice1 = jsonObject.getJSONObject("data").getString("new_price");
-//                if (nowPrice1!=null){
-//                    if ("us".equals(stock.getStockType())){
-//                        ExchangeVO exchangeVO = this.iStockFuturesService.queryExchangeVO("USD").getData();
-//                        now_price = new BigDecimal(nowPrice1).multiply(new BigDecimal(exchangeVO.getNowPrice()));
-//                    } else if ("hk".equals(stock.getStockType())){
-//                        ExchangeVO exchangeVO = this.iStockFuturesService.queryExchangeVO("HKD").getData();
-//                        now_price = new BigDecimal(nowPrice1).multiply(new BigDecimal(exchangeVO.getNowPrice()));
-//                    } else {
-//                        now_price = new BigDecimal(nowPrice1);
-//                    }
-//
-//                }else {
-//                    stockListVO.setNowPrice(stockListVO.getNowPrice());
-//                    if ("us".equals(stock.getStockType())){
-//                        ExchangeVO exchangeVO = this.iStockFuturesService.queryExchangeVO("USD").getData();
-//                        now_price = new BigDecimal(stockListVO.getNowPrice()).multiply(new BigDecimal(exchangeVO.getNowPrice()));
-//                    } else if ("hk".equals(stock.getStockType())){
-//                        ExchangeVO exchangeVO = this.iStockFuturesService.queryExchangeVO("HKD").getData();
-//                        now_price = new BigDecimal(stockListVO.getNowPrice()).multiply(new BigDecimal(exchangeVO.getNowPrice()));
-//                    } else {
-//                        now_price = new BigDecimal(stockListVO.getNowPrice());
-//                    }
-//                }
-//
-//        }
 
         if (stockListVO.getNowPrice() == null) {
-            return ServerResponse.createByErrorMsg("平倉失敗,獲取股票信息失敗");
+            return ServerResponse.createByErrorMsg("平仓失败,获取股票信息失败");
         }
 
         now_price = new BigDecimal(stockListVO.getNowPrice());
@@ -688,7 +652,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();
@@ -705,7 +669,7 @@
         log.info("股票當前漲跌幅 = {} 跌停幅度 = {}", Double.valueOf(stock_crease), ztRate);
         if ((new BigDecimal(String.valueOf(stock_crease))).compareTo(ztRate) == 0 && "買漲"
                 .equals(userPosition.getOrderDirection())) {
-            return ServerResponse.createByErrorMsg("當前股票已跌停不能賣出");
+            return ServerResponse.createByErrorMsg("当前股票已跌停不能卖出");
         }
 
         Integer buy_num = userPosition.getOrderNum();
@@ -727,7 +691,12 @@
         }
         log.info("買入總金額 = {} , 賣出總金額 = {} , 盈虧 = {}", new Object[]{all_buy_amt, all_sell_amt, profitLoss});
 
-        BigDecimal user_all_amt = user.getUserAmt();
+        BigDecimal user_all_amt = BigDecimal.ZERO;
+        if(StockType.HK.getCode().equals(userPosition.getStockType())){
+            user_all_amt = user.getHkAmt();
+        }else {
+            user_all_amt = user.getUserAmt();
+        }
         BigDecimal user_enable_amt = user.getEnableAmt();
         log.info("用戶原本總資金 = {} , 可用 = {}", user_all_amt, user_enable_amt);
 
@@ -744,11 +713,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);
+        log.info("总的手续费费用 = {}", all_fee_amt);
 
         userPosition.setSellOrderId(GeneratePosition.getPositionId());
         userPosition.setSellOrderPrice(now_price);
@@ -764,22 +733,25 @@
 
         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);
         //BigDecimal freez_amt = all_buy_amt;
-
         BigDecimal reckon_all = user_all_amt.add(all_profit);
         //修改用戶可用余額=當前可用余額+總盈虧+買入總金額+追加保證金
         BigDecimal reckon_enable = user_enable_amt.add(all_profit).add(freez_amt).add(userPosition.getMarginAdd());
-
         log.info("用戶平倉後的總資金  = {} , 可用資金 = {}", reckon_all, reckon_enable);
-        user.setUserAmt(reckon_all);
-        user.setEnableAmt(reckon_enable);
+
+        if(StockType.HK.getCode().equals(userPosition.getStockType())){
+            user.setHkAmt(reckon_enable);
+        }else {
+            user.setUserAmt(reckon_all);
+            user.setEnableAmt(reckon_enable);
+        }
         int updateUserCount = this.userMapper.updateByPrimaryKeySelective(user);
         if (updateUserCount > 0) {
             log.info("【用戶平倉】修改用戶金額成功");
@@ -794,9 +766,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 + ",总手续费:" + all_fee_amt + ",递延费:" + orderStayFee + ",印花稅:" + orderSpread + ",盈亏:" + profitLoss + ",总盈亏:" + all_profit);
 
         ucd.setAddTime(new Date());
         ucd.setIsRead(Integer.valueOf(0));
@@ -813,7 +785,216 @@
             throw new Exception("用戶平倉】保存明細記錄出錯");
         }
 
-        return ServerResponse.createBySuccessMsg("平倉成功!");
+        return ServerResponse.createBySuccessMsg("平仓成功!");
+    }
+
+    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(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("平仓失败,订单不存在");
+        }
+        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());
+        }
+        if (!DateTimeUtil.isCanSell(userPosition.getBuyOrderTime(), siteSetting.getCantSellTimes().intValue())) {
+            return ServerResponse.createByErrorMsg("当日成交不可平仓");
+        }
+
+        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 buy_fee_amt = all_buy_amt.multiply(siteSetting.getBuyFee()).setScale(2,4);
+        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 = buy_fee_amt.add(sell_fee_amt).add(orderSpread).add(orderStayFee).add(spreadRatePrice);
+        BigDecimal all_fee_amt = buy_fee_amt.add(sell_fee_amt).add(orderSpread);
+        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);
+        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);
+        user.setEnableAmt(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 + ",总手续费:" + all_fee_amt + ",递延费:" + 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("平仓成功!");
     }
 
 
@@ -1007,57 +1188,67 @@
 
         BigDecimal allProfitAndLose = new BigDecimal("0");
         BigDecimal allFreezAmt = new BigDecimal("0");
+        BigDecimal allProfitAndLoseHk = new BigDecimal("0");
+        BigDecimal allFreezAmtHk = new BigDecimal("0");
         for (UserPosition position : userPositions) {
-
             StockListVO stockListVO = SinaStockApi.assembleLideStockListVO(LiDeDataUtils.getStock(position.getStockCode()));
             if (ObjectUtils.isEmpty(stockListVO)) {
-                stockListVO = SinaStockApi.assembleStockListVO(SinaStockApi.getSinaStock(position.getStockGid()));
-            }
-            if (stockListVO.getNowPrice() == null) {
-                stockListVO.setNowPrice("0");
-            }
-            BigDecimal nowPrice = new BigDecimal(stockListVO.getNowPrice());
-
-
-            if (nowPrice.compareTo(new BigDecimal("0")) != 0) {
-
-                BigDecimal buyPrice = position.getBuyOrderPrice();
-                BigDecimal subPrice = nowPrice.subtract(buyPrice);
-
-                BigDecimal profit_and_lose = subPrice.multiply(new BigDecimal(position.getOrderNum().intValue()));
-                //BigDecimal profit_and_lose = subPrice.multiply(new BigDecimal(position.getOrderNum().intValue())).divide(new BigDecimal(position.getOrderLever())).setScale(2,4);
-                if ("买跌".equals(position.getOrderDirection())) {
-                    profit_and_lose = profit_and_lose.negate();
+                if(StockType.HK.getCode().equals(position.getStockType())){
+                    stockListVO = SinaStockApi.assembleStockListVOHk(SinaStockApi.getSinaStock(position.getStockType()+position.getStockGid()));
+                    if (stockListVO.getNowPrice() == null || new BigDecimal(stockListVO.getNowPrice()).compareTo(BigDecimal.ZERO) <= 0) {
+                        stockListVO.setNowPrice(String.valueOf(position.getBuyOrderIdIndex()));
+                    }
+                    BigDecimal nowPrice = new BigDecimal(stockListVO.getNowPrice());
+                    if (nowPrice.compareTo(new BigDecimal("0")) != 0) {
+                        BigDecimal buyPrice = position.getBuyOrderPrice();
+                        BigDecimal subPrice = nowPrice.subtract(buyPrice);
+                        BigDecimal profit_and_lose = subPrice.multiply(new BigDecimal(position.getOrderNum().intValue()));
+                        if ("买跌".equals(position.getOrderDirection())) {
+                            profit_and_lose = profit_and_lose.negate();
+                        }
+                        BigDecimal total_fee = position.getOrderFee().add(position.getOrderSpread()).add(position.getOrderStayFee());
+                        BigDecimal position_profit = profit_and_lose.subtract(total_fee);
+                        allProfitAndLoseHk = allProfitAndLoseHk.add(position_profit);
+                        BigDecimal position_freez = position.getOrderTotalPrice().divide(new BigDecimal(position.getOrderLever().intValue()), 2, 4);
+                        allFreezAmtHk = allFreezAmtHk.add(position_freez).add(position.getMarginAdd());
+                        continue;
+                    }
+                    log.info("查询所有持仓单的总盈亏,现价返回0,当前为集合竞价");
+                }else {
+                    stockListVO = SinaStockApi.assembleStockListVO(SinaStockApi.getSinaStock(position.getStockGid()));
+                    if (stockListVO.getNowPrice() == null || new BigDecimal(stockListVO.getNowPrice()).compareTo(BigDecimal.ZERO) <= 0) {
+                        stockListVO.setNowPrice(String.valueOf(position.getBuyOrderIdIndex()));
+                    }
+                    BigDecimal nowPrice = new BigDecimal(stockListVO.getNowPrice());
+                    if (nowPrice.compareTo(new BigDecimal("0")) != 0) {
+                        BigDecimal buyPrice = position.getBuyOrderPrice();
+                        BigDecimal subPrice = nowPrice.subtract(buyPrice);
+                        BigDecimal profit_and_lose = subPrice.multiply(new BigDecimal(position.getOrderNum().intValue()));
+                        if ("买跌".equals(position.getOrderDirection())) {
+                            profit_and_lose = profit_and_lose.negate();
+                        }
+                        BigDecimal total_fee = position.getOrderFee().add(position.getOrderSpread()).add(position.getOrderStayFee());
+                        BigDecimal position_profit = profit_and_lose.subtract(total_fee);
+                        allProfitAndLose = allProfitAndLose.add(position_profit);
+                        BigDecimal position_freez = position.getOrderTotalPrice().divide(new BigDecimal(position.getOrderLever().intValue()), 2, 4);
+                        allFreezAmt = allFreezAmt.add(position_freez).add(position.getMarginAdd());
+                        continue;
+                    }
+                    log.info("查询所有持仓单的总盈亏,现价返回0,当前为集合竞价");
                 }
-
-
-                BigDecimal total_fee = position.getOrderFee().add(position.getOrderSpread()).add(position.getOrderStayFee());
-
-
-                BigDecimal position_profit = profit_and_lose.subtract(total_fee);
-
-
-                allProfitAndLose = allProfitAndLose.add(position_profit);
-
-
-                BigDecimal position_freez = position.getOrderTotalPrice().divide(new BigDecimal(position.getOrderLever().intValue()), 2, 4);
-                //BigDecimal position_freez = position.getOrderTotalPrice();
-                allFreezAmt = allFreezAmt.add(position_freez).add(position.getMarginAdd());
-                continue;
             }
-            log.info("查询所有持仓单的总盈亏,现价返回0,当前为集合竞价");
         }
 
         //加上分仓交易保证金
-        List<FundsApply> fundsApplyList = fundsApplyMapper.getUserMarginList(userId);
-        for (FundsApply fundsApply : fundsApplyList) {
-            allFreezAmt = allFreezAmt.add(fundsApply.getMargin());
-        }
-
-
+//                    List<FundsApply> fundsApplyList = fundsApplyMapper.getUserMarginList(userId);
+//                    for (FundsApply fundsApply : fundsApplyList) {
+//                        allFreezAmt = allFreezAmt.add(fundsApply.getMargin());
+//                    }
         PositionVO positionVO = new PositionVO();
         positionVO.setAllProfitAndLose(allProfitAndLose);
         positionVO.setAllFreezAmt(allFreezAmt);
+        positionVO.setAllProfitAndLoseHk(allProfitAndLoseHk);
+        positionVO.setAllFreezAmtHk(allFreezAmtHk);
         return positionVO;
     }
 
@@ -1633,6 +1824,7 @@
         userPositionVO.setAllProfitAndLose(positionProfitVO.getAllProfitAndLose());
         userPositionVO.setNow_price(positionProfitVO.getNowPrice());
         userPositionVO.setAllProfitAndLoseStr(positionProfitVO.getAllProfitAndLoseStr());
+        userPositionVO.setOrderTotalPrice(positionProfitVO.getOrderTotalPrice());
 
 
         return userPositionVO;
@@ -1643,6 +1835,7 @@
         BigDecimal allProfitAndLose = new BigDecimal("0");
         String allProfitAndLoseStr = "";
         String nowPrice = "";
+        BigDecimal orderTotalPrice = position.getOrderTotalPrice();
 
         if (position.getSellOrderId() != null) {
 
@@ -1657,32 +1850,43 @@
             allProfitAndLose = profitAndLose.subtract(position.getOrderFee()).subtract(position.getOrderSpread()).subtract(position.getOrderStayFee()).subtract(position.getSpreadRatePrice());
         } else {
             StockListVO stockListVO = new StockListVO();
-            StockCoin stockCoin = new StockCoin();
             stockListVO = SinaStockApi.assembleLideStockListVO(LiDeDataUtils.getStock(position.getStockCode()));
             if (ObjectUtils.isEmpty(stockListVO)) {
-                stockListVO = SinaStockApi.assembleStockListVO(SinaStockApi.getSinaStock(position.getStockGid()));
+                if(StockType.HK.getCode().equals(position.getStockType())){
+                    stockListVO = SinaStockApi.assembleStockListVOHk(SinaStockApi.getSinaStock(StockType.HK.getCode()+position.getStockGid()));
+                }else {
+                    stockListVO = SinaStockApi.assembleStockListVO(SinaStockApi.getSinaStock(position.getStockGid()));
+                }
             }
-            //    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);
             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());
             //改成盈亏百分比
             BigDecimal allProfitAndLoseStrTemp = new BigDecimal(nowPrice).subtract(position.getBuyOrderPrice()).divide(position.getBuyOrderPrice(), 4, RoundingMode.HALF_UP).multiply(new BigDecimal(100)).setScale(2,4);
             allProfitAndLoseStr = allProfitAndLoseStrTemp.toString() + "%";
+            if ("买跌".equals(position.getOrderDirection())) {
+                profitAndLose = profitAndLose.negate();
+                allProfitAndLoseStr = allProfitAndLoseStrTemp.negate() + "%";
+            }
+            //总盈亏= 浮动盈亏 – 手续费 – 印花税 – 留仓费 – 点差费
+            allProfitAndLose = profitAndLose.subtract(position.getOrderFee()).subtract(position.getOrderSpread()).subtract(position.getOrderStayFee()).subtract(position.getSpreadRatePrice());
+
+
         }
         PositionProfitVO positionProfitVO = new PositionProfitVO();
         positionProfitVO.setProfitAndLose(profitAndLose);
+        positionProfitVO.setOrderTotalPrice(orderTotalPrice);
         positionProfitVO.setAllProfitAndLose(allProfitAndLose);
         positionProfitVO.setAllProfitAndLoseStr(allProfitAndLoseStr);
         positionProfitVO.setNowPrice(nowPrice);
@@ -2501,6 +2705,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