package com.m2pool.pool.task;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.m2pool.common.core.utils.DateUtils;
import com.m2pool.common.core.utils.StringUtils;
import com.m2pool.common.redis.service.RedisService;
import com.m2pool.pool.dto.BlockInfoDto;
import com.m2pool.pool.entity.BlockInfo;
import com.m2pool.pool.enums.PoolCalParamConfig;
import com.m2pool.pool.mapper.PoolMapper;
import com.m2pool.pool.utils.NodeRpc;
import com.m2pool.pool.vo.DateValueVo;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.Map;

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

    private boolean enable;

    private static final Logger log = LoggerFactory.getLogger(NodeTask.class);

    @Autowired
    private PoolMapper poolMapper;

    @Autowired
    private RedisService redisService;


    @Autowired
    private TransactionTemplate transactionTemplate;


    //@Scheduled(cron = "0 0/2 * * * ?")
    @Scheduled(cron = "5 0,30 * * * ?")
    public void NEXABlockInfoToRedisAndDB(){
        if(!enable){

            return;
        }
        BlockInfo blockInfo = NodeRpc.getBlock("nexa");

        int  count = 1;
        while (StringUtils.isNull(blockInfo)){
            if(count >= 10){
                break;
            }
            blockInfo = NodeRpc.getBlock("nexa");
            count++;
        }
        System.out.println("blockInfo 的信息"+blockInfo);
        if(StringUtils.isNotNull(blockInfo)){
            if(StringUtils.isNotNull(blockInfo.getPower())){
                redisService.setCacheObject("nexa_block",blockInfo);
                //存入数据库
                DateValueVo vo = new DateValueVo();
                Date now = new Date();
                int minute = (now.getMinutes() / 30) * 30;
                now.setMinutes( minute);
                now.setSeconds(0);
                vo.setDate(now);
                String date = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, now);
                System.out.println("nexa全网算力数据入库时间:"+date);
                vo.setValue(blockInfo.getPower());
                poolMapper.insertNetPower("nexa_net_power",date,vo);
            }
        }
    }

    @Scheduled(cron = "0 0/1 * * * ?")
    public void NEXABlockInfoToRedis(){
        if(!enable){
            System.out.println("NodeTask 定时任务已关闭，请在nacos修改配置");
            return;
        }
        BlockInfo blockInfo = NodeRpc.getBlock("nexa");
        int  count = 0;
        while (StringUtils.isNull(blockInfo)){
            if(count >= 3){
                break;
            }
            blockInfo = NodeRpc.getBlock("nexa");
            count++;
        }

        if(StringUtils.isNotNull(blockInfo)){
            if(StringUtils.isNotNull(blockInfo.getPower())){

                if (redisService.hasKey("nexa_block")){
                    redisService.deleteObject("nexa_block");
                    redisService.setCacheObject("nexa_block",blockInfo);
                }else {
                    redisService.setCacheObject("nexa_block",blockInfo);
                }

            }
        }
    }

    @Scheduled(cron = "10 0,30 * * * ?")
    public void GRSBlockInfoToRedisAndDB(){
        if(!enable){
            return;
        }
        BlockInfo blockInfo = NodeRpc.getBlock("grs");

        int  count = 1;
        while (StringUtils.isNull(blockInfo)){
            if(count >= 10){
                break;
            }
            blockInfo = NodeRpc.getBlock("grs");
            count++;
        }

        if(StringUtils.isNotNull(blockInfo)){
            if(StringUtils.isNotNull(blockInfo.getPower())){
                redisService.setCacheObject("grs_block",blockInfo);
                //存入数据库
                DateValueVo vo = new DateValueVo();
                Date now = new Date();
                int minute = (now.getMinutes() / 30) * 30;
                now.setMinutes( minute);
                now.setSeconds(0);
                vo.setDate(now);
                String date = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, now);
                vo.setValue(blockInfo.getPower());
                poolMapper.insertNetPower("grs_net_power",date,vo);
            }
        }
    }

    @Scheduled(cron = "2 0/1 * * * ?")
    public void GRSBlockInfoToRedis(){
        if(!enable){
            return;
        }
            BlockInfo blockInfo = NodeRpc.getBlock("grs");
            if(StringUtils.isNotNull(blockInfo)){
                if(StringUtils.isNotNull(blockInfo.getPower())){

                    if (redisService.hasKey("grs_block")){
                        redisService.deleteObject("grs_block");
                        redisService.setCacheObject("grs_block",blockInfo);
                    }else {
                        redisService.setCacheObject("grs_block",blockInfo);
                    }

                }
            }
            else {
                System.out.println("未从节点获取到grs最新数据 ："+DateUtils.dateTimeNow());
            }
    }

    @Scheduled(cron = "15 0,30 * * * ?")
    public void MONABlockInfoToRedisAndDB(){
        if(!enable){
            return;
        }
        BlockInfo blockInfo = NodeRpc.getBlock("mona");

        int  count = 1;
        while (StringUtils.isNull(blockInfo)){
            if(count >= 10){
                break;
            }
            blockInfo = NodeRpc.getBlock("mona");
            count++;
        }

        if(StringUtils.isNotNull(blockInfo)){
            if(StringUtils.isNotNull(blockInfo.getPower())){
                redisService.setCacheObject("mona_block",blockInfo);
                //存入数据库
                DateValueVo vo = new DateValueVo();
                Date now = new Date();
                int minute = (now.getMinutes() / 30) * 30;
                now.setMinutes( minute);
                now.setSeconds(0);
                vo.setDate(now);
                String date = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, now);
                System.out.println("mona 全网算力数据入库时间:"+date);
                vo.setValue(blockInfo.getPower());
                poolMapper.insertNetPower("mona_net_power",date,vo);
            }
        }
    }

    @Scheduled(cron = "4 0/1 * * * ?")
    public void MONABlockInfoToRedis(){
        if(!enable){
            return;
        }
        BlockInfo blockInfo = NodeRpc.getBlock("mona");

        if(StringUtils.isNotNull(blockInfo)){
            if(StringUtils.isNotNull(blockInfo.getPower())){

                if (redisService.hasKey("mona_block")){
                    redisService.deleteObject("mona_block");
                    redisService.setCacheObject("mona_block",blockInfo);
                }else {
                    redisService.setCacheObject("mona_block",blockInfo);
                }

            }
        }else {
            System.out.println("未从节点获取到mona最新数据 ："+DateUtils.dateTimeNow());
        }

    }

    @Scheduled(cron = "20 0,30 * * * ?")
    public void DGBBlockInfoToRedisAndDB(){
        if(!enable){
            return;
        }
        Map<String, BlockInfo> dgbMap = NodeRpc.getDGBBlock();
        int  count = 1;
        while (StringUtils.isNull(dgbMap)){
            if(count >= 10){
                break;
            }
            dgbMap = NodeRpc.getDGBBlock();
            count++;
        }
        if(StringUtils.isNotNull(dgbMap)){
            BlockInfo dgbo = dgbMap.getOrDefault("dgbo", null);
            if(StringUtils.isNotNull(dgbo)){
                if(StringUtils.isNotNull(dgbo.getPower())){
                    redisService.setCacheObject("dgbo_block",dgbo);
                    //存入数据库
                    DateValueVo vo = new DateValueVo();
                    Date now = new Date();
                    int minute = (now.getMinutes() / 30) * 30;
                    now.setMinutes( minute);
                    now.setSeconds(0);
                    vo.setDate(now);
                    String date = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, now);
                    System.out.println("dgbo 全网算力数据入库时间:"+date);
                    vo.setValue(dgbo.getPower());
                    poolMapper.insertNetPower("dgbo_net_power",date,vo);
                }else {
                    System.out.println("未获取到dgbo的power");
                }
            }
            else {
                System.out.println("未获取到dgbo");
            }

            BlockInfo dgbq = dgbMap.getOrDefault("dgbq", null);
            if(StringUtils.isNotNull(dgbq)){
                if(StringUtils.isNotNull(dgbo.getPower())){
                    redisService.setCacheObject("dgbq_block",dgbq);
                    //存入数据库
                    DateValueVo vo = new DateValueVo();
                    Date now = new Date();
                    int minute = (now.getMinutes() / 30) * 30;
                    now.setMinutes( minute);
                    now.setSeconds(0);
                    vo.setDate(now);
                    String date = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, now);
                    System.out.println("dgbq 全网算力数据入库时间:"+date);
                    vo.setValue(dgbq.getPower());
                    poolMapper.insertNetPower("dgbq_net_power",date,vo);
                }
                else {
                    System.out.println("未获取到dgbo的power");
                }
            }
            else {
                System.out.println("未获取到dgbq");
            }

            BlockInfo dgbs = dgbMap.getOrDefault("dgbs", null);
            if(StringUtils.isNotNull(dgbs)){
                if(StringUtils.isNotNull(dgbs.getPower())){
                    redisService.setCacheObject("dgbs_block",dgbs);
                    //存入数据库
                    DateValueVo vo = new DateValueVo();
                    Date now = new Date();
                    int minute = (now.getMinutes() / 30) * 30;
                    now.setMinutes( minute);
                    now.setSeconds(0);
                    vo.setDate(now);
                    String date = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, now);
                    System.out.println("dgbs 全网算力数据入库时间:"+date);
                    vo.setValue(dgbs.getPower());
                    poolMapper.insertNetPower("dgbs_net_power",date,vo);
                }
                else {
                    System.out.println("未获取到dgbs的power");
                }
            }else {
                System.out.println("未获取到dgbs");
            }

        }else {
            System.out.println("未从节点获取到dgpMap最新数据 ："+DateUtils.dateTimeNow());
        }
    }

    @Scheduled(cron = "6 0/1 * * * ?")
    public void DGBBlockInfoToRedis(){
        if(!enable){
            return;
        }
        Map<String, BlockInfo> dgbMap = NodeRpc.getDGBBlock();
        if(StringUtils.isNotNull(dgbMap)){
            BlockInfo dgbo = dgbMap.getOrDefault("dgbo", null);
            if(StringUtils.isNotNull(dgbo)){

                if (redisService.hasKey("dgbo_block")){
                    redisService.deleteObject("dgbo_block");
                    redisService.setCacheObject("dgbo_block",dgbo);
                }else {
                    redisService.setCacheObject("dgbo_block",dgbo);
                }

            }
            BlockInfo dgbq = dgbMap.getOrDefault("dgbq", null);
            if(StringUtils.isNotNull(dgbq)){

                if (redisService.hasKey("dgbq_block")){
                    redisService.deleteObject("dgbq_block");
                    redisService.setCacheObject("dgbq_block",dgbq);
                }else {
                    redisService.setCacheObject("dgbq_block",dgbq);
                }

            }
            BlockInfo dgbs = dgbMap.getOrDefault("dgbs", null);
            if(StringUtils.isNotNull(dgbs)){

                if (redisService.hasKey("dgbs_block")){
                    redisService.deleteObject("dgbs_block");
                    redisService.setCacheObject("dgbs_block",dgbs);
                }else {
                    redisService.setCacheObject("dgbs_block",dgbs);
                }

            }

        }else {
            System.out.println("未从节点获取到dgpMap最新数据 ："+DateUtils.dateTimeNow());
        }

    }


    @Scheduled(cron = "25 0,30 * * * ?")
    public void RXDBlockInfoToRedisAndDB(){
        if(!enable){
            return;
        }
        BlockInfo blockInfo = NodeRpc.getBlock("rxd");

        int  count = 1;
        while (StringUtils.isNull(blockInfo)){
            if(count >= 10){
                break;
            }
            blockInfo = NodeRpc.getBlock("rxd");
            count++;
        }

        if(StringUtils.isNotNull(blockInfo)){
            if(StringUtils.isNotNull(blockInfo.getPower())){
                redisService.setCacheObject("rxd_block",blockInfo);
                //存入数据库
                DateValueVo vo = new DateValueVo();
                Date now = new Date();
                int minute = (now.getMinutes() / 30) * 30;
                now.setMinutes( minute);
                now.setSeconds(0);
                vo.setDate(now);
                String date = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, now);
                System.out.println("rxd 全网算力数据入库时间:"+date);
                vo.setValue(blockInfo.getPower());
                poolMapper.insertNetPower("rxd_net_power",date,vo);
            }
        }
    }

    @Scheduled(cron = "8 0/1 * * * ?")
    public void RXDBlockInfoToRedis(){
        if(!enable){
            return;
        }
        BlockInfo blockInfo = NodeRpc.getBlock("rxd");

        if(StringUtils.isNotNull(blockInfo)){
            if(StringUtils.isNotNull(blockInfo.getPower())){

                if (redisService.hasKey("rxd_block")){
                    redisService.deleteObject("rxd_block");
                    redisService.setCacheObject("rxd_block",blockInfo);
                }else {
                    redisService.setCacheObject("rxd_block",blockInfo);
                }

            }
        }else {
            System.out.println("未从节点获取到最新数据 ："+DateUtils.dateTimeNow());
        }

    }

    @Scheduled(cron = "30 0,30 * * * ?")
    public void ALPHBlockInfoToRedisAndDB(){
        if(!enable){
            return;
        }
        BlockInfo blockInfo = NodeRpc.getBlock("alph");
        //改成根据swagger api调用 用与原有节点调用相比新的路径、新的api

        int  count = 1;
        while (StringUtils.isNull(blockInfo)){
            if(count >= 10){
                break;
            }
            blockInfo = NodeRpc.getBlock("alph");
            count++;
        }

        if(StringUtils.isNotNull(blockInfo)){
            if(StringUtils.isNotNull(blockInfo.getPower())){
                redisService.setCacheObject("alph_block",blockInfo);
                //存入数据库
                DateValueVo vo = new DateValueVo();
                Date now = new Date();
                int minute = (now.getMinutes() / 30) * 30;
                now.setMinutes( minute);
                now.setSeconds(0);
                vo.setDate(now);
                String date = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, now);
                System.out.println("alph 全网算力数据入库时间:"+date);
                vo.setValue(blockInfo.getPower());
                poolMapper.insertNetPower("alph_net_power",date,vo);
            }
        }
    }

    @Scheduled(cron = "14 0/1 * * * ?")
    public void ALPHBlockInfoToRedis(){
        if(!enable){
            return;
        }
        BlockInfo blockInfo = NodeRpc.getBlock("alph");

        if(StringUtils.isNotNull(blockInfo)){
            if(StringUtils.isNotNull(blockInfo.getPower())){

                if (redisService.hasKey("alph_block")){
                    redisService.deleteObject("alph_block");
                    redisService.setCacheObject("alph_block",blockInfo);
                }else {
                    redisService.setCacheObject("alph_block",blockInfo);
                }

            }
        }else {
            System.out.println("alph 未从节点获取到最新数据 ："+DateUtils.dateTimeNow());
        }

    }

    @Scheduled(cron = "35 0,30 * * * ?")
    public void ENXBlockInfoToRedisAndDB(){
        if(!enable){
            return;
        }
        BlockInfo blockInfo = NodeRpc.getBlock("enx");

        int  count = 1;
        while (StringUtils.isNull(blockInfo)){
            if(count >= 10){
                break;
            }
            blockInfo = NodeRpc.getBlock("enx");
            count++;
        }

        if(StringUtils.isNotNull(blockInfo)){
            if(StringUtils.isNotNull(blockInfo.getPower())){
                redisService.setCacheObject("enx_block",blockInfo);
                //存入数据库
                DateValueVo vo = new DateValueVo();
                Date now = new Date();
                int minute = (now.getMinutes() / 30) * 30;
                now.setMinutes( minute);
                now.setSeconds(0);
                vo.setDate(now);
                String date = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, now);
                System.out.println("enx 全网算力数据入库时间:"+date);
                vo.setValue(blockInfo.getPower());
                poolMapper.insertNetPower("enx_net_power",date,vo);
            }
        }
    }

    @Scheduled(cron = "12 0/1 * * * ?")
    public void ENXBlockInfoToRedis(){
        if(!enable){
            return;
        }
        BlockInfo blockInfo = NodeRpc.getBlock("enx");

        if(StringUtils.isNotNull(blockInfo)){
            if(StringUtils.isNotNull(blockInfo.getPower())){

                if (redisService.hasKey("enx_block")){
                    redisService.deleteObject("enx_block");
                    redisService.setCacheObject("enx_block",blockInfo);
                }else {
                    redisService.setCacheObject("enx_block",blockInfo);
                }

            }
        }else {
            System.out.println("未从节点获取到最新数据 ："+DateUtils.dateTimeNow());
        }

    }


    /**
     * 全网报块高度入库
     */
    @Scheduled(cron = "0 2 0/1 * * ?")
    //@Scheduled(cron = "0 0/2 * * * ?")
    public void insertNetBlock(){
        if(!enable){
            return;
        }
        insertBlockWithRetry("nexa", "nexa_net_block", 5);
        insertBlockWithRetry("mona", "mona_net_block", 5);
        insertBlockWithRetry("rxd", "rxd_net_block", 5);
        insertBlockWithRetry("grs", "grs_net_block", 5);
        insertBlockWithAlph(3);
        //下列三种币 高度是共用的
        insertBlockWithDgbSeries(5);
    }

    /**
     * alph 币种高度入库
     */
    private void insertBlockWithAlph(int maxRetries){
        int retryCount = 0;
        while (retryCount < maxRetries) {
            try {
                String url = "http://18.141.161.129:12973/blockflow/chain-info?fromGroup=0&toGroup=0";
                HttpHeaders headers = new HttpHeaders();
                headers.add("Content-Type","application/json");
                headers.add("User-Agent", "Mozilla/5.0");
                headers.add("apikey","0x09e220e226f2feb7a971a2b6f958e7d4b1c187c8");
                HttpEntity<String> httpEntity = new HttpEntity<String>(null, headers);
                String s = HttpUtil.get(url);
                JSONObject jsonObject = JSONObject.parseObject(s);
                int currentHeight = jsonObject.getIntValue("currentHeight");
                poolMapper.insertNetBlock("alph_net_block", (long) currentHeight);
                log.info("插入币种 alph,插入的最新高度为{}",currentHeight);
                return;
            } catch (Exception e) {
                retryCount++;
                if (retryCount >= maxRetries) {
                    log.error("插入区块 alph 数据失败，重试 {} 次后仍失败,失败原因{}", maxRetries, e.getMessage());
                    break;
                }
                try {
                    Thread.sleep(500);
                } catch (InterruptedException ie) {
                    Thread.currentThread().interrupt();
                    break;
                }
            }
        }

    }

    /**
     * nexa,mona,rxd,grs 币种高度入库
     * @param blockName
     * @param tableName
     * @param maxRetries
     */
    private void insertBlockWithRetry(String blockName, String tableName, int maxRetries) {
        int retryCount = 0;
        while (retryCount < maxRetries) {
            try {
                BlockInfo block = NodeRpc.getBlock(blockName);
                poolMapper.insertNetBlock(tableName, block.getHeight());
                log.info("插入币种 {} ,插入的最新高度为{}", blockName,block.getHeight());
                return;
            } catch (Exception e) {
                retryCount++;
                if (retryCount >= maxRetries) {
                    log.error("插入区块 {} 数据失败，重试 {} 次后仍失败", blockName, maxRetries, e);
                    break;
                }
                try {
                    Thread.sleep(500);
                } catch (InterruptedException ie) {
                    Thread.currentThread().interrupt();
                    break;
                }
            }
        }
    }

    /**
     * dgb 系列odo,qubit,skein,scrypt,sha256d币种高度入库
     * @param maxRetries
     */
    private void insertBlockWithDgbSeries(int maxRetries) {
        int retryCount = 0;
        System.out.println("dgb 总高度执行");
        while (retryCount < maxRetries) {
            try {
                //这里随便填一个dgb 的算法类型， dgb 共用一个高度
                String height = NodeRpc.getHeight("dgbq");
                System.out.println("dgb 总高度:"+height);
                //需要记录每次的dgb 总的高度，方便下次循环遍历
                Integer  lastHeight  = poolMapper.getDgbTotalHeight();
                System.out.println("dgb 总高度lastHeight:"+lastHeight);
                if (lastHeight != null){

                    // 从数据库获取dgb系列的币种的最新高度(因为这是新接口，是从0开始计算的高度)
                    BlockInfoDto dgbqHeight = poolMapper.getDgbSeriesBlock("dgbq_net_block");
                    BlockInfoDto dgbsHeight  = poolMapper.getDgbSeriesBlock("dgbs_net_block");
                    BlockInfoDto dgboHeight  = poolMapper.getDgbSeriesBlock("dgbo_net_block");
                    long dgbqIncrement = 0;
                    long dgbsIncrement = 0;
                    long dgboIncrement = 0;

                    //总高度 - 上次的总高度  然后循环遍历这些块高度，来判断该块的算法类型,
                    for (int i = Integer.parseInt(height) ; i > lastHeight ; i--){
                        String hashBlock = NodeRpc.getBlockInfoByHeight("dgbq", i);
                        String dgbType = NodeRpc.getBlockInfoByHash("dgbq", hashBlock);
                        if("qubit".equals(dgbType)){
                            dgbqIncrement++;
                        }else if ("odo".equals(dgbType)){
                            dgboIncrement++;
                        }else if("skein".equals(dgbType)){
                            dgbsIncrement++;
                        }
                    }

                    long dgbqincr = dgbqHeight == null ? 0 : dgbqHeight.getHeight() + dgbqIncrement;
                    long dgbsincr = dgbsHeight == null ? 0 :  dgbsHeight.getHeight() + dgbsIncrement;
                    long dgboincr = dgboHeight == null ? 0 :  dgboHeight.getHeight() + dgboIncrement;


                    transactionTemplate.execute(new TransactionCallbackWithoutResult() {
                        @Override
                        protected void doInTransactionWithoutResult(TransactionStatus status) {
                            try {
                                poolMapper.insertDgbTotalHeight(Long.valueOf(height));
                                poolMapper.insertNetBlock("dgbq_net_block", dgbqincr);
                                poolMapper.insertNetBlock("dgbs_net_block", dgbsincr);
                                poolMapper.insertNetBlock("dgbo_net_block", dgboincr);
                            } catch (Exception e) {
                                // 回滚事务
                                status.setRollbackOnly();
                                throw e;
                            }
                        }
                    });
                log.info("插入币种 dgbs最新高度:{} ,dgbq最新高度:{},dgbo最新高度:{}", dgbsHeight,dgbqHeight,dgboHeight);

                }else {
                    poolMapper.insertDgbTotalHeight(Long.valueOf(height));
                    log.info("首次插入dgb系列的最新高度值为---{}",height);
                }
                return;
            } catch (Exception e) {
                retryCount++;
                if (retryCount >= maxRetries) {
                    log.error("插入区块dgb数据失败，重试 {} 次后仍失败",maxRetries, e);
                    break;
                }
                try {
                    Thread.sleep(500);
                } catch (InterruptedException ie) {
                    Thread.currentThread().interrupt();
                    break;
                }
            }
        }

    }

    public  BlockInfo getMONEROBlock() {
        BlockInfo moneroBlockInfo = poolMapper.getMoneroBlockInfo();
        System.out.println("moneroBlockInfo"+moneroBlockInfo);
        if(StringUtils.isNull(moneroBlockInfo)){
            return null;
        }
        BigDecimal moreroBlocks = PoolCalParamConfig.getCoinCount("morero");
        BigDecimal profit = moneroBlockInfo.getReward();

        // 保留两位小数
        profit = profit.setScale(1, RoundingMode.HALF_UP);
        moneroBlockInfo.setProfit(profit);
        return moneroBlockInfo;
    }

    //TODO
    @Scheduled(cron = "40 0,30 * * * ?")
    public void MONEROBlockInfoToRedisAndDB(){
        System.out.println("执行全网算力入库monero");
        if(!enable){
            return;
        }
        BlockInfo blockInfo = getMONEROBlock();

        int  count = 1;
        while (StringUtils.isNull(blockInfo)){
            if(count >= 10){
                break;
            }
            blockInfo = getMONEROBlock();
            count++;
        }
        System.out.println("monero blockInfo 的信息"+blockInfo);
        if(StringUtils.isNotNull(blockInfo)){
            if(StringUtils.isNotNull(blockInfo.getPower())){
                redisService.setCacheObject("monero_block",blockInfo);
                //存入数据库
                DateValueVo vo = new DateValueVo();
                Date now = new Date();
                int minute = (now.getMinutes() / 30) * 30;
                now.setMinutes( minute);
                now.setSeconds(0);
                vo.setDate(now);
                String date = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, now);
                System.out.println("monero全网算力数据入库时间:"+date);
                vo.setValue(blockInfo.getPower());
                poolMapper.insertNetPower("monero_net_power",date,vo);
            }
        }
    }


    //TODO
    @Scheduled(cron = "0 0/1 * * * ?")
    public void MONEROBlockInfoToRedis(){
        if(!enable){
            return;
        }
        BlockInfo blockInfo = getMONEROBlock();
        int  count = 0;
        while (StringUtils.isNull(blockInfo)){
            if(count >= 3){
                break;
            }
            blockInfo = getMONEROBlock();
            count++;
        }

        if(StringUtils.isNotNull(blockInfo)){
            if(StringUtils.isNotNull(blockInfo.getPower())){

                if (redisService.hasKey("monero_block")){
                    redisService.deleteObject("monero_block");
                    redisService.setCacheObject("monero_block",blockInfo);
                }else {
                    redisService.setCacheObject("monero_block",blockInfo);
                }

            }
        }
    }


    public  BlockInfo getSha3xOBlock() {
        BlockInfo sha3xBlockInfo = poolMapper.getXtmBlockInfo();
        System.out.println("sha3xBlockInfo"+sha3xBlockInfo);
        if(StringUtils.isNull(sha3xBlockInfo)){
            return null;
        }
        BigDecimal moreroBlocks = PoolCalParamConfig.getCoinCount("sha3x");
        BigDecimal profit = sha3xBlockInfo.getReward();

        // 保留两位小数
        profit = profit.setScale(1, RoundingMode.HALF_UP);
        sha3xBlockInfo.setProfit(profit);

        return sha3xBlockInfo;
    }

    //TODO
    @Scheduled(cron = "50 0,30 * * * ?")
    public void Sha3xBlockInfoToRedisAndDB(){
        System.out.println("执行全网算力入库sha3x");
        if(!enable){
            return;
        }
        BlockInfo blockInfo = getSha3xOBlock();

        int  count = 1;
        while (StringUtils.isNull(blockInfo)){
            if(count >= 10){
                break;
            }
            blockInfo = getSha3xOBlock();
            count++;
        }
        System.out.println("sha3x blockInfo 的信息"+blockInfo);
        if(StringUtils.isNotNull(blockInfo)){
            if(StringUtils.isNotNull(blockInfo.getPower())){
                redisService.setCacheObject("sha3x_block",blockInfo);
                //存入数据库
                DateValueVo vo = new DateValueVo();
                Date now = new Date();
                int minute = (now.getMinutes() / 30) * 30;
                now.setMinutes( minute);
                now.setSeconds(0);
                vo.setDate(now);
                String date = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, now);
                System.out.println("sha3x全网算力数据入库时间:"+date);
                vo.setValue(blockInfo.getPower());
                poolMapper.insertNetPower("sha3x_net_power",date,vo);
            }
        }
    }


    //TODO
    @Scheduled(cron = "0 0/1 * * * ?")
    public void Sha3xBlockInfoToRedis(){
        if(!enable){
            return;
        }
        BlockInfo blockInfo = getSha3xOBlock();
        int  count = 0;
        while (StringUtils.isNull(blockInfo)){
            if(count >= 3){
                break;
            }
            blockInfo = getSha3xOBlock();
            count++;
        }

        if(StringUtils.isNotNull(blockInfo)){
            if(StringUtils.isNotNull(blockInfo.getPower())){

                if (redisService.hasKey("sha3x_block")){
                    redisService.deleteObject("sha3x_block");
                    redisService.setCacheObject("sha3x_block",blockInfo);
                }else {
                    redisService.setCacheObject("sha3x_block",blockInfo);
                }

            }
        }
    }

}
