Exemple #1
0
        private static WorkflowModel BuildWorkflow(Workflow workflow, List <Activity> activities, List <Link> links,
                                                   List <ActivityAuth> authes, List <Approval> approvals, List <Task> tasks)
        {
            if (workflow == null)
            {
                throw new Exception("workflow不能为null");
            }
            List <ActivityModel> activityInstanceList = new List <ActivityModel>();
            List <LinkModel>     linkModelList        = new List <LinkModel>();
            var           activityList = activities.FindAll(t => t.WorkflowID.Equals(workflow.ID));
            var           linkList     = links.FindAll(t => t.WorkflowID.Equals(workflow.ID));
            var           authList     = authes.FindAll(t => t.WorkflowID.Equals(workflow.ID));
            var           approvalList = approvals.FindAll(t => t.WorkflowID.Equals(workflow.ID));
            var           taskList     = tasks.FindAll(t => t.WorkflowID.Equals(workflow.ID));
            WorkflowModel model        = new WorkflowModel
            {
                value    = workflow,
                Approval = approvalList,
            };
            bool foundRoot = false;
            bool foundTail = false;

            foreach (var link in linkList)
            {
                linkModelList.Add(new LinkModel
                {
                    Value = link,
                });
            }
            foreach (var activity in activityList)
            {
                //pre link
                var preLink = linkModelList.FindAll(t => t.Value != null && t.Value.ToAcivityID == activity.ID);
                //next link
                var nextLink = linkModelList.FindAll(t => t.Value != null && t.Value.FromActivityID == activity.ID);

                var activityInstance = new ActivityModel
                {
                    Value         = activity,
                    Approvals     = model.Approval.FindAll(t => t.ActivityID == activity.ID),
                    PreLinks      = preLink,
                    NextLinks     = nextLink,
                    Tasks         = tasks.FindAll(t => t.ActivityID == activity.ID),
                    OwnerWorkflow = model,
                };

                //处理权限
                var activityauth = authList.FindAll(t => t.ActivityID == activity.ID);
                activityInstance.Auth.AddRange(activityauth);
                //给审批排序
                activityInstance.Approvals.Sort((l, r) =>
                {
                    if (l.CreateTime > l.CreateTime)
                    {
                        return(1);
                    }
                    else
                    {
                        return(0);
                    }
                });
                if (!foundRoot && preLink.Count == 0)
                {
                    model.Root = activityInstance;
                    foundRoot  = true;
                }
                if (!foundTail && nextLink.Count == 0)
                {
                    model.Tail = activityInstance;
                    foundTail  = true;
                }
                activityInstanceList.Add(activityInstance);
            }
            return(model);
        }
        public WorkflowModel StartNew(string creator, string processid, IWorkflowAuthority iauth)
        {
            var             mapper      = MapperHelper.GetMapper();
            WorkflowDao     workflowdao = new WorkflowDao(mapper);
            ActivityDao     activitydao = new ActivityDao(mapper);
            LinkDao         linkdao     = new LinkDao(mapper);
            ActivityAuthDao aadd        = new ActivityAuthDao(mapper);
            TaskDao         taskdao     = new TaskDao(mapper);
            WorkflowModel   model       = null;
            Workflow        wf          = this.value.ConvertTo <Workflow>();

            wf.ID      = null;
            wf.Creator = creator;
            wf.Status  = (int)WorkflowProcessStatus.Started;
            wf.WorkflowDefinitionID = this.value.ID;
            wf.ProcessID            = processid;
            workflowdao.Add(wf);

            var             activites        = this.ActivityDefinitionList;
            var             links            = this.LinkDefinitionList;
            List <Activity> activityEntities = new List <Activity>();

            foreach (var a in activites)
            {
                Activity activity = a.Value.ConvertTo <Activity>();
                activity.Creator              = creator;
                activity.ID                   = null;
                activity.WorkflowID           = wf.ID;
                activity.ActivityDefinitionID = a.Value.ID;
                activity.Title                = a.Value.Title;
                activity.Status               = (int)ActivityProcessStatus.Started;

                List <ActivityAuth> authList = new List <ActivityAuth>();

                //如果是开始节点,就设置为已处理
                if (this.Root.Equals(a))
                {
                    activity.Status      = (int)ActivityProcessStatus.Processed;
                    activity.ProcessTime = DateTime.Now;
                }
                if (this.Root.Children.Count > 0 && this.Root.Children[0].Equals(a))
                {
                    activity.Status = (int)ActivityProcessStatus.Processing;
                }
                activitydao.Add(activity);
                activityEntities.Add(activity);

                //权限处理
                foreach (var ad in a.AuthDefinition)
                {
                    ActivityAuth auth = ad.ConvertTo <ActivityAuth>();
                    auth.Creator    = creator;
                    auth.WorkflowID = wf.ID;
                    auth.ActivityID = activity.ID;
                    auth.ID         = null;
                    auth.ActivityAuthDefinitionID = ad.ID;
                    aadd.Add(auth);
                    authList.Add(auth);
                }

                //如果是第二节点,就设置成正在处理
                if (this.Root.Children.Count > 0 && this.Root.Children[0].Equals(a))
                {
                    ActivityModel activitymodel = new ActivityModel
                    {
                        Value = activity,
                    };
                    var idlist   = iauth.GetUserIDList(authList);
                    var tasklist = activitymodel.GetTask(creator, idlist);
                    foreach (var task in tasklist)
                    {
                        taskdao.Add(task);
                    }
                }
            }

            foreach (var l in links)
            {
                Link link = l.Value.ConvertTo <Link>();
                link.Creator          = creator;
                link.WorkflowID       = wf.ID;
                link.ID               = null;
                link.LinkDefinitionID = l.Value.ID;
                var fromactivity = activityEntities.Find(t => t.ActivityDefinitionID == l.Value.FromActivityDefinitionID);
                var totactivity  = activityEntities.Find(t => t.ActivityDefinitionID == l.Value.ToActivityDefinitionID);
                if (fromactivity != null)
                {
                    link.FromActivityID = fromactivity.ID;
                }
                if (totactivity != null)
                {
                    link.ToAcivityID = totactivity.ID;
                }
                linkdao.Add(link);
            }

            model = WorkflowModel.Load(wf.ID);
            return(model);
        }
        public void Process(ActivityModel activity, Approval approval, string processor, IWorkflowAuthority auth)
        {
            //已经处理过就不能再处理
            if (activity.Value.Status == (int)ActivityProcessStatus.Processed)
            {
                return;
            }
            //MonitorCache.GetInstance().PushMessage(new CacheMessage { Message = "activityid:" + activity.Value.ID + " next activity status:" + activity.Value.Status.ToString() }, CacheEnum.FormMonitor);
            ISqlMapper  mapper      = MapperHelper.GetMapper();
            ActivityDao activitydao = new ActivityDao(mapper);
            TaskDao     taskdao     = new TaskDao(mapper);

            //设置当前活动点状态
            activity.Value.Status      = (int)ActivityProcessStatus.Processed;
            activity.Value.ProcessTime = DateTime.Now;
            activity.Value.LastUpdator = processor;
            activitydao.Update(new ActivityUpdateForm
            {
                Entity = new Activity {
                    Status = activity.Value.Status, ProcessTime = activity.Value.ProcessTime, LastUpdator = activity.Value.LastUpdator
                },
                ActivityQueryForm = new ActivityQueryForm {
                    ID = activity.Value.ID
                }
            });
            var task = activity.GetUserProcessingTask(processor);

            if (task == null)
            {
                throw new Exception("环节中没有你的任务,无法进行审批操作");
            }
            task.Status      = (int)TaskProcessStatus.Processed;
            task.ProcessTime = DateTime.Now;
            task.LastUpdator = processor;
            //处理任务
            taskdao.Update(new TaskUpdateForm
            {
                Entity = new Task {
                    ProcessTime = task.ProcessTime, Status = task.Status, LastUpdator = task.LastUpdator
                },
                TaskQueryForm = new TaskQueryForm {
                    ID = task.ID
                },
            });
            //设置下个活动点的状态
            if (activity.Children.Count > 0)
            {
                foreach (var next in activity.Children)
                {
                    string nextactivityid    = next.Value.ID;
                    var    nextActivityModel = next as ActivityModel;
                    nextActivityModel.Value.Status      = (int)ActivityProcessStatus.Processing;
                    nextActivityModel.Value.LastUpdator = processor;
                    activitydao.Update(new ActivityUpdateForm
                    {
                        Entity = new Activity {
                            Status = nextActivityModel.Value.Status, LastUpdator = nextActivityModel.Value.LastUpdator
                        },
                        ActivityQueryForm = new ActivityQueryForm {
                            ID = nextactivityid
                        },
                    });

                    List <string> useridList = auth.GetUserIDList(nextActivityModel.Auth);
                    //新增下个活动点的任务
                    var tasklist = nextActivityModel.GetTask(processor, useridList);
                    foreach (var t in tasklist)
                    {
                        nextActivityModel.Tasks.Add(t);
                        taskdao.Add(t);
                    }
                }
            }
        }