Exemple #1
0
        public async Task <ValidateResult> ValidateUser(WorkflowResource resource, string actionName, string userKey)
        {
            ValidateResult result = new ValidateResult()
            {
                Result = false
            };
            List <WorkflowStep> steps = new List <WorkflowStep>();
            //根据资源的当前状态和actionName获取所有步骤
            await _workflowStepStore.QueryByResource(resource.Type, resource.Key, resource.ID, actionName, resource.Status,
                                                     async (step) =>
            {
                steps.Add(step);
                await Task.FromResult(0);
            }
                                                     );

            StringBuilder strDescript = new StringBuilder();

            if (steps.Count > 0)
            {
                foreach (var item in steps)
                {
                    var itemResult = await item.ValidateUser(userKey);

                    if (itemResult.Result)
                    {
                        result.Result      = true;
                        result.Description = null;
                        break;
                    }
                    else
                    {
                        strDescript.Append(itemResult.Description);
                        strDescript.Append("\n\r");
                    }
                }
            }
            else
            {
                var fragment = new TextFragment()
                {
                    Code = TextCodes.NotFoundWorkflowStepForAudit,
                    DefaultFormatting = "找不到工作流中需要审核的步骤;查询条件为资源类型:{0},资源关键字:{1},资源Id:{2},动作名称:{3},资源状态:{4}",
                    ReplaceParameters = new List <object>()
                    {
                        resource.Type, resource.Key, resource.ID, actionName, resource.Status
                    }
                };

                throw new UtilityException((int)Errors.NotFoundWorkflowStepForAudit
                                           , fragment);
            }

            if (!result.Result)
            {
                result.Description = strDescript.ToString();
            }

            return(result);
        }
        /// <summary>
        /// 获取指定的工作流资源
        /// 如果存在,则直接返回
        /// 如果不存在,则新建再返回
        /// </summary>
        /// <param name="type"></param>
        /// <param name="key"></param>
        /// <param name="initStatus"></param>
        /// <returns></returns>
        public static async Task <WorkflowResource> GetWorkflowResource(IWorkflowResourceRepository workflowResourceRepository, string type, string key, int initStatus)
        {
            var resource = await workflowResourceRepository.QueryByKey(type, key);

            if (resource == null)
            {
                resource = new WorkflowResource()
                {
                    Type       = type,
                    Key        = key,
                    InitStatus = initStatus,
                    Status     = initStatus
                };

                try
                {
                    await resource.Add();
                }
                catch (UtilityException ex)
                {
                    if (ex.Code == (int)Errors.ExistWorkflowResourceKey)
                    {
                        resource = await workflowResourceRepository.QueryByKey(type, key);
                    }
                    else
                    {
                        throw;
                    }
                }
            }

            return(resource);
        }
Exemple #3
0
        public async Task AddStep(WorkflowResource resource, WorkflowStep step)
        {
            //判断当前步骤是否存在,若存在,则不重复创建
            var isExistStep = await _workflowStepStore.IsExistStepByKey(resource.Type, resource.Key, resource.ID, resource.Status, step.ActionName, step.UserType, step.UserKey);

            if (!isExistStep)
            {
                //自动关联资源
                step.ResourceID = resource.ID;
                //自动补上当前资源状态
                step.Status = resource.Status;

                //如果步骤的批次号为null,则需要查找该资源下status=step.Status,createtime最晚的步骤
                //如果找到,则将找到的步骤的批次号赋值给step的批次号
                //如果没找到,则生成一个随机批次号
                var latestStep = await _workflowStepStore.QueryLatestByResource(resource.Type, resource.Key, resource.ID, step.Status);

                if (latestStep != null)
                {
                    step.SerialNo = latestStep.SerialNo;
                }
                else
                {
                    step.SerialNo = Guid.NewGuid().ToString();
                }


                await _workflowStepStore.Add(resource.Type, resource.Key, step);
            }
        }
Exemple #4
0
        public async Task <bool> IsStepComplete(WorkflowResource resource, string actionName, int status)
        {
            bool result = true;

            await _workflowStepStore.QueryByResource(resource.Type, resource.Key, resource.ID, actionName, status, async (step) =>
            {
                if (!step.Complete)
                {
                    result = false;
                }

                await Task.FromResult(0);
            });

            return(result);
        }
Exemple #5
0
        public async Task AddUserAction(WorkflowResource resource, string actionName, int status, string userKey, int result)
        {
            await using (var scope = new DBTransactionScope(System.Transactions.TransactionScopeOption.Required, new System.Transactions.TransactionOptions()
            {
                IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted, Timeout = new TimeSpan(0, 1, 0)
            }))
            {
                List <WorkflowStep> stepList = new List <WorkflowStep>();
                //找到该资源下面的所有名称为actionName,状态为status的步骤
                await _workflowStepStore.QueryByResource(resource.Type, resource.Key, resource.ID, actionName, status,
                                                         async (step) =>
                {
                    if ((await step.ValidateUser(userKey)).Result)
                    {
                        stepList.Add(step);
                    }
                }
                                                         );

                //对所有符合条件的step增加用户动作
                foreach (var item in stepList)
                {
                    WorkflowStepUserAction action = new WorkflowStepUserAction()
                    {
                        StepID  = item.ID,
                        Step    = item,
                        Result  = result,
                        UserKey = userKey
                    };

                    await _workflowStepUserActionStore.Add(resource.Type, resource.Key, action);

                    //判断是否该步骤下面的用户动作数量是否>=该步骤的用户数量,如果是,则需要修改该步骤的完成状态

                    var actionCount = await _workflowStepUserActionStore.QueryCountByStep(resource.Type, resource.Key, item.ID);

                    if (actionCount >= item.UserCount)
                    {
                        await _workflowStepStore.UpdateCompleteStatus(resource.Type, resource.Key, resource.ID, item.ID, true);
                    }
                }


                scope.Complete();
            }
        }
Exemple #6
0
 public async Task GetUserAction(WorkflowResource resource, Func <WorkflowStepUserAction, Task> callback)
 {
     await _workflowStepUserActionStore.QueryByResource(resource.Type, resource.Key, resource.ID, callback);
 }
Exemple #7
0
 public async Task GetStepByCurrentStatus(WorkflowResource resource, Func <WorkflowStep, Task> callback)
 {
     await _workflowStepStore.QueryByResource(resource.Type, resource.Key, resource.ID, resource.Status, callback);
 }
Exemple #8
0
 public async Task UpdateStepCompleteStatus(WorkflowResource resource, string actionName, int status, bool isComplete, Func <WorkflowStepUserAction, Task> returnCallback)
 {
     await _workflowStepStore.UpdateCompleteStatus(resource.Type, resource.Key, resource.ID, actionName, status, isComplete);
 }
Exemple #9
0
        public async Task UpdateStatus(WorkflowResource resource, int status)
        {
            await _workflowResourceStore.UpdateStatus(resource.Type, resource.Key, resource.ID, status);

            resource.Status = status;
        }
Exemple #10
0
        public async Task ReturnToStatus(WorkflowResource resource, int status, Func <WorkflowStepUserAction, Task> returnCallback)
        {
            using (var scope = new DBTransactionScope(System.Transactions.TransactionScopeOption.Required, new System.Transactions.TransactionOptions()
            {
                IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted, Timeout = new TimeSpan(0, 1, 0)
            }))
            {
                ////找到资源下指定状态,创建时间最晚的步骤
                var returnStep = await _workflowStepStore.QueryByStatus(resource.Type, resource.Key, resource.ID, status);

                if (returnStep != null)
                {
                    //获取创建时间大于等于returnStep的CreateTime and SerialNo不等于returnStep的SerialNo的所有步骤
                    //这些步骤需要被移除
                    List <WorkflowStep> removeSteps = new List <WorkflowStep>();
                    await _workflowStepStore.QueryByCreateTime(resource.Type, resource.Key, resource.ID, returnStep.SerialNo, returnStep.CreateTime,
                                                               async (step) =>
                    {
                        removeSteps.Add(step);
                        await Task.FromResult(0);
                    }
                                                               );

                    foreach (var item in removeSteps)
                    {
                        //每一个需要移除的步骤,都需要获取关联的用户动作,为这些用户动作执行回调
                        await _workflowStepUserActionStore.QueryByStep(resource.Type, resource.Key, item.ID, returnCallback);

                        //删除步骤
                        await _workflowStepStore.Delete(resource.Type, resource.Key, resource.ID, item.ID);
                    }

                    //找到所有SerialNo=preStep.SerialNo的步骤,删除它们下面的所有用户动作,
                    //对这些动作执行回调
                    //再把它们的complete全部改成false
                    await _workflowStepStore.QueryByResource(resource.Type, resource.Key, resource.ID, returnStep.SerialNo,
                                                             async (step) =>
                    {
                        await _workflowStepUserActionStore.QueryByStep(resource.Type, resource.Key, step.ID,
                                                                       async(action) =>
                        {
                            await returnCallback(action);
                        }
                                                                       );
                        await _workflowStepUserActionStore.Delete(resource.Type, resource.Key, step.ID);
                        await _workflowStepStore.UpdateCompleteStatus(resource.Type, resource.Key, resource.ID, step.ID, false);
                    }
                                                             );

                    //修改资源状态为preStep的状态
                    await _workflowResourceStore.UpdateStatus(resource.Type, resource.Key, resource.ID, returnStep.Status);
                }
                else
                {
                    //需要退回到最初状态,获取资源下所有步骤,为步骤下面每个用户动作调用回调,
                    //删除所有的步骤,修改资源状态为初始状态
                    List <WorkflowStep> removeSteps = new List <WorkflowStep>();
                    await _workflowStepStore.QueryByResource(resource.Type, resource.Key, resource.ID, async (step) =>
                    {
                        removeSteps.Add(step);
                        await Task.FromResult(0);
                    });

                    foreach (var item in removeSteps)
                    {
                        //每一个需要移除的步骤,都需要获取关联的用户动作,为这些用户动作执行回调
                        await _workflowStepUserActionStore.QueryByStep(resource.Type, resource.Key, item.ID, returnCallback);

                        //删除步骤
                        await _workflowStepStore.Delete(resource.Type, resource.Key, resource.ID, item.ID);
                    }

                    //修改资源状态为preStep的状态
                    await _workflowResourceStore.UpdateStatus(resource.Type, resource.Key, resource.ID, resource.InitStatus);
                }

                scope.Complete();
            }
        }
Exemple #11
0
 public async Task GetUserAction(WorkflowResource resource, string actionName, int status, Func <WorkflowStepUserAction, Task> callback)
 {
     await _workflowStepUserActionStore.QueryByResource(resource.Type, resource.Key, resource.ID, actionName, status, callback);
 }
Exemple #12
0
 public async Task DeleteStep(WorkflowResource resource, int status, string serilaNo, string excludeActionName)
 {
     await _workflowStepStore.Delete(resource.Type, resource.Key, resource.ID, status, serilaNo, excludeActionName);
 }
Exemple #13
0
 public async Task DeleteStep(WorkflowResource resource, string actionName, int status)
 {
     await _workflowStepStore.Delete(resource.Type, resource.Key, resource.ID, actionName, status);
 }
Exemple #14
0
 public async Task Delete(WorkflowResource resource)
 {
     await _workflowResourceStore.Delete(resource.Type, resource.Key, resource.ID);
 }
Exemple #15
0
 public async Task Add(WorkflowResource resource)
 {
     await _workflowResourceStore.Add(resource);
 }