package com.yem.wm.task;

import com.yem.wm.es.Util.PlanJobCMMP;
import com.yem.wm.es.Util.ViewInterfaceDocking;
import com.yem.wm.utils.DynamicObjectUtil;
import com.yem.wm.utils.YEM;
import kd.bos.context.RequestContext;
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.exception.KDException;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.schedule.executor.AbstractTask;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.OperationServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;

import java.util.*;

/**
 * @author zhouc
 * @date 2024/4/13 11:43
 * @className TrackconsoleMachineTask
 * @description 定时获取整机信息
 */
public class TrackconsoleMachineTask extends AbstractTask {
    @Override
    public void execute(RequestContext requestContext, Map<String, Object> map) throws KDException {
        DynamicObject[] dynamicObjects = BusinessDataServiceHelper.load("yem_tracktaskconsole", "id,billno,yem_bd_products,yem_es_salesorder_r,yem_es_salesorder_r.yem_es_salesorder_z" +
                        ",yem_es_salesorder_z.yem_resourcescode,yem_es_salesorder_z.yem_milltype,yem_es_salesorder_z.yem_vehicleno,yem_es_salesorder_z.yem_engineno,yem_es_salesorder_z.yem_enginemodel" +
                        ",yem_es_salesorder_z.yem_enginevendor,yem_es_salesorder_z.yem_currentstate,yem_es_salesorder_z.yem_currentstatedate,yem_es_salesorder_z.yem_prepareway" +
                        ",yem_es_salesorder_z.yem_assembleupline,yem_es_salesorder_z.yem_upline,yem_es_salesorder_z.yem_assembledownline,yem_es_salesorder_z.yem_downline" +
                        ",yem_es_salesorder_z.yem_debugst,yem_es_salesorder_z.yem_debugstatdate,yem_es_salesorder_z.yem_accuracytest,yem_es_salesorder_z.yem_accuratdate" +
                        ",yem_es_salesorder_z.yem_waitforinspection,yem_es_salesorder_z.yem_waitforinspecdate,yem_es_salesorder_z.yem_qualitytest,yem_es_salesorder_z.yem_qualitytestdate" +
                        ",yem_es_salesorder_z.yem_factputstor,yem_es_salesorder_z.yem_factputstordate,yem_es_salesorder_z.yem_backonline,yem_es_salesorder_z.yem_backonlinedate" +
                        ",yem_es_salesorder_z.yem_putintostoragedate,yem_es_salesorder_z.yem_putintostorage"
                , new QFilter[]{new QFilter("billstatus", QCP.equals, "C")});
        if (dynamicObjects.length > 0) {
            for (DynamicObject dynamicObject : dynamicObjects) {
                HashMap<String, Object> hashMap = new HashMap<>();
                Set<String> resourcesCodeSet = new HashSet();
                Set<String> vehicleNoSet = new HashSet();
//                int machineProductInfoRows = dynamicObject.getEntryRowCount("yem_es_salesorder_z");
//                for (int i = 0; i < machineProductInfoRows; i++) {
                DynamicObjectCollection yemEsSalesorderR = dynamicObject.getDynamicObjectCollection("yem_es_salesorder_r");
                if (yemEsSalesorderR.size() > 0) {
                    for (DynamicObject yemEsSalesorder : yemEsSalesorderR) {
                        DynamicObjectCollection yemEsSalesorderZ = yemEsSalesorder.getDynamicObjectCollection("yem_es_salesorder_z");
                        if (yemEsSalesorderZ.size() > 0) {
                            for (DynamicObject object : yemEsSalesorderZ) {
                                DynamicObject resourcesCode = object.getDynamicObject("yem_resourcescode");
                                if (YEM.isNotEmpty(resourcesCode)) {
                                    resourcesCodeSet.add(resourcesCode.getString("number"));
                                }
                                String vehicleNo = object.getString("yem_vehicleno");
                                vehicleNoSet.add(vehicleNo);
                            }
                        }
//                }
                        if (resourcesCodeSet.size() > 0) {
                            hashMap.put(PlanJobCMMP.productCode, resourcesCodeSet);
                        }
                        if (vehicleNoSet.size() > 0) {
                            hashMap.put(PlanJobCMMP.planPin, vehicleNoSet);
                        }
                        long productsid = dynamicObject.getDynamicObject("yem_bd_products").getLong("id");
                        List<Map> dockings = ViewInterfaceDocking.docking(ViewInterfaceDocking.PLAN_JOB, hashMap, productsid);

                        if (YEM.isNotEmpty(dockings)) {
                            dockings.sort(new Comparator<Map>() {
                                @Override
                                public int compare(Map o1, Map o2) {
                                    Date date1 = (Date) o1.get(PlanJobCMMP.jobTime);
                                    Date date2 = (Date) o1.get(PlanJobCMMP.jobTime);
                                    return date1.compareTo(date2);
                                }
                            });
//                    for (int i = 0; i < machineProductInfoRows; i++) {
                            if (yemEsSalesorderZ.size() > 0) {
                                for (DynamicObject object : yemEsSalesorderZ) {
                                    DynamicObject resourcesCodeObj = object.getDynamicObject("yem_resourcescode");
                                    String resourcescode = YEM.isNotEmpty(resourcesCodeObj) ? resourcesCodeObj.getString("number") : "";
                                    String vehicleno = object.getString("yem_vehicleno");
                                    for (Map docking : dockings) {
                                        String planPin = (String) docking.get(PlanJobCMMP.planPin);
                                        String productCode = (String) docking.get(PlanJobCMMP.productCode);
                                        if (resourcescode.equals(productCode) && vehicleno.equals(planPin)) {
                                            object.set("yem_engineno", docking.get(PlanJobCMMP.stockBatch));//发动机号
                                            object.set("yem_enginemodel", docking.get(PlanJobCMMP.specification));//发动机型号
                                            object.set("yem_enginevendor", docking.get(PlanJobCMMP.supName));//发动机厂商
//                                setInstructionValue(model, docking);
                                            String instructionCode = (String) docking.get(PlanJobCMMP.instructionCode);
                                            Date instructionTime = (Date) docking.get(PlanJobCMMP.jobTime);
                                            String instructionName = (String) docking.get(PlanJobCMMP.instructionName);
                                            object.set("yem_currentstate", instructionName);
                                            object.set("yem_currentstatedate", instructionTime);
                                            String prepareWay = object.getString("yem_prepareway");
                                            switch (prepareWay) {
                                                case "A":
                                                    ZZ(object, instructionCode, instructionTime, instructionName);
                                                    break;
                                                case "B":
                                                    GZ(object, instructionCode, instructionTime, instructionName);
                                                    break;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                OperationServiceHelper.executeOperate("getmachine", "yem_tracktaskconsole", new Object[]{dynamicObject.getLong("id")}, OperateOption.create());
            }
            SaveServiceHelper.update(dynamicObjects);
        }
    }

    public static void ZZ(DynamicObject model, String instructionCode, Date instructionTime, String instructionName) {
        switch (instructionCode) {
            case "SX":
                model.set("yem_assembleupline", "A");
                model.set("yem_upline", instructionTime);
                break;
            case "XX":
                model.set("yem_assembledownline", "A");
                model.set("yem_downline", instructionTime);
                break;
            case "TS":
                model.set("yem_debugst", "A");
                model.set("yem_debugstatdate", instructionTime);
                break;
            case "JS":
                model.set("yem_accuracytest", "A");
                model.set("yem_accuratdate", instructionTime);
                break;
            case "DJ":
                model.set("yem_waitforinspection", "A");
                model.set("yem_waitforinspecdate", instructionTime);
                break;
            case "ZJHG":
                model.set("yem_qualitytest", "A");
                model.set("yem_qualitytestdate", instructionTime);
                break;
            case "RK":
                model.set("yem_factputstor", "A");
                model.set("yem_factputstordate", instructionTime);
                break;
            case "FGSX":
                model.set("yem_backonline", "A");
                model.set("yem_backonlinedate", instructionTime);
                break;
            case "FGRK":
                model.set("yem_putintostorage", "A");
                model.set("yem_putintostoragedate", instructionTime);
                break;
        }
    }

    public static void GZ(DynamicObject model, String instructionCode, Date instructionTime, String instructionName) {
        switch (instructionCode) {
            case "SX":
                model.set("yem_assembleupline", "A");
                model.set("yem_upline", instructionTime);
                break;
            case "GZSX":
                model.set("yem_assembleupline", "A");
                model.set("yem_upline", instructionTime);
                break;
            case "GZRK":
                model.set("yem_factputstor", "A");
                model.set("yem_factputstordate", instructionTime);
                break;
            case "RK":
                model.set("yem_factputstor", "A");
                model.set("yem_factputstordate", instructionTime);
                break;
            case "XX":
                model.set("yem_assembledownline", "A");
                model.set("yem_downline", instructionTime);
                break;
            case "TS":
                model.set("yem_debugst", "A");
                model.set("yem_debugstatdate", instructionTime);
                break;
            case "JS":
                model.set("yem_accuracytest", "A");
                model.set("yem_accuratdate", instructionTime);
                break;
            case "DJ":
                model.set("yem_waitforinspection", "A");
                model.set("yem_waitforinspecdate", instructionTime);
                break;
            case "ZJHG":
                model.set("yem_qualitytest", "A");
                model.set("yem_qualitytestdate", instructionTime);
                break;
            case "FGSX":
                model.set("yem_backonline", "A");
                model.set("yem_backonlinedate", instructionTime);
                break;
            case "FGRK":
                model.set("yem_putintostorage", "A");
                model.set("yem_putintostoragedate", instructionTime);
                break;
        }
    }
}
