using YBEE.EQM.Core;

namespace YBEE.EQM.Application
{
    /// <summary>
    /// 角色菜单服务
    /// </summary>
    public class SysRoleMenuService : ISysRoleMenuService, ITransient
    {
        private readonly IRepository<SysRoleMenu> _roleMenuRep;
        private readonly IRepository<SysMenu> _menuRep;
        private readonly ICacheService _cacheService;
        public SysRoleMenuService(IRepository<SysRoleMenu> roleMenuRep, IRepository<SysMenu> menuRep, ICacheService cacheService)
        {
            _roleMenuRep = roleMenuRep;
            _menuRep = menuRep;
            _cacheService = cacheService;
        }

        /// <summary>
        /// 授权角色菜单
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task GrantRoleMenu(GrantSysRoleMenuInput input)
        {
            var roleMenus = await _roleMenuRep.DetachedEntities.Where(u => u.SysRoleId == input.RoleId).ToListAsync();
            await _roleMenuRep.DeleteAsync(roleMenus);

            // 确保对按钮菜单的父级菜单能正确的授权，发现前端构造的数据会丢弃了按钮菜单的父级菜单
            var roleMenuPidsList = await _menuRep.DetachedEntities.Where(u => input.RoleMenuScopeList.Select(t => t.MenuId).Contains(u.Id))
                                                                        .Select(u => u.Pids).ToListAsync();
            var roleMenuSplitPids = roleMenuPidsList
                .SelectMany(u => u.Split(',', StringSplitOptions.RemoveEmptyEntries)
                .Select(v => int.Parse(v[1..^1]))
                .Where(v => v != 0))
                .Union(input.RoleMenuScopeList.Select(t => t.MenuId));

            var menus = roleMenuSplitPids.Select(u => new SysRoleMenu
            {
                SysRoleId = input.RoleId,
                SysMenuId = u,
                DataScopeType = input.RoleMenuScopeList.FirstOrDefault(t => t.MenuId == u)?.DataScopeType ?? DataScopeType.SELF
            }).ToList();
            await _roleMenuRep.InsertAsync(menus);

            // 清除缓存
            await _cacheService.DelByPatternAsync(CommonConst.CACHE_KEY_MENU);
            await _cacheService.DelByPatternAsync(CommonConst.CACHE_KEY_PERMISSION);
        }
        /// <summary>
        /// 根据菜单Id集合删除对应的角色-菜单表信息
        /// </summary>
        /// <param name="menuIdList"></param>
        /// <returns></returns>
        public async Task DeleteRoleMenuListByMenuIdList(List<int> menuIdList)
        {
            var roleMenus = await _roleMenuRep.DetachedEntities.Where(u => menuIdList.Contains(u.SysMenuId)).ToListAsync();
            await _roleMenuRep.DeleteAsync(roleMenus);
        }
        /// <summary>
        /// 根据角色Id删除对应的角色-菜单表关联信息
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        public async Task DeleteRoleMenuListByRoleId(int roleId)
        {
            var roleMenus = await _roleMenuRep.DetachedEntities.Where(u => u.SysRoleId == roleId).ToListAsync();
            await _roleMenuRep.DeleteAsync(roleMenus);
        }
        /// <summary>
        /// 获取角色的菜单Id集合
        /// </summary>
        /// <param name="roleIdList"></param>
        /// <returns></returns>
        public async Task<List<int>> GetRoleMenuIdList(List<int> roleIdList)
        {
            return await _roleMenuRep.DetachedEntities
                                        .Where(u => roleIdList.Contains(u.SysRoleId))
                                        .Select(u => u.SysMenuId).ToListAsync();
        }
        /// <summary>
        /// 获取角色对象菜单列表
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        public async Task<List<SysRoleMenuOutput>> GetRoleMenuList(int roleId)
        {
            return await _roleMenuRep.DetachedEntities.Include(t => t.SysMenu).Where(t => t.SysRoleId == roleId).ProjectToType<SysRoleMenuOutput>().ToListAsync();
        }
        /// <summary>
        /// 获取指定权限标识的角色ID列表
        /// </summary>
        /// <param name="permissionCode"></param>
        /// <returns></returns>
        public async Task<List<int>> GetRoleIdListByMenuPermissionCode(string permissionCode)
        {
            return await _roleMenuRep.DetachedEntities.Where(t => t.SysMenu.Permission == permissionCode).Select(t => t.SysRoleId).Distinct().ToListAsync();
        }
    }
}