|
|
@@ -864,7 +864,6 @@ namespace OASystem.API.Controllers
|
|
|
return Ok(await _ForForeignReceivablesRep.PostReceivablesSingleSave(dto));
|
|
|
}
|
|
|
|
|
|
-
|
|
|
/// <summary>
|
|
|
/// 对外收款账单
|
|
|
/// 审核
|
|
|
@@ -1510,8 +1509,8 @@ namespace OASystem.API.Controllers
|
|
|
}
|
|
|
|
|
|
// 2. 构建源文件夹路径
|
|
|
- var groupDir = @$"{_DelegationInfo.TeamName}_{_DelegationInfo.Id}";
|
|
|
- var sourceFolderPath = AppSettingsHelper.Get("ReceivablesUploadFileBasePath") + @"/" + groupDir;
|
|
|
+ var groupDir = $"{_DelegationInfo.TeamName}_{_DelegationInfo.Id}";
|
|
|
+ var sourceFolderPath = AppSettingsHelper.Get("ReceivablesUploadFileBasePath");
|
|
|
|
|
|
if (!Directory.Exists(sourceFolderPath))
|
|
|
{
|
|
|
@@ -1527,59 +1526,100 @@ namespace OASystem.API.Controllers
|
|
|
|
|
|
for (int i = 0; i < filePaths.Count; i++)
|
|
|
{
|
|
|
- filePaths[i] = $"{sourceFolderPath}/{filePaths[i]}";
|
|
|
+ filePaths[i] = Path.Combine(sourceFolderPath, filePaths[i]);
|
|
|
}
|
|
|
|
|
|
// 4. 生成目标Zip文件路径
|
|
|
- string targetZipPath = sourceFolderPath.Trim();
|
|
|
-
|
|
|
- // 5. 确保目标目录存在
|
|
|
+ var zipFileName = $"{_DelegationInfo.TeamName}_{Guid.NewGuid().ToString().Substring(0, 8)}.zip";
|
|
|
+ string targetZipPath = Path.Combine(
|
|
|
+ AppSettingsHelper.Get("ReceivablesUploadFileBasePath") + @"/" + groupDir,
|
|
|
+ zipFileName
|
|
|
+ );
|
|
|
+
|
|
|
+ // 或者如果已经有特定的zip文件名,确保添加扩展名
|
|
|
+ // string targetZipPath = Path.Combine(sourceFolderPath, "ReceivablesFiles.zip");
|
|
|
+
|
|
|
+ // 5. 确保目标目录存在(应该已经存在)
|
|
|
var targetDirectory = Path.GetDirectoryName(targetZipPath);
|
|
|
if (!Directory.Exists(targetDirectory))
|
|
|
{
|
|
|
Directory.CreateDirectory(targetDirectory);
|
|
|
}
|
|
|
|
|
|
- // 6. 检查目标文件是否已存在(可选:覆盖或返回错误)
|
|
|
+ // 6. 检查并处理现有文件
|
|
|
if (System.IO.File.Exists(targetZipPath))
|
|
|
{
|
|
|
- System.IO.File.Delete(targetZipPath);
|
|
|
+ try
|
|
|
+ {
|
|
|
+ System.IO.File.Delete(targetZipPath);
|
|
|
+ }
|
|
|
+ catch (IOException ex)
|
|
|
+ {
|
|
|
+ // 文件可能被占用,尝试生成新的文件名
|
|
|
+ targetZipPath = Path.Combine(
|
|
|
+ Path.GetDirectoryName(targetZipPath),
|
|
|
+ $"{Path.GetFileNameWithoutExtension(targetZipPath)}_{DateTime.Now:HHmmss}{Path.GetExtension(targetZipPath)}"
|
|
|
+ );
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- // 7. 创建Zip包
|
|
|
- using (var zipFileStream = new FileStream(targetZipPath, FileMode.Create))
|
|
|
+ // 7. 验证源文件是否存在
|
|
|
+ var missingFiles = filePaths.Where(f => !System.IO.File.Exists(f)).ToList();
|
|
|
+ if (missingFiles.Any())
|
|
|
{
|
|
|
- using (var zipArchive = new ZipArchive(zipFileStream, ZipArchiveMode.Create))
|
|
|
+ return Ok(JsonView(false, $"以下文件不存在: {string.Join(", ", missingFiles.Select(Path.GetFileName))}"));
|
|
|
+ }
|
|
|
+
|
|
|
+ // 8. 创建Zip包
|
|
|
+ try
|
|
|
+ {
|
|
|
+ sourceFolderPath = sourceFolderPath + @"\" + groupDir;
|
|
|
+ using (var zipFileStream = new FileStream(targetZipPath, FileMode.Create))
|
|
|
{
|
|
|
- foreach (string filePath in filePaths)
|
|
|
+ using (var zipArchive = new ZipArchive(zipFileStream, ZipArchiveMode.Create))
|
|
|
{
|
|
|
- // 增加空值/空字符串校验,提升代码健壮性
|
|
|
- if (!string.IsNullOrEmpty(filePath) && System.IO.File.Exists(filePath))
|
|
|
+ foreach (string filePath in filePaths)
|
|
|
{
|
|
|
- // 获取文件名
|
|
|
- string fileName = Path.GetFileName(filePath);
|
|
|
- // 将文件添加到ZIP归档中
|
|
|
- zipArchive.CreateEntryFromFile(filePath, fileName);
|
|
|
+ if (!string.IsNullOrEmpty(filePath) && System.IO.File.Exists(filePath))
|
|
|
+ {
|
|
|
+ // 使用相对路径作为zip内的文件结构
|
|
|
+ string relativePath = filePath.Substring(sourceFolderPath.Length).TrimStart('\\', '/');
|
|
|
+ zipArchive.CreateEntryFromFile(filePath, relativePath);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+ catch (UnauthorizedAccessException ex)
|
|
|
+ {
|
|
|
+ // 特定处理权限错误
|
|
|
+ return Ok(JsonView(false, $"没有权限创建文件到目录: {Path.GetDirectoryName(targetZipPath)},错误: {ex.Message}"));
|
|
|
+ }
|
|
|
+ catch (IOException ex)
|
|
|
+ {
|
|
|
+ // 处理文件被占用等情况
|
|
|
+ return Ok(JsonView(false, $"文件操作失败,可能文件被占用: {ex.Message}"));
|
|
|
+ }
|
|
|
|
|
|
- // 8. 验证Zip文件是否创建成功
|
|
|
+ // 9. 验证Zip文件是否创建成功
|
|
|
if (!System.IO.File.Exists(targetZipPath))
|
|
|
{
|
|
|
return Ok(JsonView(false, "Zip文件创建失败!"));
|
|
|
}
|
|
|
|
|
|
- return Ok(JsonView(true, "操作成功!", targetZipPath));
|
|
|
+ // 10. 返回文件信息
|
|
|
+ var url = @$"{AppSettingsHelper.Get("OfficeBaseUrl")}{AppSettingsHelper.Get("ReceivablesUploadFileFtpPath")}/{zipFileName}";
|
|
|
+ return Ok(JsonView(true, "操作成功!", url));
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
- return Ok(JsonView(false, ex.Message));
|
|
|
+ // 记录详细错误日志
|
|
|
+ _logger.LogError(ex, "创建应收款文件Zip包失败");
|
|
|
+ return Ok(JsonView(false, $"系统错误: {ex.Message}"));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- return Ok(JsonView(false, "操作失败!"));
|
|
|
+ return Ok(JsonView(false));
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|