package com.ruoyi.system.util;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.system.domain.EpsNodeBandwidth;

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

public class DateUtil {
    private static final DateTimeFormatter FORMATTER =
            DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    private static final ZoneId ZONE = ZoneId.of("Asia/Shanghai");

    public static class TimeRange {
        private String start;
        private String end;

        // 构造器
        public TimeRange(String start, String end) {
            this.start = start;
            this.end = end;
        }

        // Getter
        public String getStart() { return start; }
        public String getEnd() { return end; }
    }

    /**
     * 获取当天/当月日期
     * @param createTime
     * @param type
     * @return
     */
    public static TimeRange getTimeRange(Date createTime, String type) {
        LocalDateTime baseTime = createTime.toInstant()
                .atZone(ZoneId.systemDefault())
                .toLocalDateTime();

        if ("1".equals(type)) {
            return new TimeRange(
                    baseTime.with(LocalTime.MIN).format(FORMATTER),
                    baseTime.with(LocalTime.MAX).withNano(0).format(FORMATTER)
            );
        } else {
            YearMonth month = YearMonth.from(baseTime);
            return new TimeRange(
                    month.atDay(1).atStartOfDay().format(FORMATTER),
                    month.atEndOfMonth().atTime(23, 59, 59).format(FORMATTER)
            );
        }
    }
    /**
     * Date 类型 转为 LocalDateTime 类型
     * @param date
     * @return
     */
    public static LocalDateTime dateToLocalDateTime(Date date) {
        return date.toInstant()
                .atZone(ZoneId.systemDefault())
                .toLocalDateTime();
    }
    public static void setTimeDay(EpsNodeBandwidth epsNodeBandwidth){
        if(epsNodeBandwidth.getStartTime() == null || epsNodeBandwidth.getEndTime() == null) {
            // 获取这个月第一天
            LocalDate lastMonth = LocalDate.now(ZoneId.of("Asia/Shanghai")).minusMonths(0);
            LocalDate firstDayOfMonth = lastMonth.withDayOfMonth(1);
            LocalDate lastDayOfMonth = lastMonth.withDayOfMonth(lastMonth.lengthOfMonth());
            String monthlyStartTime = firstDayOfMonth.atStartOfDay().format(FORMATTER);
            String monthlyEndTime = lastDayOfMonth.atTime(23, 59, 59).format(FORMATTER);
            epsNodeBandwidth.setStartTime(monthlyStartTime);
            epsNodeBandwidth.setEndTime(monthlyEndTime);
        }
        // 获取时间类型 1--日， 2--月
        String type = "1";
        processTime(epsNodeBandwidth, type);
    }
    public static void setTimeMonth(EpsNodeBandwidth epsNodeBandwidth){
        if(epsNodeBandwidth.getStartTime() == null || epsNodeBandwidth.getEndTime() == null) {
            // 获取今年第一个月第一天
            // 当年1月1日 00:00:00
            LocalDateTime startOfYear = LocalDate.now(ZONE)
                    .withMonth(1)
                    .withDayOfMonth(1)
                    .atStartOfDay();

            // 上个月最后一天 23:59:59
            LocalDateTime endOfLastMonth = LocalDate.now(ZONE)
                    .minusMonths(1)
                    .with(TemporalAdjusters.lastDayOfMonth())
                    .atTime(23, 59, 59);
            epsNodeBandwidth.setStartTime(startOfYear.format(FORMATTER));
            epsNodeBandwidth.setEndTime(endOfLastMonth.format(FORMATTER));
        }
        // 获取时间类型 1--日， 2--月
        String type = "2";
        processTime(epsNodeBandwidth, type);
    }

    public static void processTime(EpsNodeBandwidth epsNodeBandwidth, String type){
        // 获取开始时间 结束时间
        DateUtil.TimeRange startTimeRange = DateUtil.getTimeRange(DateUtils.parseDate(epsNodeBandwidth.getStartTime()),type);
        DateUtil.TimeRange endTimeRange = DateUtil.getTimeRange(DateUtils.parseDate(epsNodeBandwidth.getEndTime()),type);
        String startTime = startTimeRange.getStart();
        String endTime = endTimeRange.getEnd();
        epsNodeBandwidth.setStartTime(startTime);
        epsNodeBandwidth.setEndTime(endTime);
    }
}
