From 4f3b7733e0c748d416e09ad7319db53fd51ba635 Mon Sep 17 00:00:00 2001 From: Antinew <2357729423@qq.com> Date: Sat, 14 Jun 2025 17:02:06 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=E6=8E=A5=E5=8F=A3=E5=88=97?= =?UTF-8?q?=E8=A1=A8tree-list?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Interface/InterfaceCategoryService.cs | 61 ++++++++++++++++++- .../ServiceProviderInterfaceService.cs | 13 +++- .../Interface/SystemInterfaceService.cs | 19 ++++-- .../Entitys/InterfaceCategoryEntity.cs | 6 ++ .../Services/IInterfaceRepository.cs | 8 +-- .../Interface/Services/InterfaceRepository.cs | 49 ++++++++------- ...viderInterfaceTreeListInputObjectValue.cs} | 2 +- ...iderInterfaceTreeListOutputValueObject.cs} | 2 +- ...ystemInterfaceTreeListInputObjectValue.cs} | 7 +-- ... => SystemInterfaceTreeListValueObject.cs} | 15 +++-- 10 files changed, 130 insertions(+), 52 deletions(-) rename src/InterfaceForward.Repositories/Interface/ValueObjects/{ServiceProviderInterfacePaginatedInputObjectValue.cs => ServiceProviderInterfaceTreeListInputObjectValue.cs} (85%) rename src/InterfaceForward.Repositories/Interface/ValueObjects/{ServiceProviderInterfacePaginatedOutputValueObject.cs => ServiceProviderInterfaceTreeListOutputValueObject.cs} (91%) rename src/InterfaceForward.Repositories/Interface/ValueObjects/{SystemInterfacePaginatedInputObjectValue.cs => SystemInterfaceTreeListInputObjectValue.cs} (65%) rename src/InterfaceForward.Repositories/Interface/ValueObjects/{SystemInterfacePaginatedOutputValueObject.cs => SystemInterfaceTreeListValueObject.cs} (83%) diff --git a/src/InterfaceForward.Application/Services/Interface/InterfaceCategoryService.cs b/src/InterfaceForward.Application/Services/Interface/InterfaceCategoryService.cs index fc006df..beeca88 100644 --- a/src/InterfaceForward.Application/Services/Interface/InterfaceCategoryService.cs +++ b/src/InterfaceForward.Application/Services/Interface/InterfaceCategoryService.cs @@ -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 categoryRepository) : ApplicationService { + public const int CategoryRootId = 0; + + /// + /// 获取分组树 + /// + /// + /// + internal async Task>> GetCategoryTree(bool isUpstream, List datas) + where T : HasCategoryId + { + var categories = await categoryRepository.GetListAsync(x => x.IsUpStream == isUpstream); + + return GetCategoryTree(categories, CategoryRootId, datas); + } + + private List> GetCategoryTree(List categories, int pId, + List datas) where T : HasCategoryId + { + var res = new List>(); + foreach (var category in categories.Where(x => x.PId == pId)) + { + res.Add(new CategoryTreeNode + { + Id = category.Id, + Name = category.Name, + Data = datas.Where(x => x.CategoryId == category.Id).ToList(), + Children = GetCategoryTree(categories, category.Id, datas) + }); + } + + return res; + } + + /// /// 添加接口分类 /// @@ -29,7 +65,7 @@ public class InterfaceCategoryService(IBasicRepository Name = name }); } - + /// /// 更新接口分类 /// @@ -47,7 +83,7 @@ public class InterfaceCategoryService(IBasicRepository category.Name = name; await categoryRepository.UpdateAsync(category); } - + /// /// 删除接口分类 /// @@ -60,6 +96,27 @@ public class InterfaceCategoryService(IBasicRepository { throw new BusinessException("分类不存在"); } + await categoryRepository.SoftDeleteAsync(x => x.Id == id); } +} + +public class CategoryTreeNode where T : HasCategoryId +{ + public int Id { get; set; } + + /// + /// 分组名称 + /// + public string Name { get; set; } + + /// + /// 数据 + /// + public List Data { get; set; } + + /// + /// 子分组 + /// + public List> Children { get; set; } } \ No newline at end of file diff --git a/src/InterfaceForward.Application/Services/Interface/ServiceProviderInterfaceService.cs b/src/InterfaceForward.Application/Services/Interface/ServiceProviderInterfaceService.cs index abfae06..4035292 100644 --- a/src/InterfaceForward.Application/Services/Interface/ServiceProviderInterfaceService.cs +++ b/src/InterfaceForward.Application/Services/Interface/ServiceProviderInterfaceService.cs @@ -33,6 +33,9 @@ public class ServiceProviderInterfaceService : ApplicationService private IBasicRepository InterfaceMapRepository => LazyServiceProvider.GetRequiredService>(); + + private InterfaceCategoryService InterfaceCategoryService => + LazyServiceProvider.GetRequiredService(); /// @@ -41,10 +44,14 @@ public class ServiceProviderInterfaceService : ApplicationService /// /// [HttpPost("Interface/ServiceProviderInterfacePaginatedList")] - public async Task> - ServiceProviderInterfacePaginatedListAsync(ServiceProviderInterfacePaginatedInputObjectValue input) + public async Task>> + ServiceProviderInterfacePaginatedListAsync(ServiceProviderInterfaceTreeListInputObjectValue input) { - return await InterfaceRepository.GetServiceProviderInterfacePaginatedListAsync(input); + var res = await InterfaceRepository.GetServiceProviderInterfaceListAsync(input); + + var tree = await InterfaceCategoryService.GetCategoryTree(false, res); + + return tree; } /// diff --git a/src/InterfaceForward.Application/Services/Interface/SystemInterfaceService.cs b/src/InterfaceForward.Application/Services/Interface/SystemInterfaceService.cs index 3a95e12..4a69fef 100644 --- a/src/InterfaceForward.Application/Services/Interface/SystemInterfaceService.cs +++ b/src/InterfaceForward.Application/Services/Interface/SystemInterfaceService.cs @@ -24,6 +24,7 @@ public class SystemInterfaceService : ApplicationService private readonly ParameterService _parameterService; private readonly IInterfaceRepository _interfaceRepository; private readonly IBasicRepository _interfaceMapRepository; + private readonly InterfaceCategoryService _interfaceCategoryService; /// /// 系统接口下拉列表缓存key @@ -34,25 +35,31 @@ public class SystemInterfaceService : ApplicationService public SystemInterfaceService( IInterfaceRepository interfaceRepository , ParameterService parameterService - , IBasicRepository interfaceMapRepository) + , IBasicRepository interfaceMapRepository + , InterfaceCategoryService interfaceCategoryService) { _parameterService = parameterService; _interfaceMapRepository = interfaceMapRepository; + _interfaceCategoryService = interfaceCategoryService; _interfaceRepository = interfaceRepository; } #endregion /// - /// 系统接口分页列表 + /// 系统接口树形列表 /// /// /// - [HttpPost("Interface/SystemInterfacePaginatedList")] - public async Task> - SystemInterfacePaginatedListAsync(SystemInterfacePaginatedInputObjectValue input) + [HttpPost("Interface/GetSystemInterfaceTreeListAsync")] + public async Task>> + GetSystemInterfaceTreeListAsync(SystemInterfaceTreeListInputObjectValue input) { - return await _interfaceRepository.GetSystemInterfacePaginatedListAsync(input); + var res = await _interfaceRepository.GetSystemInterfaceListAsync(input); + + var tree = await _interfaceCategoryService.GetCategoryTree(true, res); + + return tree; } /// diff --git a/src/InterfaceForward.Repositories/Interface/Entitys/InterfaceCategoryEntity.cs b/src/InterfaceForward.Repositories/Interface/Entitys/InterfaceCategoryEntity.cs index 55079ca..52006bf 100644 --- a/src/InterfaceForward.Repositories/Interface/Entitys/InterfaceCategoryEntity.cs +++ b/src/InterfaceForward.Repositories/Interface/Entitys/InterfaceCategoryEntity.cs @@ -17,5 +17,11 @@ /// [SugarColumn(ColumnName = "Name")] public string Name { get; set; } = null!; + + /// + /// 0:服务商接口;1:系统接口 + /// + [SugarColumn(ColumnName = "IsUpStream")] + public bool IsUpStream { get; set; } } } \ No newline at end of file diff --git a/src/InterfaceForward.Repositories/Interface/Services/IInterfaceRepository.cs b/src/InterfaceForward.Repositories/Interface/Services/IInterfaceRepository.cs index cf249b1..dfd0e37 100644 --- a/src/InterfaceForward.Repositories/Interface/Services/IInterfaceRepository.cs +++ b/src/InterfaceForward.Repositories/Interface/Services/IInterfaceRepository.cs @@ -13,18 +13,18 @@ namespace InterfaceForward.Repositories.Interface.Services public interface IInterfaceRepository:IBasicRepository { /// - /// 获取系统接口分页列表 + /// 获取系统接口列表 /// /// /// - Task> GetSystemInterfacePaginatedListAsync(SystemInterfacePaginatedInputObjectValue input); + Task> GetSystemInterfaceListAsync(SystemInterfaceTreeListInputObjectValue input); /// - /// 获取服务商接口分页 + /// 获取服务商接口列表 /// /// /// - Task> GetServiceProviderInterfacePaginatedListAsync(ServiceProviderInterfacePaginatedInputObjectValue input); + Task> GetServiceProviderInterfaceListAsync(ServiceProviderInterfaceTreeListInputObjectValue input); /// /// 获取参数列表 diff --git a/src/InterfaceForward.Repositories/Interface/Services/InterfaceRepository.cs b/src/InterfaceForward.Repositories/Interface/Services/InterfaceRepository.cs index 4e8463a..7fb438a 100644 --- a/src/InterfaceForward.Repositories/Interface/Services/InterfaceRepository.cs +++ b/src/InterfaceForward.Repositories/Interface/Services/InterfaceRepository.cs @@ -16,39 +16,40 @@ public class InterfaceRepository : BasicRepository, IInterfaceR public string InterfaceGroup => "InterfaceGroup"; /// - public async Task> - GetSystemInterfacePaginatedListAsync(SystemInterfacePaginatedInputObjectValue input) + public async Task> + GetSystemInterfaceListAsync(SystemInterfaceTreeListInputObjectValue input) { // t_interface驱动t_interface_map,数据量上来可以在t_interface_map UpStreamId字段建立索引 var query = AsQueryable() - .LeftJoin((a, c) => a.Category == c.Id) - .LeftJoin((a, c, d) => (int)a.RequestProtocol == d.Id) - .LeftJoin((a, c, d, e) => a.RequestMethod == e.Id) + .LeftJoin((a, d) => (int)a.RequestProtocol == d.Id) + .LeftJoin((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().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().Where(x => x.UpStreamId == a.Id && !x.IsDeleted) + .DistinctCount(x => x.ServiceProviderId), + UpdateTime = a.UpdateTime, + }).ToListAsync(); } /// - public async Task> - GetServiceProviderInterfacePaginatedListAsync(ServiceProviderInterfacePaginatedInputObjectValue input) + public async Task> + GetServiceProviderInterfaceListAsync(ServiceProviderInterfaceTreeListInputObjectValue input) { var query = AsQueryable() .LeftJoin((a, b) => (int)a.RequestProtocol == b.Id) @@ -60,9 +61,12 @@ public class InterfaceRepository : BasicRepository, 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, IInterfaceR Description = a.Description, UpdateTime = a.UpdateTime, }) - .OrderByDescending(a => a.UpdateTime) - .ToPagedListAsync(input.PageIndex, input.PageSize); + .ToListAsync(); } /// diff --git a/src/InterfaceForward.Repositories/Interface/ValueObjects/ServiceProviderInterfacePaginatedInputObjectValue.cs b/src/InterfaceForward.Repositories/Interface/ValueObjects/ServiceProviderInterfaceTreeListInputObjectValue.cs similarity index 85% rename from src/InterfaceForward.Repositories/Interface/ValueObjects/ServiceProviderInterfacePaginatedInputObjectValue.cs rename to src/InterfaceForward.Repositories/Interface/ValueObjects/ServiceProviderInterfaceTreeListInputObjectValue.cs index e450bfd..c1b6338 100644 --- a/src/InterfaceForward.Repositories/Interface/ValueObjects/ServiceProviderInterfacePaginatedInputObjectValue.cs +++ b/src/InterfaceForward.Repositories/Interface/ValueObjects/ServiceProviderInterfaceTreeListInputObjectValue.cs @@ -5,7 +5,7 @@ namespace InterfaceForward.Repositories.Interface.ValueObjects; /// /// 服务商接口分页 /// -public class ServiceProviderInterfacePaginatedInputObjectValue : PagedQueryInput +public class ServiceProviderInterfaceTreeListInputObjectValue { /// /// 服务商Id diff --git a/src/InterfaceForward.Repositories/Interface/ValueObjects/ServiceProviderInterfacePaginatedOutputValueObject.cs b/src/InterfaceForward.Repositories/Interface/ValueObjects/ServiceProviderInterfaceTreeListOutputValueObject.cs similarity index 91% rename from src/InterfaceForward.Repositories/Interface/ValueObjects/ServiceProviderInterfacePaginatedOutputValueObject.cs rename to src/InterfaceForward.Repositories/Interface/ValueObjects/ServiceProviderInterfaceTreeListOutputValueObject.cs index a19a968..bdcbf6f 100644 --- a/src/InterfaceForward.Repositories/Interface/ValueObjects/ServiceProviderInterfacePaginatedOutputValueObject.cs +++ b/src/InterfaceForward.Repositories/Interface/ValueObjects/ServiceProviderInterfaceTreeListOutputValueObject.cs @@ -3,7 +3,7 @@ /// /// /// -public class ServiceProviderInterfacePaginatedOutputValueObject +public class ServiceProviderInterfaceTreeListOutputValueObject: HasCategoryId { /// /// 接口Id diff --git a/src/InterfaceForward.Repositories/Interface/ValueObjects/SystemInterfacePaginatedInputObjectValue.cs b/src/InterfaceForward.Repositories/Interface/ValueObjects/SystemInterfaceTreeListInputObjectValue.cs similarity index 65% rename from src/InterfaceForward.Repositories/Interface/ValueObjects/SystemInterfacePaginatedInputObjectValue.cs rename to src/InterfaceForward.Repositories/Interface/ValueObjects/SystemInterfaceTreeListInputObjectValue.cs index 1d3aaf2..88fc7d5 100644 --- a/src/InterfaceForward.Repositories/Interface/ValueObjects/SystemInterfacePaginatedInputObjectValue.cs +++ b/src/InterfaceForward.Repositories/Interface/ValueObjects/SystemInterfaceTreeListInputObjectValue.cs @@ -5,13 +5,8 @@ namespace InterfaceForward.Repositories.Interface.ValueObjects /// /// 分页查询系统接口 /// - public class SystemInterfacePaginatedInputObjectValue : PagedQueryInput + public class SystemInterfaceTreeListInputObjectValue { - /// - /// 接口分组,默认请选择对应0值 - /// - public int Category { get; set; } - /// /// 接口名 /// diff --git a/src/InterfaceForward.Repositories/Interface/ValueObjects/SystemInterfacePaginatedOutputValueObject.cs b/src/InterfaceForward.Repositories/Interface/ValueObjects/SystemInterfaceTreeListValueObject.cs similarity index 83% rename from src/InterfaceForward.Repositories/Interface/ValueObjects/SystemInterfacePaginatedOutputValueObject.cs rename to src/InterfaceForward.Repositories/Interface/ValueObjects/SystemInterfaceTreeListValueObject.cs index f61f8ff..a5df236 100644 --- a/src/InterfaceForward.Repositories/Interface/ValueObjects/SystemInterfacePaginatedOutputValueObject.cs +++ b/src/InterfaceForward.Repositories/Interface/ValueObjects/SystemInterfaceTreeListValueObject.cs @@ -1,20 +1,23 @@ namespace InterfaceForward.Repositories.Interface.ValueObjects { + public class HasCategoryId + { + /// + /// 接口分组id + /// + public int CategoryId { get; set; } + } + /// /// /// - public class SystemInterfacePaginatedOutputValueObject + public class SystemInterfaceTreeListValueObject: HasCategoryId { /// /// 接口Id /// public int Id { get; set; } - /// - /// 接口分组 - /// - public string Category { get; set; } - /// /// 接口名 ///