package com.yem.em.ass.Devicefiles.form;

import com.yem.em.utils.AtlasUtils;
import com.yem.wm.im.delivernotice.utils.DeliverNoticeUtilsEdit;
import com.yem.wm.utils.RequestCmmp;
import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.dataentity.OperateOption;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.entity.operate.result.OperationResult;
import kd.bos.form.events.AfterDoOperationEventArgs;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.QueryServiceHelper;
import kd.bos.servicehelper.operation.OperationServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;

import java.math.BigDecimal;
import java.util.*;

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

/**
 * @author zhouc
 * @date 2023/12/23 21:10
 * @className DevicefilesFormEdit
 * @description 整机档案表单插件
 */
public class DevicefilesFormEdit extends AbstractBillPlugIn {

    @Override
    public void afterDoOperation(AfterDoOperationEventArgs e) {
        super.afterDoOperation(e);
        String operateKey = e.getOperateKey();
        IDataModel model = this.getModel();
        DynamicObject dataEntity = this.getModel().getDataEntity(true);
        if ("getdata".equals(operateKey)) {
            //修复订单日期
            QFilter qFilter = new QFilter("yem_dateofpurchase", QCP.equals, null);//替换零件编码
            qFilter.or(new QFilter("yem_heqty", QCP.equals, null));
            DynamicObjectCollection yem_em_devicefiles = QueryServiceHelper.query("yem_em_devicefiles",
                    "id,yem_dateofpurchase,yem_salesorderno"
                    , new QFilter[]{qFilter}, null);
            if (yem_em_devicefiles.size() > 0) {
                List list = new ArrayList<>();
                for (int i = 0; i < yem_em_devicefiles.size(); i++) {
                    DynamicObject dynamicObject = yem_em_devicefiles.get(i);
                    list.add(dynamicObject.getString("yem_salesorderno"));
                }
                if (list.size() > 0) {
                    Map<String, Date> map = new HashMap<>();
                    Map<String, BigDecimal> mapqty = new HashMap<>();
                    QFilter Filter = new QFilter("billno", QCP.in, list);//替换零件编码
                    DynamicObjectCollection yem_es_salesorder = QueryServiceHelper.query("yem_es_salesorder",
                            "id,billno,yem_signdate,yem_ordersumnumber"
                            , new QFilter[]{Filter}, null);
                    if (yem_es_salesorder.size() > 0) {
                        for (int i = 0; i < yem_es_salesorder.size(); i++) {
                            DynamicObject salesorder = yem_es_salesorder.get(i);
                            map.put(salesorder.getString("billno"), salesorder.getDate("yem_signdate"));
                            mapqty.put(salesorder.getString("billno"), salesorder.getBigDecimal("yem_ordersumnumber"));
                        }
                    }
                    if (map.size() > 0) {
                        for (int i = 0; i < yem_em_devicefiles.size(); i++) {
                            DynamicObject dynamicObject = yem_em_devicefiles.get(i);
                            DynamicObject newbomchange = BusinessDataServiceHelper.loadSingle(dynamicObject.getLong("id"), "yem_em_devicefiles");
                            if (newbomchange != null) {
                                String yem_salesorderno = newbomchange.getString("yem_salesorderno");
                                if (map.containsKey(yem_salesorderno)) {
                                    newbomchange.set("yem_dateofpurchase", map.get(yem_salesorderno));
                                    if (mapqty.containsKey(yem_salesorderno)) {
                                        newbomchange.set("yem_heqty", mapqty.get(yem_salesorderno));
                                    }
                                }
                                OperationResult Operate = OperationServiceHelper.executeOperate("save",
                                        newbomchange.getDynamicObjectType().getName(), new DynamicObject[]{newbomchange}, OperateOption.create());
                                if (Operate.isSuccess()) {
                                    this.getView().updateView();
                                }
                            }
                        }
                    }
                }
            }
            this.getView().showSuccessNotification("操作完成！！");
        }
        if ("getzjsx".equals(operateKey)) {
            Map<String, Long> groupmap = new HashMap<>();
            //修复整机上线时间
            QFilter qFilter = new QFilter("yem_machinedata", QCP.equals, null);//替换零件编码
            DynamicObjectCollection yem_em_devicefiles = QueryServiceHelper.query("yem_em_devicefiles",
                    "id,yem_machinedata,yem_framenumber,yem_model.yem_productsgroup.id"
                    , new QFilter[]{qFilter}, null);
            if (yem_em_devicefiles.size() > 0) {
                long yem_productsgroup = 0;
                List list = new ArrayList<>();
                for (int i = 0; i < yem_em_devicefiles.size(); i++) {
                    DynamicObject dynamicObject = yem_em_devicefiles.get(i);
                    yem_productsgroup = dynamicObject.getLong("yem_model.yem_productsgroup.id");
                    list.add(dynamicObject.getString("yem_framenumber"));
                    groupmap.put(dynamicObject.getString("yem_framenumber"), dynamicObject.getLong("yem_model.yem_productsgroup.id"));
                }
                if (list.size() > 0) {
                    Map<String, Date> map = new HashMap<>();
//                    DynamicObject yem_bd_productsgroup = seelctDyn("yem_bd_productsgroup", "number", "05");

                    String Queryfield = AtlasUtils.Queryfield("PLAN_PIN", "INSTRUCTION_CODE", "JOB_TIME", "INSTRUCTION_NAME");
                    String sql = "select distinct " + Queryfield + "  from VIEW_PLAN_JOB_WM where INSTRUCTION_NAME='上线' and PLAN_PIN in (" + Listtostring(list) + ")";//where TO_CHAR (enable_time, 'yyyy-mm-dd') >= '2023-01-01'
                    List<Map> fromCmmp = RequestCmmp.getFromCmmp(0L, sql, yem_productsgroup);
                    if (fromCmmp.size() > 0) {
                        Map frmap = fromCmmp.get(0);
                        Date job_time = (Date) frmap.get("job_time");
                        map.put(frmap.get("plan_pin").toString(), job_time);
                    }
                    if (map.size() > 0) {
                        for (int i = 0; i < yem_em_devicefiles.size(); i++) {
                            DynamicObject dynamicObject = yem_em_devicefiles.get(i);
                            DynamicObject newbomchange = BusinessDataServiceHelper.loadSingle(dynamicObject.getLong("id"), "yem_em_devicefiles");
                            if (newbomchange != null) {
                                String yem_salesorderno = newbomchange.getString("yem_framenumber");
                                if (map.containsKey(yem_salesorderno)) {
                                    newbomchange.set("yem_machinedata", map.get(yem_salesorderno));
                                }
                                OperationResult Operate = OperationServiceHelper.executeOperate("save",
                                        newbomchange.getDynamicObjectType().getName(), new DynamicObject[]{newbomchange}, OperateOption.create());
                                if (Operate.isSuccess()) {

                                }
                            }
                        }
                    }
                }
            }
            this.getView().showSuccessNotification("操作完成！！");
        }
        if ("reloading".equals(operateKey)) {
            String yem_salesorderno = dataEntity.getString("yem_salesorderno");
            DynamicObject yem_model = dataEntity.getDynamicObject("yem_model");
            String yem_framenumber = dataEntity.getString("yem_framenumber");
            if (yem_model != null) {
                //多功能
                Map<String, DynamicObjectCollection> Gettracktaskconsole = DeliverNoticeUtilsEdit.Gettracktaskconsole(yem_salesorderno, yem_model, yem_framenumber, "yem_multiconfig");
//                DynamicObjectCollection multiconfig = DeliverNoticeUtilsEdit.Gettracktaskconsole(yem_salesorderno, yem_model, yem_framenumber, "yem_multiconfig");
                if (Gettracktaskconsole.containsKey("yem_multiconfig")) {
                    DynamicObjectCollection multiconfig = Gettracktaskconsole.get("yem_multiconfig");
                    if (multiconfig.size() > 0) {
                        //整机档案
                        DynamicObjectCollection yem_multiconfig = dataEntity.getDynamicObjectCollection("yem_multiconfig");
                        yem_multiconfig.clear();
                        for (int i = 0; i < multiconfig.size(); i++) {
                            DynamicObject dynamicObject1 = multiconfig.get(i);
                            DynamicObject dynamicObject2 = yem_multiconfig.addNew();
                            dynamicObject2.set("yem_configurtype", dynamicObject1.getString("yem_configurtype"));//配置类型
                            dynamicObject2.set("yem_configurname", dynamicObject1.getString("yem_configurname"));//配置名称
                            dynamicObject2.set("yem_configurtypeen", dynamicObject1.getString("yem_configurtypeen"));//配置英文名称
                            dynamicObject2.set("yem_specification", dynamicObject1.getString("yem_specification_o"));//规格型号
                            dynamicObject2.set("yem_qty", dynamicObject1.getBigDecimal("yem_unitset"));//数量
                            dynamicObject2.set("yem_arguments", dynamicObject1.getString("yem_arguments"));//参数（mm）
                            dynamicObject2.set("yem_remark", dynamicObject1.getString("yem_remarkss"));//备注
                        }
                    }
                }
                //加装
//                DynamicObjectCollection retrofitting = DeliverNoticeUtilsEdit.Gettracktaskconsole(yem_salesorderno, yem_model, yem_framenumber, "yem_retrofitting");
                if (Gettracktaskconsole.containsKey("yem_retrofitting")) {
                    DynamicObjectCollection retrofitting = Gettracktaskconsole.get("yem_retrofitting");
                    if (retrofitting.size() > 0) {
                        //整机档案
                        DynamicObjectCollection yem_retrofitting = dataEntity.getDynamicObjectCollection("yem_retrofitting");
                        yem_retrofitting.clear();
                        for (int i = 0; i < retrofitting.size(); i++) {
                            DynamicObject dynamicObject1 = retrofitting.get(i);
                            DynamicObject dynamicObject2 = yem_retrofitting.addNew();
                            dynamicObject2.set("yem_albumassemblyno", dynamicObject1.getDynamicObject("yem_albumassemblyno"));//物料编码
                            dynamicObject2.set("yem_materiel", dynamicObject1.getDynamicObject("yem_materiel1"));//物料编码
                            dynamicObject2.set("yem_unit1", dynamicObject1.getDynamicObject("yem_unit1"));//
                            dynamicObject2.set("yem_enname", dynamicObject1.getString("yem_enname"));//
                            dynamicObject2.set("yem_qty1", dynamicObject1.getBigDecimal("yem_qty1"));//数量
                            dynamicObject2.set("yem_desc", dynamicObject1.getString("yem_desc"));//备注
                        }
                    }
                }
                this.getView().invokeOperation("save");
                this.getView().updateView();
            }
        }

        if ("getcolor".equals(operateKey)) {
            String yem_framenumber = dataEntity.getString("yem_framenumber");
            DynamicObject yem_productsgroup = dataEntity.getDynamicObject("yem_model.yem_productsgroup");
            if (yem_framenumber.length() > 0) {
                String sql = "select DISTINCT product_color,color_name from VIEW_PLAN_PIN_WM  where PLAN_PIN='" + yem_framenumber + "'";
                List<Map> fromCmmp = RequestCmmp.getFromCmmp(0l, sql, yem_productsgroup.getLong("id"));
                if (fromCmmp.size() > 0) {
                    Map map = fromCmmp.get(0);
                    model.setValue("yem_compcode",map.get("product_color"));
                    model.setValue("yem_compname",map.get("color_name"));
                    getView().invokeOperation("save");
                }
                this.getView().showSuccessNotification("操作完成！！");
            }
        }
    }

    public String Listtostring(List list) {
        String liststring = null;
        for (int i = 0; i < list.size(); i++) {
            if (liststring != null) {
                liststring = liststring + ",'" + list.get(i).toString() + "'";
            } else {
                liststring = "'" + list.get(i).toString() + "'";
            }
        }

        return liststring;
    }

}
