using Hotline.Repository.SqlSugar.DataPermissions;
using Hotline.Settings;
using Hotline.Share.Dtos.Menu;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using XF.Domain.Dependency;

namespace Hotline.Repository.SqlSugar.System
{
    public class SystemMenuRepository : BaseRepository<SystemMenu>, ISystemMenuRepository, IScopeDependency
    {
        public SystemMenuRepository(ISugarUnitOfWork<HotlineDbContext> uow, IDataPermissionFilterBuilder dataPermissionFilterBuilder, IServiceProvider serviceProvider) : base(uow, dataPermissionFilterBuilder, serviceProvider)
        {
        }

        public async Task<IReadOnlyList<SystemMenu>> GetSystemMenus()
        {
            var list = await Db.Queryable<SystemMenu>()
                .OrderBy(x => x.DisplayOrder)
                //.Includes(t => t.ButtonArr)
                .ToTreeAsync(x => x.children, it => it.ParentId, "");
            return list;
        }


        /// <summary>
        /// 获取可选快速入口（排除已有）
        /// </summary>
        /// <param name="roles"></param>
        /// <returns></returns>
        public async Task<IReadOnlyList<SystemMenu>> GetFastMenu(string[] roles, string userId,string name)
        {
            var list = await Db.Queryable<SystemAuthority>()
                .Where(x => roles.Contains(x.RoleCode)).ToListAsync();
            var codes = list.SelectMany(d => d.SystemMenuArr).Select(d => d.Code).Distinct();
            var model = await Db.Queryable<UserFastMenu>().FirstAsync(x => x.UserId == userId);
            var perList = new List<string>();
            if (model != null)
            {
                perList = codes.Except(model.FastMenuArr).ToList();
            }
            if (perList.Count > 0)
            {
                //过滤已存在的
                var menulist = await Db.Queryable<SystemMenu>()
               .Where(x => x.IsFast && perList.Contains(x.PermissionCode) && x.PageName.Contains(name) && x.MenuType == Share.Enums.Settings.EMenuType.Page && codes.Contains(x.PermissionCode)).ToListAsync();
                return menulist;
            }
            else
            {
                var menulist = await Db.Queryable<SystemMenu>()
                    .Where(x => (x.IsFast && x.PageName.Contains(name) && x.MenuType == Share.Enums.Settings.EMenuType.Page && codes.Contains(x.PermissionCode))).ToListAsync();
                return menulist;
            }

        }

        /// <summary>
        /// 获取我的快速入口
        /// </summary>
        /// <param name="roles"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public async Task<IReadOnlyList<SystemMenu>> GetMyFastMenu(string[] roles, string userId)
        {
            var list = await Db.Queryable<SystemAuthority>()
                .Where(x => roles.Contains(x.RoleCode)).ToListAsync();
            var codes = list.SelectMany(d => d.SystemMenuArr).Select(d => d.Code).Distinct();
            var model = await Db.Queryable<UserFastMenu>().FirstAsync(x => x.UserId == userId);
            if (model != null)
            {
                var perList = codes.Intersect(model.FastMenuArr).ToList();
                return await Db.Queryable<SystemMenu>().Where(x => perList.Contains(x.PermissionCode) && x.MenuType == Share.Enums.Settings.EMenuType.Page).ToListAsync();
            }
            return new List<SystemMenu>();
        }
    }
}
