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.utils.YEM;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
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.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;

import java.math.BigDecimal;

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().add("yem_businesssort");
        e.getFieldKeys().add("yem_ifgift");
        e.getFieldKeys().add("yem_materiel");
        e.getFieldKeys().add("yem_resourcescode");
        e.getFieldKeys().add("yem_milltype");
        e.getFieldKeys().add("yem_materialname");
        e.getFieldKeys().add("yem_materialenname");
        e.getFieldKeys().add("yem_specification");
        e.getFieldKeys().add("yem_unit");
        e.getFieldKeys().add("yem_needqty");
        e.getFieldKeys().add("yem_sapricepriceclause");
        e.getFieldKeys().add("yem_sapriceclausebase");
        e.getFieldKeys().add("yem_saamtpriceclause");
        e.getFieldKeys().add("yem_samtpriceclauseba");
        e.getFieldKeys().add("yem_baseunit");
        e.getFieldKeys().add("yem_baseqty");
        e.getFieldKeys().add("yem_storageqty");
        e.getFieldKeys().add("yem_vehicleno");
        e.getFieldKeys().add("yem_contractno");
        e.getFieldKeys().add("id");
        e.getFieldKeys().add("yem_accessorytype");
        e.getFieldKeys().add("yem_volvotype");
        e.getFieldKeys().add("yem_accesmaterial");
        e.getFieldKeys().add("yem_acunit");
        e.getFieldKeys().add("yem_acallqty");
        e.getFieldKeys().add("yem_saleorderno_p");
        e.getFieldKeys().add("yem_arrearsamt");
        e.getFieldKeys().add("yem_arrearsamtapply");

        e.getFieldKeys().add("yem_es_materialinfo");
        e.getFieldKeys().add("yem_billtype");
        e.getFieldKeys().add("yem_qty");
        e.getFieldKeys().add("yem_resourcenog");
        e.getFieldKeys().add("yem_es_salesorder_z");
        e.getFieldKeys().add("yem_iouamount");
    }

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

    @Override
    public void endOperationTransaction(EndOperationTransactionArgs e)
    {
        DynamicObject[] dataEntities = e.getDataEntities();
        for (DynamicObject dataEntitie : dataEntities) {
            dataEntitie = BusinessDataServiceHelper.loadSingle(dataEntitie.getPkValue(), dataEntitie.getDynamicObjectType().getName());
            collect(dataEntitie);
            arrearsAmt(dataEntitie);
            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");

                    if ("Z".equals(businesssort)) {
                        DynamicObjectCollection completemachines = materialinfo.getDynamicObjectCollection("yem_es_salesorder_z");
                        for (DynamicObject completemachine : completemachines) {
                            DynamicObject newEntryEntity = collects.addNew();

                            newEntryEntity.set("yem_isgifit_tl", materialinfo.getBoolean("yem_ifgift"));
                            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_impactprice1", materialinfo.getString("yem_impactprice"));
                        }
                    }
                    if ("P".equals(businesssort)) {
                        DynamicObject newEntryEntity = collects.addNew();

                        newEntryEntity.set("yem_isgifit_tl", materialinfo.getBoolean("yem_ifgift"));
                        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) {
            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));//本次发运应申请欠条金额
    }
}
