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

import com.alibaba.csp.sentinel.util.StringUtil;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.PreparePropertysEventArgs;
import kd.bos.entity.plugin.args.BeforeOperationArgs;
import kd.bos.servicehelper.operation.DeleteServiceHelper;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ljw
 * @date 2024/11/8 17:42
 * @description XshippingDetailsSaveOp
 */
public class XshippingDetailsSaveOp extends AbstractOperationServicePlugIn {
    @Override
    public void onPreparePropertys(PreparePropertysEventArgs e) {
        e.getFieldKeys().addAll(this.billEntityType.getAllFields().keySet());
    }

    @Override
    public void beforeExecuteOperationTransaction(BeforeOperationArgs e) {
        super.beforeExecuteOperationTransaction(e);
        DynamicObject[] eDataEntities = e.getDataEntities();
        for (int i = 0; i < eDataEntities.length; i++) {
            DynamicObject dataEntitie = eDataEntities[i];
            DynamicObjectCollection collects = dataEntitie.getDynamicObjectCollection("yem_es_salesorder_total");
            DynamicObjectCollection followcars = dataEntitie.getDynamicObjectCollection("yem_es_salesorder_p");
            DynamicObjectCollection materialinfos = dataEntitie.getDynamicObjectCollection("yem_es_materialinfo");
            Map<Long, String> map = new HashMap<>();
            for (DynamicObject collect : collects) {
                DynamicObject yemAccesmaterialTl = collect.getDynamicObject("yem_accesmaterial_tl");
                if (yemAccesmaterialTl != null) {
                    long aLong = yemAccesmaterialTl.getLong("id");
                    String tlString = yemAccesmaterialTl.getString("number");
                    map.merge(aLong, tlString, (a, b) -> a + "," + b);
                }
            }
            for (DynamicObject followcar : followcars) {
                DynamicObject yemAccesmaterial = followcar.getDynamicObject("yem_accesmaterial");
                if (yemAccesmaterial != null) {
                    long aLong = yemAccesmaterial.getLong("id");
                    if (map.get(aLong) == null) {
                        DynamicObject newEntryEntity = collects.addNew();
                        newEntryEntity.set("yem_isgifit_tl", true);
                        newEntryEntity.set("yem_classification_tl", "A");
                        if (followcar.getDynamicObject("yem_accessorytype") != null) {
                            newEntryEntity.set("yem_basicmodel_tl", followcar.getLong("yem_accessorytype.id"));
                        }
                        if (StringUtil.isNotEmpty(followcar.getString("yem_volvotype"))) {
                            newEntryEntity.set("yem_volvotype_tl", followcar.getString("yem_volvotype"));
                        }
                        if (followcar.getDynamicObject("yem_accesmaterial") != null) {
                            newEntryEntity.set("yem_accesmaterial_tl", followcar.getLong("yem_accesmaterial.id"));
                            newEntryEntity.set("yem_materialname_tl", followcar.getString("yem_accesmaterial.name"));
                            newEntryEntity.set("yem_materialenname_tl", followcar.getString("yem_accesmaterial.yem_nameen"));
                            newEntryEntity.set("yem_specification_tl", followcar.getString("yem_accesmaterial.modelnum"));
                        }
                        if (followcar.getDynamicObject("yem_acunit") != null) {
                            newEntryEntity.set("yem_acunit_tl", followcar.getLong("yem_acunit.id"));
                            newEntryEntity.set("yem_unitfieldbase_tl", followcar.getLong("yem_acunit.id"));
                        }
                        if (StringUtil.isNotEmpty(followcar.getString("yem_saleorderno_p"))) {
                            newEntryEntity.set("yem_constratnb_tl", followcar.getString("yem_saleorderno_p"));
                        }
                        newEntryEntity.set("yem_qty_tl", followcar.getBigDecimal("yem_acallqty"));
                        newEntryEntity.set("yem_qtybase_tl", followcar.getBigDecimal("yem_acallqty"));
                        newEntryEntity.set("yem_constracteyid_tl", followcar.getLong("yem_constracteyid_p"));
                        newEntryEntity.set("yem_constractioid_tl", followcar.getLong("id"));

                    }
                }
            }
            for (DynamicObject materialinfo : materialinfos) {
                DynamicObject yemMateriel = materialinfo.getDynamicObject("yem_materiel");
                if (yemMateriel != null) {
                    long aLong = yemMateriel.getLong("id");
                    if (map.get(aLong) == null) {
                        DynamicObject newEntryEntity = collects.addNew();

                        newEntryEntity.set("yem_isgifit_tl", materialinfo.getBoolean("yem_ifgift"));
                        newEntryEntity.set("yem_classification_tl", materialinfo.getString("yem_businesssort"));
                        if (materialinfo.getDynamicObject("yem_materiel") != null) {
                            newEntryEntity.set("yem_accesmaterial_tl", materialinfo.getLong("yem_materiel.id"));
                        }
                        if (materialinfo.getDynamicObject("yem_resourcenog") != null) {
                            newEntryEntity.set("yem_resource_tl", materialinfo.getLong("yem_resourcenog.id"));
                        }
                        if (materialinfo.getDynamicObject("yem_productmodel") != null) {
                            newEntryEntity.set("yem_basicmodel_tl", materialinfo.getLong("yem_productmodel.yem_products.id"));
                        }
                        if (StringUtil.isNotEmpty(materialinfo.getString("yem_materialname"))) {
                            newEntryEntity.set("yem_materialname_tl", materialinfo.getString("yem_materialname"));
                        }
                        if (StringUtil.isNotEmpty(materialinfo.getString("yem_materialenname"))) {
                            newEntryEntity.set("yem_materialenname_tl", materialinfo.getString("yem_materialenname"));
                        }
                        if (StringUtil.isNotEmpty(materialinfo.getString("yem_specification"))) {
                            newEntryEntity.set("yem_specification_tl", materialinfo.getString("yem_specification"));
                        }
                        if (materialinfo.getDynamicObject("yem_unit") != null) {
                            newEntryEntity.set("yem_acunit_tl", materialinfo.getLong("yem_unit.id"));
                        }
                        newEntryEntity.set("yem_qty_tl", materialinfo.getBigDecimal("yem_qty"));
                        newEntryEntity.set("yem_constractqty_tl", materialinfo.getBigDecimal("yem_needqty"));
                        //销售单价（价格条款）
                        newEntryEntity.set("yem_sapepeclause_tl", materialinfo.getBigDecimal("yem_sapricepriceclause"));
                        newEntryEntity.set("yem_sapecebase_tl", materialinfo.getBigDecimal("yem_sapriceclausebase"));
                        //销售金额（价格条款）
                        newEntryEntity.set("yem_saamtpecle_tl", materialinfo.getBigDecimal("yem_saamtpriceclause"));
                        newEntryEntity.set("yem_samtpeceba_tl", materialinfo.getBigDecimal("yem_samtpriceclauseba"));

                        if (materialinfo.getDynamicObject("yem_baseunit") != null) {
                            newEntryEntity.set("yem_unitfieldbase_tl", materialinfo.getLong("yem_baseunit.id"));
                        }
                        newEntryEntity.set("yem_qtybase_tl", materialinfo.getBigDecimal("yem_baseqty"));
                        newEntryEntity.set("yem_storageqty_tl", materialinfo.getBigDecimal("yem_storageqty"));

                        if (StringUtil.isNotEmpty(materialinfo.getString("yem_contractno"))) {
                            newEntryEntity.set("yem_constratnb_tl", materialinfo.getString("yem_contractno"));
                        }
                        newEntryEntity.set("yem_constractioid_tl", materialinfo.getLong("id"));
                        newEntryEntity.set("yem_constracteyid_tl", materialinfo.getLong("yem_sourceentryid"));

                        // 汇总影响调拨价
                        newEntryEntity.set("yem_impactprice1", materialinfo.getString("yem_impactprice"));
                    }
                }
            }
        }
    }
}
