Blame view

美国版/Food Labeling Management Code/Yi.Abp.Net8/module/food-labeling-us/FoodLabeling.Application/Services/RbacMenuAppService.cs 8.18 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
41
          {
              Id = x.Id,
              ParentId = x.ParentId,
              MenuName = x.MenuName ?? string.Empty,
              PermissionCode = x.PermissionCode,
0268b0d0   李曜臣   菜单权限模块实现
42
43
              MenuType = x.MenuType,
              MenuSource = x.MenuSource,
9df65e38   李曜臣   实现美国版门店与RBAC对外接口
44
45
46
              OrderNum = x.OrderNum,
              State = x.State
          }).ToList();
9df65e38   李曜臣   实现美国版门店与RBAC对外接口
47
48
49
      }
  
      /// <inheritdoc />
0268b0d0   李曜臣   菜单权限模块实现
50
      public async Task<RbacMenuGetListOutputDto> GetAsync(string id)
9df65e38   李曜臣   实现美国版门店与RBAC对外接口
51
      {
0268b0d0   李曜臣   菜单权限模块实现
52
53
54
          var entity = await _dbContext.SqlSugarClient.Queryable<MenuDbEntity>()
              .Where(x => x.Id == id && x.IsDeleted == false)
              .SingleAsync();
9df65e38   李曜臣   实现美国版门店与RBAC对外接口
55
56
57
58
59
60
61
62
63
64
65
          if (entity is null)
          {
              throw new UserFriendlyException("权限不存在");
          }
  
          return new RbacMenuGetListOutputDto
          {
              Id = entity.Id,
              ParentId = entity.ParentId,
              MenuName = entity.MenuName ?? string.Empty,
              PermissionCode = entity.PermissionCode,
0268b0d0   李曜臣   菜单权限模块实现
66
67
              MenuType = entity.MenuType,
              MenuSource = entity.MenuSource,
9df65e38   李曜臣   实现美国版门店与RBAC对外接口
68
69
70
71
72
73
74
75
76
77
78
79
80
81
              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   李曜臣   菜单权限模块实现
82
          var entity = new MenuDbEntity
9df65e38   李曜臣   实现美国版门店与RBAC对外接口
83
          {
0268b0d0   李曜臣   菜单权限模块实现
84
              Id = GuidGenerator.Create().ToString(),
9df65e38   李曜臣   实现美国版门店与RBAC对外接口
85
              MenuName = name,
0268b0d0   李曜臣   菜单权限模块实现
86
87
88
              ParentId = string.IsNullOrWhiteSpace(input.ParentId) ? "0" : input.ParentId.Trim(),
              MenuType = input.MenuType,
              MenuSource = input.MenuSource,
9df65e38   李曜臣   实现美国版门店与RBAC对外接口
89
90
91
92
              PermissionCode = input.PermissionCode?.Trim(),
              Router = input.Router?.Trim(),
              Component = input.Component?.Trim(),
              OrderNum = input.OrderNum,
0268b0d0   李曜臣   菜单权限模块实现
93
94
95
96
97
98
99
              State = input.State,
              IsDeleted = false,
              CreationTime = DateTime.Now,
              IsCache = false,
              IsLink = false,
              IsShow = true,
              ConcurrencyStamp = string.Empty
9df65e38   李曜臣   实现美国版门店与RBAC对外接口
100
          };
0268b0d0   李曜臣   菜单权限模块实现
101
          await _dbContext.SqlSugarClient.Insertable(entity).ExecuteCommandAsync();
9df65e38   李曜臣   实现美国版门店与RBAC对外接口
102
103
104
105
          return await GetAsync(entity.Id);
      }
  
      /// <inheritdoc />
0268b0d0   李曜臣   菜单权限模块实现
106
      public async Task<RbacMenuGetListOutputDto> UpdateAsync(string id, [FromBody] RbacMenuUpdateInputVo input)
9df65e38   李曜臣   实现美国版门店与RBAC对外接口
107
      {
0268b0d0   李曜臣   菜单权限模块实现
108
109
110
          var entity = await _dbContext.SqlSugarClient.Queryable<MenuDbEntity>()
              .Where(x => x.Id == id && x.IsDeleted == false)
              .SingleAsync();
9df65e38   李曜臣   实现美国版门店与RBAC对外接口
111
112
113
114
115
116
117
118
119
120
121
122
          if (entity is null)
          {
              throw new UserFriendlyException("权限不存在");
          }
  
          var name = input.MenuName?.Trim();
          if (string.IsNullOrWhiteSpace(name))
          {
              throw new UserFriendlyException("权限名称不能为空");
          }
  
          entity.MenuName = name;
0268b0d0   李曜臣   菜单权限模块实现
123
124
125
          entity.ParentId = string.IsNullOrWhiteSpace(input.ParentId) ? "0" : input.ParentId.Trim();
          entity.MenuType = input.MenuType;
          entity.MenuSource = input.MenuSource;
9df65e38   李曜臣   实现美国版门店与RBAC对外接口
126
127
128
129
130
          entity.PermissionCode = input.PermissionCode?.Trim();
          entity.Router = input.Router?.Trim();
          entity.Component = input.Component?.Trim();
          entity.OrderNum = input.OrderNum;
          entity.State = input.State;
0268b0d0   李曜臣   菜单权限模块实现
131
          entity.LastModificationTime = DateTime.Now;
9df65e38   李曜臣   实现美国版门店与RBAC对外接口
132
  
0268b0d0   李曜臣   菜单权限模块实现
133
134
135
          await _dbContext.SqlSugarClient.Updateable(entity)
              .Where(x => x.Id == entity.Id)
              .ExecuteCommandAsync();
9df65e38   李曜臣   实现美国版门店与RBAC对外接口
136
137
138
139
          return await GetAsync(entity.Id);
      }
  
      /// <inheritdoc />
0268b0d0   李曜臣   菜单权限模块实现
140
      public async Task DeleteAsync([FromBody] List<string> ids)
9df65e38   李曜臣   实现美国版门店与RBAC对外接口
141
      {
0268b0d0   李曜臣   菜单权限模块实现
142
          var idList = ids?.Where(x => !string.IsNullOrWhiteSpace(x)).Select(x => x.Trim()).Distinct().ToList() ?? new List<string>();
9df65e38   李曜臣   实现美国版门店与RBAC对外接口
143
144
145
146
147
          if (idList.Count == 0)
          {
              return;
          }
  
0268b0d0   李曜臣   菜单权限模块实现
148
149
150
151
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
          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对外接口
235
236
      }
  }