| | |
| | | @Autowired |
| | | StockMapper stockMapper; |
| | | @Autowired |
| | | AgentAgencyFeeMapper agentAgencyFeeMapper; |
| | | @Autowired |
| | | IAgentAgencyFeeService iAgentAgencyFeeService; |
| | | @Autowired |
| | | ISiteProductService iSiteProductService; |
| | | |
| | | @Autowired |
| | | FundsApplyMapper fundsApplyMapper; |
| | | @Autowired |
| | | UserStockSubscribeMapper userStockSubscribeMapper; |
| | | @Resource |
| | |
| | | @Resource |
| | | UserIndexPositionMapper userIndexPositionMapper; |
| | | |
| | | @Autowired |
| | | IStockFuturesService iStockFuturesService; |
| | | @Autowired |
| | | IStockCoinService iStockCoinService; |
| | | @Autowired |
| | | CurrencyUtils currencyUtils; |
| | | @Resource |
| | | StockDzMapper stockDzMapper; |
| | | |
| | |
| | | } |
| | | |
| | | |
| | | 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); |
| | | } |
| | | |
| | | 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); |
| | |
| | | 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); |
| | |
| | | } |
| | | userPosition.setIsLock(Integer.valueOf(0)); |
| | | userPosition.setOrderLever(lever); |
| | | userPosition.setOrderTotalPrice(finalBuyAmt); |
| | | userPosition.setOrderTotalPrice(buyAmt); |
| | | // 手续费 |
| | | |
| | | userPosition.setOrderFee(orderFree); |
| | |
| | | |
| | | 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); |
| | |
| | | * @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) { |
| | |
| | | null, null, state, null); |
| | | |
| | | BigDecimal usMarketValue = BigDecimal.ZERO; //美股市值 |
| | | BigDecimal mxMarketValue = BigDecimal.ZERO; //墨西哥股市值 |
| | | BigDecimal hkMarketValue = BigDecimal.ZERO; //港股市值 |
| | | BigDecimal inMarketValue = BigDecimal.ZERO; //印股市值 |
| | | BigDecimal twMarketValue = BigDecimal.ZERO; //台股市值 |
| | | |
| | | BigDecimal usPositionEarnings = BigDecimal.ZERO; //美股持仓收益 |
| | | BigDecimal mxPositionEarnings = BigDecimal.ZERO; //墨西哥持仓收益 |
| | | BigDecimal hkPositionEarnings = BigDecimal.ZERO; //港股持仓收益 |
| | | BigDecimal inPositionEarnings = BigDecimal.ZERO; //印股持仓收益 |
| | | BigDecimal twPositionEarnings = BigDecimal.ZERO; //台股持仓收益 |
| | | |
| | | BigDecimal usPositionEarningsParent = BigDecimal.ZERO; //美股持仓收益百分比 |
| | | BigDecimal mxPositionEarningsParent = BigDecimal.ZERO; //墨西哥持仓收益百分比 |
| | | BigDecimal hkPositionEarningsParent = BigDecimal.ZERO; //港股持仓收益百分比 |
| | | BigDecimal inPositionEarningsParent = BigDecimal.ZERO; //印股持仓收益百分比 |
| | | BigDecimal twPositionEarningsParent = BigDecimal.ZERO; //台股持仓收益百分比 |
| | | |
| | | if (!userPositions.isEmpty()) { |
| | | for (UserPosition position : userPositions) { |
| | |
| | | }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.HK.getCode())) { |
| | | hkMarketValue = hkMarketValue.add(nowPriceSUM); |
| | | hkPositionEarnings = hkPositionEarnings.add(userPositionVO.getProfitAndLose()); |
| | | hkPositionEarningsParent = hkPositionEarningsParent.add(userPositionVO.getProfitAndLoseParent2()); |
| | | } else if(position.getStockGid().equals(EStockType.IN.getCode())) { |
| | | inMarketValue = inMarketValue.add(nowPriceSUM); |
| | | inPositionEarnings = inPositionEarnings.add(userPositionVO.getProfitAndLose()); |
| | | inPositionEarningsParent = inPositionEarningsParent.add(userPositionVO.getProfitAndLoseParent2()); |
| | | } else if(position.getStockGid().equals(EStockType.TW.getCode())) { |
| | | twMarketValue = twMarketValue.add(nowPriceSUM); |
| | | twPositionEarnings = twPositionEarnings.add(userPositionVO.getProfitAndLose()); |
| | | twPositionEarningsParent = twPositionEarningsParent.add(userPositionVO.getProfitAndLoseParent2()); |
| | | } |
| | | |
| | | } |
| | | usPositionEarnings = usPositionEarnings.setScale(2, RoundingMode.DOWN); |
| | | mxPositionEarnings = mxPositionEarnings.setScale(2, RoundingMode.DOWN); |
| | | hkPositionEarnings = hkPositionEarnings.setScale(2, RoundingMode.DOWN); |
| | | inPositionEarnings = inPositionEarnings.setScale(2, RoundingMode.DOWN); |
| | | twPositionEarnings = twPositionEarnings.setScale(2, RoundingMode.DOWN); |
| | | } |
| | | |
| | | Map<String, String> map = new HashMap<>(); |
| | |
| | | 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("hkMarketValue", String.valueOf(hkMarketValue)); //港股市值 |
| | | map.put("hkPositionEarnings", String.valueOf(hkPositionEarnings)); //港股持仓收益 |
| | | map.put("hkPositionEarningsParent", hkPositionEarningsParent + "%");//港股持仓收益率 |
| | | |
| | | map.put("inMarketValue", String.valueOf(inMarketValue)); //印股市值 |
| | | map.put("inPositionEarnings", String.valueOf(inPositionEarnings)); //印股持仓收益 |
| | | map.put("inPositionEarningsParent", inPositionEarningsParent + "%");//印股持仓收益率 |
| | | |
| | | map.put("twMarketValue", String.valueOf(twMarketValue)); //台股市值 |
| | | map.put("twPositionEarnings", String.valueOf(twPositionEarnings)); //台股持仓收益 |
| | | map.put("twPositionEarningsParent", twPositionEarningsParent + "%");//台股持仓收益率 |
| | | |
| | | return ServerResponse.createBySuccess(map); |
| | | } catch (Exception e) { |
| | | log.error("IUserPositionService getMyPositionProfitAndLose error", e); |