package com.yem.wm.bd.mechanical.from;

import com.yem.wm.utils.DynamicObjectUtil;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.datamodel.events.BeforeDeleteRowEventArgs;
import kd.bos.entity.property.EntryProp;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;

/**
 * @author ljw
 * @date 2024/4/25 19:08
 * @description MechanicalFormPlugin
 * 删除行效验
 */
public class MechanicalFormPlugin extends AbstractFormPlugin {
    @Override
    public void beforeDeleteRow(BeforeDeleteRowEventArgs e) {
        super.beforeDeleteRow(e);
        EntryProp entryProp = e.getEntryProp();
        String keyFieldId = entryProp.getName();
        int[] rowIndexs = e.getRowIndexs();
        String bool = "";
        Boolean value = (Boolean) this.getModel().getValue("yem_checkboxfield");
        if (!value) {
            return;
        }
//        bd_mechanical_c(keyFieldId, e);
        if ("yem_bd_mechanical_c".equals(keyFieldId)) {
            for (int dynamicObject : rowIndexs) {
                bool = bool + extracted("可选装分录中", dynamicObject, "yem_bd_mechanical_c.id", "yem_pricelibrary", "yem_optional", "yem_optional.yem_entryid_o", e);
                bool = bool + extracted("可选装分录中", dynamicObject, "yem_bd_mechanical_c.id", "yem_es_salesorder", "yem_es_materialinfo", "yem_es_materialinfo.yem_multiconfig.yem_configentryid", e);
                bool = bool + extracted("可选装分录中", dynamicObject, "yem_bd_mechanical_c.id", "yem_im_clientdemand", "yem_im_detailedinfor", "yem_im_detailedinfor.yem_multiconfig.yem_configentryid", e);
            }
            if (!"".equals(bool)) {
                this.getView().showTipNotification(bool);
                e.setCancel(true);
            }
        }
        if ("yem_bd_mechanical_r".equals(keyFieldId)) {
            for (int dynamicObject : rowIndexs) {
                bool = bool + extracted("可替换分录中", dynamicObject, "yem_bd_mechanical_r.id", "yem_pricelibrary", "yem_replaceable", "yem_replaceable.yem_entryid_re", e);
                bool = bool + extracted("可替换分录中", dynamicObject, "yem_bd_mechanical_r.id", "yem_es_salesorder", "yem_es_materialinfo", "yem_es_materialinfo.yem_multiconfig.yem_configentryid", e);
                bool = bool + extracted("可替换分录中", dynamicObject, "yem_bd_mechanical_r.id", "yem_im_clientdemand", "yem_im_detailedinfor", "yem_im_detailedinfor.yem_multiconfig.yem_configentryid", e);
            }
            if (!"".equals(bool)) {
                this.getView().showTipNotification(bool);
                e.setCancel(true);
            }
        }
        if ("yem_bd_mechanical_a".equals(keyFieldId)) {
            for (int dynamicObject : rowIndexs) {
                bool = bool + extracted("可加装分录中", dynamicObject, "yem_bd_mechanical_a.id", "yem_pricelibrary", "yem_retrofitting", "yem_retrofitting.yem_entryid_rt", e);
                bool = bool + extracted("可加装分录中", dynamicObject, "yem_bd_mechanical_a.id", "yem_es_salesorder", "yem_es_materialinfo", "yem_es_materialinfo.yem_multiconfig.yem_configentryid", e);
                bool = bool + extracted("可加装分录中", dynamicObject, "yem_bd_mechanical_a.id", "yem_im_clientdemand", "yem_im_detailedinfor", "yem_im_detailedinfor.yem_multiconfig.yem_configentryid", e);
            }
            if (!"".equals(bool)) {
                this.getView().showTipNotification(bool);
                e.setCancel(true);
            }
        }
    }


    private String extracted(String number, int rowIndexs, String s, String string, String tailedinfor, String property, BeforeDeleteRowEventArgs e) {
        String text = "";
        String selectfield = DynamicObjectUtil.getSelectfields(string, false);
        String selectfields = "";
        if (!"yem_pricelibrary".equals(string)) {
            selectfields = DynamicObjectUtil.getEntrySelectfields(selectfield, string, tailedinfor, false);
            selectfields = DynamicObjectUtil.getEntrySelectfields(selectfields, string, tailedinfor + ".yem_multiconfig", false);
        } else {
            selectfields = DynamicObjectUtil.getEntrySelectfields(selectfield, string, tailedinfor, false);
        }
        long aLong = (long) this.getModel().getValue(s, rowIndexs);
        Object value1 = this.getModel().getValue("yem_bd_mechanical_c.seq", rowIndexs);
        int value = 0;
        if (value1 instanceof Integer){
            value = (int) value1;
        }
        DynamicObject[] load = BusinessDataServiceHelper.load(string, selectfields, new QFilter[]{new QFilter(property, QCP.equals, aLong)});
        if (load.length > 0) {
            for (DynamicObject object : load) {
                String billno = object.getString("billno");
                if (!"yem_pricelibrary".equals(string)) {
                    DynamicObject yemBilltype = object.getDynamicObject("yem_billtype");
                    if (yemBilltype != null) {
                        String name = yemBilltype.getString("name");
                        text = text + number + "第" + value + "行" + "被" + name + "中" + billno + " 引用，请修改！！\n\r";
                    }
                } else {
                    text = text + number + "第" + value + "行" + "被" + "整机价格库" + "中" + billno + " 引用，请修改！！\n\r";
                }
            }
        }
        return text;
    }

//    @Override
//    public void beforeDoOperation(BeforeDoOperationEventArgs args) {
//        super.beforeDoOperation(args);
//        FormOperate operate = (FormOperate) args.getSource();
//        String key = operate.getOperateKey();
//        if (key.equals("delete")) {//删除
//        }
//    }

    private void bd_mechanical_c(String keyFieldId, BeforeDeleteRowEventArgs e) {
        int[] rowIndexs = e.getRowIndexs();
        String bool = "";
        Boolean value = (Boolean) this.getModel().getValue("yem_checkboxfield");
        if (!value) {
            return;
        }
        if ("yem_bd_mechanical_c".equals(keyFieldId)) {
            for (int dynamicObject : rowIndexs) {
                bool = bool + extracted("可选装分录中", dynamicObject, "yem_bd_mechanical_c.id", "yem_pricelibrary", "yem_optional", "yem_optional.yem_entryid_o", e);
                String text = "";
                String selectfield = DynamicObjectUtil.getSelectfields("yem_pricelibrary", false);
                String selectfields = DynamicObjectUtil.getEntrySelectfields(selectfield, "yem_pricelibrary", "yem_optional", false);
                long aLong = (long) this.getModel().getValue("yem_bd_mechanical_c.id", dynamicObject);
                DynamicObject[] load = BusinessDataServiceHelper.load("yem_pricelibrary", selectfields, new QFilter[]{new QFilter("yem_optional.yem_entryid_o", QCP.equals, aLong)});
                if (load.length > 0) {
                    for (DynamicObject object : load) {
                        String billno = object.getString("billno");
                        DynamicObject yemBilltype = object.getDynamicObject("yem_billtype");
                        if (yemBilltype != null) {
                            String name = yemBilltype.getString("name");
                            text = text + "可选装分录中,第" + dynamicObject + 1 + "行" + "被" + name + "中" + billno + " 引用，请修改！！";
                        }
                    }
                }
                bool = bool + extracted("可选装分录中", dynamicObject, "yem_bd_mechanical_c.id", "yem_es_salesorder", "yem_es_materialinfo", "yem_es_materialinfo.yem_multiconfig.yem_configentryid", e);
                bool = bool + extracted("可选装分录中", dynamicObject, "yem_bd_mechanical_c.id", "yem_im_clientdemand", "yem_im_detailedinfor", "yem_im_detailedinfor.yem_multiconfig.yem_configentryid", e);

            }
            if (!"".equals(bool)) {
                this.getView().showTipNotification(bool);
                e.setCancel(true);
            }
        }
        if ("yem_bd_mechanical_r".equals(keyFieldId)) {
            for (int dynamicObject : rowIndexs) {
                bool = bool + extracted("可替换分录中", dynamicObject, "yem_bd_mechanical_r.id", "yem_pricelibrary", "yem_replaceable", "yem_replaceable.yem_entryid_re", e);
                bool = bool + extracted("可替换分录中", dynamicObject, "yem_bd_mechanical_r.id", "yem_es_salesorder", "yem_es_materialinfo", "yem_es_materialinfo.yem_multiconfig.yem_configentryid", e);
                bool = bool + extracted("可替换分录中", dynamicObject, "yem_bd_mechanical_r.id", "yem_im_clientdemand", "yem_im_detailedinfor", "yem_im_detailedinfor.yem_multiconfig.yem_configentryid", e);
            }
            if (!"".equals(bool)) {
                this.getView().showTipNotification(bool);
                e.setCancel(true);
            }
        }
        if ("yem_bd_mechanical_a".equals(keyFieldId)) {
            for (int dynamicObject : rowIndexs) {
                bool = bool + extracted("可加装分录中", dynamicObject, "yem_bd_mechanical_a.id", "yem_pricelibrary", "yem_retrofitting", "yem_retrofitting.yem_entryid_rt", e);
                bool = bool + extracted("可加装分录中", dynamicObject, "yem_bd_mechanical_a.id", "yem_es_salesorder", "yem_es_materialinfo", "yem_es_materialinfo.yem_multiconfig.yem_configentryid", e);
                bool = bool + extracted("可加装分录中", dynamicObject, "yem_bd_mechanical_a.id", "yem_im_clientdemand", "yem_im_detailedinfor", "yem_im_detailedinfor.yem_multiconfig.yem_configentryid", e);
            }
            if (!"".equals(bool)) {
                this.getView().showTipNotification(bool);
                e.setCancel(true);
            }
        }
    }
}
