
1 . 新建服务器插件,集成AbstractOperationServicePlugIn,重写AfterExecuteOperationTransaction事件
```
public override void AfterExecuteOperationTransaction(AfterExecuteOperationTransaction e)
{
base.AfterExecuteOperationTransaction(e);
if (this.OperationResult.IsSuccess && this.OperationResult.OperateResult != null && this.OperationResult.OperateResult.Count > 0)
{
//自动下推处理
}
}
```
2 . 自动下推
```
// 获取源单与目标单直接的转换规则,如果规则未启用,则返回为空,注意容错
// 假设:上游单据FormId为sourceFormId,下游单据FormId为targetFormId
var rules = ConvertServiceHelper.GetConvertRules(this.Context, sourceFormId, targetFormId);
if (rules == null || rules.Count == 0)
{
//throw new KDBusinessException("", string.Format("未找到{0}到{1}之间,启用的转换规则,无法自动下推!", sourceFormId, targetFormId));
}
// 取勾选了默认选项的规则
var rule = rules.FirstOrDefault(t => t.IsDefault);
// 如果无默认规则,则取第一个
if (rule == null)
{
rule = rules[0];
}
// 开始构建下推参数:
// 待下推的源单数据行
var sourceBillMeta = MetaDataServiceHelper.Load(this.Context, sourceFormId) as FormMetadata;
// 获取单据提的属性名称
var entityPropertyName = ResearchFundCommonFunc.GetEntryName(sourceBillMeta.BusinessInfo, KEY_FENTITY);
List<ListSelectedRow> srcSelectedRows = new List<ListSelectedRow>();
// 把待下推的源单内码,逐个创建ListSelectedRow对象,添加到集合中
//srcSelectedRows.Add(new ListSelectedRow(billId.ToString(), string.Empty, 0, sourceFormId));
// 特别说明:上述代码,是整单下推;
// 如果需要指定待下推的单据体行,请参照下句代码,在ListSelectedRow中,指定EntryEntityKey以及EntryId
//查询构造下推数据
var entryEntityData = sourceBillData[entityPropertyName] as DynamicObjectCollection;
foreach (var row in entryEntityData)
{
string entityId = Convert.ToString(row[0]);
if (entityId == "0") continue;
srcSelectedRows.Add(new ListSelectedRow(Convert.ToString(sourceBillData["Id"]), entityId, 0, sourceFormId) { EntryEntityKey = KEY_FENTITY });
}
DrawArgs dArg = new DrawArgs(rule, null, srcSelectedRows.ToArray());
ConvertOperationResult operationResult = null;
// 自定义参数字典:把一些自定义参数,传递到转换插件中;转换插件再根据这些参数,进行特定处理
Dictionary<string, object> custParams = new Dictionary<string, object>();
try
{
// 组装下推参数对象
var pushArgs = new PushArgs(rule, srcSelectedRows.ToArray())
{
TargetBillTypeId = "", // 请设定目标单据单据类型。如无单据类型,可以空字符
TargetOrgId = 0, // 请设定目标单据主业务组织。如无主业务组织,可以为0
CustomParams = custParams // 可以传递额外附加的参数给单据转换插件,如无此需求,可以忽略
};
// 调用下推服务,生成下游单据数据包
operationResult = ConvertServiceHelper.Push(this.Context, pushArgs, OperateOption.Create());
}
catch (KDExceptionValidate ex)
{
if (ex.ValidateString == "不符合下推条件")
{
//**捕捉因为不符合自动下推条件而报的错误,后续捕捉判断并忽略**
throw new KDBusinessException("", "不符合下推条件");
}
else
{
throw new KDBusinessException("", ex.