package com.jxy.marketall.task;

import com.jxy.common.core.utils.DateUtils;
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.mapper.ETHV2Mapper;
import com.jxy.marketall.vo.*;
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.*;

/**
 * @Description TODO
 * @Date 2022/7/7 18:03
 * @Author 杜懿
 */
@Configuration
@EnableScheduling
public class ETHBigTxTask {

    @Autowired
    private ETHRTMapper ethrtMapper;

    @Autowired
    private ETHMapper ethMapper;

    @Autowired
    private ETHV2Mapper ethV2Mapper;


    @Autowired
    private RedisService redisService;


    /** eth每日交易量的数据 */
    @Scheduled(cron = "0 1 0/4 * * ?")
    public void ETHVolumeToRedis(){

        /** 获取最新一条数据 */

        QueryVo vo = new QueryVo();
        List<EthVolumeDto> list = ethMapper.getETHVolumeList(vo);

        if(list.size() > 0){
            //存入redis 先删除原有数据再重新插入数据
            redisService.deleteObject("ETHVolume");
            redisService.setCacheList("ETHVolume",list);
        }
    }

    /** eth大额交易最近两天的数据 */
    //@Scheduled(cron = "0 0/5 * * * ?")
    public void ethTwoDayDataToRedis(){

        //EthBigTx lastEthBigTx = ethMapper.selectLastETHBigTx();
        //String end = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,lastEthBigTx.getDate());
        //
        //Calendar startCalendar = new GregorianCalendar();
        //startCalendar.setTime(lastEthBigTx.getDate());
        //startCalendar.add(startCalendar.DATE,-2);
        //String start = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, startCalendar.getTime());
        //
        //ETHVo ethVo = new ETHVo();
        //ethVo.setEnd(end);
        //ethVo.setStart(start);
        //List<EthBigTxDto> list = ethMapper.selectETHBigTxList(ethVo);
        //if(list.size()>0) {
        //    //存入redis 先删除原有数据再重新插入数据
        //    redisService.deleteObject("ethBigTx2Day");
        //    redisService.setCacheList("ethBigTx2Day", list);
        //}
    }

    /** usdt大额交易最近两天的数据 */
    @Scheduled(cron = "0 3 0/1 * * ?")
    //@Scheduled(cron = "0 0/2 * * * ?")
    public void usdtTwoDayDataToRedis(){
        USDTBigTx usdtBigTx = ethMapper.selectLastUSDTBigTx();
        String end = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,usdtBigTx.getDate());

        Calendar startCalendar = new GregorianCalendar();
        startCalendar.setTime(usdtBigTx.getDate());
        startCalendar.add(startCalendar.HOUR,-8);
        startCalendar.add(startCalendar.DATE,-2);
        String start = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, startCalendar.getTime());

        USDTVo vo = new USDTVo();
        vo.setEnd(end);
        vo.setStart(start);
        List<USDBigTxDto> list = ethMapper.selectUSDTBigTxList(vo);
        if(list.size()>0) {
            //存入redis 先删除原有数据再重新插入数据
            redisService.deleteObject("usdtBigTx2Day");
            redisService.setCacheList("usdtBigTx2Day", list);
        }
    }

    /** usdc大额交易最近两天的数据 */
    @Scheduled(cron = "0 3 0/1 * * ?")
    public void usdcTwoDayDataToRedis(){
        USDCBigTx usdcBigTx = ethMapper.selectLastUSDCBigTx();
        String end = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,usdcBigTx.getDate());

        Calendar startCalendar = new GregorianCalendar();
        startCalendar.setTime(usdcBigTx.getDate());
        startCalendar.add(startCalendar.HOUR,-8);
        startCalendar.add(startCalendar.DATE,-2);
        String start = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, startCalendar.getTime());

        USDCVo vo = new USDCVo();
        vo.setEnd(end);
        vo.setStart(start);
        List<USDBigTxDto> list = ethMapper.selectUSDCBigTxList(vo);
        if(list.size()>0) {
            //存入redis 先删除原有数据再重新插入数据
            redisService.deleteObject("usdcBigTx2Day");
            redisService.setCacheList("usdcBigTx2Day", list);
        }
    }

    @Scheduled(cron = "0 3 0/1 * * ?")
    public void busdTwoDayDataToRedis(){
        USDTBigTx busdBigTx = ethMapper.selectLastBUSDBigTx();
        String end = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,busdBigTx.getDate());

        Calendar startCalendar = new GregorianCalendar();
        startCalendar.setTime(busdBigTx.getDate());
        startCalendar.add(startCalendar.HOUR,-8);
        startCalendar.add(startCalendar.DATE,-2);
        String start = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, startCalendar.getTime());

        USDTVo vo = new USDTVo();
        vo.setEnd(end);
        vo.setStart(start);
        List<USDBigTxDto> list = ethMapper.selectBUSDBigTxList(vo);
        if(list.size()>0) {
            //存入redis 先删除原有数据再重新插入数据
            redisService.deleteObject("busdBigTx2Day");
            redisService.setCacheList("busdBigTx2Day", list);
        }
    }

    /** eth每日数据量的数据 */
    @Scheduled(cron = "0 0/1 * * * ?")
    public void ETHDataCountToRedis(){

       List<CountDto> list = ethMapper.selectDayCountList();
       if(list.size()>0){
        //存入redis 先删除原有数据再重新插入数据
        redisService.deleteObject("ethBigTxDayCount");
        redisService.setCacheList("ethBigTxDayCount",list);
       }
    }

    /** USDT每日数据量的数据 */
    @Scheduled(cron = "0 5 * * * ?")
    public void USDTDataCountToRedis(){

        List<CountDto> list = ethMapper.selectUSDTDayCountList();
        if(list.size()>0){
            //存入redis 先删除原有数据再重新插入数据
            redisService.deleteObject("USDTBigTxDayCount");
            redisService.setCacheList("USDTBigTxDayCount",list);
        }
    }

    /** USDC每日数据量的数据 */
    @Scheduled(cron = "0 5 * * * ?")
    public void USDCCountToRedis(){

        List<CountDto> list = ethMapper.selectUSDCDayCountList();
        if(list.size()>0){
            //存入redis 先删除原有数据再重新插入数据
            redisService.deleteObject("USDCBigTxDayCount");
            redisService.setCacheList("USDCBigTxDayCount",list);
        }
    }

    /** BUSD每日数据量的数据 */
    @Scheduled(cron = "0 5 * * * ?")
    public void BUSDCountToRedis(){

        List<CountDto> list = ethMapper.selectBUSDDayCountList();
        if(list.size()>0){
            //存入redis 先删除原有数据再重新插入数据
            redisService.deleteObject("BUSDBigTxDayCount");
            redisService.setCacheList("BUSDBigTxDayCount",list);
        }
    }

    /** USDT每日数据量监控的数据 */
    @Scheduled(cron = "0 5 * * * ?")
    public void TronUSDTDataCountToRedis(){

        List<CountDto> list = ethMapper.selectTronUSDTDayCountList();

        if(list.size()>0){
            //存入redis 先删除原有数据再重新插入数据
            redisService.deleteObject("TronUSDTBigTxDayCount");
            redisService.setCacheList("TronUSDTBigTxDayCount",list);
        }
    }

    /** USDC每日数据量监控的数据 */
    @Scheduled(cron = "0 5 * * * ?")
    public void TronUSDCDataCountToRedis(){

        List<CountDto> list = ethMapper.selectTronUSDCDayCountList();
        if(list.size()>0){
            //存入redis 先删除原有数据再重新插入数据
            redisService.deleteObject("TronUSDCBigTxDayCount");
            redisService.setCacheList("TronUSDCBigTxDayCount",list);
        }
    }

    /** 最近一月数据量的数据 */
    @Scheduled(cron = "0 7 0/4 * * ?")
    public void USDTVolumeDataCountToRedis(){

        USDVolumeDto dto = ethMapper.selectLastUSDTVolume();

        QueryVo queryVo = new QueryVo();
        Calendar ca = new GregorianCalendar();
        ca.setTime(dto.getDateTime());
        ca.add(ca.HOUR,-8);
        ca.add(ca.MONTH,-1);
        queryVo.setStart(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,ca.getTime()));
        List<USDVolumeDto> list = ethMapper.getUSDTVolumeList(queryVo);
        if(list.size()>0){
            //存入redis 先删除原有数据再重新插入数据
            redisService.deleteObject("USDTVolumeLastMonth");
            redisService.setCacheList("USDTVolumeLastMonth",list);
       }
    }

    /** 最近一月数据量的数据 */
    @Scheduled(cron = "0 7 0/4 * * ?")
    public void USDCVolumeDataCountToRedis(){

        USDVolumeDto dto = ethMapper.selectLastUSDCVolume();
        QueryVo queryVo = new QueryVo();
        Calendar ca = new GregorianCalendar();
        ca.setTime(dto.getDateTime());
        ca.add(ca.HOUR,-8);
        ca.add(ca.MONTH,-1);
        queryVo.setStart(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,ca.getTime()));
        List<USDVolumeDto> list = ethMapper.getUSDCVolumeList(queryVo);
        if(list.size()>0){
            //存入redis 先删除原有数据再重新插入数据
            redisService.deleteObject("USDCVolumeLastMonth");
            redisService.setCacheList("USDCVolumeLastMonth",list);
       }
    }

    /** 最近一月数据量的数据 */
    @Scheduled(cron = "0 7 0/4 * * ?")
    public void BUSDVolumeDataCountToRedis(){

        USDVolumeDto dto = ethMapper.selectLastBUSDVolume();
        QueryVo queryVo = new QueryVo();
        Calendar ca = new GregorianCalendar();
        ca.setTime(dto.getDateTime());
        ca.add(ca.HOUR,-8);
        ca.add(ca.MONTH,-1);
        queryVo.setStart(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,ca.getTime()));
        List<USDVolumeDto> list = ethMapper.getBUSDVolumeList(queryVo);
        if(list.size()>0){
            //存入redis 先删除原有数据再重新插入数据
            redisService.deleteObject("BUSDVolumeLastMonth");
            redisService.setCacheList("BUSDVolumeLastMonth",list);
        }
    }

    /** 最近一月数据量的数据 */
    @Scheduled(cron = "0 7 0/4 * * ?")
    public void TronUSDTVolumeDataToRedis(){
        TronVolumeDto dto = ethMapper.selectLastTronUSDTVolume();
        QueryVo queryVo = new QueryVo();
        Calendar ca = new GregorianCalendar();
        ca.setTime(dto.getDateTime());
        ca.add(ca.MONTH,-1);
        queryVo.setStart(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,ca.getTime()));
        List<TronUSDTVolume> list = ethMapper.getTronUSDTVolumeList(queryVo);
        if (list.size() < 30){
            list = ethMapper.getTronUSDTVolumeLastMonthList();
            Collections.reverse(list);
        }
        if(list.size()>0){
            //存入redis 先删除原有数据再重新插入数据
            redisService.deleteObject("TronUSDTVolumeLastMonth");
            redisService.setCacheList("TronUSDTVolumeLastMonth",list);
        }
    }

    /** 最近一月数据量的数据 */
    @Scheduled(cron = "0 7 0/4 * * ?")
    public void TronUSDCVolumeDataToRedis(){
        TronVolumeDto dto = ethMapper.selectLastTronUSDCVolume();
        QueryVo queryVo = new QueryVo();
        Calendar ca = new GregorianCalendar();
        ca.setTime(dto.getDateTime());
        ca.add(ca.MONTH,-1);
        queryVo.setStart(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,ca.getTime()));
        List<TronUSDCVolume> list = ethMapper.getTronUSDCVolumeList(queryVo);
        if (list.size() < 30){
            list = ethMapper.getTronUSDCVolumeLastMonthList(queryVo);
            Collections.reverse(list);
        }
        if(list.size()>0){
            //存入redis 先删除原有数据再重新插入数据
            redisService.deleteObject("TronUSDCVolumeLastMonth");
            redisService.setCacheList("TronUSDCVolumeLastMonth",list);
        }
    }

    /** ETHToken大额交易每日数据量 */
    @Scheduled(cron = "0 0/5 * * * ?")
    public void ETHTokenBigtxCountToRedis(){

        List<CountDto> list = ethMapper.selectETHTokenBigtxDayCountList();
        if(list.size()>0){
            //存入redis 先删除原有数据再重新插入数据
            redisService.deleteObject("ETHTokenBigTxDayCount");
            redisService.setCacheList("ETHTokenBigTxDayCount",list);
        }
    }

    /** eth erc20 token每日交易量abbr列表数据 */
    @Scheduled(cron = "0 0/5 * * * ?")
    public void ETHTokenVolumeAbbrListToRedis(){

        List<ETHTokenVolumeAbbrsDto> abbrs = ethMapper.getETHTokenVolumeAbbrs();

        for (ETHTokenVolumeAbbrsDto dto : abbrs) {
            dto.setList(getListByAbbr(dto.getAbbr()));
        }

        if(abbrs.size()>0){
            //存入redis 先删除原有数据再重新插入数据
            //System.out.println("代币交易量redis更新数据条数："+abbrs.size());
            redisService.deleteObject("ETHTokenVolumeAbbrList");
            redisService.setCacheList("ETHTokenVolumeAbbrList",abbrs);
        }
    }

    public List<ETHTokenVolumeDto> getListByAbbr(String abbr){

        ETHTokenVolumeVo vo = new ETHTokenVolumeVo();
        vo.setAbbr(abbr);

        List<ETHTokenVolumeDto> list = ethMapper.getETHTokenVolumeList(vo);

        for (int i = 0 ; i < list.size(); i++){
            ETHTokenVolumeDto entry = list.get(i);
            if(entry.getVolume() > 1*Math.pow(10,20) ){
                if(i > 1){
                    entry.setVolume(list.get(i-1).getVolume());
                }else{
                    entry.setVolume(0.0);
                }
            }
        }

        return list;
    }

    /** eth 流通供应量历史数据 */
    @Scheduled(cron = "0 9 0/4 * * ?")
    //@Scheduled(cron = "0 0/2 * * * ?")
    public void CirculatingSupplyToRedis(){

        QueryVo vo = new QueryVo();
        List<DateBigDecimalDto> list = ethV2Mapper.getCsupplyList(vo);


        if(list.size() > 0){
            //存入redis 先删除原有数据再重新插入数据
            redisService.deleteObject("ethCSupplyList");
            redisService.setCacheList("ethCSupplyList",list);
        }
    }

    /** eth 抵押奖励历史数据 */
    @Scheduled(cron = "0 9 0/4 * * ?")
    //@Scheduled(cron = "0 0/2 * * * ?")
    public void RewardToRedis(){

        QueryVo vo = new QueryVo();
        List<DateBigDecimalDto> list = ethMapper.getRewardList(vo);

        if(list.size() > 0){
            //存入redis 先删除原有数据再重新插入数据
            redisService.deleteObject("ethRewardList");
            redisService.setCacheList("ethRewardList",list);
        }
    }

    /** eth 地址历史数据 */
    @Scheduled(cron = "0 9 0/4 * * ?")
    //@Scheduled(cron = "0 0/2 * * * ?")
    public void AddressToRedis(){

        QueryVo vo = new QueryVo();
        List<ETHAddressDto> list = ethMapper.getAddressList(vo);

        if(list.size() > 0){
            //存入redis 先删除原有数据再重新插入数据
            redisService.deleteObject("ethAddressList");
            redisService.setCacheList("ethAddressList",list);
        }
    }

    /** eth 交易量历史数据 */
    @Scheduled(cron = "0 11 0/4 * * ?")
    //@Scheduled(cron = "0 0/2 * * * ?")
    public void VolumeToRedis(){

        QueryVo vo = new QueryVo();
        List<DateBigDecimalDto> list = ethMapper.getVolumeList(vo);

        if(list.size() > 0){
            //存入redis 先删除原有数据再重新插入数据
            redisService.deleteObject("ethVolumeList");
            redisService.setCacheList("ethVolumeList",list);
        }
    }

    /** eth 新合约数历史数据 */
    @Scheduled(cron = "0 11 0/4 * * ?")
    //@Scheduled(cron = "0 0/2 * * * ?")
    public void ContractNewToRedis(){

        QueryVo vo = new QueryVo();
        List<DateBigDecimalDto> list = ethMapper.getCnewList(vo);

        if(list.size() > 0){
            //存入redis 先删除原有数据再重新插入数据
            redisService.deleteObject("ethContractNewList");
            redisService.setCacheList("ethContractNewList",list);
        }
    }



    /** eth token 交易量历史数据 */
    @Scheduled(cron = "0 11 0/4 * * ?")
    //@Scheduled(cron = "0 0/2 * * * ?")
    public void TokenVolumeToRedis(){

        QueryVo vo = new QueryVo();
        List<ComDataDto> list = ethMapper.getTokenVolumeList(vo);

        if(list.size() > 0){
            //存入redis 先删除原有数据再重新插入数据
            redisService.deleteObject("ethTokenVolumeList");
            redisService.setCacheList("ethTokenVolumeList",list);
        }
    }
}
