package com.jxy.marketall.task;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.BTCBigAmountVoutDto;
import com.jxy.marketall.dto.CountDto;
import com.jxy.marketall.entity.BTCBigAmountVout;
import com.jxy.marketall.mapper.BTCBigAmountVoutMapper;
import com.jxy.marketall.service.BTCBigAmountVoutService;
import com.jxy.marketall.vo.BTCVo;
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.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

/**
 * @Description BTC大额交易定时任务
 * @Date 2022/7/7 18:03
 * @Author 杜懿
 */
@Configuration
@EnableScheduling
public class  BigAmountVoutTask {

    @Autowired
    private BTCBigAmountVoutService btcBigAmountVoutService;

    @Autowired
    private BTCBigAmountVoutMapper btcBigAmountVoutMapper;

    @Autowired
    private RedisService redisService;

    /** 最近两天的数据 */
    @Scheduled(cron = "0 0/5 * * * ?")
    public void twoDayDataToRedis(){
        Date now = new Date();//todo 改为最后一条数据的时间
        String end = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,now);

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

        BTCVo btcVo = new BTCVo();
        btcVo.setEnd(end);
        btcVo.setStart(start);
        //List<BTCBigAmountVoutDto> list = btcBigAmountVoutMapper.getBTCBigAmountVoutDtoList(btcVo);
        //if(list.size()>0) {
        //    //存入redis 先删除原有数据再重新插入数据
        //    redisService.deleteObject("btcBigAmountVout2Day");
        //    redisService.setCacheList("btcBigAmountVout2Day", list);
        //}
    }

    /** 最新的一条数据 */
    @Scheduled(cron = "0 0/1 * * * ?")
    public void BTCBigAmountVOutNewDataToRedis(){

        BTCBigAmountVout data = btcBigAmountVoutMapper.getLastBigAmountVout();

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

    }

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

       List<CountDto> list = btcBigAmountVoutMapper.selectDayCountList();

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

    }

}
