From cff2dfcd7e820147af9f8c3e45b0cdfde4801b64 Mon Sep 17 00:00:00 2001
From: Helius <wangdoubleone@gmail.com>
Date: Fri, 05 Nov 2021 15:39:04 +0800
Subject: [PATCH] fix

---
 src/main/java/com/xcong/excoin/modules/coin/service/impl/CoinServiceImpl.java |  156 ++++++++++++++++++++++++++++++++++++++-------------
 1 files changed, 115 insertions(+), 41 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 5516712..61d617f 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
@@ -4,6 +4,7 @@
 import java.util.ArrayList;
 import java.util.List;
 import javax.annotation.Resource;
+import javax.validation.Valid;
 
 import com.xcong.excoin.modules.platform.entity.PlatformCnyUsdtExchangeEntity;
 import org.springframework.stereotype.Service;
@@ -21,6 +22,7 @@
 import com.xcong.excoin.modules.coin.entity.OrderCoinsDealEntity;
 import com.xcong.excoin.modules.coin.mapper.MemberAccountMoneyChangeMapper;
 import com.xcong.excoin.modules.coin.parameter.dto.RecordsPageDto;
+import com.xcong.excoin.modules.coin.parameter.vo.AllWalletCoinVo;
 import com.xcong.excoin.modules.coin.parameter.vo.MemberAccountMoneyChangeInfoVo;
 import com.xcong.excoin.modules.coin.parameter.vo.MemberAgentIntoInfoVo;
 import com.xcong.excoin.modules.coin.parameter.vo.MemberWalletAgentInfoVo;
@@ -139,7 +141,6 @@
             memberWalletCoinVo.setTotalUsdt(totalUsdts.setScale(4, BigDecimal.ROUND_DOWN));
             memberWalletCoinVo.setTotalCny(totalUsdts.multiply(cnyUsdt).setScale(4, BigDecimal.ROUND_DOWN));
             memberWalletCoinVo.setMemberWalletCoinInfoVo(memberWalletCoinlist);
-            ;
             return Result.ok(memberWalletCoinVo);
         }
     }
@@ -185,26 +186,12 @@
         if (ObjectUtil.isEmpty(walletContract)) {
             return Result.fail(MessageSourceUtils.getString("member_service_0001"));
         }
-        /**
-         * todo
-         */
-        //获取当前的合约持仓
-        //获取实时盈亏
-        BigDecimal profitAndLoss = new BigDecimal("0");
-        //获取总付款
-        BigDecimal totalPayment = new BigDecimal("0");
-        BigDecimal lastTotalBalance = new BigDecimal("0");
-        BigDecimal totalBalance = walletContract.getTotalBalance();
-        lastTotalBalance = totalBalance.add(profitAndLoss);
-        if (lastTotalBalance.compareTo(BigDecimal.ZERO) < 0) {
-            lastTotalBalance = new BigDecimal("0");
-        }
 
         MemberWalletContractInfoVo memberWalletContractInfoVo = new MemberWalletContractInfoVo();
-        memberWalletContractInfoVo.setFrozenBalance(totalPayment.setScale(4, BigDecimal.ROUND_DOWN));
-        memberWalletContractInfoVo.setAvailableBalance(walletContract.getAvailableBalance());
-        memberWalletContractInfoVo.setTotalBalance(lastTotalBalance.setScale(4, BigDecimal.ROUND_DOWN));
-        memberWalletContractInfoVo.setTotalRMBBalance(lastTotalBalance.multiply(cnyUsdt).setScale(4, BigDecimal.ROUND_DOWN));
+        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);
     }
@@ -228,9 +215,6 @@
                 return Result.fail(MessageSourceUtils.getString("member_service_0005"));
             }
             BigDecimal subtract = walletCoin.getTotalBalance().subtract(balance);
-            if (subtract.compareTo(BigDecimal.ZERO) < 0) {
-                return Result.fail(MessageSourceUtils.getString("member_service_0005"));
-            }
             walletCoin.setAvailableBalance(total);
             walletCoin.setTotalBalance(subtract);
             int updateWalletCoinById = memberWalletCoinDao.updateById(walletCoin);
@@ -497,29 +481,119 @@
     }
 
 	@Override
-	public Result getWalletAgentIntoRecords() {
+	public Result getWalletAgentIntoRecords(RecordsPageDto recordsPageDto) {
 	        //获取用户ID
 	        Long memberId = LoginUserUtils.getAppLoginUser().getId();
-
-	        List<MemberAccountMoneyChange> contractRecordList =
-	                memberAccountMoneyChangeDao.selectWalletAgentIntoRecordsByMemIdTypeSymbol(memberId);
-
-	        MemberAgentIntoInfoVo memberAgentIntoInfoVo = new MemberAgentIntoInfoVo();
+	        
+	        Page<OrderCoinsDealEntity> page = new Page<>(recordsPageDto.getPageNum(), recordsPageDto.getPageSize());
+	        MemberAccountMoneyChange memberAccountMoneyChange = new MemberAccountMoneyChange();
+	        memberAccountMoneyChange.setMemberId(memberId);
+	        IPage<MemberAccountMoneyChange> list = memberAccountMoneyChangeDao.selectWalletAgentIntoRecordsByMemIdTypeSymbol(page, memberAccountMoneyChange);
+	        List<MemberAccountMoneyChange> contractRecordList = list.getRecords();
 	        List<MemberAccountMoneyChangeInfoVo> arrayList = new ArrayList<>();
-	        if (ObjectUtil.isNotNull(contractRecordList)) {
-	            for (MemberAccountMoneyChange memberAccountMoneyChange : contractRecordList) {
-	                MemberAccountMoneyChangeInfoVo memberAccountMoneyChangeInfoVo = new MemberAccountMoneyChangeInfoVo();
-	                memberAccountMoneyChangeInfoVo.setAmount(memberAccountMoneyChange.getAmount());
-	                memberAccountMoneyChangeInfoVo.setContent(memberAccountMoneyChange.getContent());
-	                memberAccountMoneyChangeInfoVo.setStatus(memberAccountMoneyChange.getStatus());
-	                memberAccountMoneyChangeInfoVo.setSymbol(memberAccountMoneyChange.getSymbol());
-	                memberAccountMoneyChangeInfoVo.setType(memberAccountMoneyChange.getType());
-	                memberAccountMoneyChangeInfoVo.setUpdateTime(memberAccountMoneyChange.getUpdateTime());
-	                arrayList.add(memberAccountMoneyChangeInfoVo);
-	            }
+	        if(CollUtil.isNotEmpty(contractRecordList)) {
+	        	if (ObjectUtil.isNotNull(contractRecordList)) {
+	        		for (MemberAccountMoneyChange memberAccountMoneyChanges : contractRecordList) {
+	        			MemberAccountMoneyChangeInfoVo memberAccountMoneyChangeInfoVo = new MemberAccountMoneyChangeInfoVo();
+	        			memberAccountMoneyChangeInfoVo.setAmount(memberAccountMoneyChanges.getAmount());
+	        			memberAccountMoneyChangeInfoVo.setContent(memberAccountMoneyChanges.getContent());
+	        			memberAccountMoneyChangeInfoVo.setStatus(memberAccountMoneyChanges.getStatus());
+	        			memberAccountMoneyChangeInfoVo.setSymbol(memberAccountMoneyChanges.getSymbol());
+	        			memberAccountMoneyChangeInfoVo.setType(memberAccountMoneyChanges.getType());
+	        			memberAccountMoneyChangeInfoVo.setUpdateTime(memberAccountMoneyChanges.getUpdateTime());
+	        			arrayList.add(memberAccountMoneyChangeInfoVo);
+	        		}
+	        	}
 	        }
-	        memberAgentIntoInfoVo.setMemberAccountMoneyChangeInfoVo(arrayList);
-	        return Result.ok(memberAgentIntoInfoVo);
+	        Page<MemberAccountMoneyChangeInfoVo> pageEntityToPageVo = new Page<>();
+	        pageEntityToPageVo.setRecords(arrayList);
+	        
+	        return Result.ok(pageEntityToPageVo);
 	}
 
+	@Override
+	public Result getAllWalletCoin() {
+		//获取【币币】
+        Long memberId = LoginUserUtils.getAppLoginUser().getId();
+        PlatformCnyUsdtExchangeEntity cnyUsdtExchange = cnyUsdtExchangeDao.getCNYAndUSDTOne();
+        BigDecimal cnyUsdt = cnyUsdtExchange.getValue();
+        AllWalletCoinVo allWalletCoinVo = new AllWalletCoinVo();
+
+        BigDecimal totalUsdts = BigDecimal.ZERO;
+        if (!StrUtil.isEmpty(memberId.toString())) {
+
+            List<MemberWalletCoinEntity> memberWalletCoinlist = memberWalletCoinDao.selectMemberWalletCoinsByMemberId(memberId);
+            List<MemberWalletCoinInfoVo> memberWalletCoinInfoVolist = new ArrayList<MemberWalletCoinInfoVo>();
+
+            if (CollUtil.isNotEmpty(memberWalletCoinlist)) {
+                for (MemberWalletCoinEntity memberWalletCoinEntity : memberWalletCoinlist) {
+                    MemberWalletCoinInfoVo memberWalletCoinInfoVo = new MemberWalletCoinInfoVo();
+                    memberWalletCoinInfoVo.setAvailableBalance(memberWalletCoinEntity.getAvailableBalance().setScale(4, BigDecimal.ROUND_DOWN));
+                    memberWalletCoinInfoVo.setFrozenBalance(memberWalletCoinEntity.getFrozenBalance().setScale(4, BigDecimal.ROUND_DOWN));
+                    memberWalletCoinInfoVo.setMemberId(memberWalletCoinEntity.getMemberId());
+                    memberWalletCoinInfoVo.setTotalBalance(memberWalletCoinEntity.getTotalBalance().setScale(4, BigDecimal.ROUND_DOWN));
+                    memberWalletCoinInfoVo.setWalletCode(memberWalletCoinEntity.getWalletCode());
+                    memberWalletCoinInfoVolist.add(memberWalletCoinInfoVo);
+                }
+            }
+
+            if (CollUtil.isNotEmpty(memberWalletCoinInfoVolist)) {
+                for (MemberWalletCoinInfoVo walletCoin : memberWalletCoinInfoVolist) {
+                    if (MemberWalletCoinEnum.WALLETCOINCODE.getValue().equals(walletCoin.getWalletCode())) {
+                        BigDecimal totalUsdt = BigDecimal.ZERO;
+                        totalUsdt = walletCoin.getAvailableBalance().add(walletCoin.getFrozenBalance());
+                        totalUsdts = totalUsdts.add(totalUsdt);
+                    } else {
+                        BigDecimal amount = walletCoin.getAvailableBalance().add(walletCoin.getFrozenBalance());
+                        // 获取最新价
+                        BigDecimal closePrice = new BigDecimal(redisUtils.getString(CoinTypeConvert.convertToKey(walletCoin.getWalletCode()+"/USDT")));
+                        BigDecimal totalUsdt = BigDecimal.ZERO;
+                        totalUsdt = totalUsdt.add(amount.multiply(closePrice));
+                        totalUsdts = totalUsdts.add(totalUsdt);
+                    }
+                }
+            }
+        } 
+        allWalletCoinVo.setWalletUsdt(totalUsdts.setScale(4, BigDecimal.ROUND_DOWN));
+		//获取【合约】
+        String walletCode = MemberWalletCoinEnum.WALLETCOINCODE.getValue();
+        MemberWalletContractEntity walletContract = memberWalletContractDao.findWalletContractByMemberIdAndSymbol(memberId, walletCode);
+        if (ObjectUtil.isEmpty(walletContract)) {
+            return Result.fail(MessageSourceUtils.getString("member_service_0001"));
+        }
+        allWalletCoinVo.setContractUsdt(walletContract.getTotalBalance().setScale(4, BigDecimal.ROUND_DOWN));
+        totalUsdts = totalUsdts.add(walletContract.getTotalBalance());
+		//获取【代理】
+        MemberWalletAgentEntity walletAgent = memberWalletAgentDao.selectWalletAgentBymIdAndCode(memberId, walletCode);
+        BigDecimal availableBalance = walletAgent.getAvailableBalance();
+        allWalletCoinVo.setAgentUsdt(availableBalance.setScale(4, BigDecimal.ROUND_DOWN));
+        totalUsdts = totalUsdts.add(availableBalance);
+        
+        allWalletCoinVo.setTotalUsdt(totalUsdts.setScale(4, BigDecimal.ROUND_DOWN));
+        allWalletCoinVo.setTotalCny(totalUsdts.multiply(cnyUsdt).setScale(4, BigDecimal.ROUND_DOWN));
+        return Result.ok(allWalletCoinVo);
+	}
+
+    @Override
+    public void updateWalletBalance(Long id, BigDecimal availableBalance,  BigDecimal totalBalance,BigDecimal frozenBalance) {
+        if(id==null){
+            return;
+        }
+        // 这里需要加锁 保证同一个时间只有一个线程操作一个钱包
+        String key = "UPDATE_WALLET_COIN_"+id;
+        while (true){
+            boolean b = redisUtils.setNotExist(key, 1, 2);
+            if(b){
+                //System.out.println("我拿到了锁");
+                // 拿到了锁才能扣
+                memberWalletCoinDao.updateWalletBalance(id,availableBalance,totalBalance,frozenBalance);
+                // 扣完释放锁
+                redisUtils.del(key);
+                break;
+            }else {
+
+            }
+        }
+    }
+
 }

--
Gitblit v1.9.1