package cn.axzo.msg.center.notices.client.handler;

import cn.axzo.basics.common.exception.ServiceException;
import cn.axzo.msg.center.notices.common.enums.ReturnCodeEnum;
import cn.axzo.msg.center.notices.common.exception.BizException;
import cn.axzo.msg.center.notices.integration.client.DingDingClient;
import cn.azxo.framework.common.model.CommonResponse;
import cn.azxo.framework.common.utils.LogUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.annotation.Resource;

/**
 * Spring MVC 统一异常处理
 *
 * @author zhaoyong_sh
 * @see ControllerExceptionHandler
 * @since 2021-05-17 15:44
 */
@Slf4j
@RestControllerAdvice
public class ControllerExceptionHandler {

    @Resource(name = "dingDingClient")
    private DingDingClient dingDingClient;

    @ExceptionHandler(value = BizException.class)
    public CommonResponse bizException(BizException e){
        log.warn("[mns] ControllerExceptionHandler.bizException Exception", e);
        return CommonResponse.error(e.getBizCode(), e.getMessage());
    }

    @ExceptionHandler(value = ServiceException.class)
    public CommonResponse bizException(ServiceException e){
        log.warn("[mns] ControllerExceptionHandler.bizException Exception", e);
        return CommonResponse.error(e.getErrorCode(), e.getMessage());
    }

    @ExceptionHandler(value = IllegalArgumentException.class)
    public CommonResponse handleIllegalArgumentException(IllegalArgumentException e) {
        LogUtil.error("[mns] ControllerExceptionHandler.handleIllegalArgumentException Exception message ", e);
        return CommonResponse.error(ReturnCodeEnum.INVALID_PARAMETER.getCode(), e.getMessage());
    }

    @ExceptionHandler(value = DuplicateKeyException.class)
    public CommonResponse handleDuplicateKeyException(DuplicateKeyException e) {
        LogUtil.error("[mns] ControllerExceptionHandler.handleDuplicateKeyException Exception message ", e);
        return CommonResponse.error(ReturnCodeEnum.REPEATED_REQUEST.getCode(), e.getCause().getMessage());
    }

    @ExceptionHandler(value = Exception.class)
    public CommonResponse handleOtherException(Exception e) {
        log.warn("[mns] ControllerExceptionHandler.handleOtherException Exception", e);
        LogUtil.error(LogUtil.ErrorType.ERROR_SYSTEM, "[mns] ControllerExceptionHandler.handleOtherException Exception", e);
        return CommonResponse.error(ReturnCodeEnum.SYSTEM_ERROR.getCode(), ReturnCodeEnum.SYSTEM_ERROR.getMessage());
    }

    @ExceptionHandler(value = Throwable.class)
    public CommonResponse handleThrowableException(Throwable e) {
        log.warn("[mns] ControllerExceptionHandler.handleThrowableException Exception", e);
        LogUtil.error(LogUtil.ErrorType.ERROR_SYSTEM, "[mns] ControllerExceptionHandler.handleThrowableException Exception", e);
        return CommonResponse.error(ReturnCodeEnum.SYSTEM_ERROR.getCode(), ReturnCodeEnum.SYSTEM_ERROR.getMessage());
    }

    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public CommonResponse handleArgumentValidException(MethodArgumentNotValidException e){
        log.warn("[mns] ControllerExceptionHandler.handleArgumentValidException Exception", e.getMessage());
        BindingResult bindingResult = e.getBindingResult();
        StringBuilder errorMsg = new StringBuilder();
        for (FieldError fieldError : bindingResult.getFieldErrors()) {
            if (-1 == errorMsg.indexOf(fieldError.getDefaultMessage())) {
                errorMsg.append(fieldError.getDefaultMessage()).append(",");
            }
        }
        errorMsg.deleteCharAt(errorMsg.length() - 1);
        return CommonResponse.error(ReturnCodeEnum.INVALID_PARAMETER.getCode(), errorMsg.toString());
    }

}
