From 67de76a89e21c545122c6cf111ef47b35f23997d Mon Sep 17 00:00:00 2001 From: Helius <wangdoubleone@gmail.com> Date: Fri, 04 Sep 2020 11:52:32 +0800 Subject: [PATCH] modify --- src/main/java/com/xcong/excoin/modules/contract/service/impl/RabbitOrderServiceImpl.java | 261 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 files changed, 249 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/xcong/excoin/modules/contract/service/impl/RabbitOrderServiceImpl.java b/src/main/java/com/xcong/excoin/modules/contract/service/impl/RabbitOrderServiceImpl.java index ca11dda..4e4e080 100644 --- a/src/main/java/com/xcong/excoin/modules/contract/service/impl/RabbitOrderServiceImpl.java +++ b/src/main/java/com/xcong/excoin/modules/contract/service/impl/RabbitOrderServiceImpl.java @@ -2,17 +2,22 @@ import cn.hutool.core.collection.CollUtil; import cn.hutool.core.util.StrUtil; +import com.alibaba.fastjson.JSONObject; +import com.xcong.excoin.common.contants.AppContants; import com.xcong.excoin.common.enumerates.CoinTypeEnum; import com.xcong.excoin.common.enumerates.OrderClosingTypeEnum; 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; import com.xcong.excoin.modules.contract.entity.ContractHoldOrderEntity; import com.xcong.excoin.modules.contract.entity.ContractOrderEntity; import com.xcong.excoin.modules.contract.mapper.ContractHoldOrderEntityMapper; import com.xcong.excoin.modules.contract.service.RabbitOrderService; import com.xcong.excoin.modules.documentary.common.NoticeConstant; import com.xcong.excoin.modules.documentary.dao.FollowFollowerOrderRelationDao; +import com.xcong.excoin.modules.documentary.dao.FollowFollowerProfitDao; import com.xcong.excoin.modules.documentary.dao.FollowTraderInfoDao; import com.xcong.excoin.modules.documentary.dao.FollowTraderProfitDetailDao; import com.xcong.excoin.modules.documentary.entity.FollowFollowerOrderRelationEntity; @@ -21,11 +26,14 @@ import com.xcong.excoin.modules.documentary.entity.FollowTraderProfitDetailEntity; import com.xcong.excoin.modules.documentary.service.FollowOrderOperationService; import com.xcong.excoin.modules.member.dao.MemberDao; +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.MemberSettingEntity; import com.xcong.excoin.modules.member.entity.MemberWalletContractEntity; import com.xcong.excoin.modules.platform.entity.PlatformTradeSettingEntity; +import com.xcong.excoin.rabbit.pricequeue.OrderModel; import com.xcong.excoin.utils.*; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; @@ -33,6 +41,7 @@ import javax.annotation.Resource; import java.math.BigDecimal; +import java.util.ArrayList; import java.util.Date; import java.util.List; @@ -46,28 +55,22 @@ @Resource private MemberDao memberDao; - @Resource private OrderWebsocketServiceImpl orderWebsocketService; - @Resource private ContractHoldOrderDao contractHoldOrderDao; - @Resource private ContractOrderDao contractOrderDao; - @Resource private CommonService commonService; - @Resource private MemberWalletContractDao memberWalletContractDao; - @Resource private CacheSettingUtils cacheSettingUtils; - @Resource private RedisUtils redisUtils; - + @Resource + private MemberSettingDao memberSettingDao; @Resource private FollowFollowerOrderRelationDao followFollowerOrderRelationDao; @Resource @@ -76,6 +79,10 @@ private FollowTraderInfoDao followTraderInfoDao; @Resource private FollowTraderProfitDetailDao followTraderProfitDetailDao; + @Resource + private FollowFollowerProfitDao followFollowerProfitDao; + @Resource + private ContractEntrustOrderDao contractEntrustOrderDao; @Transactional(rollbackFor = Exception.class) @Override @@ -84,7 +91,15 @@ try { if (ids.size() == 1) { ContractHoldOrderEntity holdOrderEntity = contractHoldOrderDao.selectById(ids.get(0)); - cancelHoldOrderMethod(holdOrderEntity); + + // 判断仓位类型是否逐仓 + if (holdOrderEntity.getPositionType() == ContractEntrustOrderEntity.POSITION_TYPE_ADD) { + // 逐仓平仓 + cancelHoldOrderMethod(holdOrderEntity); + } else { + // 全仓模式平仓 + closingWholeOrder(holdOrderEntity); + } } else { List<ContractHoldOrderEntity> holdOrderEntities = contractHoldOrderDao.selectBatchIds(ids); if (CollUtil.isNotEmpty(holdOrderEntities)) { @@ -118,16 +133,18 @@ BigDecimal profitOrLoss = BigDecimal.ZERO; Integer orderType = null; Integer closingType = null; + + MemberSettingEntity memberSettingEntity = memberSettingDao.selectMemberSettingByMemberId(memberEntity.getId()); // 开多 if (ContractHoldOrderEntity.OPENING_TYPE_MORE == holdOrderEntity.getOpeningType()) { - newPrice = newPrice.multiply(BigDecimal.ONE.subtract(memberEntity.getClosingSpread().divide(BigDecimal.valueOf(10000), 4, BigDecimal.ROUND_DOWN))); + newPrice = newPrice.multiply(BigDecimal.ONE.subtract(memberSettingEntity.getClosingSpread().divide(BigDecimal.valueOf(10000), 4, BigDecimal.ROUND_DOWN))); // (最新价-开仓价)*规格*张数 profitOrLoss = newPrice.subtract(holdOrderEntity.getOpeningPrice()).multiply(lotNumber).multiply(new BigDecimal(holdOrderEntity.getSymbolCnt())); orderType = ContractOrderEntity.ORDER_TYPE_CLOSE_MORE; closingType = OrderClosingTypeEnum.CLOSE_MORE.getValue(); // 开空 } else { - newPrice = newPrice.multiply(BigDecimal.ONE.add(memberEntity.getClosingSpread().divide(BigDecimal.valueOf(10000), 4, BigDecimal.ROUND_DOWN))); + newPrice = newPrice.multiply(BigDecimal.ONE.add(memberSettingEntity.getClosingSpread().divide(BigDecimal.valueOf(10000), 4, BigDecimal.ROUND_DOWN))); // (开仓价-最新价)*规格*张数 profitOrLoss = holdOrderEntity.getOpeningPrice().subtract(newPrice).multiply(lotNumber).multiply(new BigDecimal(holdOrderEntity.getSymbolCnt())); orderType = ContractOrderEntity.ORDER_TYPE_CLOSE_LESS; @@ -192,7 +209,8 @@ if (MemberEntity.IS_TRADER_Y.equals(memberEntity.getIsTrader())) { followOrderOperationService.closingFollowOrders(holdOrderEntity.getOrderNo()); } else { - LogRecordUtils.insertFollowerNotice(memberEntity.getId(), NoticeConstant.CLOSE_ORDER_TITLE, StrUtil.format(NoticeConstant.CLOSE_ORDER_CONTENT, contractOrderEntity.getSymbol(), contractOrderEntity.getClosingPrice(), profitOrLoss)); + followFollowerProfitDao.updateFollowerProfitByTradeMemberId(holdOrderEntity.getBondAmount().subtract(holdOrderEntity.getOpeningFeeAmount()), profitOrLoss, traderInfoEntity.getMemberId(), memberEntity.getId()); + LogRecordUtils.insertFollowerNotice(memberEntity.getId(), NoticeConstant.CLOSE_ORDER_TITLE, StrUtil.format(NoticeConstant.CLOSE_ORDER_CONTENT, contractOrderEntity.getSymbol(), contractOrderEntity.getClosingPrice(), profitOrLoss, traderInfoEntity.getNickname())); } } } @@ -230,4 +248,223 @@ profitDetailEntity.setOrderNo(orderNo); followTraderProfitDetailDao.insert(profitDetailEntity); } + + /** + * 全仓模式平仓逻辑 + */ + public void closingWholeOrder(ContractHoldOrderEntity holdOrderEntity) { + String symbol = holdOrderEntity.getSymbol(); + // 获取最新价 + BigDecimal newPrice = new BigDecimal(redisUtils.getString(CoinTypeConvert.convertToKey(symbol))); + + MemberEntity memberEntity = memberDao.selectById(holdOrderEntity.getMemberId()); + + MemberWalletContractEntity walletContract = memberWalletContractDao.findWalletContractByMemberIdAndSymbol(holdOrderEntity.getMemberId(), CoinTypeEnum.USDT.name()); + if (walletContract != null) { + BigDecimal lotNumber = cacheSettingUtils.getSymbolSku(symbol); + // 盈亏 + BigDecimal profitOrLoss = BigDecimal.ZERO; + Integer orderType = null; + Integer closingType = null; + + // 获取平仓张数 + Integer closeCnt = (Integer) redisUtils.get(AppContants.CLOSING_ORDER_PREFIX + holdOrderEntity.getId()); + + MemberSettingEntity memberSettingEntity = memberSettingDao.selectMemberSettingByMemberId(memberEntity.getId()); + // 开多 + if (ContractHoldOrderEntity.OPENING_TYPE_MORE == holdOrderEntity.getOpeningType()) { + newPrice = newPrice.multiply(BigDecimal.ONE.subtract(memberSettingEntity.getClosingSpread().divide(BigDecimal.valueOf(10000), 4, BigDecimal.ROUND_DOWN))); + // (最新价-开仓价)*规格*张数 + profitOrLoss = newPrice.subtract(holdOrderEntity.getOpeningPrice()).multiply(lotNumber).multiply(new BigDecimal(closeCnt)); + orderType = ContractOrderEntity.ORDER_TYPE_CLOSE_MORE; + closingType = OrderClosingTypeEnum.CLOSE_MORE.getValue(); + // 开空 + } else { + newPrice = newPrice.multiply(BigDecimal.ONE.add(memberSettingEntity.getClosingSpread().divide(BigDecimal.valueOf(10000), 4, BigDecimal.ROUND_DOWN))); + // (开仓价-最新价)*规格*张数 + profitOrLoss = holdOrderEntity.getOpeningPrice().subtract(newPrice).multiply(lotNumber).multiply(new BigDecimal(closeCnt)); + orderType = ContractOrderEntity.ORDER_TYPE_CLOSE_LESS; + closingType = OrderClosingTypeEnum.CLOSE_LESS.getValue(); + } + + if (memberEntity.getIsProfit() == MemberEntity.IS_PROFIT_Y) { + PlatformTradeSettingEntity tradeSettingEntity = cacheSettingUtils.getTradeSetting(); + if (profitOrLoss.compareTo(BigDecimal.ZERO) > -1) { + profitOrLoss = profitOrLoss.multiply(BigDecimal.ONE.subtract(tradeSettingEntity.getProfitParam())); + } else { + profitOrLoss = profitOrLoss.multiply(BigDecimal.ONE.add(tradeSettingEntity.getProfitParam())); + } + } + log.info("profitOrLoss:{}", profitOrLoss); + + // 保证金 + BigDecimal bondAmount = CalculateUtil.getBondAmount(holdOrderEntity.getOpeningPrice(), lotNumber, closeCnt, holdOrderEntity.getLeverRatio()); + + log.info("bondAmount:{}", bondAmount); + // 平仓手续费 TODO 可能需要修复手续费 + BigDecimal fee = BigDecimal.ZERO; + + if (holdOrderEntity.getSymbolCntSale() != 0) { + BigDecimal totalFeeAmount = contractOrderDao.selectWholeFeeAmountByOrderNo(holdOrderEntity.getOrderNo()); + fee = totalFeeAmount.divide(BigDecimal.valueOf(holdOrderEntity.getSymbolCnt()), 8, BigDecimal.ROUND_DOWN).multiply(BigDecimal.valueOf(closeCnt)); + } else { + fee = holdOrderEntity.getOpeningFeeAmount(); + } + log.info("fee:{}", fee); + ContractOrderEntity contractOrderEntity = ContractHoldOrderEntityMapper.INSTANCE.holdOrderToOrder(holdOrderEntity); + contractOrderEntity.setId(null); + contractOrderEntity.setClosingPrice(newPrice); + contractOrderEntity.setOrderType(orderType); + contractOrderEntity.setClosingType(closingType); + contractOrderEntity.setSymbolCnt(closeCnt); + contractOrderEntity.setRewardAmount(profitOrLoss); + contractOrderEntity.setBondAmount(bondAmount); + contractOrderEntity.setClosingFeeAmount(fee); + contractOrderDao.insert(contractOrderEntity); + + if (holdOrderEntity.getSymbolCntSale() != 0) { + holdOrderEntity.setOpeningFeeAmount(holdOrderEntity.getOpeningFeeAmount().subtract(fee)); + holdOrderEntity.setBondAmount(holdOrderEntity.getBondAmount().subtract(bondAmount)); + BigDecimal rewardAmount = holdOrderEntity.getRewardAmount() == null ? BigDecimal.ZERO : holdOrderEntity.getRewardAmount(); + holdOrderEntity.setRewardAmount(rewardAmount.add(profitOrLoss)); + contractHoldOrderDao.updateById(holdOrderEntity); + } else { + contractHoldOrderDao.deleteById(holdOrderEntity.getId()); + + // 若平掉所有张数,若存在委托平仓,则删除委托平仓记录 + List<ContractEntrustOrderEntity> entrustOrders = contractEntrustOrderDao.selectEntrustOrderByOrderNo(holdOrderEntity.getOrderNo()); + if (CollUtil.isNotEmpty(entrustOrders)) { + for (ContractEntrustOrderEntity entrustOrder : entrustOrders) { + contractEntrustOrderDao.deleteById(entrustOrder.getId()); + } + } + } + + BigDecimal changeAmount = profitOrLoss.add(bondAmount); + log.info("changeAmount : {}", changeAmount); + + BigDecimal total = profitOrLoss.subtract(fee); + log.info("totalMoney : {}", total); + + memberWalletContractDao.increaseWalletContractBalanceById(changeAmount, total, null, walletContract.getId()); + + ThreadPoolUtils.sendWholeForceClosingPrice(holdOrderEntity.getSymbol(), memberEntity); + // 计算佣金 + ThreadPoolUtils.calReturnMoney(memberEntity.getId(), fee, contractOrderEntity, AgentReturnEntity.ORDER_TYPE_CLOSE); + + } + } + + @Override + public void entrustCloseOrder(List<OrderModel> list) { + if (CollUtil.isNotEmpty(list)) { + List<Long> ids = new ArrayList<>(); + list.forEach(model -> ids.add(model.getOrderId())); + List<ContractEntrustOrderEntity> contractEntrustOrderEntities = contractEntrustOrderDao.selectEntrustOrderListByIds(ids); + + if (CollUtil.isNotEmpty(contractEntrustOrderEntities)) { + for (ContractEntrustOrderEntity entrustOrderEntity : contractEntrustOrderEntities) { + closeOrder(entrustOrderEntity); + } + } else { + log.info("平仓委托单未找到"); + } + } + } + + private void closeOrder(ContractEntrustOrderEntity entrustOrder) { + log.info("执行平仓委托"); + + Long memberId = entrustOrder.getMemberId(); + int orderType = entrustOrder.getEntrustType() == ContractEntrustOrderEntity.ENTRUST_TYPE_CLOSE_MORE ? ContractHoldOrderEntity.OPENING_TYPE_MORE : ContractHoldOrderEntity.OPENING_TYPE_LESS; + String symbol = entrustOrder.getSymbol(); + + MemberEntity memberEntity = memberDao.selectById(memberId); + MemberWalletContractEntity walletContract = memberWalletContractDao.findWalletContractByMemberIdAndSymbol(memberId, CoinTypeEnum.USDT.name()); + + BigDecimal lotNumber = cacheSettingUtils.getSymbolSku(symbol); + + BigDecimal newPrice = entrustOrder.getEntrustPrice(); + int closeCnt = entrustOrder.getSymbolCnt(); + + ContractHoldOrderEntity holdOrderEntity = contractHoldOrderDao.selectWholeHoldOrderByOrderType(memberId, orderType, symbol); + + List<Object> types = redisUtils.lGet(AppContants.RABBIT_TYPE + holdOrderEntity.getId(), 0, -1); + if(types.contains(9) || types.contains(10) || types.contains(11) || types.contains(12)) { + log.info("存在止盈/止损 orderId : {}, types : {}", holdOrderEntity.getId(), JSONObject.toJSONString(types)); + return; + } + + BigDecimal profitOrLoss; + int closingType; + // 平多 + if (entrustOrder.getEntrustType() == ContractEntrustOrderEntity.ENTRUST_TYPE_CLOSE_MORE) { + profitOrLoss = newPrice.subtract(holdOrderEntity.getOpeningPrice()).multiply(lotNumber).multiply(new BigDecimal(closeCnt)); + closingType = OrderClosingTypeEnum.CLOSE_MORE.getValue(); + + // 平空 + } else { + profitOrLoss = newPrice.subtract(holdOrderEntity.getOpeningPrice()).multiply(lotNumber).multiply(new BigDecimal(closeCnt)); + closingType = OrderClosingTypeEnum.CLOSE_LESS.getValue(); + } + + if (memberEntity.getIsProfit() == MemberEntity.IS_PROFIT_Y) { + PlatformTradeSettingEntity tradeSettingEntity = cacheSettingUtils.getTradeSetting(); + if (profitOrLoss.compareTo(BigDecimal.ZERO) > -1) { + profitOrLoss = profitOrLoss.multiply(BigDecimal.ONE.subtract(tradeSettingEntity.getProfitParam())); + } else { + profitOrLoss = profitOrLoss.multiply(BigDecimal.ONE.add(tradeSettingEntity.getProfitParam())); + } + } + log.info("profitOrLoss:{}", profitOrLoss); + + // 保证金 + BigDecimal bondAmount = CalculateUtil.getBondAmount(holdOrderEntity.getOpeningPrice(), lotNumber, closeCnt, holdOrderEntity.getLeverRatio()); + + log.info("bondAmount:{}", bondAmount); + // 平仓手续费 TODO 可能需要修复手续费 + BigDecimal fee = BigDecimal.ZERO; + + if (holdOrderEntity.getSymbolCntSale() != 0) { + fee = holdOrderEntity.getOpeningFeeAmount().divide(BigDecimal.valueOf(holdOrderEntity.getSymbolCnt()), 8, BigDecimal.ROUND_DOWN).multiply(BigDecimal.valueOf(closeCnt)); + } else { + fee = holdOrderEntity.getOpeningFeeAmount(); + } + + log.info("fee:{}", fee); + ContractOrderEntity contractOrderEntity = ContractHoldOrderEntityMapper.INSTANCE.holdOrderToOrder(holdOrderEntity); + contractOrderEntity.setId(null); + contractOrderEntity.setEntrustOpeningPrice(newPrice); + contractOrderEntity.setEntrustTime(new Date()); + contractOrderEntity.setClosingPrice(newPrice); + contractOrderEntity.setOrderType(orderType); + contractOrderEntity.setClosingType(closingType); + contractOrderEntity.setSymbolCnt(closeCnt); + contractOrderEntity.setRewardAmount(profitOrLoss); + contractOrderEntity.setBondAmount(bondAmount); + contractOrderEntity.setClosingFeeAmount(fee); + contractOrderDao.insert(contractOrderEntity); + + if (holdOrderEntity.getSymbolCntSale() - closeCnt != 0) { + holdOrderEntity.setSymbolCntSale(holdOrderEntity.getSymbolCntSale() - closeCnt); + holdOrderEntity.setOpeningFeeAmount(holdOrderEntity.getOpeningFeeAmount().subtract(fee)); + holdOrderEntity.setBondAmount(holdOrderEntity.getBondAmount().subtract(bondAmount)); + contractHoldOrderDao.updateById(holdOrderEntity); + } else { + contractHoldOrderDao.deleteById(holdOrderEntity.getId()); + } + + BigDecimal changeAmount = profitOrLoss.add(bondAmount); + log.info("changeAmount : {}", changeAmount); + + BigDecimal total = profitOrLoss.subtract(fee); + log.info("totalMoney : {}", total); + + memberWalletContractDao.increaseWalletContractBalanceById(changeAmount, total, null, walletContract.getId()); + + contractEntrustOrderDao.deleteById(entrustOrder.getId()); + ThreadPoolUtils.sendWholeForceClosingPrice(holdOrderEntity.getSymbol(), memberEntity); + // 计算佣金 + ThreadPoolUtils.calReturnMoney(memberEntity.getId(), fee, contractOrderEntity, AgentReturnEntity.ORDER_TYPE_CLOSE); + } } -- Gitblit v1.9.1