/// <summary> /// 执行 流转任务 /// </summary> /// <param name="taskDto">当前任务Dto</param> /// <returns>业务操作结果</returns> public OperationResult Execute(FlowExecuteDto taskDto) { OperationResult result = new OperationResult(OperationResultType.NoChanged); switch (taskDto.ExecuteType) { case ExecuteType.Submit: result = ExecuteSubmit(taskDto); break; case ExecuteType.Back: result = ExecuteBack(taskDto); break; case ExecuteType.CallBack: result = ExecuteCallBack(taskDto); break; case ExecuteType.Completed: result = ExecuteCompleted(taskDto); break; default: break; } return result; }
/// <summary> /// 提交任务 /// </summary> /// <param name="task">当前任务Dto</param> /// <returns>业务操作结果</returns> protected OperationResult ExecuteSubmit(FlowExecuteDto task) { OperationResult re = new OperationResult(OperationResultType.Success, "处理成功!"); WorkFlowTask currentTask = FlowTaskRepository.Entities.SingleOrDefault(c => c.Id == task.TaskId); if (currentTask == null) { return CreateFirstTask(task); } var currentStep = FlowStepRepository.Entities.Where(c => c.FlowDesignId == task.FlowId && c.StepId == currentTask.StepId). Select(m => new { m.StepType, m.CountersignStrategy, m.CountersignPer, m.BackType }).SingleOrDefault(); #region 新增下级任务 if (currentStep != null) { if (currentTask.Status == 1 || currentTask.Status == 2) { FlowTaskRepository.UnitOfWork.TransactionEnabled = true; //事务处理 bool createNextTask = true; switch (currentStep.CountersignStrategy) { case 0: //所有步骤同意 int count = GetSiblingTask(currentTask.FlowItemId, currentTask.StepId).Where(c => c.Status < 10).Count(); if (count > 1) { createNextTask = false; } break; case 1: //一人同意即可 CompletedOtherSiblingTask(currentTask, 30, "", "他人已处理此任务!"); break; case 2: //比例同意即可 decimal percentage = currentStep.CountersignPer <= 0 ? 100 : currentStep.CountersignPer; IQueryable<WorkFlowTask> TaskQueryable = GetSiblingTask(currentTask.FlowItemId, currentTask.StepId).Where(c => c.PrevId == currentTask.PrevId); if (Math.Round((((decimal)(TaskQueryable.Where(p => p.Status == 10).Count() + 1) / (decimal)TaskQueryable.Where(p => p.Status <= 10).Count()) * 100), 2, MidpointRounding.AwayFromZero) < percentage) { createNextTask = false; } else { CompletedOtherSiblingTask(currentTask, 30, "", "他人已处理此任务!"); } break; } if (createNextTask) { CreatedNextTask(task, currentTask.StepId); } CompletedTask(currentTask, 10, task.Comment); FlowTaskRepository.UnitOfWork.SaveChanges(); //是否超期处理 } else { return new OperationResult(OperationResultType.ValidError, "当前步骤已经由他人处理!"); } } else { re = new OperationResult(OperationResultType.ValidError, "找不到当前步骤!"); } #endregion return re; }
/// <summary> /// 办结任务 /// </summary> /// <param name="task">当前任务Dto</param> /// <returns>业务操作结果</returns> protected OperationResult ExecuteCompleted(FlowExecuteDto task) { OperationResult re = new OperationResult(OperationResultType.NoChanged, "办结未做处理!"); var currentTask = FlowTaskRepository.Entities.SingleOrDefault(c => c.Id == task.TaskId); if (currentTask != null) { var currentItem = FlowItemRepository.Entities.Single(c => c.Id == currentTask.FlowItemId); currentItem.Status = 1; currentItem.CompletedTime = DateTime.Now; currentTask.Comment = task.Comment; currentTask.TaskNote = task.Note; currentTask.Status = 10; currentTask.CompletedTime = DateTime.Now; FlowTaskRepository.UnitOfWork.TransactionEnabled = true; //事务处理 if (currentTask.IsArchive) FlowArchiveRepository.Insert(new WorkFlowArchive() { Id = CombHelper.NewComb(), FlowItemId = currentTask.FlowItemId, CreatorUserName = task.SenderName }); FlowTaskRepository.Update(currentTask); FlowItemRepository.Update(currentItem); FlowTaskRepository.UnitOfWork.SaveChanges(); re = new OperationResult(OperationResultType.Success, "成功办结!"); } return re; }
/// <summary> /// 撤销提交 /// </summary> /// <param name="task">当前任务Dto</param> /// <returns>业务操作结果</returns> protected OperationResult ExecuteCallBack(FlowExecuteDto task) { OperationResult result = IsCallBack(task.TaskId); if (result.ResultType != OperationResultType.Success) return new OperationResult(OperationResultType.ValidError, "任务在下级已经有处理,不能再撤销!"); FlowTaskRepository.UnitOfWork.TransactionEnabled = true; //事务处理 var nextTask = (List<Guid>)result.Data; foreach (var item in nextTask) { FlowTaskRepository.Delete(item); } //标记本任务为待处理 var currentTask = FlowTaskRepository.Entities.Where(c => c.Id == task.TaskId).Single(); CompletedTask(currentTask, 1, "", ""); var currentSiblingTask = GetSiblingTask(currentTask.FlowItemId, currentTask.StepId).Where(c => c.Status == 30).ToList(); foreach (var item in currentSiblingTask) { CompletedTask(item, 1, "", "他人已撤销的任务"); } FlowTaskRepository.UnitOfWork.SaveChanges(); result = new OperationResult(OperationResultType.Success, "撤销成功!"); return result; }
/// <summary> /// 退回任务 /// </summary> /// <param name="task">当前任务Dto</param> /// <returns>业务操作结果</returns> protected OperationResult ExecuteBack(FlowExecuteDto task) { OperationResult re = new OperationResult(OperationResultType.NoChanged, "退回未处理!"); var currentTask = FlowTaskRepository.Entities.Single(c => c.Id == task.TaskId); if (currentTask != null) { if (currentTask.Status != 1 && currentTask.Status != 2) { return new OperationResult(OperationResultType.ValidError, "任务已经处理,不能进行退回!"); } var currentStep = FlowStepRepository.Entities.Where(c => c.FlowDesignId == task.FlowId && c.StepId == currentTask.StepId).Select(m => new { m.StepType, m.BackType, m.SpecifiedBackStep }).SingleOrDefault(); if (currentStep.StepType == 0) { return new OperationResult(OperationResultType.ValidError, "流程的第一个步骤不能退回!"); } if (currentStep.BackType == 0) { return new OperationResult(OperationResultType.ValidError, "当前步骤为不可退回步骤!"); } #region 退回处理 List<WorkFlowTask> backTasks = new List<WorkFlowTask>(); if (currentStep.BackType == 1) //退回到上一步 { backTasks.AddRange(FlowTaskRepository.Entities.Where(c => c.Id == currentTask.PrevId).ToList()); } else if (currentStep.BackType == 2) //退回到第一步 { var firstTack = FlowTaskRepository.Entities.Where(c => c.PrevStepId == -1 && c.FlowItemId == currentTask.FlowItemId).OrderByDescending(c => c.CreatedTime).ToList()[0]; firstTack.PrevId = task.TaskId; //上一个任务Id backTasks.Add(firstTack); } else //退回到指定步 { var backStep = FlowStepRepository.Entities.Where(c => c.FlowDesignId == task.FlowId && c.StepName == currentStep.SpecifiedBackStep).Select(c => new { c.StepId }).Single(); if (backStep == null) { return new OperationResult(OperationResultType.ValidError, "当前流程步骤配置有误,不能退回!"); } var backtask = GetSiblingTask(currentTask.FlowItemId, backStep.StepId).OrderByDescending(c=>c.SenderId).ThenByDescending(c=>c.CreatedTime).ToList(); string senderId = ""; foreach (var item in backtask) //移除同一步骤中相同的发送人的以前的任务 { if (senderId == item.SenderId) backtask.Remove(item); senderId = item.SenderId; } backTasks.AddRange(backtask); } FlowTaskRepository.UnitOfWork.TransactionEnabled = true; //事务处理 foreach (var item in backTasks) { WorkFlowTask newTask = new WorkFlowTask(); newTask.Id = CombHelper.NewComb(); newTask.PrevId = item.PrevId; newTask.FlowItemId = item.FlowItemId; newTask.PrevStepId = item.PrevStepId; newTask.StepId = item.StepId; newTask.StepName = item.StepName; newTask.SenderId = task.SenderId; newTask.SenderName = task.SenderName; newTask.ReceiverId = item.ReceiverId; newTask.ReceiverName = item.ReceiverName; newTask.OpenedTime = null; newTask.CompletedTime = null; newTask.IsComment = item.IsComment; newTask.IsSeal = item.IsSeal; newTask.IsArchive = item.IsArchive; newTask.TaskNote = "退回的任务"; newTask.StepDay = item.StepDay; newTask.DelayDay = 0; newTask.Status = 1; FlowTaskRepository.Insert(newTask); } CompletedTask(currentTask, 20, task.Comment); CompletedOtherSiblingTask(currentTask, 40, "", "他人已退回"); FlowTaskRepository.UnitOfWork.SaveChanges(); re = new OperationResult(OperationResultType.Success, "退回成功!"); #endregion } else { re = new OperationResult(OperationResultType.ValidError, "您要退回的任务不存在!"); } return re; }
/// <summary> /// 创建第一个任务 /// </summary> /// <param name="task">当前任务Dto</param> /// <returns>业务操作结果</returns> protected OperationResult CreateFirstTask(FlowExecuteDto task) { OperationResult re = CheckFlow(task.FlowId, task.EntityId); if (re.ResultType == OperationResultType.Success) { var firstStep = FlowStepRepository.Entities.Where(c => c.FlowDesignId == task.FlowId && c.StepType == 0).Select(m => new { m.StepId,m.StepName}).SingleOrDefault(); //起始步骤 var flowItemId = CombHelper.NewComb(); var id = CombHelper.NewComb(); WorkFlowTask taskModel = new WorkFlowTask() { Id = id, FlowItemId = flowItemId, PrevStepId = -1, StepId = firstStep.StepId, StepName = firstStep.StepName, SenderId = task.SenderId, SenderName=task.SenderName, ReceiverId = task.SenderId, ReceiverName = task.SenderName, CreatedTime=DateTime.Now, IsComment = false, IsSeal = false, IsArchive=false, Status = 1 }; var model = new WorkFlowItem() { Id = flowItemId, FlowDesignId = task.FlowId, EntityId = task.EntityId, EntityName = task.Title, CreatorUserId = task.SenderId, CreatorUserName=task.SenderName, //StepDay,DelayDay,HandleDay 用触发器保证数据正确性 Status = 0 }; model.Tasks.Add(taskModel); FlowItemRepository.Insert(model); re = new OperationResult(OperationResultType.Success, "流程启动成功!"); //re.Data = taskModel; } return re; }
/// <summary> /// 根据指定流程 选择的流转步骤创建任务 /// </summary> /// <param name="task">当前任务Dto</param> /// <param name="stepId">当前步骤Id</param> protected void CreatedNextTask(FlowExecuteDto task, short stepId) { foreach (var step in task.Steps) { //流转的下个步骤 var nextStep = FlowStepRepository.Entities.Single(p => p.StepId == step.Key && p.FlowDesignId==task.FlowId); foreach (var user in step.Value) { var uid=user.Key; var uname=user.Value; GetReceiver(task.FlowId,ref uid,ref uname); WorkFlowTask taskModel = new WorkFlowTask() { Id = CombHelper.NewComb(), PrevId = task.TaskId, FlowItemId = task.ItemId, PrevStepId = stepId, StepId = nextStep.StepId, StepName = nextStep.StepName, SenderId = task.SenderId, SenderName = task.SenderName, ReceiverId = uid, ReceiverName = uname, IsComment = nextStep.CountersignType == 0 ? false : true, IsSeal = nextStep.CountersignType == 2 ? true : false, IsArchive = nextStep.IsArchives, StepDay = nextStep.SpecifiedDay, Status = 1 }; FlowTaskRepository.Insert(taskModel); } } }