zainali5120
2020-09-25 286ea89f5f6cade73276f865effa5dcd2b19406d
src/main/java/com/xcong/excoin/modules/contract/service/impl/OrderWebsocketServiceImpl.java
@@ -12,8 +12,10 @@
import com.xcong.excoin.modules.contract.service.ContractHoldOrderService;
import com.xcong.excoin.modules.contract.service.ContractOrderService;
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.entity.MemberEntity;
import com.xcong.excoin.modules.member.entity.MemberSettingEntity;
import com.xcong.excoin.modules.member.entity.MemberWalletContractEntity;
import com.xcong.excoin.modules.member.parameter.vo.NeedMoneyMemberVo;
import com.xcong.excoin.modules.member.service.MemberService;
@@ -67,6 +69,8 @@
    @Resource
    private MemberAccountFlowEntityDao memberAccountFlowEntityDao;
    @Resource
    private MemberSettingDao memberSettingDao;
    public void dealOrderFromMq(List<OrderModel> list, Integer type) {
@@ -172,13 +176,16 @@
                                .multiply(new BigDecimal(currentFlat))
                                .multiply(symbolSku).setScale(8, BigDecimal.ROUND_DOWN);
                        MemberEntity memberEntity = memberService.getById(memberId);
                        MemberSettingEntity memberSettingEntity = memberSettingDao.selectMemberSettingByMemberId(memberId);
                        log.info("划点前:{}", profitLossPrice);
                        profitLossPrice = profitLossPrice.multiply(BigDecimal.ONE.subtract(memberSettingEntity.getClosingSpread().divide(BigDecimal.valueOf(100), 4, BigDecimal.ROUND_DOWN)));
                        log.info("划点后:{}", profitLossPrice);
                        if (memberEntity.getIsProfit() == 1) {
                            PlatformTradeSettingEntity tradeSetting = cacheSettingUtils.getTradeSetting();
                            if (profitLossPrice.compareTo(BigDecimal.ZERO) > 0) {
                                profitLossPrice = profitLossPrice.multiply(BigDecimal.ONE.subtract(tradeSetting.getProfitParam()));
                            } else {
                                profitLossPrice = profitLossPrice.multiply(BigDecimal.ONE.add(tradeSetting.getProfitParam()));
//                                profitLossPrice = profitLossPrice.multiply(BigDecimal.ONE.add(tradeSetting.getProfitParam()));
                            }
                        }
                        //回报率
@@ -188,17 +195,19 @@
                        contractOrderEntity.setClosingFeeAmount(order.getOpeningFeeAmount());
                        contractOrderEntity.setClosingPrice(closePrice);
                        contractOrderEntity.setClosingType(6);
                        contractOrderEntity.setOrderType(ContractOrderEntity.ORDER_TYPE_CLOSE_MORE);
                        BigDecimal totalReturn = BigDecimal.ZERO;
                        contractOrderService.save(contractOrderEntity);
                        contractEntrustOrderService.removeById(order.getId());
                        contractHoldOrderService.removeById(order.getId());
                        // 将需要退回的减去手续费
                        BigDecimal needReturn = prePrice.add(profitLossPrice);
                        //总退回金额=保证金+收益-手续费
                        totalReturn = needReturn.subtract(contractOrderEntity.getClosingFeeAmount());
                        // 总的是收益-平仓手续费
                        BigDecimal totalBalance = profitLossPrice.subtract(contractOrderEntity.getClosingFeeAmount());
                        memberWalletContractService.increaseWalletContractBalanceById(totalBalance, totalReturn, null, wallet.getId());
                        memberWalletContractService.increaseWalletContractBalanceById(totalReturn, totalBalance, null, wallet.getId());
                        // 流水记录 TODO 531e
                        insertAccountFlow(order, wallet, profitLossPrice, "止盈平仓");
@@ -260,13 +269,16 @@
                                .multiply(new BigDecimal(currentFlat + ""))
                                .multiply(symbolSku).setScale(8, BigDecimal.ROUND_DOWN);
                        MemberEntity memberEntity = memberService.getById(memberId);
                        MemberSettingEntity memberSettingEntity = memberSettingDao.selectMemberSettingByMemberId(memberId);
                        log.info("划点前:{}", profitLossPrice);
                        profitLossPrice = profitLossPrice.multiply(BigDecimal.ONE.subtract(memberSettingEntity.getClosingSpread().divide(BigDecimal.valueOf(100), 4, BigDecimal.ROUND_DOWN)));
                        log.info("划点后:{}", profitLossPrice);
                        if (memberEntity.getIsProfit() == 1) {
                            PlatformTradeSettingEntity tradeSetting = cacheSettingUtils.getTradeSetting();
                            if (profitLossPrice.compareTo(BigDecimal.ZERO) > 0) {
                                profitLossPrice = profitLossPrice.multiply(BigDecimal.ONE.subtract(tradeSetting.getProfitParam()));
                            } else {
                                profitLossPrice = profitLossPrice.multiply(BigDecimal.ONE.add(tradeSetting.getProfitParam()));
//                                profitLossPrice = profitLossPrice.multiply(BigDecimal.ONE.add(tradeSetting.getProfitParam()));
                            }
                        }
                        //回报率
@@ -276,6 +288,7 @@
                        contractOrderEntity.setClosingFeeAmount(order.getOpeningFeeAmount());
                        contractOrderEntity.setClosingPrice(closePrice);
                        contractOrderEntity.setClosingType(7);
                        contractOrderEntity.setOrderType(ContractOrderEntity.ORDER_TYPE_CLOSE_LESS);
                        BigDecimal totalReturn = BigDecimal.ZERO;
                        contractOrderService.save(contractOrderEntity);
@@ -287,8 +300,8 @@
                        // 更新钱包
                        // 总的是收益-平仓手续费
                        BigDecimal totalBalance = profitLossPrice.subtract(contractOrderEntity.getClosingFeeAmount());
                        memberWalletContractService.increaseWalletContractBalanceById(totalBalance, totalReturn, null, wallet.getId());
                        memberWalletContractService.increaseWalletContractBalanceById(totalReturn, totalBalance, null, wallet.getId());
                        insertAccountFlow(order, wallet, profitLossPrice, "止盈平仓");
@@ -357,7 +370,7 @@
                            if (profitLossPrice.compareTo(BigDecimal.ZERO) > 0) {
                                profitLossPrice = profitLossPrice.multiply(BigDecimal.ONE.subtract(tradeSetting.getProfitParam()));
                            } else {
                                profitLossPrice = profitLossPrice.multiply(BigDecimal.ONE.add(tradeSetting.getProfitParam()));
//                                profitLossPrice = profitLossPrice.multiply(BigDecimal.ONE.add(tradeSetting.getProfitParam()));
                            }
                        }
                        //回报率
@@ -366,9 +379,12 @@
                        contractOrderEntity.setRewardRatio(returnRate);
                        contractOrderEntity.setClosingFeeAmount(order.getOpeningFeeAmount());
                        contractOrderEntity.setClosingPrice(closePrice);
                        contractOrderEntity.setOrderType(ContractOrderEntity.ORDER_TYPE_CLOSE_MORE);
                        contractOrderEntity.setClosingType(8);
                        BigDecimal totalReturn = BigDecimal.ZERO;
                        contractOrderService.save(contractOrderEntity);
                        contractHoldOrderService.removeById(order.getId());
                        // 将需要退回的减去手续费
                        BigDecimal needReturn = prePrice.add(profitLossPrice);
                        //总退回金额=保证金+收益-手续费
@@ -376,7 +392,8 @@
                        // 更新钱包
                        // 总的是收益-平仓手续费
                        BigDecimal totalBalance = profitLossPrice.subtract(contractOrderEntity.getClosingFeeAmount());
                        memberWalletContractService.increaseWalletContractBalanceById(totalBalance, totalReturn, null, wallet.getId());
                        memberWalletContractService.increaseWalletContractBalanceById(totalReturn, totalBalance, null, wallet.getId());
                        insertAccountFlow(order, wallet, profitLossPrice, "开多止损平仓");
@@ -420,7 +437,7 @@
                    String symbol = order.getSymbol();
                    Long memberId = order.getMemberId();
                    // 本次需要退回的预付款
                    BigDecimal prePrice = order.getPrePaymentAmount();
                    BigDecimal prePrice = order.getBondAmount();
                    MemberWalletContractEntity wallet = memberWalletContractService.findWalletContractByMemberIdAndSymbol(memberId, "USDT");
                    if (wallet != null) {
@@ -444,7 +461,7 @@
                            if (profitLossPrice.compareTo(BigDecimal.ZERO) > 0) {
                                profitLossPrice = profitLossPrice.multiply(BigDecimal.ONE.subtract(tradeSetting.getProfitParam()));
                            } else {
                                profitLossPrice = profitLossPrice.multiply(BigDecimal.ONE.add(tradeSetting.getProfitParam()));
//                                profitLossPrice = profitLossPrice.multiply(BigDecimal.ONE.add(tradeSetting.getProfitParam()));
                            }
                        }
                        //回报率
@@ -454,8 +471,12 @@
                        contractOrderEntity.setClosingFeeAmount(order.getOpeningFeeAmount());
                        contractOrderEntity.setClosingPrice(closePrice);
                        contractOrderEntity.setClosingType(9);
                        contractOrderEntity.setOrderType(ContractOrderEntity.ORDER_TYPE_CLOSE_LESS);
                        BigDecimal totalReturn = BigDecimal.ZERO;
                        contractOrderService.save(contractOrderEntity);
                        contractHoldOrderService.removeById(order.getId());
                        // 将需要退回的减去手续费
                        BigDecimal needReturn = prePrice.add(profitLossPrice);
                        //总退回金额=保证金+收益-手续费
@@ -463,7 +484,7 @@
                        // 更新钱包
                        // 总的是收益-平仓手续费
                        BigDecimal totalBalance = profitLossPrice.subtract(contractOrderEntity.getClosingFeeAmount());
                        memberWalletContractService.increaseWalletContractBalanceById(totalBalance, totalReturn, null, wallet.getId());
                        memberWalletContractService.increaseWalletContractBalanceById(totalReturn, totalBalance, null, wallet.getId());
                        insertAccountFlow(order, wallet, profitLossPrice, "开空止损平仓");
@@ -540,6 +561,7 @@
                contractHoldOrderEntity.setLeverRatio(coinsCoinsOrder.getLeverRatio());
                contractHoldOrderEntity.setOpeningPrice(entrustPrice);
                contractHoldOrderEntity.setTradeType(ContractHoldOrderEntity.TRADE_TYPE_LIMIT);
                contractHoldOrderEntity.setOperateNo(1);
                contractHoldOrderService.save(contractHoldOrderEntity);
                // 需要一个历史插入
@@ -547,6 +569,7 @@
                contractOrderEntity.setEntrustOpeningPrice(coinsCoinsOrder.getEntrustPrice());
                contractOrderEntity.setEntrustTime(coinsCoinsOrder.getCreateTime());
                contractOrderEntity.setOpeningTime(new Date());
                contractOrderEntity.setId(null);
                contractOrderService.save(contractOrderEntity);
                // 发送爆仓的队列
@@ -614,12 +637,17 @@
                if (isDone) {
                    //删除次仓订单
                    contractHoldOrderService.removeById(orderId);
                    // 订单状态转换
                    if (ContractOrderEntity.ORDER_TYPE_OPEN_MORE == contractOrderEntity.getOrderType()) {
                        contractOrderEntity.setOrderType(ContractOrderEntity.ORDER_TYPE_CLOSE_MORE);
                    } else {
                        contractOrderEntity.setOrderType(ContractOrderEntity.ORDER_TYPE_CLOSE_LESS);
                    }
                    //更新主表订单状态位为“已平仓”
                    contractOrderEntity.setId(null);
                    contractOrderEntity.setClosingPrice(BigDecimal.ZERO);
                    contractOrderEntity.setClosingPrice(nowPrice);
                    contractOrderEntity.setClosingTime(new Date());
                    contractOrderEntity.setClosingFeeAmount(coinsOrder.getOpeningFeeAmount());
                    contractOrderEntity.setRewardAmount(coinsOrder.getBondAmount().subtract(contractOrderEntity.getOpeningFeeAmount()).negate());
                    contractOrderService.save(contractOrderEntity);
@@ -645,12 +673,17 @@
    }
    public void calYj(Long mid, BigDecimal money, ContractOrderEntity order, int type) {
        PlatformTradeSettingEntity tradeSetting = cacheSettingUtils.getTradeSetting();
        if (money != null) {
            money = money.multiply(new BigDecimal(0.7868));
            money = money.multiply(tradeSetting.getFeeSpreadRatio());
        }
        MemberEntity member = memberService.getById(mid);
        String[] referenceIds = member.getRefererIds().split(",");
        List<String> ids = Arrays.asList(referenceIds);
        if (MemberEntity.ACCOUNT_TYPE_TEST.equals(member.getAccountType())) {
            return;
        }
        // 判断该用户是否为代理商
        NeedMoneyMemberVo needMoneyMember = memberService.selectFriendRelationUserByMemberId(mid);
@@ -676,24 +709,28 @@
        // 存放uid以及对应uid用户的佣金
        Map<String, BigDecimal> map = new HashMap<>();
        Map<String, Map<String, BigDecimal>> map = new HashMap<>();
        Iterator<Map.Entry<Integer, NeedMoneyMemberVo>> it = treeMap.entrySet().iterator();
        BigDecimal lastRate = BigDecimal.ZERO;
        BigDecimal lastYj = BigDecimal.ZERO;
        while (it.hasNext()) {
            Map.Entry<Integer, NeedMoneyMemberVo> entry = it.next();
            NeedMoneyMemberVo member1 = entry.getValue();
            Map<String, BigDecimal> returnValue = new HashMap<>();
            returnValue.put("ratio", member1.getReturnRatio());
            returnValue.put("lastRate", lastRate);
            // 上下级佣金比率相减后乘以手续费 -- 即用户所得佣金
            lastYj = (member1.getReturnRatio().subtract(lastRate)).multiply(money);
            lastRate = member1.getReturnRatio();
            map.put(member1.getInviteId(), lastYj);
            returnValue.put("returnMoney", lastYj);
            map.put(member1.getInviteId(), returnValue);
        }
        // 输出对应佣金是否正确
        Iterator<Map.Entry<String, BigDecimal>> it1 = map.entrySet().iterator();
        Iterator<Map.Entry<String, Map<String, BigDecimal>>> it1 = map.entrySet().iterator();
        List<AgentReturnEntity> agentList = new ArrayList<AgentReturnEntity>();
        while (it1.hasNext()) {
            Map.Entry<String, BigDecimal> entry = it1.next();
            Map.Entry<String, Map<String, BigDecimal>> entry = it1.next();
            // System.out.println(entry.getKey() + "-----" + entry.getValue());
            MemberEntity agentMember = memberService.selectMemberInfoByInviteId(entry.getKey());
            AgentReturnEntity agent = new AgentReturnEntity();
@@ -704,7 +741,9 @@
            agent.setOrderType(order.getOrderType());
            agent.setReturnSymbol(order.getSymbol());
            agent.setIsReturn(0);
            agent.setReturnAmount(entry.getValue());
            agent.setReturnAmount(entry.getValue().get("returnMoney"));
            agent.setChildReturnRatio(entry.getValue().get("lastRate"));
            agent.setReturnRatio(entry.getValue().get("ratio"));
            agent.setClosingType(order.getClosingType());
            if (type == 1) {//开仓
                agent.setOpeningFeeAmount(order.getOpeningFeeAmount());