package com.jxy.marketall.task;

import com.jxy.common.core.utils.DateUtils;
import com.jxy.common.core.utils.StringUtils;
import com.jxy.common.redis.service.RedisService;
import com.jxy.marketall.dto.CapRealDto;
import com.jxy.marketall.dto.Utxos2ndBalanceDto;
import com.jxy.marketall.dto.Utxos2ndDto;
import com.jxy.marketall.dto.UtxosDto;

import com.jxy.marketall.entity.Utxos;

import com.jxy.marketall.mapper.Utxos2ndMapper;
import com.jxy.marketall.mapper.UtxosMapper;
import com.jxy.marketall.vo.QueryVo;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.*;
import java.util.stream.Collectors;


/**
 * @Description TODO
 * @Date 2022/7/26 17:03
 * @Author 杜懿
 */
@Configuration
@EnableScheduling
public class UtxosTask {


    @Autowired
    private UtxosMapper utxosMapper;

    @Autowired
    private Utxos2ndMapper utxos2ndMapper;


    @Autowired
    private RedisService redisService;

    @Scheduled(cron = "0 24 0/4 * * ?")
    //@Scheduled(cron = "0 0/5 * * * ?")
    public void utxosDataToRedis(){
//        UtxosDto newUtxos = utxosMapper.getNewUtxos();
        //查询utxos的数据

        Utxos newUtxos = utxosMapper.getLastUtxos();

        //通过查询的数据构造返回的utxosDto
        UtxosDto utxosDto = new UtxosDto();
        BeanUtils.copyProperties(newUtxos,utxosDto);
        Double address = Double.valueOf(newUtxos.getTotalAddress());
        utxosDto.setRate0(utxosDto.getBalance0()/address);
        utxosDto.setRate01(utxosDto.getBalance01()/address);
        utxosDto.setRate001(utxosDto.getBalance001()/address);
        utxosDto.setRate1(utxosDto.getBalance1()/address);
        utxosDto.setRate10(utxosDto.getBalance10()/address);
        utxosDto.setRate100(utxosDto.getBalance100()/address);
        utxosDto.setRate1000(utxosDto.getBalance1000()/address);
        utxosDto.setRate10000(utxosDto.getBalance10000()/address);

        //将 utxos的数据放入缓存
        if(StringUtils.isNotNull(newUtxos.getDateTime())){
            //存入redis 先删除原有数据再重新插入数据
            redisService.deleteObject("UtxosData");
            redisService.setCacheObject("UtxosData",utxosDto);
        }


        //查询utxos2nd
        Utxos2ndDto newUtxos2nd = utxos2ndMapper.getNewUtxos2nd();


        //通过查询的数据构造返回的utxos2ndDto
        Double blance = Double.valueOf(newUtxos.getTotalBalance());
        newUtxos2nd.setRateAmount0(newUtxos2nd.getBalanceAmount0()/blance);
        newUtxos2nd.setRateAmount01(newUtxos2nd.getBalanceAmount01()/blance);
        newUtxos2nd.setRateAmount001(newUtxos2nd.getBalanceAmount001()/blance);
        newUtxos2nd.setRateAmount1(newUtxos2nd.getBalanceAmount1()/blance);
        newUtxos2nd.setRateAmount10(newUtxos2nd.getBalanceAmount10()/blance);
        newUtxos2nd.setRateAmount100(newUtxos2nd.getBalanceAmount100()/blance);
        newUtxos2nd.setRateAmount1000(newUtxos2nd.getBalanceAmount1000()/blance);
        newUtxos2nd.setRateAmount10000(newUtxos2nd.getBalanceAmount10000()/blance);


        if(StringUtils.isNotNull(newUtxos2nd.getDateTime())){
            //存入redis 先删除原有数据再重新插入数据
            redisService.deleteObject("Utxos2ndData");
            redisService.setCacheObject("Utxos2ndData",newUtxos2nd);
        }

    }

    @Scheduled(cron = "0 24 0/4 * * ?")
    //@Scheduled(cron = "0 0/5 * * * ?")
    public void CapRealData(){
        QueryVo vo = new QueryVo();
        List<CapRealDto> list = utxosMapper.getCapRealList(vo);

        if(list.size() > 0){
            redisService.deleteObject("CapRealData");
            redisService.setCacheList("CapRealData",list);
        }
    }

    //@Scheduled(cron = "0 24 0/4 * * ?")
    @Scheduled(cron = "0 0/2 * * * ?")
    public void utxosListToRedis(){

        List<Utxos> list = utxosMapper.getUtxosList();

        //过滤掉2018-10-01之前的数据
        Date start = DateUtils.parseDate("2018-10-01");
        list = list.stream().filter(dto -> (dto.getDateTime().after(start) || dto.getDateTime().equals(start))).collect(Collectors.toList());

        Utxos dto1 = new Utxos();
        BeanUtils.copyProperties(list.stream().filter(e -> DateUtils.isSameDay(e.getDateTime(),DateUtils.parseDate("2018-10-21"))).findFirst().get(),dto1);
        dto1.setDateTime(DateUtils.parseDate("2018-10-22 00:00:00"));

        Utxos dto2 = new Utxos();
        BeanUtils.copyProperties(list.stream().filter(e -> DateUtils.isSameDay(e.getDateTime(),DateUtils.parseDate("2018-12-23"))).findFirst().get(),dto2);
        dto2.setDateTime(DateUtils.parseDate("2018-12-24 00:00:00"));

        Utxos dto3 = new Utxos();
        BeanUtils.copyProperties(list.stream().filter(e -> DateUtils.isSameDay(e.getDateTime(),DateUtils.parseDate("2018-12-30"))).findFirst().get(),dto3);
        dto3.setDateTime(DateUtils.parseDate("2018-12-31 00:00:00"));

        Utxos dto4 = new Utxos();
        BeanUtils.copyProperties(list.stream().filter(e -> DateUtils.isSameDay(e.getDateTime(),DateUtils.parseDate("2020-02-02"))).findFirst().get(),dto4);
        dto4.setDateTime(DateUtils.parseDate("2020-02-03 00:00:00"));

        Utxos dto5 = new Utxos();
        BeanUtils.copyProperties(list.stream().filter(e -> DateUtils.isSameDay(e.getDateTime(),DateUtils.parseDate("2021-10-25"))).findFirst().get(),dto5);
        dto5.setDateTime(DateUtils.parseDate("2021-10-26 00:00:00"));

        Utxos dto6 = new Utxos();
        BeanUtils.copyProperties(list.stream().filter(e -> DateUtils.isSameDay(e.getDateTime(),DateUtils.parseDate("2022-04-24"))).findFirst().get(),dto6);
        dto6.setDateTime(DateUtils.parseDate("2022-04-25 00:00:00"));

        Utxos dto7 = new Utxos();
        BeanUtils.copyProperties(list.stream().filter(e -> DateUtils.isSameDay(e.getDateTime(),DateUtils.parseDate("2023-02-04"))).findFirst().get(),dto7);
        dto7.setDateTime(DateUtils.parseDate("2023-02-05 00:00:00"));

        Utxos dto8 = new Utxos();
        BeanUtils.copyProperties(list.stream().filter(e -> DateUtils.isSameDay(e.getDateTime(),DateUtils.parseDate("2023-05-09"))).findFirst().get(),dto8);
        dto8.setDateTime(DateUtils.parseDate("2023-05-10 00:00:00"));

        Utxos dto9 = new Utxos();
        BeanUtils.copyProperties(list.stream().filter(e -> DateUtils.isSameDay(e.getDateTime(),DateUtils.parseDate("2023-05-09"))).findFirst().get(),dto9);
        dto9.setDateTime(DateUtils.parseDate("2023-05-11 00:00:00"));

        list.add(dto1);
        list.add(dto2);
        list.add(dto3);
        list.add(dto4);
        list.add(dto5);
        list.add(dto6);
        list.add(dto7);
        list.add(dto8);
        list.add(dto9);

        //根据时间去重排序
        list = list.stream()
                .collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Utxos::getDateTime))),ArrayList::new));


        if(list.size() > 0){
            redisService.deleteObject("UtxosList");
            redisService.setCacheList("UtxosList",list);
        }
    }

    //@Scheduled(cron = "0 24 0/4 * * ?")
    @Scheduled(cron = "0 0/2 * * * ?")
    public void utxos2ndListToRedis(){

        List<Utxos2ndBalanceDto> list = utxos2ndMapper.getUtxos2ndList();

        //过滤掉2018-10-01之前的数据
        Date start = DateUtils.parseDate("2018-10-01");
        list = list.stream().filter(dto -> (dto.getDateTime().after(start) || dto.getDateTime().equals(start))).collect(Collectors.toList());


        Utxos2ndBalanceDto dto1 = new Utxos2ndBalanceDto();
        BeanUtils.copyProperties(list.stream().filter(e -> DateUtils.isSameDay(e.getDateTime(),DateUtils.parseDate("2018-10-21"))).findFirst().get(),dto1);
        dto1.setDateTime(DateUtils.parseDate("2018-10-22 00:00:00"));

        Utxos2ndBalanceDto dto2 = new Utxos2ndBalanceDto();
        BeanUtils.copyProperties(list.stream().filter(e -> DateUtils.isSameDay(e.getDateTime(),DateUtils.parseDate("2018-12-23"))).findFirst().get(),dto2);
        dto2.setDateTime(DateUtils.parseDate("2018-12-24 00:00:00"));

        Utxos2ndBalanceDto dto3 = new Utxos2ndBalanceDto();
        BeanUtils.copyProperties(list.stream().filter(e -> DateUtils.isSameDay(e.getDateTime(),DateUtils.parseDate("2018-12-30"))).findFirst().get(),dto3);
        dto3.setDateTime(DateUtils.parseDate("2018-12-31 00:00:00"));

        Utxos2ndBalanceDto dto4 = new Utxos2ndBalanceDto();
        BeanUtils.copyProperties(list.stream().filter(e -> DateUtils.isSameDay(e.getDateTime(),DateUtils.parseDate("2020-02-02"))).findFirst().get(),dto4);
        dto4.setDateTime(DateUtils.parseDate("2020-02-03 00:00:00"));



        Utxos2ndBalanceDto dto7 = new Utxos2ndBalanceDto();
        BeanUtils.copyProperties(list.stream().filter(e -> DateUtils.isSameDay(e.getDateTime(),DateUtils.parseDate("2023-02-04"))).findFirst().get(),dto7);
        dto7.setDateTime(DateUtils.parseDate("2023-02-05 00:00:00"));

        Utxos2ndBalanceDto dto8 = new Utxos2ndBalanceDto();
        BeanUtils.copyProperties(list.stream().filter(e -> DateUtils.isSameDay(e.getDateTime(),DateUtils.parseDate("2023-05-09"))).findFirst().get(),dto8);
        dto8.setDateTime(DateUtils.parseDate("2023-05-10 00:00:00"));

        Utxos2ndBalanceDto dto9 = new Utxos2ndBalanceDto();
        BeanUtils.copyProperties(list.stream().filter(e -> DateUtils.isSameDay(e.getDateTime(),DateUtils.parseDate("2023-05-09"))).findFirst().get(),dto9);
        dto9.setDateTime(DateUtils.parseDate("2023-05-11 00:00:00"));

        list.add(dto1);
        list.add(dto2);
        list.add(dto3);
        list.add(dto4);
        list.add(dto7);
        list.add(dto8);
        list.add(dto9);

        //根据时间去重排序
        list = list.stream()
                .collect(Collectors
                        .collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Utxos2ndBalanceDto::getDateTime))),ArrayList::new));


        if(list.size() > 0){
            redisService.deleteObject("Utxos2ndList");
            redisService.setCacheList("Utxos2ndList",list);
        }
    }


}
