From 1498b23be871aec99314da998569a94c9ab53607 Mon Sep 17 00:00:00 2001
From: Helius <wangdoubleone@gmail.com>
Date: Thu, 29 Oct 2020 17:56:37 +0800
Subject: [PATCH] modify

---
 src/main/java/com/xcong/excoin/modules/coin/service/impl/CoinServiceImpl.java |  135 +++++++++++++++++++++++++++++++++++++-------
 1 files changed, 112 insertions(+), 23 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 1ce5f47..13f7a05 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
@@ -2,10 +2,15 @@
 
 import java.math.BigDecimal;
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
+
 import javax.annotation.Resource;
 
 import com.xcong.excoin.modules.platform.entity.PlatformCnyUsdtExchangeEntity;
+import com.xcong.excoin.modules.platform.entity.PlatformTradeSettingEntity;
+
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
@@ -29,6 +34,10 @@
 import com.xcong.excoin.modules.coin.parameter.vo.MemberWalletCoinVo;
 import com.xcong.excoin.modules.coin.parameter.vo.MemberWalletContractInfoVo;
 import com.xcong.excoin.modules.coin.service.CoinService;
+import com.xcong.excoin.modules.contract.dao.ContractHoldOrderDao;
+import com.xcong.excoin.modules.contract.entity.ContractHoldOrderEntity;
+import com.xcong.excoin.modules.contract.mapper.ContractHoldOrderEntityMapper;
+import com.xcong.excoin.modules.contract.parameter.vo.HoldOrderListVo;
 import com.xcong.excoin.modules.member.dao.MemberDao;
 import com.xcong.excoin.modules.member.dao.MemberWalletAgentDao;
 import com.xcong.excoin.modules.member.dao.MemberWalletCoinDao;
@@ -38,6 +47,7 @@
 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.CacheSettingUtils;
 import com.xcong.excoin.utils.CoinTypeConvert;
 import com.xcong.excoin.utils.MessageSourceUtils;
 import com.xcong.excoin.utils.RedisUtils;
@@ -66,6 +76,10 @@
     MemberDao memberDao;
     @Resource
     RedisUtils redisUtils;
+    @Resource
+    private ContractHoldOrderDao contractHoldOrderDao;
+    @Resource
+    private CacheSettingUtils cacheSettingUtils;
 
 
     @Override
@@ -181,23 +195,33 @@
 
         //获取用户ID
         Long memberId = LoginUserUtils.getAppLoginUser().getId();
-
         PlatformCnyUsdtExchangeEntity cnyUsdtExchange = cnyUsdtExchangeDao.getCNYAndUSDTOne();
         BigDecimal cnyUsdt = cnyUsdtExchange.getValue();
-
-        String walletCode = MemberWalletCoinEnum.WALLETCOINCODE.getValue();
-        MemberWalletContractEntity walletContract = memberWalletContractDao.findWalletContractByMemberIdAndSymbol(memberId, walletCode);
-        if (ObjectUtil.isEmpty(walletContract)) {
-            return Result.fail(MessageSourceUtils.getString("member_service_0001"));
-        }
-
-        MemberWalletContractInfoVo memberWalletContractInfoVo = new MemberWalletContractInfoVo();
-        memberWalletContractInfoVo.setFrozenBalance(walletContract.getFrozenBalance().setScale(4, BigDecimal.ROUND_DOWN));
-        memberWalletContractInfoVo.setAvailableBalance(walletContract.getAvailableBalance().setScale(4, BigDecimal.ROUND_DOWN));
-        memberWalletContractInfoVo.setTotalBalance(walletContract.getTotalBalance().setScale(4, BigDecimal.ROUND_DOWN));
-        memberWalletContractInfoVo.setTotalRMBBalance(walletContract.getTotalBalance().multiply(cnyUsdt).setScale(4, BigDecimal.ROUND_DOWN));
-
-        return Result.ok(memberWalletContractInfoVo);
+        Map<String, Object> columnMap = new HashMap<String, Object>();
+        columnMap.put("member_id", memberId);
+        
+        Map<String, Object> map = new HashMap<String, Object>();
+		List<MemberWalletContractEntity> selectByMap = memberWalletContractDao.selectByMap(columnMap);
+		BigDecimal totalCoin = BigDecimal.ZERO;
+		
+		List<MemberWalletContractInfoVo> list = new ArrayList<>();
+		if(CollUtil.isNotEmpty(selectByMap)) {
+			for(MemberWalletContractEntity memberWalletContractEntity : selectByMap) {
+				MemberWalletContractInfoVo memberWalletContractInfoVo = new MemberWalletContractInfoVo();
+				memberWalletContractInfoVo.setFrozenBalance(memberWalletContractEntity.getFrozenBalance().setScale(4, BigDecimal.ROUND_DOWN));
+				memberWalletContractInfoVo.setAvailableBalance(memberWalletContractEntity.getAvailableBalance().setScale(4, BigDecimal.ROUND_DOWN));
+				memberWalletContractInfoVo.setTotalBalance(memberWalletContractEntity.getTotalBalance().setScale(4, BigDecimal.ROUND_DOWN));
+				memberWalletContractInfoVo.setTotalRMBBalance(memberWalletContractEntity.getAvailableBalance().multiply(cnyUsdt).setScale(4, BigDecimal.ROUND_DOWN));
+				memberWalletContractInfoVo.setWalletCode(memberWalletContractEntity.getWalletCode()+"/USDT");
+				list.add(memberWalletContractInfoVo);
+				totalCoin = totalCoin.add(memberWalletContractEntity.getTotalBalance());
+			}
+		}
+		
+		map.put("memberWalletContractInfoVo", list);
+		map.put("totalCoin", totalCoin.setScale(4, BigDecimal.ROUND_DOWN));
+		map.put("availableCoin", totalCoin.multiply(cnyUsdt).setScale(4, BigDecimal.ROUND_DOWN));
+        return Result.ok(map);
     }
 
     @Override
@@ -390,11 +414,44 @@
     	}
     	//获取用户ID
     	Long memberId = LoginUserUtils.getAppLoginUser().getId();
+    	MemberEntity memberEntity = LoginUserUtils.getAppLoginUser();
+    	
+    	List<ContractHoldOrderEntity> list = contractHoldOrderDao.selectHoldOrderListByMemberIdAndSymbol(memberEntity.getId(), symbol+"/USDT", 1);
+    	BigDecimal totalProfitOrLoss = BigDecimal.ZERO;
+        if (CollUtil.isNotEmpty(list)) {
+            for (ContractHoldOrderEntity holdOrderEntity : list) {
+                // 获取最新价
+                BigDecimal newPrice = new BigDecimal(redisUtils.getString(CoinTypeConvert.convertToKey(holdOrderEntity.getSymbol())));
+                BigDecimal lotNumber = cacheSettingUtils.getSymbolSku(holdOrderEntity.getSymbol());
+                // 盈亏
+                BigDecimal rewardRatio = BigDecimal.ZERO;
+                // 开多
+                if (ContractHoldOrderEntity.OPENING_TYPE_MORE == holdOrderEntity.getOpeningType()) {
+                    // (最新价-开仓价)*规格*张数
+                    rewardRatio = newPrice.subtract(holdOrderEntity.getOpeningPrice()).multiply(lotNumber).multiply(new BigDecimal(holdOrderEntity.getSymbolCntSale()));
+                    // 开空
+                } else {
+                    // (开仓价-最新价)*规格*张数
+                    rewardRatio = holdOrderEntity.getOpeningPrice().subtract(newPrice).multiply(lotNumber).multiply(new BigDecimal(holdOrderEntity.getSymbolCntSale()));
+                }
+
+                if (memberEntity.getIsProfit() == MemberEntity.IS_PROFIT_Y) {
+                    PlatformTradeSettingEntity tradeSettingEntity = cacheSettingUtils.getTradeSetting();
+                    if (rewardRatio.compareTo(BigDecimal.ZERO) > -1) {
+                        rewardRatio = rewardRatio.multiply(BigDecimal.ONE.subtract(tradeSettingEntity.getProfitParam()));
+                    } 
+                }
+                totalProfitOrLoss = totalProfitOrLoss.add(rewardRatio);
+            }
+        }
     	
     	String walletCode = MemberWalletCoinEnum.WALLETCOINCODE.getValue();
     	MemberWalletContractEntity walletContract = memberWalletContractDao.findWalletContractByMemberIdAndSymbol(memberId, symbol);
     	BigDecimal availableBalance = walletContract.getAvailableBalance();
     	// 扣币
+    	if (totalProfitOrLoss.compareTo(BigDecimal.ZERO) < 0) {
+    		availableBalance = availableBalance.add(totalProfitOrLoss);
+    	}
     	BigDecimal availableSubtract = availableBalance.subtract(balance);
     	if (availableSubtract.compareTo(BigDecimal.ZERO) < 0) {
     		return Result.fail(MessageSourceUtils.getString("member_service_0007"));
@@ -423,7 +480,6 @@
     	}
     	
     	//更新合约全仓模式下的订单权益
-        MemberEntity memberEntity = memberDao.selectById(memberId);
         String symbols = symbol+"/USDT";
         ThreadPoolUtils.sendWholeForceClosingPrice(symbols, memberEntity);
     	
@@ -438,7 +494,7 @@
     	memberAccountMoneyChangeDao.insert(memberAccountRecord);
     	
     	//添加资金划转历史记录
-    	memberAccountRecord.setContent("由合约"+symbol+"账户转入");
+    	memberAccountRecord.setContent("合约"+symbol+"账户转入");
     	memberAccountRecord.setSymbol(walletCode);
     	memberAccountRecord.setType(MemberAccountMoneyChange.TYPE_WALLET_COIN);
     	memberAccountRecord.setAmount(balance);
@@ -447,13 +503,46 @@
     }
 
     @Override
-    public Result findWalletContractBySymbol() {
+    public Result findWalletContractBySymbol(String symbol) {
         //获取用户ID
         Long memberId = LoginUserUtils.getAppLoginUser().getId();
-        String walletCode = MemberWalletCoinEnum.WALLETCOINCODE.getValue();
-        MemberWalletContractEntity walletContract = memberWalletContractDao.findWalletContractByMemberIdAndSymbol(memberId, walletCode);
-        BigDecimal availableBalance = walletContract.getAvailableBalance().setScale(4, BigDecimal.ROUND_DOWN);
-        return Result.ok(availableBalance);
+        MemberWalletContractEntity walletContract = memberWalletContractDao.findWalletContractByMemberIdAndSymbol(memberId, symbol);
+        
+        MemberEntity memberEntity = memberDao.selectById(memberId);
+        List<ContractHoldOrderEntity> list = contractHoldOrderDao.selectHoldOrderListByMemberIdAndSymbol(memberEntity.getId(), symbol+"/USDT", 1);
+    	BigDecimal totalProfitOrLoss = BigDecimal.ZERO;
+        if (CollUtil.isNotEmpty(list)) {
+            for (ContractHoldOrderEntity holdOrderEntity : list) {
+                // 获取最新价
+                BigDecimal newPrice = new BigDecimal(redisUtils.getString(CoinTypeConvert.convertToKey(holdOrderEntity.getSymbol())));
+                BigDecimal lotNumber = cacheSettingUtils.getSymbolSku(holdOrderEntity.getSymbol());
+                // 盈亏
+                BigDecimal rewardRatio = BigDecimal.ZERO;
+                // 开多
+                if (ContractHoldOrderEntity.OPENING_TYPE_MORE == holdOrderEntity.getOpeningType()) {
+                    // (最新价-开仓价)*规格*张数
+                    rewardRatio = newPrice.subtract(holdOrderEntity.getOpeningPrice()).multiply(lotNumber).multiply(new BigDecimal(holdOrderEntity.getSymbolCntSale()));
+                    // 开空
+                } else {
+                    // (开仓价-最新价)*规格*张数
+                    rewardRatio = holdOrderEntity.getOpeningPrice().subtract(newPrice).multiply(lotNumber).multiply(new BigDecimal(holdOrderEntity.getSymbolCntSale()));
+                }
+
+                if (memberEntity.getIsProfit() == MemberEntity.IS_PROFIT_Y) {
+                    PlatformTradeSettingEntity tradeSettingEntity = cacheSettingUtils.getTradeSetting();
+                    if (rewardRatio.compareTo(BigDecimal.ZERO) > -1) {
+                        rewardRatio = rewardRatio.multiply(BigDecimal.ONE.subtract(tradeSettingEntity.getProfitParam()));
+                    } 
+                }
+                totalProfitOrLoss = totalProfitOrLoss.add(rewardRatio);
+            }
+        }
+        BigDecimal availableBalance = walletContract.getAvailableBalance();
+        if(totalProfitOrLoss.compareTo(BigDecimal.ZERO) < 0) {
+        	availableBalance = availableBalance.add(totalProfitOrLoss);
+        }
+        
+        return Result.ok(availableBalance.setScale(4, BigDecimal.ROUND_DOWN));
     }
 
     @Override
@@ -876,7 +965,7 @@
         memberAccountMoneyChangeDao.insert(memberAccountRecord);
 
         //添加合约资金划转历史记录
-        memberAccountRecord.setContent("由合约"+symbolOut+"账户转入至合约"+symbolIn+"账户");
+        memberAccountRecord.setContent("合约"+symbolOut+"账户转入至合约"+symbolIn+"账户");
         memberAccountRecord.setAmount(balance);
         memberAccountMoneyChangeDao.insert(memberAccountRecord);
         

--
Gitblit v1.9.1