package com.skr.mdm.handle;

import com.alibaba.excel.exception.ExcelAnalysisException;
import com.skr.mdm.enums.ResultCodeEnum;
import com.skr.mdm.exception.ServiceException;
import com.skr.mdm.result.ResponseResult;
import de.schlichtherle.license.LicenseContentException;
import de.schlichtherle.license.NoLicenseInstalledException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.AccessDeniedException;
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 javax.servlet.http.HttpServletRequest;
import javax.validation.ConstraintViolationException;

/**
 * @author nfq
 * @date 2020/8/14 17:03
 * 统一异常处理
 */
@ControllerAdvice
@ResponseBody
@Slf4j
public class ExceptionControllerHandler {

    @ExceptionHandler(ServiceException.class)
    public ResponseResult businessExceptionHandle(HttpServletRequest request, ServiceException e) {
        log.error("ServiceException URI：" + request.getRequestURI(), e);
        if (StringUtils.isNotBlank(e.getCode())) {
            return ResponseResult.failure(e.getCode(), e.getMessage());
        }
        return ResponseResult.failure(e.getMessage());
    }

    @ExceptionHandler(ExcelAnalysisException.class)
    public ResponseResult excelAnalysisExceptionHandle(HttpServletRequest request, ExcelAnalysisException e) {
        log.error("excel解析异常URI：" + request.getRequestURI(), e);
        return ResponseResult.failure(e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public ResponseResult exceptionHandle(HttpServletRequest request, Exception e) {
        log.error("全局异常捕获处理" + request.getRequestURI(), e);
        if (e instanceof LicenseContentException) {
            return ResponseResult.failure("证书内容检验失败");
        }
        if (e instanceof NoLicenseInstalledException) {
            return ResponseResult.failure("证书安装失败");
        }
        return ResponseResult.failure();
    }

    @ExceptionHandler(AccessDeniedException.class)
    public ResponseResult accessDeniedExceptionHandle(HttpServletRequest request, Exception e) {
        log.error("访问未授权异常URI：" + request.getRequestURI(), e);
        return ResponseResult.failure(ResultCodeEnum.ACCESS_DENIED);
    }

    @ExceptionHandler(LicenseContentException.class)
    public ResponseResult licenseContentExceptionHandle(HttpServletRequest request, Exception e) {
        log.error("请求异常URI：" + request.getRequestURI(), e);
        return ResponseResult.failure("证书内容检验失败：{}", e.getMessage());
    }

    @ExceptionHandler({MethodArgumentNotValidException.class, ConstraintViolationException.class})
    public ResponseResult handleException(Exception e){
        if(e instanceof MethodArgumentNotValidException){
            MethodArgumentNotValidException methodException = (MethodArgumentNotValidException) e;
            String defaultMessage = methodException.getBindingResult().getFieldErrors().get(0).getDefaultMessage();
            return ResponseResult.failure(defaultMessage);
        } else if (e instanceof ConstraintViolationException){
            ConstraintViolationException constraintException = (ConstraintViolationException) e;
            String message = constraintException.getConstraintViolations().iterator().next().getMessage();
            return ResponseResult.failure(message);
        }
        return ResponseResult.failure();
    }
}
