From d23645e976981bc9b670eea1d469fe8a36be309c Mon Sep 17 00:00:00 2001 From: KKSU <15274802129@163.com> Date: Wed, 17 Apr 2024 17:19:53 +0800 Subject: [PATCH] 55测试环境 --- src/main/java/com/xcong/excoin/modules/contract/service/impl/OrderWebsocketServiceImpl.java | 391 ++++++++++++++++++++++++++++++++++++++++++------------- 1 files changed, 295 insertions(+), 96 deletions(-) diff --git a/src/main/java/com/xcong/excoin/modules/contract/service/impl/OrderWebsocketServiceImpl.java b/src/main/java/com/xcong/excoin/modules/contract/service/impl/OrderWebsocketServiceImpl.java index 7b5339e..b7304af 100644 --- a/src/main/java/com/xcong/excoin/modules/contract/service/impl/OrderWebsocketServiceImpl.java +++ b/src/main/java/com/xcong/excoin/modules/contract/service/impl/OrderWebsocketServiceImpl.java @@ -1,8 +1,15 @@ package com.xcong.excoin.modules.contract.service.impl; +import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.util.ObjectUtil; +import cn.hutool.core.util.StrUtil; import com.alibaba.fastjson.JSONObject; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.xcong.excoin.common.contants.AppContants; +import com.xcong.excoin.common.enumerates.CoinTypeEnum; import com.xcong.excoin.modules.coin.dao.MemberAccountFlowEntityDao; import com.xcong.excoin.modules.coin.entity.MemberAccountFlowEntity; +import com.xcong.excoin.modules.contract.dao.ContractEntrustOrderDao; import com.xcong.excoin.modules.contract.dao.ContractHoldOrderDao; import com.xcong.excoin.modules.contract.entity.ContractEntrustOrderEntity; import com.xcong.excoin.modules.contract.entity.ContractHoldOrderEntity; @@ -12,6 +19,16 @@ import com.xcong.excoin.modules.contract.service.ContractEntrustOrderService; import com.xcong.excoin.modules.contract.service.ContractHoldOrderService; import com.xcong.excoin.modules.contract.service.ContractOrderService; +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.FollowFollowerSettingDao; +import com.xcong.excoin.modules.documentary.dao.FollowTraderInfoDao; +import com.xcong.excoin.modules.documentary.entity.FollowFollowerOrderRelationEntity; +import com.xcong.excoin.modules.documentary.entity.FollowFollowerSettingEntity; +import com.xcong.excoin.modules.documentary.entity.FollowTraderInfoEntity; +import com.xcong.excoin.modules.documentary.service.FollowOrderOperationService; import com.xcong.excoin.modules.member.dao.AgentReturnDao; import com.xcong.excoin.modules.member.dao.MemberSettingDao; import com.xcong.excoin.modules.member.entity.AgentReturnEntity; @@ -23,11 +40,9 @@ import com.xcong.excoin.modules.member.service.MemberWalletContractService; import com.xcong.excoin.modules.platform.entity.PlatformTradeSettingEntity; import com.xcong.excoin.rabbit.pricequeue.OrderModel; +import com.xcong.excoin.rabbit.producer.FollowProducer; import com.xcong.excoin.rabbit.producer.OrderProducer; -import com.xcong.excoin.utils.CacheSettingUtils; -import com.xcong.excoin.utils.CalculateUtil; -import com.xcong.excoin.utils.CoinTypeConvert; -import com.xcong.excoin.utils.ThreadPoolUtils; +import com.xcong.excoin.utils.*; import lombok.extern.slf4j.Slf4j; import org.apache.commons.collections.CollectionUtils; import org.springframework.stereotype.Service; @@ -58,6 +73,16 @@ MemberWalletContractService memberWalletContractService; @Resource + FollowFollowerProfitDao followFollowerProfitDao; + + @Resource + FollowFollowerSettingDao followFollowerSettingDao; + @Resource + FollowFollowerOrderRelationDao followFollowerOrderRelationDao; + @Resource + FollowTraderInfoDao followTraderInfoDao; + + @Resource CacheSettingUtils cacheSettingUtils; @Resource @@ -75,7 +100,15 @@ private MemberSettingDao memberSettingDao; @Resource private ContractHoldOrderDao contractHoldOrderDao; + @Resource + private ContractEntrustOrderDao contractEntrustOrderDao; + @Resource + private RedisUtils redisUtils; + @Resource + private FollowOrderOperationService followOrderOperationService; + @Resource + private FollowProducer followProducer; public void dealOrderFromMq(List<OrderModel> list, Integer type) { if (CollectionUtils.isNotEmpty(list)) { @@ -131,6 +164,38 @@ } } + + /** + * 止盈/止损后删除全仓委托平仓单 + * + * @param orderNo + */ + private void deleteEntrustCloseOrder(String orderNo) { + // 若平掉所有张数,若存在委托平仓,则删除委托平仓记录 + List<ContractEntrustOrderEntity> entrustOrders = contractEntrustOrderDao.selectEntrustOrderByOrderNo(orderNo); + if (CollUtil.isNotEmpty(entrustOrders)) { + for (ContractEntrustOrderEntity entrustOrder : entrustOrders) { + contractEntrustOrderDao.deleteById(entrustOrder.getId()); + } + } + } + + private void closingFollowerOrders(MemberEntity memberEntity, ContractHoldOrderEntity holdOrderEntity, ContractOrderEntity contractOrderEntity) { + // 判断当前持仓是否为跟单订单 + if (ContractOrderEntity.CONTRACTTYPE_DOCUMENTARY == holdOrderEntity.getContractType()) { + FollowTraderInfoEntity traderInfoEntity = followTraderInfoDao.selectTraderInfoByOrderId(holdOrderEntity.getId()); + updateFollowOrderRelation(holdOrderEntity.getId(), contractOrderEntity.getId()); + + // 若为交易员,则平仓跟随者订单 + 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().setScale(2, BigDecimal.ROUND_HALF_UP).toString(), contractOrderEntity.getRewardAmount().setScale(2, BigDecimal.ROUND_HALF_UP).toString(), traderInfoEntity.getNickname())); + } + } + } + + /** * 开多止盈 */ @@ -164,12 +229,7 @@ // 本次需要退回的保证金 BigDecimal prePrice = order.getBondAmount(); Long memberId = order.getMemberId(); - MemberWalletContractEntity wallet; - if (order.getPositionType() == ContractEntrustOrderEntity.POSITION_TYPE_ADD) { - wallet = memberWalletContractService.findWalletContractByMemberIdAndSymbol(memberId, "USDT"); - } else { - wallet = memberWalletContractService.findWalletContractByMemberIdAndSymbol(memberId, CoinTypeConvert.convertContractTypeToCoin(order.getSymbol())); - } + MemberWalletContractEntity wallet = memberWalletContractService.findWalletContractByMemberIdAndSymbol(memberId, CoinTypeEnum.USDT.name()); if (wallet != null) { // 历史订单 @@ -178,7 +238,7 @@ contractOrderEntity.setId(null); // 本次平仓数量 - int currentFlat = order.getSymbolCnt(); + int currentFlat = order.getSymbolCntSale(); BigDecimal symbolSku = cacheSettingUtils.getSymbolSku(order.getSymbol()); // 盈亏额度= (当前的币种的平仓价-下单时的建仓价)*购买的手数/规格*倍率 BigDecimal profitLossPrice = (closePrice @@ -224,6 +284,14 @@ //返佣 ThreadPoolUtils.calReturnMoney(order.getMemberId(), order.getOpeningFeeAmount(), contractOrderEntity, AgentReturnEntity.ORDER_TYPE_CLOSE); + + // 全仓模式,止盈/止损 删除委托平仓 + if (ContractEntrustOrderEntity.POSITION_TYPE_ALL == order.getPositionType()) { + ThreadPoolUtils.sendWholePrice(memberId); + deleteEntrustCloseOrder(order.getOrderNo()); + } else { + closingFollowerOrders(memberEntity, order, contractOrderEntity); + } } } } @@ -234,6 +302,7 @@ /** * 开空止盈 */ + @Transactional(rollbackFor = Exception.class) public void dealForLessStopPro(List<ContractHoldOrderEntity> orderList, List<OrderModel> list) { //List<CoinsCoinsOrder> orderList = orderMapper.selectOrderByBatchNo(batchno); //System.out.println("开空止盈订单batchno:" + batchno); @@ -263,12 +332,8 @@ // 本次需要退回的预付款 BigDecimal prePrice = order.getBondAmount(); Long memberId = order.getMemberId(); - MemberWalletContractEntity wallet; - if (order.getPositionType() == ContractEntrustOrderEntity.POSITION_TYPE_ADD) { - wallet = memberWalletContractService.findWalletContractByMemberIdAndSymbol(memberId, "USDT"); - } else { - wallet = memberWalletContractService.findWalletContractByMemberIdAndSymbol(memberId, CoinTypeConvert.convertContractTypeToCoin(order.getSymbol())); - } + MemberWalletContractEntity wallet = memberWalletContractService.findWalletContractByMemberIdAndSymbol(memberId, CoinTypeEnum.USDT.name()); + if (wallet != null) { // 历史订单 ContractOrderEntity contractOrderEntity = ContractHoldOrderEntityMapper.INSTANCE.holdOrderToOrder(order); @@ -276,7 +341,7 @@ contractOrderEntity.setId(null); // 本次平仓数量 - int currentFlat = order.getSymbolCnt(); + int currentFlat = order.getSymbolCntSale(); BigDecimal symbolSku = cacheSettingUtils.getSymbolSku(order.getSymbol()); // 盈亏额度= (当前的币种的平仓价-下单时的建仓价)*购买的手数/规格*倍率 BigDecimal profitLossPrice = (order.getOpeningPrice() @@ -322,6 +387,14 @@ //返佣 ThreadPoolUtils.calReturnMoney(order.getMemberId(), order.getOpeningFeeAmount(), contractOrderEntity, AgentReturnEntity.ORDER_TYPE_CLOSE); + + // 全仓模式,止盈/止损 删除委托平仓 + if (ContractEntrustOrderEntity.POSITION_TYPE_ALL == order.getPositionType()) { + ThreadPoolUtils.sendWholePrice(memberId); + deleteEntrustCloseOrder(order.getOrderNo()); + } else { + closingFollowerOrders(memberEntity, order, contractOrderEntity); + } } } } @@ -334,6 +407,7 @@ * * @param */ + @Transactional(rollbackFor = Exception.class) public void dealForMoreLoss(List<ContractHoldOrderEntity> orderList, List<OrderModel> list) { //List<CoinsCoinsOrder> orderList = orderMapper.selectOrderByBatchNo(batchno); //System.out.println("开多止损批次号batchno:" + batchno); @@ -362,12 +436,7 @@ Long memberId = order.getMemberId(); // 本次需要退回的预付款 BigDecimal prePrice = order.getBondAmount(); - MemberWalletContractEntity wallet; - if (order.getPositionType() == ContractEntrustOrderEntity.POSITION_TYPE_ADD) { - wallet = memberWalletContractService.findWalletContractByMemberIdAndSymbol(memberId, "USDT"); - } else { - wallet = memberWalletContractService.findWalletContractByMemberIdAndSymbol(memberId, CoinTypeConvert.convertContractTypeToCoin(order.getSymbol())); - } + MemberWalletContractEntity wallet = memberWalletContractService.findWalletContractByMemberIdAndSymbol(memberId, CoinTypeEnum.USDT.name()); if (wallet != null) { // 历史订单 @@ -376,7 +445,7 @@ contractOrderEntity.setId(null); // 本次平仓数量 - int currentFlat = order.getSymbolCnt(); + int currentFlat = order.getSymbolCntSale(); BigDecimal symbolSku = cacheSettingUtils.getSymbolSku(order.getSymbol()); // 盈亏额度= (当前的币种的平仓价-下单时的建仓价)*购买的手数/规格*倍率 BigDecimal profitLossPrice = (closePrice @@ -419,6 +488,14 @@ //返佣 ThreadPoolUtils.calReturnMoney(order.getMemberId(), order.getOpeningFeeAmount(), contractOrderEntity, AgentReturnEntity.ORDER_TYPE_CLOSE); + + // 全仓模式,止盈/止损 删除委托平仓 + if (ContractEntrustOrderEntity.POSITION_TYPE_ALL == order.getPositionType()) { + ThreadPoolUtils.sendWholePrice(memberId); + deleteEntrustCloseOrder(order.getOrderNo()); + } else { + closingFollowerOrders(memberEntity, order, contractOrderEntity); + } } } } @@ -430,6 +507,7 @@ * * @param */ + @Transactional(rollbackFor = Exception.class) public void dealForLessLoss(List<ContractHoldOrderEntity> orderList, List<OrderModel> list) { // List<CoinsCoinsOrder> orderList = orderMapper.selectOrderByBatchNo(batchno); //System.out.println("开空止损批次号batchno:" + batchno); @@ -458,12 +536,7 @@ Long memberId = order.getMemberId(); // 本次需要退回的预付款 BigDecimal prePrice = order.getBondAmount(); - MemberWalletContractEntity wallet; - if (order.getPositionType() == ContractEntrustOrderEntity.POSITION_TYPE_ADD) { - wallet = memberWalletContractService.findWalletContractByMemberIdAndSymbol(memberId, "USDT"); - } else { - wallet = memberWalletContractService.findWalletContractByMemberIdAndSymbol(memberId, CoinTypeConvert.convertContractTypeToCoin(order.getSymbol())); - } + MemberWalletContractEntity wallet = memberWalletContractService.findWalletContractByMemberIdAndSymbol(memberId, CoinTypeEnum.USDT.name()); if (wallet != null) { // 历史订单 @@ -472,7 +545,7 @@ contractOrderEntity.setId(null); // 本次平仓数量 - int currentFlat = order.getSymbolCnt(); + int currentFlat = order.getSymbolCntSale(); BigDecimal symbolSku = cacheSettingUtils.getSymbolSku(order.getSymbol()); // 盈亏额度= (当前的币种的平仓价-下单时的建仓价)*购买的手数/规格*倍率 BigDecimal profitLossPrice = (order.getOpeningPrice() @@ -515,6 +588,14 @@ //返佣 ThreadPoolUtils.calReturnMoney(order.getMemberId(), order.getOpeningFeeAmount(), contractOrderEntity, AgentReturnEntity.ORDER_TYPE_CLOSE); + + // 全仓模式,止盈/止损 删除委托平仓 + if (ContractEntrustOrderEntity.POSITION_TYPE_ALL == order.getPositionType()) { + ThreadPoolUtils.sendWholePrice(memberId); + deleteEntrustCloseOrder(order.getOrderNo()); + } else { + closingFollowerOrders(memberEntity, order, contractOrderEntity); + } } } } @@ -572,6 +653,16 @@ contractHoldOrderEntity.setOpeningType(ContractHoldOrderEntity.OPENING_TYPE_LESS); } + // 判断是否开启了带单 + boolean isOpenFollow = false; + FollowTraderInfoEntity tradeInfo = null; + if (MemberEntity.IS_TRADER_Y.equals(memberEntity.getIsTrader())) { + tradeInfo = followTraderInfoDao.selectTraderInfoByMemberId(memberEntity.getId()); + if (FollowTraderInfoEntity.ISOPEN_Y.equals(tradeInfo.getIsOpen())) { + isOpenFollow = true; + } + } + //持仓单赋值 contractHoldOrderEntity.setMemberId(memId); contractHoldOrderEntity.setIsCanClosing(ContractHoldOrderEntity.ORDER_CAN_CLOSING_Y); @@ -593,6 +684,8 @@ contractHoldOrderEntity.setOpeningPrice(entrustPrice); contractHoldOrderEntity.setTradeType(ContractHoldOrderEntity.TRADE_TYPE_LIMIT); contractHoldOrderEntity.setOperateNo(1); + contractHoldOrderEntity.setSymbolCntSale(contractHoldOrderEntity.getSymbolCnt()); + contractHoldOrderEntity.setContractType(isOpenFollow ? ContractOrderEntity.CONTRACTTYPE_DOCUMENTARY : ContractOrderEntity.CONTRACTTYPE_NORMAL); contractHoldOrderService.save(contractHoldOrderEntity); // 需要一个历史插入 @@ -607,11 +700,11 @@ // 市价 if (coinsCoinsOrder.getEntrustType() == 1) { // 开多 - OrderModel model = new OrderModel(contractHoldOrderEntity.getId(), 6, contractHoldOrderEntity.getForceClosingPrice().setScale(8, RoundingMode.HALF_UP).toPlainString(), coinsCoinsOrder.getSymbol(), 1); + OrderModel model = new OrderModel(contractHoldOrderEntity.getId(), 6, contractHoldOrderEntity.getForceClosingPrice().setScale(8, RoundingMode.HALF_UP).toPlainString(), coinsCoinsOrder.getSymbol(), 1, coinsCoinsOrder.getMemberId()); producer.sendPriceOperate(JSONObject.toJSONString(model)); } else { // 开空 - OrderModel model = new OrderModel(contractHoldOrderEntity.getId(), 7, contractHoldOrderEntity.getForceClosingPrice().setScale(8, RoundingMode.HALF_UP).toPlainString(), coinsCoinsOrder.getSymbol(), 1); + OrderModel model = new OrderModel(contractHoldOrderEntity.getId(), 7, contractHoldOrderEntity.getForceClosingPrice().setScale(8, RoundingMode.HALF_UP).toPlainString(), coinsCoinsOrder.getSymbol(), 1, coinsCoinsOrder.getMemberId()); producer.sendPriceOperate(JSONObject.toJSONString(model)); } // 扣除手续费 @@ -621,13 +714,34 @@ //返佣 ThreadPoolUtils.calReturnMoney(memberEntity.getId(), openFeePrice, contractOrderEntity, AgentReturnEntity.ORDER_TYPE_OPEN); + + // 若该用户为交易员且开启带单模式,则发送带单异步 + if (isOpenFollow) { + contractHoldOrderService.sendFollowOrder(tradeInfo, contractHoldOrderEntity); + } + } } } private void wholeEntrustOrder(ContractEntrustOrderEntity entrustOrder) { log.info("全仓委托逻辑处理:{}", entrustOrder.getOrderNo()); - MemberWalletContractEntity wallet = memberWalletContractService.findWalletContractByMemberIdAndSymbol(entrustOrder.getMemberId(), CoinTypeConvert.convertContractTypeToCoin(entrustOrder.getSymbol())); + + int i = 1; + while (true) { + List<Object> types = redisUtils.lGet(AppContants.MEMBER_TYPE + entrustOrder.getMemberId(), 0, -1); + if (CollUtil.isEmpty(types)) { + break; + } + log.info("存在止盈/止损/爆仓/委托平仓,等待 -- {}", i); + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + i++; + } + MemberWalletContractEntity wallet = memberWalletContractService.findWalletContractByMemberIdAndSymbol(entrustOrder.getMemberId(), CoinTypeEnum.USDT.name()); PlatformTradeSettingEntity tradeSettingEntity = cacheSettingUtils.getTradeSetting(); MemberEntity member = memberService.getById(entrustOrder.getMemberId()); @@ -659,6 +773,7 @@ holdOrderEntity.setOperateNo(1); // 设置合约类型 holdOrderEntity.setContractType(ContractOrderEntity.CONTRACTTYPE_NORMAL); + holdOrderEntity.setHoldBond(CalculateUtil.calMemberHoldBond(holdOrderEntity)); ContractOrderEntity contractOrderEntity = ContractHoldOrderEntityMapper.INSTANCE.holdOrderToOrder(holdOrderEntity); contractOrderEntity.setOpeningTime(new Date()); @@ -669,6 +784,8 @@ if (isSuccess) { contractEntrustOrderService.removeById(entrustOrder.getId()); memberWalletContractService.increaseWalletContractBalanceById(null, openFeePrice.negate(), entrustOrder.getBondAmount().negate(), wallet.getId()); + + ThreadPoolUtils.sendWholePrice(member.getId()); // 发送强平价 ThreadPoolUtils.sendWholeForceClosingPrice(entrustOrder.getSymbol(), member); @@ -723,6 +840,7 @@ * * @param */ + @Transactional(rollbackFor = Exception.class) public void dealCoinOut(List<ContractHoldOrderEntity> orderList, List<OrderModel> orderModels) { if (CollectionUtils.isNotEmpty(orderList)) { Map<Long, Integer> modelMap = new HashMap<Long, Integer>(); @@ -730,6 +848,11 @@ modelMap.put(model.getOrderId(), model.getOperateNo()); } for (ContractHoldOrderEntity coinsOrder : orderList) { + List<Object> types = redisUtils.lGet(AppContants.RABBIT_TYPE + coinsOrder.getId(), 0, -1); + if(types.contains(9) || types.contains(10) || types.contains(11) || types.contains(12)) { + log.info("存在止盈/止损 orderId : {}, types : {}", coinsOrder.getId(), JSONObject.toJSONString(types)); + continue; + } Long orderId = coinsOrder.getId(); Integer operateNo = coinsOrder.getOperateNo(); //判断当前订单是否是最新的爆仓价 不相等时直接进入下一个订单 @@ -738,69 +861,18 @@ contractHoldOrderService.updateOrderIsCanClosingAndBatchNoById(orderId); continue; } - boolean isDone = false; - Long memId = coinsOrder.getMemberId(); - BigDecimal nowPrice = coinsOrder.getForceClosingPrice(); - // 创建订单(加入历史表的订单) - ContractOrderEntity contractOrderEntity = ContractHoldOrderEntityMapper.INSTANCE.holdOrderToOrder(coinsOrder); - //查询是否满足爆仓条件 - if (coinsOrder.getOpeningType() == ContractHoldOrderEntity.OPENING_TYPE_MORE) { - //如果是开多,当前价小于预估强平价即为爆仓 - // 设置平仓类型 // 爆仓平多 - contractOrderEntity.setClosingType(4); - //更新用户钱包数据 - isDone = true; + // 仅逐仓有用 + if (ContractEntrustOrderEntity.POSITION_TYPE_ADD == coinsOrder.getPositionType()) { + bombOrder(coinsOrder); } else { - //如果是开空,当前价大于预估强平价即为爆仓 - contractOrderEntity.setClosingType(5); - //更新主表订单状态位为“已平仓” - isDone = true; + List<ContractHoldOrderEntity> holdOrderEntities = contractHoldOrderDao.selectHoldOrderListForWholeByMemberIdAndSymbol(coinsOrder.getMemberId(), coinsOrder.getSymbol()); + if (CollectionUtils.isNotEmpty(holdOrderEntities)) { + for (ContractHoldOrderEntity holdOrderEntity : holdOrderEntities) { + bombOrder(holdOrderEntity); + } - } - if (isDone) { - //删除次仓订单 - contractHoldOrderService.removeById(orderId); - // 订单状态转换 - if (ContractOrderEntity.ORDER_TYPE_OPEN_MORE == contractOrderEntity.getOrderType()) { - contractOrderEntity.setOrderType(ContractOrderEntity.ORDER_TYPE_CLOSE_MORE); - } else { - contractOrderEntity.setOrderType(ContractOrderEntity.ORDER_TYPE_CLOSE_LESS); - } - //更新主表订单状态位为“已平仓” - contractOrderEntity.setId(null); - contractOrderEntity.setClosingPrice(nowPrice); - contractOrderEntity.setClosingTime(new Date()); - contractOrderEntity.setClosingFeeAmount(coinsOrder.getOpeningFeeAmount()); - - if (coinsOrder.getPositionType() == ContractEntrustOrderEntity.POSITION_TYPE_ADD) { - contractOrderEntity.setRewardAmount(coinsOrder.getBondAmount().subtract(contractOrderEntity.getOpeningFeeAmount()).negate()); - contractOrderService.save(contractOrderEntity); - - //更新用户钱包数据 - MemberWalletContractEntity usdt = memberWalletContractService.findWalletContractByMemberIdAndSymbol(memId, "USDT"); - - // 减去的时候用负数 - BigDecimal totalPrice = coinsOrder.getBondAmount().negate(); - memberWalletContractService.increaseWalletContractBalanceById(null, totalPrice, null, usdt.getId()); - - // 流水记录 TODO - MemberAccountFlowEntity record = new MemberAccountFlowEntity(); - record.setCreateTime(new Date()); - record.setSource("系统自动平仓"); - record.setRemark("系统自动平仓"); - record.setBalance(usdt.getAvailableBalance()); - record.setMemberId(memId); - record.setSymbol(coinsOrder.getSymbol()); - record.setPrice(coinsOrder.getBondAmount()); - memberAccountFlowEntityDao.insert(record); - } else { - MemberWalletContractEntity wallet = memberWalletContractService.findWalletContractByMemberIdAndSymbol(memId, CoinTypeConvert.convertContractTypeToCoin(coinsOrder.getSymbol())); - - contractOrderEntity.setRewardAmount(wallet.getTotalBalance().subtract(coinsOrder.getOpeningFeeAmount())); - contractOrderService.save(contractOrderEntity); - - memberWalletContractService.increaseWalletContractBalanceById(wallet.getAvailableBalance(), wallet.getTotalBalance(), null, wallet.getId()); + MemberWalletContractEntity wallet = memberWalletContractService.findWalletContractByMemberIdAndSymbol(coinsOrder.getMemberId(), CoinTypeEnum.USDT.name()); // 流水记录 TODO MemberAccountFlowEntity record = new MemberAccountFlowEntity(); @@ -808,16 +880,143 @@ record.setSource("系统自动平仓"); record.setRemark("系统自动平仓"); record.setBalance(BigDecimal.ZERO); - record.setMemberId(memId); + record.setMemberId(coinsOrder.getMemberId()); record.setSymbol(coinsOrder.getSymbol()); record.setPrice(wallet.getTotalBalance().subtract(coinsOrder.getOpeningFeeAmount())); memberAccountFlowEntityDao.insert(record); } } + } } } + private void bombOrder(ContractHoldOrderEntity coinsOrder) { + Long orderId = coinsOrder.getId(); + boolean isDone = false; + Long memId = coinsOrder.getMemberId(); + BigDecimal nowPrice = coinsOrder.getForceClosingPrice(); + // 创建订单(加入历史表的订单) + ContractOrderEntity contractOrderEntity = ContractHoldOrderEntityMapper.INSTANCE.holdOrderToOrder(coinsOrder); + + //查询是否满足爆仓条件 + if (coinsOrder.getOpeningType() == ContractHoldOrderEntity.OPENING_TYPE_MORE) { + //如果是开多,当前价小于预估强平价即为爆仓 + // 设置平仓类型 // 爆仓平多 + contractOrderEntity.setClosingType(4); + //更新用户钱包数据 + isDone = true; + } else { + //如果是开空,当前价大于预估强平价即为爆仓 + contractOrderEntity.setClosingType(5); + //更新主表订单状态位为“已平仓” + isDone = true; + + } + if (isDone) { + //删除次仓订单 + contractHoldOrderService.removeById(orderId); + // 订单状态转换 + if (ContractOrderEntity.ORDER_TYPE_OPEN_MORE == contractOrderEntity.getOrderType()) { + contractOrderEntity.setOrderType(ContractOrderEntity.ORDER_TYPE_CLOSE_MORE); + } else { + contractOrderEntity.setOrderType(ContractOrderEntity.ORDER_TYPE_CLOSE_LESS); + } + //更新主表订单状态位为“已平仓” + contractOrderEntity.setId(null); + contractOrderEntity.setClosingPrice(nowPrice); + contractOrderEntity.setClosingTime(new Date()); + contractOrderEntity.setClosingFeeAmount(coinsOrder.getOpeningFeeAmount()); + + if (coinsOrder.getPositionType() == ContractEntrustOrderEntity.POSITION_TYPE_ADD) { + contractOrderEntity.setRewardAmount(coinsOrder.getBondAmount().subtract(contractOrderEntity.getOpeningFeeAmount()).negate()); + contractOrderService.save(contractOrderEntity); + + //更新用户钱包数据 + MemberWalletContractEntity usdt = memberWalletContractService.findWalletContractByMemberIdAndSymbol(memId, CoinTypeEnum.USDT.name()); + + // 减去的时候用负数 + BigDecimal totalPrice = coinsOrder.getBondAmount().negate(); + memberWalletContractService.increaseWalletContractBalanceById(null, totalPrice, null, usdt.getId()); + + // 流水记录 TODO + MemberAccountFlowEntity record = new MemberAccountFlowEntity(); + record.setCreateTime(new Date()); + record.setSource("系统自动平仓"); + record.setRemark("系统自动平仓"); + record.setBalance(usdt.getAvailableBalance()); + record.setMemberId(memId); + record.setSymbol(coinsOrder.getSymbol()); + record.setPrice(coinsOrder.getBondAmount()); + memberAccountFlowEntityDao.insert(record); + + MemberEntity memberEntity = memberService.getById(memId); + // 如果订单为交易员的订单,爆仓了,则旗下的所有跟单都进行平仓 + if (ContractOrderEntity.CONTRACTTYPE_DOCUMENTARY == coinsOrder.getContractType() && MemberEntity.IS_TRADER_Y.equals(memberEntity.getIsTrader())) { + followOrderOperationService.closingFollowOrders(coinsOrder.getOrderNo()); + } + // 如果不是交易员, + if (ContractOrderEntity.CONTRACTTYPE_DOCUMENTARY == coinsOrder.getContractType() && !MemberEntity.IS_TRADER_Y.equals(memberEntity.getIsTrader())) { + //更新他的个人跟随者收益记录 + QueryWrapper<FollowFollowerSettingEntity> queryWrapper = new QueryWrapper<>(); + queryWrapper.eq("member_id",coinsOrder.getMemberId()); + FollowFollowerSettingEntity followFollowerSettingEntity = followFollowerSettingDao.selectOne(queryWrapper); +// if(ObjectUtil.isNotEmpty(followFollowerSettingEntity)){ +// Long traderMemberId = followFollowerSettingEntity.getTraderMemberId(); +// followFollowerProfitDao.updateFollowerProfitByTradeMemberId(coinsOrder.getBondAmount().negate(), coinsOrder.getBondAmount().negate(), traderMemberId, memberEntity.getId()); +// } + FollowTraderInfoEntity followTraderInfoEntity = followTraderInfoDao.selectById(followFollowerSettingEntity.getTraderId()); + //更新跟随者-订单关联表 + updateFollowOrderRelation(coinsOrder.getId(), contractOrderEntity.getId()); + //增加爆仓消息提醒 + LogRecordUtils.insertFollowerNotice(memberEntity.getId(), NoticeConstant.BOMB_ORDER_TITLE, StrUtil.format(NoticeConstant.BOMB_ORDER_CONTENT, contractOrderEntity.getSymbol(),followTraderInfoEntity.getNickname())); + } + } else { + MemberWalletContractEntity wallet = memberWalletContractService.findWalletContractByMemberIdAndSymbol(memId, "USDT"); + +// BigDecimal rewardRatio = BigDecimal.ZERO; +// // 开多 +// if (ContractHoldOrderEntity.OPENING_TYPE_MORE == coinsOrder.getOpeningType()) { +// // (最新价-开仓价)*规格*张数 +// rewardRatio = nowPrice.subtract(coinsOrder.getOpeningPrice()).multiply(coinsOrder.getSymbolSku()).multiply(new BigDecimal(coinsOrder.getSymbolCntSale())); +// // 开空 +// } else { +// // (开仓价-最新价)*规格*张数 +// rewardRatio = coinsOrder.getOpeningPrice().subtract(nowPrice).multiply(coinsOrder.getSymbolSku()).multiply(new BigDecimal(coinsOrder.getSymbolCntSale())); +// } + + // 委托单总委托金额 + BigDecimal totalPrePayment = BigDecimal.ZERO; + List<ContractEntrustOrderEntity> entrustOrderEntities = contractEntrustOrderDao.selectEntrustOrderListByMemberId(memId); + if (CollUtil.isNotEmpty(entrustOrderEntities)) { + for (ContractEntrustOrderEntity entrustOrderEntity : entrustOrderEntities) { + totalPrePayment = totalPrePayment.add(entrustOrderEntity.getEntrustAmount()); + } + } + + contractOrderEntity.setRewardAmount(wallet.getTotalBalance().subtract(totalPrePayment).subtract(contractOrderEntity.getOpeningFeeAmount()).negate()); + contractOrderService.save(contractOrderEntity); + + memberWalletContractService.increaseWalletContractBalanceById(wallet.getAvailableBalance().negate(), wallet.getTotalBalance().subtract(totalPrePayment).negate(), null, wallet.getId()); + } + } + } + + /** + * 更新跟单订单关系 + * + * @param oldOrderId 当前持仓ID + * @param newOrderId 历史订单ID + */ + public void updateFollowOrderRelation(Long oldOrderId, Long newOrderId) { + FollowFollowerOrderRelationEntity orderRelationEntity = followFollowerOrderRelationDao.selectNowOneByorderId(oldOrderId); + if (orderRelationEntity != null) { + orderRelationEntity.setOrderId(newOrderId); + orderRelationEntity.setOrderType(FollowFollowerOrderRelationEntity.ORDER_TYPE_HISTORY); + followFollowerOrderRelationDao.updateById(orderRelationEntity); + } + } + public void calYj(Long mid, BigDecimal money, ContractOrderEntity order, int type) { PlatformTradeSettingEntity tradeSetting = cacheSettingUtils.getTradeSetting(); if (money != null) { -- Gitblit v1.9.1