实现消息记录功能,包括增删改查和标记已读,优化消息列表展示和未读消息计数
This commit is contained in:
@@ -20,7 +20,8 @@ public class EmailHandleService(
|
||||
ILogger<EmailHandleService> logger,
|
||||
IEmailMessageRepository emailRepo,
|
||||
ITransactionRecordRepository trxRepo,
|
||||
IEnumerable<IEmailParseServices> emailParsers
|
||||
IEnumerable<IEmailParseServices> emailParsers,
|
||||
IMessageRecordService messageRecordService
|
||||
) : IEmailHandleService
|
||||
{
|
||||
public async Task<bool> HandleEmailAsync(
|
||||
@@ -60,10 +61,18 @@ public class EmailHandleService(
|
||||
);
|
||||
if (parsed == null || parsed.Length == 0)
|
||||
{
|
||||
await messageRecordService.AddAsync(
|
||||
"邮件解析失败",
|
||||
$"来自 {from} 发送给 {to} 的邮件(主题:{subject})未能成功解析内容,可能格式已变更或不受支持。"
|
||||
);
|
||||
logger.LogWarning("未能成功解析邮件内容,跳过账单处理");
|
||||
return true;
|
||||
}
|
||||
|
||||
await messageRecordService.AddAsync(
|
||||
"邮件解析成功",
|
||||
$"来自 {from} 发送给 {to} 的邮件(主题:{subject})已成功解析出 {parsed.Length} 条交易记录。"
|
||||
);
|
||||
logger.LogInformation("成功解析邮件,共 {Count} 条交易记录", parsed.Length);
|
||||
|
||||
bool allSuccess = true;
|
||||
|
||||
68
Service/MessageRecordService.cs
Normal file
68
Service/MessageRecordService.cs
Normal file
@@ -0,0 +1,68 @@
|
||||
namespace Service;
|
||||
|
||||
public interface IMessageRecordService
|
||||
{
|
||||
Task<(IEnumerable<MessageRecord> List, long Total)> GetPagedListAsync(int pageIndex, int pageSize);
|
||||
Task<MessageRecord?> GetByIdAsync(long id);
|
||||
Task<bool> AddAsync(MessageRecord message);
|
||||
Task<bool> AddAsync(string title, string content);
|
||||
Task<bool> MarkAsReadAsync(long id);
|
||||
Task<bool> MarkAllAsReadAsync();
|
||||
Task<bool> DeleteAsync(long id);
|
||||
Task<long> GetUnreadCountAsync();
|
||||
}
|
||||
|
||||
public class MessageRecordService(IMessageRecordRepository messageRepo) : IMessageRecordService
|
||||
{
|
||||
public async Task<(IEnumerable<MessageRecord> List, long Total)> GetPagedListAsync(int pageIndex, int pageSize)
|
||||
{
|
||||
return await messageRepo.GetPagedListAsync(pageIndex, pageSize);
|
||||
}
|
||||
|
||||
public async Task<MessageRecord?> GetByIdAsync(long id)
|
||||
{
|
||||
return await messageRepo.GetByIdAsync(id);
|
||||
}
|
||||
|
||||
public async Task<bool> AddAsync(MessageRecord message)
|
||||
{
|
||||
return await messageRepo.AddAsync(message);
|
||||
}
|
||||
|
||||
public async Task<bool> AddAsync(string title, string content)
|
||||
{
|
||||
var message = new MessageRecord
|
||||
{
|
||||
Title = title,
|
||||
Content = content,
|
||||
IsRead = false
|
||||
};
|
||||
return await messageRepo.AddAsync(message);
|
||||
}
|
||||
|
||||
public async Task<bool> MarkAsReadAsync(long id)
|
||||
{
|
||||
var message = await messageRepo.GetByIdAsync(id);
|
||||
if (message == null) return false;
|
||||
|
||||
message.IsRead = true;
|
||||
message.UpdateTime = DateTime.Now;
|
||||
return await messageRepo.UpdateAsync(message);
|
||||
}
|
||||
|
||||
public async Task<bool> MarkAllAsReadAsync()
|
||||
{
|
||||
return await messageRepo.MarkAllAsReadAsync();
|
||||
}
|
||||
|
||||
public async Task<bool> DeleteAsync(long id)
|
||||
{
|
||||
return await messageRepo.DeleteAsync(id);
|
||||
}
|
||||
|
||||
public async Task<long> GetUnreadCountAsync()
|
||||
{
|
||||
var list = await messageRepo.GetAllAsync();
|
||||
return list.Count(x => !x.IsRead);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user