K3Cloud 在插件中调用工作流

Posted jlz-s

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了K3Cloud 在插件中调用工作流相关的知识,希望对你有一定的参考价值。

在插件中可以调用ApprovalAssignmentServiceHelper.SubmitApprovalItems()进行工作流审批。SubmitApprovalItems()需要一个AssignmentContext参数,而AssignmentContext属性非常多,构造起来很麻烦。
因此我写了两个方法,将SubmitApprovalItems()再次封装,传入几个简单参数,就能完成审批。

 

  1   /// <summary>
  2         /// 审批单据,actionName为空时,自动寻找第一个同意审批项;
  3         /// 有多个类型为通过的审批项时,可用actionName指定审批项,如actionName="审批通过"
  4         /// </summary>
  5         /// <param name="formId">单据FormId</param>
  6         /// <param name="pKValue">单据主键</param>
  7         /// <param name="receiverName">处理人</param>
  8         /// <param name="disposition">审批意见</param>
  9         /// <param name="isApprovalFlow">是否为审批流</param>
 10         public void ApproveBill(string formId, string pKValue, string receiverName, string disposition, bool isApprovalFlow = false, string actionName = null)
 11         {
 12             List<AssignResult> assignResults = GetApproveActions(formId, pKValue, receiverName);
 13             AssignResult approvalAssignResults = assignResults.FirstOrDefault(r => r.ApprovalType == AssignResultApprovalType.Forward);
 14             if (approvalAssignResults == null) throw new Exception("未找到审批项。");
 15             SubmitWorkflow(formId, pKValue, receiverName, approvalAssignResults.Id, disposition, isApprovalFlow);
 16         }
 17 
 18         /// <summary>
 19         /// 驳回单据,actionName为空时,自动寻找一个驳回审批项;
 20         /// 有多个类型为驳回的审批项时(如驳回、打回发起人),可用actionName指定审批项,如actionName="打回发起人"
 21         /// </summary>
 22         public void RejectBill(string formId, string pKValue, string receiverName, string disposition, bool isApprovalFlow = false, string actionName = null)
 23         {
 24             List<AssignResult> assignResults = GetApproveActions(formId, pKValue, receiverName);
 25             if (!string.IsNullOrEmpty(actionName))
 26                 assignResults = assignResults.Where(r => r.Name.Any(p => p.Value == actionName)).ToList();
 27             else
 28                 assignResults = assignResults.OrderBy(r => r.Name.Any(p => p.Value == "打回发起人")).ToList();
 29             AssignResult rejectAssignResults = assignResults.FirstOrDefault(r => r.ApprovalType == AssignResultApprovalType.Reject);
 30             if (rejectAssignResults == null) throw new Exception("未找到驳回审批项。");
 31             SubmitWorkflow(formId, pKValue, receiverName, rejectAssignResults.Id, disposition, isApprovalFlow);
 32         }
 33 
 34         private List<AssignResult> GetApproveActions(string formId, string pKValue, string receiverName)
 35         {
 36             DataSet ds = DBUtils.ExecuteDataSet(this.Context, @"select b.FASSIGNID,b.FAPPROVALASSIGNID
 37                                 from t_wf_assign a 
 38                                 join T_WF_APPROVALASSIGN b on a.fassignid=b.fassignid 
 39                                 where [email protected] 
 40                                 and [email protected] and a.FSTATUS=0 and [email protected]",
 41                         new List<SqlParam>
 42                         {
 43                             new SqlParam("@FormID", DbType.String, formId),
 44                             new SqlParam("@pKValue", DbType.String, pKValue),
 45                             new SqlParam("@FRECEIVERNAMES", DbType.String, receiverName)
 46                         });
 47             DataRow row = ds.Tables[0].Rows.Cast<DataRow>().FirstOrDefault();
 48             if (row == null) return new List<AssignResult> { };
 49 
 50             string assignId = row["FASSIGNID"].ToString();
 51             string approvalAssignId = row["FAPPROVALASSIGNID"].ToString();
 52             string _approvalItemId = AssignmentServiceHelper.OpenApprovalItem(this.Context, this.Context.UserId, assignId, false);
 53             var _approvalItem = AssignmentServiceHelper.GetApprovalItemById(this.View.Context, _approvalItemId);
 54             return _approvalItem.Actions.ToList();
 55         }
 56 
 57 
 58         private void SubmitWorkflow(string formId, string pKValue, string receiverName, string actionResult, string disposition, bool isApprovalFlow)
 59         {
 60             DataSet ds = DBUtils.ExecuteDataSet(this.Context, @"select b.FASSIGNID,b.FAPPROVALASSIGNID,a.FACTINSTID
 61                                 from t_wf_assign a 
 62                                 join T_WF_APPROVALASSIGN b on a.fassignid=b.fassignid 
 63                                 where [email protected] 
 64                                 and [email protected] and a.FSTATUS=0 and [email protected]",
 65                         new List<SqlParam>
 66                         {
 67                             new SqlParam("@FormID", DbType.String, formId),
 68                             new SqlParam("@pKValue", DbType.String, pKValue),
 69                             new SqlParam("@FRECEIVERNAMES", DbType.String, receiverName)
 70                         });
 71             DataRow row = ds.Tables[0].Rows.Cast<DataRow>().FirstOrDefault();
 72             if (row == null) throw new Exception("未找到待办任务");
 73 
 74             string assignId = row["FASSIGNID"].ToString();
 75             string approvalAssignId = row["FAPPROVALASSIGNID"].ToString();
 76             string _approvalItemId = AssignmentServiceHelper.OpenApprovalItem(this.Context, this.Context.UserId, assignId, false);
 77             FormMetadata formMetadata = MetaDataServiceHelper.GetFormMetaData(this.Context, formId);
 78             DynamicObject ObjData = BusinessDataServiceHelper.LoadSingle(this.Context, pKValue, formMetadata.BusinessInfo.GetDynamicObjectType());
 79             BusinessInfo businessInfo = formMetadata.BusinessInfo;
 80 
 81             var _approvalItem = AssignmentServiceHelper.GetApprovalItemById(this.View.Context, _approvalItemId);
 82             if (_approvalItem == null) throw new Exception("待办任务所在的流程实例不在运行中,不能进行处理!");
 83             _approvalItem.ObjData = ObjData;
 84             _approvalItem.ReceiverPostId = 0;
 85             _approvalItem.ActionResult = actionResult;
 86             _approvalItem.Disposition = disposition.ToString();
 87             AssignResult assignResult = _approvalItem.Actions != null ? _approvalItem.Actions.FirstOrDefault(i => i.Id == actionResult) : null;
 88             AssignResultApprovalType approvalType = assignResult != null ? assignResult.ApprovalType : AssignResultApprovalType.None;
 89             _approvalItem.ActionResultType = approvalType;
 90             _approvalItem.Status = ApprovalItemStatus.Completed;
 91             DateTime timeNow = TimeServiceHelper.GetSystemDateTime(this.Context);
 92             _approvalItem.CompletedTime = timeNow;
 93 
 94             ObjectActivityInstance _activityInstance = AssignmentServiceHelper.ConvertActivityModel(
 95                     this.Context, businessInfo, approvalAssignId, _approvalItem);
 96 
 97             var option = OperateOption.Create();
 98 
 99             if (isApprovalFlow)
100             {
101                 ApprovalAssignmentContext assignCtx = new ApprovalAssignmentContext()
102                 {
103                     ApprovalItems = new List<ApprovalItem>() { _approvalItem },
104                     Info = businessInfo,
105                     Option = option
106                 };
107                 assignCtx.NextActHandler = null;
108                 assignCtx.RejectReturn = false;
109                 assignCtx.ActivityInstance = _activityInstance;
110                 if (actionResult == AssignResultApprovalType.Reject.ToString())
111                 {
112                     string actInstId = row["FACTINSTID"].ToString();
113                     Kingdee.BOS.Workflow.App.Core.ProcInstService procInstService = new Kingdee.BOS.Workflow.App.Core.ProcInstService();
114                     var rejectActivityIds = procInstService.GetBackActInstList(this.Context, actInstId, true).Select(r => r.ActivityId);
115                     if (!rejectActivityIds.Any()) throw new Exception("无驳回节点");
116                     assignCtx.Target = rejectActivityIds.FirstOrDefault();
117                 }
118                 ApprovalAssignmentServiceHelper.SubmitApprovalItem(Context, assignCtx);
119             }
120             else
121             {
122                 ApprovalAssignmentContext assignCtx = new ApprovalAssignmentContext()
123                 {
124                     ApprovalItems = new List<ApprovalItem>() { _approvalItem },
125                     Info = businessInfo,
126                     Option = option
127                 };
128                 assignCtx.NextActHandler = null;
129                 assignCtx.RejectReturn = false;
130                 assignCtx.ActivityInstance = _activityInstance;
131                 AssignmentServiceHelper.SubmitApprovalItem(Context, assignCtx);
132             }
133         }

 


以上是关于K3Cloud 在插件中调用工作流的主要内容,如果未能解决你的问题,请参考以下文章

手把手搭建K3cloud插件开发环境

K3Cloud Python套打插件开发记录

调用金蝶K3Cloud接口

k3cloud 中 kingdee.bos在哪个文件中

如何在Sublime Text中添加代码片段

K3Cloud的整体开发思路