package com.erp.finance.exception;

import com.erp.finance.dto.FinanceApiResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import com.erp.common.core.model.ApiResponse;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(RuntimeException.class)
    public FinanceApiResponse<?> handleRuntimeException(RuntimeException e) {
        log.error("[财务服务] 运行时异常: {}", e.getMessage(), e);
        return FinanceApiResponse.error(500, e.getMessage());
    }

    @ExceptionHandler(IllegalArgumentException.class)
    public FinanceApiResponse<?> handleIllegalArgumentException(IllegalArgumentException e) {
        log.warn("[财务服务] 参数异常: {}", e.getMessage());
        return FinanceApiResponse.badRequest(e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public FinanceApiResponse<?> handleException(Exception e) {
        log.error("[财务服务] 未知异常: {}", e.getMessage(), e);
        return FinanceApiResponse.error(500, "系统异常: " + e.getMessage());
    }
}
