From c119feb821bdb1e6ef407f55056173f752c01c32 Mon Sep 17 00:00:00 2001
From: zainali5120 <512061637@qq.com>
Date: Thu, 16 Jul 2020 17:11:20 +0800
Subject: [PATCH] 订单止盈止损类提交

---
 src/main/java/com/xcong/excoin/modules/coin/service/impl/OrderCoinServiceImpl.java |   77 +++++++++++++++++++++++++++++++++-----
 1 files changed, 66 insertions(+), 11 deletions(-)

diff --git a/src/main/java/com/xcong/excoin/modules/coin/service/impl/OrderCoinServiceImpl.java b/src/main/java/com/xcong/excoin/modules/coin/service/impl/OrderCoinServiceImpl.java
index 5f330e7..c96299f 100644
--- a/src/main/java/com/xcong/excoin/modules/coin/service/impl/OrderCoinServiceImpl.java
+++ b/src/main/java/com/xcong/excoin/modules/coin/service/impl/OrderCoinServiceImpl.java
@@ -19,7 +19,6 @@
 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 +37,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;
@@ -166,7 +161,7 @@
     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")));
 
@@ -185,13 +180,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 {
@@ -204,7 +200,8 @@
 
         // 创建订单
         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()));
@@ -248,7 +245,15 @@
             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);
@@ -263,8 +268,8 @@
             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);
@@ -319,6 +324,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);
             }
         }
@@ -416,6 +435,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);
     }
@@ -426,6 +465,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);
     }
 

--
Gitblit v1.9.1