重构接口列表tree-list

This commit is contained in:
Antinew 2025-06-14 17:02:06 +08:00
parent 317dc30b17
commit 4f3b7733e0
10 changed files with 130 additions and 52 deletions

View File

@ -1,6 +1,8 @@
using InterfaceForward.Repositories;
using InterfaceForward.Repositories.Interface.Entitys;
using InterfaceForward.Repositories.Interface.ValueObjects;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
namespace InterfaceForward.Application.Services.Interface;
@ -10,6 +12,40 @@ namespace InterfaceForward.Application.Services.Interface;
[ApiExplorerSettings(GroupName = "接口服务")]
public class InterfaceCategoryService(IBasicRepository<InterfaceCategoryEntity> categoryRepository) : ApplicationService
{
public const int CategoryRootId = 0;
/// <summary>
/// 获取分组树
/// </summary>
/// <param name="isUpstream"></param>
/// <param name="datas"></param>
internal async Task<List<CategoryTreeNode<T>>> GetCategoryTree<T>(bool isUpstream, List<T> datas)
where T : HasCategoryId
{
var categories = await categoryRepository.GetListAsync(x => x.IsUpStream == isUpstream);
return GetCategoryTree(categories, CategoryRootId, datas);
}
private List<CategoryTreeNode<T>> GetCategoryTree<T>(List<InterfaceCategoryEntity> categories, int pId,
List<T> datas) where T : HasCategoryId
{
var res = new List<CategoryTreeNode<T>>();
foreach (var category in categories.Where(x => x.PId == pId))
{
res.Add(new CategoryTreeNode<T>
{
Id = category.Id,
Name = category.Name,
Data = datas.Where(x => x.CategoryId == category.Id).ToList(),
Children = GetCategoryTree(categories, category.Id, datas)
});
}
return res;
}
/// <summary>
/// 添加接口分类
/// </summary>
@ -29,7 +65,7 @@ public class InterfaceCategoryService(IBasicRepository<InterfaceCategoryEntity>
Name = name
});
}
/// <summary>
/// 更新接口分类
/// </summary>
@ -47,7 +83,7 @@ public class InterfaceCategoryService(IBasicRepository<InterfaceCategoryEntity>
category.Name = name;
await categoryRepository.UpdateAsync(category);
}
/// <summary>
/// 删除接口分类
/// </summary>
@ -60,6 +96,27 @@ public class InterfaceCategoryService(IBasicRepository<InterfaceCategoryEntity>
{
throw new BusinessException("分类不存在");
}
await categoryRepository.SoftDeleteAsync(x => x.Id == id);
}
}
public class CategoryTreeNode<T> where T : HasCategoryId
{
public int Id { get; set; }
/// <summary>
/// 分组名称
/// </summary>
public string Name { get; set; }
/// <summary>
/// 数据
/// </summary>
public List<T> Data { get; set; }
/// <summary>
/// 子分组
/// </summary>
public List<CategoryTreeNode<T>> Children { get; set; }
}

View File

@ -33,6 +33,9 @@ public class ServiceProviderInterfaceService : ApplicationService
private IBasicRepository<InterfaceMapEntity> InterfaceMapRepository =>
LazyServiceProvider.GetRequiredService<IBasicRepository<InterfaceMapEntity>>();
private InterfaceCategoryService InterfaceCategoryService =>
LazyServiceProvider.GetRequiredService<InterfaceCategoryService>();
/// <summary>
@ -41,10 +44,14 @@ public class ServiceProviderInterfaceService : ApplicationService
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("Interface/ServiceProviderInterfacePaginatedList")]
public async Task<PagedResult<ServiceProviderInterfacePaginatedOutputValueObject>>
ServiceProviderInterfacePaginatedListAsync(ServiceProviderInterfacePaginatedInputObjectValue input)
public async Task<List<CategoryTreeNode<ServiceProviderInterfaceTreeListOutputValueObject>>>
ServiceProviderInterfacePaginatedListAsync(ServiceProviderInterfaceTreeListInputObjectValue input)
{
return await InterfaceRepository.GetServiceProviderInterfacePaginatedListAsync(input);
var res = await InterfaceRepository.GetServiceProviderInterfaceListAsync(input);
var tree = await InterfaceCategoryService.GetCategoryTree(false, res);
return tree;
}
/// <summary>

View File

@ -24,6 +24,7 @@ public class SystemInterfaceService : ApplicationService
private readonly ParameterService _parameterService;
private readonly IInterfaceRepository _interfaceRepository;
private readonly IBasicRepository<InterfaceMapEntity> _interfaceMapRepository;
private readonly InterfaceCategoryService _interfaceCategoryService;
/// <summary>
/// 系统接口下拉列表缓存key
@ -34,25 +35,31 @@ public class SystemInterfaceService : ApplicationService
public SystemInterfaceService(
IInterfaceRepository interfaceRepository
, ParameterService parameterService
, IBasicRepository<InterfaceMapEntity> interfaceMapRepository)
, IBasicRepository<InterfaceMapEntity> interfaceMapRepository
, InterfaceCategoryService interfaceCategoryService)
{
_parameterService = parameterService;
_interfaceMapRepository = interfaceMapRepository;
_interfaceCategoryService = interfaceCategoryService;
_interfaceRepository = interfaceRepository;
}
#endregion
/// <summary>
/// 系统接口分页列表
/// 系统接口树形列表
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("Interface/SystemInterfacePaginatedList")]
public async Task<PagedResult<SystemInterfacePaginatedOutputValueObject>>
SystemInterfacePaginatedListAsync(SystemInterfacePaginatedInputObjectValue input)
[HttpPost("Interface/GetSystemInterfaceTreeListAsync")]
public async Task<List<CategoryTreeNode<SystemInterfaceTreeListValueObject>>>
GetSystemInterfaceTreeListAsync(SystemInterfaceTreeListInputObjectValue input)
{
return await _interfaceRepository.GetSystemInterfacePaginatedListAsync(input);
var res = await _interfaceRepository.GetSystemInterfaceListAsync(input);
var tree = await _interfaceCategoryService.GetCategoryTree(true, res);
return tree;
}
/// <summary>

View File

@ -17,5 +17,11 @@
///</summary>
[SugarColumn(ColumnName = "Name")]
public string Name { get; set; } = null!;
///<summary>
/// 0:服务商接口1:系统接口
///</summary>
[SugarColumn(ColumnName = "IsUpStream")]
public bool IsUpStream { get; set; }
}
}

View File

@ -13,18 +13,18 @@ namespace InterfaceForward.Repositories.Interface.Services
public interface IInterfaceRepository:IBasicRepository<InterfaceEntity>
{
/// <summary>
/// 获取系统接口分页列表
/// 获取系统接口列表
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
Task<PagedResult<SystemInterfacePaginatedOutputValueObject>> GetSystemInterfacePaginatedListAsync(SystemInterfacePaginatedInputObjectValue input);
Task<List<SystemInterfaceTreeListValueObject>> GetSystemInterfaceListAsync(SystemInterfaceTreeListInputObjectValue input);
/// <summary>
/// 获取服务商接口分页
/// 获取服务商接口列表
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
Task<PagedResult<ServiceProviderInterfacePaginatedOutputValueObject>> GetServiceProviderInterfacePaginatedListAsync(ServiceProviderInterfacePaginatedInputObjectValue input);
Task<List<ServiceProviderInterfaceTreeListOutputValueObject>> GetServiceProviderInterfaceListAsync(ServiceProviderInterfaceTreeListInputObjectValue input);
/// <summary>
/// 获取参数列表

View File

@ -16,39 +16,40 @@ public class InterfaceRepository : BasicRepository<InterfaceEntity>, IInterfaceR
public string InterfaceGroup => "InterfaceGroup";
/// <inheritdoc />
public async Task<PagedResult<SystemInterfacePaginatedOutputValueObject>>
GetSystemInterfacePaginatedListAsync(SystemInterfacePaginatedInputObjectValue input)
public async Task<List<SystemInterfaceTreeListValueObject>>
GetSystemInterfaceListAsync(SystemInterfaceTreeListInputObjectValue input)
{
// t_interface驱动t_interface_map数据量上来可以在t_interface_map UpStreamId字段建立索引
var query = AsQueryable()
.LeftJoin<DictionaryEntity>((a, c) => a.Category == c.Id)
.LeftJoin<DictionaryEntity>((a, c, d) => (int)a.RequestProtocol == d.Id)
.LeftJoin<DictionaryEntity>((a, c, d, e) => a.RequestMethod == e.Id)
.LeftJoin<DictionaryEntity>((a, d) => (int)a.RequestProtocol == d.Id)
.LeftJoin<DictionaryEntity>((a, d, e) => a.RequestMethod == e.Id)
.Where(a => a.IsUpStream)
;
query.WhereIF(input.Category != 0, a => a.Category == input.Category) // 默认“请选择”对应0数据源在数据库字典中配置
query
.WhereIF(!string.IsNullOrEmpty(input.Name), a => a.Name.Contains(input.Name!)) // 默认为空,支持模糊搜索
.WhereIF(!string.IsNullOrEmpty(input.Code), a => a.Code.Contains(input.Code!)) // 默认为空,支持模糊搜索
;
return await query.Select((a, c, d, e) => new SystemInterfacePaginatedOutputValueObject
{
Id = a.Id,
Category = c.Name,
Name = a.Name,
Code = a.Code,
RequestProtocol = d.Name,
Description = a.Description,
MapCount = SqlFunc.Subqueryable<InterfaceMapEntity>().Where(x => x.UpStreamId == a.Id && !x.IsDeleted)
.DistinctCount(x => x.ServiceProviderId),
UpdateTime = a.UpdateTime,
}).OrderByDescending(a => a.UpdateTime).ToPagedListAsync(input.PageIndex, input.PageSize);
return await query
.OrderBy(a => a.CreateTime)
.Select((a, d, e) => new SystemInterfaceTreeListValueObject
{
Id = a.Id,
CategoryId = a.Category,
Name = a.Name,
Code = a.Code,
RequestProtocol = d.Name,
Description = a.Description,
MapCount = SqlFunc.Subqueryable<InterfaceMapEntity>().Where(x => x.UpStreamId == a.Id && !x.IsDeleted)
.DistinctCount(x => x.ServiceProviderId),
UpdateTime = a.UpdateTime,
}).ToListAsync();
}
/// <inheritdoc />
public async Task<PagedResult<ServiceProviderInterfacePaginatedOutputValueObject>>
GetServiceProviderInterfacePaginatedListAsync(ServiceProviderInterfacePaginatedInputObjectValue input)
public async Task<List<ServiceProviderInterfaceTreeListOutputValueObject>>
GetServiceProviderInterfaceListAsync(ServiceProviderInterfaceTreeListInputObjectValue input)
{
var query = AsQueryable()
.LeftJoin<DictionaryEntity>((a, b) => (int)a.RequestProtocol == b.Id)
@ -60,9 +61,12 @@ public class InterfaceRepository : BasicRepository<InterfaceEntity>, IInterfaceR
.WhereIF(!string.IsNullOrEmpty(input.Name), a => a.Name.Contains(input.Name!)) // 默认为空,支持模糊搜索
.WhereIF(!string.IsNullOrEmpty(input.Code), a => a.Code.Contains(input.Code!)) // 默认为空,支持模糊搜索
;
return await query.Select((a, b, c) => new ServiceProviderInterfacePaginatedOutputValueObject
return await query
.OrderBy(a => a.CreateTime)
.Select((a, b, c) => new ServiceProviderInterfaceTreeListOutputValueObject
{
Id = a.Id,
CategoryId = a.Category,
Name = a.Name,
Code = a.Code,
RequestProtocol = b.Name,
@ -71,8 +75,7 @@ public class InterfaceRepository : BasicRepository<InterfaceEntity>, IInterfaceR
Description = a.Description,
UpdateTime = a.UpdateTime,
})
.OrderByDescending(a => a.UpdateTime)
.ToPagedListAsync(input.PageIndex, input.PageSize);
.ToListAsync();
}
/// <inheritdoc />

View File

@ -5,7 +5,7 @@ namespace InterfaceForward.Repositories.Interface.ValueObjects;
/// <summary>
/// 服务商接口分页
/// </summary>
public class ServiceProviderInterfacePaginatedInputObjectValue : PagedQueryInput
public class ServiceProviderInterfaceTreeListInputObjectValue
{
/// <summary>
/// 服务商Id

View File

@ -3,7 +3,7 @@
/// <summary>
///
/// </summary>
public class ServiceProviderInterfacePaginatedOutputValueObject
public class ServiceProviderInterfaceTreeListOutputValueObject: HasCategoryId
{
/// <summary>
/// 接口Id

View File

@ -5,13 +5,8 @@ namespace InterfaceForward.Repositories.Interface.ValueObjects
/// <summary>
/// 分页查询系统接口
/// </summary>
public class SystemInterfacePaginatedInputObjectValue : PagedQueryInput
public class SystemInterfaceTreeListInputObjectValue
{
///<summary>
/// 接口分组默认请选择对应0值
///</summary>
public int Category { get; set; }
///<summary>
/// 接口名
///</summary>

View File

@ -1,20 +1,23 @@
namespace InterfaceForward.Repositories.Interface.ValueObjects
{
public class HasCategoryId
{
///<summary>
/// 接口分组id
///</summary>
public int CategoryId { get; set; }
}
/// <summary>
///
/// </summary>
public class SystemInterfacePaginatedOutputValueObject
public class SystemInterfaceTreeListValueObject: HasCategoryId
{
/// <summary>
/// 接口Id
/// </summary>
public int Id { get; set; }
///<summary>
/// 接口分组
///</summary>
public string Category { get; set; }
///<summary>
/// 接口名
///</summary>