using OpenAuth.Repository.Domain;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OpenAuth.App.BasicQueryService
{
    public class PositionManager
    {
        ISqlSugarClient client;
        public PositionManager(ISqlSugarClient client)
        {
            this.client = client;
        }

        private async Task<bool> HasRelation(string beginId, Dictionary<string, int> map)
        {
            bool res = false;
            var entity = await client.Queryable<SysPosition>().Where(a => a.Id.ToString() == beginId).FirstAsync();
            if (entity == null || entity.ParentId.ToString() == "0")
            {
                res = false;
            }
            else if (map.ContainsKey(entity.ParentId.ToString()))
            {
                res = true;
            }
            else
            {
                res = await HasRelation(entity.ParentId.ToString(), map);
            }
            return res;
        }

        /// <summary>
        /// 判断是否是上级
        /// </summary>
        /// <param name="myIds">自己的岗位</param>
        /// <param name="otherIds">对方的岗位</param>
        /// <returns></returns>
        public async Task<bool> IsUp(List<string> myIds, List<string> otherIds)
        {
            bool res = false;
            if (myIds.Count > 0 && otherIds.Count > 0)
            {
                Dictionary<string, int> map = new Dictionary<string, int>();
                foreach (var otherItem in myIds)
                {
                    if (!map.ContainsKey(otherItem))
                    {
                        map.Add(otherItem, 1);
                    }
                }
                foreach (var myItem in otherIds)
                {
                    if (await HasRelation(myItem, map))
                    {
                        res = true;
                        break;
                    }
                }
            }
            return res;
        }
        /// <summary>
        /// 判断是否是下级
        /// </summary>
        /// <param name="myIds">自己的岗位</param>
        /// <param name="otherIds">对方的岗位</param>
        /// <returns></returns>
        public async Task<bool> IsDown(List<string> myIds, List<string> otherIds)
        {
            bool res = false;
            if (myIds.Count > 0 && otherIds.Count > 0)
            {
                Dictionary<string, int> map = new Dictionary<string, int>();
                foreach (var myItem in myIds)
                {
                    if (!map.ContainsKey(myItem))
                    {
                        map.Add(myItem, 1);
                    }
                }
                foreach (var otherItem in otherIds)
                {
                    if (await HasRelation(otherItem, map))
                    {
                        res = true;
                        break;
                    }
                }
            }
            return res;
        }

        /// <summary>
        /// 获取上级岗位人员ID
        /// </summary>
        /// <param name="postIds">岗位</param>
        /// <param name="level">级数</param>
        /// <returns></returns>
        public async Task<IEnumerable<string>> GetUpIdList(List<string> postIds, int level)
        {
            List<string> res = new List<string>();
            if (postIds.Count > 0 && level > 0 && level < 6)
            {// 现在支持1-5级查找
                bool isHave = false; // 判断是否指定级数的职位
                foreach (var postId in postIds)
                {
                    isHave = false;
                    var entity = await client.Queryable<SysPosition>().Where(a => a.Id.ToString() == postId).FirstAsync();
                    if (entity != null)
                    {
                        string parentId = entity.ParentId.ToString();
                        SysPosition parentEntity = null;
                        for (int i = 0; i < level; i++)
                        {
                            parentEntity = await client.Queryable<SysPosition>().Where(a => a.Id.ToString() == parentId).FirstAsync();
                            if (parentEntity != null)
                            {
                                parentId = parentEntity.ParentId.ToString();
                                if (i == (level - 1))
                                {
                                    isHave = true;
                                }
                            }
                            else
                            {
                                break;
                            }
                        }
                        if (isHave)
                        {
                            if (parentEntity != null)
                            {
                                res.Add(parentEntity.Id.ToString());
                            }
                        }
                    }
                }
            }
            return res;
        }
        /// <summary>
        /// 获取下级岗位人员ID
        /// </summary>
        /// <param name="postIds">岗位</param>
        /// <param name="level">级数</param>
        /// <returns></returns>
        public async Task<IEnumerable<string>> GetDownIdList(List<string> postIds, int level)
        {
            List<string> res = new List<string>();
            if (postIds.Count > 0 && level > 0 && level < 6)
            {// 现在支持1-5级查找
                bool isHave = false; // 判断是否指定级数的职位
                List<string> parentList = new List<string>();
                parentList.AddRange(postIds);
                for (int i = 0; i < level; i++)
                {
                    parentList = (List<string>)await client.Queryable<SysPosition>().Where(a => parentList.Contains(a.ParentId.ToString())).Select(a => a.Id.ToString()).ToListAsync();
                    if (parentList.Count > 0)
                    {
                        if (i == (level - 1))
                        {
                            isHave = true;
                        }
                    }
                    else
                    {
                        break;
                    }
                }
                if (isHave)
                {
                    res.AddRange(parentList);
                }
            }
            return res;
        }



    }
}
