using Hotline.Repository.SqlSugar.DataPermissions;
using Hotline.SeedData;
using Hotline.Settings;
using Hotline.Share.Enums.Settings;
using SqlSugar;
using XF.Domain.Dependency;

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

        /// <summary>
        /// 获取当前用户所有菜单
        /// </summary>
        /// <param name="roles"></param>
        /// <returns></returns>
        public async Task<List<SystemMenu>> GetMyMenu(string[] roles)
        {
            if (roles.Contains(RoleSeedData.AdminRole))
            {
                return await Db.Queryable<SystemMenu>()
                    .Where(x => x.MenuType != EMenuType.Button)
                    .OrderBy(x => x.DisplayOrder)
                    .ToTreeAsync(x => x.children, it => it.ParentId, "");
            }
            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();
            return await Db.Queryable<SystemMenu>()
                .Where(x => codes.Contains(x.PermissionCode) && x.MenuType != EMenuType.Button)
                .OrderBy(x => x.DisplayOrder)
                .ToTreeAsync(x => x.children, it => it.ParentId, "");
        }

        /// <summary>
        /// 获取当前用户的所有功能点
        /// </summary>
        /// <param name="roles"></param>
        /// <returns></returns>
        public async Task<List<string>> GetMyButton(string[] roles)
        {
            if (roles.Contains(RoleSeedData.AdminRole))
            {
                return await Db.Queryable<SystemMenu>().Select(x => x.ForePermissionCode).ToListAsync();
            }
            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();
            return await Db.Queryable<SystemMenu>()
                .Where(x => codes.Contains(x.PermissionCode))
                .Select(x => x.ForePermissionCode)
                .ToListAsync();
        }

        /// <summary>
        /// 获取当前用户所有功能点（包含菜单和按钮）
        /// </summary>
        /// <param name="roles"></param>
        /// <returns></returns>
        public IReadOnlyList<string> GetPermission(List<string> roles)
        {
            var list = Db.Queryable<SystemAuthority>()
                .Where(x => roles.Contains(x.RoleCode)).ToList();
            var codes = list.SelectMany(d => d.SystemMenuArr).Select(d => d.Code).Distinct();
            return codes.ToList();
        }

    }
}
