package nckd.jxccl.hr.gzbb;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.form.events.AfterDoOperationEventArgs;
import kd.bos.form.events.BeforeDoOperationEventArgs;
import kd.bos.form.operate.FormOperate;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.QueryServiceHelper;

import javax.script.ScriptException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;

/**
 * 央国企人事云》国资报表ext》基础资料》薪酬项目计算配置
 * 检查项目是否合法
 * nckd_jbxcjs
 * duanyuqi
 * */
public class XcxmCheckFormPlugin extends AbstractFormPlugin {
    private static final Pattern FORMULA_PATTERN = Pattern.compile("^[\\u4e00-\\u9fa5\\w]+([-+*/][\\u4e00-\\u9fa5\\w]+)*$");

    public static boolean isValidFormula(String formula) {
        if (formula == null || formula.trim().isEmpty()) {
            return false;
        }
        return FORMULA_PATTERN.matcher(formula).matches();
    }

    @Override
    public void beforeDoOperation(BeforeDoOperationEventArgs args) {
        FormOperate formOperate = (FormOperate) args.getSource();
        if ("save".equals(formOperate.getOperateKey()) || "submit".equals(formOperate.getOperateKey())) {
            // 保存或提交时校验薪酬项目编码是否正确
            Set<String> allXcNumber = findAllNumber();
            String nckd_xcxm = this.getModel().getDataEntity().getString("nckd_xcxm");// 薪酬项目编码
            if (!isNullEmpty(nckd_xcxm)) {
                if (!isValidFormula(nckd_xcxm)) {
                    this.getView().showErrorNotification("请检查基本薪酬的公式");
                    args.setCancel(true);
                    return;
                }
                String checkXcBm = nckd_xcxm.replace("-", ",").replace("+", ",").replace("*", ",").replace("/", ",");
                String[] xcBmStr = checkXcBm.split(",");
                for (String xcBm : xcBmStr) {
                    if(!allXcNumber.contains(xcBm)) {
                        this.getView().showErrorNotification("基本薪酬薪酬项目编码："+xcBm+"不存在,请检查");
                        args.setCancel(true);
                        return;
                    }
                }
            }
            String nckd_dnyfjx = this.getModel().getDataEntity().getString("nckd_dnyfjx");// 当年应发绩效
            if (!isNullEmpty(nckd_dnyfjx)) {
                if (!isValidFormula(nckd_dnyfjx)) {
                    this.getView().showErrorNotification("请检查当年应发绩效的公式");
                    args.setCancel(true);
                    return;
                }
                String checkXcBm = nckd_dnyfjx.replace("-", ",").replace("+", ",").replace("*", ",").replace("/", ",");
                String[] xcBmStr = checkXcBm.split(",");
                for (String xcBm : xcBmStr) {
                    if(!allXcNumber.contains(xcBm)) {
                        this.getView().showErrorNotification("当年应发绩效薪酬项目编码："+xcBm+"不存在,请检查");
                        args.setCancel(true);
                        return;
                    }
                }
            }
            String nckd_yfdnjx = this.getModel().getDataEntity().getString("nckd_yfdnjx");// 预发当年绩效
            if (!isNullEmpty(nckd_yfdnjx)) {
                if (!isValidFormula(nckd_yfdnjx)) {
                    this.getView().showErrorNotification("请检查预发当年绩效的公式");
                    args.setCancel(true);
                    return;
                }
                String checkXcBm = nckd_yfdnjx.replace("-", ",").replace("+", ",").replace("*", ",").replace("/", ",");
                String[] xcBmStr = checkXcBm.split(",");
                for (String xcBm : xcBmStr) {
                    if(!allXcNumber.contains(xcBm)) {
                        this.getView().showErrorNotification("预发当年绩效薪酬项目编码："+xcBm+"不存在,请检查");
                        args.setCancel(true);
                        return;
                    }
                }
            }
            String nckd_bfdnjx = this.getModel().getDataEntity().getString("nckd_bfdnjx");// 补发上年绩效
            if (!isNullEmpty(nckd_bfdnjx)) {
                if (!isValidFormula(nckd_bfdnjx)) {
                    this.getView().showErrorNotification("请检查补发上年绩效的公式");
                    args.setCancel(true);
                    return;
                }
                String checkXcBm = nckd_bfdnjx.replace("-", ",").replace("+", ",").replace("*", ",").replace("/", ",");
                String[] xcBmStr = checkXcBm.split(",");
                for (String xcBm : xcBmStr) {
                    if(!allXcNumber.contains(xcBm)) {
                        this.getView().showErrorNotification("补发上年绩效薪酬项目编码："+xcBm+"不存在,请检查");
                        args.setCancel(true);
                        return;
                    }
                }
            }
            String nckd_ydyzfjx = this.getModel().getDataEntity().getString("nckd_ydyzfjx");// 应递延支付绩效
            if (!isNullEmpty(nckd_ydyzfjx)) {
                if (!isValidFormula(nckd_ydyzfjx)) {
                    this.getView().showErrorNotification("请检查递延支付绩效的公式");
                    args.setCancel(true);
                    return;
                }
                String checkXcBm = nckd_ydyzfjx.replace("-", ",").replace("+", ",").replace("*", ",").replace("/", ",");
                String[] xcBmStr = checkXcBm.split(",");
                for (String xcBm : xcBmStr) {
                    if(!allXcNumber.contains(xcBm)) {
                        this.getView().showErrorNotification("应递延支付绩效薪酬项目编码："+xcBm+"不存在,请检查");
                        args.setCancel(true);
                        return;
                    }
                }
            }
            String nckd_syrrqjl = this.getModel().getDataEntity().getString("nckd_syrrqjl");// 发放上一任期的任期激励
            if (!isNullEmpty(nckd_syrrqjl)) {
                if (!isValidFormula(nckd_syrrqjl)) {
                    this.getView().showErrorNotification("请检查发放上一任期的任期激励的公式");
                    args.setCancel(true);
                    return;
                }
                String checkXcBm = nckd_syrrqjl.replace("-", ",").replace("+", ",").replace("*", ",").replace("/", ",");
                String[] xcBmStr = checkXcBm.split(",");
                for (String xcBm : xcBmStr) {
                    if(!allXcNumber.contains(xcBm)) {
                        this.getView().showErrorNotification("发放上一任期的任期激励薪酬项目编码："+xcBm+"不存在,请检查");
                        args.setCancel(true);
                        return;
                    }
                }
            }
            String nckd_bndrqjl = this.getModel().getDataEntity().getString("nckd_bndrqjl");// 预提本年度任期激励
            if (!isNullEmpty(nckd_bndrqjl)) {
                if (!isValidFormula(nckd_bndrqjl)) {
                    this.getView().showErrorNotification("请检查预提本年度任期激励的公式");
                    args.setCancel(true);
                    return;
                }
                String checkXcBm = nckd_bndrqjl.replace("-", ",").replace("+", ",").replace("*", ",").replace("/", ",");
                String[] xcBmStr = checkXcBm.split(",");
                for (String xcBm : xcBmStr) {
                    if(!allXcNumber.contains(xcBm)) {
                        this.getView().showErrorNotification("预提本年度任期激励薪酬项目编码："+xcBm+"不存在,请检查");
                        args.setCancel(true);
                        return;
                    }
                }
            }
            String nckd_jbt = this.getModel().getDataEntity().getString("nckd_jbt");// 津补贴
            if (!isNullEmpty(nckd_jbt)) {
                if (!isValidFormula(nckd_jbt)) {
                    this.getView().showErrorNotification("请检查津补贴的公式");
                    args.setCancel(true);
                    return;
                }
                String checkXcBm = nckd_jbt.replace("-", ",").replace("+", ",").replace("*", ",").replace("/", ",");
                String[] xcBmStr = checkXcBm.split(",");
                for (String xcBm : xcBmStr) {
                    if(!allXcNumber.contains(xcBm)) {
                        this.getView().showErrorNotification("津补贴薪酬项目编码："+xcBm+"不存在,请检查");
                        args.setCancel(true);
                        return;
                    }
                }
            }
            String nckd_qtycxjl = this.getModel().getDataEntity().getString("nckd_qtycxjl");// 其他一次性专项奖励
            if (!isNullEmpty(nckd_qtycxjl)) {
                if (!isValidFormula(nckd_qtycxjl)) {
                    this.getView().showErrorNotification("请检查其他一次性专项奖励的公式");
                    args.setCancel(true);
                    return;
                }
                String checkXcBm = nckd_qtycxjl.replace("-", ",").replace("+", ",").replace("*", ",").replace("/", ",");
                String[] xcBmStr = checkXcBm.split(",");
                for (String xcBm : xcBmStr) {
                    if(!allXcNumber.contains(xcBm)) {
                        this.getView().showErrorNotification("其他一次性专项奖励薪酬项目编码："+xcBm+"不存在,请检查");
                        args.setCancel(true);
                        return;
                    }
                }
            }
        }
        super.beforeDoOperation(args);
    }

    @Override
    public void afterDoOperation(AfterDoOperationEventArgs afterDoOperationEventArgs) {
        super.afterDoOperation(afterDoOperationEventArgs);
    }

    /**空判断*/
    public static boolean isNullEmpty(Object obj) {
        return obj == null || (obj instanceof String && ((String) obj).isEmpty());
    }

    /**获取所有薪酬项目编码*/
    public Set<String> findAllNumber() {
        List<QFilter> qfilter = new ArrayList<QFilter>();
        qfilter.add(new QFilter("enable", "=", 1));
        QFilter[] filtersQuery = (QFilter[]) qfilter.toArray(new QFilter[qfilter.size()]);
        DynamicObjectCollection list =  QueryServiceHelper.query("hsbs_salaryitem", "id,name,number",  filtersQuery);
        Set<String> numberSet = list.stream().map(item -> item.getString("number")).collect(java.util.stream.Collectors.toSet());
        return numberSet;
    }

}
