package com.jxy.marketall.service.impl;

import com.alibaba.nacos.shaded.org.checkerframework.checker.units.qual.C;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jxy.common.core.text.DoubleCalculate;
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.redis.service.RedisService;
import com.jxy.marketall.dto.*;
import com.jxy.marketall.entity.Utxos;
import com.jxy.marketall.mapper.Utxos2ndMapper;
import com.jxy.marketall.mapper.UtxosMapper;
import com.jxy.marketall.service.CoinPriceService;
import com.jxy.marketall.service.MarketallService;
import com.jxy.marketall.service.UtxosService;
import com.jxy.marketall.vo.CoinPriceVo;
import com.jxy.marketall.vo.CoinVo;
import com.jxy.marketall.vo.QueryVo;
import org.apache.poi.ss.formula.functions.Now;
import org.apache.poi.ss.usermodel.DateUtil;
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.time.Period;
import java.time.ZoneId;
import java.time.temporal.TemporalAdjusters;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description TODO
 * @Date 2022/7/18 14:29
 * @Author 杜懿
 */
@Service
public class UtxosServiceImpl extends ServiceImpl<UtxosMapper, Utxos> implements UtxosService {

    @Autowired
    private RedisService redisService;

    @Autowired
    private Utxos2ndMapper utxos2ndMapper;

    @Autowired
    private CoinPriceService coinPriceService;

    @Override
    public AjaxResult getNewUtxos() {



        UtxosDto utxosData = redisService.getCacheObject("UtxosData");


        if(StringUtils.isEmpty(Integer.toString(utxosData.getId()))){
            return AjaxResult.error("没有查询到数据");
        }

        return AjaxResult.success(utxosData);
    }

    @Override
    public AjaxResult getNewUtxos2nd() {

        Utxos2ndDto utxos2ndData = redisService.getCacheObject("Utxos2ndData");


        if(StringUtils.isNull(utxos2ndData.getDateTime())){
            return AjaxResult.success(null);
        }

        return AjaxResult.success(utxos2ndData);
    }

    @Override
    public AjaxResult getUtxosByTime(QueryVo vo) {

        List<Utxos> list = redisService.getCacheList("UtxosList");

        if(list.size() < 1){
            //数据库也拿不到数据
            return AjaxResult.success("数据库异常，获取数据失败！",list);
        }

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

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

        List<UtxosDto> returnList = list.stream().map(e -> {
            UtxosDto dto = new UtxosDto();
            BeanUtils.copyProperties(e, dto);
            Double address = Double.valueOf(e.getTotalAddress());
            dto.setRate001(DoubleCalculate.divide(dto.getBalance001(), address, 8));
            dto.setRate01(DoubleCalculate.divide(dto.getBalance01(), address, 8));
            dto.setRate0(DoubleCalculate.divide(dto.getBalance0(), address, 8));
            dto.setRate1(DoubleCalculate.divide(dto.getBalance1(), address, 8));
            dto.setRate10(DoubleCalculate.divide(dto.getBalance10(), address, 8));
            dto.setRate100(DoubleCalculate.divide(dto.getBalance100(), address, 8));
            dto.setRate1000(DoubleCalculate.divide(dto.getBalance1000(), address, 8));
            dto.setRate10000(DoubleCalculate.divide(dto.getBalance10000(), address, 8));
            return dto;
        }).collect(Collectors.toList());

        if(StringUtils.isNull(returnList)){
            return AjaxResult.success("该时间范围内没数据",returnList);
        }

        return AjaxResult.success(returnList);
    }

    @Override
    public AjaxResult getUtxos2ndByTime(QueryVo vo) {

        List<Utxos2ndBalanceDto> list = redisService.getCacheList("Utxos2ndList");

        if(list.size() < 1){
            //数据库也拿不到数据
            return AjaxResult.success("数据库异常，获取数据失败！",list);
        }

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

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

        List<Utxos2ndDto> returnList = list.stream().map(e -> {
            Utxos2ndDto dto = new Utxos2ndDto();
            BeanUtils.copyProperties(e, dto);
            Double balance = e.getTotalBalance();
            dto.setRateAmount001(DoubleCalculate.divide(dto.getBalanceAmount001(), balance, 8));
            dto.setRateAmount01(DoubleCalculate.divide(dto.getBalanceAmount01(), balance, 8));
            dto.setRateAmount0(DoubleCalculate.divide(dto.getBalanceAmount0(), balance, 8));
            dto.setRateAmount1(DoubleCalculate.divide(dto.getBalanceAmount1(), balance, 8));
            dto.setRateAmount10(DoubleCalculate.divide(dto.getBalanceAmount10(), balance, 8));
            dto.setRateAmount100(DoubleCalculate.divide(dto.getBalanceAmount100(), balance, 8));
            dto.setRateAmount1000(DoubleCalculate.divide(dto.getBalanceAmount1000(), balance, 8));
            dto.setRateAmount10000(DoubleCalculate.divide(dto.getBalanceAmount10000(), balance, 8));
            return dto;
        }).collect(Collectors.toList());

        if(StringUtils.isNull(returnList)){
            return AjaxResult.success("该时间范围内没数据",returnList);
        }

        return AjaxResult.success(returnList);

    }

    @Override
    public AjaxResult getCapReal(CoinVo vo) {

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

        if (StringUtils.isNotBlank(vo.getStart()) || StringUtils.isNotBlank(vo.getEnd())) {
            list = baseMapper.getCapRealList(vo);
        }else {
            /** 从缓存中拿取所有capReal数据 没有就根据查询条件直接从数据库中拿 */
            list = redisService.getCacheList("CapRealData");
        }

        if (list.size() < 1){
            //如果缓存中没有数据那就直接从数据库去查
            list = baseMapper.getCapRealList(vo);
        }

        return AjaxResult.success(list);

    }


    @Override
    public AjaxResult getMinerList(CoinVo vo) {
        return null;
    }

    public static double add(double v1, double v2)

    {

        BigDecimal b1 = new BigDecimal(Double.toString(v1));

        BigDecimal b2 = new BigDecimal(Double.toString(v2));

        return b1.add(b2).doubleValue();

    }

}
