first commot
All checks were successful
Docker Build & Deploy / Build Docker Image (push) Successful in 8s
Docker Build & Deploy / Deploy to Production (push) Successful in 7s

This commit is contained in:
孙诚
2025-12-25 11:20:56 +08:00
commit 4526cc6396
104 changed files with 11070 additions and 0 deletions

View File

@@ -0,0 +1,161 @@
namespace WebApi.Controllers.EmailMessage;
[ApiController]
[Route("api/[controller]/[action]")]
public class EmailMessageController(
IEmailMessageRepository emailRepository,
ITransactionRecordRepository transactionRepository,
ILogger<EmailMessageController> logger,
IEmailHandleService emailHandleService,
IEmailBackgroundService emailBackgroundService
) : ControllerBase
{
/// <summary>
/// 获取邮件列表(分页)
/// </summary>
[HttpGet]
public async Task<PagedResponse<EmailMessageDto>> GetListAsync(
[FromQuery] DateTime? lastReceivedDate = null,
[FromQuery] long? lastId = null
)
{
try
{
var (list, lastTime, lastIdResult) = await emailRepository.GetPagedListAsync(lastReceivedDate, lastId);
var total = await emailRepository.GetTotalCountAsync();
// 为每个邮件获取账单数量
var emailDtos = new List<EmailMessageDto>();
foreach (var email in list)
{
var transactionCount = await transactionRepository.GetCountByEmailIdAsync(email.Id);
emailDtos.Add(EmailMessageDto.FromEntity(email, transactionCount));
}
return new PagedResponse<EmailMessageDto>
{
Success = true,
Data = emailDtos.ToArray(),
Total = (int)total,
LastId = lastIdResult,
LastTime = lastTime
};
}
catch (Exception ex)
{
logger.LogError(ex, "获取邮件列表失败,时间: {LastTime}, ID: {LastId}", lastReceivedDate, lastId);
return PagedResponse<EmailMessageDto>.Fail($"获取邮件列表失败: {ex.Message}");
}
}
/// <summary>
/// 根据ID获取邮件详情
/// </summary>
[HttpGet("{id}")]
public async Task<BaseResponse<EmailMessageDto>> GetByIdAsync(long id)
{
try
{
var email = await emailRepository.GetByIdAsync(id);
if (email == null)
{
return BaseResponse<EmailMessageDto>.Fail("邮件不存在");
}
// 获取账单数量
var transactionCount = await transactionRepository.GetCountByEmailIdAsync(id);
var emailDto = EmailMessageDto.FromEntity(email, transactionCount);
return new BaseResponse<EmailMessageDto>
{
Success = true,
Data = emailDto
};
}
catch (Exception ex)
{
logger.LogError(ex, "获取邮件详情失败邮件ID: {EmailId}", id);
return BaseResponse<EmailMessageDto>.Fail($"获取邮件详情失败: {ex.Message}");
}
}
public async Task<BaseResponse> DeleteByIdAsync(long id)
{
try
{
var success = await emailRepository.DeleteAsync(id);
if (success)
{
return new BaseResponse
{
Success = true
};
}
else
{
return BaseResponse.Fail("删除邮件失败,邮件不存在");
}
}
catch (Exception ex)
{
logger.LogError(ex, "删除邮件失败邮件ID: {EmailId}", id);
return BaseResponse.Fail($"删除邮件失败: {ex.Message}");
}
}
/// <summary>
/// 重新分析邮件并刷新交易记录
/// </summary>
[HttpPost]
public async Task<BaseResponse> RefreshTransactionRecordsAsync([FromQuery] long id)
{
try
{
var email = await emailRepository.GetByIdAsync(id);
if (email == null)
{
return BaseResponse.Fail("邮件不存在");
}
var success = await emailHandleService.RefreshTransactionRecordsAsync(id);
if (success)
{
return new BaseResponse
{
Success = true
};
}
else
{
return BaseResponse.Fail("重新分析失败");
}
}
catch (Exception ex)
{
logger.LogError(ex, "重新分析邮件失败邮件ID: {EmailId}", id);
return BaseResponse.Fail($"重新分析失败: {ex.Message}");
}
}
/// <summary>
/// 立即同步邮件
/// </summary>
[HttpPost]
public async Task<BaseResponse> SyncEmailsAsync()
{
try
{
await emailBackgroundService.SyncEmailsAsync();
return new BaseResponse
{
Success = true,
Message = "同步成功"
};
}
catch (Exception ex)
{
logger.LogError(ex, "同步邮件失败");
return BaseResponse.Fail($"同步邮件失败: {ex.Message}");
}
}
}