From 2f427a16d825c67ac835cff32c8882e0b048bd17 Mon Sep 17 00:00:00 2001
From: xiaoyong931011 <15274802129@163.com>
Date: Mon, 29 Mar 2021 17:08:29 +0800
Subject: [PATCH] 20210329  全仓模式下资金划转,需要减去亏损

---
 src/main/java/com/xcong/excoin/modules/coin/service/impl/CoinServiceImpl.java |   85 +++++++++++++++++++++++++++++++++---------
 1 files changed, 67 insertions(+), 18 deletions(-)

diff --git a/src/main/java/com/xcong/excoin/modules/coin/service/impl/CoinServiceImpl.java b/src/main/java/com/xcong/excoin/modules/coin/service/impl/CoinServiceImpl.java
index a3e53bb..212a3b2 100644
--- a/src/main/java/com/xcong/excoin/modules/coin/service/impl/CoinServiceImpl.java
+++ b/src/main/java/com/xcong/excoin/modules/coin/service/impl/CoinServiceImpl.java
@@ -9,7 +9,13 @@
 import javax.annotation.Resource;
 import javax.validation.Valid;
 
+import com.xcong.excoin.modules.contract.dao.ContractHoldOrderDao;
+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.platform.entity.PlatformCnyUsdtExchangeEntity;
+import com.xcong.excoin.modules.platform.entity.PlatformTradeSettingEntity;
+import com.xcong.excoin.utils.*;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
@@ -48,10 +54,6 @@
 import com.xcong.excoin.modules.member.entity.MemberWalletCoinEntity;
 import com.xcong.excoin.modules.member.entity.MemberWalletContractEntity;
 import com.xcong.excoin.modules.platform.dao.PlatformCnyUsdtExchangeDao;
-import com.xcong.excoin.utils.CoinTypeConvert;
-import com.xcong.excoin.utils.MessageSourceUtils;
-import com.xcong.excoin.utils.RedisUtils;
-import com.xcong.excoin.utils.ThreadPoolUtils;
 
 import cn.hutool.core.collection.CollUtil;
 import cn.hutool.core.util.ObjectUtil;
@@ -81,6 +83,10 @@
     MemberCoinWithdrawDao memberCoinWithdrawDao;
     @Resource
     RedisUtils redisUtils;
+    @Resource
+    CacheSettingUtils cacheSettingUtils;
+    @Resource
+    ContractHoldOrderDao contractHoldOrderDao;
 
 
     @Override
@@ -318,7 +324,9 @@
             //更新合约全仓模式下的订单权益
             MemberEntity memberEntity = memberDao.selectById(memberId);
             String symbols = symbol+"/USDT";
-            ThreadPoolUtils.sendWholeForceClosingPrice(symbols, memberEntity);
+            //ThreadPoolUtils.sendWholeForceClosingPrice(symbols, memberEntity);
+            // 全仓爆仓
+            ThreadPoolUtils.sendWholePrice(memberEntity.getId());
             
             //添加币币资金划转历史记录
             MemberAccountMoneyChange memberAccountRecord = new MemberAccountMoneyChange();
@@ -402,15 +410,54 @@
     @Override
     @Transactional(rollbackFor = Exception.class)
     public Result contractTransferToWalletCoins(BigDecimal balance, String symbol) {
-    	if (balance.compareTo(BigDecimal.ZERO) <= 0) {
+        //获取用户ID
+        Long memberId = LoginUserUtils.getAppLoginUser().getId();
+        MemberEntity memberEntity = memberDao.selectById(memberId);
+        if (balance.compareTo(BigDecimal.ZERO) <= 0) {
     		return Result.fail(MessageSourceUtils.getString("member_service_0004"));
     	}
-    	//获取用户ID
-    	Long memberId = LoginUserUtils.getAppLoginUser().getId();
+
+        //获取全仓模式下的所有持仓信息
+        PlatformTradeSettingEntity tradeSetting = cacheSettingUtils.getTradeSetting();
+        BigDecimal newPriceSymbol = new BigDecimal(redisUtils.getString(CoinTypeConvert.convertToKey(symbol)));
+        List<ContractHoldOrderEntity> holdOrderEntities = contractHoldOrderDao.selectHoldOrderListForWholeByMemberIdAndSymbol(memberEntity.getId(),"");
+
+        // 总盈利
+        BigDecimal totalProfitOrLess = BigDecimal.ZERO;
+        if (CollUtil.isNotEmpty(holdOrderEntities)) {
+            for (ContractHoldOrderEntity holdOrderEntity : holdOrderEntities) {
+                // 获取最新价
+                BigDecimal newPrice = new BigDecimal(redisUtils.getString(CoinTypeConvert.convertToKey(holdOrderEntity.getSymbol())));
+                BigDecimal lotNumber = cacheSettingUtils.getSymbolSku(holdOrderEntity.getSymbol());
+
+                // 单个订单盈利
+                BigDecimal profitOrLess = BigDecimal.ZERO;
+                // 开多
+                if (ContractHoldOrderEntity.OPENING_TYPE_MORE == holdOrderEntity.getOpeningType()) {
+                    profitOrLess = newPrice.subtract(holdOrderEntity.getOpeningPrice()).multiply(new BigDecimal(holdOrderEntity.getSymbolCntSale())).multiply(lotNumber);
+                    // 开空
+                } else {
+                    profitOrLess = holdOrderEntity.getOpeningPrice().subtract(newPrice).multiply(new BigDecimal(holdOrderEntity.getSymbolCntSale())).multiply(lotNumber);
+                }
+
+                if (MemberEntity.IS_PROFIT_Y == memberEntity.getIsProfit()) {
+                    if (profitOrLess.compareTo(BigDecimal.ZERO) > 0) {
+                        profitOrLess = profitOrLess.multiply(BigDecimal.ONE.subtract(tradeSetting.getForceParam()));
+                    } else {
+                        profitOrLess = profitOrLess.multiply(BigDecimal.ONE.add(tradeSetting.getForceParam()));
+                    }
+                }
+                totalProfitOrLess = totalProfitOrLess.add(profitOrLess);
+            }
+        }
     	
     	String walletCode = MemberWalletCoinEnum.WALLETCOINCODE.getValue();
     	MemberWalletContractEntity walletContract = memberWalletContractDao.findWalletContractByMemberIdAndSymbol(memberId, symbol);
     	BigDecimal availableBalance = walletContract.getAvailableBalance();
+    	//可用减去盈亏
+    	if(totalProfitOrLess.compareTo(BigDecimal.ZERO) < 0){
+            availableBalance = availableBalance.add(totalProfitOrLess);
+        }
     	// 扣币
     	BigDecimal availableSubtract = availableBalance.subtract(balance);
     	if (availableSubtract.compareTo(BigDecimal.ZERO) < 0) {
@@ -438,11 +485,9 @@
     	if (updateById < 1) {
     		return Result.fail(MessageSourceUtils.getString("member_service_0096"));
     	}
-    	
-    	//更新合约全仓模式下的订单权益
-        MemberEntity memberEntity = memberDao.selectById(memberId);
-        String symbols = symbol+"/USDT";
-        ThreadPoolUtils.sendWholeForceClosingPrice(symbols, memberEntity);
+
+        // 全仓爆仓
+        ThreadPoolUtils.sendWholePrice(memberEntity.getId());
     	
     	//添加资金划转历史记录
     	MemberAccountMoneyChange memberAccountRecord = new MemberAccountMoneyChange();
@@ -682,7 +727,9 @@
     		//更新合约全仓模式下的订单权益
             MemberEntity memberEntity = memberDao.selectById(memberId);
             String symbols = symbol+"/USDT";
-            ThreadPoolUtils.sendWholeForceClosingPrice(symbols, memberEntity);
+            //ThreadPoolUtils.sendWholeForceClosingPrice(symbols, memberEntity);
+            // 全仓爆仓
+            ThreadPoolUtils.sendWholePrice(memberEntity.getId());
     		
     		//添加资金划转历史记录
     		memberAccountRecord.setMemberId(memberId);
@@ -862,7 +909,9 @@
     	//更新合约全仓模式下的订单权益
         MemberEntity memberEntity = memberDao.selectById(memberId);
         String symbols = symbolOut+"/USDT";
-        ThreadPoolUtils.sendWholeForceClosingPrice(symbols, memberEntity);
+        //ThreadPoolUtils.sendWholeForceClosingPrice(symbols, memberEntity);
+        // 全仓爆仓
+        ThreadPoolUtils.sendWholePrice(memberEntity.getId());
         
         // 加币
         // 查询合约账户
@@ -880,9 +929,9 @@
         
         //更新合约全仓模式下的订单权益
         String symbolIns = symbolIn+"/USDT";
-        ThreadPoolUtils.sendWholeForceClosingPrice(symbolIns, memberEntity);
-        
-        ThreadPoolUtils.sendWholePrice(memberId);
+        //ThreadPoolUtils.sendWholeForceClosingPrice(symbolIns, memberEntity);
+        // 全仓爆仓
+        ThreadPoolUtils.sendWholePrice(memberEntity.getId());
         
         //添加币币资金划转历史记录
         MemberAccountMoneyChange memberAccountRecord = new MemberAccountMoneyChange();

--
Gitblit v1.9.1