diff --git a/src/InterfaceForward.Application/Services/Interface/InterfaceService.cs b/src/InterfaceForward.Application/Services/Interface/InterfaceService.cs index e8ad644..9256f66 100644 --- a/src/InterfaceForward.Application/Services/Interface/InterfaceService.cs +++ b/src/InterfaceForward.Application/Services/Interface/InterfaceService.cs @@ -273,15 +273,12 @@ public class InterfaceService : ApplicationService var entity = await ServiceProviderRepository.GetFirstAsync(x => x.Id == serviceProviderId); if (entity == null) throw new BusinessException("服务商不存在,系统接口自己定义code谢谢配合"); - string res = entity.Code; + // 取末三位序号最大值,不能按 Id 取最后一条(删除/手工建码会导致 Id 与序号不一致) + var maxSequence = await InterfaceRepository.GetMaxCodeSequenceBySpId(serviceProviderId); + if (maxSequence >= 999) + throw new BusinessException("该服务商接口代码已达上限(999)"); - // tips:要考虑并发 - var code = await InterfaceRepository.GetCurrentCodeBySpId(serviceProviderId); - int count = 0; - if (!string.IsNullOrEmpty(code)) count = int.Parse(code.Substring(code.Length - 3, 3)); - res += (count + 1).ToString().PadLeft(3, '0'); - - return res; + return entity.Code + (maxSequence + 1).ToString().PadLeft(3, '0'); } private async Task UpdateInterfaceAsync(SaveInterfaceInput input) diff --git a/src/InterfaceForward.Repositories/Interface/Services/IInterfaceRepository.cs b/src/InterfaceForward.Repositories/Interface/Services/IInterfaceRepository.cs index 76e0823..14ab411 100644 --- a/src/InterfaceForward.Repositories/Interface/Services/IInterfaceRepository.cs +++ b/src/InterfaceForward.Repositories/Interface/Services/IInterfaceRepository.cs @@ -27,11 +27,11 @@ namespace InterfaceForward.Repositories.Interface.Services Task> GetParameterListByIdAsync(int id, bool onlyLeafs = false); /// - /// 获取服务商接口当前用到的code + /// 获取服务商下游接口 code 末三位序号的最大值(含软删,避免复用已占用序号) /// /// - /// /// - Task GetCurrentCodeBySpId(int spId); + /// 无记录时返回 0 + Task GetMaxCodeSequenceBySpId(int spId); Task> GetInterfacePaginatedListAsync(InterfacePaginatedInputObjectValue input); } diff --git a/src/InterfaceForward.Repositories/Interface/Services/InterfaceRepository.cs b/src/InterfaceForward.Repositories/Interface/Services/InterfaceRepository.cs index c5cd256..363bd95 100644 --- a/src/InterfaceForward.Repositories/Interface/Services/InterfaceRepository.cs +++ b/src/InterfaceForward.Repositories/Interface/Services/InterfaceRepository.cs @@ -68,16 +68,25 @@ public class InterfaceRepository : BasicRepository, IInterfaceR } /// - public async Task GetCurrentCodeBySpId(int spId) + public async Task GetMaxCodeSequenceBySpId(int spId) { - var code = await AsQueryable() + var codes = await AsQueryable() .Where(x => x.ServiceProviderId == spId && !x.IsUpStream) .Filter(null, true) - .OrderByDescending(x => x.Id) .Select(x => x.Code) - .FirstAsync(); + .ToListAsync(); - return code; + var max = 0; + foreach (var code in codes) + { + if (string.IsNullOrEmpty(code) || code.Length < 3) + continue; + + if (int.TryParse(code.AsSpan(code.Length - 3, 3), out var sequence) && sequence > max) + max = sequence; + } + + return max; } ///