From 2bb8c9ae6db2668213d03d02cd8b38e6bc7aae83 Mon Sep 17 00:00:00 2001
From: Helius <wangdoubleone@gmail.com>
Date: Fri, 21 Aug 2020 11:22:33 +0800
Subject: [PATCH] Merge branch 'whole' of https://gitee.com/chonggaoxiao/new_excoin into whole

---
 src/main/java/com/xcong/excoin/modules/coin/service/impl/CoinServiceImpl.java |  227 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 225 insertions(+), 2 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 e7ddaae..23915c1 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,7 +4,6 @@
 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;
@@ -24,15 +23,16 @@
 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;
 import com.xcong.excoin.modules.coin.parameter.vo.MemberWalletCoinInfoVo;
 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.member.dao.MemberDao;
 import com.xcong.excoin.modules.member.dao.MemberWalletAgentDao;
 import com.xcong.excoin.modules.member.dao.MemberWalletCoinDao;
 import com.xcong.excoin.modules.member.dao.MemberWalletContractDao;
+import com.xcong.excoin.modules.member.entity.MemberEntity;
 import com.xcong.excoin.modules.member.entity.MemberWalletAgentEntity;
 import com.xcong.excoin.modules.member.entity.MemberWalletCoinEntity;
 import com.xcong.excoin.modules.member.entity.MemberWalletContractEntity;
@@ -40,6 +40,7 @@
 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;
@@ -60,6 +61,8 @@
     MemberAccountMoneyChangeDao memberAccountMoneyChangeDao;
     @Resource
     MemberWalletAgentDao memberWalletAgentDao;
+    @Resource
+    MemberDao memberDao;
     @Resource
     RedisUtils redisUtils;
 
@@ -253,6 +256,71 @@
         }
         return Result.ok(MessageSourceUtils.getString("member_service_0006"));
     }
+    
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public Result coinWalletTransferToContracts(BigDecimal balance, String symbol) {
+        if (balance.compareTo(BigDecimal.ZERO) <= 0) {
+            return Result.fail(MessageSourceUtils.getString("member_service_0004"));
+        }
+        //获取用户ID
+        Long memberId = LoginUserUtils.getAppLoginUser().getId();
+
+        if (!StrUtil.isEmpty(memberId.toString())) {
+        	//获取对应的币种
+            String walletCode = MemberWalletCoinEnum.WALLETCOINCODE.getValue();
+            MemberWalletCoinEntity walletCoin = memberWalletCoinDao.selectWalletCoinBymIdAndCode(memberId, walletCode);
+            BigDecimal available = walletCoin.getAvailableBalance();
+            // 扣币
+            BigDecimal total = available.subtract(balance);
+            if (total.compareTo(BigDecimal.ZERO) < 0) {
+                return Result.fail(MessageSourceUtils.getString("member_service_0005"));
+            }
+            BigDecimal subtract = walletCoin.getTotalBalance().subtract(balance);
+            walletCoin.setAvailableBalance(total);
+            walletCoin.setTotalBalance(subtract);
+            int updateWalletCoinById = memberWalletCoinDao.updateById(walletCoin);
+            if (updateWalletCoinById < 1) {
+                return Result.fail(MessageSourceUtils.getString("member_service_0096"));
+            }
+            // 加币
+            // 查询合约账户
+            MemberWalletContractEntity walletContract = memberWalletContractDao.findWalletContractByMemberIdAndSymbol(memberId, symbol);
+            BigDecimal availableBalance = walletContract.getAvailableBalance();
+            BigDecimal add = availableBalance.add(balance);
+            walletContract.setAvailableBalance(add);
+            BigDecimal totalBalance = walletContract.getTotalBalance();
+            BigDecimal totalBigDecimal = totalBalance.add(balance);
+            walletContract.setTotalBalance(totalBigDecimal);
+            int updateWalletContractById = memberWalletContractDao.updateById(walletContract);
+            if (updateWalletContractById < 1) {
+                return Result.fail(MessageSourceUtils.getString("member_service_0096"));
+            }
+            
+            //更新合约全仓模式下的订单权益
+            MemberEntity memberEntity = memberDao.selectById(memberId);
+            String symbols = symbol+"/USDT";
+            ThreadPoolUtils.sendWholeForceClosingPrice(symbols, memberEntity);
+            
+            //添加币币资金划转历史记录
+            MemberAccountMoneyChange memberAccountRecord = new MemberAccountMoneyChange();
+            memberAccountRecord.setContent("转出至合约"+symbol+"账户");
+            memberAccountRecord.setMemberId(memberId);
+            memberAccountRecord.setAmount(balance.negate());
+            memberAccountRecord.setStatus(MemberAccountMoneyChange.STATUS_SUCCESS_INTEGER);
+            memberAccountRecord.setSymbol(MemberWalletCoinEnum.WALLETCOINCODE.getValue());
+            memberAccountRecord.setType(MemberAccountMoneyChange.TYPE_WALLET_COIN);
+            memberAccountMoneyChangeDao.insert(memberAccountRecord);
+
+            //添加合约资金划转历史记录
+            memberAccountRecord.setContent("由币币账户转入至合约"+symbol+"账户");
+            memberAccountRecord.setSymbol(MemberWalletCoinEnum.WALLETCOINCODE.getValue());
+            memberAccountRecord.setAmount(balance);
+            memberAccountRecord.setType(MemberAccountMoneyChange.TYPE_WALLET_CONTRACT);
+            memberAccountMoneyChangeDao.insert(memberAccountRecord);
+        }
+        return Result.ok(MessageSourceUtils.getString("member_service_0006"));
+    }
 
     @Override
     @Transactional(rollbackFor = Exception.class)
@@ -311,6 +379,70 @@
         memberAccountRecord.setAmount(balance);
         memberAccountMoneyChangeDao.insert(memberAccountRecord);
         return Result.ok(MessageSourceUtils.getString("member_service_0006"));
+    }
+    
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public Result contractTransferToWalletCoins(BigDecimal balance, String symbol) {
+    	if (balance.compareTo(BigDecimal.ZERO) <= 0) {
+    		return Result.fail(MessageSourceUtils.getString("member_service_0004"));
+    	}
+    	//获取用户ID
+    	Long memberId = LoginUserUtils.getAppLoginUser().getId();
+    	
+    	String walletCode = MemberWalletCoinEnum.WALLETCOINCODE.getValue();
+    	MemberWalletContractEntity walletContract = memberWalletContractDao.findWalletContractByMemberIdAndSymbol(memberId, symbol);
+    	BigDecimal availableBalance = walletContract.getAvailableBalance();
+    	// 扣币
+    	BigDecimal availableSubtract = availableBalance.subtract(balance);
+    	if (availableSubtract.compareTo(BigDecimal.ZERO) < 0) {
+    		return Result.fail(MessageSourceUtils.getString("member_service_0007"));
+    	}
+    	BigDecimal totalBalance = walletContract.getTotalBalance();
+    	BigDecimal totalSubtract = totalBalance.subtract(balance);
+    	
+    	walletContract.setAvailableBalance(availableSubtract);
+    	walletContract.setTotalBalance(totalSubtract);
+    	int updateWalletCoinById = memberWalletContractDao.updateById(walletContract);
+    	if (updateWalletCoinById < 1) {
+    		return Result.fail(MessageSourceUtils.getString("member_service_0096"));
+    	}
+    	// 加币
+    	MemberWalletCoinEntity walletCoin = memberWalletCoinDao.selectWalletCoinBymIdAndCode(memberId, walletCode);
+    	BigDecimal walletCoinAvailableBalance = walletCoin.getAvailableBalance();
+    	BigDecimal CoinAvailableBalance = walletCoinAvailableBalance.add(balance);
+    	BigDecimal walletCoinTotalBalance = walletCoin.getTotalBalance();
+    	BigDecimal CoinTotalBalance = walletCoinTotalBalance.add(balance);
+    	
+    	walletCoin.setAvailableBalance(CoinAvailableBalance);
+    	walletCoin.setTotalBalance(CoinTotalBalance);
+    	int updateById = memberWalletCoinDao.updateById(walletCoin);
+    	if (updateById < 1) {
+    		return Result.fail(MessageSourceUtils.getString("member_service_0096"));
+    	}
+    	
+    	//更新合约全仓模式下的订单权益
+        MemberEntity memberEntity = memberDao.selectById(memberId);
+        String symbols = symbol+"/USDT";
+        ThreadPoolUtils.sendWholeForceClosingPrice(symbols, memberEntity);
+    	
+    	//添加资金划转历史记录
+    	MemberAccountMoneyChange memberAccountRecord = new MemberAccountMoneyChange();
+    	memberAccountRecord.setContent("合约"+symbol+"账户转出至币币账户");
+    	memberAccountRecord.setMemberId(memberId);
+    	memberAccountRecord.setAmount(balance.negate());
+    	memberAccountRecord.setStatus(MemberAccountMoneyChange.STATUS_SUCCESS_INTEGER);
+    	memberAccountRecord.setSymbol(walletCode);
+    	memberAccountRecord.setType(MemberAccountMoneyChange.TYPE_WALLET_CONTRACT);
+    	memberAccountMoneyChangeDao.insert(memberAccountRecord);
+    	
+    	//添加资金划转历史记录
+    	memberAccountRecord.setContent("由合约"+symbol+"账户转入");
+    	memberAccountRecord.setSymbol(walletCode);
+    	memberAccountRecord.setType(MemberAccountMoneyChange.TYPE_WALLET_COIN);
+    	memberAccountRecord.setAmount(balance);
+    	memberAccountMoneyChangeDao.insert(memberAccountRecord);
+    	return Result.ok(MessageSourceUtils.getString("member_service_0006"));
     }
 
     @Override
@@ -460,6 +592,97 @@
 
         return Result.ok(MessageSourceUtils.getString("member_service_0006"));
     }
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public Result agentTransferToWalletCoins(BigDecimal balance, Integer transfertype, String symbol) {
+    	if (balance.compareTo(BigDecimal.ZERO) <= 0) {
+    		return Result.fail(MessageSourceUtils.getString("member_service_0004"));
+    	}
+    	//获取用户ID
+    	Long memberId = LoginUserUtils.getAppLoginUser().getId();
+    	
+    	// 扣币
+    	String walletCode = MemberWalletCoinEnum.WALLETCOINCODE.getValue();
+    	MemberWalletAgentEntity walletAgent = memberWalletAgentDao.selectWalletAgentBymIdAndCode(memberId, walletCode);
+    	BigDecimal availableBalance = walletAgent.getAvailableBalance();
+    	BigDecimal totalBalance = walletAgent.getTotalBalance();
+    	
+    	BigDecimal available = availableBalance.subtract(balance);
+    	if (available.compareTo(BigDecimal.ZERO) < 0) {
+    		return Result.fail(MessageSourceUtils.getString("member_service_0008"));
+    	}
+    	BigDecimal total = totalBalance.subtract(balance);
+    	if (total.compareTo(BigDecimal.ZERO) < 0) {
+    		return Result.fail(MessageSourceUtils.getString("member_service_0008"));
+    	}
+    	
+    	walletAgent.setAvailableBalance(available);
+    	walletAgent.setTotalBalance(total);
+    	
+    	int i = memberWalletAgentDao.updateById(walletAgent);
+    	if (i < 1) {
+    		return Result.fail(MessageSourceUtils.getString("member_service_0095"));
+    	}
+    	//添加资金划转历史记录
+    	MemberAccountMoneyChange memberAccountRecord = new MemberAccountMoneyChange();
+    	//代理账户转币币
+    	if (MemberAccountMoneyChange.TYPE_WALLET_COIN.equals(transfertype)) {
+    		MemberWalletCoinEntity walletCoin = memberWalletCoinDao.selectWalletCoinBymIdAndCode(memberId, walletCode);
+    		BigDecimal walletCoinAvailableBalance = walletCoin.getAvailableBalance();
+    		BigDecimal walletCoinTotalBalance = walletCoin.getTotalBalance();
+    		
+    		walletCoin.setAvailableBalance(walletCoinAvailableBalance.add(balance));
+    		walletCoin.setTotalBalance(walletCoinTotalBalance.add(balance));
+    		
+    		int updateById = memberWalletCoinDao.updateById(walletCoin);
+    		if (updateById < 1) {
+    			return Result.fail(MessageSourceUtils.getString("member_service_0095"));
+    		}
+    		//添加资金划转历史记录
+    		memberAccountRecord.setMemberId(memberId);
+    		memberAccountRecord.setStatus(MemberAccountMoneyChange.STATUS_SUCCESS_INTEGER);
+    		memberAccountRecord.setSymbol(walletCode);
+    		memberAccountRecord.setContent(MemberWalletCoinEnum.CONTENTFROMAGENT.getValue());
+    		memberAccountRecord.setType(MemberAccountMoneyChange.TYPE_WALLET_COIN);
+    		memberAccountRecord.setAmount(balance);
+    		memberAccountMoneyChangeDao.insert(memberAccountRecord);
+    		memberAccountRecord.setContent(MemberWalletCoinEnum.CONTENTTOWALLETCOIN.getValue());
+    		
+    	} else if (MemberAccountMoneyChange.TYPE_WALLET_CONTRACT.equals(transfertype)) {
+    		//代理账户转合约
+    		MemberWalletContractEntity walletContract = memberWalletContractDao.findWalletContractByMemberIdAndSymbol(memberId, symbol);
+    		BigDecimal walletContractAvailableBalance = walletContract.getAvailableBalance();
+    		BigDecimal walletContractTotalBalance = walletContract.getTotalBalance();
+    		
+    		walletContract.setAvailableBalance(walletContractAvailableBalance.add(balance));
+    		walletContract.setTotalBalance(walletContractTotalBalance.add(balance));
+    		
+    		int updateById = memberWalletContractDao.updateById(walletContract);
+    		if (updateById < 1) {
+    			return Result.fail(MessageSourceUtils.getString("member_service_0095"));
+    		}
+    		
+    		//更新合约全仓模式下的订单权益
+            MemberEntity memberEntity = memberDao.selectById(memberId);
+            String symbols = symbol+"/USDT";
+            ThreadPoolUtils.sendWholeForceClosingPrice(symbols, memberEntity);
+    		
+    		//添加资金划转历史记录
+    		memberAccountRecord.setMemberId(memberId);
+    		memberAccountRecord.setStatus(MemberAccountMoneyChange.STATUS_SUCCESS_INTEGER);
+    		memberAccountRecord.setSymbol(walletCode);
+    		memberAccountRecord.setContent("由代理账户转入合约"+symbol+"账户");
+    		memberAccountRecord.setType(MemberAccountMoneyChange.TYPE_WALLET_CONTRACT);
+    		memberAccountRecord.setAmount(balance);
+    		memberAccountMoneyChangeDao.insert(memberAccountRecord);
+    		memberAccountRecord.setContent(MemberWalletCoinEnum.CONTENTTOCONTRACT.getValue());
+    	}
+    	memberAccountRecord.setAmount(balance.negate());
+    	memberAccountRecord.setType(MemberAccountMoneyChange.TYPE_WALLET_AGENT);
+    	memberAccountMoneyChangeDao.insert(memberAccountRecord);
+    	
+    	return Result.ok(MessageSourceUtils.getString("member_service_0006"));
+    }
 
     @Override
     public Result findWalletAgentBySymbol() {

--
Gitblit v1.9.1