From b1bc403ef56a871e483e7ad91905e204a04541ec Mon Sep 17 00:00:00 2001
From: zj <1772600164@qq.com>
Date: Wed, 14 Jan 2026 22:04:08 +0800
Subject: [PATCH] 修改申购
---
src/main/java/com/nq/service/impl/UserPositionServiceImpl.java | 249 +++++++++++++++++++++++++++++--------------------
1 files changed, 149 insertions(+), 100 deletions(-)
diff --git a/src/main/java/com/nq/service/impl/UserPositionServiceImpl.java b/src/main/java/com/nq/service/impl/UserPositionServiceImpl.java
index c81c93c..8aeaa6b 100644
--- a/src/main/java/com/nq/service/impl/UserPositionServiceImpl.java
+++ b/src/main/java/com/nq/service/impl/UserPositionServiceImpl.java
@@ -117,6 +117,9 @@
@Autowired
UserPositionCheckDzService userPositionCheckDzService;
+ @Resource
+ StockBuySettingMapper stockBuySettingMapper;
+
@Transactional(rollbackFor = Exception.class)
@@ -148,16 +151,36 @@
}
- StockConfig mainBuyConfig = iStockConfigServices.queryByKey(EConfigKey.MIN_BUY.getCode());
-
+ /*StockConfig mainBuyConfig = iStockConfigServices.queryByKey(EConfigKey.MIN_BUY.getCode());
if(buyNum<Integer.parseInt(mainBuyConfig.getCValue())){
return ServerResponse.createByErrorMsg("最低购买数量"+mainBuyConfig.getCValue(), request);
+ }*/
+ //最低购买数量 US 10 印度100 香港台湾1000
+ /*Integer minBuyNum;
+ if (stock.getStockType().equals(EStockType.US.getCode())) {
+ minBuyNum = 10;
+ } else if (stock.getStockType().equals(EStockType.IN.getCode())) {
+ minBuyNum = 100;
+ } else {
+ minBuyNum = 1000;
}
+ if(buyNum < minBuyNum){
+ return ServerResponse.createByErrorMsg("最低购买数量" + minBuyNum, request);
+ }*/
+
+ StockBuySetting stockBuySetting = stockBuySettingMapper.selectOne(new QueryWrapper<StockBuySetting>().eq("accets_type", stock.getStockType()));
+ if (stockBuySetting != null && stockBuySetting.getHandsNum() != null && stockBuySetting.getStockNum() != null) {
+ if(buyNum < stockBuySetting.getHandsNum()){
+ return ServerResponse.createByErrorMsg("最低购买数量" + stockBuySetting.getHandsNum(), request);
+ }
+ buyNum = buyNum * stockBuySetting.getStockNum();
+ }
+
UserAssets userAssets = iUserAssetsServices.assetsByTypeAndUserId(stock.getStockType(), user.getId());
StockConfig maxBuyConfig = iStockConfigServices.queryByKey(EConfigKey.MAX_BUY.getCode());
- if(buyNum<Integer.parseInt(mainBuyConfig.getCValue())){
- return ServerResponse.createByErrorMsg("最高购买数量"+maxBuyConfig.getCValue(), request);
+ if(buyNum > Integer.parseInt(maxBuyConfig.getCValue())){
+ return ServerResponse.createByErrorMsg("最高购买数量" + maxBuyConfig.getCValue(), request);
}
if(userAssets.getAmountToBeCovered().compareTo(BigDecimal.ZERO) > 0){
return ServerResponse.createByErrorMsg("请先缴清待补资金", request);
@@ -177,20 +200,20 @@
return ServerResponse.createByErrorMsg("报价0,请稍后再试", request);
}
-
BigDecimal buyAmt = nowPrice.multiply(new BigDecimal(buyNum)).divide(new BigDecimal(lever));
- BigDecimal finalBuyAmt = buyAmt;
- //如果不是墨西哥币需要转换金额
- if (!stock.getStockType().equals(EStockType.MX.getCode())) {
- buyAmt = userAssetsServices.exchangeAmountByRate(stock.getStockType(), buyAmt);
- }
+ //手续费
BigDecimal orderFree = siteSettingBuyFee.multiply(buyAmt);
-
+ BigDecimal needBuyAmt = buyAmt.add(orderFree);
+ //资金校验
+ /*if (!stock.getStockType().equals(EStockType.getDefault().getCode())) {
+ needBuyAmt = userAssetsServices.exchangeAmountByRate(stock.getStockType(), buyAmt);
+ }*/
BigDecimal fundratio = new BigDecimal(user.getFundRatio()).divide(new BigDecimal(100));
BigDecimal availableBalance = fundratio.multiply(userAssets.getAvailableBalance());
- if (availableBalance.compareTo(buyAmt.add(orderFree)) < 0) {
+ if (availableBalance.compareTo(needBuyAmt) < 0) {
return ServerResponse.createByErrorMsg("订单失败,配资不足", request);
}
+
UserPosition userPosition = new UserPosition();
if (profitTarget != null && profitTarget.compareTo(new BigDecimal("0")) > 0) {
userPosition.setProfitTargetPrice(profitTarget);
@@ -217,7 +240,7 @@
}
userPosition.setIsLock(Integer.valueOf(0));
userPosition.setOrderLever(lever);
- userPosition.setOrderTotalPrice(finalBuyAmt);
+ userPosition.setOrderTotalPrice(buyAmt);
// 手续费
userPosition.setOrderFee(orderFree);
@@ -463,18 +486,38 @@
@Transactional(rollbackFor = Exception.class)
@Override
- public ServerResponse allSell(HttpServletRequest request, String stockType) throws Exception {
- //判断股票是否在可交易时间段
+ public ServerResponse allSell(HttpServletRequest request) throws Exception {
User user = iUserService.getCurrentUser(request);
QueryWrapper<UserPosition> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("user_id", user.getId());
queryWrapper.isNull("sell_order_id");
List<UserPosition> userPositionList = userPositionMapper.selectList(queryWrapper);
- for (int i = 0; i < userPositionList.size(); i++) {
- sell(userPositionList.get(i).getPositionSn(), 0);
+
+ // 如果没有需要平仓的持仓
+ if (userPositionList.isEmpty()) {
+ return ServerResponse.createBySuccessMsg("没有需要平仓的持仓订单");
}
- return ServerResponse.createBySuccessMsg("平仓成功!");
+
+ // 收集所有提示信息
+ List<String> successMessages = new ArrayList<>();
+ List<String> errorMessages = new ArrayList<>();
+
+ for (UserPosition position : userPositionList) {
+ ServerResponse response = sell(position.getPositionSn(), 0, position.getOrderNum(), request);
+
+ if (response.getStatus() == 0) {
+ // 失败的情况
+ errorMessages.add("订单号:" + position.getPositionSn() + " 失败,原因:" + response.getMsg());
+ }
+ }
+
+ // 根据是否有错误返回不同的状态
+ if (!errorMessages.isEmpty()) {
+ return ServerResponse.createByErrorMsg(errorMessages.toString(), request);
+ } else {
+ return ServerResponse.createBySuccessMsg("平仓成功!", request);
+ }
}
//用户追加保证金操作
@@ -1462,7 +1505,7 @@
userPosition.setOrderStayDays(Integer.valueOf(0));
userPosition.setOrderStayFee(new BigDecimal("0"));
- userPosition.setAmountToBeCovered(amountToBeCovered);
+ //userPosition.setAmountToBeCovered(amountToBeCovered);
userPosition.setNewId(stockSubscribe.getNewlistId());
int ret = 0;
ret = this.userPositionMapper.insert(userPosition);
@@ -1757,81 +1800,86 @@
* @return
*/
@Transactional(rollbackFor = Exception.class)
- public ServerResponse buyDz(Integer dzId, String password, Integer num, HttpServletRequest request) throws Exception {
- /*实名认证开关开启*/
- SiteProduct siteProduct = iSiteProductService.getProductSetting();
- User user = this.iUserService.getCurrentRefreshUser(request);
+ public ServerResponse buyDz(Integer dzId, String password, Integer num, HttpServletRequest request){
+ try {
+ /*实名认证开关开启*/
+ SiteProduct siteProduct = iSiteProductService.getProductSetting();
+ User user = this.iUserService.getCurrentRefreshUser(request);
- if (siteProduct.getRealNameDisplay() && user.getIsActive() != 2) {
- return ServerResponse.createByErrorMsg("Order failed, please first real name authentication");
- }
- if (siteProduct.getRealNameDisplay() && user.getIsLock().intValue() == 1) {
- return ServerResponse.createByErrorMsg("Order failed, account has been locked");
- }
- StockDz stockDz = this.stockDzMapper.selectOne(new QueryWrapper<StockDz>().eq("id", dzId));
- if (StringUtils.isNotEmpty(stockDz.getPassword()) && !Objects.equals(stockDz.getPassword(), password)) {
- return ServerResponse.createByErrorMsg("密码错误", request);
- }
- if (stockDz.getIsLock() != 0) {
- return ServerResponse.createByErrorMsg("股票被锁定,不能购买", request);
- }
- //价格处理
- Stock stock = stockMapper.selectOne(new QueryWrapper<Stock>().eq("stock_code", stockDz.getStockCode()));
+ if (siteProduct.getRealNameDisplay() && user.getIsActive() != 2) {
+ return ServerResponse.createByErrorMsg("Order failed, please first real name authentication");
+ }
+ if (siteProduct.getRealNameDisplay() && user.getIsLock().intValue() == 1) {
+ return ServerResponse.createByErrorMsg("Order failed, account has been locked");
+ }
+ StockDz stockDz = this.stockDzMapper.selectOne(new QueryWrapper<StockDz>().eq("id", dzId));
+ if (StringUtils.isNotEmpty(stockDz.getPassword()) && !Objects.equals(stockDz.getPassword(), password)) {
+ return ServerResponse.createByErrorMsg("密码错误", request);
+ }
+ if (stockDz.getIsLock() != 0) {
+ return ServerResponse.createByErrorMsg("股票被锁定,不能购买", request);
+ }
+ //价格处理
+ Stock stock = stockMapper.selectOne(new QueryWrapper<Stock>().eq("stock_code", stockDz.getStockCode()));
- UserAssets userAssets = userAssetsServices.assetsByTypeAndUserId(stock.getStockType(), user.getId());
- if(userAssets.getAmountToBeCovered().compareTo(BigDecimal.ZERO) > 0){
- return ServerResponse.createByErrorMsg("请先缴清待补资金", request);
- }
+ UserAssets userAssets = userAssetsServices.assetsByTypeAndUserId(stock.getStockType(), user.getId());
+ if(userAssets.getAmountToBeCovered().compareTo(BigDecimal.ZERO) > 0){
+ return ServerResponse.createByErrorMsg("请先缴清待补资金", request);
+ }
- if(stockDz.getStartTime().getTime() > new Date().getTime() || stockDz.getEndTime().getTime() < new Date().getTime()){
- return ServerResponse.createByErrorMsg("不在内幕交易时间之内", request);
- }
+ if(stockDz.getStartTime().getTime() > new Date().getTime() || stockDz.getEndTime().getTime() < new Date().getTime()){
+ return ServerResponse.createByErrorMsg("不在内幕交易时间之内", request);
+ }
// BigDecimal nowPrice = priceServices.getNowPrice(stockDz.getStockCode()).multiply(stockDz.getDiscount());
- BigDecimal nowPrice = stockDz.getNowPrice();
+ BigDecimal nowPrice = stockDz.getNowPrice();
- if (nowPrice.compareTo(new BigDecimal("0")) == 0) {
- return ServerResponse.createByErrorMsg("股票价格0,请重试", request);
- }
- if (stockDz.getStockNum() > num) {
- return ServerResponse.createByErrorMsg("最小购买数据" + stockDz.getStockNum(), request);
- }
+ if (nowPrice.compareTo(new BigDecimal("0")) == 0) {
+ return ServerResponse.createByErrorMsg("股票价格0,请重试", request);
+ }
+ if (stockDz.getStockNum() > num) {
+ return ServerResponse.createByErrorMsg("最小购买数据" + stockDz.getStockNum(), request);
+ }
- BigDecimal siteSettingBuyFee = new BigDecimal(iStockConfigServices.queryByKey(EConfigKey.BUY_HANDLING_CHARGE.getCode()).getCValue()) ;
+ BigDecimal siteSettingBuyFee = new BigDecimal(iStockConfigServices.queryByKey(EConfigKey.BUY_HANDLING_CHARGE.getCode()).getCValue()) ;
- BigDecimal buyAmt = nowPrice.multiply(new BigDecimal(num.intValue()));
- BigDecimal orderFree = siteSettingBuyFee.multiply(buyAmt);
- BigDecimal fundratio = new BigDecimal(user.getFundRatio()).divide(new BigDecimal(100));
- BigDecimal availableBalance = fundratio.multiply(userAssets.getAvailableBalance());
- if (availableBalance.compareTo(buyAmt.add(orderFree)) < 0) {
- return ServerResponse.createByErrorMsg("订单失败,配资不足", request);
- }
+ BigDecimal buyAmt = nowPrice.multiply(new BigDecimal(num.intValue()));
+ BigDecimal orderFree = siteSettingBuyFee.multiply(buyAmt);
+ BigDecimal fundratio = new BigDecimal(user.getFundRatio()).divide(new BigDecimal(100));
+ BigDecimal availableBalance = fundratio.multiply(userAssets.getAvailableBalance());
+ if (availableBalance.compareTo(buyAmt.add(orderFree)) < 0) {
+ return ServerResponse.createByErrorMsg("订单失败,配资不足", request);
+ }
- BigDecimal newBuyAmt = buyAmt;
- //如果不是墨西哥币需要转换金额
- if (!stock.getStockType().equals(EStockType.MX.getCode())) {
- newBuyAmt = userAssetsServices.exchangeAmountByRate(stock.getStockType(), buyAmt);
- }
- if(newBuyAmt.compareTo(userAssets.getAvailableBalance()) > 0){
- return ServerResponse.createByErrorMsg("可用余额不足" + userAssets.getAvailableBalance(), request);
- }
+ BigDecimal newBuyAmt = buyAmt;
+ //如果不是默认货币需要转换金额
+ /*if (!stock.getStockType().equals(EStockType.getDefault().getCode())) {
+ newBuyAmt = userAssetsServices.exchangeAmountByRate(stock.getStockType(), buyAmt);
+ }*/
+ if(newBuyAmt.compareTo(userAssets.getAvailableBalance()) > 0){
+ return ServerResponse.createByErrorMsg("可用余额不足" + userAssets.getAvailableBalance(), request);
+ }
- //判断审核开关
- if(stockDz.getSwitchType() == 1) {
+ //判断审核开关
+ if(stockDz.getSwitchType() == 1) {
+ UserPosition userPosition = getUserPosition(dzId,num, user, stockDz, nowPrice, stock, buyAmt);
+ UserPositionCheckDz userPositionCheckDz = Convert.convert(UserPositionCheckDz.class, userPosition);
+ userPositionCheckDz.setDzId(dzId);
+ userPositionCheckDzService.save(userPositionCheckDz);
+ return ServerResponse.createBySuccess("购买成功,等待审核", request);
+ }
+
+ // 创建UserPosition对象
UserPosition userPosition = getUserPosition(dzId,num, user, stockDz, nowPrice, stock, buyAmt);
- UserPositionCheckDz userPositionCheckDz = Convert.convert(UserPositionCheckDz.class, userPosition);
- userPositionCheckDz.setDzId(dzId);
- userPositionCheckDzService.save(userPositionCheckDz);
- return ServerResponse.createBySuccess("购买成功,等待审核", request);
+ userPositionMapper.insert(userPosition);
+ BigDecimal buy_fee_amt = siteSettingBuyFee.multiply(newBuyAmt);
+
+ userAssetsServices.availablebalanceChange(stock.getStockType(), user.getId(), EUserAssets.BUY, newBuyAmt.negate(),"","");
+ iUserAssetsServices.availablebalanceChange(stock.getStockType(), userAssets.getUserId(), EUserAssets.HANDLING_CHARGE, buy_fee_amt, "", "");
+ return ServerResponse.createBySuccess("购买成功", request);
+ } catch (Exception e) {
+ log.error("大宗下单异常{}", e.getMessage());
}
-
- // 创建UserPosition对象
- UserPosition userPosition = getUserPosition(dzId,num, user, stockDz, nowPrice, stock, buyAmt);
- userPositionMapper.insert(userPosition);
- BigDecimal buy_fee_amt = siteSettingBuyFee.multiply(newBuyAmt);
- //已经转化 直接穿MEX类型
- userAssetsServices.availablebalanceChange(EStockType.MX.getCode(), user.getId(), EUserAssets.BUY, newBuyAmt.negate(),"","");
- iUserAssetsServices.availablebalanceChange(EStockType.MX.getCode(), userAssets.getUserId(), EUserAssets.HANDLING_CHARGE, buy_fee_amt, "", "");
- return ServerResponse.createBySuccess("购买成功", request);
+ return ServerResponse.createByError();
}
private UserPosition getUserPosition(Integer dzId,Integer num, User user, StockDz stockDz, BigDecimal nowPrice, Stock stock, BigDecimal buyAmt) {
@@ -2107,20 +2155,18 @@
null, null, state, null);
BigDecimal usMarketValue = BigDecimal.ZERO; //美股市值
- BigDecimal mxMarketValue = BigDecimal.ZERO; //墨西哥股市值
+ BigDecimal jpMarketValue = BigDecimal.ZERO; //日股市值
+
BigDecimal usPositionEarnings = BigDecimal.ZERO; //美股持仓收益
- BigDecimal mxPositionEarnings = BigDecimal.ZERO; //墨西哥持仓收益
+ BigDecimal jpPositionEarnings = BigDecimal.ZERO; //日股持仓收益
+
BigDecimal usPositionEarningsParent = BigDecimal.ZERO; //美股持仓收益百分比
- BigDecimal mxPositionEarningsParent = BigDecimal.ZERO; //墨西哥持仓收益百分比
+ BigDecimal jpPositionEarningsParent = BigDecimal.ZERO; //日股持仓收益百分比
if (!userPositions.isEmpty()) {
for (UserPosition position : userPositions) {
BigDecimal nowPrice = priceServices.getNowPrice(position.getStockCode());
- /*if(state == 0){
- nowPrice = priceServices.getNowPrice(position.getStockCode());
- }else{
- nowPrice = position.getSellOrderPrice();
- }*/
+
UserPositionVO userPositionVO = UserPointUtil.assembleUserPositionVO(position,nowPrice);
userPositionVO.setOrderTotalPrice(userPositionVO.getOrderTotalPrice().multiply(new BigDecimal(userPositionVO.getOrderLever())));
@@ -2139,18 +2185,20 @@
}else{
userPositionVO.setProfitAndLose(userPositionVO.getProfitAndLose().multiply(new BigDecimal(userPositionVO.getOrderLever())));
}
+ BigDecimal nowPriceSUM = nowPrice.multiply(BigDecimal.valueOf(position.getOrderNum()));
if (position.getStockGid().equals(EStockType.US.getCode())) {
- usMarketValue = usMarketValue.add(nowPrice);
+ usMarketValue = usMarketValue.add(nowPriceSUM);
usPositionEarnings = usPositionEarnings.add(userPositionVO.getProfitAndLose());
usPositionEarningsParent = usPositionEarningsParent.add(userPositionVO.getProfitAndLoseParent2());
- } else if(position.getStockGid().equals(EStockType.MX.getCode())) {
- mxMarketValue = mxMarketValue.add(nowPrice);
- mxPositionEarnings = mxPositionEarnings.add(userPositionVO.getProfitAndLose());
- mxPositionEarningsParent = mxPositionEarningsParent.add(userPositionVO.getProfitAndLoseParent2());
+ } else if(position.getStockGid().equals(EStockType.JP.getCode())) {
+ jpMarketValue = jpMarketValue.add(nowPriceSUM);
+ jpPositionEarnings = jpPositionEarnings.add(userPositionVO.getProfitAndLose());
+ jpPositionEarningsParent = jpPositionEarningsParent.add(userPositionVO.getProfitAndLoseParent2());
}
+
}
usPositionEarnings = usPositionEarnings.setScale(2, RoundingMode.DOWN);
- mxPositionEarnings = mxPositionEarnings.setScale(2, RoundingMode.DOWN);
+ jpPositionEarnings = jpPositionEarnings.setScale(2, RoundingMode.DOWN);
}
Map<String, String> map = new HashMap<>();
@@ -2158,9 +2206,10 @@
map.put("usPositionEarnings", String.valueOf(usPositionEarnings)); //美股持仓收益
map.put("usPositionEarningsParent", usPositionEarningsParent + "%");//美股持仓收益率
- map.put("mxMarketValue", String.valueOf(mxMarketValue)); //墨西哥股市值
- map.put("mxPositionEarnings", String.valueOf(mxPositionEarnings)); //墨西哥持仓收益
- map.put("mxPositionEarningsParent", mxPositionEarningsParent + "%");//墨西哥持仓收益率
+ map.put("jpMarketValue", String.valueOf(jpMarketValue)); //日股市值
+ map.put("jpPositionEarnings", String.valueOf(jpPositionEarnings)); //日股持仓收益
+ map.put("jpPositionEarningsParent", jpPositionEarningsParent + "%");//日股持仓收益率
+
return ServerResponse.createBySuccess(map);
} catch (Exception e) {
log.error("IUserPositionService getMyPositionProfitAndLose error", e);
--
Gitblit v1.9.3