package com.yem.em.ass.dynamic;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.form.FormShowParameter;
import kd.bos.form.control.Control;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;

import java.util.EventObject;
import java.util.HashMap;
import java.util.Map;

/**
 * @author zhouc
 * @date 2024/1/2 23:26
 * @className PartSelectionDynamicEdit
 * @description 零件选择动态表单插件
 */
public class PartSelectionDynamicEdit extends AbstractFormPlugin {
    @Override
    public void afterBindData(EventObject e) {
        super.afterBindData(e);
        DynamicObject dataEntity = this.getModel().getDataEntity(true);
        IDataModel model = this.getModel();
        FormShowParameter showParameter = this.getView().getFormShowParameter();
        Map<String, Object> customParas = showParameter.getCustomParams();
        if (customParas != null) {
            String pkId = "", yem_replacement = "";
            if (customParas.containsKey("yem_replacement")) {
                yem_replacement = customParas.get("yem_replacement").toString();
            }
            if (yem_replacement.equals("Par")) {
                QFilter qFilter = new QFilter("number", QCP.equals, customParas.get("number"));
                DynamicObject yem_em_assatlasdet = BusinessDataServiceHelper.loadSingle("yem_em_cmmptable", "id", qFilter.toArray());
                if (yem_em_assatlasdet != null) {
                    pkId = yem_em_assatlasdet.getString("id");
                }
                if (pkId.length() == 0) {
                    return;
                }
                DynamicObject yem_em_cmmptable = BusinessDataServiceHelper.loadSingle(pkId, "yem_em_cmmptable");
                if (yem_em_cmmptable != null) {
                    DynamicObjectCollection yem_entryentity = yem_em_cmmptable.getDynamicObjectCollection("yem_entryentity");
                    for (int i = 0; i < yem_entryentity.size(); i++) {
                        DynamicObject dynamicObject = yem_entryentity.get(i);
                        boolean yem_isdelete = dynamicObject.getBoolean("yem_isdelete");
                        //是否替换
                        boolean yem_partisreplace = dynamicObject.getBoolean("yem_partisreplace");
                        if (!yem_isdelete && !yem_partisreplace) {
                            int row = model.createNewEntryRow("yem_entryentity");
                            model.setValue("yem_number", dynamicObject.getString("yem_asscomponent.number"), row);
                            model.setValue("yem_name", dynamicObject.getString("yem_assname"), row);
//                            model.setValue("yem_enname", dynamicObject.getString("yem_partenname"), row);
                            model.setValue("yem_qty", dynamicObject.getBigDecimal("yem_assqty"), row);
//                            model.setValue("yem_remark", dynamicObject.getString("yem_partremark"), row);
                        }
                    }
                    this.getView().updateView("yem_entryentity");
                }
            } else {
                if (customParas.containsKey("yem_parentass")) {
                    pkId = customParas.get("yem_parentass").toString();
                }
                if (customParas.containsKey("number")) {
                    QFilter qFilter = new QFilter("number", QCP.equals, customParas.get("number"));
//                    qFilter.and( new QFilter("yem_entryentity.yem_partisreplace", QCP.equals, false));
                    DynamicObject yem_em_assatlasdet = BusinessDataServiceHelper.loadSingle("yem_em_assatlasdet", "id", qFilter.toArray());
                    if (yem_em_assatlasdet != null) {
                        pkId = yem_em_assatlasdet.getString("id");
                    }
                }
                if (pkId.length() == 0) {
                    return;
                }
                DynamicObject yem_em_assatlasdet = BusinessDataServiceHelper.loadSingle(pkId, "yem_em_assatlasdet");
                if (yem_em_assatlasdet != null) {
                    DynamicObjectCollection yem_entryentity = yem_em_assatlasdet.getDynamicObjectCollection("yem_entryentity");
                    for (int i = 0; i < yem_entryentity.size(); i++) {
                        DynamicObject dynamicObject = yem_entryentity.get(i);
                        boolean yem_isdelete = dynamicObject.getBoolean("yem_isdelete");
                        //是否替换
                        boolean yem_partisreplace = dynamicObject.getBoolean("yem_partisreplace");
                        if (!yem_isdelete && !yem_partisreplace) {
                            int row = model.createNewEntryRow("yem_entryentity");
                            model.setValue("yem_number", dynamicObject.getString("yem_partnumber"), row);
                            model.setValue("yem_name", dynamicObject.getString("yem_partname"), row);
                            model.setValue("yem_enname", dynamicObject.getString("yem_partenname"), row);
                            model.setValue("yem_qty", dynamicObject.getBigDecimal("yem_partqty"), row);
                            model.setValue("yem_remark", dynamicObject.getString("yem_partremark"), row);
                        }
                    }
                    this.getView().updateView("yem_entryentity");
                }
            }
        }
    }

    @Override
    public void registerListener(EventObject e) {
        this.addClickListeners("btnok");
    }

    @Override
    public void click(EventObject e) {
        super.click(e);
        Control control = (Control) e.getSource();
        String key = control.getKey();
        IDataModel model = this.getModel();
        if ("btnok".equals(key)) {
            Map map = new HashMap();
            int row = this.getModel().getEntryCurrentRowIndex("yem_entryentity");
            map.put("yem_number", model.getValue("yem_number", row));//零件编码
            map.put("yem_name", model.getValue("yem_name", row));//零件名称
            map.put("yem_enname", model.getValue("yem_enname", row));//零件英文名称
            map.put("yem_qty", model.getValue("yem_qty", row));//零件数量
            map.put("yem_remark", model.getValue("yem_remark", row));//备注
            this.getView().returnDataToParent(map);
            this.getView().close();
        }
    }
}
