From c020f31a67fc5aa5644511bddff075f7ecc85234 Mon Sep 17 00:00:00 2001
From: qinchulong <qinchulong@hnkhzn.com>
Date: 星期二, 27 五月 2025 15:35:27 +0800
Subject: [PATCH] Merge branch 'master' of http://115.159.85.185:8098/r/HuaYiZhongHeng/ZhongHeLiTiKu

---
 代码管理/WMS/WIDESEA_WMSServer/WIDESEA_SystemService/Sys_RoleService.cs |  260 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 260 insertions(+), 0 deletions(-)

diff --git "a/\344\273\243\347\240\201\347\256\241\347\220\206/WMS/WIDESEA_WMSServer/WIDESEA_SystemService/Sys_RoleService.cs" "b/\344\273\243\347\240\201\347\256\241\347\220\206/WMS/WIDESEA_WMSServer/WIDESEA_SystemService/Sys_RoleService.cs"
new file mode 100644
index 0000000..030a5ea
--- /dev/null
+++ "b/\344\273\243\347\240\201\347\256\241\347\220\206/WMS/WIDESEA_WMSServer/WIDESEA_SystemService/Sys_RoleService.cs"
@@ -0,0 +1,260 @@
+锘縰sing Microsoft.AspNetCore.DataProtection.KeyManagement;
+using SqlSugar;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using WIDESEA_DTO.System;
+using WIDESEA_Core;
+using WIDESEA_Core.BaseRepository;
+using WIDESEA_Core.BaseServices;
+using WIDESEA_Core.HttpContextUser;
+using WIDESEA_ISystemRepository;
+using WIDESEA_ISystemService;
+using WIDESEA_Model.Models;
+using WIDESEA_Model.Models.System;
+using WIDESEA_SystemRepository;
+
+namespace WIDESEA_SystemService
+{
+    public class Sys_RoleService : ServiceBase<Sys_Role, ISys_RoleRepository>, ISys_RoleService
+    {
+        private readonly IUnitOfWorkManage _unitOfWorkManage;
+        private readonly ISys_MenuRepository _MenuRepository;
+        private readonly ISys_MenuService _MenuService;
+        private readonly ISys_RoleAuthRepository _RoleAuthRepository;
+
+        public ISys_RoleRepository Repository => BaseDal;
+
+        public Sys_RoleService(ISys_RoleRepository BaseDal, ISys_MenuRepository MenuRepository, ISys_MenuService MenuService, ISys_RoleAuthRepository roleAuthRepository, IUnitOfWorkManage unitOfWorkManage) : base(BaseDal)
+        {
+            _unitOfWorkManage = unitOfWorkManage;
+            _MenuRepository = MenuRepository;
+            _MenuService = MenuService;
+            _RoleAuthRepository = roleAuthRepository;
+        }
+
+        public List<RoleNodes> GetAllChildren(int roleId)
+        {
+            if (roleId <= 0) return new List<RoleNodes>() { };
+            var roles = GetAllRoleId();
+            if (App.User.IsRoleIdSuperAdmin(roleId)) return roles;
+
+            var list = GetChildren(roles, roleId);
+            return list;
+        }
+
+        public  List<RoleNodes> GetAllRoleId()
+        {
+            List<RoleNodes> roles = BaseDal.QueryData().Select(s => new RoleNodes() { Id = s.RoleId, ParentId = s.ParentId, RoleName = s.RoleName }).ToList();
+
+            return roles;
+        }
+
+        /// <summary>
+        /// 鑾峰彇鎵�鏈夊瓙鑺傜偣
+        /// </summary>
+        /// <param name="roleId"></param>
+        private List<RoleNodes> GetChildren(List<RoleNodes> roles, int roleId)
+        {
+            List<RoleNodes> rolesChildren = roles.Where(x => x.Id == roleId).Distinct().ToList();
+
+            for (int i = 0; i < rolesChildren.Count; i++)
+            {
+                RoleNodes node = rolesChildren[i];
+                var children = roles.Where(x => x.ParentId == node.Id && !rolesChildren.Any(c => c.Id == x.Id)).Distinct().ToList();
+                rolesChildren.AddRange(children);
+            }
+            return rolesChildren;
+        }
+
+        /// <summary>
+        /// 缂栬緫鏉冮檺鏃惰幏鍙栧綋鍓嶇敤鎴蜂笅鐨勬墍鏈夎鑹蹭笌褰撳墠鐢ㄦ埛鐨勮彍鍗曟潈闄�
+        /// </summary>
+        /// <returns></returns>
+        public WebResponseContent GetCurrentTreePermission()
+        {
+            WebResponseContent content = GetCurrentUserTreePermission();
+            int roleId = App.User.RoleId;
+            return WebResponseContent.Instance.OK(null, new
+            {
+                tree = content.Data,
+                roles = GetAllChildren(roleId)
+            });
+        }
+
+        /// <summary>
+        /// 缂栬緫鏉冮檺鏃讹紝鑾峰彇褰撳墠鐢ㄦ埛鐨勬墍鏈夎彍鍗曟潈闄�
+        /// </summary>
+        /// <returns></returns>
+        public WebResponseContent GetCurrentUserTreePermission()
+        {
+            return GetUserTreePermission(App.User.RoleId);
+        }
+
+        /// <summary>
+        /// 缂栬緫鏉冮檺鏃讹紝鑾峰彇鎸囧畾瑙掕壊鐨勬墍鏈夎彍鍗曟潈闄�
+        /// </summary>
+        /// <param name="roleId"></param>
+        /// <returns></returns>
+        public WebResponseContent GetUserTreePermission(int roleId)
+        {
+            if (!App.User.IsRoleIdSuperAdmin(roleId) && App.User.RoleId != roleId)
+            {
+                if (!(GetAllChildren(App.User.RoleId)).Exists(x => x.Id == roleId))
+                {
+                    return WebResponseContent.Instance.Error("娌℃湁鏉冮檺鑾峰彇姝よ鑹茬殑鏉冮檺淇℃伅");
+                }
+            }
+            //鑾峰彇鐢ㄦ埛鏉冮檺
+            List<Permissions> permissions = _MenuRepository.GetPermissions(roleId);
+            //鏉冮檺鐢ㄦ埛鏉冮檺鏌ヨ鎵�鏈夌殑鑿滃崟淇℃伅
+            List<MenuDTO> menus =  _MenuService.GetUserMenuList(roleId);
+            //鑾峰彇褰撳墠鐢ㄦ埛鏉冮檺濡�:(Add,Search)瀵瑰簲鐨勬樉绀烘枃鏈俊鎭:Add锛氭坊鍔狅紝Search:鏌ヨ
+            var data = menus.Select(x => new
+            {
+                Id = x.MenuId,
+                Pid = x.ParentId,
+                Text = x.MenuName,
+                IsApp = x.MenuType == 1,
+                Actions = _MenuService.GetActions(x.MenuId, x.Actions, permissions, roleId)
+            });
+            return WebResponseContent.Instance.OK(null, data);
+        }
+        public WebResponseContent GetCurrentTreePermissionPDA()
+        {
+            WebResponseContent content = GetCurrentUserTreePermissionPDA();
+            int roleId = App.User.RoleId;
+            return WebResponseContent.Instance.OK(null, new
+            {
+                tree = content.Data,
+                roles = GetAllChildren(roleId)
+            });
+        }
+
+        /// <summary>
+        /// 缂栬緫鏉冮檺鏃讹紝鑾峰彇褰撳墠鐢ㄦ埛鐨勬墍鏈夎彍鍗曟潈闄�
+        /// </summary>
+        /// <returns></returns>
+        public WebResponseContent GetCurrentUserTreePermissionPDA()
+        {
+            return GetUserTreePermissionPDA(App.User.RoleId);
+        }
+        public WebResponseContent GetUserTreePermissionPDA(int roleId)
+        {
+            if (!App.User.IsRoleIdSuperAdmin(roleId) && App.User.RoleId != roleId)
+            {
+                if (!(GetAllChildren(App.User.RoleId)).Exists(x => x.Id == roleId))
+                {
+                    return WebResponseContent.Instance.Error("娌℃湁鏉冮檺鑾峰彇姝よ鑹茬殑鏉冮檺淇℃伅");
+                }
+            }
+            //鑾峰彇鐢ㄦ埛鏉冮檺
+            List<Permissions> permissions = _MenuRepository.GetPermissions(roleId);
+            //鏉冮檺鐢ㄦ埛鏉冮檺鏌ヨ鎵�鏈夌殑鑿滃崟淇℃伅
+            List<MenuDTO> menus = _MenuService.GetUserMenuListPDA(roleId);
+
+            //鑾峰彇褰撳墠鐢ㄦ埛鏉冮檺濡�:(Add,Search)瀵瑰簲鐨勬樉绀烘枃鏈俊鎭:Add锛氭坊鍔狅紝Search:鏌ヨ
+            var data = menus.Where(x => x.MenuType==1).Select(x => new
+            {
+                Id = x.MenuId,
+                Pid = x.ParentId,
+                Text = x.MenuName,
+                IsApp = x.MenuType == 1,
+                Actions = _MenuService.GetActions(x.MenuId, x.Actions, permissions, roleId)
+            });
+            return WebResponseContent.Instance.OK(null, data);
+        }
+
+        /// <summary>
+        /// 淇濆瓨瑙掕壊鏉冮檺
+        /// </summary>
+        /// <param name="userPermissions"></param>
+        /// <param name="roleId"></param>
+        /// <returns></returns>
+        public WebResponseContent SavePermission(List<UserPermissionDTO> userPermissions, int roleId)
+        {
+            WebResponseContent content = new WebResponseContent();
+            string message = "";
+            try
+            {
+                if (!GetAllChildren(App.User.RoleId).Exists(x => x.Id == roleId))
+                    return WebResponseContent.Instance.Error("娌℃湁鏉冮檺淇敼姝よ鑹茬殑鏉冮檺淇℃伅");
+                //褰撳墠鐢ㄦ埛鐨勬潈闄�
+                List<Permissions> permissions = _MenuRepository.GetPermissions(App.User.RoleId);
+
+                List<int> originalMeunIds = new List<int>();
+                //琚垎閰嶈鑹茬殑鏉冮檺
+                List<Sys_RoleAuth> roleAuths = _RoleAuthRepository.QueryData(x => x.RoleId == roleId);
+                List<Sys_RoleAuth> updateAuths = new List<Sys_RoleAuth>();
+                foreach (UserPermissionDTO x in userPermissions)
+                {
+                    Permissions per = permissions.FirstOrDefault(p => p.MenuId == x.Id);
+                    //涓嶈兘鍒嗛厤瓒呰繃褰撳墠鐢ㄦ埛鐨勬潈闄�
+                    if (per == null) continue;
+                    //per.UserAuthArr.Contains(a.Value)鏍¢獙鏉冮檺鑼冨洿
+                    string[] arr = x.Actions == null || x.Actions.Count == 0
+                      ? new string[0]
+                      : x.Actions.Where(a => per.UserAuthArr.Contains(a.Value))
+                      .Select(s => s.Value).ToArray();
+
+                    //濡傛灉褰撳墠鏉冮檺娌℃湁鍒嗛厤杩囷紝璁剧疆Auth_Id榛樿涓�0锛岃〃绀烘柊澧炵殑鏉冮檺
+                    var auth = roleAuths.Where(r => r.MenuId == x.Id).Select(s => new { s.AuthId, s.AuthValue, s.MenuId }).FirstOrDefault();
+                    string newAuthValue = string.Join(",", arr);
+                    //鏉冮檺娌℃湁鍙戠敓鍙樺寲鍒欎笉澶勭悊
+                    if (auth == null || auth.AuthValue != newAuthValue)
+                    {
+                        updateAuths.Add(new Sys_RoleAuth()
+                        {
+                            RoleId = roleId,
+                            MenuId = x.Id,
+                            AuthValue = string.Join(",", arr),
+                            AuthId = auth == null ? 0 : auth.AuthId,
+                            ModifyDate = DateTime.Now,
+                            Modifier = App.User.UserName,
+                            CreateDate = DateTime.Now,
+                            Creater = App.User.UserName
+                        });
+                    }
+                    else
+                    {
+                        originalMeunIds.Add(auth.MenuId);
+                    }
+
+                }
+                //鏇存柊鏉冮檺
+                _RoleAuthRepository.UpdateData(updateAuths);
+                //鏂板鐨勬潈闄�
+                _RoleAuthRepository.AddData(updateAuths);
+
+                //鑾峰彇鏉冮檺鍙栨秷鐨勬潈闄�
+                int[] authIds = roleAuths.Where(x => userPermissions.Select(u => u.Id)
+                 .ToList().Contains(x.MenuId) || originalMeunIds.Contains(x.MenuId))
+                .Select(s => s.AuthId)
+                .ToArray();
+                List<Sys_RoleAuth> delAuths = roleAuths.Where(x => x.AuthValue != "" && !authIds.Contains(x.AuthId)).ToList();
+                delAuths.ForEach(x =>
+                {
+                    x.AuthValue = "";
+                });
+                //灏嗗彇娑堢殑鏉冮檺璁剧疆涓�""
+                _RoleAuthRepository.UpdateData(delAuths);
+
+                int addCount = updateAuths.Where(x => x.AuthId <= 0).Count();
+                int updateCount = updateAuths.Where(x => x.AuthId > 0).Count();
+                
+                string _version = DateTime.Now.ToString("yyyyMMddHHMMssfff");
+
+
+                content.OK($"淇濆瓨鎴愬姛锛氭柊澧炲姞閰嶈彍鍗曟潈闄恵addCount}鏉�,鏇存柊鑿滃崟{updateCount}鏉�,鍒犻櫎鏉冮檺{delAuths.Count}鏉�");
+            }
+            catch (Exception ex)
+            {
+                message = "寮傚父淇℃伅锛�" + ex.Message + ex.StackTrace + ",";
+            }
+
+            return content;
+        }
+    }
+}

--
Gitblit v1.9.3