Helius
2020-09-04 83fbe25f0672a1052b1508d7050f44c1383862b1
modify
7 files modified
133 ■■■■■ changed files
src/main/java/com/xcong/excoin/modules/contract/dao/ContractHoldOrderDao.java 1 ●●●● patch | view | raw | blame | history
src/main/java/com/xcong/excoin/modules/contract/service/impl/RabbitOrderServiceImpl.java 45 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/excoin/quartz/job/NewestPriceUpdateJob.java 1 ●●●● patch | view | raw | blame | history
src/main/java/com/xcong/excoin/rabbit/consumer/WebsocketPriceConsumer.java 2 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/excoin/rabbit/pricequeue/WebsocketPriceService.java 53 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/excoin/utils/CalculateUtil.java 25 ●●●●● patch | view | raw | blame | history
src/main/resources/mapper/contract/ContractHoldOrderDao.xml 6 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/excoin/modules/contract/dao/ContractHoldOrderDao.java
@@ -59,4 +59,5 @@
    public int updateForcePriceBySymbolAndMemberId(@Param("forcePrice") BigDecimal forcePrice, @Param("memberId") Long memberId, @Param("symbol") String symbol);
    public List<Long> selectMemberHasWholeOrder();
}
src/main/java/com/xcong/excoin/modules/contract/service/impl/RabbitOrderServiceImpl.java
@@ -7,6 +7,7 @@
import com.xcong.excoin.common.enumerates.CoinTypeEnum;
import com.xcong.excoin.common.enumerates.OrderClosingTypeEnum;
import com.xcong.excoin.common.system.service.CommonService;
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.dao.ContractOrderDao;
@@ -108,7 +109,7 @@
                        }
                    }
                }
            }catch (Exception e) {
            } catch (Exception e) {
                log.error("平仓异常", e);
            }
@@ -390,7 +391,7 @@
        ContractHoldOrderEntity holdOrderEntity = contractHoldOrderDao.selectWholeHoldOrderByOrderType(memberId, orderType, symbol);
        List<Object> types = redisUtils.lGet(AppContants.RABBIT_TYPE + holdOrderEntity.getId(), 0, -1);
        if(types.contains(9) || types.contains(10) || types.contains(11) || types.contains(12)) {
        if (types.contains(9) || types.contains(10) || types.contains(11) || types.contains(12)) {
            log.info("存在止盈/止损 orderId : {}, types : {}", holdOrderEntity.getId(), JSONObject.toJSONString(types));
            return;
        }
@@ -471,16 +472,17 @@
    @Override
    public void wholeBombOrder(List<OrderModel> list) {
        for (OrderModel orderModel : list) {
            MemberEntity memberEntity = memberDao.selectById(orderModel.getMemberId());
            Long memberId = memberEntity.getId();
            List<ContractHoldOrderEntity> holdOrderEntities = contractHoldOrderDao.selectHoldOrderListForWholeByMemberIdAndSymbol(memberId, null);
            MemberWalletContractEntity wallet = memberWalletContractDao.findWalletContractByMemberIdAndSymbol(memberId, CoinTypeEnum.USDT.name());
            BigDecimal currentPrice = new BigDecimal(orderModel.getPrice());
            if (CollUtil.isNotEmpty(holdOrderEntities)) {
                PlatformTradeSettingEntity tradeSetting = cacheSettingUtils.getTradeSetting();
                for (ContractHoldOrderEntity holdOrderEntity : holdOrderEntities) {
                    // 删除次仓订单
                    contractHoldOrderDao.deleteById(holdOrderEntity.getId());
                    BigDecimal lotNumber = cacheSettingUtils.getSymbolSku(holdOrderEntity.getSymbol());
                    // 单个订单盈利
@@ -502,7 +504,42 @@
                    }
                    ContractOrderEntity contractOrderEntity = ContractHoldOrderEntityMapper.INSTANCE.holdOrderToOrder(holdOrderEntity);
                    // 查询是否满足爆仓条件
                    if (holdOrderEntity.getOpeningType() == ContractHoldOrderEntity.OPENING_TYPE_MORE) {
                        contractOrderEntity.setClosingType(4);
                    } else {
                        contractOrderEntity.setClosingType(5);
                    }
                    // 盈亏比例(回报率)
                    BigDecimal rewardRatio = profitOrLess.divide(holdOrderEntity.getBondAmount().subtract(holdOrderEntity.getOpeningFeeAmount()), 8, BigDecimal.ROUND_DOWN);
                    contractOrderEntity.setId(null);
                    contractOrderEntity.setRewardRatio(rewardRatio);
                    contractOrderEntity.setRewardAmount(profitOrLess);
                    contractOrderEntity.setClosingPrice(currentPrice);
                    // 订单状态转换
                    if (ContractOrderEntity.ORDER_TYPE_OPEN_MORE == contractOrderEntity.getOrderType()) {
                        contractOrderEntity.setOrderType(ContractOrderEntity.ORDER_TYPE_CLOSE_MORE);
                    } else {
                        contractOrderEntity.setOrderType(ContractOrderEntity.ORDER_TYPE_CLOSE_LESS);
                    }
                    contractOrderEntity.setClosingTime(new Date());
                    contractOrderEntity.setClosingFeeAmount(holdOrderEntity.getOpeningFeeAmount());
                    contractOrderDao.insert(contractOrderEntity);
                }
                List<ContractEntrustOrderEntity> entrustOrder = contractEntrustOrderDao.selectEntrustOrderListByMemberId(memberId);
                BigDecimal totalAmount = BigDecimal.ZERO;
                if (CollUtil.isNotEmpty(entrustOrder)) {
                    for (ContractEntrustOrderEntity contractEntrustOrderEntity : entrustOrder) {
                        totalAmount.add(contractEntrustOrderEntity.getEntrustAmount());
                    }
                }
                memberWalletContractDao.increaseWalletContractBalanceById(BigDecimal.ZERO, wallet.getTotalBalance().subtract(totalAmount), null, wallet.getId());
            } else {
                log.info("无当前持仓");
            }
src/main/java/com/xcong/excoin/quartz/job/NewestPriceUpdateJob.java
@@ -54,6 +54,7 @@
                // 比较
                websocketPriceService.comparePriceAsc(symbol, price);
                websocketPriceService.comparePriceDesc(symbol, price);
//                websocketPriceService.wholeBomb(symbol, price);
                //System.out.println("比较完毕:"+symbol+"-"+price);
            }
src/main/java/com/xcong/excoin/rabbit/consumer/WebsocketPriceConsumer.java
@@ -170,5 +170,7 @@
    public void onMessageWholeBomb(Message message, Channel channel) {
        String content = new String(message.getBody());
        log.info("==message-price-consumer==我收到消息了全仓爆仓: {}", content);
        List<OrderModel> list = JSONArray.parseArray(content, OrderModel.class);
        orderService.wholeBombOrder(list);
    }
}
src/main/java/com/xcong/excoin/rabbit/pricequeue/WebsocketPriceService.java
@@ -3,14 +3,25 @@
import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSONObject;
import com.xcong.excoin.common.contants.AppContants;
import com.xcong.excoin.common.enumerates.CoinTypeEnum;
import com.xcong.excoin.modules.contract.dao.ContractHoldOrderDao;
import com.xcong.excoin.modules.contract.entity.ContractHoldOrderEntity;
import com.xcong.excoin.modules.member.dao.MemberDao;
import com.xcong.excoin.modules.member.dao.MemberWalletContractDao;
import com.xcong.excoin.modules.member.entity.MemberEntity;
import com.xcong.excoin.modules.member.entity.MemberWalletContractEntity;
import com.xcong.excoin.rabbit.producer.OrderProducer;
import com.xcong.excoin.utils.CacheSettingUtils;
import com.xcong.excoin.utils.CalculateUtil;
import com.xcong.excoin.utils.RedisUtils;
import com.xcong.excoin.utils.SpringContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.PriorityBlockingQueue;
@@ -22,7 +33,12 @@
    OrderProducer orderProducer;
    @Resource
    private RedisUtils redisUtils;
    @Resource
    private ContractHoldOrderDao contractHoldOrderDao;
    @Resource
    private MemberDao memberDao;
    @Resource
    private MemberWalletContractDao memberWalletContractDao;
    /**
     * @param symbol
     * @param price
@@ -78,7 +94,7 @@
    }
    private void addExecType(OrderModel model) {
        List<Object> orderTypes = redisUtils.lGet(AppContants.RABBIT_TYPE + model.getOrderId(), 0 , -1);
        List<Object> orderTypes = redisUtils.lGet(AppContants.RABBIT_TYPE + model.getOrderId(), 0, -1);
        if (CollUtil.isNotEmpty(orderTypes)) {
            orderTypes.add(model.getType());
        } else {
@@ -264,4 +280,37 @@
        }
    }
    public void wholeBomb(String symbol, String price) {
        List<Long> memberIds = contractHoldOrderDao.selectMemberHasWholeOrder();
        CacheSettingUtils cacheSettingUtils = SpringContextHolder.getBean(CacheSettingUtils.class);
        if (CollUtil.isNotEmpty(memberIds)) {
            for (Long memberId : memberIds) {
                List<ContractHoldOrderEntity> holdOrderEntities = contractHoldOrderDao.selectHoldOrderListByMemberId(memberId);
                MemberEntity memberEntity = memberDao.selectById(memberId);
                if (CollUtil.isNotEmpty(holdOrderEntities)) {
                    BigDecimal totalProfitOrLess = BigDecimal.ZERO;
                    for (ContractHoldOrderEntity holdOrderEntity : holdOrderEntities) {
                        BigDecimal lotNumber = cacheSettingUtils.getSymbolSku(holdOrderEntity.getSymbol());
                        BigDecimal profitOrLess = CalculateUtil.calOrderProfitOrLess(holdOrderEntity.getOpeningType(), new BigDecimal(price), holdOrderEntity.getOpeningPrice(), lotNumber, holdOrderEntity.getSymbolCnt(), memberEntity.getIsProfit());
                        totalProfitOrLess = totalProfitOrLess.add(profitOrLess);
                    }
                    MemberWalletContractEntity wallet = memberWalletContractDao.findWalletContractByMemberIdAndSymbol(memberId, CoinTypeEnum.USDT.name());
                    BigDecimal sub = wallet.getTotalBalance().subtract(totalProfitOrLess);
                    if (sub.compareTo(BigDecimal.ZERO) <= 0) {
                        List<OrderModel> list = new ArrayList<>();
                        OrderModel orderModel = new OrderModel(null, 0, price, symbol, memberId);
                        list.add(orderModel);
                        String content = JSONObject.toJSONString(list);
                        orderProducer.sendWholeBomb(content);
                    }
                }
            }
        }
    }
}
src/main/java/com/xcong/excoin/utils/CalculateUtil.java
@@ -234,4 +234,29 @@
                .multiply(feeRatio.divide(new BigDecimal(100)))
                .setScale(8, BigDecimal.ROUND_DOWN);
    }
    public static BigDecimal calOrderProfitOrLess(int type, BigDecimal newPrice, BigDecimal openPrice, BigDecimal lotNumber, int symbolCnt, int isProfit) {
        CacheSettingUtils cacheSettingUtils = SpringContextHolder.getBean(CacheSettingUtils.class);
        PlatformTradeSettingEntity tradeSetting = cacheSettingUtils.getTradeSetting();
        // 单个订单盈利
        BigDecimal profitOrLess = BigDecimal.ZERO;
        // 开多
        if (ContractHoldOrderEntity.OPENING_TYPE_MORE == type) {
            profitOrLess = newPrice.subtract(openPrice).multiply(new BigDecimal(symbolCnt)).multiply(lotNumber);
            // 开空
        } else {
            profitOrLess = openPrice.subtract(newPrice).multiply(new BigDecimal(symbolCnt)).multiply(lotNumber);
        }
        if (MemberEntity.IS_PROFIT_Y == isProfit) {
            if (profitOrLess.compareTo(BigDecimal.ZERO) > 0) {
                profitOrLess = profitOrLess.multiply(BigDecimal.ONE.subtract(tradeSetting.getForceParam()));
            } else {
                profitOrLess = profitOrLess.multiply(BigDecimal.ONE.add(tradeSetting.getForceParam()));
            }
        }
        return profitOrLess;
    }
}
src/main/resources/mapper/contract/ContractHoldOrderDao.xml
@@ -88,4 +88,10 @@
        set force_closing_price=#{forcePrice}, is_can_closing=1
        where member_id=#{memberId} and symbol=#{symbol}
    </update>
    <select id="selectMemberHasWholeOrder" resultType="java.lang.Long">
        select distinct member_id
        from contract_hold_order
        where position_type=2
    </select>
</mapper>