using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Configuration; using Autofac; using EMIS.DataLogic.SystemDAL; using EMIS.Entities; using Bowin.Common.Utility; using EMIS.CommonLogic.PlugworkflowServices; using System.Reflection; using EMIS.ViewModel.SystemView; using Bowin.Common.Linq.Entity; using System.Linq.Expressions; using EMIS.ViewModel.WorkflowManage; using EMIS.Utility.FormValidate; using EMIS.Utility; using Bowin.Common.Mobile; using System.Transactions; namespace EMIS.CommonLogic.SystemServices { public class WorkflowServices : BaseServices, IWorkflowServices { public ProcessWorkfloconfigDAL ProcessWorkfloconfigDAL { get; set; } public RoleDAL RoleDAL { get; set; } public Lazy ProcessRelationServices { get; set; } public Lazy UserServices { get; set; } private PlugworkflowServices.entreeSoapClient client = new PlugworkflowServices.entreeSoapClient(); private PlugworkflowServices.VerificationModel vm = new PlugworkflowServices.VerificationModel(); public Func MessageFunc = null; /// /// 接入系统ID /// private static string sysID = ConfigurationSettings.AppSettings["sys_InsertingID"].ToString(); /// /// 接入系统密码 /// private static string sysPwd = ConfigurationSettings.AppSettings["sys_InsertingPwd"].ToString(); /// /// 查询对应tableName的EntityType /// /// /// private Type GetEntityType(string tableName) { var assembly = typeof(EMISNewContext).Assembly; return assembly.GetType("EMIS.Entities." + tableName, false, true); } /// /// 工作流验证 /// private void InitVerificationModel() { vm.AppUID = sysID; // 接入系统ID vm.time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); vm.AppUPwd = (sysPwd + vm.time).MD5(); //验证密码 } /// /// 查询对应的接入流程表中AppID(接入流程表ID) /// /// /// private string GetAppIDByTableName(string tableName) { string AppID = string.Empty; //流程ID Sys_Workfloconfig workfloconfig = ProcessWorkfloconfigDAL.GetWorkfloconfigQueryable() .Where(x => x.TableName == tableName).FirstOrDefault(); if (workfloconfig != null) { AppID = workfloconfig.ProcessID.ToString(); } return AppID; } /// /// 查询对应的接入流程表信息List /// /// /// private List GetStatusList(string tableName) { //接入流程表ID string AppID = this.GetAppIDByTableName(tableName); this.InitVerificationModel(); PlugworkflowServices.ProcessItemAndActionResultModel piarm = client.FlowProcessAndActionSetting(AppID, vm); if (piarm.doNum == 1) { return piarm.resultDataList.ToList(); } else { return new List(); } } /// /// 查询对应的接入流程表开始流程环节的信息List /// /// /// private List GetStartStatusList(string tableName) { //接入流程表ID string AppID = this.GetAppIDByTableName(tableName); this.InitVerificationModel(); PlugworkflowServices.ProcessItemAndActionResultModel piarm = client.FlowProcessAndActionSetting(AppID, vm); if (piarm.doNum == 1) { return piarm.resultDataList.Where(x => x.Tp == "start").ToList(); } else { return new List(); } } /// /// 查询对应的接入流程表开始流程环节的Pid(对应的流程环节Pid) /// /// /// public int? GetStartStatus(string tableName) { var statusList = this.GetStatusList(tableName); return statusList.Where(x => x.Tp == "start").Select(x => x.Pid).FirstOrDefault(); } /// /// 查询对应的接入流程表已退回流程环节Pid(注:不包含开始环节、结束环节且为[BP]标识) /// /// /// public int? GetSendBackStatus(string tableName) { var statusList = this.GetStatusList(tableName); return statusList.Where(x => x.Tp == "auto" && (x.Description ?? "").Contains("[BP]")).Select(x => x.Pid).FirstOrDefault(); } /// /// 查询对应的接入流程表已通过流程环节Pid(注:不包含开始环节、结束环节且为[PASS]标识) /// /// /// public int? GetPassNoEndStatus(string tableName) { var statusList = this.GetStatusList(tableName); return statusList.Where(x => x.Tp == "auto" && (x.Description ?? "").Contains("[PASS]")).Select(x => x.Pid).FirstOrDefault(); } /// /// 查询对应的结束流程环节Pid /// /// /// public List GetEndStatus(string tableName) { var statusList = this.GetStatusList(tableName); return statusList.Where(x => x.Tp == "end").Select(x => x.Pid).ToList(); } /// /// 查询对应的结束流程环节且正常结束的流程环节Pid(注:结束环节且不为[BP]标识的) /// /// /// public int? GetCorrectEndStatus(string tableName) { var statusList = this.GetStatusList(tableName); return statusList.Where(x => x.Tp == "end" && !(x.Description ?? "").Contains("[BP]")).Select(x => x.Pid).FirstOrDefault(); } /// /// 查询重录待审,重新录入环节Pid(用于期末设定学生名单背景颜色) /// /// /// public List GetRebutStatus(string tableName) { var statusList = this.GetStatusList(tableName); return statusList.Where(x => (x.Description ?? "").Contains("[Rebut]")).Select(x => x.Pid).ToList(); } /// /// 查询对应的接入流程表非正常结束的流程环节Pid(注:通常为结束环节且为[BP]标识的) /// /// /// public List GetBackpointStatus(string tableName) { var statusList = this.GetStatusList(tableName); return statusList.Where(x => (x.Description ?? "").Contains("[BP]")).Select(x => x.Pid).ToList(); } /// /// 查询对应的接入流程表中开始环节、审核流程环节Pid(注:不包含结束、非正常[BP]结束环节) /// /// /// public List GetStartApproveStatusList(string tableName) { try { return this.GetStatusList(tableName).Where(x => x.Tp != "end" && !(x.Description ?? "").Contains("[BP]")) .Select(x => x.Pid).ToList(); } catch (Exception ex) { throw new Exception(ex.Message); } } /// /// 查询对应的接入流程表中全部流程环节信息(WorkflowStatusView) /// /// /// public List GetStatusViewList(string tableName) { return this.GetStatusList(tableName).Select(x => new WorkflowStatusView { ID = x.Pid, Name = x.PName, Description = x.Description ?? "" }).OrderBy(x => x.ID).ToList(); } /// /// 查询对应的接入流程表中审核流程环节信息(WorkflowStatusView,不包含开始、结束、非正常[BP]结束环节) /// /// /// public List GetApproveStatusViewList(string tableName) { try { return this.GetStatusList(tableName).Where(x => x.Tp != "start" && x.Tp != "end" && !(x.Description ?? "").Contains("[BP]")) .Select(x => new WorkflowStatusView { ID = x.Pid, Name = x.PName, Description = x.Description ?? "" }).OrderBy(x => x.ID).ToList(); } catch (Exception ex) { throw new Exception(ex.Message); } } /// /// 查询对应的接入流程表中全部非开始流程环节信息(WorkflowStatusView,不包含开始环节) /// /// /// public List GetNoStartStatusViewList(string tableName) { try { return this.GetStatusList(tableName).Where(x => x.Tp != "start") .Select(x => new WorkflowStatusView { ID = x.Pid, Name = x.PName, Description = x.Description ?? "" }) .OrderBy(x => x.ID).ToList(); } catch (Exception ex) { throw new Exception(ex.Message); } } /// /// 查询对应的接入流程表中全部结束流程环节信息 /// /// /// public List GetEndStatusViewList(string tableName) { try { return this.GetStatusList(tableName).Where(x => x.Tp == "end") .Select(x => new WorkflowStatusView { ID = x.Pid, Name = x.PName, Description = x.Description ?? "" }).OrderBy(x => x.ID).ToList(); } catch (Exception ex) { throw new Exception(ex.Message); } } /// /// 查询工作流程中是否有对应的权限(根据对应的信息-处理人、处理人标识) /// /// /// /// /// /// private bool IsCanSubmit(string tableName, Guid userID, List formIDs, Func, Guid, bool> dataRangeUserFunc = null) { bool isMatched = false; //获取当前用户 var user = UserServices.Value.GetUserByID(userID); this.InitVerificationModel(); List listNetxtDetail = this.GetStatusList(tableName) .Where(x => x.Tp == "start").ToList(); var userAndRoleList = listNetxtDetail.SelectMany(x => x.PendingUserType.Split(',')).Distinct().ToList(); var posibleUserList = UserServices.Value.GetUserListByLoginID(userAndRoleList); IList listrole = UserServices.Value.GetUserRoles(user.UserID); foreach (var listDetail in listNetxtDetail) { var pandingUserList = listDetail.PendingUserType.Split(','); for (int i = 0; i < pandingUserList.Length; i++) { string userAndrole = pandingUserList[i]; //获取工作流配置处理人的ID var userlist = posibleUserList.FirstOrDefault(x => x.LoginID == userAndrole); //判断工作流配置处理是否是用户操作还是角色操作处理 if (userlist != null) { if (userlist.UserID == userID) { isMatched = true; } } else { //如果配置角色和当前登录人角色对应 foreach (var role in listrole) { if (userAndrole == role.RoleName) { isMatched = true; } } } } } if (!isMatched) { return false; } if (dataRangeUserFunc != null) { return dataRangeUserFunc(formIDs, userID); } return true; } /// /// 查询工作流程中是否有对应的操作权限——动作(根据对应的信息-处理人、处理人标识) /// /// 主表表名 /// 用户ID /// 审核动作ID(路由ID) /// 表单ID列表 /// 获取数据范围用户的方法 /// public bool IsCanApprove(string tableName, Guid userID, Guid actionID, List formIDs, Func, Guid, bool> dataRangeUserFunc = null) { string AppID = this.GetAppIDByTableName(tableName); //流程ID List instanceIDList = new List(); List list = new List(); //获取当前用户 var user = UserServices.Value.GetUserByID(userID); var role = UserServices.Value.GetUserRoles(userID); this.InitVerificationModel(); //流程关联表 List processRelationList = ProcessWorkfloconfigDAL.GetProcessRelationQueryable() .Where(x => x.TableName == tableName && formIDs.Contains((Guid)x.formID)).ToList(); if (processRelationList.Count > 0) instanceIDList = processRelationList.Select(x => x.InstanceID).ToList(); PendingWorkResultModel pwrm = client.PendingWorkBatchList(AppID, instanceIDList.ToArray(), vm); //读取待办 if (pwrm.doNum == 1) { var userPendingWorkList = pwrm.resultDataList.GroupBy(x => x.InstanceID) .Where(x => !x.Any(n => role.Any(w => w.RoleName == n.PendingUsers) || n.PendingUsers == user.LoginID)).ToList(); if (userPendingWorkList.Count > 0) { return false; } } if (dataRangeUserFunc != null) { return dataRangeUserFunc.Invoke(formIDs, userID); } return true; } /// /// /// /// /// /// /// public List IsCanApprove(string tableName, Guid userID, List formIDs) { string AppID = this.GetAppIDByTableName(tableName); //流程ID List instanceIDList = new List(); List list = new List(); //获取当前用户 var user = UserServices.Value.GetUserByID(userID); var role = UserServices.Value.GetUserRoles(userID); this.InitVerificationModel(); //流程关联表 List processRelationList = ProcessWorkfloconfigDAL.GetProcessRelationQueryable() .Where(x => x.TableName == tableName && formIDs.Contains((Guid)x.formID)).ToList(); if (processRelationList.Count > 0) instanceIDList = processRelationList.Select(x => x.InstanceID).ToList(); PendingWorkResultModel pwrm = client.PendingWorkBatchList(AppID, instanceIDList.ToArray(), vm); //读取待办 if (pwrm.doNum == 1) { var userPendingWorkList = pwrm.resultDataList.GroupBy(x => x.InstanceID) .Where(x => x.Any(n => role.Any(w => w.RoleName == n.PendingUsers) || n.PendingUsers == user.LoginID)).ToList(); var permissionResult = (from fid in formIDs join pr in processRelationList on fid equals pr.formID into dpr from pr in dpr.DefaultIfEmpty() join upw in userPendingWorkList on pr.InstanceID equals upw.Key into dupw from upw in dupw.DefaultIfEmpty() select new { FormID = fid, IsCanApprove = (pr != null && upw != null) } into result group result by new { result.FormID, result.IsCanApprove } into g select new WorkflowPermissionView { FormID = g.Key.FormID, IsCanApprove = g.Key.IsCanApprove }).ToList(); return permissionResult; } else { throw new Exception(pwrm.doMessage); } } /// /// 重载方法,启动对应的流程并执行指定的动作至下一环节(提交),适用于开始节点有两个以上动作时 /// /// 主表表名 /// 需要提交的记录主键ID /// 提交人ID /// 动作ID /// 提交意见 /// 获取数据范围用户的方法 public void StartUp(string tableName, Guid formID, Guid userID, Guid actionID, string comment, Func, Guid, bool> dataRangeUserFunc = null) { try { //查询接入流程表ID string AppID = this.GetAppIDByTableName(tableName); string instanceID = string.Empty; bool isCanSubmit = this.IsCanSubmit(tableName, userID, new List { formID }, dataRangeUserFunc); if (!isCanSubmit) { throw new Exception("对不起,您没权限进行操作。"); } //获取当前用户 var user = UserServices.Value.GetUserByID(userID); this.InitVerificationModel(); PlugworkflowServices.NextDetailModel listNetxtDetail = this.GetStatusList(tableName) .Where(x => x.Tp == "start").FirstOrDefault(); var pendingUserList = listNetxtDetail.PendingUserType.Split(','); //流程关联表 Sys_ProcessRelation processRelation = ProcessWorkfloconfigDAL.GetProcessRelationQueryable() .Where(x => x.TableName == tableName && x.formID == formID).FirstOrDefault(); if (processRelation != null) instanceID = processRelation.InstanceID; if (string.IsNullOrEmpty(instanceID)) //判断是否已经存在流程如果存在就直接提交审核 { var result = client.InstanceProcessStart(AppID, "", user.LoginID, vm); //启动流程 if (result.doNum == 1) { PendingWorkItem pwi = result.resultDataList.FirstOrDefault(); //转换 ProcessRelationServices.Value.ProcessRelationAdd(formID, pwi.InstanceID, tableName); //保存流程关联表 } else { throw new Exception(result.doMessage); } } try { //判断是否卡在开始环节,否则不能随便审核 var result = client.PendingWorkList("", "", AppID, instanceID, "", "", "A", "1", "100", vm); if (result.doNum == 1) { if (result.resultDataList.Length == 0) { throw new Exception("该环节并无后续动作,无法提交。"); } var instanceProcessModel = result.resultDataList.FirstOrDefault(); if (instanceProcessModel.PID == this.GetStartStatus(tableName)) { Approve(tableName, formID, userID, actionID, comment, dataRangeUserFunc); } else { throw new Exception("该流程已经提交,不能重复提交。"); } } else { throw new Exception(result.doMessage); } } catch (Exception ex) { throw new Exception(ex.Message); } } catch (Exception ex) { throw new Exception(ex.Message); } } /// /// 启动对应的流程并执行指定的动作至下一环节(提交) /// /// 主表表名 /// 需要提交的记录主键ID /// 提交人ID /// 提交意见 /// 获取数据范围用户的方法 public void StartUp(string tableName, Guid formID, Guid userID, string comment = "", Func, Guid, bool> dataRangeUserFunc = null) { this.StartUp(tableName, formID, userID, Guid.Empty, comment, dataRangeUserFunc); } /// /// 启动对应的流程并执行指定的动作至下一环节(提交) /// /// 主表表名 /// 需要提交的记录主键ID /// 提交人ID /// 动作ID /// 提交意见 /// 获取数据范围用户的方法 public void BatchStartUp(string tableName, List formIDList, Guid userID, string comment, Func, Guid, bool> dataRangeUserFunc = null) { try { //查询接入流程表ID if (formIDList.Count == 0) { return; } string AppID = this.GetAppIDByTableName(tableName); List instanceIDList = new List(); bool isCanSubmit = this.IsCanSubmit(tableName, userID, formIDList, dataRangeUserFunc); if (!isCanSubmit) { throw new Exception("对不起,您没权限进行操作。"); } var newProcessRelationList = new List(); //获取当前用户 var user = UserServices.Value.GetUserByID(userID); this.InitVerificationModel(); PlugworkflowServices.NextDetailModel listNetxtDetail = this.GetStatusList(tableName) .Where(x => x.Tp == "start").FirstOrDefault(); var pendingUserList = listNetxtDetail.PendingUserType.Split(','); //流程关联表 List processRelationList = ProcessWorkfloconfigDAL.GetProcessRelationQueryable() .Where(x => x.TableName == tableName && formIDList.Contains((Guid)x.formID)).ToList(); if (processRelationList.Count > 0) instanceIDList = processRelationList.Select(x => x.InstanceID).ToList(); var newProcessList = (from fid in formIDList join pr in processRelationList on fid equals pr.formID into dpr from pr in dpr.DefaultIfEmpty() where pr == null select fid).ToList(); if (newProcessList.Count > 0) //判断是否已经存在流程如果存在就直接提交审核 { var result = client.InstanceProcessBatchStart(newProcessList.Count, AppID, "", user.LoginID, vm); //启动流程 if (result.doNum == 1) { List pwi = result.resultDataList.ToList(); //转换 for (var i = 0; i < newProcessList.Count; i ++ ) { newProcessRelationList.Add(new BaseProcessRelationView { FormID = newProcessList[i], InstanceID = pwi[i].InstanceID }); instanceIDList.Add(pwi[i].InstanceID); } ProcessRelationServices.Value.ProcessRelationAdd(newProcessRelationList, tableName); //保存流程关联表 } else { throw new Exception(result.doMessage); } } try { //判断是否卡在开始环节,否则不能随便审核 var result = client.PendingWorkBatchList(AppID, instanceIDList.ToArray(), vm); if (result.doNum == 1) { var noNextList = instanceIDList.Where(x => !result.resultDataList.Select(w => w.InstanceID).Contains(x)).ToList(); if (noNextList.Count > 0) { throw new Exception("部分数据在该环节并无后续动作,无法提交,可能由于没有配置相应权限,请联系系统管理员检查。"); } var instanceProcessModel = result.resultDataList.FirstOrDefault(); if (instanceProcessModel.PID == this.GetStartStatus(tableName)) { BatchApprove(tableName, formIDList, userID, Guid.Empty, comment, dataRangeUserFunc); } else { throw new Exception("该流程已经提交,不能重复提交。"); } } else { throw new Exception(result.doMessage); } } catch (Exception ex) { throw new Exception(ex.Message); } } catch (Exception ex) { throw new Exception(ex.Message); } } /// /// 审核流程(不包括提交——不包含流程启动) /// /// 主表表名 /// 需要审核的记录主键ID /// 审核人ID /// 审核动作ID /// 提交意见 /// 获取数据范围用户的方法 public void Approve(string tableName, Guid formID, Guid userID, Guid actionID, string comment = "", Func, Guid, bool> dataRangeUserFunc = null) { try { //接入流程表ID string AppID = this.GetAppIDByTableName(tableName); string instanceID = string.Empty; List nextWorkby = new List(); IList dataRangeUserList = new List(); bool isCanApprove = this.IsCanApprove(tableName, userID, actionID, new List { formID }, dataRangeUserFunc); //获取有权限审核的用户 if (!isCanApprove) { throw new Exception("对不起,您没权限进行操作。"); } //获取当前用户 var user = UserServices.Value.GetUserByID(userID); var roleList = UserServices.Value.GetUserRoles(userID); this.InitVerificationModel(); var startStepList = this.GetStatusList(tableName).Where(x => x.Tp == "start").ToList(); var endStepList = this.GetStatusList(tableName).Where(x => x.Tp == "end").ToList(); //流程关联表 Sys_ProcessRelation processRelation = ProcessWorkfloconfigDAL.GetProcessRelationQueryable() .Where(x => x.TableName == tableName && x.formID == formID).FirstOrDefault(); if (processRelation != null) instanceID = processRelation.InstanceID; PendingWorkResultModel pwrm = client.PendingWorkList("", "", AppID, instanceID, "", "", "A", "1", "100", vm); //读取待办 if (pwrm.doNum == 1) { var userPendingWorkList = pwrm.resultDataList.Where(x => roleList.Any(w => w.RoleName == x.PendingUsers) || x.PendingUsers == user.LoginID).ToList(); if (userPendingWorkList.Count == 0) { throw new Exception("对不起,您没权限进行操作。"); } foreach (var itempwi in userPendingWorkList) { if (itempwi.WorkTime == null) { List nextStepList = null; var processItemResultModel = client.ProcessLoadNextDetail(itempwi.InstanceProcessID, vm); //获取流程明细 if (processItemResultModel.resultDataItem.Tp == "start") { throw new Exception("当前流程并未提交,请先提交再进行审核。"); } if (processItemResultModel.resultDataItem.Tp == "end") { throw new Exception("当前流程已经结束,无法审核。"); } //判断是否有指定下一步动作 if (actionID != null && actionID != Guid.Empty) nextStepList = processItemResultModel.resultDataItem.NextSteps .Where(x => x.ActionID == actionID.ToString()).ToList(); //获取下一步用户 else nextStepList = processItemResultModel.resultDataItem.NextSteps.ToList(); //获取下一步用户 //根据角色ID 获取用户ID foreach (var itemnext in nextStepList) { string nextActionID = itemnext.ActionID.ToString(); string methodName = itemnext.ActionFunc; int pid = itemnext.Pid; //if (startStepList.Any(x => x.Pid == itemnext.Pid)) //如果是提交到起始环节 //{ // ProcessDetailResultModel pdrm = client.LoadProcessDetails(AppID, instanceID, vm); //获取处理记录 // if (pdrm.doNum == 1) // { // //获取开始环节信息 // InstanceProcessModel ipm = pdrm.resultDataList.Where(x => x.tp == "start").FirstOrDefault(); // if (ipm != null) // { // //获取下一步的待办名单 // nextWorkby.Add(ipm.WorkBy); // } // } //} //else if (endStepList.Any(x => x.Pid == pid)) { nextWorkby = new List(); } else { //获取下一步的待办名单 nextWorkby = itemnext.PendingUserType.Split(',').ToList(); } if (nextWorkby.Count == 0 && !endStepList.Any(x => x.Pid == pid)) { throw new Exception("后续的流程环节并没有适合的审批人,无法提交。"); } if (!string.IsNullOrEmpty(methodName)) { ReflectorHelper.RunMethod(methodName, new List() { formID }, userID); } //推送下一步流程 ResultModel rm = client.ProcessMoveNext(itempwi.InstanceProcessID, nextActionID , new ActionFunc[] { }, nextWorkby.Distinct().ToArray(), comment, user.LoginID, new string[] { }, vm); if (rm.doNum != 1) throw new Exception(rm.doMessage); else { var entity = UnitOfWork.Set(this.GetEntityType(tableName)).Find(formID); dynamic dyEntity = entity; var entityType = entity.GetType(); if (entityType.GetProperty("ApprovalStatus") != null) { dyEntity.ApprovalStatus = pid; } else if (entityType.GetProperty("RecordStatus") != null) { dyEntity.RecordStatus = pid; } UnitOfWork.Update(dyEntity); UnitOfWork.Commit(); this.SendMobileMQ(formID, nextWorkby.ToArray()); return; } } } } } else { throw new Exception(pwrm.doMessage); } } catch (Exception ex) { throw new Exception(ex.Message); } } /// /// 审核流程(不包括提交——不包含流程启动) /// /// 主表表名 /// 需要审核的记录主键ID /// 审核人ID /// 审核动作ID /// 提交意见 /// 获取数据范围用户的方法 public void BatchApprove(string tableName, List formIDList, Guid userID, Guid actionID, string comment = "", Func, Guid, bool> dataRangeUserFunc = null) { try { if (formIDList.Count == 0) { return; } //接入流程表ID string AppID = this.GetAppIDByTableName(tableName); List instanceIDList = new List(); List nextWorkby = new List(); IList dataRangeUserList = new List(); string approveMethodName = null; string nextActionID = null; int? pid = null; bool isCanApprove = this.IsCanApprove(tableName, userID, actionID, formIDList, dataRangeUserFunc); //获取有权限审核的用户 if (!isCanApprove) { throw new Exception("对不起,您没权限进行操作。"); } //获取当前用户 var user = UserServices.Value.GetUserByID(userID); var roleList = UserServices.Value.GetUserRoles(userID); this.InitVerificationModel(); var startStepList = this.GetStatusList(tableName).Where(x => x.Tp == "start").ToList(); var endStepList = this.GetStatusList(tableName).Where(x => x.Tp == "end").ToList(); //流程关联表 List processRelationList = ProcessWorkfloconfigDAL.GetProcessRelationQueryable() .Where(x => x.TableName == tableName && formIDList.Contains((Guid)x.formID)).ToList(); if (processRelationList.Count == formIDList.Count) instanceIDList = processRelationList.Select(x => x.InstanceID).ToList(); else throw new Exception("部分记录并未提交,无法直接审核。"); PendingWorkResultModel pwrm = client.PendingWorkBatchList(AppID, instanceIDList.ToArray(), vm); //读取待办 if (pwrm.doNum == 1) { var userPendingWorkList = pwrm.resultDataList.Where(x => roleList.Any(w => w.RoleName == x.PendingUsers) || x.PendingUsers == user.LoginID).ToList(); if (userPendingWorkList.Select(x => x.InstanceID).Distinct().Count() != formIDList.Count) { throw new Exception("对不起,对于部分数据,您没权限进行操作,请重新选择数据提交。"); } if (userPendingWorkList.GroupBy(x => new { x.AppID, x.PID }).Count() > 1) { throw new Exception("需要审批的数据状态不一致,不能批量审核,请重新选择数据提交。"); } var processItemResultModelList = client.ProcessBatchLoadNextDetail(userPendingWorkList.Select(x => x.InstanceProcessID).ToArray(), vm); if (processItemResultModelList.doNum != 1) { throw new Exception("流程信息读取失败。", new Exception(processItemResultModelList.doMessage)); } foreach (var itempwi in userPendingWorkList) { if (itempwi.WorkTime == null) { List nextStepList = null; var processItemResultModel = processItemResultModelList.resultDataItemList.FirstOrDefault(x => x.ID == itempwi.InstanceProcessID); //获取流程明细 if (processItemResultModel == null) { throw new Exception("流程信息读取失败。"); } if (processItemResultModel.Tp == "start") { throw new Exception("当前流程并未提交,请先提交再进行审核。"); } if (processItemResultModel.Tp == "end") { throw new Exception("当前流程已经结束,无法审核。"); } //判断是否有指定下一步动作 if (actionID != null && actionID != Guid.Empty) nextStepList = processItemResultModel.NextSteps .Where(x => x.ActionID == actionID.ToString()).ToList(); //获取下一步用户 else nextStepList = processItemResultModel.NextSteps.ToList(); //获取下一步用户 //根据角色ID 获取用户ID foreach (var itemnext in nextStepList) { nextActionID = itemnext.ActionID.ToString(); approveMethodName = itemnext.ActionFunc; pid = itemnext.Pid; if (endStepList.Any(x => x.Pid == pid)) { nextWorkby = new List(); } else { //获取下一步的待办名单 nextWorkby = itemnext.PendingUserType.Split(',').ToList(); } if (nextWorkby.Count == 0 && !endStepList.Any(x => x.Pid == pid)) { throw new Exception("后续的流程环节并没有适合的审批人,无法提交。"); } } } } using (var scope = new TransactionScope(TransactionScopeOption.RequiresNew, new TimeSpan(1, 0, 0))) { if (!string.IsNullOrEmpty(approveMethodName)) { ReflectorHelper.RunMethod(approveMethodName, formIDList, userID); } //推送下一步流程 ResultModel rm = client.ProcessBatchMoveNext(userPendingWorkList.Select(x => x.InstanceProcessID).ToArray(), nextActionID , new ActionFunc[] { }, nextWorkby.Distinct().ToArray(), comment, user.LoginID, new string[] { }, vm); if (rm.doNum != 1) throw new Exception(rm.doMessage); else { IDictionary updateKeyValueList = new Dictionary(); var entityType = this.GetEntityType(tableName); if (entityType.GetProperty("ApprovalStatus") != null) { updateKeyValueList.Add("ApprovalStatus", pid.Value); } else if (entityType.GetProperty("RecordStatus") != null) { updateKeyValueList.Add("RecordStatus", pid); } foreach (var formID in formIDList) { this.SendMobileMQ(formID, nextWorkby.ToArray()); } UnitOfWork.BatchUpdate(tableName, updateKeyValueList, formIDList); scope.Complete(); return; } } } else { throw new Exception(pwrm.doMessage); } } catch (Exception ex) { throw new Exception(ex.Message); } } /// /// 工作流流程状态修改(通常用于取消申请、撤销等环节) /// /// 主表表名 /// 表单ID /// 状态:A为活动,D为结束、其他的自己定义 /// 描述改变内容 public void ModifyProcessState(string tableName, Guid formID, string status, string description) { try { string AppID = this.GetAppIDByTableName(tableName); //流程ID string instanceID = string.Empty; List list = new List(); //流程关联表 Sys_ProcessRelation processRelation = ProcessWorkfloconfigDAL.GetProcessRelationQueryable() .Where(x => x.TableName == tableName && x.formID == formID).FirstOrDefault(); if (processRelation != null) instanceID = processRelation.InstanceID; this.InitVerificationModel(); ResultModel rm = client.InstanceProcessChange(instanceID, status, description, vm); if (rm.doNum != 1) { throw new Exception(rm.doMessage); } } catch (Exception ex) { throw new Exception(ex.Message); } } /// /// 根据当前流程环节ID获取下一步审批动作 /// /// 主表表名 /// 表单ID /// 当前用户ID /// public List GetActionView(string tableName, Guid formID, Guid userID) { try { List listAction = new List(); string AppID = this.GetAppIDByTableName(tableName); //流程ID string instanceID = string.Empty; Sys_ProcessRelation processRelation = ProcessWorkfloconfigDAL.GetProcessRelationQueryable() .Where(x => x.formID == formID && x.TableName == tableName).FirstOrDefault(); if (processRelation != null) instanceID = processRelation.InstanceID; this.InitVerificationModel(); var user = UserServices.Value.GetUserByID(userID); var role = UserServices.Value.GetUserRoles(userID); PendingWorkResultModel pwrm = client.PendingWorkList("", "", AppID, instanceID, "", "", "A", "1", "100", vm); //读取待办 if (pwrm != null) { var userPendingWorkList = pwrm.resultDataList.Where(x => role.Any(w => w.RoleName == x.PendingUsers) || x.PendingUsers == user.LoginID).ToList(); if (userPendingWorkList.Count == 0) { throw new Exception("对不起,您没权限进行操作。"); } List listpwi = userPendingWorkList.Take(1).ToList(); foreach (var itempwi in listpwi) { if (itempwi.WorkTime == null) { var processItemResultModel = client.ProcessLoadNextDetail(itempwi.InstanceProcessID, vm); //获取动作 List listnext = processItemResultModel.resultDataItem.NextSteps.OrderBy(x => x.sort).ToList(); //获取下一步用户 foreach (var item in listnext) { ActionView action = new ActionView(); action.ActionID = new Guid(item.ActionID); action.ActionName = item.AName; action.PendingUser = item.PendingUserType; action.PendingUserName = item.PendingUser; action.StepName = item.PName; action.Sort = item.sort; action.Description = item.ADescription; listAction.Add(action); } } } } return listAction; } catch (Exception ex) { throw (new Exception(ex.Message)); } } /// /// 重载方法,根据当前流程环节ID获取全部审批动作 /// /// 主表表名 /// public List GetActionView(string tableName) { try { List statusLists = GetStatusList(tableName); List listAction = new List(); foreach (var statusList in statusLists) { foreach (var item in statusList.NextSteps) { ActionView action = new ActionView(); action.ActionID = new Guid(item.ActionID); action.ActionName = item.AName; action.PendingUser = item.PendingUserType; action.PendingUserName = item.PendingUser; action.StepName = item.PName; action.Pid = statusList.Pid; action.ToPid = item.Pid; action.Sort = item.sort; listAction.Add(action); } } return listAction; } catch (Exception ex) { throw (new Exception(ex.Message)); } } /// /// 查询开始流程环节的动作列表List /// /// 主表表名 /// public List GetStartActionView(string tableName) { return this.GetStartStatusList(tableName).FirstOrDefault().NextSteps.Select(x => new ActionView { ActionID = new Guid(x.ActionID), ActionName = x.AName, StepName = x.AName, Sort = x.sort, Description = x.ADescription }).ToList(); } /// /// 查询对应的审核历史信息列表List(WorkflowApproveHistoryView) /// /// 主表表名 /// 需要获取的记录主键ID /// public IList GetApproveHistoryViewList(string tableName, Guid formID) { try { string AppID = this.GetAppIDByTableName(tableName); //流程ID string InstanceID = string.Empty; List list = new List(); this.InitVerificationModel(); Sys_ProcessRelation processRelation = ProcessWorkfloconfigDAL.GetProcessRelationQueryable() .Where(x => x.formID == formID && x.TableName == tableName).FirstOrDefault(); if (processRelation != null) InstanceID = processRelation.InstanceID; if (!string.IsNullOrEmpty(InstanceID)) { ProcessDetailResultModel processDetailResultModel = client.LoadProcessDetails(AppID, InstanceID, vm); List listipm = processDetailResultModel.resultDataList .Where(x => !string.IsNullOrEmpty(x.WorkBy)) .OrderBy(x => x.CreateTime).ToList(); foreach (var lt in listipm) { string userNo = string.Empty; string username = string.Empty; if (lt.WorkBy != null) { var userlist = UserServices.Value.GetUserByLoginID(lt.WorkBy); if (userlist != null) { userNo = userlist.LoginID; username = userlist.Name; } } if (!list.Any(x => x.PID == lt.PID && x.ApproveUserName == username && x.PName == lt.PName && x.ActionName == lt.ActionName && x.ApproveTime == lt.WorkTime.ParseStrTo() && x.Comment == lt.Comment)) { WorkflowApproveHistoryView wahv = new WorkflowApproveHistoryView(); wahv.ApproveHistoryID = lt.ID.ParseStrTo(); wahv.PID = lt.PID; wahv.ApproveUserNo = userNo; wahv.ApproveUserName = username; wahv.PName = lt.PName; wahv.ActionName = lt.ActionName; wahv.ApproveTime = lt.WorkTime.ParseStrTo(); wahv.Comment = lt.Comment; list.Add(wahv); } } } return list; } catch (Exception ex) { throw new Exception(ex.Message); } } /// /// 获取待办列表 /// /// /// 主表表名 /// 查询视图,指定一个IQueryable决定如何获取表单记录 /// 待办用户ID /// 指定主键列 /// 页码 /// 每页行数 /// public IGridResultSet QueryByPendingJob(string tableName, IQueryable query, Guid userID, Expression> pkSelector, Expression> orderby, bool isDescending = false, int? pageIndex = null, int? pageSize = null) { try { string AppID = this.GetAppIDByTableName(tableName); //流程ID string InstanceID = string.Empty; var user = UserServices.Value.GetUserByID(userID); //获取提交人 var role = UserServices.Value.GetUserRoles(userID); this.InitVerificationModel(); var queryProessRelation = ProcessWorkfloconfigDAL.processRelationRepository.GetList(x => x.TableName == tableName); PendingWorkResultModel pwrm = client.PendingWorkList("", "", AppID, "", "", "", "A", ((pageIndex ?? 0) + 1).ToString(), "9999", vm); //访问接口成功 if (pwrm.doNum == 1) { var instanceIDList = pwrm.resultDataList.Where(x => role.Any(w => w.RoleName == x.PendingUsers) || x.PendingUsers == user.LoginID) .Select(x => x.InstanceID).ToList(); var queryprrm = queryProessRelation.Where(x => instanceIDList.Contains(x.InstanceID)); var result = query.Join(queryprrm, pkSelector, (x => (x.formID ?? Guid.Empty)), ((x, y) => x)); if (isDescending) { result = result.OrderByDescending(orderby); } else { result = result.OrderBy(orderby); } return result.ToGridResultSet(pageIndex, pageSize); } else { return new GridResultSet { rows = new List(), total = 0 }; } } catch (Exception ex) { throw new Exception(ex.Message); } } /// /// 推送下一个处理人标识 /// /// /// private void SendMobileMQ(Guid formID, string[] nextWorkbyArray) { if (this.MessageFunc == null) { return; } var userIDList = RoleDAL.RoleRepository.GetList(x => nextWorkbyArray.Contains(x.RoleName)) .SelectMany(x => x.Sys_User).Where(x => x.CF_Student.UserID != null).Distinct() .Union( RoleDAL.UserRepository.GetList(x => nextWorkbyArray.Contains(x.LoginID)) .Where(x => x.CF_Student.UserID != null)) .Select(x => x.UserID).ToList(); var message = this.MessageFunc.Invoke(formID); MobileMQ.SendMessage(userIDList.Select(x => x.ToString()).ToArray(), message); } } }