李昊 4 anos atrás
pai
commit
b285620613

+ 20 - 8
gx_api/GxPress/Model/GxPress.Entity/Menus/Menus.cs

@@ -1,3 +1,4 @@
+using System.Collections.Generic;
 using Datory.Annotations;
 
 namespace GxPress.Entity.Menus
@@ -9,28 +10,39 @@ namespace GxPress.Entity.Menus
     public class Menus : Datory.Entity
     {
         /// <summary>
-        /// Controller 名称
+        /// 父级ID
         /// </summary>
         /// <value></value>
         [DataColumn]
-        public string Controller { get; set; }
+        public int ParentId { get; set; }
         /// <summary>
-        /// action名称
+        /// 标题
         /// </summary>
         /// <value></value>
         [DataColumn]
-        public string Action { get; set; }
+        public string Title { get; set; }
         /// <summary>
-        /// 父级ID
+        /// 名称
         /// </summary>
         /// <value></value>
         [DataColumn]
-        public int parentId { get; set; }
+        public string Name { get; set; }
         /// <summary>
-        /// 名称
+        /// 图标名称
         /// </summary>
         /// <value></value>
         [DataColumn]
-        public string Name { get; set; }
+        public string Icon { get; set; }
+        /// <summary>
+        /// 孩子
+        /// </summary>
+        /// <value></value>
+        public List<Menus> Children { get; set; }
+        /// <summary>
+        /// 路径
+        /// </summary>
+        /// <value></value>
+        [DataColumn]
+        public string Path { get; set; }
     }
 }

+ 18 - 4
gx_api/GxPress/Repository/GxPress.Repository.Implement/Menus/MenusRepository.cs

@@ -6,7 +6,7 @@ using GxPress.Common.AppOptions;
 using GxPress.Common.Tools;
 using GxPress.Repository.Interface.Menus;
 using Microsoft.Extensions.Options;
-
+using System.Linq;
 namespace GxPress.Repository.Implement.Menus
 {
     public class MenusRepository : IMenusRepository
@@ -44,12 +44,26 @@ namespace GxPress.Repository.Implement.Menus
 
         public async Task<IEnumerable<Entity.Menus.Menus>> GetAllAsync()
         {
-            return await _repository.GetAllAsync();
+            var all = await _repository.GetAllAsync();
+            var list = new List<Entity.Menus.Menus>();
+            list = all.Where(n => n.ParentId == 0).ToList();
+            foreach (var item in list)
+            {
+                item.Children = all.Where(n => n.ParentId == item.Id).ToList();
+            }
+            return list;
         }
 
-         public async Task<IEnumerable<Entity.Menus.Menus>> GetAllAsync(List<int> menusIds)
+        public async Task<IEnumerable<Entity.Menus.Menus>> GetAllAsync(List<int> menusIds)
         {
-            return await _repository.GetAllAsync(Q.WhereIn(nameof(Entity.Menus.Menus.Id),menusIds));
+            var all = await _repository.GetAllAsync(Q.WhereIn(nameof(Entity.Menus.Menus.Id), menusIds));
+            var list = new List<Entity.Menus.Menus>();
+            list = all.Where(n => n.ParentId == 0).ToList();
+            foreach (var item in list)
+            {
+                item.Children = all.Where(n => n.ParentId == item.Id).ToList();
+            }
+            return list;
         }
     }
 }

+ 30 - 1
gx_api/GxPress/Service/GxPress.Service.Implement/Role/RoleService.cs

@@ -6,7 +6,7 @@ using System.Threading.Tasks;
 using GxPress.Request.Role;
 using System.Collections.Generic;
 using System.Linq;
-
+using System.Transactions;
 namespace GxPress.Service.Implement.Role
 {
     public class RoleService : IRoleService
@@ -67,6 +67,35 @@ namespace GxPress.Service.Implement.Role
         {
             return await menusRepository.InsertAsync(model) > 0;
         }
+
+        /// <summary>
+        /// 添加菜单
+        /// </summary>
+        /// <param name="models"></param>
+        /// <returns></returns>
+        public async Task<bool> InsertMenusAsync(List<Entity.Menus.Menus> models)
+        {
+            try
+            {
+                using (var transactionScope = new TransactionScope())
+                {
+                    foreach (var item in models)
+                    {
+                        var id = await menusRepository.InsertAsync(item);
+                        foreach (var menu in item.Children)
+                        {
+                            await menusRepository.InsertAsync(menu);
+                        }
+                    }
+                    transactionScope.Complete();
+                }
+            }
+            catch (System.Exception ex)
+            {
+                throw new Common.Exceptions.BusinessException(ex.Message);
+            }
+            return true;
+        }
         /// <summary>
         /// 修改菜单
         /// </summary>