sunpengfei
2025-08-04 031e5ec63349abfd0817ea70304a35fa85719573
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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
235
236
237
238
using FlexJobApi.Core;
using Furion.DatabaseAccessor;
using Furion.DataValidation;
using Furion.FriendlyException;
using Mapster;
using MediatR;
using Microsoft.CodeAnalysis;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
 
namespace FlexJobApi.User.Application
{
    /// <summary>
    /// 保存菜单
    /// </summary>
    public class SaveMenuCommandHandler(
        IRepository<Menu> rep
    ) : IRequestHandler<SaveMenuCommand, Guid>
    {
        private readonly IRepository<Menu> rep = rep;
 
        /// <inheritdoc/>
        public async Task<Guid> Handle(SaveMenuCommand request, CancellationToken cancellationToken)
        {
            if (request.Id.HasValue)
            {
                var entity = await rep.AsQueryable()
                    .Include(it => it.Childrens)
                    .FirstOrDefaultAsync(it => it.Id == request.Id, cancellationToken);
                if (entity == null) throw Oops.Oh(EnumErrorCodeType.s404, "菜单");
                if (entity.UserType != request.UserType) throw Oops.Oh(EnumErrorCodeType.s410, "用户类型");
                if (entity.ClientType != request.ClientType) throw Oops.Oh(EnumErrorCodeType.s410, "客户端类型");
 
                var path = await GetPathAsync(request.ParentId, request.Code, cancellationToken);
                if (await CheckExist(entity, request.ParentId, request.Code)) throw Oops.Oh(EnumErrorCodeType.s405, "菜单编号");
                await UpdateChildrensPath($"{entity.Path}/{entity.Code}", $"{path}/{request.Code}", cancellationToken);
                request.Adapt(entity);
                SaveChildrens(entity, request);
                await rep.UpdateAsync(entity);
 
                return entity.Id;
            }
            else
            {
                var entity = new Menu();
                request.Adapt(entity);
                entity.Path = await GetPathAsync(entity.ParentId, entity.Code, cancellationToken);
                if (await CheckExist(entity, request.ParentId, request.Code)) throw Oops.Oh(EnumErrorCodeType.s405, "菜单编号");
                SaveChildrens(entity, request);
                await rep.InsertAsync(entity);
 
                return entity.Id;
            }
        }
 
        /// <summary>
        /// 保存下级
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="request"></param>
        /// <returns></returns>
        private void SaveChildrens(Menu entity, SaveMenuCommand request)
        {
            // 获取子集Id
            var childrenIds = GetRequestChildrenIds(request);
            // 删除子级
            entity.Childrens = entity.Childrens.Where(it => childrenIds.Contains(it.Id)).ToList();
 
            // 遍历分组
 
            foreach (var group in request.Groups)
            {
                // 遍历按钮位置
                foreach (var buttonLocation in group.ButtonLocations)
                {
                    // 遍历按钮 添加或更新
                    foreach (var button in buttonLocation.Buttons)
                    {
                        var buttonEntity = entity.Childrens.FirstOrDefault(it => it.Id == button.Id);
                        if (buttonEntity == null)
                        {
                            if (button.Id.HasValue) throw Oops.Oh(EnumErrorCodeType.s404, $"当前分组{group.Group}-位置{buttonLocation.Location}-按钮{button.Code}");
                            buttonEntity = new Menu
                            {
                                UserType = entity.UserType,
                                ClientType = entity.ClientType,
                                Type = EnumMenuType.Button,
                            };
                            entity.Childrens.Add(buttonEntity);
                        }
                        else if (buttonEntity.Type != EnumMenuType.Button) throw Oops.Oh(EnumErrorCodeType.s400, $"当前分组{group.Group}-字段{button.Code}并非一个按钮");
                        buttonEntity.Path = $"{entity.Path}{entity.Code}/";
                        buttonEntity.Group = group.Group;
                        buttonEntity.Location = buttonLocation.Location;
                        button.Adapt(buttonEntity);
                    }
                }
 
                // 遍历字段 添加或更新
                foreach (var field in group.Fields)
                {
                    var fieldEntity = entity.Childrens.FirstOrDefault(it => it.Id == field.Id);
                    if (fieldEntity == null)
                    {
                        if (field.Id.HasValue) throw Oops.Oh(EnumErrorCodeType.s404, $"当前分组{group.Group}-字段{field.Code}");
                        fieldEntity = new Menu
                        {
                            UserType = entity.UserType,
                            ClientType = entity.ClientType,
                            Type = EnumMenuType.Field,
                        };
                        entity.Childrens.Add(fieldEntity);
                    }
                    else if (fieldEntity.Type != EnumMenuType.Field) throw Oops.Oh(EnumErrorCodeType.s400, $"当前分组{group.Group}-位置{fieldEntity.Location}-按钮{fieldEntity.Code}并非一个字段");
                    fieldEntity.Path = $"{entity.Path}{entity.Code}/";
                    fieldEntity.Group = group.Group;
                    field.Adapt(fieldEntity);
                }
            }
            CheckRepeatChildrens(entity);
        }
 
        /// <summary>
        /// 校验重复子集
        /// </summary>
        /// <param name="entity"></param>
        private void CheckRepeatChildrens(Menu entity)
        {
            var repeats = entity.Childrens
                .GroupBy(it =>
                {
                    return it.Type == EnumMenuType.Button
                        ? $"{it.Group}:{it.Type}:{it.Location}:{it.Code}"
                        : $"{it.Group}:{it.Type}:{it.Code}";
                })
                .Select(it => new
                {
                    it.Key,
                    Count = it.Count()
                })
                .Where(it => it.Count > 1)
                .SplitJoin(it => it.Key, ",");
            if (repeats.IsNotNull()) throw Oops.Oh(EnumErrorCodeType.s406, repeats);
        }
 
        /// <summary>
        /// 获取请求子集Id
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        private List<Guid> GetRequestChildrenIds(SaveMenuCommand request)
        {
            return request.Groups
                .Select(g =>
                    g.ButtonLocations.SelectMany(bl => bl.Buttons.Where(b => b.Id.HasValue).Select(b => b.Id!.Value))
                    .Union(
                    g.Fields.Where(f => f.Id.HasValue).Select(f => f.Id!.Value)))
                .SelectMany(it => it)
                .ToList();
        }
 
        /// <summary>
        /// 校验菜单是否重复
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="parentId"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        private async Task<bool> CheckExist(Menu entity, Guid? parentId, string code)
        {
            return await rep.AsQueryable().AsNoTracking()
                .AnyAsync(it =>
                    it.UserType == entity.UserType
                    && it.ClientType == entity.ClientType
                    && it.ParentId == parentId
                    && it.Code == code
                    && it.Id != entity.Id);
        }
 
        /// <summary>
        /// 获取地址
        /// </summary>
        /// <param name="parentId"></param>
        /// <param name="code"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        private async Task<string> GetPathAsync(Guid? parentId, string code, CancellationToken cancellationToken)
        {
            if (parentId.HasValue)
            {
                var parent = await rep.AsQueryable().AsNoTracking()
                    .Where(it => it.Id == parentId)
                    .Select(it => new
                    {
                        it.Path,
                        it.Code
                    })
                    .FirstOrDefaultAsync(cancellationToken);
                if (parent == null) throw Oops.Oh(EnumErrorCodeType.s404, "上级菜单");
                return $"{parent.Path}{parent.Code}/";
            }
            else
            {
                return "/";
            }
        }
 
        /// <summary>
        /// 更新下级菜单路径
        /// </summary>
        /// <param name="oldPath"></param>
        /// <param name="newPath"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        private async Task UpdateChildrensPath(string oldPath, string newPath, CancellationToken cancellationToken)
        {
            var models = await rep.AsQueryable()
                .Where(it => it.Path.StartsWith(oldPath))
                .Where(it => it.Type == EnumMenuType.Menu || it.Type == EnumMenuType.Page || it.Type == EnumMenuType.Modal)
                .ToListAsync(cancellationToken);
            if (models.IsNotNull())
            {
                foreach (var model in models)
                {
                    model.Path = model.Path.Replace(oldPath, newPath);
                }
                await rep.UpdateAsync(models);
            }
        }
    }
}