描述:自定义下推就是在插件中调用下推接口,得到转换结果,并对结果进行处理的过程
要点:
1.下推参数的构建,包括源单内码,目标单据类型,目标主组织等。
2.下推接口传递的操作选项怎样在转换插件获取和使用等
3. 转换结果,理解转换结果的结构,怎样取到所需的数据包和怎样处理等。
案例:实现功能:
1. 销售数量大于10,并且大于操作选项传入数量的数量才能下推
2.销售数量大于100的合并成一单,100以下的合并成另外一单
3. 结果直接保存或显示下推页面。
实现步骤:
1. 在单据和列表菜单增加一按钮
2. 在上游单据中增加一隐藏字段,作为转换规则中的分组依据
3. 在维护插件中构建参数,调用下推接口,处理下推结果
4. 在转换插件中对数据进行过滤和分组
5. 销售数量大于100,设置隐藏字段为1,否则设置为0。
单据维护插件代码:
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);
// 是否验证下游单据新增权限 true:无需验证, false:需要验证
pushOption.SetVariableValue(BOSConst.CST_ConvertValidatePermission, true);
//调用单据转换接口,得到目标数据报
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);
}
}
/// <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("生成成功");
}
//后面可以处理提交和审核
// BusinessDataServiceHelper.Submit
}
/// <summary>
/// 创建下推参数
/// </summary>
/// <returns></returns>
private PushArgs CreatePushArgs(OperateOption pushOption)
{
//得到转换规则
var convertRule = this.GetConvertRule();
//源单数据
var selectedRow = this.GetSelectedRow(pushOption);
//单据下推参数
PushArgs pushArgs = new PushArgs(convertRule, selectedRow);
//目标单据主组织,可选参数,基础资料隔离,给没有住组织的目标数据包赋值
pushArgs.TargetOrgId = 0;
//目标单据类型,必填参数,除非下游单据类型不是必填的,对源单数据进行过滤,给目标单赋值
pushArgs.TargetBillTypeId = "5eb4f21542e42e";
return pushArgs;
}
/// <summary>
/// 展示目标单据
/// </summary>
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);
}
/// <summary>
/// 得到下推数据行
/// </summary>
/// <param name="pushOption"></param>
/// <returns></returns>
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();
//}
}
/// <summary>
/// 得到业务元数据
/// </summary>
/// <param name="formId"></param>
/// <returns></returns>
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;
namespace Kingdee.BOS.TestPlugIn.Demo
{
[HotUpdate]
[Description("单据A下推单据B按条件过滤和按条件分组")]
public class BillAConvertFilterPlugIn : AbstractConvertPlugIn
{
/// <summary>
/// 增加需要取数的源单字段
/// </summary>
/// <param name="e"></param>
public override void OnQueryBuilderParemeter(QueryBuilderParemeterEventArgs e)
{
base.OnQueryBuilderParemeter(e);
//加上隐藏分组字段
e.SelectItems.Add(new SelectorItemInfo("FGroupInteger"));
}
/// <summary>
/// 增加过滤条件
/// </summary>
/// <param name="e"></param>
public override void OnParseFilter(ParseFilterEventArgs e)
{
base.OnParseFilter(e);
//加上过滤条件
int filterValue =10;
var filterOptValue = ObjectUtils.Object2Int( this.Option.GetVariableValue("groupValue", "0"));
if(filterOptValue != 0)
{
filterValue =filterOptValue;
}
var strAnd = " AND ";
if (string.IsNullOrWhiteSpace(e.FilterPolicySQL))
{
strAnd = "";
}
e.FilterPolicySQL = e.FilterPolicySQL + strAnd + string.Format(" F_KKK_BaseQty > {0}", filterValue);
}
/// <summary>
/// 增加分组字段
/// </summary>
/// <param name="e"></param>
public override void OnBeforeGroupBy(BeforeGroupByEventArgs e)
{
base.OnBeforeGroupBy(e);
e.HeadGroupKey = e.HeadGroupKey + ",FGroupInteger";
}
/// <summary>
/// 取到源单数据
/// </summary>
/// <param name="e"></param>
public override void OnGetSourceData(GetSourceDataEventArgs e)
{
base.OnGetSourceData(e);
//设置分组字段值
foreach (var sData in e.SourceData)
{
var baseQty = ObjectUtils.Object2Int(sData["F_KKK_BaseQty"]);
if (baseQty > 100)
{
sData["FGroupInteger"] = 1;
}
else
{
sData["FGroupInteger"] = 0;
}
}
}
}
}
推荐阅读