| | |
| | | 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.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; |
| | |
| | | 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.*; |
| | | import lombok.extern.slf4j.Slf4j; |
| | |
| | | MemberWalletContractService memberWalletContractService; |
| | | |
| | | @Resource |
| | | FollowFollowerProfitDao followFollowerProfitDao; |
| | | |
| | | @Resource |
| | | FollowFollowerSettingDao followFollowerSettingDao; |
| | | @Resource |
| | | FollowFollowerOrderRelationDao followFollowerOrderRelationDao; |
| | | @Resource |
| | | FollowTraderInfoDao followTraderInfoDao; |
| | | |
| | | @Resource |
| | | CacheSettingUtils cacheSettingUtils; |
| | | |
| | | @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)) { |
| | |
| | | } |
| | | } |
| | | |
| | | 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())); |
| | | } |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 开多止盈 |
| | |
| | | contractOrderEntity.setId(null); |
| | | |
| | | // 本次平仓数量 |
| | | int currentFlat = order.getSymbolCnt(); |
| | | int currentFlat = order.getSymbolCntSale(); |
| | | BigDecimal symbolSku = cacheSettingUtils.getSymbolSku(order.getSymbol()); |
| | | // 盈亏额度= (当前的币种的平仓价-下单时的建仓价)*购买的手数/规格*倍率 |
| | | BigDecimal profitLossPrice = (closePrice |
| | |
| | | |
| | | // 全仓模式,止盈/止损 删除委托平仓 |
| | | if (ContractEntrustOrderEntity.POSITION_TYPE_ALL == order.getPositionType()) { |
| | | ThreadPoolUtils.sendWholePrice(memberId); |
| | | deleteEntrustCloseOrder(order.getOrderNo()); |
| | | } else { |
| | | closingFollowerOrders(memberEntity, order, contractOrderEntity); |
| | | } |
| | | } |
| | | } |
| | |
| | | contractOrderEntity.setId(null); |
| | | |
| | | // 本次平仓数量 |
| | | int currentFlat = order.getSymbolCnt(); |
| | | int currentFlat = order.getSymbolCntSale(); |
| | | BigDecimal symbolSku = cacheSettingUtils.getSymbolSku(order.getSymbol()); |
| | | // 盈亏额度= (当前的币种的平仓价-下单时的建仓价)*购买的手数/规格*倍率 |
| | | BigDecimal profitLossPrice = (order.getOpeningPrice() |
| | |
| | | |
| | | // 全仓模式,止盈/止损 删除委托平仓 |
| | | if (ContractEntrustOrderEntity.POSITION_TYPE_ALL == order.getPositionType()) { |
| | | ThreadPoolUtils.sendWholePrice(memberId); |
| | | deleteEntrustCloseOrder(order.getOrderNo()); |
| | | } else { |
| | | closingFollowerOrders(memberEntity, order, contractOrderEntity); |
| | | } |
| | | } |
| | | } |
| | |
| | | contractOrderEntity.setId(null); |
| | | |
| | | // 本次平仓数量 |
| | | int currentFlat = order.getSymbolCnt(); |
| | | int currentFlat = order.getSymbolCntSale(); |
| | | BigDecimal symbolSku = cacheSettingUtils.getSymbolSku(order.getSymbol()); |
| | | // 盈亏额度= (当前的币种的平仓价-下单时的建仓价)*购买的手数/规格*倍率 |
| | | BigDecimal profitLossPrice = (closePrice |
| | |
| | | |
| | | // 全仓模式,止盈/止损 删除委托平仓 |
| | | if (ContractEntrustOrderEntity.POSITION_TYPE_ALL == order.getPositionType()) { |
| | | ThreadPoolUtils.sendWholePrice(memberId); |
| | | deleteEntrustCloseOrder(order.getOrderNo()); |
| | | } else { |
| | | closingFollowerOrders(memberEntity, order, contractOrderEntity); |
| | | } |
| | | } |
| | | } |
| | |
| | | contractOrderEntity.setId(null); |
| | | |
| | | // 本次平仓数量 |
| | | int currentFlat = order.getSymbolCnt(); |
| | | int currentFlat = order.getSymbolCntSale(); |
| | | BigDecimal symbolSku = cacheSettingUtils.getSymbolSku(order.getSymbol()); |
| | | // 盈亏额度= (当前的币种的平仓价-下单时的建仓价)*购买的手数/规格*倍率 |
| | | BigDecimal profitLossPrice = (order.getOpeningPrice() |
| | |
| | | |
| | | // 全仓模式,止盈/止损 删除委托平仓 |
| | | if (ContractEntrustOrderEntity.POSITION_TYPE_ALL == order.getPositionType()) { |
| | | ThreadPoolUtils.sendWholePrice(memberId); |
| | | deleteEntrustCloseOrder(order.getOrderNo()); |
| | | } else { |
| | | closingFollowerOrders(memberEntity, order, contractOrderEntity); |
| | | } |
| | | } |
| | | } |
| | |
| | | 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); |
| | |
| | | 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); |
| | | |
| | | // 需要一个历史插入 |
| | |
| | | |
| | | //返佣 |
| | | ThreadPoolUtils.calReturnMoney(memberEntity.getId(), openFeePrice, contractOrderEntity, AgentReturnEntity.ORDER_TYPE_OPEN); |
| | | |
| | | // 若该用户为交易员且开启带单模式,则发送带单异步 |
| | | if (isOpenFollow) { |
| | | contractHoldOrderService.sendFollowOrder(tradeInfo, contractHoldOrderEntity); |
| | | } |
| | | |
| | | } |
| | | } |
| | | } |
| | |
| | | holdOrderEntity.setOperateNo(1); |
| | | // 设置合约类型 |
| | | holdOrderEntity.setContractType(ContractOrderEntity.CONTRACTTYPE_NORMAL); |
| | | holdOrderEntity.setHoldBond(CalculateUtil.calMemberHoldBond(holdOrderEntity)); |
| | | |
| | | ContractOrderEntity contractOrderEntity = ContractHoldOrderEntityMapper.INSTANCE.holdOrderToOrder(holdOrderEntity); |
| | | contractOrderEntity.setOpeningTime(new Date()); |
| | |
| | | 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); |
| | |
| | | * |
| | | * @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>(); |
| | |
| | | continue; |
| | | } |
| | | |
| | | // 仅逐仓有用 |
| | | if (ContractEntrustOrderEntity.POSITION_TYPE_ADD == coinsOrder.getPositionType()) { |
| | | bombOrder(coinsOrder); |
| | | } else { |
| | |
| | | bombOrder(holdOrderEntity); |
| | | } |
| | | |
| | | MemberWalletContractEntity wallet = memberWalletContractService.findWalletContractByMemberIdAndSymbol(coinsOrder.getMemberId(), CoinTypeConvert.convertContractTypeToCoin(coinsOrder.getSymbol())); |
| | | MemberWalletContractEntity wallet = memberWalletContractService.findWalletContractByMemberIdAndSymbol(coinsOrder.getMemberId(), CoinTypeEnum.USDT.name()); |
| | | |
| | | // 流水记录 TODO |
| | | MemberAccountFlowEntity record = new MemberAccountFlowEntity(); |
| | |
| | | contractOrderService.save(contractOrderEntity); |
| | | |
| | | //更新用户钱包数据 |
| | | MemberWalletContractEntity usdt = memberWalletContractService.findWalletContractByMemberIdAndSymbol(memId, "USDT"); |
| | | MemberWalletContractEntity usdt = memberWalletContractService.findWalletContractByMemberIdAndSymbol(memId, CoinTypeEnum.USDT.name()); |
| | | |
| | | // 减去的时候用负数 |
| | | BigDecimal totalPrice = coinsOrder.getBondAmount().negate(); |
| | |
| | | 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, CoinTypeConvert.convertContractTypeToCoin(coinsOrder.getSymbol())); |
| | | MemberWalletContractEntity wallet = memberWalletContractService.findWalletContractByMemberIdAndSymbol(memId, "USDT"); |
| | | |
| | | // BigDecimal rewardRatio = BigDecimal.ZERO; |
| | | // // 开多 |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 更新跟单订单关系 |
| | | * |
| | | * @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) { |