
package com.jeelowcode.framework.utils.global.exeption;


import com.jeelowcode.framework.code.JeeLowCodeErrorCode;
import com.jeelowcode.framework.constants.FrameErrorCodeConstants;
import com.jeelowcode.framework.exception.JeeLowCodeException;
import com.jeelowcode.framework.exception.JeeLowCodeMoreException;
import com.jeelowcode.framework.global.JeeLowCodeBaseConstant;
import com.jeelowcode.framework.utils.model.global.BaseWebResult;
import com.jeelowcode.framework.utils.utils.FuncBase;
import com.jeelowcode.tool.framework.common.exception.ServiceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;

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.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * 全局异常处理器
 */
@Order(Ordered.HIGHEST_PRECEDENCE)
@RestControllerAdvice(basePackages = JeeLowCodeBaseConstant.BASE_PACKAGES)//扫码我们的包
public class JeeLowCodeGlobalExceptionHandler {
    private static final Logger log = LoggerFactory.getLogger(JeeLowCodeGlobalExceptionHandler.class);


    /**
     * 拦截业务的运行时异常
     */
    @ExceptionHandler(ServiceException.class)
    public BaseWebResult handleServiceException(ServiceException e) {
        Integer code = e.getCode();
        String message = e.getMessage();
        e.printStackTrace();
        return BaseWebResult.error(code, message);
    }

    /**
     * 拦截校验异常
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public BaseWebResult handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
        String message = e.getMessage();
        e.printStackTrace();
        try{
            message =message.split("arguments \\[\\]; default message \\[")[1];

            Pattern pattern = Pattern.compile("default message \\[(.*?)\\]");
            Matcher matcher = pattern.matcher(message);

            if (matcher.find()) {
                String defaultMessage = matcher.group(1);
                return BaseWebResult.error(FrameErrorCodeConstants.FRAM_SELF_ERROR.getCode(), defaultMessage);
            }
        }catch (Exception e2){

        }
        return BaseWebResult.error(FrameErrorCodeConstants.FRAME_PARAM_ERROR);

    }


    /**
     * 拦截未知的运行时异常
     */
    @ExceptionHandler(RuntimeException.class)
    public BaseWebResult handleRuntimeException(RuntimeException e, HttpServletRequest request) {
        e.printStackTrace();
        return BaseWebResult.error(FrameErrorCodeConstants.FRAME_PARAM_ERROR);
    }

    /**
     * 系统异常
     */
    @ExceptionHandler(Exception.class)
    public BaseWebResult handleException(Exception e) {
        e.printStackTrace();
        String message = e.getMessage();
        return BaseWebResult.error(FrameErrorCodeConstants.FRAME_PARAM_ERROR);
    }


    /**
     * 自定义异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(JeeLowCodeException.class)
    public BaseWebResult handleJeeLowCodeExceptionException(RuntimeException e) {
        String message = e.getMessage();
        if (FuncBase.isNotEmpty(message) && FuncBase.jsonIsJson(message)) {
            //判断是否是 BaseWebResult，如果是BaseWebResult 则直接弹出
            if (message.contains("code") && message.contains("msg")) {
                try {
                    BaseWebResult baseWebResult = FuncBase.json2Bean(message, BaseWebResult.class);
                    if (FuncBase.isNotEmpty(baseWebResult)) {
                        return baseWebResult;
                    }
                } catch (Exception e1) {

                }
            }
        }

        return BaseWebResult.error(FrameErrorCodeConstants.FRAM_SELF_ERROR.getCode(), e.getMessage());
    }


    /**
     * 自定义异常-更多详情
     *
     * @param e
     * @return
     */
    @ExceptionHandler(JeeLowCodeMoreException.class)
    public BaseWebResult handleJeeLowCodeMoreExceptionException(RuntimeException e) {

        String messageJsonStr = e.getMessage();
        Map<String, String> map = FuncBase.json2Bean(messageJsonStr, Map.class);
        String title = map.get("title");
        String e1 = map.get("e");

        //封装map
        JeeLowCodeErrorCode jeeLowCodeErrorCode = new JeeLowCodeErrorCode(FrameErrorCodeConstants.FRAME_OP_ERROR.getCode(), title);

        return BaseWebResult.error(jeeLowCodeErrorCode, e1);

    }


}