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

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.args.BeginOperationTransactionArgs;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;

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

/**
 * @Description 发货联系书删除
 * @Date 2024/1/11 15:49
 * @Created by ZZSLL
 */

public class ContactBookDeleteOp extends AbstractOperationServicePlugIn
{
    @Override
    public void beginOperationTransaction(BeginOperationTransactionArgs e)
    {
        super.beginOperationTransaction(e);
        DynamicObject[] eDataEntities = e.getDataEntities();
        for (DynamicObject dataEntity : eDataEntities) {
            dataEntity = BusinessDataServiceHelper.loadSingle(dataEntity.getPkValue(), dataEntity.getDataEntityType().getName());
            long id = dataEntity.getLong("yem_sourcebillid");
            // 订舱通知单
            DynamicObject storagetrans = BusinessDataServiceHelper.loadSingle(id, "yem_es_storagetrans");
            if (storagetrans == null) continue;
            DynamicObjectCollection materialinfo = dataEntity.getDynamicObjectCollection("yem_es_materialinfo");
            List<Long> list = new ArrayList<>();
            for (DynamicObject dynamicObject : materialinfo) {
                list.add(dynamicObject.getLong("yem_sourceentryid"));
            }
            DynamicObjectCollection collection = storagetrans.getDynamicObjectCollection("yem_es_materialinfo");
            for (Long entryId : list) {
                for (DynamicObject dynamicObject : collection) {
                    Long pkValue = (Long) dynamicObject.getPkValue();
                    if (Objects.equals(entryId, pkValue)) {
                        dynamicObject.set("yem_contactbookqty", BigDecimal.ZERO);
                    }
                }
            }
            SaveServiceHelper.save(new DynamicObject[]{storagetrans});
        }
    }
}
