package com.ruoyi.common.utils;

import com.ruoyi.common.core.domain.ValidateResult;
import com.ruoyi.common.enums.ServiceCode;
import com.ruoyi.common.exception.ServiceException;
import org.springframework.stereotype.Component;

/**
 * @author 辉
 * 2024/3/4
 */
public class ServiceUtil {

    /**
     * 判断是否满足条件，满足则抛出异常
     * @param flag 条件
     * @param msg 异常说明
     */
    public static void assertion(boolean flag, String msg) {
        assertion(flag, msg, 500);
    }

    /**
     * 判断是否满足条件，满足则抛出异常
     * @param flag 条件
     * @param msg 异常说明
     * @param code 业务代码
     */
    public static void assertion(boolean flag, String msg, int code) {
        if (flag) {
            throw new ServiceException(msg, code);
        }
    }

    public static void assertion(ValidateResult result) {
        if (result.isError()) {
            throw new ServiceException(result.getMsg(), result.getCode());
        }
    }
    public static void assertion(boolean flag, String format, Object ...args) {
        if (flag) {
            throw new ServiceException(String.format(format, args), 500);
        }
    }
}
