package com.yem.em.ass.dynamic;

import com.yem.wm.utils.StringUtils;
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.IFormView;
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.Map;

/**
 * @author zhouc
 * @date 2023/11/2 10:44
 * @className ComPonentDynamicEdit
 * @description 设备部件展开动态表单插件
 */
public class ComPonentDynamicEdit 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 = customParas.get("pkId").toString();
            getpartunfold(pkId);
        }
    }

    public void getpartunfold(String pkId) {
        IDataModel model = this.getModel();
        IFormView view = this.getView();
        //1.根据设备零件明细(新)id获取零件编码
        String yem_pnumber = "";
        QFilter qFilter = new QFilter("id", QCP.equals, Long.valueOf(pkId));
        DynamicObject yem_em_part = BusinessDataServiceHelper.loadSingle("yem_em_asscomponent",
                "id,name,number", qFilter.toArray());
        if (yem_em_part != null) {
            yem_pnumber = yem_em_part.getString("number");
        }
        //2.根据部件编码获取《替换关系》原部件编码明细
        if (StringUtils.isNotEmpty(yem_pnumber)) {
            QFilter qFilter1 = new QFilter("yem_pnumber", QCP.equals, yem_pnumber);
            DynamicObject yem_em_substitution = BusinessDataServiceHelper.loadSingle("yem_em_compsub", "id,name,number", qFilter1.toArray());
            if (yem_em_substitution != null) {
                DynamicObject substitution = BusinessDataServiceHelper.loadSingle(yem_em_substitution.getString("id"), "yem_em_compsub");
                if (substitution != null) {
                    DynamicObjectCollection yem_entryentity = substitution.getDynamicObjectCollection("yem_entryentity");
                    for(int i=0;i<yem_entryentity.size();i++){
                        DynamicObject obj = yem_entryentity.get(i);
                        int row = model.createNewEntryRow("yem_entryentity");
                        model.setValue("yem_onumber", obj.getString("yem_onumber"), row);
                        model.setValue("yem_oname", obj.getString("yem_oname"), row);
                        model.setValue("yem_oenname", obj.getString("yem_oenname"), row);
                        model.setValue("yem_oqty", obj.getBigDecimal("yem_oqty"), row);
                        model.setValue("yem_repnumber", obj.getString("yem_repnumber"), row);
                        model.setValue("yem_repname", obj.getString("yem_repname"), row);
                        model.setValue("yem_repenname", obj.getString("yem_repenname"), row);
                        model.setValue("yem_repenqty", obj.getBigDecimal("yem_repenqty"), row);
                        model.setValue("yem_replace", obj.getString("yem_replace"), row);
                        model.setValue("yem_replacedate", obj.getDate("yem_replacedate"), row);

                        model.setValue("yem_technology", obj.getDate("yem_technology"), row);
                        model.setValue("yem_trackingno", obj.getString("yem_trackingno"), row);
                        model.setValue("yem_remark", obj.getString("yem_remark"), row);
                    }
                    view.updateView("yem_entryentity");
                }
            }
        }
    }
}
