|
|
|
@ -0,0 +1,81 @@
|
|
|
|
|
package org.dromara.common.mybatis.Utils;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
|
|
|
|
import com.github.yulichang.base.MPJBaseMapper;
|
|
|
|
|
import com.github.yulichang.toolkit.JoinWrappers;
|
|
|
|
|
import com.github.yulichang.wrapper.MPJLambdaWrapper;
|
|
|
|
|
import org.dromara.common.core.exception.ServiceException;
|
|
|
|
|
import org.dromara.common.core.utils.StringUtils;
|
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Component
|
|
|
|
|
public class UniqueCodeUtils {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 通用的编码唯一性校验方法
|
|
|
|
|
* 用于校验数据库中是否存在重复的编码值
|
|
|
|
|
*
|
|
|
|
|
* @param baseMapper 实体对应的Mapper实例,例如:baseMapper
|
|
|
|
|
* @param code 需要校验的编码值,例如:entity.getDeviceCode()
|
|
|
|
|
* @param codeField 实体类中编码字段的Lambda表达式,例如:DmsBaseDeviceBom::getDeviceCode
|
|
|
|
|
* @param idField 实体类中主键字段的Lambda表达式,例如:DmsBaseDeviceBom::getDeviceBomId
|
|
|
|
|
* @param idValue 当前记录的ID值(更新时需要传入,新增时传null),例如:entity.getDeviceBomId()
|
|
|
|
|
* @param errorMessage 重复时的错误提示信息,例如:"设备编码已存在"
|
|
|
|
|
* @param <T> 实体类型
|
|
|
|
|
* @param <M> Mapper类型
|
|
|
|
|
* @throws ServiceException 当编码重复时抛出异常,异常信息为errorMessage
|
|
|
|
|
*
|
|
|
|
|
* 使用示例1 - 新增时校验:
|
|
|
|
|
* uniqueCodeService.validateCodeUnique(
|
|
|
|
|
* baseMapper, // Mapper实例
|
|
|
|
|
* entity.getDeviceCode(), // 待校验的编码
|
|
|
|
|
* DmsBaseDeviceBom::getDeviceCode, // 编码字段
|
|
|
|
|
* DmsBaseDeviceBom::getDeviceBomId,// ID字段
|
|
|
|
|
* null, // 新增时ID为null
|
|
|
|
|
* "设备编码已存在" // 错误提示
|
|
|
|
|
* );
|
|
|
|
|
*
|
|
|
|
|
* 使用示例2 - 更新时校验:
|
|
|
|
|
* uniqueCodeService.validateCodeUnique(
|
|
|
|
|
* baseMapper, // Mapper实例
|
|
|
|
|
* entity.getDeviceCode(), // 待校验的编码
|
|
|
|
|
* DmsBaseDeviceBom::getDeviceCode, // 编码字段
|
|
|
|
|
* DmsBaseDeviceBom::getDeviceBomId,// ID字段
|
|
|
|
|
* entity.getDeviceBomId(), // 更新时传入当前记录ID
|
|
|
|
|
* "设备编码已存在" // 错误提示
|
|
|
|
|
* );
|
|
|
|
|
*/
|
|
|
|
|
public static <T, M extends MPJBaseMapper<T>> void validateCodeUnique(
|
|
|
|
|
M baseMapper,
|
|
|
|
|
String code,
|
|
|
|
|
SFunction<T, String> codeField,
|
|
|
|
|
SFunction<T, ?> idField,
|
|
|
|
|
Object idValue,
|
|
|
|
|
String errorMessage
|
|
|
|
|
) {
|
|
|
|
|
// 1. 首先判断编码是否为空,为空则不需要校验
|
|
|
|
|
if (StringUtils.isNotBlank(code)) {
|
|
|
|
|
// 2. 构建查询条件包装器
|
|
|
|
|
MPJLambdaWrapper<T> wrapper = JoinWrappers.lambda((Class<T>) codeField.getClass())
|
|
|
|
|
// 3. 添加编码相等的条件:WHERE code = 'xxx'
|
|
|
|
|
.eq(codeField, code);
|
|
|
|
|
|
|
|
|
|
// 4. 如果是更新操作(idValue不为空),需要排除自身记录
|
|
|
|
|
if (idValue != null) {
|
|
|
|
|
// 5. 添加ID不等于当前记录ID的条件:AND id != xxx
|
|
|
|
|
// 这样可以排除当前正在更新的记录,避免误报重复
|
|
|
|
|
wrapper.ne(idField, idValue);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 6. 执行查询,获取符合条件的记录数
|
|
|
|
|
Long count = baseMapper.selectCount(wrapper);
|
|
|
|
|
// 7. 如果存在符合条件的记录(count > 0),说明编码重复
|
|
|
|
|
if (count > 0) {
|
|
|
|
|
// 8. 抛出业务异常,中断当前操作
|
|
|
|
|
throw new ServiceException(errorMessage);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|