
【场景】调整下游单据的link实体实现关联和反写
【二开案例】
(0)定义下游单据的关联实体配置,用作id存储关联关系的存放表

(1)定义转换规则
转换规则是必须的,因为关联关系中需要记录转换规则id

(2)插件代码,实现调整link并重新保存实现关联和反写

```csharp
using Kingdee.BOS.Core.Bill.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.Metadata;
using Kingdee.BOS.Core.Metadata.EntityElement;
using Kingdee.BOS.Core.Metadata.FieldElement;
using Kingdee.BOS.Core.SqlBuilder;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
namespace DynamicFormPlugIn.WriteBack
{
[Kingdee.BOS.Util.HotUpdate]
public class FormSamplePlugIn_ChangeLink : AbstractBillPlugIn
{
public override void BarItemClick(BarItemClickEventArgs e)
{
if (!string.Equals(e.BarItemKey, "BHR_tbButton", StringComparison.OrdinalIgnoreCase))
{
return;
}
ChangeLink();
}
private readonly string EntityKey = "FEntity";
private readonly string FieldKey = "F_BHR_LinkInfo";
private void ChangeLink()
{
Entity entity = this.View.BillBusinessInfo.GetEntity(EntityKey);
DynamicObjectCollection objColl = this.Model.GetEntityDataObject(entity);
if (objColl == null)
return;
bool hasChange = false;
foreach(var entityRowObj in objColl)
{
string linkInfo = ObjectUtils.Object2String(entityRowObj[FieldKey]);
if (linkInfo.IsNullOrEmptyOrWhiteSpace())
continue;
hasChange = true;
string[] info = linkInfo.Split(',');
string srcBillNo = info[0];//源单编号
int seq = ObjectUtils.Object2Int(info[1]);//分录序号行
ChangeRowLink(entityRowObj, srcBillNo, seq);
entityRowObj[FieldKey] = string.Empty;
}
//将更新的link信息保存到数据库,并计算单据的反写值
if (hasChange)
{
//如果没有界面就调用BusinessDataServiceHelper.Save
this.View.Model.Save();
this.View.ShowMessage("根据自定义信息重新关联完成");
}
}
private void ChangeRowLink(DynamicObject rowObj, string srcbillNo, int seq)
{
const string srcFormId = "kc0c27c005bcc4df9a8172dff6c36f5c4";//关联源单实体
const string srcEntityKey = "FEntity";//关联的源单实体
const string srcBillNoKey = "FBillNo";
FormMetadata metaData = Kingdee.BOS.ServiceHelper.MetaDataServiceHelper.Load(this.Context, srcFormId) as FormMetadata;
if (metaData == null)
return;
Field field = metaData.BusinessInfo.GetField(srcBillNoKey);
Entity srcEntity = metaData.BusinessInfo.GetEntity(srcEntityKey);
if (field == null || srcEntity == null)
return;
string entityIdKey = string.Concat(srcEntity.Key, "_", srcEntity.EntryPkFieldName);
string seqKey = string.Concat(srcEntity.Key, "_", srcEntity.SeqFieldKey);
//<1>根据单据编号和序号,获取到需要关联的单据内码 和 分录内码
QueryBuilderParemeter query = new Query