package yem.wmc.ca.formplugin.form;
import java.util.*;

import kd.bos.form.control.Control;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.dataentity.metadata.IDataEntityProperty;
import kd.bos.dataentity.metadata.IDataEntityType;
import kd.bos.entity.EntityMetadataCache;
import kd.bos.entity.MainEntityType;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.entity.datamodel.events.PropertyChangedArgs;
import kd.bos.entity.property.*;
import kd.bos.form.control.EntryGrid;
import kd.bos.form.control.events.ClickListener;
import kd.bos.form.control.events.ItemClickEvent;
import kd.bos.form.control.events.ItemClickListener;
import kd.bos.form.events.AfterDoOperationEventArgs;
import kd.bos.metadata.dao.MetaCategory;
import kd.bos.metadata.dao.MetadataDao;
import kd.bos.entity.SubEntryType;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.metadata.form.BillFormAp;
import kd.bos.metadata.form.ControlAp;
import kd.bos.metadata.form.FormMetadata;
import kd.bos.metadata.form.control.EntryAp;
import kd.bos.metadata.form.control.FieldAp;
import yem.wmc.ca.utils.StringUtils;


/**
 * 关务单据注册表单插件
 * @author lzb
 *
 */
public class BilllogonEdit extends AbstractFormPlugin implements ItemClickListener,ClickListener {

    @Override
    public void propertyChanged(PropertyChangedArgs e) {
        super.propertyChanged(e);
//        IDataModel model = this.getModel();
        String name = e.getProperty().getName();
//        ChangeData changeData = e.getChangeSet()[0];
//        Object newValue = changeData.getNewValue();
//        Object oldValue = changeData.getOldValue();
//        int parentRowIndex = changeData.getParentRowIndex();
//        int rowIndex = changeData.getRowIndex();
        switch (name) {
            case "yem_formid":
                init_EntityColName();
                this.getView().updateView("yem_entityfieldcol");
                break;
            default:
                break;
        }
    }

    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        // 侦听按钮点击事件
        this.addItemClickListeners("yem_reload");
        this.addClickListeners("yem_reload");
    }

    @Override
    public void itemClick(ItemClickEvent evt) {
        super.itemClick(evt);
        String source = evt.getItemKey();
        switch (source) {
            case "":
                break;
            default:
                break;
        }
    }


    @Override
    public void click(EventObject evt) {
        super.click(evt);
        Control source = (Control) evt.getSource();
        String key = source.getKey();
        switch (key) {
            case "":
                break;
            default:
                break;
        }
    }

    @Override
    public void afterDoOperation(AfterDoOperationEventArgs e) {
        super.afterDoOperation(e);
        boolean isSuccess = false;
        if (e.getOperationResult() != null) {
            isSuccess = e.getOperationResult().isSuccess();
        }
        String operateKey = e.getOperateKey();
        if (StringUtils.isNotEmpty(operateKey)&&isSuccess) {
            if (operateKey.equals("yem_reload")){
                yem_reload();//增量检查
                yem_dropitem();//删除项检查
                this.getView().updateView("yem_entitycontainer");
                this.getView().updateView("yem_entityfieldcol");
            }
        }
    }

    /**++
     * 根据单据标识初始化单据实体名称
     */
    private void init_EntityColName() {
        IDataModel model = this.getModel();
        DynamicObject billDate = model.getDataEntity(true);
        DynamicObject yem_formid = (DynamicObject) model.getValue("yem_formid");
        if (null != yem_formid) {
            String formid = yem_formid.getString("number");
            //加载数据包
            MainEntityType sourceMainType = EntityMetadataCache.getDataEntityType(formid);
            FormMetadata taskMeta = (FormMetadata) MetadataDao.readRuntimeMeta(MetadataDao.getIdByNumber(sourceMainType.getName(), MetaCategory.Form), MetaCategory.Form);
            //获取所有控件集合
            List<ControlAp<?>> items = taskMeta.getItems();
//            DynamicPropertyCollection dynPropColl = (DynamicPropertyCollection) sourceMainType.getProperties();
            //数据包字段集合
            Map<String, IDataEntityProperty> fields = sourceMainType.getAllFields();
            //加载数据前清理赋值分录集合
            DynamicObjectCollection entrys = billDate.getDynamicObjectCollection("yem_entitycontainer");//实体主分录
            int count = model.getEntryRowCount("yem_entitycontainer");
            if (count > 0) {
                for (int i = count - 1; i >= 0; i--) {
                    model.deleteEntryRow("yem_entitycontainer", i);
                }
            }
            //处理元数据集合
            for (ControlAp<?> item : items) {
                //判断是否单据头
                if (item instanceof BillFormAp) {
                    int row = this.getView().getModel().createNewEntryRow("yem_entitycontainer");
                    model.setValue("yem_entitytype", "A", row);
                    model.setValue("yem_entityformid", item.getKey(), row);
                    model.setValue("yem_entityformname", item.getName().getLocaleValue(), row);
                    DynamicObject Entrys = entrys.get(0);
                    DynamicObjectCollection SubEntryfieldCol = Entrys.getDynamicObjectCollection("yem_entityfieldcol");
                    SubEntryfieldCol.clear();
                }
                // 判断当前控件是否属于FieldAp
                if (item instanceof FieldAp) {
                    DynamicObject Entrys = entrys.get(0);
                    DynamicObjectCollection SubEntryfieldCol = Entrys.getDynamicObjectCollection("yem_entityfieldcol");
//                    String qwe = fields.get(item.getKey()).getPropertyType().getSimpleName();
                    if (fields.get(item.getKey()) instanceof BasedataProp) {
                        BasedataProp field = (BasedataProp) fields.get(item.getKey());
                        IDataEntityType fieldType = field.getParent();
                        if (fieldType != null) {
                            if (fieldType.getName().equals(formid)) {
                                String name = field.getDisplayName().getLocaleValue_zh_CN();
                                String type = field.getFilterControlType();
                                String dynamicpropertyen = field.getBaseEntityId();
                                DynamicObject SubEntrynew = SubEntryfieldCol.addNew();
                                SubEntrynew.set("yem_erpfiledformid", item.getKey());
                                SubEntrynew.set("yem_erpfiledformname", name);
                                SubEntrynew.set("yem_filedtype", type);
                                SubEntrynew.set("yem_filedtypename", dynamicpropertyen);
                            }
                        }
                    } else if (fields.get(item.getKey()) instanceof FieldProp) {
                        FieldProp field = (FieldProp) fields.get(item.getKey());
                        IDataEntityType fieldType = field.getParent();
                        if (fieldType != null) {
                            if (fieldType.getName().equals(formid)) {
                                String name = field.getDisplayName().getLocaleValue_zh_CN();
                                String type = field.getFilterControlType();
                                DynamicObject SubEntrynew = SubEntryfieldCol.addNew();
                                SubEntrynew.set("yem_erpfiledformid", item.getKey());
                                SubEntrynew.set("yem_erpfiledformname", name);
                                SubEntrynew.set("yem_filedtype", type);
                            }
                        }
                    } else if (fields.get(item.getKey() + "_startdate") instanceof DateProp) {
                        DateProp fieldstart = (DateProp) fields.get(item.getKey() + "_startdate");
                        IDataEntityType fieldTypestart = fieldstart.getParent();
                        if (fieldTypestart != null) {
                            if (fieldTypestart.getName().equals(formid)) {
                                String name = fieldstart.getDisplayName().getLocaleValue_zh_CN();
                                String type = fieldstart.getFilterControlType();
                                DynamicObject SubEntrynew = SubEntryfieldCol.addNew();
                                SubEntrynew.set("yem_erpfiledformid", item.getKey() + "_startdate");
                                SubEntrynew.set("yem_erpfiledformname", name);
                                SubEntrynew.set("yem_filedtype", type);
                            }
                        }
                        DateProp fieldend = (DateProp) fields.get(item.getKey() + "_enddate");
                        IDataEntityType fieldTypeend = fieldend.getParent();
                        if (fieldTypeend != null) {
                            if (fieldTypeend.getName().equals(formid)) {
                                String name = fieldend.getDisplayName().getLocaleValue_zh_CN();
                                String type = fieldend.getFilterControlType();
                                DynamicObject SubEntrynew = SubEntryfieldCol.addNew();
                                SubEntrynew.set("yem_erpfiledformid", item.getKey() + "_enddate");
                                SubEntrynew.set("yem_erpfiledformname", name);
                                SubEntrynew.set("yem_filedtype", type);
                            }
                        }
                    } else if (fields.get(item.getKey()) instanceof MulBasedataProp) {
                        MulBasedataProp field = (MulBasedataProp) fields.get(item.getKey());
                        IDataEntityType fieldType = field.getParent();
                        if (fieldType != null) {
                            if (fieldType.getName().equals(formid)) {
                                String name = field.getDisplayName().getLocaleValue_zh_CN();
                                String type = field.getFilterControlType();
                                String dynamicpropertyen = field.getBaseEntityId();
                                DynamicObject SubEntrynew = SubEntryfieldCol.addNew();
                                SubEntrynew.set("yem_erpfiledformid", item.getKey());
                                SubEntrynew.set("yem_erpfiledformname", name);
                                SubEntrynew.set("yem_filedtype", type);
                            }
                        }
                    }
                }
                else if (item instanceof EntryAp) {
                    int row = this.getView().getModel().createNewEntryRow("yem_entitycontainer");
                    model.setValue("yem_entitytype", "B", row);
                    model.setValue("yem_entityformid", item.getKey(), row);
                    model.setValue("yem_entityformname", item.getName().getLocaleValue(), row);

                    DynamicObject Entrys = entrys.get(row);
                    DynamicObjectCollection SubEntryfieldCol = Entrys.getDynamicObjectCollection("yem_entityfieldcol");
                    SubEntryfieldCol.clear();
                    List<ControlAp<?>> Entryitems = ((EntryAp) item).getItems();
                    String Parentname = "";
                    for (ControlAp<?> entryitem : Entryitems) {
                        if (fields.get(entryitem.getKey()) instanceof BasedataProp) {
                            DynamicObject SubEntrynew = SubEntryfieldCol.addNew();
                            BasedataProp field = (BasedataProp) fields.get(entryitem.getKey());
                            IDataEntityType fieldType = field.getParent();
                            if (fieldType instanceof SubEntryType) {
                                if (!Parentname.isEmpty()) {
                                    IDataEntityType grandParent = fieldType.getParent();
                                    Parentname = grandParent.getName();
                                }
                            }
                            String name = field.getDisplayName().getLocaleValue_zh_CN();
                            String type = field.getFilterControlType();
                            String dynamicpropertyen = field.getBaseEntityId();
                            SubEntrynew.set("yem_erpfiledformid", entryitem.getKey());
                            SubEntrynew.set("yem_erpfiledformname", name);
                            SubEntrynew.set("yem_filedtype", type);
                            SubEntrynew.set("yem_filedtypename", dynamicpropertyen);
                        } else if (fields.get(entryitem.getKey()) instanceof FieldProp) {
                            DynamicObject SubEntrynew = SubEntryfieldCol.addNew();
                            FieldProp field = (FieldProp) fields.get(entryitem.getKey());
                            IDataEntityType fieldType = field.getParent();
                            if (fieldType instanceof SubEntryType) {
                                if (Parentname.isEmpty()) {
                                    IDataEntityType grandParent = fieldType.getParent();
                                    Parentname = grandParent.getName();
                                }
                            }
                            String name = field.getDisplayName().getLocaleValue_zh_CN();
                            String type = field.getFilterControlType();
                            SubEntrynew.set("yem_erpfiledformid", entryitem.getKey());
                            SubEntrynew.set("yem_erpfiledformname", name);
                            SubEntrynew.set("yem_filedtype", type);
                        } else if (fields.get(entryitem.getKey()) instanceof DateProp) {
                            DynamicObject SubEntrynew = SubEntryfieldCol.addNew();
                            DateProp field = (DateProp) fields.get(item.getKey());
                            IDataEntityType fieldType = field.getParent();
                            if (fieldType instanceof SubEntryType) {
                                if (Parentname.isEmpty()) {
                                    IDataEntityType grandParent = fieldType.getParent();
                                    Parentname = grandParent.getName();
                                }
                            }
                            String name = field.getDisplayName().getLocaleValue_zh_CN();
                            String type = field.getFilterControlType();

                            SubEntrynew.set("yem_erpfiledformid", item.getKey());
                            SubEntrynew.set("yem_erpfiledformname", name);
                            SubEntrynew.set("yem_filedtype", type);
                        }else if (fields.get(entryitem.getKey() + "_startdate") instanceof DateProp) {
                            DateProp fieldstart = (DateProp) fields.get(entryitem.getKey() + "_startdate");
                            IDataEntityType fieldTypestart = fieldstart.getParent();
                            if (fieldTypestart != null) {
                                if (fieldTypestart instanceof SubEntryType) {
                                    if (Parentname.isEmpty()) {
                                        IDataEntityType grandParent = fieldTypestart.getParent();
                                        Parentname = grandParent.getName();
                                    }
                                }
                                String name = fieldstart.getDisplayName().getLocaleValue_zh_CN();
                                String type = fieldstart.getFilterControlType();
                                DynamicObject SubEntrynew = SubEntryfieldCol.addNew();
                                SubEntrynew.set("yem_erpfiledformid", entryitem.getKey() + "_startdate");
                                SubEntrynew.set("yem_erpfiledformname", name);
                                SubEntrynew.set("yem_filedtype", type);
                            }
                            DateProp fieldend = (DateProp) fields.get(entryitem.getKey() + "_enddate");
                            IDataEntityType fieldTypeend = fieldend.getParent();
                            if (fieldTypeend != null) {
                                if (fieldTypeend instanceof SubEntryType) {
                                    if (Parentname.isEmpty()) {
                                        IDataEntityType grandParent = fieldTypeend.getParent();
                                        Parentname = grandParent.getName();
                                    }
                                }
                                String name = fieldend.getDisplayName().getLocaleValue_zh_CN();
                                String type = fieldend.getFilterControlType();
                                DynamicObject SubEntrynew = SubEntryfieldCol.addNew();
                                SubEntrynew.set("yem_erpfiledformid", entryitem.getKey() + "_enddate");
                                SubEntrynew.set("yem_erpfiledformname", name);
                                SubEntrynew.set("yem_filedtype", type);
                            }
                        }
                    }
                    model.setValue("yem_parentcontainer", Parentname, row);
                }
            }
        } else {
            int count = model.getEntryRowCount("yem_entitycontainer");
            if (count > 0) {
                for (int i = count - 1; i >= 0; i--) {
                    model.deleteEntryRow("yem_entitycontainer", i);
                }
            }
        }
    }

    /***
     * 重新加载事件
     */
    private void yem_reload() {
        IDataModel model = this.getModel();
        DynamicObject billDate = model.getDataEntity(true);
        DynamicObject yem_formid = (DynamicObject) model.getValue("yem_formid");
        if (null != yem_formid) {
            String formid = yem_formid.getString("number");
            //加载数据包
            MainEntityType sourceMainType = EntityMetadataCache.getDataEntityType(formid);
            FormMetadata taskMeta = (FormMetadata) MetadataDao.readRuntimeMeta(MetadataDao.getIdByNumber(sourceMainType.getName(), MetaCategory.Form), MetaCategory.Form);
            //获取所有控件集合
            List<ControlAp<?>> items = taskMeta.getItems();
            //数据包字段集合
            Map<String, IDataEntityProperty> fields = sourceMainType.getAllFields();
            DynamicObjectCollection yem_entitycontainers = billDate.getDynamicObjectCollection("yem_entitycontainer");
            Map<String, List<String>> EnyryEntityMap = new HashMap();
            if (yem_entitycontainers != null && yem_entitycontainers.getRowCount() > 0) {
                for (int u = 0; u < yem_entitycontainers.getRowCount(); u++) {
                    DynamicObject yem_entitycontainer = yem_entitycontainers.get(u);
                    if (yem_entitycontainer != null) {
                        String yem_entityformid = yem_entitycontainer.getString("yem_entityformid");
                        if (StringUtils.isNotEmpty(yem_entityformid)) {
                            List<String> EnyryEntityList = new ArrayList();
                            DynamicObjectCollection SubEntryfieldCols = yem_entitycontainer.getDynamicObjectCollection("yem_entityfieldcol");
                            if (SubEntryfieldCols != null && SubEntryfieldCols.getRowCount() > 0) {
                                for (int y = 0; y < SubEntryfieldCols.getRowCount(); y++) {
                                    DynamicObject SubEntryfieldCol = SubEntryfieldCols.get(y);
                                    if (SubEntryfieldCol != null) {
                                        String yem_erpfiledformid = SubEntryfieldCol.getString("yem_erpfiledformid");
                                        if (StringUtils.isNotEmpty(yem_erpfiledformid)) {
                                            EnyryEntityList.add(yem_erpfiledformid);
                                        }
                                    }
                                }

                            }
                            EnyryEntityMap.put(yem_entityformid, EnyryEntityList);
                        }
                    }
                }
            }
            //处理元数据集合
            int row = 0;
            String HeadKey = "";
            for (ControlAp<?> item : items) {
                if (item instanceof BillFormAp) {
                    if (yem_entitycontainers.getRowCount() > 0 && yem_entitycontainers != null) {
                        if (!EnyryEntityMap.containsKey(item.getKey())) {
                            for (int i = 0; i < yem_entitycontainers.getRowCount(); i++) {
                                DynamicObject yem_entitycontainer = yem_entitycontainers.get(i);
                                String yem_entityformid = yem_entitycontainer.getString("yem_entityformid");
                                String yem_entityformname = yem_entitycontainer.getString("yem_entityformname");
                                String yem_entitytype = yem_entitycontainer.getString("yem_entitytype");
                                if (yem_entitytype.equals("A") && !item.getKey().equals(yem_entityformid) && !item.getName().getLocaleValue().equals(yem_entityformname)) {
                                    row = this.getView().getModel().insertEntryRow("yem_entitycontainer", i);
                                    model.setValue("yem_entitytype", "A", row);
                                    model.setValue("yem_entityformid", item.getKey(), row);
                                    model.setValue("yem_entityformname", item.getName().getLocaleValue(), row);
                                    model.setValue("yem_isincremental", true, row);
                                    DynamicObject Entrys = yem_entitycontainers.get(row);
                                    DynamicObjectCollection SubEntryfieldCol = Entrys.getDynamicObjectCollection("yem_entityfieldcol");
                                    SubEntryfieldCol.clear();
                                    if (yem_entitytype.equals("A")) {
                                        model.setValue("yem_isdelete", true, i);
                                    }
                                } else if (yem_entitytype.equals("A")) {
                                    HeadKey = yem_entityformid;
                                }
                            }
                        } else {
                            for (int i = 0; i < yem_entitycontainers.getRowCount(); i++) {
                                DynamicObject yem_entitycontainer = yem_entitycontainers.get(i);
                                String yem_entityformid = yem_entitycontainer.getString("yem_entityformid");
                                String yem_entitytype = yem_entitycontainer.getString("yem_entitytype");
                                if (yem_entitytype.equals("A")) {
                                    HeadKey = yem_entityformid;
                                }
                            }
                        }
                    }
                }
                // 判断当前控件是否属于FieldAp
                if (item instanceof FieldAp) {
                    if (row > 0) {
                        DynamicObject Entrys = yem_entitycontainers.get(row);
                        DynamicObjectCollection SubEntryfieldCol = Entrys.getDynamicObjectCollection("yem_entityfieldcol");
                        if (fields.get(item.getKey()) instanceof BasedataProp) {
                            BasedataProp field = (BasedataProp) fields.get(item.getKey());
                            IDataEntityType fieldType = field.getParent();
                            if (fieldType != null) {
                                if (fieldType.getName().equals(formid)) {
                                    String name = field.getDisplayName().getLocaleValue_zh_CN();
                                    String type = field.getFilterControlType();
                                    String dynamicpropertyen = field.getBaseEntityId();
                                    DynamicObject SubEntrynew = SubEntryfieldCol.addNew();
                                    SubEntrynew.set("yem_erpfiledformid", item.getKey());
                                    SubEntrynew.set("yem_erpfiledformname", name);
                                    SubEntrynew.set("yem_filedtype", type);
                                    SubEntrynew.set("yem_filedtypename", dynamicpropertyen);
                                    SubEntrynew.set("yem_subisincremental", true);
                                }
                            }
                        } else if (fields.get(item.getKey()) instanceof FieldProp) {
                            FieldProp field = (FieldProp) fields.get(item.getKey());
                            IDataEntityType fieldType = field.getParent();
                            if (fieldType != null) {
                                if (fieldType.getName().equals(formid)) {
                                    String name = field.getDisplayName().getLocaleValue_zh_CN();
                                    String type = field.getFilterControlType();
                                    DynamicObject SubEntrynew = SubEntryfieldCol.addNew();
                                    SubEntrynew.set("yem_erpfiledformid", item.getKey());
                                    SubEntrynew.set("yem_erpfiledformname", name);
                                    SubEntrynew.set("yem_filedtype", type);
                                    SubEntrynew.set("yem_subisincremental", true);
                                }
                            }
                        } else if (fields.get(item.getKey() + "_startdate") instanceof DateProp) {
                            DateProp fieldstart = (DateProp) fields.get(item.getKey() + "_startdate");
                            IDataEntityType fieldTypestart = fieldstart.getParent();
                            if (fieldTypestart != null) {
                                if (fieldTypestart.getName().equals(formid)) {
                                    String name = fieldstart.getDisplayName().getLocaleValue_zh_CN();
                                    String type = fieldstart.getFilterControlType();
                                    DynamicObject SubEntrynew = SubEntryfieldCol.addNew();
                                    SubEntrynew.set("yem_erpfiledformid", item.getKey() + "_startdate");
                                    SubEntrynew.set("yem_erpfiledformname", name);
                                    SubEntrynew.set("yem_filedtype", type);
                                    SubEntrynew.set("yem_subisincremental", true);
                                }
                            }
                            DateProp fieldend = (DateProp) fields.get(item.getKey() + "_enddate");
                            IDataEntityType fieldTypeend = fieldend.getParent();
                            if (fieldTypeend != null) {
                                if (fieldTypeend.getName().equals(formid)) {
                                    String name = fieldend.getDisplayName().getLocaleValue_zh_CN();
                                    String type = fieldend.getFilterControlType();
                                    DynamicObject SubEntrynew = SubEntryfieldCol.addNew();
                                    SubEntrynew.set("yem_erpfiledformid", item.getKey() + "_enddate");
                                    SubEntrynew.set("yem_erpfiledformname", name);
                                    SubEntrynew.set("yem_filedtype", type);
                                    SubEntrynew.set("yem_subisincremental", true);
                                }
                            }
                        } else if (fields.get(item.getKey()) instanceof MulBasedataProp) {
                            MulBasedataProp field = (MulBasedataProp) fields.get(item.getKey());
                            IDataEntityType fieldType = field.getParent();
                            if (fieldType != null) {
                                if (fieldType.getName().equals(formid)) {
                                    String name = field.getDisplayName().getLocaleValue_zh_CN();
                                    String type = field.getFilterControlType();
                                    String dynamicpropertyen = field.getBaseEntityId();
                                    DynamicObject SubEntrynew = SubEntryfieldCol.addNew();
                                    SubEntrynew.set("yem_erpfiledformid", item.getKey());
                                    SubEntrynew.set("yem_erpfiledformname", name);
                                    SubEntrynew.set("yem_filedtype", type);
                                    SubEntrynew.set("yem_subisincremental", true);
                                }
                            }
                        }
                    } else {
                        List<String> A_EntryEntity = EnyryEntityMap.get(HeadKey);
                        DynamicObject Entrys = yem_entitycontainers.get(0);
                        DynamicObjectCollection SubEntryfieldCols = Entrys.getDynamicObjectCollection("yem_entityfieldcol");
                        if (A_EntryEntity != null && A_EntryEntity.size() > 0) {
                            if (fields.get(item.getKey()) instanceof BasedataProp) {
                                BasedataProp field = (BasedataProp) fields.get(item.getKey());
                                IDataEntityType fieldType = field.getParent();
                                if (fieldType != null) {
                                    if (fieldType.getName().equals(formid)) {
                                        String name = field.getDisplayName().getLocaleValue_zh_CN();
                                        String type = field.getFilterControlType();
                                        String dynamicpropertyen = field.getBaseEntityId();
                                        if (!A_EntryEntity.contains(item.getKey())) {
                                            DynamicObject SubEntrynew = SubEntryfieldCols.addNew();
                                            SubEntrynew.set("yem_erpfiledformid", item.getKey());
                                            SubEntrynew.set("yem_erpfiledformname", name);
                                            SubEntrynew.set("yem_filedtype", type);
                                            SubEntrynew.set("yem_filedtypename", dynamicpropertyen);
                                            SubEntrynew.set("yem_subisincremental", true);
                                        }
                                    }
                                }
                            } else if (fields.get(item.getKey()) instanceof FieldProp) {
                                FieldProp field = (FieldProp) fields.get(item.getKey());
                                IDataEntityType fieldType = field.getParent();
                                if (fieldType != null) {
                                    if (fieldType.getName().equals(formid)) {
                                        String name = field.getDisplayName().getLocaleValue_zh_CN();
                                        String type = field.getFilterControlType();
                                        if (!A_EntryEntity.contains(item.getKey())) {
                                            DynamicObject SubEntrynew = SubEntryfieldCols.addNew();
                                            SubEntrynew.set("yem_erpfiledformid", item.getKey());
                                            SubEntrynew.set("yem_erpfiledformname", name);
                                            SubEntrynew.set("yem_filedtype", type);
                                            SubEntrynew.set("yem_subisincremental", true);
                                        }
                                    }
                                }
                            } else if (fields.get(item.getKey() + "_startdate") instanceof DateProp) {
                                if (!A_EntryEntity.contains(item.getKey() + "_startdate")) {
                                    DateProp fieldstart = (DateProp) fields.get(item.getKey() + "_startdate");
                                    IDataEntityType fieldTypestart = fieldstart.getParent();
                                    if (fieldTypestart != null) {
                                        if (fieldTypestart.getName().equals(formid)) {
                                            String name = fieldstart.getDisplayName().getLocaleValue_zh_CN();
                                            String type = fieldstart.getFilterControlType();
                                            DynamicObject SubEntrynew = SubEntryfieldCols.addNew();
                                            SubEntrynew.set("yem_erpfiledformid", item.getKey() + "_startdate");
                                            SubEntrynew.set("yem_erpfiledformname", name);
                                            SubEntrynew.set("yem_filedtype", type);
                                            SubEntrynew.set("yem_subisincremental", true);

                                        }
                                    }
                                }
                                if (!A_EntryEntity.contains(item.getKey() + "_enddate")) {
                                    DateProp fieldend = (DateProp) fields.get(item.getKey() + "_enddate");
                                    IDataEntityType fieldTypeend = fieldend.getParent();
                                    if (fieldTypeend != null) {
                                        if (fieldTypeend.getName().equals(formid)) {
                                            String name = fieldend.getDisplayName().getLocaleValue_zh_CN();
                                            String type = fieldend.getFilterControlType();
                                            DynamicObject SubEntrynew = SubEntryfieldCols.addNew();
                                            SubEntrynew.set("yem_erpfiledformid", item.getKey() + "_enddate");
                                            SubEntrynew.set("yem_erpfiledformname", name);
                                            SubEntrynew.set("yem_filedtype", type);
                                            SubEntrynew.set("yem_subisincremental", true);
                                        }
                                    }
                                }
                            } else if (fields.get(item.getKey()) instanceof MulBasedataProp) {
                                MulBasedataProp field = (MulBasedataProp) fields.get(item.getKey());
                                IDataEntityType fieldType = field.getParent();
                                if (fieldType != null) {
                                    if (fieldType.getName().equals(formid)) {
                                        String name = field.getDisplayName().getLocaleValue_zh_CN();
                                        String type = field.getFilterControlType();
                                        String dynamicpropertyen = field.getBaseEntityId();
                                        DynamicObject SubEntrynew = SubEntryfieldCols.addNew();
                                        SubEntrynew.set("yem_erpfiledformid", item.getKey());
                                        SubEntrynew.set("yem_erpfiledformname", name);
                                        SubEntrynew.set("yem_filedtype", type);
                                        SubEntrynew.set("yem_subisincremental", true);
                                    }
                                }
                            }
                        }
                    }
                } else if (item instanceof EntryAp) {
                    if (!EnyryEntityMap.containsKey(item.getKey())) {
                        int Head_B_Row = this.getView().getModel().createNewEntryRow("yem_entitycontainer");
                        model.setValue("yem_entitytype", "B", Head_B_Row);
                        model.setValue("yem_entityformid", item.getKey(), Head_B_Row);
                        model.setValue("yem_entityformname", item.getName().getLocaleValue(), Head_B_Row);
                        model.setValue("yem_isincremental", true, Head_B_Row);
//                        DynamicObject Entrys = yem_entitycontainers.get(Head_B_Row);
//                        DynamicObjectCollection SubEntryfieldCol = Entrys.getDynamicObjectCollection("yem_entityfieldcol");
//                        SubEntryfieldCol.clear();
                        DynamicObject B_Entrys = yem_entitycontainers.get(Head_B_Row);
                        DynamicObjectCollection B_SubEntryfieldCol = B_Entrys.getDynamicObjectCollection("yem_entityfieldcol");
                        B_SubEntryfieldCol.clear();
                        List<ControlAp<?>> Entryitems = ((EntryAp) item).getItems();
                        String Parentname = "";
                        for (ControlAp<?> entryitem : Entryitems) {
                            if (fields.get(entryitem.getKey()) instanceof BasedataProp) {
                                DynamicObject SubEntrynew = B_SubEntryfieldCol.addNew();
                                BasedataProp field = (BasedataProp) fields.get(entryitem.getKey());
                                IDataEntityType fieldType = field.getParent();
                                if (fieldType instanceof SubEntryType) {
                                    if (!Parentname.isEmpty()) {
                                        IDataEntityType grandParent = fieldType.getParent();
                                        Parentname = grandParent.getName();
                                    }
                                }
                                String name = field.getDisplayName().getLocaleValue_zh_CN();
                                String type = field.getFilterControlType();
                                String dynamicpropertyen = field.getBaseEntityId();
                                SubEntrynew.set("yem_erpfiledformid", entryitem.getKey());
                                SubEntrynew.set("yem_erpfiledformname", name);
                                SubEntrynew.set("yem_filedtype", type);
                                SubEntrynew.set("yem_filedtypename", dynamicpropertyen);
                                SubEntrynew.set("yem_subisincremental", true);
                            } else if (fields.get(entryitem.getKey()) instanceof FieldProp) {
                                DynamicObject SubEntrynew = B_SubEntryfieldCol.addNew();
                                FieldProp field = (FieldProp) fields.get(entryitem.getKey());
                                IDataEntityType fieldType = field.getParent();
                                if (fieldType instanceof SubEntryType) {
                                    if (Parentname.isEmpty()) {
                                        IDataEntityType grandParent = fieldType.getParent();
                                        Parentname = grandParent.getName();
                                    }
                                }
                                String name = field.getDisplayName().getLocaleValue_zh_CN();
                                String type = field.getFilterControlType();
                                SubEntrynew.set("yem_erpfiledformid", entryitem.getKey());
                                SubEntrynew.set("yem_erpfiledformname", name);
                                SubEntrynew.set("yem_filedtype", type);
                                SubEntrynew.set("yem_subisincremental", true);
                            } else if (fields.get(entryitem.getKey()) instanceof DateProp) {
                                DynamicObject SubEntrynew = B_SubEntryfieldCol.addNew();
                                DateProp field = (DateProp) fields.get(item.getKey());
                                IDataEntityType fieldType = field.getParent();
                                if (fieldType instanceof SubEntryType) {
                                    if (Parentname.isEmpty()) {
                                        IDataEntityType grandParent = fieldType.getParent();
                                        Parentname = grandParent.getName();
                                    }
                                }
                                String name = field.getDisplayName().getLocaleValue_zh_CN();
                                String type = field.getFilterControlType();

                                SubEntrynew.set("yem_erpfiledformid", item.getKey());
                                SubEntrynew.set("yem_erpfiledformname", name);
                                SubEntrynew.set("yem_filedtype", type);
                                SubEntrynew.set("yem_subisincremental", true);
                            } else if (fields.get(entryitem.getKey() + "_startdate") instanceof DateProp) {
                                DateProp fieldstart = (DateProp) fields.get(entryitem.getKey() + "_startdate");
                                IDataEntityType fieldTypestart = fieldstart.getParent();
                                if (fieldTypestart != null) {
                                    if (fieldTypestart instanceof SubEntryType) {
                                        if (Parentname.isEmpty()) {
                                            IDataEntityType grandParent = fieldTypestart.getParent();
                                            Parentname = grandParent.getName();
                                        }
                                    }
                                    String name = fieldstart.getDisplayName().getLocaleValue_zh_CN();
                                    String type = fieldstart.getFilterControlType();
                                    DynamicObject SubEntrynew = B_SubEntryfieldCol.addNew();
                                    SubEntrynew.set("yem_erpfiledformid", entryitem.getKey() + "_startdate");
                                    SubEntrynew.set("yem_erpfiledformname", name);
                                    SubEntrynew.set("yem_filedtype", type);
                                    SubEntrynew.set("yem_subisincremental", true);
                                }
                                DateProp fieldend = (DateProp) fields.get(entryitem.getKey() + "_enddate");
                                IDataEntityType fieldTypeend = fieldend.getParent();
                                if (fieldTypeend != null) {
                                    if (fieldTypeend instanceof SubEntryType) {
                                        if (Parentname.isEmpty()) {
                                            IDataEntityType grandParent = fieldTypeend.getParent();
                                            Parentname = grandParent.getName();
                                        }
                                    }
                                    String name = fieldend.getDisplayName().getLocaleValue_zh_CN();
                                    String type = fieldend.getFilterControlType();
                                    DynamicObject SubEntrynew = B_SubEntryfieldCol.addNew();
                                    SubEntrynew.set("yem_erpfiledformid", entryitem.getKey() + "_enddate");
                                    SubEntrynew.set("yem_erpfiledformname", name);
                                    SubEntrynew.set("yem_filedtype", type);
                                    SubEntrynew.set("yem_subisincremental", true);
                                }
                            }
                        }
                        model.setValue("yem_parentcontainer", Parentname, row);
                    } else {
                        List<String> B_EntryEntity = EnyryEntityMap.get(item.getKey());
                        List<ControlAp<?>> Entryitems = ((EntryAp) item).getItems();
                        for (ControlAp<?> entryitem : Entryitems) {
                            if (yem_entitycontainers.getRowCount() > 0 && yem_entitycontainers != null) {
                                for (int i = 0; i < yem_entitycontainers.getRowCount(); i++) {
                                    DynamicObject yem_entitycontainer = yem_entitycontainers.get(i);
                                    String yem_entityformid = yem_entitycontainer.getString("yem_entityformid");
                                    if (item.getKey().equals(yem_entityformid)) {
                                        DynamicObjectCollection B_SubEntryfieldCol = yem_entitycontainer.getDynamicObjectCollection("yem_entityfieldcol");
                                        if (B_SubEntryfieldCol != null && B_SubEntryfieldCol.getRowCount() > 0) {
                                            if (fields.get(entryitem.getKey()) instanceof BasedataProp) {
                                                if (!B_EntryEntity.contains(entryitem.getKey())) {
                                                    DynamicObject SubEntrynew = B_SubEntryfieldCol.addNew();
                                                    BasedataProp field = (BasedataProp) fields.get(entryitem.getKey());
                                                    IDataEntityType fieldType = field.getParent();
                                                    String name = field.getDisplayName().getLocaleValue_zh_CN();
                                                    String type = field.getFilterControlType();
                                                    String dynamicpropertyen = field.getBaseEntityId();
                                                    SubEntrynew.set("yem_erpfiledformid", entryitem.getKey());
                                                    SubEntrynew.set("yem_erpfiledformname", name);
                                                    SubEntrynew.set("yem_filedtype", type);
                                                    SubEntrynew.set("yem_filedtypename", dynamicpropertyen);
                                                    SubEntrynew.set("yem_subisincremental", true);
                                                }
                                            } else if (fields.get(entryitem.getKey()) instanceof FieldProp) {
                                                if (!B_EntryEntity.contains(entryitem.getKey())) {
                                                    DynamicObject SubEntrynew = B_SubEntryfieldCol.addNew();
                                                    FieldProp field = (FieldProp) fields.get(entryitem.getKey());
                                                    IDataEntityType fieldType = field.getParent();
                                                    String name = field.getDisplayName().getLocaleValue_zh_CN();
                                                    String type = field.getFilterControlType();
                                                    SubEntrynew.set("yem_erpfiledformid", entryitem.getKey());
                                                    SubEntrynew.set("yem_erpfiledformname", name);
                                                    SubEntrynew.set("yem_filedtype", type);
                                                    SubEntrynew.set("yem_subisincremental", true);
                                                }
                                            } else if (fields.get(entryitem.getKey()) instanceof DateProp) {
                                                if (!B_EntryEntity.contains(entryitem.getKey())) {
                                                    DynamicObject SubEntrynew = B_SubEntryfieldCol.addNew();
                                                    DateProp field = (DateProp) fields.get(item.getKey());
                                                    IDataEntityType fieldType = field.getParent();
                                                    String name = field.getDisplayName().getLocaleValue_zh_CN();
                                                    String type = field.getFilterControlType();
                                                    SubEntrynew.set("yem_erpfiledformid", item.getKey());
                                                    SubEntrynew.set("yem_erpfiledformname", name);
                                                    SubEntrynew.set("yem_filedtype", type);
                                                    SubEntrynew.set("yem_subisincremental", true);
                                                }
                                            } else if (fields.get(entryitem.getKey() + "_startdate") instanceof DateProp) {
                                                if (!B_EntryEntity.contains(entryitem.getKey() + "_startdate")) {
                                                    DateProp fieldstart = (DateProp) fields.get(entryitem.getKey() + "_startdate");
                                                    IDataEntityType fieldTypestart = fieldstart.getParent();
                                                    if (fieldTypestart != null) {
                                                        String name = fieldstart.getDisplayName().getLocaleValue_zh_CN();
                                                        String type = fieldstart.getFilterControlType();
                                                        DynamicObject SubEntrynew = B_SubEntryfieldCol.addNew();
                                                        SubEntrynew.set("yem_erpfiledformid", entryitem.getKey() + "_startdate");
                                                        SubEntrynew.set("yem_erpfiledformname", name);
                                                        SubEntrynew.set("yem_filedtype", type);
                                                        SubEntrynew.set("yem_subisincremental", true);
                                                    }
                                                }
                                                if (!B_EntryEntity.contains(entryitem.getKey() + "_startdate")) {
                                                    DateProp fieldend = (DateProp) fields.get(entryitem.getKey() + "_enddate");
                                                    IDataEntityType fieldTypeend = fieldend.getParent();
                                                    if (fieldTypeend != null) {
                                                        String name = fieldend.getDisplayName().getLocaleValue_zh_CN();
                                                        String type = fieldend.getFilterControlType();
                                                        DynamicObject SubEntrynew = B_SubEntryfieldCol.addNew();
                                                        SubEntrynew.set("yem_erpfiledformid", entryitem.getKey() + "_enddate");
                                                        SubEntrynew.set("yem_erpfiledformname", name);
                                                        SubEntrynew.set("yem_filedtype", type);
                                                        SubEntrynew.set("yem_subisincremental", true);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    /***
     * 删除项检查
     */
    private void yem_dropitem() {
        IDataModel model = this.getModel();
        DynamicObject billDate = model.getDataEntity(true);
        DynamicObjectCollection yem_entitycontainers = billDate.getDynamicObjectCollection("yem_entitycontainer");
        if (yem_entitycontainers != null && yem_entitycontainers.getRowCount() > 0) {
            for (int i = yem_entitycontainers.getRowCount() - 1; i >= 0; i--) {
                DynamicObject yem_entitycontainer = yem_entitycontainers.get(i);
                if (yem_entitycontainer != null) {
                    String Key = yem_entitycontainer.getString("yem_entityformid");
                    if (StringUtils.isNotEmpty(Key)) {
                        if (!GetBillMate(Key)) {
                            model.deleteEntryRow("yem_entitycontainer", i);
                        } else {
                            DynamicObjectCollection yem_entityfieldcols = yem_entitycontainer.getDynamicObjectCollection("yem_entityfieldcol");
                            if (yem_entityfieldcols != null && yem_entityfieldcols.getRowCount() > 0) {
                                for (int o = yem_entityfieldcols.getRowCount() - 1; o >= 0; o--) {
                                    DynamicObject yem_entityfieldcol = yem_entityfieldcols.get(o);
                                    if (yem_entityfieldcol != null) {
                                        String EntryKey = yem_entityfieldcol.getString("yem_erpfiledformid");
                                        if (StringUtils.isNotEmpty(EntryKey)) {
                                            if (!GetBillMate(EntryKey)) {
                                                model.deleteEntryRow("yem_entityfieldcol", o);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }


    private Boolean GetBillMate(String EntityKey){
        int Isappearcount =0;
        Boolean Isappear =false;
        IDataModel model = this.getModel();
        DynamicObject billDate = model.getDataEntity(true);
        DynamicObject yem_formid = (DynamicObject) model.getValue("yem_formid");
        if (null != yem_formid) {
            String formid = yem_formid.getString("number");
            //加载数据包
            MainEntityType sourceMainType = EntityMetadataCache.getDataEntityType(formid);
            FormMetadata taskMeta = (FormMetadata) MetadataDao.readRuntimeMeta(MetadataDao.getIdByNumber(sourceMainType.getName(), MetaCategory.Form), MetaCategory.Form);
            //获取所有控件集合
            List<ControlAp<?>> items = taskMeta.getItems();
            //数据包字段集合
            Map<String, IDataEntityProperty> fields = sourceMainType.getAllFields();
            //处理元数据集合
            for (ControlAp<?> item : items) {
                //判断是否单据头
                if (item instanceof BillFormAp) {
                    if(item.getKey().equals(EntityKey)){
                        Isappearcount++;
                    }
                }
                // 判断当前控件是否属于FieldAp
                if (item instanceof FieldAp) {
                    if (fields.get(item.getKey()) instanceof BasedataProp) {
                        BasedataProp field = (BasedataProp) fields.get(item.getKey());
                        IDataEntityType fieldType = field.getParent();
                        if (fieldType != null) {
                            if (fieldType.getName().equals(formid)) {
                                if(item.getKey().equals(EntityKey)){
                                    Isappearcount++;
                                }
                            }
                        }
                    } else if (fields.get(item.getKey()) instanceof FieldProp) {
                        FieldProp field = (FieldProp) fields.get(item.getKey());
                        IDataEntityType fieldType = field.getParent();
                        if (fieldType != null) {
                            if (fieldType.getName().equals(formid)) {
                                if(item.getKey().equals(EntityKey)){
                                    Isappearcount++;
                                }
                            }
                        }
                    } else if (fields.get(item.getKey() + "_startdate") instanceof DateProp) {
                        DateProp fieldstart = (DateProp) fields.get(item.getKey() + "_startdate");
                        IDataEntityType fieldTypestart = fieldstart.getParent();
                        if (fieldTypestart != null) {
                            if (fieldTypestart.getName().equals(formid)) {
                                if(EntityKey.equals(item.getKey() + "_startdate")){
                                    Isappearcount++;
                                }
                            }
                        }
                        DateProp fieldend = (DateProp) fields.get(item.getKey() + "_enddate");
                        IDataEntityType fieldTypeend = fieldend.getParent();
                        if (fieldTypeend != null) {
                            if (fieldTypeend.getName().equals(formid)) {
                                if(EntityKey.equals(item.getKey() + "_enddate")){
                                    Isappearcount++;
                                }
                            }
                        }
                    } else if (fields.get(item.getKey()) instanceof MulBasedataProp) {
                        MulBasedataProp field = (MulBasedataProp) fields.get(item.getKey());
                        IDataEntityType fieldType = field.getParent();
                        if (fieldType != null) {
                            if (fieldType.getName().equals(formid)) {
                                if(item.getKey().equals(EntityKey)){
                                    Isappearcount++;
                                }
                            }
                        }
                    }
                } else if (item instanceof EntryAp) {
                    String EntryKry = item.getKey();
                    if(item.getKey().equals(EntityKey)){
                        Isappearcount++;
                    }
                    List<ControlAp<?>> Entryitems = ((EntryAp) item).getItems();
                    String Parentname = "";
                    for (ControlAp<?> entryitem : Entryitems) {
                        if (fields.get(entryitem.getKey()) instanceof BasedataProp) {
                            BasedataProp field = (BasedataProp) fields.get(entryitem.getKey());
                            IDataEntityType fieldType = field.getParent();
                            if(entryitem.getKey().equals(EntityKey)){
                                Isappearcount++;
                            }
                        } else if (fields.get(entryitem.getKey()) instanceof FieldProp) {
                            FieldProp field = (FieldProp) fields.get(entryitem.getKey());
                            IDataEntityType fieldType = field.getParent();
                            if(entryitem.getKey().equals(EntityKey)){
                                Isappearcount++;
                            }
                        } else if (fields.get(entryitem.getKey()) instanceof DateProp) {
                            DateProp field = (DateProp) fields.get(item.getKey());
                            IDataEntityType fieldType = field.getParent();
                            if(entryitem.getKey().equals(EntityKey)){
                                Isappearcount++;
                            }
                        }else if (fields.get(entryitem.getKey() + "_startdate") instanceof DateProp) {
                            DateProp fieldstart = (DateProp) fields.get(entryitem.getKey() + "_startdate");
                            IDataEntityType fieldTypestart = fieldstart.getParent();
                            if (fieldTypestart != null) {
                                if(EntityKey.equals(entryitem.getKey()+ "_startdate")){
                                    Isappearcount++;
                                }
                            }
                            DateProp fieldend = (DateProp) fields.get(entryitem.getKey() + "_enddate");
                            IDataEntityType fieldTypeend = fieldend.getParent();
                            if (fieldTypeend != null) {
                                if(EntityKey.equals(entryitem.getKey()+ "_enddate")){
                                    Isappearcount++;
                                }
                            }
                        }
                    }
                }
            }
        }
        if(Isappearcount>0){
            Isappear = true ;
        }
        return Isappear;
    }
}
