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

import com.alibaba.csp.sentinel.util.StringUtil;
import com.yem.wm.es.contractamtlist.util.FunUtil;
import com.yem.wm.es.shippingdetails.util.PaymentControlUtil;
import com.yem.wm.es.shippingdetails.validator.ShippingDetailsAuditValidator;
import com.yem.wm.utils.YEM;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.ExtendedDataEntity;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.AddValidatorsEventArgs;
import kd.bos.entity.plugin.PreparePropertysEventArgs;
import kd.bos.entity.plugin.args.BeginOperationTransactionArgs;
import kd.bos.entity.plugin.args.EndOperationTransactionArgs;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import static com.yem.wm.es.contractamtlist.util.FunUtil.getPayBills;
import static com.yem.wm.es.contractamtlist.util.FunUtil.getShipBills;

/**
 * @author power
 * @ClassName
 * @description: 出运明细单
 * @date 2023/10/18
 */
public class ShippingDetailsAuditOp extends AbstractOperationServicePlugIn {
    @Override
    public void onPreparePropertys(PreparePropertysEventArgs e) {
        e.getFieldKeys().addAll(this.billEntityType.getAllFields().keySet());
    }

    @Override
    public void onAddValidators(AddValidatorsEventArgs e) {
        super.onAddValidators(e);
        e.addValidator(new ShippingDetailsAuditValidator());
    }

    @Override
    public void beginOperationTransaction(BeginOperationTransactionArgs e) {
        super.beginOperationTransaction(e);
        DynamicObject[] eDataEntities = e.getDataEntities();
        for (DynamicObject dataEntity : eDataEntities) {
            dataEntity = BusinessDataServiceHelper.loadSingle(dataEntity.getPkValue(), dataEntity.getDynamicObjectType().getName());
            String name = dataEntity.getDynamicObjectType().getName();
            if (name.equals("yem_shippingdetails")) {
                collect(dataEntity);
            }
            arrearsAmt(dataEntity);
            SaveServiceHelper.save(new DynamicObject[]{dataEntity});
        }
    }

    @Override
    public void endOperationTransaction(EndOperationTransactionArgs e) {
        DynamicObject[] dataEntities = e.getDataEntities();
        for (DynamicObject dataEntitie : dataEntities) {
//            dataEntitie = BusinessDataServiceHelper.loadSingle(dataEntitie.getPkValue(), dataEntitie.getDynamicObjectType().getName());
            //2024-11-4 移动至事务前
//            collect(dataEntitie);
//            arrearsAmt(dataEntitie);
//            if (dataEntitie.getDynamicObjectType().getName().equals("yem_xshippingdetails")) {
//                DynamicObjectCollection yemEsSalesorderTotal = dataEntitie.getDynamicObjectCollection("yem_es_salesorder_total");
//                if (!yemEsSalesorderTotal.isEmpty()) {
//                    for (DynamicObject dynamicObject : yemEsSalesorderTotal) {
//                        BigDecimal yemQtyTl = dynamicObject.getBigDecimal("yem_qty_tl");
//                        BigDecimal yemAllocateqtyTl = dynamicObject.getBigDecimal("yem_allocateqty_tl");
//                        int i = dynamicObject.getInt("seq");
//                        String yemAccesmaterial = dynamicObject.getString("yem_accesmaterial_tl.name");
//                        boolean yemIsgifitTl = dynamicObject.getBoolean("yem_isgifit_tl");
//
//                        if (yemQtyTl.compareTo(yemAllocateqtyTl) < 0&&!yemIsgifitTl&&) {
//                            this.getOperationResult().setSuccess(true);
//                            this.getOperationResult().setMessage("出运汇总页签，第" + i + "行物料" + yemAccesmaterial + "，已调数量" + yemQtyTl + "，出运数量" + yemAllocateqtyTl + "，不允许审核，请修改调拨后处理！");    // 提示内容
//                            this.getOperationResult().setShowMessage(true);    // 是否显示提示消息
//                        }
//                    }
//                }
//                }

//                SaveServiceHelper.save(new DynamicObject[]{dataEntitie});
        }
    }

    /**
     * @description 审核时生成出运汇总分录
     * @author power
     */
    public static void collect(DynamicObject dataEntitie) {
        DynamicObjectCollection materialinfos = dataEntitie.getDynamicObjectCollection("yem_es_materialinfo");
        DynamicObjectCollection followcars = dataEntitie.getDynamicObjectCollection("yem_es_salesorder_p");
        DynamicObjectCollection collects = dataEntitie.getDynamicObjectCollection("yem_es_salesorder_total");
        if (materialinfos != null) {
            for (DynamicObject materialinfo : materialinfos) {
                if (StringUtil.isNotEmpty(materialinfo.getString("yem_businesssort"))) {
                    String businesssort = materialinfo.getString("yem_businesssort");

                    boolean yemIfgift = materialinfo.getBoolean("yem_ifgift");
                    if ("Z".equals(businesssort) && !yemIfgift) {
                        DynamicObjectCollection completemachines = materialinfo.getDynamicObjectCollection("yem_es_salesorder_z");
                        for (DynamicObject completemachine : completemachines) {
                            DynamicObject newEntryEntity = collects.addNew();
                            newEntryEntity.set("yem_isgifit_tl", yemIfgift);
                            newEntryEntity.set("yem_classification_tl", businesssort);
                            if (materialinfo.getDynamicObject("yem_materiel") != null) {
                                newEntryEntity.set("yem_accesmaterial_tl", materialinfo.getLong("yem_materiel.id"));
                            }
                            if (completemachine.getDynamicObject("yem_resourcescode") != null) {
                                newEntryEntity.set("yem_resource_tl", completemachine.getLong("yem_resourcescode.id"));
                            }
                            if (completemachine.getDynamicObject("yem_milltype") != null) {
                                newEntryEntity.set("yem_basicmodel_tl", completemachine.getLong("yem_milltype.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", BigDecimal.ONE);
                            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(completemachine.getString("yem_vehicleno"))) {
                                newEntryEntity.set("yem_zjnumber_tl", completemachine.getString("yem_vehicleno"));
                            }
                            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_constracteyid_tl", completemachine.getLong("yem_soureceid"));

                            // 汇总影响调拨价
                            newEntryEntity.set("yem_impactprice1", materialinfo.getString("yem_impactprice"));
                        }
                    }
                    if ("P".equals(businesssort) && !yemIfgift) {
                        DynamicObject newEntryEntity = collects.addNew();

                        newEntryEntity.set("yem_isgifit_tl", yemIfgift);
                        newEntryEntity.set("yem_classification_tl", 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"));
                    }
                    if (yemIfgift) {
                        DynamicObject newEntryEntity = collects.addNew();

                        newEntryEntity.set("yem_isgifit_tl", yemIfgift);
                        newEntryEntity.set("yem_classification_tl", 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 (materialinfo.getDynamicObject("yem_materiel")!=null) {
//                            newEntryEntity.set("yem_zjnumber_tl", materialinfo.getLong("yem_materiel.id"));
//                        }
                        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"));
                    }
                }
            }
        }
        if (followcars != null) {
            for (DynamicObject followcar : followcars) {
                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"));

            }
        }
    }


    /**
     * @description 计算欠款金额
     * @author xwudd
     * @param[1] dataEntity
     * return
     * @time 2023/11/9 14:24
     */

    public static void arrearsAmt(DynamicObject dataEntity) {
        String sourceBillType = dataEntity.getString("yem_sourcebilltype");
        long sourceBillId = dataEntity.getLong("yem_sourcebillid");
        DynamicObject saleOrder = BusinessDataServiceHelper.loadSingle(sourceBillId, sourceBillType);
        String saleorderno = dataEntity.getString("yem_saleorderno");
        if (YEM.isEmpty(saleorderno)) return;
        DynamicObject[] shipBills = getShipBills(saleOrder, dataEntity, "A");//出运
        DynamicObject[] payBills = getPayBills(saleOrder, dataEntity, "B");//收款单
        BigDecimal thisRealityAmt = PaymentControlUtil.getThisRealityAmt(saleOrder, shipBills, payBills, dataEntity);
        dataEntity.set("yem_arrearsamt", thisRealityAmt);//本次发运欠款金额
        dataEntity.set("yem_arrearsamtapply", FunUtil.getAbs(thisRealityAmt));//本次发运应申请欠条金额
    }

    /**
     * 变更单重新赋值汇总信息页签
     *
     * @param dataEntity 当前单据对象
     */
    private static void collecttotal(DynamicObject dataEntity) {
        //明细信息
        DynamicObjectCollection materialinfos = dataEntity.getDynamicObjectCollection("yem_es_materialinfo");
        //随机资料及配件工具包
        DynamicObjectCollection followcars = dataEntity.getDynamicObjectCollection("yem_es_salesorder_p");
        //汇总信息
        DynamicObjectCollection collects = dataEntity.getDynamicObjectCollection("yem_es_salesorder_total");
        if (materialinfos == null && followcars == null) {
            collects.clear();
            return;
        }
        if (materialinfos != null) {
            List<Long> newlsit = new ArrayList<>();
            newlsit.clear();
            List<Long> deletelsit = new ArrayList<>();
            deletelsit.clear();
            for (DynamicObject materialinfo : materialinfos) {
                String businesssort = materialinfo.getString("yem_businesssort");
                boolean yemIfgift = materialinfo.getBoolean("yem_ifgift");
                if (businesssort.length() > 0) {
                    if ("Z".equals(businesssort) && !yemIfgift) {
                        DynamicObjectCollection completemachines = materialinfo.getDynamicObjectCollection("yem_es_salesorder_z");
                        for (DynamicObject completemachine : completemachines) {
                            long sourceentryid = completemachine.getLong("yem_soureceid");
                            newlsit.add(sourceentryid);
                            for (int i = 0; i < collects.size(); i++) {
                                DynamicObject newEntryEntity = collects.get(i);
                                long constracteyidtl = newEntryEntity.getLong("yem_constracteyid_tl");
                                deletelsit.add(constracteyidtl);
                                if (sourceentryid == constracteyidtl) {
                                    newEntryEntity.set("yem_isgifit_tl", yemIfgift);
                                    newEntryEntity.set("yem_classification_tl", businesssort);
                                    if (materialinfo.getDynamicObject("yem_materiel") != null) {
                                        newEntryEntity.set("yem_accesmaterial_tl", materialinfo.getLong("yem_materiel.id"));
                                    }
                                    if (completemachine.getDynamicObject("yem_resourcescode") != null) {
                                        newEntryEntity.set("yem_resource_tl", completemachine.getLong("yem_resourcescode.id"));
                                    }
                                    if (completemachine.getDynamicObject("yem_milltype") != null) {
                                        newEntryEntity.set("yem_basicmodel_tl", completemachine.getLong("yem_milltype.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", BigDecimal.ONE);
                                    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(completemachine.getString("yem_vehicleno"))) {
                                        newEntryEntity.set("yem_zjnumber_tl", completemachine.getString("yem_vehicleno"));
                                    }
                                    if (StringUtil.isNotEmpty(materialinfo.getString("yem_contractno"))) {
                                        newEntryEntity.set("yem_constratnb_tl", materialinfo.getString("yem_contractno"));
                                    }
                                    // 汇总影响调拨价
                                    newEntryEntity.set("yem_impactprice1", materialinfo.getString("yem_impactprice"));
                                    deletelsit.remove(constracteyidtl);
                                    newlsit.remove(sourceentryid);
                                }
                            }
                            //删除
                            for (int j = 0; j < deletelsit.size(); j++) {
                                Long aLong = deletelsit.get(j);
                                for (int i = 0; i < collects.size(); i++) {
                                    DynamicObject newEntryEntity = collects.get(i);
                                    long constracteyid_tl = newEntryEntity.getLong("yem_constracteyid_tl");
                                    if (aLong == constracteyid_tl) {
                                        collects.remove(i);
                                    }
                                }
                            }
                            //新增
                            for (int i = 0; i < newlsit.size(); i++) {
                                Long aLong = newlsit.get(i);
                                for (DynamicObject llowcar : completemachines) {
                                    long constracteyid = llowcar.getLong("yem_soureceid");
                                    if (constracteyid == aLong) {
                                        DynamicObject newEntryEntity = collects.addNew();
                                        newEntryEntity.set("yem_isgifit_tl", yemIfgift);
                                        newEntryEntity.set("yem_classification_tl", businesssort);
                                        if (materialinfo.getDynamicObject("yem_materiel") != null) {
                                            newEntryEntity.set("yem_accesmaterial_tl", materialinfo.getLong("yem_materiel.id"));
                                        }
                                        if (completemachine.getDynamicObject("yem_resourcescode") != null) {
                                            newEntryEntity.set("yem_resource_tl", completemachine.getLong("yem_resourcescode.id"));
                                        }
                                        if (completemachine.getDynamicObject("yem_milltype") != null) {
                                            newEntryEntity.set("yem_basicmodel_tl", completemachine.getLong("yem_milltype.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", BigDecimal.ONE);
                                        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(completemachine.getString("yem_vehicleno"))) {
                                            newEntryEntity.set("yem_zjnumber_tl", completemachine.getString("yem_vehicleno"));
                                        }
                                        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", completemachine.getLong("yem_soureceid"));
                                        // 汇总影响调拨价
                                        newEntryEntity.set("yem_impactprice1", materialinfo.getString("yem_impactprice"));
                                    }

                                }
                            }

                        }
                    }
                    if ("P".equals(businesssort) && !yemIfgift) {
                        long sourceentryid = materialinfo.getLong("yem_sourceentryid");
                        for (int i = 0; i < collects.size(); i++) {
                            DynamicObject newEntryEntity = collects.get(i);
                            long constracteyidtl = newEntryEntity.getLong("yem_constracteyid_tl");
                            if (sourceentryid == constracteyidtl) {
                                newEntryEntity.set("yem_isgifit_tl", yemIfgift);
                                newEntryEntity.set("yem_classification_tl", 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_impactprice1", materialinfo.getString("yem_impactprice"));
                            }
                        }
                        //删除
                        for (int j = 0; j < deletelsit.size(); j++) {
                            Long aLong = deletelsit.get(j);
                            for (int i = 0; i < collects.size(); i++) {
                                DynamicObject newEntryEntity = collects.get(i);
                                long constracteyid_tl = newEntryEntity.getLong("yem_constracteyid_tl");
                                if (aLong == constracteyid_tl) {
                                    collects.remove(i);
                                }
                            }
                        }
                        //新增
                        for (int i = 0; i < newlsit.size(); i++) {
                            Long aLong = newlsit.get(i);
                            for (DynamicObject llowcar : materialinfos) {
                                long constracteyid = llowcar.getLong("yem_sourceentryid");
                                if (constracteyid == aLong) {
                                    DynamicObject newEntryEntity = collects.addNew();
                                    newEntryEntity.set("yem_isgifit_tl", yemIfgift);
                                    newEntryEntity.set("yem_classification_tl", 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"));
                                }
                            }
                        }
                    }
                    if (yemIfgift) {
                        long sourceentryid = materialinfo.getLong("yem_sourceentryid");
                        for (int i = 0; i < collects.size(); i++) {
                            DynamicObject newEntryEntity = collects.get(i);
                            long constracteyidtl = newEntryEntity.getLong("yem_constracteyid_tl");
                            if (sourceentryid == constracteyidtl) {
                                newEntryEntity.set("yem_isgifit_tl", yemIfgift);
                                newEntryEntity.set("yem_classification_tl", 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_impactprice1", materialinfo.getString("yem_impactprice"));
                            }
                        }
                        //删除
                        for (int j = 0; j < deletelsit.size(); j++) {
                            Long aLong = deletelsit.get(j);
                            for (int i = 0; i < collects.size(); i++) {
                                DynamicObject newEntryEntity = collects.get(i);
                                long constracteyid_tl = newEntryEntity.getLong("yem_constracteyid_tl");
                                if (aLong == constracteyid_tl) {
                                    collects.remove(i);
                                }
                            }
                        }
                        //新增
                        for (int i = 0; i < newlsit.size(); i++) {
                            Long aLong = newlsit.get(i);
                            for (DynamicObject llowcar : materialinfos) {
                                long constracteyid = llowcar.getLong("yem_sourceentryid");
                                if (constracteyid == aLong) {
                                    DynamicObject newEntryEntity = collects.addNew();
                                    newEntryEntity.set("yem_isgifit_tl", yemIfgift);
                                    newEntryEntity.set("yem_classification_tl", 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"));
                                }
                            }
                        }
                    }
                }
            }
        }

        if (followcars != null) {
            List<Long> newlsit = new ArrayList<>();
            newlsit.clear();
            List<Long> deletelsit = new ArrayList<>();
            deletelsit.clear();
            for (DynamicObject followcar : followcars) {
                long constracteyid_p = followcar.getLong("yem_constracteyid_p");
                newlsit.add(constracteyid_p);
                for (int i = 0; i < collects.size(); i++) {
                    DynamicObject newEntryEntity = collects.get(i);
                    long constracteyid_tl = newEntryEntity.getLong("yem_constracteyid_tl");
                    deletelsit.add(constracteyid_tl);
                    if (constracteyid_p == constracteyid_tl) {
                        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_constractioid_tl", followcar.getLong("id"));
                        deletelsit.remove(constracteyid_tl);
                        newlsit.remove(constracteyid_p);
                    }
                }
                //删除
                for (int j = 0; j < deletelsit.size(); j++) {
                    Long aLong = deletelsit.get(j);
                    for (int i = 0; i < collects.size(); i++) {
                        DynamicObject newEntryEntity = collects.get(i);
                        long constracteyid_tl = newEntryEntity.getLong("yem_constracteyid_tl");
                        if (aLong == constracteyid_tl) {
                            collects.remove(i);
                        }
                    }
                }
                //新增
                for (int i = 0; i < newlsit.size(); i++) {
                    Long aLong = newlsit.get(i);
                    for (DynamicObject llowcar : followcars) {
                        long constracteyid = llowcar.getLong("yem_constracteyid_p");
                        if (constracteyid == aLong) {
                            DynamicObject newEntryEntity = collects.addNew();
                            newEntryEntity.set("yem_isgifit_tl", true);
                            newEntryEntity.set("yem_classification_tl", "A");
                            if (llowcar.getDynamicObject("yem_accessorytype") != null) {
                                newEntryEntity.set("yem_basicmodel_tl", llowcar.getLong("yem_accessorytype.id"));
                            }
                            if (StringUtil.isNotEmpty(llowcar.getString("yem_volvotype"))) {
                                newEntryEntity.set("yem_volvotype_tl", llowcar.getString("yem_volvotype"));
                            }
                            if (llowcar.getDynamicObject("yem_accesmaterial") != null) {
                                newEntryEntity.set("yem_accesmaterial_tl", llowcar.getLong("yem_accesmaterial.id"));
                                newEntryEntity.set("yem_materialname_tl", llowcar.getString("yem_accesmaterial.name"));
                                newEntryEntity.set("yem_materialenname_tl", llowcar.getString("yem_accesmaterial.yem_nameen"));
                                newEntryEntity.set("yem_specification_tl", llowcar.getString("yem_accesmaterial.modelnum"));
                            }
                            if (llowcar.getDynamicObject("yem_acunit") != null) {
                                newEntryEntity.set("yem_acunit_tl", llowcar.getLong("yem_acunit.id"));
                                newEntryEntity.set("yem_unitfieldbase_tl", llowcar.getLong("yem_acunit.id"));
                            }
                            if (StringUtil.isNotEmpty(llowcar.getString("yem_saleorderno_p"))) {
                                newEntryEntity.set("yem_constratnb_tl", llowcar.getString("yem_saleorderno_p"));
                            }
                            newEntryEntity.set("yem_qty_tl", llowcar.getBigDecimal("yem_acallqty"));
                            newEntryEntity.set("yem_qtybase_tl", llowcar.getBigDecimal("yem_acallqty"));
                            newEntryEntity.set("yem_constracteyid_tl", llowcar.getLong("yem_constracteyid_p"));
                            newEntryEntity.set("yem_constractioid_tl", llowcar.getLong("id"));
                        }

                    }
                }
            }

        }
    }
}
