xiaoyong931011
2020-06-09 33bf873937ae03b96ce6d217c24d0b403b4468a1
src/main/java/com/xcong/excoin/modules/coin/service/impl/OrderCoinServiceImpl.java
@@ -19,6 +19,8 @@
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.common.LoginUserUtils;
import com.xcong.excoin.common.enumerates.MemberWalletCoinEnum;
@@ -32,6 +34,7 @@
import com.xcong.excoin.modules.coin.entity.OrderCoinsEntity;
import com.xcong.excoin.modules.coin.mapper.OrderWalletCoinDealMapper;
import com.xcong.excoin.modules.coin.mapper.OrderWalletCoinMapper;
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;
@@ -40,6 +43,9 @@
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;
@@ -203,15 +209,15 @@
            order.setOrderNo(generateSimpleSerialno(memberId.toString()));
            order.setOrderType(type);
            order.setSymbol(symbol);
            order.setMarkPrice(nowPrice.setScale(4, BigDecimal.ROUND_DOWN));
            order.setEntrustCnt(amount.setScale(4, BigDecimal.ROUND_DOWN));
            order.setEntrustPrice(price.setScale(4, BigDecimal.ROUND_DOWN));
            order.setDealCnt(amount.setScale(4, BigDecimal.ROUND_DOWN));
            order.setDealPrice(price.setScale(4, BigDecimal.ROUND_DOWN));
            order.setDealAmount(totalPayPrice.setScale(4, BigDecimal.ROUND_DOWN));
            order.setMarkPrice(nowPrice);
            order.setEntrustCnt(amount);
            order.setEntrustPrice(price);
            order.setDealCnt(amount);
            order.setDealPrice(price);
            order.setDealAmount(totalPayPrice);
            order.setOrderStatus(OrderCoinsEntity.ORDERSTATUS_DODING);
            order.setTradeType(tradeType);
            order.setFeeAmount(closingPrice.setScale(4, BigDecimal.ROUND_DOWN));
            order.setFeeAmount(closingPrice);
            orderCoinsDao.insert(order);
            
            //更新用户钱包信息
@@ -220,15 +226,15 @@
               //如果是买入,所对应的币种增加,USDT账户减少金额
               BigDecimal availableBalance = walletCoinUsdt.getAvailableBalance().subtract(totalPayPrice);
               BigDecimal frozenBalance = walletCoinUsdt.getFrozenBalance().add(totalPayPrice);
               walletCoinUsdt.setAvailableBalance(availableBalance.setScale(4, BigDecimal.ROUND_DOWN));
               walletCoinUsdt.setFrozenBalance(frozenBalance.setScale(4, BigDecimal.ROUND_DOWN));
               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.setScale(4, BigDecimal.ROUND_DOWN));
               walletCoin.setFrozenBalance(frozenBalance.setScale(4, BigDecimal.ROUND_DOWN));
               walletCoin.setAvailableBalance(availableBalance);
               walletCoin.setFrozenBalance(frozenBalance);
               memberWalletCoinDao.updateById(walletCoin);
            } 
         } else {
@@ -237,15 +243,15 @@
            order.setOrderNo(generateSimpleSerialno(memberId.toString()));
            order.setOrderType(type);
            order.setSymbol(symbol);
            order.setMarkPrice(nowPrice.setScale(4, BigDecimal.ROUND_DOWN));
            order.setEntrustCnt(amount.setScale(4, BigDecimal.ROUND_DOWN));
            order.setEntrustPrice(price.setScale(4, BigDecimal.ROUND_DOWN));
            order.setDealCnt(amount.setScale(4, BigDecimal.ROUND_DOWN));
            order.setDealPrice(price.setScale(4, BigDecimal.ROUND_DOWN));
            order.setDealAmount(totalPayPrice.setScale(4, BigDecimal.ROUND_DOWN));
            order.setMarkPrice(nowPrice);
            order.setEntrustCnt(amount);
            order.setEntrustPrice(price);
            order.setDealCnt(amount);
            order.setDealPrice(price);
            order.setDealAmount(totalPayPrice);
            order.setOrderStatus(OrderCoinsEntity.ORDERSTATUS_DONE);
            order.setTradeType(tradeType);
            order.setFeeAmount(closingPrice.setScale(4, BigDecimal.ROUND_DOWN));
            order.setFeeAmount(closingPrice);
            orderCoinsDao.insert(order);
            
            OrderCoinsDealEntity detail = new OrderCoinsDealEntity();
@@ -255,29 +261,29 @@
            detail.setOrderType(type);
            detail.setTradeType(tradeType);
            detail.setSymbol(symbol);
            detail.setSymbolCnt(amount.setScale(4, BigDecimal.ROUND_DOWN));
            detail.setEntrustPrice(price.setScale(4, BigDecimal.ROUND_DOWN));
            detail.setDealPrice(price.setScale(4, BigDecimal.ROUND_DOWN));
            detail.setDealAmount(totalPayPrice.setScale(4, BigDecimal.ROUND_DOWN));
            detail.setFeeAmount(closingPrice.setScale(4, BigDecimal.ROUND_DOWN));
            detail.setSymbolCnt(amount);
            detail.setEntrustPrice(price);
            detail.setDealPrice(price);
            detail.setDealAmount(totalPayPrice);
            detail.setFeeAmount(closingPrice);
            detail.setOrderStatus(OrderCoinsDealEntity.ORDERSTATUS_DONE);
            orderCoinDealDao.insert(detail);
            
            if(OrderCoinsEntity.ORDERTYPE_BUY.equals(type)) {
               //如果是买入,所对应的币种增加,USDT账户减少金额
               // 更新用户的可用金额
               walletCoin.setAvailableBalance(walletCoin.getAvailableBalance().add(amount).setScale(4, BigDecimal.ROUND_DOWN));
               walletCoin.setAvailableBalance(walletCoin.getAvailableBalance().add(amount));
               memberWalletCoinDao.updateById(walletCoin);
               
               walletCoinUsdt.setAvailableBalance(walletCoinUsdt.getAvailableBalance().subtract(totalPayPrice).setScale(4, BigDecimal.ROUND_DOWN));
               walletCoinUsdt.setAvailableBalance(walletCoinUsdt.getAvailableBalance().subtract(totalPayPrice));
               memberWalletCoinDao.updateById(walletCoinUsdt);
            }else {
               //如果是卖出,币种减少,USDT增加
               walletCoin.setAvailableBalance(walletCoin.getAvailableBalance().subtract(amount).setScale(4, BigDecimal.ROUND_DOWN));
               walletCoin.setAvailableBalance(walletCoin.getAvailableBalance().subtract(amount));
               memberWalletCoinDao.updateById(walletCoin);
               
               BigDecimal subtract = totalPayPrice.subtract(closingPrice).subtract(closingPrice);
               walletCoinUsdt.setAvailableBalance(walletCoinUsdt.getAvailableBalance().add(subtract).setScale(4, BigDecimal.ROUND_DOWN));
               walletCoinUsdt.setAvailableBalance(walletCoinUsdt.getAvailableBalance().add(subtract));
               memberWalletCoinDao.updateById(walletCoinUsdt);
            }
         }
@@ -287,14 +293,14 @@
         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.setScale(4, BigDecimal.ROUND_DOWN));
            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.setScale(4, BigDecimal.ROUND_DOWN));
            record.setRemark(MemberAccountFlowEntity.REMARK_SALE+symbol+":"+amount);
         }
         record.setSymbol(symbol);
         record.setBalance(walletCoinUsdt.getAvailableBalance().setScale(4, BigDecimal.ROUND_DOWN));
         record.setBalance(walletCoinUsdt.getAvailableBalance());
         
         memberAccountFlowEntityDao.insert(record);
         
@@ -343,10 +349,10 @@
               detail.setSymbol(symbol);
               detail.setOrderStatus(OrderCoinsDealEntity.ORDERSTATUS_CANCEL);
               detail.setSymbolCnt(orderCoinsEntity.getEntrustCnt());
               detail.setEntrustPrice(orderCoinsEntity.getEntrustPrice().setScale(4, BigDecimal.ROUND_DOWN));
               detail.setDealPrice(orderCoinsEntity.getDealPrice().setScale(4, BigDecimal.ROUND_DOWN));
               detail.setDealAmount(orderCoinsEntity.getDealAmount().setScale(4, BigDecimal.ROUND_DOWN));
               detail.setFeeAmount(orderCoinsEntity.getFeeAmount().setScale(4, BigDecimal.ROUND_DOWN));
               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())) {
@@ -359,14 +365,14 @@
                     //返还金额=开仓价*未成交数量+手续费
                     BigDecimal returnBalance = orderCoinsEntity.getDealAmount();
                     
                     walletCoin.setAvailableBalance(walletCoin.getAvailableBalance().add(returnBalance).setScale(4, BigDecimal.ROUND_DOWN));
                     walletCoin.setFrozenBalance(walletCoin.getFrozenBalance().subtract(returnBalance).setScale(4, BigDecimal.ROUND_DOWN));
                     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().setScale(4, BigDecimal.ROUND_DOWN));
                     record.setBalance(walletCoin.getAvailableBalance());
                     record.setMemberId(memberId);
                     record.setSymbol(symbol);
                     record.setPrice(returnBalance);
@@ -379,17 +385,17 @@
                  if (ObjectUtil.isNotEmpty(walletCoin)) {
                     
                     BigDecimal returnBalance = orderCoinsEntity.getEntrustCnt();
                     walletCoin.setAvailableBalance(walletCoin.getAvailableBalance().add(returnBalance).setScale(4, BigDecimal.ROUND_DOWN));
                     walletCoin.setFrozenBalance(walletCoin.getFrozenBalance().subtract(returnBalance).setScale(4, BigDecimal.ROUND_DOWN));
                     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().setScale(4, BigDecimal.ROUND_DOWN));
                     record.setBalance(walletCoin.getAvailableBalance());
                     record.setMemberId(memberId);
                     record.setSymbol(symbol);
                     record.setPrice(walletCoin.getFrozenBalance().setScale(4, BigDecimal.ROUND_DOWN));
                     record.setPrice(walletCoin.getFrozenBalance());
                     memberAccountFlowEntityDao.insert(record);
                     return Result.ok(MessageSourceUtils.getString("order_service_0013"));
                  }
@@ -399,22 +405,19 @@
   }
   @Override
   public Result findAllWalletCoinOrder(String symbol) {
   public Result findAllWalletCoinOrder(FindAllWalletCoinOrderDto findAllWalletCoinOrderDto) {
      //获取用户ID
      Long memberId = LoginUserUtils.getAppLoginUser().getId();
      
      OrderWalletCoinDealListVo orderWalletCoinDealListVo = new OrderWalletCoinDealListVo();
      List<OrderWalletCoinDealVo> arrayList = new ArrayList<OrderWalletCoinDealVo>();
      List<OrderCoinsDealEntity> selectAllWalletCoinOrder = orderCoinDealDao.selectAllWalletCoinOrderBySymbol(memberId,symbol);
      if(CollUtil.isNotEmpty(selectAllWalletCoinOrder)) {
         for(OrderCoinsDealEntity orderCoinsDealEntity: selectAllWalletCoinOrder) {
            OrderWalletCoinDealVo entityToVo = OrderWalletCoinDealMapper.INSTANCE.entityToVoOrder(orderCoinsDealEntity);
            arrayList.add(entityToVo);
         }
      }
      orderWalletCoinDealListVo.setOrderWalletCoinDealVo(arrayList);
      return Result.ok(orderWalletCoinDealListVo);
      Page<OrderCoinsDealEntity> page = new Page<>(findAllWalletCoinOrderDto.getPageNum(), findAllWalletCoinOrderDto.getPageSize());
      OrderCoinsDealEntity orderCoinsDealEntity = new OrderCoinsDealEntity();
      orderCoinsDealEntity.setMemberId(memberId);
      orderCoinsDealEntity.setSymbol(findAllWalletCoinOrderDto.getSymbol());
        IPage<OrderCoinsDealEntity> list = orderCoinDealDao.findAllWalletCoinOrderInPage(page, orderCoinsDealEntity);
        Page<OrderWalletCoinDealVo> pageEntityToPageVo = OrderWalletCoinDealMapper.INSTANCE.pageEntityToPageVo(list);
      return Result.ok(pageEntityToPageVo);
   }
   @Override
@@ -500,8 +503,6 @@
            for(MemberSelectSymbolsEntity memberSelectSymbolsEntity : selectSymbolByMemIdAndSymbol) {
               if(platformSymbolsCoinEntity.getName().equals(memberSelectSymbolsEntity.getSymbol())) {
                  memberSelectSymbolsVo.setIsCollect(MemberSelectSymbolsVo.ISCOLLECT_YES);
               }else {
                  memberSelectSymbolsVo.setIsCollect(MemberSelectSymbolsVo.ISCOLLECT_NO);
               }
            }
         }else {