package com.yem.wm.es.encasement.dynamic;

import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.form.control.Control;
import kd.bos.form.control.EntryGrid;
import kd.bos.servicehelper.BusinessDataServiceHelper;

import java.util.*;

/**
 * @Classname MachineNumEdit
 * @Description TODO
 * @Date 2023/12/21 17:09
 * @Created by ZZSLL
 */

public class MachineNumEdit extends AbstractBillPlugIn {
    @Override
    public void afterCreateNewData(EventObject e) {
        super.afterCreateNewData(e);
        Map<String, Object> params = this.getView().getFormShowParameter().getCustomParams();
        if (params.containsKey("sourcebillid") && params.containsKey("sourcebilltype")) {
            long sourcebillid = (long) params.get("sourcebillid");
            String sourcebilltype = (String) params.get("sourcebilltype");
            if ("yem_es_storagetrans".equals(sourcebilltype) || "yem_im_encasement_sbgy".equals(sourcebilltype)) {
                DynamicObject dynamicObject = BusinessDataServiceHelper.loadSingle(sourcebillid, "yem_es_storagetrans");
                if (dynamicObject == null) {
                    return;
                }
                Map map1 = new HashMap<>();
                if (params.containsKey("yem_machinenumber")) {
                    String yem_machinenumber = (String) params.get("yem_machinenumber");
                    String[] split = yem_machinenumber.split(";");
                    if (split.length > 0) {
                        for (int i = 0; i < split.length; i++) {
                            map1.put(split[i], split[i]);
                        }
                    }
                }

                ArrayList<Map<String, Object>> list = new ArrayList<>();

                DynamicObjectCollection collection = dynamicObject.getDynamicObjectCollection("yem_es_materialinfo");
                for (DynamicObject object : collection) {
                    if ("Z".equals(object.getString("yem_businesssort"))) {
                        // 整机生产信息
                        DynamicObjectCollection salesorder = object.getDynamicObjectCollection("yem_es_salesorder_z");
                        for (DynamicObject entry : salesorder) {
                            DynamicObject resources = entry.getDynamicObject("yem_resourcescode");
                            String vehicleno = entry.getString("yem_vehicleno");
                            if (!map1.containsKey(vehicleno)) {
                                LinkedHashMap<String, Object> map = new LinkedHashMap<>();
                                map.put("resources", resources);
                                map.put("vehicleno", vehicleno);
                                list.add(map);
                            }
                        }
                    }
                }

                IDataModel model = this.getModel();
                DynamicObject dataEntity = model.getDataEntity();
                DynamicObjectCollection entryentity = dataEntity.getDynamicObjectCollection("yem_entryentity");
                entryentity.clear();
                for (Map<String, Object> map : list) {
                    DynamicObject addNew = entryentity.addNew();
                    addNew.set("yem_resourcescode", map.get("resources"));
                    addNew.set("yem_vehicleno", map.get("vehicleno"));
                }
            }
        }
    }

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

    @Override
    public void click(EventObject e) {
        super.click(e);
        String key = ((Control) e.getSource()).getKey();
        IDataModel model = this.getModel();
        DynamicObject dataEntity = model.getDataEntity(true);
        if ("btnok".equals(key)) {
            EntryGrid entryGrid = this.getControl("yem_entryentity");
            int[] selectRows = entryGrid.getSelectRows();
//            String retData = null;
            StringBuffer retData = new StringBuffer();
            if (selectRows.length > 0) {
                for (int selectRow : selectRows) {
                    DynamicObject yem_entryentity = dataEntity.getDynamicObjectCollection("yem_entryentity").get(selectRow);
                    String yem_vehicleno = yem_entryentity.getString("yem_vehicleno");
                    retData.append(yem_vehicleno).append(";");
                }
//                retData = dataEntity.getDynamicObjectCollection("yem_entryentity").get(selectRows[0]).getString("yem_vehicleno");
            }

            this.getView().returnDataToParent(retData.deleteCharAt(retData.length() - 1).toString());
            this.getView().close();
        }
        if ("btncancel".equals(key)) {
            this.getView().close();
        }
    }
}
