From a7ceaaa38a4394b82501ca60230e97d25a6871e3 Mon Sep 17 00:00:00 2001
From: wangxinhui <wangxinhui@hnkhzn.com>
Date: 星期六, 18 一月 2025 17:30:33 +0800
Subject: [PATCH] 1

---
 代码管理/WMS/WIDESEA_WMSServer/WIDESEA_SystemService/Sys_RoleService.cs |  113 ++++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 109 insertions(+), 4 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"
index ff749a1..3a58e39 100644
--- "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"
@@ -16,6 +16,7 @@
 using WIDESEA_Model.Models.System;
 using WIDESEA_SystemRepository;
 using WIDESEA_Core.Helper;
+using WIDESEA_Core.HostedService;
 
 namespace WIDESEA_SystemService
 {
@@ -38,7 +39,7 @@
 
         public override WebResponseContent AddData(SaveModel saveModel)
         {
-            string authorityScope = saveModel.MainData["authorityScope"].ToString();
+            string authorityScope = saveModel.MainData["authorityScope"].ToString() ?? "";
             Sys_Role role = saveModel.MainData.DicToModel<Sys_Role>();
             if (BaseDal.QueryFirst(x => x.RoleName == role.RoleName) != null)
             {
@@ -71,12 +72,14 @@
                 BaseDal.Db.Insertable(roleDataPermissions).ExecuteCommand();
             }
 
+            PermissionDataHostService.UserRoles = PermissionDataHostService.GetUserRoles(Db);
+
             return WebResponseContent.Instance.OK();
         }
 
         public override WebResponseContent UpdateData(SaveModel saveModel)
         {
-            string authorityScope = saveModel.MainData["authorityScope"].ToString();
+            string authorityScope = saveModel.MainData["authorityScope"].ToString() ?? "";
             Sys_Role role = saveModel.MainData.DicToModel<Sys_Role>();
             if (BaseDal.QueryFirst(x => x.RoleId == role.RoleId) == null)
             {
@@ -116,7 +119,14 @@
             BaseDal.Db.Insertable(roleDataPermissions).ExecuteCommand();
             BaseDal.Db.Deleteable(oldDatas).ExecuteCommand();
 
+            PermissionDataHostService.UserRoles = PermissionDataHostService.GetUserRoles(Db);
+
             return WebResponseContent.Instance.OK();
+        }
+
+        public List<int> GetAllChildrenRoleId(int roleId)
+        {
+            return GetAllChildren(roleId).Select(x => x.Id).ToList();
         }
 
         public List<RoleNodes> GetAllChildren(int roleId)
@@ -268,9 +278,11 @@
                 //褰撳墠鐢ㄦ埛鐨勬潈闄�
                 List<Permissions> permissions = _MenuRepository.GetPermissions(App.User.RoleId);
 
+                List<int> menuIds = _MenuRepository.QueryData(x => x.MenuId, x => x.MenuType == 1);
+
                 List<int> originalMeunIds = new List<int>();
                 //琚垎閰嶈鑹茬殑鏉冮檺
-                List<Sys_RoleAuth> roleAuths = _RoleAuthRepository.QueryData(x => x.RoleId == roleId);
+                List<Sys_RoleAuth> roleAuths = _RoleAuthRepository.QueryData(x => x.RoleId == roleId && menuIds.Contains(x.MenuId));
                 List<Sys_RoleAuth> updateAuths = new List<Sys_RoleAuth>();
                 foreach (UserPermissionDTO x in userPermissions)
                 {
@@ -323,7 +335,100 @@
                     x.AuthValue = "";
                 });
                 //灏嗗彇娑堢殑鏉冮檺璁剧疆涓�""
-                _RoleAuthRepository.UpdateData(delAuths);
+                _RoleAuthRepository.DeleteData(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;
+        }
+
+        /// <summary>
+        /// 淇濆瓨瑙掕壊鏉冮檺
+        /// </summary>
+        /// <param name="userPermissions"></param>
+        /// <param name="roleId"></param>
+        /// <returns></returns>
+        public WebResponseContent SavePermissionPDA(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<int> menuIds = _MenuRepository.QueryData(x => x.MenuId, x => x.MenuType == 1);
+
+                //琚垎閰嶈鑹茬殑鏉冮檺
+                List<Sys_RoleAuth> roleAuths = _RoleAuthRepository.QueryData(x => x.RoleId == roleId && menuIds.Contains(x.MenuId));
+                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.DeleteData(delAuths);
 
                 int addCount = updateAuths.Where(x => x.AuthId <= 0).Count();
                 int updateCount = updateAuths.Where(x => x.AuthId > 0).Count();

--
Gitblit v1.9.3