1、公式校验,校验if判断

This commit is contained in:
zhengyf 2025-08-05 14:41:59 +08:00
parent edb450daad
commit 6c89e1665c
1 changed files with 72 additions and 3 deletions

View File

@ -246,23 +246,92 @@ public class AeConfVoucherTemplateServiceImpl extends BaseService<AeConfVoucherT
return null; return null;
} }
/**
* 公式校验
* -判断前后是否为数值数值相减字符连接
* +-+为连接符
* if(A>0?b:c)
* substr(A,0,4)
*/
@Override @Override
public boolean checkAbstract(AeConfVoucherTemplateEntity entity) { public boolean checkAbstract(AeConfVoucherTemplateEntity entity) {
String abstractRes = entity.getAbstractRes(); String abstractRes = entity.getAbstractRes();
// 校验基本格式 // // 提取括号中的内容
if (abstractRes.contains("++") || abstractRes.startsWith("+") || abstractRes.endsWith("+")) { // List<String> parenthesesContents = new ArrayList<>();
// Pattern contentPattern = Pattern.compile("\\((.*?)\\)");
// Matcher contentMatcher = contentPattern.matcher(abstractRes);
// while (contentMatcher.find()) {
// parenthesesContents.add(contentMatcher.group(1));
// }
//
// // 提取运算符(-+)
// List<String> operators = new ArrayList<>();
// Pattern operatorPattern = Pattern.compile("(?<=\\))([-+])(?=\\()");
// Matcher operatorMatcher = operatorPattern.matcher(abstractRes);
// while (operatorMatcher.find()) {
// operators.add(operatorMatcher.group(1));
// }
//
// // 输出结果
// System.out.println("括号中的内容:");
// for (int i = 0; i < parenthesesContents.size(); i++) {
// System.out.println("内容 " + (i + 1) + ": " + parenthesesContents.get(i));
// }
//
// System.out.println("运算符:");
// for (int i = 0; i < operators.size(); i++) {
// System.out.println("运算符 " + (i + 1) + ": " + operators.get(i));
// }
// // 校验基本格式
if (abstractRes.contains("++") || abstractRes.endsWith("+")) {
return false; return false;
} }
String[] parts = abstractRes.split("\\+"); String[] parts = abstractRes.split("\\+");
for (String part : parts) { for (String part : parts) {
//判断if公式 if(A>0?b:c)
if (part.startsWith("if(")) {
// 定义正则表达式模式
String regex = "if\\(\\s*([^<>]+?)\\s*([<>]=?)\\s*(\\d+)\\s*\\?\\s*([^:]+?)\\s*:\\s*([^)]+?)\\s*\\)";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(part);
if (matcher.find()) {
String left = matcher.group(1).trim(); // 条件左值
String op = matcher.group(2); // 操作符 > >= < <=
String right = matcher.group(3); // 右值 3
String trueValue = matcher.group(4); // ? 后的值 1
String falseValue = matcher.group(5); // : 后的值 2
if (left == null || op == null || right == null || trueValue == null || falseValue == null) {
return false;
}
String condition = left + op + right; // 完整条件A>3
System.out.println("条件: " + condition);
System.out.println("真值: " + trueValue);
System.out.println("假值: " + falseValue);
if (left.startsWith("@@$")) {
String fieldPath = left.substring(3); // 去除 @@$
if (!isValidFieldPath(entity.getMdmId(), fieldPath)) {
return false;
}
}
}else {
return false;
}
}
//判断substr公式 //判断substr公式
if (part.startsWith("substr(")) { if (part.startsWith("substr(")) {
// 定义正则表达式模式 // 定义正则表达式模式
String regex = "substr\\(([^(),]+|([^()]*\\([^()]*\\)[^()]*)*),\\s*(\\d+),\\s*(\\d+)\\)"; // String regex = "substr\\(([^(),]+|([^()]*\\([^()]*\\)[^()]*)*),\\s*(\\d+),\\s*(\\d+)\\)";
String regex = "if\\(([^<>]+)([<>]=?)(\\d+)\\?([^:]+):([^)]+)\\)";
Pattern pattern = Pattern.compile(regex); Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(part); Matcher matcher = pattern.matcher(part);
if (matcher.find()) { if (matcher.find()) {