From 829f629756e5402bcd3e9f6ef1f9c6dbffbabb11 Mon Sep 17 00:00:00 2001 From: KKSU <15274802129@163.com> Date: Wed, 17 Apr 2024 14:46:29 +0800 Subject: [PATCH] 55测试环境 --- src/main/java/com/xcong/excoin/modules/documentary/service/impl/FollowOrderOperationServiceImpl.java | 215 +++++++++++++++++++++++++++++++++++++++++++---------- 1 files changed, 173 insertions(+), 42 deletions(-) diff --git a/src/main/java/com/xcong/excoin/modules/documentary/service/impl/FollowOrderOperationServiceImpl.java b/src/main/java/com/xcong/excoin/modules/documentary/service/impl/FollowOrderOperationServiceImpl.java index a081a3e..96d06e3 100644 --- a/src/main/java/com/xcong/excoin/modules/documentary/service/impl/FollowOrderOperationServiceImpl.java +++ b/src/main/java/com/xcong/excoin/modules/documentary/service/impl/FollowOrderOperationServiceImpl.java @@ -6,9 +6,11 @@ import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.Wrapper; 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.common.enumerates.MemberWalletCoinEnum; import com.xcong.excoin.common.enumerates.RabbitPriceTypeEnum; +import com.xcong.excoin.common.exception.GlobalException; import com.xcong.excoin.common.response.Result; import com.xcong.excoin.common.system.service.CommonService; import com.xcong.excoin.modules.coin.entity.MemberAccountMoneyChange; @@ -18,6 +20,7 @@ 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.parameter.dto.ChangeBondDto; import com.xcong.excoin.modules.contract.service.RabbitOrderService; import com.xcong.excoin.modules.documentary.common.NoticeConstant; import com.xcong.excoin.modules.documentary.dao.*; @@ -35,13 +38,11 @@ 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.LogRecordUtils; -import com.xcong.excoin.utils.ThreadPoolUtils; +import com.xcong.excoin.utils.*; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Isolation; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; @@ -89,10 +90,11 @@ private FollowProducer followProducer; @Autowired private FollowTraderProfitDetailDao followTraderProfitDetailDao; - + @Resource + private RedisUtils redisUtils; @Override - @Transactional(rollbackFor = Exception.class) + @Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED) public void addFollowerOrder(Long id) { log.info("进入跟单处理逻辑 : {}", id); // 查询交易员订单 @@ -122,9 +124,33 @@ BigDecimal lotNumber = cacheSettingUtils.getSymbolSku(holdOrderEntity.getSymbol()); Long tradeMemberId = holdOrderEntity.getMemberId(); if (CollUtil.isNotEmpty(followerSettings)) { + List<Object> hasExist = new ArrayList<>(); for (FollowFollowerSettingEntity followerSetting : followerSettings) { + // 加redis锁,同一个用户不能同时触发两个跟单任务,否则会出现金额问题 + while (true) { + boolean flag = redisUtils.setNotExist(AppContants.MEMBER_HAS_FOLLOW + "_" + followerSetting.getMemberId(), "1", 5); +// List<Object> followerMemberId = redisUtils.lGet(AppContants.MEMBER_HAS_FOLLOW, 0, -1); +// log.info("#跟单用户任务已存在:{}, 当前:{}#", followerMemberId, followerSetting.getMemberId()); +// log.info("#------->{}#", followerMemberId.contains(followerSetting.getMemberId().intValue())); +// if (CollUtil.isEmpty(followerMemberId) || !followerMemberId.contains(followerSetting.getMemberId().intValue())) { +// log.info("跳出"); +// hasExist.add(followerSetting.getMemberId()); +// redisUtils.lSet(AppContants.MEMBER_HAS_FOLLOW, hasExist); +// break; +// } - //更新更新消息提醒的状态 + if (flag) { + log.info("跳出"); + break; + } + try { + Thread.sleep(500); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + //更新更新消息提醒的状态 MemberSettingEntity memberSettingEntity = memberSettingDao.selectMemberSettingByMemberId(followerSetting.getMemberId()); if(ObjectUtil.isNotEmpty(memberSettingEntity)){ Long memberId = memberSettingEntity.getMemberId(); @@ -180,9 +206,38 @@ // 预付款 BigDecimal prePaymentAmount = bondAmount.add(openFeePrice).add(openFeePrice); + log.info("可用的余额:{}, {}", prePaymentAmount, walletContract.getAvailableBalance()); if (prePaymentAmount.compareTo(walletContract.getAvailableBalance()) > -1) { log.info("可用金额不足"); LogRecordUtils.insertFollowerNotice(followerSetting.getMemberId(), NoticeConstant.MONEY_NOT_ENOUGH_TITLE, StrUtil.format(NoticeConstant.MONEY_NOT_ENOUGH_CONTENT, followTraderInfoEntity.getNickname())); + continue; + } + + boolean flag = false; + while(true) { + MemberWalletContractEntity updateEntity = new MemberWalletContractEntity(); + updateEntity.setAvailableBalance(prePaymentAmount.negate()); + updateEntity.setTotalBalance(openFeePrice.negate()); + updateEntity.setId(walletContract.getId()); + updateEntity.setVersion(walletContract.getVersion()); + log.info("==={}, {}===", walletContract.getAvailableBalance(), walletContract.getVersion()); + int i = memberWalletContractDao.updateWalletContractWithVersion(updateEntity); + if (i > 0) { + break; + } + + walletContract = memberWalletContractDao.findWalletContractByMemberIdAndSymbol(followerSetting.getMemberId(), CoinTypeEnum.USDT.name()); + if (prePaymentAmount.compareTo(walletContract.getAvailableBalance()) > -1) { + log.info("可用金额不足"); + LogRecordUtils.insertFollowerNotice(followerSetting.getMemberId(), NoticeConstant.MONEY_NOT_ENOUGH_TITLE, StrUtil.format(NoticeConstant.MONEY_NOT_ENOUGH_CONTENT, followTraderInfoEntity.getNickname())); + flag = true; + break; + } + + log.info("---{}, {}--", walletContract.getAvailableBalance(), walletContract.getVersion()); + } + + if (flag) { continue; } @@ -216,7 +271,7 @@ contractHoldOrderDao.insert(followHoldOrder); int i = contractOrderDao.insert(contractOrderEntity); if (i > 0) { - memberWalletContractDao.increaseWalletContractBalanceById(prePaymentAmount.negate(), openFeePrice.negate(), null, walletContract.getId()); +// memberWalletContractDao.increaseWalletContractBalanceById(prePaymentAmount.negate(), openFeePrice.negate(), null, walletContract.getId()); FollowFollowerOrderRelationEntity relationEntity = new FollowFollowerOrderRelationEntity(); relationEntity.setIsShow(FollowFollowerOrderRelationEntity.IS_SHOW_Y); @@ -243,7 +298,9 @@ LogRecordUtils.insertFollowerNotice(memberEntity.getId(), NoticeConstant.OPEN_ORDER_TITLE, StrUtil.format(NoticeConstant.OPEN_ORDER_CONTENT, holdOrderEntity.getSymbol() + "开空", openPrice.setScale(2, BigDecimal.ROUND_HALF_UP).toString(), followTraderInfoEntity.getNickname())); } } + redisUtils.del(AppContants.MEMBER_HAS_FOLLOW + "_" + followerSetting.getMemberId()); } +// redisUtils.del(AppContants.MEMBER_HAS_FOLLOW); } } @@ -264,46 +321,120 @@ public void closingFollowOrders(String orderNo) { List<FollowFollowerOrderRelationEntity> orderRelations = followFollowerOrderRelationDao.selectFollowHoldOrderByTradeOrderNo(orderNo); if (CollUtil.isNotEmpty(orderRelations)) { - List<Long> orderIds = new ArrayList<>(); for (FollowFollowerOrderRelationEntity orderRelation : orderRelations) { List<Long> ids= new ArrayList<>(); ids.add(orderRelation.getOrderId()); - orderIds.add(orderRelation.getOrderId()); rabbitOrderService.cancelHoldOrder(ids); } - if(CollUtil.isNotEmpty(orderIds)){ - //获取对应的平仓记录单号 - List<String> orderNosList = new ArrayList<>(); - - for(Long orderId : orderIds) { - String orderNos = contractOrderDao.selectOrderNoByOrderIds(orderId); - ContractOrderEntity contractOrderEntity = contractOrderDao.selectById(orderId); - orderNosList.add(orderNos); + //生成返利记录和资金变化记录 + List<FollowFollowerOrderRelationEntity> orderRelationDone = followFollowerOrderRelationDao.selectFollowOrderByTradeOrderNo(orderNo); + if(CollUtil.isNotEmpty(orderRelationDone)) { + List<Long> orderIds = new ArrayList<>(); + for (FollowFollowerOrderRelationEntity orderRelationd : orderRelationDone) { + orderIds.add(orderRelationd.getOrderId()); } - //获取总返佣 - BigDecimal totalAmount = followTraderProfitDetailDao.selectFollowHoldOrderByFollowOrderNo(orderNosList); - totalAmount = (totalAmount == null?BigDecimal.ZERO:totalAmount.setScale(2, BigDecimal.ROUND_DOWN)); - if(totalAmount.compareTo(BigDecimal.ZERO) > 0){ - //增加返佣提醒 - String symbol = contractOrderDao.selectById(orderIds.get(0)).getSymbol(); - String orderNoTrader = orderRelations.get(0).getTradeOrderNo(); - Long traderMemberId = orderRelations.get(0).getTradeMemberId(); - LogRecordUtils.insertFollowerNotice(traderMemberId, - NoticeConstant.RETURN_MONEY_TITLE, - StrUtil.format(NoticeConstant.RETURN_MONEY_CONTENT, - orderNoTrader, - symbol, - totalAmount)); - //带单返利的记录要在资产页面的其他记录 - LogRecordUtils.insertMemberAccountMoneyChange( - traderMemberId, - StrUtil.format(NoticeConstant.RETURN_MONEY_CONTENT_MAMC, - orderNoTrader, - symbol), - totalAmount, - MemberWalletCoinEnum.WALLETCOINCODE.getValue(), - MemberAccountMoneyChange.STATUS_SUCCESS_INTEGER, - MemberAccountMoneyChange.TYPE_WALLET_AGENT); + + if(CollUtil.isNotEmpty(orderIds)){ + //获取对应的平仓记录单号 + List<String> orderNosList = new ArrayList<>(); + for(Long orderId : orderIds) { + String orderNos = contractOrderDao.selectOrderNoByOrderIds(orderId); + orderNosList.add(orderNos); + } + //获取总返佣 + BigDecimal totalAmount = followTraderProfitDetailDao.selectFollowHoldOrderByFollowOrderNo(orderNosList); + totalAmount = (totalAmount == null?BigDecimal.ZERO:totalAmount.setScale(2, BigDecimal.ROUND_DOWN)); + if(totalAmount.compareTo(BigDecimal.ZERO) > 0){ + //增加返佣提醒 + String symbol = contractOrderDao.selectById(orderIds.get(0)).getSymbol(); + String orderNoTrader = orderRelationDone.get(0).getTradeOrderNo(); + Long traderMemberId = orderRelationDone.get(0).getTradeMemberId(); + LogRecordUtils.insertFollowerNotice(traderMemberId, + NoticeConstant.RETURN_MONEY_TITLE, + StrUtil.format(NoticeConstant.RETURN_MONEY_CONTENT, + orderNoTrader, + symbol, + totalAmount)); + //带单返利的记录要在资产页面的其他记录 + LogRecordUtils.insertMemberAccountMoneyChange( + traderMemberId, + StrUtil.format(NoticeConstant.RETURN_MONEY_CONTENT_MAMC, + symbol, + orderNoTrader.substring(8)), + totalAmount, + MemberWalletCoinEnum.WALLETCOINCODE.getValue(), + MemberAccountMoneyChange.STATUS_SUCCESS_INTEGER, + MemberAccountMoneyChange.TYPE_WALLET_AGENT); + } + } + } + } + } + + @Override + @Transactional(rollbackFor = Exception.class) + public void changeFollowOrdersBond(Long id, BigDecimal bond, Integer type) { + log.info("==跟单调整保证金:{}, {}, {}==", id, bond, type); + ContractHoldOrderEntity contractHoldOrderEntity = contractHoldOrderDao.selectById(id); + if (contractHoldOrderEntity == null) { + log.info("持仓不存在:{}", id); + return; + } + + // 交易员信息 + FollowTraderInfoEntity followTraderInfoEntity = followTraderInfoDao.selectTraderInfoByMemberId(contractHoldOrderEntity.getMemberId()); + + List<FollowFollowerOrderRelationEntity> relations = followFollowerOrderRelationDao.selectFollowHoldOrderByTradeOrderNo(contractHoldOrderEntity.getOrderNo()); + if (CollUtil.isNotEmpty(relations)) { + for (FollowFollowerOrderRelationEntity relation : relations) { + if (contractHoldOrderEntity.getId().equals(relation.getOrderId())) { + continue; + } + ContractHoldOrderEntity followHoldOrder = contractHoldOrderDao.selectById(relation.getOrderId()); + MemberEntity memberEntity = memberDao.selectById(relation.getMemberId()); + MemberWalletContractEntity wallet = memberWalletContractDao.findWalletContractByMemberIdAndSymbol(memberEntity.getId(), CoinTypeEnum.USDT.name()); + + // 增加保证金 + if (type == ChangeBondDto.TYPE_ADD) { + if (bond.compareTo(wallet.getAvailableBalance()) > 0) { + log.info("可用金额不足:{}, {}", memberEntity.getId(), followHoldOrder.getOrderNo()); + LogRecordUtils.insertFollowerNotice(memberEntity.getId(), NoticeConstant.CHANGE_BOND_TITLE, NoticeConstant.CHANGE_BOND_ADD_CONTENT_FAIL); + continue; + } + + memberWalletContractDao.increaseWalletContractBalanceById(bond.negate(), null, null, wallet.getId()); + followHoldOrder.setBondAmount(followHoldOrder.getBondAmount().add(bond)); + LogRecordUtils.insertFollowerNotice(memberEntity.getId(), NoticeConstant.CHANGE_BOND_TITLE, StrUtil.format(NoticeConstant.CHANGE_BOND_CONTENT_SUCCESS, bond, followTraderInfoEntity.getNickname())); + } else { + if (followHoldOrder.getBondAmount().subtract(followHoldOrder.getPrePaymentAmount()).subtract(bond).compareTo(BigDecimal.ZERO) < 0) { + log.info("超出保证金最大减少金额-1"); + LogRecordUtils.insertFollowerNotice(memberEntity.getId(), NoticeConstant.CHANGE_BOND_TITLE, NoticeConstant.CHANGE_BOND_REDUCE_CONTENT_FAIL); + continue; + } + + BigDecimal profitOrLoss = CalculateUtil.calProfitOrLoss(followHoldOrder, memberEntity); + if (profitOrLoss.compareTo(BigDecimal.ZERO) < 0) { + BigDecimal canReduceMax = followHoldOrder.getBondAmount().subtract(followHoldOrder.getPrePaymentAmount()).add(profitOrLoss); + if (canReduceMax.subtract(bond).compareTo(BigDecimal.ZERO) < 0) { + log.info("超出保证金最大减少金额-2"); + LogRecordUtils.insertFollowerNotice(memberEntity.getId(), NoticeConstant.CHANGE_BOND_TITLE, NoticeConstant.CHANGE_BOND_REDUCE_CONTENT_FAIL); + continue; + } + } + + memberWalletContractDao.increaseWalletContractBalanceById(bond, null, null, wallet.getId()); + followHoldOrder.setBondAmount(followHoldOrder.getBondAmount().subtract(bond)); + LogRecordUtils.insertFollowerNotice(memberEntity.getId(), NoticeConstant.CHANGE_BOND_TITLE, StrUtil.format(NoticeConstant.CHANGE_BOND_CONTENT_SUCCESS, bond.negate(), followTraderInfoEntity.getNickname())); + } + + BigDecimal forceClosingPrice = CalculateUtil.getForceSetPrice(followHoldOrder.getBondAmount(), followHoldOrder.getOpeningPrice(), followHoldOrder.getSymbolCnt(), followHoldOrder.getSymbolSku(), followHoldOrder.getOpeningType(), memberEntity); + followHoldOrder.setForceClosingPrice(forceClosingPrice); + followHoldOrder.setOperateNo(followHoldOrder.getOperateNo() + 1); + int i = contractHoldOrderDao.updateById(followHoldOrder); + if (i > 0) { + sendOrderBombMsg(followHoldOrder.getId(), followHoldOrder.getOpeningType(), forceClosingPrice, followHoldOrder.getSymbol(), followHoldOrder.getOperateNo(), followHoldOrder.getMemberId()); + } else { + throw new GlobalException("更新失败"); } } } -- Gitblit v1.9.1