【validator】更新校验表中唯一编码的问题

pull/3/head
fengshuonan 2020-12-21 21:24:32 +08:00
parent a5fc70e51b
commit 079884ec94
3 changed files with 8 additions and 4 deletions

View File

@ -39,7 +39,7 @@ public @interface TableUniqueValue {
String columnName();
/**
* idid
* id user_id
*/
String idFieldName() default "id";

View File

@ -2,6 +2,7 @@ package cn.stylefeng.roses.kernel.validator.validators.unique;
import cn.hutool.core.lang.Dict;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.stylefeng.roses.kernel.rule.pojo.request.BaseRequest;
import cn.stylefeng.roses.kernel.validator.context.RequestGroupContext;
import cn.stylefeng.roses.kernel.validator.context.RequestParamContext;
@ -117,13 +118,16 @@ public class TableUniqueValueValidator implements ConstraintValidator<TableUniqu
// 获取请求字段中id的值
Dict requestParam = RequestParamContext.get();
// 获取id字段的驼峰命名法
String camelCaseIdFieldName = StrUtil.toCamelCase(idFieldName);
return UniqueValidateParam.builder()
.tableName(tableName)
.columnName(columnName)
.value(fieldValue)
.idFieldName(idFieldName)
.excludeCurrentRecord(Boolean.TRUE)
.id(requestParam.getLong(idFieldName))
.id(requestParam.getLong(camelCaseIdFieldName))
.excludeLogicDeleteItems(excludeLogicDeleteItems)
.logicDeleteFieldName(logicDeleteFieldName)
.logicDeleteValue(logicDeleteValue).build();

View File

@ -42,7 +42,7 @@ public class TableUniqueValueService {
// 不排除当前记录,排除逻辑删除的内容
if (!uniqueValidateParam.getExcludeCurrentRecord()
&& uniqueValidateParam.getExcludeLogicDeleteItems()) {
String sqlTemplate = "select count(*) from {} where {} = {0} and ({} is null || {} <> {})";
String sqlTemplate = "select count(*) from {} where {} = {0} and ({} is null || {} <> '{}')";
String finalSql = StrUtil.format(sqlTemplate,
uniqueValidateParam.getTableName(),
uniqueValidateParam.getColumnName(),
@ -71,7 +71,7 @@ public class TableUniqueValueService {
// id判空
paramIdValidate(uniqueValidateParam);
String sqlTemplate = "select count(*) from {} where {} = {0} and {} <> {1} and ({} is null || {} <> {})";
String sqlTemplate = "select count(*) from {} where {} = {0} and {} <> {1} and ({} is null || {} <> '{}')";
String finalSql = StrUtil.format(sqlTemplate,
uniqueValidateParam.getTableName(),
uniqueValidateParam.getColumnName(),