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

import com.yem.wm.utils.DynamicObjectUtil;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.ExtendedDataEntity;
import kd.bos.entity.validate.AbstractValidator;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ljw
 * @date 2024/4/25 20:46
 * @description MechanicalValidator
 * 删除效验
 */
public class MechanicalValidator extends AbstractValidator {
    @Override
    public void validate() {
        ExtendedDataEntity[] datas = this.getDataEntities();
        for (ExtendedDataEntity temp : datas) {
            DynamicObject bill = temp.getDataEntity();
            long id = bill.getLong("id");
            String selectfield = DynamicObjectUtil.getSelectfields("yem_bd_mechanical", false);
            selectfield = DynamicObjectUtil.getEntrySelectfields(selectfield, "yem_bd_mechanical", "yem_bd_mechanical_a", false);
            selectfield = DynamicObjectUtil.getEntrySelectfields(selectfield, "yem_bd_mechanical", "yem_bd_mechanical_r", false);
            selectfield = DynamicObjectUtil.getEntrySelectfields(selectfield, "yem_bd_mechanical", "yem_bd_mechanical_c", false);
            DynamicObject dynamicObject = BusinessDataServiceHelper.loadSingle("yem_bd_mechanical", selectfield, new QFilter[]{new QFilter("id", QCP.equals, id)});
            if (dynamicObject != null) {
                DynamicObject value = dynamicObject.getDynamicObject("yem_bd_products");
                if (value != null) {
                    long aLong = value.getLong("id");
                    String select = DynamicObjectUtil.getSelectfields("yem_pricelibrary", false);
                    DynamicObject yem_pricelibrary = BusinessDataServiceHelper.loadSingle("yem_pricelibrary", select, new QFilter[]{new QFilter("yem_bd_products.id", QCP.equals, aLong)});
                    if (yem_pricelibrary != null) {
                        this.addErrorMessage(temp, "当前单已被引用，禁止删除！！！");
                    } else {
                        String list = "";
                        DynamicObjectCollection yemBdMechanicalA = dynamicObject.getDynamicObjectCollection("yem_bd_mechanical_a");
                        DynamicObjectCollection yemBdMechanicalR = dynamicObject.getDynamicObjectCollection("yem_bd_mechanical_r");
                        DynamicObjectCollection yemBdMechanicalC = dynamicObject.getDynamicObjectCollection("yem_bd_mechanical_c");
                        list = list + extracted("可选装分录中", yemBdMechanicalC, "yem_pricelibrary", "yem_optional", "yem_optional.yem_entryid_o");
                        list = list + extracted("可选装分录中", yemBdMechanicalC, "yem_es_salesorder", "yem_es_materialinfo", "yem_es_materialinfo.yem_multiconfig.yem_configentryid");
                        list = list + extracted("可选装分录中", yemBdMechanicalC, "yem_im_clientdemand", "yem_im_detailedinfor", "yem_im_detailedinfor.yem_multiconfig.yem_configentryid");
                        list = list + extracted("可替换分录中", yemBdMechanicalR, "yem_pricelibrary", "yem_replaceable", "yem_replaceable.yem_entryid_re");
                        list = list + extracted("可替换分录中", yemBdMechanicalR, "yem_es_salesorder", "yem_es_materialinfo", "yem_es_materialinfo.yem_multiconfig.yem_configentryid");
                        list = list + extracted("可替换分录中", yemBdMechanicalR, "yem_im_clientdemand", "yem_im_detailedinfor", "yem_im_detailedinfor.yem_multiconfig.yem_configentryid");
                        list = list + extracted("可加装分录中", yemBdMechanicalA, "yem_pricelibrary", "yem_retrofitting", "yem_retrofitting.yem_entryid_rt");
                        list = list + extracted("可加装分录中", yemBdMechanicalA, "yem_es_salesorder", "yem_es_materialinfo", "yem_es_materialinfo.yem_multiconfig.yem_configentryid");
                        list = list + extracted("可加装分录中", yemBdMechanicalA, "yem_im_clientdemand", "yem_im_detailedinfor", "yem_im_detailedinfor.yem_multiconfig.yem_configentryid");
                        if (!"".equals(list)) {
                            this.addMessage(temp, list);
                        }
                    }
                }
            }
        }
    }

    private String extracted(String number, DynamicObjectCollection yemBdMechanicalC, String materialinfo, String string, String yemEntryid) {
        String text = "";
        String selectfields = "";
        for (DynamicObject dynamicObject : yemBdMechanicalC) {
            long aLong = dynamicObject.getLong("id");
            String selectfield = DynamicObjectUtil.getSelectfields(materialinfo, false);
            String seq = dynamicObject.getString("seq");
            if ("yem_pricelibrary".equals(materialinfo)) {
                selectfields = DynamicObjectUtil.getEntrySelectfields(selectfield, materialinfo, string, false);
            } else {
                selectfields = DynamicObjectUtil.getEntrySelectfields(selectfield, materialinfo, string, false);
                selectfields = DynamicObjectUtil.getEntrySelectfields(selectfields, materialinfo, string + ".yem_multiconfig", false);
            }
            DynamicObject[] load = BusinessDataServiceHelper.load(materialinfo, selectfields, new QFilter[]{new QFilter(yemEntryid, QCP.equals, aLong)});
            if (load.length > 0) {
                for (DynamicObject object : load) {
                    String billno = object.getString("billno");
                    if (!"yem_pricelibrary".equals(materialinfo)) {
                        DynamicObject yemBilltype = object.getDynamicObject("yem_billtype");
                        if (yemBilltype != null) {
                            String name = yemBilltype.getString("name");
                            text = text + number + "第" + seq + "行" + "被" + name + "中" + billno + " 引用，请修改！！\n\r";
                        }
                    } else {
                        text = text + number + "第" + seq + "行" + "被" + "整机价格库" + "中" + billno + " 引用，请修改！！\n\r";
                    }
                }
            }
        }
        return text;
    }
}
