From 716f77637e6e324fd5865e82039c2c2ee3f94bfa Mon Sep 17 00:00:00 2001
From: zj <1772600164@qq.com>
Date: Wed, 14 Jan 2026 15:53:06 +0800
Subject: [PATCH] 1
---
src/main/java/com/nq/service/impl/UserPositionServiceImpl.java | 982 +++++++++++++++++++++++++++++++++++++--------------------
1 files changed, 640 insertions(+), 342 deletions(-)
diff --git a/src/main/java/com/nq/service/impl/UserPositionServiceImpl.java b/src/main/java/com/nq/service/impl/UserPositionServiceImpl.java
index 9dc1536..8aeaa6b 100644
--- a/src/main/java/com/nq/service/impl/UserPositionServiceImpl.java
+++ b/src/main/java/com/nq/service/impl/UserPositionServiceImpl.java
@@ -4,6 +4,7 @@
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.nq.common.ResponseCode;
import com.nq.dao.*;
import com.nq.enums.EConfigKey;
import com.nq.enums.EStockType;
@@ -15,11 +16,9 @@
import com.google.common.collect.Lists;
import com.nq.common.ServerResponse;
import com.nq.utils.*;
-import com.nq.utils.redis.RedisKeyUtil;
import com.nq.utils.stock.BuyAndSellUtils;
import com.nq.utils.stock.GeneratePosition;
import com.nq.utils.stock.GetStayDays;
-import com.nq.utils.stock.pinyin.GetPyByChinese;
import com.nq.utils.stock.sina.StockApi;
import com.nq.utils.timeutil.DateTimeUtil;
import com.nq.vo.agent.AgentIncomeVO;
@@ -35,14 +34,12 @@
import java.math.BigDecimal;
+import java.math.RoundingMode;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.*;
-import java.util.concurrent.atomic.AtomicBoolean;
-import java.util.concurrent.locks.Lock;
-import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
@@ -91,14 +88,10 @@
@Autowired
StockMapper stockMapper;
@Autowired
- AgentAgencyFeeMapper agentAgencyFeeMapper;
- @Autowired
IAgentAgencyFeeService iAgentAgencyFeeService;
@Autowired
ISiteProductService iSiteProductService;
- @Autowired
- FundsApplyMapper fundsApplyMapper;
@Autowired
UserStockSubscribeMapper userStockSubscribeMapper;
@Resource
@@ -106,12 +99,6 @@
@Resource
UserIndexPositionMapper userIndexPositionMapper;
- @Autowired
- IStockFuturesService iStockFuturesService;
- @Autowired
- IStockCoinService iStockCoinService;
- @Autowired
- CurrencyUtils currencyUtils;
@Resource
StockDzMapper stockDzMapper;
@@ -130,114 +117,147 @@
@Autowired
UserPositionCheckDzService userPositionCheckDzService;
+ @Resource
+ StockBuySettingMapper stockBuySettingMapper;
- @Transactional
+
+ @Transactional(rollbackFor = Exception.class)
public ServerResponse buy(Integer stockId, Integer buyNum, Integer buyType, Integer lever, BigDecimal profitTarget, BigDecimal stopTarget, HttpServletRequest request) {
SiteProduct siteProduct = iSiteProductService.getProductSetting();
User user = this.iUserService.getCurrentRefreshUser(request);
- synchronized (user.getId()){
- if (siteProduct.getRealNameDisplay() && user.getIsActive() != 2) {
- return ServerResponse.createByErrorMsg("订单失败,请先实名认证", request);
- }
- // 手续费率
- BigDecimal siteSettingBuyFee = new BigDecimal(iStockConfigServices.queryByKey(EConfigKey.BUY_HANDLING_CHARGE.getCode()).getCValue()) ;
+ try {
+ synchronized (user.getId()){
+ if (siteProduct.getRealNameDisplay() && user.getIsActive() != 2) {
+ return ServerResponse.createByErrorMsg("订单失败,请先实名认证", request);
+ }
+ // 手续费率
+ BigDecimal siteSettingBuyFee = new BigDecimal(iStockConfigServices.queryByKey(EConfigKey.BUY_HANDLING_CHARGE.getCode()).getCValue()) ;
- if (siteProduct.getRealNameDisplay() && user.getIsLock().intValue() == 1) {
- return ServerResponse.createByErrorMsg("订单失败,帐户已被锁定", request);
- }
+ if (siteProduct.getRealNameDisplay() && user.getIsLock().intValue() == 1) {
+ return ServerResponse.createByErrorMsg("订单失败,帐户已被锁定", request);
+ }
- Stock stock = stockMapper.selectByPrimaryKey(stockId);
- if (stock == null) {
- return ServerResponse.createByErrorMsg("订单失败,股票代码不存在", request);
- }
- //判断股票是否在可交易时间段
- Boolean b = tradingHourService.timeCheck(stock.getStockCode(), stock.getStockType());
- if (!b) {
- return ServerResponse.createByErrorMsg("订单失败,不在交易时间之内", request);
- }
+ Stock stock = stockMapper.selectByPrimaryKey(stockId);
+ if (stock == null) {
+ return ServerResponse.createByErrorMsg("订单失败,股票代码不存在", request);
+ }
+ //判断股票是否在可交易时间段
+ Boolean b = tradingHourService.timeCheck(stock.getStockCode(), stock.getStockType());
+ if (!b) {
+ return ServerResponse.createByErrorMsg("订单失败,不在交易时间之内", request);
+ }
- 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);
+ }*/
- if(buyNum<Integer.parseInt(mainBuyConfig.getCValue())){
- return ServerResponse.createByErrorMsg("最低购买数量"+mainBuyConfig.getCValue(), 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(userAssets.getAmountToBeCovered().compareTo(BigDecimal.ZERO) > 0){
- return ServerResponse.createByErrorMsg("请先缴清待补资金", request);
- }
- if (stock.getIsLock() != 0) {
- return ServerResponse.createByErrorMsg("订单失败,股票被锁定", request);
- }
- if (!priceServices.isLimitUpBuy(stock.getStockCode())) {
- return ServerResponse.createByErrorMsg("暂无配额", request);
- }
+ UserAssets userAssets = iUserAssetsServices.assetsByTypeAndUserId(stock.getStockType(), user.getId());
+ StockConfig maxBuyConfig = iStockConfigServices.queryByKey(EConfigKey.MAX_BUY.getCode());
+ if(buyNum > Integer.parseInt(maxBuyConfig.getCValue())){
+ return ServerResponse.createByErrorMsg("最高购买数量" + maxBuyConfig.getCValue(), request);
+ }
+ if(userAssets.getAmountToBeCovered().compareTo(BigDecimal.ZERO) > 0){
+ return ServerResponse.createByErrorMsg("请先缴清待补资金", request);
+ }
+ if (stock.getIsLock() != 0) {
+ return ServerResponse.createByErrorMsg("订单失败,股票被锁定", request);
+ }
- //股票类型 现价 数据源的处理
- BigDecimal nowPrice = priceServices.getNowPrice(stock.getStockCode());
+ if (!priceServices.isLimitUpBuy(stock.getStockCode())) {
+ return ServerResponse.createByErrorMsg("暂无配额", request);
+ }
- if (nowPrice.compareTo(new BigDecimal("0")) == 0) {
- return ServerResponse.createByErrorMsg("报价0,请稍后再试", request);
- }
+ //股票类型 现价 数据源的处理
+ BigDecimal nowPrice = priceServices.getNowPrice(stock.getStockCode());
- BigDecimal buyAmt = nowPrice.multiply(new BigDecimal(buyNum)).divide(new BigDecimal(lever));
- BigDecimal orderFree = siteSettingBuyFee.multiply(buyAmt);
+ if (nowPrice.compareTo(new BigDecimal("0")) == 0) {
+ return ServerResponse.createByErrorMsg("报价0,请稍后再试", request);
+ }
- 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);
- }
- UserPosition userPosition = new UserPosition();
- if (profitTarget != null && profitTarget.compareTo(new BigDecimal("0")) > 0) {
- userPosition.setProfitTargetPrice(profitTarget);
- }
- if (stopTarget != null && stopTarget.compareTo(new BigDecimal("0")) > 0) {
- userPosition.setStopTargetPrice(stopTarget);
- }
- userPosition.setPositionType(user.getAccountType());
- userPosition.setPositionSn(KeyUtils.getUniqueKey());
- userPosition.setUserId(user.getId());
- userPosition.setNickName(user.getRealName());
- userPosition.setAgentId(user.getAgentId());
- userPosition.setStockCode(stock.getStockCode());
- userPosition.setStockName(stock.getStockName());
- userPosition.setStockGid(stock.getStockType());
- userPosition.setStockSpell(stock.getStockSpell());
- userPosition.setBuyOrderId(GeneratePosition.getPositionId());
- userPosition.setBuyOrderTime(new Date());
- userPosition.setBuyOrderPrice(nowPrice);
- userPosition.setOrderDirection((buyType.intValue() == 0) ? "买涨" : "买跌");
- userPosition.setOrderNum(buyNum);
- if (stock.getStockPlate() != null) {
- userPosition.setStockPlate(stock.getStockPlate());
- }
- userPosition.setIsLock(Integer.valueOf(0));
- userPosition.setOrderLever(lever);
- userPosition.setOrderTotalPrice(buyAmt);
- // 手续费
+ BigDecimal buyAmt = nowPrice.multiply(new BigDecimal(buyNum)).divide(new BigDecimal(lever));
+ //手续费
+ 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(needBuyAmt) < 0) {
+ return ServerResponse.createByErrorMsg("订单失败,配资不足", request);
+ }
- userPosition.setOrderFee(orderFree);
- userPosition.setOrderSpread(BigDecimal.ZERO);
- userPosition.setSpreadRatePrice(BigDecimal.ZERO);
- BigDecimal profit_and_lose = new BigDecimal("0");
- userPosition.setProfitAndLose(profit_and_lose);
- userPosition.setAllProfitAndLose(profit_and_lose.add(orderFree));
- userPosition.setOrderStayDays(Integer.valueOf(0));
- userPosition.setOrderStayFee(BigDecimal.ZERO);
- userPositionMapper.insert(userPosition);
- iUserAssetsServices.availablebalanceChange(EStockType.MX.getCode(), user.getId(), EUserAssets.BUY, buyAmt.negate(), "", "");
- iUserAssetsServices.availablebalanceChange(EStockType.MX.getCode(), user.getId(), EUserAssets.HANDLING_CHARGE, orderFree, "", "");
- return ServerResponse.createBySuccessMsg("下单成功", request);
+ UserPosition userPosition = new UserPosition();
+ if (profitTarget != null && profitTarget.compareTo(new BigDecimal("0")) > 0) {
+ userPosition.setProfitTargetPrice(profitTarget);
+ }
+ if (stopTarget != null && stopTarget.compareTo(new BigDecimal("0")) > 0) {
+ userPosition.setStopTargetPrice(stopTarget);
+ }
+ userPosition.setPositionType(user.getAccountType());
+ userPosition.setPositionSn(KeyUtils.getUniqueKey());
+ userPosition.setUserId(user.getId());
+ userPosition.setNickName(user.getRealName());
+ userPosition.setAgentId(user.getAgentId());
+ userPosition.setStockCode(stock.getStockCode());
+ userPosition.setStockName(stock.getStockName());
+ userPosition.setStockGid(stock.getStockType());
+ userPosition.setStockSpell(stock.getStockSpell());
+ userPosition.setBuyOrderId(GeneratePosition.getPositionId());
+ userPosition.setBuyOrderTime(new Date());
+ userPosition.setBuyOrderPrice(nowPrice);
+ userPosition.setOrderDirection((buyType.intValue() == 0) ? "买涨" : "买跌");
+ userPosition.setOrderNum(buyNum);
+ if (stock.getStockPlate() != null) {
+ userPosition.setStockPlate(stock.getStockPlate());
+ }
+ userPosition.setIsLock(Integer.valueOf(0));
+ userPosition.setOrderLever(lever);
+ userPosition.setOrderTotalPrice(buyAmt);
+ // 手续费
+
+ userPosition.setOrderFee(orderFree);
+ userPosition.setOrderSpread(BigDecimal.ZERO);
+ userPosition.setSpreadRatePrice(BigDecimal.ZERO);
+ BigDecimal profit_and_lose = new BigDecimal("0");
+ userPosition.setProfitAndLose(profit_and_lose);
+ userPosition.setAllProfitAndLose(profit_and_lose.add(orderFree));
+ userPosition.setOrderStayDays(Integer.valueOf(0));
+ userPosition.setOrderStayFee(BigDecimal.ZERO);
+ userPositionMapper.insert(userPosition);
+ iUserAssetsServices.availablebalanceChange(stock.getStockType(), user.getId(), EUserAssets.BUY, buyAmt.negate(), "", "");
+ iUserAssetsServices.availablebalanceChange(stock.getStockType(), user.getId(), EUserAssets.HANDLING_CHARGE, orderFree, "", "");
+ return ServerResponse.createBySuccessMsg("下单成功", request);
+ }
+ } catch (Exception e) {
+ return ServerResponse.createByErrorMsg(e.getMessage());
}
}
@@ -284,61 +304,65 @@
}
- @Transactional
+ @Transactional(rollbackFor = Exception.class)
public ServerResponse sell(String positionSn, int doType) {
- UserPosition userPosition = this.userPositionMapper.findPositionBySn(positionSn);
- if (userPosition == null) {
- return ServerResponse.createByErrorMsg("平仓失败,订单不存在");
- }
- BigDecimal siitteBuyFee = iSiteSettingService.getSiteSetting().getBuyFee();
- Stock stock = stockMapper.selectOne(new QueryWrapper<Stock>().eq("stock_code", userPosition.getStockCode()));
- if(null == stock){
- return ServerResponse.createByErrorMsg("股票不存在,平仓失败");
- }
- Boolean b = tradingHourService.timeCheck(userPosition.getStockCode(), stock.getStockType());
- if (!b) {
- return ServerResponse.createByErrorMsg("订单失败,不在交易时间之内");
- }
- User user = this.userMapper.selectById(userPosition.getUserId());
- if (user == null) {
- return ServerResponse.createByErrorMsg("平仓失败,用户不存在");
- }
- if (user.getIsLock() == 1) {
- return ServerResponse.createByErrorMsg("账户已被限制交易");
- }
- if (userPosition.getSellOrderId() != null) {
- return ServerResponse.createByErrorMsg("平仓失败, 订单已平仓");
- }
- if (1 == userPosition.getIsLock().intValue()) {
- return ServerResponse.createByErrorMsg("this order is closed " + userPosition.getLockMsg());
- }
- //Stock stock = stockMapper.selectOne(new QueryWrapper<Stock>().eq("stock_code", userPosition.getStockCode()));
- BigDecimal nowPrice = priceServices.getNowPrice(userPosition.getStockCode());
- if (nowPrice.compareTo(new BigDecimal("0")) != 1) {
- return ServerResponse.createByErrorMsg("报价0,平仓失败,请稍后再试");
- }
- userPosition.setSellOrderId(GeneratePosition.getPositionId());
- userPosition.setSellOrderPrice(nowPrice);
- userPosition.setSellOrderTime(new Date());
+ try {
+ UserPosition userPosition = this.userPositionMapper.findPositionBySn(positionSn);
+ if (userPosition == null) {
+ return ServerResponse.createByErrorMsg("平仓失败,订单不存在");
+ }
+ BigDecimal siitteBuyFee = iSiteSettingService.getSiteSetting().getBuyFee();
+ Stock stock = stockMapper.selectOne(new QueryWrapper<Stock>().eq("stock_code", userPosition.getStockCode()));
+ if(null == stock){
+ return ServerResponse.createByErrorMsg("股票不存在,平仓失败");
+ }
+ Boolean b = tradingHourService.timeCheck(userPosition.getStockCode(), stock.getStockType());
+ if (!b) {
+ return ServerResponse.createByErrorMsg("订单失败,不在交易时间之内");
+ }
+ User user = this.userMapper.selectById(userPosition.getUserId());
+ if (user == null) {
+ return ServerResponse.createByErrorMsg("平仓失败,用户不存在");
+ }
+ if (user.getIsLock() == 1) {
+ return ServerResponse.createByErrorMsg("账户已被限制交易");
+ }
+ if (userPosition.getSellOrderId() != null) {
+ return ServerResponse.createByErrorMsg("平仓失败, 订单已平仓");
+ }
+ if (1 == userPosition.getIsLock().intValue()) {
+ return ServerResponse.createByErrorMsg("this order is closed " + userPosition.getLockMsg());
+ }
+ //Stock stock = stockMapper.selectOne(new QueryWrapper<Stock>().eq("stock_code", userPosition.getStockCode()));
+ BigDecimal nowPrice = priceServices.getNowPrice(userPosition.getStockCode());
+ if (nowPrice.compareTo(new BigDecimal("0")) != 1) {
+ return ServerResponse.createByErrorMsg("报价0,平仓失败,请稍后再试");
+ }
+ userPosition.setSellOrderId(GeneratePosition.getPositionId());
+ userPosition.setSellOrderPrice(nowPrice);
+ userPosition.setSellOrderTime(new Date());
- BigDecimal sellOrderTotel = nowPrice.multiply(new BigDecimal(userPosition.getOrderNum()));
- BigDecimal xsPrice = sellOrderTotel.multiply(siitteBuyFee);
- userPositionMapper.updateById(userPosition);
- userAssetsServices.availablebalanceChange(stock.getStockType(),
- userPosition.getUserId(), EUserAssets.CLOSE_POSITION_RETURN_SECURITY_DEPOSIT,
- userPosition.getOrderTotalPrice(), "", "");
- userAssetsServices.availablebalanceChange(stock.getStockType(),
- userPosition.getUserId(), EUserAssets.HANDLING_CHARGE,
- xsPrice, "", "");
+ BigDecimal sellOrderTotel = nowPrice.multiply(new BigDecimal(userPosition.getOrderNum()));
+ BigDecimal xsPrice = sellOrderTotel.multiply(siitteBuyFee);
+ userPositionMapper.updateById(userPosition);
+ userAssetsServices.availablebalanceChange(stock.getStockType(),
+ userPosition.getUserId(), EUserAssets.CLOSE_POSITION_RETURN_SECURITY_DEPOSIT,
+ userPosition.getOrderTotalPrice(), "", "");
+ userAssetsServices.availablebalanceChange(stock.getStockType(),
+ userPosition.getUserId(), EUserAssets.HANDLING_CHARGE,
+ xsPrice, "", "");
- PositionProfitVO profitVO = UserPointUtil.getPositionProfitVO(userPosition, priceServices.getNowPrice(userPosition.getStockCode()));
- userAssetsServices.availablebalanceChange(stock.getStockType(), userPosition.getUserId(), EUserAssets.CLOSE_POSITION,
- profitVO.getAllProfitAndLose(), "", "");
- return ServerResponse.createBySuccessMsg("平仓成功!");
+ PositionProfitVO profitVO = UserPointUtil.getPositionProfitVO(userPosition, priceServices.getNowPrice(userPosition.getStockCode()));
+ userAssetsServices.availablebalanceChange(stock.getStockType(), userPosition.getUserId(), EUserAssets.CLOSE_POSITION,
+ profitVO.getAllProfitAndLose(), "", "");
+ return ServerResponse.createBySuccessMsg("平仓成功!");
+ } catch (Exception e) {
+ return ServerResponse.createByErrorMsg(e.getMessage());
+ }
}
- @Transactional
+ @Transactional(rollbackFor = Exception.class)
public ServerResponse sell(String positionSn, int doType, Integer number,HttpServletRequest request) {
UserPosition userPosition = this.userPositionMapper.findPositionBySn(positionSn);
if (userPosition == null) {
@@ -354,10 +378,11 @@
if(null == stock){
return ServerResponse.createByErrorMsg("股票不存在,平仓失败", request);
}
- UserAssets userAssets = userAssetsMapper.selectOne(new LambdaQueryWrapper<UserAssets>()
+ /*UserAssets userAssets = userAssetsMapper.selectOne(new LambdaQueryWrapper<UserAssets>()
.eq(UserAssets::getUserId, userPosition.getUserId())
.eq(UserAssets::getAccectType, stock.getStockType())
- );
+ );*/
+ UserAssets userAssets = userAssetsServices.assetsByTypeAndUserId(stock.getStockType(), userPosition.getUserId());
if(userAssets.getAmountToBeCovered().compareTo(BigDecimal.ZERO) > 0){
return ServerResponse.createByErrorMsg("请先缴清待补资金", request);
}
@@ -430,45 +455,69 @@
}
private ServerResponse<Object> getObjectServerResponse(HttpServletRequest request, UserPosition userPosition, BigDecimal nowPrice, BigDecimal siitteBuyFee, Stock stock) {
- userPosition.setSellOrderId(GeneratePosition.getPositionId());
- userPosition.setSellOrderPrice(nowPrice);
- userPosition.setSellOrderTime(new Date());
+ try {
+ userPosition.setSellOrderId(GeneratePosition.getPositionId());
+ userPosition.setSellOrderPrice(nowPrice);
+ userPosition.setSellOrderTime(new Date());
- BigDecimal sellOrderTotel = nowPrice.multiply(new BigDecimal(userPosition.getOrderNum()));
- BigDecimal xsPrice = sellOrderTotel.multiply(siitteBuyFee);
- userPosition.setOrderFee(userPosition.getOrderFee().add(xsPrice));
- userPositionMapper.updateById(userPosition);
- userAssetsServices.availablebalanceChange(stock.getStockType(),
- userPosition.getUserId(),
- EUserAssets.CLOSE_POSITION_RETURN_SECURITY_DEPOSIT,
- userPosition.getOrderTotalPrice(), "", "");
- userAssetsServices.availablebalanceChange(stock.getStockType(),
- userPosition.getUserId(), EUserAssets.HANDLING_CHARGE,
- xsPrice, "", "");
+ BigDecimal sellOrderTotel = nowPrice.multiply(new BigDecimal(userPosition.getOrderNum()));
+ BigDecimal xsPrice = sellOrderTotel.multiply(siitteBuyFee);
+ userPosition.setOrderFee(userPosition.getOrderFee().add(xsPrice));
+ userPositionMapper.updateById(userPosition);
+ userAssetsServices.availablebalanceChange(stock.getStockType(),
+ userPosition.getUserId(),
+ EUserAssets.CLOSE_POSITION_RETURN_SECURITY_DEPOSIT,
+ userPosition.getOrderTotalPrice(), "", "");
+ userAssetsServices.availablebalanceChange(stock.getStockType(),
+ userPosition.getUserId(), EUserAssets.HANDLING_CHARGE,
+ xsPrice, "", "");
- PositionProfitVO profitVO = UserPointUtil.getPositionProfitVO(userPosition,
- priceServices.getNowPrice(userPosition.getStockCode()));
+ PositionProfitVO profitVO = UserPointUtil.getPositionProfitVO(userPosition,
+ priceServices.getNowPrice(userPosition.getStockCode()));
- userAssetsServices.availablebalanceChange(stock.getStockType(),
- userPosition.getUserId(), EUserAssets.CLOSE_POSITION,
- profitVO.getAllProfitAndLose(), "", "");
- return ServerResponse.createBySuccessMsg("平仓成功!", request);
+ userAssetsServices.availablebalanceChange(stock.getStockType(),
+ userPosition.getUserId(), EUserAssets.CLOSE_POSITION,
+ profitVO.getAllProfitAndLose(), "", "");
+ return ServerResponse.createBySuccessMsg("平仓成功!", request);
+ } catch (Exception e) {
+ return ServerResponse.createByErrorMsg(e.getMessage());
+ }
}
- @Transactional
+ @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);
+ }
}
//用户追加保证金操作
@@ -678,6 +727,71 @@
pageInfo.setList(userPositionVOS);
return ServerResponse.createBySuccess(pageInfo);
+ }
+
+ public ServerResponse findMyPositionByParam(String stockCode, String stockSpell,
+ Integer state, HttpServletRequest request,
+ int pageNum, int pageSize, String stockType, Integer positionType) {
+ try {
+ User user = iUserService.getCurrentUser(request);
+ if (user == null ){
+ return ServerResponse.createByErrorCodeMsg(ResponseCode.NEED_LOGIN.getCode(),"请先登录", request);
+ }
+
+ PageHelper.startPage(pageNum, pageSize);
+ List<UserPositionVO> userPositions = userPositionMapper.findMyPositionByParam(user.getId(),
+ stockCode, stockSpell, state, stockType, positionType);
+ PageInfo<UserPositionVO> pageInfo = new PageInfo<>(userPositions);
+
+ List<UserPositionVO> resultUserPositions = new ArrayList<>();
+ if (!pageInfo.getList().isEmpty()) {
+ for (UserPositionVO position : userPositions) {
+ UserPositionVO userPositionVO = position;
+ if (position.getPositionType() != 4) {
+ BigDecimal nowPrice;
+ if(state == 0){
+ nowPrice = priceServices.getNowPrice(position.getStockCode());
+ }else{
+ nowPrice = position.getSellOrderPrice();
+ }
+
+ userPositionVO = UserPointUtil.assembleUserPositionVO2(position,nowPrice);
+ userPositionVO.setOrderTotalPrice(userPositionVO.getOrderTotalPrice().multiply(new BigDecimal(userPositionVO.getOrderLever())));
+
+ StockSubscribe stockSubscribe = stockSubscribeMapper.selectOne(new LambdaQueryWrapper<StockSubscribe>()
+ .eq(StockSubscribe::getCode, userPositionVO.getStockCode()));
+ if(position.getSellOrderId() == null){
+ if (null != stockSubscribe && DateUtil.date().before(stockSubscribe.getListDate())) {
+ userPositionVO.setProfitAndLose(BigDecimal.ZERO);
+ userPositionVO.setProfitAndLoseParent("0%");
+ userPositionVO.setIsListed(false);
+ }else{
+ userPositionVO.setIsListed(true);
+ userPositionVO.setProfitAndLose(userPositionVO.getProfitAndLose().multiply(new BigDecimal(userPositionVO.getOrderLever())));
+ }
+ }else{
+ userPositionVO.setProfitAndLose(userPositionVO.getProfitAndLose().multiply(new BigDecimal(userPositionVO.getOrderLever())));
+ }
+ } else {
+ //ai交易 计算收益率 收益/本金*100
+ BigDecimal profitAndLose = userPositionVO.getProfitAndLose()
+ .divide(userPositionVO.getOrderTotalPrice(), RoundingMode.HALF_UP)
+ .multiply(new BigDecimal(100))
+ .setScale(2, RoundingMode.DOWN);
+ userPositionVO.setNow_price(String.valueOf(userPositionVO.getSellOrderPrice().setScale(2, RoundingMode.HALF_UP)));
+ userPositionVO.setProfitAndLoseParent(profitAndLose + "%");
+ userPositionVO.setSellOrderTotalPrice(userPositionVO.getSellOrderPrice().multiply(BigDecimal.valueOf(userPositionVO.getOrderNum())));
+ }
+ resultUserPositions.add(userPositionVO);
+ }
+ }
+ pageInfo.setList(resultUserPositions);
+ return ServerResponse.createBySuccess(pageInfo);
+ } catch (Exception e) {
+ e.printStackTrace();
+ log.error("IUserPositionService findMyPositionByParam {}", e.getMessage());
+ }
+ return ServerResponse.createByError();
}
public PositionVO findUserPositionAllProfitAndLose(Integer userId) {
@@ -948,7 +1062,7 @@
userPosition.setAgentId(user.getAgentId());
userPosition.setStockCode(stock.getStockCode());
userPosition.setStockName(stock.getStockName());
- userPosition.setStockGid(stock.getStockGid());
+ userPosition.setStockGid(stock.getStockType());
userPosition.setStockSpell(stock.getStockSpell());
userPosition.setBuyOrderId(GeneratePosition.getPositionId());
userPosition.setBuyOrderTime(DateTimeUtil.strToDate(buyTime));
@@ -1128,7 +1242,7 @@
}
}
- @Transactional
+ @Transactional(rollbackFor = Exception.class)
public ServerResponse closingStayTask(UserPosition position, Integer stayDays) throws Exception {
log.info("=================closingStayTask====================");
log.info("修改留仓费,持仓id={},持仓天数={}", position.getId(), stayDays);
@@ -1305,114 +1419,119 @@
* @Date: 2022/10/26
*/
@Override
+ @Transactional(rollbackFor = Exception.class)
public ServerResponse newStockToPosition(Integer id,BigDecimal amountToBeCovered) {
- UserStockSubscribe userStockSubscribe = userStockSubscribeMapper.load(id);
- if (userStockSubscribe == null) {
- return ServerResponse.createByErrorMsg("无该申购记录");
- }
- StockSubscribe stockSubscribe = stockSubscribeMapper.selectOne(new QueryWrapper<StockSubscribe>().eq("newlist_id", userStockSubscribe.getNewStockId()));
- if (stockSubscribe == null) {
- return ServerResponse.createByErrorMsg("该新股不存在");
- }
+ try {
+ UserStockSubscribe userStockSubscribe = userStockSubscribeMapper.load(id);
+ if (userStockSubscribe == null) {
+ return ServerResponse.createByErrorMsg("无该申购记录");
+ }
+ StockSubscribe stockSubscribe = stockSubscribeMapper.selectOne(new QueryWrapper<StockSubscribe>().eq("newlist_id", userStockSubscribe.getNewStockId()));
+ if (stockSubscribe == null) {
+ return ServerResponse.createByErrorMsg("该新股不存在");
+ }
- Stock stock = stockMapper.selectOne(new LambdaQueryWrapper<Stock>().eq(Stock::getStockCode, userStockSubscribe.getNewCode()));
- String stockType;
- UserPosition userPosition = new UserPosition();
+ Stock stock = stockMapper.selectOne(new LambdaQueryWrapper<Stock>().eq(Stock::getStockCode, userStockSubscribe.getNewCode()));
+ String stockType;
+ UserPosition userPosition = new UserPosition();
- if(null == stock){
- userPosition.setStockCode(stockSubscribe.getCode());
- userPosition.setStockSpell(stockSubscribe.getName());
- stockType = stockSubscribe.getStockType();
- }else{
- userPosition.setStockCode(stock.getStockCode());
- userPosition.setStockSpell(stock.getStockSpell());
- stockType = stock.getStockType();
- }
+ if(null == stock){
+ userPosition.setStockCode(stockSubscribe.getCode());
+ userPosition.setStockSpell(stockSubscribe.getName());
+ stockType = stockSubscribe.getStockType();
+ }else{
+ userPosition.setStockCode(stock.getStockCode());
+ userPosition.setStockSpell(stock.getStockSpell());
+ stockType = stock.getStockType();
+ }
- userPosition.setPositionType(1);
- userPosition.setPositionSn(KeyUtils.getUniqueKey());
- userPosition.setUserId(userStockSubscribe.getUserId());
- userPosition.setNickName(userStockSubscribe.getRealName());
- userPosition.setAgentId(userStockSubscribe.getAgentId());
+ userPosition.setPositionType(2);
+ userPosition.setPositionSn(KeyUtils.getUniqueKey());
+ userPosition.setUserId(userStockSubscribe.getUserId());
+ userPosition.setNickName(userStockSubscribe.getRealName());
+ userPosition.setAgentId(userStockSubscribe.getAgentId());
- userPosition.setStockName(userStockSubscribe.getNewName());
+ userPosition.setStockName(userStockSubscribe.getNewName());
// StringBuffer gid = new StringBuffer();
// gid.append(stockSubscribe.getStockType()!=null?stockSubscribe.getStockType():"");
// gid.append(userStockSubscribe.getNewCode()!=null?userStockSubscribe.getNewCode():"stock code invaild");
- userPosition.setStockGid(stockType);
- userPosition.setBuyOrderId(GeneratePosition.getPositionId());
- userPosition.setBuyOrderTime(new Date());
- userPosition.setBuyOrderPrice(userStockSubscribe.getBuyPrice());
- userPosition.setOrderDirection("买涨");
+ userPosition.setStockGid(stockType);
+ userPosition.setBuyOrderId(GeneratePosition.getPositionId());
+ userPosition.setBuyOrderTime(new Date());
+ userPosition.setBuyOrderPrice(userStockSubscribe.getBuyPrice());
+ userPosition.setOrderDirection("买涨");
- userPosition.setOrderNum(userStockSubscribe.getApplyNumber());
+ userPosition.setOrderNum(userStockSubscribe.getApplyNumber());
- userPosition.setIsLock(Integer.valueOf(0));
+ userPosition.setIsLock(Integer.valueOf(0));
- userPosition.setOrderLever(1);
+ userPosition.setOrderLever(1);
- //递延费特殊处理
- // BigDecimal stayFee = userPosition.getOrderTotalPrice().multiply(siteSetting.getStayFee());
- BigDecimal stayFee = new BigDecimal(0);
- BigDecimal allStayFee = stayFee.multiply(new BigDecimal(1));
- userPosition.setOrderStayFee(allStayFee);
- userPosition.setOrderStayDays(1);
- userPosition.setOrderTotalPrice(userStockSubscribe.getBond());
+ //递延费特殊处理
+ // BigDecimal stayFee = userPosition.getOrderTotalPrice().multiply(siteSetting.getStayFee());
+ BigDecimal stayFee = new BigDecimal(0);
+ BigDecimal allStayFee = stayFee.multiply(new BigDecimal(1));
+ userPosition.setOrderStayFee(allStayFee);
+ userPosition.setOrderStayDays(1);
+ userPosition.setOrderTotalPrice(userStockSubscribe.getBond());
- // BigDecimal buy_fee_amt = buy_amt.multiply(siteSetting.getBuyFee()).setScale(2, 4);
- // 手续费率
- BigDecimal siteSettingBuyFee = new BigDecimal(iStockConfigServices.queryByKey(EConfigKey.BUY_HANDLING_CHARGE.getCode()).getCValue()) ;
- BigDecimal buy_fee_amt = siteSettingBuyFee.multiply(userStockSubscribe.getBond());
- log.info("用户购买手续费(配资后总资金 * 百分比) = {}", buy_fee_amt);
- userPosition.setOrderFee(buy_fee_amt);
+ // BigDecimal buy_fee_amt = buy_amt.multiply(siteSetting.getBuyFee()).setScale(2, 4);
+ // 手续费率
+ BigDecimal siteSettingBuyFee = new BigDecimal(iStockConfigServices.queryByKey(EConfigKey.BUY_HANDLING_CHARGE.getCode()).getCValue()) ;
+ BigDecimal buy_fee_amt = siteSettingBuyFee.multiply(userStockSubscribe.getBond());
+ log.info("用户购买手续费(配资后总资金 * 百分比) = {}", buy_fee_amt);
+ userPosition.setOrderFee(buy_fee_amt);
- // BigDecimal buy_yhs_amt = buy_amt.multiply(siteSetting.getDutyFee()).setScale(2, 4);
- BigDecimal buy_yhs_amt = new BigDecimal(0);
- log.info("用户购买印花税(配资后总资金 * 百分比) = {}", buy_yhs_amt);
- userPosition.setOrderSpread(buy_yhs_amt);
+ // BigDecimal buy_yhs_amt = buy_amt.multiply(siteSetting.getDutyFee()).setScale(2, 4);
+ BigDecimal buy_yhs_amt = new BigDecimal(0);
+ log.info("用户购买印花税(配资后总资金 * 百分比) = {}", buy_yhs_amt);
+ userPosition.setOrderSpread(buy_yhs_amt);
- BigDecimal spread_rate_amt = new BigDecimal(0);
- userPosition.setSpreadRatePrice(spread_rate_amt);
+ BigDecimal spread_rate_amt = new BigDecimal(0);
+ userPosition.setSpreadRatePrice(spread_rate_amt);
- BigDecimal profit_and_lose = new BigDecimal("0");
- userPosition.setProfitAndLose(profit_and_lose);
+ BigDecimal profit_and_lose = new BigDecimal("0");
+ userPosition.setProfitAndLose(profit_and_lose);
- BigDecimal all_profit_and_lose = profit_and_lose.subtract(buy_fee_amt).subtract(buy_yhs_amt).subtract(spread_rate_amt);
- userPosition.setAllProfitAndLose(all_profit_and_lose);
+ BigDecimal all_profit_and_lose = profit_and_lose.subtract(buy_fee_amt).subtract(buy_yhs_amt).subtract(spread_rate_amt);
+ userPosition.setAllProfitAndLose(all_profit_and_lose);
- userPosition.setOrderStayDays(Integer.valueOf(0));
- userPosition.setOrderStayFee(new BigDecimal("0"));
- userPosition.setAmountToBeCovered(amountToBeCovered);
- userPosition.setNewId(stockSubscribe.getNewlistId());
- int ret = 0;
- ret = this.userPositionMapper.insert(userPosition);
- UserAssets userAssets = iUserAssetsServices.assetsByTypeAndUserId(stockType, userPosition.getUserId());
- if(null == userAssets){
- return ServerResponse.createByErrorMsg("新股转持仓失败");
- }
- userAssetsMapper.updateById(userAssets);
- iUserAssetsServices.availablebalanceChange(stockType, userAssets.getUserId(), EUserAssets.HANDLING_CHARGE, buy_fee_amt, "", "");
- if (ret > 0) {
- userStockSubscribe.setStatus(5);
- userStockSubscribeMapper.update1(userStockSubscribe);
- if (userStockSubscribe.getType() == 1 || userStockSubscribe.getType() == 2) {
- User user = userMapper.selectById(userStockSubscribe.getUserId());
- ret = userMapper.updateById(user);
+ userPosition.setOrderStayDays(Integer.valueOf(0));
+ userPosition.setOrderStayFee(new BigDecimal("0"));
+ //userPosition.setAmountToBeCovered(amountToBeCovered);
+ userPosition.setNewId(stockSubscribe.getNewlistId());
+ int ret = 0;
+ ret = this.userPositionMapper.insert(userPosition);
+ UserAssets userAssets = iUserAssetsServices.assetsByTypeAndUserId(stockType, userPosition.getUserId());
+ if(null == userAssets){
+ return ServerResponse.createByErrorMsg("新股转持仓失败");
}
+ userAssetsMapper.updateById(userAssets);
+ iUserAssetsServices.availablebalanceChange(stockType, userAssets.getUserId(), EUserAssets.HANDLING_CHARGE, buy_fee_amt, "", "");
if (ret > 0) {
- return ServerResponse.createBySuccessMsg("新股转持仓成功");
+ userStockSubscribe.setStatus(5);
+ userStockSubscribeMapper.update1(userStockSubscribe);
+ if (userStockSubscribe.getType() == 1 || userStockSubscribe.getType() == 2) {
+ User user = userMapper.selectById(userStockSubscribe.getUserId());
+ ret = userMapper.updateById(user);
+ }
+ if (ret > 0) {
+ return ServerResponse.createBySuccessMsg("新股转持仓成功");
+ } else {
+ return ServerResponse.createByErrorMsg("新股转持仓失败");
+ }
} else {
return ServerResponse.createByErrorMsg("新股转持仓失败");
}
- } else {
- return ServerResponse.createByErrorMsg("新股转持仓失败");
+ } catch (Exception e) {
+ return ServerResponse.createByErrorMsg(e.getMessage());
}
}
@@ -1427,7 +1546,7 @@
* @param request
* @return
*/
- @Transactional
+ @Transactional(rollbackFor = Exception.class)
public ServerResponse buyVipQc(String stockCode, Integer buyNum, Integer buyType, Integer lever, BigDecimal profitTarget, BigDecimal stopTarget, HttpServletRequest request) throws Exception {
/*实名认证开关开启*/
@@ -1584,7 +1703,7 @@
userPosition.setAgentId(user.getAgentId());
userPosition.setStockCode(stock.getStockCode());
userPosition.setStockName(stock.getStockName());
- userPosition.setStockGid(stock.getStockGid());
+ userPosition.setStockGid(stock.getStockType());
userPosition.setStockSpell(stock.getStockSpell());
userPosition.setBuyOrderId(GeneratePosition.getPositionId());
userPosition.setBuyOrderTime(new Date());
@@ -1680,72 +1799,87 @@
* @param request
* @return
*/
- @Transactional
- public ServerResponse buyDz(Integer dzId, String password, Integer num, HttpServletRequest request) throws Exception {
- /*实名认证开关开启*/
- SiteProduct siteProduct = iSiteProductService.getProductSetting();
- User user = this.iUserService.getCurrentRefreshUser(request);
+ @Transactional(rollbackFor = Exception.class)
+ 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);
+ }
- //判断审核开关
- if(stockDz.getSwitchType() == 1) {
+ 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) {
+ 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(buyAmt);
- userAssetsServices.availablebalanceChange(stock.getStockType(), user.getId(), EUserAssets.BUY, buyAmt.negate(),"","");
- iUserAssetsServices.availablebalanceChange(stock.getStockType(), 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) {
@@ -1794,7 +1928,7 @@
}
@Override
- @Transactional
+ @Transactional(rollbackFor = Exception.class)
public void stockConstraint(List<UserPosition> list) {
try {
//SiteSetting siteSetting = iSiteSettingService.getSiteSetting();
@@ -1827,6 +1961,34 @@
}catch (Exception e){
log.error("强制平仓--->错误",e);
}
+ }
+
+ /**
+ * 获取大宗交易订单
+ * @param pageNum
+ * @param pageSize
+ * @param status
+ * @param request
+ * @return
+ */
+ @Override
+ public ServerResponse getDzOrderList(int pageNum, int pageSize, Integer status, HttpServletRequest request) {
+ try {
+ User user = iUserService.getCurrentUser(request);
+ if (user == null ){
+ return ServerResponse.createByErrorCodeMsg(ResponseCode.NEED_LOGIN.getCode(),"请先登录");
+ }
+ PageHelper.startPage(pageNum, pageSize);
+ List<UserPositionCheckDz> userPositionCheckDzs = userPositionCheckDzService.list(
+ new QueryWrapper<UserPositionCheckDz>()
+ .eq("user_id", user.getId()).eq(status != null,"check_type", status));
+ // 获取分页信息
+ PageInfo<UserPositionCheckDz> pageInfo = new PageInfo<>(userPositionCheckDzs);
+ return ServerResponse.createBySuccess(pageInfo);
+ } catch (Exception e) {
+ log.error("IUserPositionService getDzOrderList error", e);
+ }
+ return ServerResponse.createByError();
}
private Result getResult(UserPosition position) {
@@ -1945,32 +2107,168 @@
//平仓
private void extracted(UserPosition position, BigDecimal nowPrice, Stock stock,Integer liquidation) {
- //-1强平 0未触发 1止损强平 2止盈强平
- BigDecimal siitteBuyFee = new BigDecimal(iStockConfigServices.queryByKey(EConfigKey.SELL_HANDLING_CHARGE.getCode()).getCValue()) ;
- BigDecimal sellOrderTotel = nowPrice.multiply(new BigDecimal(position.getOrderNum()));
- BigDecimal xsPrice = sellOrderTotel.multiply(siitteBuyFee);//手续费
- // 更新订单信息
- position.setSellOrderId(GeneratePosition.getPositionId());
- position.setSellOrderPrice(nowPrice);
- position.setSellOrderTime(new Date());
- userPositionMapper.updateById(position);
- if(liquidation == -1){
- userAssetsServices.availablebalanceChange(stock.getStockType(),
- position.getUserId(),
- EUserAssets.CONSTRAINT_CLOSE_POSITION,
- position.getOrderTotalPrice(), "", "");
- }else if(liquidation == 1 || liquidation == 2){
- userAssetsServices.availablebalanceChange(stock.getStockType(),
- position.getUserId(), EUserAssets.HANDLING_CHARGE,
- xsPrice, "", "");
- PositionProfitVO profitVO = UserPointUtil.getPositionProfitVO(position, priceServices.getNowPrice(position.getStockCode()));
- userAssetsServices.availablebalanceChange(stock.getStockType(), position.getUserId(), EUserAssets.CLOSE_POSITION,
- profitVO.getAllProfitAndLose(), "", "");
+ try {
+ //-1强平 0未触发 1止损强平 2止盈强平
+ BigDecimal siitteBuyFee = new BigDecimal(iStockConfigServices.queryByKey(EConfigKey.SELL_HANDLING_CHARGE.getCode()).getCValue()) ;
+ BigDecimal sellOrderTotel = nowPrice.multiply(new BigDecimal(position.getOrderNum()));
+ BigDecimal xsPrice = sellOrderTotel.multiply(siitteBuyFee);//手续费
+ // 更新订单信息
+ position.setSellOrderId(GeneratePosition.getPositionId());
+ position.setSellOrderPrice(nowPrice);
+ position.setSellOrderTime(new Date());
+ userPositionMapper.updateById(position);
+ if(liquidation == -1){
+ userAssetsServices.availablebalanceChange(stock.getStockType(),
+ position.getUserId(),
+ EUserAssets.CONSTRAINT_CLOSE_POSITION,
+ position.getOrderTotalPrice(), "", "");
+ }else if(liquidation == 1 || liquidation == 2){
+ userAssetsServices.availablebalanceChange(stock.getStockType(),
+ position.getUserId(), EUserAssets.HANDLING_CHARGE,
+ xsPrice, "", "");
+ PositionProfitVO profitVO = UserPointUtil.getPositionProfitVO(position, priceServices.getNowPrice(position.getStockCode()));
+ userAssetsServices.availablebalanceChange(stock.getStockType(), position.getUserId(), EUserAssets.CLOSE_POSITION,
+ profitVO.getAllProfitAndLose(), "", "");
+ }
+ log.info("强制平仓成功,订单id: {}", position.getId());
+ } catch (Exception e) {
+ log.info("extracted Exception {}", e.getMessage());
}
+ }
- log.info("强制平仓成功,订单id: {}", position.getId());
+ /**
+ * 查询账户市值和持仓收益
+ * @param request
+ * @return
+ */
+ public ServerResponse getMyPositionProfitAndLose(HttpServletRequest request) {
+ try {
+ User user = iUserService.getCurrentUser(request);
+ if (user == null ){
+ return ServerResponse.createByErrorCodeMsg(ResponseCode.NEED_LOGIN.getCode(),"请先登录");
+ }
+ Integer state = 0;
+ //status 0 持仓 1 平仓
+ List<UserPosition> userPositions = userPositionMapper.findMyPositionByCodeAndSpell(user.getId(),
+ null, null, state, null);
+
+ BigDecimal usMarketValue = BigDecimal.ZERO; //美股市值
+ BigDecimal jpMarketValue = BigDecimal.ZERO; //日股市值
+
+ BigDecimal usPositionEarnings = BigDecimal.ZERO; //美股持仓收益
+ BigDecimal jpPositionEarnings = BigDecimal.ZERO; //日股持仓收益
+
+ BigDecimal usPositionEarningsParent = BigDecimal.ZERO; //美股持仓收益百分比
+ BigDecimal jpPositionEarningsParent = BigDecimal.ZERO; //日股持仓收益百分比
+
+ if (!userPositions.isEmpty()) {
+ for (UserPosition position : userPositions) {
+ BigDecimal nowPrice = priceServices.getNowPrice(position.getStockCode());
+
+ UserPositionVO userPositionVO = UserPointUtil.assembleUserPositionVO(position,nowPrice);
+ userPositionVO.setOrderTotalPrice(userPositionVO.getOrderTotalPrice().multiply(new BigDecimal(userPositionVO.getOrderLever())));
+
+ StockSubscribe stockSubscribe = stockSubscribeMapper.selectOne(new LambdaQueryWrapper<StockSubscribe>()
+ .eq(StockSubscribe::getCode, userPositionVO.getStockCode()));
+ if(position.getSellOrderId() == null){
+ if (null != stockSubscribe && DateUtil.date().before(stockSubscribe.getListDate())) {
+ userPositionVO.setProfitAndLose(BigDecimal.ZERO);
+ userPositionVO.setProfitAndLoseParent("0%");
+ userPositionVO.setProfitAndLoseParent2(BigDecimal.ZERO);
+ userPositionVO.setIsListed(false);
+ }else{
+ userPositionVO.setIsListed(true);
+ userPositionVO.setProfitAndLose(userPositionVO.getProfitAndLose().multiply(new BigDecimal(userPositionVO.getOrderLever())));
+ }
+ }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(nowPriceSUM);
+ usPositionEarnings = usPositionEarnings.add(userPositionVO.getProfitAndLose());
+ usPositionEarningsParent = usPositionEarningsParent.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);
+ jpPositionEarnings = jpPositionEarnings.setScale(2, RoundingMode.DOWN);
+ }
+
+ Map<String, String> map = new HashMap<>();
+ map.put("usMarketValue", String.valueOf(usMarketValue)); //美股市值
+ map.put("usPositionEarnings", String.valueOf(usPositionEarnings)); //美股持仓收益
+ map.put("usPositionEarningsParent", usPositionEarningsParent + "%");//美股持仓收益率
+
+ 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);
+ }
+ return ServerResponse.createByError();
+ }
+
+ @Override
+ public ServerResponse checkDz(Integer id, Integer checkType, Integer orderNum, HttpServletRequest request) {
+ try {
+ UserPositionCheckDz userPositionCheckDz = userPositionCheckDzService.getById(id);
+ if (userPositionCheckDz == null) {
+ return ServerResponse.createByErrorMsg("订单不存在", request);
+ }
+ if (userPositionCheckDz.getCheckType() != 0) {
+ return ServerResponse.createByErrorMsg("订单已审核", request);
+ }
+ if (checkType == 2) {
+ userPositionCheckDz.setCheckType(checkType);
+ userPositionCheckDzService.updateById(userPositionCheckDz);
+ return ServerResponse.createBySuccess("审核成功", request);
+ }
+ if (orderNum > userPositionCheckDz.getOrderNum()) {
+ return ServerResponse.createByErrorMsg("输入数量大于用户买入数量", request);
+ }
+ User user = userMapper.selectById(userPositionCheckDz.getUserId());
+
+ StockDz stockDz = this.stockDzMapper.selectOne(new QueryWrapper<StockDz>().eq("id", userPositionCheckDz.getDzId()));
+
+ UserAssets userAssets = userAssetsServices.assetsByTypeAndUserId(stockDz.getStockType(), user.getId());
+ if (userAssets.getAmountToBeCovered().compareTo(BigDecimal.ZERO) > 0) {
+ return ServerResponse.createByErrorMsg("用户账户有待补资金未补齐,审核失败", request);
+ }
+
+ BigDecimal nowPrice = stockDz.getNowPrice();
+
+ if (nowPrice.compareTo(new BigDecimal("0")) == 0) {
+ return ServerResponse.createByErrorMsg("股票价格0,请重试", request);
+ }
+ userPositionCheckDz.setOrderNum(orderNum);
+ userPositionCheckDz.setOrderTotalPrice(nowPrice.multiply(new BigDecimal(orderNum.intValue())));
+ BigDecimal buyAmt = nowPrice.multiply(new BigDecimal(userPositionCheckDz.getOrderNum()));
+ BigDecimal fundratio = new BigDecimal(user.getFundRatio()).divide(new BigDecimal(100));
+ BigDecimal availableBalance = fundratio.multiply(userAssets.getAvailableBalance());
+ if (buyAmt.compareTo(availableBalance) > 0) {
+ return ServerResponse.createByErrorMsg("订单失败,配资不足", request);
+ }
+ userPositionCheckDz.setCheckType(checkType);
+ userPositionCheckDzService.updateById(userPositionCheckDz);
+ UserPosition userPosition = Convert.convert(UserPosition.class, userPositionCheckDz);
+ userPosition.setBuyOrderPrice(nowPrice);
+ userPosition.setId(null);
+ userPosition.setDzId(stockDz.getId());
+ userPositionMapper.insert(userPosition);
+ userAssetsServices.availablebalanceChange(stockDz.getStockType(), user.getId(), EUserAssets.BUY, buyAmt.negate(), "", "");
+ return ServerResponse.createBySuccessMsg("审核成功,订单已转客户持仓", request);
+ } catch (Exception e) {
+ return ServerResponse.createByErrorMsg(e.getMessage());
+ }
}
}
--
Gitblit v1.9.3