1、 在公共项目下新建类
记得修改类为public。
2、 添加引用
HotUpdate:Kingdee.BOS.dll【using Kingdee.BOS.Util】 启用热更新
Description:System.dll【using System.ComponentModel】
Context:Kingdee.BOS.dll 登录上下文环境
BusinessInfo:Kingdee.BOS.Core.dll【using Kingdee.BOS.Core.Metadata】New DTO处理,主要针对WebApi请求参数构建
IOperationResult:Kingdee.BOS.Core.dll【using Kingdee.BOS.Core.DynamicForm】所有操作服务类函数调用返回结果必须继承此接口
OperateOption:Kingdee.BOS.DataEntity.dll【using Kingdee.BOS.Orm】为数据的操作提供额外的选项
ISubmitService:Kingdee.BOS.Contracts.dll【using Kingdee.BOS.Contracts】提交服务接口
ServiceHelper: Kingdee.BOS.App.dll【using Kingdee.BOS.App】
SetInteractionFlag、SetIgnoreInteractionFlag 【using Kingdee.BOS.Core.Interaction】
mm.k3.Core.dll:是我自己的项目,主要是一些静态参数的设置集合。
3、 添加命名空间
using Kingdee.BOS;
using Kingdee.BOS.App;
using Kingdee.BOS.Contracts;
using Kingdee.BOS.Core.DynamicForm;
using Kingdee.BOS.Core.Interaction;
using Kingdee.BOS.Core.Metadata;
using Kingdee.BOS.Orm;
using Kingdee.BOS.Util;
using mm.K3.Core.Const;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
4、 创建自动审核方法
#region 自动审核方法
///
/// 自动审核方法
///
/// 上下文
/// 主键数组
/// 表单参数构建
/// 保存操作返回结果
/// 操作选项
/// 是否自动提交且审核:false=自动审核,true=自动提交且审核
public void AutoAudit(Context ctx, object[] billId, BusinessInfo businessInfo, IOperationResult operationResult, OperateOption option, bool IsAutoSubmitAndAudit = false)
{
if (IsAutoSubmitAndAudit)
{
//设置提交参数
OperateOption submitOption = OperateOption.Create();
submitOption.SetIgnoreWarning(true);
submitOption.SetIgnoreScopeValidateFlag(true);
//创建提交服务
ISubmitService submitService = ServiceHelper.GetService
IOperationResult submitResult = submitService.Submit(ctx, businessInfo, billId, OptionConst.Submit, submitOption);
//判断提交结果,如果失败,则内部跑出错误,回滚代码
if (!CheckOptionResult(submitResult, operationResult))
{
return;//回滚
}
}
//构建操作可选参数对象
OperateOption auditOption = OperateOption.Create();
auditOption.SetIgnoreWarning(option.GetIgnoreWarning());
auditOption.SetInteractionFlag(option.GetInteractionFlag());
auditOption.SetIgnoreInteractionFlag(option.GetIgnoreInteractionFlag());
//构建单据主键参数
List
foreach (var pkValue in billId)
{
pkEntityIds.Add(new KeyValuePair
}
List
paras.Add("1");
paras.Add("");
//调用审核操作
ISetStatusService setStatusService = ServiceHelper.GetService
//如下调用方式,需显示交互信息
IOperationResult auditResult = setStatusService.SetBillStatus(ctx, businessInfo, pkEntityIds, paras, OptionConst.Audit, auditOption);
//判断审核结果,如果失败,在内部会抛出错误,回滚代码
if (!CheckOptionResult(auditResult, operationResult))
{
return;//回滚代码
}
}
#endregion
5、 创建检查操作是否成功方法
#region 检查操作是否成功
///
/// 判断操作结果是否成功,如果不成功,则直接抛出中断进程
///
/// 当前操作的返回结果
/// 外部参数的操作返回结果
/// 操作选项
/// 抛出错误的方式
///
private bool CheckOptionResult(IOperationResult operation, IOperationResult operationResult, OperateOption operateOption = null, bool isShowError = false)
{
bool isSuccess = false;//操作成功的标识,默认操作不成功
if (operation.IsSuccess)
{
isSuccess = true;//操作成功
}
else
{
if (operation.InteractionContext != null && operation.InteractionContext.Option.GetInteractionFlag().Count > 0)
{
//有交互性提示
//传出交互性提示完整信息对象
operationResult.InteractionContext = operation.InteractionContext;
//传出本次交互的标识
//用户在确认继续后,会重新进入操作
//将以此标识取本次交互是否已经确认过,避免重复交互
operationResult.Sponsor = operation.Sponsor;
if (operateOption != null)
{
if (isShowError)
{
throw new KDException("", operation.InteractionContext.SimpleMessage);
}
else
{
//throw new KDInteractionException(opOption, opResult.Sponsor);
throw new KDException("", ((AbstractInteractionResult)operation).InteractionContext.SimpleMessage);
}
}
else
{
//抛出错误,终止本次操作
throw new KDBusinessException("", "本次操作需要用户确认是否继续,暂时中断。");
}
}
else//无交互性提示
{
//操作失败,拼接失败原因,然后抛出中断
operation.MergeValidateErrors();
if (operation.OperateResult == null)
{
throw new KDBusinessException("", "未知原因导致自动提交、审核失败!");
}
else
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("自动提交、审核失败,失败原因:");
foreach (var or in operation.OperateResult)
{
sb.AppendLine(or.Message);
}
throw new KDBusinessException("", sb.ToString());
}
}
}
return isSuccess;
}
#endregion
6、 方法使用示例
7、整体代码
#region << 版 本 注 释 >>
/*----------------------------------------------------------------
* 版权所有 (c) 2023 保留所有权利。
* CLR版本:4.0.30319.42000
* 机器名称:
* 公司名称:
* 命名空间:mm.K3.App.Service.Common
* 唯一标识:
* 文件名:ActionCommon
* 当前用户域:
*
* 创建者:Krystal
* 电子邮箱:
* 创建时间:2023/9/19 16:41:59
* 版本:V1.0.0
* 描述:
*
* ----------------------------------------------------------------
* 修改人:
* 时间:
* 修改说明:
*
* 版本:V1.0.1
*----------------------------------------------------------------*/
#endregion << 版 本 注 释 >>
using Kingdee.BOS;
using Kingdee.BOS.App;
using Kingdee.BOS.Contracts;
using Kingdee.BOS.Core.DynamicForm;
using Kingdee.BOS.Core.Interaction;
using Kingdee.BOS.Core.Metadata;
using Kingdee.BOS.Orm;
using Kingdee.BOS.Util;
using mm.K3.Core.Const;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace mm.K3.App.Service.Common
{
///
/// ActionCommon 的摘要说明
///
[Description("操作公共类"), HotUpdate]
public class ActionCommon
{
#region <常量>
#endregion <常量>
#region <变量>
#endregion <变量>
#region <方法>
#region 自动审核方法
///
/// 自动审核方法
///
/// 上下文
/// 主键数组
/// 表单参数构建
/// 保存操作返回结果
/// 操作选项
/// 是否自动提交且审核:false=自动审核,true=自动提交且审核
public void AutoAudit(Context ctx, object[] billId, BusinessInfo businessInfo, IOperationResult operationResult, OperateOption option, bool IsAutoSubmitAndAudit = false)
{
if (IsAutoSubmitAndAudit)
{
//设置提交参数
OperateOption submitOption = OperateOption.Create();
submitOption.SetIgnoreWarning(true);
submitOption.SetIgnoreScopeValidateFlag(true);
//创建提交服务
ISubmitService submitService = ServiceHelper.GetService
IOperationResult submitResult = submitService.Submit(ctx, businessInfo, billId, OptionConst.Submit, submitOption);
//判断提交结果,如果失败,则内部跑出错误,回滚代码
if (!CheckOptionResult(submitResult, operationResult))
{
return;//回滚
}
}
//构建操作可选参数对象
OperateOption auditOption = OperateOption.Create();
auditOption.SetIgnoreWarning(option.GetIgnoreWarning());
auditOption.SetInteractionFlag(option.GetInteractionFlag());
auditOption.SetIgnoreInteractionFlag(option.GetIgnoreInteractionFlag());
//构建单据主键参数
List
foreach (var pkValue in billId)
{
pkEntityIds.Add(new KeyValuePair
}
List
paras.Add("1");
paras.Add("");
//调用审核操作
ISetStatusService setStatusService = ServiceHelper.GetService
//如下调用方式,需显示交互信息
IOperationResult auditResult = setStatusService.SetBillStatus(ctx, businessInfo, pkEntityIds, paras, OptionConst.Audit, auditOption);
//判断审核结果,如果失败,在内部会抛出错误,回滚代码
if (!CheckOptionResult(auditResult, operationResult))
{
return;//回滚代码
}
}
#endregion
#region 检查操作是否成功
///
/// 判断操作结果是否成功,如果不成功,则直接抛出中断进程
///
/// 当前操作的返回结果
/// 外部参数的操作返回结果
/// 操作选项
/// 抛出错误的方式
///
private bool CheckOptionResult(IOperationResult operation, IOperationResult operationResult, OperateOption operateOption = null, bool isShowError = false)
{
bool isSuccess = false;//操作成功的标识,默认操作不成功
if (operation.IsSuccess)
{
isSuccess = true;//操作成功
}
else
{
if (operation.InteractionContext != null && operation.InteractionContext.Option.GetInteractionFlag().Count > 0)
{
//有交互性提示
//传出交互性提示完整信息对象
operationResult.InteractionContext = operation.InteractionContext;
//传出本次交互的标识
//用户在确认继续后,会重新进入操作
//将以此标识取本次交互是否已经确认过,避免重复交互
operationResult.Sponsor = operation.Sponsor;
if (operateOption != null)
{
if (isShowError)
{
throw new KDException("", operation.InteractionContext.SimpleMessage);
}
else
{
//throw new KDInteractionException(opOption, opResult.Sponsor);
throw new KDException("", ((AbstractInteractionResult)operation).InteractionContext.SimpleMessage);
}
}
else
{
//抛出错误,终止本次操作
throw new KDBusinessException("", "本次操作需要用户确认是否继续,暂时中断。");
}
}
else//无交互性提示
{
//操作失败,拼接失败原因,然后抛出中断
operation.MergeValidateErrors();
if (operation.OperateResult == null)
{
throw new KDBusinessException("", "未知原因导致自动提交、审核失败!");
}
else
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("自动提交、审核失败,失败原因:");
foreach (var or in operation.OperateResult)
{
sb.AppendLine(or.Message);
}
throw new KDBusinessException("", sb.ToString());
}
}
}
return isSuccess;
}
#endregion
#endregion <方法>
}
}
ActionCommon
完美,又是优秀的自己,度过完美的一天。