| | |
| | | using System; |
| | | using FlexJobApi.Core; |
| | | using Furion.DatabaseAccessor; |
| | | using Mapster; |
| | | using MediatR; |
| | | using Microsoft.EntityFrameworkCore; |
| | | using System; |
| | | using System.Collections.Generic; |
| | | using System.Linq; |
| | | using System.Text; |
| | |
| | | |
| | | namespace FlexJobApi.User.Application |
| | | { |
| | | public class GetRolesQueryHandler |
| | | /// <summary> |
| | | /// 查询角色分页列表 |
| | | /// </summary> |
| | | /// <param name="repRole"></param> |
| | | /// <param name="repUserInfoRole"></param> |
| | | public class GetRolesQueryHandler( |
| | | IRepository<Role> repRole, |
| | | IRepository<UserInfoRole> repUserInfoRole |
| | | ) : IRequestHandler<GetRolesQuery, PagedListQueryResult<GetRolesQueryResultItem>> |
| | | { |
| | | private readonly IRepository<Role> repRole = repRole; |
| | | private readonly IRepository<UserInfoRole> repUserInfoRole = repUserInfoRole; |
| | | |
| | | /// <inheritdoc/> |
| | | public async Task<PagedListQueryResult<GetRolesQueryResultItem>> Handle(GetRolesQuery request, CancellationToken cancellationToken) |
| | | { |
| | | var result = await request.PageModel.GetPagedListAsync<Role, GetRolesQueryResultItem>( |
| | | q => |
| | | { |
| | | q = q.OrderBy(it => it.Sort).ThenBy(it => it.CreatedTime); |
| | | if (request.UserType.HasValue) |
| | | { |
| | | q = q.Where(it => it.UserType == request.UserType); |
| | | } |
| | | if (request.ClientType.HasValue) |
| | | { |
| | | q = q.Where(it => it.ClientType == request.ClientType); |
| | | } |
| | | if (request.Keywords.IsNotNull()) |
| | | { |
| | | q = q.Where(it => it.Name.Contains(request.Keywords) || it.Remark.Contains(request.Keywords)); |
| | | } |
| | | return q; |
| | | }, cancellationToken: cancellationToken); |
| | | |
| | | if (result.Data.Any()) |
| | | { |
| | | var ids = result.Data.DistinctSelect(it => it.Id); |
| | | var userInfoRoles = await repUserInfoRole.AsQueryable().AsNoTracking() |
| | | .Where(it => ids.Contains(it.RoleId)) |
| | | .Select(it => new |
| | | { |
| | | it.UserInfoId, |
| | | it.RoleId |
| | | }) |
| | | .ToListAsync(); |
| | | foreach (var item in result.Data) |
| | | { |
| | | item.UserCount = userInfoRoles.Count(it => it.RoleId == item.Id); |
| | | } |
| | | } |
| | | |
| | | return result; |
| | | } |
| | | } |
| | | } |