diff --git a/src/Phoenix.Services/Handlers/Reports/Queries/GetPlcReportHandler.cs b/src/Phoenix.Services/Handlers/Reports/Queries/GetPlcReportHandler.cs index 300fd15..232457d 100644 --- a/src/Phoenix.Services/Handlers/Reports/Queries/GetPlcReportHandler.cs +++ b/src/Phoenix.Services/Handlers/Reports/Queries/GetPlcReportHandler.cs @@ -11,6 +11,7 @@ using Phoenix.Models.Reports.Queries; using Phoenix.Services.Extensions; using Phoenix.Services.Handlers.Base; +using Phoenix.Services.Helpers; using Phoenix.Services.Mappings; using Phoenix.Services.Reports.Base; using Phoenix.Services.Repositories; @@ -90,19 +91,21 @@ private void CreateDeviceSheets(ExcelWorksheets sheets, IReadOnlyCollection devices, DateOnly date, ITypeProcessor typeProcessor, CancellationToken cancellationToken) { - IEnumerable plcTasks = devices + IEnumerable plcTypes = devices .Select(x => x.PlcType) - .Distinct() - .Select(x => _plcProcessors[x].FillDataAsync(sheets, date, typeProcessor, cancellationToken)); + .Distinct(); - await Task.WhenAll(plcTasks); + foreach (PlcType plcType in plcTypes) + { + await _plcProcessors[plcType].FillDataAsync(_uow, sheets, date, typeProcessor, cancellationToken); + }; } private static void CreateResultSheets(ExcelWorksheets sheets, IReadOnlyCollection devices, DateOnly date, ITypeProcessor typeProcessor) { foreach (IGrouping group in devices.GroupBy(x => x.LocationName)) { - ExcelWorksheet groupSheet = sheets.Copy(PlcProcessorBase.BaseSheet, group.Key); + ExcelWorksheet groupSheet = sheets.Copy(PlcHandlerHelper.BaseSheet, group.Key); foreach (DeviceReportDto device in group) { diff --git a/src/Phoenix.Services/Helpers/PlcHandlerHelper.cs b/src/Phoenix.Services/Helpers/PlcHandlerHelper.cs index 36c4c84..6a54558 100644 --- a/src/Phoenix.Services/Helpers/PlcHandlerHelper.cs +++ b/src/Phoenix.Services/Helpers/PlcHandlerHelper.cs @@ -1,5 +1,7 @@ using System; +using System.Collections.Generic; using System.Linq; +using System.Linq.Expressions; using System.Threading; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; @@ -8,7 +10,10 @@ using Phoenix.Entities.Devices; using Phoenix.Entities.Users; using Phoenix.Models.Base.Commands; +using Phoenix.Models.Base.Dto; using Phoenix.Models.Base.Queries; +using Phoenix.Models.Plcs; +using Phoenix.Services.Reports.Base; using Phoenix.Services.Repositories; using Phoenix.Shared.Extensions; using Phoenix.Shared.Results; @@ -17,6 +22,10 @@ namespace Phoenix.Services.Helpers { internal static class PlcHandlerHelper { + public const string BaseSheet = "Base"; + public const string MeterSheet = "Meter"; + public const string PlcSheet = "Plc"; + public static async Task AddPlcAsync(UnitOfWork uow, CreatePlcCommandBase request, T plc, DateTime serverDate, CancellationToken cancellationToken) where T : PlcBase { Client? client = await uow.Client @@ -72,6 +81,27 @@ public static IQueryable GetPlcChartQuery(DbSet plcs, GetPlcChartQueryB ); } + public static async Task> GetPlcDataAsync(DbSet plc, Tuple range, ITypeProcessor typeProcessor, Expression, R>> selector, CancellationToken cancellationToken) where S : PlcBase where R : PlcReportDtoBase + { + IReadOnlyCollection result = await plc + .AsNoTracking() + .Include(x => x.Device) + .ThenInclude(x => x.Location) + .Where(x => + x.Date >= range.Item1 && + x.Date < range.Item2 && + x.Device.IncludeReport && + x.Device.Location.IncludeReport + ) + .GroupBy(typeProcessor.GetPlcGroup()) + .Select(selector) + .ToArrayAsync(cancellationToken); + + return result + .GroupBy(x => x.DeviceId) + .ToDictionary(x => x.Key, x => x.OrderBy(x => x.Date).ToArray()); + } + public static IQueryable GetPlcLastQuery(DbSet plcs, GetPlcLastQueryBase request) where T : PlcBase { return plcs diff --git a/src/Phoenix.Services/Reports/Base/IPlcProcessor.cs b/src/Phoenix.Services/Reports/Base/IPlcProcessor.cs index 47269e0..8467c55 100644 --- a/src/Phoenix.Services/Reports/Base/IPlcProcessor.cs +++ b/src/Phoenix.Services/Reports/Base/IPlcProcessor.cs @@ -2,6 +2,7 @@ using System.Threading; using System.Threading.Tasks; using OfficeOpenXml; +using Phoenix.Services.Repositories; namespace Phoenix.Services.Reports.Base { @@ -9,6 +10,6 @@ internal interface IPlcProcessor { string TemplateSheetName { get; } - Task FillDataAsync(ExcelWorksheets sheets, DateOnly date, ITypeProcessor typeProcessor, CancellationToken cancellationToken); + Task FillDataAsync(UnitOfWork uow, ExcelWorksheets sheets, DateOnly date, ITypeProcessor typeProcessor, CancellationToken cancellationToken); } } diff --git a/src/Phoenix.Services/Reports/Base/PlcProcessorBase.cs b/src/Phoenix.Services/Reports/Base/PlcProcessorBase.cs deleted file mode 100644 index 462045a..0000000 --- a/src/Phoenix.Services/Reports/Base/PlcProcessorBase.cs +++ /dev/null @@ -1,47 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Linq.Expressions; -using System.Threading; -using System.Threading.Tasks; -using Microsoft.EntityFrameworkCore; -using Phoenix.Entities.Base; -using Phoenix.Models.Base.Dto; -using Phoenix.Models.Plcs; -using Phoenix.Services.Handlers.Base; -using Phoenix.Services.Repositories; - -namespace Phoenix.Services.Reports.Base -{ - internal abstract class PlcProcessorBase : HandlerBase - { - public const string BaseSheet = "Base"; - public const string MeterSheet = "Meter"; - public const string PlcSheet = "Plc"; - - public PlcProcessorBase(UnitOfWork uow) : base(uow) - { - } - - protected static async Task> GetPlcDataAsync(DbSet plc, Tuple range, ITypeProcessor typeProcessor, Expression, R>> selector, CancellationToken cancellationToken) where S : PlcBase where R : PlcReportDtoBase - { - IReadOnlyCollection result = await plc - .AsNoTracking() - .Include(x => x.Device) - .ThenInclude(x => x.Location) - .Where(x => - x.Date >= range.Item1 && - x.Date < range.Item2 && - x.Device.IncludeReport && - x.Device.Location.IncludeReport - ) - .GroupBy(typeProcessor.GetPlcGroup()) - .Select(selector) - .ToArrayAsync(cancellationToken); - - return result - .GroupBy(x => x.DeviceId) - .ToDictionary(x => x.Key, x => x.OrderBy(x => x.Date).ToArray()); - } - } -} diff --git a/src/Phoenix.Services/Reports/Plcs/ClimatixPlcProcessor.cs b/src/Phoenix.Services/Reports/Plcs/ClimatixPlcProcessor.cs index 97fc739..3a5b11b 100644 --- a/src/Phoenix.Services/Reports/Plcs/ClimatixPlcProcessor.cs +++ b/src/Phoenix.Services/Reports/Plcs/ClimatixPlcProcessor.cs @@ -5,6 +5,7 @@ using System.Threading.Tasks; using OfficeOpenXml; using Phoenix.Models.Plcs.Climatixs.Dto; +using Phoenix.Services.Helpers; using Phoenix.Services.Mappings; using Phoenix.Services.Reports.Base; using Phoenix.Services.Repositories; @@ -13,20 +14,20 @@ namespace Phoenix.Services.Reports.Plcs { - internal sealed class ClimatixPlcProcessor : PlcProcessorBase, IPlcProcessor + internal sealed class ClimatixPlcProcessor : IPlcProcessor { public string TemplateSheetName { get; } - public ClimatixPlcProcessor(UnitOfWork uow) : base(uow) + public ClimatixPlcProcessor() { - TemplateSheetName = PlcSheet; + TemplateSheetName = PlcHandlerHelper.PlcSheet; } - public async Task FillDataAsync(ExcelWorksheets sheets, DateOnly date, ITypeProcessor typeProcessor, CancellationToken cancellationToken) + public async Task FillDataAsync(UnitOfWork uow, ExcelWorksheets sheets, DateOnly date, ITypeProcessor typeProcessor, CancellationToken cancellationToken) { Tuple range = typeProcessor.GetRange(date); - IReadOnlyDictionary plcData = await GetPlcDataAsync(_uow.Climatix, range, typeProcessor, ClimatixMappings.ToClimatixReportDto, cancellationToken); + IReadOnlyDictionary plcData = await PlcHandlerHelper.GetPlcDataAsync(uow.Climatix, range, typeProcessor, ClimatixMappings.ToClimatixReportDto, cancellationToken); foreach (KeyValuePair plc in plcData) { FillData(sheets[plc.Key.ToString()], plc.Value, typeProcessor); diff --git a/src/Phoenix.Services/Reports/Plcs/KamstrupPlcProcessor.cs b/src/Phoenix.Services/Reports/Plcs/KamstrupPlcProcessor.cs index 51e43d2..624867e 100644 --- a/src/Phoenix.Services/Reports/Plcs/KamstrupPlcProcessor.cs +++ b/src/Phoenix.Services/Reports/Plcs/KamstrupPlcProcessor.cs @@ -6,6 +6,7 @@ using Microsoft.EntityFrameworkCore; using OfficeOpenXml; using Phoenix.Models.Plcs.Meters.Dto; +using Phoenix.Services.Helpers; using Phoenix.Services.Mappings; using Phoenix.Services.Reports.Base; using Phoenix.Services.Repositories; @@ -13,23 +14,23 @@ namespace Phoenix.Services.Reports.Plcs { - internal sealed class KamstrupPlcProcessor : PlcProcessorBase, IPlcProcessor + internal sealed class KamstrupPlcProcessor : IPlcProcessor { public string TemplateSheetName { get; } - public KamstrupPlcProcessor(UnitOfWork uow) : base(uow) + public KamstrupPlcProcessor() { - TemplateSheetName = MeterSheet; + TemplateSheetName = PlcHandlerHelper.MeterSheet; } - public async Task FillDataAsync(ExcelWorksheets sheets, DateOnly date, ITypeProcessor typeProcessor, CancellationToken cancellationToken) + public async Task FillDataAsync(UnitOfWork uow, ExcelWorksheets sheets, DateOnly date, ITypeProcessor typeProcessor, CancellationToken cancellationToken) { Tuple range = typeProcessor.GetRange(date); - IReadOnlyDictionary plcData = await GetPlcDataAsync(_uow.Kamstrup, range, typeProcessor, KamstrupMappings.ToKamstrupReportDto, cancellationToken); + IReadOnlyDictionary plcData = await PlcHandlerHelper.GetPlcDataAsync(uow.Kamstrup, range, typeProcessor, KamstrupMappings.ToKamstrupReportDto, cancellationToken); foreach (KeyValuePair plc in plcData) { - KamstrupDto before = await GetBeforeDataAsync(plc.Key, range.Item1, cancellationToken); + KamstrupDto before = await GetBeforeDataAsync(uow, plc.Key, range.Item1, cancellationToken); FillData(sheets[plc.Key.ToString()], before, plc.Value, typeProcessor); } } @@ -86,9 +87,9 @@ private static void FillData(ExcelWorksheet sheet, KamstrupDto before, IReadOnly sheet.Cells[sheet.Dimension.Rows, 14].Value = plcData.Max(x => x.EnergySummaryMax) - before.EnergySummary; } - private async Task GetBeforeDataAsync(int deviceId, DateTime date, CancellationToken cancellationToken) + private async Task GetBeforeDataAsync(UnitOfWork uow, int deviceId, DateTime date, CancellationToken cancellationToken) { - KamstrupDto? before = await _uow.Kamstrup + KamstrupDto? before = await uow.Kamstrup .AsNoTracking() .Include(x => x.Device) .Where(x => @@ -104,7 +105,7 @@ private async Task GetBeforeDataAsync(int deviceId, DateTime date, return before; } - return await _uow.Kamstrup + return await uow.Kamstrup .AsNoTracking() .Include(x => x.Device) .Where(x => diff --git a/src/Phoenix.Services/Reports/Plcs/Rvd145PlcProcessor.cs b/src/Phoenix.Services/Reports/Plcs/Rvd145PlcProcessor.cs index 0305b78..942ae35 100644 --- a/src/Phoenix.Services/Reports/Plcs/Rvd145PlcProcessor.cs +++ b/src/Phoenix.Services/Reports/Plcs/Rvd145PlcProcessor.cs @@ -5,6 +5,7 @@ using System.Threading.Tasks; using OfficeOpenXml; using Phoenix.Models.Plcs.Rvds.Dto; +using Phoenix.Services.Helpers; using Phoenix.Services.Mappings; using Phoenix.Services.Reports.Base; using Phoenix.Services.Repositories; @@ -14,20 +15,20 @@ namespace Phoenix.Services.Reports.Plcs { - internal sealed class Rvd145PlcProcessor : PlcProcessorBase, IPlcProcessor + internal sealed class Rvd145PlcProcessor : IPlcProcessor { public string TemplateSheetName { get; } - public Rvd145PlcProcessor(UnitOfWork uow) : base(uow) + public Rvd145PlcProcessor() { - TemplateSheetName = PlcSheet; + TemplateSheetName = PlcHandlerHelper.PlcSheet; } - public async Task FillDataAsync(ExcelWorksheets sheets, DateOnly date, ITypeProcessor typeProcessor, CancellationToken cancellationToken) + public async Task FillDataAsync(UnitOfWork uow, ExcelWorksheets sheets, DateOnly date, ITypeProcessor typeProcessor, CancellationToken cancellationToken) { Tuple range = typeProcessor.GetRange(date); - IReadOnlyDictionary plcData = await GetPlcDataAsync(_uow.Rvd145, range, typeProcessor, Rvd145Mappings.ToRvd145ReportDto, cancellationToken); + IReadOnlyDictionary plcData = await PlcHandlerHelper.GetPlcDataAsync(uow.Rvd145, range, typeProcessor, Rvd145Mappings.ToRvd145ReportDto, cancellationToken); foreach (KeyValuePair plc in plcData) { FillData(sheets[plc.Key.ToString()], plc.Value, typeProcessor);