package com.jxy.marketall.task;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jxy.common.core.utils.StringUtils;
import com.jxy.common.redis.service.RedisService;
import com.jxy.marketall.dto.CoinPriceListDto;
import com.jxy.marketall.entity.CoinMarketData;
import com.jxy.marketall.mapper.BiAnMapper2;
import com.jxy.marketall.service.NodeJsMarketFeign;
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.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @Description 币价任务
 * @Date 2022/7/7 18:03
 * @Author 杜懿
 */
@Configuration
@EnableScheduling
public class CoinPriceTask {

    @Autowired
    private NodeJsMarketFeign nodeJsMarketFeign;

    @Autowired
    private BiAnMapper2 biAnMapper2;

    @Autowired
    private RedisService redisService;

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

        List<CoinPriceListDto> list = biAnMapper2.getCoinRankList();

        while (list.size() < 100){
            list = biAnMapper2.getCoinRankList();
        }

        for(int i = 99; i >= 0; i--){
            list.get(i).setList(DayPrice(list.get(i).getParam()));
        }

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

    public List<CoinMarketData> DayPrice(String coin){
        if(StringUtils.isEmpty(coin)){
            return null;
        }

        List<CoinMarketData> list = biAnMapper2.getCoinDailyList(coin);
        return list;
    }

}
