插件案例 —— 下推携带多表体原创
金蝶云社区-MiLai
MiLai
15人赞赏了该文章 2,100次浏览 未经作者许可,禁止转载编辑于2020年08月03日 17:32:25

下推携带的时候,通版内容只能下推主单据体,但是有的时候,我们又想把其他单据体内容,这个时候,简单插件处理一些,就可以。千万不要想着用SQL查询,然后插入,下推不一定会先保存。



一、准备好,需要下推的主表体标识、二开携带的表体标识

  1. 下游主表体

    image.png

  2. 下游 二开携带表体

    image.png


二、配置单据转换插件

插件策略中挂载单据转换插件


image.png



三、插件代码

代码具有通用性,只需要标识变更,还需要根据字段查询变更即可


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using Kingdee.BOS;
using Kingdee.BOS.Util;
using Kingdee.BOS.Core;
using Kingdee.BOS.Core.Metadata;
using Kingdee.BOS.Core.Metadata.ConvertElement;
using Kingdee.BOS.Core.Metadata.ConvertElement.PlugIn;
using Kingdee.BOS.Core.Metadata.ConvertElement.PlugIn.Args;
using Kingdee.BOS.Core.Metadata.EntityElement;
using Kingdee.BOS.Core.Metadata.FormElement;
using Kingdee.BOS.Core.SqlBuilder;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Contracts;
using Kingdee.BOS.App;
using System.Data;
namespace 你的命名空间
{
    [HotUpdate]
    public class SalOrderOutStockCar : AbstractConvertPlugIn
    {
        ///
        /// 主单据体的字段携带完毕,与源单的关联关系创建好之后,触发此事件
        ///
        ///
        public override void OnAfterCreateLink(CreateLinkEventArgs e)
        {
            // 预先获取一些必要的元数据,后续代码要用到:
            // 源单第二单据体
            Entity srcSecondEntity = e.SourceBusinessInfo.GetEntity("F_BHR_Cars");
            // 目标单第一单据体
            Entity mainEntity = e.TargetBusinessInfo.GetEntity("FEntity");
            // 目标单第二单据体
            Entity secondEntity = e.TargetBusinessInfo.GetEntity("F_BHR_Cars");
            // 目标单关联子单据体
            Entity linkEntity = null;
            Form form = e.TargetBusinessInfo.GetForm();
            if (form.LinkSet != null
            && form.LinkSet.LinkEntitys != null
            && form.LinkSet.LinkEntitys.Count != 0)
            {
                linkEntity = e.TargetBusinessInfo.GetEntity(
                form.LinkSet.LinkEntitys[0].Key);
            }
            if (linkEntity == null)
            {
                return;
            }
            // 获取生成的全部下游单据
            ExtendedDataEntity[] billDataEntitys = e.TargetExtendedDataEntities.FindByEntityKey("FBillHead");
            // 对下游单据,逐张单据进行处理
            foreach (var item in billDataEntitys)
            {
                DynamicObject dataObject = item.DataEntity;
                // 定义一个集合,用于收集本单对应的源单内码
                HashSet<long> srcBillIds = new HashSet<long>();
                // 开始到主单据体中,读取关联的源单内码
                DynamicObjectCollection mainEntryRows =
                mainEntity.DynamicProperty.GetValue(dataObject) as DynamicObjectCollection;
                foreach (var mainEntityRow in mainEntryRows)
                {
                    DynamicObjectCollection linkRows =
                    linkEntity.DynamicProperty.GetValue(mainEntityRow) as DynamicObjectCollection;
                    foreach (var linkRow in linkRows)
                    {
                        long srcBillId = Convert.ToInt64(linkRow["SBillId"]);
                        if (srcBillId != 0
                        && srcBillIds.Contains(srcBillId) == false)
                        {
                            srcBillIds.Add(srcBillId);
                        }
                    }
                }
                if (srcBillIds.Count == 0)
                {
                    continue;
                }
                // 开始加载源单第二单据体上的字段
                // 确定需要加载的源单字段(仅加载需要携带的字段)
                List<SelectorItemInfo> selector = new List<SelectorItemInfo>();
                selector.Add(new SelectorItemInfo("F_BHR_BILLENTRYID"));
                selector.Add(new SelectorItemInfo("F_BHR_Material"));
                selector.Add(new SelectorItemInfo("F_BHR_UnitID"));
                selector.Add(new SelectorItemInfo("F_BHR_Qty"));
                selector.Add(new SelectorItemInfo("F_BHR_CAR"));
                // TODO: 继续添加其他需要携带的字段,示例代码略
                // 设置过滤条件
                string filter = string.Format(" {0} IN ({1}) ",
                e.SourceBusinessInfo.GetForm().PkFieldName,
                string.Join(",", srcBillIds));
                OQLFilter filterObj = OQLFilter.CreateHeadEntityFilter(filter);
                // 读取源单
                IViewService viewService = ServiceFactory.GetViewService(this.Context);
                var srcBillObjs = viewService.Load(this.Context,
                e.SourceBusinessInfo.GetForm().Id,
                selector,
                filterObj);
                // 开始把源单单据体数据,填写到目标单上
                DynamicObjectCollection secondEntryRows =
                secondEntity.DynamicProperty.GetValue(dataObject) as DynamicObjectCollection;
                secondEntryRows.Clear();  
                foreach (var srcBillObj in srcBillObjs)
                {
                    DynamicObjectCollection srcEntryRows =
                    srcSecondEntity.DynamicProperty.GetValue(srcBillObj) as DynamicObjectCollection;
                    foreach (var srcEntryRow in srcEntryRows)
                    {
                        // 目标单添加新行,并接受源单字段值
                        DynamicObject newRow = new DynamicObject(secondEntity.DynamicObjectType);
                        secondEntryRows.Add(newRow);
                        // 填写字段值
                        //newRow["F_BHR_BILLENTRYID"] = srcEntryRow["F_BHR_BILLENTRYID"];
                        newRow["F_BHR_Material"] = srcEntryRow["F_BHR_Material"];
                        newRow["F_BHR_UnitID"] = srcEntryRow["F_BHR_UnitID"];
                        newRow["F_BHR_Qty"] = srcEntryRow["F_BHR_Qty"];
                        newRow["F_BHR_CAR"] = srcEntryRow["F_BHR_CAR"];
                    }
                }
            }
        }
    }
}


图标赞 15
15人点赞
还没有人点赞,快来当第一个点赞的人吧!
图标打赏
0人打赏
还没有人打赏,快来当第一个打赏的人吧!