Compare commits

...

2 Commits

Author SHA1 Message Date
zzh
0a865b9d1a 1.05
All checks were successful
Build and Deploy / deploy (push) Successful in 1m8s
1.05
2026-10-12 15:03:12 +08:00
6dbc338c0f fix:interface code bug
All checks were successful
Build and Deploy / deploy (push) Successful in 46s
2026-10-12 14:58:15 +08:00
3 changed files with 22 additions and 16 deletions

View File

@ -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)

View File

@ -27,11 +27,11 @@ namespace InterfaceForward.Repositories.Interface.Services
Task<List<InterfaceParameterOutputValueObject>> GetParameterListByIdAsync(int id, bool onlyLeafs = false);
/// <summary>
/// 获取服务商接口当前用到的code
/// 获取服务商下游接口 code 末三位序号的最大值(含软删,避免复用已占用序号)
/// </summary>
/// <param name="spId"></param>
/// /// <returns></returns>
Task<string> GetCurrentCodeBySpId(int spId);
/// <returns>无记录时返回 0</returns>
Task<int> GetMaxCodeSequenceBySpId(int spId);
Task<PagedResult<InterfacePaginatedOutputValueObject>> GetInterfacePaginatedListAsync(InterfacePaginatedInputObjectValue input);
}

View File

@ -68,16 +68,25 @@ public class InterfaceRepository : BasicRepository<InterfaceEntity>, IInterfaceR
}
/// <inheritdoc />
public async Task<string> GetCurrentCodeBySpId(int spId)
public async Task<int> 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;
}
/// <inheritdoc />