package com.yem.wm.im.transferorder.convert;

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.ExtendedDataEntitySet;
import kd.bos.entity.botp.plugin.AbstractConvertPlugIn;
import kd.bos.entity.botp.plugin.args.AfterConvertEventArgs;
import kd.bos.servicehelper.BusinessDataServiceHelper;

/**
 * @Description: 发货、供应仓库默认等于调出仓库
 *               需求、收货仓库默认等于调入仓库
 * @Date: 2024/5/30 16:25
 * @Created: by ZZSLL
 */

public class ShippingDetailToTransFerOrderConvertPlugin extends AbstractConvertPlugIn {
    @Override
    public void afterConvert(AfterConvertEventArgs e) {
        super.afterConvert(e);
        ExtendedDataEntitySet extDataEntitySet = e.getTargetExtDataEntitySet();
        ExtendedDataEntity[] extendedDataEntities = extDataEntitySet.FindByEntityKey(this.getTgtMainType().toString());
        for (ExtendedDataEntity extendedDataEntity : extendedDataEntities) {
            DynamicObject dataEntity = extendedDataEntity.getDataEntity();
            DynamicObjectCollection collection = dataEntity.getDynamicObjectCollection("yem_es_materialinfo");
            for (DynamicObject dynamicObject : collection) {
//                yem_cuswarehouse = yem_entryinwarehouse.id
//                yem_receinventory = yem_entryinwarehouse.id
                // 需求、收货仓库默认等于调入仓库
                long entryinwarehouse = dynamicObject.getLong("yem_entryinwarehouse.id");
                if (YEM.isNotEmpty(entryinwarehouse)) {
                    DynamicObject warehouse = BusinessDataServiceHelper.loadSingle(entryinwarehouse, "yem_warehouse");
                    dynamicObject.set("yem_cuswarehouse", warehouse);
                    dynamicObject.set("yem_receinventory", warehouse);
                }

                long entryoutwarehouse = dynamicObject.getLong("yem_entryoutwarehouse.id");
                if (YEM.isNotEmpty(entryoutwarehouse)) {
                    DynamicObject warehouse = BusinessDataServiceHelper.loadSingle(entryoutwarehouse, "yem_warehouse");
                    // 发货、供应仓库默认等于调出仓库
                    dynamicObject.set("yem_deliverywarehouse", warehouse);
                    dynamicObject.set("yem_supwarehouse", warehouse);
                }
            }
        }
    }
}
