package com.ruoyi.system.util;


import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.LinkedHashSet;
import java.util.Set;

/**
 * 获取表明util
 */
public class TableRouterUtil {

    // 表名前缀
    private static final String TABLE_PREFIX = "eps_traffic_details";

    private static final String TABLE_PREFIX_INITIAL = "initial_bandwidth_traffic";

    // 日期格式
    private static final DateTimeFormatter YEAR_MONTH_FORMAT =
            DateTimeFormatter.ofPattern("yyyy_MM");
    private static final DateTimeFormatter DATE_TIME_FORMATTER =
            DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    /**
     * 根据创建时间获取表名
     * @param createTime 记录创建时间
     * @return 对应的分表名称
     * @throws IllegalArgumentException 如果createTime为null
     *
     * 示例：
     *   2023-08-05 14:30:00 → eps_initial_traffic_2023_08_1_10
     *   2023-08-15 09:15:00 → eps_initial_traffic_2023_08_11_20
     *   2023-08-25 18:45:00 → eps_initial_traffic_2023_08_21_31
     */
    public static String getTableName(LocalDateTime createTime) {
        if (createTime == null) {
            throw new IllegalArgumentException("创建时间不能为null");
        }

        String yearMonth = createTime.format(YEAR_MONTH_FORMAT);
        int day = createTime.getDayOfMonth();

        return String.format("%s_%s_%s",
                TABLE_PREFIX,
                yearMonth,
                getDayRange(day));
    }
    public static String getInitialTableName(LocalDateTime createTime) {
        if (createTime == null) {
            throw new IllegalArgumentException("创建时间不能为null");
        }

        String yearMonth = createTime.format(YEAR_MONTH_FORMAT);
        int day = createTime.getDayOfMonth();

        return String.format("%s_%s_%s",
                TABLE_PREFIX_INITIAL,
                yearMonth,
                getDayRange(day));
    }

    /**
     * 获取时间范围内涉及的所有表名
     * @param startTime 开始时间 (格式: "yyyy-MM-dd HH:mm:ss")
     * @param endTime 结束时间 (格式: "yyyy-MM-dd HH:mm:ss")
     * @return 按时间顺序排列的表名集合
     */
    public static Set<String> getTableNamesBetween(String startTime, String endTime) {
        LocalDateTime start = parseDateTime(startTime);
        LocalDateTime end = parseDateTime(endTime);
        validateTimeRange(start, end);

        Set<String> tableNames = new LinkedHashSet<>();
        LocalDateTime current = start.withHour(0).withMinute(0).withSecond(0);

        while (!current.isAfter(end)) {
            tableNames.add(getTableName(current));
            current = current.plusDays(1);
        }

        return tableNames;
    }
    /**
     * 获取时间范围内涉及的所有初始表名
     * @param startTime 开始时间 (格式: "yyyy-MM-dd HH:mm:ss")
     * @param endTime 结束时间 (格式: "yyyy-MM-dd HH:mm:ss")
     * @return 按时间顺序排列的表名集合
     */
    public static Set<String> getTableNamesBetweenInitial(String startTime, String endTime) {
        LocalDateTime start = parseDateTime(startTime);
        LocalDateTime end = parseDateTime(endTime);
        validateTimeRange(start, end);

        Set<String> tableNames = new LinkedHashSet<>();
        LocalDateTime current = start.withHour(0).withMinute(0).withSecond(0);

        while (!current.isAfter(end)) {
            tableNames.add(getInitialTableName(current));
            current = current.plusDays(1);
        }

        return tableNames;
    }
    // 解析字符串为LocalDateTime
    public static LocalDateTime parseDateTime(String dateTimeStr) {
        if (dateTimeStr == null || dateTimeStr.trim().isEmpty()) {
            throw new IllegalArgumentException("时间字符串不能为空");
        }
        try {
            return LocalDateTime.parse(dateTimeStr, DATE_TIME_FORMATTER);
        } catch (Exception e) {
            throw new IllegalArgumentException("时间格式必须为: yyyy-MM-dd HH:mm:ss", e);
        }
    }

    // 获取日期区间
    private static String getDayRange(int day) {
        if (day < 1 || day > 31) {
            throw new IllegalArgumentException("日期必须在1-31之间");
        }

        if (day <= 10) return "1_10";
        if (day <= 20) return "11_20";
        return "21_31";
    }

    // 验证时间范围
    private static void validateTimeRange(LocalDateTime start, LocalDateTime end) {
        if (start == null || end == null) {
            throw new IllegalArgumentException("时间范围参数不能为null");
        }
        if (start.isAfter(end)) {
            throw new IllegalArgumentException("开始时间不能晚于结束时间");
        }
    }
}
