void AdminLock(WorkitemInstance wii)
 {
     if (wii.GetActivity().IsLock)
     {
         wii.GetWorkflowInstance().Locker = new WorkitemLocker()
         {
             User = this.Context.CurUser,
             IsLock = true,
             LoggedTime = DateTime.Now,
         };
     }
 }
        /// <summary>
        /// 强制加锁
        /// </summary>
        /// <param name="wii"></param>
        /// <returns></returns>
        public bool? AdminLock(WorkitemInstance wii)
        {
            ///如果处理锁竞争状态, 如果无需上锁则直接退出
            if (!wii.GetActivity().IsLock)
            {
                return null;
            }

            return wii.GetWorkflowInstance().SaveWorkflowInstanceLock(new WorkitemLocker()
            {
                User = this.Context.CurUser,
                IsLock = true,
                LoggedTime = DateTime.Now,
            });
        }
 /// <summary>
 /// 解锁
 /// </summary>
 /// <param name="wii"></param>
 void DecodeLock(WorkitemInstance wii)
 {
     if (wii.GetActivity().IsLock
         &&
         wii.GetWorkflowInstance().Locker.IsLock
         &&
         wii.GetWorkflowInstance().Locker.User.Guid.Equals(this.Context.CurUser.Guid))
     {
         wii.GetWorkflowInstance().Locker = new WorkitemLocker()
         {
             IsLock = false,
             User = this.Context.CurUser,
             LoggedTime = DateTime.Now,
         };
     }
 }
 /// <summary>
 /// 工作项锁处理
 /// 对于已经加锁的超过锁定期的自动解锁,并重新上锁, 返回可处理流程权限为可控制状态
 /// 对于未加锁的直接上锁,返回可处理流程权限为可控制状态
 /// 对于已经加锁的在有效锁定期内的,返回处理流程权限为不可控制状态
 /// </summary>
 /// <param name="wii"></param>
 /// <returns>工作项只读状态</returns>
 bool EncodeLock(WorkitemInstance wii)
 {
     bool result = false;
     #region 锁定处理
     ///如果处理锁竞争状态, 则直接上锁
     if (wii.GetActivity().IsLock)
     {
         if (wii.GetWorkflowInstance().Locker == null
             ||
             wii.GetWorkflowInstance().Locker.IsLock == false
             )
         {
             wii.GetWorkflowInstance().Locker = new WorkitemLocker()
             {
                 User = this.Context.CurUser,
                 IsLock = true,
                 LoggedTime = DateTime.Now,
             };
             result = this.SetWorkflowInstance(wii.GetWorkflowInstance(), eStoreType.Update);
         }
         else
             ///超过设置时间自动解锁(默认为一小时)
             if (wii.GetWorkflowInstance().Locker.LoggedTime.AddHours(this.Context.Config.LockMinute) >= DateTime.Now)
             {
                 wii.GetWorkflowInstance().Locker = new WorkitemLocker()
                 {
                     User = this.Context.CurUser,
                     IsLock = true,
                     LoggedTime = DateTime.Now,
                 };
                 result = this.SetWorkflowInstance(wii.GetWorkflowInstance(), eStoreType.Update);
             }
             else
             {
                 result = false;
             }
     }
     else
     {
         result = true;
     }
     #endregion
     return result;
 }
        /// <summary>
        /// 当前用户是否可以访问该流程实例活动
        /// </summary>
        /// <param name="wii"></param>
        /// <returns></returns>
        bool IsEffective(WorkitemInstance wii)
        {
            ///是否大管理员
            if (this.Context.CurUser.IsAdministrator())
                return true;

            ///是否流程模型管理员
            if (wii.GetWorkflowInstance().GetWorkflow().IsAdministrators(this.Context.CurUser))
                return true;

            ///是否当前流程实例活动的操作者
            if (wii.GetWorkflowInstance().GetWorkflow().IsLegalAuthor(this.Context.CurUser))
                return true;

            ///是否当前流程实例活动的代理操作者
            if (wii.IsProxy && _uc.Equals( wii.ProxyUser,this.Context.CurUser))
                return true;

            ///是否当前流程实例活动的潜在代理操作者
            return wii.IsEffectiveByProxyUser();
        }
 /// <summary>
 /// 保存工作流活动实例
 /// </summary>
 /// <param name="wii"></param>
 /// <param name="storeType"></param>
 /// <returns></returns>
 bool SetWorkitemInstance(WorkitemInstance wii, eStoreType storeType)
 {
     return this.Context.Store.SetWorkitemInstance(wii, storeType);
 }
 public bool SetWorkitemInstance(WorkitemInstance wii, eStoreType storeType)
 {
     throw new NotImplementedException();
 }
        /// <summary>
        /// 流程活动处理
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public ResultMessage SubmitWorkitemInstance(RequestBySubmitWorkitem request)
        {
            ResultMessage rm = new ResultMessage() { State = true, Message = "" };

            WorkitemInstance wii = this.GetWorkitemInstance(request.WorkitemInstanceGuid);
            ///不在许可范围之内的,直接返回失败
            if (!this.IsEffective(wii)) return new ResultMessage() { State = false, Message = "不在许可操作范围内!" };

            wii.AduitContent = request.AduitContent;
            wii.AduitSign = request.AduitSign;

            wii.SelectVoteItem = request.SelectVoteItem;
            wii.OtherVoteItemContent = request.OtherVoteItemContent;
            wii.IsProxy = request.IsProxyUser;
            if (wii.IsProxy)
            {
                wii.ProxyUser = this.Context.CurUser;
            }
            else
            {
                wii.User = this.Context.CurUser;
            }
            //wii.SetDataEntity(request.DataEntity);

            switch (request.State)
            {
                #region Running 挂起流程操作
                case eWorkitemState.Running:
                    {
                        wii.Hung();
                        wii.Save();
                    }
                    break;
                #endregion

                #region Abort 退出流程处理,中止流程
                case eWorkitemState.Abort:
                    {
                        #region 条件判断
                        bool b = false;
                        ///是否大管理员
                        if (this.Context.CurUser.IsAdministrator())
                            b = true;
                        ///是否流程模型管理员
                        if (wii.GetWorkflowInstance().GetWorkflow().Administrators.Contains(this.Context.CurUser, _uc))
                            b = true;
                        ///是否是流程发起人
                        if (_uc.Equals(this.Context.CurUser, wii.GetWorkflowInstance().Author))
                            b = true;
                        if (!b)
                        {
                            rm.State = false;
                            rm.Message = "你不具体中止流程的权限。只有管理员和发起人才可以中止流程!";
                            break;
                        }
                        #endregion

                        wii.Abort();
                        wii.Save();
                        wii.GetWorkflowInstance().Save();
                        break;
                    }
                #endregion

                #region Completed 正常完成流程处理
                case eWorkitemState.Completed:
                    {
                        wii.GetWorkflowInstance().SetDataEntity(request.DataEntity);

                        #region 创建后继流程活动工作项
                        IList<NextActivity> tmp = new List<NextActivity>();
                        foreach (var na in request.NextActivities)
                        {
                            foreach (var o in na.Operaters)
                            {
                                var x = new NextActivity()
                                {
                                    WorkitemInstanceGuid = System.Guid.NewGuid().ToString(),
                                    ActivityGuid = na.Activity,
                                    ActivityByname = na.ActivityName,
                                    User = this.GetUser(o),
                                };

                                #region 创建后继流程活动工作项
                                WorkitemInstance xwii = new WorkitemInstance(wii.GetWorkflowInstance().Guid,
                                    wii.GetWorkflow().GetActivity(na.Activity, eDaoItem.Guid))
                                {
                                    ActivityGuid = x.ActivityGuid,
                                    AduitContent = "",
                                    ReadTime = null,
                                    NextActivities = null,
                                    EndTime = null,
                                    User = x.User,
                                    ProxyUser = null,
                                    //WorkitemInstanceState = eWorkitemState.Ready,
                                    IsProxy = false,
                                    FromWorkitemInstanceGuid = wii.Guid,
                                    AttachmentTrainsitions = null,
                                };
                                xwii.SetContext(this.Context);
                                ///取流程实例数据到流程实例活动
                                xwii.Start();
                                //xwii.BeforeTrigger();
                                xwii.Save();
                                #endregion
                                tmp.Add(x);
                            }
                        }
                        wii.NextActivities = tmp;
                        #endregion

                        ///完成流程实例活动
                        wii.AfterTrigger();
                        wii.Save();
                        wii.GetWorkflowInstance().Save();
                        break;
                    }
                #endregion

                #region Other
                case eWorkitemState.Ready:
                case eWorkitemState.ExclusionCompleted:
                default:
                    {
                        break;
                    }
                #endregion
            }
            ///解锁
            this.DecodeLock(wii);
            if (!rm.State)
                return rm;

            return rm;
        }
        /// <summary>
        /// 用途:创建流程实例并返回首个流程活动工作项
        /// 约定:
        /// 1、一个流程有且仅有一个根活动
        /// 2、代理人不能发起委托人的新流程,只能处理委托日期后正在流转的流程数据
        /// 3、有发起流程模型权限方可以操作
        /// </summary>
        /// <param name="workflowGuid">流程模型guid</param>
        /// <returns>首个流程活动工作项</returns>
        public ResponseByWorkitem CreateWorkflowInstance(string workflowGuid, string name = null)
        {
            ///创建流程实例
            WorkflowInstance wfi = new WorkflowInstance(workflowGuid, name);
            //wfi.Guid ...
            //wfi.Name ...
            //wfi.WorkflowGuid ...
            wfi.Author = this.Context.CurUser;
            //wfi.WorkflowState ...
            //wfi.Participator ...
            //wfi.Locker ...
            //wfi.BeginTime ...;
            //wfi.EndTime ...;
            //wfi.DataEntity ...

            wfi.SetContext(this.Context);

            ///加入权限控制
            if (!(this.Context.CurUser.IsAdministrator()//是否大管理员
                  || wfi.GetWorkflow().IsAdministrators(this.Context.CurUser)//是否流程模型管理员
                  || wfi.GetWorkflow().IsLegalAuthor(this.Context.CurUser)//流程的合法发起用户
                  ))
            { return new ResponseByWorkitem() { CallBackMessage = new ResultMessage() { State = false, Message = "不能发起流程!" } }; }

            ///开启流程实例服务
            wfi.Start();
            wfi.Save();

            //创建活动实例
            WorkitemInstance wii = new WorkitemInstance(wfi.Guid, wfi.GetWorkflow().GetStartNode());
            //wii.Guid ...
            //wii.Name ...
            //wii.ActivityGuid ...
            //wii.WorkflowInstanceGuid ...
            //wii.AduitContent = "";
            //wii.AduitSign = "";
            //wii.ReadTime = null;
            //wii.NextActivities = null;
            wii.User = this.Context.CurUser;
            //wii.ProxyUser = null;
            //wii.IsProxy = false;
            //wii.WorkitemInstanceState ...
            //wii.FromWorkitemInstanceGuid = "";
            //wii.AttachmentTrainsitions = null;
            //wii.SelectVoteItem = "";
            //wii.OtherVoteItemContent = "";
            //wii.BeginTime ...;
            //wii.EndTime ...;
            //wii.DataEntity ...

            wii.SetContext(this.Context);
            ///开启活动实例
            wii.Start();
            wii.Save();
            ///活动前事件处理
            var rm = wii.BeforeTrigger();

            ResponseByWorkitem response;

            ///如果不满足
            if (!rm.State)
            {
                response = new ResponseByWorkitem()
                {
                    ActivityInstance = wii,
                    //NextMaybeActivities=null,
                    UIRight = this.Context.Config.RightByReadOnly,
                    CallBackMessage = rm,
                };
            }
            else
            {
                response = new ResponseByWorkitem()
                {
                    ActivityInstance = wii,
                    NextMaybeActivities = wii.GetNextEffectiveActivities(),
                    UIRight = wii.GetActivity().UIRight,
                    CallBackMessage = new ResultMessage() { State = true, Message = "" },
                };
            }
            return response;
        }
        /// <summary>
        /// 工作项锁处理
        /// 对于已经加锁的超过锁定期的自动解锁,并重新上锁, 返回可处理流程权限为可控制状态
        /// 对于未加锁的直接上锁,返回可处理流程权限为可控制状态
        /// 对于已经加锁的在有效锁定期内的,返回处理流程权限为不可控制状态
        /// </summary>
        /// <param name="wii"></param>
        /// <returns>工作项只读状态</returns>
        public bool? EncodeLock(WorkitemInstance wii)
        {
            ///如果处理锁竞争状态, 如果无需上锁则直接退出
            if (!wii.GetActivity().IsLock)
            {
                return null;
            }

            ///如果还没有被锁过,则置初始锁
            var locker = wii.GetWorkflowInstance().GetWorkflowInstanceLock();
            if (locker == null || !locker.IsLock)
            {
                return wii.GetWorkflowInstance().SaveWorkflowInstanceLock(new WorkitemLocker()
                {
                    User = this.Context.CurUser,
                    IsLock = true,
                    LoggedTime = DateTime.Now,
                });
            }

            ///超过设置时间自动解锁(默认单位为分钟)
            if (locker.LoggedTime.AddMinutes(this.Context.Config.LockMinute) >= DateTime.Now)
            {
                return wii.GetWorkflowInstance().SaveWorkflowInstanceLock(new WorkitemLocker()
                {
                    User = this.Context.CurUser,
                    IsLock = true,
                    LoggedTime = DateTime.Now,
                });
            }

            ///当前还在有效被锁的状态中
            return false;
        }
        /// <summary>
        /// 解锁
        /// </summary>
        /// <param name="wii"></param>
        public bool? DecodeLock(WorkitemInstance wii)
        {
            var locker = wii.GetWorkflowInstance().GetWorkflowInstanceLock();
            if (locker == null)
                return null;

            if (wii.GetActivity().IsLock
                &&
               locker.IsLock
                &&
                _uc.Equals(locker.User, this.Context.CurUser))
            {
                return wii.GetWorkflowInstance().SaveWorkflowInstanceLock(new WorkitemLocker()
                {
                    User = this.Context.CurUser,
                    IsLock = true,
                    LoggedTime = DateTime.Now,
                });
            }
            else
                return null;
        }