/// <summary>
        /// 获取指定工作流、角色、时间段的实例集合
        /// </summary>
        /// <param name="workflowName">流程名称</param>
        /// <param name="userIdentity">用户身份</param>
        /// <param name="role">审批角色</param>
        /// <param name="startDate">时间段起始时间</param>
        /// <param name="endDate">时间段截止时间</param>
        /// <returns></returns>
        protected override InstanceCollection Distill(string workflowName, IUserIdentity userIdentity, ApprovalRole role, DateTime startDate, DateTime endDate)
        {
            InstanceCollection   instances = new InstanceCollection();
            StateMachineWorkflow workflow  = (StateMachineWorkflow)WorkflowRuntime.Current.GetService <IWorkFlowDefinePersistService>().GetWorkflowDefine(workflowName);
            //遍历工作流中的每一个状态,获取可以处理的状态对应的实例.
            List <string> canDoStates = new List <string>();

            foreach (ApprovalState oneState in workflow.States)
            {
                if (oneState.IsApprovalState && InstanceDistillerHelper.IsMineICanDo(oneState, role))
                {
                    canDoStates.Add(oneState.Name);
                }
            }

            if (canDoStates.Count == 0)
            {
                return(instances);
            }
            List <StateMachineWorkflowInstance> list = WorkflowRuntime.Current.GetListByState(workflowName, canDoStates.ToArray());

            //获取指定给本单位办理的实例
            foreach (StateMachineWorkflowInstance instance in list)
            {
                if (instance.PersistTime >= startDate &&
                    instance.PersistTime <= endDate)
                {
                    instances.Add(new InstanceWithRole(instance, role, true));
                }
            }

            return(instances);
        }
        /// <summary>
        /// 获取指定工作流、角色、时间段的实例集合
        /// </summary>
        /// <param name="workflowName">流程名称</param>
        /// <param name="userIdentity">用户身份</param>
        /// <param name="role">审批角色</param>
        /// <param name="startDate">时间段起始时间</param>
        /// <param name="endDate">时间段截止时间</param>
        /// <returns></returns>
        protected override InstanceCollection Distill(string workflowName, IUserIdentity userIdentity, ApprovalRole role, DateTime startDate, DateTime endDate)
        {
            List <StateMachineWorkflowInstance> instances = new List <StateMachineWorkflowInstance>();
            StateMachineWorkflow workflow  = (StateMachineWorkflow)WorkflowRuntime.Current.GetService <IWorkFlowDefinePersistService>().GetWorkflowDefine(workflowName);
            List <string>        allStates = new List <string>();

            foreach (ApprovalState state in workflow.States)
            {
                if (state.IsApprovalState && !state.Name.Equals(workflow.InitState))
                {
                    allStates.Add(state.Name);
                }
            }
            if (allStates.Count == 0)
            {
                return(new InstanceCollection());
            }

            string unitCode = userIdentity.GetUserUnitCode();

            unitCode = string.IsNullOrEmpty(unitCode) ? "  " : (unitCode.Trim() + "%");
            instances.AddRange(WorkflowRuntime.Current.GetUnitList(workflowName, allStates.ToArray(), unitCode));

            InstanceCollection collection = new InstanceCollection();

            foreach (StateMachineWorkflowInstance one in instances)
            {
                InstanceWithRole o = new InstanceWithRole(one, role, true);
                o.TaskName = "a." + one.CurrentState.Description;
                collection.Add(o);
            }
            return(collection);
        }
        /// <summary>
        /// 获取指定工作流、角色、时间段的实例集合
        /// </summary>
        /// <param name="workflowName">流程名称</param>
        /// <param name="userIdentity">用户身份</param>
        /// <param name="role">审批角色</param>
        /// <param name="startDate">时间段起始时间</param>
        /// <param name="endDate">时间段截止时间</param>
        /// <returns></returns>
        protected override InstanceCollection Distill(string workflowName, IUserIdentity userIdentity, ApprovalRole role, DateTime startDate, DateTime endDate)
        {
            List <StateMachineWorkflowInstance> instances      = new List <StateMachineWorkflowInstance>();
            List <ApprovalAssignment>           assignmentList = WorkflowRuntime.Current.GetService <IApprovalSaveService>().GetAssignmentByToUnit(workflowName, userIdentity.GetUserUnitCode());
            List <Guid> ids = new List <Guid>();

            foreach (ApprovalAssignment assignment in assignmentList)
            {
                if (!string.IsNullOrEmpty(assignment.ToUserId))
                {
                    continue;
                }
                if (!ids.Contains(assignment.WorkflowInstanceId))
                {
                    ids.Add(assignment.WorkflowInstanceId);
                }
            }
            foreach (Guid id in ids)
            {
                instances.Add((StateMachineWorkflowInstance)WorkflowRuntime.Current.GetInstance(id));
            }
            InstanceCollection collection = new InstanceCollection();

            foreach (StateMachineWorkflowInstance instance in instances)
            {
                if (InstanceDistillerHelper.IsAssignedICanDo(instance.CurrentState, role) &&
                    instance.PersistTime >= startDate &&
                    instance.PersistTime <= endDate)
                {
                    collection.Add(new InstanceWithRole(instance, role, false));
                }
            }
            return(collection);
        }
        /// <summary>
        /// 获取指定工作流、角色、时间段的实例集合
        /// </summary>
        /// <param name="workflowName">流程名称</param>
        /// <param name="userIdentity">用户身份</param>
        /// <param name="role">审批角色</param>
        /// <param name="startDate">时间段起始时间</param>
        /// <param name="endDate">时间段截止时间</param>
        /// <returns></returns>
        protected override InstanceCollection Distill(string workflowName, IUserIdentity userIdentity, ApprovalRole role, DateTime startDate, DateTime endDate)
        {
            List <StateMachineWorkflowInstance> instances = new List <StateMachineWorkflowInstance>();
            StateMachineWorkflow     workflow             = (StateMachineWorkflow)WorkflowRuntime.Current.GetService <IWorkFlowDefinePersistService>().GetWorkflowDefine(workflowName);
            Dictionary <string, int> stateIndex           = new Dictionary <string, int>();
            List <string>            allStates            = new List <string>();

            for (int i = 0; i < workflow.States.Length; i++)
            {
                ApprovalState state = workflow.States[i];
                if (state.IsApprovalState && !state.Name.Equals(workflow.InitState))
                {
                    stateIndex.Add(state.Name, i);
                    allStates.Add(state.Name);
                }
            }
            if (allStates.Count == 0)
            {
                return(new InstanceCollection());
            }

            instances.AddRange(WorkflowRuntime.Current.GetListByState(workflowName, allStates.ToArray()));
            InstanceCollection collection = new InstanceCollection();

            foreach (StateMachineWorkflowInstance one in instances)
            {
                InstanceWithRole o = new InstanceWithRole(one, role, true);
                o.TaskName = ((char)((int)'a' + stateIndex[one.CurrentState.Name])) + "." + one.CurrentState.Description;
                collection.Add(o);
            }
            return(collection);
        }
        /// <summary>
        /// 获取指定工作流、角色、时间段的实例集合
        /// </summary>
        /// <param name="workflowName">流程名称</param>
        /// <param name="userIdentity">用户身份</param>
        /// <param name="role">审批角色</param>
        /// <param name="startDate">时间段起始时间</param>
        /// <param name="endDate">时间段截止时间</param>
        /// <returns></returns>
        protected override InstanceCollection Distill(string workflowName, IUserIdentity userIdentity, ApprovalRole role, DateTime startDate, DateTime endDate)
        {
            InstanceCollection        collection  = new InstanceCollection();
            List <ApprovalAssignment> assignments = WorkflowRuntime.Current.SaveService.GetAssignmentByToUnit(workflowName, userIdentity.GetUserUnitCode());
            List <Guid> ids = new List <Guid>();

            collection = new InstanceCollection();
            foreach (ApprovalAssignment assignment in assignments)
            {
                if (string.IsNullOrEmpty(assignment.ToUserId))
                {
                    if (!ids.Contains(assignment.WorkflowInstanceId))
                    {
                        ids.Add(assignment.WorkflowInstanceId);
                    }
                }
            }
            foreach (Guid id in ids)
            {
                StateMachineWorkflowInstance instance = (StateMachineWorkflowInstance)WorkflowRuntime.Current.GetInstance(id);
                if (instance.PersistTime > startDate && instance.PersistTime < endDate)
                {
                    collection.Add(new InstanceWithRole(instance, role, false));
                }
            }
            return(collection);
        }
        /// <summary>
        /// 获取指定工作流、角色、时间段的实例集合
        /// </summary>
        /// <param name="workflowName">流程名称</param>
        /// <param name="userIdentity">用户身份</param>
        /// <param name="role">审批角色</param>
        /// <param name="startDate">时间段起始时间</param>
        /// <param name="endDate">时间段截止时间</param>
        /// <returns></returns>
        protected override InstanceCollection Distill(string workflowName, IUserIdentity userIdentity, ApprovalRole role, DateTime startDate, DateTime endDate)
        {
            InstanceCollection collection = new InstanceCollection();
            List <StateMachineWorkflowInstance> instances = new List <StateMachineWorkflowInstance>();
            List <string> states = InstanceDistillerHelper.GetMineICanCancelStates(workflowName, role);

            if (states.Count == 0)
            {
                return(collection);
            }

            string unitCode = userIdentity.GetUserUnitCode();

            unitCode = string.IsNullOrEmpty(unitCode) ? "  " : (unitCode.Trim() + "%");
            instances.AddRange(WorkflowRuntime.Current.GetUnitList(workflowName, states.ToArray(), unitCode));
            foreach (StateMachineWorkflowInstance instance in instances)
            {
                if (instance.PersistTime >= startDate &&
                    instance.PersistTime <= endDate)
                {
                    collection.Add(new InstanceWithRole(instance, role, true));
                }
            }
            return(collection);
        }
        /// <summary>
        /// 按角色名称合并集合,并按EaId和任务名称进行分组
        /// </summary>
        /// <returns></returns>
        public Dictionary <string, List <InstanceCollection> > Split()
        {
            //按角色分组
            Dictionary <string, InstanceCollection> dic = new Dictionary <string, InstanceCollection>();

            foreach (InstanceWithRole one in this)
            {
                if (dic.ContainsKey(one.Role.Name))
                {
                    dic[one.Role.Name].Add(one);
                }
                else
                {
                    InstanceCollection c = new InstanceCollection();
                    c.Add(one);
                    dic.Add(one.Role.Name, c);
                }
            }
            //对每个角色下的分组进行EaId和任务名称分组
            Dictionary <string, List <InstanceCollection> > dic2 = new Dictionary <string, List <InstanceCollection> >();

            foreach (string key in dic.Keys)
            {
                dic2.Add(key, dic[key].SplitByEaId());
            }
            return(dic2);
        }
        /// <summary>
        /// 获取指定工作流、角色、时间段的实例集合
        /// </summary>
        /// <param name="workflowName">流程名称</param>
        /// <param name="userIdentity">用户身份</param>
        /// <param name="role">审批角色</param>
        /// <param name="startDate">时间段起始时间</param>
        /// <param name="endDate">时间段截止时间</param>
        /// <returns></returns>
        protected override InstanceCollection Distill(string workflowName, IUserIdentity userIdentity, ApprovalRole role, DateTime startDate, DateTime endDate)
        {
            StateMachineWorkflow workflow = (StateMachineWorkflow)WorkflowRuntime.Current.GetService <IWorkFlowDefinePersistService>().GetWorkflowDefine(workflowName);
            List <StateMachineWorkflowInstance> instances = WorkflowRuntime.Current.GetInstance(workflowName, startDate, endDate, new string[] { workflow.EndState }, userIdentity.GetUserUnitCode());
            InstanceCollection collection = new InstanceCollection();

            foreach (StateMachineWorkflowInstance instance in instances)
            {
                collection.Add(new InstanceWithRole(instance, role, false));
            }
            return(collection);
        }
        /// <summary>
        ///按角色查找匹配的实例结合.
        /// </summary>
        /// <param name="role">The role.</param>
        /// <returns></returns>
        public InstanceCollection GetByRole(ApprovalRole role)
        {
            InstanceCollection rtn = new InstanceCollection();

            foreach (InstanceWithRole one in this)
            {
                if (one.Role.Equals(role))
                {
                    rtn.Add(one);
                }
            }
            return(rtn);
        }
        /// <summary>
        /// 按照Ea id和任务名称进行分类合并
        /// </summary>
        /// <returns></returns>
        public List <InstanceCollection> SplitByEaId()
        {
            //先按任务名称,EaId生成两级分组
            Dictionary <string, Dictionary <int, InstanceCollection> > dic2 = new Dictionary <string, Dictionary <int, InstanceCollection> >();

            foreach (InstanceWithRole one in this)
            {
                string taskName = one.TaskName;
                int    eaid     = one.Instance.EaId;
                //有任务名键
                if (dic2.ContainsKey(taskName))
                {
                    //有EaId的键则添加实例,否则添加键值
                    if (dic2[taskName].ContainsKey(eaid))
                    {
                        dic2[taskName][eaid].Add(one);
                    }
                    else
                    {
                        InstanceCollection o = new InstanceCollection();
                        o.Add(one);
                        dic2[taskName].Add(eaid, o);
                    }
                }
                else
                {
                    //任务名键值,则添加键值
                    InstanceCollection o = new InstanceCollection();
                    o.Add(one);
                    Dictionary <int, InstanceCollection> oDic = new Dictionary <int, InstanceCollection>();
                    oDic.Add(eaid, o);
                    dic2.Add(taskName, oDic);
                }
            }
            //返回按EaId和任务名称分组的实例集合列表
            List <InstanceCollection> splited = new List <InstanceCollection>();

            foreach (string key1 in dic2.Keys)
            {
                foreach (int key2 in dic2[key1].Keys)
                {
                    splited.Add(dic2[key1][key2]);
                }
            }
            return(splited);
        }
        /// <summary>
        /// 获取指定工作流、角色、时间段的实例集合
        /// </summary>
        /// <param name="workflowName">流程名称</param>
        /// <param name="userIdentity">用户身份</param>
        /// <param name="role">审批角色</param>
        /// <param name="startDate">时间段起始时间</param>
        /// <param name="endDate">时间段截止时间</param>
        /// <returns></returns>
        protected override InstanceCollection Distill(string workflowName, IUserIdentity userIdentity, ApprovalRole role, DateTime startDate, DateTime endDate)
        {
            List <StateMachineWorkflowInstance> instances = new List <StateMachineWorkflowInstance>();
            string userName   = "";
            string fromUserId = "";
            //找到代理条目给获取授权人Id和授权人姓名
            List <ApprovalAgent> agentList = WorkflowRuntime.Current.GetService <IApprovalSaveService>().GetAgentInfoByToUser(userIdentity.GetUserId());

            foreach (ApprovalAgent agent in agentList)
            {
                if (agent.BeginDate == startDate && agent.EndDate == endDate)
                {
                    userName   = string.Format("{0}(代{1})", agent.ToUserName, agent.SetUserName);
                    fromUserId = agent.SetUserId;
                }
            }
            //获取用户时间段内的审批记录,找到符合代理信息的记录,获取相应的实例
            StateMachineWorkflow  workflow = WorkflowRuntime.Current.GetService <IWorkFlowDefinePersistService>().GetWorkflowDefine(workflowName) as StateMachineWorkflow;
            List <ApprovalRecord> records  = WorkflowRuntime.Current.GetService <IApprovalSaveService>().GetRecord(workflowName, startDate, endDate, fromUserId);
            List <Guid>           ids      = new List <Guid>();

            foreach (ApprovalRecord record in records)
            {
                if (record.OperatorName == userName)
                {
                    ids.Add(record.WorkflowInstanceId);
                }
            }
            instances = new List <StateMachineWorkflowInstance>();
            foreach (Guid id in ids)
            {
                instances.Add((StateMachineWorkflowInstance)WorkflowRuntime.Current.GetInstance(id));
            }
            InstanceCollection collection = new InstanceCollection();

            foreach (StateMachineWorkflowInstance instance in instances)
            {
                if (instance.WorkflowName == workflowName)
                {
                    collection.Add(new InstanceWithRole(instance, role, false));
                }
            }
            return(collection);
        }
        /// <summary>
        /// 获取指定工作流、角色、时间段的实例集合
        /// </summary>
        /// <param name="workflowName">流程名称</param>
        /// <param name="userIdentity">用户身份</param>
        /// <param name="role">审批角色</param>
        /// <param name="startDate">时间段起始时间</param>
        /// <param name="endDate">时间段截止时间</param>
        /// <returns></returns>
        protected override InstanceCollection Distill(string workflowName, IUserIdentity userIdentity, ApprovalRole role, DateTime startDate, DateTime endDate)
        {
            StateMachineWorkflow workflow = WorkflowRuntime.Current.GetService <IWorkFlowDefinePersistService>().GetWorkflowDefine(workflowName) as StateMachineWorkflow;
            string userId, unitCode, roleName;

            userId = unitCode = roleName = "";
            if (isMatchUser)
            {
                userId = userIdentity.GetUserId();
            }
            if (isMatchUnit)
            {
                unitCode = userIdentity.GetUserUnitCode();
            }
            if (isMatchRole)
            {
                roleName = role.Name;
            }
            List <ApprovalRecord> records = WorkflowRuntime.Current.GetService <IApprovalSaveService>().GetRecord(workflowName, startDate, endDate, userId, unitCode, roleName);
            List <Guid>           ids     = new List <Guid>();

            foreach (ApprovalRecord record in records)
            {
                if (!ids.Contains(record.WorkflowInstanceId))
                {
                    ids.Add(record.WorkflowInstanceId);
                }
            }
            List <StateMachineWorkflowInstance> instances = new List <StateMachineWorkflowInstance>();

            foreach (Guid id in ids)
            {
                instances.Add((StateMachineWorkflowInstance)WorkflowRuntime.Current.GetInstance(id));
            }
            InstanceCollection collection = new InstanceCollection();

            foreach (StateMachineWorkflowInstance instance in instances)
            {
                collection.Add(new InstanceWithRole(instance, role, false));
            }
            return(collection);
        }
        /// <summary>
        /// 获取指定工作流、角色、时间段的实例集合
        /// </summary>
        /// <param name="workflowName">流程名称</param>
        /// <param name="userIdentity">用户身份</param>
        /// <param name="role">审批角色</param>
        /// <param name="startDate">时间段起始时间</param>
        /// <param name="endDate">时间段截止时间</param>
        /// <returns></returns>
        protected override InstanceCollection Distill(string workflowName, IUserIdentity userIdentity, ApprovalRole role, DateTime startDate, DateTime endDate)
        {
            InstanceCollection instances = new InstanceCollection();

            if (this.states != null)
            {
                List <StateMachineWorkflowInstance> list = WorkflowRuntime.Current.GetListByState(workflowName, this.states);
                //获取指定给本单位办理的实例
                foreach (StateMachineWorkflowInstance instance in list)
                {
                    if (instance.PersistTime >= startDate &&
                        instance.PersistTime <= endDate)
                    {
                        instances.Add(new InstanceWithRole(instance, role, true));
                    }
                }
            }

            return(instances);
        }
        /// <summary>
        /// 获取指定工作流、角色、时间段的实例集合
        /// </summary>
        /// <param name="workflowName">流程名称</param>
        /// <param name="userIdentity">用户身份</param>
        /// <param name="role">审批角色</param>
        /// <param name="startDate">时间段起始时间</param>
        /// <param name="endDate">时间段截止时间</param>
        /// <returns></returns>
        protected override InstanceCollection Distill(string workflowName, IUserIdentity userIdentity, ApprovalRole role, DateTime startDate, DateTime endDate)
        {
            InstanceCollection collection = new InstanceCollection();
            List <StateMachineWorkflowInstance> instances = new List <StateMachineWorkflowInstance>();
            List <string> states = InstanceDistillerHelper.GetMineICanCancelStates(workflowName, role);

            if (states.Count == 0)
            {
                return(collection);
            }
            List <StateMachineWorkflowInstance> list = WorkflowRuntime.Current.GetListByState(workflowName, states.ToArray());

            instances.AddRange(list);

            foreach (StateMachineWorkflowInstance instance in instances)
            {
                if (instance.PersistTime >= startDate &&
                    instance.PersistTime <= endDate)
                {
                    collection.Add(new InstanceWithRole(instance, role, true));
                }
            }
            return(collection);
        }