mirror of
https://codeup.aliyun.com/64f7d6b8ce01efaafef1e678/coal/coal.git
synced 2026-01-25 07:46:40 +08:00
完善错误消息
This commit is contained in:
@@ -1026,7 +1026,7 @@ public class Codegen {
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
private static void saveFile(String pkg, TypeSpec entity) throws IOException {
|
||||
public static void saveFile(String pkg, TypeSpec entity) throws IOException {
|
||||
|
||||
JavaFile javaFile = JavaFile.builder(pkg, entity).build();
|
||||
|
||||
|
||||
63
src/main/java/cn/lihongjie/coal/ErrorMsgCodeGen.java
Normal file
63
src/main/java/cn/lihongjie/coal/ErrorMsgCodeGen.java
Normal file
@@ -0,0 +1,63 @@
|
||||
package cn.lihongjie.coal;
|
||||
|
||||
import cn.lihongjie.coal.errorMsg.entity.ErrorMsgEntity;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.google.common.base.CaseFormat;
|
||||
import com.squareup.javapoet.FieldSpec;
|
||||
import com.squareup.javapoet.TypeSpec;
|
||||
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.experimental.UtilityClass;
|
||||
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.*;
|
||||
|
||||
import javax.lang.model.element.Modifier;
|
||||
|
||||
public class ErrorMsgCodeGen {
|
||||
|
||||
@SneakyThrows
|
||||
public static void main(String[] args) {
|
||||
|
||||
ClassPathResource classPathResource = new ClassPathResource("/config/errorMsg.json");
|
||||
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
|
||||
|
||||
List<ErrorMsgEntity> errorMsgs;
|
||||
|
||||
try (InputStream inputStream = classPathResource.getInputStream()) {
|
||||
errorMsgs = mapper.readValue(inputStream, new TypeReference<List<ErrorMsgEntity>>() {});
|
||||
}
|
||||
|
||||
TypeSpec.Builder builder =
|
||||
TypeSpec.classBuilder("ErrorMsgCode")
|
||||
.addModifiers(Modifier.PUBLIC)
|
||||
.addAnnotation(UtilityClass.class);
|
||||
|
||||
for (ErrorMsgEntity errorMsg : errorMsgs) {
|
||||
|
||||
builder.addField(
|
||||
FieldSpec.builder(
|
||||
String.class,
|
||||
CaseFormat.LOWER_UNDERSCORE.to(
|
||||
CaseFormat.UPPER_UNDERSCORE,
|
||||
errorMsg.getCode().replace(".", "_")),
|
||||
Modifier.PUBLIC,
|
||||
Modifier.STATIC,
|
||||
Modifier.FINAL)
|
||||
.initializer("$S", "{" + errorMsg.getCode() + "}")
|
||||
.build());
|
||||
}
|
||||
|
||||
builder.build();
|
||||
|
||||
|
||||
Codegen.saveFile("cn.lihongjie.coal.errorMsg", builder.build());
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
package cn.lihongjie.coal.empSalaryItem.dto;
|
||||
|
||||
import cn.lihongjie.coal.base.dto.OrgCommonDto;
|
||||
import cn.lihongjie.coal.empSalaryItem.entity.EmpSalaryItemEntity;
|
||||
import cn.lihongjie.coal.validator.OrgUniq;
|
||||
import cn.lihongjie.coal.validator.RequireCode;
|
||||
import cn.lihongjie.coal.validator.RequireName;
|
||||
|
||||
@@ -12,6 +14,8 @@ import org.hibernate.annotations.Formula;
|
||||
@Data
|
||||
@RequireName
|
||||
@RequireCode
|
||||
@OrgUniq(fields = {"code"}, message = "{uniq.code}", entityClass = EmpSalaryItemEntity.class)
|
||||
@OrgUniq(fields = {"name"}, message = "{uniq.name}", entityClass = EmpSalaryItemEntity.class)
|
||||
public class UpdateEmpSalaryItemDto extends OrgCommonDto {
|
||||
|
||||
|
||||
|
||||
24
src/main/java/cn/lihongjie/coal/errorMsg/ErrorMsgCode.java
Normal file
24
src/main/java/cn/lihongjie/coal/errorMsg/ErrorMsgCode.java
Normal file
@@ -0,0 +1,24 @@
|
||||
package cn.lihongjie.coal.errorMsg;
|
||||
|
||||
import lombok.experimental.UtilityClass;
|
||||
|
||||
@UtilityClass
|
||||
public class ErrorMsgCode {
|
||||
public static final String SUCCESS = "{success}";
|
||||
|
||||
public static final String REQUIRE_DEFAULT = "{require.default}";
|
||||
|
||||
public static final String REQUIRE_NAME = "{require.name}";
|
||||
|
||||
public static final String REQUIRE_CODE = "{require.code}";
|
||||
|
||||
public static final String REQUIRE_SORTKEY = "{require.sortKey}";
|
||||
|
||||
public static final String REQUIRE_FILEIDS = "{require.fileIds}";
|
||||
|
||||
public static final String UNIQ_DEFAULT = "{uniq.default}";
|
||||
|
||||
public static final String UNIQ_NAME = "{uniq.name}";
|
||||
|
||||
public static final String UNIQ_CODE = "{uniq.code}";
|
||||
}
|
||||
39
src/main/java/cn/lihongjie/coal/validator/OrgUniq.java
Normal file
39
src/main/java/cn/lihongjie/coal/validator/OrgUniq.java
Normal file
@@ -0,0 +1,39 @@
|
||||
package cn.lihongjie.coal.validator;
|
||||
|
||||
import static java.lang.annotation.ElementType.*;
|
||||
|
||||
import jakarta.validation.Constraint;
|
||||
import jakarta.validation.Payload;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
import java.lang.annotation.Documented;
|
||||
|
||||
@Target({ TYPE, ANNOTATION_TYPE, TYPE_USE })
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Constraint(validatedBy = OrgUniqValidator.class)
|
||||
@Documented
|
||||
@Repeatable(OrgUniq.List.class)
|
||||
public @interface OrgUniq {
|
||||
|
||||
String message() default "{uniq.default}";
|
||||
|
||||
|
||||
String[] fields()default {} ;
|
||||
|
||||
Class<?> entityClass() default Object.class;
|
||||
String entityName() default "";
|
||||
|
||||
String idField() default "id";
|
||||
|
||||
Class<?>[] groups() default { };
|
||||
|
||||
Class<? extends Payload>[] payload() default { };
|
||||
|
||||
|
||||
@Target({ TYPE, ANNOTATION_TYPE, TYPE_USE })
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
@interface List {
|
||||
OrgUniq[] value();
|
||||
}
|
||||
}
|
||||
104
src/main/java/cn/lihongjie/coal/validator/OrgUniqValidator.java
Normal file
104
src/main/java/cn/lihongjie/coal/validator/OrgUniqValidator.java
Normal file
@@ -0,0 +1,104 @@
|
||||
package cn.lihongjie.coal.validator;
|
||||
|
||||
import cn.lihongjie.coal.common.Ctx;
|
||||
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.PersistenceContext;
|
||||
import jakarta.persistence.TypedQuery;
|
||||
import jakarta.validation.ConstraintValidator;
|
||||
import jakarta.validation.ConstraintValidatorContext;
|
||||
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import org.apache.commons.beanutils.PropertyUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
@Slf4j
|
||||
public class OrgUniqValidator implements ConstraintValidator<OrgUniq, Object> {
|
||||
|
||||
@PersistenceContext
|
||||
private EntityManager entityManager;
|
||||
private String[] fields;
|
||||
private String entity;
|
||||
private String idField;
|
||||
|
||||
@Override
|
||||
public void initialize(OrgUniq constraintAnnotation) {
|
||||
|
||||
this.fields = constraintAnnotation.fields();
|
||||
|
||||
this.entity =
|
||||
StringUtils.firstNonBlank(
|
||||
constraintAnnotation.entityName(),
|
||||
constraintAnnotation.entityClass() != null
|
||||
? constraintAnnotation.entityClass().getSimpleName()
|
||||
: "");
|
||||
|
||||
this.idField = constraintAnnotation.idField();
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
@Override
|
||||
public boolean isValid(Object value, ConstraintValidatorContext context) {
|
||||
|
||||
|
||||
|
||||
try {
|
||||
|
||||
StringBuilder sb = new StringBuilder("select count(1) from ").append(entity).append(" where 1=1 ");
|
||||
|
||||
for (String field : fields) {
|
||||
|
||||
sb.append(" and ").append(field).append(" = :").append(field);
|
||||
|
||||
}
|
||||
|
||||
sb.append(" and organizationId = :organizationId");
|
||||
|
||||
String id = "";
|
||||
try {
|
||||
id = (String) PropertyUtils.getProperty(value, "id");
|
||||
} catch (Exception e) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
if (StringUtils.isNotBlank(id)) {
|
||||
sb.append(" and " +
|
||||
idField +
|
||||
" != :id");
|
||||
}
|
||||
|
||||
|
||||
TypedQuery<Long> query = entityManager.createQuery(sb.toString(), Long.class);
|
||||
|
||||
|
||||
for (String field : fields) {
|
||||
query.setParameter(field, PropertyUtils.getProperty(value, field));
|
||||
}
|
||||
|
||||
try{
|
||||
query.setParameter("organizationId", PropertyUtils.getProperty(value, "organizationId"));
|
||||
}catch (NoSuchMethodException e){
|
||||
|
||||
query.setParameter("organizationId", Ctx.currentUser().getOrganizationId());
|
||||
}
|
||||
|
||||
if (StringUtils.isNotBlank(id)) {
|
||||
query.setParameter("id", id);
|
||||
}
|
||||
|
||||
|
||||
Long count = query.getSingleResult();
|
||||
|
||||
return count == 0;
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("error", e);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
39
src/main/java/cn/lihongjie/coal/validator/Uniq.java
Normal file
39
src/main/java/cn/lihongjie/coal/validator/Uniq.java
Normal file
@@ -0,0 +1,39 @@
|
||||
package cn.lihongjie.coal.validator;
|
||||
|
||||
import static java.lang.annotation.ElementType.*;
|
||||
|
||||
import jakarta.validation.Constraint;
|
||||
import jakarta.validation.Payload;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
import java.lang.annotation.Documented;
|
||||
|
||||
@Target({ TYPE, ANNOTATION_TYPE, TYPE_USE })
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Constraint(validatedBy = UniqValidator.class)
|
||||
@Documented
|
||||
@Repeatable(Uniq.List.class)
|
||||
public @interface Uniq {
|
||||
|
||||
String message() default "{uniq.default}";
|
||||
|
||||
|
||||
String[] fields()default {} ;
|
||||
|
||||
|
||||
|
||||
Class<?> entityClass() default Object.class;
|
||||
String entityName() default "";
|
||||
String idField() default "id";
|
||||
Class<?>[] groups() default { };
|
||||
|
||||
Class<? extends Payload>[] payload() default { };
|
||||
|
||||
|
||||
@Target({ TYPE, ANNOTATION_TYPE, TYPE_USE })
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
@interface List {
|
||||
Uniq[] value();
|
||||
}
|
||||
}
|
||||
86
src/main/java/cn/lihongjie/coal/validator/UniqValidator.java
Normal file
86
src/main/java/cn/lihongjie/coal/validator/UniqValidator.java
Normal file
@@ -0,0 +1,86 @@
|
||||
package cn.lihongjie.coal.validator;
|
||||
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.PersistenceContext;
|
||||
import jakarta.persistence.TypedQuery;
|
||||
import jakarta.validation.ConstraintValidator;
|
||||
import jakarta.validation.ConstraintValidatorContext;
|
||||
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import org.apache.commons.beanutils.PropertyUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
@Slf4j
|
||||
public class UniqValidator implements ConstraintValidator<Uniq, Object> {
|
||||
|
||||
@PersistenceContext private EntityManager entityManager;
|
||||
private String[] fields;
|
||||
private String entity;
|
||||
private String idField
|
||||
;
|
||||
|
||||
@Override
|
||||
public void initialize(Uniq constraintAnnotation) {
|
||||
|
||||
this.fields = constraintAnnotation.fields();
|
||||
this.entity =
|
||||
StringUtils.firstNonBlank(
|
||||
constraintAnnotation.entityName(),
|
||||
constraintAnnotation.entityClass() != null
|
||||
? constraintAnnotation.entityClass().getSimpleName()
|
||||
: "");
|
||||
this.idField = constraintAnnotation.idField();
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
@Override
|
||||
public boolean isValid(Object value, ConstraintValidatorContext context) {
|
||||
|
||||
try {
|
||||
|
||||
StringBuilder sb =
|
||||
new StringBuilder("select count(1) from ").append(entity).append(" where 1=1 ");
|
||||
|
||||
for (String field : fields) {
|
||||
|
||||
sb.append(" and ").append(field).append(" = :").append(field);
|
||||
}
|
||||
|
||||
String id = "";
|
||||
|
||||
try {
|
||||
id = (String) PropertyUtils.getProperty(value, "id");
|
||||
} catch (Exception e) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
if (StringUtils.isNotBlank(id)) {
|
||||
sb.append(" and " +
|
||||
idField +
|
||||
" != :id");
|
||||
}
|
||||
|
||||
|
||||
TypedQuery<Long> query = entityManager.createQuery(sb.toString(), Long.class);
|
||||
|
||||
for (String field : fields) {
|
||||
query.setParameter(field, value.getClass().getField(field).get(value));
|
||||
}
|
||||
|
||||
if (StringUtils.isNotBlank(id)) {
|
||||
query.setParameter("id", id);
|
||||
}
|
||||
|
||||
Long count = query.getSingleResult();
|
||||
|
||||
return count == 0;
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("error", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -22,5 +22,19 @@
|
||||
{
|
||||
"code": "require.fileIds",
|
||||
"msg": "必须上传附件"
|
||||
},
|
||||
|
||||
{
|
||||
"code": "uniq.default",
|
||||
"msg": "重复数据"
|
||||
},
|
||||
{
|
||||
"code": "uniq.name",
|
||||
"msg": "名称重复"
|
||||
},
|
||||
{
|
||||
"code": "uniq.code",
|
||||
"msg": "编码重复"
|
||||
}
|
||||
|
||||
]
|
||||
Reference in New Issue
Block a user