From 9e8333656f689291c9aea9052d2ee50cb784d5ed Mon Sep 17 00:00:00 2001
From: sunpengfei <i@angelzzz.com>
Date: 星期一, 04 八月 2025 18:23:34 +0800
Subject: [PATCH] feat:菜单适配

---
 FlexJobApi.Core/Models/User/Menus/Commands/SetMenuSwitchCommand.cs         |   40 +++++
 FlexJobApi.User.Application/Menus/Commands/SaveMenuButtonCommandHandler.cs |   62 ++++++++
 FlexJobApi.Core/Models/User/Menus/Commands/SaveMenuFieldCommand.cs         |   65 +++++++++
 FlexJobApi.User.Application/Menus/Commands/SetMenuSwitchCommandHandler.cs  |   53 +++++++
 FlexJobApi.User.Application/Menus/Queries/GetMenusQueryHandler.cs          |    1 
 FlexJobApi.Core/Models/User/Menus/Commands/SaveMenuButtonCommand.cs        |   65 +++++++++
 FlexJobApi.User.Application/Menus/Commands/SaveMenuFieldCommandHandler.cs  |   62 ++++++++
 FlexJobApi.User.Application/Menus/MenuAppService.cs                        |   30 ++++
 8 files changed, 378 insertions(+), 0 deletions(-)

diff --git a/FlexJobApi.Core/Models/User/Menus/Commands/SaveMenuButtonCommand.cs b/FlexJobApi.Core/Models/User/Menus/Commands/SaveMenuButtonCommand.cs
new file mode 100644
index 0000000..e2c8279
--- /dev/null
+++ b/FlexJobApi.Core/Models/User/Menus/Commands/SaveMenuButtonCommand.cs
@@ -0,0 +1,65 @@
+锘縰sing MediatR;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace FlexJobApi.Core
+{
+    /// <summary>
+    /// 淇濆瓨鑿滃崟鎸夐挳
+    /// </summary>
+    public class SaveMenuButtonCommand : IRequest<Guid>
+    {
+        /// <summary>
+        /// Id
+        /// </summary>
+        public Guid? Id { get; set; }
+
+        /// <summary>
+        /// 鑿滃崟Id
+        /// </summary>
+        public Guid ParentId { get; set; }
+
+        /// <summary>
+        /// 缂栧彿
+        /// </summary>
+        public string Code { get; set; }
+
+        /// <summary>
+        /// 鍚嶇О
+        /// </summary>
+        public string Name { get; set; }
+
+        /// <summary>
+        /// 鍒嗙粍鍚嶇О锛堢敤浜庢寜閽�/瀛楁锛�
+        /// </summary>
+        public string Group { get; set; }
+
+        /// <summary>
+        /// 浣嶇疆锛堢敤浜庢寜閽級
+        /// </summary>
+        public string Location { get; set; }
+
+        /// <summary>
+        /// 鍥炬爣
+        /// </summary>
+        public string Icon { get; set; }
+
+        /// <summary>
+        /// 瀹藉害锛堢敤浜庢寜閽�/鍒�/鍏冪礌锛�
+        /// </summary>
+        public string Width { get; set; }
+
+        /// <summary>
+        /// 鎺掑簭
+        /// </summary>
+        public int Sort { get; set; }
+
+        /// <summary>
+        /// 澶囨敞
+        /// </summary>
+        public string Remark { get; set; }
+    }
+}
diff --git a/FlexJobApi.Core/Models/User/Menus/Commands/SaveMenuFieldCommand.cs b/FlexJobApi.Core/Models/User/Menus/Commands/SaveMenuFieldCommand.cs
new file mode 100644
index 0000000..d856536
--- /dev/null
+++ b/FlexJobApi.Core/Models/User/Menus/Commands/SaveMenuFieldCommand.cs
@@ -0,0 +1,65 @@
+锘縰sing MediatR;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace FlexJobApi.Core
+{
+    /// <summary>
+    /// 淇濆瓨鑿滃崟瀛楁
+    /// </summary>
+    public class SaveMenuFieldCommand : IRequest<Guid>
+    {
+        /// <summary>
+        /// Id
+        /// </summary>
+        public Guid? Id { get; set; }
+
+        /// <summary>
+        /// 鑿滃崟Id
+        /// </summary>
+        public Guid ParentId { get; set; }
+
+        /// <summary>
+        /// 缂栧彿
+        /// </summary>
+        public string Code { get; set; }
+
+        /// <summary>
+        /// 鍚嶇О
+        /// </summary>
+        public string Name { get; set; }
+
+        /// <summary>
+        /// 鍒嗙粍鍚嶇О锛堢敤浜庢寜閽�/瀛楁锛�
+        /// </summary>
+        public string Group { get; set; }
+
+        /// <summary>
+        /// 浣嶇疆锛堢敤浜庢寜閽級
+        /// </summary>
+        public string Location { get; set; }
+
+        /// <summary>
+        /// 鍥炬爣
+        /// </summary>
+        public string Icon { get; set; }
+
+        /// <summary>
+        /// 瀹藉害锛堢敤浜庢寜閽�/鍒�/鍏冪礌锛�
+        /// </summary>
+        public string Width { get; set; }
+
+        /// <summary>
+        /// 鎺掑簭
+        /// </summary>
+        public int Sort { get; set; }
+
+        /// <summary>
+        /// 澶囨敞
+        /// </summary>
+        public string Remark { get; set; }
+    }
+}
diff --git a/FlexJobApi.Core/Models/User/Menus/Commands/SetMenuSwitchCommand.cs b/FlexJobApi.Core/Models/User/Menus/Commands/SetMenuSwitchCommand.cs
new file mode 100644
index 0000000..404ff97
--- /dev/null
+++ b/FlexJobApi.Core/Models/User/Menus/Commands/SetMenuSwitchCommand.cs
@@ -0,0 +1,40 @@
+锘縰sing MediatR;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace FlexJobApi.Core
+{
+    /// <summary>
+    /// 璁剧疆鑿滃崟鍒囨崲淇℃伅
+    /// </summary>
+    public class SetMenuSwitchCommand : IRequest<int>
+    {
+        public SetMenuSwitchCommand()
+        {
+            Ids = [];
+        }
+
+        /// <summary>
+        /// Id
+        /// </summary>
+        public List<Guid> Ids { get; set; }
+
+        /// <summary>
+        /// 绫诲瀷锛坣ull鏃朵笉鏇存柊锛�
+        /// </summary>
+        public EnumMenuType? Type { get; set; }
+
+        /// <summary>
+        /// 鏄惁绂佺敤锛坣ull鏃朵笉鏇存柊锛�
+        /// </summary>
+        public bool? IsDisabled { get; set; }
+
+        /// <summary>
+        /// 鏄惁缂撳瓨锛坣ull鏃朵笉鏇存柊锛�
+        /// </summary>
+        public bool? IsCache { get; set; }
+    }
+}
diff --git a/FlexJobApi.User.Application/Menus/Commands/SaveMenuButtonCommandHandler.cs b/FlexJobApi.User.Application/Menus/Commands/SaveMenuButtonCommandHandler.cs
new file mode 100644
index 0000000..21d9a7e
--- /dev/null
+++ b/FlexJobApi.User.Application/Menus/Commands/SaveMenuButtonCommandHandler.cs
@@ -0,0 +1,62 @@
+锘縰sing FlexJobApi.Core;
+using Furion.DatabaseAccessor;
+using Furion.FriendlyException;
+using Mapster;
+using MediatR;
+using Microsoft.EntityFrameworkCore;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace FlexJobApi.User.Application
+{
+    /// <summary>
+    /// 淇濆瓨鑿滃崟鎸夐挳
+    /// </summary>
+    public class SaveMenuButtonCommandHandler(
+            IRepository<Menu> rep
+        ) : IRequestHandler<SaveMenuButtonCommand, Guid>
+    {
+        private readonly IRepository<Menu> rep = rep;
+
+        /// <inheritdoc/>
+        public async Task<Guid> Handle(SaveMenuButtonCommand request, CancellationToken cancellationToken)
+        {
+            if (request.Id.HasValue)
+            {
+                var entity = await rep.FirstOrDefaultAsync(it => it.Id == it.Id);
+                if (entity.ParentId != request.ParentId) throw Oops.Oh(EnumErrorCodeType.s410, "涓婄骇Id");
+                request.Adapt(entity);
+                if (await CheckExist(entity)) throw Oops.Oh(EnumErrorCodeType.s405, "鑿滃崟缂栧彿");
+                await rep.UpdateAsync(entity);
+                return entity.Id;
+            }
+            else
+            {
+                var entity = new Menu();
+                request.Adapt(entity);
+                if (await CheckExist(entity)) throw Oops.Oh(EnumErrorCodeType.s405, "鑿滃崟缂栧彿");
+                await rep.InsertAsync(entity);
+                return entity.Id;
+            }
+        }
+
+        /// <summary>
+        /// 鏍¢獙鑿滃崟鏄惁閲嶅
+        /// </summary>
+        /// <param name="entity"></param>
+        /// <returns></returns>
+        private async Task<bool> CheckExist(Menu entity)
+        {
+            return await rep.AsQueryable().AsNoTracking()
+                .AnyAsync(it =>
+                    it.ParentId == entity.ParentId
+                    && it.Group == entity.Group
+                    && it.Location == entity.Location
+                    && it.Code == entity.Code
+                    && it.Id != entity.Id);
+        }
+    }
+}
diff --git a/FlexJobApi.User.Application/Menus/Commands/SaveMenuFieldCommandHandler.cs b/FlexJobApi.User.Application/Menus/Commands/SaveMenuFieldCommandHandler.cs
new file mode 100644
index 0000000..774ce2f
--- /dev/null
+++ b/FlexJobApi.User.Application/Menus/Commands/SaveMenuFieldCommandHandler.cs
@@ -0,0 +1,62 @@
+锘縰sing FlexJobApi.Core;
+using Furion.DatabaseAccessor;
+using Furion.FriendlyException;
+using Mapster;
+using MediatR;
+using Microsoft.EntityFrameworkCore;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace FlexJobApi.User.Application
+{
+    /// <summary>
+    /// 
+    /// </summary>
+    public class SaveMenuFieldCommandHandler(
+            IRepository<Menu> rep
+        ) : IRequestHandler<SaveMenuFieldCommand, Guid>
+    {
+        private readonly IRepository<Menu> rep = rep;
+
+        /// <inheritdoc/>
+        public async Task<Guid> Handle(SaveMenuFieldCommand request, CancellationToken cancellationToken)
+        {
+            if (request.Id.HasValue)
+            {
+                var entity = await rep.FirstOrDefaultAsync(it => it.Id == it.Id);
+                if (entity.ParentId != request.ParentId) throw Oops.Oh(EnumErrorCodeType.s410, "涓婄骇Id");
+                request.Adapt(entity);
+                if (await CheckExist(entity)) throw Oops.Oh(EnumErrorCodeType.s405, "鑿滃崟缂栧彿");
+                await rep.UpdateAsync(entity);
+                return entity.Id;
+            }
+            else
+            {
+                var entity = new Menu();
+                request.Adapt(entity);
+                if (await CheckExist(entity)) throw Oops.Oh(EnumErrorCodeType.s405, "鑿滃崟缂栧彿");
+                await rep.InsertAsync(entity);
+                return entity.Id;
+            }
+        }
+
+        /// <summary>
+        /// 鏍¢獙鑿滃崟鏄惁閲嶅
+        /// </summary>
+        /// <param name="entity"></param>
+        /// <returns></returns>
+        private async Task<bool> CheckExist(Menu entity)
+        {
+            return await rep.AsQueryable().AsNoTracking()
+                .AnyAsync(it =>
+                    it.ParentId == entity.ParentId
+                    && it.Group == entity.Group
+                    && it.Code == entity.Code
+                    && it.Id != entity.Id);
+        }
+
+    }
+}
diff --git a/FlexJobApi.User.Application/Menus/Commands/SetMenuSwitchCommandHandler.cs b/FlexJobApi.User.Application/Menus/Commands/SetMenuSwitchCommandHandler.cs
new file mode 100644
index 0000000..2a758fe
--- /dev/null
+++ b/FlexJobApi.User.Application/Menus/Commands/SetMenuSwitchCommandHandler.cs
@@ -0,0 +1,53 @@
+锘縰sing FlexJobApi.Core;
+using Furion.DatabaseAccessor;
+using MediatR;
+using Microsoft.EntityFrameworkCore;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace FlexJobApi.User.Application
+{
+    /// <summary>
+    /// 璁剧疆鑿滃崟鍒囨崲淇℃伅
+    /// </summary>
+    public class SetMenuSwitchCommandHandler(
+            IRepository<Menu> rep
+        ) : IRequestHandler<SetMenuSwitchCommand, int>
+    {
+        private readonly IRepository<Menu> rep = rep;
+
+        /// <inheritdoc/>
+        public async Task<int> Handle(SetMenuSwitchCommand request, CancellationToken cancellationToken)
+        {
+            var entities = await rep.AsQueryable()
+                .Where(it => request.Ids.Contains(it.Id))
+                .ToListAsync(cancellationToken);
+            foreach (var entity in entities)
+            {
+                if (request.Type.HasValue)
+                {
+                    entity.Type = request.Type.Value;
+                }
+                if (request.IsDisabled.HasValue)
+                {
+                    entity.IsDisabled = request.IsDisabled.Value;
+                }
+                if (request.IsCache.HasValue)
+                {
+                    entity.IsCache = request.IsCache.Value;
+                }
+            }
+
+            if (entities.Any())
+            {
+                await rep.UpdateAsync(entities);
+                return entities.Count;
+            }
+
+            return 0;
+        }
+    }
+}
diff --git a/FlexJobApi.User.Application/Menus/MenuAppService.cs b/FlexJobApi.User.Application/Menus/MenuAppService.cs
index e425eb6..e58bdc2 100644
--- a/FlexJobApi.User.Application/Menus/MenuAppService.cs
+++ b/FlexJobApi.User.Application/Menus/MenuAppService.cs
@@ -56,6 +56,36 @@
         }
 
         /// <summary>
+        /// 淇濆瓨鑿滃崟鎸夐挳
+        /// </summary>
+        /// <param name="command"></param>
+        /// <returns></returns>
+        public Task<Guid> SaveMenuButton(SaveMenuButtonCommand command)
+        {
+            return mediator.Send(command);
+        }
+
+        /// <summary>
+        /// 淇濆瓨鑿滃崟瀛楁
+        /// </summary>
+        /// <param name="command"></param>
+        /// <returns></returns>
+        public Task<Guid> SaveMenuField(SaveMenuFieldCommand command)
+        {
+            return mediator.Send(command);
+        }
+
+        /// <summary>
+        /// 璁剧疆鑿滃崟鍒囨崲淇℃伅
+        /// </summary>
+        /// <param name="command"></param>
+        /// <returns></returns>
+        public Task<int> SetMenuSwitch(SetMenuSwitchCommand command)
+        {
+            return mediator.Send(command);
+        }
+
+        /// <summary>
         /// 鍒犻櫎鑿滃崟
         /// </summary>
         /// <param name="command"></param>
diff --git a/FlexJobApi.User.Application/Menus/Queries/GetMenusQueryHandler.cs b/FlexJobApi.User.Application/Menus/Queries/GetMenusQueryHandler.cs
index 65c69ff..09ea343 100644
--- a/FlexJobApi.User.Application/Menus/Queries/GetMenusQueryHandler.cs
+++ b/FlexJobApi.User.Application/Menus/Queries/GetMenusQueryHandler.cs
@@ -25,6 +25,7 @@
         public async Task<List<GetMenusQueryResultItem>> Handle(GetMenusQuery request, CancellationToken cancellationToken)
         {
             var q = rep.AsQueryable().AsNoTracking()
+                .OrderBy(it => it.Sort)
                 .Where(it => it.UserType == request.UserType && it.ClientType == request.ClientType)
                 .Where(it => it.Type == EnumMenuType.Menu || it.Type == EnumMenuType.Page || it.Type == EnumMenuType.Modal);
             var all = await q.ProjectToType<GetMenusQueryResultItem>().ToListAsync();

--
Gitblit v1.9.1