fix
This commit is contained in:
@@ -75,6 +75,9 @@ public class EmailHandleService(
|
||||
);
|
||||
logger.LogInformation("成功解析邮件,共 {Count} 条交易记录", parsed.Length);
|
||||
|
||||
// TODO 接入AI分类
|
||||
// 目前已经
|
||||
|
||||
bool allSuccess = true;
|
||||
foreach (var (card, reason, amount, balance, type, occurredAt) in parsed)
|
||||
{
|
||||
|
||||
@@ -9,5 +9,5 @@ global using System.Text.Json;
|
||||
global using Entity;
|
||||
global using FreeSql;
|
||||
global using System.Linq;
|
||||
global using System.Security.Cryptography;
|
||||
global using Service.AppSettingModel;
|
||||
global using Service.AppSettingModel;
|
||||
global using System.Text.Json.Serialization;
|
||||
246
Service/SmartClassify.cs
Normal file
246
Service/SmartClassify.cs
Normal file
@@ -0,0 +1,246 @@
|
||||
namespace Service;
|
||||
|
||||
public interface ISmartHandleService
|
||||
{
|
||||
Task SmartClassifyAsync(long[] transactionIds, Action<(string , string)> chunkAction);
|
||||
}
|
||||
|
||||
public class SmartHandleService(
|
||||
ITransactionRecordRepository transactionRepository,
|
||||
ITextSegmentService textSegmentService,
|
||||
ILogger<SmartHandleService> logger,
|
||||
ITransactionCategoryRepository categoryRepository,
|
||||
IOpenAiService openAiService
|
||||
) : ISmartHandleService
|
||||
{
|
||||
public async Task SmartClassifyAsync(long[] transactionIds, Action<(string , string)> chunkAction)
|
||||
{
|
||||
try
|
||||
{
|
||||
// 获取指定ID的账单(作为样本)
|
||||
var sampleRecords = await transactionRepository.GetByIdsAsync(transactionIds);
|
||||
|
||||
if (sampleRecords.Length == 0)
|
||||
{
|
||||
// await WriteEventAsync("error", "找不到指定的账单");
|
||||
chunkAction(("error", "找不到指定的账单"));
|
||||
return;
|
||||
}
|
||||
|
||||
// 重新按Reason分组所有待分类账单
|
||||
var groupedRecords = sampleRecords
|
||||
.GroupBy(r => r.Reason)
|
||||
.Select(g => new
|
||||
{
|
||||
Reason = g.Key,
|
||||
Ids = g.Select(r => r.Id).ToList(),
|
||||
Count = g.Count(),
|
||||
TotalAmount = g.Sum(r => r.Amount),
|
||||
SampleType = g.First().Type
|
||||
})
|
||||
.OrderByDescending(g => Math.Abs(g.TotalAmount))
|
||||
.ToList();
|
||||
|
||||
// 【增强功能】对每个分组的摘要进行分词,查询已分类的相似账单
|
||||
var referenceRecords = new Dictionary<string, List<TransactionRecord>>();
|
||||
foreach (var group in groupedRecords)
|
||||
{
|
||||
// 使用专业分词库提取关键词
|
||||
var keywords = textSegmentService.ExtractKeywords(group.Reason);
|
||||
|
||||
if (keywords.Count > 0)
|
||||
{
|
||||
// 查询包含这些关键词且已分类的账单(带相关度评分)
|
||||
// minMatchRate=0.4 表示至少匹配40%的关键词才被认为是相似的
|
||||
var similarClassifiedWithScore = await transactionRepository.GetClassifiedByKeywordsWithScoreAsync(keywords, minMatchRate: 0.4, limit: 10);
|
||||
|
||||
if (similarClassifiedWithScore.Count > 0)
|
||||
{
|
||||
// 只取前5个最相关的
|
||||
var topSimilar = similarClassifiedWithScore.Take(5).Select(x => x.record).ToList();
|
||||
referenceRecords[group.Reason] = topSimilar;
|
||||
|
||||
// 记录调试信息
|
||||
logger.LogDebug("摘要 '{Reason}' 提取关键词: {Keywords}, 找到 {Count} 个相似账单,相关度分数: {Scores}",
|
||||
group.Reason,
|
||||
string.Join(", ", keywords),
|
||||
similarClassifiedWithScore.Count,
|
||||
string.Join(", ", similarClassifiedWithScore.Select(x => $"{x.record.Reason}({x.relevanceScore:F2})")));
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.LogDebug("摘要 '{Reason}' 提取关键词: {Keywords}, 未找到高相关度的相似账单",
|
||||
group.Reason,
|
||||
string.Join(", ", keywords));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 获取所有分类
|
||||
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 billsInfo = new StringBuilder();
|
||||
foreach (var (group, index) in groupedRecords.Select((g, i) => (g, i)))
|
||||
{
|
||||
billsInfo.AppendLine($"{index + 1}. 摘要={group.Reason}, 当前类型={GetTypeName(group.SampleType)}, 当前分类={(string.IsNullOrEmpty(group.SampleType.ToString()) ? "未分类" : group.SampleType.ToString())}, 涉及金额={group.TotalAmount}");
|
||||
|
||||
// 如果有相似的已分类账单,添加参考信息
|
||||
if (referenceRecords.TryGetValue(group.Reason, out var references))
|
||||
{
|
||||
billsInfo.AppendLine(" 【参考】相似且已分类的账单:");
|
||||
foreach (var refer in references.Take(3)) // 最多显示3个参考
|
||||
{
|
||||
billsInfo.AppendLine($" - 摘要={refer.Reason}, 分类={refer.Classify}, 类型={GetTypeName(refer.Type)}, 金额={refer.Amount}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var systemPrompt = $$"""
|
||||
你是一个专业的账单分类助手。请根据提供的账单分组信息和分类列表,为每个分组选择最合适的分类。
|
||||
|
||||
可用的分类列表:
|
||||
{{categoryInfo}}
|
||||
|
||||
分类规则:
|
||||
1. 根据账单的摘要和涉及金额,选择最匹配的分类
|
||||
2. 如果提供了【参考】信息,优先参考相似账单的分类,这些是历史上已分类的相似账单
|
||||
3. 如果无法确定分类,可以选择"其他"
|
||||
4. 每个分组可能包含多条账单,你需要为整个分组选择一个分类
|
||||
|
||||
请对每个分组进行分类,每次输出一个分组的分类结果,格式如下:
|
||||
{"reason": "交易摘要", "type": 0:支出/1:收入/2:不计入收支(Type为Number枚举值) ,"classify": "分类名称"}
|
||||
|
||||
只输出JSON,不要有其他文字说明。
|
||||
""";
|
||||
|
||||
var userPrompt = $$"""
|
||||
请为以下账单分组进行分类:
|
||||
|
||||
{{billsInfo}}
|
||||
|
||||
请逐个输出分类结果。
|
||||
""";
|
||||
|
||||
// 流式调用AI
|
||||
chunkAction(("start", $"开始分类,共 {sampleRecords.Length} 条账单"));
|
||||
|
||||
// 用于存储AI返回的分组分类结果
|
||||
var classifyResults = new List<(string Reason, string Classify, TransactionType Type)>();
|
||||
var buffer = new StringBuilder();
|
||||
var sendedIds = new HashSet<long>();
|
||||
await foreach (var chunk in openAiService.ChatStreamAsync(systemPrompt, userPrompt))
|
||||
{
|
||||
buffer.Append(chunk);
|
||||
|
||||
// 尝试解析完整的JSON对象
|
||||
var bufferStr = buffer.ToString();
|
||||
var startIdx = 0;
|
||||
while (startIdx < bufferStr.Length)
|
||||
{
|
||||
var openBrace = bufferStr.IndexOf('{', startIdx);
|
||||
if (openBrace == -1) break;
|
||||
|
||||
var closeBrace = FindMatchingBrace(bufferStr, openBrace);
|
||||
if (closeBrace == -1) break;
|
||||
|
||||
var jsonStr = bufferStr.Substring(openBrace, closeBrace - openBrace + 1);
|
||||
try
|
||||
{
|
||||
var result = JsonSerializer.Deserialize<GroupClassifyResult>(jsonStr);
|
||||
if (result != null && !string.IsNullOrEmpty(result.Reason))
|
||||
{
|
||||
classifyResults.Add((result.Reason, result.Classify ?? "", result.Type));
|
||||
// 每一条结果单独通知
|
||||
var group = groupedRecords.FirstOrDefault(g => g.Reason == result.Reason);
|
||||
if (group != null)
|
||||
{
|
||||
// 为该分组的所有账单ID返回分类结果
|
||||
foreach (var id in group.Ids)
|
||||
{
|
||||
if (!sendedIds.Contains(id))
|
||||
{
|
||||
sendedIds.Add(id);
|
||||
var resultJson = JsonSerializer.Serialize(new { id, result.Classify, result.Type });
|
||||
chunkAction(("data", resultJson));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogWarning(ex, "解析AI分类结果失败: {JsonStr}", jsonStr);
|
||||
}
|
||||
|
||||
startIdx = closeBrace + 1;
|
||||
}
|
||||
}
|
||||
|
||||
chunkAction(("end", "分类完成"));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "智能分类失败");
|
||||
chunkAction(("error", $"智能分类失败: {ex.Message}"));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查找匹配的右括号
|
||||
/// </summary>
|
||||
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 => "不计入收支",
|
||||
_ => "未知"
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 分组分类结果DTO(用于AI返回结果解析)
|
||||
/// </summary>
|
||||
public record GroupClassifyResult
|
||||
{
|
||||
[JsonPropertyName("reason")]
|
||||
public string Reason { get; set; } = string.Empty;
|
||||
|
||||
[JsonPropertyName("classify")]
|
||||
public string? Classify { get; set; }
|
||||
|
||||
[JsonPropertyName("type")]
|
||||
public TransactionType Type { get; set; }
|
||||
}
|
||||
|
||||
@@ -90,12 +90,17 @@ const hasActions = computed(() => !!slots['header-actions'])
|
||||
margin: 0;
|
||||
text-align: center;
|
||||
color: var(--van-text-color, #323233);
|
||||
/*超出长度*/
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
|
||||
}
|
||||
|
||||
.header-stats {
|
||||
display: flex;
|
||||
display: grid;
|
||||
grid-template-columns: 1fr auto 1fr;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
gap: 12px;
|
||||
margin-top: 12px;
|
||||
}
|
||||
@@ -104,10 +109,16 @@ const hasActions = computed(() => !!slots['header-actions'])
|
||||
margin: 0;
|
||||
font-size: 14px;
|
||||
color: var(--van-text-color-2, #646566);
|
||||
flex: 1;
|
||||
grid-column: 2;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
/* 按钮区域放在右侧 */
|
||||
.header-stats :deep(> :last-child:not(.stats-text)) {
|
||||
grid-column: 3;
|
||||
justify-self: end;
|
||||
}
|
||||
|
||||
.popup-scroll-content {
|
||||
flex: 1;
|
||||
overflow-y: auto;
|
||||
|
||||
@@ -211,7 +211,7 @@ const props = defineProps({
|
||||
// 每页数量
|
||||
pageSize: {
|
||||
type: Number,
|
||||
default: 20
|
||||
default: 5
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<template>
|
||||
<van-button
|
||||
v-if="hasTransactions"
|
||||
:type="hasClassifiedResults ? 'success' : 'primary'"
|
||||
:type="buttonType"
|
||||
size="small"
|
||||
:loading="loading || saving"
|
||||
:disabled="loading || saving"
|
||||
@@ -9,17 +9,17 @@
|
||||
class="smart-classify-btn"
|
||||
>
|
||||
<template v-if="!loading && !saving">
|
||||
<van-icon :name="hasClassifiedResults ? 'success' : 'fire'" />
|
||||
<span style="margin-left: 4px;">{{ hasClassifiedResults ? '保存分类' : '智能分类' }}</span>
|
||||
<van-icon :name="buttonIcon" />
|
||||
<span style="margin-left: 4px;">{{ buttonText }}</span>
|
||||
</template>
|
||||
<template v-else>
|
||||
{{ saving ? '保存中...' : '分类中...' }}
|
||||
<span>{{ loadingText }}</span>
|
||||
</template>
|
||||
</van-button>
|
||||
</template>
|
||||
|
||||
<script setup>
|
||||
import { ref, computed } from 'vue'
|
||||
import { ref, computed, nextTick } from 'vue'
|
||||
import { showToast, closeToast } from 'vant'
|
||||
import { smartClassify, batchUpdateClassify } from '@/api/transactionRecord'
|
||||
|
||||
@@ -34,11 +34,12 @@ const props = defineProps({
|
||||
}
|
||||
})
|
||||
|
||||
const emit = defineEmits(['update', 'save', 'beforeClassify'])
|
||||
const emit = defineEmits(['update', 'save', 'notifyDonedTransactionId'])
|
||||
|
||||
const loading = ref(false)
|
||||
const saving = ref(false)
|
||||
const classifiedResults = ref([])
|
||||
const isAllCompleted = ref(false)
|
||||
let toastInstance = null
|
||||
|
||||
const hasTransactions = computed(() => {
|
||||
@@ -46,7 +47,34 @@ const hasTransactions = computed(() => {
|
||||
})
|
||||
|
||||
const hasClassifiedResults = computed(() => {
|
||||
return classifiedResults.value.length > 0
|
||||
return isAllCompleted.value && classifiedResults.value.length > 0
|
||||
})
|
||||
|
||||
// 按钮类型
|
||||
const buttonType = computed(() => {
|
||||
if (saving.value) return 'warning'
|
||||
if (loading.value) return 'primary'
|
||||
if (hasClassifiedResults.value) return 'success'
|
||||
return 'primary'
|
||||
})
|
||||
|
||||
// 按钮图标
|
||||
const buttonIcon = computed(() => {
|
||||
if (hasClassifiedResults.value) return 'success'
|
||||
return 'fire'
|
||||
})
|
||||
|
||||
// 按钮文字(非加载状态)
|
||||
const buttonText = computed(() => {
|
||||
if (hasClassifiedResults.value) return '保存分类'
|
||||
return '智能分类'
|
||||
})
|
||||
|
||||
// 加载中文字
|
||||
const loadingText = computed(() => {
|
||||
if (saving.value) return '保存中...'
|
||||
if (loading.value) return '分类中...'
|
||||
return ''
|
||||
})
|
||||
|
||||
/**
|
||||
@@ -93,6 +121,7 @@ const handleSaveClassify = async () => {
|
||||
|
||||
// 清空已分类结果
|
||||
classifiedResults.value = []
|
||||
isAllCompleted.value = false
|
||||
|
||||
// 通知父组件刷新数据
|
||||
emit('save')
|
||||
@@ -126,10 +155,9 @@ const handleSmartClassify = async () => {
|
||||
}
|
||||
|
||||
// 清空之前的分类结果
|
||||
isAllCompleted.value = false
|
||||
classifiedResults.value = []
|
||||
|
||||
const allTransactions = props.transactions
|
||||
const totalCount = allTransactions.length
|
||||
const batchSize = 30
|
||||
let processedCount = 0
|
||||
|
||||
@@ -149,10 +177,15 @@ const handleSmartClassify = async () => {
|
||||
}
|
||||
}
|
||||
|
||||
await nextTick()
|
||||
|
||||
const allTransactions = props.transactions
|
||||
const totalCount = allTransactions.length
|
||||
|
||||
toastInstance = showToast({
|
||||
message: '正在智能分类...',
|
||||
duration: 0,
|
||||
forbidClick: true,
|
||||
forbidClick: false, // 允许用户点击页面其他地方
|
||||
loadingType: 'spinner'
|
||||
})
|
||||
|
||||
@@ -168,7 +201,7 @@ const handleSmartClassify = async () => {
|
||||
toastInstance = showToast({
|
||||
message: `正在处理第 ${currentBatch}/${totalBatches} 批 (${i + 1}-${Math.min(i + batchSize, totalCount)} / ${totalCount})...`,
|
||||
duration: 0,
|
||||
forbidClick: true,
|
||||
forbidClick: false, // 允许用户点击
|
||||
loadingType: 'spinner'
|
||||
})
|
||||
|
||||
@@ -182,6 +215,8 @@ const handleSmartClassify = async () => {
|
||||
const reader = response.body.getReader()
|
||||
const decoder = new TextDecoder()
|
||||
let buffer = ''
|
||||
let lastUpdateTime = 0
|
||||
const updateInterval = 300 // 最多每300ms更新一次Toast,减少DOM操作
|
||||
|
||||
while (true) {
|
||||
const { done, value } = await reader.read()
|
||||
@@ -216,9 +251,10 @@ const handleSmartClassify = async () => {
|
||||
toastInstance = showToast({
|
||||
message: `${eventData} (批次 ${currentBatch}/${totalBatches})`,
|
||||
duration: 0,
|
||||
forbidClick: true,
|
||||
forbidClick: false, // 允许用户点击
|
||||
loadingType: 'spinner'
|
||||
})
|
||||
lastUpdateTime = Date.now()
|
||||
} else if (eventType === 'data') {
|
||||
// 收到分类结果
|
||||
const data = JSON.parse(eventData)
|
||||
@@ -237,16 +273,21 @@ const handleSmartClassify = async () => {
|
||||
const transaction = props.transactions[index]
|
||||
transaction.upsetedClassify = data.Classify
|
||||
transaction.upsetedType = data.Type
|
||||
emit('notifyDonedTransactionId', data.id)
|
||||
}
|
||||
|
||||
// 更新进度
|
||||
closeToast()
|
||||
toastInstance = showToast({
|
||||
message: `已分类 ${processedCount}/${totalCount} 条 (批次 ${currentBatch}/${totalBatches})...`,
|
||||
duration: 0,
|
||||
forbidClick: true,
|
||||
loadingType: 'spinner'
|
||||
})
|
||||
// 限制Toast更新频率,避免频繁的DOM操作
|
||||
const now = Date.now()
|
||||
if (now - lastUpdateTime > updateInterval) {
|
||||
closeToast()
|
||||
toastInstance = showToast({
|
||||
message: `已分类 ${processedCount}/${totalCount} 条 (批次 ${currentBatch}/${totalBatches})...`,
|
||||
duration: 0,
|
||||
forbidClick: false, // 允许用户点击
|
||||
loadingType: 'spinner'
|
||||
})
|
||||
lastUpdateTime = now
|
||||
}
|
||||
} else if (eventType === 'end') {
|
||||
// 当前批次完成
|
||||
console.log(`批次 ${currentBatch}/${totalBatches} 完成`)
|
||||
@@ -265,6 +306,7 @@ const handleSmartClassify = async () => {
|
||||
// 所有批次完成
|
||||
closeToast()
|
||||
toastInstance = null
|
||||
isAllCompleted.value = true
|
||||
showToast({
|
||||
type: 'success',
|
||||
message: `分类完成,共处理 ${processedCount} 条记录,请点击"保存分类"按钮保存结果`,
|
||||
@@ -295,6 +337,7 @@ const handleSmartClassify = async () => {
|
||||
* 重置组件状态
|
||||
*/
|
||||
const reset = () => {
|
||||
isAllCompleted.value = false
|
||||
classifiedResults.value = []
|
||||
loading.value = false
|
||||
saving.value = false
|
||||
|
||||
@@ -29,7 +29,7 @@
|
||||
:transactions="dateTransactions"
|
||||
:loading="listLoading"
|
||||
:finished="true"
|
||||
:show-delete="false"
|
||||
:show-delete="true"
|
||||
@click="viewDetail"
|
||||
/>
|
||||
</PopupContainer>
|
||||
@@ -120,7 +120,7 @@ const fetchDateTransactions = async (date) => {
|
||||
.data
|
||||
.sort((a, b) => b.amount - a.amount);
|
||||
// 重置智能分类按钮
|
||||
smartClassifyButtonRef.value.reset()
|
||||
smartClassifyButtonRef.value?.reset()
|
||||
} else {
|
||||
dateTransactions.value = [];
|
||||
showToast(response.message || "获取交易列表失败");
|
||||
|
||||
@@ -290,6 +290,7 @@
|
||||
:transactions="categoryBills"
|
||||
:onBeforeClassify="beforeSmartClassify"
|
||||
@save="onSmartClassifySave"
|
||||
@notify-doned-transaction-id="handleNotifiedTransactionId"
|
||||
/>
|
||||
</template>
|
||||
|
||||
@@ -313,7 +314,7 @@
|
||||
</template>
|
||||
|
||||
<script setup>
|
||||
import { ref, computed, onMounted, onActivated } from 'vue'
|
||||
import { ref, computed, onMounted, onActivated, nextTick } from 'vue'
|
||||
import { showToast } from 'vant'
|
||||
import { useRouter } from 'vue-router'
|
||||
import { getMonthlyStatistics, getCategoryStatistics, getTrendStatistics } from '@/api/statistics'
|
||||
@@ -684,7 +685,7 @@ const loadCategoryBills = async (customIndex = null, customSize = null) => {
|
||||
billPageIndex.value++
|
||||
}
|
||||
|
||||
smartClassifyButtonRef.value.reset()
|
||||
smartClassifyButtonRef.value?.reset()
|
||||
} else {
|
||||
showToast(response.message || '加载账单失败')
|
||||
billListFinished.value = true
|
||||
@@ -749,6 +750,25 @@ const onSmartClassifySave = async () => {
|
||||
showToast('智能分类已保存')
|
||||
}
|
||||
|
||||
const handleNotifiedTransactionId = async (transactionId) => {
|
||||
console.log('收到已处理交易ID通知:', transactionId)
|
||||
// 滚动到指定的交易项
|
||||
const index = categoryBills.value.findIndex(item => item.id === transactionId)
|
||||
if (index !== -1) {
|
||||
// 等待 DOM 更新
|
||||
await nextTick()
|
||||
|
||||
const listElement = document.querySelector('.transaction-list')
|
||||
if (listElement) {
|
||||
const items = listElement.querySelectorAll('.transaction-item')
|
||||
const itemElement = items[index]
|
||||
if (itemElement) {
|
||||
itemElement.scrollIntoView({ behavior: 'smooth', block: 'center' })
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 初始化
|
||||
onMounted(() => {
|
||||
fetchStatistics()
|
||||
|
||||
@@ -8,7 +8,7 @@ public class TransactionRecordController(
|
||||
ITransactionRecordRepository transactionRepository,
|
||||
ITransactionCategoryRepository categoryRepository,
|
||||
IOpenAiService openAiService,
|
||||
ITextSegmentService textSegmentService,
|
||||
ISmartHandleService smartHandleService,
|
||||
ILogger<TransactionRecordController> logger
|
||||
) : ControllerBase
|
||||
{
|
||||
@@ -572,194 +572,20 @@ public class TransactionRecordController(
|
||||
Response.Headers.Append("Cache-Control", "no-cache");
|
||||
Response.Headers.Append("Connection", "keep-alive");
|
||||
|
||||
try
|
||||
// 验证账单ID列表
|
||||
if (request.TransactionIds == null || request.TransactionIds.Count == 0)
|
||||
{
|
||||
// 验证账单ID列表
|
||||
if (request.TransactionIds == null || request.TransactionIds.Count == 0)
|
||||
{
|
||||
await WriteEventAsync("error", "请提供要分类的账单ID");
|
||||
return;
|
||||
}
|
||||
|
||||
// 获取指定ID的账单(作为样本)
|
||||
var sampleRecords = await transactionRepository.GetByIdsAsync(request.TransactionIds.ToArray());
|
||||
|
||||
if (sampleRecords.Length == 0)
|
||||
{
|
||||
await WriteEventAsync("error", "找不到指定的账单");
|
||||
return;
|
||||
}
|
||||
|
||||
// 重新按Reason分组所有待分类账单
|
||||
var groupedRecords = sampleRecords
|
||||
.GroupBy(r => r.Reason)
|
||||
.Select(g => new
|
||||
{
|
||||
Reason = g.Key,
|
||||
Ids = g.Select(r => r.Id).ToList(),
|
||||
Count = g.Count(),
|
||||
TotalAmount = g.Sum(r => r.Amount),
|
||||
SampleType = g.First().Type
|
||||
})
|
||||
.OrderByDescending(g => Math.Abs(g.TotalAmount))
|
||||
.ToList();
|
||||
|
||||
// 【增强功能】对每个分组的摘要进行分词,查询已分类的相似账单
|
||||
var referenceRecords = new Dictionary<string, List<TransactionRecord>>();
|
||||
foreach (var group in groupedRecords)
|
||||
{
|
||||
// 使用专业分词库提取关键词
|
||||
var keywords = textSegmentService.ExtractKeywords(group.Reason);
|
||||
|
||||
if (keywords.Count > 0)
|
||||
{
|
||||
// 查询包含这些关键词且已分类的账单(带相关度评分)
|
||||
// minMatchRate=0.4 表示至少匹配40%的关键词才被认为是相似的
|
||||
var similarClassifiedWithScore = await transactionRepository.GetClassifiedByKeywordsWithScoreAsync(keywords, minMatchRate: 0.4, limit: 10);
|
||||
|
||||
if (similarClassifiedWithScore.Count > 0)
|
||||
{
|
||||
// 只取前5个最相关的
|
||||
var topSimilar = similarClassifiedWithScore.Take(5).Select(x => x.record).ToList();
|
||||
referenceRecords[group.Reason] = topSimilar;
|
||||
|
||||
// 记录调试信息
|
||||
logger.LogDebug("摘要 '{Reason}' 提取关键词: {Keywords}, 找到 {Count} 个相似账单,相关度分数: {Scores}",
|
||||
group.Reason,
|
||||
string.Join(", ", keywords),
|
||||
similarClassifiedWithScore.Count,
|
||||
string.Join(", ", similarClassifiedWithScore.Select(x => $"{x.record.Reason}({x.relevanceScore:F2})")));
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.LogDebug("摘要 '{Reason}' 提取关键词: {Keywords}, 未找到高相关度的相似账单",
|
||||
group.Reason,
|
||||
string.Join(", ", keywords));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 获取所有分类
|
||||
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 billsInfo = new StringBuilder();
|
||||
foreach (var (group, index) in groupedRecords.Select((g, i) => (g, i)))
|
||||
{
|
||||
billsInfo.AppendLine($"{index + 1}. 摘要={group.Reason}, 当前类型={GetTypeName(group.SampleType)}, 当前分类={(string.IsNullOrEmpty(group.SampleType.ToString()) ? "未分类" : group.SampleType.ToString())}, 涉及金额={group.TotalAmount}");
|
||||
|
||||
// 如果有相似的已分类账单,添加参考信息
|
||||
if (referenceRecords.TryGetValue(group.Reason, out var references))
|
||||
{
|
||||
billsInfo.AppendLine(" 【参考】相似且已分类的账单:");
|
||||
foreach (var refer in references.Take(3)) // 最多显示3个参考
|
||||
{
|
||||
billsInfo.AppendLine($" - 摘要={refer.Reason}, 分类={refer.Classify}, 类型={GetTypeName(refer.Type)}, 金额={refer.Amount}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var systemPrompt = $$"""
|
||||
你是一个专业的账单分类助手。请根据提供的账单分组信息和分类列表,为每个分组选择最合适的分类。
|
||||
|
||||
可用的分类列表:
|
||||
{{categoryInfo}}
|
||||
|
||||
分类规则:
|
||||
1. 根据账单的摘要和涉及金额,选择最匹配的分类
|
||||
2. 如果提供了【参考】信息,优先参考相似账单的分类,这些是历史上已分类的相似账单
|
||||
3. 如果无法确定分类,可以选择"其他"
|
||||
4. 每个分组可能包含多条账单,你需要为整个分组选择一个分类
|
||||
|
||||
请对每个分组进行分类,每次输出一个分组的分类结果,格式如下:
|
||||
{"reason": "交易摘要", "type": 0:支出/1:收入/2:不计入收支(Type为Number枚举值) ,"classify": "分类名称"}
|
||||
|
||||
只输出JSON,不要有其他文字说明。
|
||||
""";
|
||||
|
||||
var userPrompt = $$"""
|
||||
请为以下账单分组进行分类:
|
||||
|
||||
{{billsInfo}}
|
||||
|
||||
请逐个输出分类结果。
|
||||
""";
|
||||
|
||||
// 流式调用AI
|
||||
await WriteEventAsync("start", $"开始分类,共 {sampleRecords.Length} 条账单");
|
||||
|
||||
// 用于存储AI返回的分组分类结果
|
||||
var classifyResults = new List<(string Reason, string Classify, TransactionType Type)>();
|
||||
var buffer = new StringBuilder();
|
||||
var sendedIds = new HashSet<long>();
|
||||
await foreach (var chunk in openAiService.ChatStreamAsync(systemPrompt, userPrompt))
|
||||
{
|
||||
buffer.Append(chunk);
|
||||
|
||||
// 尝试解析完整的JSON对象
|
||||
var bufferStr = buffer.ToString();
|
||||
var startIdx = 0;
|
||||
while (startIdx < bufferStr.Length)
|
||||
{
|
||||
var openBrace = bufferStr.IndexOf('{', startIdx);
|
||||
if (openBrace == -1) break;
|
||||
|
||||
var closeBrace = FindMatchingBrace(bufferStr, openBrace);
|
||||
if (closeBrace == -1) break;
|
||||
|
||||
var jsonStr = bufferStr.Substring(openBrace, closeBrace - openBrace + 1);
|
||||
try
|
||||
{
|
||||
var result = JsonSerializer.Deserialize<GroupClassifyResult>(jsonStr);
|
||||
if (result != null && !string.IsNullOrEmpty(result.Reason))
|
||||
{
|
||||
classifyResults.Add((result.Reason, result.Classify ?? "", result.Type));
|
||||
// 每一条结果单独通知
|
||||
var group = groupedRecords.FirstOrDefault(g => g.Reason == result.Reason);
|
||||
if (group != null)
|
||||
{
|
||||
// 为该分组的所有账单ID返回分类结果
|
||||
foreach (var id in group.Ids)
|
||||
{
|
||||
if (!sendedIds.Contains(id))
|
||||
{
|
||||
sendedIds.Add(id);
|
||||
var resultJson = JsonSerializer.Serialize(new { id, result.Classify, result.Type });
|
||||
await WriteEventAsync("data", resultJson);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogWarning(ex, "解析AI分类结果失败: {JsonStr}", jsonStr);
|
||||
}
|
||||
|
||||
startIdx = closeBrace + 1;
|
||||
}
|
||||
}
|
||||
|
||||
await WriteEventAsync("end", "分类完成");
|
||||
await WriteEventAsync("error", "请提供要分类的账单ID");
|
||||
return;
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
||||
await smartHandleService.SmartClassifyAsync(request.TransactionIds.ToArray(), async (chunk) =>
|
||||
{
|
||||
logger.LogError(ex, "智能分类失败");
|
||||
await WriteEventAsync("error", $"智能分类失败: {ex.Message}");
|
||||
}
|
||||
var (eventType, content) = chunk;
|
||||
await WriteEventAsync(eventType, content);
|
||||
});
|
||||
|
||||
await Response.Body.FlushAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -1074,21 +900,6 @@ public record SmartClassifyRequest(
|
||||
List<long>? TransactionIds = null
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
/// 分组分类结果DTO(用于AI返回结果解析)
|
||||
/// </summary>
|
||||
public record GroupClassifyResult
|
||||
{
|
||||
[JsonPropertyName("reason")]
|
||||
public string Reason { get; set; } = string.Empty;
|
||||
|
||||
[JsonPropertyName("classify")]
|
||||
public string? Classify { get; set; }
|
||||
|
||||
[JsonPropertyName("type")]
|
||||
public TransactionType Type { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 批量更新分类项DTO
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user