| | |
| | | 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.xcong.excoin.modules.platform.entity.PlatformCnyUsdtExchangeEntity;
|
| | | import com.xcong.excoin.modules.platform.entity.PlatformSymbolsCoinEntity;
|
| | |
|
| | | 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.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;
|
| | |
| | | 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;
|
| | |
| | | RedisUtils redisUtils;
|
| | | @Resource
|
| | | PlatformSymbolsCoinDao platformSymbolsCoinDao;
|
| | |
|
| | | @Resource
|
| | | private CoinTraderFactory factory;
|
| | |
|
| | |
|
| | | @Override
|
| | | public String generateSimpleSerialno(String userId) {
|
| | |
| | | public Result submitSalesWalletCoinOrder(String symbol, Integer type, Integer tradeType, BigDecimal price, BigDecimal amount) {
|
| | | //获取用户ID
|
| | | Long memberId = LoginUserUtils.getAppLoginUser().getId();
|
| | |
|
| | | BigDecimal nowPriceinBigDecimal = price;
|
| | | //查询当前价
|
| | | BigDecimal nowPrice = new BigDecimal(redisUtils.getString(CoinTypeConvert.convertToKey(symbol + "/USDT")));
|
| | |
|
| | |
| | | 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 {
|
| | |
| | |
|
| | | // 创建订单
|
| | | OrderCoinsEntity order = new OrderCoinsEntity();
|
| | | if (OrderCoinsEntity.TRADETYPE_FIXEDPRICE.equals(tradeType)) {
|
| | | 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)) {
|
| | | // 如果是限价交易直接插入主表数据
|
| | | order.setMemberId(memberId);
|
| | | order.setOrderNo(generateSimpleSerialno(memberId.toString()));
|
| | |
| | | order.setEntrustCnt(amount);
|
| | | order.setEntrustPrice(price);
|
| | | order.setDealCnt(amount);
|
| | | order.setDealPrice(price);
|
| | | 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手续费率)
|
| | | closingPrice = nowPrice.multiply(amount).multiply(tradeSetting.getCoinFeeRatio());
|
| | | //总费用 = 成交价*数量+手续费
|
| | | totalPayPrice = nowPrice.multiply(amount).add(closingPrice);
|
| | | price = nowPrice;
|
| | | }
|
| | | order.setDealPrice(nowPrice);
|
| | | order.setDealAmount(totalPayPrice);
|
| | | order.setOrderStatus(OrderCoinsEntity.ORDERSTATUS_DONE);
|
| | | order.setTradeType(tradeType);
|
| | |
| | | detail.setTradeType(tradeType);
|
| | | detail.setSymbol(symbol);
|
| | | detail.setSymbolCnt(amount);
|
| | | detail.setEntrustPrice(price);
|
| | | detail.setDealPrice(price);
|
| | | detail.setEntrustPrice(nowPriceinBigDecimal);
|
| | | detail.setDealPrice(nowPrice);
|
| | | detail.setDealAmount(totalPayPrice);
|
| | | detail.setFeeAmount(closingPrice);
|
| | | detail.setOrderStatus(OrderCoinsDealEntity.ORDERSTATUS_DONE);
|
| | |
| | | }
|
| | |
|
| | | @Override
|
| | | public Result submitSalesWalletCoinOrderWithMatch(String symbol, Integer type, Integer tradeType, BigDecimal price, BigDecimal amount, BigDecimal entrustAmount) {
|
| | | //获取用户ID
|
| | | Long memberId = 13L;
|
| | | 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)) {
|
| | | return Result.fail(MessageSourceUtils.getString("order_service_0003"));
|
| | | }
|
| | | // 查询交易设置
|
| | | PlatformTradeSettingEntity tradeSetting = platformTradeSettingDao.findTradeSetting();
|
| | | if (ObjectUtil.isEmpty(tradeSetting)) {
|
| | | return Result.fail(MessageSourceUtils.getString("order_service_0009"));
|
| | | }
|
| | | // 手续费用(手续费=建仓价X数量X手续费率)
|
| | | BigDecimal closingPrice ;
|
| | | // 总费用分两种 1,限价交易 是价格*数量+手续费 2,市价交易 用户输入的金额+手续费
|
| | | //总费用 = 成交价*数量+手续费
|
| | | BigDecimal totalPayPrice ;
|
| | | if(OrderCoinsEntity.TRADETYPE_FIXEDPRICE.equals(tradeType) ){
|
| | | // 限价
|
| | | closingPrice = price.multiply(amount).multiply(tradeSetting.getCoinFeeRatio());
|
| | | totalPayPrice = price.multiply(amount).add(closingPrice);
|
| | | }else{
|
| | | // 市价
|
| | | 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.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);
|
| | |
|
| | | //更新用户钱包信息
|
| | | //冻结相应的资产
|
| | | 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);
|
| | | } else {
|
| | | //如果是卖出,币种减少,USDT增加
|
| | | BigDecimal availableBalance = walletCoin.getAvailableBalance().subtract(amount);
|
| | | BigDecimal frozenBalance = walletCoin.getFrozenBalance().add(amount);
|
| | | walletCoin.setAvailableBalance(availableBalance);
|
| | | walletCoin.setFrozenBalance(frozenBalance);
|
| | | memberWalletCoinDao.updateById(walletCoin);
|
| | | }
|
| | | // 加入到撮合
|
| | | CoinTrader trader = factory.getTrader(symbol);
|
| | | // if(trader==null){
|
| | | //
|
| | | // trader = new CoinTrader("NEKK");
|
| | | // //newTrader.setKafkaTemplate(kafkaTemplate);
|
| | | // //newTrader.setBaseCoinScale(coin.getBaseCoinScale());
|
| | | // //newTrader.setCoinScale(coin.getCoinScale());
|
| | | // // newTrader.setPublishType(coin.getPublishType());
|
| | | // //newTrader.setClearTime(coin.getClearTime());
|
| | | //
|
| | | // // 创建成功以后需要对未处理订单预处理
|
| | | // //log.info("======CoinTrader Process: " + symbol + "======");
|
| | | // List<OrderCoinsEntity> orders = orderCoinsDao.selectAllEntrustingCoinOrderList();
|
| | | // if(CollectionUtils.isNotEmpty(orders)){
|
| | | // List<OrderCoinsEntity> tradingOrders = new ArrayList<>();
|
| | | // List<OrderCoinsEntity> completedOrders = new ArrayList<>();
|
| | | // orders.forEach(order1 -> {
|
| | | // tradingOrders.add(order1);
|
| | | // });
|
| | | // try {
|
| | | // trader.trade(tradingOrders);
|
| | | // } catch (ParseException e) {
|
| | | // e.printStackTrace();
|
| | | // // log.info("异常:trader.trade(tradingOrders);");
|
| | | // }
|
| | | // }
|
| | | //
|
| | | // trader.setReady(true);
|
| | | // factory.addTrader(symbol, trader);
|
| | | // }
|
| | | trader.trade(order);
|
| | |
|
| | |
|
| | | // // 流水记录 TODO
|
| | | // MemberAccountFlowEntity record = new MemberAccountFlowEntity();
|
| | | // record.setMemberId(memberId);
|
| | | // if (OrderCoinsEntity.ORDERTYPE_BUY.equals(type)) {
|
| | | // record.setPrice(totalPayPrice.setScale(4, BigDecimal.ROUND_DOWN));
|
| | | // record.setSource(MemberAccountFlowEntity.SOURCE_BUY + symbol);
|
| | | // record.setRemark(MemberAccountFlowEntity.REMARK_BUY + symbol + ":" + amount);
|
| | | // } else {
|
| | | // record.setPrice(totalPayPrice.negate().setScale(4, BigDecimal.ROUND_DOWN));
|
| | | // record.setSource(MemberAccountFlowEntity.SOURCE_SALE + symbol);
|
| | | // record.setRemark(MemberAccountFlowEntity.REMARK_SALE + symbol + ":" + amount);
|
| | | // }
|
| | | // record.setSymbol(symbol);
|
| | | // record.setBalance(walletCoinUsdt.getAvailableBalance());
|
| | | //
|
| | | // memberAccountFlowEntityDao.insert(record);
|
| | |
|
| | | return Result.ok(MessageSourceUtils.getString("order_service_0011"));
|
| | | }
|
| | |
|
| | | @Override
|
| | | public Result getEntrustWalletCoinOrder(String symbol, Integer status) {
|
| | | //获取用户ID
|
| | | Long memberId = LoginUserUtils.getAppLoginUser().getId();
|
| | |
| | | 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);
|
| | | }
|
| | | }
|
| | |
| | | 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);
|
| | | }
|
| | |
| | | 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);
|
| | | }
|
| | |
|
| | |
| | | }
|
| | | }
|
| | | }
|
| | |
|
| | | public void handleOrder(List<ExchangeTrade> trades){
|
| | | // 处理撮合交易的订单
|
| | | for(ExchangeTrade exchangeTrade : trades){
|
| | |
|
| | | BigDecimal amount = exchangeTrade.getAmount();
|
| | | Long buyOrderId = exchangeTrade.getBuyOrderId();
|
| | | BigDecimal buyTurnover = exchangeTrade.getBuyTurnover();
|
| | | int direction = exchangeTrade.getDirection();
|
| | | BigDecimal price = exchangeTrade.getPrice();
|
| | | Long sellOrderId = exchangeTrade.getSellOrderId();
|
| | | // 买卖单都需要处理
|
| | | // 买单
|
| | | OrderCoinsEntity buyOrderCoinsEntity = orderCoinsDao.selectById(buyOrderId);
|
| | | if(buyOrderCoinsEntity!=null){
|
| | | // 比较剩余的量
|
| | | BigDecimal dealAmount = buyOrderCoinsEntity.getDealAmount();
|
| | | // 单的总金额
|
| | | BigDecimal entrustAmount = buyOrderCoinsEntity.getEntrustAmount();
|
| | | BigDecimal add = dealAmount.add(buyTurnover);
|
| | | // 创建一个完成的单
|
| | | OrderCoinsDealEntity detail = new OrderCoinsDealEntity();
|
| | | detail.setMemberId(buyOrderCoinsEntity.getMemberId());
|
| | | //detail.setOrderId(order.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){
|
| | | 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(order.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 memberWalletCoinEntity = memberWalletCoinDao.selectWalletCoinBymIdAndCode(sellOrderCoinsEntity.getMemberId(), sellOrderCoinsEntity.getSymbol());
|
| | | if(memberWalletCoinEntity!=null){
|
| | | memberWalletCoinDao.updateWalletBalance(memberWalletCoinEntity.getId(),buyTurnover,buyTurnover,null);
|
| | | }
|
| | |
|
| | | }
|
| | | }
|
| | | }
|
| | | }
|