This commit is contained in:
SunCheng
2026-02-10 17:49:19 +08:00
parent 3e18283e52
commit d052ae5197
104 changed files with 10369 additions and 3000 deletions

View File

@@ -1,4 +1,7 @@
namespace WebApi.Controllers;
using Application;
using Application.Dto;
namespace WebApi.Controllers;
/// <summary>
/// 账单导入控制器
@@ -6,8 +9,7 @@
[ApiController]
[Route("api/[controller]/[action]")]
public class BillImportController(
ILogger<BillImportController> logger,
IImportService importService
IImportApplication importApplication
) : ControllerBase
{
/// <summary>
@@ -22,61 +24,34 @@ public class BillImportController(
[FromForm] string type
)
{
try
// 将IFormFile转换为ImportRequest
var stream = new MemoryStream();
await file.CopyToAsync(stream);
stream.Position = 0;
var request = new ImportRequest
{
// 验证参数
if (file.Length == 0)
{
return "请选择要上传的文件".Fail();
}
FileStream = stream,
FileExtension = Path.GetExtension(file.FileName).ToLowerInvariant(),
FileName = file.FileName,
FileSize = file.Length
};
if (string.IsNullOrWhiteSpace(type) || (type != "Alipay" && type != "WeChat"))
{
return "账单类型参数错误,必须是 Alipay 或 WeChat".Fail();
}
ImportResponse result;
// 验证文件类型
var allowedExtensions = new[] { ".csv", ".xlsx", ".xls" };
var fileExtension = Path.GetExtension(file.FileName).ToLowerInvariant();
if (!allowedExtensions.Contains(fileExtension))
{
return "只支持 CSV 或 Excel 文件格式".Fail();
}
// 验证文件大小10MB限制
const long maxFileSize = 10 * 1024 * 1024;
if (file.Length > maxFileSize)
{
return "文件大小不能超过 10MB".Fail();
}
// 保存文件
var ok = false;
var message = string.Empty;
await using (var stream = new MemoryStream())
{
await file.CopyToAsync(stream);
if (type == "Alipay")
{
(ok, message) = await importService.ImportAlipayAsync(stream, fileExtension);
}
else if (type == "WeChat")
{
(ok, message) = await importService.ImportWeChatAsync(stream, fileExtension);
}
}
if (!ok)
{
return message.Fail();
}
return message.Ok();
}
catch (Exception ex)
if (type == "Alipay")
{
logger.LogError(ex, "文件上传失败,类型: {Type}", type);
return $"文件上传失败: {ex.Message}".Fail();
result = await importApplication.ImportAlipayAsync(request);
}
else if (type == "WeChat")
{
result = await importApplication.ImportWeChatAsync(request);
}
else
{
return "账单类型参数错误,必须是 Alipay 或 WeChat".Fail();
}
return result.Message.Ok();
}
}