diff --git a/Service/SmartHandleService.cs b/Service/SmartHandleService.cs index 1a46ea5..d1513ae 100644 --- a/Service/SmartHandleService.cs +++ b/Service/SmartHandleService.cs @@ -85,21 +85,8 @@ public class SmartHandleService( } } - // 获取所有分类 - var categories = await categoryRepository.GetAllAsync(); - // 构建分类信息 - var categoryInfo = new StringBuilder(); - foreach (var type in new[] { 0, 1, 2 }) - { - var typeName = GetTypeName((TransactionType)type); - categoryInfo.AppendLine($"{typeName}: "); - var categoriesOfType = categories.Where(c => (int)c.Type == type).ToList(); - foreach (var category in categoriesOfType) - { - categoryInfo.AppendLine($"- {category.Name}"); - } - } + var categoryInfo = await GetCategoryInfoAsync(); // 构建账单分组信息 var billsInfo = new StringBuilder(); @@ -257,6 +244,9 @@ public class SmartHandleService( { try { + // 构建分类信息 + var categoryInfo = await GetCategoryInfoAsync(); + // 第一步:使用AI生成聚合SQL查询 var now = DateTime.Now; var sqlPrompt = $$""" @@ -313,6 +303,9 @@ public class SmartHandleService( } ``` + 【重要】请从以下分类列表中选择分类条件: + {{categoryInfo}} + 只返回SQL语句。 """; @@ -419,35 +412,6 @@ public class SmartHandleService( } } - /// - /// 查找匹配的右括号 - /// - private static int FindMatchingBrace(string str, int startPos) - { - int braceCount = 0; - for (int i = startPos; i < str.Length; i++) - { - if (str[i] == '{') braceCount++; - else if (str[i] == '}') - { - braceCount--; - if (braceCount == 0) return i; - } - } - return -1; - } - - private static string GetTypeName(TransactionType type) - { - return type switch - { - TransactionType.Expense => "支出", - TransactionType.Income => "收入", - TransactionType.None => "不计入收支", - _ => "未知" - }; - } - public async Task ParseOneLineBillAsync(string text) { // 获取所有分类 @@ -507,6 +471,56 @@ public class SmartHandleService( return null; } } + + /// + /// 查找匹配的右括号 + /// + private static int FindMatchingBrace(string str, int startPos) + { + int braceCount = 0; + for (int i = startPos; i < str.Length; i++) + { + if (str[i] == '{') braceCount++; + else if (str[i] == '}') + { + braceCount--; + if (braceCount == 0) return i; + } + } + return -1; + } + + private static string GetTypeName(TransactionType type) + { + return type switch + { + TransactionType.Expense => "支出", + TransactionType.Income => "收入", + TransactionType.None => "不计入收支", + _ => "未知" + }; + } + + private async Task GetCategoryInfoAsync() + { + // 获取所有分类 + var categories = await categoryRepository.GetAllAsync(); + + // 构建分类信息 + var categoryInfo = new StringBuilder(); + foreach (var type in new[] { 0, 1, 2 }) + { + var typeName = GetTypeName((TransactionType)type); + categoryInfo.AppendLine($"{typeName}: "); + var categoriesOfType = categories.Where(c => (int)c.Type == type).ToList(); + foreach (var category in categoriesOfType) + { + categoryInfo.AppendLine($"- {category.Name}"); + } + } + + return categoryInfo.ToString(); + } } ///