package com.tzld.piaoquan.api.handle;

import com.tzld.piaoquan.api.common.enums.ExceptionEnum;
import com.tzld.piaoquan.api.common.exception.CommonException;
import com.tzld.piaoquan.growth.common.common.base.CommonResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.BindException;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 全局异常处理器
 */

@RestControllerAdvice
public class GlobalExceptionHandle {

    private static Logger LOGGER = LoggerFactory.getLogger(GlobalExceptionHandle.class);

    @ExceptionHandler
    public Object handleException(HttpServletRequest req, Exception exception) throws Exception {
        String uri = req.getRequestURI();
        CommonResponse<Object> response = new CommonResponse<Object>();
        // 业务异常
        if (exception instanceof CommonException) {
            CommonException e = (CommonException) exception;
            response.setCode(e.getCode());
            response.setMsg(e.getMsg());
            LOGGER.warn("uri:" + uri + "\n" + "CustomException log.", exception);
        } else if (exception instanceof MethodArgumentNotValidException) {
            // 参数校验异常
            MethodArgumentNotValidException e = (MethodArgumentNotValidException) exception;
            List<ObjectError> errorList = e.getBindingResult().getAllErrors();
            StringBuilder errorMsg = new StringBuilder();
            errorMsg.append("|");
            if (!CollectionUtils.isEmpty(errorList)) {
                for (ObjectError objectError : errorList) {
                    errorMsg.append(objectError.getDefaultMessage()).append("|");
                }
            }
            response.setCode(ExceptionEnum.PARAM_ERROR.getCode());
            response.setMsg(errorMsg.toString());
            LOGGER.warn("uri:" + uri + "\n" + "MethodArgumentNotValidException log.", exception);
        } else if (exception instanceof BindException) {
            // 参数绑定异常
            BindException e = (BindException) exception;
            List<ObjectError> errorList = e.getBindingResult().getAllErrors();
            StringBuilder errorMsg = new StringBuilder();
            errorMsg.append("|");
            if (!CollectionUtils.isEmpty(errorList)) {
                for (ObjectError objectError : errorList) {
                    errorMsg.append(objectError.getDefaultMessage()).append("|");
                }
            }
            response.setCode(ExceptionEnum.PARAM_ERROR.getCode());
            response.setMsg(errorMsg.toString());
            LOGGER.warn("uri:" + uri + "\n" + "BindException log.", exception);
        } else {
            response.setCode(ExceptionEnum.SYSTEM_ERROR.getCode());
            response.setMsg(ExceptionEnum.SYSTEM_ERROR.getMsg());
            LOGGER.warn("uri:" + uri + "\n" + "unknownException log.", exception);
        }
        return response;
    }

}
