fix
This commit is contained in:
112
Application/ImportApplication.cs
Normal file
112
Application/ImportApplication.cs
Normal file
@@ -0,0 +1,112 @@
|
||||
namespace Application;
|
||||
|
||||
/// <summary>
|
||||
/// 导入类型
|
||||
/// </summary>
|
||||
public enum ImportType
|
||||
{
|
||||
/// <summary>
|
||||
/// 支付宝
|
||||
/// </summary>
|
||||
Alipay,
|
||||
|
||||
/// <summary>
|
||||
/// 微信
|
||||
/// </summary>
|
||||
WeChat
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 导入应用服务接口
|
||||
/// </summary>
|
||||
public interface IImportApplication
|
||||
{
|
||||
/// <summary>
|
||||
/// 导入支付宝账单
|
||||
/// </summary>
|
||||
Task<ImportResponse> ImportAlipayAsync(ImportRequest request);
|
||||
|
||||
/// <summary>
|
||||
/// 导入微信账单
|
||||
/// </summary>
|
||||
Task<ImportResponse> ImportWeChatAsync(ImportRequest request);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 导入应用服务实现
|
||||
/// </summary>
|
||||
public class ImportApplication(
|
||||
IImportService importService,
|
||||
ILogger<ImportApplication> logger
|
||||
) : IImportApplication
|
||||
{
|
||||
private static readonly string[] AllowedExtensions = { ".csv", ".xlsx", ".xls" };
|
||||
private const long MaxFileSize = 10 * 1024 * 1024; // 10MB
|
||||
|
||||
|
||||
public async Task<ImportResponse> ImportAlipayAsync(ImportRequest request)
|
||||
{
|
||||
ValidateRequest(request);
|
||||
|
||||
var (ok, message) = await importService.ImportAlipayAsync(
|
||||
(MemoryStream)request.FileStream,
|
||||
request.FileExtension
|
||||
);
|
||||
|
||||
if (!ok)
|
||||
{
|
||||
throw new BusinessException(message);
|
||||
}
|
||||
|
||||
logger.LogInformation("支付宝账单导入成功: {Message}", message);
|
||||
return new ImportResponse { Message = message };
|
||||
}
|
||||
|
||||
public async Task<ImportResponse> ImportWeChatAsync(ImportRequest request)
|
||||
{
|
||||
ValidateRequest(request);
|
||||
|
||||
var (ok, message) = await importService.ImportWeChatAsync(
|
||||
(MemoryStream)request.FileStream,
|
||||
request.FileExtension
|
||||
);
|
||||
|
||||
if (!ok)
|
||||
{
|
||||
throw new BusinessException(message);
|
||||
}
|
||||
|
||||
logger.LogInformation("微信账单导入成功: {Message}", message);
|
||||
return new ImportResponse { Message = message };
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 验证导入请求
|
||||
/// </summary>
|
||||
private static void ValidateRequest(ImportRequest request)
|
||||
{
|
||||
// 验证文件流
|
||||
if (request.FileStream == null || request.FileStream.Length == 0)
|
||||
{
|
||||
throw new ValidationException("请选择要上传的文件");
|
||||
}
|
||||
|
||||
// 验证文件扩展名
|
||||
if (string.IsNullOrWhiteSpace(request.FileExtension))
|
||||
{
|
||||
throw new ValidationException("文件扩展名不能为空");
|
||||
}
|
||||
|
||||
var extension = request.FileExtension.ToLowerInvariant();
|
||||
if (!AllowedExtensions.Contains(extension))
|
||||
{
|
||||
throw new ValidationException("只支持 CSV 或 Excel 文件格式");
|
||||
}
|
||||
|
||||
// 验证文件大小
|
||||
if (request.FileSize > MaxFileSize)
|
||||
{
|
||||
throw new ValidationException("文件大小不能超过 10MB");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user