package cn.autoform.web.client.util; import cn.autoform.bean.form.autowhere.ScreenCondition; import cn.autoform.bean.form.autowhere.ScreenCondition.Condition; import cn.autoform.bean.form.autowhere.ScreenCondition.Condition.Type; import cn.autoform.util.form.AutoConditionUtil; import cn.autoform.util.tool.JSONTool; import cn.autoform.util.tool.ObjectUtil; import com.googlecode.aviator.AviatorEvaluator; import com.googlecode.aviator.exception.CompileExpressionErrorException; import com.googlecode.aviator.runtime.function.AbstractFunction; import com.googlecode.aviator.runtime.type.AviatorBoolean; import com.googlecode.aviator.runtime.type.AviatorObject; import lombok.extern.slf4j.Slf4j; import org.springframework.util.PropertyPlaceholderHelper; import org.springframework.util.SystemPropertyUtils; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Objects; import java.util.regex.Matcher; import java.util.regex.Pattern; @Slf4j public class FormatParsingTool { static { AviatorEvaluator.addFunction(new AbstractFunction(){ @Override public String getName() { return "if"; } @Override public AviatorObject call(Map env, AviatorObject arg1, AviatorObject arg2, AviatorObject arg3) { return arg1.booleanValue(env) ? arg2 : arg3; } }); AviatorEvaluator.addFunction(new AbstractFunction(){ @Override public String getName() { return "empty"; } @Override public AviatorObject call(Map env, AviatorObject arg1) { return AviatorBoolean.valueOf(ObjectUtil.empty(arg1.stringValue(env))); } }); } public static void main(String... args){ System.out.println(AviatorEvaluator.execute("\nif(empty(''), \"\", \"and budget_project = ''\")\n".replace("", ""))); } public static void main1(String... args){ System.out.println(parsingExpression( JSONTool.toObj("{\n" + " \"child\":[\n" + " {\n" + " \"fieldKey\":\"budget_project\",\n" + " \"side\":\"thi\",\n" + " \"pIndex\":0,\n" + " \"value\":\"\"\n" + " },\n" + " {\n" + " \"fieldKey\":\"budget_project\",\n" + " \"side\":\"ref\",\n" + " \"pIndex\":1\n" + " },\n" + " {\n" + " \"fieldKey\":\"budget_project\",\n" + " \"side\":\"thi\",\n" + " \"pIndex\":2,\n" + " \"value\":\"\"\n" + " },\n" + " {\n" + " \"fieldKey\":\"budgetStartDate\",\n" + " \"side\":\"thi\",\n" + " \"pIndex\":3,\n" + " \"value\":\"2018-10-19\"\n" + " },\n" + " {\n" + " \"fieldKey\":\"budgetStartDate\",\n" + " \"side\":\"ref\",\n" + " \"pIndex\":4\n" + " }\n" + " ],\n" + " \"count\":\"processstate = 30 \n" + "and stateExecution=\\\"\\\" \n" + "$(\n" + "if(empty({0}), \\\"\\\", \\\"and {1} = {2}\\\")\n" + ") \n" + "and {3} > {4}\"\n" + " }", ScreenCondition.class) )); } public static String parsingExpression(ScreenCondition screenCondition){ String val = parsing(screenCondition); if(ObjectUtil.empty(val)){ return val; } PropertyPlaceholderHelper p = new PropertyPlaceholderHelper( "$(" , ")" , SystemPropertyUtils.VALUE_SEPARATOR , true); return p.replacePlaceholders(val, (expression)->{ try { return Objects.toString(AviatorEvaluator.execute(Objects.toString(expression, "").replace("\n", ""))); }catch (CompileExpressionErrorException e){ log.error(e.getMessage(), e); throw e; } }); } public static String parsing(ScreenCondition screenCondition){ List child = screenCondition.getChild(); String count = screenCondition.getCount(); List li = null; if(child.size()> 0){ li = new ArrayList<>(); for(Condition m : child){ if(m.getSide() == Type.thi){ li.add("'"+ getVal(m.getValue())+ "'"); }else{ li.add(m.getFieldKey()); } } count = screenCondition.getCount(); } if( null == count || "".equals(count) || count.contains(" or 1=1 ")){ return null; }else{ return parsingString(count, li); } } public static String getVal(Object val){ if(val instanceof String) return AutoConditionUtil.escapeSql(val); else if(val instanceof List){ if(((List)val).size() == 1){ return AutoConditionUtil.escapeSql(((List)val).get(0)); }else if(((List)val).size() > 1){ final StringBuilder vals = new StringBuilder(); ((List)val).stream().filter(v->vals.append(',') != null).forEach(vals::append); return AutoConditionUtil.escapeSql(vals.toString().replaceAll("^,", "")); } } return ""; } public static String parsingString(String count, List li){ Pattern pattern = Pattern.compile("[{][^}]+[}]"); Matcher matcher = pattern.matcher(count); String group = null; Integer index = null; while(matcher.find()){ group = matcher.group(); index = Integer.parseInt(group.replaceAll("[{]|[}]", "")); count = count.replace(group, (CharSequence) li.get(index)); } return count; } }