From 34c641653819ee29c1ffaa0d3ddfc99b490a0173 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=99=E8=AF=9A?= Date: Mon, 5 Jan 2026 15:51:47 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=E5=88=86=E7=B1=BB=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=E6=9E=84=E5=BB=BA=E9=80=BB=E8=BE=91=EF=BC=8C=E6=8F=90?= =?UTF-8?q?=E5=8F=96=E4=B8=BA=E7=8B=AC=E7=AB=8B=E6=96=B9=E6=B3=95=E5=B9=B6?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Service/SmartHandleService.cs | 100 +++++++++++++++++++--------------- 1 file changed, 57 insertions(+), 43 deletions(-) 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(); + } } ///