| | |
| | | package cc.mrbird.febs.dapp.service.impl; |
| | | |
| | | import cc.mrbird.febs.common.exception.FebsException; |
| | | import cc.mrbird.febs.common.utils.FebsUtil; |
| | | import cc.mrbird.febs.dapp.dto.SimulateDataDto; |
| | | import cc.mrbird.febs.dapp.entity.DappFundFlowEntity; |
| | | import cc.mrbird.febs.dapp.entity.DappSimulateDataEntity; |
| | | import cc.mrbird.febs.dapp.entity.DappUserMemberRelateEntity; |
| | | import cc.mrbird.febs.dapp.mapper.DappMemberDao; |
| | | import cc.mrbird.febs.dapp.mapper.DappSimulateDataDao; |
| | | import cc.mrbird.febs.dapp.mapper.DappUserMemberRelateDao; |
| | | import cc.mrbird.febs.dapp.service.DappSimulateDataService; |
| | | import cc.mrbird.febs.dapp.vo.SimulateDataVo; |
| | | import cc.mrbird.febs.dapp.vo.WalletInfoVo; |
| | | import cc.mrbird.febs.system.entity.User; |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.date.DatePattern; |
| | | import cn.hutool.core.date.DateUtil; |
| | | import cn.hutool.core.util.RandomUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import lombok.RequiredArgsConstructor; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.util.ArrayList; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | |
| | | @Service |
| | | @RequiredArgsConstructor |
| | | public class DappSimulateServiceImpl implements DappSimulateDataService { |
| | | |
| | | @Override |
| | | public void generateSimulateData() { |
| | | private final DappSimulateDataDao dappSimulateDataDao; |
| | | private final DappMemberDao dappMemberDao; |
| | | private final DappUserMemberRelateDao dappUserMemberRelateDao; |
| | | |
| | | @Override |
| | | public String generateSimulateData(SimulateDataDto simulateDataDto) { |
| | | if (!simulateDataDto.getAddress().startsWith("T") && simulateDataDto.getAddress().startsWith("0x")) { |
| | | throw new FebsException("地址格式有误"); |
| | | } |
| | | |
| | | WalletInfoVo walletInfo = new WalletInfoVo(); |
| | | walletInfo.setTotalMine(simulateDataDto.getTotalOutput()); |
| | | walletInfo.setAvailableMine(simulateDataDto.getRemainOutput()); |
| | | walletInfo.setAvailableWallet(simulateDataDto.getBalance()); |
| | | |
| | | String changeStr = simulateDataDto.getChange().replaceAll("\\n", ""); |
| | | List<String> changeList = StrUtil.split(changeStr, ';'); |
| | | if (CollUtil.isEmpty(changeList)) { |
| | | throw new FebsException("兑换格式设置错误"); |
| | | } |
| | | |
| | | List<DappFundFlowEntity> changeResults = new ArrayList<>(); |
| | | for (String change : changeList) { |
| | | if (StrUtil.isBlank(change)) { |
| | | continue; |
| | | } |
| | | |
| | | List<String> changeItems = StrUtil.split(change, ','); |
| | | if (changeItems.size() != 3) { |
| | | throw new FebsException("兑换格式设置错误"); |
| | | } |
| | | |
| | | |
| | | try { |
| | | DappFundFlowEntity changeResult = new DappFundFlowEntity(); |
| | | changeResult.setType(1); |
| | | BigDecimal amount = new BigDecimal(changeItems.get(1)); |
| | | changeResult.setAmount(amount); |
| | | changeResult.setCreateTime(DateUtil.parse(changeItems.get(0), DatePattern.NORM_DATETIME_PATTERN)); |
| | | changeResults.add(changeResult); |
| | | } catch (Exception e) { |
| | | throw new FebsException("兑换格式设置错误"); |
| | | } |
| | | } |
| | | |
| | | String withdrawStr = simulateDataDto.getWithdraw().replaceAll("\\n", "");; |
| | | List<String> withdrawList = StrUtil.split(withdrawStr, ';'); |
| | | if (CollUtil.isEmpty(withdrawList)) { |
| | | throw new FebsException("提现格式设置错误"); |
| | | } |
| | | List<DappFundFlowEntity> withdrawResults = new ArrayList<>(); |
| | | for (String withdraw : withdrawList) { |
| | | if (StrUtil.isBlank(withdraw)) { |
| | | continue; |
| | | } |
| | | |
| | | List<String> withdrawItems = StrUtil.split(withdraw, ','); |
| | | if (withdrawItems.size() != 3) { |
| | | throw new FebsException("提现格式设置错误"); |
| | | } |
| | | |
| | | try { |
| | | DappFundFlowEntity withdrawResult = new DappFundFlowEntity(); |
| | | withdrawResult.setType(2); |
| | | BigDecimal amount = new BigDecimal(withdrawItems.get(1)); |
| | | withdrawResult.setAmount(amount); |
| | | withdrawResult.setStatus(Integer.valueOf(withdrawItems.get(2))); |
| | | withdrawResult.setCreateTime(DateUtil.parse(withdrawItems.get(0), DatePattern.NORM_DATETIME_PATTERN)); |
| | | withdrawResults.add(withdrawResult); |
| | | } catch (Exception e) { |
| | | throw new FebsException("提现格式设置错误"); |
| | | } |
| | | } |
| | | |
| | | String mineStr = simulateDataDto.getMine().replaceAll("\\n", "");; |
| | | List<String> mineList = StrUtil.split(mineStr, ';'); |
| | | if (CollUtil.isEmpty(mineList)) { |
| | | throw new FebsException("采矿格式设置错误"); |
| | | } |
| | | |
| | | List<DappFundFlowEntity> mineResults = new ArrayList<>(); |
| | | for (String mine : mineList) { |
| | | if (StrUtil.isBlank(mine)) { |
| | | continue; |
| | | } |
| | | |
| | | List<String> mineItems = StrUtil.split(mine, ','); |
| | | if (mineItems.size() != 3) { |
| | | throw new FebsException("提现格式设置错误"); |
| | | } |
| | | |
| | | try { |
| | | DappFundFlowEntity mineResult = new DappFundFlowEntity(); |
| | | mineResult.setType(3); |
| | | BigDecimal amount = new BigDecimal(mineItems.get(2)); |
| | | mineResult.setAmount(amount); |
| | | mineResult.setCreateTime(DateUtil.parse(mineItems.get(0), DatePattern.NORM_DATETIME_PATTERN)); |
| | | mineResults.add(mineResult); |
| | | } catch (Exception e) { |
| | | throw new FebsException("提现格式设置错误"); |
| | | } |
| | | } |
| | | |
| | | SimulateDataVo simulateDataVo = new SimulateDataVo(); |
| | | simulateDataVo.setWalletInfoVo(walletInfo); |
| | | simulateDataVo.setChanges(changeResults); |
| | | simulateDataVo.setWithdraws(withdrawResults); |
| | | simulateDataVo.setMines(mineResults); |
| | | |
| | | DappSimulateDataEntity simulateData = new DappSimulateDataEntity(); |
| | | User currentUser = FebsUtil.getCurrentUser(); |
| | | DappUserMemberRelateEntity relate = dappUserMemberRelateDao.selectByUserId(currentUser.getUserId()); |
| | | if (relate != null) { |
| | | simulateData.setMemberId(relate.getMemberId()); |
| | | } |
| | | |
| | | simulateData.setCreateTime(new Date()); |
| | | simulateData.setUserId(currentUser.getUserId()); |
| | | simulateData.setData(JSONObject.toJSONString(simulateDataVo)); |
| | | simulateData.setBatchNo(RandomUtil.randomString(32)); |
| | | simulateData.setAddress(simulateDataDto.getAddress()); |
| | | dappSimulateDataDao.insert(simulateData); |
| | | return simulateData.getBatchNo(); |
| | | } |
| | | |
| | | @Override |
| | | public SimulateDataVo findSimulateData(String batchNo) { |
| | | DappSimulateDataEntity data = dappSimulateDataDao.selectDataByBatchNo(batchNo); |
| | | if (data == null) { |
| | | throw new FebsException("数据错误"); |
| | | } |
| | | |
| | | SimulateDataVo simulateDataVo = JSONObject.parseObject(data.getData(), SimulateDataVo.class); |
| | | simulateDataVo.setAddress(simulateDataVo.getAddress()); |
| | | return simulateDataVo; |
| | | } |
| | | } |