package com.ema.admin.handler;


import cn.dev33.satoken.exception.*;
import com.baomidou.lock.exception.LockFailureException;
import com.ema.common.domain.vo.ResultVo;
import com.ema.common.exceptions.MyException;
import com.ema.common.exceptions.ResultEnum;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.NestedServletException;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.util.stream.Collectors;

/**
 * 异常处理
 */
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {
    /**
     * 处理业务异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(value = MyException.class)
    @ResponseBody
    public ResultVo<String> handleBaseException(MyException e) {
        log.error(">>>>>>   业务异常 : {}", e.getMessage());
        return ResultVo.failure(e.getCode(), e.getMessage());
    }

    /**
     * 处理系统异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public ResultVo<String> handleException(Exception e) {
        log.error(">>>>>>   系统异常!", e);
        return ResultVo.failure(ResultEnum.SYSTEM_ERROR);
    }

    /**
     * 处理方法参数无效异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    @ResponseBody
    public ResultVo<String> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
        return ResultVo.failure(ResultEnum.PARAMETER_ERROR, e.getBindingResult().getFieldErrors().stream().map(FieldError::getDefaultMessage).collect(Collectors.joining("；")));
    }

    /**
     * 处理验证约束异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(value = ConstraintViolationException.class)
    @ResponseBody
    public ResultVo<String> handleConstraintViolationException(ConstraintViolationException e) {
        return ResultVo.failure(ResultEnum.PARAMETER_ERROR, e.getConstraintViolations().stream().map(ConstraintViolation::getMessage).collect(Collectors.joining("；")));
    }

    /**
     * 处理数据绑定异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(value = BindException.class)
    @ResponseBody
    public ResultVo<String> handleBindException(BindException e) {
        return ResultVo.failure(ResultEnum.PARAMETER_ERROR, e.getBindingResult().getFieldErrors().stream().map(FieldError::getDefaultMessage).collect(Collectors.joining("；")));
    }

    // 拦截：未登录异常
    @ExceptionHandler(NotLoginException.class)
    @ResponseBody
    @SneakyThrows
    public ResultVo<String> handlerException(NotLoginException e) {
        return ResultVo.failure(ResultEnum.NOT_LOGIN);
    }

    // 拦截：缺少权限异常
    @ExceptionHandler(NotPermissionException.class)
    @ResponseBody
    public ResultVo<String> handlerException(NotPermissionException e) {
        return ResultVo.failure(ResultEnum.NOT_POWER);
    }

    // 拦截：缺少角色异常
    @ExceptionHandler(NotRoleException.class)
    @ResponseBody
    public ResultVo<String> handlerException(NotRoleException e) {
        return ResultVo.failure(ResultEnum.NOT_POWER);
    }

    // 拦截：二级认证校验失败异常
    @ExceptionHandler(NotSafeException.class)
    @ResponseBody
    public ResultVo<String> handlerException(NotSafeException e) {
        return ResultVo.failure(ResultEnum.NOT_POWER,"二次认证校验失败！");
    }

    // 拦截：服务封禁异常
    @ExceptionHandler(DisableServiceException.class)
    @ResponseBody
    public ResultVo<String> handlerException(DisableServiceException e) {
        return ResultVo.failure(ResultEnum.NOT_POWER,"服务封禁异常 ！");
    }

    // 拦截：Http Basic 校验失败异常
    @ExceptionHandler(LockFailureException.class)
    @ResponseBody
    public ResultVo<String> handlerException(LockFailureException e) {
        return ResultVo.failure(ResultEnum.FAILURE,"获取分布式锁失败 ！");
    }

    @ExceptionHandler(SaTokenContextException.class)
    @ResponseBody
    public ResultVo<String> handlerException(SaTokenContextException e) {
        return ResultVo.failure(ResultEnum.NOT_LOGIN);
    }

    @ExceptionHandler(NestedServletException.class)
    @ResponseBody
    public ResultVo<String> handlerException(NestedServletException e) {
        return ResultVo.failure(ResultEnum.FAILURE,"访问接口地址错误 ！");
    }


}
