package com.global;

import org.hibernate.service.spi.ServiceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import javax.validation.ValidationException;
import java.sql.SQLException;
import java.util.Set;

@RestControllerAdvice
public class GlobalHandler {
    private static final Logger LOGGER = LoggerFactory.getLogger(GlobalHandler.class);

    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    @ExceptionHandler(AuthenticationException.class)
    public ResponseEntity<String> handleAuthenticationException(AuthenticationException e) {
        LOGGER.error("AuthenticationException:{}", e.getMessage(), e);
        return ResponseEntity.badRequest().body(e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public ResponseEntity<?> exceptionHandler(Throwable e) {
        LOGGER.error(e.getMessage(), e);
        if (e instanceof SQLException | e instanceof DataAccessException) {
            return ResponseEntity.badRequest().body("The server is off.Please contact the developer");
        }
        return ResponseEntity.badRequest().body(e.getMessage());
    }

    @ExceptionHandler(BindException.class)
    public ResponseEntity handleBindException(HttpServletRequest request, BindException e) {
        StringBuilder result = new StringBuilder();
        for (FieldError fieldError : e.getFieldErrors()) {
            result.append(fieldError.getField()).append(":").
                    append(fieldError.getDefaultMessage()).
                    append(System.lineSeparator());
        }
        LOGGER.error("{}?{},{}", request.getRequestURI(), request.getQueryString(), result, e.getMessage());
        return ResponseEntity.badRequest().body(result.toString());
    }

    /**
     * 400 - Bad Request
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(MissingServletRequestParameterException.class)
    public ResponseEntity handleMissingServletRequestParameterException(MissingServletRequestParameterException e) {
        LOGGER.error("缺少请求参数", e);
        return ResponseEntity.badRequest().body("required_parameter_is_not_present");
    }

    /**
     * 400 - Bad Request
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(HttpMessageNotReadableException.class)
    public ResponseEntity handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {
        LOGGER.error("参数解析失败", e);
        return ResponseEntity.badRequest().body("could_not_read_json");
    }

    /**
     * 400 - Bad Request
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResponseEntity handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
        LOGGER.error("参数验证失败", e);
        BindingResult result = e.getBindingResult();
        FieldError error = result.getFieldError();
        String field = error.getField();
        String code = error.getDefaultMessage();
        String message = String.format("%s:%s", field, code);
        return ResponseEntity.badRequest().body(message);
    }

    /**
     * 400 - Bad Request
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(BindException.class)
    public ResponseEntity handleBindException(BindException e) {
        LOGGER.error("参数绑定失败", e);
        BindingResult result = e.getBindingResult();
        FieldError error = result.getFieldError();
        String field = error.getField();
        String code = error.getDefaultMessage();
        String message = String.format("%s:%s", field, code);
        return ResponseEntity.badRequest().body(message);
    }

    /**
     * 400 - Bad Request
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(ConstraintViolationException.class)
    public ResponseEntity handleServiceException(ConstraintViolationException e) {
        LOGGER.error("参数验证失败", e);
        Set<ConstraintViolation<?>> violations = e.getConstraintViolations();
        ConstraintViolation<?> violation = violations.iterator().next();
        String message = violation.getMessage();
        return ResponseEntity.badRequest().body("parameter:" + message);
    }

    /**
     * 400 - Bad Request
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(ValidationException.class)
    public ResponseEntity handleValidationException(ValidationException e) {
        LOGGER.error("参数验证失败", e);
        return ResponseEntity.badRequest().body("validation_exception");
    }

    /**
     * 405 - Method Not Allowed
     */
    @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public ResponseEntity handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
        LOGGER.error("不支持当前请求方法", e);
        return ResponseEntity.badRequest().body("request_method_not_supported");
    }

    /**
     * 415 - Unsupported Media Type
     */
    @ResponseStatus(HttpStatus.UNSUPPORTED_MEDIA_TYPE)
    @ExceptionHandler(HttpMediaTypeNotSupportedException.class)
    public ResponseEntity handleHttpMediaTypeNotSupportedException(Exception e) {
        LOGGER.error("不支持当前媒体类型", e);
        return ResponseEntity.badRequest().body("content_type_not_supported");
    }

    /**
     * 500 - Internal Server Error
     */
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(ServiceException.class)
    public ResponseEntity handleServiceException(ServiceException e) {
        LOGGER.error("业务逻辑异常", e);
        return ResponseEntity.badRequest().body("业务逻辑异常：" + e.getMessage());
    }

    /**
     * 500 - Internal Server Error
     */
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(Exception.class)
    public ResponseEntity handleException(Exception e) {
        LOGGER.error("通用异常", e);
        return ResponseEntity.badRequest().body("通用异常：" + e.getMessage());
    }

    /**
     * 操作数据库出现异常:名称重复，外键关联
     */
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(DataIntegrityViolationException.class)
    public ResponseEntity handleException(DataIntegrityViolationException e) {
        LOGGER.error("操作数据库出现异常:", e);
        return ResponseEntity.badRequest().body("操作数据库出现异常：字段重复、有外键关联等");
    }


}
@@ -1491,16 +1491,333 @@
 lass);%0A%0A
+%0A    /**%0A     * 500 - Internal Server Error%0A     */%0A    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)%0A    @ExceptionHandler(Exception.class)%0A    public ResponseEntity handleException(Exception e) %7B%0A        LOGGER.error(%22%E9%80%9A%E7%94%A8%E5%BC%82%E5%B8%B8%22, e);%0A        return ResponseEntity.badRequest().body(%22%E9%80%9A%E7%94%A8%E5%BC%82%E5%B8%B8%EF%BC%9A%22 + e.getMessage());%0A    %7D%0A%0A
     @Res
@@ -6576,32 +6576,57 @@
 portedException(
+HttpMediaTypeNotSupported
 Exception e) %7B%0A 
@@ -7093,323 +7093,8 @@
  %7D%0A%0A
-    /**%0A     * 500 - Internal Server Error%0A     */%0A    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)%0A    @ExceptionHandler(Exception.class)%0A    public ResponseEntity handleException(Exception e) %7B%0A        LOGGER.error(%22%E9%80%9A%E7%94%A8%E5%BC%82%E5%B8%B8%22, e);%0A        return ResponseEntity.badRequest().body(%22%E9%80%9A%E7%94%A8%E5%BC%82%E5%B8%B8%EF%BC%9A%22 + e.getMessage());%0A    %7D%0A
 %0A   

fix bug