package com.yem.wm.im.orderproreqbill.from;

import com.yem.wm.es.Util.ViewInterfaceDocking;
import com.yem.wm.utils.FunctionalCommon;
import com.yem.wm.utils.RequestCmmp;
import com.yem.wm.utils.StringUtils;
import com.yem.wm.utils.YEM;
import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.entity.datamodel.events.ChangeData;
import kd.bos.entity.datamodel.events.PropertyChangedArgs;
import kd.bos.form.FormShowParameter;
import kd.bos.form.IFormView;
import kd.bos.form.ShowType;
import kd.bos.form.control.Control;
import kd.bos.form.events.BeforeDoOperationEventArgs;
import kd.bos.form.field.BasedataEdit;
import kd.bos.form.field.FieldEdit;
import kd.bos.form.field.TextEdit;
import kd.bos.form.field.events.BeforeF7SelectEvent;
import kd.bos.form.field.events.BeforeF7SelectListener;
import kd.bos.form.operate.FormOperate;
import kd.bos.list.ListShowParameter;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.QueryServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;

import java.math.BigDecimal;
import java.util.*;

/**
 * @author power
 * @ClassName 订单产品需求单:yem_orderproreqbill
 * @description: 订单产品需求单 表单插件
 * @date 2023/8/21
 */
public class OrderproreqbillEdit extends AbstractBillPlugIn implements BeforeF7SelectListener {

    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        BasedataEdit warehouse = this.getControl("yem_warehouse");
        warehouse.addBeforeF7SelectListener(this);
        BasedataEdit yem_outwhcode = this.getControl("yem_outwhcode");
        yem_outwhcode.addBeforeF7SelectListener(this);
        BasedataEdit wareArea = this.getControl("yem_warearea");
        wareArea.addBeforeF7SelectListener(this);
        BasedataEdit yem_outareacode = this.getControl("yem_outareacode");
        yem_outareacode.addBeforeF7SelectListener(this);
        BasedataEdit whLocation = this.getControl("yem_whlocation");
        whLocation.addBeforeF7SelectListener(this);
        BasedataEdit yem_outpositioncode = this.getControl("yem_outpositioncode");
        yem_outpositioncode.addBeforeF7SelectListener(this);
        BasedataEdit materialProperty = this.getControl("yem_materialproperty");
        materialProperty.addBeforeF7SelectListener(this);
        BasedataEdit sup = this.getControl("yem_sup");
        sup.addBeforeF7SelectListener(this);
        BasedataEdit whMan = this.getControl("yem_whman");
        whMan.addBeforeF7SelectListener(this);
        BasedataEdit yem_outstockercode = this.getControl("yem_outstockercode");
        yem_outstockercode.addBeforeF7SelectListener(this);
        BasedataEdit qualitierr = this.getControl("yem_qualitierr");
        qualitierr.addBeforeF7SelectListener(this);
        BasedataEdit buyer = this.getControl("yem_buyerr");
        buyer.addBeforeF7SelectListener(this);
        TextEdit manufacturer = this.getControl("yem_manufacturer");
        manufacturer.addClickListener(this);
        BasedataEdit gcxm = this.getControl("yem_gcxm");
        gcxm.addBeforeF7SelectListener(this);
        BasedataEdit yfxm = this.getControl("yem_yfxm");
        yfxm.addBeforeF7SelectListener(this);
        BasedataEdit fyxm = this.getControl("yem_fyxm");
        fyxm.addBeforeF7SelectListener(this);
    }

    @Override
    public void propertyChanged(PropertyChangedArgs e) {
        super.propertyChanged(e);
        IDataModel model = this.getModel();
        String name = e.getProperty().getName();
        ChangeData cd = e.getChangeSet()[0];
        ChangeData[] changeData = e.getChangeSet();
        int row = changeData[0].getRowIndex();
        switch (name) {
            case "yem_realpurmateriel":
                DynamicObject realPurMateriel = (DynamicObject) model.getValue("yem_realpurmateriel", row);
                if (YEM.isNotEmpty(realPurMateriel)) {
                    doClearEntryData(row);

                    setWarehouseAreaInfo(row, "A", "yem_warehouse", "yem_warearea", "yem_whlocation", "yem_whman");
                    setMaterialPropertyOption(row);
                    setMaterialInfo(row);
                    setQualityUser(row);
                    setBuyer(row);
                    bringOutSupplier(row);
//                    bringDefaultWarehouse(row);
                    bringOutManufacturer(row);
                } else {
                    doClearEntryData(row);
                }
                break;
            case "yem_warehouse":
                setWarehouseAreaInfo(row, "B", "yem_warehouse", "yem_warearea", "yem_whlocation", "yem_whman");
                DynamicObject billType = (DynamicObject) model.getValue("yem_billtype");
                String billTypeNumber = YEM.isNotEmpty(billType) ? billType.getString("number") : "";
                if ("yem_orderproreqbill_DB".equals(billTypeNumber))
                    model.setValue("yem_retrievecode", e.getChangeSet()[0].getNewValue());
                break;
            case "yem_outwhcode":
                setWarehouseAreaInfo(row, "B", "yem_outwhcode", "yem_outareacode", "yem_outpositioncode", "yem_outstockercode");
                DynamicObject billType1 = (DynamicObject) model.getValue("yem_billtype");
                String billTypeNumber1 = YEM.isNotEmpty(billType1) ? billType1.getString("number") : "";
                if ("yem_orderproreqbill_DB".equals(billTypeNumber1))
                    model.setValue("yem_transfercode", e.getChangeSet()[0].getNewValue());
                break;
            case "yem_warearea":
                this.hint("yem_warehouse", "yem_warearea", "仓库", row);
                break;
            case "yem_whlocation":
                this.hint("yem_warearea", "yem_whlocation", "库区", row);
                break;
            case "yem_whman":
                this.hint("yem_warehouse", "yem_whman", "仓库", row);
                break;
            case "yem_outstockercode":
                this.hint("yem_outpositioncode", "yem_outstockercode", "库位", row);
                break;

            case "yem_outareacode":
                this.hint("yem_outwhcode", "yem_outareacode", "仓库", row);
                break;
            case "yem_outpositioncode":
                this.hint("yem_outareacode", "yem_outpositioncode", "库区", row);
                break;
            case "yem_isvmi":
                String yemIsvmi = (String) model.getValue("yem_isvmi");
                if ("Z".equals(yemIsvmi)) {
                    this.getView().setEnable(false, "yem_whlocation");
                }
                setFieldMustInput();
                break;
            case "yem_manufacturer":
//                setyem_sup(row);
                break;
            default:
        }

    }

    private void doClearEntryData(int row) {
        IDataModel model = this.getModel();
        model.setValue("yem_manufacturer", null, row);
        model.setValue("yem_manufacturername", null, row);
        model.setValue("yem_warehouse", null, row);
        model.setValue("yem_warearea", null, row);
        model.setValue("yem_whlocation", null, row);
        model.setValue("yem_buyerr", null, row);
        model.setValue("yem_qualitierr", null, row);
        model.setValue("yem_sup", null, row);
        model.setValue("yem_materialproperty", null, row);
        model.setValue("yem_whman", null, row);
        model.setValue("yem_outwhcode", null, row);
        model.setValue("yem_outstockercode", null, row);
        model.setValue("yem_outpositioncode", null, row);
        model.setValue("yem_outareacode", null, row);
    }

    /**
     * 选择物料携带第一个制造商
     *
     * @param currentRowIndex
     */
    private void bringOutManufacturer(int currentRowIndex) {
        IDataModel model = this.getModel();

        List<Map> results = getResult(model, currentRowIndex, ViewInterfaceDocking.MATERIAL_INFO, "A", null, null, null);
        Map<String, String> suppliers = new LinkedHashMap<>();
        if (YEM.isNotEmpty(results)) {
            for (Map result : results) {
                String supplierNumber = (String) result.get("supplier_code");
                String suppliername = (String) result.get("supplier_name");
                suppliers.put(supplierNumber, suppliername);
            }
        }

        if (!suppliers.isEmpty()) {
            Set<String> supplierNumbers = suppliers.keySet();
            String supplierNumber = supplierNumbers.iterator().next();
            model.setValue("yem_manufacturer", supplierNumber, currentRowIndex);
            model.setValue("yem_manufacturername", suppliers.get(supplierNumber), currentRowIndex);
        }
    }

    /**
     * 如果仓库唯一，携带仓库
     *
     * @param row
     */
    private void bringDefaultWarehouse(int row) {
        String yemIsvmi = (String) this.getModel().getValue("yem_isvmi");
        String yemBelongTo = getYemBelongTo();
        if (yemBelongTo == null) return;
        QFilter[] qFilter = null;
        IDataModel model = this.getModel();
        if ("V".equals(yemIsvmi) || "C".equals(yemIsvmi) || StringUtils.isBlank(yemIsvmi)) {
            List<Map> results = getResult(model, row, ViewInterfaceDocking.MATERIAL_WAREHOUSE, "A", null, null, null);
            if (YEM.isNotEmpty(results) && !"Z".equals(model.getValue("yem_isvmi"))) {
                HashSet<String> warehouseNumbers = new HashSet<>();
                for (Map result : results) {
                    String warehouseNumber = (String) result.get("wh_code");
                    warehouseNumbers.add(warehouseNumber);
                }
                qFilter = new QFilter[]{new QFilter("number", QCP.in, warehouseNumbers)};
            } else if ("Z".equals(model.getValue("yem_isvmi"))) {
                qFilter = new QFilter[]{new QFilter("yem_belong", QCP.equals, "A")};
            }

            if (qFilter != null) {
                DynamicObject[] load = BusinessDataServiceHelper.load("yem_warehouse", "id", qFilter);
                if (load.length == 1) {
                    model.setValue("yem_warehouse", load[0]);
                    return;
                }
            }
        }
        if (!StringUtils.isBlank(yemIsvmi)) {
            QFilter[] qFilters = new QFilter[]{new QFilter("yem_sort", QCP.equals, yemIsvmi)};
            DynamicObject[] load = BusinessDataServiceHelper.load("yem_warehouse", "id", qFilters);
            if (load.length == 1) {
                model.setValue("yem_warehouse", load[0]);
            }
        } else {
            QFilter[] qFilters = new QFilter[]{new QFilter("yem_belong", QCP.equals, yemBelongTo)};
            DynamicObject[] load = BusinessDataServiceHelper.load("yem_warehouse", "id", qFilters);
            if (load.length == 1) {
                model.setValue("yem_warehouse", load[0]);
            }
        }
    }

    /**
     * 默认携带第一个调出供应商
     *
     * @param row
     */
    private void bringOutSupplier(int row) {
        IDataModel model = this.getModel();
        List<Map> results = getResult(model, row, ViewInterfaceDocking.MATERIAL_INFO, "D", null, null, null);
        HashSet<String> sups = new LinkedHashSet<>();
        if (YEM.isNotEmpty(results)) {
            for (Map result : results) {
                String supNumber = (String) result.get("sup_code");
                sups.add(supNumber);
            }
            QFilter qFilter = new QFilter("number", QCP.in, sups);
            DynamicObject[] load = BusinessDataServiceHelper.load("bd_supplier", "id", qFilter.toArray());
            String next = sups.iterator().next();

            for (DynamicObject dynamicObject : load) {
                if (dynamicObject.getString("number").equals(next)) {

                    model.setValue("yem_sup", dynamicObject.getPkValue());
                }
            }
        }
    }

    /**
     * 携带供应商编码名称值制造商
     *
     * @param row
     */
    private void setyem_sup(int row) {
        String yem_manufacturer = (String) getModel().getValue("yem_manufacturer", row);
        if (yem_manufacturer != null) {
            DynamicObject sup = BusinessDataServiceHelper.loadSingle("bd_supplier", "id", new QFilter[]{new QFilter("number", QCP.equals, yem_manufacturer)});
            getModel().setValue("yem_sup", sup, row);
        }
    }

    @Override
    public void afterCreateNewData(EventObject e) {
        super.afterCreateNewData(e);
    }

    @Override
    public void afterBindData(EventObject e) {
        super.afterBindData(e);
        setFieldMustInput();
    }

    /**
     * 根据条件设置必录
     */
    private void setFieldMustInput() {
        IDataModel model = this.getModel();
        DynamicObject dataEntity = model.getDataEntity(true);
        String isvmi = dataEntity.getString("yem_isvmi");
        String billtype = dataEntity.getString("yem_billtype.number");
        // 订单产品需求单，单据类型为配件调拨计划且仓库类型为正常仓库，字段必填
        FieldEdit applyuser = this.getControl("yem_applyuser");
        FieldEdit depcode = this.getControl("yem_depcode");
        FieldEdit retrievecode = this.getControl("yem_retrievecode");
        FieldEdit retrievename = this.getControl("yem_retrievecodename");
        FieldEdit transfercode = this.getControl("yem_transfercode");
        FieldEdit transfername = this.getControl("yem_transfercodename");
        FieldEdit materiel = this.getControl("yem_materiel");
        FieldEdit materialname = this.getControl("yem_materialname");
        FieldEdit qty = this.getControl("yem_qty");
        FieldEdit warehouse = this.getControl("yem_warehouse");
        FieldEdit basedatapropfield2 = this.getControl("yem_basedatapropfield2");
        FieldEdit warearea = this.getControl("yem_warearea");
        FieldEdit basedatapropfield3 = this.getControl("yem_basedatapropfield3");
        FieldEdit whlocation = this.getControl("yem_whlocation");
        FieldEdit basedatapropfield4 = this.getControl("yem_basedatapropfield4");
        FieldEdit whman = this.getControl("yem_whman");
        FieldEdit basedatapropfield = this.getControl("yem_basedatapropfield");
        FieldEdit receiveruser = this.getControl("yem_receiveruser");
        FieldEdit gcxm = this.getControl("yem_gcxm");
        FieldEdit fyxm = this.getControl("yem_fyxm");
        FieldEdit outwhcode = this.getControl("yem_outwhcode");
        FieldEdit outwhname = this.getControl("yem_outwhname");
        FieldEdit outstockercode = this.getControl("yem_outstockercode");
        FieldEdit outstockername = this.getControl("yem_outstockername");
        FieldEdit outpositioncode = this.getControl("yem_outpositioncode");
        FieldEdit outpositionname = this.getControl("yem_outpositionname");
        FieldEdit outareacode = this.getControl("yem_outareacode");
        FieldEdit outareaname = this.getControl("yem_outareaname");
        boolean isMustInput = "yem_orderproreqbill_DB".equals(billtype) && "C".equals(isvmi);
        boolean isMustInputType2 = "yem_orderproreqbill_DB".equals(billtype) && "V".equals(isvmi);

        // clean
        applyuser.setMustInput(false);
        depcode.setMustInput(false);
        retrievecode.setMustInput(false);
        retrievename.setMustInput(false);
        transfercode.setMustInput(false);
        transfername.setMustInput(false);
        materiel.setMustInput(false);
        materialname.setMustInput(false);
        qty.setMustInput(false);
        warehouse.setMustInput(false);
        basedatapropfield2.setMustInput(false);
        warearea.setMustInput(false);
        basedatapropfield3.setMustInput(false);
        whlocation.setMustInput(false);
        basedatapropfield4.setMustInput(false);
        whman.setMustInput(false);
        basedatapropfield.setMustInput(false);
        receiveruser.setMustInput(false);
        gcxm.setMustInput(false);
        fyxm.setMustInput(false);
        outwhcode.setMustInput(false);
        outwhname.setMustInput(false);
        outstockercode.setMustInput(false);
        outstockername.setMustInput(false);
        outpositioncode.setMustInput(false);
        outpositionname.setMustInput(false);
        outareacode.setMustInput(false);
        outareaname.setMustInput(false);

        if ("C".equals(isvmi)) {
            applyuser.setMustInput(isMustInput);
            depcode.setMustInput(isMustInput);
            retrievecode.setMustInput(isMustInput);
            retrievename.setMustInput(isMustInput);
            transfercode.setMustInput(isMustInput);
            transfername.setMustInput(isMustInput);
            materiel.setMustInput(isMustInput);
            materialname.setMustInput(isMustInput);
            qty.setMustInput(isMustInput);
            warehouse.setMustInput(isMustInput);
            basedatapropfield2.setMustInput(isMustInput);
            warearea.setMustInput(isMustInput);
            basedatapropfield3.setMustInput(isMustInput);
            whlocation.setMustInput(isMustInput);
            basedatapropfield4.setMustInput(isMustInput);
            whman.setMustInput(isMustInput);
            basedatapropfield.setMustInput(isMustInput);

            outwhcode.setMustInput(isMustInput);
            outwhname.setMustInput(isMustInput);
            outstockercode.setMustInput(isMustInput);
            outstockername.setMustInput(isMustInput);
            outpositioncode.setMustInput(isMustInput);
            outpositionname.setMustInput(isMustInput);
            outareacode.setMustInput(isMustInput);
            outareaname.setMustInput(isMustInput);
        } else if ("V".equals(isvmi)) {
            depcode.setMustInput(isMustInputType2);
            receiveruser.setMustInput(isMustInputType2);
            gcxm.setMustInput(isMustInputType2);
            fyxm.setMustInput(isMustInputType2);
            materiel.setMustInput(isMustInputType2);
            whman.setMustInput(isMustInputType2);
            basedatapropfield.setMustInput(isMustInputType2);
        }
    }

    @Override
    public void beforeF7Select(BeforeF7SelectEvent e) {
        String name = e.getProperty().getName();
//        String yemIsvmi = (String) this.getModel().getValue("yem_isvmi");
        String yemBelongTo = getYemBelongTo();
        if (yemBelongTo == null) return;
        int row = e.getRow();
        switch (name) {
            case "yem_warehouse":
                selectWarehouse(e);
//                if ("V".equals(yemIsvmi) || "C".equals(yemIsvmi) || StringUtils.isBlank(yemIsvmi))
//                if (!StringUtils.isBlank(yemIsvmi)) e.addCustomQFilter(new QFilter("yem_sort", QCP.equals, yemIsvmi));
                //根据产品类型过滤仓库所属
                e.addCustomQFilter(new QFilter("yem_belong", QCP.equals, yemBelongTo));
                break;
            case "yem_outwhcode":
                selectWarehouse(e);
//                if ("V".equals(yemIsvmi) || "C".equals(yemIsvmi) || StringUtils.isBlank(yemIsvmi)) ;
//                if(!StringUtils.isBlank(yemIsvmi))e.addCustomQFilter(new QFilter("yem_sort",QCP.equals,yemIsvmi));
                //根据产品类型过滤仓库所属
                e.addCustomQFilter(new QFilter("yem_belong", QCP.equals, yemBelongTo));
                break;
            case "yem_warearea":
//                if ("V".equals(yemIsvmi) || "C".equals(yemIsvmi) || StringUtils.isBlank(yemIsvmi))
                    selectWareArea(e, "yem_warehouse");
//                if (!StringUtils.isBlank(yemIsvmi))
//                    e.addCustomQFilter(new QFilter("group.yem_sort", QCP.equals, yemIsvmi));
                //根据仓库过滤库区
                DynamicObject wh = (DynamicObject) getModel().getValue("yem_warehouse", row);
                e.addCustomQFilter(new QFilter("group.id", QCP.equals, wh.get("id")));
                break;
            case "yem_outareacode":
//                if ("V".equals(yemIsvmi) || "C".equals(yemIsvmi) || StringUtils.isBlank(yemIsvmi))
                    selectWareArea(e, "yem_outwhcode");
//                if(!StringUtils.isBlank(yemIsvmi))e.addCustomQFilter(new QFilter("group.yem_sort",QCP.equals,yemIsvmi));
                //根据仓库过滤库区
                DynamicObject wh1 = (DynamicObject) getModel().getValue("yem_outwhcode", row);
                e.addCustomQFilter(new QFilter("group.id", QCP.equals, wh1.get("id")));
                break;
            case "yem_whlocation":
//                if ("V".equals(yemIsvmi) || "C".equals(yemIsvmi) || StringUtils.isBlank(yemIsvmi))
                    selectWhLocation(e, "yem_warearea", "yem_warehouse");
//                if (!StringUtils.isBlank(yemIsvmi))
//                    e.addCustomQFilter(new QFilter("group.group.yem_sort", QCP.equals, yemIsvmi));
                //库位根据库区过滤
                DynamicObject wa = (DynamicObject) getModel().getValue("yem_warearea", row);
                if (wa == null) {
                    getView().showErrorNotification("请填写调出库区！");
                    e.setCancel(true);
                } else {
                    e.addCustomQFilter(new QFilter("group.id", QCP.equals, wa.get("id")));
                }
                break;
            case "yem_outpositioncode":
//                if ("V".equals(yemIsvmi) || "C".equals(yemIsvmi) || StringUtils.isBlank(yemIsvmi))
                    selectWhLocation(e, "yem_outareacode", "yem_outwhcode");
//                if(!StringUtils.isBlank(yemIsvmi))e.addCustomQFilter(new QFilter("group.group.yem_sort",QCP.equals,yemIsvmi));
                //库位根据库区过滤
                DynamicObject wa1 = (DynamicObject) getModel().getValue("yem_outareacode", row);
                if (wa1 == null) {
                    getView().showErrorNotification("请填写调入库区！");
                    e.setCancel(true);
                } else {
                    e.addCustomQFilter(new QFilter("group.id", QCP.equals, wa1.get("id")));
                }
                break;
            case "yem_materialproperty":
                selectMaterialProperty(e);
                break;
            case "yem_sup":
                selectSup(e);
                break;
            case "yem_whman":
                selectWhMan(e, "yem_warehouse");
                break;
            case "yem_outstockercode":
                selectWhMan(e, "yem_outwhcode");
                break;
            case "yem_qualitierr":
                selectQualityUser(e);
                break;
            case "yem_buyerr":
                selectBuyer(e);
                break;
//            case "yem_gcxm":
//                selectGCXM(e);
//                break;
//            case "yem_yfxm":
//                selectYFXM(e);
//                break;
//            case "yem_fyxm":
//                selectFYXM(e);
//                break;
        }
    }

    private String getYemBelongTo() {
        DynamicObject cplx = (DynamicObject) this.getModel().getValue("yem_bd_products");
        if (cplx == null) {
            this.getView().showErrorNotification("请先选择产品类型！");
            return null;
        }
        cplx = BusinessDataServiceHelper.loadSingle(cplx.getLong("id"), "yem_bd_productsgroup");
        Long id = RequestCmmp.getTopCplx(cplx);
        DynamicObject yem_cmmpconfig = QueryServiceHelper.queryOne("yem_cmmpconfig", "id,yem_belong",
                new QFilter[]{new QFilter("yem_cmmpconfigentry.yem_bd_productsgroupn.id", QCP.equals, id)});
        String yemBelong = yem_cmmpconfig.getString("yem_belong");
        return yemBelong;
    }


    private void selectGCXM(BeforeF7SelectEvent e) {
        DynamicObject genasstypegroup = BusinessDataServiceHelper.loadSingle("yem_genasstypegroup", new QFilter("number", QCP.equals, "A").toArray());
        ListShowParameter form = (ListShowParameter) e.getFormShowParameter();
        QFilter qFilter = new QFilter("yem_group.id", QCP.equals, genasstypegroup.getPkValue().toString());
        form.getTreeFilterParameter().getQFilters().add(qFilter);
        e.addCustomQFilter(qFilter);
    }

    private void selectYFXM(BeforeF7SelectEvent e) {
        DynamicObject genasstypegroup = BusinessDataServiceHelper.loadSingle("yem_genasstypegroup", new QFilter("number", QCP.equals, "B").toArray());
        ListShowParameter form = (ListShowParameter) e.getFormShowParameter();
        QFilter qFilter = new QFilter("yem_group.id", QCP.equals, genasstypegroup.getPkValue().toString());
        form.getTreeFilterParameter().getQFilters().add(qFilter);
        e.addCustomQFilter(qFilter);
    }

    private void selectFYXM(BeforeF7SelectEvent e) {
        DynamicObject genasstypegroup = BusinessDataServiceHelper.loadSingle("yem_genasstypegroup", new QFilter("number", QCP.equals, "H").toArray());
        ListShowParameter form = (ListShowParameter) e.getFormShowParameter();
        QFilter qFilter = new QFilter("yem_group.id", QCP.equals, genasstypegroup.getPkValue().toString());
        form.getTreeFilterParameter().getQFilters().add(qFilter);
        e.addCustomQFilter(qFilter);
    }


    @Override
    public void click(EventObject e) {
        super.click(e);
        Control control = (Control) e.getSource();
        String key = control.getKey();
        switch (key) {
            case "yem_manufacturer":
                selectManufacturer();
                break;
        }
    }

    /**
     * 仓库，库区，库位 提示提示
     */
    private void hint(String choose, String hint, String name, int row) {
        DynamicObject chooses = (DynamicObject) this.getModel().getValue(choose, row);
        if (chooses == null) {
            this.getModel().setValue(hint, null, row);
            this.getView().showErrorNotification("请先选择" + name);
        }
    }


    @Override
    public void initPropertyChanged(PropertyChangedArgs e) {
        super.initPropertyChanged(e);
    }

    public void setWarehouseAreaInfo(int rowIndex, String a, String yem_warehouse, String yem_warearea, String yem_whlocation, String yemWhman) {
        IDataModel model = this.getModel();
        DynamicObject billType = (DynamicObject) model.getValue("yem_billtype");
        String billTypeNumber = YEM.isNotEmpty(billType) ? billType.getString("number") : "";
        String isVmi = (String) model.getValue("yem_isvmi");
        //根据产品类型获取连接配置
        DynamicObject cplx = (DynamicObject) model.getValue("yem_bd_products");
        if (cplx == null) {
            this.getView().showErrorNotification("请先选择产品类型！");
        }
        cplx = BusinessDataServiceHelper.loadSingle(cplx.getLong("id"), "yem_bd_productsgroup");
        Long id = RequestCmmp.getTopCplx(cplx);
        DynamicObject yem_cmmpconfig = QueryServiceHelper.queryOne("yem_cmmpconfig", "id,yem_belong",
                new QFilter[]{new QFilter("yem_cmmpconfigentry.yem_bd_productsgroupn.id", QCP.equals, id)});
        //根据仓库类型、产品类型获取仓库
        DynamicObjectCollection whs = null;
        //根据仓库类型、产品类型获取仓位
        DynamicObjectCollection wls = null;
        //根据仓库类型、产品类型获取库区
        DynamicObjectCollection was = null;
        if (yem_warehouse.contains("out")) {
            whs = QueryServiceHelper.query("yem_warehouse", "number",
                    new QFilter[]{new QFilter("yem_belong", QCP.equals, yem_cmmpconfig.getString("yem_belong"))});
            was = QueryServiceHelper.query("yem_warearea", "number",
                    new QFilter[]{
                            new QFilter("yem_belong", QCP.equals, yem_cmmpconfig.getString("yem_belong"))});
            wls = QueryServiceHelper.query("yem_whlocation", "number",
                    new QFilter[]{
                            new QFilter("group.yem_belong", QCP.equals, yem_cmmpconfig.getString("yem_belong"))});
        } else {
            whs = QueryServiceHelper.query("yem_warehouse", "number",
//
                    new QFilter[]{new QFilter("yem_sort", QCP.equals, isVmi),
                            new QFilter("yem_belong", QCP.equals, yem_cmmpconfig.getString("yem_belong"))});
            was = QueryServiceHelper.query("yem_warearea", "number",
                    new QFilter[]{
//                            new QFilter("group.yem_sort", QCP.equals, isVmi),
                            new QFilter("yem_belong", QCP.equals, yem_cmmpconfig.getString("yem_belong"))});
            wls = QueryServiceHelper.query("yem_whlocation", "number",
                    new QFilter[]{
//                                    new QFilter("group.group.yem_sort", QCP.equals, isVmi),
                            new QFilter("group.yem_belong", QCP.equals, yem_cmmpconfig.getString("yem_belong"))});
        }

        Set<String> warehouseSet1 = new HashSet();
        HashSet<String> wareAreaSet1 = new HashSet();
        HashSet<String> positionSet1 = new HashSet();
        for (DynamicObject wh : whs) {
            warehouseSet1.add(wh.getString("number"));
        }


        for (DynamicObject wa : was) {
            wareAreaSet1.add(wa.getString("number"));
        }

        for (DynamicObject wl : wls) {
            positionSet1.add(wl.getString("number"));
        }
        if (("yem_orderproreqbill_DB".equals(billTypeNumber) || "yem_orderproreqbill_CG".equals(billTypeNumber)) && !"Z".equals(isVmi)) {
            List<Map> results = getResult(model, rowIndex, ViewInterfaceDocking.MATERIAL_WAREHOUSE, a, yem_warehouse, yem_warearea, yem_whlocation);
            if (YEM.isNotEmpty(results)) {
                Set<String> warehouseSet = new HashSet();
                HashSet<String> wareAreaSet = new HashSet();
                HashSet<String> positionSet = new HashSet();
                Set<String> warehouseSet2 = new HashSet();
                HashSet<String> wareAreaSet2 = new HashSet();
                HashSet<String> positionSet2 = new HashSet();
                HashSet<String> stockerSet = new HashSet();
                for (Map result : results) {
                    String warehouseNumber = (String) result.get("wh_code");
                    String wareAreaNumber = (String) result.get("area_code");
                    String positionNumber = (String) result.get("position_code");
                    String stocker_code = (String) result.get("stocker_code");
                    warehouseSet.add(warehouseNumber);
                    wareAreaSet.add(wareAreaNumber);
                    positionSet.add(positionNumber);
                    stockerSet.add(stocker_code);
                }
                warehouseSet2.addAll(warehouseSet1);
                warehouseSet2.retainAll(warehouseSet);
                if (warehouseSet2.size() == 1) {
                    List<String> list = new ArrayList(warehouseSet2);
                    String warehouseNumber = list.get(0);
                    DynamicObject warehouse = FunctionalCommon.getBaseDynamicObject("yem_warehouse", warehouseNumber);
                    model.setValue(yem_warehouse, warehouse, rowIndex);
                    if ("yem_orderproreqbill_DB".equals(billTypeNumber) && "yem_warehouse".equals(yem_warehouse))
                        model.setValue("yem_retrievecode", warehouse);
                    if ("yem_orderproreqbill_DB".equals(billTypeNumber) && "yem_outwhcode".equals(yem_warehouse))
                        model.setValue("yem_transfercode", warehouse);
                }
                wareAreaSet2.addAll(wareAreaSet1);
                wareAreaSet2.retainAll(wareAreaSet);
                if (wareAreaSet2.size() == 1) {
                    List<String> list = new ArrayList(wareAreaSet2);
                    String wareAreaNumber = list.get(0);
                    DynamicObject wareArea = FunctionalCommon.getBaseDynamicObject("yem_warearea", wareAreaNumber);
                    model.setValue(yem_warearea, wareArea, rowIndex);
                }
                positionSet2.addAll(positionSet1);
                positionSet2.retainAll(positionSet);

                if (positionSet2.size() == 1) {
                    List<String> list = new ArrayList(positionSet2);
                    String positionNumber = list.get(0);
                    DynamicObject position = FunctionalCommon.getBaseDynamicObject("yem_whlocation", positionNumber);
                    model.setValue(yem_whlocation, position, rowIndex);
                }
                if (stockerSet.size() == 1) {
                    List<String> list = new ArrayList(stockerSet);
                    String stockerNum = list.get(0);
                    DynamicObject stocker = FunctionalCommon.getBaseDynamicObject("bos_user", stockerNum);
                    model.setValue(yemWhman, stocker, rowIndex);
                }
            }
        }
    }


    /**
     * @description 物料属性赋值
     * @author xwudd
     * @param[1] row
     * return
     * @time 2023/8/21 16:32
     */

    public void setMaterialPropertyOption(int row) {
        IDataModel model = this.getModel();
        List<Map> results = getResult(model, row, ViewInterfaceDocking.MATERIAL_PROPERTY, "A", null, null, null);
        HashSet<String> propertys = new HashSet<>();
        if (YEM.isNotEmpty(results) && !"Z".equals(model.getValue("yem_isvmi"))) {
            for (Map result : results) {
                String property = (String) result.get("property");
                propertys.add(property);
            }
            if (propertys.size() == 1) {
                DynamicObject materialProperty = BusinessDataServiceHelper.loadSingle("yem_materialpropert", "id,name,number",
                        new QFilter[]{new QFilter("name", QCP.equals, new ArrayList<>(propertys).get(0))});
                model.setValue("yem_materialproperty", materialProperty, row);
            }
        }

    }

    /**
     * @description 制造商 供应商赋值
     * @author xwudd
     * * @param[1] row
     * return
     * @time 2023/8/21 17:20
     */

    public void setMaterialInfo(int row) {
        IDataModel model = this.getModel();
        List<Map> results = getResult(model, row, ViewInterfaceDocking.MATERIAL_INFO, "A", null, null, null);
        HashMap<String, String> suppliers = new HashMap<>();
        HashSet<String> sups = new HashSet<>();
        if (YEM.isNotEmpty(results) && !"Z".equals(model.getValue("yem_isvmi"))) {
            for (Map result : results) {
                String supplierNumber = (String) result.get("supplier_code");
                String supplierName = (String) result.get("supplier_name");
                String supNumber = (String) result.get("sup_code");
                suppliers.put(supplierNumber, supplierName);
                sups.add(supNumber);
            }
            if (suppliers.size() == 1) {
                Set<String> keys = suppliers.keySet();
                for (String key : keys) {
                    model.setValue("yem_manufacturer", key, row);
                    model.setValue("yem_manufacturername", suppliers.get(key), row);
                }
            }
            if (sups.size() == 1) {
                for (String sup : sups) {
                    DynamicObject supplier = BusinessDataServiceHelper.loadSingle("bd_supplier", "id",
                            new QFilter[]{new QFilter("number", QCP.equals, sup)});
                    model.setValue("yem_sup", supplier, row);
                }
            }
        }

    }

    /**
     * @description 质检员赋值
     * @author xwudd
     * @param[1] row
     * return
     * @time 2023/8/21 16:32
     */

    public void setQualityUser(int row) {
        IDataModel model = this.getModel();
        List<Map> results = getResult(model, row, ViewInterfaceDocking.QUALITY_USER_INFO, "A", null, null, null);
        HashSet<String> userNumbers = new HashSet<>();
        if (YEM.isNotEmpty(results) && !"Z".equals(model.getValue("yem_isvmi"))) {
            for (Map result : results) {
                String property = (String) result.get("checker_code");
                userNumbers.add(property);
            }
            if (userNumbers.size() == 1) {
                DynamicObject user = BusinessDataServiceHelper.loadSingle("bos_user", "id,name,number",
                        new QFilter[]{new QFilter("number", QCP.equals, new ArrayList<>(userNumbers).get(0))});
                model.setValue("yem_qualitierr", user, row);
            }
        }

    }

    /**
     * @description 质检员根据物料过滤
     * @author xwudd
     * @param[1] e
     * return
     * @time 2023/8/22 11:44
     */

    public void selectQualityUser(BeforeF7SelectEvent e) {
        IDataModel model = this.getModel();
        int row = e.getRow();
        List<Map> results = getResult(model, row, ViewInterfaceDocking.QUALITY_USER_INFO, "A", null, null, null);
        HashSet<String> userNumbers = new HashSet<>();
        if (YEM.isNotEmpty(results) && !"Z".equals(model.getValue("yem_isvmi"))) {
            for (Map result : results) {
                String property = (String) result.get("checker_code");
                userNumbers.add(property);
            }
        }
        e.addCustomQFilter(new QFilter("number", QCP.in, userNumbers));
    }


    /**
     * @description 采购员赋值
     * @author xwudd
     * @param[1] row
     * return
     * @time 2023/8/21 16:32
     */

    public void setBuyer(int row) {
        IDataModel model = this.getModel();
        List<Map> results = getResult(model, row, ViewInterfaceDocking.MATERIAL_BUYER, "A", null, null, null);
        HashSet<String> userNumbers = new HashSet<>();
        if (YEM.isNotEmpty(results) && !"Z".equals(model.getValue("yem_isvmi"))) {
            for (Map result : results) {
                String property = (String) result.get("buyer_code");
                userNumbers.add(property);
            }
            if (userNumbers.size() == 1) {
                DynamicObject user = BusinessDataServiceHelper.loadSingle("bos_user", "id,name,number",
                        new QFilter[]{new QFilter("number", QCP.equals, new ArrayList<>(userNumbers).get(0))});
                model.setValue("yem_buyerr", user, row);
            }
        }

    }


    /**
     * @description 采购员根据物料过滤
     * @author xwudd
     * @param[1] e
     * return
     * @time 2023/8/22 11:44
     */

    public void selectBuyer(BeforeF7SelectEvent e) {
        IDataModel model = this.getModel();
        int row = e.getRow();
        List<Map> results = getResult(model, row, ViewInterfaceDocking.MATERIAL_BUYER, "A", null, null, null);
        HashSet<String> userNumbers = new HashSet<>();
        if (YEM.isNotEmpty(results) && !"Z".equals(model.getValue("yem_isvmi"))) {
            for (Map result : results) {
                String property = (String) result.get("buyer_code");
                userNumbers.add(property);
            }
        }
        e.addCustomQFilter(new QFilter("number", QCP.in, userNumbers));
    }

    /**
     * @description 选择仓库根据物料过滤
     * @author xwudd
     * @param[1] e
     * return
     * @time 2023/8/21 15:24
     */
    public void selectWarehouse(BeforeF7SelectEvent e) {
        IDataModel model = this.getModel();
        int row = e.getRow();
        List<Map> results = getResult(model, row, ViewInterfaceDocking.MATERIAL_WAREHOUSE, "A", null, null, null);
        if (YEM.isNotEmpty(results) && !"Z".equals(model.getValue("yem_isvmi"))) {
            HashSet<String> warehouseNumbers = new HashSet<>();
            for (Map result : results) {
                String warehouseNumber = (String) result.get("wh_code");
                warehouseNumbers.add(warehouseNumber);
            }
            e.addCustomQFilter(new QFilter("number", QCP.in, warehouseNumbers));
            e.addCustomQFilter(new QFilter("number", QCP.in, warehouseNumbers));
        } else if ("Z".equals(model.getValue("yem_isvmi"))) {
            e.addCustomQFilter(new QFilter("yem_belong", QCP.equals, "A"));
        } else {
            DynamicObject materiel = (DynamicObject) model.getValue("yem_realpurmateriel", row);
            String materialNumber = YEM.isNotEmpty(materiel) ? materiel.getString("number") : "";
            this.getView().showErrorNotification("请联系系统管理员物料编码为：【" + materialNumber + "】维护CMMP【物料仓库维护】视图！");
            e.setCancel(true);
        }
    }


    /**
     * @description 选择库区根据物料、仓库过滤
     * @author xwudd
     * @param[1] e
     * return
     * @time 2023/8/21 15:24
     */
    public void selectWareArea(BeforeF7SelectEvent e, String yemWarehouse) {
        IFormView view = this.getView();
        IDataModel model = this.getModel();
        int row = e.getRow();
        DynamicObject warehouse = (DynamicObject) model.getValue(yemWarehouse, row);
        if (YEM.isNotEmpty(warehouse) && !"Z".equals(model.getValue("yem_isvmi"))) {
            List<Map> results = getResult(model, row, ViewInterfaceDocking.MATERIAL_WAREHOUSE, "B", yemWarehouse, null, null);
            if (YEM.isNotEmpty(results)) {
                HashSet<String> warehouseNumbers = new HashSet<>();
                for (Map result : results) {
                    String warehouseNumber = (String) result.get("area_code");
                    warehouseNumbers.add(warehouseNumber);
                }
                e.addCustomQFilter(new QFilter("number", QCP.in, warehouseNumbers));
            }
        } else if ("Z".equals(model.getValue("yem_isvmi"))) {
            e.addCustomQFilter(new QFilter("yem_belong", QCP.equals, "A"));
        } else {
            view.showErrorNotification("请先选择仓库！");
            e.setCancel(true);
        }

    }


    /**
     * @description 选择仓位根据物料、仓库、库区过滤
     * @author xwudd
     * @param[1] e
     * return
     * @time 2023/8/21 15:24
     */
    public void selectWhLocation(BeforeF7SelectEvent e, String yemWarearea, String yem_warehouse) {
        IFormView view = this.getView();
        IDataModel model = this.getModel();
        int row = e.getRow();
        DynamicObject wareArea = (DynamicObject) model.getValue(yemWarearea, row);
        if (YEM.isNotEmpty(wareArea) && !"Z".equals(model.getValue("yem_isvmi"))) {
            List<Map> results = getResult(model, row, ViewInterfaceDocking.MATERIAL_WAREHOUSE, "C", yem_warehouse, yemWarearea, null);
            if (YEM.isNotEmpty(results)) {
                HashSet<String> warehouseNumbers = new HashSet<>();
                for (Map result : results) {
                    String warehouseNumber = (String) result.get("position_code");
                    warehouseNumbers.add(warehouseNumber);
                }
                e.addCustomQFilter(new QFilter("number", QCP.in, warehouseNumbers));
            }
        } else {
            view.showErrorNotification("请先选择库区！");
            e.setCancel(true);
        }

    }


    public void selectWhMan(BeforeF7SelectEvent e, String yemWhlocation) {
        IFormView view = this.getView();
        IDataModel model = this.getModel();
        int row = e.getRow();
        DynamicObject whLocation = (DynamicObject) model.getValue(yemWhlocation, row);
        if (YEM.isNotEmpty(whLocation) && !"Z".equals(model.getValue("yem_isvmi"))) {
            List<Map> results = getResult(model, row, ViewInterfaceDocking.MATERIAL_WAREHOUSE, "B", yemWhlocation, null, null);
            HashSet<String> propertys = new HashSet<>();
            for (Map result : results) {
                String property = (String) result.get("stocker_code");
                propertys.add(property);
            }
            e.addCustomQFilter(new QFilter("number", QCP.in, propertys));
        } else {
            view.showErrorNotification("请先选择库位！");
            e.setCancel(true);
        }
    }


    /**
     * @description 物料属性过滤
     * @author xwudd
     * @param[1] e
     * return
     * @time 2023/8/21 18:02
     */

    public void selectMaterialProperty(BeforeF7SelectEvent e) {
        IDataModel model = this.getModel();
        int row = e.getRow();
        List<Map> results = getResult(model, row, ViewInterfaceDocking.MATERIAL_PROPERTY, "A", null, null, null);
        HashSet<String> propertys = new HashSet<>();
        if (YEM.isNotEmpty(results) && !"Z".equals(model.getValue("yem_isvmi"))) {
            for (Map result : results) {
                String property = (String) result.get("property");
                propertys.add(property);
            }
            e.addCustomQFilter(new QFilter("name", QCP.in, propertys));
        }

    }


    public void selectSup(BeforeF7SelectEvent e) {
        IDataModel model = this.getModel();
        int row = e.getRow();
        List<Map> results = getResult(model, row, ViewInterfaceDocking.MATERIAL_INFO, "D", null, null, null);
        HashSet<String> sups = new HashSet<>();
        if (YEM.isNotEmpty(results) && !"Z".equals(model.getValue("yem_isvmi"))) {
            for (Map result : results) {
                String supNumber = (String) result.get("sup_code");
                sups.add(supNumber);
            }
            e.addCustomQFilter(new QFilter("number", QCP.in, sups));
        }

    }

    @Override
    public void beforePropertyChanged(PropertyChangedArgs e) {
        super.beforePropertyChanged(e);
    }

    public void selectManufacturer() {
        IFormView view = this.getView();
        IDataModel model = this.getModel();
        int currentRowIndex = model.getEntryCurrentRowIndex("yem_es_materialinfo");
        List<Map> results = getResult(model, currentRowIndex, ViewInterfaceDocking.MATERIAL_INFO, "A", null, null, null);
        Map<String, String> suppliers = new HashMap<>();
        if (YEM.isNotEmpty(results)) {
            for (Map result : results) {
                String supplierNumber = (String) result.get("supplier_code");
                String suppliername = (String) result.get("supplier_name");
                suppliers.put(supplierNumber, suppliername);
            }
        }
        FormShowParameter fsp = new FormShowParameter();
        fsp.setFormId("yem_selectmanufacturer");
        fsp.getOpenStyle().setShowType(ShowType.Modal);
        fsp.setCustomParam("data", suppliers);
        fsp.setCustomParam("rowIndex", currentRowIndex);
        view.showForm(fsp);
    }

    /**
     * @description 获取物料仓库接口结果
     * @author xwudd
     * @param[1] rowIndex
     * @param[2] flag
     * return
     * @time 2023/8/21 15:17
     */

    public List<Map> getResult(IDataModel model, int rowIndex, String methodKey, String flag, String yem_warehouse, String yem_warearea, String yem_whlocation) {
        List<Map> results = null;
        Map<String, Object> map = new HashMap();
        String warehouseNumber = "";
        if (yem_warehouse != null) {
            DynamicObject warehouse = (DynamicObject) model.getValue(yem_warehouse, rowIndex);
            warehouseNumber = YEM.isNotEmpty(warehouse) ? warehouse.getString("number") : "";
        }
        String wareAreaNumber = "";
        if (yem_warearea != null) {
            DynamicObject wareArea = (DynamicObject) model.getValue(yem_warearea, rowIndex);
            wareAreaNumber = YEM.isNotEmpty(wareArea) ? wareArea.getString("number") : "";
        }
        DynamicObject materiel = (DynamicObject) model.getValue("yem_realpurmateriel", rowIndex);
        String materialNumber = YEM.isNotEmpty(materiel) ? materiel.getString("number") : "";


        if ("A".equals(flag)) {
            map.put("mater_code", materialNumber);
        }
        if ("B".equals(flag)) {
            map.put("mater_code", materialNumber);
            map.put("wh_code", warehouseNumber);
        }
        if ("C".equals(flag)) {
            map.put("mater_code", materialNumber);
            map.put("wh_code", warehouseNumber);
            map.put("area_code", wareAreaNumber);
        }
        if ("D".equals(flag)) {
            String supplierNumber = (String) model.getValue("yem_manufacturer", rowIndex);
            map.put("mater_code", materialNumber);
            map.put("supplier_code", supplierNumber);
        }
        Long yemBdProductsid = ((DynamicObject) model.getValue("yem_bd_products")).getLong("id");
        results = ViewInterfaceDocking.docking(methodKey, map, yemBdProductsid);
        return results;
    }

    @Override
    public void beforeDoOperation(BeforeDoOperationEventArgs e) {
        super.beforeDoOperation(e);
        FormOperate source = (FormOperate) e.getSource();
        String operateKey = source.getOperateKey();
        DynamicObject dataEntity = this.getModel().getDataEntity(true);
        IFormView view = this.getView();
        if ("submit".equals(operateKey)) {

//            fillWithCanUseNum(dataEntity);

//            SaveServiceHelper.update(dataEntity);
        }

        if ("save".equals(operateKey)) {
            fillWithCanUseNum(dataEntity);
            view.updateView();
        }
    }

    /**
     * 订单产品订单需求单仓库类型为正常仓库和VMI库的提交获取CMMP的可用数量，云之家加字段，同步到云之家
     * CMMP可用数量视图中可能有多条，根据仓库和物料合计
     *
     * @param dataEntity
     */
    public static void fillWithCanUseNum(DynamicObject dataEntity) {
        String isvmi = dataEntity.getString("yem_isvmi");
        long products = dataEntity.getLong("yem_bd_products.id");
        DynamicObjectCollection collection = dataEntity.getDynamicObjectCollection("yem_es_materialinfo");
        // 正常仓库和VMI库
        if (("C".equals(isvmi) || "V".equals(isvmi)) && YEM.isNotEmpty(products)) {

            Map<String, List<DynamicObject>> merge = new HashMap<>();

            for (DynamicObject dynamicObject : collection) {
                // 调出仓库编码
                String warehouse = dynamicObject.getString("yem_warehouse.number");

                if (merge.containsKey(warehouse)) {
                    List<DynamicObject> exist = merge.get(warehouse);
                    exist.add(dynamicObject);
                    merge.put(warehouse, exist);
                } else {
                    List<DynamicObject> exist = new ArrayList<>();
                    exist.add(dynamicObject);
                    merge.put(warehouse, exist);
                }
            }

            for (Map.Entry<String, List<DynamicObject>> entry : merge.entrySet()) {
                String wh_code = entry.getKey();
                List<DynamicObject> value = entry.getValue();

                StringBuilder mater_code = new StringBuilder();
                for (DynamicObject dynamicObject : value) {
                    String materiel = dynamicObject.getString("yem_materiel.number");
                    if (YEM.isNotEmpty(materiel)) {
                        mater_code.append(String.format("'%s'", materiel)).append(",");
                    }
                }

                if (mater_code.length() > 0) {
                    mater_code.deleteCharAt(mater_code.length() - 1);
                    List<Map> fromCmmp = RequestCmmp.getFromCmmp(getMainOrgId(), String.format(
                            "SELECT mater_code, wh_code, SUM(canuse_num) AS canuse_num " +
                                    "FROM VIEW_MATER_STOCK_WM " +
                                    "WHERE wh_code = '%s' " +
                                    "and mater_code in (%s) " +
                                    "GROUP BY mater_code, wh_code", wh_code, mater_code), products);
                    fillBackCMMPData(dataEntity, fromCmmp);
                }
            }
        }
    }

    public static void fillBackCMMPData(DynamicObject dataEntity, List<Map> fromCmmp) {
        DynamicObjectCollection collection = dataEntity.getDynamicObjectCollection("yem_es_materialinfo");
        for (DynamicObject dynamicObject : collection) {
            String warehouse = dynamicObject.getString("yem_warehouse.number");
            String materiel = dynamicObject.getString("yem_materiel.number");
            if (YEM.isNotEmpty(materiel) && YEM.isNotEmpty(warehouse)) {
                for (Map map : fromCmmp) {
                    Map<String, Object> convert = map;
                    String mater_code = "";
                    if (convert.containsKey("mater_code")) {
                        mater_code = convert.get("mater_code").toString();
                    }

                    String wh_code = "";
                    if (convert.containsKey("wh_code")) {
                        wh_code = convert.get("wh_code").toString();
                    }

                    BigDecimal canuse_num = BigDecimal.ZERO;
                    if (convert.containsKey("canuse_num")) {
                        canuse_num = new BigDecimal(String.valueOf(convert.get("canuse_num")));
                    }

                    if (YEM.isNotEmpty(mater_code) && YEM.isNotEmpty(wh_code) && YEM.isNotEmpty(canuse_num)) {
                        if (materiel.equals(mater_code) && warehouse.equals(wh_code)) {
                            dynamicObject.set("yem_accqty", canuse_num);
                        }
                    }
                }
            }
        }
    }

    public static long getMainOrgId() {
        QFilter qFilter = new QFilter("number", QCP.equals, "110");
        DynamicObject bos_org = BusinessDataServiceHelper.loadSingle("bos_org", "id", qFilter.toArray());
        if (bos_org != null) {
            return bos_org.getLong("id");
        }
        return 0L;
    }
}
