Blame view

美国版/Food Labeling Management Code/Yi.Abp.Net8/module/food-labeling-us/FoodLabeling.Application/Services/RbacMenuAppService.cs 8.33 KB
9df65e38   李曜臣   实现美国版门店与RBAC对外接口
1
  using FoodLabeling.Application.Contracts.Dtos.RbacMenu;
9df65e38   李曜臣   实现美国版门店与RBAC对外接口
2
  using FoodLabeling.Application.Contracts.IServices;
0268b0d0   李曜臣   菜单权限模块实现
3
  using FoodLabeling.Application.Services.DbModels;
9df65e38   李曜臣   实现美国版门店与RBAC对外接口
4
5
6
7
  using Microsoft.AspNetCore.Mvc;
  using SqlSugar;
  using Volo.Abp;
  using Volo.Abp.Application.Services;
9df65e38   李曜臣   实现美国版门店与RBAC对外接口
8
9
10
11
12
13
14
15
16
  using Yi.Framework.SqlSugarCore.Abstractions;
  
  namespace FoodLabeling.Application.Services;
  
  /// <summary>
  /// 权限(Menu)管理(食品标签-美国版对外)
  /// </summary>
  public class RbacMenuAppService : ApplicationService, IRbacMenuAppService
  {
0268b0d0   李曜臣   菜单权限模块实现
17
      private readonly ISqlSugarDbContext _dbContext;
9df65e38   李曜臣   实现美国版门店与RBAC对外接口
18
  
0268b0d0   李曜臣   菜单权限模块实现
19
      public RbacMenuAppService(ISqlSugarDbContext dbContext)
9df65e38   李曜臣   实现美国版门店与RBAC对外接口
20
      {
0268b0d0   李曜臣   菜单权限模块实现
21
          _dbContext = dbContext;
9df65e38   李曜臣   实现美国版门店与RBAC对外接口
22
23
24
      }
  
      /// <inheritdoc />
0268b0d0   李曜臣   菜单权限模块实现
25
      public async Task<List<RbacMenuGetListOutputDto>> GetListAsync([FromQuery] RbacMenuGetListInputVo input)
9df65e38   李曜臣   实现美国版门店与RBAC对外接口
26
      {
0268b0d0   李曜臣   菜单权限模块实现
27
          var query = _dbContext.SqlSugarClient.Queryable<MenuDbEntity>()
9df65e38   李曜臣   实现美国版门店与RBAC对外接口
28
29
30
              .Where(x => x.IsDeleted == false)
              .WhereIF(!string.IsNullOrWhiteSpace(input.MenuName), x => x.MenuName.Contains(input.MenuName!.Trim()))
              .WhereIF(input.State is not null, x => x.State == input.State)
0268b0d0   李曜臣   菜单权限模块实现
31
              .WhereIF(input.MenuSource is not null, x => x.MenuSource == input.MenuSource!.Value)
9df65e38   李曜臣   实现美国版门店与RBAC对外接口
32
33
              .OrderBy(x => x.OrderNum, OrderByType.Desc);
  
0268b0d0   李曜臣   菜单权限模块实现
34
          var entities = await query.ToListAsync();
9df65e38   李曜臣   实现美国版门店与RBAC对外接口
35
  
0268b0d0   李曜臣   菜单权限模块实现
36
          return entities.Select(x => new RbacMenuGetListOutputDto
9df65e38   李曜臣   实现美国版门店与RBAC对外接口
37
38
39
40
          {
              Id = x.Id,
              ParentId = x.ParentId,
              MenuName = x.MenuName ?? string.Empty,
8e0c49eb   李曜臣   产品标签类别优化;
41
42
              RouterName = x.RouterName,
              Router = x.Router,
9df65e38   李曜臣   实现美国版门店与RBAC对外接口
43
              PermissionCode = x.PermissionCode,
0268b0d0   李曜臣   菜单权限模块实现
44
45
              MenuType = x.MenuType,
              MenuSource = x.MenuSource,
9df65e38   李曜臣   实现美国版门店与RBAC对外接口
46
47
48
              OrderNum = x.OrderNum,
              State = x.State
          }).ToList();
9df65e38   李曜臣   实现美国版门店与RBAC对外接口
49
50
51
      }
  
      /// <inheritdoc />
0268b0d0   李曜臣   菜单权限模块实现
52
      public async Task<RbacMenuGetListOutputDto> GetAsync(string id)
9df65e38   李曜臣   实现美国版门店与RBAC对外接口
53
      {
0268b0d0   李曜臣   菜单权限模块实现
54
55
56
          var entity = await _dbContext.SqlSugarClient.Queryable<MenuDbEntity>()
              .Where(x => x.Id == id && x.IsDeleted == false)
              .SingleAsync();
9df65e38   李曜臣   实现美国版门店与RBAC对外接口
57
58
59
60
61
62
63
64
65
66
          if (entity is null)
          {
              throw new UserFriendlyException("权限不存在");
          }
  
          return new RbacMenuGetListOutputDto
          {
              Id = entity.Id,
              ParentId = entity.ParentId,
              MenuName = entity.MenuName ?? string.Empty,
8e0c49eb   李曜臣   产品标签类别优化;
67
68
              RouterName = entity.RouterName,
              Router = entity.Router,
9df65e38   李曜臣   实现美国版门店与RBAC对外接口
69
              PermissionCode = entity.PermissionCode,
0268b0d0   李曜臣   菜单权限模块实现
70
71
              MenuType = entity.MenuType,
              MenuSource = entity.MenuSource,
9df65e38   李曜臣   实现美国版门店与RBAC对外接口
72
73
74
75
76
77
78
79
80
81
82
83
84
85
              OrderNum = entity.OrderNum,
              State = entity.State
          };
      }
  
      /// <inheritdoc />
      public async Task<RbacMenuGetListOutputDto> CreateAsync([FromBody] RbacMenuCreateInputVo input)
      {
          var name = input.MenuName?.Trim();
          if (string.IsNullOrWhiteSpace(name))
          {
              throw new UserFriendlyException("权限名称不能为空");
          }
  
0268b0d0   李曜臣   菜单权限模块实现
86
          var entity = new MenuDbEntity
9df65e38   李曜臣   实现美国版门店与RBAC对外接口
87
          {
0268b0d0   李曜臣   菜单权限模块实现
88
              Id = GuidGenerator.Create().ToString(),
9df65e38   李曜臣   实现美国版门店与RBAC对外接口
89
              MenuName = name,
0268b0d0   李曜臣   菜单权限模块实现
90
91
92
              ParentId = string.IsNullOrWhiteSpace(input.ParentId) ? "0" : input.ParentId.Trim(),
              MenuType = input.MenuType,
              MenuSource = input.MenuSource,
9df65e38   李曜臣   实现美国版门店与RBAC对外接口
93
94
95
96
              PermissionCode = input.PermissionCode?.Trim(),
              Router = input.Router?.Trim(),
              Component = input.Component?.Trim(),
              OrderNum = input.OrderNum,
0268b0d0   李曜臣   菜单权限模块实现
97
98
99
100
101
102
103
              State = input.State,
              IsDeleted = false,
              CreationTime = DateTime.Now,
              IsCache = false,
              IsLink = false,
              IsShow = true,
              ConcurrencyStamp = string.Empty
9df65e38   李曜臣   实现美国版门店与RBAC对外接口
104
          };
0268b0d0   李曜臣   菜单权限模块实现
105
          await _dbContext.SqlSugarClient.Insertable(entity).ExecuteCommandAsync();
9df65e38   李曜臣   实现美国版门店与RBAC对外接口
106
107
108
109
          return await GetAsync(entity.Id);
      }
  
      /// <inheritdoc />
0268b0d0   李曜臣   菜单权限模块实现
110
      public async Task<RbacMenuGetListOutputDto> UpdateAsync(string id, [FromBody] RbacMenuUpdateInputVo input)
9df65e38   李曜臣   实现美国版门店与RBAC对外接口
111
      {
0268b0d0   李曜臣   菜单权限模块实现
112
113
114
          var entity = await _dbContext.SqlSugarClient.Queryable<MenuDbEntity>()
              .Where(x => x.Id == id && x.IsDeleted == false)
              .SingleAsync();
9df65e38   李曜臣   实现美国版门店与RBAC对外接口
115
116
117
118
119
120
121
122
123
124
125
126
          if (entity is null)
          {
              throw new UserFriendlyException("权限不存在");
          }
  
          var name = input.MenuName?.Trim();
          if (string.IsNullOrWhiteSpace(name))
          {
              throw new UserFriendlyException("权限名称不能为空");
          }
  
          entity.MenuName = name;
0268b0d0   李曜臣   菜单权限模块实现
127
128
129
          entity.ParentId = string.IsNullOrWhiteSpace(input.ParentId) ? "0" : input.ParentId.Trim();
          entity.MenuType = input.MenuType;
          entity.MenuSource = input.MenuSource;
9df65e38   李曜臣   实现美国版门店与RBAC对外接口
130
131
132
133
134
          entity.PermissionCode = input.PermissionCode?.Trim();
          entity.Router = input.Router?.Trim();
          entity.Component = input.Component?.Trim();
          entity.OrderNum = input.OrderNum;
          entity.State = input.State;
0268b0d0   李曜臣   菜单权限模块实现
135
          entity.LastModificationTime = DateTime.Now;
9df65e38   李曜臣   实现美国版门店与RBAC对外接口
136
  
0268b0d0   李曜臣   菜单权限模块实现
137
138
139
          await _dbContext.SqlSugarClient.Updateable(entity)
              .Where(x => x.Id == entity.Id)
              .ExecuteCommandAsync();
9df65e38   李曜臣   实现美国版门店与RBAC对外接口
140
141
142
143
          return await GetAsync(entity.Id);
      }
  
      /// <inheritdoc />
0268b0d0   李曜臣   菜单权限模块实现
144
      public async Task DeleteAsync([FromBody] List<string> ids)
9df65e38   李曜臣   实现美国版门店与RBAC对外接口
145
      {
0268b0d0   李曜臣   菜单权限模块实现
146
          var idList = ids?.Where(x => !string.IsNullOrWhiteSpace(x)).Select(x => x.Trim()).Distinct().ToList() ?? new List<string>();
9df65e38   李曜臣   实现美国版门店与RBAC对外接口
147
148
149
150
151
          if (idList.Count == 0)
          {
              return;
          }
  
0268b0d0   李曜臣   菜单权限模块实现
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
          await _dbContext.SqlSugarClient.Updateable<MenuDbEntity>()
              .SetColumns(x => new MenuDbEntity { IsDeleted = true })
              .Where(x => idList.Contains(x.Id))
              .ExecuteCommandAsync();
      }
  
      /// <inheritdoc />
      public async Task<List<RbacMenuTreeDto>> GetTreeAsync()
      {
          // 返回所有字段,但过滤逻辑删除数据
          var menus = await _dbContext.SqlSugarClient.Queryable<MenuDbEntity>()
              .Where(x => x.IsDeleted == false)
              .OrderBy(x => x.OrderNum, OrderByType.Desc)
              .ToListAsync();
  
          var nodes = menus.Select(m => new RbacMenuTreeDto
          {
              Id = m.Id,
              IsDeleted = m.IsDeleted,
              CreationTime = m.CreationTime,
              CreatorId = m.CreatorId,
              LastModifierId = m.LastModifierId,
              LastModificationTime = m.LastModificationTime,
              OrderNum = m.OrderNum,
              State = m.State,
              MenuName = m.MenuName ?? string.Empty,
              RouterName = m.RouterName,
              MenuType = m.MenuType,
              PermissionCode = m.PermissionCode,
              ParentId = m.ParentId,
              MenuIcon = m.MenuIcon,
              Router = m.Router,
              IsLink = m.IsLink,
              IsCache = m.IsCache,
              IsShow = m.IsShow,
              Remark = m.Remark,
              Component = m.Component,
              MenuSource = m.MenuSource,
              Query = m.Query,
              ConcurrencyStamp = m.ConcurrencyStamp,
              Children = new List<RbacMenuTreeDto>()
          }).ToList();
  
          // TreeHelper 仅支持 Guid Id/ParentId,这里使用字符串 ParentId 自行构建树
          var nodeById = nodes
              .Where(x => !string.IsNullOrWhiteSpace(x.Id))
              .GroupBy(x => x.Id)
              .ToDictionary(g => g.Key, g => g.First());
  
          foreach (var node in nodes)
          {
              node.Children ??= new List<RbacMenuTreeDto>();
              var parentId = string.IsNullOrWhiteSpace(node.ParentId) ? "0" : node.ParentId.Trim();
              if (parentId == "0" || parentId == "00000000-0000-0000-0000-000000000000")
              {
                  continue;
              }
  
              if (nodeById.TryGetValue(parentId, out var parent))
              {
                  parent.Children ??= new List<RbacMenuTreeDto>();
                  parent.Children.Add(node);
              }
          }
  
          var roots = nodes
              .Where(n =>
              {
                  var pid = string.IsNullOrWhiteSpace(n.ParentId) ? "0" : n.ParentId.Trim();
                  return pid == "0" || pid == "00000000-0000-0000-0000-000000000000" || !nodeById.ContainsKey(pid);
              })
              .ToList();
  
          SortTree(roots);
          return roots;
      }
  
      private static void SortTree(List<RbacMenuTreeDto> nodes)
      {
          nodes.Sort((a, b) => b.OrderNum.CompareTo(a.OrderNum));
          foreach (var node in nodes)
          {
              if (node.Children is { Count: > 0 })
              {
                  SortTree(node.Children);
              }
          }
9df65e38   李曜臣   实现美国版门店与RBAC对外接口
239
240
      }
  }