From 2e2eeeb8291415706f4941e55270a0b23e76bf23 Mon Sep 17 00:00:00 2001 From: xiaoyong931011 <15274802129@163.com> Date: Fri, 04 Mar 2022 15:16:06 +0800 Subject: [PATCH] 20222223 --- src/main/java/com/xcong/excoin/modules/coin/service/impl/CoinServiceImpl.java | 1494 ++++++++++++++++++++++++++++++++++++++++------------------- 1 files changed, 1,019 insertions(+), 475 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 719b6a8..6b956fa 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 @@ -1,513 +1,1057 @@ package com.xcong.excoin.modules.coin.service.impl; import java.math.BigDecimal; -import java.util.ArrayList; -import java.util.List; +import java.text.SimpleDateFormat; +import java.util.*; import javax.annotation.Resource; +import javax.validation.Valid; +import cn.hutool.core.util.RandomUtil; +import cn.hutool.core.date.DateUtil; +import com.xcong.excoin.modules.coin.dao.ZhiYaDao; +import com.xcong.excoin.modules.coin.dao.ZhiYaRecordDao; +import com.xcong.excoin.modules.coin.dao.ZhiyaRewardDao; +import com.xcong.excoin.modules.coin.entity.*; +import com.xcong.excoin.modules.coin.parameter.dto.CoinInListDto; +import com.xcong.excoin.modules.coin.parameter.dto.ZhiyaRewardRecordsPageDto; +import com.xcong.excoin.modules.coin.parameter.vo.*; +import com.xcong.excoin.modules.member.dao.*; +import com.xcong.excoin.modules.member.entity.*; +import com.xcong.excoin.modules.platform.entity.PlatformCnyUsdtExchangeEntity; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; + +import com.baomidou.mybatisplus.core.metadata.IPage; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.xcong.excoin.common.LoginUserUtils; import com.xcong.excoin.common.enumerates.CoinTypeEnum; import com.xcong.excoin.common.enumerates.MemberWalletCoinEnum; import com.xcong.excoin.common.response.Result; import com.xcong.excoin.modules.coin.dao.MemberAccountMoneyChangeDao; -import com.xcong.excoin.modules.coin.entity.CnyUsdtExchange; -import com.xcong.excoin.modules.coin.entity.MemberAccountMoneyChange; -import com.xcong.excoin.modules.coin.parameter.vo.MemberAccountMoneyChangeInfoVo; -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.mapper.MemberAccountMoneyChangeMapper; +import com.xcong.excoin.modules.coin.parameter.dto.RecordsPageDto; import com.xcong.excoin.modules.coin.service.CoinService; -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.MemberWalletAgentEntity; -import com.xcong.excoin.modules.member.entity.MemberWalletCoinEntity; -import com.xcong.excoin.modules.member.entity.MemberWalletContractEntity; -import com.xcong.excoin.modules.platform.dao.CnyUsdtExchangeDao; +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 cn.hutool.core.collection.CollUtil; import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.StrUtil; @Service -public class CoinServiceImpl extends ServiceImpl<MemberWalletCoinDao, MemberWalletCoinEntity> implements CoinService{ +public class CoinServiceImpl extends ServiceImpl<MemberWalletCoinDao, MemberWalletCoinEntity> implements CoinService { - //@Resource - //SymbolsService symbolsService; - @Resource - CnyUsdtExchangeDao cnyUsdtExchangeDao; - @Resource - MemberWalletCoinDao memberWalletCoinDao; - @Resource - MemberWalletContractDao memberWalletContractDao; - @Resource - MemberAccountMoneyChangeDao memberAccountMoneyChangeDao; - @Resource - MemberWalletAgentDao memberWalletAgentDao; - - + //@Resource + //SymbolsService symbolsService; + @Resource + PlatformCnyUsdtExchangeDao cnyUsdtExchangeDao; + @Resource + MemberWalletCoinDao memberWalletCoinDao; + @Resource + MemberWalletContractDao memberWalletContractDao; + @Resource + MemberAccountMoneyChangeDao memberAccountMoneyChangeDao; + @Resource + MemberWalletAgentDao memberWalletAgentDao; + @Resource + ZhiYaRecordDao zhiYaRecordDao; + @Resource + ZhiyaRewardDao zhiyaRewardDao; + @Resource + MemberCoinChargeDao memberCoinChargeDao; + @Resource + MemberCoinWithdrawDao memberCoinWithdrawDao; + @Resource + ZhiYaDao zhiYaDao; + @Resource + RedisUtils redisUtils; + + + @Override + public Result getWalletCoin() { + //获取用户ID + Long memberId = LoginUserUtils.getAppLoginUser().getId(); + PlatformCnyUsdtExchangeEntity cnyUsdtExchange = cnyUsdtExchangeDao.getCNYAndUSDTOne(); + BigDecimal cnyUsdt = cnyUsdtExchange.getValue(); + + 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); + BigDecimal totalCny = totalUsdt.multiply(cnyUsdt); + walletCoin.setTotalBalance(totalCny); + } +// else if (MemberWalletCoinEnum.WALLETGUSDCOINCODE.getValue().equals(walletCoin.getWalletCode())) { +// BigDecimal totalUsdt = BigDecimal.ZERO; +// totalUsdt = walletCoin.getAvailableBalance().add(walletCoin.getFrozenBalance()); +// totalUsdts = totalUsdts.add(totalUsdt); +// BigDecimal totalCny = totalUsdt.multiply(cnyUsdt); +// walletCoin.setTotalBalance(totalCny); +// } + else { + BigDecimal amount = walletCoin.getAvailableBalance().add(walletCoin.getFrozenBalance()); + // 获取最新价 + BigDecimal closePrice = new BigDecimal(redisUtils.getString(CoinTypeConvert.convertToKey(walletCoin.getWalletCode()+"/USDT"))); + BigDecimal totalUsdt = BigDecimal.ZERO; + //Double closePrice = symbolsService.getCloseSymbolsBySymbolsName(walletCoin.getWalletCode()+"/USDT"); + totalUsdt = totalUsdt.add(amount.multiply(closePrice)); + totalUsdts = totalUsdts.add(totalUsdt); + walletCoin.setTotalBalance(totalUsdt.multiply(cnyUsdt)); + } + } + } + MemberWalletCoinVo memberWalletCoinVo = new MemberWalletCoinVo(); + memberWalletCoinVo.setTotalUsdt(totalUsdts.setScale(4, BigDecimal.ROUND_DOWN)); + memberWalletCoinVo.setTotalCny(totalUsdts.multiply(cnyUsdt).setScale(4, BigDecimal.ROUND_DOWN)); + memberWalletCoinVo.setMemberWalletCoinInfoVo(memberWalletCoinInfoVolist); + return Result.ok(memberWalletCoinVo); + } else { + List<MemberWalletCoinInfoVo> memberWalletCoinlist = new ArrayList<MemberWalletCoinInfoVo>(); + MemberWalletCoinInfoVo coin = new MemberWalletCoinInfoVo(); + coin.setAvailableBalance(BigDecimal.ZERO); + coin.setTotalBalance(BigDecimal.ZERO); + coin.setFrozenBalance(BigDecimal.ZERO); + coin.setWalletCode(CoinTypeEnum.BTC.toString()); + memberWalletCoinlist.add(coin); + coin.setWalletCode(CoinTypeEnum.ETH.toString()); + memberWalletCoinlist.add(coin); + coin.setWalletCode(CoinTypeEnum.LTC.toString()); + memberWalletCoinlist.add(coin); + coin.setWalletCode(CoinTypeEnum.BCH.toString()); + memberWalletCoinlist.add(coin); + coin.setWalletCode(CoinTypeEnum.USDT.toString()); + memberWalletCoinlist.add(coin); + coin.setWalletCode(CoinTypeEnum.EOS.toString()); + memberWalletCoinlist.add(coin); + coin.setWalletCode(CoinTypeEnum.XRP.toString()); + memberWalletCoinlist.add(coin); + coin.setWalletCode(CoinTypeEnum.ETC.toString()); + memberWalletCoinlist.add(coin); +// coin.setWalletCode(CoinTypeEnum.GUSD.toString()); +// memberWalletCoinlist.add(coin); + coin.setWalletCode(CoinTypeEnum.GRICE.toString()); + memberWalletCoinlist.add(coin); + + MemberWalletCoinVo memberWalletCoinVo = new MemberWalletCoinVo(); + 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); + } + } + + @Override + public Result getWalletCoinBySymbol(String symbol) { + //获取用户ID + Long memberId = LoginUserUtils.getAppLoginUser().getId(); + MemberWalletCoinEntity walletCoin = memberWalletCoinDao.selectWalletCoinBymIdAndCode(memberId, symbol); + MemberWalletCoinInfoVo memberWalletCoinInfoVo = new MemberWalletCoinInfoVo(); + memberWalletCoinInfoVo.setFrozenBalance(walletCoin.getFrozenBalance()); + memberWalletCoinInfoVo.setAvailableBalance(walletCoin.getAvailableBalance()); + memberWalletCoinInfoVo.setMemberId(memberId); + memberWalletCoinInfoVo.setWalletCode(symbol); + if (!StrUtil.isEmpty(memberId.toString())) { + PlatformCnyUsdtExchangeEntity cnyUsdtExchange = cnyUsdtExchangeDao.getCNYAndUSDTOne(); + BigDecimal cnyUsdt = cnyUsdtExchange.getValue(); + BigDecimal total = walletCoin.getAvailableBalance().add(walletCoin.getFrozenBalance()); + + if (MemberWalletCoinEnum.WALLETCOINCODE.getValue().equals(walletCoin.getWalletCode())) { + memberWalletCoinInfoVo.setTotalBalance(total.multiply(cnyUsdt).setScale(4, BigDecimal.ROUND_DOWN)); + + } else { + BigDecimal closePrice = new BigDecimal(redisUtils.getString(CoinTypeConvert.convertToKey(symbol+"/USDT"))); + //Double closePrice = symbolsService.getCloseSymbolsBySymbolsName(wallet.getCode()+"/USDT"); + memberWalletCoinInfoVo.setTotalBalance(total.multiply(closePrice).multiply(cnyUsdt).setScale(4, BigDecimal.ROUND_DOWN)); + } + } + return Result.ok(memberWalletCoinInfoVo); + } + + @Override + public Result getWalletContractById() { + + //获取用户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); + } + + @Override + @Transactional(rollbackFor = Exception.class) + public Result coinWalletTransferToContract(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, walletCode); + 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")); + } + //添加币币资金划转历史记录 + MemberAccountMoneyChange memberAccountRecord = new MemberAccountMoneyChange(); + memberAccountRecord.setContent(MemberWalletCoinEnum.CONTENTTOCONTRACT.getValue()); + 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(MemberWalletCoinEnum.CONTENTFROMWALLETCOIN.getValue()); + 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) + public Result contractTransferToWalletCoin(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, walletCode); + 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")); + } + + //添加资金划转历史记录 + MemberAccountMoneyChange memberAccountRecord = new MemberAccountMoneyChange(); + memberAccountRecord.setContent(MemberWalletCoinEnum.CONTENTTOWALLETCOIN.getValue()); + 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(MemberWalletCoinEnum.CONTENTFROMCONTRACT.getValue()); + memberAccountRecord.setSymbol(walletCode); + memberAccountRecord.setType(MemberAccountMoneyChange.TYPE_WALLET_COIN); + memberAccountRecord.setAmount(balance); + memberAccountMoneyChangeDao.insert(memberAccountRecord); + return Result.ok(MessageSourceUtils.getString("member_service_0006")); + } + + @Override + public Result findWalletContractBySymbol() { + //获取用户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); + } + + @Override + public Result findWalletCoinBySymbol(String symbol) { + //获取用户ID + Long memberId = LoginUserUtils.getAppLoginUser().getId(); + + MemberWalletCoinEntity walletCoin = memberWalletCoinDao.selectWalletCoinBymIdAndCode(memberId, symbol); + BigDecimal availableBalance = walletCoin.getAvailableBalance().setScale(4, BigDecimal.ROUND_DOWN); + return Result.ok(availableBalance); + } + + @Override + public Result getWalletCoinRecords(RecordsPageDto recordsPageDto) { + //获取用户ID + Long memberId = LoginUserUtils.getAppLoginUser().getId(); + + Page<OrderCoinsDealEntity> page = new Page<>(recordsPageDto.getPageNum(), recordsPageDto.getPageSize()); + MemberAccountMoneyChange memberAccountMoneyChange = new MemberAccountMoneyChange(); + memberAccountMoneyChange.setMemberId(memberId); + IPage<MemberAccountMoneyChange> list = memberAccountMoneyChangeDao.selectWalletCoinRecordsInPage(page, memberAccountMoneyChange); + Page<MemberAccountMoneyChangeInfoVo> pageEntityToPageVo = MemberAccountMoneyChangeMapper.INSTANCE.pageEntityToPageVo(list); + + return Result.ok(pageEntityToPageVo); + } + + @Override + public Result getWalletContractRecords(RecordsPageDto recordsPageDto) { + //获取用户ID + Long memberId = LoginUserUtils.getAppLoginUser().getId(); + + Page<OrderCoinsDealEntity> page = new Page<>(recordsPageDto.getPageNum(), recordsPageDto.getPageSize()); + MemberAccountMoneyChange memberAccountMoneyChange = new MemberAccountMoneyChange(); + memberAccountMoneyChange.setMemberId(memberId); + IPage<MemberAccountMoneyChange> list = memberAccountMoneyChangeDao.selectWalletContractRecordsInPage(page, memberAccountMoneyChange); + Page<MemberAccountMoneyChangeInfoVo> pageEntityToPageVo = MemberAccountMoneyChangeMapper.INSTANCE.pageEntityToPageVo(list); + return Result.ok(pageEntityToPageVo); + } + + @Override + public Result getWalletAgentRecords(RecordsPageDto recordsPageDto) { + //获取用户ID + Long memberId = LoginUserUtils.getAppLoginUser().getId(); + + Page<OrderCoinsDealEntity> page = new Page<>(recordsPageDto.getPageNum(), recordsPageDto.getPageSize()); + MemberAccountMoneyChange memberAccountMoneyChange = new MemberAccountMoneyChange(); + memberAccountMoneyChange.setMemberId(memberId); + IPage<MemberAccountMoneyChange> list = memberAccountMoneyChangeDao.selectWalletAgentRecordsInPage(page, memberAccountMoneyChange); + Page<MemberAccountMoneyChangeInfoVo> pageEntityToPageVo = MemberAccountMoneyChangeMapper.INSTANCE.pageEntityToPageVo(list); + + return Result.ok(pageEntityToPageVo); + } + + @Override + @Transactional(rollbackFor = Exception.class) + public Result agentTransferToWalletCoin(BigDecimal balance, Integer transfertype) { + 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, walletCode); + 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")); + } + + //添加资金划转历史记录 + memberAccountRecord.setMemberId(memberId); + memberAccountRecord.setStatus(MemberAccountMoneyChange.STATUS_SUCCESS_INTEGER); + memberAccountRecord.setSymbol(walletCode); + memberAccountRecord.setContent(MemberWalletCoinEnum.CONTENTFROMAGENT.getValue()); + 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() { + //获取用户ID + Long memberId = LoginUserUtils.getAppLoginUser().getId(); + String walletCode = MemberWalletCoinEnum.WALLETCOINCODE.getValue(); + + MemberWalletAgentEntity walletAgent = memberWalletAgentDao.selectWalletAgentBymIdAndCode(memberId, walletCode); + BigDecimal availableBalance = walletAgent.getAvailableBalance(); + + PlatformCnyUsdtExchangeEntity cnyUsdtExchange = cnyUsdtExchangeDao.getCNYAndUSDTOne(); + BigDecimal cnyUsdt = cnyUsdtExchange.getValue(); + BigDecimal multiply = availableBalance.multiply(cnyUsdt); + + MemberWalletAgentInfoVo memberWalletAgentInfoVo = new MemberWalletAgentInfoVo(); + memberWalletAgentInfoVo.setTotalBalance(availableBalance.setScale(4, BigDecimal.ROUND_DOWN)); + memberWalletAgentInfoVo.setTotalRMBBalance(multiply.setScale(4, BigDecimal.ROUND_DOWN)); + return Result.ok(memberWalletAgentInfoVo); + } + @Override - public Result getWalletCoin() { - //获取用户ID - Long memberId = LoginUserUtils.getAppLoginUser().getId(); - CnyUsdtExchange cnyUsdtExchange =cnyUsdtExchangeDao.getCNYAndUSDTOne(); - BigDecimal cnyUsdt = cnyUsdtExchange.getValue(); - - 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); - BigDecimal totalCny = totalUsdt.multiply(cnyUsdt); - walletCoin.setTotalBalance(totalCny.setScale(4, BigDecimal.ROUND_DOWN)); - }else { - BigDecimal amount = walletCoin.getAvailableBalance().add(walletCoin.getFrozenBalance()); - BigDecimal closePrice = new BigDecimal("10.0000"); - BigDecimal totalUsdt = BigDecimal.ZERO; - //Double closePrice = symbolsService.getCloseSymbolsBySymbolsName(walletCoin.getWalletCode()+"/USDT"); - totalUsdt = totalUsdt.add(amount.multiply(closePrice)); - totalUsdts = totalUsdts.add(totalUsdt); - walletCoin.setTotalBalance(totalUsdt.multiply(cnyUsdt).setScale(4, BigDecimal.ROUND_DOWN)); - } - } - } - MemberWalletCoinVo memberWalletCoinVo = new MemberWalletCoinVo(); - memberWalletCoinVo.setTotalUsdt(totalUsdts.setScale(4, BigDecimal.ROUND_DOWN)); - memberWalletCoinVo.setTotalCny(totalUsdts.multiply(cnyUsdt).setScale(4, BigDecimal.ROUND_DOWN)); - memberWalletCoinVo.setMemberWalletCoinInfoVo(memberWalletCoinInfoVolist); - return Result.ok(memberWalletCoinVo); - }else { - List<MemberWalletCoinInfoVo> memberWalletCoinlist = new ArrayList<MemberWalletCoinInfoVo>(); - MemberWalletCoinInfoVo coin = new MemberWalletCoinInfoVo(); - coin.setAvailableBalance(BigDecimal.ZERO); - coin.setTotalBalance(BigDecimal.ZERO); - coin.setFrozenBalance(BigDecimal.ZERO); - coin.setWalletCode(CoinTypeEnum.BTC.toString()); - memberWalletCoinlist.add(coin); - coin.setWalletCode(CoinTypeEnum.ETH.toString()); - memberWalletCoinlist.add(coin); - coin.setWalletCode(CoinTypeEnum.LTC.toString()); - memberWalletCoinlist.add(coin); - coin.setWalletCode(CoinTypeEnum.BCH.toString()); - memberWalletCoinlist.add(coin); - coin.setWalletCode(CoinTypeEnum.USDT.toString()); - memberWalletCoinlist.add(coin); - coin.setWalletCode(CoinTypeEnum.EOS.toString()); - memberWalletCoinlist.add(coin); - coin.setWalletCode(CoinTypeEnum.XRP.toString()); - memberWalletCoinlist.add(coin); - coin.setWalletCode(CoinTypeEnum.ETC.toString()); - memberWalletCoinlist.add(coin); - - MemberWalletCoinVo memberWalletCoinVo = new MemberWalletCoinVo(); - 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); - } + public Result getWalletAgentIntoRecords(RecordsPageDto recordsPageDto) { + //获取用户ID + Long memberId = LoginUserUtils.getAppLoginUser().getId(); + + 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(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); + } + } + } + Page<MemberAccountMoneyChangeInfoVo> pageEntityToPageVo = new Page<>(); + pageEntityToPageVo.setRecords(arrayList); + + return Result.ok(pageEntityToPageVo); } @Override - public Result getWalletCoinBySymbol(String symbol) { - //获取用户ID - Long memberId = LoginUserUtils.getAppLoginUser().getId(); - MemberWalletCoinEntity walletCoin = memberWalletCoinDao.selectWalletCoinBymIdAndCode(memberId,symbol); - MemberWalletCoinInfoVo memberWalletCoinInfoVo = new MemberWalletCoinInfoVo(); - memberWalletCoinInfoVo.setFrozenBalance(walletCoin.getFrozenBalance()); - memberWalletCoinInfoVo.setAvailableBalance(walletCoin.getAvailableBalance()); - memberWalletCoinInfoVo.setMemberId(memberId); - memberWalletCoinInfoVo.setWalletCode(symbol); - if(!StrUtil.isEmpty(memberId.toString())) { - CnyUsdtExchange cnyUsdtExchange =cnyUsdtExchangeDao.getCNYAndUSDTOne(); - BigDecimal cnyUsdt = cnyUsdtExchange.getValue(); - BigDecimal total = walletCoin.getAvailableBalance().add(walletCoin.getFrozenBalance()); - - if(MemberWalletCoinEnum.WALLETCOINCODE.getValue().equals(walletCoin.getWalletCode())) { - memberWalletCoinInfoVo.setTotalBalance(total.multiply(cnyUsdt).setScale(4, BigDecimal.ROUND_DOWN)); - - }else { - BigDecimal closePrice = new BigDecimal("10.0000"); - //Double closePrice = symbolsService.getCloseSymbolsBySymbolsName(wallet.getCode()+"/USDT"); - memberWalletCoinInfoVo.setTotalBalance(total.multiply(closePrice).multiply(cnyUsdt).setScale(4, BigDecimal.ROUND_DOWN)); - } - } - return Result.ok(memberWalletCoinInfoVo); + 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 Result getWalletContractById() { - - //获取用户ID - Long memberId = LoginUserUtils.getAppLoginUser().getId(); - - CnyUsdtExchange 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")); - } - /** - * todo - */ - //获取当前的合约持仓 - //获取实时盈亏 - BigDecimal profitAndLoss = new BigDecimal("50.000"); - //获取总付款 - BigDecimal totalPayment = new BigDecimal("50.000"); - 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)); - - return Result.ok(memberWalletContractInfoVo); - } + @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 { - @Override - @Transactional(rollbackFor = Exception.class) - public Result coinWalletTransferToContract(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); - if(subtract.compareTo(BigDecimal.ZERO)<0) { - return Result.fail(MessageSourceUtils.getString("member_service_0005")); - } - walletCoin.setAvailableBalance(total.setScale(4, BigDecimal.ROUND_DOWN)); - walletCoin.setTotalBalance(subtract.setScale(4, BigDecimal.ROUND_DOWN)); - int updateWalletCoinById = memberWalletCoinDao.updateById(walletCoin); - if(updateWalletCoinById < 1) { - return Result.fail(MessageSourceUtils.getString("member_service_0096")); - } - // 加币 - // 查询合约账户 - MemberWalletContractEntity walletContract = memberWalletContractDao.findWalletContractByMemberIdAndSymbol(memberId, walletCode); - BigDecimal availableBalance = walletContract.getAvailableBalance(); - BigDecimal add = availableBalance.add(balance); - walletContract.setAvailableBalance(add.setScale(4, BigDecimal.ROUND_DOWN)); - BigDecimal totalBalance = walletContract.getTotalBalance(); - BigDecimal totalBigDecimal = totalBalance.add(balance); - walletContract.setTotalBalance(totalBigDecimal.setScale(4, BigDecimal.ROUND_DOWN)); - int updateWalletContractById = memberWalletContractDao.updateById(walletContract); - if(updateWalletContractById < 1) { - return Result.fail(MessageSourceUtils.getString("member_service_0096")); - } - //添加币币资金划转历史记录 - MemberAccountMoneyChange memberAccountRecord = new MemberAccountMoneyChange(); - memberAccountRecord.setContent(MemberWalletCoinEnum.CONTENTTOCONTRACT.getValue()); - memberAccountRecord.setMemberId(memberId); - memberAccountRecord.setAmount(balance.negate().setScale(4, BigDecimal.ROUND_DOWN)); - memberAccountRecord.setStatus(MemberAccountMoneyChange.STATUS_SUCCESS_INTEGER); - memberAccountRecord.setSymbol(MemberWalletCoinEnum.WALLETCOINCODE.getValue()); - memberAccountRecord.setType(MemberAccountMoneyChange.TYPE_WALLET_COIN); - memberAccountMoneyChangeDao.insert(memberAccountRecord); - - //添加合约资金划转历史记录 - memberAccountRecord.setContent(MemberWalletCoinEnum.CONTENTFROMWALLETCOIN.getValue()); - memberAccountRecord.setSymbol(MemberWalletCoinEnum.WALLETCOINCODE.getValue()); - memberAccountRecord.setAmount(balance.setScale(4, BigDecimal.ROUND_DOWN)); - memberAccountRecord.setType(MemberAccountMoneyChange.TYPE_WALLET_CONTRACT); - memberAccountMoneyChangeDao.insert(memberAccountRecord); - } - return Result.ok(MessageSourceUtils.getString("member_service_0006")); - } + } + } + } - @Override - @Transactional(rollbackFor = Exception.class) - public Result contractTransferToWalletCoin(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, walletCode); - 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.setScale(4, BigDecimal.ROUND_DOWN)); - walletContract.setTotalBalance(totalSubtract.setScale(4, BigDecimal.ROUND_DOWN)); - 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.setScale(4, BigDecimal.ROUND_DOWN)); - walletCoin.setTotalBalance(CoinTotalBalance.setScale(4, BigDecimal.ROUND_DOWN)); - int updateById = memberWalletCoinDao.updateById(walletCoin); - if(updateById < 1) { - return Result.fail(MessageSourceUtils.getString("member_service_0096")); - } - - //添加资金划转历史记录 - MemberAccountMoneyChange memberAccountRecord = new MemberAccountMoneyChange(); - memberAccountRecord.setContent(MemberWalletCoinEnum.CONTENTTOWALLETCOIN.getValue()); - memberAccountRecord.setMemberId(memberId); - memberAccountRecord.setAmount(balance.negate().setScale(4, BigDecimal.ROUND_DOWN)); - memberAccountRecord.setStatus(MemberAccountMoneyChange.STATUS_SUCCESS_INTEGER); - memberAccountRecord.setSymbol(walletCode); - memberAccountRecord.setType(MemberAccountMoneyChange.TYPE_WALLET_CONTRACT); - memberAccountMoneyChangeDao.insert(memberAccountRecord); - - //添加资金划转历史记录 - memberAccountRecord.setContent(MemberWalletCoinEnum.CONTENTFROMCONTRACT.getValue()); - memberAccountRecord.setSymbol(walletCode); - memberAccountRecord.setType(MemberAccountMoneyChange.TYPE_WALLET_COIN); - memberAccountRecord.setAmount(balance.setScale(4, BigDecimal.ROUND_DOWN)); - memberAccountMoneyChangeDao.insert(memberAccountRecord); - return Result.ok(MessageSourceUtils.getString("member_service_0006")); - } + @Override + @Transactional + public Result usdtToGusd(BigDecimal balance,Integer type) { + //获取用户ID + Long memberId = LoginUserUtils.getAppLoginUser().getId(); + if (balance.compareTo(BigDecimal.ZERO) <= 0) { + return Result.fail(MessageSourceUtils.getString("member_service_0004")); + } + if(1 == type){ + // 扣币 + String walletCode = MemberWalletCoinEnum.WALLETCOINCODE.getValue(); + MemberWalletCoinEntity memberWalletCoinEntity = memberWalletCoinDao.selectWalletCoinBymIdAndCode(memberId, walletCode); + BigDecimal availableBalance = memberWalletCoinEntity.getAvailableBalance(); + BigDecimal totalBalance = memberWalletCoinEntity.getTotalBalance(); - @Override - public Result findWalletContractBySymbol() { - //获取用户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); - } + BigDecimal available = availableBalance.subtract(balance); + if (available.compareTo(BigDecimal.ZERO) < 0) { + return Result.fail(MessageSourceUtils.getString("member_service_0005")); + } + BigDecimal total = totalBalance.subtract(balance); + if (total.compareTo(BigDecimal.ZERO) < 0) { + return Result.fail(MessageSourceUtils.getString("member_service_0005")); + } - @Override - public Result findWalletCoinBySymbol(String symbol) { - //获取用户ID - Long memberId = LoginUserUtils.getAppLoginUser().getId(); - - MemberWalletCoinEntity walletCoin = memberWalletCoinDao.selectWalletCoinBymIdAndCode(memberId, symbol); - BigDecimal availableBalance = walletCoin.getAvailableBalance().setScale(4, BigDecimal.ROUND_DOWN); - return Result.ok(availableBalance); - } + memberWalletCoinEntity.setAvailableBalance(available); + memberWalletCoinEntity.setTotalBalance(total); + int i = memberWalletCoinDao.updateById(memberWalletCoinEntity); + if (i < 1) { + return Result.fail(MessageSourceUtils.getString("member_service_0096")); + } + //添加资金划转历史记录 + MemberAccountMoneyChange memberAccountRecord = new MemberAccountMoneyChange(); + //获取usdt兑换gusd的兑换比例 + BigDecimal usdtToGusd = new BigDecimal(redisUtils.getString("USDTTOGSD")); + BigDecimal gusdBalance = balance.multiply(usdtToGusd); - @Override - public Result getWalletCoinRecords() { - //获取用户ID - Long memberId = LoginUserUtils.getAppLoginUser().getId(); - List<MemberAccountMoneyChange> coinRecordList = memberAccountMoneyChangeDao.selectWalletCoinRecordsByMemIdTypeSymbol(memberId); - List<MemberAccountMoneyChangeInfoVo> arrayList = new ArrayList<>(); - if(CollUtil.isNotEmpty(coinRecordList)) { - for(MemberAccountMoneyChange memberAccountMoneyChange : coinRecordList) { - 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); - } - } - return Result.ok(arrayList); - } + ZhiYaEntity zhiya = zhiYaDao.selectByMemberId(memberId); + BigDecimal gusdAvailableBalance = zhiya.getAvailableBalance(); + BigDecimal gusdTotalBalance = zhiya.getTotalBalance(); + zhiya.setAvailableBalance(gusdAvailableBalance.add(gusdBalance)); + zhiya.setTotalBalance(gusdTotalBalance.add(gusdBalance)); - @Override - public Result getWalletContractRecords() { - //获取用户ID - Long memberId = LoginUserUtils.getAppLoginUser().getId(); - String symbol = MemberWalletCoinEnum.WALLETCOINCODE.getValue(); - List<MemberAccountMoneyChange> contractRecordList = memberAccountMoneyChangeDao.selectWalletContractRecordsByMemIdTypeSymbol(symbol,memberId); - - 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); - } - } - - return Result.ok(arrayList); - } + int updateById = zhiYaDao.updateById(zhiya); + if (updateById < 1) { + return Result.fail(MessageSourceUtils.getString("member_service_0096")); + } - @Override - public Result getWalletAgentRecords() { - //获取用户ID - Long memberId = LoginUserUtils.getAppLoginUser().getId(); - - List<MemberAccountMoneyChange> contractRecordList = - memberAccountMoneyChangeDao.selectWalletAgentRecordByMemIdTypeSymbol(MemberWalletCoinEnum.WALLETCOINCODE.getValue(),memberId); - - 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); - } - } - return Result.ok(arrayList); - } + //添加资金划转历史记录 + memberAccountRecord.setMemberId(memberId); + memberAccountRecord.setStatus(MemberAccountMoneyChange.STATUS_SUCCESS_INTEGER); + memberAccountRecord.setSymbol(walletCode); + memberAccountRecord.setContent(MemberWalletCoinEnum.ZHIYATOGUSD.getValue()); + memberAccountRecord.setType(MemberAccountMoneyChange.TYPE_WALLET_COIN); + memberAccountRecord.setAmount(gusdBalance); + memberAccountMoneyChangeDao.insert(memberAccountRecord); + }else if(2 == type){ + // 扣币 + ZhiYaEntity zhiya = zhiYaDao.selectByMemberId(memberId); + BigDecimal availableBalance = zhiya.getAvailableBalance(); + BigDecimal totalBalance = zhiya.getTotalBalance(); - @Override - @Transactional(rollbackFor = Exception.class) - public Result agentTransferToWalletCoin(BigDecimal balance, Integer transfertype) { - 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.setScale(4, BigDecimal.ROUND_DOWN)); - walletAgent.setTotalBalance(total.setScale(4, BigDecimal.ROUND_DOWN)); - - 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).setScale(4, BigDecimal.ROUND_DOWN)); - walletCoin.setTotalBalance(walletCoinTotalBalance.add(balance).setScale(4, BigDecimal.ROUND_DOWN)); - - 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.setScale(4, BigDecimal.ROUND_DOWN)); - memberAccountMoneyChangeDao.insert(memberAccountRecord); - memberAccountRecord.setContent(MemberWalletCoinEnum.CONTENTTOWALLETCOIN.getValue()); - - }else if(MemberAccountMoneyChange.TYPE_WALLET_CONTRACT.equals(transfertype)) { - //代理账户转合约 - MemberWalletContractEntity walletContract = memberWalletContractDao.findWalletContractByMemberIdAndSymbol(memberId, walletCode); - BigDecimal walletContractAvailableBalance = walletContract.getAvailableBalance(); - BigDecimal walletContractTotalBalance = walletContract.getTotalBalance(); - - walletContract.setAvailableBalance(walletContractAvailableBalance.add(balance).setScale(4, BigDecimal.ROUND_DOWN)); - walletContract.setTotalBalance(walletContractTotalBalance.add(balance).setScale(4, BigDecimal.ROUND_DOWN)); - - int updateById = memberWalletContractDao.updateById(walletContract); - 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_CONTRACT); - memberAccountRecord.setAmount(balance.setScale(4, BigDecimal.ROUND_DOWN)); - memberAccountMoneyChangeDao.insert(memberAccountRecord); - memberAccountRecord.setContent(MemberWalletCoinEnum.CONTENTTOCONTRACT.getValue()); - } - memberAccountRecord.setAmount(balance.negate().setScale(4, BigDecimal.ROUND_DOWN)); - memberAccountRecord.setType(MemberAccountMoneyChange.TYPE_WALLET_AGENT); - memberAccountMoneyChangeDao.insert(memberAccountRecord); - - return Result.ok(MessageSourceUtils.getString("member_service_0006")); - } + BigDecimal available = availableBalance.subtract(balance); + if (available.compareTo(BigDecimal.ZERO) < 0) { + return Result.fail(MessageSourceUtils.getString("member_service_0005")); + } + BigDecimal total = totalBalance.subtract(balance); + if (total.compareTo(BigDecimal.ZERO) < 0) { + return Result.fail(MessageSourceUtils.getString("member_service_0005")); + } - @Override - public Result findWalletAgentBySymbol() { - //获取用户ID - Long memberId = LoginUserUtils.getAppLoginUser().getId(); - String walletCode = MemberWalletCoinEnum.WALLETCOINCODE.getValue(); - - MemberWalletAgentEntity walletAgent = memberWalletAgentDao.selectWalletAgentBymIdAndCode(memberId, walletCode); - BigDecimal availableBalance = walletAgent.getAvailableBalance(); - - CnyUsdtExchange cnyUsdtExchange =cnyUsdtExchangeDao.getCNYAndUSDTOne(); - BigDecimal cnyUsdt = cnyUsdtExchange.getValue(); - BigDecimal multiply = availableBalance.multiply(cnyUsdt); - - MemberWalletAgentInfoVo memberWalletAgentInfoVo = new MemberWalletAgentInfoVo(); - memberWalletAgentInfoVo.setTotalBalance(availableBalance.setScale(4, BigDecimal.ROUND_DOWN)); - memberWalletAgentInfoVo.setTotalRMBBalance(multiply.setScale(4, BigDecimal.ROUND_DOWN)); - return Result.ok(memberWalletAgentInfoVo); - } + zhiya.setAvailableBalance(available); + zhiya.setTotalBalance(total); + int i = zhiYaDao.updateById(zhiya); + if (i < 1) { + return Result.fail(MessageSourceUtils.getString("member_service_0096")); + } + //添加资金划转历史记录 + MemberAccountMoneyChange memberAccountRecord = new MemberAccountMoneyChange(); + //获取usdt兑换gusd的兑换比例 + BigDecimal usdtToGusd = new BigDecimal(redisUtils.getString("USDTTOGSD")); + BigDecimal usdtBalance = balance.divide(usdtToGusd); + + String walletCode = MemberWalletCoinEnum.WALLETCOINCODE.getValue(); + MemberWalletCoinEntity memberWalletCoinEntity = memberWalletCoinDao.selectWalletCoinBymIdAndCode(memberId, walletCode); + + BigDecimal gusdAvailableBalance = memberWalletCoinEntity.getAvailableBalance(); + BigDecimal gusdTotalBalance = memberWalletCoinEntity.getTotalBalance(); + memberWalletCoinEntity.setAvailableBalance(gusdAvailableBalance.add(usdtBalance)); + memberWalletCoinEntity.setTotalBalance(gusdTotalBalance.add(usdtBalance)); + + int updateById = memberWalletCoinDao.updateById(memberWalletCoinEntity); + if (updateById < 1) { + return Result.fail(MessageSourceUtils.getString("member_service_0096")); + } + + //添加资金划转历史记录 + memberAccountRecord.setMemberId(memberId); + memberAccountRecord.setStatus(MemberAccountMoneyChange.STATUS_SUCCESS_INTEGER); + memberAccountRecord.setSymbol(walletCode); + memberAccountRecord.setContent(MemberWalletCoinEnum.ZHIYATOUSDT.getValue()); + memberAccountRecord.setType(MemberAccountMoneyChange.TYPE_WALLET_COIN); + memberAccountRecord.setAmount(usdtBalance); + memberAccountMoneyChangeDao.insert(memberAccountRecord); + + }else{ + return Result.fail("member_controller_0005"); + } + + return Result.ok(MessageSourceUtils.getString("member_service_0024")); + } + + @Override + @Transactional + public Result zhiYaGusd(BigDecimal balance) { + //获取用户ID + Long memberId = LoginUserUtils.getAppLoginUser().getId(); + if (balance.compareTo(new BigDecimal(100)) < 0) { + return Result.fail(MessageSourceUtils.getString("zhiya_service_0006")); + } + + //质押Gusd,质押数量进入冻结余额, + ZhiYaEntity zhiya = zhiYaDao.selectByMemberId(memberId); + BigDecimal gusdAvailableBalance = zhiya.getAvailableBalance(); + BigDecimal gusdTotalBalance = zhiya.getTotalBalance(); + BigDecimal gusdfrozenBalance = zhiya.getFrozenBalance(); + + BigDecimal available = gusdAvailableBalance.subtract(balance); + if (available.compareTo(BigDecimal.ZERO) < 0) { + return Result.fail(MessageSourceUtils.getString("zhiya_service_0005")); + } + BigDecimal total = gusdTotalBalance.subtract(balance); + if (total.compareTo(BigDecimal.ZERO) < 0) { + return Result.fail(MessageSourceUtils.getString("zhiya_service_0005")); + } + zhiya.setFrozenBalance(gusdfrozenBalance.add(balance)); + zhiya.setAvailableBalance(gusdAvailableBalance.subtract(balance)); + + int updateById = zhiYaDao.updateById(zhiya); + if (updateById < 1) { + return Result.fail(MessageSourceUtils.getString("member_service_0096")); + } + //产生质押单,当前有的话,累加质押数量 +// ZhiYaRecordEntity zhiYaRecord = zhiYaRecordDao.selectByMemberIdAndOrderStatus(memberId,1); +// if(ObjectUtil.isEmpty(zhiYaRecord)){ + //产生一条质押记录 + ZhiYaRecordEntity zhiYaRecordEntity = new ZhiYaRecordEntity(); + zhiYaRecordEntity.setMemberId(memberId); + zhiYaRecordEntity.setOrderNo(generateSimpleSerialno(memberId.toString())); + zhiYaRecordEntity.setSymbol(MemberWalletCoinEnum.WALLETGUSDCOINCODE.getValue()); + zhiYaRecordEntity.setZhiyaCnt(balance); + zhiYaRecordEntity.setOrderStatus(1); + Date date = new Date(); + Date newDate = DateUtil.offsetDay(date, 1); + zhiYaRecordEntity.setEffectDate(newDate); + zhiYaRecordDao.insert(zhiYaRecordEntity); +// }else{ +// zhiYaRecord.setZhiyaCnt(zhiYaRecord.getZhiyaCnt().add(balance)); +// Date date = new Date(); +// Date newDate = DateUtil.offsetDay(date, 1); +// zhiYaRecord.setEffectDate(newDate); +// zhiYaRecordDao.updateById(zhiYaRecord); +// } + //更新团队算力表 + //只计算直属团队的算力信息 + + + return Result.ok(MessageSourceUtils.getString("member_service_0024")); + } + + @Override + @Transactional + public Result shuhuiGusd(BigDecimal balance,Long id) { + //获取用户ID + Long memberId = LoginUserUtils.getAppLoginUser().getId(); + if (balance.compareTo(BigDecimal.ZERO) <= 0) { + return Result.fail(MessageSourceUtils.getString("zhiya_service_0004")); + } + //赎回Gusd,获取质押单据, + ZhiYaRecordEntity zhiYaRecordEntity = zhiYaRecordDao.selectByIdAndMemberId(id,memberId); + if(ObjectUtil.isEmpty(zhiYaRecordEntity)){ + return Result.fail(MessageSourceUtils.getString("zhiya_service_0002")); + } + if(1 != zhiYaRecordEntity.getOrderStatus()){ + return Result.fail(MessageSourceUtils.getString("zhiya_service_0003")); + } + BigDecimal zhiyaCnt = zhiYaRecordEntity.getZhiyaCnt(); + if(balance.compareTo(zhiyaCnt) > 0){ + return Result.fail(MessageSourceUtils.getString("zhiya_service_0004")); + } +// zhiYaRecordEntity.setZhiyaCnt(zhiyaCnt.subtract(balance)); + if(zhiyaCnt.compareTo(balance) != 0){ + return Result.fail(MessageSourceUtils.getString("zhiya_service_0001")); + } + zhiYaRecordEntity.setOrderStatus(2); + zhiYaRecordDao.updateById(zhiYaRecordEntity); + //赎回Gusd,质押数量进入可用余额, + ZhiYaEntity zhiya = zhiYaDao.selectByMemberId(memberId); + BigDecimal gusdAvailableBalance = zhiya.getAvailableBalance(); + BigDecimal gusdTotalBalance = zhiya.getTotalBalance(); + BigDecimal gusdfrozenBalance = zhiya.getFrozenBalance(); + + BigDecimal frozen = gusdfrozenBalance.subtract(balance); + if (frozen.compareTo(BigDecimal.ZERO) < 0) { + return Result.fail(MessageSourceUtils.getString("zhiya_service_0001")); + } + BigDecimal total = gusdTotalBalance.subtract(balance); + if (total.compareTo(BigDecimal.ZERO) < 0) { + return Result.fail(MessageSourceUtils.getString("member_service_0005")); + } + zhiya.setFrozenBalance(gusdfrozenBalance.subtract(balance)); + zhiya.setAvailableBalance(gusdAvailableBalance.add(balance)); + zhiYaDao.updateById(zhiya); + return Result.ok(MessageSourceUtils.getString("member_service_0024")); + } + + @Override + public Result findMemberGusdInfo() { + //获取用户ID + Long memberId = LoginUserUtils.getAppLoginUser().getId(); + ZhiYaEntity zhiYaEntity = zhiYaDao.selectByMemberId(memberId); + MemberGusdInfoVo memberGusdInfoVo = new MemberGusdInfoVo(); + memberGusdInfoVo.setMemberId(memberId); + memberGusdInfoVo.setFrozenBalance(zhiYaEntity.getFrozenBalance()); + memberGusdInfoVo.setTotalBalance(zhiYaEntity.getTotalBalance()); + memberGusdInfoVo.setAvailableBalance(zhiYaEntity.getAvailableBalance()); + return Result.ok(memberGusdInfoVo); + } + + @Override + public Result getZhiyaRecords(RecordsPageDto recordsPageDto) { + //获取用户ID + Long memberId = LoginUserUtils.getAppLoginUser().getId(); + + Page<ZhiyaInfoVo> page = new Page<>(recordsPageDto.getPageNum(), recordsPageDto.getPageSize()); + ZhiYaEntity zhiya = new ZhiYaEntity(); + zhiya.setMemberId(memberId); + IPage<ZhiyaInfoVo> lists = zhiYaRecordDao.selectByMemberIdInPage(page, zhiya); + return Result.ok(lists); + } + + @Override + public Result getusdtToGusdRecords(RecordsPageDto recordsPageDto) { + //获取用户ID + Long memberId = LoginUserUtils.getAppLoginUser().getId(); + + Page<UsdtToGusdVo> page = new Page<>(recordsPageDto.getPageNum(), recordsPageDto.getPageSize()); + MemberAccountMoneyChange memberAccountMoneyChange = new MemberAccountMoneyChange(); + memberAccountMoneyChange.setMemberId(memberId); + IPage<UsdtToGusdVo> lists = memberAccountMoneyChangeDao.selectByMemberIdInPage(page, memberAccountMoneyChange); + return Result.ok(lists); + } + + @Override + @Transactional + public void grantZhiyaAmount() { + //获取每日总产出 + BigDecimal gusdReward = new BigDecimal(redisUtils.getString("GUSDREWARD")); + //获取每日质押总数 + Long totalGusd = zhiYaRecordDao.selectTotalGusdByStatueAndTime(1,new Date()); + BigDecimal totalGusdBigDecimal = new BigDecimal(totalGusd); + if(totalGusdBigDecimal.compareTo(BigDecimal.ZERO) > 0){ + //平均一个获取奖励数量 + BigDecimal divide = gusdReward.divide(totalGusdBigDecimal,2, BigDecimal.ROUND_HALF_UP); + //获取今天之前满足发放奖励的全部质押单 + //生效时间小于当前时间 + //状态为生效中 + List<ZhiYaRecordEntity> lists = zhiYaRecordDao.selectZhiYaRewardByStatueAndTime(1,new Date()); + if(CollUtil.isNotEmpty(lists)){ + for(ZhiYaRecordEntity zhiYaRecordEntity : lists){ + ZhiyaRewardEntity zhiyaRewardEntity = new ZhiyaRewardEntity(); + zhiyaRewardEntity.setRecordId(zhiYaRecordEntity.getId()); + zhiyaRewardEntity.setRecordNo(zhiYaRecordEntity.getOrderNo()); + zhiyaRewardEntity.setMemberId(zhiYaRecordEntity.getMemberId()); + zhiyaRewardEntity.setRewardAmout(divide.multiply(zhiYaRecordEntity.getZhiyaCnt())); + zhiyaRewardDao.insert(zhiyaRewardEntity); + + //给团队上级发送奖励goldrice + //给账户的冻结 + + } + } + } + } + + @Override + public Result getZhiyaReward(ZhiyaRewardRecordsPageDto recordsPageDto) { + + //获取用户ID + Long memberId = LoginUserUtils.getAppLoginUser().getId(); + + Page<ZhiyaRewardVo> page = new Page<>(recordsPageDto.getPageNum(), recordsPageDto.getPageSize()); + ZhiyaRewardEntity zhiyaRewardEntity = new ZhiyaRewardEntity(); + zhiyaRewardEntity.setMemberId(memberId); + zhiyaRewardEntity.setRecordNo(recordsPageDto.getOrderNo()); + IPage<ZhiyaRewardVo> lists = zhiYaRecordDao.selectZhiyaRewardByMemberIdInPage(page, zhiyaRewardEntity); + return Result.ok(lists); + } + + @Override + public Result coinInList(@Valid CoinInListDto coinInListDto) { + //获取用户ID + Long memberId = LoginUserUtils.getAppLoginUser().getId(); + int type = coinInListDto.getType(); + //充币记录 + if(type == 1) { + Page<MemberCoinChargeEntity> page = new Page<>(coinInListDto.getPageNum(), coinInListDto.getPageSize()); + MemberCoinChargeEntity memberCoinChargeEntity = new MemberCoinChargeEntity(); + memberCoinChargeEntity.setMemberId(memberId); + IPage<MemberCoinChargeEntity> memberCoinCharge = memberCoinChargeDao.findMemberCoinChargeInPage(page, memberCoinChargeEntity); + List<MemberCoinChargeEntity> records = memberCoinCharge.getRecords(); + + Page<CoinInListVo> responsePage = new Page<>(coinInListDto.getPageNum(), coinInListDto.getPageSize()); + if(CollUtil.isNotEmpty(records)) { + ArrayList<CoinInListVo> arrayList = new ArrayList<>(); + for(MemberCoinChargeEntity memberCoinChargeEntitys : records) { + CoinInListVo coinInListVo = new CoinInListVo(); + coinInListVo.setSymbol(memberCoinChargeEntitys.getSymbol()); + coinInListVo.setAmount(memberCoinChargeEntitys.getAmount().toPlainString()); + coinInListVo.setContent("充币"); + coinInListVo.setStatus(memberCoinChargeEntitys.getStatus()); + coinInListVo.setUpdateTime(memberCoinChargeEntitys.getUpdateTime()); + arrayList.add(coinInListVo); + } + responsePage.setRecords(arrayList); + } + return Result.ok(responsePage); + } + //提币记录 + if(type == 2) { + Page<MemberCoinWithdrawEntity> page = new Page<>(coinInListDto.getPageNum(), coinInListDto.getPageSize()); + MemberCoinWithdrawEntity memberCoinWithdrawEntity = new MemberCoinWithdrawEntity(); + memberCoinWithdrawEntity.setMemberId(memberId); + IPage<MemberCoinWithdrawEntity> memberCoinWithdraw = memberCoinWithdrawDao.findMemberCoinWithdrawInPage(page, memberCoinWithdrawEntity); + List<MemberCoinWithdrawEntity> records = memberCoinWithdraw.getRecords(); + + Page<CoinInListVo> responsePage = new Page<>(coinInListDto.getPageNum(), coinInListDto.getPageSize()); + if(CollUtil.isNotEmpty(records)) { + ArrayList<CoinInListVo> arrayList = new ArrayList<>(); + for(MemberCoinWithdrawEntity memberCoinWithdrawEntitys : records) { + CoinInListVo coinInListVo = new CoinInListVo(); + coinInListVo.setSymbol(memberCoinWithdrawEntitys.getSymbol()); + coinInListVo.setAmount(memberCoinWithdrawEntitys.getAmount().toPlainString()); + coinInListVo.setContent("提币"); + int status = memberCoinWithdrawEntitys.getStatus(); + int statusOut = 0; + if(status == 1) { + statusOut = 0; + } + if(status == 2) { + statusOut = 1; + } + if(status == 3) { + statusOut = 2; + } + coinInListVo.setStatus(statusOut); + coinInListVo.setUpdateTime(memberCoinWithdrawEntitys.getUpdateTime()); + coinInListVo.setIsInside(memberCoinWithdrawEntitys.getIsInside()); + arrayList.add(coinInListVo); + } + responsePage.setRecords(arrayList); + } + return Result.ok(responsePage); + } + //划转记录 + if(type == 3) { + Page<MemberAccountMoneyChange> page = new Page<>(coinInListDto.getPageNum(), coinInListDto.getPageSize()); + MemberAccountMoneyChange memberAccountMoneyChange = new MemberAccountMoneyChange(); + memberAccountMoneyChange.setMemberId(memberId); + IPage<MemberAccountMoneyChange> list = memberAccountMoneyChangeDao.coinInList(page, memberAccountMoneyChange); + List<MemberAccountMoneyChange> records = list.getRecords(); + + Page<CoinInListVo> responsePage = new Page<>(coinInListDto.getPageNum(), coinInListDto.getPageSize()); + if(CollUtil.isNotEmpty(records)) { + ArrayList<CoinInListVo> arrayList = new ArrayList<>(); + for(MemberAccountMoneyChange memberAccountMoneyChanges : records) { + CoinInListVo coinInListVo = new CoinInListVo(); + coinInListVo.setSymbol(memberAccountMoneyChanges.getSymbol()); + coinInListVo.setAmount(memberAccountMoneyChanges.getAmount().toPlainString()); + coinInListVo.setContent(memberAccountMoneyChanges.getContent()); + coinInListVo.setStatus(memberAccountMoneyChanges.getStatus()); + coinInListVo.setUpdateTime(memberAccountMoneyChanges.getUpdateTime()); + arrayList.add(coinInListVo); + } + responsePage.setRecords(arrayList); + } + return Result.ok(responsePage); + } + //其他记录 + if(type == 4) { + Page<OrderCoinsDealEntity> page = new Page<>(coinInListDto.getPageNum(), coinInListDto.getPageSize()); + MemberAccountMoneyChange memberAccountMoneyChange = new MemberAccountMoneyChange(); + memberAccountMoneyChange.setMemberId(memberId); + IPage<MemberAccountMoneyChange> list = memberAccountMoneyChangeDao.selectWalletAgentIntoRecordsByMemIdTypeSymbol(page, memberAccountMoneyChange); + List<MemberAccountMoneyChange> records = list.getRecords(); + + Page<CoinInListVo> responsePage = new Page<>(coinInListDto.getPageNum(), coinInListDto.getPageSize()); + if(CollUtil.isNotEmpty(records)) { + ArrayList<CoinInListVo> arrayList = new ArrayList<>(); + for(MemberAccountMoneyChange memberAccountMoneyChanges : records) { + CoinInListVo coinInListVo = new CoinInListVo(); + coinInListVo.setSymbol(memberAccountMoneyChanges.getSymbol()); + coinInListVo.setAmount(memberAccountMoneyChanges.getAmount().toPlainString()); + coinInListVo.setContent(memberAccountMoneyChanges.getContent()); + coinInListVo.setStatus(memberAccountMoneyChanges.getStatus()); + coinInListVo.setUpdateTime(memberAccountMoneyChanges.getUpdateTime()); + arrayList.add(coinInListVo); + } + responsePage.setRecords(arrayList); + } + return Result.ok(responsePage); + } + + return Result.fail(MessageSourceUtils.getString("member_controller_0005")); + + } + + public String generateSimpleSerialno(String userId) { + StringBuilder sb = new StringBuilder(); + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); + SimpleDateFormat sd = new SimpleDateFormat("yyyyMMdd"); + Date now = new Date(); + sb.append(sd.format(now)); + Calendar calendar = new GregorianCalendar(); + calendar.setTime(now); + calendar.add(calendar.DATE, 1); + Date nextDate = calendar.getTime(); + if (StrUtil.isNotEmpty(userId)) { + sb.append(userId); + } + sb.append(RandomUtil.randomInt(2)); + long count = zhiYaRecordDao.getOrderCountByToday(sdf.format(now), sdf.format(nextDate)); + count++; + int size = 4; + for (int i = 0; i < size - String.valueOf(count).length(); i++) { + sb.append("0"); + } + sb.append(count); + return sb.toString(); + } } -- Gitblit v1.9.1