package com.yem.wm.es.storagetrans.convert;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.ExtendedDataEntity;
import kd.bos.entity.botp.plugin.AbstractConvertPlugIn;
import kd.bos.entity.botp.plugin.args.AfterConvertEventArgs;
import kd.bos.servicehelper.BusinessDataServiceHelper;

import java.util.HashSet;
import java.util.Set;

/**
 * @Description 订舱通知单 -> 发货联系书
 * @Date 2024/1/10 10:48
 * @Created by ZZSLL
 */

public class StorageTransToContactBookConvert extends AbstractConvertPlugIn
{
    @Override
    public void afterConvert(AfterConvertEventArgs e)
    {
        super.afterConvert(e);
        String tgtName = this.getTgtMainType().getName();
        ExtendedDataEntity[] findByEntityKeys = e.getTargetExtDataEntitySet().FindByEntityKey(tgtName);
        for (ExtendedDataEntity findByEntityKey : findByEntityKeys) {
            DynamicObject dataEntity = findByEntityKey.getDataEntity();
            Set<String> category = new HashSet<>();
            DynamicObjectCollection collection = dataEntity.getDynamicObjectCollection("yem_es_materialinfo");
            if (collection.isEmpty()) continue;
            dataEntity.set("yem_producttype", collection.get(0).getDynamicObject("yem_products"));
            for (DynamicObject dynamicObject : collection) {
                DynamicObject products = dynamicObject.getDynamicObject("yem_products");
                DynamicObject single = BusinessDataServiceHelper.loadSingle(products.getPkValue(), products.getDynamicObjectType().getName(), "yem_businesssort");
                if (single == null) continue;
                category.add(single.getString("yem_businesssort"));
            }
            if (category.contains("P")) {
                dataEntity.set("yem_procbilltype", "P");
            } else {
                dataEntity.set("yem_procbilltype", "Z");
            }
        }
    }
}
