package com.purui.prims.headless.server.utils;

import com.purui.prims.common.util.DatePeriodEnum;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Cron表达式生成器
 *
 * @author charlie
 * @date 2024/11/27
 */
public class CronUtils {

    public static String buildCron(DatePeriodEnum period, String startTime) {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        String defaultCron = "0 0 2 * * ?";
        Date time = null;
        try {
            time = sdf.parse(startTime);
        } catch (ParseException e) {
            return defaultCron;
        }
        int hour = time.getHours();
        int minute = time.getMinutes();
        int second = time.getSeconds();

        switch (period) {
            case HOUR:
                // 每小时的第minute分钟执行
                return String.format("%d %d * * * ?", second, minute);
            case DAY:
                // 每天的hour:minute:second执行
                return String.format("%d %d %d * * ?", second, minute, hour);
            case WEEK:
                // 每周的第一天（周一）的hour:minute:second执行
                return String.format("%d %d %d ? * MON", second, minute, hour);
            case MONTH:
                // 每月的1号的hour:minute:second执行
                return String.format("%d %d %d 1 * ?", second, minute, hour);
            case QUARTER:
                // 每季度的第一个月的1号的hour:minute:second执行
                return String.format("%d %d %d 1 1,4,7,10 ?", second, minute, hour);
            case YEAR:
                // 每年的1月1日的hour:minute:second执行
                return String.format("%d %d %d 1 1 ?", second, minute, hour);
            default:
                return defaultCron;
        }
    }

    public static String[] calculateDateRange(DatePeriodEnum period, int unit) {
        StringBuilder startDate = new StringBuilder();
        StringBuilder endDate = new StringBuilder();
        String[] dateRange = new String[2];
        String currentDate = "NOW()";
        switch (period) {
            case HOUR:
                // 前1个小时
                endDate.append("DATE_SUB(").append(currentDate).append(", INTERVAL ").append(0).append(" HOUR)");
                startDate.append("DATE_SUB(").append(currentDate).append(", INTERVAL ").append(unit).append(" HOUR)");
                break;
            case DAY:
                // 前1天
                endDate.append("DATE_SUB(").append(currentDate).append(", INTERVAL ").append(0).append(" DAY)");
                startDate.append("DATE_SUB(").append(currentDate).append(", INTERVAL ").append(unit).append(" DAY)");
                break;
            case WEEK:
                // 前1周
                endDate.append("DATE_SUB(").append(currentDate).append(", INTERVAL ").append(0).append(" WEEK)");
                startDate.append("DATE_SUB(").append(currentDate).append(", INTERVAL ").append(unit).append(" WEEK)");
                break;
            case MONTH:
                // 前1个月
                endDate.append("DATE_SUB(").append(currentDate).append(", INTERVAL ").append(0).append(" MONTH)");
                startDate.append("DATE_SUB(").append(currentDate).append(", INTERVAL ").append(unit).append(" MONTH)");
                break;
            case YEAR:
                // 前1年
                endDate.append("DATE_SUB(").append(currentDate).append(", INTERVAL ").append(0).append(" YEAR)");
                startDate.append("DATE_SUB(").append(currentDate).append(", INTERVAL ").append(unit).append(" YEAR)");
                break;
            case QUARTER:
                // 前1个季度
                endDate.append("DATE_SUB(").append(currentDate).append(", INTERVAL ").append(unit * 3).append(" MONTH)");
                startDate.append("DATE_SUB(").append(currentDate).append(", INTERVAL ").append(unit * 6).append(" MONTH)");
                break;
            default:
                break;
        }
        dateRange[0] = startDate.toString();
        dateRange[1] = endDate.toString();
        return dateRange;
    }
}
