package com.yem.wm.es.shippingdetails.convert;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.ExtendedDataEntity;
import kd.bos.entity.botp.plugin.AbstractConvertPlugIn;
import kd.bos.entity.botp.plugin.args.AfterConvertEventArgs;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.servicehelper.BusinessDataServiceHelper;

import java.util.*;

/**
 * @Classname ShippingDetailToTransferOrderConvert
 * @Description TODO
 * @Date 2023/12/16 17:12
 * @Created by ZZSLL
 */

public class ShippingDetailToTransferOrderConvert extends AbstractConvertPlugIn
{
    @Override
    public void afterConvert(AfterConvertEventArgs e)
    {
        super.afterConvert(e);
        // 目标单据
        String targetName = this.getTgtMainType().getName();
        // 来源单据
        String sourceName = this.getSrcMainType().getName();
        if (!"yem_transferorder".equals(targetName) || !"yem_shippingdetails".equals(sourceName)) {
            return;
        }
        ExtendedDataEntity[] findByEntityKeys = e.getTargetExtDataEntitySet().FindByEntityKey(targetName);
        for (ExtendedDataEntity findByEntityKey : findByEntityKeys) {
            // origin data
            DynamicObject dataEntity = findByEntityKey.getDataEntity();
            long sourcebillid = dataEntity.getLong("yem_sourcebillid");
            DynamicObject sourceEntryData = BusinessDataServiceHelper.loadSingle(sourcebillid, sourceName);
            // 商品明细
            DynamicObjectCollection materialinfo = sourceEntryData.getDynamicObjectCollection("yem_es_materialinfo");

            // 出运汇总
            DynamicObjectCollection salesorderTotal = sourceEntryData.getDynamicObjectCollection("yem_es_salesorder_total");

            Map<Long, DynamicObjectCollection> map = new LinkedHashMap<>();

            for (DynamicObject object : salesorderTotal) {

                // 明细行号ID
                long id = object.getLong("yem_constractioid_tl");

                for (DynamicObject materialinfoEntitiy : materialinfo) {
                    if (id == materialinfoEntitiy.getLong("id")) {
                        map.put(object.getLong("id"), materialinfoEntitiy.getDynamicObjectCollection("yem_multiconfig"));
                    }
                }
            }

            Set<String> set = new HashSet<>();

            DynamicObjectCollection targetMaterialinfo = dataEntity.getDynamicObjectCollection("yem_es_materialinfo");
            for (DynamicObject object : targetMaterialinfo) {
                long sourceentryid = object.getLong("yem_sourceentryid");
                if (map.containsKey(sourceentryid)) {
                    DynamicObjectCollection multiconfig = object.getDynamicObjectCollection("yem_multiconfig");

                    DynamicObjectCollection collection = map.get(sourceentryid);
                    for (DynamicObject dynamicObject : collection) {
                        DynamicObject addNew = multiconfig.addNew();
                        addNew.set("yem_configurtype", dynamicObject.get("yem_configurtype"));
                        addNew.set("yem_impactprice1", dynamicObject.get("yem_impactprice11"));
                        addNew.set("yem_productmodel_m", dynamicObject.get("yem_productmodel_m"));
                        addNew.set("yem_basemodel_m", dynamicObject.get("yem_basemodel_m"));
                        addNew.set("yem_configurname", dynamicObject.get("yem_configurname"));
                        addNew.set("yem_configurtypeen", dynamicObject.get("yem_configurtypeen"));
                        addNew.set("yem_specification_o", dynamicObject.get("yem_specification_o"));
                        addNew.set("yem_arguments", dynamicObject.get("yem_arguments"));
                        addNew.set("yem_remarkss", dynamicObject.get("yem_remarkss"));
                        addNew.set("yem_marketprice", dynamicObject.get("yem_marketprice"));
                        addNew.set("yem_marketraisepribase", dynamicObject.get("yem_marketraisepribase"));
                        addNew.set("yem_marketraiseprice", dynamicObject.get("yem_marketraiseprice"));
                        addNew.set("yem_unitsetnumber", dynamicObject.get("yem_unitsetnumber"));
                        addNew.set("yem_unitsetamountbase", dynamicObject.get("yem_unitsetamountbase"));
                        addNew.set("yem_unitsetamount", dynamicObject.get("yem_unitsetamount"));
                    }
                }

                String impactprice = object.getString("yem_impactprice");
                set.add(impactprice);
            }

            if (set.contains("A")) {
                dataEntity.set("yem_impactprices", "A");
            } else {
                dataEntity.set("yem_impactprices", "B");
            }
        }
    }
}
