package com.erp.task.exception;

import lombok.Getter;

/**
 * 任务异常
 */
@Getter
public class TaskException extends RuntimeException {

    private final String code;

    public TaskException(String code, String message) {
        super(message);
        this.code = code;
    }

    public TaskException(String code, String message, Throwable cause) {
        super(message, cause);
        this.code = code;
    }

    public static TaskException notFound(Long taskId) {
        return new TaskException("TASK_NOT_FOUND", "任务不存在: " + taskId);
    }

    public static TaskException alreadyExists(String taskName, String taskGroup) {
        return new TaskException("TASK_EXISTS", "任务已存在: " + taskName + "@" + taskGroup);
    }

    public static TaskException invalidCron(String cronExpression) {
        return new TaskException("INVALID_CRON", "无效的Cron表达式: " + cronExpression);
    }

    public static TaskException executionFailed(Long taskId, String reason) {
        return new TaskException("EXECUTION_FAILED", "任务执行失败: " + taskId + ", 原因: " + reason);
    }

    public static TaskException cannotStart(Long taskId) {
        return new TaskException("CANNOT_START", "无法启动任务: " + taskId);
    }

    public static TaskException cannotStop(Long taskId) {
        return new TaskException("CANNOT_STOP", "无法停止任务: " + taskId);
    }

    public static TaskException timeout(Long taskId, Integer timeout) {
        return new TaskException("TASK_TIMEOUT", "任务执行超时: " + taskId + ", 超时时间: " + timeout + "秒");
    }
}
