Helius
2021-03-17 2bc7169b568195cfc5160bf88df6525e50afdc7c
src/main/java/com/xcong/excoin/modules/contract/service/impl/ContractHoldOrderServiceImpl.java
@@ -1,6 +1,8 @@
package com.xcong.excoin.modules.contract.service.impl;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.druid.sql.visitor.functions.If;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
@@ -37,11 +39,13 @@
import com.xcong.excoin.modules.member.entity.*;
import com.xcong.excoin.modules.platform.dao.TradeSettingDao;
import com.xcong.excoin.modules.platform.entity.PlatformTradeSettingEntity;
import com.xcong.excoin.rabbit.producer.FollowProducer;
import com.xcong.excoin.rabbit.producer.OrderProducer;
import com.xcong.excoin.utils.*;
import com.xcong.excoin.rabbit.pricequeue.OrderModel;
import jnr.a64asm.Mem;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sun.rmi.runtime.Log;
@@ -100,6 +104,9 @@
    private FollowTraderInfoDao followTraderInfoDao;
    @Resource
    private FollowFollowerOrderRelationDao followFollowerOrderRelationDao;
    @Autowired
    private FollowProducer followProducer;
    @Transactional(rollbackFor = Exception.class)
    @Override
@@ -320,7 +327,7 @@
        FollowTraderInfoEntity tradeInfo = null;
        if (MemberEntity.IS_TRADER_Y.equals(memberEntity.getIsTrader())) {
            tradeInfo = followTraderInfoDao.selectTraderInfoByMemberId(memberEntity.getId());
            if (tradeInfo.getIsOpen().equals(FollowTraderInfoEntity.ISOPEN_Y)) {
            if (FollowTraderInfoEntity.ISOPEN_Y.equals(tradeInfo.getIsOpen())) {
                isOpenFollow = true;
            }
        }
@@ -380,7 +387,8 @@
                relationEntity.setTradeOrderNo(holdOrderEntity.getOrderNo());
                followFollowerOrderRelationDao.insert(relationEntity);
                ThreadPoolUtils.sendFollowOrderTask(holdOrderEntity.getId());
                followProducer.sendAddFollowOrder(holdOrderEntity.getId());
//                ThreadPoolUtils.sendFollowOrderTask(holdOrderEntity.getId());
            }
            // 提交成功
            return Result.ok(MessageSourceUtils.getString("member_service_0024"));
@@ -506,7 +514,6 @@
                    canReduceMaxBond = BigDecimal.ZERO;
                }
                holdOrderListVo.setBondAmount(walletContractEntity.getTotalBalance());
                holdOrderListVo.setCanReduceMaxBond(canReduceMaxBond);
                holdOrderListVo.setCanAddMaxBond(walletContractEntity.getAvailableBalance());
                holdOrderListVo.setReturnRate(returnRate);
@@ -526,7 +533,7 @@
                BigDecimal totalEntrustAmount = BigDecimal.ZERO;
                if (CollUtil.isNotEmpty(entrustOrder)) {
                    for (ContractEntrustOrderEntity contractEntrustOrderEntity : entrustOrder) {
                        totalEntrustAmount.add(contractEntrustOrderEntity.getEntrustAmount());
                        totalEntrustAmount = totalEntrustAmount.add(contractEntrustOrderEntity.getEntrustAmount());
                    }
                }
                BigDecimal riskRatio = totalHoldBond.divide(walletContractEntity.getTotalBalance().add(totalProfitOrLoss).subtract(totalEntrustAmount), 4, BigDecimal.ROUND_DOWN).multiply(new BigDecimal(100));
@@ -825,6 +832,8 @@
        // 占用保证金 -- 即持仓单中的保证金之和
        BigDecimal beUsedBondAmount = BigDecimal.ZERO;
        BigDecimal moreBondAmount = BigDecimal.ZERO;
        BigDecimal lessBondAmount = BigDecimal.ZERO;
        // 总盈利
        BigDecimal totalProfitOrLess = BigDecimal.ZERO;
        if (CollUtil.isNotEmpty(holdOrderEntities)) {
@@ -838,9 +847,11 @@
                BigDecimal profitOrLess = BigDecimal.ZERO;
                // 开多
                if (ContractHoldOrderEntity.OPENING_TYPE_MORE == holdOrderEntity.getOpeningType()) {
                    moreBondAmount = moreBondAmount.add(holdOrderEntity.getBondAmount());
                    profitOrLess = newPrice.subtract(holdOrderEntity.getOpeningPrice()).multiply(new BigDecimal(holdOrderEntity.getSymbolCntSale())).multiply(lotNumber);
                    // 开空
                } else {
                    lessBondAmount = lessBondAmount.add(holdOrderEntity.getBondAmount());
                    profitOrLess = holdOrderEntity.getOpeningPrice().subtract(newPrice).multiply(new BigDecimal(holdOrderEntity.getSymbolCntSale())).multiply(lotNumber);
                }
@@ -887,6 +898,8 @@
        contractMoneyInfoVo.setUpOrDown(upOrDown);
        contractMoneyInfoVo.setSymbolSku(cacheSettingUtils.getSymbolSku(symbol));
        contractMoneyInfoVo.setLeverRate(rateEntity.getLevelRateUp());
        contractMoneyInfoVo.setMoreBondAmount(moreBondAmount);
        contractMoneyInfoVo.setLessBondAmount(lessBondAmount);
        return Result.ok(contractMoneyInfoVo);
    }
@@ -1017,8 +1030,27 @@
        List<ContractHoldOrderEntity> list = contractHoldOrderDao.selectHoldOrderListByMemberId(member.getId());
        List<ContractEntrustOrderEntity> entrustList = contractEntrustOrderDao.selectEntrustOrderListByMemberId(member.getId());
        if (CollUtil.isNotEmpty(list) || CollUtil.isNotEmpty(entrustList)) {
            return Result.fail("存在持仓/委托, 无法更改");
//        if (CollUtil.isNotEmpty(list) || CollUtil.isNotEmpty(entrustList)) {
//            return Result.fail("存在持仓/委托, 无法更改");
//        }
        if(CollUtil.isNotEmpty(list)) {
           for(ContractHoldOrderEntity contractHoldOrderEntity : list) {
              String symbol = contractHoldOrderEntity.getSymbol();
              if(StrUtil.isEmpty(symbol)) {
                 return Result.fail("存在持仓/委托, 无法更改");
              }
              return Result.fail("币种"+symbol+"存在持仓/委托, 无法更改");
           }
        }
        if(CollUtil.isNotEmpty(entrustList)) {
           for(ContractEntrustOrderEntity contractEntrustOrderEntity : entrustList) {
              String symbol = contractEntrustOrderEntity.getSymbol();
              if(StrUtil.isEmpty(symbol)) {
                 return Result.fail("存在持仓/委托, 无法更改");
              }
              return Result.fail("币种"+symbol+"存在持仓/委托, 无法更改");
           }
        }
        
        /**
@@ -1070,8 +1102,10 @@
                holdOrderEntity.setOperateNo(holdOrderEntity.getOperateNo() + 1);
                contractHoldOrderDao.updateById(holdOrderEntity);
                // 发送爆仓消息
                sendOrderBombMsg(holdOrderEntity.getId(), holdOrderEntity.getOpeningType(), newForcePrice, holdOrderEntity.getSymbol(), holdOrderEntity.getOperateNo(), holdOrderEntity.getMemberId());
                if (ContractEntrustOrderEntity.POSITION_TYPE_ADD == holdOrderEntity.getPositionType()) {
                    // 发送爆仓消息
                    sendOrderBombMsg(holdOrderEntity.getId(), holdOrderEntity.getOpeningType(), newForcePrice, holdOrderEntity.getSymbol(), holdOrderEntity.getOperateNo(), holdOrderEntity.getMemberId());
                }
            }
        }
    }