From 3a9537996edb1c9f97a6b047556e910ba2bd1cb7 Mon Sep 17 00:00:00 2001 From: Helius <wangdoubleone@gmail.com> Date: Sun, 13 Sep 2020 03:20:46 +0800 Subject: [PATCH] modify --- src/main/java/com/xcong/excoin/modules/contract/service/impl/ContractHoldOrderServiceImpl.java | 123 ++++++++++++++++++++++++++++++---------- 1 files changed, 92 insertions(+), 31 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 981ce56..a4024e1 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 @@ -24,11 +24,9 @@ import com.xcong.excoin.modules.contract.service.ContractHoldOrderService; import com.xcong.excoin.modules.member.dao.MemberDao; import com.xcong.excoin.modules.member.dao.MemberLevelRateDao; +import com.xcong.excoin.modules.member.dao.MemberSettingDao; import com.xcong.excoin.modules.member.dao.MemberWalletContractDao; -import com.xcong.excoin.modules.member.entity.AgentReturnEntity; -import com.xcong.excoin.modules.member.entity.MemberEntity; -import com.xcong.excoin.modules.member.entity.MemberLevelRateEntity; -import com.xcong.excoin.modules.member.entity.MemberWalletContractEntity; +import com.xcong.excoin.modules.member.entity.*; import com.xcong.excoin.modules.platform.dao.TradeSettingDao; import com.xcong.excoin.modules.platform.entity.PlatformTradeSettingEntity; import com.xcong.excoin.rabbit.producer.OrderProducer; @@ -82,6 +80,8 @@ @Resource private MemberDao memberDao; + @Resource + private MemberSettingDao memberSettingDao; @Transactional(rollbackFor = Exception.class) @Override @@ -94,7 +94,9 @@ MemberWalletContractEntity walletContract = memberWalletContractDao.findWalletContractByMemberIdAndSymbol(memberEntity.getId(), CoinTypeEnum.USDT.name()); PlatformTradeSettingEntity tradeSettingEntity = cacheSettingUtils.getTradeSetting(); - BigDecimal spread = memberEntity.getSpread(); + + MemberSettingEntity memberSetting = memberSettingDao.selectMemberSettingByMemberId(memberEntity.getId()); + BigDecimal spread = memberSetting.getSpread(); // 规格 BigDecimal lotNumber = cacheSettingUtils.getSymbolSku(submitOrderDto.getSymbol()); @@ -157,15 +159,28 @@ holdOrderEntity.setIsCanClosing(ContractHoldOrderEntity.ORDER_CAN_CLOSING_Y); holdOrderEntity.setPrePaymentAmount(prePaymentAmount); holdOrderEntity.setBondAmount(bondAmount.add(openFeePrice)); + holdOrderEntity.setOperateNo(1); ContractOrderEntity contractOrderEntity = ContractHoldOrderEntityMapper.INSTANCE.holdOrderToOrder(holdOrderEntity); contractOrderEntity.setOpeningTime(new Date()); contractHoldOrderDao.insert(holdOrderEntity); int i = contractOrderDao.insert(contractOrderEntity); + if (i > 0) { memberWalletContractDao.increaseWalletContractBalanceById(prePaymentAmount.negate(), openFeePrice.negate(), null, walletContract.getId()); + + // 发送爆仓消息 + sendOrderBombMsg(holdOrderEntity.getId(), holdOrderEntity.getOpeningType(), forceClosingPrice, holdOrderEntity.getSymbol(), holdOrderEntity.getOperateNo()); + // 计算佣金 ThreadPoolUtils.calReturnMoney(memberEntity.getId(), contractOrderEntity.getOpeningFeeAmount(), contractOrderEntity, AgentReturnEntity.ORDER_TYPE_OPEN); + + // 插入财务流水 + if (submitOrderDto.getOrderType() == ContractHoldOrderEntity.OPENING_TYPE_MORE) { + LogRecordUtils.insertMemberAccountFlow(memberEntity.getId(), prePaymentAmount, walletContract.getAvailableBalance().subtract(prePaymentAmount), submitOrderDto.getSymbol(), "买涨持仓", "买涨:" + submitOrderDto.getSymbol()); + } else { + LogRecordUtils.insertMemberAccountFlow(memberEntity.getId(), prePaymentAmount, walletContract.getAvailableBalance().subtract(prePaymentAmount), submitOrderDto.getSymbol(), "买跌持仓", "买跌:" + submitOrderDto.getSymbol()); + } return Result.ok("提交成功"); } return Result.fail("提交失败"); @@ -217,7 +232,7 @@ if (rewardRatio.compareTo(BigDecimal.ZERO) > -1) { rewardRatio = rewardRatio.multiply(BigDecimal.ONE.subtract(tradeSettingEntity.getProfitParam())); } else { - rewardRatio = rewardRatio.multiply(BigDecimal.ONE.add(tradeSettingEntity.getProfitParam())); +// rewardRatio = rewardRatio.multiply(BigDecimal.ONE.add(tradeSettingEntity.getProfitParam())); } } @@ -240,6 +255,10 @@ canReduceMaxBond = BigDecimal.ZERO; } + if (rewardRatio.compareTo(BigDecimal.ZERO) < 0) { + canReduceMaxBond = canReduceMaxBond.add(rewardRatio); + } + holdOrderListVo.setCanReduceMaxBond(canReduceMaxBond); holdOrderListVo.setCanAddMaxBond(walletContractEntity.getAvailableBalance()); holdOrderListVo.setReturnRate(returnRate); @@ -250,7 +269,7 @@ Map<String, Object> result = new HashMap<>(); result.put("hold", resultList); - result.put("totalProfitOrLoss", totalProfitOrLoss); + result.put("totalProfitOrLoss", totalProfitOrLoss.setScale(4, BigDecimal.ROUND_DOWN).toPlainString()); return Result.ok(result); } return Result.ok("success"); @@ -374,22 +393,25 @@ // 开多止盈 if (stopProfitPrice != null) { model = new OrderModel(holdOrderEntity.getId(), RabbitPriceTypeEnum.CLOSE_MORE_STOP_PROFIT.getValue(), stopProfitPrice.setScale(8, RoundingMode.HALF_UP).toPlainString(), holdOrderEntity.getSymbol()); + producer.sendPriceOperate(JSONObject.toJSONString(model)); } // 开多止损 if (stopLessPrice != null) { model = new OrderModel(holdOrderEntity.getId(), RabbitPriceTypeEnum.CLOSE_MORE_STOP_LESS.getValue(), stopLessPrice.setScale(8, RoundingMode.HALF_UP).toPlainString(), holdOrderEntity.getSymbol()); + producer.sendPriceOperate(JSONObject.toJSONString(model)); } } else { // 开空止盈 if (stopProfitPrice != null) { model = new OrderModel(holdOrderEntity.getId(), RabbitPriceTypeEnum.CLOSE_LESS_STOP_PROFIT.getValue(), stopProfitPrice.setScale(8, RoundingMode.HALF_UP).toPlainString(), holdOrderEntity.getSymbol()); + producer.sendPriceOperate(JSONObject.toJSONString(model)); } // 开空止损 if (stopLessPrice != null) { model = new OrderModel(holdOrderEntity.getId(), RabbitPriceTypeEnum.CLOSE_LESS_STOP_LESS.getValue(), stopLessPrice.setScale(8, RoundingMode.HALF_UP).toPlainString(), holdOrderEntity.getSymbol()); + producer.sendPriceOperate(JSONObject.toJSONString(model)); } } - producer.sendPriceOperate(JSONObject.toJSONString(model)); return Result.ok("设置成功"); } return Result.fail("设置失败"); @@ -411,14 +433,14 @@ if (changeBondDto.getAmount().compareTo(walletContract.getAvailableBalance()) > 0) { return Result.fail("可用余额不足"); } - memberWalletContractDao.increaseWalletContractBalanceById(changeBondDto.getAmount().negate(), null, changeBondDto.getAmount(), walletContract.getId()); + memberWalletContractDao.increaseWalletContractBalanceById(changeBondDto.getAmount().negate(), null, null, walletContract.getId()); holdOrderEntity.setBondAmount(holdOrderEntity.getBondAmount().add(changeBondDto.getAmount())); // 减少保证金 } else { if (holdOrderEntity.getBondAmount().subtract(holdOrderEntity.getPrePaymentAmount()).subtract(changeBondDto.getAmount()).compareTo(BigDecimal.ZERO) < 0) { return Result.fail("超出保证金最大减少金额"); } - memberWalletContractDao.increaseWalletContractBalanceById(changeBondDto.getAmount(), null, changeBondDto.getAmount().negate(), walletContract.getId()); + memberWalletContractDao.increaseWalletContractBalanceById(changeBondDto.getAmount(), null, null, walletContract.getId()); holdOrderEntity.setBondAmount(holdOrderEntity.getBondAmount().subtract(changeBondDto.getAmount())); } @@ -429,15 +451,8 @@ int i = contractHoldOrderDao.updateById(holdOrderEntity); if (i > 0) { - OrderModel model = null; - // 开多 - if (ContractHoldOrderEntity.OPENING_TYPE_MORE == holdOrderEntity.getOpeningType()) { - 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.setScale(8, RoundingMode.HALF_UP).toPlainString(), holdOrderEntity.getSymbol()); - } - producer.sendPriceOperate(JSONObject.toJSONString(model)); + // 发送爆仓消息 + sendOrderBombMsg(holdOrderEntity.getId(), holdOrderEntity.getOpeningType(), forceClosingPrice, holdOrderEntity.getSymbol(), holdOrderEntity.getOperateNo()); return Result.ok("调整成功"); } return Result.fail("调整失败"); @@ -479,10 +494,10 @@ 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()); + profitOrLess = newPrice.subtract(holdOrderEntity.getOpeningPrice()).multiply(new BigDecimal(holdOrderEntity.getSymbolCnt())).multiply(lotNumber); // 开空 } else { - profitOrLess = holdOrderEntity.getOpeningPrice().subtract(newPrice).multiply(new BigDecimal(holdOrderEntity.getSymbolCnt())).multiply(lotNumber).multiply(holdOrderEntity.getSymbolSku()); + profitOrLess = holdOrderEntity.getOpeningPrice().subtract(newPrice).multiply(new BigDecimal(holdOrderEntity.getSymbolCnt())).multiply(lotNumber); } if (MemberEntity.IS_PROFIT_Y == memberEntity.getIsProfit()) { @@ -539,6 +554,9 @@ } HoldOrderDetailVo holdOrderDetailVo = ContractHoldOrderEntityMapper.INSTANCE.holdOrderToOrderDetailVo(holdOrderEntity); + BigDecimal feeSpread = cacheSettingUtils.getTradeSetting().getFeeSpreadRatio(); +// holdOrderDetailVo.setOpeningFeeAmount(holdOrderDetailVo.getOpeningFeeAmount().multiply(feeSpread).setScale(8, BigDecimal.ROUND_DOWN)); + holdOrderDetailVo.setOpeningFeeAmount(holdOrderEntity.getOpeningFeeAmount(), feeSpread); return Result.ok(holdOrderDetailVo); } @@ -563,6 +581,11 @@ } OrderDetailVo orderDetailVo = ContractOrderEntityMapper.INSTANCE.entityToDetailVo(contractOrderEntity); + BigDecimal feeSpread = cacheSettingUtils.getTradeSetting().getFeeSpreadRatio(); +// orderDetailVo.setClosingFeeAmount(orderDetailVo.getClosingFeeAmount() == null ? orderDetailVo.getClosingFeeAmount() : orderDetailVo.getClosingFeeAmount().multiply(feeSpread).setScale(8, BigDecimal.ROUND_DOWN)); +// orderDetailVo.setOpeningFeeAmount(orderDetailVo.getOpeningFeeAmount() == null ? orderDetailVo.getOpeningFeeAmount() : orderDetailVo.getOpeningFeeAmount().multiply(feeSpread).setScale(8, BigDecimal.ROUND_DOWN)); + orderDetailVo.setOpeningFeeAmount(contractOrderEntity.getOpeningFeeAmount(), feeSpread); + orderDetailVo.setClosingFeeAmount(contractOrderEntity.getClosingFeeAmount(), feeSpread); return Result.ok(orderDetailVo); } @@ -592,7 +615,7 @@ } else { BigDecimal available = walletContractEntity.getAvailableBalance(); BigDecimal lessAmount = thisTimeHold.subtract(available); - MemberEntity memberEntity = memberDao.selectById(holdOrderEntity.getId()); + MemberEntity memberEntity = memberDao.selectById(holdOrderEntity.getMemberId()); memberWalletContractDao.increaseWalletContractBalanceById(available.negate(), available.negate(), null, walletContractEntity.getId()); BigDecimal newBondAmount = holdOrderEntity.getBondAmount().subtract(lessAmount); @@ -605,17 +628,55 @@ holdOrderEntity.setOperateNo(holdOrderEntity.getOperateNo() + 1); contractHoldOrderDao.updateById(holdOrderEntity); - OrderModel model = null; - // 开多 - if (ContractHoldOrderEntity.OPENING_TYPE_MORE == holdOrderEntity.getOpeningType()) { - model = new OrderModel(holdOrderEntity.getId(), RabbitPriceTypeEnum.CLOSE_MORE_BOMB.getValue(), newForcePrice.setScale(8, RoundingMode.HALF_UP).toPlainString(), holdOrderEntity.getSymbol()); - // 开空 - } else { - model = new OrderModel(holdOrderEntity.getId(), RabbitPriceTypeEnum.CLOSE_LESS_BOMB.getValue(), newForcePrice.setScale(8, RoundingMode.HALF_UP).toPlainString(), holdOrderEntity.getSymbol()); - } - producer.sendPriceOperate(JSONObject.toJSONString(model)); + // 发送爆仓消息 + sendOrderBombMsg(holdOrderEntity.getId(), holdOrderEntity.getOpeningType(), newForcePrice, holdOrderEntity.getSymbol(), holdOrderEntity.getOperateNo()); } } } } + + public void sendOrderBombMsg(Long id, int type, BigDecimal forceClosingPrice, String symbol, int operateNo) { + OrderModel model = null; + // 开多 + if (ContractHoldOrderEntity.OPENING_TYPE_MORE == type) { + model = new OrderModel(id, RabbitPriceTypeEnum.CLOSE_MORE_BOMB.getValue(), forceClosingPrice.setScale(8, RoundingMode.HALF_UP).toPlainString(), symbol, operateNo); + // 开空 + } else { + model = new OrderModel(id, RabbitPriceTypeEnum.CLOSE_LESS_BOMB.getValue(), forceClosingPrice.setScale(8, RoundingMode.HALF_UP).toPlainString(), symbol, operateNo); + } + producer.sendPriceOperate(JSONObject.toJSONString(model)); + } + + @Transactional(rollbackFor = Exception.class) + @Override + public void calHoldFeeAmountForBondAmount() { + List<ContractHoldOrderEntity> list = contractHoldOrderDao.selectAllHoldOrder(); + PlatformTradeSettingEntity tradeSettingEntity = cacheSettingUtils.getTradeSetting(); + + if (CollUtil.isNotEmpty(list)) { + for (ContractHoldOrderEntity holdOrderEntity : list) { + BigDecimal holdAmount = holdOrderEntity.getHoldAmount(); + if (holdAmount == null) { + holdAmount = BigDecimal.ZERO; + } + + BigDecimal thisTimeHold = holdOrderEntity.getBondAmount().multiply(tradeSettingEntity.getDoingRatio()); + log.info("订单编号:{}, 持仓费:{}", holdOrderEntity.getOrderNo(), thisTimeHold); + + MemberEntity memberEntity = memberDao.selectById(holdOrderEntity.getMemberId()); + BigDecimal subBond = holdOrderEntity.getBondAmount().subtract(thisTimeHold); + + BigDecimal newForcePrice = CalculateUtil.getForceSetPrice(subBond.subtract(holdOrderEntity.getOpeningFeeAmount()), holdOrderEntity.getOpeningPrice(), holdOrderEntity.getSymbolCnt(), holdOrderEntity.getSymbolSku(), holdOrderEntity.getOpeningType(), memberEntity); + holdAmount = holdAmount.add(thisTimeHold); + holdOrderEntity.setBondAmount(subBond); + holdOrderEntity.setHoldAmount(holdAmount); + holdOrderEntity.setForceClosingPrice(newForcePrice); + holdOrderEntity.setOperateNo(holdOrderEntity.getOperateNo() + 1); + contractHoldOrderDao.updateById(holdOrderEntity); + + // 发送爆仓消息 + sendOrderBombMsg(holdOrderEntity.getId(), holdOrderEntity.getOpeningType(), newForcePrice, holdOrderEntity.getSymbol(), holdOrderEntity.getOperateNo()); + } + } + } } -- Gitblit v1.9.1