package com.ho.business.config;

import cn.hutool.core.date.DateUtil;
import com.ho.common.tools.constant.RedisKeyConstant;
import com.ho.common.tools.util.RedisCommon;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author fancl
 * @desc: 时间处理类, 将时间以等值间隔字符串形势存储在redis中
 * @date 2022/9/15
 */
//@Component
public class TimeIntervalPostConstructor {

    @Autowired
    RedisCommon redisUtil;

    @PostConstruct
    public void buildTimeMinutes() {
        Date date = new Date();
        //建立5分钟间隔数组
        List<String> fiveMinutesList = new ArrayList<>(300);
        List<String> OneMinutesList = new ArrayList<>(1440);
        List<String> sixtyMinutesList = new ArrayList<>(24);
        List<String> thirtyMinutesList = new ArrayList<>(48);
        fiveMinutesList = minute(date, 5);
        OneMinutesList = minute(date, 1);
        thirtyMinutesList = minute(date,30);
        sixtyMinutesList = minute2(date, 60);
        redisUtil.set(RedisKeyConstant.FIVE_MINUTE_INTERVAL, fiveMinutesList);
        redisUtil.set(RedisKeyConstant.ONE_MINUTE_INTERVAL, OneMinutesList);
        redisUtil.set(RedisKeyConstant.SIXTY_MINUTE_INTERVAL,sixtyMinutesList);
        redisUtil.set(RedisKeyConstant.THIRTY_MINUTE_INTERVAL,thirtyMinutesList);
    }

    //增加每月每天日期 和 增加每年的12个月
    @PostConstruct
    public void buildTimeDaysAndMonth() {
        //每月每天
        Date date = new Date();
        List<String> currentMonthList = new ArrayList<>(getCurrentMonthLastDay());
            currentMonthList = day(date, 1);
        redisUtil.set(RedisKeyConstant.ONE_DAY_INTERVAL,currentMonthList);
        //每年每月
        Date dateMonth = new Date();
        List<String> currentYearMonthList = new ArrayList<>(20);
        currentYearMonthList = month(date, 1);
        redisUtil.set(RedisKeyConstant.MONTH_INTERVAL,currentYearMonthList);



    }

    //时间字符串格式(以分钟为单位)
    static List<String> minute(Date date, int offset) {
        Date start = dayStartDate(date);//转换为天的起始date
        Date nextDayDate = nextDay(start);//下一天的date
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        List<String> result = new ArrayList<>(720);
        while (start.compareTo(nextDayDate) < 0) {
            result.add(sdf.format(start));
            //日期加5分钟
            start = addMinutes(start, offset);
        }

        return result;
    }

    //年月日时分秒
    static List<String> minute2(Date date, int offset) {
        Date start = dayStartDate(date);//转换为天的起始date
        Date nextDayDate = nextDay(start);//下一天的date
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<String> result = new ArrayList<>(20);
        while (start.compareTo(nextDayDate) < 0) {
            result.add(sdf.format(start));
            //日期加5分钟
            start = addMinutes(start, offset);
        }
        return result;
    }

    //以天为单位
    static List<String> day(Date date, int offset) {
        Date start = DateUtil.beginOfMonth(date);//转换为天的起始date
        Date nextDayDate = DateUtil.endOfMonth(date);//下一天的date
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        List<String> result = new ArrayList<>(getCurrentMonthLastDay());
        while (start.compareTo(nextDayDate) < 0) {
            result.add(sdf.format(start));
            //日期加一天
            start = addDays(start, offset);
        }
        return result;
    }

    //以月为单位
    static List<String> month(Date date, int offset) {
        Date start = DateUtil.beginOfYear(date);//转换为天的起始date
        Date nextDayDate = DateUtil.endOfYear(date);//下一天的date
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        List<String> result = new ArrayList<>(20);
        while (start.compareTo(nextDayDate) < 0) {
            result.add(sdf.format(start));
            //加一个月
            start = addMonth(start, offset);
        }
        return result;
    }

    //增加时间间隔
    private static Date addMinutes(Date start, int offset) {
        Calendar c = Calendar.getInstance();
        c.setTime(start);
        c.add(Calendar.MINUTE, offset);
        return c.getTime();
    }
    private static Date addDays(Date start, int offset) {
        Calendar c = Calendar.getInstance();
        c.setTime(start);
        c.add(Calendar.DAY_OF_MONTH, offset);
        return c.getTime();
    }

    private static Date addMonth(Date start, int offset) {
        Calendar c = Calendar.getInstance();
        c.setTime(start);
        c.add(Calendar.MONTH, offset);
        return c.getTime();
    }

    private static Date nextDay(Date start) {
        Calendar c = Calendar.getInstance();
        c.setTime(start);
        c.add(Calendar.DATE, 1);
        return c.getTime();
    }

    private static Date dayStartDate(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        return c.getTime();
    }

    public static int getCurrentMonthLastDay()
    {
        Calendar a = Calendar.getInstance();
        a.set(Calendar.DATE, 1);//把日期设置为当月第一天
        a.roll(Calendar.DATE, -1);//日期回滚一天，也就是最后一天
        int maxDate = a.get(Calendar.DATE);
        return maxDate;
    }

    public static void main(String[] args) {
        Date dateMonth = new Date();
        List<String> currentYearMonthList = new ArrayList<>(20);
        currentYearMonthList = month(dateMonth, 1);
        System.out.println();
    }


}
