代码优化
This commit is contained in:
@@ -21,7 +21,8 @@ public class EmailHandleService(
|
||||
IEmailMessageRepository emailRepo,
|
||||
ITransactionRecordRepository trxRepo,
|
||||
IEnumerable<IEmailParseServices> emailParsers,
|
||||
IMessageRecordService messageRecordService
|
||||
IMessageRecordService messageRecordService,
|
||||
ISmartHandleService smartHandleService
|
||||
) : IEmailHandleService
|
||||
{
|
||||
public async Task<bool> HandleEmailAsync(
|
||||
@@ -79,11 +80,12 @@ public class EmailHandleService(
|
||||
// 目前已经
|
||||
|
||||
bool allSuccess = true;
|
||||
var records = new List<TransactionRecord>();
|
||||
foreach (var (card, reason, amount, balance, type, occurredAt) in parsed)
|
||||
{
|
||||
logger.LogInformation("处理交易记录: 卡号 {Card}, 交易原因 {Reason}, 金额 {Amount}, 余额 {Balance}, 类型 {Type}", card, reason, amount, balance, type);
|
||||
|
||||
var success = await SaveTransactionRecordAsync(
|
||||
var record = await SaveTransactionRecordAsync(
|
||||
card,
|
||||
reason,
|
||||
amount,
|
||||
@@ -93,12 +95,17 @@ public class EmailHandleService(
|
||||
emailMessage.Id
|
||||
);
|
||||
|
||||
if (!success)
|
||||
if (record == null)
|
||||
{
|
||||
allSuccess = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
records.Add(record);
|
||||
}
|
||||
|
||||
_ = await AnalyzeClassifyAsync(records.ToArray());
|
||||
|
||||
return allSuccess;
|
||||
}
|
||||
|
||||
@@ -141,11 +148,12 @@ public class EmailHandleService(
|
||||
logger.LogInformation("成功解析邮件,共 {Count} 条交易记录", parsed.Length);
|
||||
|
||||
bool allSuccess = true;
|
||||
var records = new List<TransactionRecord>();
|
||||
foreach (var (card, reason, amount, balance, type, occurredAt) in parsed)
|
||||
{
|
||||
logger.LogInformation("刷新交易记录: 卡号 {Card}, 交易原因 {Reason}, 金额 {Amount}, 余额 {Balance}, 类型 {Type}", card, reason, amount, balance, type);
|
||||
|
||||
var success = await SaveTransactionRecordAsync(
|
||||
var record = await SaveTransactionRecordAsync(
|
||||
card,
|
||||
reason,
|
||||
amount,
|
||||
@@ -155,12 +163,17 @@ public class EmailHandleService(
|
||||
emailMessage.Id
|
||||
);
|
||||
|
||||
if (!success)
|
||||
if (record == null)
|
||||
{
|
||||
allSuccess = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
records.Add(record);
|
||||
}
|
||||
|
||||
_ = await AnalyzeClassifyAsync(records.ToArray());
|
||||
|
||||
return allSuccess;
|
||||
}
|
||||
|
||||
@@ -221,7 +234,7 @@ public class EmailHandleService(
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<bool> SaveTransactionRecordAsync(
|
||||
private async Task<TransactionRecord?> SaveTransactionRecordAsync(
|
||||
string card,
|
||||
string reason,
|
||||
decimal amount,
|
||||
@@ -251,9 +264,10 @@ public class EmailHandleService(
|
||||
else
|
||||
{
|
||||
logger.LogWarning("交易记录更新失败,卡号 {Card}, 金额 {Amount}", card, amount);
|
||||
return null;
|
||||
}
|
||||
|
||||
return updated;
|
||||
return existing;
|
||||
}
|
||||
|
||||
var trx = new TransactionRecord
|
||||
@@ -276,9 +290,10 @@ public class EmailHandleService(
|
||||
else
|
||||
{
|
||||
logger.LogWarning("交易记录落库失败,卡号 {Card}, 金额 {Amount}", card, amount);
|
||||
return null;
|
||||
}
|
||||
|
||||
return inserted;
|
||||
return trx;
|
||||
}
|
||||
|
||||
private async Task<(string card, string reason, decimal amount, decimal balance, TransactionType type, DateTime? occurredAt)[]?> ParseEmailBodyAsync(string from, string subject, string body)
|
||||
@@ -293,4 +308,59 @@ public class EmailHandleService(
|
||||
|
||||
return await service.ParseAsync(body);
|
||||
}
|
||||
|
||||
private async Task<TransactionRecord[]> AnalyzeClassifyAsync(TransactionRecord[] records)
|
||||
{
|
||||
var result = new List<TransactionRecord>();
|
||||
await smartHandleService.SmartClassifyAsync(records.Select(r => r.Id).ToArray(), chunk =>
|
||||
{
|
||||
// 处理分类结果
|
||||
var (type, data) = chunk;
|
||||
|
||||
if (type != "data")
|
||||
{
|
||||
logger.LogWarning("未知的分类结果类型: {Type}, {Data}. 跳过分类", type, data);
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var item = JsonSerializer.Deserialize<JsonObject>(data);
|
||||
|
||||
var recordId = item?["id"]?.GetValue<long>();
|
||||
var classify = item?["Classify"]?.GetValue<string>();
|
||||
var recordType = item?["Type"]?.GetValue<int>();
|
||||
|
||||
if (recordId == null || string.IsNullOrEmpty(classify) || recordType == null)
|
||||
{
|
||||
logger.LogWarning("AI分类结果数据不完整,跳过分类: {Data}", data);
|
||||
return;
|
||||
}
|
||||
|
||||
if (recordType < (int)TransactionType.Expense || recordType > (int)TransactionType.None)
|
||||
{
|
||||
logger.LogWarning("AI分类结果交易类型无效,跳过分类: {Data}", data);
|
||||
return;
|
||||
}
|
||||
|
||||
var record = records.FirstOrDefault(r => r.Id == recordId);
|
||||
if (record == null)
|
||||
{
|
||||
logger.LogWarning("未找到对应的交易记录(AI返回内容有误),跳过分类,ID: {Id}", recordId);
|
||||
return;
|
||||
}
|
||||
|
||||
record.Classify = classify;
|
||||
record.Type = (TransactionType)recordType;
|
||||
|
||||
result.Add(record);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogWarning(ex, "解析AI分类结果失败,跳过分类: {Data}", data);
|
||||
}
|
||||
});
|
||||
|
||||
return result.ToArray();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user