public List <WorkflowNodeInstance> GetAllNodeInstance(Guid planId, int twfid)
        {
            ActualSteps _instance = Find(a => a.PlanID == planId && a.PrevID == Guid.Empty && a.TWFID == twfid);


            //将流程节点进行排序
            List <WorkflowNodeInstance> orderInstList = new List <WorkflowNodeInstance>();

            if (_instance != null)
            {
                WorkflowNodeInstance wfInst = ExecReader(_instance);
                orderInstList.Add(wfInst);
                int count = 0;
                WorkflowNodeInstance tempInst = GetNodeInstance(wfInst.NextId);
                while (true)
                {
                    orderInstList.Add(tempInst);
                    if (tempInst.NextId == Guid.Empty)
                    {
                        break;
                    }
                    else
                    {
                        tempInst = GetNodeInstance(tempInst.NextId);
                        count++;
                    }
                }
            }
            return(orderInstList);
        }
 public bool UpdateFirstNode(WorkflowNodeInstance firstNodeInst, int userID, string userName)
 {
     try
     {
         firstNodeInst.State = WorkflowNodeInstance.StepStateType.Processing;
         var entity = new ActualSteps {
             ActorID = userID, ActorName = userName, State = (byte)firstNodeInst.State, ApplyTime = DateTime.Now, ID = firstNodeInst.Id
         };
         return(Update(entity, "ActorID", "ActorName", "State", "ApplyTime") > 0);
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
Example #3
0
        /// <summary>
        /// 创建流程实例
        /// </summary>
        /// <param name="tnode"></param>
        /// <param name="applyId"></param>
        /// <returns></returns>
        public WorkflowNodeInstance CreateNodeInstance(WorkflowTplNode tnode, Guid planId)
        {
            var guid = Guid.NewGuid();
            var date = DateTime.Now;
            WorkflowNodeInstance nodeInst = new WorkflowNodeInstance();

            ActualSteps _instance = new ActualSteps
            {
                ID         = guid,
                PlanID     = planId,
                StepID     = tnode.StepId,
                TWFID      = tnode.TWFID,
                State      = 0,
                PrevID     = Guid.Empty,
                NextID     = Guid.Empty,
                CreateTime = DateTime.Now,
                IsParallel = tnode.IsParallel
            };

            context.ActualSteps.Add(_instance);
            foreach (var item in tnode.SubTWFStepsList)
            {
                SubActualSteps _subinstance = new SubActualSteps
                {
                    ID           = Guid.NewGuid(),
                    ParentStepID = guid.ToString(),
                    State        = 0,
                    CreateTime   = DateTime.Now,
                    ActorName    = item.AuthorType,
                    RoleName     = item.AuthorType
                };
                context.SubActualSteps.Add(_subinstance);
                nodeInst.SubActualStepsList.Add(_subinstance);
            }
            if (context.SaveChanges() > 0)
            {
                nodeInst.Id         = guid;
                nodeInst.PlanID     = planId;
                nodeInst.StepId     = tnode.StepId;
                nodeInst.TWFID      = tnode.TWFID;
                nodeInst.State      = WorkflowNodeInstance.StepStateType.Initialized;
                nodeInst.PrevId     = Guid.Empty;
                nodeInst.NextId     = Guid.Empty;
                nodeInst.CreateTime = date;
            }
            return(nodeInst);
        }
        private WorkflowNodeInstance ExecReader(ActualSteps entity)
        {
            WorkflowNodeInstance ninst = new WorkflowNodeInstance();

            ninst.Id     = entity.ID;
            ninst.State  = (WorkflowNodeInstance.StepStateType)entity.State;
            ninst.PlanID = entity.PlanID;
            ninst.StepId = entity.StepID;
            ninst.TWFID  = entity.TWFID;
            if (entity.PrevID.HasValue)
            {
                ninst.PrevId = entity.PrevID.Value;
            }
            if (entity.NextID.HasValue)
            {
                ninst.NextId = entity.NextID.Value;
            }
            if (entity.ActorID.HasValue)
            {
                ninst.ActorID = entity.ActorID.Value;
            }
            ninst.ActorName = entity.ActorName ?? "";
            ninst.RoleName  = entity.RoleName ?? "";
            if (entity.ActorTime.HasValue)
            {
                ninst.ActorTime = entity.ActorTime.Value;
            }
            ninst.Comments = entity.Comments ?? "";
            if (entity.ApplyTime.HasValue)
            {
                ninst.ApplyTime = entity.ApplyTime.Value;
            }
            if (entity.IsParallel.HasValue)
            {
                ninst.IsParallel = entity.IsParallel.Value;
                if (ninst.IsParallel.Value)
                {
                    ninst.SubActualStepsList = context.SubActualSteps.Where(u => u.ParentStepID == entity.ID.ToString() && u.State != 4 && u.State != 0).ToList();
                }
            }
            return(ninst);
        }
        public Guid CreateWorkflowInstance(int twfId, Guid planId, int userID, string userName)
        {
            Guid firstStepId = Guid.Empty;
            List <WorkflowNodeInstance> tempNodesInst    = new List <WorkflowNodeInstance>();
            List <WorkflowNodeInstance> nodesInstance    = new List <WorkflowNodeInstance>();
            List <SubActualSteps>       subnodesInstance = new List <SubActualSteps>();
            WorkflowNodeInstance        firstNodeInst    = new WorkflowNodeInstance();

            //找出指定流程模板中所有的流程节点,并创建流程节点实例
            List <WorkflowTplNode> tnodeList = wtndal.GetNodeByTWFID(twfId);

            foreach (WorkflowTplNode tnode in tnodeList)
            {
                WorkflowNodeInstance ninst = wtndal.CreateNodeInstance(tnode, planId);
                tempNodesInst.Add(ninst);
            }
            //当流程节点实例创建完成后,给实例加上流程链,让实例节点串联起来
            WorkflowTplNode firstTnode = new WorkflowTplNode();
            var             query1     = from c in tnodeList where c.PrevId == 0 select c;

            if (query1.Count() != 1)
            {
                throw new Exception("由于没有或者存在多个流程初始节点,导致无法创建流程。");
            }
            else
            {
                firstTnode    = query1.First();
                firstNodeInst = tempNodesInst.First(item => item.StepId == firstTnode.StepId && item.PlanID == planId && item.TWFID == twfId);
                firstStepId   = firstNodeInst.Id;
            }
            var query2 = from c in tnodeList where c.NextId == 0 select c;

            if (query2.Count() != 1)
            {
                throw new Exception("由于没有或者存在多个流程结束节点,导致无法创建流程。");
            }
            WorkflowTplNode      prevTnode    = firstTnode;
            WorkflowNodeInstance prevNodeInst = firstNodeInst;

            while (prevTnode.NextId != 0)
            {
                var nextTnode    = tnodeList.First(item => item.PrevId == prevTnode.StepId);
                var nextNodeInst = tempNodesInst.First(item => item.StepId == nextTnode.StepId && item.PlanID == planId && item.TWFID == twfId);
                nextNodeInst.PrevId = prevNodeInst.Id;
                prevNodeInst.NextId = nextNodeInst.Id;
                if (nodesInstance.Count <= 0)
                {
                    nodesInstance.Add(prevNodeInst);  //第一次循环的时候,需要添加开始节点
                }
                nodesInstance.Add(nextNodeInst);
                prevTnode    = nextTnode;
                prevNodeInst = nextNodeInst;
            }

            //完成流程节点实例的串联工作,更新到数据库
            insdal.UpdateNodeInstance(nodesInstance);

            // 将第一个节点的状态改为Processing,并设置活动所有者
            insdal.UpdateFirstNode(firstNodeInst, userID, userName);
            return(firstStepId);
        }
        public WorkflowNodeInstance Submit(Guid planId, int twfid, int userID, string userName, string roleName, string comments, Action <WorkflowPlan> action, string controlDepList = null)
        {
            List <WorkflowNodeInstance> ninstList = GetAllNodeInstance(planId, twfid);
            var currInst = ninstList.First(item => item.State == WorkflowNodeInstance.StepStateType.Processing);
            WorkflowNodeInstance nextInst = null;

            using (var dbContextTransaction = context.Database.BeginTransaction())
            {
                try
                {
                    if (currInst.IsParallel.HasValue && currInst.IsParallel.Value)
                    {
                        //根据登陆角色获取流程实例
                        var entity = context.SubActualSteps.FirstOrDefault(u => u.ParentStepID == currInst.Id.ToString() && roleName.Contains(u.ActorName) && u.State == (int)WorkflowNodeInstance.StepStateType.Processing);
                        if (entity != null)
                        {
                            entity.State     = (byte)WorkflowNodeInstance.StepStateType.Processed;
                            entity.Comments  = comments;
                            entity.ActorID   = userID;
                            entity.ActorName = userName;
                            entity.ActorTime = DateTime.Now;
                            context.SaveChanges();
                            if (context.SubActualSteps.Count(u => u.ParentStepID == currInst.Id.ToString() && u.State == (int)WorkflowNodeInstance.StepStateType.Processing) != 0)
                            {
                                if (twfid == 1)
                                {
                                    var repplan = context.RepetitivePlan.Find(planId);
                                    if (repplan != null)
                                    {
                                        var list = new List <string>(repplan.ActorName.Split(','));
                                        list.Remove(roleName);
                                        repplan.ActorName = string.Join(",", list);
                                        context.SaveChanges();
                                    }
                                }
                                else if (twfid == 2)
                                {
                                    var flightplan = context.FlightPlan.Find(planId);
                                    if (flightplan != null)
                                    {
                                        var list = new List <string>(flightplan.ActorName.Split(','));
                                        list.Remove(roleName);
                                        flightplan.ActorName = string.Join(",", list);
                                        context.SaveChanges();
                                    }
                                }
                                else if (twfid == 3)
                                {
                                    var currentplan = context.CurrentFlightPlan.Find(planId);
                                    if (currentplan != null)
                                    {
                                        var list = new List <string>(currentplan.ActorName.Split(','));
                                        list.Remove(roleName);
                                        currentplan.ActorName = string.Join(",", list);
                                        context.SaveChanges();
                                    }
                                }
                                dbContextTransaction.Commit();
                                return(null);
                            }
                        }
                        else
                        {
                            throw new Exception("找不到流程实例!");
                        }
                    }
                    Update(new ActualSteps {
                        State = (byte)WorkflowNodeInstance.StepStateType.Processed, Comments = comments, ActorID = userID, ActorName = userName, ActorTime = DateTime.Now, ID = currInst.Id
                    }, "State", "ActorID", "ActorName", "Comments", "ActorTime");
                    currInst.State = WorkflowNodeInstance.StepStateType.Processed;
                    //更新下一个节点状态为处理中
                    if (currInst.NextId != Guid.Empty)
                    {
                        nextInst = GetNodeInstance(currInst.NextId);
                        WorkflowTplNode tnode = _dal.GetNode(nextInst.StepId);
                        #region 如果是并行流程
                        if (tnode.IsParallel.HasValue && tnode.IsParallel.Value)
                        {
                            if (controlDepList != null && !string.IsNullOrEmpty(controlDepList))
                            {
                                var controlDepArray = controlDepList.Split(',');
                                var list            = context.SubActualSteps.Where(u => u.ParentStepID == currInst.NextId.ToString());
                                foreach (var item in list)
                                {
                                    if (controlDepArray.Contains(item.ActorName))
                                    {
                                        item.State     = (byte)WorkflowNodeInstance.StepStateType.Processing;
                                        item.ApplyTime = DateTime.Now;
                                    }
                                    else
                                    {
                                        item.State = (byte)WorkflowNodeInstance.StepStateType.NoValid;
                                    }
                                }
                                tnode.AuthorType = controlDepList;
                                context.SaveChanges();
                            }
                            else   //将下个节点全部设为无效
                            {
                                var list = context.SubActualSteps.Where(u => u.ParentStepID == currInst.NextId.ToString());
                                foreach (var item in list)
                                {
                                    item.State = (byte)WorkflowNodeInstance.StepStateType.NoValid;
                                }
                                context.SaveChanges();
                                var nextid = currInst.NextId;
                                while (true)
                                {
                                    if (nextid == Guid.Empty)
                                    {
                                        break;
                                    }
                                    Update(new ActualSteps {
                                        State = (byte)WorkflowNodeInstance.StepStateType.NoValid, ID = nextid
                                    }, "State");
                                    nextid = GetNodeInstance(nextid).NextId;
                                }
                                Update(new ActualSteps {
                                    NextID = Guid.Empty, ID = currInst.Id
                                }, "NextID");
                                action(new WorkflowPlan {
                                    Actor = null, ActorName = null, PlanState = "end", PlanID = planId
                                });

                                dbContextTransaction.Commit();
                                return(null);
                            }
                        }
                        #endregion

                        //判断节点的活动所有者类型
                        Update(new ActualSteps {
                            State = (byte)WorkflowNodeInstance.StepStateType.Processing, ApplyTime = DateTime.Now, ActorName = tnode.AuthorType, RoleName = tnode.AuthorType, ID = currInst.NextId
                        }, "State", "ApplyTime", "ActorID", "ActorName", "RoleName");
                        action(new WorkflowPlan {
                            ActorName = tnode.AuthorType, PlanState = tnode.AuthorType, PlanID = planId
                        });

                        nextInst = GetNodeInstance(currInst.NextId);
                    }
                    else
                    {
                        action(new WorkflowPlan {
                            ActorName = null, PlanState = "end", PlanID = planId
                        });
                    }

                    dbContextTransaction.Commit();
                }
                catch (Exception ex)
                {
                    dbContextTransaction.Rollback();
                    throw ex;
                }
            }
            return(nextInst);
        }
        /// <summary>
        /// 审核不通过(管制)
        /// </summary>
        /// <param name="planId"></param>
        /// <param name="comments"></param>
        /// <returns></returns>
        public void TerminateGZ(Guid planId, int twfid, int userID, string userName, string roleName, string comments, Action <WorkflowPlan> func)
        {
            List <WorkflowNodeInstance> ninstList = GetAllNodeInstance(planId, twfid);
            var currInst = ninstList.First(item => item.State == WorkflowNodeInstance.StepStateType.Processing);
            WorkflowNodeInstance nextInst = null;

            using (var dbContextTransaction = context.Database.BeginTransaction())
            {
                try
                {
                    if (currInst.IsParallel.HasValue && currInst.IsParallel.Value)
                    {
                        //根据登陆角色获取流程实例
                        var entity = context.SubActualSteps.FirstOrDefault(u => u.ParentStepID == currInst.Id.ToString() && roleName.Contains(u.ActorName) && u.State == (int)WorkflowNodeInstance.StepStateType.Processing);
                        if (entity != null)
                        {
                            entity.State     = (byte)WorkflowNodeInstance.StepStateType.Deserted;
                            entity.Comments  = comments;
                            entity.ActorID   = userID;
                            entity.ActorName = userName;
                            entity.ActorTime = DateTime.Now;
                            context.SaveChanges();
                            if (context.SubActualSteps.Count(u => u.ParentStepID == currInst.Id.ToString() && u.State == (int)WorkflowNodeInstance.StepStateType.Processing) != 0)
                            {
                                if (twfid == 1)
                                {
                                    var repplan = context.RepetitivePlan.Find(planId);
                                    if (repplan != null)
                                    {
                                        var list = new List <string>(repplan.ActorName.Split(','));
                                        list.Remove(roleName);
                                        repplan.ActorName = string.Join(",", list);
                                        context.SaveChanges();
                                    }
                                }
                                else if (twfid == 2)
                                {
                                    var flightplan = context.FlightPlan.Find(planId);
                                    if (flightplan != null)
                                    {
                                        var list = new List <string>(flightplan.ActorName.Split(','));
                                        list.Remove(roleName);
                                        flightplan.ActorName = string.Join(",", list);
                                        context.SaveChanges();
                                    }
                                }
                                else if (twfid == 3)
                                {
                                    var currentplan = context.CurrentFlightPlan.Find(planId);
                                    if (currentplan != null)
                                    {
                                        var list = new List <string>(currentplan.ActorName.Split(','));
                                        list.Remove(roleName);
                                        currentplan.ActorName = string.Join(",", list);
                                        context.SaveChanges();
                                    }
                                }
                                dbContextTransaction.Commit();
                                return;
                            }
                        }
                        else
                        {
                            throw new Exception("找不到流程实例!");
                        }
                    }
                    Update(new ActualSteps {
                        State = (byte)WorkflowNodeInstance.StepStateType.Processed, Comments = comments, ActorID = userID, ActorName = userName, ActorTime = DateTime.Now, ID = currInst.Id
                    }, "State", "ActorID", "ActorName", "Comments", "ActorTime");
                    currInst.State = WorkflowNodeInstance.StepStateType.Processed;
                    //更新下一个节点状态为处理中
                    if (currInst.NextId != Guid.Empty)
                    {
                        nextInst = GetNodeInstance(currInst.NextId);
                        WorkflowTplNode tnode = _dal.GetNode(nextInst.StepId);

                        //判断节点的活动所有者类型
                        Update(new ActualSteps {
                            State = (byte)WorkflowNodeInstance.StepStateType.Processing, ApplyTime = DateTime.Now, ActorName = tnode.AuthorType, RoleName = tnode.AuthorType, ID = currInst.NextId
                        }, "State", "ApplyTime", "ActorID", "ActorName", "RoleName");
                        func(new WorkflowPlan {
                            ActorName = tnode.AuthorType, PlanState = tnode.AuthorType, PlanID = planId
                        });

                        nextInst = GetNodeInstance(currInst.NextId);
                    }
                    else
                    {
                        func(new WorkflowPlan {
                            ActorName = null, PlanState = "end", PlanID = planId
                        });
                    }

                    dbContextTransaction.Commit();
                }
                catch (Exception ex)
                {
                    dbContextTransaction.Rollback();
                    throw ex;
                }
            }
        }