package com.yem.em.dynamic;

import com.yem.wm.utils.RequestCmmp;
import com.yem.wm.utils.YEM;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.form.IFormView;
import kd.bos.form.events.AfterDoOperationEventArgs;
import kd.bos.form.plugin.AbstractFormPlugin;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.yem.em.utils.AtlasUtils.seelctDyn;

/**
 * @author zhouc
 * @date 2023/10/23 17:05
 * @className CompleteDynamicEdit
 * @description 整机领料明细查询
 */
public class CompleteDynamicEdit extends AbstractFormPlugin {
    @Override
    public void afterDoOperation(AfterDoOperationEventArgs e) {
        super.afterDoOperation(e);
        IFormView view = this.getView();
        String operateKey = e.getOperateKey();
        if (operateKey.equals("query")) {
            Object yem_machineno = view.getModel().getValue("yem_machineno");
            query(yem_machineno);

        }
        if (operateKey.equals("exit")) {
            view.close();
        }
    }

    /**
     * 查询sql
     *
     * @param yem_machineno
     */
    public void query(Object yem_machineno) {
        IFormView view = this.getView();
        IDataModel model = this.getModel();
        String yem_producttype = (String) model.getValue("yem_producttype");
        if (yem_producttype == null || yem_producttype.length() == 0) {
            view.showErrorNotification("请选择产品类型后重试！");
            return;
        }
        DynamicObject yem_bd_productsgroup = seelctDyn("yem_bd_productsgroup", "number", yem_producttype);
        String sql = "select  " + Queryfield() + "  from VIEW_MATERDETAIL_WM where plan_pin='" + yem_machineno + "'";
        List<Map> fromCmmp = RequestCmmp.getFromCmmp(YEM.getCurrentOrgId(), sql, yem_bd_productsgroup.getLong("id"));
        for (int i = 0; i < fromCmmp.size(); i++) {
            Map map = fromCmmp.get(i);
            if (map != null) {
                int newEntryRow = model.createNewEntryRow("yem_entryentity");
                model.setValue("yem_type_one_name", map.get("type_one_name"), newEntryRow);
                model.setValue("yem_type_two_name", map.get("type_two_name"), newEntryRow);
                model.setValue("yem_bill_code", map.get("bill_code"), newEntryRow);
                model.setValue("yem_confirm_date", map.get("confirm_date"), newEntryRow);
                model.setValue("yem_tmater_code", map.get("mater_code"), newEntryRow);
                model.setValue("yem_mater_name", map.get("mater_name"), newEntryRow);
                model.setValue("yem_order_num", map.get("order_num"), newEntryRow);
                model.setValue("yem_supplier_code", map.get("supplier_code"), newEntryRow);
                model.setValue("yem_supplier_name", map.get("supplier_name"), newEntryRow);
            }
        }
    }

    /**
     * 拼接查询字段
     *
     * @return
     */
    public String Queryfield() {
        String result = null;
        List<String> list = new ArrayList<>();
        list.add("type_one_name");
        list.add("type_two_name");
        list.add("bill_code");
        list.add("confirm_date");
        list.add("mater_code");
        list.add("mater_name");
        list.add("order_num");
        list.add("supplier_code");
        list.add("supplier_name");
        result = String.join(",", list);
        return result;
    }
}
