From 59baf780dc74b95b65c453a9cb6bd8e1d9f3a2b1 Mon Sep 17 00:00:00 2001 From: xiaoyong931011 <15274802129@163.com> Date: Wed, 03 Jun 2020 15:09:46 +0800 Subject: [PATCH] Merge branch 'master' of https://gitee.com/chonggaoxiao/new_excoin.git --- src/main/java/com/xcong/excoin/modules/contract/service/impl/ContractHoldOrderServiceImpl.java | 83 +++++++++++++++++++++++++++++++++++++++-- 1 files changed, 79 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/xcong/excoin/modules/contract/service/impl/ContractHoldOrderServiceImpl.java b/src/main/java/com/xcong/excoin/modules/contract/service/impl/ContractHoldOrderServiceImpl.java index 10e2f85..2c5e3f0 100644 --- a/src/main/java/com/xcong/excoin/modules/contract/service/impl/ContractHoldOrderServiceImpl.java +++ b/src/main/java/com/xcong/excoin/modules/contract/service/impl/ContractHoldOrderServiceImpl.java @@ -9,6 +9,7 @@ import com.xcong.excoin.common.enumerates.RabbitPriceTypeEnum; import com.xcong.excoin.common.response.Result; import com.xcong.excoin.common.system.service.CommonService; +import com.xcong.excoin.modules.contract.dao.ContractEntrustOrderDao; import com.xcong.excoin.modules.contract.dao.ContractHoldOrderDao; import com.xcong.excoin.modules.contract.dao.ContractOrderDao; import com.xcong.excoin.modules.contract.entity.ContractEntrustOrderEntity; @@ -18,6 +19,7 @@ import com.xcong.excoin.modules.contract.parameter.dto.ChangeBondDto; import com.xcong.excoin.modules.contract.parameter.dto.ProfitOrLessDto; import com.xcong.excoin.modules.contract.parameter.dto.SubmitOrderDto; +import com.xcong.excoin.modules.contract.parameter.vo.ContractMoneyInfoVo; import com.xcong.excoin.modules.contract.parameter.vo.HoldOrderListVo; import com.xcong.excoin.modules.contract.service.ContractHoldOrderService; import com.xcong.excoin.modules.member.dao.MemberWalletContractDao; @@ -56,6 +58,9 @@ @Resource private ContractOrderDao contractOrderDao; + + @Resource + private ContractEntrustOrderDao contractEntrustOrderDao; @Resource private CommonService commonService; @@ -367,10 +372,10 @@ } else { // 开空止盈 if (ProfitOrLessDto.TYPE_PROFIT == profitOrLessDto.getType()) { - model = new OrderModel(holdOrderEntity.getId(), RabbitPriceTypeEnum.CLOSE_LESS_STOP_PROFIT.getValue(), price.toPlainString(), holdOrderEntity.getSymbol()); + model = new OrderModel(holdOrderEntity.getId(), RabbitPriceTypeEnum.CLOSE_LESS_STOP_PROFIT.getValue(), price.setScale(8, RoundingMode.HALF_UP).toPlainString(), holdOrderEntity.getSymbol()); // 开空止损 } else { - model = new OrderModel(holdOrderEntity.getId(), RabbitPriceTypeEnum.CLOSE_LESS_STOP_LESS.getValue(), price.toPlainString(), holdOrderEntity.getSymbol()); + model = new OrderModel(holdOrderEntity.getId(), RabbitPriceTypeEnum.CLOSE_LESS_STOP_LESS.getValue(), price.setScale(8, RoundingMode.HALF_UP).toPlainString(), holdOrderEntity.getSymbol()); } } producer.sendPriceOperate(JSONObject.toJSONString(model)); @@ -419,10 +424,10 @@ OrderModel model = null; // 开多 if (ContractHoldOrderEntity.OPENING_TYPE_MORE == holdOrderEntity.getOpeningType()) { - model = new OrderModel(holdOrderEntity.getId(), RabbitPriceTypeEnum.CLOSE_MORE_BOMB.getValue(), forceClosingPrice.toPlainString(), holdOrderEntity.getSymbol()); + model = new OrderModel(holdOrderEntity.getId(), RabbitPriceTypeEnum.CLOSE_MORE_BOMB.getValue(), forceClosingPrice.setScale(8, RoundingMode.HALF_UP).toPlainString(), holdOrderEntity.getSymbol()); // 开空 } else { - model = new OrderModel(holdOrderEntity.getId(), RabbitPriceTypeEnum.CLOSE_LESS_BOMB.getValue(), forceClosingPrice.toPlainString(), holdOrderEntity.getSymbol()); + model = new OrderModel(holdOrderEntity.getId(), RabbitPriceTypeEnum.CLOSE_LESS_BOMB.getValue(), forceClosingPrice.setScale(8, RoundingMode.HALF_UP).toPlainString(), holdOrderEntity.getSymbol()); } producer.sendPriceOperate(JSONObject.toJSONString(model)); @@ -431,4 +436,74 @@ } return Result.fail("调整失败"); } + + @Override + public Result findContractMoneyInfo(String symbol) { + MemberEntity memberEntity = LoginUserUtils.getAppLoginUser(); + + PlatformTradeSettingEntity tradeSetting = cacheSettingUtils.getTradeSetting(); + // 获取最新价 + BigDecimal newPrice = new BigDecimal(redisUtils.getString(CoinTypeConvert.convertToKey(symbol))); + + // 当前合约委托单 + List<ContractEntrustOrderEntity> entrustOrderEntities = contractEntrustOrderDao.selectEntrustOrderListByMemberId(memberEntity.getId()); + + // 当前持仓列表 + List<ContractHoldOrderEntity> holdOrderEntities = contractHoldOrderDao.selectHoldOrderListByMemberId(memberEntity.getId()); + + // 冻结保证金 -- 即委托单中的保证金之和 + BigDecimal frozenBondAmount = BigDecimal.ZERO; + if (CollUtil.isNotEmpty(entrustOrderEntities)) { + for (ContractEntrustOrderEntity entrustOrderEntity : entrustOrderEntities) { + frozenBondAmount = frozenBondAmount.add(entrustOrderEntity.getBondAmount()); + } + } + + // 占用保证金 -- 即持仓单中的保证金之和 + BigDecimal beUsedBondAmount = BigDecimal.ZERO; + // 总盈利 + BigDecimal totalProfitOrLess = BigDecimal.ZERO; + if (CollUtil.isNotEmpty(holdOrderEntities)) { + BigDecimal lotNumber = cacheSettingUtils.getSymbolSku(symbol); + for (ContractHoldOrderEntity holdOrderEntity : holdOrderEntities) { + beUsedBondAmount = beUsedBondAmount.add(holdOrderEntity.getBondAmount()); + + // 单个订单盈利 + BigDecimal profitOrLess = BigDecimal.ZERO; + // 开多 + if (ContractHoldOrderEntity.OPENING_TYPE_MORE == holdOrderEntity.getOpeningType()) { + profitOrLess = newPrice.subtract(holdOrderEntity.getOpeningPrice()).multiply(new BigDecimal(holdOrderEntity.getSymbolCnt())).multiply(lotNumber).multiply(holdOrderEntity.getSymbolSku()); + // 开空 + } else { + profitOrLess = holdOrderEntity.getOpeningPrice().subtract(newPrice).multiply(new BigDecimal(holdOrderEntity.getSymbolCnt())).multiply(lotNumber).multiply(holdOrderEntity.getSymbolSku()); + } + + if (MemberEntity.IS_PROFIT_Y == memberEntity.getIsProfit()) { + if (profitOrLess.compareTo(BigDecimal.ZERO) > 0) { + profitOrLess = profitOrLess.multiply(BigDecimal.ONE.subtract(tradeSetting.getForceParam())); + } else { + profitOrLess = profitOrLess.multiply(BigDecimal.ONE.add(tradeSetting.getForceParam())); + } + } + + totalProfitOrLess = totalProfitOrLess.add(profitOrLess); + } + } + + MemberWalletContractEntity walletContractEntity = memberWalletContractDao.findWalletContractByMemberIdAndSymbol(memberEntity.getId(), CoinTypeEnum.USDT.name()); + + // 权益 + BigDecimal equity = walletContractEntity.getTotalBalance().add(totalProfitOrLess); + + ContractMoneyInfoVo contractMoneyInfoVo = new ContractMoneyInfoVo(); + contractMoneyInfoVo.setAvailableBalance(walletContractEntity.getAvailableBalance()); + contractMoneyInfoVo.setBeUsedBondAmount(beUsedBondAmount); + contractMoneyInfoVo.setFrozenBondAmount(frozenBondAmount); + contractMoneyInfoVo.setEquity(equity); + contractMoneyInfoVo.setFeeRatio(tradeSetting.getFeeRatio()); + contractMoneyInfoVo.setLeverAgeRatio(tradeSetting.getLeverageRatio()); + contractMoneyInfoVo.setNewPrice(newPrice); + contractMoneyInfoVo.setSymbolSku(cacheSettingUtils.getSymbolSku(symbol)); + return Result.ok(contractMoneyInfoVo); + } } -- Gitblit v1.9.1