package com.ag.account.feeUtil;

import cn.hutool.core.bean.BeanUtil;
import com.ag.entity.account.FeeDetail;
import com.ag.entity.account.vo.BizVO;
import com.ag.entity.fee.ContractItem;
import com.ag.entity.fee.face.SourceProperty;
import com.ag.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ql.util.express.DefaultContext;
import com.ql.util.express.ExpressRunner;

import java.math.BigDecimal;

/**
 * Created by yangxh on 2022/1/5.
 */
public class FormulaUtil {

    public static void execute(SourceProperty source, ContractItem citem, FeeDetail fee){
        String formula = citem.getFormula();
        if (StrUtil.isEmpty(formula) || StrUtil.isEmpty(formula.trim())){
            return;
        }
        ExpressRunner runner = new ExpressRunner();
        DefaultContext<String, Object> context = new DefaultContext<>();
        context.putAll(source.getProperty());
        context.put("RATE", fee.getActualRate());
        Object res = null;
        try {
            res = runner.execute(formula, context, null, false, false);
        } catch (Exception e) {
            throw new RuntimeException("执行自定义表达式异常！"+citem.getFormula());
        }
        if(res != null) {
            fee.setActualRate(Double.valueOf(res.toString()));
        }
    }
}
