sunpengfei
2025-08-12 d77f3d84ce7cd4e06dcc98653b38370bac2b4926
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
using 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.UserServer.Application.Menus.Queries
{
    /// <summary>
    /// 查询菜单详情
    /// </summary>
    /// <param name="rep"></param>
    public class GetMenuQueryHandler(IRepository<Menu> rep)
        : IRequestHandler<GetMenuQuery, GetMenuQueryResult>
    {
        private readonly IRepository<Menu> rep = rep;
 
        /// <inheritdoc/>
        public async Task<GetMenuQueryResult> Handle(GetMenuQuery request, CancellationToken cancellationToken)
        {
            var model = await rep.AsQueryable().AsNoTracking()
                .Where(it => it.Id == request.Id)
                .ProjectToType<GetMenuQueryResult>()
                .FirstOrDefaultAsync(cancellationToken);
            if (model == null) throw Oops.Oh(EnumErrorCodeType.s404, "该菜单");
            var roleMenuIds =
                request.RoleId.HasValue
                ? rep.Change<RoleMenu>().Where(it => it.RoleId == request.RoleId).DistinctSelect(it => it.MenuId)
                : [];
            model.IsChecked = roleMenuIds.Contains(model.Id);
            var items = await rep.AsQueryable().AsNoTracking()
                .OrderBy(it => it.Sort).ThenBy(it => it.CreatedTime)
                .Where(it => it.ParentId == model.Id)
                .Where(it => it.Type == EnumMenuType.Button || it.Type == EnumMenuType.Field)
                .ProjectToType<GetMenuQueryResultItem>()
                .ToListAsync(cancellationToken);
            model.Groups = items.GroupBy(it => it.Group ?? "default")
                .Select(it => new GetMenuQueryResultGroup
                {
                    Group = it.Key,
                    ButtonLocations = it
                        .Where(l => l.Type == EnumMenuType.Button)
                        .GroupBy(l => l.Location ?? "default")
                        .Select(l => new GetMenuQueryResultButtonLocation
                        {
                            Location = l.Key,
                            Buttons = l
                                .Select(b =>
                                {
                                    b.IsChecked = roleMenuIds.Contains(b.Id);
                                    return b;
                                })
                                .ToList()
                                .Adapt<List<GetMenuQueryResultButton>>()
                        })
                        .ToList(),
                    Fields = it
                        .Where(f => f.Type == EnumMenuType.Field)
                        .Select(f =>
                        {
                            f.IsChecked = roleMenuIds.Contains(f.Id);
                            return f;
                        })
                        .ToList()
                        .Adapt<List<GetMenuQueryResultField>>()
                })
                .ToList();
            return model;
        }
    }
}