sunpengfei
2025-08-07 a7287db2ada9ef606db5fe81cc8b66e2da44d345
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
using FlexJobApi.Core;
using Furion.DatabaseAccessor;
using Furion.FriendlyException;
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 GetUserInfoRolesQueryHandler(
            IRepository<Role> repRole,
            IRepository<UserInfo> repUserInfo,
            IRepository<UserInfoRole> repUserInfoRole
        ) : IRequestHandler<GetUserInfoRolesQuery, List<GetUserInfoRolesQueryResultItem>>
    {
        private readonly IRepository<Role> repRole = repRole;
        private readonly IRepository<UserInfo> repUserInfo = repUserInfo;
        private readonly IRepository<UserInfoRole> repUserInfoRole = repUserInfoRole;
 
        /// <inheritdoc/>
        public async Task<List<GetUserInfoRolesQueryResultItem>> Handle(GetUserInfoRolesQuery request, CancellationToken cancellationToken)
        {
            var userInfo = await repUserInfo.AsQueryable().AsNoTracking()
                .Where(it => it.Id == request.UserInfoId)
                .Select(it => new
                {
                    it.Type,
                })
                .FirstOrDefaultAsync(cancellationToken);
            if (userInfo == null) throw Oops.Oh(EnumErrorCodeType.s404, "该用户信息");
            var roleIds = await repUserInfoRole.AsQueryable().AsNoTracking()
                .Where(it => it.UserInfoId == request.UserInfoId)
                .Select(it => it.RoleId)
                .ToListAsync(cancellationToken);
            var roles = await repRole.AsQueryable().AsNoTracking()
                .Where(it => it.UserType == userInfo.Type && it.ClientType == request.ClientType)
                .Select(it => new GetUserInfoRolesQueryResultItem
                {
                    Id = it.Id,
                    Name = it.Name,
                    Remark = it.Remark,
                })
                .ToListAsync(cancellationToken);
            foreach (var role in roles)
            {
                role.IsChecked = roleIds.Contains(role.Id);
            }
            return roles;
        }
    }
}