package com.jwipc.web.config;

import cn.hutool.core.util.ObjectUtil;
import com.jwipc.util.FormatterPriceUtil;
import freemarker.template.TemplateMethodModel;
import freemarker.template.TemplateModelException;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author shenyang.ou
 * @date 2024/4/19 11:35
 * @desc freeMark价格处理函数
 */
public class FormatterFormPriceMethod implements TemplateMethodModel{
    private final Integer precision;
    private final Boolean repair;
    private final Boolean thousand;
    private final Integer cutType;

    /**
     * 带参数的构造函数
     */
    public FormatterFormPriceMethod(Integer precision, Boolean repair, Boolean thousand, Integer cutType)
    {
        this.precision=precision;
        this.repair=repair;
        this.thousand=thousand;
        this.cutType=cutType;
    }

    @Override
    public Object exec(List list) throws TemplateModelException {
        if(list.size()!=1)   //限定方法中必须且只能传递一个参数
        {
            return "";
        }
        Object a = list.get(0);
        //返回response.encodeURL执行结果
        return FormatterPriceUtil.handle(ObjectUtil.isEmpty(list.get(0))?null:new BigDecimal(String.valueOf(list.get(0))),precision,repair,thousand,cutType);
    }
}
