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; /// /// 网络栈配置仓储实现 /// public class NetworkStackConfigRepository : BaseRepository, INetworkStackConfigRepository { private readonly ILogger _logger; /// /// 初始化仓储 /// public NetworkStackConfigRepository( ICommandRepository commandRepository, IQueryRepository queryRepository, ILogger logger) : base(commandRepository, queryRepository, logger) { _logger = logger; } /// /// 添加网络栈配置 /// public async Task AddNetworkStackConfigAsync(NetworkStackConfig networkStackConfig, CancellationToken cancellationToken = default) { await CommandRepository.AddAsync(networkStackConfig, cancellationToken); } /// /// 更新网络栈配置 /// public void UpdateNetworkStackConfig(NetworkStackConfig networkStackConfig) { CommandRepository.Update(networkStackConfig); } /// /// 删除网络栈配置 /// public async Task DeleteNetworkStackConfigAsync(string id, CancellationToken cancellationToken = default) { await CommandRepository.DeleteByIdAsync(id, cancellationToken); } /// /// 获取所有网络栈配置 /// public async Task> GetAllNetworkStackConfigsAsync(CancellationToken cancellationToken = default) { var configs = await QueryRepository.GetAllAsync(cancellationToken: cancellationToken); return configs.OrderBy(x => x.NetworkStackName).ToList(); } /// /// 根据ID获取网络栈配置 /// public async Task GetNetworkStackConfigByIdAsync(string id, CancellationToken cancellationToken = default) { return await QueryRepository.GetByIdAsync(id, cancellationToken: cancellationToken); } /// /// 根据网络栈名称获取网络栈配置 /// public async Task GetNetworkStackConfigByNameAsync(string networkStackName, CancellationToken cancellationToken = default) { return await QueryRepository.FirstOrDefaultAsync(nsc => nsc.NetworkStackName == networkStackName, cancellationToken: cancellationToken); } /// /// 根据RAN ID获取网络栈配置 /// public async Task> GetNetworkStackConfigsByRanIdAsync(string ranId, CancellationToken cancellationToken = default) { var configs = await QueryRepository.FindAsync(nsc => nsc.RanId == ranId, cancellationToken: cancellationToken); return configs.OrderBy(x => x.NetworkStackName).ToList(); } /// /// 获取激活的网络栈配置 /// public async Task> GetActiveNetworkStackConfigsAsync(CancellationToken cancellationToken = default) { var configs = await QueryRepository.FindAsync(nsc => nsc.IsActive, cancellationToken: cancellationToken); return configs.OrderBy(x => x.NetworkStackName).ToList(); } /// /// 搜索网络栈配置 /// public async Task<(int TotalCount, IList 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()); } /// /// 搜索网络栈配置(分页) /// public async Task<(int TotalCount, IList Items)> SearchNetworkStackConfigsAsync( string? keyword, int pageNumber, int pageSize, CancellationToken cancellationToken = default) { // 构建查询条件 Expression> 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()); } /// /// 检查网络栈配置是否存在 /// public async Task ExistsAsync(string id, CancellationToken cancellationToken = default) { return await QueryRepository.AnyAsync(nsc => nsc.Id == id, cancellationToken: cancellationToken); } /// /// 检查网络栈名称是否存在 /// public async Task NameExistsAsync(string networkStackName, CancellationToken cancellationToken = default) { return await QueryRepository.AnyAsync(nsc => nsc.NetworkStackName == networkStackName, cancellationToken: cancellationToken); } /// /// 检查网络栈编码是否存在 /// public async Task CodeExistsAsync(string networkStackCode, CancellationToken cancellationToken = default) { return await QueryRepository.AnyAsync(nsc => nsc.NetworkStackCode == networkStackCode, cancellationToken: cancellationToken); } /// /// 获取网络栈配置总数 /// public async Task GetNetworkStackConfigCountAsync(CancellationToken cancellationToken = default) { return await QueryRepository.CountAsync(nsc => true, cancellationToken: cancellationToken); } /// /// 根据ID获取网络栈配置(包含绑定关系) /// public async Task GetNetworkStackConfigByIdWithBindingsAsync(string id, CancellationToken cancellationToken = default) { return await QueryRepository.GetByIdAsync( id, include: query => query.Include(x => x.StackCoreIMSBindings), cancellationToken: cancellationToken); } /// /// 搜索网络栈配置 /// public async Task<(int TotalCount, IList Items)> SearchNetworkStackConfigsAsync( string? networkStackName = null, string? ranId = null, bool? isActive = null, int pageNumber = 1, int pageSize = 10, CancellationToken cancellationToken = default) { // 构建查询条件 Expression> 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()); } /// /// 搜索网络栈配置(包含绑定关系) /// public async Task<(int TotalCount, IList Items)> SearchNetworkStackConfigsWithBindingsAsync( string? networkStackName = null, string? ranId = null, bool? isActive = null, int pageNumber = 1, int pageSize = 10, CancellationToken cancellationToken = default) { // 构建查询条件 Expression> 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()); } /// /// 根据ID获取网络栈配置(包含绑定关系和关联配置名称) /// public async Task> GetNetworkStackConfigByIdWithBindingNamesAsync(string id, CancellationToken cancellationToken = default) { // 使用原生SQL查询来一次性获取所有需要的数据 - PostgreSQL语法 var sql = @" SELECT nsc.""Id"" AS ""NetworkStackConfigId"", nsc.""NetworkStackName"", nsc.""NetworkStackCode"", nsc.""RanId"", ran.""Name"" AS ""RanName"", 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"", cnc.""ConfigContent"" AS ""CoreNetworkConfigContent"", binding.""ImsId"" AS ""IMSConfigId"", 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"" WHERE nsc.""Id"" = @p0 ORDER BY binding.""Index"""; var parameters = new object[] { id }; // 执行查询 var results = await ExecuteSqlQueryAsync(sql, parameters, cancellationToken); return results.ToList(); } public async Task> GetNetworkStackConfigsByCodesAsync(string[] networkStackCodes, CancellationToken cancellationToken = default) { if (networkStackCodes == null || !networkStackCodes.Any()) { return new List(); } // 构建IN查询的参数占位符 var paramPlaceholders = string.Join(",", networkStackCodes.Select((_, index) => $"@p{index}")); // 使用原生SQL查询来一次性获取所有需要的数据 - PostgreSQL语法 var sql = $@" SELECT nsc.""NetworkStackName"", nsc.""NetworkStackCode"", nsc.""RanId"", cnc.""Id"" AS ""CoreNetworkConfigId"", ims.""Id"" AS ""IMSConfigId"", ran.""ConfigContent"" AS ""RanConfigContent"", 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().ToArray(); // 执行查询 var results = await ExecuteSqlQueryAsync(sql, parameters, cancellationToken); return results.ToList(); } /// /// 搜索网络栈配置(包含绑定关系和关联配置名称,分页) /// public async Task<(int TotalCount, IList 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(); var parameters = new List(); 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(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"", 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"", cnc.""ConfigContent"" AS ""CoreNetworkConfigContent"", binding.""ImsId"" AS ""IMSConfigId"", 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(sql, parameters.ToArray(), cancellationToken); return (count, results.ToList()); } }