|  |  |  | 
|---|
|  |  |  | //package com.xcong.excoin.modules.contract.service.impl; | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //import cn.hutool.core.bean.BeanUtil; | 
|---|
|  |  |  | //import com.alibaba.fastjson.JSONObject; | 
|---|
|  |  |  | //import com.kebex.app.dao.member.AgentReturnMapper; | 
|---|
|  |  |  | //import com.kebex.app.dao.member.MemberMapper; | 
|---|
|  |  |  | //import com.kebex.app.dao.member.WalletDao; | 
|---|
|  |  |  | //import com.kebex.app.dao.order.OrderMapper; | 
|---|
|  |  |  | //import com.kebex.app.dao.trade.TradeMapper; | 
|---|
|  |  |  | //import com.kebex.app.entity.member.*; | 
|---|
|  |  |  | //import com.kebex.app.entity.order.CoinsCoinsOrder; | 
|---|
|  |  |  | //import com.kebex.app.entity.trade.TradeSetting; | 
|---|
|  |  |  | //import com.kebex.app.entity.trade.TradeSymbolSku; | 
|---|
|  |  |  | //import com.kebex.app.service.member.MemberService; | 
|---|
|  |  |  | //import com.kebex.common.cache.TradeSettingCache; | 
|---|
|  |  |  | //import com.kebex.pricequeue.OrderModel; | 
|---|
|  |  |  | //import com.kebex.rabbit.producer.ExampleProducer; | 
|---|
|  |  |  | //import com.xcong.excoin.modules.contract.entity.ContractEntrustOrderEntity; | 
|---|
|  |  |  | //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.service.ContractEntrustOrderService; | 
|---|
|  |  |  | //import com.xcong.excoin.modules.contract.service.ContractHoldOrderService; | 
|---|
|  |  |  | //import com.xcong.excoin.modules.contract.service.ContractOrderService; | 
|---|
|  |  |  | //import com.xcong.excoin.modules.member.entity.MemberEntity; | 
|---|
|  |  |  | //import com.xcong.excoin.modules.member.entity.MemberWalletContractEntity; | 
|---|
|  |  |  | //import com.xcong.excoin.modules.member.service.MemberService; | 
|---|
|  |  |  | //import com.xcong.excoin.modules.member.service.MemberWalletContractService; | 
|---|
|  |  |  | //import com.xcong.excoin.modules.platform.entity.PlatformTradeSettingEntity; | 
|---|
|  |  |  | //import com.xcong.excoin.rabbit.pricequeue.OrderModel; | 
|---|
|  |  |  | //import com.xcong.excoin.rabbit.producer.OrderProducer; | 
|---|
|  |  |  | //import com.xcong.excoin.utils.CacheSettingUtils; | 
|---|
|  |  |  | //import com.xcong.excoin.utils.CalculateUtil; | 
|---|
|  |  |  | //import org.apache.commons.collections.CollectionUtils; | 
|---|
|  |  |  | //import org.springframework.beans.factory.annotation.Autowired; | 
|---|
|  |  |  | //import org.springframework.stereotype.Component; | 
|---|
|  |  |  | //import org.springframework.stereotype.Service; | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //import javax.annotation.Resource; | 
|---|
|  |  |  | //import java.math.BigDecimal; | 
|---|
|  |  |  | //import java.math.RoundingMode; | 
|---|
|  |  |  | //import java.util.*; | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //@Service | 
|---|
|  |  |  | //public class OrderWebsocketServiceImpl { | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //    @Resource | 
|---|
|  |  |  | //    ContractHoldOrderService contractHoldOrderService; | 
|---|
|  |  |  | // | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //    @Resource | 
|---|
|  |  |  | //    ContractOrderService contractOrderService; | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //    @Resource | 
|---|
|  |  |  | //    ContractEntrustOrderService contractEntrustOrderService; | 
|---|
|  |  |  | // | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //    @Resource | 
|---|
|  |  |  | //    MemberWalletContractService memberWalletContractService; | 
|---|
|  |  |  | // | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //    @Resource | 
|---|
|  |  |  | //    CacheSettingUtils cacheSettingUtils; | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //    @Resource | 
|---|
|  |  |  | //    OrderProducer producer; | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //    @Resource | 
|---|
|  |  |  | //    MemberService memberService; | 
|---|
|  |  |  | // | 
|---|
|  |  |  | // | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //    public void dealOrderFromMq(List<OrderModel> list, Integer type) { | 
|---|
|  |  |  | //        if (CollectionUtils.isNotEmpty(list)) { | 
|---|
|  |  |  | //            String batchno = UUID.randomUUID().toString(); | 
|---|
|  |  |  | //            // 更新订单状态 | 
|---|
|  |  |  | //            // 更新为不可平仓状态 | 
|---|
|  |  |  | //            int count = contractHoldOrderService.updateContractHoldOrderCanNotClosingByIds(list, batchno); | 
|---|
|  |  |  | //            // 查询 | 
|---|
|  |  |  | //            if (count > 0) { | 
|---|
|  |  |  | //                // 查询 | 
|---|
|  |  |  | //                List<ContractHoldOrderEntity> coinsCoinsOrders = contractHoldOrderService.selectContractHoldOrderByBatchNo(batchno); | 
|---|
|  |  |  | //                // 根据状态调用不同的方法 | 
|---|
|  |  |  | //                // 1:买入委托2:开多3:开空4:平多5:平空6:爆仓平多7:爆仓平空8:撤单9:止盈平多10:止盈平空11:止损平多12:止损平空 | 
|---|
|  |  |  | //                // 6在这里是爆仓 包括爆空和暴多 | 
|---|
|  |  |  | //                switch (type) { | 
|---|
|  |  |  | //                    case 6: | 
|---|
|  |  |  | //                        this.dealCoinOut(coinsCoinsOrders,list); | 
|---|
|  |  |  | //                        break; | 
|---|
|  |  |  | //                    case 9: | 
|---|
|  |  |  | //                        this.dealForMoreStopPro(coinsCoinsOrders,list); | 
|---|
|  |  |  | //                        break; | 
|---|
|  |  |  | //                    case 10: | 
|---|
|  |  |  | //                        this.dealForLessStopPro(coinsCoinsOrders,list); | 
|---|
|  |  |  | //                        break; | 
|---|
|  |  |  | //                    case 11: | 
|---|
|  |  |  | //                        this.dealForMoreLoss(coinsCoinsOrders,list); | 
|---|
|  |  |  | //                        break; | 
|---|
|  |  |  | //                    case 12: | 
|---|
|  |  |  | //                        this.dealForLessLoss(coinsCoinsOrders,list); | 
|---|
|  |  |  | //                        break; | 
|---|
|  |  |  | //                } | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //            } | 
|---|
|  |  |  | // | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //        } | 
|---|
|  |  |  | //    } | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //    public void dealForLimitMq(List<OrderModel> list){ | 
|---|
|  |  |  | //        if(CollectionUtils.isNotEmpty(list)){ | 
|---|
|  |  |  | //            //查询限价委托的单 | 
|---|
|  |  |  | //            String batchno =UUID.randomUUID().toString(); | 
|---|
|  |  |  | //            List<Long> ids = new ArrayList<>(); | 
|---|
|  |  |  | //            list.forEach(model->ids.add(model.getOrderId())); | 
|---|
|  |  |  | //            List<ContractEntrustOrderEntity> contractEntrustOrderEntities = contractEntrustOrderService.selectEntrustOrderListByIds(ids); | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //            if(CollectionUtils.isNotEmpty(contractEntrustOrderEntities)){ | 
|---|
|  |  |  | //                this.dealLimitBuyOrder(contractEntrustOrderEntities); | 
|---|
|  |  |  | //            } | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //        } | 
|---|
|  |  |  | //    } | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //    /** | 
|---|
|  |  |  | //     * 开多止盈 | 
|---|
|  |  |  | //     */ | 
|---|
|  |  |  | //    public void dealForMoreStopPro(List<ContractHoldOrderEntity> orderList,List<OrderModel> list) { | 
|---|
|  |  |  | //        if (CollectionUtils.isNotEmpty(orderList)) { | 
|---|
|  |  |  | //            Map<Long,BigDecimal> modelMap = new HashMap<Long,BigDecimal>(); | 
|---|
|  |  |  | //            for(OrderModel model : list){ | 
|---|
|  |  |  | //                modelMap.put(model.getOrderId(),new BigDecimal(model.getPrice())); | 
|---|
|  |  |  | //            } | 
|---|
|  |  |  | //            for (ContractHoldOrderEntity order : orderList) { | 
|---|
|  |  |  | //                Long orderId = order.getId(); | 
|---|
|  |  |  | //                System.out.println("开多止盈订单号:" + order.getOrderNo()); | 
|---|
|  |  |  | //                System.out.println("传来的止盈价格:"+modelMap.get(order.getId())); | 
|---|
|  |  |  | //                if (null != order.getStopProfitPrice()) { | 
|---|
|  |  |  | //                    BigDecimal closePrice = order.getStopProfitPrice(); | 
|---|
|  |  |  | //                    BigDecimal queuePrice = modelMap.get(order.getId()); | 
|---|
|  |  |  | //                    System.out.println("订单的止盈价格:"+closePrice); | 
|---|
|  |  |  | //                    // 判断 保留七位是为了忽略以为小数 防止不精确导致的不相等 | 
|---|
|  |  |  | //                    if(queuePrice.compareTo(closePrice)!=0){ | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //                        // 不能止盈 | 
|---|
|  |  |  | //                        System.out.println("数据库价格:"+queuePrice.toPlainString()+"--价格不能止盈:"+closePrice); | 
|---|
|  |  |  | //                        //更新数据 | 
|---|
|  |  |  | //                        contractHoldOrderService.updateOrderIsCanClosingAndBatchNoById(orderId); | 
|---|
|  |  |  | //                        continue; | 
|---|
|  |  |  | //                    } | 
|---|
|  |  |  | //                    System.out.println("执行操作"); | 
|---|
|  |  |  | //                    // 止盈价 | 
|---|
|  |  |  | //                    String symbol = order.getSymbol(); | 
|---|
|  |  |  | //                    // 本次需要退回的预付款 | 
|---|
|  |  |  | //                    BigDecimal prePrice = order.getPrePaymentAmount(); | 
|---|
|  |  |  | //                    Long memberId = order.getMemberId(); | 
|---|
|  |  |  | //                    MemberWalletContractEntity wallet = memberWalletContractService.findWalletContractByMemberIdAndSymbol(memberId, "USDT"); | 
|---|
|  |  |  | //                    if (wallet != null) { | 
|---|
|  |  |  | //                        // 历史订单 | 
|---|
|  |  |  | //                        ContractOrderEntity contractOrderEntity = ContractHoldOrderEntityMapper.INSTANCE.holdOrderToOrder(order); | 
|---|
|  |  |  | //                        contractOrderEntity.setClosingType(6); | 
|---|
|  |  |  | //                        contractOrderEntity.setClosingTime(new Date()); | 
|---|
|  |  |  | //                        contractOrderEntity.setId(null); | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //                        // 本次平仓数量 | 
|---|
|  |  |  | //                        int currentFlat = order.getSymbolCnt(); | 
|---|
|  |  |  | //                        BigDecimal symbolSku = cacheSettingUtils.getSymbolSku(order.getSymbol()); | 
|---|
|  |  |  | //                        // 盈亏额度= (当前的币种的平仓价-下单时的建仓价)*购买的手数/规格*倍率 | 
|---|
|  |  |  | //                        BigDecimal profitLossPrice = (closePrice | 
|---|
|  |  |  | //                                .subtract(order.getOpeningPrice())) | 
|---|
|  |  |  | //                                .multiply(new BigDecimal(currentFlat+"")) | 
|---|
|  |  |  | //                                .multiply(symbolSku).setScale(8, BigDecimal.ROUND_DOWN); | 
|---|
|  |  |  | //                        MemberEntity memberEntity = memberService.getById(memberId); | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //                        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())); | 
|---|
|  |  |  | //                            } | 
|---|
|  |  |  | //                        } | 
|---|
|  |  |  | //                        //回报率 | 
|---|
|  |  |  | //                        BigDecimal returnRate = profitLossPrice.divide((order.getPrePaymentAmount().subtract(contractOrderEntity.getClosingFeeAmount())), 8, BigDecimal.ROUND_DOWN); | 
|---|
|  |  |  | //                       contractOrderEntity.setRewardAmount(profitLossPrice); | 
|---|
|  |  |  | //                        contractOrderEntity.setRewardRatio(returnRate); | 
|---|
|  |  |  | //                        contractOrderEntity.setClosingFeeAmount(order.getOpeningFeeAmount()); | 
|---|
|  |  |  | //                        contractOrderEntity.setClosingPrice(closePrice); | 
|---|
|  |  |  | //                        contractOrderEntity.setClosingType(9); | 
|---|
|  |  |  | //                        BigDecimal totalReturn = BigDecimal.ZERO; | 
|---|
|  |  |  | //                         contractOrderService.save(contractOrderEntity); | 
|---|
|  |  |  | //                        // 将需要退回的减去手续费 | 
|---|
|  |  |  | //                        BigDecimal needReturn = prePrice.add(profitLossPrice); | 
|---|
|  |  |  | //                        //总退回金额=保证金+收益-手续费 | 
|---|
|  |  |  | //                        totalReturn = needReturn.subtract(contractOrderEntity.getClosingFeeAmount()); | 
|---|
|  |  |  | //                        // 更新钱包 | 
|---|
|  |  |  | //                        // 总的是收益-平仓手续费 | 
|---|
|  |  |  | //                        BigDecimal totalBalance = profitLossPrice.subtract(contractOrderEntity.getClosingFeeAmount()); | 
|---|
|  |  |  | //                        memberWalletContractService.increaseWalletContractBalanceById(totalBalance,totalReturn,null,wallet.getId()); | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //                        // 流水记录 TODO 531 | 
|---|
|  |  |  | ////                        MemberAccountFlowRecord record = new MemberAccountFlowRecord(); | 
|---|
|  |  |  | ////                        record.setCreateTime(new Date()); | 
|---|
|  |  |  | ////                        record.setDirectionSource("止盈平仓"); | 
|---|
|  |  |  | ////                        record.setRemark("止盈平仓"); | 
|---|
|  |  |  | ////                        record.setMemberBalance(wallet.getAvailableBalance()); | 
|---|
|  |  |  | ////                        record.setMemberid(order.getMemberId()); | 
|---|
|  |  |  | ////                        record.setSymbolName(order.getSymbol()); | 
|---|
|  |  |  | ////                        record.setMemberName(wallet.getMemberName()); | 
|---|
|  |  |  | ////                        record.setMemberPhone(order.getMemberPhone()); | 
|---|
|  |  |  | ////                        record.setPrice(profitLossPrice + order.getPrePrice()); | 
|---|
|  |  |  | ////                        memberService.addFlowRecord(record); | 
|---|
|  |  |  | //// | 
|---|
|  |  |  | ////                        //返佣 TODO 531 | 
|---|
|  |  |  | ////                        calYj(order.getMemberId(), new BigDecimal(order.getClosingPrice()), order, 2); | 
|---|
|  |  |  | //                    } | 
|---|
|  |  |  | //                } | 
|---|
|  |  |  | //            } | 
|---|
|  |  |  | //        } | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //    } | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //    /** | 
|---|
|  |  |  | //     * 开空止盈 | 
|---|
|  |  |  | //     */ | 
|---|
|  |  |  | //    public void dealForLessStopPro(List<ContractHoldOrderEntity> orderList,List<OrderModel> list) { | 
|---|
|  |  |  | //        //List<CoinsCoinsOrder> orderList = orderMapper.selectOrderByBatchNo(batchno); | 
|---|
|  |  |  | //        //System.out.println("开空止盈订单batchno:" + batchno); | 
|---|
|  |  |  | //        if (CollectionUtils.isNotEmpty(orderList)) { | 
|---|
|  |  |  | //            Map<Long,BigDecimal> modelMap = new HashMap<Long,BigDecimal>(); | 
|---|
|  |  |  | //            for(OrderModel model : list){ | 
|---|
|  |  |  | //                modelMap.put(model.getOrderId(),new BigDecimal(model.getPrice())); | 
|---|
|  |  |  | //            } | 
|---|
|  |  |  | //            for (ContractHoldOrderEntity order : orderList) { | 
|---|
|  |  |  | //                System.out.println("开空止盈订单号:" + order.getOrderNo()); | 
|---|
|  |  |  | //                System.out.println("传来的止盈价格:"+modelMap.get(order.getId()).toPlainString()); | 
|---|
|  |  |  | //                BigDecimal closePrice = order.getStopProfitPrice(); | 
|---|
|  |  |  | //                if (null != closePrice) { | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //                    // 止盈价 | 
|---|
|  |  |  | //                    BigDecimal closePrice = new BigDecimal(order.getStopProfitPrice()).setScale(7, RoundingMode.HALF_UP); | 
|---|
|  |  |  | //                    System.out.println("订单的止盈价格:"+closePrice); | 
|---|
|  |  |  | //                    System.out.println(closePrice.compareTo(modelMap.get(order.getId()))); | 
|---|
|  |  |  | //                    BigDecimal queuePrice = modelMap.get(order.getId()).setScale(7, RoundingMode.HALF_UP); | 
|---|
|  |  |  | //                    if(closePrice.compareTo(queuePrice)!=0){ | 
|---|
|  |  |  | //                        System.out.println("数据库价格:"+queuePrice.toPlainString()+"--价格不能开空止盈:"+closePrice); | 
|---|
|  |  |  | //                        orderMapper.updateOrderIsExitAndBatchno(order.getId()); | 
|---|
|  |  |  | //                        continue; | 
|---|
|  |  |  | //                    } | 
|---|
|  |  |  | //                    System.out.println("执行操作"); | 
|---|
|  |  |  | //                    String symbol = order.getSymbol(); | 
|---|
|  |  |  | //                    // 本次需要退回的预付款 | 
|---|
|  |  |  | //                    BigDecimal prePrice = new BigDecimal(order.getPrePrice()); | 
|---|
|  |  |  | //                    Wallet wallet = memberService.findWalletByMemberIdAndSymbol(order.getMemberId(), "USDT"); | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //                    if (wallet != null) { | 
|---|
|  |  |  | //                        // 更新订单 | 
|---|
|  |  |  | //                        order.setIsEixt(0); | 
|---|
|  |  |  | //                        orderMapper.updateOrder(order); | 
|---|
|  |  |  | //                        order.setExitPrice(order.getStopProfitPrice());// 平仓价格 | 
|---|
|  |  |  | //                        order.setExitType(2);// 平仓类型,1:表示手动平仓,2:止盈平仓,3:止损平仓,4:爆仓 | 
|---|
|  |  |  | //                        order.setExitTime(new Date());// 平仓时间 | 
|---|
|  |  |  | //                        order.setEntrustStatus(7);// 平仓状态 | 
|---|
|  |  |  | //                        // 本次平仓数量 | 
|---|
|  |  |  | //                        Long currentFlat = order.getSymbolSkuNumber(); | 
|---|
|  |  |  | //                        TradeSymbolSku symbolSku = tradeMapper.findSymbolSkubySymbol(symbol); | 
|---|
|  |  |  | //                        // 盈亏额度= (当前的币种的平仓价-下单时的建仓价)*购买的手数/规格*倍率 | 
|---|
|  |  |  | //                        Double profitLossPrice = (new BigDecimal(order.getTradePrice()).subtract(closePrice)) | 
|---|
|  |  |  | //                                .multiply(new BigDecimal(currentFlat)) | 
|---|
|  |  |  | //                                .multiply(symbolSku.getLotNumber()) | 
|---|
|  |  |  | //                                .setScale(8, BigDecimal.ROUND_DOWN).doubleValue(); | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //                        Member member = memberService.selectMemberById(order.getMemberId()); | 
|---|
|  |  |  | //                        if(member.getIsProfit() == 1) { | 
|---|
|  |  |  | //                            TradeSetting tradeSetting = tradeSettingCache.getTradeSetting(); | 
|---|
|  |  |  | //                           if(profitLossPrice>0) { | 
|---|
|  |  |  | //                              profitLossPrice = profitLossPrice*(1-tradeSetting.getProfitParam()); | 
|---|
|  |  |  | //                            }else { | 
|---|
|  |  |  | //                               profitLossPrice = profitLossPrice*(1+tradeSetting.getProfitParam());; | 
|---|
|  |  |  | //                            } | 
|---|
|  |  |  | //                        } | 
|---|
|  |  |  | //                        //回报率 | 
|---|
|  |  |  | //                        double returnRate = new BigDecimal(profitLossPrice).divide((new BigDecimal(order.getPrePrice()).subtract(new BigDecimal(order.getClosingPrice()))), 8, BigDecimal.ROUND_DOWN) | 
|---|
|  |  |  | //                                .doubleValue(); | 
|---|
|  |  |  | //                        order.setRewardRatio(profitLossPrice);// 盈亏 | 
|---|
|  |  |  | //                        order.setReturnRate(returnRate); | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //                        BigDecimal totalReturn = BigDecimal.ZERO; | 
|---|
|  |  |  | //                        // 查询交易设置 | 
|---|
|  |  |  | //                        //TradeSetting tradeSetting = tradeMapper.findTradeSetting(); | 
|---|
|  |  |  | //                        // 计算手续费 | 
|---|
|  |  |  | ////                        Double closingPrice = closePrice | 
|---|
|  |  |  | ////                                .multiply(new BigDecimal(tradeSetting.getClosingRatio()).divide(new BigDecimal(100), 8, BigDecimal.ROUND_DOWN)) | 
|---|
|  |  |  | ////                                .multiply(symbolSku.getLotNumber())// 规格 | 
|---|
|  |  |  | ////                                .multiply(new BigDecimal(currentFlat)).setScale(8, BigDecimal.ROUND_DOWN) | 
|---|
|  |  |  | ////                                .doubleValue(); | 
|---|
|  |  |  | //                        order.setSellClosingPrice(order.getClosingPrice()); | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //                        //添加止盈订单 | 
|---|
|  |  |  | //                        order.setId(null); | 
|---|
|  |  |  | //                        //10:止盈平空 | 
|---|
|  |  |  | //                        order.setStatus(10); | 
|---|
|  |  |  | //                        orderMapper.addCoinsCoinsOrder(order); | 
|---|
|  |  |  | //                        // 将需要退回的减去手续费 | 
|---|
|  |  |  | //                        BigDecimal needReturn = prePrice.add(new BigDecimal(profitLossPrice)); | 
|---|
|  |  |  | //                        //总退回金额=保证金+收益-手续费 | 
|---|
|  |  |  | //                        totalReturn = needReturn.subtract(new BigDecimal(order.getClosingPrice())); | 
|---|
|  |  |  | //                        // 更新钱包 | 
|---|
|  |  |  | //                       // Double availableBalance = totalReturn.add(new BigDecimal(wallet.getAvailableBalance())) | 
|---|
|  |  |  | //                       //         .doubleValue(); | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //                        Double availableBalance = totalReturn.doubleValue(); | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //                        Double totalBalance = - order.getClosingPrice() + profitLossPrice; | 
|---|
|  |  |  | ////                        wallet.setAvailableBalance(availableBalance); | 
|---|
|  |  |  | ////                        wallet.setTotalBalance(totalBalance); | 
|---|
|  |  |  | ////                        int i = memberService.updateWallet(wallet); | 
|---|
|  |  |  | //                        walletDao.updateWallet(wallet.getId(),totalBalance,availableBalance,null); | 
|---|
|  |  |  | //                        // 流水记录 | 
|---|
|  |  |  | //                        MemberAccountFlowRecord record = new MemberAccountFlowRecord(); | 
|---|
|  |  |  | //                        record.setCreateTime(new Date()); | 
|---|
|  |  |  | //                        record.setDirectionSource("止盈平仓"); | 
|---|
|  |  |  | //                        record.setRemark("止盈平仓"); | 
|---|
|  |  |  | //                        record.setMemberBalance(wallet.getAvailableBalance()); | 
|---|
|  |  |  | //                        record.setMemberid(order.getMemberId()); | 
|---|
|  |  |  | //                        record.setSymbolName(order.getSymbol()); | 
|---|
|  |  |  | //                        record.setMemberName(wallet.getMemberName()); | 
|---|
|  |  |  | //                        record.setMemberPhone(order.getMemberPhone()); | 
|---|
|  |  |  | //                        record.setPrice(profitLossPrice + order.getPrePrice()); | 
|---|
|  |  |  | //                        memberService.addFlowRecord(record); | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //                        //返佣 TODO | 
|---|
|  |  |  | //                        calYj(order.getMemberId(), new BigDecimal(order.getClosingPrice()), order, 2); | 
|---|
|  |  |  | //                    } | 
|---|
|  |  |  | //                } | 
|---|
|  |  |  | //            } | 
|---|
|  |  |  | //        } | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //    } | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //    /** | 
|---|
|  |  |  | //     * 开多止损 | 
|---|
|  |  |  | //     * | 
|---|
|  |  |  | //     * @param | 
|---|
|  |  |  | //     */ | 
|---|
|  |  |  | //    public void dealForMoreLoss(List<CoinsCoinsOrder> orderList,List<OrderModel> list) { | 
|---|
|  |  |  | //        //List<CoinsCoinsOrder> orderList = orderMapper.selectOrderByBatchNo(batchno); | 
|---|
|  |  |  | //        //System.out.println("开多止损批次号batchno:" + batchno); | 
|---|
|  |  |  | //        if (CollectionUtils.isNotEmpty(orderList)) { | 
|---|
|  |  |  | //            Map<Long,BigDecimal> modelMap = new HashMap<Long,BigDecimal>(); | 
|---|
|  |  |  | //            for(OrderModel model : list){ | 
|---|
|  |  |  | //                modelMap.put(model.getOrderId(),new BigDecimal(model.getPrice())); | 
|---|
|  |  |  | //            } | 
|---|
|  |  |  | //            for (CoinsCoinsOrder order : orderList) { | 
|---|
|  |  |  | //                System.out.println("开多止损订单号:" + order.getSerialno()); | 
|---|
|  |  |  | //                System.out.println("传来的止损价格:"+modelMap.get(order.getId())); | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //                if (null != order.getStopLossPrice()) { | 
|---|
|  |  |  | //                    // 止损价 | 
|---|
|  |  |  | //                    BigDecimal closePrice = new BigDecimal(order.getStopLossPrice()).setScale(7,RoundingMode.HALF_UP); | 
|---|
|  |  |  | //                    System.out.println("订单止损价格:"+closePrice.toPlainString()); | 
|---|
|  |  |  | //                    BigDecimal queuePrice = modelMap.get(order.getId()).setScale(7, RoundingMode.HALF_UP); | 
|---|
|  |  |  | //                    if(closePrice.compareTo(queuePrice)!=0){ | 
|---|
|  |  |  | //                        System.out.println("数据库价格:"+queuePrice.toPlainString()+"--价格不能开多止损:"+closePrice); | 
|---|
|  |  |  | //                        orderMapper.updateOrderIsExitAndBatchno(order.getId()); | 
|---|
|  |  |  | //                        continue; | 
|---|
|  |  |  | //                    } | 
|---|
|  |  |  | //                    System.out.println("执行操作"); | 
|---|
|  |  |  | //                    String symbol = order.getSymbol(); | 
|---|
|  |  |  | //                    // 本次需要退回的预付款 | 
|---|
|  |  |  | //                    BigDecimal prePrice = new BigDecimal(order.getPrePrice()); | 
|---|
|  |  |  | //                    Wallet wallet = memberService.findWalletByMemberIdAndSymbol(order.getMemberId(), "USDT"); | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //                    if (wallet != null) { | 
|---|
|  |  |  | //                        // 更新订单 | 
|---|
|  |  |  | //                        order.setIsEixt(0); | 
|---|
|  |  |  | //                        orderMapper.updateOrder(order); | 
|---|
|  |  |  | //                        order.setExitPrice(order.getStopLossPrice());// 平仓价格 | 
|---|
|  |  |  | //                        order.setExitType(3);// 平仓类型,1:表示手动平仓,2:止盈平仓,3:止损平仓,4:爆仓 | 
|---|
|  |  |  | //                        order.setExitTime(new Date());// 平仓时间 | 
|---|
|  |  |  | //                        order.setEntrustStatus(8);// 平仓状态 | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //                        TradeSymbolSku symbolSku = tradeMapper.findSymbolSkubySymbol(symbol); | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //                        Double profitLossPrice = (closePrice | 
|---|
|  |  |  | //                                .subtract(new BigDecimal(order.getTradePrice()))) | 
|---|
|  |  |  | //                                .multiply(new BigDecimal(order.getSymbolSkuNumber())) | 
|---|
|  |  |  | //                                .multiply(symbolSku.getLotNumber()) | 
|---|
|  |  |  | //                                .setScale(8, BigDecimal.ROUND_DOWN).doubleValue(); | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //                        Member member = memberService.selectMemberById(order.getMemberId()); | 
|---|
|  |  |  | //                        if(member.getIsProfit() == 1) { | 
|---|
|  |  |  | //                            TradeSetting tradeSetting = tradeSettingCache.getTradeSetting(); | 
|---|
|  |  |  | //                            if(profitLossPrice>0) { | 
|---|
|  |  |  | //                              profitLossPrice = profitLossPrice*(1-tradeSetting.getProfitParam()); | 
|---|
|  |  |  | //                            }else { | 
|---|
|  |  |  | //                               profitLossPrice = profitLossPrice*(1+tradeSetting.getProfitParam());; | 
|---|
|  |  |  | //                            } | 
|---|
|  |  |  | //                        } | 
|---|
|  |  |  | //                        //回报率 | 
|---|
|  |  |  | //                        double returnRate = new BigDecimal(profitLossPrice).divide((new BigDecimal(order.getPrePrice()).subtract(new BigDecimal(order.getClosingPrice()))), 8, BigDecimal.ROUND_DOWN) | 
|---|
|  |  |  | //                                .doubleValue(); | 
|---|
|  |  |  | //                        // 查询交易设置 | 
|---|
|  |  |  | //                        TradeSetting tradeSetting = tradeSettingCache.getTradeSetting(); | 
|---|
|  |  |  | //                        // 计算手续费 | 
|---|
|  |  |  | ////                        Double closingPrice = closePrice | 
|---|
|  |  |  | ////                                .multiply(new BigDecimal(tradeSetting.getClosingRatio()).divide(new BigDecimal(100), 8, BigDecimal.ROUND_DOWN)) | 
|---|
|  |  |  | ////                                .multiply(symbolSku.getLotNumber())// 规格 | 
|---|
|  |  |  | ////                                .multiply(new BigDecimal(currentFlat)).setScale(8, BigDecimal.ROUND_DOWN) | 
|---|
|  |  |  | ////                                .doubleValue(); | 
|---|
|  |  |  | //                        order.setRewardRatio(profitLossPrice);// 盈亏 | 
|---|
|  |  |  | //                        order.setReturnRate(returnRate); | 
|---|
|  |  |  | //                        order.setSellClosingPrice(order.getClosingPrice()); | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //                        //添加止损 | 
|---|
|  |  |  | //                        order.setId(null); | 
|---|
|  |  |  | //                        order.setStatus(11); | 
|---|
|  |  |  | //                        orderMapper.addCoinsCoinsOrder(order); | 
|---|
|  |  |  | //                        BigDecimal totalReturn = BigDecimal.ZERO; | 
|---|
|  |  |  | //                        // 将需要退回的减去手续费 | 
|---|
|  |  |  | //                        BigDecimal needReturn = prePrice.add(new BigDecimal(profitLossPrice)); | 
|---|
|  |  |  | //                        //总退回金额=保证金+收益-手续费 | 
|---|
|  |  |  | //                        totalReturn = needReturn.subtract(new BigDecimal(order.getClosingPrice())); | 
|---|
|  |  |  | //                        // 更新钱包 | 
|---|
|  |  |  | //                       // Double availableBalance = totalReturn.add(new BigDecimal(wallet.getAvailableBalance())) | 
|---|
|  |  |  | //                        //        .doubleValue(); | 
|---|
|  |  |  | //                        Double availableBalance = totalReturn.doubleValue(); | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //                        //Double totalBalance = wallet.getTotalBalance() - order.getClosingPrice() + profitLossPrice; | 
|---|
|  |  |  | //                        Double totalBalance = -order.getClosingPrice() + profitLossPrice; | 
|---|
|  |  |  | // | 
|---|
|  |  |  | ////                        wallet.setAvailableBalance(availableBalance); | 
|---|
|  |  |  | ////                        wallet.setTotalBalance(totalBalance); | 
|---|
|  |  |  | ////                        int i = memberService.updateWallet(wallet); | 
|---|
|  |  |  | //                       walletDao.updateWallet(wallet.getId(),totalBalance,availableBalance,null); | 
|---|
|  |  |  | //                        // 流水记录 | 
|---|
|  |  |  | //                        MemberAccountFlowRecord record = new MemberAccountFlowRecord(); | 
|---|
|  |  |  | //                        record.setCreateTime(new Date()); | 
|---|
|  |  |  | //                        record.setDirectionSource("开多止损平仓"); | 
|---|
|  |  |  | //                        record.setRemark("开多止损平仓"); | 
|---|
|  |  |  | //                        record.setMemberBalance(wallet.getAvailableBalance()); | 
|---|
|  |  |  | //                        record.setMemberid(order.getMemberId()); | 
|---|
|  |  |  | //                        record.setSymbolName(order.getSymbol()); | 
|---|
|  |  |  | //                        record.setMemberName(wallet.getMemberName()); | 
|---|
|  |  |  | //                        record.setMemberPhone(order.getMemberPhone()); | 
|---|
|  |  |  | //                        record.setPrice(profitLossPrice + order.getPrePrice()); | 
|---|
|  |  |  | //                        memberService.addFlowRecord(record); | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //                        //返佣 TODO | 
|---|
|  |  |  | //                        calYj(order.getMemberId(), new BigDecimal(order.getClosingPrice()), order, 2); | 
|---|
|  |  |  | //                    } | 
|---|
|  |  |  | //                } | 
|---|
|  |  |  | //            } | 
|---|
|  |  |  | //        } | 
|---|
|  |  |  | //    } | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //    /** | 
|---|
|  |  |  | //     * 开空止损 | 
|---|
|  |  |  | //     * | 
|---|
|  |  |  | //     * @param | 
|---|
|  |  |  | //     */ | 
|---|
|  |  |  | //    public void dealForLessLoss(List<CoinsCoinsOrder> orderList,List<OrderModel> list) { | 
|---|
|  |  |  | //        // List<CoinsCoinsOrder> orderList = orderMapper.selectOrderByBatchNo(batchno); | 
|---|
|  |  |  | //        //System.out.println("开空止损批次号batchno:" + batchno); | 
|---|
|  |  |  | //        if (CollectionUtils.isNotEmpty(orderList)) { | 
|---|
|  |  |  | //            Map<Long,BigDecimal> modelMap = new HashMap<Long,BigDecimal>(); | 
|---|
|  |  |  | //            for(OrderModel model : list){ | 
|---|
|  |  |  | //                modelMap.put(model.getOrderId(),new BigDecimal(model.getPrice())); | 
|---|
|  |  |  | //            } | 
|---|
|  |  |  | //            for (CoinsCoinsOrder order : orderList) { | 
|---|
|  |  |  | //                System.out.println("传来的止损价格:"+modelMap.get(order.getId()).toPlainString()); | 
|---|
|  |  |  | //                System.out.println("开空止损订单号:" + order.getSerialno()); | 
|---|
|  |  |  | //                if (null != order.getStopLossPrice()) { | 
|---|
|  |  |  | //                    // 止损价 | 
|---|
|  |  |  | //                    BigDecimal closePrice = new BigDecimal(order.getStopLossPrice()).setScale(7,RoundingMode.HALF_UP); | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //                    System.out.println("订单止损价格:"+closePrice.toPlainString()); | 
|---|
|  |  |  | //                    BigDecimal queuePrice = modelMap.get(order.getId()).setScale(7, RoundingMode.HALF_UP); | 
|---|
|  |  |  | //                    if(closePrice.compareTo(queuePrice)!=0){ | 
|---|
|  |  |  | //                        System.out.println("数据库价格:"+queuePrice.toPlainString()+"--价格不能开空止损:"+closePrice); | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //                        orderMapper.updateOrderIsExitAndBatchno(order.getId()); | 
|---|
|  |  |  | //                        continue; | 
|---|
|  |  |  | //                    } | 
|---|
|  |  |  | //                    System.out.println("执行操作"); | 
|---|
|  |  |  | //                    String symbol = order.getSymbol(); | 
|---|
|  |  |  | //                    // 本次需要退回的预付款 | 
|---|
|  |  |  | //                    BigDecimal prePrice = new BigDecimal(order.getPrePrice()); | 
|---|
|  |  |  | //                    Wallet wallet = memberService.findWalletByMemberIdAndSymbol(order.getMemberId(), "USDT"); | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //                    if (wallet != null) { | 
|---|
|  |  |  | //                        // 更新订单 | 
|---|
|  |  |  | //                        order.setIsEixt(0); | 
|---|
|  |  |  | //                        orderMapper.updateOrder(order); | 
|---|
|  |  |  | //                        order.setExitPrice(order.getStopLossPrice());// 平仓价格 | 
|---|
|  |  |  | //                        order.setExitType(3);// 平仓类型,1:表示手动平仓,2:止盈平仓,3:止损平仓,4:爆仓 | 
|---|
|  |  |  | //                        order.setExitTime(new Date());// 平仓时间 | 
|---|
|  |  |  | //                        order.setEntrustStatus(8);// 平仓状态 | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //                        TradeSymbolSku symbolSku = tradeMapper.findSymbolSkubySymbol(symbol); | 
|---|
|  |  |  | //                        // 盈亏额度= (当前的币种的平仓价-下单时的建仓价)*购买的手数 | 
|---|
|  |  |  | //                        Double profitLossPrice = (new BigDecimal(order.getTradePrice())) | 
|---|
|  |  |  | //                                .subtract(closePrice) | 
|---|
|  |  |  | //                                .multiply(new BigDecimal(order.getSymbolSkuNumber())) | 
|---|
|  |  |  | //                                .multiply(symbolSku.getLotNumber()) | 
|---|
|  |  |  | //                                .setScale(8, BigDecimal.ROUND_DOWN).doubleValue(); | 
|---|
|  |  |  | //                        Member member = memberService.selectMemberById(order.getMemberId()); | 
|---|
|  |  |  | //                        if(member.getIsProfit() == 1) { | 
|---|
|  |  |  | //                           TradeSetting tradeSetting=tradeSettingCache.getTradeSetting(); | 
|---|
|  |  |  | //                           if(profitLossPrice>0) { | 
|---|
|  |  |  | //                              profitLossPrice = profitLossPrice*(1-tradeSetting.getProfitParam()); | 
|---|
|  |  |  | //                            }else { | 
|---|
|  |  |  | //                               profitLossPrice = profitLossPrice*(1+tradeSetting.getProfitParam());; | 
|---|
|  |  |  | //                            } | 
|---|
|  |  |  | //                        } | 
|---|
|  |  |  | //                        //回报率 | 
|---|
|  |  |  | //                        double returnRate = new BigDecimal(profitLossPrice).divide((new BigDecimal(order.getPrePrice()).subtract(new BigDecimal(order.getClosingPrice()))), 8, BigDecimal.ROUND_DOWN) | 
|---|
|  |  |  | //                                .doubleValue(); | 
|---|
|  |  |  | //                        order.setRewardRatio(profitLossPrice);// 盈亏 | 
|---|
|  |  |  | //                        order.setReturnRate(returnRate); | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //                        BigDecimal totalReturn = BigDecimal.ZERO; | 
|---|
|  |  |  | ////                           TradeSymbolSku symbolSku = tradeMapper.findSymbolSkubySymbol(symbol); | 
|---|
|  |  |  | //                        // 本次平仓数量 | 
|---|
|  |  |  | //                     //   Long currentFlat = order.getSymbolSkuNumber(); | 
|---|
|  |  |  | //                        // 查询交易设置 | 
|---|
|  |  |  | //                       // TradeSetting tradeSetting = tradeMapper.findTradeSetting(); | 
|---|
|  |  |  | //                        // 计算手续费 | 
|---|
|  |  |  | ////                        Double closingPrice = new BigDecimal(order.getStopLossPrice()) | 
|---|
|  |  |  | ////                                .multiply(new BigDecimal(tradeSetting.getClosingRatio()).divide(new BigDecimal(100), 8, BigDecimal.ROUND_DOWN)) | 
|---|
|  |  |  | ////                                .multiply(symbolSku.getLotNumber())// 规格 | 
|---|
|  |  |  | ////                                .multiply(new BigDecimal(currentFlat)).setScale(8, BigDecimal.ROUND_DOWN) | 
|---|
|  |  |  | ////                                .doubleValue(); | 
|---|
|  |  |  | //                        order.setSellClosingPrice(order.getClosingPrice()); | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //                        //添加止盈订单 | 
|---|
|  |  |  | //                        order.setId(null); | 
|---|
|  |  |  | //                        order.setStatus(12); | 
|---|
|  |  |  | //                        orderMapper.addCoinsCoinsOrder(order); | 
|---|
|  |  |  | //                        // 将需要退回的减去手续费 | 
|---|
|  |  |  | //                        BigDecimal needReturn = prePrice.add(new BigDecimal(profitLossPrice)); | 
|---|
|  |  |  | //                        //总退回金额=保证金+收益-手续费 | 
|---|
|  |  |  | //                        totalReturn = needReturn.subtract(new BigDecimal(order.getClosingPrice())); | 
|---|
|  |  |  | //                        // 更新钱包 | 
|---|
|  |  |  | //                       // Double availableBalance = totalReturn.add(new BigDecimal(wallet.getAvailableBalance())) | 
|---|
|  |  |  | //                        //        .doubleValue(); | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //                        Double availableBalance = totalReturn.doubleValue(); | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //                        //Double totalBalance = wallet.getTotalBalance() - order.getClosingPrice() + profitLossPrice; | 
|---|
|  |  |  | //                        Double totalBalance = profitLossPrice- order.getClosingPrice() ; | 
|---|
|  |  |  | // | 
|---|
|  |  |  | ////                        wallet.setAvailableBalance(availableBalance); | 
|---|
|  |  |  | ////                        wallet.setTotalBalance(totalBalance); | 
|---|
|  |  |  | ////                        int i = memberService.updateWallet(wallet); | 
|---|
|  |  |  | //                        walletDao.updateWallet(wallet.getId(),totalBalance,availableBalance,null); | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //                        // 流水记录 | 
|---|
|  |  |  | //                        MemberAccountFlowRecord record = new MemberAccountFlowRecord(); | 
|---|
|  |  |  | //                        record.setCreateTime(new Date()); | 
|---|
|  |  |  | //                        record.setDirectionSource("开空止损平仓"); | 
|---|
|  |  |  | //                        record.setRemark("开空止损平仓"); | 
|---|
|  |  |  | //                        record.setMemberBalance(wallet.getAvailableBalance()); | 
|---|
|  |  |  | //                        record.setMemberid(order.getMemberId()); | 
|---|
|  |  |  | //                        record.setSymbolName(order.getSymbol()); | 
|---|
|  |  |  | //                        record.setMemberName(wallet.getMemberName()); | 
|---|
|  |  |  | //                        record.setMemberPhone(order.getMemberPhone()); | 
|---|
|  |  |  | //                        record.setPrice(profitLossPrice + order.getPrePrice()); | 
|---|
|  |  |  | //                        memberService.addFlowRecord(record); | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //                        //返佣 TODO | 
|---|
|  |  |  | //                        calYj(order.getMemberId(), new BigDecimal(order.getClosingPrice()), order, 2); | 
|---|
|  |  |  | //                    } | 
|---|
|  |  |  | //                } | 
|---|
|  |  |  | //            } | 
|---|
|  |  |  | //        } | 
|---|
|  |  |  | //    } | 
|---|
|  |  |  | // | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //    /** | 
|---|
|  |  |  | //     * 限价委托 | 
|---|
|  |  |  | //     * | 
|---|
|  |  |  | //     * @param | 
|---|
|  |  |  | //     */ | 
|---|
|  |  |  | //    public void dealLimitBuyOrder(List<ContractEntrustOrderEntity> orderList) { | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //        //List<CoinsCoinsOrder> orderList = orderMapper.selectOrderByBatchNo(batchno); | 
|---|
|  |  |  | //        if (CollectionUtils.isNotEmpty(orderList)) { | 
|---|
|  |  |  | //            ContractHoldOrderEntity contractHoldOrderEntity=null; | 
|---|
|  |  |  | //            for (ContractEntrustOrderEntity coinsCoinsOrder : orderList) { | 
|---|
|  |  |  | //                MemberWalletContractEntity wallet = memberWalletContractService.findWalletContractByMemberIdAndSymbol(coinsCoinsOrder.getMemberId(), "USDT"); | 
|---|
|  |  |  | //                if (wallet == null) { | 
|---|
|  |  |  | //                    continue; | 
|---|
|  |  |  | //                } | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //                contractHoldOrderEntity = new ContractHoldOrderEntity(); | 
|---|
|  |  |  | //                Long memId = coinsCoinsOrder.getMemberId(); | 
|---|
|  |  |  | //                MemberEntity memberEntity = memberService.getById(memId); | 
|---|
|  |  |  | //                BigDecimal entrustPrice = coinsCoinsOrder.getEntrustPrice(); | 
|---|
|  |  |  | //                int symbolCnt = coinsCoinsOrder.getSymbolCnt(); | 
|---|
|  |  |  | //                int type = coinsCoinsOrder.getEntrustType(); | 
|---|
|  |  |  | //                //开仓价 | 
|---|
|  |  |  | //               // Double openPrice = coinsCoinsOrder.getOpenPrice().doubleValue(); | 
|---|
|  |  |  | //                //委托价 | 
|---|
|  |  |  | //                // Double markPrice = coinsCoinsOrder.getMarkPrice(); | 
|---|
|  |  |  | //                if (type == 1) { | 
|---|
|  |  |  | //                    // 开多 | 
|---|
|  |  |  | //                    contractHoldOrderEntity.setOpeningType(ContractHoldOrderEntity.ORDER_OPENING_TYPE_MORE); | 
|---|
|  |  |  | //                } else { | 
|---|
|  |  |  | //                    // 开空 | 
|---|
|  |  |  | //                    contractHoldOrderEntity.setOpeningType(ContractHoldOrderEntity.ORDER_OPENING_TYPE_LESS); | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //                } | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //                //持仓单赋值 | 
|---|
|  |  |  | //                contractHoldOrderEntity.setMemberId(memId); | 
|---|
|  |  |  | //                contractHoldOrderEntity.setIsCanClosing(ContractHoldOrderEntity.ORDER_CAN_CLOSING_Y); | 
|---|
|  |  |  | //                contractHoldOrderEntity.setMarkPrice(coinsCoinsOrder.getEntrustPrice()); | 
|---|
|  |  |  | //                contractHoldOrderEntity.setBondPrice(coinsCoinsOrder.getBondAmount()); | 
|---|
|  |  |  | //                // 开仓手续费 建仓价*规格*手数*手续费率 | 
|---|
|  |  |  | //                BigDecimal lotNumber = cacheSettingUtils.getSymbolSku(coinsCoinsOrder.getSymbol()); | 
|---|
|  |  |  | //                PlatformTradeSettingEntity tradeSettingEntity = cacheSettingUtils.getTradeSetting(); | 
|---|
|  |  |  | //                BigDecimal openFeePrice = coinsCoinsOrder.getEntrustPrice().multiply(lotNumber) | 
|---|
|  |  |  | //                        .multiply(new BigDecimal(coinsCoinsOrder.getSymbolCnt())) | 
|---|
|  |  |  | //                        .multiply(tradeSettingEntity.getFeeRatio().divide(new BigDecimal(100))) | 
|---|
|  |  |  | //                        .setScale(8, BigDecimal.ROUND_DOWN); | 
|---|
|  |  |  | //                contractHoldOrderEntity.setOpeningFeeAmount(openFeePrice); | 
|---|
|  |  |  | //                contractHoldOrderEntity.setVersion(1); | 
|---|
|  |  |  | //                BigDecimal forceSetPrice = CalculateUtil.getForceSetPrice(coinsCoinsOrder.getBondAmount(), entrustPrice, symbolCnt, lotNumber, type, memberEntity); | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //                contractHoldOrderEntity.setForceClosingPrice(forceSetPrice); | 
|---|
|  |  |  | //                contractHoldOrderEntity.setLeverRatio(coinsCoinsOrder.getLeverRatio()); | 
|---|
|  |  |  | //                contractHoldOrderEntity.setOpeningPrice(entrustPrice); | 
|---|
|  |  |  | //                contractHoldOrderService.save(contractHoldOrderEntity); | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //                // 需要一个历史插入 | 
|---|
|  |  |  | //                ContractOrderEntity contractOrderEntity = ContractHoldOrderEntityMapper.INSTANCE.holdOrderToOrder(contractHoldOrderEntity); | 
|---|
|  |  |  | //                contractOrderEntity.setId(null); | 
|---|
|  |  |  | //                contractOrderService.save(contractOrderEntity); | 
|---|
|  |  |  | //                // 发送爆仓的队列 | 
|---|
|  |  |  | //                // 市价 | 
|---|
|  |  |  | //                if (coinsCoinsOrder.getEntrustType() == 1) { | 
|---|
|  |  |  | //                    // 开多 | 
|---|
|  |  |  | //                    OrderModel model = new OrderModel(contractHoldOrderEntity.getId(), 6, contractHoldOrderEntity.getForceClosingPrice().toPlainString(), coinsCoinsOrder.getSymbol(),1); | 
|---|
|  |  |  | //                    producer.sendPriceOperate(JSONObject.toJSONString(model)); | 
|---|
|  |  |  | //                } else { | 
|---|
|  |  |  | //                    // 开空 | 
|---|
|  |  |  | //                    OrderModel model = new OrderModel(contractHoldOrderEntity.getId(), 7, contractHoldOrderEntity.getForceClosingPrice().toPlainString(), coinsCoinsOrder.getSymbol(),1); | 
|---|
|  |  |  | //                    producer.sendPriceOperate(JSONObject.toJSONString(model)); | 
|---|
|  |  |  | //                } | 
|---|
|  |  |  | //                // 扣除手续费 | 
|---|
|  |  |  | //                //double totalBalance = wallet.getTotalBalance() - coinsCoinsOrder.getClosingPrice(); | 
|---|
|  |  |  | //                BigDecimal totalBalance =  openFeePrice.negate(); | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //               /** TradeSetting tradeSetting = tradeSettingCache.getTradeSetting(); | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //                Double totalPayPrice = new BigDecimal(coinsCoinsOrder.getPrePrice()).add(new BigDecimal(coinsCoinsOrder.getClosingPrice())).add(new BigDecimal(coinsCoinsOrder.getClosingPrice())) | 
|---|
|  |  |  | //                        .setScale(8, BigDecimal.ROUND_HALF_UP).doubleValue(); | 
|---|
|  |  |  | //                totalBalance =  totalBalance+(-totalPayPrice*(Double.valueOf(tradeSetting.getSpread())/10000));*/ | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //                memberWalletContractService.increaseWalletContractBalanceById(null,totalBalance,null,wallet.getId()); | 
|---|
|  |  |  | //                // TODO 531 待写 | 
|---|
|  |  |  | //                //calYj(memId, new BigDecimal(coinsCoinsOrder.getClosingPrice()), coinsCoinsOrder, 1); | 
|---|
|  |  |  | //            } | 
|---|
|  |  |  | //        } | 
|---|
|  |  |  | //    } | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //    /** | 
|---|
|  |  |  | //     * 爆仓 | 
|---|
|  |  |  | //     * | 
|---|
|  |  |  | //     * @param | 
|---|
|  |  |  | //     */ | 
|---|
|  |  |  | //    public void dealCoinOut(List<ContractHoldOrderEntity> orderList,List<OrderModel> orderModels) { | 
|---|
|  |  |  | //        // 需要比较查询到的和消息发来的单的爆仓操作次序号是否相同 | 
|---|
|  |  |  | //        // 构建map | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //        if (CollectionUtils.isNotEmpty(orderList)) { | 
|---|
|  |  |  | //            Map<Long,Integer> modelMap = new HashMap<Long,Integer>(); | 
|---|
|  |  |  | //            for(OrderModel model : orderModels){ | 
|---|
|  |  |  | //                modelMap.put(model.getOrderId(),model.getOperateNo()); | 
|---|
|  |  |  | //            } | 
|---|
|  |  |  | //           // TradeSetting tradeSetting = tradeMapper.findTradeSetting(); | 
|---|
|  |  |  | //            for (ContractHoldOrderEntity coinsOrder : orderList) { | 
|---|
|  |  |  | //                Long orderId = coinsOrder.getId(); | 
|---|
|  |  |  | //                Integer operateNo = coinsOrder.getOperateNo(); | 
|---|
|  |  |  | //                //判断当前订单是否是最新的爆仓价 不相等时直接进入下一个订单 | 
|---|
|  |  |  | //                if(!modelMap.get(orderId).equals(operateNo)){ | 
|---|
|  |  |  | //                    // 将订单更新为可平仓并删除批次号 | 
|---|
|  |  |  | //                    contractHoldOrderService.updateOrderIsCanClosingAndBatchNoById(orderId); | 
|---|
|  |  |  | //                    continue; | 
|---|
|  |  |  | //                } | 
|---|
|  |  |  | //                boolean isDone = false; | 
|---|
|  |  |  | //                Long memId = coinsOrder.getMemberId(); | 
|---|
|  |  |  | //                MemberEntity byId = memberService.getById(memId); | 
|---|
|  |  |  | //                String symbol = coinsOrder.getSymbol(); | 
|---|
|  |  |  | //                //TradeSymbolSku symbolSku = tradeMapper.findSymbolSkubySymbol(symbol); | 
|---|
|  |  |  | //                BigDecimal nowPrice = coinsOrder.getForceClosingPrice(); | 
|---|
|  |  |  | //                // 创建订单(加入历史表的订单) | 
|---|
|  |  |  | //                ContractOrderEntity contractOrderEntity = ContractHoldOrderEntityMapper.INSTANCE.holdOrderToOrder(coinsOrder); | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //                //查询是否满足爆仓条件 | 
|---|
|  |  |  | //                if (coinsOrder.getOpeningType() == ContractHoldOrderEntity.ORDER_OPENING_TYPE_MORE) { | 
|---|
|  |  |  | //                    //如果是开多,当前价小于预估强平价即为爆仓 | 
|---|
|  |  |  | //                    // 设置平仓类型 // 爆仓平多 | 
|---|
|  |  |  | //                    contractOrderEntity.setClosingType(4); | 
|---|
|  |  |  | //                    //更新用户钱包数据 | 
|---|
|  |  |  | //                    isDone = true; | 
|---|
|  |  |  | //                } else { | 
|---|
|  |  |  | //                    //如果是开空,当前价大于预估强平价即为爆仓 | 
|---|
|  |  |  | //                    contractOrderEntity.setClosingType(5); | 
|---|
|  |  |  | //                    //更新主表订单状态位为“已平仓” | 
|---|
|  |  |  | //                    isDone = true; | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //                } | 
|---|
|  |  |  | //                if (isDone) { | 
|---|
|  |  |  | //                    //删除次仓订单 | 
|---|
|  |  |  | //                    contractHoldOrderService.removeById(orderId); | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //                    //系统自动平仓(爆仓) | 
|---|
|  |  |  | ////                    Double closingPrice = nowPrice | 
|---|
|  |  |  | ////                            .multiply(new BigDecimal(tradeSetting.getClosingRatio()).divide(new BigDecimal(100), 8, BigDecimal.ROUND_DOWN)) | 
|---|
|  |  |  | ////                            .multiply(symbolSku.getLotNumber())// 规格 | 
|---|
|  |  |  | ////                            .multiply(new BigDecimal(coinsOrder.getSymbolSkuNumber())) | 
|---|
|  |  |  | ////                            .setScale(8, BigDecimal.ROUND_DOWN).doubleValue(); | 
|---|
|  |  |  | //                    //更新主表订单状态位为“已平仓” | 
|---|
|  |  |  | //                    contractOrderEntity.setId(null); | 
|---|
|  |  |  | //                    contractOrderEntity.setClosingPrice(BigDecimal.ZERO); | 
|---|
|  |  |  | //                    //order.setPayTotalPrice(coinsOrder.getPrePrice()+coinsOrder.getClosingPrice());// 总支付金额(预付款) | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //                    contractOrderEntity.setClosingPrice(nowPrice); | 
|---|
|  |  |  | //                    contractOrderEntity.setClosingTime(new Date()); | 
|---|
|  |  |  | //                    contractOrderEntity.setRewardAmount((coinsOrder.getOpeningFeeAmount().multiply(new BigDecimal("2")).subtract(coinsOrder.getPrePaymentAmount()))); | 
|---|
|  |  |  | //                    //order.setRewardRatio(-(coinsOrder.getPrePrice() - (2*coinsOrder.getClosingPrice()))); | 
|---|
|  |  |  | //                    contractOrderService.save(contractOrderEntity); | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //                    //更新用户钱包数据 | 
|---|
|  |  |  | //                    MemberWalletContractEntity usdt = memberWalletContractService.findWalletContractByMemberIdAndSymbol(memId, "USDT"); | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //                   // Double totalPrice = wallet.getTotalBalance() - coinsOrder.getPrePrice(); | 
|---|
|  |  |  | //                    // 减去的时候用负数 | 
|---|
|  |  |  | //                    BigDecimal totalPrice = coinsOrder.getPrePaymentAmount().negate(); | 
|---|
|  |  |  | //                    //Double totalPrice = - coinsOrder.getPrePrice(); | 
|---|
|  |  |  | //                    memberWalletContractService.increaseWalletContractBalanceById(null,totalPrice,null,usdt.getId()); | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //                    // 流水记录 TODO | 
|---|
|  |  |  | ////                    MemberAccountFlowRecord record = new MemberAccountFlowRecord(); | 
|---|
|  |  |  | ////                    record.setCreateTime(new Date()); | 
|---|
|  |  |  | ////                    record.setDirectionSource("系统自动平仓"); | 
|---|
|  |  |  | ////                    record.setRemark("系统自动平仓"); | 
|---|
|  |  |  | ////                    record.setMemberBalance(coinsOrder.getPrePrice()); | 
|---|
|  |  |  | ////                    record.setMemberid(memId); | 
|---|
|  |  |  | ////                    record.setSymbolName(null); | 
|---|
|  |  |  | ////                    record.setMemberName(null); | 
|---|
|  |  |  | ////                    record.setMemberPhone(null); | 
|---|
|  |  |  | ////                    record.setPrice(coinsOrder.getPrePrice()); | 
|---|
|  |  |  | ////                    memberService.addFlowRecord(record); | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //                } | 
|---|
|  |  |  | //            } | 
|---|
|  |  |  | //        } | 
|---|
|  |  |  | //    } | 
|---|
|  |  |  | // | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //    public void calYj(Long mid, BigDecimal money, CoinsCoinsOrder order, int type) { | 
|---|
|  |  |  | //       if(money!=null) { | 
|---|
|  |  |  | //          money = money.multiply(new BigDecimal(0.7868)); | 
|---|
|  |  |  | //       } | 
|---|
|  |  |  | //        Member member = memberService.selectMemberById(mid); | 
|---|
|  |  |  | //        String[] referenceIds = member.getReferenceIds().split(","); | 
|---|
|  |  |  | //        List<String> ids = Arrays.asList(referenceIds); | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //        // 判断该用户是否为代理商 | 
|---|
|  |  |  | //        NeedMoneyMember needMoneyMember = memberMapper.selectFriendRelationUserByMid(mid); | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //        // 查询该用户下所有需要返佣的代理商 | 
|---|
|  |  |  | //        List<NeedMoneyMember> list = memberMapper.selectAllNeedMoneyMember(ids); | 
|---|
|  |  |  | //        TreeMap<String, NeedMoneyMember> treeMap = new TreeMap<>(new Comparator<String>() { | 
|---|
|  |  |  | //            @Override | 
|---|
|  |  |  | //            public int compare(String o1, String o2) { | 
|---|
|  |  |  | //                return o2.compareTo(o1); | 
|---|
|  |  |  | //            } | 
|---|
|  |  |  | //        }); | 
|---|
|  |  |  | //        // 建立层级关系 | 
|---|
|  |  |  | //        for (int i = 0; i < list.size(); i++) { | 
|---|
|  |  |  | //            treeMap.put(list.get(i).getLevelId(), list.get(i)); | 
|---|
|  |  |  | //        } | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //        // 该用户为代理商则判断is_self字段,判断是否保留其手续费 | 
|---|
|  |  |  | //        // 该用户为代理商则判断is_self字段,判断是否保留其手续费 | 
|---|
|  |  |  | //        if (needMoneyMember != null && needMoneyMember.getIsSelf() == 1) { | 
|---|
|  |  |  | //            treeMap.put(needMoneyMember.getLevelId(), needMoneyMember); | 
|---|
|  |  |  | //        } | 
|---|
|  |  |  | // | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //        // 存放uid以及对应uid用户的佣金 | 
|---|
|  |  |  | //        Map<String, BigDecimal> map = new HashMap<>(); | 
|---|
|  |  |  | //        Iterator<Map.Entry<String, NeedMoneyMember>> it = treeMap.entrySet().iterator(); | 
|---|
|  |  |  | //        BigDecimal lastRate = BigDecimal.ZERO; | 
|---|
|  |  |  | //        BigDecimal lastYj = BigDecimal.ZERO; | 
|---|
|  |  |  | //        while (it.hasNext()) { | 
|---|
|  |  |  | //            Map.Entry<String, NeedMoneyMember> entry = it.next(); | 
|---|
|  |  |  | //            NeedMoneyMember member1 = entry.getValue(); | 
|---|
|  |  |  | //            // 上下级佣金比率相减后乘以手续费 -- 即用户所得佣金 | 
|---|
|  |  |  | //            lastYj = (member1.getReturnRate().subtract(lastRate)).multiply(money); | 
|---|
|  |  |  | //            lastRate = member1.getReturnRate(); | 
|---|
|  |  |  | //            map.put(member1.getUid(), lastYj); | 
|---|
|  |  |  | //        } | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //        // 输出对应佣金是否正确 | 
|---|
|  |  |  | //        Iterator<Map.Entry<String, BigDecimal>> it1 = map.entrySet().iterator(); | 
|---|
|  |  |  | //        List<AgentReturn> agentList = new ArrayList<AgentReturn>(); | 
|---|
|  |  |  | //        while (it1.hasNext()) { | 
|---|
|  |  |  | //            Map.Entry<String, BigDecimal> entry = it1.next(); | 
|---|
|  |  |  | //            // System.out.println(entry.getKey() + "-----" + entry.getValue()); | 
|---|
|  |  |  | //            Member agentMember = memberMapper.findMemberByUID(entry.getKey()); | 
|---|
|  |  |  | //            AgentReturn agent = new AgentReturn(); | 
|---|
|  |  |  | //            agent.setMemId(mid); | 
|---|
|  |  |  | //            agent.setOrderId(order.getId()); | 
|---|
|  |  |  | //            agent.setOrderNo(order.getSerialno()); | 
|---|
|  |  |  | //            agent.setAgentId(agentMember.getmId()); | 
|---|
|  |  |  | //            agent.setReturnType(order.getStatus()); | 
|---|
|  |  |  | //            agent.setReturnTime(new Date()); | 
|---|
|  |  |  | //            agent.setReturnSymbol(order.getSymbol()); | 
|---|
|  |  |  | //            agent.setIsReturn(0); | 
|---|
|  |  |  | //            agent.setReturnMoney(entry.getValue()); | 
|---|
|  |  |  | //            if (type == 1) {//开仓 | 
|---|
|  |  |  | //                agent.setOpenPrice(new BigDecimal(order.getClosingPrice())); | 
|---|
|  |  |  | //            } else if (type == 2) {//平仓 | 
|---|
|  |  |  | //                agent.setClosePrice(new BigDecimal(order.getClosingPrice())); | 
|---|
|  |  |  | //            } else {//持仓费 | 
|---|
|  |  |  | //                agent.setDoingPrice(order.getDoingPrice()); | 
|---|
|  |  |  | //            } | 
|---|
|  |  |  | //            agent.setReturnMenId(agentMember.getmId()); | 
|---|
|  |  |  | //            agent.setUid(entry.getKey()); | 
|---|
|  |  |  | //            agent.setOrderType(order.getStatus()); | 
|---|
|  |  |  | //            agentReturnMapper.insert(agent); | 
|---|
|  |  |  | //        } | 
|---|
|  |  |  | // | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //    } | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //} | 
|---|
|  |  |  | package com.xcong.excoin.modules.contract.service.impl; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.alibaba.fastjson.JSONObject; | 
|---|
|  |  |  | import com.xcong.excoin.modules.coin.dao.MemberAccountFlowEntityDao; | 
|---|
|  |  |  | import com.xcong.excoin.modules.coin.entity.MemberAccountFlowEntity; | 
|---|
|  |  |  | import com.xcong.excoin.modules.contract.entity.ContractEntrustOrderEntity; | 
|---|
|  |  |  | import com.xcong.excoin.modules.contract.entity.ContractHoldOrderEntity; | 
|---|
|  |  |  | import com.xcong.excoin.modules.contract.entity.ContractOrderEntity; | 
|---|
|  |  |  | import com.xcong.excoin.modules.contract.mapper.ContractEntrustOrderEntityMapper; | 
|---|
|  |  |  | import com.xcong.excoin.modules.contract.mapper.ContractHoldOrderEntityMapper; | 
|---|
|  |  |  | import com.xcong.excoin.modules.contract.service.ContractEntrustOrderService; | 
|---|
|  |  |  | 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.entity.AgentReturnEntity; | 
|---|
|  |  |  | import com.xcong.excoin.modules.member.entity.MemberEntity; | 
|---|
|  |  |  | 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; | 
|---|
|  |  |  | import com.xcong.excoin.modules.member.service.MemberWalletContractService; | 
|---|
|  |  |  | import com.xcong.excoin.modules.platform.entity.PlatformTradeSettingEntity; | 
|---|
|  |  |  | import com.xcong.excoin.rabbit.pricequeue.OrderModel; | 
|---|
|  |  |  | import com.xcong.excoin.rabbit.producer.OrderProducer; | 
|---|
|  |  |  | import com.xcong.excoin.utils.CacheSettingUtils; | 
|---|
|  |  |  | import com.xcong.excoin.utils.CalculateUtil; | 
|---|
|  |  |  | import com.xcong.excoin.utils.ThreadPoolUtils; | 
|---|
|  |  |  | import lombok.extern.slf4j.Slf4j; | 
|---|
|  |  |  | import org.apache.commons.collections.CollectionUtils; | 
|---|
|  |  |  | import org.springframework.stereotype.Service; | 
|---|
|  |  |  | import org.springframework.transaction.annotation.Transactional; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import javax.annotation.Resource; | 
|---|
|  |  |  | import java.math.BigDecimal; | 
|---|
|  |  |  | import java.math.RoundingMode; | 
|---|
|  |  |  | import java.util.*; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * @author helius | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @Slf4j | 
|---|
|  |  |  | @Service | 
|---|
|  |  |  | public class OrderWebsocketServiceImpl { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Resource | 
|---|
|  |  |  | ContractHoldOrderService contractHoldOrderService; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Resource | 
|---|
|  |  |  | ContractOrderService contractOrderService; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Resource | 
|---|
|  |  |  | ContractEntrustOrderService contractEntrustOrderService; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Resource | 
|---|
|  |  |  | MemberWalletContractService memberWalletContractService; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Resource | 
|---|
|  |  |  | CacheSettingUtils cacheSettingUtils; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Resource | 
|---|
|  |  |  | OrderProducer producer; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Resource | 
|---|
|  |  |  | MemberService memberService; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Resource | 
|---|
|  |  |  | private AgentReturnDao agentReturnDao; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Resource | 
|---|
|  |  |  | private MemberAccountFlowEntityDao memberAccountFlowEntityDao; | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public void dealOrderFromMq(List<OrderModel> list, Integer type) { | 
|---|
|  |  |  | if (CollectionUtils.isNotEmpty(list)) { | 
|---|
|  |  |  | String batchno = UUID.randomUUID().toString(); | 
|---|
|  |  |  | // 更新订单状态 | 
|---|
|  |  |  | // 更新为不可平仓状态 | 
|---|
|  |  |  | int count = contractHoldOrderService.updateContractHoldOrderCanNotClosingByIds(list, batchno); | 
|---|
|  |  |  | // 查询 | 
|---|
|  |  |  | if (count > 0) { | 
|---|
|  |  |  | // 查询 | 
|---|
|  |  |  | List<ContractHoldOrderEntity> coinsCoinsOrders = contractHoldOrderService.selectContractHoldOrderByBatchNo(batchno); | 
|---|
|  |  |  | // 根据状态调用不同的方法 | 
|---|
|  |  |  | // 1:买入委托2:开多3:开空4:平多5:平空6:爆仓平多7:爆仓平空8:撤单9:止盈平多10:止盈平空11:止损平多12:止损平空 | 
|---|
|  |  |  | // 6在这里是爆仓 包括爆空和暴多 | 
|---|
|  |  |  | switch (type) { | 
|---|
|  |  |  | case 6: | 
|---|
|  |  |  | this.dealCoinOut(coinsCoinsOrders, list); | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | case 9: | 
|---|
|  |  |  | this.dealForMoreStopPro(coinsCoinsOrders, list); | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | case 10: | 
|---|
|  |  |  | this.dealForLessStopPro(coinsCoinsOrders, list); | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | case 11: | 
|---|
|  |  |  | this.dealForMoreLoss(coinsCoinsOrders, list); | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | case 12: | 
|---|
|  |  |  | this.dealForLessLoss(coinsCoinsOrders, list); | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | default: | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public void dealForLimitMq(List<OrderModel> list) { | 
|---|
|  |  |  | if (CollectionUtils.isNotEmpty(list)) { | 
|---|
|  |  |  | //查询限价委托的单 | 
|---|
|  |  |  | String batchno = UUID.randomUUID().toString(); | 
|---|
|  |  |  | List<Long> ids = new ArrayList<>(); | 
|---|
|  |  |  | list.forEach(model -> ids.add(model.getOrderId())); | 
|---|
|  |  |  | List<ContractEntrustOrderEntity> contractEntrustOrderEntities = contractEntrustOrderService.selectEntrustOrderListByIds(ids); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (CollectionUtils.isNotEmpty(contractEntrustOrderEntities)) { | 
|---|
|  |  |  | this.dealLimitBuyOrder(contractEntrustOrderEntities); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 开多止盈 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @Transactional(rollbackFor = Exception.class) | 
|---|
|  |  |  | public void dealForMoreStopPro(List<ContractHoldOrderEntity> orderList, List<OrderModel> list) { | 
|---|
|  |  |  | if (CollectionUtils.isNotEmpty(orderList)) { | 
|---|
|  |  |  | Map<Long, BigDecimal> modelMap = new HashMap<Long, BigDecimal>(); | 
|---|
|  |  |  | for (OrderModel model : list) { | 
|---|
|  |  |  | modelMap.put(model.getOrderId(), new BigDecimal(model.getPrice())); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | for (ContractHoldOrderEntity order : orderList) { | 
|---|
|  |  |  | Long orderId = order.getId(); | 
|---|
|  |  |  | System.out.println("开多止盈订单号:" + order.getOrderNo()); | 
|---|
|  |  |  | System.out.println("传来的止盈价格:" + modelMap.get(order.getId())); | 
|---|
|  |  |  | if (null != order.getStopProfitPrice()) { | 
|---|
|  |  |  | BigDecimal closePrice = order.getStopProfitPrice(); | 
|---|
|  |  |  | BigDecimal queuePrice = modelMap.get(order.getId()); | 
|---|
|  |  |  | System.out.println("订单的止盈价格:" + closePrice); | 
|---|
|  |  |  | // 判断 保留七位是为了忽略以为小数 防止不精确导致的不相等 | 
|---|
|  |  |  | if (queuePrice.compareTo(closePrice) != 0) { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 不能止盈 | 
|---|
|  |  |  | System.out.println("数据库价格:" + queuePrice.toPlainString() + "--价格不能止盈:" + closePrice); | 
|---|
|  |  |  | //更新数据 | 
|---|
|  |  |  | contractHoldOrderService.updateOrderIsCanClosingAndBatchNoById(orderId); | 
|---|
|  |  |  | continue; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | System.out.println("执行操作"); | 
|---|
|  |  |  | // 止盈价 | 
|---|
|  |  |  | String symbol = order.getSymbol(); | 
|---|
|  |  |  | // 本次需要退回的保证金 | 
|---|
|  |  |  | BigDecimal prePrice = order.getBondAmount(); | 
|---|
|  |  |  | Long memberId = order.getMemberId(); | 
|---|
|  |  |  | MemberWalletContractEntity wallet = memberWalletContractService.findWalletContractByMemberIdAndSymbol(memberId, "USDT"); | 
|---|
|  |  |  | if (wallet != null) { | 
|---|
|  |  |  | // 历史订单 | 
|---|
|  |  |  | ContractOrderEntity contractOrderEntity = ContractHoldOrderEntityMapper.INSTANCE.holdOrderToOrder(order); | 
|---|
|  |  |  | contractOrderEntity.setClosingTime(new Date()); | 
|---|
|  |  |  | contractOrderEntity.setId(null); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 本次平仓数量 | 
|---|
|  |  |  | int currentFlat = order.getSymbolCnt(); | 
|---|
|  |  |  | BigDecimal symbolSku = cacheSettingUtils.getSymbolSku(order.getSymbol()); | 
|---|
|  |  |  | // 盈亏额度= (当前的币种的平仓价-下单时的建仓价)*购买的手数/规格*倍率 | 
|---|
|  |  |  | BigDecimal profitLossPrice = (closePrice | 
|---|
|  |  |  | .subtract(order.getOpeningPrice())) | 
|---|
|  |  |  | .multiply(new BigDecimal(currentFlat)) | 
|---|
|  |  |  | .multiply(symbolSku).setScale(8, BigDecimal.ROUND_DOWN); | 
|---|
|  |  |  | MemberEntity memberEntity = memberService.getById(memberId); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 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())); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //回报率 | 
|---|
|  |  |  | BigDecimal returnRate = profitLossPrice.divide((order.getBondAmount().subtract(contractOrderEntity.getOpeningFeeAmount())), 8, BigDecimal.ROUND_DOWN); | 
|---|
|  |  |  | contractOrderEntity.setRewardAmount(profitLossPrice); | 
|---|
|  |  |  | contractOrderEntity.setRewardRatio(returnRate); | 
|---|
|  |  |  | contractOrderEntity.setClosingFeeAmount(order.getOpeningFeeAmount()); | 
|---|
|  |  |  | contractOrderEntity.setClosingPrice(closePrice); | 
|---|
|  |  |  | contractOrderEntity.setClosingType(6); | 
|---|
|  |  |  | contractOrderEntity.setOrderType(ContractOrderEntity.ORDER_TYPE_CLOSE_MORE); | 
|---|
|  |  |  | BigDecimal totalReturn = BigDecimal.ZERO; | 
|---|
|  |  |  | contractOrderService.save(contractOrderEntity); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | contractHoldOrderService.removeById(order.getId()); | 
|---|
|  |  |  | // 将需要退回的减去手续费 | 
|---|
|  |  |  | BigDecimal needReturn = prePrice.add(profitLossPrice); | 
|---|
|  |  |  | //总退回金额=保证金+收益-手续费 | 
|---|
|  |  |  | totalReturn = needReturn.subtract(contractOrderEntity.getClosingFeeAmount()); | 
|---|
|  |  |  | // 总的是收益-平仓手续费 | 
|---|
|  |  |  | BigDecimal totalBalance = profitLossPrice.subtract(contractOrderEntity.getClosingFeeAmount()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | memberWalletContractService.increaseWalletContractBalanceById(totalReturn, totalBalance, null, wallet.getId()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 流水记录 TODO 531e | 
|---|
|  |  |  | insertAccountFlow(order, wallet, profitLossPrice, "止盈平仓"); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //返佣 | 
|---|
|  |  |  | ThreadPoolUtils.calReturnMoney(order.getMemberId(), order.getOpeningFeeAmount(), contractOrderEntity, AgentReturnEntity.ORDER_TYPE_CLOSE); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 开空止盈 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public void dealForLessStopPro(List<ContractHoldOrderEntity> orderList, List<OrderModel> list) { | 
|---|
|  |  |  | //List<CoinsCoinsOrder> orderList = orderMapper.selectOrderByBatchNo(batchno); | 
|---|
|  |  |  | //System.out.println("开空止盈订单batchno:" + batchno); | 
|---|
|  |  |  | if (CollectionUtils.isNotEmpty(orderList)) { | 
|---|
|  |  |  | Map<Long, BigDecimal> modelMap = new HashMap<Long, BigDecimal>(); | 
|---|
|  |  |  | for (OrderModel model : list) { | 
|---|
|  |  |  | modelMap.put(model.getOrderId(), new BigDecimal(model.getPrice())); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | for (ContractHoldOrderEntity order : orderList) { | 
|---|
|  |  |  | System.out.println("开空止盈订单号:" + order.getOrderNo()); | 
|---|
|  |  |  | System.out.println("传来的止盈价格:" + modelMap.get(order.getId()).toPlainString()); | 
|---|
|  |  |  | BigDecimal closePrice = order.getStopProfitPrice(); | 
|---|
|  |  |  | Long orderId = order.getId(); | 
|---|
|  |  |  | if (null != closePrice) { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 止盈价 | 
|---|
|  |  |  | System.out.println("订单的止盈价格:" + closePrice); | 
|---|
|  |  |  | System.out.println(closePrice.compareTo(modelMap.get(order.getId()))); | 
|---|
|  |  |  | BigDecimal queuePrice = modelMap.get(order.getId()).setScale(7, RoundingMode.HALF_UP); | 
|---|
|  |  |  | if (closePrice.compareTo(queuePrice) != 0) { | 
|---|
|  |  |  | System.out.println("数据库价格:" + queuePrice.toPlainString() + "--价格不能开空止盈:" + closePrice); | 
|---|
|  |  |  | contractHoldOrderService.updateOrderIsCanClosingAndBatchNoById(orderId); | 
|---|
|  |  |  | continue; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | System.out.println("执行操作"); | 
|---|
|  |  |  | String symbol = order.getSymbol(); | 
|---|
|  |  |  | // 本次需要退回的预付款 | 
|---|
|  |  |  | BigDecimal prePrice = order.getBondAmount(); | 
|---|
|  |  |  | Long memberId = order.getMemberId(); | 
|---|
|  |  |  | MemberWalletContractEntity wallet = memberWalletContractService.findWalletContractByMemberIdAndSymbol(memberId, "USDT"); | 
|---|
|  |  |  | if (wallet != null) { | 
|---|
|  |  |  | // 历史订单 | 
|---|
|  |  |  | ContractOrderEntity contractOrderEntity = ContractHoldOrderEntityMapper.INSTANCE.holdOrderToOrder(order); | 
|---|
|  |  |  | contractOrderEntity.setClosingTime(new Date()); | 
|---|
|  |  |  | contractOrderEntity.setId(null); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 本次平仓数量 | 
|---|
|  |  |  | int currentFlat = order.getSymbolCnt(); | 
|---|
|  |  |  | BigDecimal symbolSku = cacheSettingUtils.getSymbolSku(order.getSymbol()); | 
|---|
|  |  |  | // 盈亏额度= (当前的币种的平仓价-下单时的建仓价)*购买的手数/规格*倍率 | 
|---|
|  |  |  | BigDecimal profitLossPrice = (order.getOpeningPrice() | 
|---|
|  |  |  | .subtract(closePrice)) | 
|---|
|  |  |  | .multiply(new BigDecimal(currentFlat + "")) | 
|---|
|  |  |  | .multiply(symbolSku).setScale(8, BigDecimal.ROUND_DOWN); | 
|---|
|  |  |  | MemberEntity memberEntity = memberService.getById(memberId); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 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())); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //回报率 | 
|---|
|  |  |  | BigDecimal returnRate = profitLossPrice.divide((order.getBondAmount().subtract(contractOrderEntity.getOpeningFeeAmount())), 8, BigDecimal.ROUND_DOWN); | 
|---|
|  |  |  | contractOrderEntity.setRewardAmount(profitLossPrice); | 
|---|
|  |  |  | contractOrderEntity.setRewardRatio(returnRate); | 
|---|
|  |  |  | contractOrderEntity.setClosingFeeAmount(order.getOpeningFeeAmount()); | 
|---|
|  |  |  | contractOrderEntity.setClosingPrice(closePrice); | 
|---|
|  |  |  | contractOrderEntity.setClosingType(7); | 
|---|
|  |  |  | contractOrderEntity.setOrderType(ContractOrderEntity.ORDER_TYPE_CLOSE_MORE); | 
|---|
|  |  |  | BigDecimal totalReturn = BigDecimal.ZERO; | 
|---|
|  |  |  | contractOrderService.save(contractOrderEntity); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | contractHoldOrderService.removeById(order.getId()); | 
|---|
|  |  |  | // 将需要退回的减去手续费 | 
|---|
|  |  |  | BigDecimal needReturn = prePrice.add(profitLossPrice); | 
|---|
|  |  |  | //总退回金额=保证金+收益-手续费 | 
|---|
|  |  |  | totalReturn = needReturn.subtract(contractOrderEntity.getClosingFeeAmount()); | 
|---|
|  |  |  | // 更新钱包 | 
|---|
|  |  |  | // 总的是收益-平仓手续费 | 
|---|
|  |  |  | BigDecimal totalBalance = profitLossPrice.subtract(contractOrderEntity.getClosingFeeAmount()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | memberWalletContractService.increaseWalletContractBalanceById(totalReturn, totalBalance, null, wallet.getId()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | insertAccountFlow(order, wallet, profitLossPrice, "止盈平仓"); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //返佣 | 
|---|
|  |  |  | ThreadPoolUtils.calReturnMoney(order.getMemberId(), order.getOpeningFeeAmount(), contractOrderEntity, AgentReturnEntity.ORDER_TYPE_CLOSE); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 开多止损 | 
|---|
|  |  |  | * | 
|---|
|  |  |  | * @param | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public void dealForMoreLoss(List<ContractHoldOrderEntity> orderList, List<OrderModel> list) { | 
|---|
|  |  |  | //List<CoinsCoinsOrder> orderList = orderMapper.selectOrderByBatchNo(batchno); | 
|---|
|  |  |  | //System.out.println("开多止损批次号batchno:" + batchno); | 
|---|
|  |  |  | if (CollectionUtils.isNotEmpty(orderList)) { | 
|---|
|  |  |  | Map<Long, BigDecimal> modelMap = new HashMap<Long, BigDecimal>(); | 
|---|
|  |  |  | for (OrderModel model : list) { | 
|---|
|  |  |  | modelMap.put(model.getOrderId(), new BigDecimal(model.getPrice())); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | for (ContractHoldOrderEntity order : orderList) { | 
|---|
|  |  |  | System.out.println("开多止损订单号:" + order.getOrderNo()); | 
|---|
|  |  |  | Long orderId = order.getId(); | 
|---|
|  |  |  | System.out.println("传来的止损价格:" + modelMap.get(orderId)); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (null != order.getStopLossPrice()) { | 
|---|
|  |  |  | // 止损价 | 
|---|
|  |  |  | BigDecimal closePrice = order.getStopLossPrice(); | 
|---|
|  |  |  | System.out.println("订单止损价格:" + closePrice.toPlainString()); | 
|---|
|  |  |  | BigDecimal queuePrice = modelMap.get(orderId); | 
|---|
|  |  |  | if (closePrice.compareTo(queuePrice) != 0) { | 
|---|
|  |  |  | System.out.println("数据库价格:" + queuePrice.toPlainString() + "--价格不能开多止损:" + closePrice); | 
|---|
|  |  |  | contractHoldOrderService.updateOrderIsCanClosingAndBatchNoById(orderId); | 
|---|
|  |  |  | continue; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | System.out.println("执行操作"); | 
|---|
|  |  |  | String symbol = order.getSymbol(); | 
|---|
|  |  |  | Long memberId = order.getMemberId(); | 
|---|
|  |  |  | // 本次需要退回的预付款 | 
|---|
|  |  |  | BigDecimal prePrice = order.getBondAmount(); | 
|---|
|  |  |  | MemberWalletContractEntity wallet = memberWalletContractService.findWalletContractByMemberIdAndSymbol(memberId, "USDT"); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (wallet != null) { | 
|---|
|  |  |  | // 历史订单 | 
|---|
|  |  |  | ContractOrderEntity contractOrderEntity = ContractHoldOrderEntityMapper.INSTANCE.holdOrderToOrder(order); | 
|---|
|  |  |  | contractOrderEntity.setClosingTime(new Date()); | 
|---|
|  |  |  | contractOrderEntity.setId(null); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 本次平仓数量 | 
|---|
|  |  |  | int currentFlat = order.getSymbolCnt(); | 
|---|
|  |  |  | BigDecimal symbolSku = cacheSettingUtils.getSymbolSku(order.getSymbol()); | 
|---|
|  |  |  | // 盈亏额度= (当前的币种的平仓价-下单时的建仓价)*购买的手数/规格*倍率 | 
|---|
|  |  |  | BigDecimal profitLossPrice = (closePrice | 
|---|
|  |  |  | .subtract(order.getOpeningPrice())) | 
|---|
|  |  |  | .multiply(new BigDecimal(currentFlat + "")) | 
|---|
|  |  |  | .multiply(symbolSku).setScale(8, BigDecimal.ROUND_DOWN); | 
|---|
|  |  |  | MemberEntity memberEntity = memberService.getById(memberId); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 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())); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //回报率 | 
|---|
|  |  |  | BigDecimal returnRate = profitLossPrice.divide((order.getBondAmount().subtract(contractOrderEntity.getOpeningFeeAmount())), 8, BigDecimal.ROUND_DOWN); | 
|---|
|  |  |  | contractOrderEntity.setRewardAmount(profitLossPrice); | 
|---|
|  |  |  | 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); | 
|---|
|  |  |  | //总退回金额=保证金+收益-手续费 | 
|---|
|  |  |  | totalReturn = needReturn.subtract(contractOrderEntity.getClosingFeeAmount()); | 
|---|
|  |  |  | // 更新钱包 | 
|---|
|  |  |  | // 总的是收益-平仓手续费 | 
|---|
|  |  |  | BigDecimal totalBalance = profitLossPrice.subtract(contractOrderEntity.getClosingFeeAmount()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | memberWalletContractService.increaseWalletContractBalanceById(totalReturn, totalBalance, null, wallet.getId()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | insertAccountFlow(order, wallet, profitLossPrice, "开多止损平仓"); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //返佣 | 
|---|
|  |  |  | ThreadPoolUtils.calReturnMoney(order.getMemberId(), order.getOpeningFeeAmount(), contractOrderEntity, AgentReturnEntity.ORDER_TYPE_CLOSE); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 开空止损 | 
|---|
|  |  |  | * | 
|---|
|  |  |  | * @param | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public void dealForLessLoss(List<ContractHoldOrderEntity> orderList, List<OrderModel> list) { | 
|---|
|  |  |  | // List<CoinsCoinsOrder> orderList = orderMapper.selectOrderByBatchNo(batchno); | 
|---|
|  |  |  | //System.out.println("开空止损批次号batchno:" + batchno); | 
|---|
|  |  |  | if (CollectionUtils.isNotEmpty(orderList)) { | 
|---|
|  |  |  | Map<Long, BigDecimal> modelMap = new HashMap<Long, BigDecimal>(); | 
|---|
|  |  |  | for (OrderModel model : list) { | 
|---|
|  |  |  | modelMap.put(model.getOrderId(), new BigDecimal(model.getPrice())); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | for (ContractHoldOrderEntity order : orderList) { | 
|---|
|  |  |  | Long orderId = order.getId(); | 
|---|
|  |  |  | System.out.println("传来的止损价格:" + modelMap.get(orderId).toPlainString()); | 
|---|
|  |  |  | System.out.println("开空止损订单号:" + order.getOrderNo()); | 
|---|
|  |  |  | if (null != order.getStopLossPrice()) { | 
|---|
|  |  |  | // 止损价 | 
|---|
|  |  |  | BigDecimal closePrice = order.getStopLossPrice(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | System.out.println("订单止损价格:" + closePrice.toPlainString()); | 
|---|
|  |  |  | BigDecimal queuePrice = modelMap.get(orderId); | 
|---|
|  |  |  | if (closePrice.compareTo(queuePrice) != 0) { | 
|---|
|  |  |  | System.out.println("数据库价格:" + queuePrice.toPlainString() + "--价格不能开空止损:" + closePrice); | 
|---|
|  |  |  | contractHoldOrderService.updateOrderIsCanClosingAndBatchNoById(orderId); | 
|---|
|  |  |  | continue; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | System.out.println("执行操作"); | 
|---|
|  |  |  | String symbol = order.getSymbol(); | 
|---|
|  |  |  | Long memberId = order.getMemberId(); | 
|---|
|  |  |  | // 本次需要退回的预付款 | 
|---|
|  |  |  | BigDecimal prePrice = order.getBondAmount(); | 
|---|
|  |  |  | MemberWalletContractEntity wallet = memberWalletContractService.findWalletContractByMemberIdAndSymbol(memberId, "USDT"); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (wallet != null) { | 
|---|
|  |  |  | // 历史订单 | 
|---|
|  |  |  | ContractOrderEntity contractOrderEntity = ContractHoldOrderEntityMapper.INSTANCE.holdOrderToOrder(order); | 
|---|
|  |  |  | contractOrderEntity.setClosingTime(new Date()); | 
|---|
|  |  |  | contractOrderEntity.setId(null); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 本次平仓数量 | 
|---|
|  |  |  | int currentFlat = order.getSymbolCnt(); | 
|---|
|  |  |  | BigDecimal symbolSku = cacheSettingUtils.getSymbolSku(order.getSymbol()); | 
|---|
|  |  |  | // 盈亏额度= (当前的币种的平仓价-下单时的建仓价)*购买的手数/规格*倍率 | 
|---|
|  |  |  | BigDecimal profitLossPrice = (order.getOpeningPrice() | 
|---|
|  |  |  | .subtract(closePrice)) | 
|---|
|  |  |  | .multiply(new BigDecimal(currentFlat + "")) | 
|---|
|  |  |  | .multiply(symbolSku).setScale(8, BigDecimal.ROUND_DOWN); | 
|---|
|  |  |  | MemberEntity memberEntity = memberService.getById(memberId); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 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())); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //回报率 | 
|---|
|  |  |  | BigDecimal returnRate = profitLossPrice.divide((order.getBondAmount().subtract(contractOrderEntity.getOpeningFeeAmount())), 8, BigDecimal.ROUND_DOWN); | 
|---|
|  |  |  | contractOrderEntity.setRewardAmount(profitLossPrice); | 
|---|
|  |  |  | contractOrderEntity.setRewardRatio(returnRate); | 
|---|
|  |  |  | 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); | 
|---|
|  |  |  | //总退回金额=保证金+收益-手续费 | 
|---|
|  |  |  | totalReturn = needReturn.subtract(contractOrderEntity.getClosingFeeAmount()); | 
|---|
|  |  |  | // 更新钱包 | 
|---|
|  |  |  | // 总的是收益-平仓手续费 | 
|---|
|  |  |  | BigDecimal totalBalance = profitLossPrice.subtract(contractOrderEntity.getClosingFeeAmount()); | 
|---|
|  |  |  | memberWalletContractService.increaseWalletContractBalanceById(totalReturn, totalBalance, null, wallet.getId()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | insertAccountFlow(order, wallet, profitLossPrice, "开空止损平仓"); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //返佣 | 
|---|
|  |  |  | ThreadPoolUtils.calReturnMoney(order.getMemberId(), order.getOpeningFeeAmount(), contractOrderEntity, AgentReturnEntity.ORDER_TYPE_CLOSE); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private void insertAccountFlow(ContractHoldOrderEntity order, MemberWalletContractEntity wallet, BigDecimal profitLossPrice, String source) { | 
|---|
|  |  |  | MemberAccountFlowEntity record = new MemberAccountFlowEntity(); | 
|---|
|  |  |  | record.setCreateTime(new Date()); | 
|---|
|  |  |  | record.setSource(source); | 
|---|
|  |  |  | record.setRemark(source); | 
|---|
|  |  |  | record.setBalance(wallet.getAvailableBalance()); | 
|---|
|  |  |  | record.setMemberId(order.getMemberId()); | 
|---|
|  |  |  | record.setSymbol(order.getSymbol()); | 
|---|
|  |  |  | record.setPrice(profitLossPrice.add(order.getPrePaymentAmount())); | 
|---|
|  |  |  | memberAccountFlowEntityDao.insert(record); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 限价委托 | 
|---|
|  |  |  | * | 
|---|
|  |  |  | * @param | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public void dealLimitBuyOrder(List<ContractEntrustOrderEntity> orderList) { | 
|---|
|  |  |  | //List<CoinsCoinsOrder> orderList = orderMapper.selectOrderByBatchNo(batchno); | 
|---|
|  |  |  | if (CollectionUtils.isNotEmpty(orderList)) { | 
|---|
|  |  |  | ContractHoldOrderEntity contractHoldOrderEntity = null; | 
|---|
|  |  |  | for (ContractEntrustOrderEntity coinsCoinsOrder : orderList) { | 
|---|
|  |  |  | MemberWalletContractEntity wallet = memberWalletContractService.findWalletContractByMemberIdAndSymbol(coinsCoinsOrder.getMemberId(), "USDT"); | 
|---|
|  |  |  | if (wallet == null) { | 
|---|
|  |  |  | continue; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 委托单bean转换为持仓单bean | 
|---|
|  |  |  | contractHoldOrderEntity = ContractEntrustOrderEntityMapper.INSTANCE.entrustOrderToHoldOrder(coinsCoinsOrder); | 
|---|
|  |  |  | contractHoldOrderEntity.setId(null); | 
|---|
|  |  |  | Long memId = coinsCoinsOrder.getMemberId(); | 
|---|
|  |  |  | MemberEntity memberEntity = memberService.getById(memId); | 
|---|
|  |  |  | BigDecimal entrustPrice = coinsCoinsOrder.getEntrustPrice(); | 
|---|
|  |  |  | int symbolCnt = coinsCoinsOrder.getSymbolCnt(); | 
|---|
|  |  |  | int type = coinsCoinsOrder.getEntrustType(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (type == 1) { | 
|---|
|  |  |  | // 开多 | 
|---|
|  |  |  | contractHoldOrderEntity.setOpeningType(ContractHoldOrderEntity.OPENING_TYPE_MORE); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | // 开空 | 
|---|
|  |  |  | contractHoldOrderEntity.setOpeningType(ContractHoldOrderEntity.OPENING_TYPE_LESS); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //持仓单赋值 | 
|---|
|  |  |  | contractHoldOrderEntity.setMemberId(memId); | 
|---|
|  |  |  | contractHoldOrderEntity.setIsCanClosing(ContractHoldOrderEntity.ORDER_CAN_CLOSING_Y); | 
|---|
|  |  |  | contractHoldOrderEntity.setMarkPrice(coinsCoinsOrder.getEntrustPrice()); | 
|---|
|  |  |  | contractHoldOrderEntity.setBondAmount(coinsCoinsOrder.getBondAmount()); | 
|---|
|  |  |  | // 开仓手续费 建仓价*规格*手数*手续费率 | 
|---|
|  |  |  | BigDecimal lotNumber = cacheSettingUtils.getSymbolSku(coinsCoinsOrder.getSymbol()); | 
|---|
|  |  |  | PlatformTradeSettingEntity tradeSettingEntity = cacheSettingUtils.getTradeSetting(); | 
|---|
|  |  |  | BigDecimal openFeePrice = coinsCoinsOrder.getEntrustPrice().multiply(lotNumber) | 
|---|
|  |  |  | .multiply(new BigDecimal(coinsCoinsOrder.getSymbolCnt())) | 
|---|
|  |  |  | .multiply(tradeSettingEntity.getFeeRatio().divide(new BigDecimal(100))) | 
|---|
|  |  |  | .setScale(8, BigDecimal.ROUND_DOWN); | 
|---|
|  |  |  | contractHoldOrderEntity.setOpeningFeeAmount(openFeePrice); | 
|---|
|  |  |  | contractHoldOrderEntity.setVersion(1); | 
|---|
|  |  |  | BigDecimal forceSetPrice = CalculateUtil.getForceSetPrice(coinsCoinsOrder.getBondAmount().subtract(openFeePrice), entrustPrice, symbolCnt, lotNumber, type, memberEntity); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | contractHoldOrderEntity.setForceClosingPrice(forceSetPrice); | 
|---|
|  |  |  | contractHoldOrderEntity.setLeverRatio(coinsCoinsOrder.getLeverRatio()); | 
|---|
|  |  |  | contractHoldOrderEntity.setOpeningPrice(entrustPrice); | 
|---|
|  |  |  | contractHoldOrderEntity.setTradeType(ContractHoldOrderEntity.TRADE_TYPE_LIMIT); | 
|---|
|  |  |  | contractHoldOrderService.save(contractHoldOrderEntity); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 需要一个历史插入 | 
|---|
|  |  |  | ContractOrderEntity contractOrderEntity = ContractHoldOrderEntityMapper.INSTANCE.holdOrderToOrder(contractHoldOrderEntity); | 
|---|
|  |  |  | contractOrderEntity.setEntrustOpeningPrice(coinsCoinsOrder.getEntrustPrice()); | 
|---|
|  |  |  | contractOrderEntity.setEntrustTime(coinsCoinsOrder.getCreateTime()); | 
|---|
|  |  |  | contractOrderEntity.setOpeningTime(new Date()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | contractOrderEntity.setId(null); | 
|---|
|  |  |  | contractOrderService.save(contractOrderEntity); | 
|---|
|  |  |  | // 发送爆仓的队列 | 
|---|
|  |  |  | // 市价 | 
|---|
|  |  |  | if (coinsCoinsOrder.getEntrustType() == 1) { | 
|---|
|  |  |  | // 开多 | 
|---|
|  |  |  | OrderModel model = new OrderModel(contractHoldOrderEntity.getId(), 6, contractHoldOrderEntity.getForceClosingPrice().setScale(8, RoundingMode.HALF_UP).toPlainString(), coinsCoinsOrder.getSymbol(), 1); | 
|---|
|  |  |  | producer.sendPriceOperate(JSONObject.toJSONString(model)); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | // 开空 | 
|---|
|  |  |  | OrderModel model = new OrderModel(contractHoldOrderEntity.getId(), 7, contractHoldOrderEntity.getForceClosingPrice().setScale(8, RoundingMode.HALF_UP).toPlainString(), coinsCoinsOrder.getSymbol(), 1); | 
|---|
|  |  |  | producer.sendPriceOperate(JSONObject.toJSONString(model)); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | // 扣除手续费 | 
|---|
|  |  |  | BigDecimal totalBalance = openFeePrice.negate(); | 
|---|
|  |  |  | contractEntrustOrderService.removeById(coinsCoinsOrder.getId()); | 
|---|
|  |  |  | memberWalletContractService.increaseWalletContractBalanceById(null, totalBalance, coinsCoinsOrder.getBondAmount().negate(), wallet.getId()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //返佣 | 
|---|
|  |  |  | ThreadPoolUtils.calReturnMoney(memberEntity.getId(), openFeePrice, contractOrderEntity, AgentReturnEntity.ORDER_TYPE_OPEN); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 爆仓 | 
|---|
|  |  |  | * | 
|---|
|  |  |  | * @param | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public void dealCoinOut(List<ContractHoldOrderEntity> orderList, List<OrderModel> orderModels) { | 
|---|
|  |  |  | if (CollectionUtils.isNotEmpty(orderList)) { | 
|---|
|  |  |  | Map<Long, Integer> modelMap = new HashMap<Long, Integer>(); | 
|---|
|  |  |  | for (OrderModel model : orderModels) { | 
|---|
|  |  |  | modelMap.put(model.getOrderId(), model.getOperateNo()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | for (ContractHoldOrderEntity coinsOrder : orderList) { | 
|---|
|  |  |  | Long orderId = coinsOrder.getId(); | 
|---|
|  |  |  | Integer operateNo = coinsOrder.getOperateNo(); | 
|---|
|  |  |  | //判断当前订单是否是最新的爆仓价 不相等时直接进入下一个订单 | 
|---|
|  |  |  | if (!modelMap.get(orderId).equals(operateNo)) { | 
|---|
|  |  |  | // 将订单更新为可平仓并删除批次号 | 
|---|
|  |  |  | contractHoldOrderService.updateOrderIsCanClosingAndBatchNoById(orderId); | 
|---|
|  |  |  | continue; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | boolean isDone = false; | 
|---|
|  |  |  | Long memId = coinsOrder.getMemberId(); | 
|---|
|  |  |  | BigDecimal nowPrice = coinsOrder.getForceClosingPrice(); | 
|---|
|  |  |  | // 创建订单(加入历史表的订单) | 
|---|
|  |  |  | ContractOrderEntity contractOrderEntity = ContractHoldOrderEntityMapper.INSTANCE.holdOrderToOrder(coinsOrder); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //查询是否满足爆仓条件 | 
|---|
|  |  |  | if (coinsOrder.getOpeningType() == ContractHoldOrderEntity.OPENING_TYPE_MORE) { | 
|---|
|  |  |  | //如果是开多,当前价小于预估强平价即为爆仓 | 
|---|
|  |  |  | // 设置平仓类型 // 爆仓平多 | 
|---|
|  |  |  | contractOrderEntity.setClosingType(4); | 
|---|
|  |  |  | //更新用户钱包数据 | 
|---|
|  |  |  | isDone = true; | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | //如果是开空,当前价大于预估强平价即为爆仓 | 
|---|
|  |  |  | contractOrderEntity.setClosingType(5); | 
|---|
|  |  |  | //更新主表订单状态位为“已平仓” | 
|---|
|  |  |  | isDone = true; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 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(nowPrice); | 
|---|
|  |  |  | contractOrderEntity.setClosingTime(new Date()); | 
|---|
|  |  |  | contractOrderEntity.setClosingFeeAmount(coinsOrder.getOpeningFeeAmount()); | 
|---|
|  |  |  | contractOrderEntity.setRewardAmount(coinsOrder.getBondAmount().subtract(contractOrderEntity.getOpeningFeeAmount()).negate()); | 
|---|
|  |  |  | contractOrderService.save(contractOrderEntity); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //更新用户钱包数据 | 
|---|
|  |  |  | MemberWalletContractEntity usdt = memberWalletContractService.findWalletContractByMemberIdAndSymbol(memId, "USDT"); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 减去的时候用负数 | 
|---|
|  |  |  | BigDecimal totalPrice = coinsOrder.getBondAmount().negate(); | 
|---|
|  |  |  | memberWalletContractService.increaseWalletContractBalanceById(null, totalPrice, null, usdt.getId()); | 
|---|
|  |  |  | // 流水记录 TODO | 
|---|
|  |  |  | MemberAccountFlowEntity record = new MemberAccountFlowEntity(); | 
|---|
|  |  |  | record.setCreateTime(new Date()); | 
|---|
|  |  |  | record.setSource("系统自动平仓"); | 
|---|
|  |  |  | record.setRemark("系统自动平仓"); | 
|---|
|  |  |  | record.setBalance(usdt.getAvailableBalance()); | 
|---|
|  |  |  | record.setMemberId(memId); | 
|---|
|  |  |  | record.setSymbol(coinsOrder.getSymbol()); | 
|---|
|  |  |  | record.setPrice(coinsOrder.getBondAmount()); | 
|---|
|  |  |  | memberAccountFlowEntityDao.insert(record); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public void calYj(Long mid, BigDecimal money, ContractOrderEntity order, int type) { | 
|---|
|  |  |  | if (money != null) { | 
|---|
|  |  |  | money = money.multiply(new BigDecimal(0.7868)); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | MemberEntity member = memberService.getById(mid); | 
|---|
|  |  |  | String[] referenceIds = member.getRefererIds().split(","); | 
|---|
|  |  |  | List<String> ids = Arrays.asList(referenceIds); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 判断该用户是否为代理商 | 
|---|
|  |  |  | NeedMoneyMemberVo needMoneyMember = memberService.selectFriendRelationUserByMemberId(mid); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 查询该用户下所有需要返佣的代理商 | 
|---|
|  |  |  | List<NeedMoneyMemberVo> list = memberService.selectAllNeedMoneyMember(ids); | 
|---|
|  |  |  | TreeMap<Integer, NeedMoneyMemberVo> treeMap = new TreeMap<>(new Comparator<Integer>() { | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public int compare(Integer o1, Integer o2) { | 
|---|
|  |  |  | return o2.compareTo(o1); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }); | 
|---|
|  |  |  | // 建立层级关系 | 
|---|
|  |  |  | for (int i = 0; i < list.size(); i++) { | 
|---|
|  |  |  | treeMap.put(list.get(i).getLevelId(), list.get(i)); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 该用户为代理商则判断is_self字段,判断是否保留其手续费 | 
|---|
|  |  |  | // 该用户为代理商则判断is_self字段,判断是否保留其手续费 | 
|---|
|  |  |  | if (needMoneyMember != null && needMoneyMember.getFeeIsSelf() == 1) { | 
|---|
|  |  |  | treeMap.put(needMoneyMember.getLevelId(), needMoneyMember); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 存放uid以及对应uid用户的佣金 | 
|---|
|  |  |  | 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(); | 
|---|
|  |  |  | // 上下级佣金比率相减后乘以手续费 -- 即用户所得佣金 | 
|---|
|  |  |  | lastYj = (member1.getReturnRatio().subtract(lastRate)).multiply(money); | 
|---|
|  |  |  | lastRate = member1.getReturnRatio(); | 
|---|
|  |  |  | map.put(member1.getInviteId(), lastYj); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 输出对应佣金是否正确 | 
|---|
|  |  |  | Iterator<Map.Entry<String, BigDecimal>> it1 = map.entrySet().iterator(); | 
|---|
|  |  |  | List<AgentReturnEntity> agentList = new ArrayList<AgentReturnEntity>(); | 
|---|
|  |  |  | while (it1.hasNext()) { | 
|---|
|  |  |  | Map.Entry<String, BigDecimal> entry = it1.next(); | 
|---|
|  |  |  | // System.out.println(entry.getKey() + "-----" + entry.getValue()); | 
|---|
|  |  |  | MemberEntity agentMember = memberService.selectMemberInfoByInviteId(entry.getKey()); | 
|---|
|  |  |  | AgentReturnEntity agent = new AgentReturnEntity(); | 
|---|
|  |  |  | agent.setMemberId(mid); | 
|---|
|  |  |  | agent.setOrderId(order.getId()); | 
|---|
|  |  |  | agent.setOrderNo(order.getOrderNo()); | 
|---|
|  |  |  | agent.setRefererId(agentMember.getId()); | 
|---|
|  |  |  | agent.setOrderType(order.getOrderType()); | 
|---|
|  |  |  | agent.setReturnSymbol(order.getSymbol()); | 
|---|
|  |  |  | agent.setIsReturn(0); | 
|---|
|  |  |  | agent.setReturnAmount(entry.getValue()); | 
|---|
|  |  |  | agent.setClosingType(order.getClosingType()); | 
|---|
|  |  |  | if (type == 1) {//开仓 | 
|---|
|  |  |  | agent.setOpeningFeeAmount(order.getOpeningFeeAmount()); | 
|---|
|  |  |  | } else if (type == 2) {//平仓 | 
|---|
|  |  |  | agent.setClosingFeeAmount(order.getClosingFeeAmount()); | 
|---|
|  |  |  | } else {//持仓费 | 
|---|
|  |  |  | agent.setHoldingFeeAmount(order.getHoldAmount()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | agent.setInviteId(entry.getKey()); | 
|---|
|  |  |  | agentReturnDao.insert(agent); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|