package com.syjy.calculate.function;

import com.googlecode.aviator.AviatorEvaluator;
import com.googlecode.aviator.runtime.RuntimeUtils;
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.CalculateResult;
import com.syjy.calculate.entity.FormulaParam;

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

/**
 * 计算准确率 :均方根准确率绝对值
 *
 * @author zcl
 * @version 1.0
 * @since 2022/9/8 9:30
 */
public class RootMeanSquaredErrorA extends AbstractFunction {
    @Override
    public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2, AviatorObject arg3, AviatorObject arg4) {
        // 获取参数
        FormulaParam formulaParam = CommonUtil.getFormulaParam(env, arg1, arg2, arg3, arg4, null);
        env.put("list", formulaParam.getFirst());
        // 拼接公式
        String exp = "sumDifferenceAbsolute(list," + formulaParam.getSecond() + "," + formulaParam.getThird() + ")";
        // 获取Σ|实际-预测|
        BigDecimal sumDifferenceAbsolute = new BigDecimal(String.valueOf(AviatorEvaluator.execute(exp, env)));
        //当所有点位的预测和实际相减之和为0 ，说明所有点 预测和实际相等 那么准确率是100%
        if (sumDifferenceAbsolute.compareTo(BigDecimal.ZERO) == 0) {
            return new AviatorDecimal(CalculateResult.ONE_HUNDRED);
        }
        BigDecimal sum = BigDecimal.ZERO;
        Sequence seq = RuntimeUtils.seq(formulaParam.getFirst(), env);
        // 循环集合获取实际功率、预测功率信息并计算
        for (Object obj : seq) {
            Map<String, Object> map = (Map<String, Object>) obj;
            map.put("sumDifferenceAbsolute", sumDifferenceAbsolute);
            String forExp = "differenceSquare(" + formulaParam.getSecond() + "," + formulaParam.getThird() + ") * (differenceAbsolute(" + formulaParam.getSecond() + "," + formulaParam.getThird() + ")/sumDifferenceAbsolute)";
            BigDecimal forResult = new BigDecimal(String.valueOf(AviatorEvaluator.execute(forExp, map)));
            sum = sum.add(forResult);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("sum", sum);
        // 获取开根号结果并转为BigDecimal类型(因为Aviator中，只要有double类型的数据进行计算，结果就会是double类型，开根号的计算结果必为double，所以这里先将开根号计算的结果转为BigDecimal)
        BigDecimal sqrtValue = new BigDecimal(String.valueOf(AviatorEvaluator.execute("math.sqrt(sum)", map)));
        // 获取容量
        BigDecimal electricCapacity = (BigDecimal) env.get("electricCapacity");
        // 获取容量
        BigDecimal maxCapacity = (BigDecimal) env.get("maxCapacity");
        map.put("sqrtValue", sqrtValue);
        map.put("electricCapacity", electricCapacity);
        map.put("maxCapacity", maxCapacity);
        String expFinal = "1-(sqrtValue/" + formulaParam.getFourth() + ")";
        BigDecimal result = new BigDecimal(String.valueOf(AviatorEvaluator.execute(expFinal, map)));
        result = result.setScale(4, BigDecimal.ROUND_HALF_UP);
        return new AviatorDecimal(result);
    }

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