Compare commits

..

No commits in common. "0a865b9d1ade1c546deae227420920c1654d6a2b" and "0ded5da8dd0494867b9c8a9184c5b36c61e36f22" have entirely different histories.

3 changed files with 16 additions and 22 deletions

View File

@ -273,12 +273,15 @@ public class InterfaceService : ApplicationService
var entity = await ServiceProviderRepository.GetFirstAsync(x => x.Id == serviceProviderId);
if (entity == null) throw new BusinessException("服务商不存在,系统接口自己定义code谢谢配合");
// 取末三位序号最大值,不能按 Id 取最后一条(删除/手工建码会导致 Id 与序号不一致)
var maxSequence = await InterfaceRepository.GetMaxCodeSequenceBySpId(serviceProviderId);
if (maxSequence >= 999)
throw new BusinessException("该服务商接口代码已达上限(999)");
string res = entity.Code;
return entity.Code + (maxSequence + 1).ToString().PadLeft(3, '0');
// 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;
}
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>无记录时返回 0</returns>
Task<int> GetMaxCodeSequenceBySpId(int spId);
/// /// <returns></returns>
Task<string> GetCurrentCodeBySpId(int spId);
Task<PagedResult<InterfacePaginatedOutputValueObject>> GetInterfacePaginatedListAsync(InterfacePaginatedInputObjectValue input);
}

View File

@ -68,25 +68,16 @@ public class InterfaceRepository : BasicRepository<InterfaceEntity>, IInterfaceR
}
/// <inheritdoc />
public async Task<int> GetMaxCodeSequenceBySpId(int spId)
public async Task<string> GetCurrentCodeBySpId(int spId)
{
var codes = await AsQueryable()
var code = await AsQueryable()
.Where(x => x.ServiceProviderId == spId && !x.IsUpStream)
.Filter(null, true)
.OrderByDescending(x => x.Id)
.Select(x => x.Code)
.ToListAsync();
.FirstAsync();
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;
return code;
}
/// <inheritdoc />