package com.jxy.marketall.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jxy.common.core.text.Convert;
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.mapper.ETHMapper;
import com.jxy.marketall.mapper.ETHRTMapper;
import com.jxy.marketall.service.ETHService;
import com.jxy.marketall.vo.ETHTopAddrVo;
import com.jxy.marketall.vo.PageVo;
import com.jxy.marketall.vo.QueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description TODO
 * @Date 2022/7/18 14:29
 * @Author 杜懿
 */
@Service
public class ETHServiceImpl implements ETHService {

    @Autowired
    private ETHMapper ethMapper;

    @Autowired
    private ETHRTMapper ethrtMapper;

    @Autowired
    private RedisService redisService;

    @Override
    public AjaxResult getETHTopAddrByPage(ETHTopAddrVo vo) {

        if(StringUtils.isNotEmpty(vo.getTotal())){
            Double balance = Convert.toDouble(vo.getTotal().replaceAll(",",""));
            if(balance != null){
                vo.setBalance(balance);
            }
            else{
                vo.setAddress(vo.getTotal());
            }
        }

        Page<ETHTopAddrDto> page = new Page<>(vo.getPage(),vo.getLimit());
        //todo 过滤
        IPage<ETHTopAddrDto> list = ethMapper.selectETHTopAddrByPage(page,vo);

        PageResult result = new PageResult();
        List<ETHTopAddrDto> records = list.getRecords();
        for (int i=0 ; i < records.size() ;i++){
            if(StringUtils.equals(records.get(i).getTag(),"unknown")){
                records.get(i).setTag("");
            }
        }
        result.setCount(list.getTotal());
        result.setTotalPage(list.getPages());
        result.setData(records);
        return AjaxResult.success(result);
    }

    @Override
    public AjaxResult getETHTagsByPage(PageVo vo) {

        Page<ETHTags> page = new Page<>(vo.getPage(),vo.getLimit());
        IPage<TagDto> list = ethrtMapper.selectETHTagsByPage(page);
        PageResult result = new PageResult();

        result.setCount(list.getTotal());
        result.setTotalPage(list.getPages());
        result.setData(list.getRecords());
        return AjaxResult.success(result);
    }

    @Override
    public AjaxResult getArh99ByTime(QueryVo vo) {
        return null;
    }

    @Override
    public AjaxResult getArh99Day() {
        return null;
    }

    @Override
    public AjaxResult getJzr60ByTime(QueryVo vo) {
        List<Jzr60> list = ethrtMapper.selectJzr60ByTime(vo);
        //if (list.size() < 1){
        //    return AjaxResult.error("未查询到数据");
        //}
        return AjaxResult.success(list);
    }

    @Override
    public AjaxResult getJzr60Day() {
        List<Jzr60Day> list = ethrtMapper.selectJzr60Day();
        //if (list.size() < 1){
        //    return AjaxResult.error("未查询到数据");
        //}
        return AjaxResult.success(list);
    }

    @Override
    public AjaxResult getMa730ByTime(QueryVo vo) {
        List<Ma730Dto> list = ethrtMapper.selectMa730ByTime(vo);
        //if (list.size() < 1){
        //    return AjaxResult.error("未查询到数据");
        //}
        return AjaxResult.success(list);
    }

    @Override
    public AjaxResult geMa730Day() {
        List<Ma730Dto> list = ethrtMapper.selectMa730Day();
        //if (list.size() < 1){
        //    return AjaxResult.error("未查询到数据");
        //}
        return AjaxResult.success(list);
    }

    @Override
    public AjaxResult getCSUSDCList(QueryVo queryVo) {
        List<CirculatingSupplyDataDto> list = ethrtMapper.getCSUSDCList(queryVo);
        //if(!StringUtils.isEmpty(queryVo.getEnd()) || !StringUtils.isEmpty(queryVo.getStart())){
        //    list = ethMapper.getUSDCVolumeList(queryVo);
        //}else{
        //    list = redisService.getCacheList("CSupplyUSDCList");
        //}
        //
        //if(list.size()<1){
        //    return AjaxResult.error("没有查询到数据");
        //}
        //
        //List<CirculatingSupplyDataDto> 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(list);
    }

    @Override
    public AjaxResult getCSUSDTList(QueryVo queryVo) {

        List<CirculatingSupplyDataDto> list = ethrtMapper.getCSUSDTList(queryVo);

        //if(list.size()<1){
        //    return AjaxResult.error("没有查询到数据");
        //}

        return AjaxResult.success(list);
    }

    @Override
    public AjaxResult getCSBUSDList(QueryVo queryVo) {

        List<CirculatingSupplyDataDto> list = ethrtMapper.getCSBUSDList(queryVo);

        //if(list.size()<1){
        //    return AjaxResult.error("没有查询到数据");
        //}

        return AjaxResult.success(list);
    }

    @Override
    public AjaxResult getTotalCSupplyList(QueryVo queryVo) {

        List<AllTypeCSDto> list = ethrtMapper.getTotalCSList(queryVo);

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

        List<CirculatingSupplyDataDto> returnList = new ArrayList<>();
        for (int i = 0 ;i < list.size() ; i++){
            CirculatingSupplyDataDto dataDto = new CirculatingSupplyDataDto();
            AllTypeCSDto dto = list.get(i);
            dataDto.setDate(dto.getDate());

            if(StringUtils.isNull(dto.getBusd())){
                dto.setBusd(BigDecimal.ZERO);
            }
            if(StringUtils.isNull(dto.getUsdc())){
                dto.setUsdc(BigDecimal.ZERO);
            }
            if(StringUtils.isNull(dto.getUsdt())){
                dto.setUsdt(BigDecimal.ZERO);
            }

            dataDto.setValue(dto.getUsdt().add(dto.getUsdc()).add(dto.getBusd()));
            returnList.add(dataDto);
        }
        return AjaxResult.success(returnList);
    }
}
