场景描述:工序计划列表点击按钮直接生成一张对应的工序汇报
代码:
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;
using Kingdee.BOS.Core.DynamicForm;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.Bill;
using Kingdee.BOS.Core.Metadata;
using Kingdee.BOS.Core.List;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Core.SqlBuilder;
using Kingdee.BOS.ServiceHelper;
using Kingdee.BOS.Core.List.PlugIn;
using Kingdee.BOS.Contracts;
using Kingdee.BOS.Core.DynamicForm.PlugIn.ControlModel;
using Kingdee.BOS.Core.DynamicForm.Operation;
using Kingdee.BOS.Core.Metadata.ConvertElement.ServiceArgs;
namespace HL.K3.TwoPhase.Business.Plugin
{
[Description("工序计划列表开工按钮插件")]
[Kingdee.BOS.Util.HotUpdate]
public class ProductReportListButton:AbstractListPlugIn
{
/// <summary>
/// 设置行高
/// </summary>
/// <param name="e"></param>
//public override void OnLoad(EventArgs e)
//{
// base.OnLoad(e);
// this.View.GetControl<EntryGrid>("FLIST").SetRowHeight(30);
//}
/// <summary>
/// 按钮点击后功能处理
/// </summary>
/// <param name="e"></param>
public override void AfterBarItemClick(AfterBarItemClickEventArgs e)
{
base.AfterBarItemClick(e);
switch (e.BarItemKey)
{
case "tbButtonRep"://报工按钮
ReportedMonthod();
break;
}
}
/// <summary>
///
/// 报工产生一张对应得工序汇报
/// </summary>
private void ReportedMonthod()
{
//获取选中行数据
ListSelectedRowCollection rows = this.ListView.SelectedRowsInfo;
if (rows.Count>=2)
{
throw new Exception("只能选中一行进行开工!!");
}
//string key = rows[0].PrimaryKeyValue;
//FormMetadata formMetadata = MetaDataServiceHelper.Load(this.Context, "SFC_OperationPlanning") as FormMetadata;
//DynamicObject dynamicObject = BusinessDataServiceHelper.LoadSingle(
// this.Context,
// key,
// formMetadata.BusinessInfo.GetDynamicObjectType());
//long targetOrgId = Convert.ToInt64(dynamicObject["SubEntity"]);
var rules = ConvertServiceHelper.GetConvertRules(this.View.Context, "SFC_OperationPlanning", "SFC_OperationReport");
var rule = rules.FirstOrDefault(t => t.IsDefault);
// 获取在列表上当前选择需下推的行
ListSelectedRow[] selectedRows = ((IListView)this.View).SelectedRowsInfo.ToArray();
long targetOrgId =Convert.ToInt64(selectedRows[0].DataRow["FProcessId_Id"]);
Int32 FOPERSTATUS = Convert.ToInt32(selectedRows[0].DataRow["FOPERSTATUS"]);
if (FOPERSTATUS==5)
{
throw new Exception("已经是完工状态不需报工!!");
}
if (FOPERSTATUS == 3)
{
throw new Exception("该工序计划是下达状态请先开工才能报工!!");
}
// 如下代码为单据上获取当前当前选择行
// string primaryKeyValue = ((IBillView)this.View).Model.GetPKValue().ToString();
// ListSelectedRow row = new ListSelectedRow(primaryKeyValue, string.Empty, 0, this.View.BillBusinessInfo.GetForm().Id);
// ListSelectedRow[] selectedRows = new ListSelectedRow[] { row };
// 调用下推服务,生成下游单据数据包
ConvertOperationResult operationResult = null;
Dictionary<string, object> option = new Dictionary<string, object>();
try
{
PushArgs pushArgs = new PushArgs(rule, selectedRows)
{
TargetBillTypeId = "001f29d2c9af844211e342cba3aaa38c", // 工序汇报入库
TargetOrgId = targetOrgId, // 请设定目标单据主业务组织
CustomParams = option, // 可以传递额外附加的参数给单据转换插件
};
//执行下推操作,并获取下推结果
operationResult = ConvertServiceHelper.Push(this.View.Context, pushArgs, null);
}
catch (KDExceptionValidate ex)
{
this.View.ShowErrMessage(ex.Message, ex.ValidateString);
return ;
}
catch (KDException ex)
{
this.View.ShowErrMessage(ex.Message);
return ;
}
catch
{
throw;
}
// 获取生成的目标单据数据包
DynamicObject[] objs = (from p in operationResult.TargetDataEntities
select p.DataEntity).ToArray();
// 读取目标单据元数据
var targetBillMeta = MetaDataServiceHelper.Load(this.View.Context, "SFC_OperationReport") as FormMetadata;
// 提交数据库保存,并获取保存结果
var saveResult = BusinessDataServiceHelper.Save(this.View.Context, targetBillMeta.BusinessInfo, objs);
ShowPushResult("SFC_OperationReport", operationResult, objs);//显示下推后的单据
}
/// <summary>
/// 显示下推后得单据
/// </summary>
/// <param name="targetFormId"></param>
/// <param name="pushResult"></param>
/// <param name="objs"></param>
private void ShowPushResult(string targetFormId,ConvertOperationResult pushResult,DynamicObject[] objs)
{
// 构建界面显示参数
BillShowParameter param = new BillShowParameter
{
ParentPageId = this.View.PageId
};
if (objs.Length == 1)
{
// 如果下推生成的目标单仅仅只有一张,则直接打开下游单据的编辑界面
param.FormId = targetFormId; // formId
param.Status = OperationStatus.ADDNEW; // 新建状态
param.CreateFrom = CreateFrom.Push; // 标志:下推创建的单据
param.AllowNavigation = false; // 不显示导航菜单
// 把下推结果放在缓存交换区
string customParamKey = "_ConvertSessionKey";
string sessionKey_Result = "ConverOneResult";
string sessionKey_ErrorInfo = "ConvertValidationInfo";
param.CustomParams.Add(customParamKey, sessionKey_Result);
this.View.Session[sessionKey_ErrorInfo] = pushResult.ValidationErrors;
this.View.Session[sessionKey_Result] = objs[0];
}
else if (objs.Length > 1)
{
// 如果下推生成的目标单有多行,则打开批量编辑界面
param.FormId = "BOS_ConvertResultForm";
// 把下推结果放在缓存交换区
string sessionKey_Result = "ConvertResults";
string sessionKey_ErrorInfo = "ConvertValidationInfo";
this.View.Session[sessionKey_Result] = objs;
this.View.Session[sessionKey_ErrorInfo] = pushResult.ValidationErrors;
param.CustomParams.Add("_ConvertResultFormId", targetFormId);
}
else
{
return;
}
// 显示界面
param.OpenStyle.ShowType = ShowType.MainNewTabPage;
this.View.ShowForm(param);
}
}
}
推荐阅读