package com.example.zxweb.utils;

import com.example.zxweb.exception.BusinessException;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
/**
 * @Description 变量校验工具
 * @Author ZhouWenTao
 * @Date 2023/6/26 19:14
 */
public abstract class AssertUtils {
    /**
     * 判断真假值
     * @param flag
     * @param message
     */
    public static void isTrue(boolean flag, String message) {
        if (!flag) {
            throw new BusinessException(message);
        }
    }

    /**
     * true时拦截
     * @param flag
     * @param message
     */
    public static void notTrue(boolean flag, String message) {
        if (flag) {
            throw new BusinessException(message);
        }
    }

    public static void notNull(Object object, String message) {
        if (object == null) {
            throw new BusinessException(message);
        }
    }

    public static void isEmpty(String str, String message) {
        if (StringUtils.isNotBlank(str)) {
            throw new BusinessException(message);
        }
    }

    public static void notEmpty(String str, String message) {
        if (StringUtils.isBlank(str)) {
            throw new BusinessException(message);
        }
    }

    /**
     * 校验Array 不可为空/不可空数据
     * @param list
     * @param message
     */
    public static void hasSize(List list,String message){
        if (list==null || list.size()==0) {
            throw new BusinessException(message);
        }
    }

    /**
     * 校验Array 不可为空/不可空数据
     * @param list
     * @param message
     */
    public static void hasSize(Object[] list,String message){
        if (list==null || list.length==0) {
            throw new BusinessException(message);
        }
    }

    /**
     * 判断字符串长度
     * @param o 判读对象
     * @param len 字符串长度，不可超过
     * @param message 异常提示
     */
    public static void checkLength(Object o,Integer len,String message){
        notNull(o,"<"+message+">判断之前参数不可空!");
        if (o.toString().length()>len) {
            throw new BusinessException(message);
        }
    }
}
