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

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.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 java.math.BigDecimal;
import java.util.*;

/**
 * @author xwudd
 * @Description 撤销插件 名称写错了
 * @date 11:34
 **/
public class DeliverNoticeUnAuditOp extends AbstractOperationServicePlugIn {
    @Override
    public void endOperationTransaction(EndOperationTransactionArgs e) {
        super.endOperationTransaction(e);
        DynamicObject[] dataEntities = e.getDataEntities();
        for(DynamicObject dataEntitie : dataEntities) {
            dataEntitie = BusinessDataServiceHelper.loadSingle(dataEntitie.getPkValue(), dataEntitie.getDynamicObjectType().getName());
            delShipCounterDeliver(dataEntitie);
            setValueToStorage(dataEntitie);
            emptyRealityAmt(dataEntitie);
            SaveServiceHelper.save(new DynamicObject[]{dataEntitie});
        }
    }

    /**
     * @description 删除出运对应发货信息
     * @author xwudd
     * @param[1] dataEntitie
     * return
     * @time 2023/11/2 14:14
     */

    public void delShipCounterDeliver(DynamicObject dataEntitie){
        DynamicObjectCollection materialInfos = dataEntitie.getDynamicObjectCollection("yem_es_materialinfo");
        for (DynamicObject materialInfo : materialInfos) {
            DynamicObject shippingDetails = BusinessDataServiceHelper.loadSingle(materialInfo.getLong("yem_sourcebillid_d"), materialInfo.getString("yem_sourcebilltype_e"));
            if (YEM.isNotEmpty(shippingDetails)) {
                long deliverEntryId = materialInfo.getLong("id");
                long sourceEntryId = materialInfo.getLong("yem_sourceentryid");
                DynamicObjectCollection shipMaterialInfos = shippingDetails.getDynamicObjectCollection("yem_es_materialinfo");
                for (DynamicObject shipMaterialInfo : shipMaterialInfos) {
                    long id = shipMaterialInfo.getLong("id");
                    if (id == sourceEntryId) {
                        List<DynamicObject> objs = new ArrayList<>();
                        DynamicObjectCollection shipDeliverNotices = shipMaterialInfo.getDynamicObjectCollection("yem_delivernotice");
                        for (int i = 0; i < shipDeliverNotices.size(); i++) {
                            DynamicObject shipDeliverNotice = shipDeliverNotices.get(i);
                            long deliverEntryId_d = shipDeliverNotice.getLong("yem_deliverentryid_d");
                            if(deliverEntryId == deliverEntryId_d){
                                objs.add(shipDeliverNotice);
                            }
                        }
                        for(DynamicObject obj : objs){
                            shipDeliverNotices.remove(obj);
                        }
                    }
                }
                SaveServiceHelper.save(new DynamicObject[]{shippingDetails});
            }
        }
    }


    /**
     * @description 撤销储运托单 关联发货数量  发货通知单单号
     * @author zlp
     * @param[1] dataEntitie
     * return
     * @time 2023/11/10
     */
    public static void setValueToStorage(DynamicObject dataEntitie)
    {
        //key 销售出库单Id  value 出运明细分录行数量
        Map<Long, List<BigDecimal>> map = new HashMap<>();
        DynamicObjectCollection entryDeliver = dataEntitie.getDynamicObjectCollection("yem_es_materialinfo");

        for (DynamicObject entry : entryDeliver) {
            map.put(entry.getLong("yem_sourceentryid"), Arrays.asList(entry.getBigDecimal("yem_qty"), entry.getBigDecimal("yem_amt")));
        }
        Set<Long> longs = map.keySet();

        DynamicObject[] storagetrans = BusinessDataServiceHelper.load("yem_es_storagetrans", "yem_es_materialinfo.yem_sourceentryid" +
                ",yem_es_materialinfo.yem_adstqty,yem_es_materialinfo.yem_sgnenumber,yem_es_materialinfo.yem_adstamount", new QFilter[]{
                new QFilter("yem_es_materialinfo.yem_sourceentryid", QCP.in, longs)
        });
        if (storagetrans != null && storagetrans.length > 0) {
            for (DynamicObject dt : storagetrans) {
                DynamicObjectCollection materialinfo = dt.getDynamicObjectCollection("yem_es_materialinfo");
                for (DynamicObject entry : materialinfo) {
                    long sourceentryid = entry.getLong("yem_sourceentryid");
                    if (map.containsKey(sourceentryid)) {

                        BigDecimal yemAdstqty = entry.getBigDecimal("yem_adstqty");
                        BigDecimal yemAdstamount = entry.getBigDecimal("yem_adstamount");
                        BigDecimal qty = map.get(sourceentryid).get(0);
                        if (qty != null) {
                            entry.set("yem_adstqty", yemAdstqty.subtract(qty));
                        }
                        BigDecimal amt = map.get(sourceentryid).get(1);
                        if (amt != null) {
                            entry.set("yem_adstamount", yemAdstamount.subtract(amt));
                        }
                        String sgnenumber = entry.getString("yem_sgnenumber");
                        if (YEM.isNotEmpty(sgnenumber)) {
                            String billno = dataEntitie.getString("billno");
                            if (sgnenumber.contains(";" + billno)) {
                                entry.set("yem_sgnenumber", sgnenumber.replace(";" + billno, ""));
                            } else if (sgnenumber.contains(billno)) {
                                entry.set("yem_sgnenumber", sgnenumber.replace(billno, ""));
                            }

                        }

                    }
                }
            }
            SaveServiceHelper.save(storagetrans);
        }
    }

    /**
     * @description 清空欠款相关字段
     * @author xwudd
     * @param[1] dataEntitie
     * return
     * @time 2023/11/16 9:45
     */

    public void emptyRealityAmt(DynamicObject dataEntitie){
        dataEntitie.set("yem_contractallamt",null);//合同所有收款
        dataEntitie.set("yem_contractadvanceamt",null);//合同的预收款
        dataEntitie.set("yem_deliveramt",null);//已发货金额
        dataEntitie.set("yem_thisdeliveramt",null);//本次发货尾款
        dataEntitie.set("yem_thisrealityamt",null);//本次发货实际收款总欠款金额
    }
}
