场景介绍:
写道 其实我想表达的是能不能通过“阶梯1: 交易额5000万以内取55%”这一句话,直接得到一个这样的表达式: if(p1<=50000000) return "55%";?
?
解决方案:
复杂逻辑的脚本的可读性确实是个大问题,QlExpress除了可以在脚本里用自定义的function,更常用的方法是底层业务封装一些基础函数,然后通过函数绑定、caozuofu.html" target="_blank">操作符、宏定义来解决。
以下是我的一个简单的解决方案:
更多介绍在?《QlExpress实战宝典》http://t.cn/RvO2qtn?(提取码:RE1Z0F
?
?
class="java">package com.ql.util.express.test; import org.junit.Test; import com.ql.util.express.DefaultContext; import com.ql.util.express.ExpressRunner; public class StepTest { @Test public void testMax() throws Exception { String express = "阶梯1 = 阶梯(0.0,100.0,0.2);阶梯2 = 阶梯(100.0,200.0,0.15);阶梯3 = 阶梯(200.0,10000.0,0.1);阶梯取值(220,阶梯1,阶梯2,阶梯3)"; ExpressRunner runner = new ExpressRunner(false,true); DefaultContext<String, Object> context = new DefaultContext<String, Object>(); runner.addFunctionOfClassMethod("阶梯", "com.ql.util.express.test.Step", "createStep", new Class[]{double.class,double.class,double.class}, null); runner.addFunctionOfClassMethod("阶梯取值", "com.ql.util.express.test.Step", "chooseStep", new Class[]{double.class,Step.class,Step.class,Step.class}, null); Object r = runner.execute(express, context, null, false, true); System.out.println(r); } }
?
package com.ql.util.express.test; public class Step { private double min; private double max; private double value; public double getMin() { return min; } public void setMin(double min) { this.min = min; } public double getMax() { return max; } public void setMax(double max) { this.max = max; } public double getValue() { return value; } public void setValue(double value) { this.value = value; } public static Step createStep(double _min,double _max,double _value) { Step step = new Step(); step.min = _min; step.max = _max; step.value = _value; return step; } public static double chooseStep(double input,Step step1,Step step2,Step step3) { Step[] steps = {step1,step2,step3}; for(Step step:steps){ if(step.min<=input && step.max>=input){ return step.value; } } return -1; } }
?
?