package com.m2pool.pool.task;

import com.m2pool.common.core.utils.DateUtils;
import com.m2pool.common.redis.service.RedisService;
import com.m2pool.pool.dto.BlockInfoDto;
import com.m2pool.pool.mapper.PoolMapper;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

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

/**
 * @Description TODO
 * @Date 2024/6/14 14:03
 * @Author dy
 */
@Data
@Configuration
@ConfigurationProperties(prefix = "task.pool")
@EnableScheduling
public class PoolBlkStatsTask {

    private boolean enable;

    @Autowired
    private PoolMapper poolMapper;

    @Autowired
    private RedisService redisService;

    @Scheduled(cron = "0 0/2 * * * ?")
    public void NEXABlockInfoToRedis(){
        if(!enable){
            System.out.println("PoolBlkStatsTask 定时任务已关闭，请在nacos修改配置");
            return;
        }

        boolean result = NEXABlockInfo();

        int  count = 1;
        //执行失败返回false 再次执行
        while (!result){
            if(count >= 3){
                break;
            }
            result = NEXABlockInfo();
            count++;
        }
    }

    @Scheduled(cron = "5 0/2 * * * ?")
    public void GRSBlockInfoToRedis(){
        if(!enable){
            return;
        }
        boolean result = GRSBlockInfo();

        int  count = 1;
        //执行失败返回false 再次执行
        while (!result){
            if(count >= 3){
                break;
            }
            result = GRSBlockInfo();
            count++;
        }
    }

    @Scheduled(cron = "10 0/2 * * * ?")
    public void MONABlockInfoToRedis(){
        if(!enable){
            return;
        }
        boolean result = MONABlockInfo();

        int  count = 1;
        //执行失败返回false 再次执行
        while (!result){
            if(count >= 3){
                break;
            }
            result = MONABlockInfo();
            count++;
        }
    }

    @Scheduled(cron = "15 0/2 * * * ?")
    public void DGBOBlockInfoToRedis(){
        if(!enable){
            return;
        }
        boolean result = DGBOBlockInfo();

        int  count = 1;
        //执行失败返回false 再次执行
        while (!result){
            if(count >= 3){
                break;
            }
            result = DGBOBlockInfo();
            count++;
        }
    }

    @Scheduled(cron = "20 0/2 * * * ?")
    public void DGBQBlockInfoToRedis(){
        if(!enable){
            return;
        }
        boolean result = DGBQBlockInfo();

        int  count = 1;
        //执行失败返回false 再次执行
        while (!result){
            if(count >= 3){
                break;
            }
            result = DGBQBlockInfo();
            count++;
        }
    }

    @Scheduled(cron = "25 0/2 * * * ?")
    public void DGBSBlockInfoToRedis(){
        if(!enable){
            return;
        }
        boolean result = DGBSBlockInfo();

        int  count = 1;
        //执行失败返回false 再次执行
        while (!result){
            if(count >= 3){
                break;
            }
            result = DGBSBlockInfo();
            count++;
        }
    }

    @Scheduled(cron = "30 0/2 * * * ?")
    public void RXDBlockInfoToRedis(){
        if(!enable){
            return;
        }
        boolean result = RXDBlockInfo();

        int  count = 1;
        //执行失败返回false 再次执行
        while (!result){
            if(count >= 3){
                break;
            }
            result = RXDBlockInfo();
            count++;
        }
    }

    @Scheduled(cron = "35 0/2 * * * ?")
    public void ALPHBlockInfoToRedis(){
        if(!enable){
            return;
        }
        boolean result = ALPHBlockInfo();

        int  count = 1;
        //执行失败返回false 再次执行
        while (!result){
            if(count >= 3){
                break;
            }
            result = ALPHBlockInfo();
            count++;
        }
    }

    @Scheduled(cron = "40 0/2 * * * ?")
    public void ENXBlockInfoToRedis(){
        if(!enable){
            return;
        }
        boolean result = ENXBlockInfo();

        int  count = 1;
        //执行失败返回false 再次执行
        while (!result){
            if(count >= 3){
                break;
            }
            result = ENXBlockInfo();
            count++;
        }
    }


    @Scheduled(cron = "45 0/2 * * * ?")
    public void MONEROBlockInfoToRedis(){
        if(!enable){
            return;
        }

        boolean result = MONEROBlockInfo();

        int  count = 1;
        //执行失败返回false 再次执行
        while (!result){
            if(count >= 3){
                break;
            }
            result = MONEROBlockInfo();
            count++;
        }
    }
    @Scheduled(cron = "50 0/2 * * * ?")
    public void XTMBlockInfoToRedis(){
        if(!enable){
            return;
        }

        boolean result = XTMBlockInfo();

        int  count = 1;
        //执行失败返回false 再次执行
        while (!result){
            if(count >= 3){
                break;
            }
            result = XTMBlockInfo();
            count++;
        }
    }

    public boolean NEXABlockInfo(){
        try {
            String lastDate= "";

            if(redisService.hasKey("nexa_pool_last_date")){
                lastDate = (String) redisService.getCacheObject("nexa_pool_last_date");
                System.out.println("redis中的lastDate:"+lastDate);
            }else {
                List<BlockInfoDto> list = poolMapper.getNEXABlockInfoList();
                if (list.size() > 0){
                    lastDate =DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,list.get(0).getDate());
                }else {
                    lastDate = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,new Date());
                }
            }
            System.out.println("最终的lastDate:"+lastDate);

            List<BlockInfoDto> list = poolMapper.getNewNEXABlockInfoList(lastDate);

            if(list.size() > 0){
                //存入数据库
                poolMapper.batchInsertNexaPoolBlkToDB(list);
                //把最大时间存入redis
                String maxDate =DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,list.get(list.size()-1).getDate());
                redisService.setCacheObject("nexa_pool_last_date",maxDate);
            }
            return true;
        }catch (Exception e){
            System.out.println("出错内容:"+e);
            return false;
        }
    }

    public boolean GRSBlockInfo(){
        try {
            String lastDate= "";

            if(redisService.hasKey("grs_pool_last_date")){
                lastDate = (String) redisService.getCacheObject("grs_pool_last_date");
                System.out.println("redis中的grs lastDate:"+lastDate);
            }else {
                List<BlockInfoDto> list = poolMapper.getGRSBlockInfoList();
                if (list.size() > 0){
                    lastDate =DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,list.get(0).getDate());
                }else {
                    lastDate = "2024-10-01";
                }
            }
            System.out.println("grs最终的lastDate:"+lastDate);

            List<BlockInfoDto> list = poolMapper.getNewGRSBlockInfoList(lastDate);

            if(list.size() > 0){
                //存入数据库
                poolMapper.batchInsertGRSPoolBlkToDB(list);
                //把最大时间存入redis
                String maxDate =DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,list.get(list.size()-1).getDate());
                redisService.setCacheObject("grs_pool_last_date",maxDate);
            }
            return true;
        }catch (Exception e){
            System.out.println("出错内容:"+e);
            return false;
        }
    }

    public boolean MONABlockInfo(){
        try {
            String lastDate= "";

            if(redisService.hasKey("mona_pool_last_date")){
                lastDate = (String) redisService.getCacheObject("mona_pool_last_date");
                System.out.println("redis中的mona lastDate:"+lastDate);
            }else {
                List<BlockInfoDto> list = poolMapper.getMonaBlockInfoList();
                if (list.size() > 0){
                    lastDate =DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,list.get(0).getDate());
                }else {
                    lastDate = "2024-10-01";
                }
            }
            System.out.println("mona最终的lastDate:"+lastDate);

            List<BlockInfoDto> list = poolMapper.getNewMONABlockInfoList(lastDate);

            if(list.size() > 0){
                //存入数据库
                poolMapper.batchInsertMONAPoolBlkToDB(list);
                //把最大时间存入redis
                String maxDate =DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,list.get(list.size()-1).getDate());
                redisService.setCacheObject("mona_pool_last_date",maxDate);
            }
            return true;
        }catch (Exception e){
            System.out.println("出错内容:"+e);
            return false;
        }
    }

    public boolean DGBOBlockInfo(){
        try {
            String lastDate= "";

            if(redisService.hasKey("dgbo_pool_last_date")){
                lastDate = (String) redisService.getCacheObject("dgbo_pool_last_date");
                System.out.println("redis中的dgbo lastDate:"+lastDate);
            }else {
                List<BlockInfoDto> list = poolMapper.getDGBODOBlockInfoList();
                if (list.size() > 0){
                    lastDate =DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,list.get(0).getDate());
                }else {
                    lastDate = "2024-10-01";
                }
            }
            System.out.println("DGBO最终的lastDate:"+lastDate);

            List<BlockInfoDto> list = poolMapper.getNewDGBOBlockInfoList(lastDate);

            if(list.size() > 0){
                //存入数据库
                poolMapper.batchInsertDGBOPoolBlkToDB(list);
                //把最大时间存入redis
                String maxDate =DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,list.get(list.size()-1).getDate());
                redisService.setCacheObject("dgbo_pool_last_date",maxDate);
            }
            return true;
        }catch (Exception e){
            System.out.println("出错内容:"+e);
            return false;
        }
    }

    public boolean DGBQBlockInfo(){
        try {
            String lastDate= "";

            if(redisService.hasKey("dgbq_pool_last_date")){
                lastDate = (String) redisService.getCacheObject("dgbq_pool_last_date");
                System.out.println("redis中的dgbq lastDate:"+lastDate);
            }else {
                List<BlockInfoDto> list = poolMapper.getDgbQubitBlockInfoList();
                if (list.size() > 0){
                    lastDate =DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,list.get(0).getDate());
                }else {
                    lastDate = "2024-10-01";
                }
            }
                System.out.println("DGBQ最终的lastDate:"+lastDate);

            List<BlockInfoDto> list = poolMapper.getNewDGBQBlockInfoList(lastDate);

            if(list.size() > 0){
                //存入数据库
                poolMapper.batchInsertDGBQPoolBlkToDB(list);
                //把最大时间存入redis
                String maxDate =DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,list.get(list.size()-1).getDate());
                redisService.setCacheObject("dgbq_pool_last_date",maxDate);
            }
            return true;
        }catch (Exception e){
            System.out.println("出错内容:"+e);
            return false;
        }
    }

    public boolean DGBSBlockInfo(){
        try {
            String lastDate= "";

            if(redisService.hasKey("dgbs_pool_last_date")){
                lastDate = (String) redisService.getCacheObject("dgbs_pool_last_date");
                System.out.println("redis中的dgbs lastDate:"+lastDate);
            }else {
                List<BlockInfoDto> list = poolMapper.getDgbSkeinBlockInfoList();
                if (list.size() > 0){
                    lastDate =DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,list.get(0).getDate());
                }else {
                    lastDate = "2024-10-01";
                }
            }
            System.out.println("DGBS最终的lastDate:"+lastDate);

            List<BlockInfoDto> list = poolMapper.getNewDGBSBlockInfoList(lastDate);

            if(list.size() > 0){
                //存入数据库
                poolMapper.batchInsertDGBSPoolBlkToDB(list);
                //把最大时间存入redis
                String maxDate =DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,list.get(list.size()-1).getDate());
                redisService.setCacheObject("dgbs_pool_last_date",maxDate);
            }
            return true;
        }catch (Exception e){
            System.out.println("出错内容:"+e);
            return false;
        }
    }

    public boolean RXDBlockInfo(){
        try {
            String lastDate= "";

            if(redisService.hasKey("rxd_pool_last_date")){
                lastDate = (String) redisService.getCacheObject("rxd_pool_last_date");
                System.out.println("redis中的rxd lastDate:"+lastDate);
            }else {
                List<BlockInfoDto> list = poolMapper.getRXDBlockInfoList();
                if (list.size() > 0){
                    lastDate =DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,list.get(0).getDate());
                }else {
                    lastDate = "2024-10-01";
                }
            }
            System.out.println("rxd最终的lastDate:"+lastDate);

            List<BlockInfoDto> list = poolMapper.getNewRXDBlockInfoList(lastDate);

            if(list.size() > 0){
                //存入数据库
                poolMapper.batchInsertRXDPoolBlkToDB(list);
                //把最大时间存入redis
                String maxDate =DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,list.get(list.size()-1).getDate());
                redisService.setCacheObject("rxd_pool_last_date",maxDate);
            }
            return true;
        }catch (Exception e){
            System.out.println("出错内容:"+e);
            return false;
        }
    }

    public boolean ALPHBlockInfo(){
        try {
            String lastDate= "";

            if(redisService.hasKey("alph_pool_last_date")){
                lastDate = (String) redisService.getCacheObject("alph_pool_last_date");
                System.out.println("redis中的alph lastDate:"+lastDate);
            }else {
                List<BlockInfoDto> list = poolMapper.getALPHBlockInfoList();
                if (list.size() > 0){
                    lastDate =DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,list.get(0).getDate());
                }else {
                    lastDate = "2024-10-01";
                }
            }
            System.out.println("rxd最终的lastDate:"+lastDate);

            List<BlockInfoDto> list = poolMapper.getNewALPHBlockInfoList(lastDate);

            if(list.size() > 0){
                //存入数据库
                //alph 报块奖励需要除以10的18次方
                list.stream().forEach(e ->{
                    if(e.getReward() !=0){
                        BigDecimal reward = BigDecimal.valueOf(e.getReward()).divide(BigDecimal.valueOf(1000 * 1000), 6, BigDecimal.ROUND_HALF_UP)
                                .divide(BigDecimal.valueOf(1000*1000), 12, BigDecimal.ROUND_HALF_UP)
                                .divide(BigDecimal.valueOf(1000*1000), 8, BigDecimal.ROUND_HALF_UP);
                        e.setReward(reward.doubleValue());
                    }
                });
                //poolMapper.batchInsertRXDPoolBlkToDB(list);
                poolMapper.batchInsertALPHPoolBlkToDB(list);
                //把最大时间存入redis
                String maxDate =DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,list.get(list.size()-1).getDate());
                redisService.setCacheObject("alph_pool_last_date",maxDate);
            }
            return true;
        }catch (Exception e){
            System.out.println("出错内容:"+e);
            return false;
        }
    }

    public boolean ENXBlockInfo(){
        try {
            String lastDate= "";

            if(redisService.hasKey("enx_pool_last_date")){
                lastDate = (String) redisService.getCacheObject("enx_pool_last_date");
                System.out.println("redis中的enx lastDate:"+lastDate);
            }else {
                List<BlockInfoDto> list = poolMapper.getENXBlockInfoList();
                if (list.size() > 0){
                    lastDate =DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,list.get(0).getDate());
                }else {
                    lastDate = "2024-10-01";
                }
            }
            System.out.println("enx最终的lastDate:"+lastDate);

            List<BlockInfoDto> list = poolMapper.getNewENXBlockInfoList(lastDate);

            if(list.size() > 0){
                //存入数据库
                //poolMapper.batchInsertRXDPoolBlkToDB(list);
                poolMapper.batchInsertENXPoolBlkToDB(list);
                //把最大时间存入redis
                String maxDate =DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,list.get(list.size()-1).getDate());
                redisService.setCacheObject("enx_pool_last_date",maxDate);
            }
            return true;
        }catch (Exception e){
            System.out.println("出错内容:"+e);
            return false;
        }
    }

    public boolean MONEROBlockInfo(){
        try {
            String lastDate= "";

            if(redisService.hasKey("monero_pool_last_date")){
                lastDate = (String) redisService.getCacheObject("monero_pool_last_date");
                System.out.println("redis中的lastDate:"+lastDate);
            }else {
                List<BlockInfoDto> list = poolMapper.getMONEROBlockInfoList();
                if (list.size() > 0){
                    lastDate =DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,list.get(0).getDate());
                }else {
                    lastDate = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,new Date());
                }
            }
            System.out.println("最终的lastDate:"+lastDate);

            List<BlockInfoDto> list = poolMapper.getNewMONEROBlockInfoList(lastDate);

            if(!list.isEmpty()){
                //存入数据库
                poolMapper.batchInsertNexaPoolBlkToDB(list);
                //把最大时间存入redis
                String maxDate =DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,list.get(list.size()-1).getDate());
                redisService.setCacheObject("monero_pool_last_date",maxDate);
            }
            return true;
        }catch (Exception e){
            System.out.println("出错内容:"+e);
            return false;
        }
    }

    public boolean XTMBlockInfo(){
        try {
            String lastDate= "";

            if(redisService.hasKey("sha3x_pool_last_date")){
                lastDate = (String) redisService.getCacheObject("sha3x_pool_last_date");
                System.out.println("redis中的lastDate:"+lastDate);
            }else {
                List<BlockInfoDto> list = poolMapper.getXtmBlockInfoList();
                if (list.size() > 0){
                    lastDate =DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,list.get(0).getDate());
                }else {
                    lastDate = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,new Date());
                }
            }
            System.out.println("最终的lastDate:"+lastDate);

            List<BlockInfoDto> list = poolMapper.getNewXtmBlockInfoList(lastDate);

            if(!list.isEmpty()){
                //存入数据库
                poolMapper.batchInsertNexaPoolBlkToDB(list);
                //把最大时间存入redis
                String maxDate =DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,list.get(list.size()-1).getDate());
                redisService.setCacheObject("sha3x_pool_last_date",maxDate);
            }
            return true;
        }catch (Exception e){
            System.out.println("出错内容:"+e);
            return false;
        }
    }

}
