From 64bc81d5f7bd99b470422b329aaca2182b79531c Mon Sep 17 00:00:00 2001
From: dd <gitluke@outlook.com>
Date: Mon, 01 Jun 2026 09:11:45 +0800
Subject: [PATCH] 1
---
src/main/java/com/nq/service/impl/UserPositionServiceImpl.java | 359 ++++++++++++++++++++++++++++++++++-------------------------
1 files changed, 204 insertions(+), 155 deletions(-)
diff --git a/src/main/java/com/nq/service/impl/UserPositionServiceImpl.java b/src/main/java/com/nq/service/impl/UserPositionServiceImpl.java
index 377b1fd..5162932 100644
--- a/src/main/java/com/nq/service/impl/UserPositionServiceImpl.java
+++ b/src/main/java/com/nq/service/impl/UserPositionServiceImpl.java
@@ -11,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;
@@ -111,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 {
@@ -385,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);
+ 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) {
@@ -445,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);
@@ -477,33 +480,11 @@
userPosition.setOrderStayDays(Integer.valueOf(0));
userPosition.setOrderStayFee(new BigDecimal("0"));
- 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("用户交易下单】保存持仓记录出错");
@@ -514,12 +495,111 @@
}
- public ServerResponse fee(Integer buyNum,BigDecimal nowPrice){
+ @Override
+ public ServerResponse fee(Integer buyNum, BigDecimal nowPrice) {
BigDecimal buy_amt = nowPrice.multiply(new BigDecimal(buyNum.intValue()));
- SiteSetting siteSetting = this.iSiteSettingService.getSiteSetting();
- BigDecimal buy_fee_amt = buy_amt.multiply(siteSetting.getBuyFee()).setScale(2, 4);
+ BigDecimal buy_fee_amt = TradeFeeUtil.calcBuyFee(buy_amt).setScale(2, 4);
return ServerResponse.createBySuccess(buy_fee_amt);
}
+
+ /** 下单从两融可用资金扣除:保证金 + 买入手续费 */
+ 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 {
@@ -708,11 +788,10 @@
.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 = TradeFeeUtil.calcBuyFee(buy_amt);
+ 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");
@@ -751,7 +830,6 @@
BigDecimal allStayFee = stayFee.multiply(new BigDecimal(1));
userPosition.setOrderStayFee(allStayFee);
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);
BigDecimal buy_yhs_amt = buy_amt.multiply(siteSetting.getDutyFee()).setScale(2, 4);
@@ -772,40 +850,60 @@
userPosition.setAllProfitAndLose(all_profit_and_lose);
userPosition.setOrderStayDays(Integer.valueOf(0));
userPosition.setOrderStayFee(new BigDecimal("0"));
- 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);
-// user.setDjzj(user.getDjzj().subtract(buy_amt_autual));
- int updateUserCount = this.userMapper.updateByPrimaryKeySelective(user);
- if (updateUserCount > 0) {
- log.info("【用户交易下单】修改用户金额成功");
- } else {
- log.error("用户交易下单】修改用户金额出错");
- throw new Exception("用户交易下单】修改用户金额出错");
- }
- //核算代理收入-入仓手续费
-// iAgentAgencyFeeService.AgencyFeeIncome(1, userPosition.getPositionSn());
+ 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("撤单成功");
}
@@ -1055,8 +1153,8 @@
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 = sell_fee_amt.add(orderSpread).add(orderStayFee).add(spreadRatePrice);
log.info("总的手续费费用 = {}", all_fee_amt);
userPosition.setSellOrderId(GeneratePosition.getPositionId());
@@ -1266,7 +1364,7 @@
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);
+ BigDecimal buy_fee_amt = TradeFeeUtil.calcBuyFee(all_buy_amt);
log.info("買入手續費 = {}", buy_fee_amt);
BigDecimal orderSpread = all_buy_amt.multiply(siteSetting.getDutyFee()).setScale(2, 4);
@@ -1281,9 +1379,8 @@
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);
+ // 买入手续费已在下单时扣除
+ BigDecimal all_fee_amt = sell_fee_amt.add(orderSpread);
log.info("總的手續費費用 = {}", all_fee_amt);
//复制一条新订单
UserPosition userPositionNew = new UserPosition();
@@ -1425,7 +1522,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) {
@@ -1823,13 +1921,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);
+ 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) {
@@ -1886,7 +1982,7 @@
userPosition.setOrderStayDays(1);
- BigDecimal buy_fee_amt = buy_amt.multiply(siteSetting.getBuyFee()).setScale(2, 4);
+ BigDecimal buy_fee_amt = TradeFeeUtil.calcBuyFee(buy_amt).setScale(2, 4);
log.info("创建模拟持仓 手续费(配资后总资金 * 百分比) = {}", buy_fee_amt);
userPosition.setOrderFee(buy_fee_amt);
@@ -1931,20 +2027,19 @@
userPosition.setOrderStayFee(new BigDecimal("0"));
userPosition.setSpreadRatePrice(new BigDecimal("0"));
- 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("生成持仓成功");
@@ -2375,7 +2470,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);
@@ -2649,11 +2744,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_dz = TradeFeeUtil.calcBuyFee(buy_amt);
+ 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) {
@@ -2690,7 +2784,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);
@@ -2723,36 +2817,14 @@
log.info("--------------购买逻辑股票数据 buyDz stock------" + new Gson().toJson(userPosition));
- int insertPositionCount = 0;
this.userPositionMapper.insert(userPosition);
- insertPositionCount = userPosition.getId();
- if (insertPositionCount > 0) {
+ if (userPosition.getId() != null && userPosition.getId() > 0) {
//修改大宗剩余
stockDz.setStockShare(stockDz.getStockShare() - num);
stockDz.setStockSurplus(stockDz.getStockSurplus() + num);
stockDzMapper.updateById(stockDz);
- //修改用戶可用余額= 當前余額-下單金額-買入手續費-印花稅-點差費
- //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("用戶交易下單】修改用戶金額出錯");
- }
- //核算代理收入-入倉手續費
+ deductUserEnableOnBuy(user, buy_debit_dz, buy_amt_autual, buy_fee_amt_dz, userPosition);
+ syncUserCacheAfterTrade(request);
iAgentAgencyFeeService.AgencyFeeIncome(1, userPosition.getPositionSn());
log.info("【用戶交易下單】保存持倉記錄成功");
} else {
@@ -2980,11 +3052,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);
+ 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) {
@@ -3038,7 +3109,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);
@@ -3072,31 +3142,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("【用戶交易下單】保存持倉記錄成功");
--
Gitblit v1.9.3