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;
using System.Threading.Tasks;
namespace FlexJobApi.User.Application
{
///
/// 查询角色分页列表
///
///
///
public class GetRolesQueryHandler(
IRepository repRole,
IRepository repUserInfoRole
) : IRequestHandler>
{
private readonly IRepository repRole = repRole;
private readonly IRepository repUserInfoRole = repUserInfoRole;
///
public async Task> Handle(GetRolesQuery request, CancellationToken cancellationToken)
{
var result = await request.PageModel.GetPagedListAsync(
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;
}
}
}