package com.jxy.marketall.service.impl;

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.jxy.common.core.text.Convert;
import com.jxy.common.core.utils.DateUtils;
import com.jxy.common.core.utils.StringUtils;
import com.jxy.common.core.web.Result.AjaxResult;
import com.jxy.common.core.web.Result.PageResult;
import com.jxy.common.redis.service.RedisService;
import com.jxy.marketall.dto.*;
import com.jxy.marketall.entity.*;
import com.jxy.marketall.enums.ETHV2RedisName;
import com.jxy.marketall.mapper.ETHMapper;
import com.jxy.marketall.mapper.ETHV2Mapper;
import com.jxy.marketall.mapper.EthBigTxMapper;
import com.jxy.marketall.service.EthBigTxService;
import com.jxy.marketall.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;


/**
 * @Description EthBigTx查询业务处理
 * @Date 2022/6/21  11:51
 * @Author 杜懿
 */
@Service
public class EthBigTxServiceImpl extends ServiceImpl<EthBigTxMapper, EthBigTx> implements EthBigTxService {

    @Autowired
    private RedisService redisService;

    @Autowired
    private ETHMapper ethMapper;

    @Autowired
    private ETHV2Mapper ethv2Mapper;



    @Override
    public AjaxResult getETHVolumeList(QueryVo vo) {

        List<EthVolumeDto> list = redisService.getCacheList("ETHVolume");

        if(list.size() < 1){
            return AjaxResult.success(list);
        }

        if(StringUtils.isNotNull(vo.getStart())){
            Date start = DateUtils.parseCNDate(vo.getStart());
            System.out.println(start);
            list = list.stream().filter(dto -> (dto.getDate().after(start) || dto.getDate().equals(start))).collect(Collectors.toList());
        }

        if(StringUtils.isNotNull(vo.getEnd())){
            Date end = DateUtils.parseCNDate(vo.getEnd());
            list = list.stream().filter(dto -> (dto.getDate().before(end) || dto.getDate().equals(end))).collect(Collectors.toList());
        }

        //if(StringUtils.isNotNull(vo)){
        //    if(vo.getSma() > 0){
        //        list = getSMA(list,vo.getSma());
        //    }
        //}


        return AjaxResult.success(list);
    }

    @Override
    public AjaxResult getETHFeesList(QueryVo vo) {

        if(StringUtils.isEmpty(vo.getEnd()) && StringUtils.isEmpty(vo.getStart())){
            //默认查询最后一条数据前一个月的数据
            ETHFeesDto ethFeesDto = ethMapper.selectLastETHFees();

            if(StringUtils.isNull(ethFeesDto)){
                return AjaxResult.success(new ArrayList<>());
            }

            Calendar startCalendar = new GregorianCalendar();
            startCalendar.setTime(ethFeesDto.getDate());
            startCalendar.add(startCalendar.HOUR,-8);
            startCalendar.add(startCalendar.MONTH,-1);

            vo.setStart(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, startCalendar.getTime()));
        }

        List<ETHFeesDto> list = ethMapper.getETHFeesList(vo);


        return AjaxResult.success(list);
    }

    @Override
    public AjaxResult findEthBigTxByETHVo(ETHVo ethVo) {

        boolean nullFlag = true;

        if (!StringUtils.isEmpty(ethVo.getTotal())){

            //去除逗号
            ethVo.setTotal(ethVo.getTotal().replaceAll(",",""));
            //校验字符合法性
            //校验是否为字母或数字
            if(!ethVo.getTotal().matches("^[A-Za-z0-9]+$")){
                //total不是数字或者字母  1、输入了浮点数  2、输入非法字符 直接返回空数据
                if(!ethVo.getTotal().matches("^(-?\\d+)(\\.\\d+)?$")){
                    ArrayList<Object> list = new ArrayList<>();
                    PageResult result = new PageResult();
                    result.setCount(0L);
                    result.setTotalPage(0L);
                    result.setData(list);
                    return AjaxResult.success("模糊搜索传参格式有误，仅支持数字字母",list);
                }
            }
            Double amount = Convert.toDouble(ethVo.getTotal());
            if(amount != null){
                ethVo.setAmount(amount);
            }else{
                if(ethVo.getTotal().length()>=64){
                    ethVo.setTxId(ethVo.getTotal());
                }else{
                    ethVo.setAddress(ethVo.getTotal());
                   }
            }
            nullFlag = false;
        }
        if(StringUtils.isNotNull(ethVo.getAddress()) || StringUtils.isNotNull(ethVo.getAmount()) || StringUtils.isNotNull(ethVo.getTxId())){
            nullFlag = false;
        }

        if(StringUtils.isEmpty(ethVo.getStart()) && StringUtils.isEmpty(ethVo.getEnd()) && nullFlag){
            //没传时间 默认查询最后一条数据之前8小时的数据
            EthBigTx lastData = ethMapper.selectLastETHBigTx();

            Calendar calendar = new GregorianCalendar();

            calendar.setTime(lastData.getDate());
            //selectLastETHBigTx查询出来的时间是数据库时间+8小时
            calendar.add(calendar.HOUR,-16);

            ethVo.setStart(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,calendar.getTime()));

        }

        Page<EthBigTxDto> page = new Page<>(ethVo.getPage(),ethVo.getLimit());

        IPage<EthBigTxDto> list = ethMapper.selectETHBigTxList(page, ethVo);

        PageResult result = new PageResult();


        result.setCount(list.getTotal());
        result.setTotalPage(list.getPages());
        result.setData(list.getRecords());

        return AjaxResult.success(result);
    }

    @Override
    public AjaxResult findEthTopAddrChangeByETHVo(ETHVo ethVo) {

        boolean nullFlag = true;

        if (!StringUtils.isEmpty(ethVo.getTotal())){
            ethVo.setTotal(ethVo.getTotal().replaceAll(",",""));
            //校验字符合法性
            //校验是否为字母或数字
            if(!ethVo.getTotal().matches("^[A-Za-z0-9]+$")){
                //total不是数字或者字母  1、输入了浮点数  2、输入非法字符 直接返回空数据
                if(!ethVo.getTotal().matches("^(-?\\d+)(\\.\\d+)?$")){

                    PageResult result = new PageResult();
                    result.setCount(0L);
                    result.setTotalPage(0L);
                    result.setData(new ArrayList<>());
                    return AjaxResult.success("模糊搜索传参格式有误，仅支持数字字母",result);
                }
            }
            Double amount = Convert.toDouble(ethVo.getTotal());
            if(amount != null){
                ethVo.setAmount(amount);
            }else{
                if(ethVo.getTotal().length()>=64){
                    ethVo.setTxId(ethVo.getTotal());
                }else{
                    ethVo.setAddress(ethVo.getTotal());
                }
            }
            nullFlag = false;
        }
        if(StringUtils.isNotNull(ethVo.getAddress()) || StringUtils.isNotNull(ethVo.getAmount()) || StringUtils.isNotNull(ethVo.getTxId())){
            nullFlag = false;
        }

        if(StringUtils.isEmpty(ethVo.getStart()) && StringUtils.isEmpty(ethVo.getEnd()) && nullFlag){
            //没传时间 默认查询最后一条数据之前8小时的数据

            EthTopAddrChangeDto lastData = ethMapper.selectLastETHTopAddrChange();

            Calendar calendar = new GregorianCalendar();

            calendar.setTime(lastData.getDate());
            //selectLastETHBigTx查询出来的时间是数据库时间+8小时
            calendar.add(calendar.HOUR,-16);

            ethVo.setStart(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,calendar.getTime()));

        }

        Page<EthTopAddrChangeDto> page = new Page<>(ethVo.getPage(),ethVo.getLimit());

        IPage<EthTopAddrChangeDto> list = ethMapper.selectEthTopAddrChangeList(page, ethVo);

        PageResult result = new PageResult();

        List<EthTopAddrChangeDto> records = list.getRecords();

        result.setCount(list.getTotal());
        result.setTotalPage(list.getPages());
        result.setData(records);

        return AjaxResult.success(result);
    }

    @Override
    public AjaxResult findEthBigTxByTime(ETHBigTxCountVo vo) {


        if(StringUtils.isEmpty(vo.getStart()) && StringUtils.isEmpty(vo.getEnd())){

            EthBigTx lastEthBigTx = ethMapper.selectLastETHBigTx();
            Date date = lastEthBigTx.getDate();//北京时间
            vo.setEnd(DateUtils.dateTime(date));
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(date);
            //calendar.add(calendar.HOUR,-8);
            calendar.add(calendar.MONTH,-1);
            vo.setStart(DateUtils.dateTime(calendar.getTime()));
            vo.setType("day");

        }

        List<CountDto> list = redisService.getCacheList("ethBigTxDayCount");

        if(list.size() == 0){
            return AjaxResult.success("该时间范围内没有数据",list);
        }

        if(StringUtils.isNotNull(vo.getStart())){
            Date start = DateUtils.parseCNDate(vo.getStart());
            System.out.println(start);
            list = list.stream().filter(dto -> (dto.getDataTime().after(start) || dto.getDataTime().equals(start))).collect(Collectors.toList());
        }

        if(StringUtils.isNotNull(vo.getEnd())){
            Date end = DateUtils.parseCNDate(vo.getEnd());
            list = list.stream().filter(dto -> (dto.getDataTime().before(end) || dto.getDataTime().equals(end))).collect(Collectors.toList());
        }


        return AjaxResult.success(list);
    }

    @Override
    public AjaxResult findUSDTBigTxByUSDTVo(ETHVo vo) {

        //boolean amountTag = false;
        boolean paramTag = false;
        int page = vo.getPage();
        int pageSize = vo.getLimit();
        USDTVo usdtVo = new USDTVo();

        if(StringUtils.isNull(vo.getAmount())){
            vo.setAmount(0.0);
        }

        if (!StringUtils.isEmpty(vo.getTotal())){
            vo.setTotal(vo.getTotal().replaceAll(",",""));
            //校验字符合法性
            //校验是否为字母或数字
            if(!vo.getTotal().matches("^[A-Za-z0-9]+$")){
                //total不是数字或者字母  1、输入了浮点数  2、输入非法字符 直接返回空数据
                if(!vo.getTotal().matches("^(-?\\d+)(\\.\\d+)?$")){
                    PageResult result = new PageResult();
                    result.setCount(0L);
                    result.setTotalPage(0L);
                    result.setData(new ArrayList<>());
                    return AjaxResult.success("模糊搜索传参格式有误，仅支持数字字母",result);
                }
            }

            Double amount = Convert.toDouble(vo.getTotal());
            if(amount != null){
                //ethBigTxLambdaQueryWrapper.ge(EthBigTx::getAmount, amount*Math.pow(10,18));
                usdtVo.setAmount(amount);
                //amountTag = true;
            }
            else{
                if(vo.getTotal().length()>=64){
                    usdtVo.setTxId(vo.getTotal());
                }
                else{
                    usdtVo.setAddress(vo.getTotal());
                }
            }
        }else if(StringUtils.isEmpty(vo.getAddress()) && vo.getAmount() == 0.0 && StringUtils.isEmpty(vo.getTxId()) ){
            paramTag = true;
        }

        List<USDBigTxDto> list = new ArrayList<>();

        if(StringUtils.isEmpty(vo.getStart()) && StringUtils.isEmpty(vo.getEnd()) && paramTag){
            //起止时间和条件都没传 则默认查最新一条数据的当前时间前两天
            list = redisService.getCacheList("usdtBigTx2Day");
        }else{
            //传了时间或者条件直接从数据库拿数据
            list = ethMapper.selectUSDTBigTxList(usdtVo);
        }

        //去掉异常数据 大于10的20次方的数据 并且筛选大于amount的值
        list = list.stream().filter(e -> (e.getAmount() <= Math.pow(10, 9) && e.getAmount() >= vo.getAmount())).collect(Collectors.toList());

        if(list.size() == 0)
        {
            PageResult result = new PageResult();
            result.setCount(0L);
            result.setTotalPage(0L);
            result.setData(list);
            return AjaxResult.success("当前条件下没有查到数据，请修改参数信息", result);
        }

        //分页
        PageResult result = new PageResult();
        int count = list.size();
        int totalPage = count/pageSize;
        if(count % pageSize != 0){
            totalPage++;
        }
        result.setCount((long) count);
        result.setTotalPage((long) totalPage);
        list = list.stream().skip((page - 1) * pageSize).limit(pageSize).collect(Collectors.toList());
        result.setData(list);

        return AjaxResult.success(result);
    }

    @Override
    public AjaxResult findUSDTBigTxByTime(ETHBigTxCountVo vo) {

        List<CountDto> list = redisService.getCacheList("USDTBigTxDayCount");

        if(list.size() == 0){
            return AjaxResult.success("该时间范围内没有数据",list);
        }

        //缺失日期补齐
        list = dataMunging(list);

        if(StringUtils.isEmpty(vo.getStart()) && StringUtils.isEmpty(vo.getEnd())){
            //起止时间都没传 则默认查最新一条数据的当前时间前一个月
            Date date = list.get(list.size()-1).getDataTime();//北京时间
            vo.setEnd(DateUtils.dateTime(date));
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(date);
            calendar.add(calendar.MONTH,-1);
            vo.setStart(DateUtils.dateTime(calendar.getTime()));
        }

        if(StringUtils.isNotEmpty(vo.getStart())){
            Date start = DateUtils.parseDate(vo.getStart());
            list = list.stream().filter(dto -> (dto.getDataTime().after(start) || dto.getDataTime().equals(start))).collect(Collectors.toList());
        }

        if(StringUtils.isNotEmpty(vo.getEnd())){
            Date end = DateUtils.parseDate(vo.getEnd());
            list = list.stream().filter(dto -> (dto.getDataTime().before(end) || dto.getDataTime().equals(end))).collect(Collectors.toList());
        }

        return AjaxResult.success(list);
    }

    @Override
    public AjaxResult findUSDCBigTxByUSDCVo(ETHVo vo) {

        //boolean amountTag = false;
        boolean paramTag = false;
        int page = vo.getPage();
        int pageSize = vo.getLimit();
        USDCVo usdcVo = new USDCVo();

        if(StringUtils.isNull(vo.getAmount())){
            vo.setAmount(0.0);
        }

        if (!StringUtils.isEmpty(vo.getTotal())){
            vo.setTotal(vo.getTotal().replaceAll(",",""));
            //校验字符合法性
            //校验是否为字母或数字
            if(!vo.getTotal().matches("^[A-Za-z0-9]+$")){
                //total不是数字或者字母  1、输入了浮点数  2、输入非法字符 直接返回空数据
                if(!vo.getTotal().matches("^(-?\\d+)(\\.\\d+)?$")){
                    PageResult result = new PageResult();
                    result.setCount(0L);
                    result.setTotalPage(0L);
                    result.setData(new ArrayList<>());
                    return AjaxResult.success("模糊搜索传参格式有误，仅支持数字字母",result);
                }
            }
            Double amount = Convert.toDouble(vo.getTotal());
            if(amount != null){
                //ethBigTxLambdaQueryWrapper.ge(EthBigTx::getAmount, amount*Math.pow(10,18));
                usdcVo.setAmount(amount);
                //amountTag = true;
            }
            else{
                if(vo.getTotal().length()>=64){
                    usdcVo.setTxId(vo.getTotal());
                }
                else{
                    usdcVo.setAddress(vo.getTotal());
                }
            }
        }else if(StringUtils.isEmpty(vo.getAddress()) && vo.getAmount() == 0.0 && StringUtils.isEmpty(vo.getTxId()) ){
            paramTag = true;
        }


        List<USDBigTxDto> list = new ArrayList<>();
        if(StringUtils.isEmpty(vo.getStart()) && StringUtils.isEmpty(vo.getEnd()) && paramTag){
            //起止时间和条件都没传 则默认查最新一条数据的当前时间前两天
            list = redisService.getCacheList("usdcBigTx2Day");
        }else{
            //传了时间或者条件直接从数据库拿数据
            list = ethMapper.selectUSDCBigTxList(usdcVo);
        }

        //去掉异常数据 大于10的20次方的数据 并且筛选大于amount的值
        list = list.stream().filter(e -> (e.getAmount() <= Math.pow(10, 9) && e.getAmount() >= vo.getAmount())).collect(Collectors.toList());

        if(list.size() == 0)
        {
            PageResult result = new PageResult();
            result.setCount(0L);
            result.setTotalPage(0L);
            result.setData(list);
            return AjaxResult.success("当前条件下没有查到数据，请修改参数信息", result);
        }

        //分页
        PageResult result = new PageResult();
        int count = list.size();
        int totalPage = count/pageSize;
        if(count % pageSize != 0){
            totalPage++;
        }
        result.setCount((long) count);
        result.setTotalPage((long) totalPage);
        list = list.stream().skip((page - 1) * pageSize).limit(pageSize).collect(Collectors.toList());
        result.setData(list);

        return AjaxResult.success(result);
    }

    @Override
    public AjaxResult findUSDCBigTxByTime(ETHBigTxCountVo vo) {

        List<CountDto> list = redisService.getCacheList("USDCBigTxDayCount");

        if(list.size() == 0){
            return AjaxResult.success("该时间范围内没有数据",list);
        }

        //缺失日期补齐
        list = dataMunging(list);

        if(StringUtils.isEmpty(vo.getStart()) && StringUtils.isEmpty(vo.getEnd())){
            //起止时间都没传 则默认查最新一条数据的当前时间前一个月
            Date date = list.get(list.size()-1).getDataTime();//北京时间
            vo.setEnd(DateUtils.dateTime(date));
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(date);
            calendar.add(calendar.MONTH,-1);
            vo.setStart(DateUtils.dateTime(calendar.getTime()));
        }

        if(StringUtils.isNotEmpty(vo.getStart())){
            Date start = DateUtils.parseDate(vo.getStart());
            list = list.stream().filter(dto -> (dto.getDataTime().after(start) || dto.getDataTime().equals(start))).collect(Collectors.toList());
        }

        if(StringUtils.isNotEmpty(vo.getEnd())){
            Date end = DateUtils.parseDate(vo.getEnd());
            list = list.stream().filter(dto -> (dto.getDataTime().before(end) || dto.getDataTime().equals(end))).collect(Collectors.toList());
        }


        return AjaxResult.success(list);
    }

    @Override
    public AjaxResult findBUSDBigTxByUSDTVo(ETHVo vo) {

        boolean paramTag = false;
        int page = vo.getPage();
        int pageSize = vo.getLimit();
        USDTVo usdtVo = new USDTVo();

        if(StringUtils.isNull(vo.getAmount())){
            vo.setAmount(0.0);
        }

        if (!StringUtils.isEmpty(vo.getTotal())){
            vo.setTotal(vo.getTotal().replaceAll(",",""));
            //校验字符合法性
            //校验是否为字母或数字
            if(!vo.getTotal().matches("^[A-Za-z0-9]+$")){
                //total不是数字或者字母  1、输入了浮点数  2、输入非法字符 直接返回空数据
                if(!vo.getTotal().matches("^(-?\\d+)(\\.\\d+)?$")){
                    PageResult result = new PageResult();
                    result.setCount(0L);
                    result.setTotalPage(0L);
                    result.setData(new ArrayList<>());
                    return AjaxResult.success("模糊搜索传参格式有误，仅支持数字字母",result);
                }
            }
            Double amount = Convert.toDouble(vo.getTotal());
            if(amount != null){
                usdtVo.setAmount(amount);
            }
            else{
                if(vo.getTotal().length()>=64){
                    usdtVo.setTxId(vo.getTotal());
                }
                else{
                    usdtVo.setAddress(vo.getTotal());
                }
            }
        }
        else if(StringUtils.isEmpty(vo.getAddress()) && vo.getAmount() == 0.0 && StringUtils.isEmpty(vo.getTxId()) ){
            paramTag = true;
        }


        List<USDBigTxDto> list = new ArrayList<>();
        if(StringUtils.isEmpty(vo.getStart()) && StringUtils.isEmpty(vo.getEnd()) && paramTag){
            //起止时间和条件都没传 则默认查最新一条数据的当前时间前两天
            list = redisService.getCacheList("busdBigTx2Day");
        }else{
            //传了时间或者条件直接从数据库拿数据
            list = ethMapper.selectBUSDBigTxList(usdtVo);
        }

        //去掉异常数据 大于10的20次方的数据 并且筛选大于amount的值
        list = list.stream().filter(e -> (e.getAmount() <= Math.pow(10, 9) && e.getAmount() >= vo.getAmount())).collect(Collectors.toList());

        if(list.size() == 0)
        {
            PageResult result = new PageResult();
            result.setCount(0L);
            result.setTotalPage(0L);
            result.setData(list);
            return AjaxResult.success("当前条件下没有查到数据，请修改参数信息", result);
        }

        //分页
        PageResult result = new PageResult();
        int count = list.size();
        int totalPage = count/pageSize;
        if(count % pageSize != 0){
            totalPage++;
        }
        result.setCount((long) count);
        result.setTotalPage((long) totalPage);
        list = list.stream().skip((page - 1) * pageSize).limit(pageSize).collect(Collectors.toList());
        result.setData(list);

        return AjaxResult.success(result);
    }

    @Override
    public AjaxResult findBUSDBigTxByTime(ETHBigTxCountVo vo) {

        List<CountDto> list = redisService.getCacheList("BUSDBigTxDayCount");

        if(list.size() == 0){
            return AjaxResult.success("该时间范围内没有数据",list);
        }

        //缺失日期补齐
        list = dataMunging(list);


        if(StringUtils.isEmpty(vo.getStart()) && StringUtils.isEmpty(vo.getEnd())){
            //起止时间都没传 则默认查最新一条数据的当前时间前一个月
            Date date = list.get(list.size()-1).getDataTime();//北京时间
            vo.setEnd(DateUtils.dateTime(date));
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(date);
            calendar.add(calendar.MONTH,-1);
            vo.setStart(DateUtils.dateTime(calendar.getTime()));
        }

        if(StringUtils.isNotEmpty(vo.getStart())){
            Date start = DateUtils.parseDate(vo.getStart());
            list = list.stream().filter(dto -> (dto.getDataTime().after(start) || dto.getDataTime().equals(start))).collect(Collectors.toList());
        }

        if(StringUtils.isNotEmpty(vo.getEnd())){
            Date end = DateUtils.parseDate(vo.getEnd());
            list = list.stream().filter(dto -> (dto.getDataTime().before(end) || dto.getDataTime().equals(end))).collect(Collectors.toList());
        }

        return AjaxResult.success(list);
    }

    @Override
    public AjaxResult getUSDTVolumeList(QueryVo queryVo) {
        List<USDVolumeDto> list = new ArrayList<>();
        if(!StringUtils.isEmpty(queryVo.getEnd()) || !StringUtils.isEmpty(queryVo.getStart())){
           list = ethMapper.getUSDTVolumeList(queryVo);
        }else{
            list = redisService.getCacheList("USDTVolumeLastMonth");
        }

        if(list.size()<1){
            return AjaxResult.success(list);
        }

        List<USDVolumeDto> returnList = new ArrayList<>();
        for (int i =0 ;i < list.size();i++){
            if(list.get(i).getVolume() < Math.pow(10,30)){
                returnList.add(list.get(i));
            }
        }


        //todo 改成从缓存中拿
        return AjaxResult.success(returnList);
    }

    @Override
    public AjaxResult getUSDCVolumeList(QueryVo queryVo) {
        List<USDVolumeDto> list = new ArrayList<>();
        if(!StringUtils.isEmpty(queryVo.getEnd()) || !StringUtils.isEmpty(queryVo.getStart())){
            list = ethMapper.getUSDCVolumeList(queryVo);
        }else{
            list = redisService.getCacheList("USDCVolumeLastMonth");
        }

        if(list.size()<1){
            return AjaxResult.success(list);
        }

        List<USDVolumeDto> returnList = new ArrayList<>();
        for (int i =0 ;i < list.size();i++){
            if(list.get(i).getVolume() < Math.pow(10,30)){
                returnList.add(list.get(i));
            }
        }

        //todo 改成从缓存中拿
        return AjaxResult.success(returnList);
    }

    @Override
    public AjaxResult getBUSDVolumeList(QueryVo queryVo) {
        List<USDVolumeDto> list = new ArrayList<>();
        if(!StringUtils.isEmpty(queryVo.getEnd()) || !StringUtils.isEmpty(queryVo.getStart())){
            list = ethMapper.getBUSDVolumeList(queryVo);
        }else{
            list = redisService.getCacheList("BUSDVolumeLastMonth");
        }

        if(list.size()<1){
            return AjaxResult.success(list);
        }

        List<USDVolumeDto> returnList = new ArrayList<>();
        for (int i =0 ;i < list.size();i++){
            if(list.get(i).getVolume() < Math.pow(10,30)){
                returnList.add(list.get(i));
            }
        }

        //todo 改成从缓存中拿
        return AjaxResult.success(returnList);
    }


    @Override
    public AjaxResult getTronUSDCVolumeList(QueryVo queryVo) {
        List<TronUSDCVolume> list = new ArrayList<>();
        if(!StringUtils.isEmpty(queryVo.getEnd()) || !StringUtils.isEmpty(queryVo.getStart())){
            list = ethMapper.getTronUSDCVolumeList(queryVo);
        }else{
            list = redisService.getCacheList("TronUSDCVolumeLastMonth");
        }

        if(list.size()<1){
            return AjaxResult.success(list);
        }
        return AjaxResult.success(list);
    }

    @Override
    public AjaxResult getTronUSDTVolumeList(QueryVo queryVo) {
        List<TronUSDTVolume> list = new ArrayList<>();
        if(!StringUtils.isEmpty(queryVo.getEnd()) || !StringUtils.isEmpty(queryVo.getStart())){
            list = ethMapper.getTronUSDTVolumeList(queryVo);
        }else{
            list = redisService.getCacheList("TronUSDTVolumeLastMonth");
        }

        if(list.size()<1){
            return AjaxResult.success(list);
        }
        return AjaxResult.success(list);
    }

    @Override
    public AjaxResult findTronUSDTBigTxByVo(ETHVo vo) {
        boolean amountTag = false;
        boolean paramTag = false;

        TronUSDTVo usdtVo = new TronUSDTVo();

        if (!StringUtils.isEmpty(vo.getTotal())){
            vo.setTotal(vo.getTotal().replaceAll(",",""));
            //校验字符合法性
            //校验是否为字母或数字
            if(!vo.getTotal().matches("^[A-Za-z0-9]+$")){
                //total不是数字或者字母  1、输入了浮点数  2、输入非法字符 直接返回空数据
                if(!vo.getTotal().matches("^(-?\\d+)(\\.\\d+)?$")){
                    PageResult result = new PageResult();
                    result.setCount(0L);
                    result.setTotalPage(0L);
                    result.setData(new ArrayList<>());
                    return AjaxResult.success("模糊搜索传参格式有误，仅支持数字字母",result);
                }
            }
            Double amount = Convert.toDouble(vo.getTotal());
            if(amount != null){
                usdtVo.setAmount(amount);
                amountTag = true;
            }
            else{
                if(vo.getTotal().length()>=64){
                    usdtVo.setTxId(vo.getTotal());
                }
                else if(vo.getTotal().length()<12){
                    usdtVo.setTag(vo.getTotal());
                }else {
                    usdtVo.setAddress(vo.getTotal());
                }
            }
        }
        else{
            //所有参数都没传则返回空值
            //return AjaxResult.success("传参有误，请检查参数",null);
            paramTag = true;
        }

        if(StringUtils.isNotEmpty(vo.getStart()) || StringUtils.isNotEmpty(vo.getEnd())){
            if(StringUtils.isNotEmpty(vo.getEnd())){
                Date end = DateUtils.parseDate(vo.getEnd());
                Calendar calendar = new GregorianCalendar();
                calendar.setTime(end);
                calendar.add(calendar.HOUR,-8);
                Date start = calendar.getTime();

                usdtVo.setStart(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,start));

                calendar.add(calendar.DATE,+1);

                end = calendar.getTime();
                usdtVo.setEnd(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,end));
            }

        }else if((amountTag || paramTag) && StringUtils.isEmpty(vo.getStart()) && StringUtils.isEmpty(vo.getEnd())){
            //起止时间都没传 则默认查最新一条数据的当前时间前两天
            TronUSDTBigTx tronUSDTBigTx = ethMapper.selectLastTronUSDTBigTx();
            Date end = tronUSDTBigTx.getDate();
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(end);
            calendar.add(calendar.DATE,-2);

            usdtVo.setEnd(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,end));
            usdtVo.setStart(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,calendar.getTime()));
        }
        Page<TronUSDTBigTx> page = new Page<>(vo.getPage(),vo.getLimit());
        IPage<TronUSDTBigTx> list = ethMapper.selectTronUSDTBigTxList(page, usdtVo);

        list.getRecords().stream().forEach(e ->{
            if("unknown".equals(e.getFromTag())){
                e.setFromTag("");
            }
            if("unknown".equals(e.getToTag())){
                e.setToTag("");
            }
        });

        PageResult result = new PageResult();

        result.setCount(list.getTotal());
        result.setTotalPage(list.getPages());
        result.setData(list.getRecords());

        return AjaxResult.success(result);
    }

    @Override
    public AjaxResult findTronUSDTBigTxByTime(QueryVo vo) {
        //if(StringUtils.isEmpty(vo.getStart()) && StringUtils.isEmpty(vo.getEnd())){
        //        //起止时间都没传 则默认查最新一条数据的当前时间前一个月
        //
        //    TronUSDTBigTx tronUSDTBigTx = ethMapper.selectLastTronUSDTBigTx();
        //        Date date = tronUSDTBigTx.getDate();//北京时间
        //        vo.setEnd(DateUtils.dateTime(date));
        //        Calendar calendar = new GregorianCalendar();
        //        calendar.setTime(date);
        //        calendar.add(calendar.MONTH,-1);
        //        vo.setStart(DateUtils.dateTime(calendar.getTime()));
        //}

        List<CountDto> list = redisService.getCacheList("TronUSDTBigTxDayCount");

        if(list.size() == 0){
            return AjaxResult.success("该时间范围内没有数据",list);
        }

        //缺失日期补齐
        list = dataMunging(list);

        if(StringUtils.isEmpty(vo.getStart()) && StringUtils.isEmpty(vo.getEnd())){
            //起止时间都没传 则默认查最新一条数据的当前时间前一个月
            Date date = list.get(list.size()-1).getDataTime();//北京时间
            vo.setEnd(DateUtils.dateTime(date));
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(date);
            calendar.add(calendar.MONTH,-1);
            vo.setStart(DateUtils.dateTime(calendar.getTime()));
        }

        if(StringUtils.isNotEmpty(vo.getStart())){
            Date start = DateUtils.parseDate(vo.getStart());
            list = list.stream().filter(dto -> (dto.getDataTime().after(start) || dto.getDataTime().equals(start))).collect(Collectors.toList());
        }

        if(StringUtils.isNotEmpty(vo.getEnd())){
            Date end = DateUtils.parseDate(vo.getEnd());
            list = list.stream().filter(dto -> (dto.getDataTime().before(end) || dto.getDataTime().equals(end))).collect(Collectors.toList());
        }

        return AjaxResult.success(list);
    }

    @Override
    public AjaxResult findTronUSDCBigTxByVo(ETHVo vo) {
        boolean amountTag = false;
        boolean paramTag = false;

        TronUSDCVo usdcVo = new TronUSDCVo();

        if (!StringUtils.isEmpty(vo.getTotal())){
            vo.setTotal(vo.getTotal().replaceAll(",",""));
            //校验字符合法性
            //校验是否为字母或数字
            if(!vo.getTotal().matches("^[A-Za-z0-9]+$")){
                //total不是数字或者字母  1、输入了浮点数  2、输入非法字符 直接返回空数据
                if(!vo.getTotal().matches("^(-?\\d+)(\\.\\d+)?$")){
                    PageResult result = new PageResult();
                    result.setCount(0L);
                    result.setTotalPage(0L);
                    result.setData(new ArrayList<>());
                    return AjaxResult.success("模糊搜索传参格式有误，仅支持数字字母",result);
                }
            }
            Double amount = Convert.toDouble(vo.getTotal());
            if(amount != null){
                usdcVo.setAmount(amount);
                amountTag = true;
            }
            else{
                if(vo.getTotal().length()>=64){
                    usdcVo.setTxId(vo.getTotal());
                }
                else if(vo.getTotal().length()<12){
                    usdcVo.setTag(vo.getTotal());
                }else {
                    usdcVo.setAddress(vo.getTotal());
                }
            }
        }
        else{
            //所有参数都没传则返回空值
            //return AjaxResult.success("传参有误，请检查参数",null);
            paramTag = true;
        }

        if(StringUtils.isNotEmpty(vo.getStart()) || StringUtils.isNotEmpty(vo.getEnd())){
            if(StringUtils.isNotEmpty(vo.getEnd())){
                Date end = DateUtils.parseDate(vo.getEnd());
                Calendar calendar = new GregorianCalendar();
                calendar.setTime(end);
                calendar.add(calendar.HOUR,-8);
                Date start = calendar.getTime();

                usdcVo.setStart(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,start));

                calendar.add(calendar.DATE,+1);

                end = calendar.getTime();
                usdcVo.setEnd(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,end));
            }

        }else if((amountTag || paramTag) && StringUtils.isEmpty(vo.getStart()) && StringUtils.isEmpty(vo.getEnd())){
            //起止时间都没传 则默认查最新一条数据的当前时间前两天
            TronUSDCBigTx tronUSDCBigTx = ethMapper.selectLastTronUSDCBigTx();
            Date end = tronUSDCBigTx.getDate();
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(end);
            calendar.add(calendar.DATE,-2);

            usdcVo.setEnd(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,end));
            usdcVo.setStart(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,calendar.getTime()));
        }

        Page<TronUSDCBigTx> page = new Page<>(vo.getPage(),vo.getLimit());
        IPage<TronUSDCBigTx> list = ethMapper.selectTronUSDCBigTxList(page, usdcVo);

        list.getRecords().stream().forEach(e ->{
            if("unknown".equals(e.getFromTag())){
                e.setFromTag("");
            }
            if("unknown".equals(e.getToTag())){
                e.setToTag("");
            }
        });


        PageResult result = new PageResult();

        result.setCount(list.getTotal());
        result.setTotalPage(list.getPages());
        result.setData(list.getRecords());

        return AjaxResult.success(result);
    }

    @Override
    public AjaxResult findTronUSDCBigTxByTime(QueryVo vo) {

        List<CountDto> list = redisService.getCacheList("TronUSDCBigTxDayCount");

        if(list.size() == 0){
            return AjaxResult.success("该时间范围内没有数据",list);
        }

        //缺失日期补齐
        list = dataMunging(list);

        if(StringUtils.isEmpty(vo.getStart()) && StringUtils.isEmpty(vo.getEnd())){
            //起止时间都没传 则默认查最新一条数据的当前时间前一个月
            Date date = list.get(list.size()-1).getDataTime();//北京时间
            vo.setEnd(DateUtils.dateTime(date));
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(date);
            calendar.add(calendar.MONTH,-1);
            vo.setStart(DateUtils.dateTime(calendar.getTime()));
        }

        if(StringUtils.isNotEmpty(vo.getStart())){
            Date start = DateUtils.parseDate(vo.getStart());
            list = list.stream().filter(dto -> (dto.getDataTime().after(start) || dto.getDataTime().equals(start))).collect(Collectors.toList());
        }

        if(StringUtils.isNotEmpty(vo.getEnd())){
            Date end = DateUtils.parseDate(vo.getEnd());
            list = list.stream().filter(dto -> (dto.getDataTime().before(end) || dto.getDataTime().equals(end))).collect(Collectors.toList());
        }

        return AjaxResult.success(list);
    }

    @Override
    public AjaxResult getETHTokenVolumeList(ETHTokenVolumeVo vo) {

        //List<ETHTokenVolumeDto> list = new ArrayList<>();
        //if(!StringUtils.isEmpty(queryVo.getEnd()) || !StringUtils.isEmpty(queryVo.getStart())){
        //    list = ethMapper.getETHTokenVolumeList(queryVo);
        //}else{
        //    list = redisService.getCacheList("TronUSDTVolumeLastMonth");
        //}
        List<ETHTokenVolumeDto> list = ethMapper.getETHTokenVolumeList(vo);
        if(list.size()<1){
            return AjaxResult.success(list);
        }

        List<ETHTokenVolumeDto> returnList = new ArrayList<>();
        for(int i = 0; i < list.size() ; i++){
            ETHTokenVolumeDto dto = list.get(i);
            if(dto.getVolume() > Math.pow(10,20)){
                if(i > 1){
                    dto.setVolume(list.get(i-1).getVolume());
                }else
                    dto.setVolume(0.0);
            }
            returnList.add(dto);
        }

        return AjaxResult.success(returnList);
    }

    @Override
    public AjaxResult getETHTokenVolumeAbbrList(ETHTokenVolumeVo vo) {

        List<ETHTokenVolumeAbbrsDto> list = redisService.getCacheList("ETHTokenVolumeAbbrList");
        if (list.size() < 1){
            return AjaxResult.success(list);
        }

        if(StringUtils.isNull(vo)){
            return AjaxResult.success(list);
        }

        if(vo.getSma() < 1){
            return AjaxResult.success(list);
        }

        int sma = vo.getSma();

        if(StringUtils.isNotNull(vo)){
            List<ETHTokenVolumeDto> addrList = ethMapper.getETHTokenVolumeList(vo);

            if (list.size()< sma){
                list.stream().filter(dto -> vo.getAbbr().equals(dto.getAbbr())).forEach(dto2 -> dto2.setList(new ArrayList<>()));
                return AjaxResult.success(list);
            }

            BigDecimal total = BigDecimal.ZERO;
            BigDecimal divideNumber = new BigDecimal(sma);
            for(int i = 0 ; i < sma ;i++){
                total =  BigDecimal.valueOf(addrList.get(i).getVolume()).add(total);
            }

            BigDecimal startY = total.divide(divideNumber, 8, BigDecimal.ROUND_HALF_UP);
            ETHTokenVolumeDto dto = new ETHTokenVolumeDto();
            BeanUtils.copyProperties(addrList.get(sma-1),dto);
            dto.setVolume(startY.stripTrailingZeros().doubleValue());

            List<ETHTokenVolumeDto> smaList = new ArrayList<>();
            smaList.add(dto);
            for (int i = sma; i < addrList.size(); i++) {

                total = total.add(BigDecimal.valueOf(addrList.get(i).getVolume())).subtract(BigDecimal.valueOf(addrList.get(i-sma).getVolume()));

                startY = total.divide(divideNumber,8,BigDecimal.ROUND_HALF_UP);

                ETHTokenVolumeDto dto2 = new ETHTokenVolumeDto();
                BeanUtils.copyProperties(addrList.get(i),dto2);
                //返回给前端的参数需要删除多余的0
                dto2.setVolume(startY.stripTrailingZeros().doubleValue());

                smaList.add(dto);
            }
            System.out.println(vo.getAbbr());


            list.stream().peek(e -> {
                System.out.println(e.getAbbr());
                if(vo.getAbbr().equals(e.getAbbr())){
                    e.setList(smaList);
                }
            }).collect(Collectors.toList());


        }

        return AjaxResult.success(list);
    }

    @Override
    public AjaxResult getETHTokenAbbrList() {
        List<ETHTokenAbbrsDto> list = ethMapper.getETHTokenAbbrs();
        //if (list.size() < 1){
        //    return AjaxResult.error("数据库提供数据异常");
        //}
        return AjaxResult.success(list);
    }

    @Override
    public AjaxResult getETHTokenBigtxList(ETHTokenVo vo) {
        if(vo.getPage() < 1){
            vo.setPage(1);
        }
        if(vo.getLimit() < 10){
            vo.setLimit(10);
        }

        //根据page-1*pagesize 处理page -> limit page,pagesize
        int page = ( vo.getPage() -1) * vo.getLimit();
        vo.setPage(page);

        long t1 = System.currentTimeMillis();
        if (!StringUtils.isEmpty(vo.getTotal())){
            vo.setTotal(vo.getTotal().replaceAll(",",""));
            //校验字符合法性
            //校验是否为字母或数字
            if(!vo.getTotal().matches("^[A-Za-z0-9]+$")){
                //total不是数字或者字母  1、输入了浮点数  2、输入非法字符 直接返回空数据
                if(!vo.getTotal().matches("^(-?\\d+)(\\.\\d+)?$")){
                    PageResult result = new PageResult();
                    result.setData(new ArrayList<>());
                    result.setTotalPage(0L);
                    result.setCount(0L);
                    return AjaxResult.success("模糊搜索传参格式有误，仅支持数字字母",result);
                }
            }
            Double amount = Convert.toDouble(vo.getTotal());
            if(amount != null){
                vo.setAmount(amount);
                //因为amount要根据单位做数据转换 所以不能直接搜索
            }
            else{
                if(vo.getTotal().length()>=64){
                    vo.setTxId(vo.getTotal());
                }
                else {
                    vo.setAddress(vo.getTotal());
                }
            }
        }
        long t2 = System.currentTimeMillis();
        System.out.println("处理total耗时"+(t2-t1));

        //if(StringUtils.isBlank(vo.getStart()) && StringUtils.isBlank(vo.getEnd()) && StringUtils.isBlank(vo.getTotal())
        //        && StringUtils.isBlank(vo.getAddress()) && StringUtils.isBlank(vo.getTxId()) && vo.getAmount() ==0.0){
        //    //任何参数都没传  最后一周大额交易
        //    ETHTokenBigtxDto dto = ethMapper.selectLastETHTokenBigTx();
        //    vo.setEnd(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,dto.getDateTime()));
        //    Calendar calendar = new GregorianCalendar();
        //    calendar.setTime(dto.getDateTime());
        //    //calendar.add(calendar.HOUR,-8);
        //    calendar.add(calendar.DATE,-7);
        //    vo.setStart(DateUtils.dateTime(calendar.getTime()));
        //}
        long t3 = System.currentTimeMillis();
        System.out.println("构造请求条件耗时"+(t3-t2));

        //获取符合条件的数据总条数
        int count = ethMapper.getETHTokenBigtxCount(vo);
        long t4 = System.currentTimeMillis();
        System.out.println("查询数据总数耗时"+(t4-t3));
        //分页获取符合条件的数据 通过limit 0,50
        List<ETHTokenBigtxDto> list = ethMapper.getETHTokenBigtxList(vo);
        long t5 = System.currentTimeMillis();
        System.out.println("分页查询耗时"+(t5-t4));
        if(count == 0){
            return AjaxResult.success(list);
        }

        PageResult result = new PageResult();
        int totalPage = count/vo.getLimit();
        if(count % vo.getLimit() != 0){
            totalPage++;
        }
        result.setCount((long) count);
        result.setTotalPage((long) totalPage);
        result.setData(list);

        return AjaxResult.success(result);

    }

    @Override
    public AjaxResult findETHTokenBigtxByTime(QueryVo vo) {
        long t1 = System.currentTimeMillis();
        if(StringUtils.isEmpty(vo.getStart()) && StringUtils.isEmpty(vo.getEnd())){
                //起止时间都没传 则默认查最新一条数据的当前时间前一个月
                vo.setEnd(DateUtils.dateTimeNow(DateUtils.YYYY_MM_DD));
                Calendar calendar = new GregorianCalendar();
                calendar.setTime(DateUtils.parseDate(vo.getEnd()));
                calendar.add(calendar.HOUR,-8);
                calendar.add(calendar.MONTH,-1);
                vo.setStart(DateUtils.dateTime(calendar.getTime()));
        }
        long t2 = System.currentTimeMillis();
        System.out.println("构造请求条件耗时"+(t2-t1));
        List<CountDto> list = redisService.getCacheList("ETHTokenBigTxDayCount");
        long t3 = System.currentTimeMillis();
        System.out.println("请求数据耗时"+(t3-t2));
        if(StringUtils.isNotEmpty(vo.getStart())){
            Date start = DateUtils.parseDate(vo.getStart());
            list = list.stream().filter(dto -> (dto.getDataTime().after(start) || dto.getDataTime().equals(start))).collect(Collectors.toList());
        }

        if(StringUtils.isNotEmpty(vo.getEnd())){
            Date end = DateUtils.parseDate(vo.getEnd());
            list = list.stream().filter(dto -> (dto.getDataTime().before(end) || dto.getDataTime().equals(end))).collect(Collectors.toList());
        }
        long t4 = System.currentTimeMillis();
        System.out.println("根据时间筛选数据耗时"+(t4-t3));

        if(list.size() == 0){
            return AjaxResult.success("该时间范围内没有数据",list);
        }

        return AjaxResult.success(list);
    }

    @Override
    public AjaxResult getETHNewContractByPage(ETHVo vo) {

        if(StringUtils.isNotEmpty(vo.getTotal())){
            vo.setTotal(vo.getTotal().replaceAll(",",""));
            //校验字符合法性
            //校验是否为字母或数字
            if(!vo.getTotal().matches("^[A-Za-z0-9]+$")){
                //total不是数字或者字母  1、输入了浮点数  2、输入非法字符 直接返回空数据
                if(!vo.getTotal().matches("^(-?\\d+)(\\.\\d+)?$")){
                    PageResult result = new PageResult();
                    result.setCount(0L);
                    result.setTotalPage(0L);
                    result.setData(new ArrayList<>());
                    return AjaxResult.success("模糊搜索传参格式有误，仅支持数字字母",result);
                }
            }

            if(vo.getTotal().length()>=64){
                vo.setTxId(vo.getTotal());
            }
            else {
                vo.setAddress(vo.getTotal());
            }
        }

        System.out.println("vo："+vo);

        Page<EthNewContractDto> page = new Page<>(vo.getPage(),vo.getLimit());
        //todo 过滤
        IPage<EthNewContractDto> list = ethMapper.selectNewContractByPage(page,vo);
        System.out.println("查询结果："+list);
        PageResult result = new PageResult();
        result.setCount(list.getTotal());
        result.setTotalPage(list.getPages());
        result.setData(list.getRecords());
        return AjaxResult.success(result);
    }

    @Override
    public AjaxResult getCSupplyList(QueryVo vo) {

        List<DateBigDecimalDto> list = redisService.getCacheList("ethCSupplyList");

        if(list.size()<1){
            return AjaxResult.success(list);
        }

        if(StringUtils.isNotNull(vo.getStart())){
            Date start = DateUtils.parseCNDate(vo.getStart());
            System.out.println(start);
            list = list.stream().filter(dto -> (dto.getDate().after(start) || dto.getDate().equals(start))).collect(Collectors.toList());
        }

        if(StringUtils.isNotNull(vo.getEnd())){
            Date end = DateUtils.parseCNDate(vo.getEnd());
            list = list.stream().filter(dto -> (dto.getDate().before(end) || dto.getDate().equals(end))).collect(Collectors.toList());
        }

        if(list.size()<1) {
            return AjaxResult.success("当前时间段没有数据",list);
        }

        //异常数据处理 主要是对0数据
        return AjaxResult.success(list);
    }

    @Override
    public AjaxResult getRewardList(QueryVo vo) {

        List<DateBigDecimalDto> list = redisService.getCacheList("ethRewardList");

        if(list.size()<1){
            return AjaxResult.success(list);
        }

        if(StringUtils.isNotNull(vo.getStart())){
            Date start = DateUtils.parseCNDate(vo.getStart());
            System.out.println(start);
            list = list.stream().filter(dto -> (dto.getDate().after(start) || dto.getDate().equals(start))).collect(Collectors.toList());
        }

        if(StringUtils.isNotNull(vo.getEnd())){
            Date end = DateUtils.parseCNDate(vo.getEnd());
            list = list.stream().filter(dto -> (dto.getDate().before(end) || dto.getDate().equals(end))).collect(Collectors.toList());
        }


        if(StringUtils.isNotNull(vo)){
            if(vo.getSma() >0){
                list = getSMA(list,vo.getSma());
            }
        }

        if(list.size()<1) {
            return AjaxResult.success("当前时间段没有数据",list);
        }
        //异常数据处理 主要是对0数据
        return AjaxResult.success(list);
    }

    @Override
    public AjaxResult getAddressList(QueryVo vo) {

        List<ETHAddressDto> list = redisService.getCacheList("ethAddressList");

        if(StringUtils.isNotNull(vo.getStart())){
            Date start = DateUtils.parseCNDate(vo.getStart());
            System.out.println(start);
            list = list.stream().filter(dto -> (dto.getDate().after(start) || dto.getDate().equals(start))).collect(Collectors.toList());
        }

        if(StringUtils.isNotNull(vo.getEnd())){
            Date end = DateUtils.parseCNDate(vo.getEnd());
            list = list.stream().filter(dto -> (dto.getDate().before(end) || dto.getDate().equals(end))).collect(Collectors.toList());
        }
        //
        //if(StringUtils.isNotNull(vo)){
        //    if(vo.getSma() >0){
        //        list = getSMA(list,vo.getSma());
        //    }
        //}

        if(list.size()<1) {
            return AjaxResult.success("当前时间段没有数据",list);
        }

        //异常数据处理 主要是对0数据
        return AjaxResult.success(list);
    }

    @Override
    public AjaxResult getVolumeList(QueryVo vo) {

        List<DateBigDecimalDto> list = redisService.getCacheList("ethVolumeList");


        if(list.size()<1){
            return AjaxResult.success(list);
        }

        if(StringUtils.isNotNull(vo.getStart())){
            Date start = DateUtils.parseCNDate(vo.getStart());
            System.out.println(start);
            list = list.stream().filter(dto -> (dto.getDate().after(start) || dto.getDate().equals(start))).collect(Collectors.toList());
        }

        if(StringUtils.isNotNull(vo.getEnd())){
            Date end = DateUtils.parseCNDate(vo.getEnd());
            list = list.stream().filter(dto -> (dto.getDate().before(end) || dto.getDate().equals(end))).collect(Collectors.toList());
        }

        if(StringUtils.isNotNull(vo)){
            if(vo.getSma() >0){
                list = getSMA(list,vo.getSma());
            }
        }

        if(list.size()<1) {
            return AjaxResult.success("当前时间段没有数据",list);
        }
        //异常数据处理 主要是对0数据
        return AjaxResult.success(list);
    }

    @Override
    public AjaxResult getCnewList(QueryVo vo) {

        List<DateBigDecimalDto> list = redisService.getCacheList("ethContractNewList");


        if(list.size()<1){
            return AjaxResult.success(list);
        }

        if(StringUtils.isNotNull(vo.getStart())){
            Date start = DateUtils.parseCNDate(vo.getStart());
            System.out.println(start);
            list = list.stream().filter(dto -> (dto.getDate().after(start) || dto.getDate().equals(start))).collect(Collectors.toList());
        }

        if(StringUtils.isNotNull(vo.getEnd())){
            Date end = DateUtils.parseCNDate(vo.getEnd());
            list = list.stream().filter(dto -> (dto.getDate().before(end) || dto.getDate().equals(end))).collect(Collectors.toList());
        }

        if(StringUtils.isNotNull(vo)){
            if(vo.getSma() >0){
                list = getSMA(list,vo.getSma());
            }
        }

        if(list.size()<1) {
            return AjaxResult.success("当前时间段没有数据",list);
        }

            //异常数据处理 主要是对0数据
        return AjaxResult.success(list);
    }

    @Override
    public AjaxResult getPrice(QueryVo vo) {
        List<PriceDataDto> list = redisService.getCacheList("ethPriceList");

        if(list.size()<1){
            return AjaxResult.success(list);
        }

        if(StringUtils.isNotNull(vo.getStart())){
            Date start = DateUtils.parseCNDate(vo.getStart());
            System.out.println(start);
            list = list.stream().filter(dto -> (dto.getTime().after(start) || dto.getTime().equals(start))).collect(Collectors.toList());
        }

        if(StringUtils.isNotNull(vo.getEnd())){
            Date end = DateUtils.parseCNDate(vo.getEnd());
            list = list.stream().filter(dto -> (dto.getTime().before(end) || dto.getTime().equals(end))).collect(Collectors.toList());
        }

        if(list.size()<1) {
            return AjaxResult.success("当前时间段没有数据",list);
        }

        //异常数据处理 主要是对0数据
        return AjaxResult.success(list);
    }


    @Override
    public AjaxResult getTokenVolumeList(QueryVo vo) {

        List<ComDataDto> list = redisService.getCacheList("ethTokenVolumeList");


        if(list.size()<1){
            return AjaxResult.success(list);
        }

        if(StringUtils.isNotNull(vo.getStart())){
            Date start = DateUtils.parseCNDate(vo.getStart());
            System.out.println(start);
            list = list.stream().filter(dto -> (dto.getDate().after(start) || dto.getDate().equals(start))).collect(Collectors.toList());
        }

        if(StringUtils.isNotNull(vo.getEnd())){
            Date end = DateUtils.parseCNDate(vo.getEnd());
            list = list.stream().filter(dto -> (dto.getDate().before(end) || dto.getDate().equals(end))).collect(Collectors.toList());
        }

        if(list.size()<1) {
            return AjaxResult.success("当前时间段没有数据",list);
        }
        //异常数据处理 主要是对0数据
        return AjaxResult.success(list);
    }


    /**
     * sma处理
     * @param list
     * @return
     */
    public List<DateBigDecimalDto> getSMA(List<DateBigDecimalDto> list,int day) {

        if (list.size()< day){
            return new ArrayList<>();
        }

        if(list.size() < 1){
            return list;
        }

        BigDecimal total = BigDecimal.ZERO;
        BigDecimal divideNumber = new BigDecimal(day);
        for(int i = 0 ; i < day ;i++){
            total =  list.get(i).getValue().add(total);
        }
        BigDecimal startY = total.divide(divideNumber, 8, BigDecimal.ROUND_HALF_UP);
        DateBigDecimalDto startDto = new DateBigDecimalDto();
        startDto.setDate(list.get(day-1).getDate());
        startDto.setValue(startY.stripTrailingZeros());
        startDto.setPrice(list.get(day-1).getPrice());

        List<DateBigDecimalDto> returnList = new ArrayList<>();
        returnList.add(startDto);
        for (int i = day; i < list.size(); i++) {

            total = total.add(list.get(i).getValue()).subtract(list.get(i-day).getValue());

            startY = total.divide(divideNumber,8,BigDecimal.ROUND_HALF_UP);

            DateBigDecimalDto dto = new DateBigDecimalDto();
            dto.setDate(list.get(i).getDate());
            //返回给前端的参数需要删除多余的0
            dto.setValue(startY.stripTrailingZeros());
            dto.setPrice(list.get(i).getPrice());

            returnList.add(dto);
        }
        return returnList;
    }

    /**
     * 缺失日期数据补0
     */
    public List<CountDto> dataMunging(List<CountDto> list){

        if(list.size() <= 1){
            return list;
        }

        Map<Date, Integer> map = list.stream().collect(Collectors.toMap(CountDto::getDataTime, CountDto::getNum, (k1, k2) -> k1));

        Date start = list.get(0).getDataTime();
        Date end = list.get(list.size()-1).getDataTime();

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(start);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        int i = 0;
        while (calendar.getTime().getTime() <= end.getTime()) {
            if (null == map.get(calendar.getTime())) {
                CountDto dto = new CountDto();
                dto.setDataTime(calendar.getTime());
                dto.setNum(0);
                list.add(i, dto);
            }
            calendar.add(Calendar.DAY_OF_MONTH, 1);
            i++;
        }

        return list;
    }

}
