package org.springblade.modules.api.utils;

import org.springblade.modules.api.request.TimeRangeReq;

import java.util.Calendar;
import java.util.Objects;

public class TimeRangeReqUtils {

    private TimeRangeReqUtils() {

    }

    public static TimeRangeReq setTimeRangeReq(TimeRangeReq timeRangeReq) {

        if (timeRangeReq.getDateType() == 6
                && (Objects.isNull(timeRangeReq.getStartTime()) || Objects.isNull(timeRangeReq.getEndTime()))) {
            throw new IllegalArgumentException("请选择自定义时间范围");
        }

        switch (timeRangeReq.getDateType()) {
            case 1:
                //Calendar获取今天的开始时间和结束时间
                Calendar todayStart = Calendar.getInstance();
                todayStart.set(Calendar.HOUR, 0);        //小时
                todayStart.set(Calendar.MINUTE, 0);        //分钟
                todayStart.set(Calendar.SECOND, 0);        //秒
                todayStart.set(Calendar.MILLISECOND, 0);   //毫秒
                timeRangeReq.setStartTime(todayStart.getTime());

                Calendar todayEnd = Calendar.getInstance();
                todayEnd.set(Calendar.HOUR, 23);
                todayEnd.set(Calendar.MINUTE, 59);
                todayEnd.set(Calendar.SECOND, 59);
                todayEnd.set(Calendar.MILLISECOND, 999);
                timeRangeReq.setEndTime(todayEnd.getTime());

                return timeRangeReq;
            case 2:
                //Calendar获取昨天的开始时间和结束时间
                Calendar yesterdayStart = Calendar.getInstance();
                yesterdayStart.add(Calendar.DAY_OF_MONTH, -1);
                yesterdayStart.set(Calendar.HOUR, 0);        //小时
                yesterdayStart.set(Calendar.MINUTE, 0);        //分钟
                yesterdayStart.set(Calendar.SECOND, 0);        //秒
                yesterdayStart.set(Calendar.MILLISECOND, 0);   //毫秒
                timeRangeReq.setStartTime(yesterdayStart.getTime());

                Calendar yesterdayEnd = Calendar.getInstance();
                yesterdayEnd.add(Calendar.DAY_OF_MONTH, -1);
                yesterdayEnd.set(Calendar.HOUR, 23);
                yesterdayEnd.set(Calendar.MINUTE, 59);
                yesterdayEnd.set(Calendar.SECOND, 59);
                yesterdayEnd.set(Calendar.MILLISECOND, 999);
                timeRangeReq.setEndTime(yesterdayEnd.getTime());

                return timeRangeReq;
            case 3:
                //Calendar获取本月的开始时间和结束时间
                Calendar currentMonthStart = Calendar.getInstance();
                currentMonthStart.set(Calendar.DAY_OF_MONTH, 1);
                currentMonthStart.set(Calendar.HOUR, 0);        //小时
                currentMonthStart.set(Calendar.MINUTE, 0);        //分钟
                currentMonthStart.set(Calendar.SECOND, 0);        //秒
                currentMonthStart.set(Calendar.MILLISECOND, 0);   //毫秒
                timeRangeReq.setStartTime(currentMonthStart.getTime());

                Calendar currentMonthEnd = Calendar.getInstance();
                currentMonthEnd.add(Calendar.MONTH, 1);
                currentMonthEnd.add(Calendar.DAY_OF_MONTH, -1);
                currentMonthEnd.set(Calendar.HOUR, 23);
                currentMonthEnd.set(Calendar.MINUTE, 59);
                currentMonthEnd.set(Calendar.SECOND, 59);
                currentMonthEnd.set(Calendar.MILLISECOND, 999);
                timeRangeReq.setEndTime(currentMonthEnd.getTime());
                return timeRangeReq;

            case 4:
                //Calendar获取本月的开始时间和结束时间
                Calendar lastMonthStart = Calendar.getInstance();
                lastMonthStart.add(Calendar.MONTH, -1);
                lastMonthStart.set(Calendar.DAY_OF_MONTH, 1);
                lastMonthStart.set(Calendar.HOUR, 0);        //小时
                lastMonthStart.set(Calendar.MINUTE, 0);        //分钟
                lastMonthStart.set(Calendar.SECOND, 0);        //秒
                lastMonthStart.set(Calendar.MILLISECOND, 0);   //毫秒
                timeRangeReq.setStartTime(lastMonthStart.getTime());

                Calendar lastMonthEnd = Calendar.getInstance();
                lastMonthEnd.add(Calendar.MONTH, 1);
                lastMonthEnd.add(Calendar.DAY_OF_MONTH, -1);
                lastMonthEnd.set(Calendar.HOUR, 23);
                lastMonthEnd.set(Calendar.MINUTE, 59);
                lastMonthEnd.set(Calendar.SECOND, 59);
                lastMonthEnd.set(Calendar.MILLISECOND, 999);
                timeRangeReq.setEndTime(lastMonthEnd.getTime());

                return timeRangeReq;
            case 5:
                Calendar yearStart = Calendar.getInstance();
                yearStart.set(Calendar.MONTH, 1);
                yearStart.set(Calendar.DAY_OF_MONTH, 1);
                yearStart.set(Calendar.HOUR, 0);        //小时
                yearStart.set(Calendar.MINUTE, 0);        //分钟
                yearStart.set(Calendar.SECOND, 0);        //秒
                yearStart.set(Calendar.MILLISECOND, 0);   //毫秒
                timeRangeReq.setStartTime(yearStart.getTime());

                Calendar yearEnd = Calendar.getInstance();
                yearEnd.set(Calendar.MONTH, 12);
                yearEnd.set(Calendar.DAY_OF_MONTH, 31);
                yearEnd.set(Calendar.HOUR, 23);
                yearEnd.set(Calendar.MINUTE, 59);
                yearEnd.set(Calendar.SECOND, 59);
                yearEnd.set(Calendar.MILLISECOND, 999);
                timeRangeReq.setEndTime(yearEnd.getTime());
                return timeRangeReq;
            case 6:
                return timeRangeReq;
            default:
                return timeRangeReq;
        }

    }

}
