zainali5120
2020-09-25 286ea89f5f6cade73276f865effa5dcd2b19406d
src/main/java/com/xcong/excoin/modules/coin/service/impl/OrderCoinServiceImpl.java
@@ -1,25 +1,27 @@
package com.xcong.excoin.modules.coin.service.impl;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import javax.annotation.Resource;
import com.alibaba.fastjson.JSONObject;
import com.xcong.excoin.modules.blackchain.service.RocService;
import com.xcong.excoin.modules.coin.mapper.OrderCoinsDealMapper;
import com.xcong.excoin.modules.platform.entity.PlatformCnyUsdtExchangeEntity;
import com.xcong.excoin.modules.platform.entity.PlatformSymbolsCoinEntity;
import com.xcong.excoin.modules.symbols.constants.SymbolsConstats;
import com.xcong.excoin.trade.CoinTrader;
import com.xcong.excoin.trade.CoinTraderFactory;
import com.xcong.excoin.trade.ExchangeTrade;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
@@ -38,15 +40,11 @@
import com.xcong.excoin.modules.coin.parameter.dto.FindAllWalletCoinOrderDto;
import com.xcong.excoin.modules.coin.parameter.vo.FindCollectListVo;
import com.xcong.excoin.modules.coin.parameter.vo.MemberSelectSymbolsVo;
import com.xcong.excoin.modules.coin.parameter.vo.OrderWalletCoinDealListVo;
import com.xcong.excoin.modules.coin.parameter.vo.OrderWalletCoinDealVo;
import com.xcong.excoin.modules.coin.parameter.vo.OrderWalletCoinListVo;
import com.xcong.excoin.modules.coin.parameter.vo.OrderWalletCoinVo;
import com.xcong.excoin.modules.coin.parameter.vo.TransactionPageOfWalletCoinVo;
import com.xcong.excoin.modules.coin.service.OrderCoinService;
import com.xcong.excoin.modules.contract.entity.ContractOrderEntity;
import com.xcong.excoin.modules.contract.mapper.ContractOrderEntityMapper;
import com.xcong.excoin.modules.contract.parameter.vo.OrderListVo;
import com.xcong.excoin.modules.member.dao.MemberWalletCoinDao;
import com.xcong.excoin.modules.member.entity.MemberSelectSymbolsEntity;
import com.xcong.excoin.modules.member.entity.MemberWalletCoinEntity;
@@ -84,6 +82,10 @@
    RedisUtils redisUtils;
    @Resource
    PlatformSymbolsCoinDao platformSymbolsCoinDao;
    @Resource
    private CoinTraderFactory factory;
    @Override
    public String generateSimpleSerialno(String userId) {
@@ -163,13 +165,20 @@
    @Override
    @Transactional
    public Result submitSalesWalletCoinOrder(String symbol, Integer type, Integer tradeType, BigDecimal price, BigDecimal amount) {
    public Result submitSalesWalletCoinOrder(String symbol, Integer type, Integer tradeType, BigDecimal price, BigDecimal amount, BigDecimal entrustAmount) {
        //获取用户ID
        Long memberId = LoginUserUtils.getAppLoginUser().getId();
        BigDecimal nowPriceinBigDecimal = price;
        //查询当前价
        BigDecimal nowPrice = new BigDecimal(redisUtils.getString(CoinTypeConvert.convertToKey(symbol + "/USDT")));
        System.out.println(symbol + "当前价:" + nowPrice);
        if (OrderCoinsEntity.ORDERTYPE_BUY.equals(type) && OrderCoinsEntity.TRADETYPE_MARKETPRICE.equals(tradeType)) {
            amount = entrustAmount.divide(nowPrice, 8, BigDecimal.ROUND_DOWN);
            System.out.println(symbol + "市价量:" + amount);
        }
        // 处理市价
        // 获取交易管理的杠杠倍率,手续费率等信息,由平台进行设置
        symbol = symbol.toUpperCase();
        MemberWalletCoinEntity walletCoin = memberWalletCoinDao.selectWalletCoinBymIdAndCode(memberId, symbol);
@@ -185,13 +194,14 @@
        BigDecimal closingPrice = price.multiply(amount).multiply(tradeSetting.getCoinFeeRatio());
        //总费用 = 成交价*数量+手续费
        BigDecimal totalPayPrice = price.multiply(amount).add(closingPrice);
        BigDecimal totalPayPricCoin = nowPrice.multiply(amount).add(closingPrice);
        String walletCode = MemberWalletCoinEnum.WALLETCOINCODE.getValue();
        MemberWalletCoinEntity walletCoinUsdt = memberWalletCoinDao.selectWalletCoinBymIdAndCode(memberId, walletCode);
        if (OrderCoinsEntity.ORDERTYPE_BUY.equals(type)) {
            //买入,所需总费用跟用户USDT金额进行比较
            BigDecimal availableBalance = walletCoinUsdt.getAvailableBalance();
            if (totalPayPrice.compareTo(availableBalance) > 0) {
            if (totalPayPrice.compareTo(availableBalance) > 0 || totalPayPricCoin.compareTo(availableBalance) > 0) {
                return Result.fail(MessageSourceUtils.getString("order_service_0010"));
            }
        } else {
@@ -205,7 +215,7 @@
        // 创建订单
        OrderCoinsEntity order = new OrderCoinsEntity();
        if ((OrderCoinsEntity.TRADETYPE_FIXEDPRICE.equals(tradeType) && type.equals(1) && price.compareTo(nowPrice) < 0)
              || (OrderCoinsEntity.TRADETYPE_FIXEDPRICE.equals(tradeType) && type.equals(2) && price.compareTo(nowPrice) > 0)) {
                || (OrderCoinsEntity.TRADETYPE_FIXEDPRICE.equals(tradeType) && type.equals(2) && price.compareTo(nowPrice) > 0)) {
            // 如果是限价交易直接插入主表数据
            order.setMemberId(memberId);
            order.setOrderNo(generateSimpleSerialno(memberId.toString()));
@@ -249,15 +259,16 @@
            order.setEntrustCnt(amount);
            order.setEntrustPrice(price);
            order.setDealCnt(amount);
            order.setEntrustAmount(entrustAmount);
            if ((OrderCoinsEntity.TRADETYPE_FIXEDPRICE.equals(tradeType) && type.equals(1) && price.compareTo(nowPrice) >= 0)
                  || (OrderCoinsEntity.TRADETYPE_FIXEDPRICE.equals(tradeType) && type.equals(2) && price.compareTo(nowPrice) <= 0)) {
               // 手续费用(手续费=建仓价X数量X手续费率)
                    || (OrderCoinsEntity.TRADETYPE_FIXEDPRICE.equals(tradeType) && type.equals(2) && price.compareTo(nowPrice) <= 0)) {
                // 手续费用(手续费=建仓价X数量X手续费率)
                closingPrice = nowPrice.multiply(amount).multiply(tradeSetting.getCoinFeeRatio());
                //总费用 = 成交价*数量+手续费
                totalPayPrice = nowPrice.multiply(amount).add(closingPrice);
                price = nowPrice;
            }
            order.setDealPrice(price);
            order.setDealPrice(nowPrice);
            order.setDealAmount(totalPayPrice);
            order.setOrderStatus(OrderCoinsEntity.ORDERSTATUS_DONE);
            order.setTradeType(tradeType);
@@ -273,7 +284,7 @@
            detail.setSymbol(symbol);
            detail.setSymbolCnt(amount);
            detail.setEntrustPrice(nowPriceinBigDecimal);
            detail.setDealPrice(price);
            detail.setDealPrice(nowPrice);
            detail.setDealAmount(totalPayPrice);
            detail.setFeeAmount(closingPrice);
            detail.setOrderStatus(OrderCoinsDealEntity.ORDERSTATUS_DONE);
@@ -318,6 +329,138 @@
    }
    @Override
    public Result submitSalesWalletCoinOrderWithMatch(String symbol, Integer type, Integer tradeType, BigDecimal price, BigDecimal amount, BigDecimal entrustAmount) {
        //获取用户ID
        Long memberId = LoginUserUtils.getAppLoginUser().getId();
        BigDecimal nowPriceinBigDecimal = price;
        //查询当前价
        //BigDecimal nowPrice = new BigDecimal(redisUtils.getString(CoinTypeConvert.convertToKey(symbol + "/USDT")));
        // 获取交易管理的杠杠倍率,手续费率等信息,由平台进行设置
        symbol = symbol.toUpperCase();
        MemberWalletCoinEntity walletCoin = memberWalletCoinDao.selectWalletCoinBymIdAndCode(memberId, symbol);
        if (ObjectUtil.isEmpty(walletCoin)) {
            // 创建这个钱包
            walletCoin = new MemberWalletCoinEntity();
            walletCoin.setAvailableBalance(BigDecimal.ZERO);
            walletCoin.setFrozenBalance(BigDecimal.ZERO);
            walletCoin.setTotalBalance(BigDecimal.ZERO);
            walletCoin.setBorrowedFund(BigDecimal.ZERO);
            walletCoin.setMemberId(memberId);
            walletCoin.setWalletCode(symbol);
            memberWalletCoinDao.insert(walletCoin);
        }
        // 查询交易设置
        PlatformTradeSettingEntity tradeSetting = platformTradeSettingDao.findTradeSetting();
        if (ObjectUtil.isEmpty(tradeSetting)) {
            return Result.fail(MessageSourceUtils.getString("order_service_0009"));
        }
        // 手续费用(手续费=建仓价X数量X手续费率)
        BigDecimal closingPrice = BigDecimal.ZERO;
        // 总费用分两种 1,限价交易 是价格*数量+手续费 2,市价交易 用户输入的金额+手续费
        //总费用 = 成交价*数量+手续费
        BigDecimal totalPayPrice = BigDecimal.ZERO;
        if (OrderCoinsEntity.TRADETYPE_FIXEDPRICE.equals(tradeType)) {
            // 限价
            closingPrice = price.multiply(amount).multiply(tradeSetting.getCoinFeeRatio());
            totalPayPrice = price.multiply(amount).add(closingPrice);
            entrustAmount = price.multiply(amount);
        } else {
            // 市价
            if(OrderCoinsEntity.ORDERTYPE_BUY==type){
                closingPrice = entrustAmount.multiply(tradeSetting.getCoinFeeRatio());
                totalPayPrice = entrustAmount.add(closingPrice);
            }
        }
        // BigDecimal totalPayPricCoin = nowPrice.multiply(amount).add(closingPrice);
        String walletCode = MemberWalletCoinEnum.WALLETCOINCODE.getValue();
        MemberWalletCoinEntity walletCoinUsdt = memberWalletCoinDao.selectWalletCoinBymIdAndCode(memberId, walletCode);
        if (OrderCoinsEntity.ORDERTYPE_BUY.equals(type)) {
            //买入,所需总费用跟用户USDT金额进行比较
            BigDecimal availableBalance = walletCoinUsdt.getAvailableBalance();
            if (totalPayPrice.compareTo(availableBalance) > 0) {
                return Result.fail(MessageSourceUtils.getString("order_service_0010"));
            }
        } else {
            //卖出,用户币是否足够
            BigDecimal availableBalance = walletCoin.getAvailableBalance();
            if (amount.compareTo(availableBalance) > 0) {
                return Result.fail(MessageSourceUtils.getString("order_service_0010"));
            }
        }
        // 首先将单插入到数据库主表(委托表)
        // 创建订单
        OrderCoinsEntity order = new OrderCoinsEntity();
        //根据委托类型生成不同数据
        // 如果是限价交易直接插入主表数据
        order.setMemberId(memberId);
        order.setOrderNo(generateSimpleSerialno(memberId.toString()));
        order.setOrderType(type);
        order.setSymbol(symbol);
        //order.setMarkPrice(nowPrice);
        // 成交量 先设置为0
        order.setDealCnt(BigDecimal.ZERO);
        order.setEntrustCnt(BigDecimal.ZERO);
        order.setEntrustPrice(BigDecimal.ZERO);
        // 成交价
        //order.setDealPrice(price);
        // 成交金额
        order.setDealAmount(BigDecimal.ZERO);
        order.setOrderStatus(OrderCoinsEntity.ORDERSTATUS_DODING);
        order.setTradeType(tradeType);
        // 手续费
        order.setFeeAmount(closingPrice);
        if (OrderCoinsEntity.TRADETYPE_FIXEDPRICE.equals(tradeType)) {
            // 限价 是需要价格和数量 可以得到成交金额
            // 下单量
            order.setEntrustCnt(amount);
            // 下单价格
            order.setEntrustPrice(price);
            order.setEntrustAmount(amount.multiply(price));
        } else {
            if (OrderCoinsEntity.ORDERTYPE_BUY.equals(type)) {
                // 市价 只有金额
                order.setEntrustAmount(entrustAmount);
            } else {
                // 市价卖单
                // 下单量
                order.setEntrustCnt(amount);
                // 下单价格
                order.setEntrustPrice(BigDecimal.ZERO);
                order.setEntrustAmount(BigDecimal.ZERO);
            }
        }
        orderCoinsDao.insert(order);
        //更新用户钱包信息
        //冻结相应的资产
        if (OrderCoinsEntity.ORDERTYPE_BUY.equals(type)) {
            //如果是买入,所对应的币种增加,USDT账户减少金额
//            BigDecimal availableBalance = walletCoinUsdt.getAvailableBalance().subtract(totalPayPrice);
//            BigDecimal frozenBalance = walletCoinUsdt.getFrozenBalance().add(totalPayPrice);
//            walletCoinUsdt.setAvailableBalance(availableBalance);
//            walletCoinUsdt.setFrozenBalance(frozenBalance);
//            memberWalletCoinDao.updateById(walletCoinUsdt);
            memberWalletCoinDao.updateWalletBalance(walletCoinUsdt.getId(),totalPayPrice.negate(),totalPayPrice.negate(),entrustAmount);
        } else {
            //如果是卖出,币种减少,USDT增加
//            BigDecimal availableBalance = walletCoin.getAvailableBalance().subtract(amount);
//            BigDecimal frozenBalance = walletCoin.getFrozenBalance().add(amount);
//            walletCoin.setAvailableBalance(availableBalance);
//            walletCoin.setFrozenBalance(frozenBalance);
//            memberWalletCoinDao.updateById(walletCoin);
            memberWalletCoinDao.updateWalletBalance(walletCoin.getId(),amount.negate(),amount.negate(),amount);
        }
        // 加入到撮合
        CoinTrader trader = factory.getTrader(symbol);
        trader.trade(order);
        return Result.ok(MessageSourceUtils.getString("order_service_0011"));
    }
    @Override
    public Result getEntrustWalletCoinOrder(String symbol, Integer status) {
        //获取用户ID
        Long memberId = LoginUserUtils.getAppLoginUser().getId();
@@ -328,6 +471,20 @@
        if (CollUtil.isNotEmpty(findCoinOrderListByMemberIdAndSysmbol)) {
            for (OrderCoinsEntity orderCoinsEntity : findCoinOrderListByMemberIdAndSysmbol) {
                OrderWalletCoinVo entityToVo = OrderWalletCoinMapper.INSTANCE.entityToVo(orderCoinsEntity);
                entityToVo.setFeeAmount(entityToVo.getFeeAmount() == null
                        ? BigDecimal.ZERO : entityToVo.getFeeAmount().setScale(4, BigDecimal.ROUND_DOWN));
                entityToVo.setMarkPrice(entityToVo.getMarkPrice() == null
                        ? BigDecimal.ZERO : entityToVo.getMarkPrice().setScale(4, BigDecimal.ROUND_DOWN));
                entityToVo.setEntrustCnt(entityToVo.getEntrustCnt() == null
                        ? BigDecimal.ZERO : entityToVo.getEntrustCnt().setScale(4, BigDecimal.ROUND_DOWN));
                entityToVo.setEntrustPrice(entityToVo.getEntrustPrice() == null
                        ? BigDecimal.ZERO : entityToVo.getEntrustPrice().setScale(4, BigDecimal.ROUND_DOWN));
                entityToVo.setDealCnt(entityToVo.getDealCnt() == null
                        ? BigDecimal.ZERO : entityToVo.getDealCnt().setScale(4, BigDecimal.ROUND_DOWN));
                entityToVo.setDealPrice(entityToVo.getDealPrice() == null
                        ? BigDecimal.ZERO : entityToVo.getDealPrice().setScale(4, BigDecimal.ROUND_DOWN));
                entityToVo.setDealAmount(entityToVo.getDealAmount() == null
                        ? BigDecimal.ZERO : entityToVo.getDealAmount().setScale(4, BigDecimal.ROUND_DOWN));
                arrayList.add(entityToVo);
            }
        }
@@ -342,7 +499,11 @@
        Long memberId = LoginUserUtils.getAppLoginUser().getId();
        OrderCoinsEntity orderCoinsEntity = orderCoinsDao.selectById(orderId);
        if (ObjectUtil.isNotEmpty(orderCoinsEntity) && orderCoinsEntity.getMemberId() == memberId) {
            if (orderCoinsEntity.getOrderStatus() == OrderCoinsEntity.ORDERSTATUS_CANCEL) {
            // 如果是撮合交易单
            if (SymbolsConstats.EXCHANGE_SYMBOLS.contains(orderCoinsEntity.getSymbol())) {
                return this.cancelEntrustWalletCoinOrderForMatch(orderId);
            }
            if (orderCoinsEntity.getOrderStatus() == OrderCoinsEntity.ORDERSTATUS_CANCEL || orderCoinsEntity.getOrderStatus()==OrderCoinsEntity.ORDERSTATUS_DONE) {
                return Result.fail(MessageSourceUtils.getString("order_service_0012"));
            }
            orderCoinsEntity.setOrderStatus(OrderCoinsEntity.ORDERSTATUS_CANCEL);
@@ -415,6 +576,96 @@
    }
    @Override
    @Transactional
    public Result cancelEntrustWalletCoinOrderForMatch(String orderId) {
        //获取用户ID
        Long memberId = LoginUserUtils.getAppLoginUser().getId();
        OrderCoinsEntity orderCoinsEntity = orderCoinsDao.selectById(orderId);
        // 取消撮合订单的单
        CoinTrader trader = factory.getTrader(orderCoinsEntity.getSymbol());
        trader.cancelOrder(orderCoinsEntity);
        if (ObjectUtil.isNotEmpty(orderCoinsEntity) && orderCoinsEntity.getMemberId() == memberId) {
            if (orderCoinsEntity.getOrderStatus() == OrderCoinsEntity.ORDERSTATUS_CANCEL || orderCoinsEntity.getOrderStatus()==OrderCoinsEntity.ORDERSTATUS_DONE) {
                return Result.fail(MessageSourceUtils.getString("order_service_0012"));
            }
            orderCoinsEntity.setOrderStatus(OrderCoinsEntity.ORDERSTATUS_CANCEL);
            orderCoinsDao.updateById(orderCoinsEntity);
            String symbol = orderCoinsEntity.getSymbol();
            OrderCoinsDealEntity detail = new OrderCoinsDealEntity();
            detail.setMemberId(memberId);
            detail.setOrderId(orderCoinsEntity.getId());
            detail.setOrderNo(generateSimpleSerialno(memberId.toString()));
            detail.setOrderType(orderCoinsEntity.getOrderType());
            detail.setTradeType(orderCoinsEntity.getTradeType());
            detail.setSymbol(symbol);
            detail.setOrderStatus(OrderCoinsDealEntity.ORDERSTATUS_CANCEL);
            detail.setSymbolCnt(orderCoinsEntity.getEntrustCnt());
            detail.setEntrustPrice(orderCoinsEntity.getEntrustPrice());
            detail.setDealPrice(orderCoinsEntity.getDealPrice());
            detail.setDealAmount(orderCoinsEntity.getDealAmount());
            detail.setFeeAmount(orderCoinsEntity.getFeeAmount());
            orderCoinDealDao.insert(detail);
            if (OrderCoinsEntity.ORDERTYPE_BUY.equals(orderCoinsEntity.getOrderType())) {
                //如果是限价买入,撤单将USDT账户冻结金额返回
                String walletCode = MemberWalletCoinEnum.WALLETCOINCODE.getValue();
                MemberWalletCoinEntity walletCoin = memberWalletCoinDao.selectWalletCoinBymIdAndCode(memberId, walletCode);
                if (ObjectUtil.isNotEmpty(walletCoin)) {
                    //手续费 = 开仓价*数量*手续费率
                    //返还金额=开仓价*未成交数量+手续费
                    BigDecimal returnBalance = orderCoinsEntity.getEntrustAmount().subtract(orderCoinsEntity.getDealAmount());
                    // 需要退回的手续费
                    BigDecimal returnFee = BigDecimal.ZERO;
                    if (returnBalance.compareTo(orderCoinsEntity.getEntrustAmount()) == 0) {
                        returnFee = orderCoinsEntity.getFeeAmount();
                    } else {
                        // 按比例退回
                        BigDecimal needFee = orderCoinsEntity.getDealAmount().divide(orderCoinsEntity.getEntrustAmount(), 8, BigDecimal.ROUND_DOWN).multiply(orderCoinsEntity.getFeeAmount());
                        returnFee = orderCoinsEntity.getFeeAmount().subtract(needFee);
                    }
                    walletCoin.setAvailableBalance(walletCoin.getAvailableBalance().add(returnBalance).add(returnFee));
                    walletCoin.setFrozenBalance(walletCoin.getFrozenBalance().subtract(returnBalance));
                    memberWalletCoinDao.updateById(walletCoin);
                    // 流水记录
                    MemberAccountFlowEntity record = new MemberAccountFlowEntity();
                    record.setSource(MemberAccountFlowEntity.SOURCE_CANCEL);
                    record.setRemark(MemberAccountFlowEntity.REMARK_CANCEL + symbol + MemberAccountFlowEntity.REMARK_RETURNBALANCE + returnBalance.add(returnFee));
                    record.setBalance(walletCoin.getAvailableBalance());
                    record.setMemberId(memberId);
                    record.setSymbol(symbol);
                    record.setPrice(returnBalance);
                    memberAccountFlowEntityDao.insert(record);
                    return Result.ok(MessageSourceUtils.getString("order_service_0013"));
                }
            } else {
                //如果是限价卖出,撤单将对应的钱包冻结金额返回
                MemberWalletCoinEntity walletCoin = memberWalletCoinDao.selectWalletCoinBymIdAndCode(memberId, symbol);
                if (ObjectUtil.isNotEmpty(walletCoin)) {
                    // 卖出按卖出的数量计算手续费
                    BigDecimal returnBalance = orderCoinsEntity.getEntrustCnt().subtract(orderCoinsEntity.getDealCnt());
                    walletCoin.setAvailableBalance(walletCoin.getAvailableBalance().add(returnBalance));
                    walletCoin.setFrozenBalance(walletCoin.getFrozenBalance().subtract(returnBalance));
                    memberWalletCoinDao.updateById(walletCoin);
                    // 流水记录
                    MemberAccountFlowEntity record = new MemberAccountFlowEntity();
                    record.setSource(MemberAccountFlowEntity.SOURCE_CANCEL);
                    record.setRemark(MemberAccountFlowEntity.REMARK_CANCEL + symbol + MemberAccountFlowEntity.REMARK_RETURNBALANCE + returnBalance);
                    record.setBalance(walletCoin.getAvailableBalance());
                    record.setMemberId(memberId);
                    record.setSymbol(symbol);
                    record.setPrice(walletCoin.getFrozenBalance());
                    memberAccountFlowEntityDao.insert(record);
                    return Result.ok(MessageSourceUtils.getString("order_service_0013"));
                }
            }
        }
        return Result.fail(MessageSourceUtils.getString("order_service_0043"));
    }
    @Override
    public Result findAllWalletCoinOrder(FindAllWalletCoinOrderDto findAllWalletCoinOrderDto) {
        //获取用户ID
        Long memberId = LoginUserUtils.getAppLoginUser().getId();
@@ -425,6 +676,26 @@
        orderCoinsDealEntity.setSymbol(findAllWalletCoinOrderDto.getSymbol());
        IPage<OrderCoinsDealEntity> list = orderCoinDealDao.findAllWalletCoinOrderInPage(page, orderCoinsDealEntity);
        Page<OrderWalletCoinDealVo> pageEntityToPageVo = OrderWalletCoinDealMapper.INSTANCE.pageEntityToPageVo(list);
        List<OrderWalletCoinDealVo> records = pageEntityToPageVo.getRecords();
        if (CollUtil.isNotEmpty(records)) {
            for (OrderWalletCoinDealVo orderWalletCoinDealVo : records) {
                orderWalletCoinDealVo.setFeeAmount(orderWalletCoinDealVo.getFeeAmount() == null
                        ? BigDecimal.ZERO : orderWalletCoinDealVo.getFeeAmount().setScale(4, BigDecimal.ROUND_DOWN));
                orderWalletCoinDealVo.setDealAmount(orderWalletCoinDealVo.getDealAmount() == null
                        ? BigDecimal.ZERO : orderWalletCoinDealVo.getDealAmount().setScale(4, BigDecimal.ROUND_DOWN));
                orderWalletCoinDealVo.setSymbolCnt(orderWalletCoinDealVo.getSymbolCnt() == null
                        ? BigDecimal.ZERO : orderWalletCoinDealVo.getSymbolCnt().setScale(4, BigDecimal.ROUND_DOWN));
                orderWalletCoinDealVo.setEntrustPrice(orderWalletCoinDealVo.getEntrustPrice() == null
                        ? BigDecimal.ZERO : orderWalletCoinDealVo.getEntrustPrice().setScale(4, BigDecimal.ROUND_DOWN));
                orderWalletCoinDealVo.setDealPrice(orderWalletCoinDealVo.getDealPrice() == null
                        ? BigDecimal.ZERO : orderWalletCoinDealVo.getDealPrice().setScale(4, BigDecimal.ROUND_DOWN));
            }
        }
        return Result.ok(pageEntityToPageVo);
    }
@@ -435,6 +706,22 @@
        Long memberId = LoginUserUtils.getAppLoginUser().getId();
        OrderCoinsDealEntity selectWalletCoinOrder = orderCoinDealDao.selectWalletCoinOrder(orderId, memberId);
        OrderWalletCoinDealVo entityToVo = OrderWalletCoinDealMapper.INSTANCE.entityToVoOrder(selectWalletCoinOrder);
        if (ObjectUtil.isNotEmpty(entityToVo)) {
            entityToVo.setFeeAmount(entityToVo.getFeeAmount() == null
                    ? BigDecimal.ZERO : entityToVo.getFeeAmount().setScale(4, BigDecimal.ROUND_DOWN));
            entityToVo.setDealAmount(entityToVo.getDealAmount() == null
                    ? BigDecimal.ZERO : entityToVo.getDealAmount().setScale(4, BigDecimal.ROUND_DOWN));
            entityToVo.setSymbolCnt(entityToVo.getSymbolCnt() == null
                    ? BigDecimal.ZERO : entityToVo.getSymbolCnt().setScale(4, BigDecimal.ROUND_DOWN));
            entityToVo.setEntrustPrice(entityToVo.getEntrustPrice() == null
                    ? BigDecimal.ZERO : entityToVo.getEntrustPrice().setScale(4, BigDecimal.ROUND_DOWN));
            entityToVo.setDealPrice(entityToVo.getDealPrice() == null
                    ? BigDecimal.ZERO : entityToVo.getDealPrice().setScale(4, BigDecimal.ROUND_DOWN));
        }
        return Result.ok(entityToVo);
    }
@@ -527,7 +814,9 @@
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void dealEntrustCoinOrder() {
        List<OrderCoinsEntity> list = orderCoinsDao.selectAllEntrustingCoinOrderList();
        List<String> ignoreTypes = new ArrayList<>();
        ignoreTypes.add(SymbolsConstats.ROC);
        List<OrderCoinsEntity> list = orderCoinsDao.selectAllEntrustingCoinOrderList(ignoreTypes);
        if (CollUtil.isNotEmpty(list)) {
            for (OrderCoinsEntity orderCoinsEntity : list) {
                BigDecimal nowPrice = new BigDecimal(redisUtils.getString(CoinTypeConvert.convertToKey(orderCoinsEntity.getSymbol() + "/USDT")));
@@ -592,4 +881,229 @@
            }
        }
    }
    @Transactional
    public void handleOrder(List<ExchangeTrade> trades) {
        // 处理撮合交易的订单
        for (ExchangeTrade exchangeTrade : trades) {
            if (exchangeTrade == null) {
                continue;
            }
            // 量
            BigDecimal amount = exchangeTrade.getAmount();
            // 买单ID
            Long buyOrderId = exchangeTrade.getBuyOrderId();
            // 成交金额(usdt)
            BigDecimal buyTurnover = exchangeTrade.getBuyTurnover();
            int direction = exchangeTrade.getDirection();
            // 成交价
            BigDecimal price = exchangeTrade.getPrice();
            // 卖单
            Long sellOrderId = exchangeTrade.getSellOrderId();
            // 买卖单都需要处理
            // 买单
            OrderCoinsEntity buyOrderCoinsEntity = orderCoinsDao.selectById(buyOrderId);
            if(buyOrderCoinsEntity==null){
                return;
            }
            BigDecimal buyEntrustCnt = buyOrderCoinsEntity.getEntrustCnt();
            if(buyEntrustCnt==null){
                buyEntrustCnt = BigDecimal.ZERO;
            }
            Long memberId = buyOrderCoinsEntity.getMemberId();
            if (buyOrderCoinsEntity != null) {
                // 比较剩余的量
                BigDecimal dealAmount = buyOrderCoinsEntity.getDealAmount();
                // 单的总金额
                BigDecimal entrustAmount = buyOrderCoinsEntity.getEntrustAmount();
                BigDecimal add = dealAmount.add(buyTurnover);
                BigDecimal closingPrice = buyTurnover.multiply(new BigDecimal("0.002"));
                //成交量
                BigDecimal dealCnt = buyOrderCoinsEntity.getDealCnt().add(amount);
                // 创建一个完成的单
                OrderCoinsDealEntity detail = new OrderCoinsDealEntity();
                detail.setMemberId(buyOrderCoinsEntity.getMemberId());
                detail.setOrderId(buyOrderCoinsEntity.getId());
                detail.setOrderNo(buyOrderCoinsEntity.getOrderNo());
                detail.setOrderType(buyOrderCoinsEntity.getOrderType());
                detail.setTradeType(buyOrderCoinsEntity.getTradeType());
                detail.setSymbol(buyOrderCoinsEntity.getSymbol());
                detail.setSymbolCnt(amount);
                detail.setEntrustPrice(buyOrderCoinsEntity.getEntrustPrice());
                detail.setDealPrice(price);
                detail.setDealAmount(buyTurnover);
                detail.setFeeAmount(closingPrice);
                detail.setOrderStatus(OrderCoinsDealEntity.ORDERSTATUS_DONE);
                orderCoinDealDao.insert(detail);
                // 如果这个单成交完  更改状态
                if (add.compareTo(entrustAmount) >= 0 ||(buyEntrustCnt.compareTo(BigDecimal.ZERO)>0 &&dealCnt.compareTo(buyEntrustCnt)>=0) ) {
                    OrderCoinsEntity update = new OrderCoinsEntity();
                    update.setId(buyOrderId);
                    update.setDealAmount(entrustAmount);
                    update.setDealCnt(buyOrderCoinsEntity.getDealCnt().add(amount));
                    update.setOrderStatus(OrderCoinsEntity.ORDERSTATUS_DONE);
                    update.setUpdateTime(new Date());
                    orderCoinsDao.updateById(update);
                } else {
                    OrderCoinsEntity update = new OrderCoinsEntity();
                    update.setId(buyOrderId);
                    update.setDealAmount(add);
                    update.setDealCnt(buyOrderCoinsEntity.getDealCnt().add(amount));
                    update.setUpdateTime(new Date());
                    orderCoinsDao.updateById(update);
                }
            }
            // 卖单
            OrderCoinsEntity sellOrderCoinsEntity = orderCoinsDao.selectById(sellOrderId);
            if (sellOrderCoinsEntity != null) {
                // 比较剩余的量
                BigDecimal dealAmount = sellOrderCoinsEntity.getDealCnt();
                // 单的总数量
                BigDecimal entrustCnt = sellOrderCoinsEntity.getEntrustCnt();
                BigDecimal add = dealAmount.add(amount);
                // 创建一个完成的单
                OrderCoinsDealEntity detail = new OrderCoinsDealEntity();
                detail.setMemberId(sellOrderCoinsEntity.getMemberId());
                detail.setOrderId(sellOrderCoinsEntity.getId());
                detail.setOrderNo(sellOrderCoinsEntity.getOrderNo());
                detail.setOrderType(sellOrderCoinsEntity.getOrderType());
                detail.setTradeType(sellOrderCoinsEntity.getTradeType());
                detail.setSymbol(sellOrderCoinsEntity.getSymbol());
                detail.setSymbolCnt(amount);
                detail.setEntrustPrice(sellOrderCoinsEntity.getEntrustPrice());
                detail.setDealPrice(price);
                detail.setDealAmount(buyTurnover);
                //detail.setFeeAmount(closingPrice);
                detail.setOrderStatus(OrderCoinsDealEntity.ORDERSTATUS_DONE);
                orderCoinDealDao.insert(detail);
                // 如果这个单成交完  更改状态
                if (add.compareTo(entrustCnt) >= 0) {
                    OrderCoinsEntity update = new OrderCoinsEntity();
                    update.setId(sellOrderId);
                    // 总成交额
                    update.setDealAmount(buyTurnover.add(sellOrderCoinsEntity.getDealAmount()));
                    update.setOrderStatus(OrderCoinsEntity.ORDERSTATUS_DONE);
                    update.setDealCnt(entrustCnt);
                    update.setUpdateTime(new Date());
                    orderCoinsDao.updateById(update);
                } else {
                    // 未完成
                    OrderCoinsEntity update = new OrderCoinsEntity();
                    update.setId(sellOrderId);
                    // 总成交额
                    update.setDealAmount(buyTurnover.add(sellOrderCoinsEntity.getDealAmount()));
                    update.setDealCnt(sellOrderCoinsEntity.getDealCnt().add(amount));
                    update.setUpdateTime(new Date());
                    orderCoinsDao.updateById(update);
                }
                // 买币扣除冻结usdt 增加币种的可用
                MemberWalletCoinEntity usdtWallet = memberWalletCoinDao.selectWalletCoinBymIdAndCode(buyOrderCoinsEntity.getMemberId(), MemberWalletCoinEnum.WALLETCOINCODE.getValue());
                if (usdtWallet != null) {
                    // 减少usdt冻结
                    memberWalletCoinDao.updateWalletBalance(usdtWallet.getId(), null, null, buyTurnover.negate());
                }
                // 增加买的币
                MemberWalletCoinEntity buySymbolWallet = memberWalletCoinDao.selectWalletCoinBymIdAndCode(buyOrderCoinsEntity.getMemberId(), buyOrderCoinsEntity.getSymbol());
                if (buySymbolWallet != null) {
                    memberWalletCoinDao.updateWalletBalance(buySymbolWallet.getId(), amount, amount, null);
                }
                // 流水记录
                MemberAccountFlowEntity record = new MemberAccountFlowEntity();
                record.setMemberId(buyOrderCoinsEntity.getMemberId());
                record.setPrice(buyTurnover.setScale(4, BigDecimal.ROUND_DOWN).negate());
                record.setSource(MemberAccountFlowEntity.SOURCE_BUY + buyOrderCoinsEntity.getSymbol());
                record.setRemark(MemberAccountFlowEntity.REMARK_BUY + buyOrderCoinsEntity.getSymbol() + ":" + amount);
                record.setSymbol(buyOrderCoinsEntity.getSymbol());
                record.setBalance(usdtWallet.getAvailableBalance().subtract(buyTurnover));
                memberAccountFlowEntityDao.insert(record);
                // 卖家需要减少冻结的币种  增加usdt
                MemberWalletCoinEntity memberWalletCoinEntity = memberWalletCoinDao.selectWalletCoinBymIdAndCode(sellOrderCoinsEntity.getMemberId(), sellOrderCoinsEntity.getSymbol());
                if (memberWalletCoinEntity != null) {
                    // 更新卖币减少的币种
                    memberWalletCoinDao.updateWalletBalance(memberWalletCoinEntity.getId(), null, null, amount.negate());
                }
                // 更新卖币得到的usdt
                MemberWalletCoinEntity sellWalletCoinEntity = memberWalletCoinDao.selectWalletCoinBymIdAndCode(sellOrderCoinsEntity.getMemberId(), MemberWalletCoinEnum.WALLETCOINCODE.getValue());
                if (sellOrderCoinsEntity != null) {
                    memberWalletCoinDao.updateWalletBalance(sellWalletCoinEntity.getId(), buyTurnover, buyTurnover, null);
                }
                // 流水记录
                MemberAccountFlowEntity recordSell = new MemberAccountFlowEntity();
                recordSell.setMemberId(sellOrderCoinsEntity.getMemberId());
                recordSell.setPrice(buyTurnover.setScale(4, BigDecimal.ROUND_DOWN));
                recordSell.setSource(MemberAccountFlowEntity.SOURCE_SALE + buyOrderCoinsEntity.getSymbol());
                recordSell.setRemark(MemberAccountFlowEntity.REMARK_SALE + buyOrderCoinsEntity.getSymbol() + ":" + amount.toPlainString());
                recordSell.setSymbol(buyOrderCoinsEntity.getSymbol());
                recordSell.setBalance(sellWalletCoinEntity.getAvailableBalance().add(buyTurnover));
                memberAccountFlowEntityDao.insert(recordSell);
            }
        }
    }
    @Override
    public void initOrders(String symbol, Integer type, Integer tradeType, BigDecimal price,
                           BigDecimal amount, BigDecimal entrustAmount) {
        //获取用户ID
        Long memberId = 10L;
        BigDecimal nowPriceinBigDecimal = price;
        //查询当前价
        //BigDecimal nowPrice = new BigDecimal(redisUtils.getString(CoinTypeConvert.convertToKey(symbol + "/USDT")));
        // 获取交易管理的杠杠倍率,手续费率等信息,由平台进行设置
        symbol = symbol.toUpperCase();
        // 手续费用(手续费=建仓价X数量X手续费率)
        BigDecimal closingPrice = BigDecimal.ZERO;
        // BigDecimal totalPayPricCoin = nowPrice.multiply(amount).add(closingPrice);
        // 首先将单插入到数据库主表(委托表)
        // 创建订单
        OrderCoinsEntity order = new OrderCoinsEntity();
        //根据委托类型生成不同数据
        // 如果是限价交易直接插入主表数据
        order.setMemberId(memberId);
        order.setOrderNo(generateSimpleSerialno(memberId.toString()));
        order.setOrderType(type);
        order.setSymbol(symbol);
        //order.setMarkPrice(nowPrice);
        // 成交量 先设置为0
        order.setDealCnt(BigDecimal.ZERO);
        // 成交价
        //order.setDealPrice(price);
        // 成交金额
        order.setDealAmount(BigDecimal.ZERO);
        order.setOrderStatus(OrderCoinsEntity.ORDERSTATUS_DODING);
        order.setTradeType(tradeType);
        // 手续费
        order.setFeeAmount(closingPrice);
        if (OrderCoinsEntity.TRADETYPE_FIXEDPRICE.equals(tradeType)) {
            // 限价 是需要价格和数量 可以得到成交金额
            // 下单量
            order.setEntrustCnt(amount);
            // 下单价格
            order.setEntrustPrice(price);
            order.setEntrustAmount(amount.multiply(price));
        } else {
            if (OrderCoinsEntity.ORDERTYPE_BUY.equals(type)) {
                // 市价 只有金额
                order.setEntrustAmount(entrustAmount);
            } else {
                // 下单量
                order.setEntrustCnt(amount);
                // 下单价格
                order.setEntrustPrice(price);
                order.setEntrustAmount(amount.multiply(price));
            }
        }
        orderCoinsDao.insert(order);
        // 加入到撮合
        CoinTrader trader = factory.getTrader(symbol);
        trader.trade(order);
    }
}