This commit is contained in:
SunCheng
2026-02-10 17:49:19 +08:00
parent 3e18283e52
commit d052ae5197
104 changed files with 10369 additions and 3000 deletions

View File

@@ -1,4 +1,4 @@
using Quartz;
using Quartz;
using Service.AI;
namespace Service.Jobs;
@@ -9,7 +9,7 @@ namespace Service.Jobs;
/// </summary>
public class CategoryIconGenerationJob(
ITransactionCategoryRepository categoryRepository,
IOpenAiService openAiService,
ISmartHandleService smartHandleService,
ILogger<CategoryIconGenerationJob> logger) : IJob
{
private static readonly SemaphoreSlim _semaphore = new(1, 1);
@@ -77,74 +77,21 @@ public class CategoryIconGenerationJob(
logger.LogInformation("正在为分类 {CategoryName}(ID:{CategoryId}) 生成图标",
category.Name, category.Id);
var typeText = category.Type == TransactionType.Expense ? "支出" : "收入";
// 使用 SmartHandleService 统一封装的图标生成方法
var icons = await smartHandleService.GenerateCategoryIconsAsync(category.Name, category.Type, iconCount: 5);
var systemPrompt = """
SVG
5 SVG
1. 24x24viewBox="0 0 24 24"
2. 使
- 使 <linearGradient> <radialGradient>
- 使
-
3. 5
- 1使
- 2线
- 33D使
- 4
- 5线
4.
-
-
-
5.
6. JSON 5 SVG
SVG gradient
""";
var userPrompt = $"""
分类名称:{category.Name}
分类类型:{typeText}
请为这个分类生成 5 个精美的、风格各异的彩色 SVG 图标。
确保每个图标都有独特的视觉特征,不会与其他图标混淆。
返回格式(纯 JSON 数组,无其他内容):
["<svg>...</svg>", "<svg>...</svg>", "<svg>...</svg>", "<svg>...</svg>", "<svg>...</svg>"]
""";
var response = await openAiService.ChatAsync(systemPrompt, userPrompt, timeoutSeconds: 60 * 10);
if (string.IsNullOrWhiteSpace(response))
if (icons == null || icons.Count == 0)
{
logger.LogWarning("AI 未返回有效的图标数据,分类: {CategoryName}", category.Name);
logger.LogWarning("分类 {CategoryName}(ID:{CategoryId}) 生成图标失败",
category.Name, category.Id);
return;
}
// 验证返回的是有效的 JSON 数组
try
{
var icons = JsonSerializer.Deserialize<List<string>>(response);
if (icons == null || icons.Count != 5)
{
logger.LogWarning("AI 返回的图标数量不正确期望5个分类: {CategoryName}", category.Name);
return;
}
// 保存图标到数据库
category.Icon = JsonSerializer.Serialize(icons);
await categoryRepository.UpdateAsync(category);
// 保存图标到数据库
category.Icon = response;
await categoryRepository.UpdateAsync(category);
logger.LogInformation("成功为分类 {CategoryName}(ID:{CategoryId}) 生成并保存了 5 个图标",
category.Name, category.Id);
}
catch (JsonException ex)
{
logger.LogError(ex, "解析 AI 返回的图标数据失败,分类: {CategoryName},响应内容: {Response}",
category.Name, response);
}
logger.LogInformation("成功为分类 {CategoryName}(ID:{CategoryId}) 生成并保存了 {IconCount} 个图标",
category.Name, category.Id, icons.Count);
}
}