package com.syjy.calculate.function;

import com.googlecode.aviator.AviatorEvaluator;
import com.googlecode.aviator.runtime.function.AbstractFunction;
import com.googlecode.aviator.runtime.type.AviatorDecimal;
import com.googlecode.aviator.runtime.type.AviatorJavaType;
import com.googlecode.aviator.runtime.type.AviatorObject;
import com.googlecode.aviator.runtime.type.Sequence;
import com.syjy.calculate.entity.FormulaParam;

import java.math.BigDecimal;
import java.util.Map;

/**
 * 计算准确率 :单点短期准确率
 *
 * @author zcl
 * @version 1.0
 * @since 2022/9/8 9:30
 */
public class PointS extends AbstractFunction {
    @Override
    public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2, AviatorObject arg3) {
        // 获取参数
        FormulaParam formulaParam = CommonUtil.getFormulaParam(env, arg1, arg2, arg3, null, null);
        BigDecimal result = BigDecimal.ZERO;
        // 将参数转为集合
        Sequence seq = CommonUtil.getSeq(formulaParam.getFirst(), env);
        // 循环集合
        for (Object obj : seq) {
            Map<String, Object> map = (Map<String, Object>) obj;
            String exp = "1-math.abs((" + formulaParam.getSecond() + "-" + formulaParam.getThird() + ")/" + formulaParam.getThird() + ")";
            result = new BigDecimal(String.valueOf(AviatorEvaluator.execute(exp, map)));
            break;
        }
        return new AviatorDecimal(result);
    }

    @Override
    public String getName() {
        return "pointS";
    }
}
