package com.yem.wm.im.tracktaskconsole.op;

import com.yem.wm.im.tracktaskconsole.validator.TracktaskconsoleSavevalidator;
import com.yem.wm.im.tracktaskconsole.validator.TracktaskconsoleSubmitvalidator;
import com.yem.wm.utils.DynamicObjectUtil;
import com.yem.wm.utils.YEM;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.dataentity.metadata.IDataEntityType;
import kd.bos.dataentity.metadata.dynamicobject.DynamicObjectType;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.AddValidatorsEventArgs;
import kd.bos.entity.plugin.PreparePropertysEventArgs;
import kd.bos.entity.plugin.args.EndOperationTransactionArgs;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;
import kd.sdk.plugin.Plugin;

import java.util.*;

/**
 * 单据操作
 */
public class TrackTaskConsoleSaveOp extends AbstractOperationServicePlugIn implements Plugin {
    @Override
    public void onPreparePropertys(PreparePropertysEventArgs e) {
        List<String> fieldKeys = e.getFieldKeys();
        fieldKeys.add("yem_billtype");
        fieldKeys.add("yem_sourcebillid_d");
        fieldKeys.add("yem_sourceentryid");
        fieldKeys.add("yem_entrtyid");
        fieldKeys.add("yem_resourcescode");
        fieldKeys.add("yem_milltype");
        fieldKeys.add("yem_yieldway");
        fieldKeys.add("yem_prepareway");
        fieldKeys.add("yem_vehicleno");
        fieldKeys.add("yem_reformresources");
        fieldKeys.add("yem_engineno");
        fieldKeys.add("yem_enginemodel");
        fieldKeys.add("yem_enginevendor");
        fieldKeys.add("yem_currentstate");
        fieldKeys.add("yem_currentstatedate");
        fieldKeys.add("yem_assembleupline");
        fieldKeys.add("yem_upline");
        fieldKeys.add("yem_assembledownline");
        fieldKeys.add("yem_downline");
        fieldKeys.add("yem_debugst");
        fieldKeys.add("yem_debugstatdate");
        fieldKeys.add("yem_waitforinspection");
        fieldKeys.add("yem_waitforinspecdate");
        fieldKeys.add("yem_accuracytest");
        fieldKeys.add("yem_accuratdate");
        fieldKeys.add("yem_qualitytest");
        fieldKeys.add("yem_qualitytestdate");
        fieldKeys.add("yem_factputstor");
        fieldKeys.add("yem_factputstordate");
        fieldKeys.add("yem_backonline");
        fieldKeys.add("yem_backonlinedate");
        fieldKeys.add("yem_putintostorage");
        fieldKeys.add("yem_putintostoragedate");
        fieldKeys.add("yem_vehicleremark");
        fieldKeys.add("yem_ifdelivered");
        fieldKeys.add("yem_stockway");
        fieldKeys.add("yem_isvmi");
    }

    @Override
    public void endOperationTransaction(EndOperationTransactionArgs e) {
        DynamicObject[] dataEntities = e.getDataEntities();
        for(DynamicObject dataEntitie : dataEntities){
            DynamicObject billType = dataEntitie.getDynamicObject("yem_billtype");
            String billTypeNumber = YEM.isNotEmpty(billType)?billType.getString("number"):"";
            if("yem_tracktaskconsole_zj".equals(billTypeNumber)){
                Set<Long> ids = new HashSet<>();
                Map<Long, Map<Long, Map<String, Object>>> allMap = new HashMap<>();
                DynamicObjectCollection materialInfos = dataEntitie.getDynamicObjectCollection("yem_es_materialinfo");
                for(DynamicObject materialInfo : materialInfos) {
                    long sourceBillId = materialInfo.getLong("yem_sourcebillid_d");
                    ids.add(sourceBillId);
                }
                String selectfields = DynamicObjectUtil.getSelectfields("yem_tracktaskconsole", false);
                selectfields = DynamicObjectUtil.getEntrySelectfields(selectfields,"yem_tracktaskconsole","yem_es_materialinfo",false);
                selectfields = DynamicObjectUtil.getEntrySelectfields(selectfields,"yem_tracktaskconsole","yem_es_salesorder_r",false);
                selectfields = DynamicObjectUtil.getEntrySelectfields(selectfields,"yem_tracktaskconsole","yem_es_salesorder_r","yem_es_salesorder_z",false);
                DynamicObject[] trackTaskConsoles = BusinessDataServiceHelper.load("yem_tracktaskconsole", selectfields,
                        new QFilter[]{new QFilter("yem_es_materialinfo.yem_sourcebillid_d", QCP.in, ids),
                                new QFilter("yem_billtype.number", QCP.equals, "yem_tracktaskconsole_zj")});
                for(DynamicObject trackTaskConsole : trackTaskConsoles){
                    materialInfos = trackTaskConsole.getDynamicObjectCollection("yem_es_materialinfo");
                    DynamicObjectCollection stockInfos = trackTaskConsole.getDynamicObjectCollection("yem_es_salesorder_r");
                    for(DynamicObject materialInfo : materialInfos){
                        long sourceBillId = materialInfo.getLong("yem_sourcebillid_d");
                        ids.add(sourceBillId);
                        long id = materialInfo.getLong("id");
                        long sourceEntryId = materialInfo.getLong("yem_sourceentryid");
                        Map<Long, Map<String,Object>> machineInfoIdMap = allMap.containsKey(sourceEntryId)?allMap.get(sourceEntryId):new HashMap();
                        for(DynamicObject stockInfo : stockInfos){
                            long entryId = stockInfo.getLong("yem_entrtyid");
                            if(id == entryId){
                                DynamicObjectCollection machineInfos = stockInfo.getDynamicObjectCollection("yem_es_salesorder_z");
                                for(DynamicObject machineInfo : machineInfos){
                                    Map<String,Object> machineInfoMap = new HashMap<>();
                                    machineInfoIdMap.put(machineInfo.getLong("id"),machineInfoMap);
                                    machineInfoMap.put("yem_resourcescode",machineInfo.get("yem_resourcescode"));//资源号
                                    machineInfoMap.put("yem_milltype",machineInfo.get("yem_milltype"));//机型
                                    machineInfoMap.put("yem_yieldway",machineInfo.get("yem_yieldway"));//物料属性
                                    machineInfoMap.put("yem_prepareway",machineInfo.get("yem_prepareway"));//备料方式
                                    machineInfoMap.put("yem_vehicleno",machineInfo.get("yem_vehicleno"));//整机编号
                                    machineInfoMap.put("yem_reformresources",machineInfo.get("yem_reformresources"));//改制资源号
                                    machineInfoMap.put("yem_engineno",machineInfo.get("yem_engineno"));//发动机号
                                    machineInfoMap.put("yem_enginemodel",machineInfo.get("yem_enginemodel"));//发动机型号
                                    machineInfoMap.put("yem_enginevendor",machineInfo.get("yem_enginevendor"));//发动机厂商
                                    machineInfoMap.put("yem_currentstate",machineInfo.get("yem_currentstate"));//当前状态
                                    machineInfoMap.put("yem_currentstatedate",machineInfo.get("yem_currentstatedate"));//当前状态时间
                                    machineInfoMap.put("yem_assembleupline",machineInfo.get("yem_assembleupline"));//总装上线
                                    machineInfoMap.put("yem_upline",machineInfo.get("yem_upline"));//上线时间
                                    machineInfoMap.put("yem_assembledownline",machineInfo.get("yem_assembledownline"));//总装下线
                                    machineInfoMap.put("yem_downline",machineInfo.get("yem_downline"));//下线时间
                                    machineInfoMap.put("yem_debugst",machineInfo.get("yem_debugst"));//调试
                                    machineInfoMap.put("yem_debugstatdate",machineInfo.get("yem_debugstatdate"));//调试完成时间
                                    machineInfoMap.put("yem_waitforinspection",machineInfo.get("yem_waitforinspection"));//待检
                                    machineInfoMap.put("yem_waitforinspecdate",machineInfo.get("yem_waitforinspecdate"));//待检时间
                                    machineInfoMap.put("yem_accuracytest",machineInfo.get("yem_accuracytest"));//精试
                                    machineInfoMap.put("yem_accuratdate",machineInfo.get("yem_accuratdate"));//精试完成时间
                                    machineInfoMap.put("yem_qualitytest",machineInfo.get("yem_qualitytest"));//质检
                                    machineInfoMap.put("yem_qualitytestdate",machineInfo.get("yem_qualitytestdate"));//质检合格时间
                                    machineInfoMap.put("yem_factputstor",machineInfo.get("yem_factputstor"));//入库
                                    machineInfoMap.put("yem_factputstordate",machineInfo.get("yem_factputstordate"));//实际入库时间
                                    machineInfoMap.put("yem_backonline",machineInfo.get("yem_backonline"));//返工上线
                                    machineInfoMap.put("yem_backonlinedate",machineInfo.get("yem_backonlinedate"));//返工上线时间
                                    machineInfoMap.put("yem_putintostorage",machineInfo.get("yem_putintostorage"));//改制入库
                                    machineInfoMap.put("yem_putintostoragedate",machineInfo.get("yem_putintostoragedate"));//改制入库时间
                                    machineInfoMap.put("yem_vehicleremark",machineInfo.get("yem_vehicleremark"));//备注
                                    machineInfoMap.put("yem_ifdelivered",machineInfo.get("yem_ifdelivered"));//是否已发运
                                }
                            }
                        }
                        allMap.put(sourceEntryId,machineInfoIdMap);
                    }
                }

                DynamicObject dynamicObject = BusinessDataServiceHelper.newDynamicObject("yem_es_salesorder");
                DynamicObject[] salesOrders = BusinessDataServiceHelper.load(ids.toArray(), dynamicObject.getDynamicObjectType());
                renewSaleMachineInfo(salesOrders,allMap);
            }
        }
    }

    @Override
    public void onAddValidators(AddValidatorsEventArgs e) {
        super.onAddValidators(e);
        e.addValidator(new TracktaskconsoleSavevalidator());
    }
    public void renewSaleMachineInfo(DynamicObject[] salesOrders,Map<Long, Map<Long, Map<String, Object>>> allMap){
        for(DynamicObject salesOrder : salesOrders){
            DynamicObjectCollection saleMaterialInfos = salesOrder.getDynamicObjectCollection("yem_es_materialinfo");
            for(DynamicObject saleMaterialInfo : saleMaterialInfos){
                long saleMaterialInfoId = saleMaterialInfo.getLong("id");
                if(allMap.containsKey(saleMaterialInfoId)){
                    DynamicObjectCollection saleMachineInfo = saleMaterialInfo.getDynamicObjectCollection("yem_es_salesorder_z");
                    saleMachineInfo.clear();
                    Map<Long, Map<String, Object>> machineInfoIdMap = allMap.get(saleMaterialInfoId);
                    Set<Long> ids = machineInfoIdMap.keySet();
                    for (Long id : ids){
                        Map<String, Object> machineInfoMap = machineInfoIdMap.get(id);
                        DynamicObject addNew = saleMachineInfo.addNew();
                        addNew.set("yem_tracktaskmachinid",id);
                        addNew.set("yem_resourcescode",machineInfoMap.get("yem_resourcescode"));//资源号
                        addNew.set("yem_milltype",machineInfoMap.get("yem_milltype"));//机型
                        addNew.set("yem_yieldway",machineInfoMap.get("yem_yieldway"));//物料属性
                        addNew.set("yem_prepareway",machineInfoMap.get("yem_prepareway"));//备料方式
                        addNew.set("yem_vehicleno",machineInfoMap.get("yem_vehicleno"));//整机编号
                        addNew.set("yem_engineno",machineInfoMap.get("yem_engineno"));//发动机号
                        addNew.set("yem_enginemodel",machineInfoMap.get("yem_enginemodel"));//发动机型号
                        addNew.set("yem_enginevendor",machineInfoMap.get("yem_enginevendor"));//发动机厂商
                        addNew.set("yem_currentstate",machineInfoMap.get("yem_currentstate"));//当前状态
                        addNew.set("yem_currentstatedate",machineInfoMap.get("yem_currentstatedate"));//当前状态时间
                        addNew.set("yem_assembleupline",machineInfoMap.get("yem_assembleupline"));//总装上线
                        addNew.set("yem_upline",machineInfoMap.get("yem_upline"));//上线时间
                        addNew.set("yem_assembledownline",machineInfoMap.get("yem_assembledownline"));//总装下线
                        addNew.set("yem_downline",machineInfoMap.get("yem_downline"));//下线时间
                        addNew.set("yem_debugst",machineInfoMap.get("yem_debugst"));//调试
                        addNew.set("yem_debugstatdate",machineInfoMap.get("yem_debugstatdate"));//调试完成时间
                        addNew.set("yem_waitforinspection",machineInfoMap.get("yem_waitforinspection"));//待检
                        addNew.set("yem_waitforinspecdate",machineInfoMap.get("yem_waitforinspecdate"));//待检时间
                        addNew.set("yem_accuracytest",machineInfoMap.get("yem_accuracytest"));//精试
                        addNew.set("yem_accuratdate",machineInfoMap.get("yem_accuratdate"));//精试完成时间
                        addNew.set("yem_qualitytest",machineInfoMap.get("yem_qualitytest"));//质检
                        addNew.set("yem_qualitytestdate",machineInfoMap.get("yem_qualitytestdate"));//质检合格时间
                        addNew.set("yem_factputstor",machineInfoMap.get("yem_factputstor"));//入库
                        addNew.set("yem_factputstordate",machineInfoMap.get("yem_factputstordate"));//实际入库时间
                        addNew.set("yem_backonline",machineInfoMap.get("yem_backonline"));//返工上线
                        addNew.set("yem_backonlinedate",machineInfoMap.get("yem_backonlinedate"));//返工上线时间
                        addNew.set("yem_putintostorage",machineInfoMap.get("yem_putintostorage"));//改制入库
                        addNew.set("yem_putintostoragedate",machineInfoMap.get("yem_putintostoragedate"));//改制入库时间
                        addNew.set("yem_vehicleremark",machineInfoMap.get("yem_vehicleremark"));//备注
                        addNew.set("yem_ifdelivered",machineInfoMap.get("yem_ifdelivered"));//备注
                    }
                }
            }
        }
        SaveServiceHelper.save(salesOrders);
    }
}