单据自动转换并审核的方法示例
金蝶云社区-天冥异
天冥异
7人赞赏了该文章 1,226次浏览 未经作者许可,禁止转载编辑于2018年08月02日 20:53:07

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

需求要点:在单据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)))。

代码示例:

  1. public override void EndOperationTransaction(BOS.Core.DynamicForm.PlugIn.Args.EndOperationTransactionArgs e)

  2. {

  3. base.EndOperationTransaction(e);

  4. ////////////////////////////// 转换 //////////////////////////////

  5. ConvertRuleElement ruleElement = AppServiceContext.ConvertService.GetConvertRules(this.Context, "srcFormId", "destFormId").Where(o => o.IsDefault == true).FirstOrDefault();

  6. ListSelectedRowCollection rows = new ListSelectedRowCollection();

  7. int i = 0;

  8. foreach (var srcData in e.DataEntitys)

  9. {

  10. ListSelectedRow row = new ListSelectedRow(srcData.GetDynamicObjectItemValue<string>("Id"), string.Empty, i++,"srcFormId");

  11. rows.Add(row);

  12. }

  13. OperateOption option = OperateOption.Create();

  14. option.SetVariableValue(BOSConst.CST_ConvertValidatePermission, true);

  15. PushArgs pushArgs = new PushArgs(ruleElement, rows.ToArray());

  16. // 转换生成目标单

  17. ConvertOperationResult convResult = AppServiceContext.ConvertService.Push(this.Context, pushArgs, option);

  18. DynamicObject[] destObjs = convResult.TargetDataEntities.Select(r => r.DataEntity).ToArray();

  19. // 目标单元数据

  20. FormMetadata destFormMetadata = AppMetadataContext.LoadWithNoVerCheck(this.Context, "destFormId") as FormMetadata;

  21. // 操作选项

  22. option = OperateOption.Create();

  23. option.SetIgnoreWarning(false);

  24. // 上一步操作成功的目标单

  25. List<object> lastSuccessIdLst = new List<object>();

  26. // 本操作成功的目标单

  27. List<object> successIdLst = new List<object>();

  28. // 操作失败的目标单

  29. List<DynamicObject> failedobjs = new List<DynamicObject>();

  30. // 操作结果

  31. IOperationResult result = new OperationResult();

  32. // 错误信息

  33. List<ValidationErrorInfo> errorLst = new List<ValidationErrorInfo>();

  34. ////////////////////////////// 暂存 //////////////////////////////

  35. IDraftService service = Kingdee.BOS.Contracts.ServiceFactory.GetService<IDraftService>(this.Context);

  36. IOperationResult innerResult = service.Draft(this.Context, destFormMetadata.BusinessInfo, destObjs);

  37. // 暂存成功的目标单

  38. successIdLst = innerResult.OperateResult.Select(o => o.PKValue).Distinct().ToList();

  39. // 暂存不通过的目标单

  40. failedobjs = destObjs.Where(o => !successIdLst.Contains(o.GetDynamicObjectItemValue<object>("Id"))).ToList();

  41. if (failedobjs != null && !failedobjs.IsEmpty())

  42. {

  43. errorLst.Add(new ValidationErrorInfo("", "Id", 0, 0, "DraftError", string.Format("{0}暂存失败:{1}", 

  44. string.Join(",", failedobjs.Select(o => o.GetDynamicObjectItemValue<string>("BillNo"))), string.Join("\n", innerResult.ValidationErrors.Select(o => o.Message))), "", ErrorLevel.Error));

  45. }

  46. // 暂存不通过,则操作失败

  47. if (successIdLst.IsEmpty())

  48. {

  49. result.IsSuccess = false;

  50. }

  51. e.DataEntitys = e.DataEntitys.Where(o => successIdLst.Contains(o.GetDynamicObjectItemValue<object>("Id"))).ToArray();

  52. ////////////////////////////// 保存 //////////////////////////////

  53. if (!successIdLst.IsEmpty())

  54. {

  55. lastSuccessIdLst = successIdLst;

  56. innerResult = AppServiceContext.SaveService.Save(this.Context, destFormMetadata.BusinessInfo, destObjs.Where(o => successIdLst.Contains(o.GetDynamicObjectItemValue<object>("Id"))).ToArray(), option);

  57. // 保存成功的目标单

  58. successIdLst = innerResult.OperateResult.Select(o => o.PKValue).Distinct().ToList();

  59. // 校验不通过的目标单

  60. failedobjs = destObjs.Where(o => lastSuccessIdLst.Contains(o.GetDynamicObjectItemValue<object>("Id"))

  61. && !successIdLst.Contains(o.GetDynamicObjectItemValue<object>("Id"))).ToList();

  62. if (failedobjs != null && !failedobjs.IsEmpty())

  63. {

  64. errorLst.Add(new ValidationErrorInfo("", "Id", 0, 0, "SaveError", string.Format("{0}保存失败,已暂存:{1}",

  65. string.Join(",", failedobjs.Select(o => o.GetDynamicObjectItemValue<string>("BillNo"))), string.Join("\n", innerResult.ValidationErrors.Select(o => o.Message))), "", ErrorLevel.Error));

  66. }

  67. }

  68. ////////////////////////////// 提交 //////////////////////////////

  69. if (!successIdLst.IsEmpty())

  70. {

  71. lastSuccessIdLst = successIdLst;

  72. innerResult = AppServiceContext.SubmitService.Submit(this.Context, destFormMetadata.BusinessInfo, successIdLst.ToArray(), "Submit", option);

  73. // 提交成功的目标单

  74. successIdLst = innerResult.OperateResult.Select(o => o.PKValue).Distinct().ToList();

  75. // 校验不通过的目标单

  76. failedobjs = destObjs.Where(o => lastSuccessIdLst.Contains(o.GetDynamicObjectItemValue<object>("Id"))

  77. && !successIdLst.Contains(o.GetDynamicObjectItemValue<object>("Id"))).ToList();

  78. if (failedobjs != null && !failedobjs.IsEmpty())

  79. {

  80. errorLst.Add(new ValidationErrorInfo("", "Id", 0, 0, "SubmitError", string.Format("{0}提交失败,已保存:{1}",

  81. string.Join(",", failedobjs.Select(o => o.GetDynamicObjectItemValue<string>("BillNo"))), string.Join("\n", innerResult.ValidationErrors.Select(o => o.Message))), "", ErrorLevel.Error));

  82. }

  83. }

  84. ////////////////////////////// 审核 //////////////////////////////

  85. if (!successIdLst.IsEmpty())

  86. {

  87. lastSuccessIdLst = successIdLst;

  88. List<KeyValuePair<object, object>> pkIds = new List<KeyValuePair<object, object>>();

  89. foreach (var o in successIdLst)

  90. {

  91. pkIds.Add(new KeyValuePair<object, object>(o, ""));

  92. }

  93. //审核

  94. List<object> paraAudit = new List<object>();

  95. //1审核通过

  96. paraAudit.Add("1");

  97. //审核意见

  98. paraAudit.Add("");

  99. innerResult = AppServiceContext.SetStatusService.SetBillStatus(this.Context, destFormMetadata.BusinessInfo, pkIds, paraAudit, "Audit", option);

  100. // 审核成功的目标单

  101. successIdLst = innerResult.OperateResult.Select(o => o.PKValue).Distinct().ToList();

  102. // 审核不通过的目标单

  103. failedobjs = destObjs.Where(o => lastSuccessIdLst.Contains(o.GetDynamicObjectItemValue<object>("Id"))

  104. && !successIdLst.Contains(o.GetDynamicObjectItemValue<object>("Id"))).ToList();

  105. if (failedobjs != null && !failedobjs.IsEmpty())

  106. {

  107. errorLst.Add(new ValidationErrorInfo("", "Id", 0, 0, "AuditError", string.Format("{0}审核失败,已提交:{1}",

  108. string.Join(",", failedobjs.Select(o => o.GetDynamicObjectItemValue<string>("BillNo"))), string.Join("\n", innerResult.ValidationErrors.Select(o => o.Message))), "", ErrorLevel.Error));

  109. }

  110. }

  111. ////////////////////////////// 构造错误信息 //////////////////////////////

  112. if (!errorLst.IsNullOrEmpty())

  113. {

  114. result.ValidationErrors.AddRange(errorLst);

  115. }

  116. // 将操作结果合并到空操作结果中

  117. this.OperationResult.IsSuccess = result.IsSuccess;

  118. if (!this.OperationResult.IsSuccess)

  119. {

  120. this.OperationResult.ValidationErrors.Add(new ValidationErrorInfo("", "Id", 0, 0, "Error", string.Format("自动转换失败"), "", ErrorLevel.Error));

  121. }

  122. this.OperationResult.MergeResult(result);

  123. }

复制代码


请问,要引用哪些DLL??


using Kingdee.BOS.Contracts;
using Kingdee.BOS.Core.Const;
using Kingdee.BOS.Core.DynamicForm;
using Kingdee.BOS.Core.DynamicForm.Operation;
using Kingdee.BOS.Core.List;
using Kingdee.BOS.Core.Metadata;
using Kingdee.BOS.Core.Metadata.ConvertElement;
using Kingdee.BOS.Core.Metadata.ConvertElement.ServiceArgs;
using Kingdee.BOS.Core.Validation;
using Kingdee.BOS.Orm;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Util;
using Kingdee.K3.Core.MFG.EntityHelper;
using Kingdee.K3.MFG.App;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;



图标赞 7
7人点赞
还没有人点赞,快来当第一个点赞的人吧!
图标打赏
0人打赏
还没有人打赏,快来当第一个打赏的人吧!