Пример #1
0
        /// <summary>
        /// 处理驳回方式的按钮显示
        /// </summary>
        private void CheckRejectButton()
        {
            //找到当前活动驳回的连接线
            SysTransition t = this.DataHelper.Set <SysTransition>()
                              .Where(p => p.PreActivityId == this.ActivityId && p.Direction == (int)FlowStepDirection.False).FirstOrDefault();

            if (t == null) //不存在,表示运行时用户指定驳回下一活动
            {
                var source = from a in this.DataHelper.Set <SysActivity>()
                             where a.ProcessId == this.ProcessId &&
                             ((a.ActivityType == (int)ActivityType.Approve && a.DisplayOrder < this.ActivityDispalyOrder) ||
                              (a.ActivityType == (int)ActivityType.End && a.ActivityName == "驳回"))
                             orderby a.DisplayOrder
                             select new
                {
                    a.DisplayOrder,
                    a.ActivityId,
                    a.ActivityName,
                    ButtonName = a.ActivityType == (int)ActivityType.End ? "驳回(结束流程)" : string.Format("驳回到({0})", a.ActivityName),
                };

                this.rReject.DataSource = source;
                this.rReject.DataBind();

                this.rReject.Visible   = true;
                this.btnReject.Visible = false;
            }
            else
            {
                this.rReject.Visible   = false;
                this.btnReject.Visible = true;
            }
        }
        private void CompleteNormalActivity(Queue <WorkflowMessage> queue, SysProcess process, SysActivity activity)
        {
            if (activity.ExecType == 0)
            {
                new ActivityRemindHandler(base.PICacheFactory, base.PI, this.AI, ActivityRemindUseTimeType.ActivityEnd, null, null).Execute();
            }
            List <SysTransition> trans = process.Transitions.Where <SysTransition>(delegate(SysTransition t) {
                long?preActivityId = t.PreActivityId;
                long activityId    = activity.ActivityId;
                return((preActivityId.GetValueOrDefault() == activityId) && preActivityId.HasValue);
            }).ToList <SysTransition>();

            if (trans.Count <= 0)
            {
                throw new ApplicationException("没有后续的转换条件");
            }
            SysTransition nextTransition = this.GetNextTransition(trans);

            if (nextTransition == null)
            {
                throw new ApplicationException("所有后续连接线均不满足条件,流程无法继续");
            }
            SysActivityInstance ai   = this.CreatePostAcitivtyInstance(base.PI, nextTransition);
            WorkflowMessage     item = base.Engine.NewActivityInstanceCreatedMessage(ai);

            queue.Enqueue(item);
        }
 private static void LoadTransition(SysTransition t, SysProcess p)
 {
     t.Process      = p;
     t.PreActivity  = p.Activities.FirstOrDefault <SysActivity>(i => i.ActivityId == t.PreActivityId);
     t.PostActivity = p.Activities.FirstOrDefault <SysActivity>(i => i.ActivityId == t.PostActivityId);
     t.Expression   = p.Expressions.FirstOrDefault <SysExpression>(i => i.ExpressionId == t.ExpressionId);
     t.Expressions  = p.Expressions.Where <SysExpression>(delegate(SysExpression i) {
         long?transitionId = i.TransitionId;
         long num          = t.TransitionId;
         return((transitionId.GetValueOrDefault() == num) && transitionId.HasValue);
     }).ToList <SysExpression>();
 }
        private void CreateTransitionInstance(SysActivityInstance postAi, SysTransition tran)
        {
            SysTransitionInstance ti = new SysTransitionInstance {
                ProcessInstanceId      = new int?(base.PI.ProcessInstanceId),
                PreActivityInstanceId  = new int?(this.AI.ActivityInstanceId),
                PostActivityInstanceId = new int?(postAi.ActivityInstanceId),
                TransitionId           = new long?(tran.TransitionId),
                TransitionInstanceId   = base.Manager.GetNextIdentity()
            };

            base.PICacheFactory.AddTransitionInstance(ti, this.AI, postAi);
        }
        /// <summary>
        /// 绑定活动列表(只显示审核活动)
        /// </summary>
        private void BindActivityList()
        {
            var source = from a in this.DataHelper.Set <SysActivity>()
                         where a.ProcessId == this.ProcessId &&
                         a.ActivityType == (int)ActivityType.Approve
                         orderby a.DisplayOrder
                         select new
            {
                a.DisplayOrder,
                a.ActivityId,
                a.ActivityName,
                SelectClass = a.ActivityId == this.SelectedActivityId ? "selected" : "unselected",
            };

            this.rActivity.DataSource = source.ToList();
            this.rActivity.DataBind();

            //驳回方式
            this.ccRejectType.DataSource = typeof(FormProcessActivityRejectType);
            this.ccRejectType.DataBind();

            //驳回目标活动(只能选当前活动上面的的审核活动)
            this.ccRejectTargetActivity.DataSource = source.Where(p => p.DisplayOrder < this.SelectedActivity.DisplayOrder).ToList();
            this.ccRejectTargetActivity.DataBind();

            //找到当前活动驳回指向的活动
            this.divRejectTargetActivity.Visible = false;
            SysTransition t = this.DataHelper.Set <SysTransition>()
                              .Where(p => p.PreActivityId == this.SelectedActivityId && p.Direction == (int)FlowStepDirection.False).FirstOrDefault();

            if (t == null)
            {
                this.ccRejectType.SelectedValue = ((int)FormProcessActivityRejectType.SelectActivityWhenRunning).ToString();
            }
            else
            {
                SysActivity target = this.DataHelper.FindById <SysActivity>(t.PostActivityId);
                if (target.ActivityType == (int)ActivityType.End)
                {
                    this.ccRejectType.SelectedValue = ((int)FormProcessActivityRejectType.EndActivity).ToString();
                }
                else
                {
                    this.ccRejectType.SelectedValue           = ((int)FormProcessActivityRejectType.SelectActivity).ToString();
                    this.ccRejectTargetActivity.SelectedValue = target.ActivityId.ToString();
                    this.divRejectTargetActivity.Visible      = true;
                }
            }
        }
        private bool CalExpressionValue(SysTransition tran)
        {
            SysExpression expr;
            bool          flag = true;

            if ((tran.Expressions != null) && (tran.Expressions.Count != 0))
            {
                if (!tran.ExpressionId.HasValue)
                {
                    return(flag);
                }
                expr = tran.Expressions.FirstOrDefault <SysExpression>(e => e.ExpressionId == tran.ExpressionId.Value);
                if (expr == null)
                {
                    throw new ApplicationException("指定的入口表达式不在连接线的表达式中");
                }
                Queue <SysExpression> calOrder = ExpressionHelper.GetCalOrder(tran.Expressions.ToList <SysExpression>());
                if (calOrder.Count <SysExpression>(p => (p.ExpressionId == expr.ExpressionId)) <= 0)
                {
                    throw new ApplicationException("无法计算表达式的值");
                }
                EntityCache     cache  = new EntityCache(base.Manager);
                ExpressionCache cache2 = new ExpressionCache();
                while (calOrder.Count > 0)
                {
                    SysExpression expression = calOrder.Dequeue();
                    object        obj2       = ExpressionHelper.GetHelper(expression).GetValue(expression, cache, cache2, base.PI, this.AI);
                    if (expression.ExpressionId == expr.ExpressionId)
                    {
                        if (obj2 == null)
                        {
                            throw new ApplicationException("表达式的值返回null");
                        }
                        if (obj2.GetType() != typeof(bool))
                        {
                            throw new ApplicationException("入口条件表达式的值不是布尔类型");
                        }
                        return((bool)obj2);
                    }
                }
            }
            return(flag);
        }
        /// <summary>
        /// 连接两个活动
        /// </summary>
        private void ConnectActivity(BizDataContext db, SysActivity prev, SysActivity post, FlowStepDirection direction)
        {
            SysTransition t = new SysTransition()
            {
                TransitionId   = db.GetNextIdentity(),
                PreActivityId  = prev.ActivityId,
                PostActivityId = post.ActivityId,
                ProcessId      = this.ProcessId,
                Direction      = (int)direction,
            };

            switch (direction)
            {
            case FlowStepDirection.False: t.DisplayText = "驳回"; break;

            case FlowStepDirection.True: t.DisplayText = "通过"; break;

            default: break;
            }
            db.Insert(t);
        }
        private void CompleteApproveActivity(Queue <WorkflowMessage> queue, SysProcess process, SysActivity activity)
        {
            new ActivityRemindHandler(base.PICacheFactory, base.PI, this.AI, ActivityRemindUseTimeType.ActivityEnd, null, this.AI.ApproveResult).Execute();
            List <SysTransition> source = process.Transitions.Where <SysTransition>(delegate(SysTransition t) {
                long?preActivityId = t.PreActivityId;
                long activityId    = activity.ActivityId;
                return((preActivityId.GetValueOrDefault() == activityId) && preActivityId.HasValue);
            }).ToList <SysTransition>();

            if (source.Count < 1)
            {
                throw new ApplicationException("没有后续的转换条件");
            }
            SysTransition tran = null;

            if (this.AI.ExpressionValue.HasValue)
            {
                int direction = Convert.ToBoolean(this.AI.ExpressionValue.Value) ? 1 : 2;
                tran = source.FirstOrDefault <SysTransition>(delegate(SysTransition p) {
                    int?nullable1 = p.Direction;
                    int num       = direction;
                    return((nullable1.GetValueOrDefault() == num) && nullable1.HasValue);
                });
            }
            if (tran == null)
            {
                tran = this.GetNextTransition(source);
            }
            if (tran == null)
            {
                throw new ApplicationException("所有后续连接线均不满足条件,流程无法继续");
            }
            SysActivityInstance ai   = this.CreatePostAcitivtyInstance(base.PI, tran);
            WorkflowMessage     item = base.Engine.NewActivityInstanceCreatedMessage(ai);

            queue.Enqueue(item);
        }
        private void CompleteDecisionActivity(Queue <WorkflowMessage> queue, SysProcess process, SysActivity activity)
        {
            List <SysTransition> source = process.Transitions.Where <SysTransition>(delegate(SysTransition t) {
                long?preActivityId = t.PreActivityId;
                long activityId    = activity.ActivityId;
                return((preActivityId.GetValueOrDefault() == activityId) && preActivityId.HasValue);
            }).ToList <SysTransition>();

            if (source.Count < 1)
            {
                throw new ApplicationException("没有后续的转换条件");
            }
            if (!this.AI.ExpressionValue.HasValue)
            {
                throw new ApplicationException("未计算决策活动的值");
            }
            int           direction = Convert.ToBoolean(this.AI.ExpressionValue.Value) ? 1 : 2;
            SysTransition tran      = source.FirstOrDefault <SysTransition>(delegate(SysTransition p) {
                int?nullable1 = p.Direction;
                int num       = direction;
                return((nullable1.GetValueOrDefault() == num) && nullable1.HasValue);
            });

            if (tran == null)
            {
                throw new ApplicationException(string.Format("没有方向为{0}的后续活动", direction));
            }
            if (!tran.PostActivityId.HasValue)
            {
                throw new ApplicationException("后续活动为空");
            }
            SysActivityInstance ai   = this.CreatePostAcitivtyInstance(base.PI, tran);
            WorkflowMessage     item = base.Engine.NewActivityInstanceCreatedMessage(ai);

            queue.Enqueue(item);
        }
        /// <summary>
        /// 复制流程,从“已启用”或“已升级”的流程进入设计,需要先复制一份流程供设计
        /// 状态为“新增”,版本为当前最高版本 + 1
        /// </summary>
        public SysProcess CopyProcess()
        {
            SysProcess result;

            using (System.Transactions.TransactionScope ts = new System.Transactions.TransactionScope())
            {
                using (BizDataContext db = new BizDataContext())
                {
                    //新流程
                    SysProcess newP = _process.ConvertTo <SysProcess>();
                    newP.ProcessId = db.GetNextIdentity();

                    //版本处理
                    newP.ProcessStatus = (int)ProcessState.Created;
                    string maxVersion = db.Set <SysProcess>().Where(p => p.ProcessType == _process.ProcessType).OrderByDescending(p => p.ProcessVersion).First().ProcessVersion;
                    newP.ProcessVersion = (maxVersion.ToInt() + 1).ToString();

                    result = newP;
                    db.Insert(newP);

                    //新流程参与人
                    var ppList = db.Set <SysProcessParticipant>().Where(p => p.ProcessId == _process.ProcessId).ToList();
                    Dictionary <long?, SysProcessParticipant> tempPpDict = new Dictionary <long?, SysProcessParticipant>();
                    foreach (var pp in ppList)
                    {
                        SysProcessParticipant newPp = pp.ConvertTo <SysProcessParticipant>();
                        newPp.ParticipantId = db.GetNextIdentity();
                        newPp.ProcessId     = newP.ProcessId;

                        tempPpDict[pp.ParticipantId] = newPp; //记录原始ID和新对象的关系
                    }
                    foreach (var newPp in tempPpDict.Values)
                    {
                        if (newPp.Param_ParticipantId != null) //通过刚才记录的关系找到实际的值
                        {
                            newPp.Param_ParticipantId = tempPpDict[newPp.Param_ParticipantId].ParticipantId;
                        }
                        db.Insert(newPp);
                    }

                    //新活动
                    var aList = db.Set <SysActivity>().Where(p => p.ProcessId == _process.ProcessId).ToList();
                    Dictionary <long?, SysActivity> tempADict = new Dictionary <long?, SysActivity>();
                    foreach (var a in aList)
                    {
                        SysActivity newA = a.ConvertTo <SysActivity>();
                        newA.ActivityId = db.GetNextIdentity();
                        newA.ProcessId  = newP.ProcessId;

                        tempADict[a.ActivityId] = newA;//记录原始ID和新对象的关系
                        db.Insert(newA);

                        //新活动参与人
                        var apList = db.Set <SysActivityParticipant>().Where(p => p.ActivityId == a.ActivityId).ToList();
                        foreach (var ap in apList)
                        {
                            SysActivityParticipant newAp = ap.ConvertTo <SysActivityParticipant>();
                            newAp.ActivityParticipantId = db.GetNextIdentity();
                            newAp.ActivityId            = newA.ActivityId;
                            newAp.ParticipantId         = tempPpDict[newAp.ParticipantId].ParticipantId; //通过刚才记录的关系找到实际的值
                            db.Insert(newAp);
                        }
                    }

                    //连接线
                    var tList = db.Set <SysTransition>().Where(p => p.ProcessId == _process.ProcessId).ToList();
                    foreach (var t in tList)
                    {
                        SysTransition newT = t.ConvertTo <SysTransition>();
                        newT.TransitionId = db.GetNextIdentity();
                        newT.ProcessId    = newP.ProcessId;

                        //通过刚才记录的关系找到实际的值
                        newT.PreActivityId  = tempADict[newT.PreActivityId].ActivityId;
                        newT.PostActivityId = tempADict[newT.PostActivityId].ActivityId;

                        db.Insert(newT);
                    }

                    //表单活动权限
                    var fpList = db.Set <SysFormPrivilege>().Where(p => p.ProcessId == _process.ProcessId).ToList();
                    foreach (var fp in fpList)
                    {
                        SysFormPrivilege newFp = fp.ConvertTo <SysFormPrivilege>();
                        newFp.PrivilegeId = db.GetNextIdentity();
                        newFp.ProcessId   = newP.ProcessId;
                        newFp.ActivityId  = tempADict[newFp.ActivityId].ActivityId;//通过刚才记录的关系找到实际的值

                        db.Insert(newFp);
                    }
                }
                ts.Complete();
            }
            return(result);
        }
        /// <summary>
        /// 验证流程的完整性,绑定流程详情和验证错误信息
        /// </summary>
        private void BindAndValidateData(SysProcess process)
        {
            this.lblProcessName.Text    = process.ProcessName;
            this.lblProcessVersion.Text = process.ProcessVersion;

            //表单相关
            SysForm form = this.DataHelper.FindById <SysForm>(process.FormId);

            if (form != null)
            {
                this.lblFormName.Text = form.FormName;

                SysEntity entity = GetEntity(form.EntityId);
                if (entity != null)
                {
                    this.lblProcessEntity.Text = entity.DisplayText;
                }
                else
                {
                    AddError(new Exception("流程实体为空,请至表单配置页面设置"));
                }
            }
            else
            {
                AddError(new Exception("流程关联表单为空,请返回至第一步配置"));
            }

            //流程参与人
            var ppCount = this.DataHelper.Set <SysProcessParticipant>()
                          .Where(p => p.ProcessId == this.ProcessId).Count();

            if (ppCount == 0)
            {
                AddError(new Exception("流程参与人未配置,请返回至第一步配置"));
            }

            //活动相关
            var activityList = this.DataHelper.Set <SysActivity>()
                               .Where(p => p.ProcessId == this.ProcessId).OrderBy(p => p.DisplayOrder ?? 0).ToList();

            this.gcActivity.DataSource = activityList.Select(p => new
            {
                p.ActivityName,
                ActivityType        = EnumHelper.GetDescription((ActivityType)p.ActivityType),
                ExecuteType         = EnumHelper.GetDescription((ActivityExecType)p.ExecType),
                ActivityParticipant = GetActivityParticipant(p),
            }).ToList();
            this.gcActivity.DataBind();

            if (activityList.Count(p => p.ActivityType == (int)ActivityType.Start) != 1)
            {
                AddError(new Exception("流程开始活动内部错误,请重建流程"));
                return;
            }
            if (activityList.Count(p => p.ActivityType == (int)ActivityType.End) != 2)
            {
                AddError(new Exception("流程结束活动内部错误,请重建流程"));
                return;
            }
            if (activityList.Count(p => p.ActivityType == (int)ActivityType.Approve) == 0)
            {
                AddError(new Exception("流程审核活动内部错误,请重建流程"));
                return;
            }

            foreach (var a in activityList)
            {
                if (string.IsNullOrEmpty(a.ActivityName))
                {
                    AddError(new Exception("活动名称为空,请返回至第二步配置"));
                }
                if (a.ActivityType == (int)ActivityType.Approve)
                {
                    var apCount = this.DataHelper.Set <SysActivityParticipant>()
                                  .Where(p => p.ActivityId == a.ActivityId).Count();
                    if (apCount == 0)
                    {
                        AddError(new Exception(string.Format("活动【{0}】参与人未配置,请返回至第二步配置", a.ActivityName)));
                    }
                }
            }

            //连接线完整性验证
            List <SysTransition> tList = this.DataHelper.Set <SysTransition>()
                                         .Where(p => p.ProcessId == this.ProcessId).ToList();
            Queue <SysActivity> queue = new Queue <SysActivity>();
            SysActivity         start = activityList.FirstOrDefault(p => p.ActivityType == (int)ActivityType.Start);
            SysActivity         temp  = start;

            queue.Enqueue(start);
            while (queue.Count > 0)
            {
                temp = queue.Dequeue();
                SysTransition t = tList.FirstOrDefault(p => p.PreActivityId == temp.ActivityId &&
                                                       p.Direction == (int)FlowStepDirection.True);
                if (t != null)
                {
                    temp = activityList.FirstOrDefault(p => p.ActivityId == t.PostActivityId);
                    queue.Enqueue(temp);
                }
                else
                {
                    break;
                }
            }
            if (!(temp.ActivityType == (int)ActivityType.End && temp.ActivityName == "通过"))
            {
                AddError(new Exception(string.Format("流程在活动【{0}】处断开,内部错误,请重建流程", temp.ActivityName)));
            }
        }
Пример #12
0
        internal SysProcess LoadProcess(long processId)
        {
            SysProcess process = this._context.FindById <SysProcess>(new object[] { processId });

            if (process == null)
            {
                throw new ApplicationException("流程ID不正确");
            }
            if (process.EntityId.HasValue)
            {
                process.ProcessEntity = this._cacheFactory.GetEntityCache(process.EntityId.Value);
            }
            if (process.ActivityEntityId.HasValue)
            {
                process.ActivityEntity = this._cacheFactory.GetEntityCache(process.ActivityEntityId.Value);
            }
            if (process.FormId.HasValue)
            {
                process.Form = this._context.FindById <SysForm>(new object[] { process.FormId });
            }
            string condition = string.Format("processId = {0}", processId);
            List <SysProcessProxy>             list    = this._context.Where <SysProcessProxy>(condition, new DbParameter[0]);
            List <SysProcessParticipant>       list2   = this._context.Where <SysProcessParticipant>(condition, new DbParameter[0]);
            List <SysExpression>               list3   = this._context.Where <SysExpression>(condition, new DbParameter[0]);
            List <SysTransition>               list4   = this._context.Where <SysTransition>(condition, new DbParameter[0]);
            List <SysProcessRemind>            list5   = this._context.Where <SysProcessRemind>(condition, new DbParameter[0]);
            List <SysProcessRemindParticipant> source  = this._context.Where <SysProcessRemindParticipant>(condition, new DbParameter[0]);
            List <SysActivity>                  list7  = this._context.Where <SysActivity>(condition, new DbParameter[0]);
            List <SysActivityParticipant>       list8  = this._context.Where <SysActivityParticipant>(condition, new DbParameter[0]);
            List <SysActivityOperation>         list9  = this._context.Where <SysActivityOperation>(condition, new DbParameter[0]);
            List <SysActivityStep>              list10 = this._context.Where <SysActivityStep>(condition, new DbParameter[0]);
            List <SysActivityRemind>            list11 = this._context.Where <SysActivityRemind>(condition, new DbParameter[0]);
            List <SysActivityRemindParticipant> list12 = this._context.Where <SysActivityRemindParticipant>(condition, new DbParameter[0]);
            List <SysActivityApproveGroup>      list13 = this._context.Where <SysActivityApproveGroup>(condition, new DbParameter[0]);

            process.Activities          = list7;
            process.ProcessProxies      = list;
            process.ProcessParticipants = list2;
            process.ProcessReminds      = list5;
            process.Expressions         = list3;
            process.Transitions         = list4;
            using (IEnumerator <SysActivity> enumerator = process.Activities.GetEnumerator())
            {
                while (enumerator.MoveNext())
                {
                    Func <SysExpression, bool>          predicate = null;
                    Func <SysActivityParticipant, bool> func8     = null;
                    Func <SysActivityRemind, bool>      func9     = null;
                    Func <SysActivityOperation, bool>   func10    = null;
                    Func <SysExpression, bool>          func11    = null;
                    Func <SysTransition, bool>          func12    = null;
                    Func <SysTransition, bool>          func13    = null;
                    SysActivity a = enumerator.Current;
                    a.Process = process;
                    if (predicate == null)
                    {
                        predicate = i => i.ExpressionId == a.ExpressionId;
                    }
                    a.Expression = process.Expressions.FirstOrDefault <SysExpression>(predicate);
                    if (func8 == null)
                    {
                        func8 = delegate(SysActivityParticipant i) {
                            long?activityId = i.ActivityId;
                            long num        = a.ActivityId;
                            return((activityId.GetValueOrDefault() == num) && activityId.HasValue);
                        };
                    }
                    a.ActivityParticipants = list8.Where <SysActivityParticipant>(func8).ToList <SysActivityParticipant>();
                    if (func9 == null)
                    {
                        func9 = delegate(SysActivityRemind i) {
                            long?activityId = i.ActivityId;
                            long num        = a.ActivityId;
                            return((activityId.GetValueOrDefault() == num) && activityId.HasValue);
                        };
                    }
                    a.ActivityReminds = list11.Where <SysActivityRemind>(func9).ToList <SysActivityRemind>();
                    if (func10 == null)
                    {
                        func10 = delegate(SysActivityOperation i) {
                            long?activityId = i.ActivityId;
                            long num        = a.ActivityId;
                            return((activityId.GetValueOrDefault() == num) && activityId.HasValue);
                        };
                    }
                    a.ActivityOperations = list9.Where <SysActivityOperation>(func10).ToList <SysActivityOperation>();
                    if (func11 == null)
                    {
                        func11 = delegate(SysExpression i) {
                            long?activityId = i.ActivityId;
                            long num        = a.ActivityId;
                            return((activityId.GetValueOrDefault() == num) && activityId.HasValue);
                        };
                    }
                    a.Expressions = process.Expressions.Where <SysExpression>(func11).ToList <SysExpression>();
                    if (func12 == null)
                    {
                        func12 = delegate(SysTransition i) {
                            long?preActivityId = i.PreActivityId;
                            long activityId    = a.ActivityId;
                            return((preActivityId.GetValueOrDefault() == activityId) && preActivityId.HasValue);
                        };
                    }
                    a.FromTransitions = process.Transitions.Where <SysTransition>(func12).ToList <SysTransition>();
                    if (func13 == null)
                    {
                        func13 = delegate(SysTransition i) {
                            long?postActivityId = i.PostActivityId;
                            long activityId     = a.ActivityId;
                            return((postActivityId.GetValueOrDefault() == activityId) && postActivityId.HasValue);
                        };
                    }
                    a.ToTransitions = process.Transitions.Where <SysTransition>(func13).ToList <SysTransition>();
                    using (IEnumerator <SysActivityParticipant> enumerator2 = a.ActivityParticipants.GetEnumerator())
                    {
                        while (enumerator2.MoveNext())
                        {
                            Func <SysActivityApproveGroup, bool> func  = null;
                            Func <SysActivityParticipant, bool>  func2 = null;
                            Func <SysProcessParticipant, bool>   func3 = null;
                            SysActivityParticipant ap = enumerator2.Current;
                            ap.Activity = a;
                            if (func == null)
                            {
                                func = i => i.ActivityApproveGroupId == ap.ActivityApproveGroupId;
                            }
                            ap.ActivityApproveGroup = list13.FirstOrDefault <SysActivityApproveGroup>(func);
                            if (ap.ActivityApproveGroup != null)
                            {
                                ap.ActivityApproveGroup.Activity = a;
                                if (func2 == null)
                                {
                                    func2 = i => i.ActivityApproveGroupId == ap.ActivityApproveGroupId;
                                }
                                ap.ActivityApproveGroup.ActivityParticipants = list8.Where <SysActivityParticipant>(func2).ToList <SysActivityParticipant>();
                            }
                            if (func3 == null)
                            {
                                func3 = i => i.ParticipantId == ap.ParticipantId;
                            }
                            ap.ProcessParticipant = process.ProcessParticipants.FirstOrDefault <SysProcessParticipant>(func3);
                        }
                    }
                    using (IEnumerator <SysActivityRemind> enumerator3 = a.ActivityReminds.GetEnumerator())
                    {
                        while (enumerator3.MoveNext())
                        {
                            Func <SysActivityRemindParticipant, bool> func5 = null;
                            SysActivityRemind ar = enumerator3.Current;
                            ar.Activity = a;
                            if (func5 == null)
                            {
                                func5 = delegate(SysActivityRemindParticipant i) {
                                    long?remindId = i.RemindId;
                                    long num      = ar.RemindId;
                                    return((remindId.GetValueOrDefault() == num) && remindId.HasValue);
                                };
                            }
                            ar.RemindParticipants = list12.Where <SysActivityRemindParticipant>(func5).ToList <SysActivityRemindParticipant>();
                            using (IEnumerator <SysActivityRemindParticipant> enumerator4 = ar.RemindParticipants.GetEnumerator())
                            {
                                while (enumerator4.MoveNext())
                                {
                                    Func <SysProcessParticipant, bool> func4 = null;
                                    SysActivityRemindParticipant       arp   = enumerator4.Current;
                                    arp.Remind = ar;
                                    if (func4 == null)
                                    {
                                        func4 = i => i.ParticipantId == arp.ParticipantId;
                                    }
                                    arp.Participant    = process.ProcessParticipants.FirstOrDefault <SysProcessParticipant>(func4);
                                    arp.RemindTemplate = this._context.FindById <SysProcessRemindTemplate>(new object[] { arp.TemplateId });
                                }
                                continue;
                            }
                        }
                    }
                    using (IEnumerator <SysActivityOperation> enumerator5 = a.ActivityOperations.GetEnumerator())
                    {
                        while (enumerator5.MoveNext())
                        {
                            Func <SysActivityStep, bool> func6 = null;
                            SysActivityOperation         ao    = enumerator5.Current;
                            ao.Activity = a;
                            if (func6 == null)
                            {
                                func6 = delegate(SysActivityStep i) {
                                    long?operationId = i.OperationId;
                                    long num         = ao.OperationId;
                                    return((operationId.GetValueOrDefault() == num) && operationId.HasValue);
                                };
                            }
                            ao.ActivitySteps = list10.Where <SysActivityStep>(func6).ToList <SysActivityStep>();
                            foreach (SysActivityStep step in ao.ActivitySteps)
                            {
                                step.ActivityOperation = ao;
                            }
                        }
                        continue;
                    }
                }
            }
            foreach (SysProcessProxy proxy in process.ProcessProxies)
            {
                proxy.Process = process;
            }
            using (IEnumerator <SysProcessParticipant> enumerator8 = process.ProcessParticipants.GetEnumerator())
            {
                while (enumerator8.MoveNext())
                {
                    Func <SysProcessParticipant, bool>  func14 = null;
                    Func <SysActivityParticipant, bool> func15 = null;
                    SysProcessParticipant pp = enumerator8.Current;
                    pp.Process = process;
                    if (func14 == null)
                    {
                        func14 = i => i.ParticipantId == pp.Param_ParticipantId;
                    }
                    pp.Param_Participant = process.ProcessParticipants.FirstOrDefault <SysProcessParticipant>(func14);
                    if (func15 == null)
                    {
                        func15 = delegate(SysActivityParticipant i) {
                            long?participantId = i.ParticipantId;
                            long num           = pp.ParticipantId;
                            return((participantId.GetValueOrDefault() == num) && participantId.HasValue);
                        };
                    }
                    pp.ActivityParticipants = list8.Where <SysActivityParticipant>(func15).ToList <SysActivityParticipant>();
                }
            }
            using (IEnumerator <SysProcessRemind> enumerator9 = process.ProcessReminds.GetEnumerator())
            {
                while (enumerator9.MoveNext())
                {
                    Func <SysProcessRemindParticipant, bool> func17 = null;
                    SysProcessRemind pr = enumerator9.Current;
                    pr.Process = process;
                    if (func17 == null)
                    {
                        func17 = delegate(SysProcessRemindParticipant i) {
                            long?remindId = i.RemindId;
                            long num      = pr.RemindId;
                            return((remindId.GetValueOrDefault() == num) && remindId.HasValue);
                        };
                    }
                    pr.RemindParticipants = source.Where <SysProcessRemindParticipant>(func17).ToList <SysProcessRemindParticipant>();
                    using (IEnumerator <SysProcessRemindParticipant> enumerator10 = pr.RemindParticipants.GetEnumerator())
                    {
                        while (enumerator10.MoveNext())
                        {
                            Func <SysProcessParticipant, bool> func16 = null;
                            SysProcessRemindParticipant        rp     = enumerator10.Current;
                            rp.Remind = pr;
                            if (func16 == null)
                            {
                                func16 = i => i.ParticipantId == rp.ParticipantId;
                            }
                            rp.Participant    = process.ProcessParticipants.FirstOrDefault <SysProcessParticipant>(func16);
                            rp.RemindTemplate = this._context.FindById <SysProcessRemindTemplate>(new object[] { rp.TemplateId });
                        }
                        continue;
                    }
                }
            }
            using (IEnumerator <SysExpression> enumerator11 = process.Expressions.GetEnumerator())
            {
                while (enumerator11.MoveNext())
                {
                    Func <SysActivity, bool> func18 = null;
                    SysExpression            e      = enumerator11.Current;
                    e.Process = process;
                    if (func18 == null)
                    {
                        func18 = i => i.ActivityId == e.ActivityId;
                    }
                    e.Activity = process.Activities.FirstOrDefault <SysActivity>(func18);
                    if (e.FieldId.HasValue)
                    {
                        e.Field = this._cacheFactory.GetFieldCache(e.FieldId.Value);
                    }
                    if (e.RelationId.HasValue)
                    {
                        e.Relation = this._cacheFactory.GetRelationCache(e.RelationId.Value);
                    }
                }
            }
            using (IEnumerator <SysTransition> enumerator12 = process.Transitions.GetEnumerator())
            {
                while (enumerator12.MoveNext())
                {
                    Func <SysActivity, bool>   func19 = null;
                    Func <SysActivity, bool>   func20 = null;
                    Func <SysExpression, bool> func21 = null;
                    Func <SysExpression, bool> func22 = null;
                    SysTransition t = enumerator12.Current;
                    t.Process = process;
                    if (func19 == null)
                    {
                        func19 = i => i.ActivityId == t.PreActivityId;
                    }
                    t.PreActivity = process.Activities.FirstOrDefault <SysActivity>(func19);
                    if (func20 == null)
                    {
                        func20 = i => i.ActivityId == t.PostActivityId;
                    }
                    t.PostActivity = process.Activities.FirstOrDefault <SysActivity>(func20);
                    if (func21 == null)
                    {
                        func21 = i => i.ExpressionId == t.ExpressionId;
                    }
                    t.Expression = process.Expressions.FirstOrDefault <SysExpression>(func21);
                    if (func22 == null)
                    {
                        func22 = delegate(SysExpression i) {
                            long?transitionId = i.TransitionId;
                            long num          = t.TransitionId;
                            return((transitionId.GetValueOrDefault() == num) && transitionId.HasValue);
                        };
                    }
                    t.Expressions = process.Expressions.Where <SysExpression>(func22).ToList <SysExpression>();
                }
            }
            return(process);
        }
        private SysActivityInstance CreatePostAcitivtyInstance(SysProcessInstance pi, SysTransition tran)
        {
            if (!tran.PostActivityId.HasValue)
            {
                throw new ApplicationException("后续活动为空");
            }
            SysActivity         postActivity = pi.Process.Activities.FirstOrDefault <SysActivity>(p => p.ActivityId == tran.PostActivityId.Value);
            SysActivityInstance postAi       = this.CreatePostActivityInstance(pi, postActivity);

            this.CreateTransitionInstance(postAi, tran);
            return(postAi);
        }
        /// <summary>
        /// 删除活动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                //所有活动
                Dictionary <long, SysActivity> activityDict = this.DataHelper.Set <SysActivity>()
                                                              .Where(p => p.ProcessId == this.ProcessId).ToDictionary(p => p.ActivityId);

                if (activityDict.Count == 4)
                {
                    throw new Exception("只有一个审核活动,不能删除");
                }

                //当前活动
                SysActivity a = activityDict[this.SelectedActivityId];

                //当前活动参与人
                List <SysActivityParticipant> apList = this.DataHelper.Set <SysActivityParticipant>()
                                                       .Where(p => p.ActivityId == this.SelectedActivityId).ToList();

                //此活动之后的审核活动
                List <SysActivity> nextActivityList = activityDict.Values.Where(p => (p.DisplayOrder ?? 0) > a.DisplayOrder).ToList();

                //最后一个审核活动
                SysActivity lastActivity = activityDict.Values.OrderByDescending(p => p.DisplayOrder ?? 0).FirstOrDefault();

                //结束活动(驳回)
                SysActivity endReject = activityDict.Values.FirstOrDefault(p => p.ActivityType == (int)ActivityType.End && p.ActivityName == "驳回");

                //通过指向当前活动的连接线
                SysTransition tIn_Pass = this.DataHelper.Set <SysTransition>()
                                         .Where(p => p.PostActivityId == this.SelectedActivityId &&
                                                p.Direction == (int)FlowStepDirection.True).FirstOrDefault();

                //驳回指向当前活动的连接线(集合)
                List <SysTransition> tIn_RejectList = this.DataHelper.Set <SysTransition>()
                                                      .Where(p => p.PostActivityId == this.SelectedActivityId &&
                                                             p.Direction == (int)FlowStepDirection.False).ToList();

                //从当前活动通过指向的连接线
                SysTransition tOut_Pass = this.DataHelper.Set <SysTransition>()
                                          .Where(p => p.PreActivityId == this.SelectedActivityId &&
                                                 p.Direction == (int)FlowStepDirection.True).FirstOrDefault();

                //从当前活动驳回指向的连接线
                SysTransition tOut_Reject = this.DataHelper.Set <SysTransition>()
                                            .Where(p => p.PreActivityId == this.SelectedActivityId &&
                                                   p.Direction == (int)FlowStepDirection.False).FirstOrDefault();

                using (System.Transactions.TransactionScope ts = new System.Transactions.TransactionScope())
                {
                    using (BizDataContext db = new BizDataContext())
                    {
                        //让上一个活动通过指向当前活动通过指向的活动
                        tIn_Pass.PostActivityId = tOut_Pass.PostActivityId;
                        db.UpdatePartial(tIn_Pass, p => new { p.PostActivityId });

                        //让其它驳回指向当前活动的活动驳回指向结束活动(驳回)
                        foreach (var tIn_Reject in tIn_RejectList)
                        {
                            tIn_Reject.PostActivityId = endReject.ActivityId;
                            db.UpdatePartial(tIn_Reject, p => new { p.PostActivityId });
                        }

                        //删除当前活动及连接线
                        db.Delete(a);
                        db.Delete(tOut_Pass);
                        if (tOut_Reject != null)
                        {
                            db.Delete(tOut_Reject);
                        }

                        //删除当前活动参与人
                        foreach (var ap in apList)
                        {
                            db.Delete(ap);
                        }

                        //将后面的活动的顺序提前1位
                        foreach (var na in nextActivityList)
                        {
                            na.DisplayOrder--;
                            na.TopPos -= 130;
                            db.UpdatePartial(na, p => new { p.DisplayOrder, p.TopPos });
                        }
                    }
                    ts.Complete();
                }

                //选择最后一个活动
                SelectActivity(lastActivity);

                this.EnableButton();
            }
            catch (Exception ex)
            {
                this.AjaxAlertAndEnableButton(ex);
            }
        }
        /// <summary>
        /// 新增一个活动,并选中
        /// </summary>
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                //所有活动
                Dictionary <long, SysActivity> activityDict = this.DataHelper.Set <SysActivity>()
                                                              .Where(p => p.ProcessId == this.ProcessId).ToDictionary(p => p.ActivityId);

                //最后一个审核活动
                SysActivity lastActivity = activityDict.Values.Where(p => p.ActivityType == (int)ActivityType.Approve)
                                           .OrderByDescending(p => p.DisplayOrder).FirstOrDefault();

                SysActivity endReject = activityDict.Values.FirstOrDefault(p => p.ActivityType == (int)ActivityType.End && p.ActivityName == "驳回");
                SysActivity endPass   = activityDict.Values.FirstOrDefault(p => p.ActivityType == (int)ActivityType.End && p.ActivityName == "通过");

                int maxCount = activityDict.Values.Max(p => p.DisplayOrder ?? 0);

                //最后一个审核活动通过指向的连接线
                SysTransition t = this.DataHelper.Set <SysTransition>()
                                  .Where(p => p.PreActivityId == lastActivity.ActivityId &&
                                         p.Direction == (int)FlowStepDirection.True).FirstOrDefault();

                SysActivity newActivity;
                using (System.Transactions.TransactionScope ts = new System.Transactions.TransactionScope())
                {
                    using (BizDataContext db = new BizDataContext())
                    {
                        //新活动
                        newActivity = new SysActivity()
                        {
                            ActivityId   = db.GetNextIdentity(),
                            ProcessId    = this.ProcessId,
                            ActivityType = (int)ActivityType.Approve,
                            DisplayOrder = maxCount + 1,
                            ExecType     = (int)ActivityExecType.Manual,
                            PassType     = (int)ActivityPassType.FirstDefault,
                            IsPassedWithNoParticipants = true,
                            ActivityName = "活动名称",
                            LeftPos      = 350,
                            TopPos       = (maxCount + 1) * 130 + 50,
                        };
                        db.Insert(newActivity);

                        //把上一个活动接到新活动
                        t.PostActivityId = newActivity.ActivityId;
                        db.UpdatePartial(t, p => new { p.PostActivityId });

                        //按默认规则链接新活动和结束活动
                        ConnectActivity(db, newActivity, endPass, FlowStepDirection.True);
                        ConnectActivity(db, newActivity, endReject, FlowStepDirection.False);
                    }
                    ts.Complete();
                }

                //选中新活动
                SelectActivity(newActivity);

                this.EnableButton();
            }
            catch (Exception ex)
            {
                this.AjaxAlertAndEnableButton(ex);
            }
        }
        /// <summary>
        /// 保存当前活动配置
        /// </summary>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                //所有活动
                Dictionary <long, SysActivity> activityDict = this.DataHelper.Set <SysActivity>()
                                                              .Where(p => p.ProcessId == this.ProcessId).ToDictionary(p => p.ActivityId);

                //当前活动
                SysActivity a = activityDict[this.SelectedActivityId];

                using (System.Transactions.TransactionScope ts = new System.Transactions.TransactionScope())
                {
                    using (BizDataContext db = new BizDataContext())
                    {
                        a.ActivityName = this.txtActivityName.Text.Trim();
                        db.UpdatePartial(a, p => new { p.ActivityName });

                        #region 处理驳回方式
                        //找到当前活动驳回指向的连接线
                        SysTransition t = this.DataHelper.Set <SysTransition>()
                                          .Where(p => p.PreActivityId == this.SelectedActivityId && p.Direction == (int)FlowStepDirection.False).FirstOrDefault();
                        FormProcessActivityRejectType rejectType = (FormProcessActivityRejectType)this.ccRejectType.SelectedValue.ToInt();
                        switch (rejectType)
                        {
                        case FormProcessActivityRejectType.SelectActivityWhenRunning:
                        {
                            if (t != null)
                            {
                                db.Delete(t);
                            }
                            break;
                        }

                        case FormProcessActivityRejectType.SelectActivity:
                        {
                            SysActivity target = activityDict[this.ccRejectTargetActivity.SelectedValue.ToLong()];
                            if (t == null)
                            {
                                ConnectActivity(db, a, target, FlowStepDirection.False);
                            }
                            else if (t.PostActivityId != target.ActivityId)
                            {
                                t.PostActivityId = target.ActivityId;
                                db.UpdatePartial(t, p => new { p.PostActivityId });
                            }
                            break;
                        }

                        case FormProcessActivityRejectType.EndActivity:
                        {
                            SysActivity end = activityDict.Values.FirstOrDefault(p => p.ActivityType == (int)ActivityType.End && p.ActivityName == "驳回");
                            if (t == null)
                            {
                                ConnectActivity(db, a, end, FlowStepDirection.False);
                            }
                            else if (t.PostActivityId != end.ActivityId)
                            {
                                t.PostActivityId = end.ActivityId;
                                db.UpdatePartial(t, p => new { p.PostActivityId });
                            }
                            break;
                        }

                        default: break;
                        }
                        #endregion
                    }
                    ts.Complete();
                }

                //重新选择一下
                SelectActivity(a);

                this.AjaxAlertAndEnableButton("保存成功");
            }
            catch (Exception ex)
            {
                this.AjaxAlertAndEnableButton(ex);
            }
        }