package com.ruoyi.common.core.utils;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;

/**
 * @author YaphetS
 * @date 2023-12-14
 */
public class TimeRangeUtils {

    /** 时间类型 日 */
    public static final int DAY = 1;

    /** 时间类型 周 */
    public static final int WEEK = 2;

    /** 时间类型 月 */
    public static final int MONTH = 3;

    /** 时间类型 季度 */
    public static final int QUARTER = 4;

    /** 时间类型 年 */
    public static final int YEAR = 5;


    public static void main(String[] args) {
        LocalDateTime[] nowTimeRange = getNowTimeRange(6);
        System.out.println(nowTimeRange[0].format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        System.out.println(nowTimeRange[1].format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
    }
    public static LocalDateTime[] getNowTimeRange(Integer type){
        LocalDate localDate=LocalDate.now();
        return getTimeRange(localDate,type);
    }
    public static LocalDateTime[] getTimeRange(LocalDate localDate,Integer type){
        LocalDateTime[] localDateTime=new LocalDateTime[2];
        switch (type){
            case DAY:
                localDateTime[0]=getStartOrEndDayOfDay(localDate,true);
                localDateTime[1]=getStartOrEndDayOfDay(localDate,false);
                break;
            case WEEK:
                localDateTime[0]=getStartOrEndDayOfWeek(localDate,true);
                localDateTime[1]=getStartOrEndDayOfWeek(localDate,false);
                break;
            case MONTH:
                localDateTime[0]=getStartOrEndDayOfMonth(localDate,true);
                localDateTime[1]=getStartOrEndDayOfMonth(localDate,false);
                break;
            case QUARTER:
                localDateTime[0]=getStartOrEndDayOfQuarter(localDate,true);
                localDateTime[1]=getStartOrEndDayOfQuarter(localDate,false);
                break;
            case YEAR:
                localDateTime[0]=getStartOrEndDayOfYear(localDate,true);
                localDateTime[1]=getStartOrEndDayOfYear(localDate,false);
                break;
            default:
                throw new RuntimeException("时间类型错误！");
        }
        return localDateTime;
    }
    /**
     * @Description:当天的开始时间*
     *
     * @Param: [localDate, isFirst: true 表示开始时间，false表示结束时间]
     *
     */
    public static LocalDateTime getStartOrEndDayOfDay(LocalDate localDate, Boolean isFirst){
        LocalDate resDate = LocalDate.now();
        if (localDate == null) {
            localDate = resDate;
        }
        if(isFirst){
            return localDate.atStartOfDay();
        }else{
            return LocalDateTime.of(localDate, LocalTime.MAX);
        }
    }
    /**
     * @Description:本周的开始时间
     * @Param: [localDate, isFirst: true 表示开始时间，false表示结束时间]
     */
    public static LocalDateTime getStartOrEndDayOfWeek(LocalDate localDate, Boolean isFirst){
        LocalDateTime localDateTime;
        if (localDate == null) {
            localDate = LocalDate.now();
        }
        if (isFirst) {
            localDateTime = localDate.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)).atStartOfDay();;
        }else {
            localDateTime = LocalDateTime.of(localDate.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY)), LocalTime.MAX);
        }
        return localDateTime;
    }
    /***
     * @Description:本月的开始时间
     * @Param: [localDate, isFirst: true 表示开始时间，false表示结束时间]
     */
    public static LocalDateTime getStartOrEndDayOfMonth(LocalDate localDate, Boolean isFirst){
        if (localDate == null) {
            localDate =LocalDate.now();
        }
        LocalDateTime localDateTime;
        if (isFirst) {
            localDateTime = localDate.with(TemporalAdjusters.firstDayOfMonth()).atStartOfDay();
        }else {
            localDateTime = LocalDateTime.of(localDate.with(TemporalAdjusters.lastDayOfMonth()), LocalTime.MAX);
        }
        return localDateTime;
    }
    /***
     * @Description:本季度的开始时间
     * @Param: [localDate, isFirst: true 表示开始时间，false表示结束时间]
     */
    public static LocalDateTime getStartOrEndDayOfQuarter(LocalDate localDate, Boolean isFirst){
        if (localDate == null) {
            localDate = LocalDate.now();
        }
        LocalDateTime localDateTime;
        Month minMonth = localDate.getMonth().firstMonthOfQuarter();
        //获取当前季度结束月
        Month maxMonth = minMonth.plus(2);
        if (isFirst) {
            localDateTime = LocalDateTime.of(localDate.getYear(),minMonth,1,0,0);
        } else {
            LocalDate lastDate = LocalDate.of(localDate.getYear(),maxMonth,maxMonth.length(localDate.isLeapYear()));
            localDateTime = LocalDateTime.of(lastDate,LocalTime.MAX);
        }
        return localDateTime;
    }
    /*** @Description:本年度的开始时间* @Param: [localDate, isFirst: true 表示开始时间，false表示结束时间]*/
    public static LocalDateTime getStartOrEndDayOfYear(LocalDate localDate, Boolean isFirst){
        LocalDateTime localDateTime;
        if (localDate == null) {
            localDate =  LocalDate.now();
        }
        if (isFirst) {
            localDateTime = localDate.with(TemporalAdjusters.firstDayOfYear()).atStartOfDay();
        } else {
            localDateTime = LocalDateTime.of(localDate.with(TemporalAdjusters.lastDayOfYear()), LocalTime.MAX);
        }
        return localDateTime;
    }
}

