using APT.BaseData.Domain.ApiModel.PF;
using APT.BaseData.Domain.Entities;
using APT.BaseData.Domain.Entities.FM;
using APT.BaseData.Domain.Enums;
using APT.BaseData.Domain.IServices;
using APT.BaseData.Domain.IServices.FM;
using APT.Infrastructure.Core;
using APT.MS.Domain.Entities.BS;
using APT.MS.Domain.Entities.HM;
using APT.MS.Domain.Enums;
using APT.Utility;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace APT.BS.WebApi.Controllers.Api
{
    /// <summary>
    /// 
    /// </summary>
    [Route("api/FM/FMUser")]
    public partial class FMUserController : AuthorizeApiController<T_FM_USER>
    {

        IFMUserService UserService { get; set; }

        public FMUserController(IFMUserService userService)
        {
            UserService = userService;
        }
        ///// <summary>
        ///// 正常使用的用户
        ///// 重写  api/FM/User/OrderPaged
        ///// 目前最多只支持4个生产单元有重叠的
        ///// </summary>
        ///// <param name="pageFilter"></param>
        ///// <returns></returns>
        //[HttpPost, Route("OrderPagedUseful")]
        //public PagedActionResult<T_FM_USER> OrderPagedUseful([FromBody] KeywordPageFilter pageFilter)
        //{
        //    if (string.IsNullOrEmpty(pageFilter.Parameter1))
        //    {
        //        List<int> listMineType = new List<int>();
        //        Guid DEPARTMENT_ID_WORKSHOP = Guid.Empty;
        //        Guid DEPARTMENT_ID = Guid.Empty;
        //        if (pageFilter.FilterGroup.Rules.Count == 2)
        //        {
        //            foreach (var item in pageFilter.FilterGroup.Rules)
        //            {
        //                if (item.Field == "DEPARTMENT_ID_WORKSHOP")
        //                {
        //                    DEPARTMENT_ID_WORKSHOP = new Guid(item.Value.ToString());
        //                }
        //                else if (item.Field == "DEPARTMENT_ID")
        //                {
        //                    DEPARTMENT_ID = new Guid(item.Value.ToString());
        //                }
        //            }
        //        }
        //        bool isMintype = false;//是否 区分生产单元
        //        if (pageFilter.FilterGroup.Groups != null && pageFilter.FilterGroup.Groups.Any())
        //        {
        //            var groups = pageFilter.FilterGroup.Groups.ToList();
        //            string MineType = string.Empty;
        //            if (groups[0].Rules != null && groups[0].Rules.Any())
        //            {
        //                FilterRule ruleType = null;
        //                foreach (var rule in groups[0].Rules)
        //                {
        //                    if (rule.Field == "MineType")
        //                    {
        //                        ruleType = rule;
        //                        MineType = rule.Value.ToString();
        //                        isMintype = true;
        //                        break;
        //                    }
        //                }

        //                if (!string.IsNullOrEmpty(MineType))
        //                {
        //                    listMineType = UserService.GetMineType(MineType);
        //                    if (ruleType != null)
        //                    {
        //                        groups[0].Rules.Remove(ruleType);
        //                        pageFilter.FilterGroup.Groups = groups;
        //                    }
        //                }
        //            }
        //        }
        //        if (isMintype)
        //        {
        //            return SafeGetPagedData(delegate (PagedActionResult<T_FM_USER> result)
        //            {
        //                Expression<Func<T_FM_USER, bool>> expression = e => e.ENABLE_STATUS == 0 && !e.CODE.StartsWith("admin");// && e.CODE != "admin";
        //                //linq 运行不通过  修改为查找后内存中过滤
        //                //expression = expression.And(e => e.Nav_ProdutionUnit == null || (e.Nav_ProdutionUnit.Any() && e.Nav_ProdutionUnit.Where(x => listMineType.Contains(x.Nav_Enums.VALUE)) != null));
        //                //PagedActionResult<T_FM_USER> orderPageEntities = GetOrderPageEntities<T_FM_USER>(expression, pageFilter, "Nav_ProdutionUnit.Nav_Enums");
        //                //result.Data = orderPageEntities.Data;
        //                //result.TotalCount = orderPageEntities.TotalCount;

        //                //var resultListUser = GetEntities<T_FM_USER>(expression, pageFilter, "Nav_ProdutionUnit.Nav_Enums");//这样不行
        //                pageFilter.SelectField.Add("Nav_ProdutionUnit.Nav_Enums");
        //                var resultListUser = GetEntities<T_FM_USER>(expression, pageFilter);

        //                if (resultListUser != null && resultListUser.Any())
        //                {
        //                    IEnumerable<T_FM_USER> resultListUserFilter = null;
        //                    if (listMineType.Count > 0)
        //                    {
        //                        if (listMineType.Count == 1)
        //                        {
        //                            resultListUserFilter = resultListUser.Where(e => e.Nav_ProdutionUnit == null || (e.Nav_ProdutionUnit.Any() && e.Nav_ProdutionUnit.Where(x => x.Nav_Enums != null && listMineType[0] == x.Nav_Enums.VALUE).Any()));// != null
        //                        }
        //                        else
        //                        {
        //                            resultListUserFilter = resultListUser.Where(e => e.Nav_ProdutionUnit == null || (e.Nav_ProdutionUnit.Any() && e.Nav_ProdutionUnit.Where(x => x.Nav_Enums != null && listMineType.Contains(x.Nav_Enums.VALUE)) != null));
        //                        }
        //                    }

        //                    if (resultListUserFilter != null && resultListUserFilter.Any())
        //                    {
        //                        result.Data = resultListUserFilter.Skip((pageFilter.PageIndex - 1) * pageFilter.Limit).Take(pageFilter.Limit);
        //                        result.TotalCount = resultListUserFilter.Count();
        //                    }
        //                }

        //                if (result.Data == null || !result.Data.Any())
        //                {
        //                    result.Data = new List<T_FM_USER>();
        //                    result.TotalCount = 0;
        //                }
        //            });

        //        }

        //        else if (DEPARTMENT_ID_WORKSHOP != Guid.Empty && DEPARTMENT_ID != Guid.Empty)
        //        {
        //            pageFilter.FilterGroup.Rules.Clear();
        //            return SafeGetPagedData(delegate (PagedActionResult<T_FM_USER> result)
        //            {
        //                PagedActionResult<T_FM_USER> orderPageEntities = GetOrderPageEntities<T_FM_USER>(e => e.ENABLE_STATUS == 0 && e.CODE != "admin" && e.DEPARTMENT_ID == DEPARTMENT_ID_WORKSHOP || (e.Nav_Department.PARENT_ID.HasValue && e.Nav_Department.PARENT_ID == DEPARTMENT_ID_WORKSHOP), pageFilter, new string[] { "Nav_Department" });
        //                result.Data = orderPageEntities.Data;
        //                result.TotalCount = orderPageEntities.TotalCount;
        //            });
        //        }
        //        else
        //        {
        //            return WitOrderPaged(e => e.ENABLE_STATUS == 0 && e.CODE != "admin", pageFilter);
        //        }

        //        #region    旧
        //        //List<string> listMineType = new List<string>();
        //        //List<string> listMineTypeTemp = new List<string>();
        //        //Guid DEPARTMENT_ID_WORKSHOP = Guid.Empty;
        //        //Guid DEPARTMENT_ID = Guid.Empty;
        //        //if (pageFilter.FilterGroup.Rules.Count == 2)
        //        //{
        //        //    foreach (var item in pageFilter.FilterGroup.Rules)
        //        //    {
        //        //        if (item.Field == "DEPARTMENT_ID_WORKSHOP")
        //        //        {
        //        //            DEPARTMENT_ID_WORKSHOP = new Guid(item.Value.ToString());
        //        //        }
        //        //        else if (item.Field == "DEPARTMENT_ID")
        //        //        {
        //        //            DEPARTMENT_ID = new Guid(item.Value.ToString());
        //        //        }
        //        //    }
        //        //}
        //        //if (pageFilter.FilterGroup.Groups != null && pageFilter.FilterGroup.Groups.Any())
        //        //{
        //        //    var groups = pageFilter.FilterGroup.Groups.ToList();
        //        //    string MineType = string.Empty;
        //        //    if (groups[0].Rules != null && groups[0].Rules.Any())
        //        //    {
        //        //        FilterRule ruleType = null;
        //        //        foreach (var rule in groups[0].Rules)
        //        //        {
        //        //            if (rule.Field == "MineType")
        //        //            {
        //        //                ruleType = rule;
        //        //                MineType = rule.Value.ToString();
        //        //                break;
        //        //            }
        //        //        }


        //        //        if (!string.IsNullOrEmpty(MineType))
        //        //        {
        //        //            if (ruleType != null)
        //        //            {
        //        //                groups[0].Rules.Remove(ruleType);
        //        //                pageFilter.FilterGroup.Groups = groups;
        //        //            }
        //        //            if (MineType.IndexOf(",") > -1)
        //        //            {
        //        //                listMineTypeTemp = MineType.Split(',', StringSplitOptions.RemoveEmptyEntries).Distinct().ToList();
        //        //                listMineType.AddRange(listMineTypeTemp);
        //        //            }

        //        //            string strTemp = string.Empty;
        //        //            if (!listMineType.Contains(MineType))
        //        //            {
        //        //                listMineType.Add(MineType);
        //        //                if (listMineTypeTemp.Count > 2)//多个生产单元  
        //        //                {
        //        //                    for (int i = 0; i < listMineTypeTemp.Count; i++)
        //        //                    {
        //        //                        for (int j = 0; j < listMineTypeTemp.Count; j++)
        //        //                        {
        //        //                            strTemp = listMineTypeTemp[i] + "," + listMineTypeTemp[j];
        //        //                            if (!listMineType.Contains(strTemp))
        //        //                            {
        //        //                                listMineType.Add(strTemp);
        //        //                            }
        //        //                        }
        //        //                    }
        //        //                }
        //        //                if (listMineTypeTemp.Count > 3)
        //        //                {
        //        //                    for (int i = 0; i < listMineTypeTemp.Count; i++)
        //        //                    {
        //        //                        for (int j = 0; j < listMineTypeTemp.Count; j++)
        //        //                        {
        //        //                            for (int k = 0; k < listMineTypeTemp.Count; k++)
        //        //                            {
        //        //                                strTemp = listMineTypeTemp[i] + "," + listMineTypeTemp[j] + "," + listMineTypeTemp[k];
        //        //                                if (!listMineType.Contains(strTemp))
        //        //                                {
        //        //                                    listMineType.Add(strTemp);
        //        //                                }
        //        //                            }
        //        //                        }
        //        //                    }
        //        //                }
        //        //            }
        //        //        }
        //        //    }
        //        //}

        //        //if (listMineType.Count > 0)
        //        //{
        //        //    return SafeGetPagedData(delegate (PagedActionResult<T_FM_USER> result)
        //        //    {
        //        //        Expression<Func<T_FM_USER, bool>> expression = e => e.ENABLE_STATUS == 0 && e.CODE != "admin";
        //        //        if (listMineType.Count == 1)
        //        //        {
        //        //            if (!string.IsNullOrEmpty(listMineType[0]))
        //        //            {
        //        //                expression = expression.And(e => e.MineType != null && e.MineType.Contains(listMineType[0]));
        //        //            }
        //        //        }
        //        //        else
        //        //        {
        //        //            expression = expression.And(e => e.MineType != null && listMineType.Contains(e.MineType));
        //        //        }
        //        //        PagedActionResult<T_FM_USER> orderPageEntities = GetOrderPageEntities<T_FM_USER>(expression, pageFilter, null);
        //        //        result.Data = orderPageEntities.Data;
        //        //        result.TotalCount = orderPageEntities.TotalCount;
        //        //    });
        //        //}
        //        //else if (DEPARTMENT_ID_WORKSHOP != Guid.Empty && DEPARTMENT_ID != Guid.Empty)
        //        //{
        //        //    pageFilter.FilterGroup.Rules.Clear();
        //        //    return SafeGetPagedData(delegate (PagedActionResult<T_FM_USER> result)
        //        //    {
        //        //        PagedActionResult<T_FM_USER> orderPageEntities = GetOrderPageEntities<T_FM_USER>(e => e.ENABLE_STATUS == 0 && e.CODE != "admin" && e.DEPARTMENT_ID == DEPARTMENT_ID_WORKSHOP || (e.Nav_Department.PARENT_ID.HasValue && e.Nav_Department.PARENT_ID == DEPARTMENT_ID_WORKSHOP), pageFilter, new string[] { "Nav_Department" });
        //        //        result.Data = orderPageEntities.Data;
        //        //        result.TotalCount = orderPageEntities.TotalCount;
        //        //    });
        //        //}
        //        //else
        //        //{
        //        //    return WitOrderPaged(e => e.ENABLE_STATUS == 0 && e.CODE != "admin", pageFilter);
        //        //} 
        //        #endregion
        //    }
        //    else
        //    {
        //        return WitOrderPaged(t => t.Nav_BelongRoles.Any(i => i.Nav_BelongRole.NAME.Contains(pageFilter.Parameter1)), pageFilter);
        //    }
        //}

        /// <summary>
        /// 正常使用的用户
        /// 重写  api/FM/User/OrderPaged
        /// 目前最多只支持4个生产单元有重叠的
        /// </summary>
        /// <param name="pageFilter"></param>
        /// <returns></returns>
        [HttpPost, Route("OrderPagedUseful")]
        public PagedActionResult<T_FM_USER> OrderPagedUseful([FromBody] KeywordPageFilter pageFilter)
        {
            if (string.IsNullOrEmpty(pageFilter.Parameter1))
            {
                List<int> listMineType = new List<int>();
                Guid DEPARTMENT_ID_WORKSHOP = Guid.Empty;
                Guid DEPARTMENT_ID = Guid.Empty;
                bool isMintype = false;//是否 区分生产单元
                string MineType = string.Empty;
                if (pageFilter.FilterGroup.Rules.Count == 2)
                {
                    foreach (var item in pageFilter.FilterGroup.Rules)
                    {
                        if (item.Field == "DEPARTMENT_ID_WORKSHOP")
                        {
                            DEPARTMENT_ID_WORKSHOP = new Guid(item.Value.ToString());
                        }
                        else if (item.Field == "DEPARTMENT_ID")
                        {
                            DEPARTMENT_ID = new Guid(item.Value.ToString());
                        }
                    }
                }
                else if (pageFilter.FilterGroup.Rules.Count == 1)
                {
                    foreach (var item in pageFilter.FilterGroup.Rules)
                    {
                        if (item.Field == "MineType")
                        {
                            isMintype = true;
                            MineType = item.Value.ToString();
                            pageFilter.FilterGroup.Rules.Clear();
                            break;
                        }
                    }

                    if (!string.IsNullOrEmpty(MineType))
                    {
                        listMineType = UserService.GetMineType(MineType);
                    }
                }
                if (pageFilter.FilterGroup.Groups != null && pageFilter.FilterGroup.Groups.Any())
                {
                    var groups = pageFilter.FilterGroup.Groups.ToList();
                    if (groups[0].Rules != null && groups[0].Rules.Any())
                    {
                        FilterRule ruleType = null;
                        foreach (var rule in groups[0].Rules)
                        {
                            if (rule.Field == "MineType")
                            {
                                ruleType = rule;
                                MineType = rule.Value.ToString();
                                isMintype = true;
                                break;
                            }
                        }

                        if (!string.IsNullOrEmpty(MineType))
                        {
                            listMineType = UserService.GetMineType(MineType);
                            if (ruleType != null)
                            {
                                groups[0].Rules.Remove(ruleType);
                                pageFilter.FilterGroup.Groups = groups;
                            }
                        }
                    }
                }


                var LoginDepID = APT.Infrastructure.Api.AppContext.CurrentSession.DepartmentID;
                T_FM_DEPARTMENT dep = null;
                if (LoginDepID.HasValue)
                {
                    dep = GetEntity<T_FM_DEPARTMENT>(LoginDepID.Value);
                }
                if (DataHelper.GetRequestType(HttpContext.Request.Headers) == 2 && LoginDepID.HasValue && dep != null && (dep.DEPARTMENT_TYPE == 1 || dep.DEPARTMENT_TYPE == 2))
                {
                    //班组或者车间 向下取值 否则取全部
                    //FMDepartmentType 部门=0,  车间=1,   班组=2,  公司=3
                    if (dep.DEPARTMENT_TYPE == 2)
                    {
                        return WitOrderPaged(e => e.ENABLE_STATUS == 0 && e.DEPARTMENT_ID.HasValue && e.DEPARTMENT_ID.Value == LoginDepID.Value && e.CODE != "admin", pageFilter);
                    }
                    else
                    {
                        var listDepID = GetEntities<T_FM_DEPARTMENT>(e => e.PARENT_ID.HasValue && e.PARENT_ID.Value == LoginDepID.Value, null).Select(e => e.ID);
                        if (listDepID != null && listDepID.Any())
                        {
                            var ListDepID = listDepID.ToList();
                            ListDepID.Add(LoginDepID.Value);

                            return WitOrderPaged(e => e.ENABLE_STATUS == 0 && e.DEPARTMENT_ID.HasValue && ListDepID.Contains(e.DEPARTMENT_ID.Value) && e.CODE != "admin", pageFilter);
                        }
                        else
                        {
                            return WitOrderPaged(e => e.ENABLE_STATUS == 0 && e.DEPARTMENT_ID.HasValue && e.DEPARTMENT_ID.Value == LoginDepID.Value && e.CODE != "admin", pageFilter);
                        }
                    }
                }
                else if (isMintype)
                {
                    return SafeGetPagedData(delegate (PagedActionResult<T_FM_USER> result)
                    {
                        Expression<Func<T_FM_USER, bool>> expression = e => e.ENABLE_STATUS == 0 && !e.CODE.StartsWith("admin");// && e.CODE != "admin";
                        //linq 运行不通过  修改为查找后内存中过滤
                        //expression = expression.And(e => e.Nav_ProdutionUnit == null || (e.Nav_ProdutionUnit.Any() && e.Nav_ProdutionUnit.Where(x => listMineType.Contains(x.Nav_Enums.VALUE)) != null));
                        //PagedActionResult<T_FM_USER> orderPageEntities = GetOrderPageEntities<T_FM_USER>(expression, pageFilter, "Nav_ProdutionUnit.Nav_Enums");
                        //result.Data = orderPageEntities.Data;
                        //result.TotalCount = orderPageEntities.TotalCount;

                        //var resultListUser = GetEntities<T_FM_USER>(expression, pageFilter, "Nav_ProdutionUnit.Nav_Enums");//这样不行
                        pageFilter.SelectField.Add("Nav_ProdutionUnit.Nav_Enums");
                        if (pageFilter.SelectField.Count() == 1)
                        {
                            pageFilter.SelectField.Add("ID");
                            pageFilter.SelectField.Add("NAME");
                            pageFilter.SelectField.Add("CODE");
                        }
                        var resultListUser = GetEntities<T_FM_USER>(expression, pageFilter);

                        if (resultListUser != null && resultListUser.Any())
                        {
                            IEnumerable<T_FM_USER> resultListUserFilter = null;
                            if (listMineType.Count > 0)
                            {
                                //if (listMineType.Count == 1)
                                //{
                                //    resultListUserFilter = resultListUser.Where(e => e.Nav_ProdutionUnit == null || (e.Nav_ProdutionUnit.Any() && e.Nav_ProdutionUnit.Where(x => x.Nav_Enums != null && listMineType[0] == x.Nav_Enums.VALUE).Any()));// != null
                                //}
                                //else
                                //{
                                resultListUserFilter = resultListUser.Where(e => e.Nav_ProdutionUnit == null || (e.Nav_ProdutionUnit.Any() && e.Nav_ProdutionUnit.Where(x => x.Nav_Enums != null && listMineType.Contains(x.Nav_Enums.VALUE)) != null));
                                //}
                            }

                            if (resultListUserFilter != null && resultListUserFilter.Any())
                            {
                                result.Data = resultListUserFilter.Skip((pageFilter.PageIndex - 1) * pageFilter.Limit).Take(pageFilter.Limit);
                                result.TotalCount = resultListUserFilter.Count();
                            }
                        }

                        if (result.Data == null || !result.Data.Any())
                        {
                            result.Data = new List<T_FM_USER>();
                            result.TotalCount = 0;
                        }
                    });
                }
                else if (DEPARTMENT_ID_WORKSHOP != Guid.Empty && DEPARTMENT_ID != Guid.Empty)
                {
                    pageFilter.FilterGroup.Rules.Clear();
                    return SafeGetPagedData(delegate (PagedActionResult<T_FM_USER> result)
                    {
                        PagedActionResult<T_FM_USER> orderPageEntities = GetOrderPageEntities<T_FM_USER>(e => e.ENABLE_STATUS == 0 && e.CODE != "admin" && e.DEPARTMENT_ID == DEPARTMENT_ID_WORKSHOP || (e.Nav_Department.PARENT_ID.HasValue && e.Nav_Department.PARENT_ID == DEPARTMENT_ID_WORKSHOP), pageFilter, new string[] { "Nav_Department" });
                        result.Data = orderPageEntities.Data;
                        result.TotalCount = orderPageEntities.TotalCount;
                    });
                }
                else
                {
                    if (DataHelper.GetRequestType(HttpContext.Request.Headers) == 2 && LoginDepID.HasValue)
                    {
                        //班组或者车间 向下取值 否则取全部
                        dep = GetEntity<T_FM_DEPARTMENT>(LoginDepID.Value);
                        if (dep != null && (dep.DEPARTMENT_TYPE == 1 || dep.DEPARTMENT_TYPE == 2))
                        {
                            //FMDepartmentType 部门=0,  车间=1,   班组=2,  公司=3
                            if (dep.DEPARTMENT_TYPE == 2)
                            {
                                return WitOrderPaged(e => e.ENABLE_STATUS == 0 && e.DEPARTMENT_ID.HasValue && e.DEPARTMENT_ID.Value == LoginDepID.Value && e.CODE != "admin", pageFilter);
                            }
                            else
                            {
                                var listDepID = GetEntities<T_FM_DEPARTMENT>(e => e.PARENT_ID.HasValue && e.PARENT_ID.Value == LoginDepID.Value, null).Select(e => e.ID);
                                if (listDepID != null && listDepID.Any())
                                {
                                    var ListDepID = listDepID.ToList();
                                    ListDepID.Add(LoginDepID.Value);

                                    return WitOrderPaged(e => e.ENABLE_STATUS == 0 && e.DEPARTMENT_ID.HasValue && ListDepID.Contains(e.DEPARTMENT_ID.Value) && e.CODE != "admin", pageFilter);
                                }
                                else
                                {
                                    return WitOrderPaged(e => e.ENABLE_STATUS == 0 && e.DEPARTMENT_ID.HasValue && e.DEPARTMENT_ID.Value == LoginDepID.Value && e.CODE != "admin", pageFilter);
                                }
                            }
                        }
                        else
                        {
                            return WitOrderPaged(e => e.ENABLE_STATUS == 0 && e.CODE != "admin", pageFilter);
                        }
                    }
                    else
                    {
                        return WitOrderPaged(e => e.ENABLE_STATUS == 0 && e.CODE != "admin", pageFilter);
                    }
                }

                #region    旧
                //List<string> listMineType = new List<string>();
                //List<string> listMineTypeTemp = new List<string>();
                //Guid DEPARTMENT_ID_WORKSHOP = Guid.Empty;
                //Guid DEPARTMENT_ID = Guid.Empty;
                //if (pageFilter.FilterGroup.Rules.Count == 2)
                //{
                //    foreach (var item in pageFilter.FilterGroup.Rules)
                //    {
                //        if (item.Field == "DEPARTMENT_ID_WORKSHOP")
                //        {
                //            DEPARTMENT_ID_WORKSHOP = new Guid(item.Value.ToString());
                //        }
                //        else if (item.Field == "DEPARTMENT_ID")
                //        {
                //            DEPARTMENT_ID = new Guid(item.Value.ToString());
                //        }
                //    }
                //}
                //if (pageFilter.FilterGroup.Groups != null && pageFilter.FilterGroup.Groups.Any())
                //{
                //    var groups = pageFilter.FilterGroup.Groups.ToList();
                //    string MineType = string.Empty;
                //    if (groups[0].Rules != null && groups[0].Rules.Any())
                //    {
                //        FilterRule ruleType = null;
                //        foreach (var rule in groups[0].Rules)
                //        {
                //            if (rule.Field == "MineType")
                //            {
                //                ruleType = rule;
                //                MineType = rule.Value.ToString();
                //                break;
                //            }
                //        }


                //        if (!string.IsNullOrEmpty(MineType))
                //        {
                //            if (ruleType != null)
                //            {
                //                groups[0].Rules.Remove(ruleType);
                //                pageFilter.FilterGroup.Groups = groups;
                //            }
                //            if (MineType.IndexOf(",") > -1)
                //            {
                //                listMineTypeTemp = MineType.Split(',', StringSplitOptions.RemoveEmptyEntries).Distinct().ToList();
                //                listMineType.AddRange(listMineTypeTemp);
                //            }

                //            string strTemp = string.Empty;
                //            if (!listMineType.Contains(MineType))
                //            {
                //                listMineType.Add(MineType);
                //                if (listMineTypeTemp.Count > 2)//多个生产单元  
                //                {
                //                    for (int i = 0; i < listMineTypeTemp.Count; i++)
                //                    {
                //                        for (int j = 0; j < listMineTypeTemp.Count; j++)
                //                        {
                //                            strTemp = listMineTypeTemp[i] + "," + listMineTypeTemp[j];
                //                            if (!listMineType.Contains(strTemp))
                //                            {
                //                                listMineType.Add(strTemp);
                //                            }
                //                        }
                //                    }
                //                }
                //                if (listMineTypeTemp.Count > 3)
                //                {
                //                    for (int i = 0; i < listMineTypeTemp.Count; i++)
                //                    {
                //                        for (int j = 0; j < listMineTypeTemp.Count; j++)
                //                        {
                //                            for (int k = 0; k < listMineTypeTemp.Count; k++)
                //                            {
                //                                strTemp = listMineTypeTemp[i] + "," + listMineTypeTemp[j] + "," + listMineTypeTemp[k];
                //                                if (!listMineType.Contains(strTemp))
                //                                {
                //                                    listMineType.Add(strTemp);
                //                                }
                //                            }
                //                        }
                //                    }
                //                }
                //            }
                //        }
                //    }
                //}

                //if (listMineType.Count > 0)
                //{
                //    return SafeGetPagedData(delegate (PagedActionResult<T_FM_USER> result)
                //    {
                //        Expression<Func<T_FM_USER, bool>> expression = e => e.ENABLE_STATUS == 0 && e.CODE != "admin";
                //        if (listMineType.Count == 1)
                //        {
                //            if (!string.IsNullOrEmpty(listMineType[0]))
                //            {
                //                expression = expression.And(e => e.MineType != null && e.MineType.Contains(listMineType[0]));
                //            }
                //        }
                //        else
                //        {
                //            expression = expression.And(e => e.MineType != null && listMineType.Contains(e.MineType));
                //        }
                //        PagedActionResult<T_FM_USER> orderPageEntities = GetOrderPageEntities<T_FM_USER>(expression, pageFilter, null);
                //        result.Data = orderPageEntities.Data;
                //        result.TotalCount = orderPageEntities.TotalCount;
                //    });
                //}
                //else if (DEPARTMENT_ID_WORKSHOP != Guid.Empty && DEPARTMENT_ID != Guid.Empty)
                //{
                //    pageFilter.FilterGroup.Rules.Clear();
                //    return SafeGetPagedData(delegate (PagedActionResult<T_FM_USER> result)
                //    {
                //        PagedActionResult<T_FM_USER> orderPageEntities = GetOrderPageEntities<T_FM_USER>(e => e.ENABLE_STATUS == 0 && e.CODE != "admin" && e.DEPARTMENT_ID == DEPARTMENT_ID_WORKSHOP || (e.Nav_Department.PARENT_ID.HasValue && e.Nav_Department.PARENT_ID == DEPARTMENT_ID_WORKSHOP), pageFilter, new string[] { "Nav_Department" });
                //        result.Data = orderPageEntities.Data;
                //        result.TotalCount = orderPageEntities.TotalCount;
                //    });
                //}
                //else
                //{
                //    return WitOrderPaged(e => e.ENABLE_STATUS == 0 && e.CODE != "admin", pageFilter);
                //} 
                #endregion
            }
            else
            {
                return WitOrderPaged(t => t.Nav_BelongRoles.Any(i => i.Nav_BelongRole.NAME.Contains(pageFilter.Parameter1)), pageFilter);
            }
        }

        /// <summary>
        /// 递归获取部门以及子部门的所有用户
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        [HttpPost, Route("GetDepUsersRecursion")]
        public JsonActionResult<IEnumerable<T_FM_USER>> GetDepUsersRecursion([FromBody] KeywordFilter filter)
        {
            return SafeExecute<IEnumerable<T_FM_USER>>(() =>
            {
                var baseFilter = new BaseFilter(filter.GetOrgId());
                baseFilter.SelectField = new string[] { "ID", "PARENT_ID" };
                var departments = this.GetEntities<T_FM_DEPARTMENT>(i => i.ENABLE_STATUS == (int)FMEnableStatusEnum.启用, baseFilter);
                var selectDepatmentIds = new List<Guid>();
                if (!string.IsNullOrEmpty(filter.Keyword))
                {
                    var parentid = Guid.Parse(filter.Keyword);
                    GetRecursionDepIds(departments, selectDepatmentIds, parentid);
                    return this.GetEntities<T_FM_USER>(i => selectDepatmentIds.Contains(i.DEPARTMENT_ID.Value), filter);
                }
                else
                {
                    return this.GetEntities<T_FM_USER>(null, filter);
                }
            });

        }

        private static void GetRecursionDepIds(IEnumerable<T_FM_DEPARTMENT> departments, List<Guid> selectDepatmentIds, Guid parentid)
        {
            selectDepatmentIds.Add(parentid);
            if (departments.Any(i => i.PARENT_ID == parentid))
            {
                var childIds = departments.Where(i => i.PARENT_ID == parentid).Select(i => i.ID);
                foreach (var childId in childIds)
                {
                    GetRecursionDepIds(departments, selectDepatmentIds, childId);
                }
            }
        }
    }
}