package com.tuoheng.common.core.utils;

import cn.hutool.core.util.ObjectUtil;
import com.tuoheng.common.core.exception.ExceptionInterface;
import com.tuoheng.common.core.exception.ServiceException;

import java.util.Optional;

/**
 * 异常工具
 * @author chenyukun
 */
public class ExceptionUtil {

    private ExceptionUtil(){}

    /**
     * 抛服务异常
     *
     * @param code 异常编码
     * @param exceptionMsg 异常信息
     */
    public static void throwServiceExcetion(Integer code, String exceptionMsg){
        throw new ServiceException(code, exceptionMsg);
    }

    /**
     * 抛服务异常
     *
     * @param code 异常编码
     * @param exceptionMsg 异常信息
     * @param logMsg 日志消息
     * @param logObj 日志消息占位对象
     */
    public static void throwServiceExcetion(Integer code, String exceptionMsg, String logMsg, String ...logObj){
        if(StringUtils.isNotEmpty(logObj)){
            Optional.ofNullable(logMsg).ifPresent(x -> LogUtil.LOGGER.error(x, logObj));
        }
        if(StringUtils.isEmpty(logObj)){
            Optional.ofNullable(logMsg).ifPresent(x -> LogUtil.LOGGER.error(x));
        }
        throw new ServiceException(code, exceptionMsg);
    }

    /**
     * 抛服务异常
     *
     * @param code 异常编码
     * @param exceptionMsg 异常信息
     * @param exception 异常
     * @param logMsg 日志消息
     * @param logObj 日志消息占位对象
     */
    public static void throwServiceExcetion(Integer code, String exceptionMsg, Exception exception, String logMsg,
        String ...logObj){
        if(StringUtils.isNotEmpty(logObj) && exception != null){
            Optional.ofNullable(logMsg).ifPresent(x -> LogUtil.LOGGER.error(x, logObj, exception));
        }
        if(StringUtils.isNotEmpty(logObj) && exception == null){
            Optional.ofNullable(logMsg).ifPresent(x -> LogUtil.LOGGER.error(x, logObj));
        }
        if(StringUtils.isEmpty(logObj) && exception != null){
            Optional.ofNullable(logMsg).ifPresent(x -> LogUtil.LOGGER.error(x, exception));
        }
        if(StringUtils.isEmpty(logObj) && exception == null){
            Optional.ofNullable(logMsg).ifPresent(x -> LogUtil.LOGGER.error(x));
        }
        throw new ServiceException(code, exceptionMsg);
    }

    /**
     * 抛服务异常
     *
     * @param exceptionInterface 异常接口
     */
    public static void throwServiceExcetion(ExceptionInterface exceptionInterface){
        throw new ServiceException(exceptionInterface);
    }

    /**
     * 抛服务异常
     *
     * @param exceptionInterface 异常接口
     * @param logMsg 日志消息
     * @param logObj 日志对象
     */
    public static void throwServiceExcetion(ExceptionInterface exceptionInterface, String logMsg, String ...logObj){
        if(StringUtils.isNotEmpty(logObj)){
            Optional.ofNullable(logMsg).ifPresent(x -> LogUtil.LOGGER.error(x, logObj));
        }
        if(StringUtils.isEmpty(logObj)){
            Optional.ofNullable(logMsg).ifPresent(x -> LogUtil.LOGGER.error(x));
        }
        throw new ServiceException(exceptionInterface);
    }

    /**
     * 抛服务异常
     *
     * @param exceptionInterface 异常接口
     * @param exception 异常
     * @param logMsg 日志消息
     * @param logObj 日志对象
     */
    public static void throwServiceExcetion(ExceptionInterface exceptionInterface, Exception exception, String logMsg,
        String ...logObj){
        if(StringUtils.isNotEmpty(logObj) && exception != null){
            Optional.ofNullable(logMsg).ifPresent(x -> LogUtil.LOGGER.error(x, logObj, exception));
        }
        if(StringUtils.isNotEmpty(logObj) && exception == null){
            Optional.ofNullable(logMsg).ifPresent(x -> LogUtil.LOGGER.error(x, logObj));
        }
        if(StringUtils.isEmpty(logObj) && exception != null){
            Optional.ofNullable(logMsg).ifPresent(x -> LogUtil.LOGGER.error(x, exception));
        }
        if(StringUtils.isEmpty(logObj) && exception == null){
            Optional.ofNullable(logMsg).ifPresent(x -> LogUtil.LOGGER.error(x));
        }
        throw new ServiceException(exceptionInterface);
    }

    /**
     * 校验参数是否为空，为空抛异常
     *
     * @param obj 参数对象
     * @param exceptionInterface 异常接口
     */
    public static void paramIsNull(Object obj, ExceptionInterface exceptionInterface){
        if(ObjectUtil.isEmpty(obj)){
            throwServiceExcetion(exceptionInterface);
        }
    }

    /**
     * 校验参数是否为空，为空抛异常
     *
     * @param obj 参数对象
     * @param exceptionInterface 异常接口
     * @param logMsg 日志消息
     * @param logObj 日志对象
     */
    public static void paramIsNull(Object obj, ExceptionInterface exceptionInterface, String logMsg, String ...logObj){
        if(ObjectUtil.isEmpty(obj)){
            throwServiceExcetion(exceptionInterface, logMsg, logObj);
        }
    }

    /**
     * 校验参数是否为空，为空抛异常
     *
     * @param obj 参数对象
     * @param exceptionInterface 异常接口
     * @param exception 异常
     * @param logMsg 日志消息
     * @param logObj 日志对象
     */
    public static void paramIsNull(Object obj, ExceptionInterface exceptionInterface, Exception exception,
                                   String logMsg, String ...logObj){
        if(ObjectUtil.isEmpty(obj)){
            throwServiceExcetion(exceptionInterface, exception, logMsg, logObj);
        }
    }
}
