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/UserServiceImpl.java |  117 +++++++++++++++++++++++++++++++++++++++-------------------
 1 files changed, 78 insertions(+), 39 deletions(-)

diff --git a/src/main/java/com/nq/service/impl/UserServiceImpl.java b/src/main/java/com/nq/service/impl/UserServiceImpl.java
index 64b4d6c..f90f3ef 100644
--- a/src/main/java/com/nq/service/impl/UserServiceImpl.java
+++ b/src/main/java/com/nq/service/impl/UserServiceImpl.java
@@ -7,10 +7,12 @@
 import com.google.common.collect.Lists;
 import com.nq.common.ServerResponse;
 import com.nq.config.StockPoll;
+import com.nq.config.StockType;
 import com.nq.dao.*;
 import com.nq.pojo.*;
 import com.nq.service.*;
 import com.nq.utils.DateTimeUtil;
+import com.nq.utils.KeyUtils;
 import com.nq.utils.PropertiesUtil;
 import com.nq.utils.SymmetricCryptoUtil;
 import com.nq.utils.ip.IpUtils;
@@ -44,6 +46,7 @@
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
+import org.springframework.web.bind.annotation.RequestParam;
 
 @Service("iUserService")
 public class UserServiceImpl implements IUserService {
@@ -178,12 +181,28 @@
         int insertCount = this.userMapper.insert(user);
 
         if (insertCount > 0) {
+            //用户注册成功重新代理机构码重新生成
+            String newAgentCode = generateUniqueAgentCode();
+            agentUser.setAgentCode(newAgentCode);
+            iAgentUserService.updateAgentCode(agentUser);
             log.info("用户注册成功 手机 {} , ip = {} 地址 = {}", new Object[] { phone, uip, uadd });
             return ServerResponse.createBySuccessMsg("注册成功.请登录");
         }
         return ServerResponse.createBySuccessMsg("注册出错, 请重试");
     }
 
+    public String generateUniqueAgentCode() {
+        // 生成新的 agent code
+        String newAgentCode = KeyUtils.getAgentUniqueKey();
+
+        // 如果找到重复的 agent code,则递归重新生成
+        if (iAgentUserService.findByCode(newAgentCode) != null) {
+            return generateUniqueAgentCode();  // 递归调用
+        }
+
+        // 返回唯一的 agent code
+        return newAgentCode;
+    }
 
 
     public ServerResponse login(String phone, String userPwd, HttpServletRequest request) {
@@ -238,15 +257,13 @@
         }
     }
 
-    public ServerResponse addOption(String code, HttpServletRequest request) {
+    public ServerResponse addOption(String stockType, String code, HttpServletRequest request) {
         User user = getCurrentUser(request);
         if (user == null) {
             return ServerResponse.createBySuccessMsg("請先登錄");
         }
+
         String stockcode = code;
-        if(code.contains("hf")){
-            stockcode = code.split("_")[1];
-        }
         stockcode = stockcode.replace("sh","").replace("sz","").replace("bj","");
         StockOption dboption = this.stockOptionMapper.findMyOptionIsExistByCode(user.getId(), stockcode);
 
@@ -276,7 +293,11 @@
 //                stock.setIsLock(0);
 //            }
         } else {
-            stock = this.stockMapper.findStockByCode(code);
+            if(StockType.HK.getCode().equals(stockType)) {
+                stock = stockMapper.findStockByCodeAndType(stockType,code);
+            }else {
+                stock = this.stockMapper.findStockByCode(code);
+            }
         }
         if (stock == null) {
             return ServerResponse.createByErrorMsg("添加失败,股票不存在");
@@ -285,7 +306,14 @@
         stockOption.setUserId(user.getId());
         stockOption.setStockId(stock.getId());
         stockOption.setAddTime(new Date());
-        stockOption.setStockCode(stock.getStockCode());
+
+        if(StockType.HK.getCode().equals(stockType)) {
+            stockOption.setStockCode(stock.getStockGid());
+            stockOption.setStockType(StockType.HK.getCode());
+        }else {
+            stockOption.setStockCode(stock.getStockCode());
+            stockOption.setStockType(StockType.A.getCode());
+        }
         stockOption.setStockName(stock.getStockName());
         stockOption.setStockGid(stock.getStockGid());
         stockOption.setIsLock(stock.getIsLock());
@@ -1814,53 +1842,59 @@
 
         PositionVO positionVO = this.iUserPositionService.findUserPositionAllProfitAndLose(user.getId());
         userInfoVO.setAllFreezAmt(positionVO.getAllFreezAmt());
+        userInfoVO.setAllFreezAmtHk(positionVO.getAllFreezAmtHk());
         BigDecimal allProfitAndLose = positionVO.getAllProfitAndLose();
+        BigDecimal allProfitAndLosehk = positionVO.getAllProfitAndLoseHk();
         userInfoVO.setAllProfitAndLose(allProfitAndLose);
+        userInfoVO.setAllProfitAndLoseHk(allProfitAndLosehk);
 
-//        BigDecimal userAllAmt = user.getUserAmt();
         BigDecimal userAllAmt = user.getEnableAmt();
         userAllAmt = userAllAmt.add(allProfitAndLose);
-        userInfoVO.setUserAmt(userAllAmt);
+
 
         userInfoVO.setEnableIndexAmt(user.getEnableIndexAmt());
         userInfoVO.setEnaleWithdrawAmt(user.getEnaleWithdrawAmt());
         userInfoVO.setHkAmt(user.getHkAmt());
 
         BigDecimal historyProfitLoss = new BigDecimal(0);
+        BigDecimal buyAmtAutual = new BigDecimal(0);
+        BigDecimal historyProfitLossHk = new BigDecimal(0);
+        BigDecimal buyAmtAutualHk = new BigDecimal(0);
+        List<UserPosition> userPositionscc = this.userPositionMapper.findMyPositionByCodeAndSpell(user.getId(), "", "", 0);
+
+        if (userPositionscc.size() > 0) {
+            for (UserPosition position : userPositionscc) {
+                if(StockType.HK.getCode().equals(position.getStockType())){
+                    buyAmtAutualHk = buyAmtAutualHk.add(position.getBuyOrderPrice()
+                            .multiply(new BigDecimal(position.getOrderNum()))
+                            .divide(new BigDecimal(position.getOrderLever()), 2, RoundingMode.HALF_UP));
+                }else {
+                    buyAmtAutual = buyAmtAutual.add(position.getBuyOrderPrice()
+                            .multiply(new BigDecimal(position.getOrderNum()))
+                            .divide(new BigDecimal(position.getOrderLever()), 2, RoundingMode.HALF_UP));
+                }
+            }
+        }
+        userInfoVO.setBuyAmtAutual(buyAmtAutual);
+        userAllAmt = userAllAmt.add(buyAmtAutual);
+        userInfoVO.setUserAmt(userAllAmt);
+
         List<UserPosition> userPositions = this.userPositionMapper.findMyPositionByCodeAndSpell(user.getId(), "", "", 1);
 
         if (userPositions.size() > 0) {
             for (UserPosition position : userPositions) {
-                UserPositionVO userPositionVO = assembleUserPositionVO(position);
-                historyProfitLoss = historyProfitLoss.add(userPositionVO.getAllProfitAndLose());
+                if(StockType.HK.getCode().equals(position.getStockType())){
+                    UserPositionVO userPositionVO = assembleUserPositionVO(position);
+                    historyProfitLossHk = historyProfitLossHk.add(userPositionVO.getAllProfitAndLose());
+                }else {
+                    UserPositionVO userPositionVO = assembleUserPositionVO(position);
+                    historyProfitLoss = historyProfitLoss.add(userPositionVO.getAllProfitAndLose());
+                }
+
             }
         }
         userInfoVO.setHistoryProfitLoss(historyProfitLoss);
-
-//        IndexPositionVO indexPositionVO = this.iUserIndexPositionService.findUserIndexPositionAllProfitAndLose(user.getId());
-//        BigDecimal allIndexProfitAndLose = indexPositionVO.getAllIndexProfitAndLose();
-//        userInfoVO.setAllIndexProfitAndLose(allIndexProfitAndLose);
-//        userInfoVO.setAllIndexFreezAmt(indexPositionVO.getAllIndexFreezAmt());
-//
-//        BigDecimal userAllIndexAmt = user.getUserIndexAmt();
-//        userAllIndexAmt = userAllIndexAmt.add(allIndexProfitAndLose);
-//        userInfoVO.setUserIndexAmt(userAllIndexAmt);
-//
-//        userInfoVO.setEnableFuturesAmt(user.getEnableFutAmt());
-//
-//
-//        FuturesPositionVO futuresPositionVO = this.iUserFuturesPositionService.findUserFuturesPositionAllProfitAndLose(user.getId());
-//
-//        userInfoVO.setAllFuturesFreezAmt(futuresPositionVO.getAllFuturesDepositAmt());
-//
-//
-//        BigDecimal allFuturesProfitAndLose = futuresPositionVO.getAllFuturesProfitAndLose();
-//        userInfoVO.setAllFuturesProfitAndLose(allFuturesProfitAndLose);
-
-
-//        BigDecimal userAllFuturesAmt = user.getUserFutAmt();
-//        userAllFuturesAmt = userAllFuturesAmt.add(allFuturesProfitAndLose);
-//        userInfoVO.setUserFuturesAmt(userAllFuturesAmt);
+        userInfoVO.setHistoryProfitLossHk(historyProfitLossHk);
         userInfoVO.setDjzj(user.getDjzj());
         return userInfoVO;
     }
@@ -1916,7 +1950,8 @@
         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())).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();
             }
@@ -1928,16 +1963,20 @@
             StockCoin stockCoin = new StockCoin();
             stockListVO = SinaStockApi.assembleLideStockListVO(LiDeDataUtils.getStock(position.getStockCode()));
             if (org.springframework.util.ObjectUtils.isEmpty(stockListVO)) {
-                stockListVO = SinaStockApi.assembleStockListVO(SinaStockApi.getSinaStock(position.getStockGid()));
+                if(StockType.HK.getCode().equals(position.getStockType())){
+                    stockListVO = SinaStockApi.assembleStockListVOHk(SinaStockApi.getSinaStock(position.getStockType()+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);
             }
 
             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();
             }

--
Gitblit v1.9.3