using SqlSugar; 
 | 
using System; 
 | 
using System.Collections.Concurrent; 
 | 
using System.Collections.Generic; 
 | 
using System.Linq; 
 | 
using System.Text; 
 | 
using System.Threading.Tasks; 
 | 
using WIDESEA_Core; 
 | 
using WIDESEA_Core.BaseRepository; 
 | 
using WIDESEA_Core.BaseServices; 
 | 
using WIDESEA_Core.Helper; 
 | 
using WIDESEA_DTO.System; 
 | 
using WIDESEA_IRepository; 
 | 
using WIDESEA_IServices; 
 | 
using WIDESEA_Model; 
 | 
using WIDESEA_Model.Models; 
 | 
  
 | 
namespace WIDESEA_Services 
 | 
{ 
 | 
    public class Sys_MenuService : ServiceBase<Sys_Menu, ISys_MenuRepository>, ISys_MenuService 
 | 
    { 
 | 
        private readonly IUnitOfWorkManage _unitOfWorkManage; 
 | 
  
 | 
        public Sys_MenuService(ISys_MenuRepository BaseDal, IUnitOfWorkManage unitOfWorkManage) : base(BaseDal) 
 | 
        { 
 | 
            _unitOfWorkManage = unitOfWorkManage; 
 | 
        } 
 | 
  
 | 
        /// <summary> 
 | 
        /// 获取当前用户所有菜单与权限 
 | 
        /// </summary> 
 | 
        /// <returns></returns> 
 | 
        public object GetCurrentMenuActionList() 
 | 
        { 
 | 
            object obj = GetMenuActionList(App.User.RoleId); 
 | 
            if (obj is IEnumerable<object> list) 
 | 
            { 
 | 
                if (list.Any()) 
 | 
                { 
 | 
                    return obj; 
 | 
                } 
 | 
                else 
 | 
                { 
 | 
                    return WebResponseContent.Instance.Error("未获取到菜单信息"); 
 | 
                } 
 | 
            } 
 | 
            return obj; 
 | 
        } 
 | 
  
 | 
        public object GetCurrentMenuPhoneActionList() 
 | 
        { 
 | 
            object obj = GetMenuActionPhoneList(App.User.RoleId); 
 | 
            if (obj is IEnumerable<object> list) 
 | 
            { 
 | 
                if (list.Any()) 
 | 
                { 
 | 
                    return obj; 
 | 
                } 
 | 
                else 
 | 
                { 
 | 
                    return WebResponseContent.Instance.Error("未获取到菜单信息"); 
 | 
                } 
 | 
            } 
 | 
            return obj; 
 | 
        } 
 | 
  
 | 
        /// <summary> 
 | 
        /// 根据角色ID获取菜单与权限 
 | 
        /// </summary> 
 | 
        /// <param name="roleId"></param> 
 | 
        /// <returns></returns> 
 | 
        public object GetMenuActionList(int roleId) 
 | 
        { 
 | 
            return BaseDal.GetMenuByRoleId(roleId); 
 | 
        } 
 | 
  
 | 
        /// <summary> 
 | 
        /// 根据角色ID获取菜单与权限 
 | 
        /// </summary> 
 | 
        /// <param name="roleId"></param> 
 | 
        /// <returns></returns> 
 | 
        public object GetMenuActionPhoneList(int roleId) 
 | 
        { 
 | 
            var allMenu = BaseDal.QueryData(x => (x.Enable == 1 || x.Enable == 2) && x.MenuType == 1 && x.ParentId > 0).OrderByDescending(a => a.OrderNo).ThenByDescending(q => q.ParentId).ToList(); 
 | 
  
 | 
            var menu = allMenu.Select(x => new 
 | 
            { 
 | 
                text = x.MenuName, 
 | 
                url = x.Url, 
 | 
                icon = x.Icon, 
 | 
            }); 
 | 
            return menu.ToList(); 
 | 
        } 
 | 
  
 | 
        public List<MenuDTO> GetUserMenuList(int roleId) 
 | 
        { 
 | 
            if (App.User.IsRoleIdSuperAdmin(roleId)) 
 | 
            { 
 | 
                return BaseDal.GetAllMenu(); 
 | 
            } 
 | 
            List<int> menuIds = BaseDal.GetPermissions(roleId).Select(x => x.MenuId).ToList(); 
 | 
            return BaseDal.GetAllMenu().Where(x => menuIds.Contains(x.MenuId)).ToList(); 
 | 
        } 
 | 
  
 | 
        public List<ActionDTO> GetActions(int menuId, List<ActionDTO> menuActions, List<Permissions> permissions, int roleId) 
 | 
        { 
 | 
            if (App.User.IsRoleIdSuperAdmin(roleId)) 
 | 
            { 
 | 
                return menuActions; 
 | 
            } 
 | 
  
 | 
            return menuActions.Where(p => permissions 
 | 
                 .Exists(w => menuId == w.MenuId 
 | 
                 && w.UserAuthArr.Contains(p.Value))) 
 | 
                  .ToList(); 
 | 
        } 
 | 
  
 | 
        /// <summary> 
 | 
        /// 编辑修改菜单时,获取所有菜单 
 | 
        /// </summary> 
 | 
        /// <returns></returns> 
 | 
        public object GetMenu() 
 | 
        { 
 | 
            if (App.User.IsRoleIdSuperAdmin(App.User.RoleId)) 
 | 
            { 
 | 
                List<int> menuIds = BaseDal.QueryData().Select(x => x.MenuId).ToList(); 
 | 
                return BaseDal.GetMenu(menuIds); 
 | 
            } 
 | 
            else 
 | 
            { 
 | 
                List<int> menuIds = BaseDal.GetPermissions(App.User.RoleId).Select(x => x.MenuId).ToList(); 
 | 
                return BaseDal.GetMenu(menuIds); 
 | 
            } 
 | 
        } 
 | 
  
 | 
        /// <summary> 
 | 
        /// 编辑菜单时,获取菜单信息 
 | 
        /// </summary> 
 | 
        /// <param name="menuId"></param> 
 | 
        /// <returns></returns> 
 | 
        public object GetTreeItem(int menuId) 
 | 
        { 
 | 
            return BaseDal.GetTreeItem(menuId); 
 | 
        } 
 | 
  
 | 
        /// <summary> 
 | 
        /// 新建或编辑菜单 
 | 
        /// </summary> 
 | 
        /// <param name="menu"></param> 
 | 
        /// <returns></returns> 
 | 
        public WebResponseContent Save(Sys_Menu menu) 
 | 
        { 
 | 
            WebResponseContent webResponse = new WebResponseContent(); 
 | 
            if (menu == null) return webResponse.Error("没有获取到提交的参数"); 
 | 
            if (menu.MenuId > 0 && menu.MenuId == menu.ParentId) return webResponse.Error("父级ID不能是当前菜单的ID"); 
 | 
            try 
 | 
            { 
 | 
                //webResponse = menu.ValidationEntity(x => new { x.MenuName, x.TableName }); 
 | 
                //if (!webResponse.Status) return webResponse; 
 | 
                if (menu.TableName != "/" && menu.TableName != ".") 
 | 
                { 
 | 
                    // 2022.03.26增移动端加菜单类型判断 
 | 
                    Sys_Menu sysMenu = BaseDal.QueryFirst(x => x.TableName == menu.TableName); 
 | 
                    if (sysMenu != null) 
 | 
                    { 
 | 
                        sysMenu.MenuType ??= 0; 
 | 
                        if (sysMenu.MenuType == menu.MenuType) 
 | 
                        { 
 | 
                            if ((menu.MenuId > 0 && sysMenu.MenuId != menu.MenuId) 
 | 
                            || menu.MenuId <= 0) 
 | 
                            { 
 | 
                                return webResponse.Error($"视图/表名【{menu.TableName}】已被其他菜单使用"); 
 | 
                            } 
 | 
                        } 
 | 
                    } 
 | 
                } 
 | 
                bool _changed = false; 
 | 
                if (menu.MenuId <= 0) 
 | 
                { 
 | 
                    BaseDal.AddData(menu); 
 | 
                } 
 | 
                else 
 | 
                { 
 | 
                    //2020.05.07新增禁止选择上级角色为自己 
 | 
                    if (menu.MenuId == menu.ParentId) 
 | 
                    { 
 | 
                        return webResponse.Error($"父级id不能为自己"); 
 | 
                    } 
 | 
                    if (BaseDal.QueryFirst(x => x.ParentId == menu.MenuId && menu.ParentId == x.MenuId) != null) 
 | 
                    { 
 | 
                        return webResponse.Error($"不能选择此父级id,选择的父级id与当前菜单形成依赖关系"); 
 | 
                    } 
 | 
  
 | 
                    _changed = BaseDal.QueryData(c => c.MenuId == menu.MenuId).Select(s => s.Auth).FirstOrDefault() != menu.Auth; 
 | 
  
 | 
                    BaseDal.UpdateData(menu); 
 | 
                } 
 | 
                webResponse.OK("保存成功", menu); 
 | 
            } 
 | 
            catch (Exception ex) 
 | 
            { 
 | 
                webResponse.Error(ex.Message); 
 | 
            } 
 | 
            return webResponse; 
 | 
        } 
 | 
    } 
 | 
} 
 |