refactor: 优化AI评分与二维码ID生成逻辑,调整RabbitMQ配置

1. 调整RabbitMQ预取计数配置,支持从配置读取
2. 新增随机ID帮助类,生成唯一长整型ID
3. 重构二维码ID生成逻辑,新增重试机制避免重复
4. 优化AI评分配置,调整温度系数与并发限制
5. 重构跨页题评分逻辑,支持分组评分与结果去重
6. 新增AI评分异常分类与结果校验逻辑
7. 优化评分提示词与结果归一化处理
This commit is contained in:
glz
2026-07-02 16:05:13 +08:00
parent 519428c52d
commit 12d488a5ca
5 changed files with 524 additions and 85 deletions

View File

@ -32,6 +32,7 @@ public class UserJournalService(
private const string QrCodeGenerateQueue = "mq.journal.qrcode.generate";
private const string QrCodeGenerateRoutingKey = "rk.journal.qrcode.generate";
private const int MaxBatchQrCodeCount = 500;
private const int MaxRandomIdGenerateRetryCount = 5;
/// <summary>
/// 用户绑定期刊(扫码绑定)
@ -183,8 +184,10 @@ public class UserJournalService(
throw new BusinessException("该期刊暂未发布,无法生成二维码", ResultCode.UNPROCESSABLE_ENTITY);
}
var recordId = await GenerateUniqueQrCodeIdAsync();
var record = new UserJournal
{
Id = recordId,
UserId = null,
JournalId = input.JournalId,
Type = 0,
@ -243,9 +246,11 @@ public class UserJournalService(
}
var now = DateTime.Now;
var records = Enumerable.Range(0, input.Count)
.Select(_ => new UserJournal
var randomIds = await GenerateUniqueQrCodeIdsAsync(input.Count);
var records = randomIds
.Select(id => new UserJournal
{
Id = id,
UserId = null,
JournalId = input.JournalId,
Type = 0,
@ -459,6 +464,51 @@ public class UserJournalService(
return JsonSerializer.Serialize(new { JournalId = journalId, Id = id });
}
private async Task<long> GenerateUniqueQrCodeIdAsync()
{
for (var i = 0; i < MaxRandomIdGenerateRetryCount; i++)
{
var id = RandomIdHelper.GenerateLongId();
var exists = await userJournalRepository.Queryable()
.AnyAsync(uj => uj.Id == id);
if (!exists)
{
return id;
}
}
throw new BusinessException("生成二维码ID失败请稍后重试", ResultCode.GLOBAL_ERROR);
}
private async Task<List<long>> GenerateUniqueQrCodeIdsAsync(int count)
{
var ids = new HashSet<long>();
for (var i = 0; i < MaxRandomIdGenerateRetryCount && ids.Count < count; i++)
{
while (ids.Count < count)
{
ids.Add(RandomIdHelper.GenerateLongId());
}
var candidateIds = ids.ToList();
var existingIds = await userJournalRepository.Queryable()
.Where(uj => candidateIds.Contains(uj.Id))
.Select(uj => uj.Id)
.ToListAsync();
if (existingIds.Count == 0)
{
return candidateIds;
}
ids.ExceptWith(existingIds);
}
throw new BusinessException("生成二维码ID失败请稍后重试", ResultCode.GLOBAL_ERROR);
}
private async Task SendBindJournalMessageAsync(Users user, Journal journal)
{
try