import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class ValidTableNamesTest {

    public static void main(String[] age) {

        long day = 7-1;

        // 2. 确定日期范围：近365天（包含当天）
        LocalDate endDate = LocalDate.now().minusDays(1);
        LocalDate startDate = endDate.minusDays(day);

        List<String> table = getValidTableNames(startDate, endDate);
        System.out.println(table);

        List<LocalDate> list =  generateContinuousDates(startDate,endDate);
        System.out.println(list);

    }


    private static List<String> getValidTableNames(LocalDate start, LocalDate end) {
        Set<String> tableNames = new HashSet<>();
        String tablePrefix = "healthscores_";

        // 遍历日期范围的所有年份
        int startYear = start.getYear();
        int endYear = end.getYear();
        for (int year = startYear; year <= endYear; year++) {
            tableNames.add(tablePrefix + year);
        }

        // 严格校验表名格式（防SQL注入）
        tableNames.forEach(name -> {
            if (!name.matches("^healthscores_\\d{4}$")) {
                throw new IllegalArgumentException("非法表名：" + name + "（仅允许healthscores_四位数字格式）");
            }
        });

        return new ArrayList<>(tableNames);
    }

    private static List<LocalDate> generateContinuousDates(LocalDate start, LocalDate end) {
        long days = java.time.temporal.ChronoUnit.DAYS.between(start, end);
        return Stream.iterate(start, date -> date.plusDays(1))
                .limit(days + 1) // +1 包含结束日期
                .collect(Collectors.toList());
    }
}
