package com.yem.em.dynamic;

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.entity.datamodel.IDataModel;
import kd.bos.form.FormShowParameter;
import kd.bos.form.IFormView;
import kd.bos.form.ShowType;
import kd.bos.form.control.Label;
import kd.bos.form.events.BeforeDoOperationEventArgs;
import kd.bos.form.operate.FormOperate;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.QueryServiceHelper;

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

public class CusordereditDynamicEdit extends AbstractFormPlugin {
    @Override
    public void afterCreateNewData(EventObject e) {
        super.afterCreateNewData(e);
        IDataModel model = this.getModel();
        FormShowParameter showParameter = this.getView().getFormShowParameter();
        Map<String, Object> cusMap = showParameter.getCustomParams();
        if (null != cusMap && cusMap.size() > 0) {
                ((Label) this.getControl("yem_labelap1263")).setText(cusMap.get("yem_textfield").toString());
                ((Label) this.getControl("yem_labelap12611")).setText(cusMap.get("yem_textfield2").toString());
                ((Label) this.getControl("yem_labelap1272")).setText(cusMap.get("yem_textfield5").toString());
                ((Label) this.getControl("yem_labelap12711")).setText(cusMap.get("yem_textfield1").toString());
                ((Label) this.getControl("yem_labelap12621")).setText(cusMap.get("yem_textfield3").toString());
                ((Label) this.getControl("yem_labelap12641")).setText(cusMap.get("yem_textfield4").toString());
            QFilter filter = new QFilter("yem_salesorderno", QCP.equals, cusMap.get("yem_textfield").toString());
            DynamicObjectCollection collection = QueryServiceHelper.query("yem_em_devicefiles",
                    "yem_framenumber,yem_model.number,group.name,yem_color,yem_enginecode,yem_enginemodel,yem_dateofmanufacture", new QFilter[]{filter}, null);
            for (DynamicObject obj : collection) {
                int row = this.getModel().createNewEntryRow("yem_entryentity1");
                model.setValue("yem_textfield6", obj.getString("yem_framenumber"), row);
                model.setValue("yem_textfield7", obj.getString("yem_model.number"), row);
                model.setValue("yem_textfield11", obj.getString("group.name"), row);
                model.setValue("yem_textfield51", obj.getString("yem_color"), row);
                model.setValue("yem_textfield21", obj.getString("yem_enginecode"), row);
                model.setValue("yem_textfield31", obj.getString("yem_enginemodel"), row);
                model.setValue("yem_textfield41", obj.getString("yem_dateofmanufacture"), row);
            }
            this.getView().updateView("yem_entryentity");
        }
    }

    @Override
    public void beforeDoOperation(BeforeDoOperationEventArgs e) {
        super.beforeDoOperation(e);
        FormOperate operate = (FormOperate) e.getSource();
        String key = operate.getOperateKey();
        DynamicObject dataEntity = this.getModel().getDataEntity(true);
        IFormView view = this.getView();
        IDataModel model = this.getModel();
        if(key.equals("xq")){//整车详情
            int row = model.getEntryCurrentRowIndex("yem_entryentity1");
            DynamicObjectCollection yem_entryentity1 = dataEntity.getDynamicObjectCollection("yem_entryentity1");
            if (yem_entryentity1.size() > 0) {
                DynamicObject entryentity = yem_entryentity1.get(row);
                QFilter qFilters = new QFilter("yem_framenumber", QCP.equals, entryentity.getString("yem_textfield6"));
                DynamicObjectCollection yem_em_devicefiles = QueryServiceHelper.query("yem_em_devicefiles",
                        "id,number", new QFilter[]{qFilters});
                if (yem_em_devicefiles.size() > 0) {
                    long id = yem_em_devicefiles.get(0).getLong("id");
                    BillShowParameter parameter = new BillShowParameter();
                    parameter.setPkId(id);
                    parameter.getOpenStyle().setShowType(ShowType.MainNewTabPage);
                    parameter.setFormId("yem_em_devicefiles");
                    parameter.setStatus(OperationStatus.VIEW);
                    Map<String, Object> entryInfo = new HashMap<String, Object>();
                    entryInfo.put("key","value");
                    parameter.setCustomParams(entryInfo);
                    this.getView().showForm(parameter);
                }
                else{
                    view.showErrorNotification("未获取到设备档案！");
                }
            }
        }
        if(key.equals("cxtc")){//查询图册
            int row = model.getEntryCurrentRowIndex("yem_entryentity1");
            DynamicObjectCollection yem_entryentity1 = dataEntity.getDynamicObjectCollection("yem_entryentity1");
            if (yem_entryentity1.size() > 0) {
                DynamicObject entryentity = yem_entryentity1.get(row);

                QFilter qFilters = new QFilter("number", QCP.equals, entryentity.getString("yem_textfield7"));
                DynamicObjectCollection yem_em_devicefiles = QueryServiceHelper.query("yem_em_equipment",
                        "id,number", new QFilter[]{qFilters});
                if (yem_em_devicefiles.size() > 0) {
                    long id = yem_em_devicefiles.get(0).getLong("id");
                    BillShowParameter parameter = new BillShowParameter();
                    parameter.setPkId(id);
                    parameter.getOpenStyle().setShowType(ShowType.MainNewTabPage);
                    parameter.setFormId("yem_em_equipment");
                    parameter.setStatus(OperationStatus.VIEW);
                    Map<String, Object> entryInfo = new HashMap<String, Object>();
                    entryInfo.put("key","value");
                    entryInfo.put("complete",entryentity.getString("yem_textfield6"));
                    parameter.setCustomParams(entryInfo);
                    this.getView().showForm(parameter);
                }
                else{
                    view.showErrorNotification("未获取到设备电子目录！");
                }
            }

        }
    }

}
