xiaoyong931011
2021-03-30 c428522a1d47199b30acf4522a33c88f54180fdd
src/main/java/com/xcong/excoin/modules/documentary/service/impl/FollowOrderOperationServiceImpl.java
@@ -7,9 +7,11 @@
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.response.Result;
import com.xcong.excoin.common.system.service.CommonService;
import com.xcong.excoin.modules.coin.entity.MemberAccountMoneyChange;
import com.xcong.excoin.modules.contract.dao.ContractHoldOrderDao;
import com.xcong.excoin.modules.contract.dao.ContractOrderDao;
import com.xcong.excoin.modules.contract.entity.ContractEntrustOrderEntity;
@@ -18,10 +20,7 @@
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.FollowFollowerSettingDao;
import com.xcong.excoin.modules.documentary.dao.FollowTraderInfoDao;
import com.xcong.excoin.modules.documentary.dao.*;
import com.xcong.excoin.modules.documentary.entity.FollowFollowerOrderRelationEntity;
import com.xcong.excoin.modules.documentary.entity.FollowFollowerProfitEntity;
import com.xcong.excoin.modules.documentary.entity.FollowFollowerSettingEntity;
@@ -34,12 +33,14 @@
import com.xcong.excoin.modules.member.entity.*;
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.LogRecordUtils;
import com.xcong.excoin.utils.ThreadPoolUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -84,19 +85,25 @@
    @Resource
    private MemberSettingDao memberSettingDao;
    @Autowired
    private FollowProducer followProducer;
    @Autowired
    private FollowTraderProfitDetailDao followTraderProfitDetailDao;
    @Override
//    @Transactional(rollbackFor = Exception.class)
    @Transactional(rollbackFor = Exception.class)
    public void addFollowerOrder(Long id) {
        log.info("进入跟单处理逻辑 : {}", id);
        // 查询交易员订单
        ContractHoldOrderEntity holdOrderEntity = contractHoldOrderDao.selectById(id);
        while (holdOrderEntity == null) {
        if (holdOrderEntity == null) {
            try {
                log.info("等待事务提交:{}", id);
                Thread.sleep(200);
                holdOrderEntity = contractHoldOrderDao.selectById(id);
                followProducer.sendAddFollowOrder(id);
                return;
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
@@ -118,7 +125,7 @@
            for (FollowFollowerSettingEntity followerSetting : followerSettings) {
               //更新更新消息提醒的状态
                MemberSettingEntity memberSettingEntity = memberSettingDao.selectMemberSettingByMemberId(followerSetting.getId());
                MemberSettingEntity memberSettingEntity = memberSettingDao.selectMemberSettingByMemberId(followerSetting.getMemberId());
                if(ObjectUtil.isNotEmpty(memberSettingEntity)){
                   Long memberId = memberSettingEntity.getMemberId();
                   log.info("更新更新消息提醒的状态");
@@ -230,10 +237,10 @@
                    // 插入财务流水
                    if (holdOrderEntity.getOpeningType() == ContractHoldOrderEntity.OPENING_TYPE_MORE) {
                        LogRecordUtils.insertMemberAccountFlow(memberEntity.getId(), prePaymentAmount, walletContract.getAvailableBalance().subtract(prePaymentAmount), holdOrderEntity.getSymbol(), "买涨持仓", "买涨:" + holdOrderEntity.getSymbol());
                        LogRecordUtils.insertFollowerNotice(memberEntity.getId(), NoticeConstant.OPEN_ORDER_TITLE, StrUtil.format(NoticeConstant.OPEN_ORDER_CONTENT, holdOrderEntity.getSymbol() + "开多", openPrice, followTraderInfoEntity.getNickname()));
                        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()));
                    } else {
                        LogRecordUtils.insertMemberAccountFlow(memberEntity.getId(), prePaymentAmount, walletContract.getAvailableBalance().subtract(prePaymentAmount), holdOrderEntity.getSymbol(), "买跌持仓", "买跌:" + holdOrderEntity.getSymbol());
                        LogRecordUtils.insertFollowerNotice(memberEntity.getId(), NoticeConstant.OPEN_ORDER_TITLE, StrUtil.format(NoticeConstant.OPEN_ORDER_CONTENT, holdOrderEntity.getSymbol() + "开空", openPrice, followTraderInfoEntity.getNickname()));
                        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()));
                    }
                }
            }
@@ -257,11 +264,48 @@
    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);
                }
                //获取总返佣
                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);
                }
            }
        }
    }
}