package cc.mrbird.febs.common.utils;
|
|
import cc.mrbird.febs.common.exception.FebsException;
|
import cn.hutool.core.util.StrUtil;
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
|
import java.util.Objects;
|
|
/**
|
* 实体验证工具类
|
*/
|
public class ValidateEntityUtils {
|
|
/**
|
* 确保指定列的值在数据库中是存在的,
|
* 该方法通过查询数据库来验证给定的列值是否存在如果不存在,则抛出异常
|
*
|
* @param valueToCheck 需要验证的列值
|
* @param columnExtractor 用于提取实体类中列值的函数式接口
|
* @param queryWrapperExtractor 用于构建查询条件并返回实体类的函数式接口
|
* @param errMsg 当列值无效时抛出的异常消息格式字符串
|
* @throws IllegalArgumentException 如果需要验证的值为null
|
* @throws FebsException 如果在数据库中找不到指定的列值或列值为null,或者在查询过程中发生异常
|
*/
|
public static <T, R, V> void ensureColumnValid(
|
V valueToCheck,
|
SFunction<T, R> columnExtractor,
|
SFunction<LambdaQueryWrapper<T>, T> queryWrapperExtractor,
|
String errMsg) {
|
// 检查需要验证的值是否为null
|
if (valueToCheck == null) {
|
throw new IllegalArgumentException("The value to check cannot be null while ensureColumnValid column");
|
}
|
|
try {
|
// 创建LambdaQueryWrapper并配置查询条件
|
LambdaQueryWrapper<T> wrapper = new LambdaQueryWrapper<>();
|
wrapper.select(columnExtractor)
|
.eq(columnExtractor, valueToCheck)
|
.last("limit 1");
|
|
// 执行查询并获取结果实体
|
T entity = queryWrapperExtractor.apply(wrapper);
|
// 如果查询结果为空,则抛出异常
|
if (entity == null) {
|
throw new FebsException(StrUtil.format(errMsg, valueToCheck));
|
}
|
|
// 提取查询结果中的列值
|
R columnValue = columnExtractor.apply(entity);
|
// 如果列值为null,则抛出异常
|
if (columnValue == null) {
|
throw new FebsException(StrUtil.format(errMsg, valueToCheck));
|
}
|
} catch (Exception e) {
|
// 记录异常日志
|
throw new FebsException(e.getMessage());
|
}
|
}
|
|
/**
|
* 确保指定值在数据库中是唯一的
|
* 该方法通过查询数据库来验证给定的列值是否已经存在,如果存在,则抛出异常,以确保数据的唯一性
|
*
|
* @param valueToCheck 需要检查的值
|
* @param columnExtractor 用于提取实体类字段的函数式接口
|
* @param countWrapperExtractor 用于获取查询条件包装器中记录数的函数式接口
|
* @param errMsg 错误消息模板,当值不唯一时使用
|
* @param <T> 实体类类型
|
* @param <R> 字段类型
|
* @param <V> 需要检查的值的类型
|
* @throws IllegalArgumentException 如果需要检查的值为null,则抛出此异常
|
* @throws FebsException 如果值已存在或在检查过程中发生错误,则抛出此异常
|
*/
|
public static <T, R, V> void ensureUnique(
|
V valueToCheck,
|
SFunction<T, R> columnExtractor,
|
SFunction<LambdaQueryWrapper<T>, Integer> countWrapperExtractor,
|
String errMsg) {
|
// 检查输入值是否为null,如果为null,则抛出IllegalArgumentException异常
|
if (valueToCheck == null) {
|
throw new IllegalArgumentException("The value to check cannot be null while ensureUnique column");
|
}
|
if (columnExtractor == null) {
|
throw new IllegalArgumentException("The columnExtractor cannot be null while ensureUnique column");
|
}
|
if (countWrapperExtractor == null) {
|
throw new IllegalArgumentException("The countWrapperExtractor cannot be null while ensureUnique column");
|
}
|
|
int count = 0;
|
try {
|
// 创建LambdaQueryWrapper对象,用于构建查询条件
|
LambdaQueryWrapper<T> wrapper = new LambdaQueryWrapper<>();
|
// 添加等于条件,检查的字段=需要检查的值
|
wrapper.eq(columnExtractor, valueToCheck);
|
// 执行查询并获取结果数量
|
count = countWrapperExtractor.apply(wrapper);
|
|
} catch (Exception e) {
|
// 记录异常日志
|
// 如果在检查过程中发生异常,抛出FebsException异常
|
throw new FebsException(StrUtil.format("An error occurred while ensuring unique column: {}", valueToCheck));
|
}
|
|
// 如果结果数量大于0,说明值已存在,抛出FebsException异常
|
if (count > 0) {
|
throw new FebsException(StrUtil.format(errMsg, valueToCheck));
|
}
|
}
|
|
|
/**
|
* 确保两个参数相等,如果不相等则抛出异常
|
*
|
* @param value1 第一个参数
|
* @param value2 第二个参数
|
* @param errMsg 当两个参数不相等时抛出的异常消息格式字符串
|
* @throws FebsException 如果两个参数不相等
|
*/
|
public static <T> void ensureEqual(
|
T value1,
|
T value2,
|
String errMsg) {
|
// 使用 Objects.equals 处理 null 值,避免显式 null 检查
|
if (!Objects.equals(value1, value2)) {
|
// 延迟字符串格式化,只在需要抛出异常时进行
|
throw new FebsException(StrUtil.format(errMsg, value1, value2));
|
}
|
}
|
|
|
}
|