diff --git a/PARR.AIHITMainLoader/Models/PtkData.cs b/PARR.AIHITMainLoader/Models/PtkData.cs index 585ef376..34604a8d 100644 --- a/PARR.AIHITMainLoader/Models/PtkData.cs +++ b/PARR.AIHITMainLoader/Models/PtkData.cs @@ -155,7 +155,7 @@ namespace PARR.AIHITMainLoader.Models { {"IP_АДРЕС", IP }, //{"МЕТКА", Metka }, - //{"АКТИВЕН", IsActive }, + {"АКТИВЕН", IsActive }, //{"ВАЖНЫЙ_ЭК", IsImportant }, //{"ВРЕМЯ_СОЗДАНИЯ", CreateTime.ToString() ?? null}, //{"ДОПОЛНИТЕЛЬНАЯ_ИНФОРМАЦИЯ", AdditionalInfo }, diff --git a/PARR.API/Controllers/V1/RobotTaskController.cs b/PARR.API/Controllers/V1/RobotTaskController.cs index 8d5fd7cd..f925d963 100644 --- a/PARR.API/Controllers/V1/RobotTaskController.cs +++ b/PARR.API/Controllers/V1/RobotTaskController.cs @@ -11,7 +11,7 @@ using PARR.API.Services.Interfaces; using PARR.Common.Domain; using PARR.Constants; using PARR.DAL.Contracts; -using PARR.DAL.DomainServices; +using PARR.DAL.DomainServices.Interfaces; using PARR.DAL.Models; using PARR.DAL.Services.Interfaces; using PARR.DAL.TransformServices; diff --git a/PARR.API/MappingProfiles/Resolvers/RobotTaskTemplateShortCodeResolver.cs b/PARR.API/MappingProfiles/Resolvers/RobotTaskTemplateShortCodeResolver.cs deleted file mode 100644 index 4cb84b9e..00000000 --- a/PARR.API/MappingProfiles/Resolvers/RobotTaskTemplateShortCodeResolver.cs +++ /dev/null @@ -1,23 +0,0 @@ -using AutoMapper; -using PARR.API.Contracts.V1.Responses; -using PARR.DAL.DomainServices; -using PARR.DAL.Models; - -namespace PARR.API.MappingProfiles.Resolvers -{ - public class RobotTaskTemplateShortCodeResolver : IValueResolver - { - private readonly IShortcodesService shortcodesService; - - public RobotTaskTemplateShortCodeResolver( - IShortcodesService shortcodesService - ) - { - this.shortcodesService = shortcodesService; - } - public string Resolve(RobotConfiguration source, RobotTaskTemplateResponse destination, string destMember, ResolutionContext context) - { - throw new NotImplementedException(); - } - } -} diff --git a/PARR.DAL/DomainServices/ShortcodesService.cs b/PARR.DAL/DomainServices/Implementations/ShortcodesService.cs similarity index 98% rename from PARR.DAL/DomainServices/ShortcodesService.cs rename to PARR.DAL/DomainServices/Implementations/ShortcodesService.cs index 0ef7f4b4..690f43aa 100644 --- a/PARR.DAL/DomainServices/ShortcodesService.cs +++ b/PARR.DAL/DomainServices/Implementations/ShortcodesService.cs @@ -1,10 +1,11 @@ using Microsoft.EntityFrameworkCore; using PARR.DAL.Contracts; +using PARR.DAL.DomainServices.Interfaces; using PARR.DAL.Services.Interfaces.Job; using PARR.DAL.Services.Interfaces.Unit; using System.Text.RegularExpressions; -namespace PARR.DAL.DomainServices +namespace PARR.DAL.DomainServices.Implementations { internal class ShortcodesService : IShortcodesService { diff --git a/PARR.DAL/DomainServices/TemplateNameGeneratorService.cs b/PARR.DAL/DomainServices/Implementations/TemplateNameGeneratorService.cs similarity index 98% rename from PARR.DAL/DomainServices/TemplateNameGeneratorService.cs rename to PARR.DAL/DomainServices/Implementations/TemplateNameGeneratorService.cs index 6fe86fe1..a4d96128 100644 --- a/PARR.DAL/DomainServices/TemplateNameGeneratorService.cs +++ b/PARR.DAL/DomainServices/Implementations/TemplateNameGeneratorService.cs @@ -1,10 +1,11 @@ using Microsoft.EntityFrameworkCore; using PARR.DAL.Contracts; +using PARR.DAL.DomainServices.Interfaces; using PARR.DAL.Services.Interfaces.Job; using PARR.DAL.Services.Interfaces.Unit; using System.Text.RegularExpressions; -namespace PARR.DAL.DomainServices +namespace PARR.DAL.DomainServices.Implementations { internal class TemplateNameGeneratorService : ITemplateNameGeneratorService { diff --git a/PARR.DAL/DomainServices/Implementations/UnitFilterService.cs b/PARR.DAL/DomainServices/Implementations/UnitFilterService.cs new file mode 100644 index 00000000..d7f80a5c --- /dev/null +++ b/PARR.DAL/DomainServices/Implementations/UnitFilterService.cs @@ -0,0 +1,133 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Logging; +using PARR.DAL.DomainServices.Interfaces; +using PARR.DAL.Services.Interfaces; +using PARR.DAL.Services.Interfaces.Job; +using PARR.DAL.Services.Interfaces.Unit; + + +namespace PARR.DAL.DomainServices.Implementations +{ + internal class UnitFilterService : IUnitFilterService + { + private readonly ILogger logger; + private readonly IJobService jobService; + private readonly IUnitService unitService; + private readonly ITemplateService templateService; + + public UnitFilterService( + ILogger logger, + IJobService jobService, + IUnitService unitService, + ITemplateService templateService + ) + { + this.logger = logger; + this.jobService = jobService; + this.unitService = unitService; + this.templateService = templateService; + } + + public async Task?> GetUnitsIdByJobFilterAsync(Guid jobId) + { + var result = new List(); + + var job = await jobService + .Get().AsNoTracking() + .Include(t => t.UnitFilters) + .ThenInclude(t => t.FieldFilters) + .Include(t => t.UnitFilters) + .ThenInclude(t => t.RelationshipFilters) + .FirstOrDefaultAsync(t => t.Id == jobId); + + if (job == null) + return null; + + var baseQuery = unitService + .Get().AsNoTracking() + .Include(t => t.UnitValues) + .ThenInclude(t => t.Field) + .Include(t => t.UnitValues) + .ThenInclude(t => t.Value) + .Include(t => t.ParentUnits) + .ThenInclude(t => t.ParentUnit) + .ThenInclude(t => t.UnitValues) + .ThenInclude(t => t.Field) + .Include(t => t.ParentUnits) + .ThenInclude(t => t.ParentUnit) + .ThenInclude(t => t.UnitValues) + .ThenInclude(t => t.Value) + .Include(t => t.ChildUnits) + .ThenInclude(t => t.ChildUnit) + .ThenInclude(t => t.UnitValues) + .ThenInclude(t => t.Field) + .Include(t => t.ChildUnits) + .ThenInclude(t => t.ChildUnit) + .ThenInclude(t => t.UnitValues) + .ThenInclude(t => t.Value); + + logger.LogInformation($"В baseQuery записей {await baseQuery.CountAsync()}"); + + foreach (var unitFilter in job.UnitFilters) + { + var query = baseQuery.Where(t => EF.Functions.ILike(t.Name, unitFilter.UnitFilter)); + + foreach (var fieldFilter in unitFilter.FieldFilters) + query = query.Where(t => t.UnitValues.Any(x => x.FieldId == fieldFilter.FieldId && (x.Value!.Value != null && EF.Functions.ILike(x.Value!.Value, fieldFilter.ValueMask)))); + + if (unitFilter.RelationshipFilters.Any()) + { + var parentFilters = unitFilter.RelationshipFilters.Where(t => t.IsParent == true).ToList(); + foreach (var parentFilter in parentFilters) + { + if (parentFilter.IsInverse == false) + { + if (parentFilter.IsFullMatch) + query = query.Where(t => !t.ParentUnits.Any() || t.ParentUnits.All(p => p.ParentUnit!.UnitValues.Any(pf => pf.FieldId == parentFilter.FieldId && (pf.Value!.Value != null && EF.Functions.ILike(pf.Value.Value, parentFilter.ValueMask))))); + else + query = query.Where(t => !t.ParentUnits.Any() || t.ParentUnits.Any(p => p.ParentUnit!.UnitValues.Any(pf => pf.FieldId == parentFilter.FieldId && (pf.Value!.Value != null && EF.Functions.ILike(pf.Value.Value, parentFilter.ValueMask))))); + } + else + { + if (parentFilter.IsFullMatch) + query = query.Where(t => !t.ParentUnits.Any() || !t.ParentUnits.All(p => p.ParentUnit!.UnitValues.Any(pf => pf.FieldId == parentFilter.FieldId && (pf.Value!.Value != null && EF.Functions.ILike(pf.Value.Value, parentFilter.ValueMask))))); + else + query = query.Where(t => !t.ParentUnits.Any() || !t.ParentUnits.Any(p => p.ParentUnit!.UnitValues.Any(pf => pf.FieldId == parentFilter.FieldId && (pf.Value!.Value != null && EF.Functions.ILike(pf.Value.Value, parentFilter.ValueMask))))); + } + logger.LogInformation($"!В query записей {await query.CountAsync()}"); + } + } + var newUnits = await query.Select(t => t.Id).ToListAsync(); + result.AddRange(newUnits); + } + + return result; + } + + public async Task?> GetUnitsIdForExistTemplatesByJobFilterAsync(Guid jobId) + { + var unitIdsMustBeCreated = await GetUnitsIdByJobFilterAsync(jobId); + + if (unitIdsMustBeCreated == null || !unitIdsMustBeCreated.Any()) + return null; + + var unitWithTemplates = await templateService.Get().AsNoTracking().Where(t => t.JobId == jobId && unitIdsMustBeCreated.Any(x => x == t.UnitId)).Select(t => t.UnitId).ToListAsync(); + + return unitWithTemplates; + } + + public async Task?> GetUnitsIdForNotExistTemplatesByJobFilterAsync(Guid jobId) + { + var unitIdsMustBeCreated = await GetUnitsIdByJobFilterAsync(jobId); + + if (unitIdsMustBeCreated == null || !unitIdsMustBeCreated.Any()) + return null; + + var unitIdsWithTemplate = await templateService.Get().AsNoTracking().Where(t => t.JobId == jobId).Select(t => t.UnitId).ToListAsync(); + + var unitsIdToCreateTemplate = unitIdsMustBeCreated.Where(t => !unitIdsWithTemplate.Any(x => x == t)); + + return unitsIdToCreateTemplate; + } + } +} diff --git a/PARR.DAL/DomainServices/IShortcodesService.cs b/PARR.DAL/DomainServices/Interfaces/IShortcodesService.cs similarity index 53% rename from PARR.DAL/DomainServices/IShortcodesService.cs rename to PARR.DAL/DomainServices/Interfaces/IShortcodesService.cs index 8b51835a..f604ee73 100644 --- a/PARR.DAL/DomainServices/IShortcodesService.cs +++ b/PARR.DAL/DomainServices/Interfaces/IShortcodesService.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace PARR.DAL.DomainServices +namespace PARR.DAL.DomainServices.Interfaces { public interface IShortcodesService { diff --git a/PARR.DAL/DomainServices/ITemplateNameGeneratorService.cs b/PARR.DAL/DomainServices/Interfaces/ITemplateNameGeneratorService.cs similarity index 87% rename from PARR.DAL/DomainServices/ITemplateNameGeneratorService.cs rename to PARR.DAL/DomainServices/Interfaces/ITemplateNameGeneratorService.cs index 36401382..eab86a19 100644 --- a/PARR.DAL/DomainServices/ITemplateNameGeneratorService.cs +++ b/PARR.DAL/DomainServices/Interfaces/ITemplateNameGeneratorService.cs @@ -6,7 +6,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace PARR.DAL.DomainServices +namespace PARR.DAL.DomainServices.Interfaces { public interface ITemplateNameGeneratorService { diff --git a/PARR.DAL/DomainServices/Interfaces/IUnitFilterService.cs b/PARR.DAL/DomainServices/Interfaces/IUnitFilterService.cs new file mode 100644 index 00000000..05a237b8 --- /dev/null +++ b/PARR.DAL/DomainServices/Interfaces/IUnitFilterService.cs @@ -0,0 +1,27 @@ +namespace PARR.DAL.DomainServices.Interfaces +{ + public interface IUnitFilterService + { + /// + /// Получить все Unit.Id, для которых должны быть созданы Template + /// + /// Id работы + /// + Task?> GetUnitsIdByJobFilterAsync(Guid jobId); + + /// + /// Получить все Unit.Id, для которых не были созданы Template + /// + /// Id работы + /// + Task?> GetUnitsIdForNotExistTemplatesByJobFilterAsync(Guid jobId); + + /// + /// Получить все Unit.Id, для которых были созданы Template + /// + /// Id работы + /// + Task?> GetUnitsIdForExistTemplatesByJobFilterAsync(Guid jobId); + + } +} diff --git a/PARR.DAL/ParrDalInstaller.cs b/PARR.DAL/ParrDalInstaller.cs index 1f4125cf..84fb0fd7 100644 --- a/PARR.DAL/ParrDalInstaller.cs +++ b/PARR.DAL/ParrDalInstaller.cs @@ -5,7 +5,8 @@ using PARR.DAL.CacheServices; using PARR.DAL.Configurations.DbSettings; using PARR.DAL.Context; using PARR.DAL.Contracts; -using PARR.DAL.DomainServices; +using PARR.DAL.DomainServices.Implementations; +using PARR.DAL.DomainServices.Interfaces; using PARR.DAL.InfluxDbServices; using PARR.DAL.Services.Implementation; using PARR.DAL.Services.Implementations; @@ -99,6 +100,7 @@ namespace PARR.DAL services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); #endregion diff --git a/PARR.EsppSync/SyncService.cs b/PARR.EsppSync/SyncService.cs index 2f31f702..8fddb68f 100644 --- a/PARR.EsppSync/SyncService.cs +++ b/PARR.EsppSync/SyncService.cs @@ -2,7 +2,7 @@ using Microsoft.Extensions.Logging; using PARR.Constants; using PARR.DAL.Contracts; -using PARR.DAL.DomainServices; +using PARR.DAL.DomainServices.Interfaces; using PARR.DAL.Models; using PARR.DAL.Services.Interfaces; using System.Reflection; diff --git a/PARR.Test/Worker.cs b/PARR.Test/Worker.cs index fa3f6551..70062f76 100644 --- a/PARR.Test/Worker.cs +++ b/PARR.Test/Worker.cs @@ -1,15 +1,11 @@ -using Elastic.CommonSchema; using Microsoft.EntityFrameworkCore; using PARR.BLL.Services.Interfaces; using PARR.Constants; -using PARR.DAL.DomainServices; -using PARR.DAL.Services.Interfaces; -using PARR.DAL.Services.Interfaces.Job; +using PARR.DAL.DomainServices.Interfaces; using PARR.DAL.Services.Interfaces.Unit; using PARR.EsppApi; using PARR.EsppApi.Constants; using PARR.EsppApi.Models.Query; -using System.Net.WebSockets; namespace PARR.Test { @@ -39,85 +35,29 @@ namespace PARR.Test protected override async Task ExecuteAsync(CancellationToken stoppingToken) { #region test - using (var scope = serviceProvider.CreateScope()) - { - var unitService = scope.ServiceProvider.GetService(); + //using (var scope = serviceProvider.CreateScope()) + //{ + // var unitFilterService = scope.ServiceProvider.GetService(); - var unitsQuery = unitService!.Get() - .Include(t => t.UnitValues) - .ThenInclude(t => t.Field) - .Include(t => t.UnitValues) - .ThenInclude(t => t.Value) - .Where(t => /*EF.Functions.Like(t.Name, "%-ГВЦ") &&*/ - t.UnitValues.Any(v => - v.Field!.AihitName == "ПОДКАТЕГОРИЯ_ЭК" && v.Value!.Value == "система хранения данных (схд)") - && t.UnitValues.Any(v => - v.Field!.AihitName == "ТИП_ЭК" && v.Value!.Value == "коммутатор") - //&& t.UnitValues.Any(v => - // v.Field!.AihitName == "АКТИВЕН" && v.Value!.Value == "1") - && t.UnitValues.Any(v => - v.Field!.AihitName == "НЕУНИКАЛЬНЫЙ_ЭК" && v.Value!.Value == "0") - && !t.UnitValues.Any(v => - v.Field!.AihitName == "СТАТУС" && v.Value!.Value == "6-Выведен из эксплуатации") - ); + // if (unitFilterService == null) + // return; - var v1 = await unitsQuery - .Include(t => t.ParentUnits) - .ThenInclude(t => t.ParentUnit) - .ThenInclude(t => t.UnitValues) - .ThenInclude(t => t.Field) - .Include(t => t.ParentUnits) - .ThenInclude(t => t.ParentUnit) - .ThenInclude(t => t.UnitValues) - .ThenInclude(t => t.Value) - .ToListAsync(); - var res = v1 - .Where(t => (t.ParentUnits == null || t.ParentUnits.Count == 0) || !t.ParentUnits!.Any(a => a.ParentUnit != null && a.ParentUnit!.BaseFields!.NotUnique == "1")) - .OrderBy(t => t.Name).ToList(); - var service = scope.ServiceProvider.GetService(); - foreach (var item in res) - { - var name = await service!.GetTemplateNameAsync(Guid.Parse("926793f7-c6d3-4f08-b5ce-517ce1a1ac37"), item.Id); - } - /*var service = scope.ServiceProvider.GetService(); + // var jobId = Guid.Parse("58b28517-8178-4683-a519-22f5989c9912"); + // var unitsId = await unitFilterService.GetUnitsIdByJobFilterAsync(jobId); - var ttt = await service.GetTemplateNameAsync(Guid.Parse("ba05948b-b040-48cc-ab77-0accc9c223a0"), Guid.Parse("d4a86f24-6975-4614-98ab-431f26e3eb69"));*/ - //var service = scope.ServiceProvider.GetService(); - //var templates = await service.Get() - // .Include(t => t.ApplicationsInWork) - // .ThenInclude(t=>t.Work) - // //.Select(s => new { s.Id, s.Host, s.HostId, s.JobId, s.UnitId }) - // .Where(t => t.JobId == Guid.Parse("ba05948b-b040-48cc-ab77-0accc9c223a0") - // ).ToListAsync(); + // var unitService = scope.ServiceProvider.GetService(); - //var workNames = templates.Select(t=>t.ApplicationsInWork!.Work!.TemplateSuffix).Distinct(); + // if (unitService == null) + // return; - //var jobService = scope.ServiceProvider.GetService(); - //var jobs = await jobService!.Get().Where(t => workNames.Any(tt => tt == t.Name)).ToListAsync(); - ////var hostsName = templates.Select(t => t.Host!.Ek); - ////var unitService = scope.ServiceProvider.GetService(); - ////var units = await unitService.Get() - //// //.Select(s => new { s.Id, s.Name }) - //// .Where(t => hostsName.Any(tt => tt == t.Name)) - //// .ToListAsync(); + // var tForCreate = await unitFilterService.GetUnitsIdForExistTemplatesByJobFilterAsync(jobId); + // if (tForCreate == null) + // return; - //foreach (var t in templates) { - - // var workName = t.ApplicationsInWork!.Work!.TemplateSuffix; - // //var unitId = units.FirstOrDefault(u => u.Name == hostName); - // var jobId = jobs.FirstOrDefault(t => t.Name == workName); - // if (jobId != null) - // t.JobId = jobId.Id; - //} - - //await service.CommitAsync(); + // var units = await unitService + // .Get().AsNoTracking() + // .Where(t => tForCreate.Any(x => x == t.Id)).ToListAsync(); } - - //var tName = "ДВС-ЭИТИ-ПТК-ПАРР__ВРТ-VCD-02-ДВС__ПРОЧЕЕ(РАБОТЫ)"; - //var prefix = "ЭИТИ-ПТК-ПАРР"; - ////templateName.Contains(settingsFromDb.TemplatePrefixWithoutVariable, StringComparison.CurrentCultureIgnoreCase) - //var res = tName.Contains(prefix); - #endregion #region unit