package com.syjy.calculate.function;

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 SumDifferenceSquare 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);
        // 将参数转为集合
        Sequence seq = CommonUtil.getSeq(formulaParam.getFirst(), env);
        BigDecimal sum;
        // 拼接表达式
        String exp = "differenceSquare(" + formulaParam.getSecond() + "," + formulaParam.getThird() + ")";
        // 根据公式和集合，循环计算公式结果并求和
        sum = CommonUtil.sum(exp, seq);
        return new AviatorDecimal(sum);
    }

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