多单据体携带转换通用插件原创
14人赞赏了该文章
2,749次浏览
编辑于2020年08月20日 17:18:32
多单据体携带,建立第二个单据体转换规则和第一个主转换规则的名称一样后面加次序1234
比如 第一个转换规则代码xxx
那么建立第二第三个转换规则 xxx1,xxx2
最多10个
把插件挂在第一个转换规则下,然后第二个第三个转换规则隐藏,这样就可以携带多单据体携带
通用插件,前台配置后台转换,不需要代码。
{ [HotUpdate, Description("第二单具体携带")] public class secondentitytakePlug : AbstractConvertPlugIn { public override void AfterConvert(AfterConvertEventArgs e) { string pruleKey = e.Configuration.Rule.Id; string Entrykey = ""; string srcentitykey = e.Configuration.SourceEntryKey; string dstentitykey = e.Configuration.TargetEntryKey; string linkentitykey = string.Format("{0}_Link", dstentitykey); BusinessInfo srcInfo = e.SourceBusinessInfo; EntryEntity srcEntity = srcInfo.GetEntryEntity(srcentitykey);//源单单据体 EntryEntity targetEntity = e.TargetBusinessInfo.GetEntryEntity(dstentitykey);//目标单单据体 var dataEntities = e.Result.FindByEntityKey("FBillHead"); //获取目标单据 foreach (var dataEntity in dataEntities) { long sbillId = 0; //获取原单 //目标单据分录 DynamicObjectCollection dstEntity = targetEntity.DynamicProperty.GetValue<DynamicObjectCollection>(dataEntity.DataEntity); DynamicObject dstRouteEntryRow = new DynamicObject(dstEntity.DynamicCollectionItemPropertyType); List<ListSelectedRow> selectedRows = new List<ListSelectedRow>(); foreach (var dstrow in dstEntity) { DynamicObjectCollection FEntity_Link = dstrow[linkentitykey] as DynamicObjectCollection; sbillId = Convert.ToInt64(FEntity_Link[0]["SBillId"]); long sId = Convert.ToInt64(FEntity_Link[0]["SId"]); var sourceDataEntities = dataEntity["ConvertSource"] as IEnumerable<DynamicObject>; if (selectedRows.Where(t => t.PrimaryKeyValue.EqualsIgnoreCase(sbillId.ToString())).ToArray().Length==0) { ListSelectedRow row = new ListSelectedRow(sbillId.ToString(), string.Empty, 0, e.Configuration.Rule.SourceFormId); selectedRows.Add(row); } } //下推 for (int i = 1; i < 10; i++) { var ruleKey = string.Format("{0}{1}",pruleKey ,i); var rules = ConvertServiceHelper.GetConvertRules(this.Context, e.Configuration.Rule.SourceFormId, e.Configuration.Rule.TargetFormId); var rule = rules.FirstOrDefault((ConvertRuleElement t) => t.Key.EqualsIgnoreCase(ruleKey) || t.Id.EqualsIgnoreCase(ruleKey)); if (rule == null) continue; var police = rule.Policies[2] as DefaultConvertPolicyElement; Entrykey = police.TargetEntryKey; var destentity = e.TargetBusinessInfo.GetEntity(Entrykey); if (rule == null) continue; ConvertOperationResult operationResult = null; Dictionary<string, object> custParams = new Dictionary<string, object>(); try { PushArgs pushArgs = new PushArgs(rule, selectedRows.ToArray()) { TargetBillTypeId = "", // 请设定目标单据单据类型。如无单据类型,可以空字符 TargetOrgId = 0, // 请设定目标单据主业务组织。如无主业务组织,可以为0 CustomParams = custParams, // 可以传递额外附加的参数给单据转换插件,如无此需求,可忽略 }; //执行下推操作,并获取下推结果 operationResult = ConvertServiceHelper.Push(this.Context, pushArgs, OperateOption.Create()); } catch (KDExceptionValidate ex) { throw ex; } catch (KDException ex) { throw ex; } catch { throw; } if (operationResult.TargetDataEntities.ToList().Count > 0) { var dstrows=dataEntity[destentity.DynamicProperty.Name] as DynamicObjectCollection; dstrows.Clear(); var dataentity= operationResult.TargetDataEntities.ToList()[0]; var tdata = dataentity.DataEntity[destentity.DynamicProperty.Name] as DynamicObjectCollection; foreach (var row in tdata) { var newRow = tdata.DynamicCollectionItemPropertyType.CreateInstance() as DynamicObject; newRow = row.CopyValues(newRow.DynamicObjectType); dstrows.Add(newRow); } //dataEntity.DataEntity[Entrykey]=tdata.Clone(); } } } } } }
上面我做了一个extend
代码如下
public static DynamicObject CopyValues(this DynamicObject dy, DynamicObjectType desttype) { DynamicObject dynamicObject = new DynamicObject(desttype); DynamicPropertyCollection properties = dy.DynamicObjectType.Properties; foreach (DynamicProperty current in properties) { if (dy[current] != null) { if (!desttype.Properties.ContainsKey(current.Name)) continue; if (current.PropertyType == typeof(DynamicObject)) { DynamicObject dy2 = dy[current] as DynamicObject; dynamicObject[current] = dy2.Clone(); } else if (current.PropertyType == typeof(DynamicObjectCollection)) { DynamicObjectCollection dys = dy[current] as DynamicObjectCollection; DynamicObjectCollection dys2 = dynamicObject[current] as DynamicObjectCollection; dys2.AddRange(dys.Clone()); } else if (current.PropertyType == typeof(LocalDynamicObjectCollection)) { LocalDynamicObjectCollection locals = dy[current] as LocalDynamicObjectCollection; LocalDynamicObjectCollection dys3 = dynamicObject[current] as LocalDynamicObjectCollection; dys3.AddRange(locals.Clone()); } else if (current.PropertyType.GetInterface("ICloneable") != null) { ICloneable cloneable = dy[current] as ICloneable; dynamicObject[current] = cloneable.Clone(); } else { dynamicObject[current] = dy[current]; } } else { dynamicObject[current] = dy[current]; } } return dynamicObject; }