怎樣優雅地增刪查改(五):按組織架構查詢

2023-07-13 21:00:39

@


之前我們實現了Employee,Alarm管理模組以及通用查詢應用層。

Employee的集合查詢業務,是通過重寫CreateFilteredQueryAsync方法,來實現按組織架構查詢的過濾條件。

我們將這段邏輯程式碼提取到通用查詢應用層中,便可實現在任何業務的按組織架構查詢。

原理

EmployeeAppService中,CreateFilteredQueryAsync方法組織架構的過濾條件程式碼如下:

var organizationUnitUsers = await organizationUnitAppService.GetOrganizationUnitUsersAsync(new GetOrganizationUnitUsersInput()
{
    Id = input.OrganizationUnitId.Value
});
if (organizationUnitUsers.Count() > 0)
{
    var ids = organizationUnitUsers.Select(c => c.Id);
    query = query.Where(t => ids.Contains(t.Id));
}
else
{
    query = query.Where(c => false);
}

CreateFilteredQueryAsync是通過業務使用者的IRepository獲取實體的IQueryable 然後通過query.Where()實現了按組織架構的過濾條件。

IQueryable是一泛型類介面,泛型引數是實體類。要想在任意實體實現Where的過濾條件,我們使用動態拼接語言整合查詢 (LINQ) 的方式實現通用查詢介面,有關LINQ表示式,請閱讀 LINQ 教學和有關 Lambda 表示式的文章。

實現

定義按組織架構查詢過濾器(IOrganizationOrientedFilter)介面,查詢實體列表Dto若實現該介面,將篩選指定 OrganizationUnitId 下的使用者關聯的實體。

public interface IOrganizationOrientedFilter
{
    Guid? OrganizationUnitId { get; set; }
}

重寫CreateFilteredQueryAsync方法,程式碼如下


protected override async Task<IQueryable<TEntity>> CreateFilteredQueryAsync(TGetListInput input)
{
    var query = await ReadOnlyRepository.GetQueryableAsync();

    query = await ApplyOrganizationOrientedFiltered(query,input);

    return query;
}

對於OrganizationUnit服務,其依賴關係在應用層,查詢指定組織架構的使用者將在CurdAppServiceBase的子類實現。建立一個抽象方法GetUserIdsByOrganizationAsync

protected abstract Task<IEnumerable<Guid>> GetUserIdsByOrganizationAsync(Guid organizationUnitId)

建立應用過濾條件方法:ApplyOrganizationOrientedFiltered,在此實現拼接LINQ表示式,程式碼如下:

protected virtual async Task<IQueryable<TEntity>> ApplyOrganizationOrientedFiltered(IQueryable<TEntity> query, TGetListInput input)
{
    if (input is IOrganizationOrientedFilter && HasProperty<TEntity>("UserId"))
    {
        var property = typeof(TEntity).GetProperty("UserId");
        var filteredInput = input as IOrganizationOrientedFilter;
        if (filteredInput != null && filteredInput.OrganizationUnitId.HasValue)
        {

            var ids = await GetUserIdsByOrganizationAsync(filteredInput.OrganizationUnitId.Value);
            Expression originalExpression = null;
            var parameter = Expression.Parameter(typeof(TEntity), "p");
            foreach (var id in ids)
            {
                var keyConstantExpression = Expression.Constant(id, typeof(Guid));
                var propertyAccess = Expression.MakeMemberAccess(parameter, property);
                var expressionSegment = Expression.Equal(propertyAccess, keyConstantExpression);

                if (originalExpression == null)
                {
                    originalExpression = expressionSegment;
                }
                else
                {
                    originalExpression = Expression.Or(originalExpression, expressionSegment);
                }
            }

            var equalExpression = originalExpression != null ?
                    Expression.Lambda<Func<TEntity, bool>>(originalExpression, parameter)
                    : p => false;

            query = query.Where(equalExpression);

        }

    }
    return query;
}

請注意,可應用過濾的條件為:

  1. input需實現IOrganizationOrientedFilter介面
  2. 實體必須包含UserId欄位

否則將原封不動返回IQueryable物件。

應用

在上一章Alarm管理模組中,我們已經寫好了AlarmAppService,我們需要為其實現GetUserIdsByOrganizationAsync方法。改造AlarmAppService程式碼如下:

public class AlarmAppService : ExtendedCurdAppServiceBase<Matoapp.Health.Alarm.Alarm, AlarmDto, AlarmDto, AlarmBriefDto, long, GetAllAlarmInput, GetAllAlarmInput, CreateAlarmInput, UpdateAlarmInput>, IAlarmAppService
{
    private readonly IOrganizationUnitAppService organizationUnitAppService;

    public AlarmAppService(
        IOrganizationUnitAppService organizationUnitAppService,
        IRepository<Matoapp.Health.Alarm.Alarm, long> basicInventoryRepository) : base(basicInventoryRepository)
    {
        this.organizationUnitAppService = organizationUnitAppService;
    }

    protected override async Task<IEnumerable<Guid>> GetUserIdsByOrganizationAsync(Guid organizationUnitId)
    {
        var organizationUnitUsers = await organizationUnitAppService.GetOrganizationUnitUsersAsync(new GetOrganizationUnitUsersInput()
        {
            Id = organizationUnitId
        });

        var ids = organizationUnitUsers.Select(c => c.Id);
        return ids;
    }
}

測試

建立一些組織架構,命名「群組」

在不同「群組」下建立一些客戶(Client)

在告警管理頁面中,建立一些告警,並將這些告警分配給不同的客戶

在客戶管理中,通過選擇不同的組織架構,查詢當前「群組」下的客戶告警