提交 0b048555 编写于 作者: zhouweidong's avatar zhouweidong

补充实体属性值检查

上级 54a25bd2
......@@ -23,6 +23,7 @@ import ${pub.getPKGCodeName()}.util.annotation.DEField;
import ${pub.getPKGCodeName()}.util.enums.DEPredefinedFieldType;
import ${pub.getPKGCodeName()}.util.enums.DEFieldDefaultValueType;
import ${pub.getPKGCodeName()}.util.helper.DataObject;
import ${pub.getPKGCodeName()}.util.enums.DupCheck;
import java.io.Serializable;
import lombok.*;
import org.springframework.data.annotation.Transient;
......@@ -113,6 +114,14 @@ public class ${item.getCodeName()} extends EntityMP implements Serializable {
<#assign defieldano=defieldano+",fieldType=\"DATETIME\",format=\"yyyy-MM-dd HH:mm:ss\"">
</#if>
</#if>
<#if defield.getDupCheckMode?? && defield.getDupCheckMode()?? && defield.getDupCheckMode()=='ALL'>
<#if defield.getDupCheckPSDEField?? && defield.getDupCheckPSDEField()??>
<#assign dupCheckField = srfcaseformat(defield.getDupCheckPSDEField().getCodeName(),'l_u2lC') >
<#assign defieldano=defieldano+",dupCheck = DupCheck.ALL , dupCheckField=\""+dupCheckField+"\"">
<#else>
<#assign defieldano=defieldano+",dupCheck = DupCheck.ALL" >
</#if>
</#if>
</#if>
/**
* ${defield.getLogicName()}
......
......@@ -66,6 +66,7 @@ TARGET=PSDATAENTITY
<#break>
</#list>
</#if>
<#assign isDupCheck=isDupCheckEntity(de)>
package ${pub.getPKGCodeName()}.core.${item.getPSSystemModule().getCodeName()?lower_case}.service.impl;
import java.io.Serializable;
......@@ -962,6 +963,9 @@ public class ${item.getCodeName()}ServiceImpl extends ServiceImpl<${de.getCodeNa
<#comment>发布es服务对象</#comment>
<@esAnno/>
<#comment>重复值检查-searchContext</#comment>
<@outputSearchContext/>
}
<#comment>NOSQL存储</#comment>
......@@ -1455,6 +1459,9 @@ public class ${item.getCodeName()}ServiceImpl implements I${de.getCodeName()}Ser
<#comment>发布es服务对象</#comment>
<@esAnno/>
<#comment>重复值检查-SearchContext</#comment>
<@outputSearchContext/>
}
......@@ -1965,6 +1972,9 @@ public class ${item.getCodeName()}ServiceImpl implements I${de.getCodeName()}Ser
<#comment>实体数据导入</#comment>
<@deImportData/>
<#comment>重复值检查-SearchContext</#comment>
<@outputSearchContext/>
}
<#comment>无存储</#comment>
......@@ -2955,4 +2965,31 @@ public class ${item.getCodeName()}ServiceImpl implements I${de.getCodeName()}Ser
</#macro>
<#comment>输出SearchContext,用于属性重复值检查</#comment>
<#macro outputSearchContext>
<#if isDupCheck>
/**
* 获取searchContext
* @return
*/
public ${de.codeName}SearchContext getSearchContext(){
return new ${de.codeName}SearchContext();
}
</#if>
</#macro>
<#comment>判断属性是否配置了重复值检查</#comment>
<#function isDupCheckEntity dataEntity>
<#assign dupCheck=false>
<#if dataEntity.getAllPSDEFields()??>
<#list dataEntity.getAllPSDEFields() as defield>
<#if defield.getDupCheckMode?? && defield.getDupCheckMode()?? && defield.getDupCheckMode()=='ALL'>
<#assign dupCheck=true>
<#break>
</#if>
</#list>
</#if>
<#return dupCheck>
</#function>
</#if>
......@@ -5,6 +5,7 @@ package ${pub.getPKGCodeName()}.util.annotation;
import ${pub.getPKGCodeName()}.util.enums.DEFieldDefaultValueType;
import ${pub.getPKGCodeName()}.util.enums.DEPredefinedFieldType;
import ${pub.getPKGCodeName()}.util.enums.DupCheck;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
......@@ -69,5 +70,14 @@ public @interface DEField
* @return
*/
String format() default "";
/**
* 重复性检查
* @return
*/
DupCheck dupCheck() default DupCheck.NONE;
/**
* 范围属性
*/
String dupCheckField() default "";
}
<#ibiztemplate>
TARGET=PSSYSTEM
</#ibiztemplate>
<#assign hasDupCheckEntity=false>
<#list sys.getAllPSDataEntities() as dataEntity>
<#assign isDupCheck=isDupCheckEntity(dataEntity)>
<#if isDupCheck>
<#assign hasDupCheckEntity=true>
<#break>
</#if>
</#list>
<#if hasDupCheckEntity>
package ${pub.getPKGCodeName()}.util.aspect;
import lombok.extern.slf4j.Slf4j;
import ${pub.getPKGCodeName()}.util.annotation.DEField;
import ${pub.getPKGCodeName()}.util.enums.DupCheck;
import ${pub.getPKGCodeName()}.util.errors.BadRequestAlertException;
import ${pub.getPKGCodeName()}.util.filter.QueryFilter;
import ${pub.getPKGCodeName()}.util.filter.SearchContextBase;
import ${pub.getPKGCodeName()}.util.helper.DEFieldCacheMap;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.data.domain.Page;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import java.util.Map;
@Aspect
@Component
@Slf4j
public class DupCheckAspect {
private final ExpressionParser parser = new SpelExpressionParser();
<#list sys.getAllPSDataEntities() as dataEntity>
<#assign isDupCheck=isDupCheckEntity(dataEntity)>
<#assign hasDefaultDS=hasDefaultDS(dataEntity)>
<#if isDupCheck && hasDefaultDS>
/**
* 实体[${dataEntity.codeName}]
*
* @param point
*/
@AfterReturning(value = "(execution(* ${pub.getPKGCodeName()}.core.*.service.*${dataEntity.codeName}*.create*(..))||execution(* ${pub.getPKGCodeName()}.core.*.service.*${dataEntity.codeName}*.update*(..))||execution(* ${pub.getPKGCodeName()}.core.*.service.*${dataEntity.codeName}*.save*(..)) ||execution(* ${pub.getPKGCodeName()}.core.*.service.*${dataEntity.codeName}*.remove*(..))) && !execution(* ${pub.getPKGCodeName()}.core.es.service.*.create*(..)) && !execution(* ${pub.getPKGCodeName()}.core.es.service.*.update*(..)) && !execution(* ${pub.getPKGCodeName()}.core.es.service.*.save*(..)) && !execution(* ${pub.getPKGCodeName()}.core.es.service.*.remove*(..))")
public void check${dataEntity.codeName?lower_case?cap_first}(JoinPoint point) {
check(point, "search${dataEntity.getDefaultPSDEDataSet().codeName}");
}
</#if>
</#list>
/**
* 实体属性重复值检查
* @param point 切点
* @param defaultDS 实体默认数据集名称
*/
private void check(JoinPoint point, String defaultDS) {
Object[] args = point.getArgs();
if (args.length > 0) {
Object entity = args[0];
Object service = point.getTarget();
Map<String, DEField> deFields = DEFieldCacheMap.getDEFields(entity.getClass());
for (Map.Entry<String, DEField> deField : deFields.entrySet()) {
String fieldName = deField.getKey();
try {
DEField fieldAnnotation = deField.getValue();
DupCheck dupCheck = fieldAnnotation.dupCheck();
String dupCheckField=fieldAnnotation.dupCheckField();
if (dupCheck == DupCheck.ALL) {
Object newValue =getDEFieldValue(entity,fieldName);
//获取searchContext
EvaluationContext searchContextCtx = new StandardEvaluationContext();
searchContextCtx.setVariable("service", service);
Expression searchContextExp = parser.parseExpression("#service.getSearchContext()");
SearchContextBase searchContext = searchContextExp.getValue(searchContextCtx, SearchContextBase.class);
//设置检查属性值
QueryFilter filter = new QueryFilter();
filter.eq(DEFieldCacheMap.getFieldColumnName(entity.getClass(), fieldName), newValue);
searchContext.setFilter(filter);
//设定重复值检查范围
if(!StringUtils.isEmpty(dupCheckField)){
Object dupFieldValue=getDEFieldValue(entity,dupCheckField);
filter.eq(DEFieldCacheMap.getFieldColumnName(entity.getClass(), dupCheckField),dupFieldValue);
}
//使用当前值到数据库中进行查询,判断是否重复
EvaluationContext oldValueMappingCtx = new StandardEvaluationContext();
oldValueMappingCtx.setVariable("service", service);
oldValueMappingCtx.setVariable("searchContext", searchContext);
Expression oldValueMappingExp = parser.parseExpression(String.format("#service.%s(#searchContext)",defaultDS));
Page oldData = oldValueMappingExp.getValue(oldValueMappingCtx, Page.class);
if (!ObjectUtils.isEmpty(oldData) && !ObjectUtils.isEmpty(oldData.getContent()) && oldData.getContent().size() > 1) {
throw new BadRequestAlertException(String.format("属性[%s]已存在[%s]值!", fieldName, newValue), "ValueRuleAspect", "repeatCheck");
}
}
} catch (Exception e) {
throw new BadRequestAlertException(String.format("实体属性[%s]重复值检查失败,%s",fieldName,e),"ValueRuleAspect","repeatCheck");
}
}
}
}
/**
* 获取实体属性值
* @param entity
* @param fieldName
* @return
*/
private Object getDEFieldValue(Object entity, String fieldName){
EvaluationContext exMappingCtx = new StandardEvaluationContext();
exMappingCtx.setVariable("entity", entity);
Expression esMappingExp = parser.parseExpression(String.format("#entity.get(\"%s\")", fieldName));
return esMappingExp.getValue(exMappingCtx);
}
}
</#if>
<#comment>判断属性是否配置了重复值检查</#comment>
<#function isDupCheckEntity dataEntity>
<#assign dupCheck=false>
<#if dataEntity.getAllPSDEFields()??>
<#list dataEntity.getAllPSDEFields() as defield>
<#if defield.getDupCheckMode?? && defield.getDupCheckMode()?? && defield.getDupCheckMode()=='ALL'>
<#assign dupCheck=true>
<#break>
</#if>
</#list>
</#if>
<#return dupCheck>
</#function>
<#comment>判断实体中是否配置了默认数据集</#comment>
<#function hasDefaultDS dataEntity>
<#assign defaultDS=false>
<#if dataEntity.getDefaultPSDEDataSet?? && dataEntity.getDefaultPSDEDataSet()?? && dataEntity.getDefaultPSDEDataSet().getPSDEDataQueries()??>
<#list dataEntity.getDefaultPSDEDataSet().getPSDEDataQueries() as dq>
<#assign defaultDS=true>
<#break>
</#list>
</#if>
<#return defaultDS>
</#function>
\ No newline at end of file
<#ibiztemplate>
TARGET=PSDATAENTITY
TARGET=PSSYSTEM
</#ibiztemplate>
<#assign hasESEntity=false>
<#list sys.getAllPSDataEntities() as dataEntity>
......
<#ibiztemplate>
TARGET=PSSYSTEM
</#ibiztemplate>
package ${pub.getPKGCodeName()}.util.enums;
/**
* 属性重复值检查
*/
public enum DupCheck {
/**
* 不检查
*/
NONE,
/**
* 全部检查
*/
ALL,
/**
* 非空检查
*/
NOTNULL,
/**
* 指定范围检查
*/
RANGE,
}
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册