主要步骤:
1. 在表单插件中调用单据转换
在单据转换插件中根据参数控制增加自定义条件,和过滤分析
选择下推行数跟取到的行数不一样,则抛出异常
在表单插件中捕获特定异常,并向前端发出交互指令,根据用户选择进行是否再次下推
示例代码:
表单插件: 自定义下推操作
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using Kingdee.BOS.Util;
using Kingdee.BOS.Core.Bill.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.Metadata.ConvertElement.ServiceArgs;
using Kingdee.BOS.ServiceHelper;
using Kingdee.BOS.Core.Metadata.ConvertElement;
using Kingdee.BOS.Core.List;
using Kingdee.BOS.Orm;
using Kingdee.BOS.Core.Const;
using Kingdee.BOS.Core.DynamicForm.Operation;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Core.Interaction;
using Kingdee.BOS.Core.Metadata;
using Kingdee.BOS.Core.Bill;
using Kingdee.BOS.Core;
using Kingdee.BOS.Core.DynamicForm;
using Kingdee.BOS.Core.Metadata.EntityElement;
using Kingdee.BOS.Core.Metadata.FieldElement;
namespace Kingdee.BOS.TestPlugIn.Demo
{
[HotUpdate]
[Description("自定义下推单据")]
public class CustomPushPlugIn : AbstractBillPlugIn
{
//菜单点击事件
public override void BarItemClick(BarItemClickEventArgs e)
{
base.BarItemClick(e);
if (e.BarItemKey.EqualsIgnoreCase("FBtnCustomPush"))
{
//操作参数
OperateOption pushOption = OperateOption.Create();
//构建下推参数
var pushArgs = this.CreatePushArgs(pushOption);
//调用单据转换接口,得到目标数据报
try
{
var convertResult = ConvertServiceHelper.Push(this.Context, pushArgs, pushOption);
var targetArrDynObjs = (from p in convertResult.TargetDataEntities select p.DataEntity).ToArray();
//对转换结果进行处理
var targetBusinessInfo = this.GetBusinessInfo(pushArgs.ConvertRule.TargetFormId, pushArgs.ConvertRule.TargetFormMetadata);
//1. 直接调用保存接口,对数据进行保存
this.SaveTargetBill(targetBusinessInfo, targetArrDynObjs);
//2. 或者展示目标单据
// this.ShowTargetForm(targetBusinessInfo, targetArrDynObjs);
}
catch(KDExceptionValidate ex)
{
if (ex.Code == "partRowFilter")
{
this.View.ShowWarnningMessage(ex.Message, "", MessageBoxOptions.YesNo, new Action<MessageBoxResult>((result) =>
{
//选择yes再次下推
if (result == MessageBoxResult.Yes)
{
pushOption.SetVariableValue("IsIgnoreWarnCondition", true);
pushOption.SetVariableValue("IsIgnoreFilter", true);
var convertResult = ConvertServiceHelper.Push(this.Context, pushArgs, pushOption);
}
}));
}
}
}
}
/// <summary>
/// 保存目标单据
/// </summary>
private void SaveTargetBill(BusinessInfo targetBusinessInfo, DynamicObject[] targetBillObjs)
{
OperateOption saveOption = OperateOption.Create();
saveOption.SetIgnoreWarning(true);
saveOption.SetIgnoreInteractionFlag(true);
var saveResult = BusinessDataServiceHelper.Save(this.Model.Context, targetBusinessInfo, targetBillObjs, saveOption);
if (!saveResult.IsSuccess)
{
var errorInfo = string.Join(";", saveResult.ValidationErrors.Select(x => x.Message));
this.View.ShowErrMessage(errorInfo);
return;
}
else
{
this.View.ShowMessage("生成成功");
}
}
// 创建下推参数
private PushArgs CreatePushArgs(OperateOption pushOption)
{
//得到转换规则
var convertRule = this.GetConvertRule();
//源单数据
var selectedRow = this.GetSelectedRow(pushOption);
//单据下推参数
PushArgs pushArgs = new PushArgs(convertRule, selectedRow);
//目标单据主组织,可选参数,基础资料隔离,给没有住组织的目标数据包赋值,取当前登录组织即可
pushArgs.TargetOrgId = this.Context.CurrentOrganizationInfo.ID;
//目标单据类型,必填参数,除非下游单据类型不是必填的,对源单数据进行过滤,给目标单赋值
pushArgs.TargetBillTypeId = "5eb4f21542e42e";
return pushArgs;
}
// 展示目标单据
private void ShowTargetForm(BusinessInfo targetBusinessInfo, DynamicObject[] targetBillObjs)
{
BillShowParameter param = new BillShowParameter();
param.ParentPageId = this.View.PageId;
if (targetBillObjs.Length == 1)
{
param.Status = OperationStatus.ADDNEW;
param.CreateFrom = CreateFrom.Push;
param.AllowNavigation = false;
string sessionKey = FormConst.ConvParamKey_OneResultSessionKey;// "_ConvertSessionKey";
string sessionName = FormConst.ConvParamKey_OneResultSessionName;// "ConverOneResult";
param.CustomParams.Add(sessionKey, sessionName);
this.View.Session[sessionName] = targetBillObjs[0]; // 把结果放在交换区,供子页面取用
param.FormId = targetBusinessInfo.GetForm().Id; //formID
}
else if (targetBillObjs.Length > 1)
{
param.FormId = FormIdConst.BOS_ConvertResultForm;
string sessionName = FormConst.ConvParamKey_ResultSessionName;//"ConvertResults";
this.View.Session[sessionName] = targetBillObjs; // 把结果放在交换区,供子页面取用
param.CustomParams.Add(FormConst.ConvParamKey_TargetFormId, targetBusinessInfo.GetForm().Id);
}
else
{
//没有数据时直接返回
return;
}
param.OpenStyle.ShowType = ShowType.MainNewTabPage;
this.View.ShowForm(param);
}
// 得到下推数据行
private ListSelectedRow[] GetSelectedRow(OperateOption pushOption)
{
//列表页面,按分录下推
if (this.View is IListViewService)
{
var listView = this.View as IListView;
if ((listView.SelectedRowsInfo == null) || (listView.SelectedRowsInfo.Count == 0))
{
var msg = "请选择下推数据行!";
this.View.ShowMessage(msg);
}
pushOption.SetVariableValue(ConvertConst.SelectByBillId, false);
return listView.SelectedRowsInfo.ToArray();
}
else //单据维护界面,按整单下推
{
var billView = this.View as IBillView;
string primaryKeyValue = billView.Model.GetPKValue().ToString();
ListSelectedRow row = new ListSelectedRow(primaryKeyValue, string.Empty, 0, billView.BillBusinessInfo.GetForm().Id);
return new ListSelectedRow[] { row };
}
}
//得到转换规则
private ConvertRuleElement GetConvertRule()
{
//得到转换规则
var convertRuleId = "49481112-7031-488b-9315-1bf450ca0840";//转换规则内码
var convertRuleMeta = ConvertServiceHelper.GetConvertRule(this.Context, convertRuleId);
return convertRuleMeta.Rule;
//根据上下游formId得到所有的转换规则
//var convertRuleMetas = ConvertServiceHelper.GetConvertRules(this.Context, "上游 formId", "下游formId");
//var defaultConvertRuleMeta = convertRuleMetas.FirstOrDefault(x => x.IsDefault == true);
//if (defaultConvertRuleMeta == null) //有可能没有默认转换规则,则取第一个转换规则
//{
// var firstConvertRuleMeta = convertRuleMetas.First();
//}
}
// 得到业务元数据
private BusinessInfo GetBusinessInfo(string formId, FormMetadata metaData = null)
{
if (metaData != null) return metaData.BusinessInfo;
metaData = FormMetaDataCache.GetCachedFormMetaData(this.Context, formId);
return metaData.BusinessInfo;
}
}
}
转换插件:
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.Metadata;
using Kingdee.BOS.Core.Metadata.ConvertElement.PlugIn;
using Kingdee.BOS.Core.Metadata.ConvertElement.PlugIn.Args;
using Kingdee.BOS.App.Core.Query;
namespace Kingdee.BOS.TestPlugIn.Demo
{
[HotUpdate]
[Description("数据携带不全抛出警告提示")]
public class WarnAnalyzeConvertPlugIn : AbstractConvertPlugIn
{
int selectedRowCount = 0;
public override void OnInSelectedRow(InSelectedRowEventArgs e)
{
base.OnInSelectedRow(e);
selectedRowCount= e.SelectedRows.Count();
}
// 加上自定义过滤条件,e.FilterPolicySQL:转换规则选单条件策略中的过滤条件
public override void OnParseFilter(ParseFilterEventArgs e)
{
base.OnParseFilter(e);
bool isIgnoreWarnCondition = false;
this.Option.TryGetVariableValue("IsIgnoreWarnCondition", out isIgnoreWarnCondition);
if (isIgnoreWarnCondition)//忽略条件
{
e.FilterPolicySQL = "";
}
else //加上自定义过滤条件
{
e.FilterPolicySQL = e.FilterPolicySQL + " AND " + "1=1";
}
}
// 对转换过程取到的源单数据进行判断
public override void OnGetSourceData(GetSourceDataEventArgs e)
{
base.OnGetSourceData(e);
bool isIgnoreFilter = false;
this.Option.TryGetVariableValue("IsIgnoreFilter", out isIgnoreFilter);
if (!isIgnoreFilter)
{
var entryEntity = e.SourceBusinessInfo.GetEntryEntity("单据体key");
var entryPkName = e.DicFieldAlias[entryEntity.Key];
var sFilterRowCount = e.SourceData.GroupBy(x => x[entryPkName]).Count();
//选择的行数大于转换过程中取到的行数,说明有行不满足下推条件, 抛出异常在表单插件中处理交换
if (selectedRowCount > sFilterRowCount)
{
throw new KDExceptionValidate("partRowFilter", "", "部分数据行数据不满足条件被过滤掉,是否继续?");
}
}
}
}
}
推荐阅读