namespace Application;
///
/// 导入类型
///
public enum ImportType
{
///
/// 支付宝
///
Alipay,
///
/// 微信
///
WeChat
}
///
/// 导入应用服务接口
///
public interface IImportApplication
{
///
/// 导入支付宝账单
///
Task ImportAlipayAsync(ImportRequest request);
///
/// 导入微信账单
///
Task ImportWeChatAsync(ImportRequest request);
}
///
/// 导入应用服务实现
///
public class ImportApplication(
IImportService importService,
ILogger logger
) : IImportApplication
{
private static readonly string[] AllowedExtensions = { ".csv", ".xlsx", ".xls" };
private const long MaxFileSize = 10 * 1024 * 1024; // 10MB
public async Task 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 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 };
}
///
/// 验证导入请求
///
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");
}
}
}