package com.yem.em.bd.etcmanual.from;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.EventObject;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yem.em.utils.BaseDataFilterUtils;
import com.yem.em.utils.CarShopUtils;
import com.yem.em.utils.EmptyUtils;
import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.bill.BillShowParameter;
import kd.bos.bill.OperationStatus;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.dataentity.resource.ResManager;
import kd.bos.entity.datamodel.IBillModel;
import kd.bos.entity.datamodel.events.AfterDeleteRowEventArgs;
import kd.bos.entity.datamodel.events.ChangeData;
import kd.bos.entity.datamodel.events.PropertyChangedArgs;
import kd.bos.entity.tree.TreeNode;
import kd.bos.form.CloseCallBack;
import kd.bos.form.FormShowParameter;
import kd.bos.form.IFormView;
import kd.bos.form.IPageCache;
import kd.bos.form.ShowFormHelper;
import kd.bos.form.ShowType;
import kd.bos.form.control.Control;
import kd.bos.form.control.EntryGrid;
import kd.bos.form.control.Label;
import kd.bos.form.control.TreeView;
import kd.bos.form.control.events.EntryGridBindDataEvent;
import kd.bos.form.control.events.EntryGridBindDataListener;
import kd.bos.form.control.events.ItemClickEvent;
import kd.bos.form.control.events.ItemClickListener;
import kd.bos.form.control.events.TreeNodeClickListener;
import kd.bos.form.control.events.TreeNodeEvent;
import kd.bos.form.events.AfterDoOperationEventArgs;
import kd.bos.form.events.BeforeClosedEvent;
import kd.bos.form.events.ClosedCallBackEvent;
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.user.UserServiceHelper;

/**
 * @author 夜子
 * @ClassName EtcmanualEdit
 * @description: 设备电子目录表单插件
 * @date 2023年07月25日
 * @version: 1.0
 */
public class EtcmanualEdit extends AbstractBillPlugIn
        implements TreeNodeClickListener, ItemClickListener, EntryGridBindDataListener{

    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        TreeView treeView = this.getView().getControl("yem_treeviewap");
        treeView.addTreeNodeClickListener(this);
        this.addItemClickListeners("yem_advcontoolbarap");
        for (int i = 0; i < 30; i++) {
            Label lab = this.getView().getControl("yem_labelap" + i);
            lab.addClickListener(this);
        }
    }

    @Override
    public void afterBindData(EventObject e) {
        super.afterBindData(e);
        String em_ecataloggroup_Key = "yem_em_ecataloggroup";
        String em_ecatalog_Key = "yem_em_ecatalog";
        FormShowParameter showParameter = this.getView().getFormShowParameter();
        Map<String, Object> customParas = showParameter.getCustomParams();
        if (customParas.containsKey("parentpageid")) {
            IPageCache cache = this.getPageCache();
            cache.put("parentpageid", customParas.get("parentpageid").toString());
        }
        if (customParas.containsKey("orgId")) {
            IPageCache cache = this.getPageCache();
            cache.put("orgId", customParas.get("orgId").toString());
        }

        RefreshControl();
        DynamicObject yem_brandmodel = (DynamicObject) this.getModel().getValue("yem_brandmodel");
        if (yem_brandmodel != null) {
            String yem_picture = yem_brandmodel.getString("yem_picture");
            this.getModel().setValue("yem_picture", yem_picture);
        }
        IBillModel billModel = (IBillModel) this.getModel();
        TreeView tv = this.getView().getControl("yem_treeviewap");
        tv.deleteAllNodes();
        String etcId = billModel.getPKValue().toString();
        String name = this.getView().getModel().getValue("name").toString();
        DynamicObject useorg = (DynamicObject) this.getModel().getValue("useorg");
        if (EmptyUtils.isNotEmpty(this.getPageCache().get("orgId"))) {
            useorg = BusinessDataServiceHelper.loadSingle(this.getPageCache().get("orgId"), "bos_adminorg");
        }
        final String rootId = "0";
        if (!etcId.equals("0")) {
            QFilter _qFilter = new QFilter("masterid", QCP.equals, Long.parseLong(etcId));
            DynamicObject[] etcmanualsCol = BusinessDataServiceHelper.load("yem_em_etcmanual",
                    "id,number,name,masterid,createorg,yem_resourc", new QFilter[] { _qFilter });
            if (etcmanualsCol.length > 0) {
                if (etcmanualsCol.length == 1) {
                    etcId = etcmanualsCol[0].getPkValue().toString();
                } else {
                    for (DynamicObject etcmanual : etcmanualsCol) {
                        DynamicObject createorg = etcmanual.getDynamicObject("createorg");
                        if ("100000".equals(createorg.getPkValue().toString())) {
                            etcId = etcmanual.getPkValue().toString();
                        }
                    }
                }
            }
            DynamicObject etcmanual  = BusinessDataServiceHelper.loadSingle(etcId,"yem_em_etcmanual");
            if(etcmanual.getBoolean("yem_resourc")){
                 em_ecataloggroup_Key = "yem_em_ecataloggroup_res";
                 em_ecatalog_Key = "yem_em_ecatalog_res";
            }else{
                em_ecataloggroup_Key = "yem_em_ecataloggroup";
                em_ecatalog_Key = "yem_em_ecatalog";
            }
            this.getView().getFormShowParameter().setCustomParam("em_ecataloggroup_Key",em_ecataloggroup_Key);
            this.getView().getFormShowParameter().setCustomParam("em_ecatalog_Key",em_ecatalog_Key);

            QFilter qFilter = new QFilter("yem_etcmanualid", QCP.equals, etcId);
            QFilter qFilter1 = new QFilter("enable", QCP.equals, "1");
            QFilter qFilter2 = new QFilter("parent", QCP.equals, 0);
            QFilter qFilter3 = new QFilter("status", QCP.equals, "C");
            DynamicObject etcmanualsub = BusinessDataServiceHelper.loadSingle(em_ecataloggroup_Key, "id,number,name",
                    new QFilter[] { qFilter, qFilter1, qFilter2, qFilter3 });
            if (etcmanualsub != null) {
                TreeNode rootNode = new TreeNode(null, etcmanualsub.getPkValue().toString(), name, true);
                rootNode.setIsOpened(true);
                constructorData(rootNode, etcId, etcmanualsub.getPkValue(), useorg);
                tv.addNode(rootNode);
                List<TreeNode> list = rootNode.getChildren();
                if (list.size() > 0) {
                    TreeNode node = GetLastNode(rootNode.getChildren());
                    if (node != null) {
                        node.setExpend(true);
                        tv.checkNode(node);
                        tv.focusNode(node);
                        tv.showNode(node.getId());
                        SetTreeNode(node.getId());
                    } else {
                        tv.checkNode(rootNode.getChildren().get(0));
                        tv.focusNode(rootNode.getChildren().get(0));
                        SetTreeNode(rootNode.getChildren().get(0).getId());
                    }
                } else {
                    tv.checkNode(rootNode);
                    tv.focusNode(rootNode);
                    SetTreeNode(rootNode.getId());
                }
            } else {
                TreeNode rootNode = new TreeNode(null, etcId, name, true);
                rootNode.setIsOpened(true);
                if (rootNode.getChildren().size() > 0) {
                    tv.addNode(rootNode.getChildren().get(0));
                    tv.checkNode(rootNode);
                } else {
                    tv.addNode(rootNode);
                    tv.checkNode(rootNode);
                }
            }
        } else {
            TreeNode rootNode = new TreeNode(null, rootId,
                    ResManager.loadKDString("无数据", "EtcmanualEdit_0", "bfcl_equipment"), true);
            tv.addNode(rootNode);
            tv.checkNode(rootNode);
        }
        this.getModel().setValue("yem_selecttreenode", rootId);
    }

    private TreeNode GetLastNode(List<TreeNode> list) {
        TreeNode node = null;
        if (list.size() > 1) {
            if (list.get(0).getChildren().size() > 0) {
                node = GetLastNode(list.get(0).getChildren());
            } else {
                node = list.get(0);
            }

        } else if (list.size() == 1) {
            node = list.get(0);
        }
        return node;
    }

    /**
     * 构建树型控件并绑定数据
     *
     * @param rootNode
     *            树节点
     * @param pkValue
     *            上级节点编码
     * @param useorg
     *            编码
     * @param etcId
     *            名称
     */
    private void constructorData(TreeNode rootNode, String etcId, Object pkValue, DynamicObject useorg) {
        DynamicObject etcmanual =  this.getModel().getDataEntity();
        String em_ecataloggroup_Key = "yem_em_ecataloggroup";
        String em_ecatalog_Key = "yem_em_ecatalog";
        if(etcmanual.getBoolean("yem_resourc")){
            em_ecataloggroup_Key = "yem_em_ecataloggroup_res";
            em_ecatalog_Key = "yem_em_ecatalog_res";
        }else{
            em_ecataloggroup_Key = "yem_em_ecataloggroup";
            em_ecatalog_Key = "yem_em_ecatalog";
        }

        QFilter qFilter1 = new QFilter("yem_etcmanualid", QCP.equals, etcId);
        QFilter qFilter2 = new QFilter("parent", QCP.equals, pkValue);
        QFilter qFilter3 = new QFilter("enable", QCP.equals, "1");
        QFilter qFilter4 = new QFilter("status", QCP.equals, "C");
        DynamicObject[] etcmanualsub = BusinessDataServiceHelper.load(em_ecataloggroup_Key, "id,number,name",
                new QFilter[] { qFilter2, qFilter1, qFilter3, qFilter4 });
        for (DynamicObject obj : etcmanualsub) {

            QFilter qFilter = new QFilter("masterid", QCP.equals, obj.getPkValue());
            DynamicObject[] ecataloggroups = BusinessDataServiceHelper.load(em_ecataloggroup_Key,
                    "id,number,name,createorg", new QFilter[]{qFilter, qFilter4});
            if (ecataloggroups.length > 0) {
                if (ecataloggroups.length == 1) {
                    TreeNode tn = new TreeNode(pkValue.toString(), obj.getPkValue().toString(),
                            obj.getString("name"), true);
                    tn.setIsOpened(false);
                    constructorData(tn, etcId, obj.getPkValue(), useorg);
                    rootNode.addChild(tn);
                } else {
                    for (DynamicObject ecataloggroup : ecataloggroups) {
                        DynamicObject createorg = ecataloggroup.getDynamicObject("createorg");
                        if (useorg.getPkValue().toString().equals(createorg.getPkValue().toString())) {
                            TreeNode tn = new TreeNode(pkValue.toString(), obj.getPkValue().toString(),
                                    ecataloggroup.getString("name"), true);
                            tn.setIsOpened(false);
                            constructorData(tn, etcId, obj.getPkValue(), useorg);
                            rootNode.addChild(tn);
                        }
                    }
                }
            }
        }
    }

    @Override
    public void treeNodeClick(TreeNodeEvent evt) {
        TreeNodeClickListener.super.treeNodeClick(evt);
        TreeView treeView = (TreeView) evt.getSource();
        if (treeView.getKey().equals("yem_treeviewap")) {
            String selectNodeId = evt.getNodeId().toString();
            SetTreeNode(selectNodeId);
        }
    }

    private void SetTreeNode(String selectNodeId) {
        DynamicObject etcmanual =  this.getModel().getDataEntity();
        String em_ecataloggroup_Key = "yem_em_ecataloggroup";
        String em_ecatalog_Key = "yem_em_ecatalog";
        if(etcmanual.getBoolean("yem_resourc")){
            em_ecataloggroup_Key = "yem_em_ecataloggroup_res";
            em_ecatalog_Key = "yem_em_ecatalog_res";
        }else{
            em_ecataloggroup_Key = "yem_em_ecataloggroup";
            em_ecatalog_Key = "yem_em_ecatalog";
        }

        this.getModel().setValue("yem_isnew", false);
        this.getModel().setValue("yem_selecttreenode", selectNodeId);
        this.getView().getModel().deleteEntryData("yem_entryentity");
        this.getModel().setValue("yem_ecataloggroup", 0);
        this.getModel().setValue("yem_picture1", null);

//        this.getModel().setValue("yem_unit", 0);
//        this.getModel().setValue("yem_seq", "");
//        this.getModel().setValue("yem_qty", 0);
//        this.getModel().setValue("yem_price", 0);
//        this.getModel().setValue("yem_remark", "");

        IBillModel billModel = (IBillModel) this.getModel();
        String orgId = "100000";
        if (EmptyUtils.isNotEmpty(this.getPageCache().get("orgId"))) {
            orgId = this.getPageCache().get("orgId");
        }
        QFilter qFilter1 = new QFilter("group.id", QCP.equals, Long.parseLong(selectNodeId));
        QFilter qFilter2 = new QFilter("yem_etcmanualid", QCP.equals, billModel.getPKValue().toString());
        QFilter qFilter7 = new QFilter("enable", QCP.equals, "1");
        QFilter qFilter8 = new QFilter("yem_ispart", QCP.equals, true);
        QFilter qFilter9 = new QFilter("status", QCP.equals, "C");
        DynamicObject ecatalog = BusinessDataServiceHelper.loadSingle(em_ecatalog_Key, "id,number,name",
                new QFilter[] { qFilter2, qFilter1, qFilter7, qFilter8, qFilter9 ,});
        if (ecatalog != null) {
            ecatalog = BusinessDataServiceHelper.loadSingle(ecatalog.getPkValue(), em_ecatalog_Key);
            if(em_ecatalog_Key.equals("yem_em_ecatalog_res")){
                String yem_partid=  ecatalog.getDynamicObject("yem_ecatalog").getString("yem_partid");
                this.getModel().setValue("yem_ecataloggroup",yem_partid);
            }else{
                String yem_partid=  ecatalog.getString("yem_partid");
                this.getModel().setValue("yem_ecataloggroup",yem_partid);
            }
            String yem_picture = ecatalog.getString("yem_picture");
            this.getModel().setValue("yem_picture1", yem_picture);


//            DynamicObject unit = ecatalog.getDynamicObject("yem_unit");
//            if (unit != null) {
//                this.getModel().setValue("yem_unit", unit.getPkValue());
//            }
//            this.getModel().setValue("yem_seq", ecatalog.getString("yem_atlasno"));
//            this.getModel().setValue("yem_qty", ecatalog.getBigDecimal("yem_qty"));
//            this.getModel().setValue("yem_price", ecatalog.getBigDecimal("yem_price"));
//            this.getModel().setValue("yem_remark", ecatalog.getString("yem_remark"));
            List<Long> groupIds = new ArrayList<Long>();
            groupIds.add(Long.valueOf(selectNodeId));
            getAllGroupPkValues(billModel.getPKValue().toString(), selectNodeId, orgId, groupIds);
            constructorData2(billModel.getPKValue().toString(), groupIds, orgId);
        }else{
            DynamicObject etcmanualsub  = BusinessDataServiceHelper.loadSingle( Long.parseLong(selectNodeId), em_ecataloggroup_Key);
            if(em_ecataloggroup_Key.equals("yem_em_ecataloggroup_res")){
                DynamicObject yem_ecataloggroup = etcmanualsub.getDynamicObject("yem_ecataloggroup");
                yem_ecataloggroup  = BusinessDataServiceHelper.loadSingle( yem_ecataloggroup.getPkValue(), yem_ecataloggroup.getDynamicObjectType().getName());
                String yem_picture = yem_ecataloggroup.getString("yem_picture");
                this.getModel().setValue("yem_picture1", yem_picture);
            }else{
                this.getModel().setValue("yem_ecataloggroup",etcmanualsub);
                String yem_picture = etcmanualsub.getString("yem_picture");
                this.getModel().setValue("yem_picture1", yem_picture);
            }
        }
        RefreshControl();
    }

    private void getAllGroupPkValues(String etcmanualid, String groupId, String orgId, List<Long> groupIds) {
        DynamicObject etcmanual =  this.getModel().getDataEntity();
        String em_ecataloggroup_Key = "yem_em_ecataloggroup";
        String em_ecatalog_Key = "yem_em_ecatalog";
        if(etcmanual.getBoolean("yem_resourc")){
            em_ecataloggroup_Key = "yem_em_ecataloggroup_res";
            em_ecatalog_Key = "yem_em_ecatalog_res";
        }else{
            em_ecataloggroup_Key = "yem_em_ecataloggroup";
            em_ecatalog_Key = "yem_em_ecatalog";
        }

        QFilter qFilter = new QFilter("yem_etcmanualid", QCP.equals, etcmanualid);
        QFilter qFilter1 = new QFilter("parent", QCP.equals, Long.parseLong(groupId));
        QFilter qFilter2 = new QFilter("enable", QCP.equals, "1");
        QFilter qFilter4 = new QFilter("status", QCP.equals, "C");
        QFilter qFilter3 = BaseDataFilterUtils.getBaseDataFilter(em_ecataloggroup_Key, Long.parseLong(orgId));
        DynamicObject[] ecataloggroupObjs = BusinessDataServiceHelper.load(em_ecataloggroup_Key, "id,number,name",
                new QFilter[] { qFilter, qFilter1, qFilter2, qFilter3, qFilter4 });
        for (DynamicObject dy : ecataloggroupObjs) {
            if (!groupIds.contains(Long.valueOf(dy.getPkValue().toString()))) {
                groupIds.add(Long.valueOf(dy.getPkValue().toString()));
            }
            getAllGroupPkValues(etcmanualid, dy.getPkValue().toString(), orgId, groupIds);
        }
    }

    private void constructorData2(String etcmanualid, List<Long> groupIds, String orgId) {
        DynamicObject etcmanual =  this.getModel().getDataEntity();
        String em_ecataloggroup_Key = "yem_em_ecataloggroup";
        String em_ecatalog_Key = "yem_em_ecatalog";
        if(etcmanual.getBoolean("yem_resourc")){
            em_ecataloggroup_Key = "yem_em_ecataloggroup_res";
            em_ecatalog_Key = "yem_em_ecatalog_res";
        }else{
            em_ecataloggroup_Key = "yem_em_ecataloggroup";
            em_ecatalog_Key = "yem_em_ecatalog";
        }

        int row = 0;
        QFilter qFilter = new QFilter("yem_etcmanualid", QCP.equals, etcmanualid);
        QFilter qFilter1 = new QFilter("group", QCP.in, groupIds);
        QFilter qFilter2 = new QFilter("yem_ispart", QCP.equals, false);
        QFilter qFilter3 = new QFilter("enable", QCP.equals, "1");
        QFilter qFilter6 = new QFilter("status", QCP.equals, "C");
        DynamicObject[] ecatalogObjs = BusinessDataServiceHelper.load(em_ecatalog_Key, "id,number,name",
                new QFilter[] { qFilter, qFilter1, qFilter2, qFilter3, qFilter6 });
        for (DynamicObject dy : ecatalogObjs) {
            dy = BusinessDataServiceHelper.loadSingle(dy.getPkValue(), em_ecatalog_Key);
            if(em_ecatalog_Key.equals("yem_em_ecatalog_res")){
                dy = BusinessDataServiceHelper.loadSingle(dy.getDynamicObject("yem_ecatalog").getPkValue(), "yem_em_ecatalog");
            }

            QFilter qFilter4 = new QFilter("masterid", QCP.equals, dy.getPkValue());
            DynamicObject[] ecatalogs = BusinessDataServiceHelper.load("yem_em_ecatalog",
                    "id,number,name,createorg", new QFilter[]{qFilter4, qFilter6});
            if (ecatalogs.length > 0) {
                if (ecatalogs.length == 1) {
                    this.getView().getModel().createNewEntryRow("yem_entryentity");
                    this.getModel().setValue("yem_entryid", dy.getPkValue(), row);
                    this.getModel().setValue("yem_partno", dy.getPkValue(), row);
//                        DynamicObject materielsub = dy.getDynamicObject("yem_materiel");
//                        if (materielsub != null) {
//                            this.getModel().setValue("yem_materielsub", materielsub.getPkValue(), row);
//                        }
                    DynamicObject unitsub = dy.getDynamicObject("yem_unit");
                    if (unitsub != null) {
                        this.getModel().setValue("yem_unitsub", unitsub.getPkValue(), row);
                    }
                    this.getModel().setValue("yem_seqsub", dy.getString("yem_atlasno"), row);
                    this.getModel().setValue("yem_qtysub", dy.getBigDecimal("yem_qty"), row);
                    this.getModel().setValue("yem_pricesub", dy.getBigDecimal("yem_price"), row);
                    this.getModel().setValue("yem_remarksub", dy.getString("yem_remark"), row);
//                    QFilter qFilter5 = new QFilter("yem_originalsystemcode", QCP.equals,
//                            dy.getPkValue().toString());
//                    DynamicObject[] replacepart = BusinessDataServiceHelper.load("yem_em_replaceparts",
//                            "id,number,name,yem_originalsystemcode", new QFilter[]{qFilter5});
//                    if (replacepart.length > 0) {
//                        this.getModel().setValue("yem_isreplacepart", Boolean.TRUE, row);
//                    }
                    row++;
                } else {
                    for (DynamicObject _ecatalog : ecatalogs) {
                        _ecatalog = BusinessDataServiceHelper.loadSingle(_ecatalog.getPkValue(), em_ecatalog_Key);
                        DynamicObject createorg = _ecatalog.getDynamicObject("createorg");
                        if (orgId.equals(createorg.getPkValue().toString())) {
                            this.getView().getModel().createNewEntryRow("yem_entryentity");
                            this.getModel().setValue("yem_entryid", _ecatalog.getPkValue(), row);
                            this.getModel().setValue("yem_partno", _ecatalog.getPkValue(), row);
//                                DynamicObject materielsub = _ecatalog.getDynamicObject("yem_materiel");
//                                if (materielsub != null) {
//                                    this.getModel().setValue("yem_materielsub", materielsub.getPkValue(), row);
//                                }
                            DynamicObject unitsub = _ecatalog.getDynamicObject("yem_unit");
                            if (unitsub != null) {
                                this.getModel().setValue("yem_unitsub", unitsub.getPkValue(), row);
                            }
                            this.getModel().setValue("yem_seqsub", _ecatalog.getString("yem_atlasno"), row);
                            this.getModel().setValue("yem_qtysub", _ecatalog.getBigDecimal("yem_qty"), row);
                            this.getModel().setValue("yem_pricesub", _ecatalog.getBigDecimal("yem_price"), row);
                            this.getModel().setValue("yem_remarksub", _ecatalog.getString("yem_remark"), row);
                            row++;
                        }
                    }
                }
            }
        }
    }

    @Override
    public void afterDeleteRow(AfterDeleteRowEventArgs e) {
        super.afterDeleteRow(e);
        String name = e.getEntryProp().getName();
        if (name.equals("yem_entryentity")) {
            RefreshControl();
        }
    }

    private void RefreshControl() {
        for (int i = 0; i < 30; i++) {
            this.getView().setVisible(false, "yem_labelap" + i);
        }
        DynamicObject dataEntity = this.getModel().getDataEntity(true);
        DynamicObjectCollection entryentityList = dataEntity.getDynamicObjectCollection("yem_entryentity");
        for (int i = 0; i < entryentityList.size(); i++) {
            if (i >= 29)
                return;
            String seq = entryentityList.get(i).getString("yem_seqsub").toString();
            if (EmptyUtils.isNotEmpty(seq)) {
                this.getView().setVisible(true, "yem_labelap" + i);
                Label lab = this.getView().getControl("yem_labelap" + i);
                lab.setText(seq);
            }
        }
    }

    @Override
    public void propertyChanged(PropertyChangedArgs e) {
        super.propertyChanged(e);
        ChangeData[] changeData = e.getChangeSet();
        Object newValue = changeData[0].getNewValue();
        int row = changeData[0].getRowIndex();
        String key = e.getProperty().getName();
        if (key.equals("yem_seqsub")) {
            RefreshControl();
        }
//        if (key.equals("yem_materielsub")) {
//            if (newValue == null) {
//                return;
//            }
//            if (((DynamicObject) newValue).getDynamicObject("baseunit") != null) {
//                this.getModel().setValue("yem_unitsub",
//                        ((DynamicObject) newValue).getDynamicObject("baseunit").getPkValue(), row);
//            }
//        }
    }

    @Override
    public void beforeClosed(BeforeClosedEvent e) {
        super.beforeClosed(e);
        e.setCheckDataChange(false);
    }

    @Override
    public void entryGridBindData(EntryGridBindDataEvent e) {
        EntryGridBindDataListener.super.entryGridBindData(e);
        EntryGrid entryGrid = (EntryGrid) e.getSource();
        if (entryGrid.getEntryKey().equals("yem_entryentity")) {
            String index = this.getView().getPageCache().get("labelseq");
            if (EmptyUtils.isNotEmpty(index)) {
                entryGrid = this.getControl("yem_entryentity");
                DynamicObject dataEntity = this.getModel().getDataEntity(true);
                DynamicObjectCollection entryentityList = dataEntity.getDynamicObjectCollection("yem_entryentity");
                for (int i = 0; i < entryentityList.size(); i++) {
                    int[] rows = { i };
                    if (i == Integer.valueOf(index)) {
                        entryGrid.setRowBackcolor("#87CEFA", rows);
                    } else {
                        entryGrid.setRowBackcolor("#FFFFFF", rows);
                    }
                }
            }
        }
    }

    @Override
    public void click(EventObject evt) {
        super.click(evt);
        Control source = (Control) evt.getSource();
        String key = source.getKey();
        if (key.startsWith("yem_labelap")) {// 图册标记
            String index = key.substring(11);
            this.getView().getPageCache().remove("labelseq");
            this.getView().getPageCache().put("labelseq", index);
            EntryGrid entryGrid = this.getControl("yem_entryentity");
            DynamicObject dataEntity = this.getModel().getDataEntity(true);
            DynamicObjectCollection entryentityList = dataEntity.getDynamicObjectCollection("yem_entryentity");
            for (int i = 0; i < entryentityList.size(); i++) {
                int[] rows = { i };
                if (i == Integer.valueOf(index)) {
                    entryGrid.setRowBackcolor("#87CEFA", rows);
                } else {
                    entryGrid.setRowBackcolor("#FFFFFF", rows);
                }
            }
        }
    }

    @Override
    public void itemClick(ItemClickEvent evt) {
        super.itemClick(evt);
        String itemKey = evt.getItemKey();
        IBillModel billModel = (IBillModel) this.getModel();
        if (itemKey.equals("addreplacepart")) {// 新增替换件
            BillShowParameter bsp = new BillShowParameter();
            bsp.setFormId("yem_em_ecatalog");
            bsp.setStatus(OperationStatus.ADDNEW);
            bsp.setParentPageId(this.getView().getPageId());
            bsp.setCustomParam("etcmanualid", billModel.getPKValue().toString());
            bsp.setCustomParam("type", "replacepart");
            bsp.getOpenStyle().setShowType(ShowType.MainNewTabPage);
            this.getView().showForm(bsp);
        }
        if (itemKey.equals("newreplacepart")) {// 设置替换件
            EntryGrid entryGrid = this.getControl("yem_entryentity");
            int[] selRows = entryGrid.getEntryState().getSelectedRows();
            if (selRows.length == 1) {
                for (int row : selRows) {
                    int focusRow = row;
                    DynamicObject dataEntity = billModel.getDataEntity(true);
                    DynamicObjectCollection entryentitys = dataEntity.getDynamicObjectCollection("yem_entryentity");
                    DynamicObject entryentity = entryentitys.get(focusRow);
                    DynamicObject materiel = entryentity.getDynamicObject("yem_materielsub");
                    if (materiel != null) {
                        BillShowParameter openParam = new BillShowParameter();
                        openParam.setFormId("yem_em_replaceparts");
                        openParam.getOpenStyle().setShowType(ShowType.MainNewTabPage);
                        openParam.setCustomParam("materiel", materiel.getPkValue());
                        openParam.setCustomParam("entryid", entryentity.getString("yem_entryid"));
                        openParam.setParentPageId(this.getView().getPageId());
                        openParam.setStatus(OperationStatus.ADDNEW);
                        this.getView().showForm(openParam);
                    }
                }
            } else {
                this.getView().showErrorNotification(
                        ResManager.loadKDString("只允许选择一条数据进行设置！", "EtcmanualEdit_8", "bfcl_equipment"));
            }
        }
        if (itemKey.equals("viewreplacepart")) {// 查询替换件
            EntryGrid entryGrid = this.getControl("yem_entryentity");
            int[] selRows = entryGrid.getEntryState().getSelectedRows();
            if (selRows.length == 1) {
                for (int row : selRows) {
                    int focusRow = row;
                    DynamicObject dataEntity = billModel.getDataEntity(true);
                    DynamicObjectCollection entryentitys = dataEntity.getDynamicObjectCollection("yem_entryentity");
                    DynamicObject entryentity = entryentitys.get(focusRow);
                    String entryid = entryentity.getString("yem_entryid");
                    if (entryid != null) {
                        ListShowParameter listShowParameter = ShowFormHelper.createShowListForm("yem_em_replaceparts",
                                false);
                        QFilter qFilter2 = new QFilter("yem_entryid", QCP.equals, entryid);
                        listShowParameter.getListFilterParameter().setFilter(qFilter2);
                        this.getView().showForm(listShowParameter);
                    }
                }
            } else {
                this.getView().showErrorNotification(
                        ResManager.loadKDString("只允许选择一条数据进行查看！", "EtcmanualEdit_9", "bfcl_equipment"));
            }
        }
    }

    @Override
    public void afterDoOperation(AfterDoOperationEventArgs e) {
        super.afterDoOperation(e);
        String operateKey = e.getOperateKey();
        IBillModel billModel = (IBillModel) this.getModel();
        if ("replace".equals(operateKey)) {
            int focusRow = this.getView().getModel().getEntryCurrentRowIndex("yem_entryentity");
            DynamicObject dataEntity = billModel.getDataEntity(true);
            DynamicObjectCollection entryentitys = dataEntity.getDynamicObjectCollection("yem_entryentity");
            DynamicObject entryentity = entryentitys.get(focusRow);
            DynamicObject partno = entryentity.getDynamicObject("yem_partno");// 设备电子目录明细
            if (partno != null) {
                Map<String, Object> replacemap = new HashMap<>();
                replacemap.put("orgid", this.getPageCache().get("orgId"));
                replacemap.put("materialId", partno.getPkValue());

                FormShowParameter openParam = new FormShowParameter();
                openParam.setFormId("yem_em_shopreplacement");
                openParam.getOpenStyle().setShowType(ShowType.Modal);
                openParam.setCustomParam("materialId", replacemap);
                openParam.setParentPageId(this.getView().getPageId());
                openParam.setStatus(OperationStatus.EDIT);
                openParam.setCloseCallBack(new CloseCallBack(this, "replacement"));
                this.getView().showForm(openParam);
            }
        }
        if ("info".equals(operateKey)) {
            int focusRow = this.getView().getModel().getEntryCurrentRowIndex("yem_entryentity");
            DynamicObject dataEntity = billModel.getDataEntity(true);
            DynamicObjectCollection entryentitys = dataEntity.getDynamicObjectCollection("yem_entryentity");
            DynamicObject entryentity = entryentitys.get(focusRow);
            DynamicObject yem_partno = entryentity.getDynamicObject("yem_partno");
            if (yem_partno != null) {
                FormShowParameter openParam = new FormShowParameter();
                Map<String, Object> replacemaped = new HashMap<>();
                replacemaped.put("orgid", this.getPageCache().get("orgId"));
                replacemaped.put("ecatalogId", entryentity.getString("yem_entryid"));
                openParam.setFormId("yem_em_replacepartview");
                openParam.getOpenStyle().setShowType(ShowType.MainNewTabPage);
                openParam.setCustomParam("ecatalogId", replacemaped);
                openParam.setParentPageId(this.getView().getPageId());
                openParam.setStatus(OperationStatus.VIEW);
                this.getView().showForm(openParam);
            }
        }
        if ("insertcarshop".equals(operateKey)) {
            int focusRow = this.getView().getModel().getEntryCurrentRowIndex("yem_entryentity");
            DynamicObject dataEntity = billModel.getDataEntity(true);
            DynamicObjectCollection entryentitys = dataEntity.getDynamicObjectCollection("yem_entryentity");
            DynamicObject entryentity = entryentitys.get(focusRow);
            BigDecimal qty = entryentity.getBigDecimal("yem_qtysub");
            DynamicObject goods = null;
            Object partno = entryentity.get("yem_partno");
            if (partno instanceof Long) {
                goods = BusinessDataServiceHelper.loadSingle(partno, "yem_em_ecatalog");
            } else if (partno instanceof DynamicObject) {
                goods = (DynamicObject) partno;
                goods = BusinessDataServiceHelper.loadSingle(goods.getPkValue(), "yem_em_ecatalog");
            }

            if (goods != null) {
                IPageCache pageCache = this.getPageCache();
                String orgId = pageCache.get("orgId");
                int count = CarShopUtils.insertCarShop(goods, qty, UserServiceHelper.getCurrentUserId(), orgId);
                if (count > 0) {
                    this.getView().showSuccessNotification(
                            ResManager.loadKDString("加入购物车成功！", "EtcmanualEdit_10", "bfcl_equipment"));

                    int count2 = CarShopUtils.SelectCarShop(UserServiceHelper.getCurrentUserId(), orgId);
                    String parentpageid = pageCache.get("parentpageid");
                    if (parentpageid != null) {
                        IFormView childView = this.getView().getView(parentpageid);
                        Label lab = childView.getControl("yem_labelap2");
                        lab.setText(count2 + "");
                        this.getView().sendFormAction(childView);
                    }
                } else {
                    this.getView().showErrorNotification(
                            ResManager.loadKDString("加入购物车失败！", "EtcmanualEdit_11", "bfcl_equipment"));
                }
            }
        }
    }

    @Override
    public void closedCallBack(ClosedCallBackEvent closedCallBackEvent) {
        String actionId = closedCallBackEvent.getActionId();
        if ("replacement".equals(actionId)) {
            String parentpageid = this.getPageCache().get("parentpageid");
            String orgId = this.getPageCache().get("orgId");
            IFormView childView = this.getView().getView(parentpageid);
            Label lab = childView.getControl("yem_labelap2");
            int count2 = CarShopUtils.SelectCarShop(UserServiceHelper.getCurrentUserId(), orgId);
            lab.setText(count2 + "");
            lab = childView.getControl("yem_labelap2");
            this.getView().sendFormAction(childView);
            this.getView().updateView("yem_labelap2");
        }
    }

}