提交 2aa37ccd 编写于 作者: zhouweidong's avatar zhouweidong

属性值规则(规则组)

上级 7097d1bb
...@@ -122,7 +122,7 @@ TARGET=PSDELOGIC ...@@ -122,7 +122,7 @@ TARGET=PSDELOGIC
<#assign targetDBValueOP=LogicLinkCond.getPSDBValueOPId()><#comment>表达式</#comment> <#assign targetDBValueOP=LogicLinkCond.getPSDBValueOPId()><#comment>表达式</#comment>
<#assign targetValue=LogicLinkCond.getValue()><#comment>值项</#comment> <#assign targetValue=LogicLinkCond.getValue()><#comment>值项</#comment>
<#if targetDEField!=''> <#if targetDEField!=''>
targetFieldName=srfcaseformat(targetDEField.codeName,'l_u2lC') ; <#assign targetFieldName=srfcaseformat(targetDEField.codeName,'l_u2lC')>
</#if> </#if>
<#assign condBody="RuleUtils.test($"+logicName+targetParam.getCodeName()?lower_case+".get(\""+targetFieldName+"\"),\""+targetDBValueOP+"\","+"\""+targetValue+"\")"> <#assign condBody="RuleUtils.test($"+logicName+targetParam.getCodeName()?lower_case+".get(\""+targetFieldName+"\"),\""+targetDBValueOP+"\","+"\""+targetValue+"\")">
<#assign fieldCond=fieldCond+condBody > <#assign fieldCond=fieldCond+condBody >
......
<#ibiztemplate>
TARGET=PSSYSTEM
</#ibiztemplate>
<#if sys.codeName == 'Sample'>
package ${pub.getPKGCodeName()}.util.annotation;
import ${pub.getPKGCodeName()}.util.service.ValueRuleValidator;
import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
@Target({FIELD, METHOD, PARAMETER, ANNOTATION_TYPE, TYPE_USE})
@Retention(RUNTIME)
@Constraint(validatedBy = {ValueRuleValidator.class})
@Documented
public @interface ValueRuleCheck {
String field();
String rule();
String message() default "值规则检查异常!";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
@Target({FIELD, METHOD, PARAMETER, ANNOTATION_TYPE})
@Retention(RUNTIME)
@Documented
@interface List {
ValueRuleCheck[] value();
}
}
</#if>
\ No newline at end of file
<#ibiztemplate>
TARGET=PSSYSTEM
</#ibiztemplate>
<#if sys.codeName == 'Sample'>
package ${pub.getPKGCodeName()}.util.service;
import ${pub.getPKGCodeName()}.util.annotation.ValueRuleCheck;
import ${pub.getPKGCodeName()}.util.domain.DTOBase;
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.util.ObjectUtils;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashMap;
import java.util.Map;
public class ValueRuleValidator implements ConstraintValidator<ValueRuleCheck, DTOBase> {
private static Map<String, String> sysRule = new HashMap<>();
private final ExpressionParser parser = new SpelExpressionParser();
private String field;
private String rule;
@Override
public void initialize(ValueRuleCheck constraintAnnotation) {
this.field = constraintAnnotation.field();
this.rule = constraintAnnotation.rule();
}
@Override
public boolean isValid(DTOBase dto, ConstraintValidatorContext constraintContext) {
if (ObjectUtils.isEmpty(dto) || ObjectUtils.isEmpty(rule) || ObjectUtils.isEmpty(sysRule.get(rule))) {
return true;
}
EvaluationContext searchContextCtx = new StandardEvaluationContext();
searchContextCtx.setVariable("dto", dto);
Expression searchContextExp = parser.parseExpression(sysRule.get(rule));
return searchContextExp.getValue(searchContextCtx, Boolean.class);
}
static {
<#if sys.getAllPSDataEntities()??>
<#list sys.getAllPSDataEntities() as de>
<#if de.getAllPSDEFValueRules()??>
<#list de.getAllPSDEFValueRules() as deRule>
<#if deRule.codeName!='Default'>
<#if deRule.getPSDEFVRGroupCondition()??>
<#assign group=deRule.getPSDEFVRGroupCondition()>
<#assign ruleName=de.codeName+"_"+deRule.getPSDEField().codeName+"_"+deRule.codeName>
<#assign ruleCond=getGroupCond(group)>
sysRule.put("${ruleName}", "${ruleCond}");
</#if>
</#if>
</#list>
</#if>
</#list>
</#if>
}
}
</#if>
<#comment>获取组条件表达式</#comment>
<#function getGroupCond group>
<#assign strRuleCond="(">
<#if group.getPSDEFVRConditions()??>
<#assign condOp=group.getCondOp()?replace("AND","&&")?replace("OR","||")>
<#list group.getPSDEFVRConditions() as childRule>
<#assign childRuleType=childRule.getCondType()>
<#if childRuleType=='GROUP'>
<#assign strRuleCond=strRuleCond+getGroupCond(childRule)>
<#else>
<#assign strRuleCond=strRuleCond+getFieldCond(childRule)>
</#if>
<#if childRule_has_next>
<#assign strRuleCond=strRuleCond+condOp>//拼接连接符
</#if>
</#list>
</#if>
<#assign strRuleCond=strRuleCond+")">
<#return strRuleCond/>
</#function>
<#comment>获取单项条件表达式</#comment>
<#function getFieldCond fieldRule>
<#assign fieldCond="(" >
<#assign condBody="">
<#assign targetFieldName=fieldRule.getDEFName()><#comment>目标属性名称</#comment>
<#assign targetDEField=((de.getPSDEField(targetFieldName,true))!'')><#comment>目标属性</#comment>
<#assign targetDBValueOP=fieldRule.getCondOp()><#comment>表达式</#comment>
<#assign targetValue=fieldRule.getParamValue()><#comment>值项</#comment>
<#if targetDEField!=''>
<#assign targetFieldName=srfcaseformat(targetDEField.codeName,'l_u2lC')>
</#if>
<#assign condBody="T(${pub.getPKGCodeName()}.util.helper.RuleUtils).test(#dto.get(\""+targetFieldName+"\"),\""+targetDBValueOP+"\","+"\""+targetValue+"\")">
<#assign fieldCond=fieldCond+condBody >
<#assign fieldCond=fieldCond+")" >
<#return fieldCond/>
</#function>
\ No newline at end of file
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册