From 98e9f94e68379acd02e54a4ee5349eea1395c994 Mon Sep 17 00:00:00 2001 From: colin Date: Thu, 5 Sep 2024 16:08:22 +0800 Subject: [PATCH] feat(task-management): Use specification to query the list of job logs --- .../BackgroundJobLogAppService.cs | 46 +++++++++++++------ .../Expressions/ExpressionFuncExtensions.cs | 32 +++++++++++++ .../TaskManagement/BackgroundJobLogFilter.cs | 35 -------------- .../IBackgroundJobLogRepository.cs | 18 +++----- .../EfCoreBackgroundJobLogRepository.cs | 31 +++---------- 5 files changed, 78 insertions(+), 84 deletions(-) create mode 100644 aspnet-core/modules/task-management/LINGYUN.Abp.TaskManagement.Application/System/Linq/Expressions/ExpressionFuncExtensions.cs delete mode 100644 aspnet-core/modules/task-management/LINGYUN.Abp.TaskManagement.Domain/LINGYUN/Abp/TaskManagement/BackgroundJobLogFilter.cs diff --git a/aspnet-core/modules/task-management/LINGYUN.Abp.TaskManagement.Application/LINGYUN/Abp/TaskManagement/BackgroundJobLogAppService.cs b/aspnet-core/modules/task-management/LINGYUN.Abp.TaskManagement.Application/LINGYUN/Abp/TaskManagement/BackgroundJobLogAppService.cs index 837e68f8c..447e6b09a 100644 --- a/aspnet-core/modules/task-management/LINGYUN.Abp.TaskManagement.Application/LINGYUN/Abp/TaskManagement/BackgroundJobLogAppService.cs +++ b/aspnet-core/modules/task-management/LINGYUN.Abp.TaskManagement.Application/LINGYUN/Abp/TaskManagement/BackgroundJobLogAppService.cs @@ -1,6 +1,8 @@ using LINGYUN.Abp.TaskManagement.Permissions; using Microsoft.AspNetCore.Authorization; +using System; using System.Collections.Generic; +using System.Linq.Expressions; using System.Threading.Tasks; using Volo.Abp.Application.Dtos; @@ -32,22 +34,38 @@ public class BackgroundJobLogAppService : TaskManagementApplicationService, IBac public async virtual Task> GetListAsync(BackgroundJobLogGetListInput input) { - var filter = new BackgroundJobLogFilter - { - BeginRunTime = input.BeginRunTime, - EndRunTime = input.EndRunTime, - HasExceptions = input.HasExceptions, - Filter = input.Filter, - Group = input.Group, - Name = input.Name, - Type = input.Type - }; - - var totalCount = await BackgroundJobLogRepository.GetCountAsync(filter, input.JobId); + var specification = new BackgroundJobLogGetListSpecification(input); + + var totalCount = await BackgroundJobLogRepository.GetCountAsync(specification); var backgroundJobLogs = await BackgroundJobLogRepository.GetListAsync( - filter, input.JobId, input.Sorting, input.MaxResultCount, input.SkipCount); + specification, input.Sorting, input.MaxResultCount, input.SkipCount); return new PagedResultDto(totalCount, - ObjectMapper.Map, List>(backgroundJobLogs)); + ObjectMapper.Map, List>(backgroundJobLogs)); + } + + private class BackgroundJobLogGetListSpecification : Volo.Abp.Specifications.Specification + { + protected BackgroundJobLogGetListInput Input { get; } + public BackgroundJobLogGetListSpecification(BackgroundJobLogGetListInput input) + { + Input = input; + } + + public override Expression> ToExpression() + { + Expression> expression = _ => true; + + return expression + .AndIf(!Input.JobId.IsNullOrWhiteSpace(), x => x.JobId.Equals(Input.JobId)) + .AndIf(!Input.Type.IsNullOrWhiteSpace(), x => x.JobType.Contains(Input.Type)) + .AndIf(!Input.Group.IsNullOrWhiteSpace(), x => x.JobGroup.Equals(Input.Group)) + .AndIf(!Input.Name.IsNullOrWhiteSpace(), x => x.JobName.Equals(Input.Name)) + .AndIf(!Input.Filter.IsNullOrWhiteSpace(), x => x.JobName.Contains(Input.Filter) || + x.JobGroup.Contains(Input.Filter) || x.JobType.Contains(Input.Filter) || x.Message.Contains(Input.Filter)) + .AndIf(Input.HasExceptions.HasValue, x => !string.IsNullOrWhiteSpace(x.Exception)) + .AndIf(Input.BeginRunTime.HasValue, x => x.RunTime >= Input.BeginRunTime) + .AndIf(Input.EndRunTime.HasValue, x => x.RunTime <= Input.EndRunTime); + } } } diff --git a/aspnet-core/modules/task-management/LINGYUN.Abp.TaskManagement.Application/System/Linq/Expressions/ExpressionFuncExtensions.cs b/aspnet-core/modules/task-management/LINGYUN.Abp.TaskManagement.Application/System/Linq/Expressions/ExpressionFuncExtensions.cs new file mode 100644 index 000000000..fd7a017ea --- /dev/null +++ b/aspnet-core/modules/task-management/LINGYUN.Abp.TaskManagement.Application/System/Linq/Expressions/ExpressionFuncExtensions.cs @@ -0,0 +1,32 @@ +using Volo.Abp.Specifications; + +namespace System.Linq.Expressions; + +internal static class ExpressionFuncExtensions +{ + public static Expression> AndIf( + this Expression> first, + bool condition, + Expression> second) + { + if (condition) + { + return ExpressionFuncExtender.And(first, second); + } + + return first; + } + + public static Expression> OrIf( + this Expression> first, + bool condition, + Expression> second) + { + if (condition) + { + return ExpressionFuncExtender.Or(first, second); + } + + return first; + } +} diff --git a/aspnet-core/modules/task-management/LINGYUN.Abp.TaskManagement.Domain/LINGYUN/Abp/TaskManagement/BackgroundJobLogFilter.cs b/aspnet-core/modules/task-management/LINGYUN.Abp.TaskManagement.Domain/LINGYUN/Abp/TaskManagement/BackgroundJobLogFilter.cs deleted file mode 100644 index 99bbdd984..000000000 --- a/aspnet-core/modules/task-management/LINGYUN.Abp.TaskManagement.Domain/LINGYUN/Abp/TaskManagement/BackgroundJobLogFilter.cs +++ /dev/null @@ -1,35 +0,0 @@ -using System; - -namespace LINGYUN.Abp.TaskManagement; - -public class BackgroundJobLogFilter -{ - /// - /// 其他过滤条件 - /// - public string Filter { get; set; } - /// - /// 存在异常 - /// - public bool? HasExceptions { get; set; } - /// - /// 任务名称 - /// - public string Name { get; set; } - /// - /// 任务分组 - /// - public string Group { get; set; } - /// - /// 任务类型 - /// - public string Type { get; set; } - /// - /// 开始触发时间 - /// - public DateTime? BeginRunTime { get; set; } - /// - /// 结束触发时间 - /// - public DateTime? EndRunTime { get; set; } -} diff --git a/aspnet-core/modules/task-management/LINGYUN.Abp.TaskManagement.Domain/LINGYUN/Abp/TaskManagement/IBackgroundJobLogRepository.cs b/aspnet-core/modules/task-management/LINGYUN.Abp.TaskManagement.Domain/LINGYUN/Abp/TaskManagement/IBackgroundJobLogRepository.cs index bc11de3cf..9a517f173 100644 --- a/aspnet-core/modules/task-management/LINGYUN.Abp.TaskManagement.Domain/LINGYUN/Abp/TaskManagement/IBackgroundJobLogRepository.cs +++ b/aspnet-core/modules/task-management/LINGYUN.Abp.TaskManagement.Domain/LINGYUN/Abp/TaskManagement/IBackgroundJobLogRepository.cs @@ -1,8 +1,8 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using Volo.Abp.Domain.Repositories; +using Volo.Abp.Specifications; namespace LINGYUN.Abp.TaskManagement; @@ -11,28 +11,24 @@ public interface IBackgroundJobLogRepository : IRepository /// 获取过滤后的任务日志数量 /// - /// - /// + /// /// /// Task GetCountAsync( - BackgroundJobLogFilter filter, - string jobId = null, + ISpecification specification, CancellationToken cancellationToken = default); /// /// 获取过滤后的任务日志列表 /// - /// - /// + /// /// /// /// /// /// Task> GetListAsync( - BackgroundJobLogFilter filter, - string jobId = null, - string sorting = nameof(BackgroundJobLog.RunTime), + ISpecification specification, + string sorting = $"{nameof(BackgroundJobLog.RunTime)} DESC", int maxResultCount = 10, int skipCount = 0, CancellationToken cancellationToken = default); diff --git a/aspnet-core/modules/task-management/LINGYUN.Abp.TaskManagement.EntityFrameworkCore/LINGYUN/Abp/TaskManagement/EntityFrameworkCore/EfCoreBackgroundJobLogRepository.cs b/aspnet-core/modules/task-management/LINGYUN.Abp.TaskManagement.EntityFrameworkCore/LINGYUN/Abp/TaskManagement/EntityFrameworkCore/EfCoreBackgroundJobLogRepository.cs index 94e35ce0d..a22494057 100644 --- a/aspnet-core/modules/task-management/LINGYUN.Abp.TaskManagement.EntityFrameworkCore/LINGYUN/Abp/TaskManagement/EntityFrameworkCore/EfCoreBackgroundJobLogRepository.cs +++ b/aspnet-core/modules/task-management/LINGYUN.Abp.TaskManagement.EntityFrameworkCore/LINGYUN/Abp/TaskManagement/EntityFrameworkCore/EfCoreBackgroundJobLogRepository.cs @@ -7,6 +7,7 @@ using System.Threading; using System.Threading.Tasks; using Volo.Abp.Domain.Repositories.EntityFrameworkCore; using Volo.Abp.EntityFrameworkCore; +using Volo.Abp.Specifications; namespace LINGYUN.Abp.TaskManagement.EntityFrameworkCore; @@ -21,45 +22,27 @@ public class EfCoreBackgroundJobLogRepository : } public async virtual Task GetCountAsync( - BackgroundJobLogFilter filter, - string jobId = null, + ISpecification specification, CancellationToken cancellationToken = default) { return await (await GetDbSetAsync()) - .WhereIf(!jobId.IsNullOrWhiteSpace(), x => x.JobId.Equals(jobId)) - .WhereIf(!filter.Type.IsNullOrWhiteSpace(), x => x.JobType.Contains(filter.Type)) - .WhereIf(!filter.Group.IsNullOrWhiteSpace(), x => x.JobGroup.Equals(filter.Group)) - .WhereIf(!filter.Name.IsNullOrWhiteSpace(), x => x.JobName.Equals(filter.Name)) - .WhereIf(!filter.Filter.IsNullOrWhiteSpace(), x => x.JobName.Contains(filter.Filter) || - x.JobGroup.Contains(filter.Filter) || x.JobType.Contains(filter.Filter) || x.Message.Contains(filter.Filter)) - .WhereIf(filter.HasExceptions.HasValue, x => !string.IsNullOrWhiteSpace(x.Exception)) - .WhereIf(filter.BeginRunTime.HasValue, x => x.RunTime.CompareTo(filter.BeginRunTime.Value) >= 0) - .WhereIf(filter.EndRunTime.HasValue, x => x.RunTime.CompareTo(filter.EndRunTime.Value) <= 0) + .Where(specification.ToExpression()) .CountAsync(GetCancellationToken(cancellationToken)); } public async virtual Task> GetListAsync( - BackgroundJobLogFilter filter, - string jobId = null, - string sorting = nameof(BackgroundJobLog.RunTime), + ISpecification specification, + string sorting = $"{nameof(BackgroundJobLog.RunTime)} DESC", int maxResultCount = 10, int skipCount = 0, CancellationToken cancellationToken = default) { if (sorting.IsNullOrWhiteSpace()) { - sorting = $"{nameof(BackgroundJobLog.RunTime)}"; + sorting = $"{nameof(BackgroundJobLog.RunTime)} DESC"; } return await (await GetDbSetAsync()) - .WhereIf(!jobId.IsNullOrWhiteSpace(), x => x.JobId.Equals(jobId)) - .WhereIf(!filter.Type.IsNullOrWhiteSpace(), x => x.JobType.Contains(filter.Type)) - .WhereIf(!filter.Group.IsNullOrWhiteSpace(), x => x.JobGroup.Equals(filter.Group)) - .WhereIf(!filter.Name.IsNullOrWhiteSpace(), x => x.JobName.Equals(filter.Name)) - .WhereIf(!filter.Filter.IsNullOrWhiteSpace(), x => x.JobName.Contains(filter.Filter) || - x.JobGroup.Contains(filter.Filter) || x.JobType.Contains(filter.Filter) || x.Message.Contains(filter.Filter)) - .WhereIf(filter.HasExceptions.HasValue, x => !string.IsNullOrWhiteSpace(x.Exception)) - .WhereIf(filter.BeginRunTime.HasValue, x => x.RunTime.CompareTo(filter.BeginRunTime.Value) >= 0) - .WhereIf(filter.EndRunTime.HasValue, x => x.RunTime.CompareTo(filter.EndRunTime.Value) <= 0) + .Where(specification.ToExpression()) .OrderBy(sorting) .PageBy(skipCount, maxResultCount) .ToListAsync(GetCancellationToken(cancellationToken));