在插件中可以调用ApprovalAssignmentServiceHelper.SubmitApprovalItems()进行工作流审批。但SubmitApprovalItems()需要一个AssignmentContext参数,而AssignmentContext属性非常多,构造起来很麻烦。
因此我写了两个方法,将SubmitApprovalItems()再次封装,传入几个简单参数,就能完成审批。
using Kingdee.BOS.App.Data;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.Metadata;
using Kingdee.BOS.Orm;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.ServiceHelper;
using Kingdee.BOS.Workflow.Assignment;
using Kingdee.BOS.Workflow.Interface;
using Kingdee.BOS.Workflow.Models.ApprovalAssignment;
using Kingdee.BOS.Workflow.Models.EnumStatus;
using Kingdee.BOS.Workflow.ServiceHelper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
/// <summary>
/// 审批单据,actionName为空时,自动寻找第一个同意审批项;
/// 有多个类型为通过的审批项时,可用actionName指定审批项,如actionName="审批通过"
/// </summary>
/// <param name="formId">单据FormId</param>
/// <param name="pKValue">单据主键</param>
/// <param name="receiverName">处理人</param>
/// <param name="disposition">审批意见</param>
/// <param name="isApprovalFlow">是否为审批流</param>
public void ApproveBill(Context ctx,string formId, string pKValue, string receiverName, string disposition, bool isApprovalFlow = false, string actionName = null)
{
List<AssignResult> assignResults = GetApproveActions(ctx, formId, pKValue, receiverName);
AssignResult approvalAssignResults = assignResults.FirstOrDefault(r => r.ApprovalType == AssignResultApprovalType.Forward);
if (approvalAssignResults == null) throw new Exception("未找到审批项。");
SubmitWorkflow(ctx, formId, pKValue, receiverName, approvalAssignResults.Id, disposition, isApprovalFlow);
}
/// <summary>
/// 驳回单据,actionName为空时,自动寻找一个驳回审批项;
/// 有多个类型为驳回的审批项时(如驳回、打回发起人),可用actionName指定审批项,如actionName="打回发起人"
/// </summary>
public void RejectBill(Context ctx,string formId, string pKValue, string receiverName, string disposition, bool isApprovalFlow = false, string actionName = null)
{
List<AssignResult> assignResults = GetApproveActions(ctx, formId, pKValue, receiverName);
if (!string.IsNullOrEmpty(actionName))
assignResults = assignResults.Where(r => r.Name.Any(p => p.Value == actionName)).ToList();
else
assignResults = assignResults.OrderBy(r => r.Name.Any(p => p.Value == "打回发起人")).ToList();
AssignResult rejectAssignResults = assignResults.FirstOrDefault(r => r.ApprovalType == AssignResultApprovalType.Reject);
if (rejectAssignResults == null) throw new Exception("未找到驳回审批项。");
SubmitWorkflow(ctx, formId, pKValue, receiverName, rejectAssignResults.Id, disposition, isApprovalFlow);
}
private List<AssignResult> GetApproveActions(Context ctx,string formId, string pKValue, string receiverName)
{
DataSet ds = DBUtils.ExecuteDataSet(ctx, @"select b.FASSIGNID,b.FAPPROVALASSIGNID,a.FRECEIVERNAMES
from t_wf_assign a
join T_WF_APPROVALASSIGN b on a.fassignid=b.fassignid
where b.Fobjecttypeid=@FormID
and b.Fkeyvalue=@pKValue and a.FSTATUS=0",
new List<SqlParam>
{
new SqlParam("@FormID", DbType.String, formId),
new SqlParam("@pKValue", DbType.String, pKValue)
});
DataRow row = ds.Tables[0].Rows.Cast<DataRow>().FirstOrDefault(dr => dr["FRECEIVERNAMES"].ToString().Split(',').Any(r => r == receiverName));
if (row == null) return new List<AssignResult> { };
string assignId = row["FASSIGNID"].ToString();
string approvalAssignId = row["FAPPROVALASSIGNID"].ToString();
string _approvalItemId = AssignmentServiceHelper.OpenApprovalItem(ctx, ctx.UserId, assignId, false);
var _approvalItem = AssignmentServiceHelper.GetApprovalItemById(ctx, _approvalItemId);
return _approvalItem.Actions.ToList();
}
private void SubmitWorkflow(Context ctx,string formId, string pKValue, string receiverName, string actionResult, string disposition, bool isApprovalFlow)
{
DataSet ds = DBUtils.ExecuteDataSet(ctx, @"select b.FASSIGNID,b.FAPPROVALASSIGNID,a.FACTINSTID,a.FRECEIVERNAMES
from t_wf_assign a
join T_WF_APPROVALASSIGN b on a.fassignid=b.fassignid
where b.Fobjecttypeid=@FormID
and b.Fkeyvalue=@pKValue and a.FSTATUS=0",
new List<SqlParam>
{
new SqlParam("@FormID", DbType.String, formId),
new SqlParam("@pKValue", DbType.String, pKValue)
});
DataRow row = ds.Tables[0].Rows.Cast<DataRow>().FirstOrDefault(dr => dr["FRECEIVERNAMES"].ToString().Split(',').Any(r => r == receiverName));
if (row == null) throw new Exception("未找到待办任务");
string assignId = row["FASSIGNID"].ToString();
string approvalAssignId = row["FAPPROVALASSIGNID"].ToString();
string _approvalItemId = AssignmentServiceHelper.OpenApprovalItem(ctx, ctx.UserId, assignId, false);
FormMetadata formMetadata = MetaDataServiceHelper.GetFormMetaData(ctx, formId);
DynamicObject ObjData = BusinessDataServiceHelper.LoadSingle(ctx, pKValue, formMetadata.BusinessInfo.GetDynamicObjectType());
BusinessInfo businessInfo = formMetadata.BusinessInfo;
var _approvalItem = AssignmentServiceHelper.GetApprovalItemById(ctx, _approvalItemId);
if (_approvalItem == null) throw new Exception("待办任务所在的流程实例不在运行中,不能进行处理!");
_approvalItem.ObjData = ObjData;
_approvalItem.ReceiverPostId = 0;
_approvalItem.ActionResult = actionResult;
_approvalItem.Disposition = disposition.ToString();
AssignResult assignResult = _approvalItem.Actions != null ? _approvalItem.Actions.FirstOrDefault(i => i.Id == actionResult) : null;
AssignResultApprovalType approvalType = assignResult != null ? assignResult.ApprovalType : AssignResultApprovalType.None;
_approvalItem.ActionResultType = approvalType;
_approvalItem.Status = ApprovalItemStatus.Completed;
DateTime timeNow = TimeServiceHelper.GetSystemDateTime(ctx);
_approvalItem.CompletedTime = timeNow;
ObjectActivityInstance _activityInstance = AssignmentServiceHelper.ConvertActivityModel(
ctx, businessInfo, approvalAssignId, _approvalItem);
var option = OperateOption.Create();
if (isApprovalFlow)
{
ApprovalAssignmentContext assignCtx = new ApprovalAssignmentContext()
{
ApprovalItems = new List<ApprovalItem>() { _approvalItem },
Info = businessInfo,
Option = option
};
assignCtx.NextActHandler = null;
assignCtx.RejectReturn = false;
assignCtx.ActivityInstance = _activityInstance;
if (actionResult == AssignResultApprovalType.Reject.ToString())
{
var _policy = AssignmentServiceHelper.GetApprovalAssignByAssignId(ctx, assignId);
var nextHandleWrapper = new Kingdee.BOS.ApprovalFlow.PlugIns.Mobile.MobNextHandleWrapper(assignId, ctx, _policy);
assignCtx.Target = nextHandleWrapper.RejectActivityModel.ActivityId;
}
ApprovalAssignmentServiceHelper.SubmitApprovalItem(Context, assignCtx);
}
else
{
ApprovalAssignmentContext assignCtx = new ApprovalAssignmentContext()
{
ApprovalItems = new List<ApprovalItem>() { _approvalItem },
Info = businessInfo,
Option = option
};
assignCtx.NextActHandler = null;
assignCtx.RejectReturn = false;
assignCtx.ActivityInstance = _activityInstance;
AssignmentServiceHelper.SubmitApprovalItem(Context, assignCtx);
}
}
推荐阅读