package com.syjy.calculate.function;

import com.googlecode.aviator.AviatorEvaluator;
import com.googlecode.aviator.runtime.RuntimeUtils;
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 CommonUtil {
    public static Sequence getSeq(Object obj, final Map<String, Object> env){
        if (obj == null) {
            throw new NullPointerException("null seq");
        }
        // 将参数放入抽象集合中
        Sequence seq = RuntimeUtils.seq(obj, env);
        return seq;
    }

    /**
     * 根据集合循环公式求和
     * @param exp
     * @param seq
     * @return
     */
    public static BigDecimal sum(String exp,Sequence seq){
        BigDecimal sum = BigDecimal.ZERO;
        for (Object obj : seq) {
            Map<String, Object> map = (Map<String, Object>) obj;
            // 计算: (实际-预测)的平方
            BigDecimal forResult = new BigDecimal(String.valueOf( AviatorEvaluator.execute(exp,map)));
            // 求和
            sum = sum.add(forResult);
        }
        return sum;
    }

    /**
     * 校验参数是否为空
     * @param paramOne
     * @param paramTwo
     * @return
     */
    public static boolean checkParam(Object paramOne,Object paramTwo,Object paramThree){
        if(paramOne==null || paramTwo==null || paramThree == null ){
            return false;
        }
        return true;
    }

    /**
     * 获取参数中的数据
     * @return
     */
    public static FormulaParam getFormulaParam(Map<String, Object> env,AviatorObject first, AviatorObject second, AviatorObject third, AviatorObject fourth, AviatorObject fifth){
        FormulaParam formulaParam = new FormulaParam();
        // 如果first不为空，将first放入实体中
        if(first!=null && env!=null){
            formulaParam.setFirst(first.getValue(env));
        }
        // 如果second不为空，将second放入实体中
        if(second!=null){
            if(second instanceof AviatorJavaType){
                formulaParam.setSecond(((AviatorJavaType) second).getName());
            }else{
                formulaParam.setSecond(String.valueOf(second.getValue(env)));
            }
        }
        // 如果third不为空，将third放入实体中
        if(third!=null){
            if(third instanceof AviatorJavaType){
                formulaParam.setThird(((AviatorJavaType) third).getName());
            }else{
                formulaParam.setThird(String.valueOf(third.getValue(env)));
            }
        }
        // 如果fourth不为空，将fourth放入实体中
        if(fourth!=null){
            if(fourth instanceof AviatorJavaType){
                formulaParam.setFourth(((AviatorJavaType) fourth).getName());
            }else{
                formulaParam.setFourth(String.valueOf(fourth.getValue(env)));
            }
        }
        // 如果fifth不为空，将fifth放入实体中
        if(fifth!=null){
            if(fifth instanceof AviatorJavaType){
                formulaParam.setFifth(((AviatorJavaType) fifth).getName());
            }else{
                formulaParam.setFifth(String.valueOf(fifth.getValue(env)));
            }
        }
        return formulaParam;
    }

}
