feat: Из dal перенесены все модели в Domain. Из dal переименованы service в repository, вынесены в Core.
This commit is contained in:
19
PARR.DAL/Repositories/Unit/UnitFieldRepository.cs
Normal file
19
PARR.DAL/Repositories/Unit/UnitFieldRepository.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using PARR.Core.Repositories.Interfaces.Unit;
|
||||
using PARR.DAL.Context;
|
||||
using PARR.DAL.Repositories.Base;
|
||||
using PARR.Domain.Entities.Unit;
|
||||
|
||||
namespace PARR.DAL.Repositories.Unit
|
||||
{
|
||||
internal class UnitFieldRepository : BaseRepository<UnitField>, IUnitFieldRepository
|
||||
{
|
||||
public UnitFieldRepository(DataContext dataContext, ILogger<UnitFieldRepository> logger) : base(logger, dataContext) { }
|
||||
|
||||
public async Task<UnitField?> GetByAihitNameAsync(string name)
|
||||
{
|
||||
return await EntitySet.FirstOrDefaultAsync(uf => uf.AihitName.ToLower().Trim() == name.ToLower().Trim());
|
||||
}
|
||||
}
|
||||
}
|
||||
26
PARR.DAL/Repositories/Unit/UnitFieldValueRepository.cs
Normal file
26
PARR.DAL/Repositories/Unit/UnitFieldValueRepository.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using PARR.Core.Repositories.Interfaces.Unit;
|
||||
using PARR.DAL.Context;
|
||||
using PARR.DAL.Repositories.Base;
|
||||
using PARR.Domain.Entities.Unit;
|
||||
|
||||
namespace PARR.DAL.Repositories.Unit
|
||||
{
|
||||
internal class UnitFieldValueRepository : BaseRepository<UnitFieldValue>, IUnitFieldValueRepository
|
||||
{
|
||||
public UnitFieldValueRepository(DataContext dataContext, ILogger<UnitFieldValueRepository> logger) : base(logger, dataContext) { }
|
||||
|
||||
|
||||
public async Task<UnitFieldValue?> GetByValueNameAsync(string? value)
|
||||
{
|
||||
var query = EntitySet
|
||||
.Include(v => v.FieldValues);
|
||||
|
||||
if (string.IsNullOrWhiteSpace(value))
|
||||
return await query.FirstOrDefaultAsync(uf => uf.Value == null);
|
||||
|
||||
return await query.FirstOrDefaultAsync(uf => uf.Value!.ToLower().Trim() == value.ToLower().Trim());
|
||||
}
|
||||
}
|
||||
}
|
||||
64
PARR.DAL/Repositories/Unit/UnitInUnitRepository.cs
Normal file
64
PARR.DAL/Repositories/Unit/UnitInUnitRepository.cs
Normal file
@@ -0,0 +1,64 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using PARR.Core.Repositories.Interfaces.Unit;
|
||||
using PARR.DAL.Context;
|
||||
using PARR.Domain.Entities.Unit;
|
||||
|
||||
namespace PARR.DAL.Repositories.Unit
|
||||
{
|
||||
internal class UnitInUnitRepository : IUnitInUnitRepository
|
||||
{
|
||||
private readonly DataContext dataContext;
|
||||
private readonly ILogger<UnitInUnitRepository> logger;
|
||||
|
||||
public UnitInUnitRepository(
|
||||
DataContext dataContext,
|
||||
ILogger<UnitInUnitRepository> logger
|
||||
)
|
||||
{
|
||||
this.dataContext = dataContext;
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
|
||||
public IQueryable<UnitInUnit> Get()
|
||||
{
|
||||
return dataContext.UnitInUnits;
|
||||
}
|
||||
|
||||
|
||||
public Task<List<UnitInUnit>> GetByParentIdAsync(Guid parentId)
|
||||
{
|
||||
return dataContext.UnitInUnits
|
||||
.Where(u => u.ParentUnitId == parentId)
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
|
||||
public Task<List<UnitInUnit>> GetByChildIdAsync(Guid childId)
|
||||
{
|
||||
return dataContext.UnitInUnits
|
||||
.Where(u => u.ChildUnitId == childId)
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
|
||||
public async Task<List<UnitInUnit>> GetParentLinksByChildIdsAsync(IEnumerable<Guid> childUnitIds)
|
||||
{
|
||||
var set = childUnitIds.ToHashSet();
|
||||
return await dataContext.UnitInUnits
|
||||
.AsNoTracking()
|
||||
.Where(uinu => set.Contains(uinu.ChildUnitId))
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<List<UnitInUnit>> GetChildLinksByParentIdsAsync(IEnumerable<Guid> parentUnitIds)
|
||||
{
|
||||
var set = parentUnitIds.ToHashSet();
|
||||
return await dataContext.UnitInUnits
|
||||
.AsNoTracking()
|
||||
.Where(uinu => set.Contains(uinu.ParentUnitId))
|
||||
.ToListAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
132
PARR.DAL/Repositories/Unit/UnitInValueRepository.cs
Normal file
132
PARR.DAL/Repositories/Unit/UnitInValueRepository.cs
Normal file
@@ -0,0 +1,132 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using PARR.Core.Repositories.Interfaces.Unit;
|
||||
using PARR.DAL.Context;
|
||||
using PARR.Domain.Entities.Unit;
|
||||
|
||||
namespace PARR.DAL.Repositories.Unit
|
||||
{
|
||||
internal class UnitInValueRepository : IUnitInValueRepository
|
||||
{
|
||||
private readonly ILogger<UnitInValueRepository> logger;
|
||||
private readonly DataContext dataContext;
|
||||
|
||||
public UnitInValueRepository(
|
||||
ILogger<UnitInValueRepository> logger,
|
||||
DataContext dataContext
|
||||
)
|
||||
{
|
||||
this.logger = logger;
|
||||
this.dataContext = dataContext;
|
||||
}
|
||||
|
||||
public async Task<List<UnitInValue>> GetByUnitIdAsync(Guid unitId)
|
||||
{
|
||||
return await dataContext.UnitInValues.AsNoTracking()
|
||||
.Include(t => t.Value)
|
||||
.Where(uv => uv.UnitId == unitId)
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<List<UnitInValue>> GetByUnitIdsAsync(IEnumerable<Guid> unitIds)
|
||||
{
|
||||
return await dataContext.UnitInValues.AsNoTracking()
|
||||
.Include(t => t.Value)
|
||||
.Where(uv => unitIds.Contains(uv.UnitId))
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
|
||||
public async Task<List<(string FieldName, string? Value)>> GetFieldValuesAsync(
|
||||
Guid unitId,
|
||||
IReadOnlyCollection<string> aihitNames)
|
||||
{
|
||||
if (aihitNames == null || aihitNames.Count == 0)
|
||||
return new List<(string, string?)>();
|
||||
|
||||
var keyValuePairs = await dataContext.UnitInValues
|
||||
.AsNoTracking()
|
||||
.AsSplitQuery()
|
||||
.Where(uiv => uiv.UnitId == unitId
|
||||
&& uiv.Field != null
|
||||
&& uiv.Value != null
|
||||
&& aihitNames.Contains(uiv.Field.AihitName.ToUpper()))
|
||||
.Select(uiv => new { Key = uiv.Field!.AihitName.ToUpper(), Value = uiv.Value!.Value })
|
||||
.ToListAsync();
|
||||
|
||||
// Журналируем если не нашли поля
|
||||
var foundFieldNames = keyValuePairs.Select(kvp => kvp.Key).ToHashSet(StringComparer.OrdinalIgnoreCase);
|
||||
var missing = aihitNames
|
||||
.Where(name => !foundFieldNames.Contains(name.ToUpper()))
|
||||
.ToList();
|
||||
if (missing.Any())
|
||||
{
|
||||
logger.LogDebug("UnitInValueService: поля не найдены для unitId={UnitId}: {Fields}",
|
||||
unitId, string.Join(", ", missing));
|
||||
}
|
||||
|
||||
return keyValuePairs.Select(kvp => (kvp.Key, kvp.Value)).ToList();
|
||||
}
|
||||
|
||||
|
||||
public async Task<List<UnitInValue>> GetByUnitIdsAndFieldIdsAsync(IEnumerable<Guid> unitIds, IEnumerable<Guid> fieldIds)
|
||||
{
|
||||
var unitIdSet = unitIds.ToHashSet();
|
||||
var fieldIdSet = fieldIds.ToHashSet();
|
||||
|
||||
return await dataContext.UnitInValues
|
||||
.AsNoTracking()
|
||||
.Include(uv => uv.Value)
|
||||
.Where(uv => unitIdSet.Contains(uv.UnitId) && fieldIdSet.Contains(uv.FieldId))
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
public IQueryable<UnitInValue> Get()
|
||||
{
|
||||
return dataContext.UnitInValues;
|
||||
}
|
||||
|
||||
public async Task<string?> GetMostFrequentValueForFieldAsync(List<Guid> unitIds, string fieldName)
|
||||
{
|
||||
if (unitIds == null || !unitIds.Any() || string.IsNullOrWhiteSpace(fieldName))
|
||||
{
|
||||
logger.LogDebug("GetMostFrequentValueForFieldAsync: пустой список юнитов или имя поля. unitIds count: {Count}, fieldName: {FieldName}", unitIds?.Count ?? 0, fieldName);
|
||||
return null;
|
||||
}
|
||||
|
||||
logger.LogDebug("Поиск наиболее частого значения для поля '{FieldName}' среди {Count} юнитов.", fieldName, unitIds.Count);
|
||||
|
||||
var result = await dataContext.UnitInValues
|
||||
.AsNoTracking()
|
||||
.Where(uv =>
|
||||
unitIds.Contains(uv.UnitId)
|
||||
)
|
||||
.Join(
|
||||
dataContext.UnitFields,
|
||||
uv => uv.FieldId,
|
||||
f => f.Id,
|
||||
(uv, f) => new { uv, f }
|
||||
)
|
||||
.Where(x =>
|
||||
EF.Functions.ILike(x.f.AihitName, fieldName)
|
||||
)
|
||||
.Join(
|
||||
dataContext.UnitFieldValues,
|
||||
x => x.uv.ValueId,
|
||||
v => v.Id,
|
||||
(x, v) => new { x.uv.UnitId, v.Value }
|
||||
)
|
||||
.Where(x => !string.IsNullOrWhiteSpace(x.Value))
|
||||
.GroupBy(x => x.Value)
|
||||
.Select(g => new { Value = g.Key, Count = g.Count() })
|
||||
.OrderByDescending(x => x.Count)
|
||||
.ThenBy(x => x.Value)
|
||||
.FirstOrDefaultAsync();
|
||||
|
||||
var mostFrequentValue = result?.Value;
|
||||
logger.LogDebug("Наиболее частое значение для поля '{FieldName}': {Value}", fieldName, mostFrequentValue);
|
||||
return mostFrequentValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
21
PARR.DAL/Repositories/Unit/UnitKiiUnitRepository.cs
Normal file
21
PARR.DAL/Repositories/Unit/UnitKiiUnitRepository.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
using PARR.Core.Repositories.Interfaces.Unit;
|
||||
using PARR.DAL.Context;
|
||||
using PARR.Domain.Entities.Unit;
|
||||
|
||||
namespace PARR.DAL.Repositories.Unit
|
||||
{
|
||||
internal class UnitKiiUnitRepository : IUnitKiiUnitRepository
|
||||
{
|
||||
private readonly DataContext dataContext;
|
||||
|
||||
public UnitKiiUnitRepository(DataContext dataContext)
|
||||
{
|
||||
this.dataContext = dataContext;
|
||||
}
|
||||
|
||||
public IQueryable<UnitKiiUnit> Get()
|
||||
{
|
||||
return dataContext.UnitKiiUnits;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using PARR.Core.Repositories.Interfaces.Unit;
|
||||
using PARR.DAL.Context;
|
||||
using PARR.Domain.Entities.Unit;
|
||||
|
||||
namespace PARR.DAL.Repositories.Unit
|
||||
{
|
||||
internal class UnitRegionalEkPtkGroupRepository : IUnitRegionalEkPtkGroupRepository
|
||||
{
|
||||
private readonly DataContext dataContext;
|
||||
|
||||
public UnitRegionalEkPtkGroupRepository(DataContext dataContext)
|
||||
{
|
||||
this.dataContext = dataContext;
|
||||
}
|
||||
|
||||
public IQueryable<UnitRegionalEkPtkGroup> Get()
|
||||
{
|
||||
return dataContext.UnitRegionalEkPtkGroups;
|
||||
}
|
||||
}
|
||||
}
|
||||
23
PARR.DAL/Repositories/Unit/UnitRepository.cs
Normal file
23
PARR.DAL/Repositories/Unit/UnitRepository.cs
Normal file
@@ -0,0 +1,23 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using PARR.Core.Repositories.Interfaces.Unit;
|
||||
using PARR.DAL.Context;
|
||||
using PARR.DAL.Repositories.Base;
|
||||
|
||||
namespace PARR.DAL.Repositories.Unit
|
||||
{
|
||||
internal class UnitRepository : BaseRepository<Domain.Entities.Unit.Unit>, IUnitRepository
|
||||
{
|
||||
public UnitRepository(DataContext dataContext, ILogger<UnitRepository> logger) : base(logger, dataContext) { }
|
||||
|
||||
|
||||
public IQueryable<Domain.Entities.Unit.Unit> GetWithIncludes()
|
||||
{
|
||||
return Get()
|
||||
.Include(t => t.UnitValues)
|
||||
.ThenInclude(t => t.Field)
|
||||
.Include(t => t.UnitValues)
|
||||
.ThenInclude(t => t.Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user