You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

433 lines
17 KiB

using System.Linq.Expressions;
using Microsoft.Extensions.Logging;
using Microsoft.EntityFrameworkCore;
using CellularManagement.Domain.Entities.NetworkProfile;
using CellularManagement.Domain.Repositories.Base;
using CellularManagement.Domain.Repositories.NetworkProfile;
using CellularManagement.Infrastructure.Repositories.Base;
using X1.Domain.Models;
namespace CellularManagement.Infrastructure.Repositories.NetworkProfile;
/// <summary>
/// 网络栈配置仓储实现
/// </summary>
public class NetworkStackConfigRepository : BaseRepository<NetworkStackConfig>, INetworkStackConfigRepository
{
private readonly ILogger<NetworkStackConfigRepository> _logger;
/// <summary>
/// 初始化仓储
/// </summary>
public NetworkStackConfigRepository(
ICommandRepository<NetworkStackConfig> commandRepository,
IQueryRepository<NetworkStackConfig> queryRepository,
ILogger<NetworkStackConfigRepository> logger)
: base(commandRepository, queryRepository, logger)
{
_logger = logger;
}
/// <summary>
/// 添加网络栈配置
/// </summary>
public async Task AddNetworkStackConfigAsync(NetworkStackConfig networkStackConfig, CancellationToken cancellationToken = default)
{
await CommandRepository.AddAsync(networkStackConfig, cancellationToken);
}
/// <summary>
/// 更新网络栈配置
/// </summary>
public void UpdateNetworkStackConfig(NetworkStackConfig networkStackConfig)
{
CommandRepository.Update(networkStackConfig);
}
/// <summary>
/// 删除网络栈配置
/// </summary>
public async Task DeleteNetworkStackConfigAsync(string id, CancellationToken cancellationToken = default)
{
await CommandRepository.DeleteByIdAsync(id, cancellationToken);
}
/// <summary>
/// 获取所有网络栈配置
/// </summary>
public async Task<IList<NetworkStackConfig>> GetAllNetworkStackConfigsAsync(CancellationToken cancellationToken = default)
{
var configs = await QueryRepository.GetAllAsync(cancellationToken: cancellationToken);
return configs.OrderBy(x => x.NetworkStackName).ToList();
}
/// <summary>
/// 根据ID获取网络栈配置
/// </summary>
public async Task<NetworkStackConfig?> GetNetworkStackConfigByIdAsync(string id, CancellationToken cancellationToken = default)
{
return await QueryRepository.GetByIdAsync(id, cancellationToken: cancellationToken);
}
/// <summary>
/// 根据网络栈名称获取网络栈配置
/// </summary>
public async Task<NetworkStackConfig?> GetNetworkStackConfigByNameAsync(string networkStackName, CancellationToken cancellationToken = default)
{
return await QueryRepository.FirstOrDefaultAsync(nsc => nsc.NetworkStackName == networkStackName, cancellationToken: cancellationToken);
}
/// <summary>
/// 根据RAN ID获取网络栈配置
/// </summary>
public async Task<IList<NetworkStackConfig>> GetNetworkStackConfigsByRanIdAsync(string ranId, CancellationToken cancellationToken = default)
{
var configs = await QueryRepository.FindAsync(nsc => nsc.RanId == ranId, cancellationToken: cancellationToken);
return configs.OrderBy(x => x.NetworkStackName).ToList();
}
/// <summary>
/// 获取激活的网络栈配置
/// </summary>
public async Task<IList<NetworkStackConfig>> GetActiveNetworkStackConfigsAsync(CancellationToken cancellationToken = default)
{
var configs = await QueryRepository.FindAsync(nsc => nsc.IsActive, cancellationToken: cancellationToken);
return configs.OrderBy(x => x.NetworkStackName).ToList();
}
/// <summary>
/// 搜索网络栈配置
/// </summary>
public async Task<(int TotalCount, IList<NetworkStackConfig> Items)> SearchNetworkStackConfigsAsync(
string? keyword,
CancellationToken cancellationToken = default)
{
var query = await QueryRepository.FindAsync(nsc => true, cancellationToken: cancellationToken);
if (!string.IsNullOrWhiteSpace(keyword))
{
query = query.Where(nsc =>
nsc.NetworkStackName.Contains(keyword) ||
nsc.Description.Contains(keyword));
}
var configs = query;
return (configs.Count(), configs.OrderBy(x => x.NetworkStackName).ToList());
}
/// <summary>
/// 搜索网络栈配置(分页)
/// </summary>
public async Task<(int TotalCount, IList<NetworkStackConfig> Items)> SearchNetworkStackConfigsAsync(
string? keyword,
int pageNumber,
int pageSize,
CancellationToken cancellationToken = default)
{
// 构建查询条件
Expression<Func<NetworkStackConfig, bool>> predicate = nsc => true;
if (!string.IsNullOrWhiteSpace(keyword))
{
predicate = nsc => nsc.NetworkStackName.Contains(keyword) ||
nsc.Description.Contains(keyword);
}
// 执行分页查询
var result = await QueryRepository.GetPagedAsync(predicate, pageNumber, pageSize, null, cancellationToken);
return (result.TotalCount, result.Items.OrderBy(x => x.NetworkStackName).ToList());
}
/// <summary>
/// 检查网络栈配置是否存在
/// </summary>
public async Task<bool> ExistsAsync(string id, CancellationToken cancellationToken = default)
{
return await QueryRepository.AnyAsync(nsc => nsc.Id == id, cancellationToken: cancellationToken);
}
/// <summary>
/// 检查网络栈名称是否存在
/// </summary>
public async Task<bool> NameExistsAsync(string networkStackName, CancellationToken cancellationToken = default)
{
return await QueryRepository.AnyAsync(nsc => nsc.NetworkStackName == networkStackName, cancellationToken: cancellationToken);
}
5 days ago
/// <summary>
/// 检查网络栈编码是否存在
/// </summary>
public async Task<bool> CodeExistsAsync(string networkStackCode, CancellationToken cancellationToken = default)
{
return await QueryRepository.AnyAsync(nsc => nsc.NetworkStackCode == networkStackCode, cancellationToken: cancellationToken);
}
/// <summary>
/// 获取网络栈配置总数
/// </summary>
public async Task<int> GetNetworkStackConfigCountAsync(CancellationToken cancellationToken = default)
{
return await QueryRepository.CountAsync(nsc => true, cancellationToken: cancellationToken);
}
/// <summary>
/// 根据ID获取网络栈配置(包含绑定关系)
/// </summary>
public async Task<NetworkStackConfig?> GetNetworkStackConfigByIdWithBindingsAsync(string id, CancellationToken cancellationToken = default)
{
return await QueryRepository.GetByIdAsync(
id,
include: query => query.Include(x => x.StackCoreIMSBindings),
cancellationToken: cancellationToken);
}
/// <summary>
/// 搜索网络栈配置
/// </summary>
public async Task<(int TotalCount, IList<NetworkStackConfig> Items)> SearchNetworkStackConfigsAsync(
string? networkStackName = null,
string? ranId = null,
bool? isActive = null,
int pageNumber = 1,
int pageSize = 10,
CancellationToken cancellationToken = default)
{
// 构建查询条件
Expression<Func<NetworkStackConfig, bool>> predicate = nsc => true;
if (!string.IsNullOrWhiteSpace(networkStackName))
{
predicate = nsc => nsc.NetworkStackName.Contains(networkStackName);
}
if (!string.IsNullOrWhiteSpace(ranId))
{
var ranIdPredicate = predicate;
predicate = nsc => ranIdPredicate.Compile()(nsc) && nsc.RanId == ranId;
}
if (isActive.HasValue)
{
var isActivePredicate = predicate;
predicate = nsc => isActivePredicate.Compile()(nsc) && nsc.IsActive == isActive.Value;
}
// 执行分页查询
var result = await QueryRepository.GetPagedAsync(predicate, pageNumber, pageSize, cancellationToken: cancellationToken);
return (result.TotalCount, result.Items.OrderBy(x => x.NetworkStackName).ToList());
}
/// <summary>
/// 搜索网络栈配置(包含绑定关系)
/// </summary>
public async Task<(int TotalCount, IList<NetworkStackConfig> Items)> SearchNetworkStackConfigsWithBindingsAsync(
string? networkStackName = null,
string? ranId = null,
bool? isActive = null,
int pageNumber = 1,
int pageSize = 10,
CancellationToken cancellationToken = default)
{
// 构建查询条件
Expression<Func<NetworkStackConfig, bool>> predicate = nsc => true;
if (!string.IsNullOrWhiteSpace(networkStackName))
{
predicate = nsc => nsc.NetworkStackName.Contains(networkStackName);
}
if (!string.IsNullOrWhiteSpace(ranId))
{
var ranIdPredicate = predicate;
predicate = nsc => ranIdPredicate.Compile()(nsc) && nsc.RanId == ranId;
}
if (isActive.HasValue)
{
var isActivePredicate = predicate;
predicate = nsc => isActivePredicate.Compile()(nsc) && nsc.IsActive == isActive.Value;
}
// 执行分页查询(包含导航属性)
var result = await QueryRepository.GetPagedAsync(
predicate,
pageNumber,
pageSize,
include: query => query.Include(x => x.StackCoreIMSBindings),
cancellationToken: cancellationToken);
return (result.TotalCount, result.Items.OrderBy(x => x.NetworkStackName).ToList());
}
/// <summary>
/// 根据ID获取网络栈配置(包含绑定关系和关联配置名称)
/// </summary>
4 days ago
public async Task<IList<NetworkStackConfigWithBindingNamesDto>> GetNetworkStackConfigByIdWithBindingNamesAsync(string id, CancellationToken cancellationToken = default)
{
// 使用原生SQL查询来一次性获取所有需要的数据 - PostgreSQL语法
var sql = @"
SELECT
4 days ago
nsc.""Id"" AS ""NetworkStackConfigId"",
nsc.""NetworkStackName"",
nsc.""NetworkStackCode"",
nsc.""RanId"",
4 days ago
ran.""Name"" AS ""RanName"",
4 days ago
ran.""ConfigContent"" AS ""RanConfigContent"",
4 days ago
nsc.""Description"",
nsc.""IsActive"",
nsc.""CreatedAt"",
nsc.""UpdatedAt"",
nsc.""CreatedBy"",
nsc.""UpdatedBy"",
binding.""Id"" AS ""StackCoreIMSBindingId"",
binding.""Index"",
binding.""CnId"" AS ""CoreNetworkConfigId"",
cnc.""Name"" AS ""CoreNetworkConfigName"",
4 days ago
cnc.""ConfigContent"" AS ""CoreNetworkConfigContent"",
4 days ago
binding.""ImsId"" AS ""IMSConfigId"",
ims.""Name"" AS ""IMSConfigName"",
4 days ago
ims.""ConfigContent"" AS ""IMSConfigContent""
FROM ""NetworkStackConfigs"" nsc
LEFT JOIN ""RAN_Configurations"" ran ON nsc.""RanId"" = ran.""Id""
LEFT JOIN ""Stack_CoreIMS_Bindings"" binding ON nsc.""Id"" = binding.""NetworkStackConfigId""
LEFT JOIN ""CoreNetworkConfigs"" cnc ON binding.""CnId"" = cnc.""Id""
LEFT JOIN ""IMS_Configurations"" ims ON binding.""ImsId"" = ims.""Id""
WHERE nsc.""Id"" = @p0
ORDER BY binding.""Index""";
var parameters = new object[] { id };
// 执行查询
4 days ago
var results = await ExecuteSqlQueryAsync<NetworkStackConfigWithBindingNamesDto>(sql, parameters, cancellationToken);
return results.ToList();
}
4 days ago
public async Task<IList<NetworkStackConfigBasicDto>> GetNetworkStackConfigsByCodesAsync(string[] networkStackCodes, CancellationToken cancellationToken = default)
{
if (networkStackCodes == null || !networkStackCodes.Any())
{
4 days ago
return new List<NetworkStackConfigBasicDto>();
}
// 构建IN查询的参数占位符
var paramPlaceholders = string.Join(",", networkStackCodes.Select((_, index) => $"@p{index}"));
// 使用原生SQL查询来一次性获取所有需要的数据 - PostgreSQL语法
var sql = $@"
SELECT
nsc.""NetworkStackName"",
nsc.""NetworkStackCode"",
nsc.""RanId"",
4 days ago
cnc.""Id"" AS ""CoreNetworkConfigId"",
ims.""Id"" AS ""IMSConfigId"",
ran.""ConfigContent"" AS ""RanConfigContent"",
4 days ago
cnc.""ConfigContent"" AS ""CoreNetworkConfigContent"",
ims.""ConfigContent"" AS ""IMSConfigContent""
FROM ""NetworkStackConfigs"" nsc
LEFT JOIN ""RAN_Configurations"" ran ON nsc.""RanId"" = ran.""Id""
LEFT JOIN ""Stack_CoreIMS_Bindings"" binding ON nsc.""Id"" = binding.""NetworkStackConfigId""
LEFT JOIN ""CoreNetworkConfigs"" cnc ON binding.""CnId"" = cnc.""Id""
LEFT JOIN ""IMS_Configurations"" ims ON binding.""ImsId"" = ims.""Id""
WHERE nsc.""NetworkStackCode"" IN ({paramPlaceholders})
ORDER BY nsc.""NetworkStackCode"", binding.""Index""";
var parameters = networkStackCodes.Cast<object>().ToArray();
// 执行查询
4 days ago
var results = await ExecuteSqlQueryAsync<NetworkStackConfigBasicDto>(sql, parameters, cancellationToken);
return results.ToList();
}
/// <summary>
/// 搜索网络栈配置(包含绑定关系和关联配置名称,分页)
/// </summary>
public async Task<(int TotalCount, IList<NetworkStackConfigWithBindingNamesDto> Items)> SearchNetworkStackConfigsWithBindingNamesAsync(
string? networkStackName = null,
string? ranId = null,
bool? isActive = null,
int pageNumber = 1,
int pageSize = 10,
CancellationToken cancellationToken = default)
{
// 构建WHERE条件和参数
var whereConditions = new List<string>();
var parameters = new List<object>();
var paramIndex = 0;
if (!string.IsNullOrWhiteSpace(networkStackName))
{
whereConditions.Add($"nsc.\"NetworkStackName\" LIKE @p{paramIndex}");
parameters.Add($"%{networkStackName}%");
paramIndex++;
}
if (!string.IsNullOrWhiteSpace(ranId))
{
whereConditions.Add($"nsc.\"RanId\" = @p{paramIndex}");
parameters.Add(ranId);
paramIndex++;
}
if (isActive.HasValue)
{
whereConditions.Add($"nsc.\"IsActive\" = @p{paramIndex}");
parameters.Add(isActive.Value);
paramIndex++;
}
var whereClause = whereConditions.Any() ? "WHERE " + string.Join(" AND ", whereConditions) : "";
// 计算总记录数的SQL - PostgreSQL语法
var countSql = $@"
SELECT COUNT(DISTINCT nsc.""Id"")
FROM ""NetworkStackConfigs"" nsc
{whereClause}";
// 获取总记录数
var totalCount = await ExecuteSqlQueryAsync<int>(countSql, parameters.ToArray(), cancellationToken);
var count = totalCount.FirstOrDefault();
// 分页查询SQL - PostgreSQL语法
var offset = (pageNumber - 1) * pageSize;
var sql = $@"
SELECT
nsc.""Id"" AS ""NetworkStackConfigId"",
nsc.""NetworkStackName"",
nsc.""NetworkStackCode"",
nsc.""RanId"",
ran.""Name"" AS ""RanName"",
4 days ago
ran.""ConfigContent"" AS ""RanConfigContent"",
nsc.""Description"",
nsc.""IsActive"",
nsc.""CreatedAt"",
nsc.""UpdatedAt"",
nsc.""CreatedBy"",
nsc.""UpdatedBy"",
binding.""Id"" AS ""StackCoreIMSBindingId"",
binding.""Index"",
binding.""CnId"" AS ""CoreNetworkConfigId"",
cnc.""Name"" AS ""CoreNetworkConfigName"",
4 days ago
cnc.""ConfigContent"" AS ""CoreNetworkConfigContent"",
binding.""ImsId"" AS ""IMSConfigId"",
4 days ago
ims.""Name"" AS ""IMSConfigName"",
ims.""ConfigContent"" AS ""IMSConfigContent""
FROM ""NetworkStackConfigs"" nsc
LEFT JOIN ""RAN_Configurations"" ran ON nsc.""RanId"" = ran.""Id""
LEFT JOIN ""Stack_CoreIMS_Bindings"" binding ON nsc.""Id"" = binding.""NetworkStackConfigId""
LEFT JOIN ""CoreNetworkConfigs"" cnc ON binding.""CnId"" = cnc.""Id""
LEFT JOIN ""IMS_Configurations"" ims ON binding.""ImsId"" = ims.""Id""
{whereClause}
ORDER BY nsc.""NetworkStackName"", binding.""Index""
LIMIT @p{paramIndex} OFFSET @p{paramIndex + 1}";
parameters.Add(pageSize);
parameters.Add(offset);
// 执行查询
var results = await ExecuteSqlQueryAsync<NetworkStackConfigWithBindingNamesDto>(sql, parameters.ToArray(), cancellationToken);
return (count, results.ToList());
}
}