单据自动转换并审核的方法示例
金蝶云社区-付豆_kd
付豆_kd
2人赞赏了该文章 6,726次浏览 未经作者许可,禁止转载编辑于2014年11月22日 14:02:05

需求要点:在单据A的界面点击“自动转换”,则自动产生审核态的单据B;若单据B不满足审核条件,则维持“提交”态;若单据B不满足保存校验,则维持“暂存”态;若转换失败,给出界面提示。

实现思路:
1.通过转换规则来定义单据A到单据B的转换关系,便于扩展,也便于使用已有的插件和服务;
2.定义一个空操作“自动转换”,扩展AbstractOperationServicePlugIn,重写EndOperationTransaction方法,方便处理事务;
3.依次调用单据转换服务、暂存服务、保存服务、提交服务和审核服务,以保证当前服务失败时能维持前一服务的状态;
4.根据上述服务调用的结果构造“自动转换”的操作结果;
5.若需求变更,只能生成审核态的单据B,否则不予生成,就要调用删除服务将所有审核不通过的单据删除,再构造操作结果;也可以直接抛出异常throw new KDBusinessException("", string.Join("\n", innerResult.ValidationErrors.Select(o => o.Message)))。

代码示例:
[code]public override void EndOperationTransaction(BOS.Core.DynamicForm.PlugIn.Args.EndOperationTransactionArgs e)
{
base.EndOperationTransaction(e);
////////////////////////////// 转换 //////////////////////////////
ConvertRuleElement ruleElement = AppServiceContext.ConvertService.GetConvertRules(this.Context, "srcFormId", "destFormId").Where(o => o.IsDefault == true).FirstOrDefault();
ListSelectedRowCollection rows = new ListSelectedRowCollection();
int i = 0;
foreach (var srcData in e.DataEntitys)
{
ListSelectedRow row = new ListSelectedRow(srcData.GetDynamicObjectItemValue("Id"), string.Empty, i++,"srcFormId");
rows.Add(row);
}
OperateOption option = OperateOption.Create();
option.SetVariableValue(BOSConst.CST_ConvertValidatePermission, true);
PushArgs pushArgs = new PushArgs(ruleElement, rows.ToArray());
// 转换生成目标单
ConvertOperationResult convResult = AppServiceContext.ConvertService.Push(this.Context, pushArgs, option);
DynamicObject[] destObjs = convResult.TargetDataEntities.Select(r => r.DataEntity).ToArray();
// 目标单元数据
FormMetadata destFormMetadata = AppMetadataContext.LoadWithNoVerCheck(this.Context, "destFormId") as FormMetadata;
// 操作选项
option = OperateOption.Create();
option.SetIgnoreWarning(false);
// 上一步操作成功的目标单
List lastSuccessIdLst = new List();
// 本操作成功的目标单
List successIdLst = new List();
// 操作失败的目标单
List failedobjs = new List();
// 操作结果
IOperationResult result = new OperationResult();
// 错误信息
List errorLst = new List();
////////////////////////////// 暂存 //////////////////////////////
IDraftService service = Kingdee.BOS.Contracts.ServiceFactory.GetService(this.Context);
IOperationResult innerResult = service.Draft(this.Context, destFormMetadata.BusinessInfo, destObjs);
// 暂存成功的目标单
successIdLst = innerResult.OperateResult.Select(o => o.PKValue).Distinct().ToList();
// 暂存不通过的目标单
failedobjs = destObjs.Where(o => !successIdLst.Contains(o.GetDynamicObjectItemValue("Id"))).ToList();
if (failedobjs != null && !failedobjs.IsEmpty())
{
errorLst.Add(new ValidationErrorInfo("", "Id", 0, 0, "DraftError", string.Format("{0}暂存失败:{1}",
string.Join(",", failedobjs.Select(o => o.GetDynamicObjectItemValue("BillNo"))), string.Join("\n", innerResult.ValidationErrors.Select(o => o.Message))), "", ErrorLevel.Error));
}
// 暂存不通过,则操作失败
if (successIdLst.IsEmpty())
{
result.IsSuccess = false;
}
e.DataEntitys = e.DataEntitys.Where(o => successIdLst.Contains(o.GetDynamicObjectItemValue("Id"))).ToArray();
////////////////////////////// 保存 //////////////////////////////
if (!successIdLst.IsEmpty())
{
lastSuccessIdLst = successIdLst;
innerResult = AppServiceContext.SaveService.Save(this.Context, destFormMetadata.BusinessInfo, destObjs.Where(o => successIdLst.Contains(o.GetDynamicObjectItemValue("Id"))).ToArray(), option);
// 保存成功的目标单
successIdLst = innerResult.OperateResult.Select(o => o.PKValue).Distinct().ToList();
// 校验不通过的目标单
failedobjs = destObjs.Where(o => lastSuccessIdLst.Contains(o.GetDynamicObjectItemValue("Id"))
&& !successIdLst.Contains(o.GetDynamicObjectItemValue("Id"))).ToList();
if (failedobjs != null && !failedobjs.IsEmpty())
{
errorLst.Add(new ValidationErrorInfo("", "Id", 0, 0, "SaveError", string.Format("{0}保存失败,已暂存:{1}",
string.Join(",", failedobjs.Select(o => o.GetDynamicObjectItemValue("BillNo"))), string.Join("\n", innerResult.ValidationErrors.Select(o => o.Message))), "", ErrorLevel.Error));
}
}
////////////////////////////// 提交 //////////////////////////////
if (!successIdLst.IsEmpty())
{
lastSuccessIdLst = successIdLst;
innerResult = AppServiceContext.SubmitService.Submit(this.Context, destFormMetadata.BusinessInfo, successIdLst.ToArray(), "Submit", option);
// 提交成功的目标单
successIdLst = innerResult.OperateResult.Select(o => o.PKValue).Distinct().ToList();
// 校验不通过的目标单
failedobjs = destObjs.Where(o => lastSuccessIdLst.Contains(o.GetDynamicObjectItemValue("Id"))
&& !successIdLst.Contains(o.GetDynamicObjectItemValue("Id"))).ToList();
if (failedobjs != null && !failedobjs.IsEmpty())
{
errorLst.Add(new ValidationErrorInfo("", "Id", 0, 0, "SubmitError", string.Format("{0}提交失败,已保存:{1}",
string.Join(",", failedobjs.Select(o => o.GetDynamicObjectItemValue("BillNo"))), string.Join("\n", innerResult.ValidationErrors.Select(o => o.Message))), "", ErrorLevel.Error));
}
}
////////////////////////////// 审核 //////////////////////////////
if (!successIdLst.IsEmpty())
{
lastSuccessIdLst = successIdLst;
List> pkIds = new List>();
foreach (var o in successIdLst)
{
pkIds.Add(new KeyValuePair(o, ""));
}
//审核
List paraAudit = new List();
//1审核通过
paraAudit.Add("1");
//审核意见
paraAudit.Add("");
innerResult = AppServiceContext.SetStatusService.SetBillStatus(this.Context, destFormMetadata.BusinessInfo, pkIds, paraAudit, "Audit", option);
// 审核成功的目标单
successIdLst = innerResult.OperateResult.Select(o => o.PKValue).Distinct().ToList();
// 审核不通过的目标单
failedobjs = destObjs.Where(o => lastSuccessIdLst.Contains(o.GetDynamicObjectItemValue("Id"))
&& !successIdLst.Contains(o.GetDynamicObjectItemValue("Id"))).ToList();
if (failedobjs != null && !failedobjs.IsEmpty())
{
errorLst.Add(new ValidationErrorInfo("", "Id", 0, 0, "AuditError", string.Format("{0}审核失败,已提交:{1}",
string.Join(",", failedobjs.Select(o => o.GetDynamicObjectItemValue("BillNo"))), string.Join("\n", innerResult.ValidationErrors.Select(o => o.Message))), "", ErrorLevel.Error));
}
}
////////////////////////////// 构造错误信息 //////////////////////////////
if (!errorLst.IsNullOrEmpty())
{
result.ValidationErrors.AddRange(errorLst);
}
// 将操作结果合并到空操作结果中
this.OperationResult.IsSuccess = result.IsSuccess;
if (!this.OperationResult.IsSuccess)
{
this.OperationResult.ValidationErrors.Add(new ValidationErrorInfo("", "Id", 0, 0, "Error", string.Format("自动转换失败"), "", ErrorLevel.Error));
}
this.OperationResult.MergeResult(result);
}[/code]