From 451f88585ce56b852ceba3a7f20c62bf6fa9f8fe Mon Sep 17 00:00:00 2001 From: Helius <wangdoubleone@gmail.com> Date: Thu, 01 Apr 2021 17:53:02 +0800 Subject: [PATCH] modify --- src/main/java/com/xcong/excoin/modules/coin/service/impl/CoinServiceImpl.java | 282 +++++++++++++++++++++++++++++++++++++++++++++++++------ 1 files changed, 249 insertions(+), 33 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 b41f206..48bd01d 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 @@ -7,8 +7,15 @@ import java.util.Map; import javax.annotation.Resource; +import javax.validation.Valid; +import com.xcong.excoin.modules.contract.dao.ContractHoldOrderDao; +import com.xcong.excoin.modules.contract.entity.ContractEntrustOrderEntity; +import com.xcong.excoin.modules.contract.entity.ContractHoldOrderEntity; +import com.xcong.excoin.modules.contract.entity.ContractOrderEntity; import com.xcong.excoin.modules.platform.entity.PlatformCnyUsdtExchangeEntity; +import com.xcong.excoin.modules.platform.entity.PlatformTradeSettingEntity; +import com.xcong.excoin.utils.*; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -23,8 +30,10 @@ import com.xcong.excoin.modules.coin.entity.MemberAccountMoneyChange; import com.xcong.excoin.modules.coin.entity.OrderCoinsDealEntity; import com.xcong.excoin.modules.coin.mapper.MemberAccountMoneyChangeMapper; +import com.xcong.excoin.modules.coin.parameter.dto.CoinInListDto; 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.CoinInListVo; import com.xcong.excoin.modules.coin.parameter.vo.ContractSymbolListVo; import com.xcong.excoin.modules.coin.parameter.vo.MemberAccountMoneyChangeInfoVo; import com.xcong.excoin.modules.coin.parameter.vo.MemberWalletAgentInfoVo; @@ -32,23 +41,24 @@ 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.MemberCoinChargeDao; +import com.xcong.excoin.modules.member.dao.MemberCoinWithdrawDao; 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.MemberCoinChargeEntity; +import com.xcong.excoin.modules.member.entity.MemberCoinWithdrawEntity; 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; import com.xcong.excoin.modules.platform.dao.PlatformCnyUsdtExchangeDao; -import com.xcong.excoin.utils.CoinTypeConvert; -import com.xcong.excoin.utils.MessageSourceUtils; -import com.xcong.excoin.utils.RedisUtils; -import com.xcong.excoin.utils.ThreadPoolUtils; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.StrUtil; +import jnr.ffi.Struct.int16_t; @Service public class CoinServiceImpl extends ServiceImpl<MemberWalletCoinDao, MemberWalletCoinEntity> implements CoinService { @@ -68,7 +78,15 @@ @Resource MemberDao memberDao; @Resource + MemberCoinChargeDao memberCoinChargeDao; + @Resource + MemberCoinWithdrawDao memberCoinWithdrawDao; + @Resource RedisUtils redisUtils; + @Resource + CacheSettingUtils cacheSettingUtils; + @Resource + ContractHoldOrderDao contractHoldOrderDao; @Override @@ -186,31 +204,23 @@ Long memberId = LoginUserUtils.getAppLoginUser().getId(); PlatformCnyUsdtExchangeEntity cnyUsdtExchange = cnyUsdtExchangeDao.getCNYAndUSDTOne(); BigDecimal cnyUsdt = cnyUsdtExchange.getValue(); + Map<String, Object> columnMap = new HashMap<String, Object>(); columnMap.put("member_id", memberId); - - Map<String, Object> map = new HashMap<String, Object>(); + columnMap.put("wallet_code", "USDT"); List<MemberWalletContractEntity> selectByMap = memberWalletContractDao.selectByMap(columnMap); - BigDecimal totalCoin = BigDecimal.ZERO; - - List<MemberWalletContractInfoVo> list = new ArrayList<>(); + MemberWalletContractInfoVo memberWalletContractInfoVo = new MemberWalletContractInfoVo(); if(CollUtil.isNotEmpty(selectByMap)) { for(MemberWalletContractEntity memberWalletContractEntity : selectByMap) { - MemberWalletContractInfoVo memberWalletContractInfoVo = new MemberWalletContractInfoVo(); memberWalletContractInfoVo.setFrozenBalance(memberWalletContractEntity.getFrozenBalance().setScale(4, BigDecimal.ROUND_DOWN)); memberWalletContractInfoVo.setAvailableBalance(memberWalletContractEntity.getAvailableBalance().setScale(4, BigDecimal.ROUND_DOWN)); - memberWalletContractInfoVo.setTotalBalance(memberWalletContractEntity.getTotalBalance().setScale(4, BigDecimal.ROUND_DOWN)); - memberWalletContractInfoVo.setTotalRMBBalance(memberWalletContractEntity.getTotalBalance().multiply(cnyUsdt).setScale(4, BigDecimal.ROUND_DOWN)); - memberWalletContractInfoVo.setWalletCode(memberWalletContractEntity.getWalletCode()); - list.add(memberWalletContractInfoVo); - totalCoin = totalCoin.add(memberWalletContractEntity.getTotalBalance()); + memberWalletContractInfoVo.setTotalCoin(memberWalletContractEntity.getTotalBalance().setScale(4, BigDecimal.ROUND_DOWN)); + memberWalletContractInfoVo.setAvailableCoin(memberWalletContractEntity.getTotalBalance().multiply(cnyUsdt).setScale(4, BigDecimal.ROUND_DOWN)); + memberWalletContractInfoVo.setWalletCode(memberWalletContractEntity.getWalletCode()+"/USDT"); } } - map.put("memberWalletContractInfoVo", list); - map.put("totalCoin", totalCoin.setScale(4, BigDecimal.ROUND_DOWN)); - map.put("availableCoin", totalCoin.multiply(cnyUsdt).setScale(4, BigDecimal.ROUND_DOWN)); - return Result.ok(map); + return Result.ok(memberWalletContractInfoVo); } @Override @@ -314,7 +324,9 @@ //更新合约全仓模式下的订单权益 MemberEntity memberEntity = memberDao.selectById(memberId); String symbols = symbol+"/USDT"; - ThreadPoolUtils.sendWholeForceClosingPrice(symbols, memberEntity); + //ThreadPoolUtils.sendWholeForceClosingPrice(symbols, memberEntity); + // 全仓爆仓 + ThreadPoolUtils.sendWholePrice(memberEntity.getId()); //添加币币资金划转历史记录 MemberAccountMoneyChange memberAccountRecord = new MemberAccountMoneyChange(); @@ -398,15 +410,53 @@ @Override @Transactional(rollbackFor = Exception.class) public Result contractTransferToWalletCoins(BigDecimal balance, String symbol) { - if (balance.compareTo(BigDecimal.ZERO) <= 0) { + //获取用户ID + Long memberId = LoginUserUtils.getAppLoginUser().getId(); + MemberEntity memberEntity = memberDao.selectById(memberId); + if (balance.compareTo(BigDecimal.ZERO) <= 0) { return Result.fail(MessageSourceUtils.getString("member_service_0004")); } - //获取用户ID - Long memberId = LoginUserUtils.getAppLoginUser().getId(); - + //获取合约当前持仓类型 + Integer contractPositionType = memberEntity.getContractPositionType(); + // 总盈利 + BigDecimal totalProfitOrLess = BigDecimal.ZERO; + if(contractPositionType == 2){ + //获取全仓模式下的所有持仓信息 + PlatformTradeSettingEntity tradeSetting = cacheSettingUtils.getTradeSetting(); + List<ContractHoldOrderEntity> holdOrderEntities = contractHoldOrderDao.selectHoldOrderListForWholeByMemberIdAndSymbol(memberEntity.getId(),""); + if (CollUtil.isNotEmpty(holdOrderEntities)) { + for (ContractHoldOrderEntity holdOrderEntity : holdOrderEntities) { + // 获取最新价 + BigDecimal newPrice = new BigDecimal(redisUtils.getString(CoinTypeConvert.convertToKey(holdOrderEntity.getSymbol()))); + BigDecimal lotNumber = cacheSettingUtils.getSymbolSku(holdOrderEntity.getSymbol()); + // 单个订单盈利 + BigDecimal profitOrLess = BigDecimal.ZERO; + // 开多 + if (ContractHoldOrderEntity.OPENING_TYPE_MORE == holdOrderEntity.getOpeningType()) { + profitOrLess = newPrice.subtract(holdOrderEntity.getOpeningPrice()).multiply(new BigDecimal(holdOrderEntity.getSymbolCntSale())).multiply(lotNumber); + // 开空 + } else { + profitOrLess = holdOrderEntity.getOpeningPrice().subtract(newPrice).multiply(new BigDecimal(holdOrderEntity.getSymbolCntSale())).multiply(lotNumber); + } + if (MemberEntity.IS_PROFIT_Y == memberEntity.getIsProfit()) { + if (profitOrLess.compareTo(BigDecimal.ZERO) > 0) { + profitOrLess = profitOrLess.multiply(BigDecimal.ONE.subtract(tradeSetting.getForceParam())); + } else { + profitOrLess = profitOrLess.multiply(BigDecimal.ONE.add(tradeSetting.getForceParam())); + } + } + totalProfitOrLess = totalProfitOrLess.add(profitOrLess); + } + } + } + String walletCode = MemberWalletCoinEnum.WALLETCOINCODE.getValue(); MemberWalletContractEntity walletContract = memberWalletContractDao.findWalletContractByMemberIdAndSymbol(memberId, symbol); BigDecimal availableBalance = walletContract.getAvailableBalance(); + //可用减去盈亏 + if(totalProfitOrLess.compareTo(BigDecimal.ZERO) < 0){ + availableBalance = availableBalance.add(totalProfitOrLess); + } // 扣币 BigDecimal availableSubtract = availableBalance.subtract(balance); if (availableSubtract.compareTo(BigDecimal.ZERO) < 0) { @@ -434,11 +484,9 @@ if (updateById < 1) { return Result.fail(MessageSourceUtils.getString("member_service_0096")); } - - //更新合约全仓模式下的订单权益 - MemberEntity memberEntity = memberDao.selectById(memberId); - String symbols = symbol+"/USDT"; - ThreadPoolUtils.sendWholeForceClosingPrice(symbols, memberEntity); + + // 全仓爆仓 + ThreadPoolUtils.sendWholePrice(memberEntity.getId()); //添加资金划转历史记录 MemberAccountMoneyChange memberAccountRecord = new MemberAccountMoneyChange(); @@ -463,8 +511,52 @@ public Result findWalletContractBySymbol(String symbol) { //获取用户ID Long memberId = LoginUserUtils.getAppLoginUser().getId(); + MemberEntity memberEntity = memberDao.selectById(memberId); + //获取合约当前持仓类型 + Integer contractPositionType = memberEntity.getContractPositionType(); + // 总盈利 + BigDecimal totalProfitOrLess = BigDecimal.ZERO; + if(contractPositionType == 2){ + //获取全仓模式下的所有持仓信息 + PlatformTradeSettingEntity tradeSetting = cacheSettingUtils.getTradeSetting(); + List<ContractHoldOrderEntity> holdOrderEntities = contractHoldOrderDao.selectHoldOrderListForWholeByMemberIdAndSymbol(memberEntity.getId(),""); + if (CollUtil.isNotEmpty(holdOrderEntities)) { + for (ContractHoldOrderEntity holdOrderEntity : holdOrderEntities) { + // 获取最新价 + BigDecimal newPrice = new BigDecimal(redisUtils.getString(CoinTypeConvert.convertToKey(holdOrderEntity.getSymbol()))); + BigDecimal lotNumber = cacheSettingUtils.getSymbolSku(holdOrderEntity.getSymbol()); + // 单个订单盈利 + BigDecimal profitOrLess = BigDecimal.ZERO; + // 开多 + if (ContractHoldOrderEntity.OPENING_TYPE_MORE == holdOrderEntity.getOpeningType()) { + profitOrLess = newPrice.subtract(holdOrderEntity.getOpeningPrice()).multiply(new BigDecimal(holdOrderEntity.getSymbolCntSale())).multiply(lotNumber); + // 开空 + } else { + profitOrLess = holdOrderEntity.getOpeningPrice().subtract(newPrice).multiply(new BigDecimal(holdOrderEntity.getSymbolCntSale())).multiply(lotNumber); + } + if (MemberEntity.IS_PROFIT_Y == memberEntity.getIsProfit()) { + if (profitOrLess.compareTo(BigDecimal.ZERO) > 0) { + profitOrLess = profitOrLess.multiply(BigDecimal.ONE.subtract(tradeSetting.getForceParam())); + } else { + profitOrLess = profitOrLess.multiply(BigDecimal.ONE.add(tradeSetting.getForceParam())); + } + } + totalProfitOrLess = totalProfitOrLess.add(profitOrLess); + } + } + } + MemberWalletContractEntity walletContract = memberWalletContractDao.findWalletContractByMemberIdAndSymbol(memberId, symbol); - BigDecimal availableBalance = walletContract.getAvailableBalance().setScale(4, BigDecimal.ROUND_DOWN); + BigDecimal availableBalance = walletContract.getAvailableBalance(); + //可用减去盈亏 + if(totalProfitOrLess.compareTo(BigDecimal.ZERO) < 0){ + availableBalance = availableBalance.add(totalProfitOrLess); + } + if(availableBalance.compareTo(BigDecimal.ZERO) > 0){ + availableBalance = availableBalance.setScale(4, BigDecimal.ROUND_DOWN); + }else{ + availableBalance = BigDecimal.ZERO; + } return Result.ok(availableBalance); } @@ -678,7 +770,9 @@ //更新合约全仓模式下的订单权益 MemberEntity memberEntity = memberDao.selectById(memberId); String symbols = symbol+"/USDT"; - ThreadPoolUtils.sendWholeForceClosingPrice(symbols, memberEntity); + //ThreadPoolUtils.sendWholeForceClosingPrice(symbols, memberEntity); + // 全仓爆仓 + ThreadPoolUtils.sendWholePrice(memberEntity.getId()); //添加资金划转历史记录 memberAccountRecord.setMemberId(memberId); @@ -858,7 +952,9 @@ //更新合约全仓模式下的订单权益 MemberEntity memberEntity = memberDao.selectById(memberId); String symbols = symbolOut+"/USDT"; - ThreadPoolUtils.sendWholeForceClosingPrice(symbols, memberEntity); + //ThreadPoolUtils.sendWholeForceClosingPrice(symbols, memberEntity); + // 全仓爆仓 + ThreadPoolUtils.sendWholePrice(memberEntity.getId()); // 加币 // 查询合约账户 @@ -876,7 +972,9 @@ //更新合约全仓模式下的订单权益 String symbolIns = symbolIn+"/USDT"; - ThreadPoolUtils.sendWholeForceClosingPrice(symbolIns, memberEntity); + //ThreadPoolUtils.sendWholeForceClosingPrice(symbolIns, memberEntity); + // 全仓爆仓 + ThreadPoolUtils.sendWholePrice(memberEntity.getId()); //添加币币资金划转历史记录 MemberAccountMoneyChange memberAccountRecord = new MemberAccountMoneyChange(); @@ -894,6 +992,124 @@ return Result.ok(MessageSourceUtils.getString("member_service_0006")); } + + @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()); + 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()); + 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()); + 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()); + 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")); + + } -- Gitblit v1.9.1