提交 cbf3faef authored 作者: douxy's avatar douxy

修改常规陈列导出导入失败场景的返回值编码

上级 37cfca9b
......@@ -38,11 +38,11 @@ public class ExportExcelServiceImpl implements IExportExcelService {
log.info("导出前缀为:{}的excel", fileNamePrefix);
byte[] excelBytesArray = strategy.generateExcel(salesApRequest);
ExcelUtils.exportExcelBytesToResponse(excelBytesArray, response, fileNamePrefix);
log.info("常规陈列数据导出成功!");
log.info("数据导出成功!");
return R.ok(R.SUCCESS, "数据导出成功") ;
} catch (Exception e) {
log.error("常规陈列数据导出异常!具体错误原因:", e);
return R.fail(R.FAIL,e.getMessage());
log.error("数据导出异常!具体错误原因:", e);
return R.fail(910,e.getMessage());
}
}
......
......@@ -45,16 +45,17 @@ public class ImportExcelServiceImpl implements IImportExcelService {
public R importApExcel(ImportApExcelRequest request) {
// 参数校验
if (request == null || request.getImportApFilePath() == null || request.getImportApFilePath().trim().isEmpty()){
return R.fail("导入文件路径不能为空!");
return R.fail(910,"文件上传失败,请重新上传!");
}
if (request.getImportApType() == null || request.getImportApType().trim().isEmpty()){
return R.fail("导入类型不能为空!");
return R.fail(910,"文件上传失败,请重新上传!");
}
// 获取策略
IImportApExcelStrategy strategy = apImportExcelStrategyFactory.getStrategy(request.getImportApType());
if (strategy == null){
return R.fail("未找到对应的导入策略!");
log.error("未找到对应的导入策略!");
return R.fail(910,"文件上传失败,请重新上传!");
}
// 执行导入的数据验证(根据不同的策略独自设计验证)
......@@ -86,16 +87,16 @@ public class ImportExcelServiceImpl implements IImportExcelService {
@Override
public R updateApEntity(ImportApExcelRequest request) {
if (request.getImportApType() == null || request.getImportApType().trim().isEmpty()) {
return R.fail("导入类型不能为空!");
return R.fail(910,"数据更新失败,请重新导入!");
}
if (request.getUuid() == null || request.getUuid().trim().isEmpty()) {
return R.fail("导入数据标识不能为空!");
return R.fail(910,"数据更新失败,请重新导入!");
}
// 获取策略
IImportApExcelStrategy strategy = apImportExcelStrategyFactory.getStrategy(request.getImportApType());
if (strategy == null) {
return R.fail("未找到对应的导入策略!");
return R.fail(910,"数据更新失败,请重新导入!");
}
// 从redis获取数据
......@@ -106,7 +107,7 @@ public class ImportExcelServiceImpl implements IImportExcelService {
// 1校验Redis数据是否存在
if (redisDataJson == null || redisDataJson.trim().isEmpty()) {
log.error("Redis中无有效数据,key={}", redisKey);
return R.fail("导入数据已过期或不存在,请重新导入!");
return R.fail(910,"数据更新失败,请重新导入!");
}
// 解析Redis数据为JSONObject,仅提取table数组(核心修改点,适配JDK8)
......@@ -126,7 +127,7 @@ public class ImportExcelServiceImpl implements IImportExcelService {
}
} catch (Exception e) {
log.error("解析Redis中的table数组失败,redisDataJson={}", redisDataJson, e);
return R.fail("数据解析失败,请重新导入!");
return R.fail(910,"数据更新失败,请重新导入!");
}
// 校验table数组非空
......@@ -146,7 +147,7 @@ public class ImportExcelServiceImpl implements IImportExcelService {
log.info("批量更新结果:{}", updateResult);
if ("更新失败".equals(updateResult)) {
return R.fail(updateResult);
return R.fail(910,updateResult);
}
return R.ok(updateResult);
......
......@@ -53,8 +53,8 @@ public class NormalDisplayImportStrategyImpl implements IImportApExcelStrategy<S
Map<String, List<SalesApDisplayImportExcelDto>> errorMap) {
// 基础校验
if (inputStream == null || StringUtils.isBlank(filePathUrl)) {
SalesApDisplayImportExcelDto errorDto = buildSimpleErrorDto("文件流/URL为空");
errorMap.put("参数异常", Collections.singletonList(errorDto));
SalesApDisplayImportExcelDto errorDto = buildSimpleErrorDto("文件上传失败");
errorMap.put("导如失败", Collections.singletonList(errorDto));
// 修改:返回含错误DTO的列表
return Collections.singletonList(errorDto);
}
......@@ -342,14 +342,14 @@ public class NormalDisplayImportStrategyImpl implements IImportApExcelStrategy<S
// 获取文件流
inputStream = ExcelUtils.getOssFileInputStream(filePathUrl);
if (inputStream == null) {
SalesApDisplayImportExcelDto errorDto = buildSimpleErrorDto("文件流获取失败:OSS文件不存在或权限不足");
SalesApDisplayImportExcelDto errorDto = buildSimpleErrorDto("文件上传失败");
List<SalesApDisplayImportExcelDto> errorList = Collections.singletonList(errorDto);
resultMap.put("uuid", UUID.randomUUID().toString());
resultMap.put("table", errorList);
resultMap.put("successCount", 0);
resultMap.put("failCount", errorList.size());
resultMap.put("errorMsg", "文件流获取失败:OSS文件不存在或权限不足");
resultMap.put("errorMsg", "文件上传失败");
return resultMap;
}
......@@ -373,7 +373,7 @@ public class NormalDisplayImportStrategyImpl implements IImportApExcelStrategy<S
} catch (Exception e) {
log.error("常规陈列导入失败,filePathUrl={}", filePathUrl, e);
// 修改:异常时构建错误DTO,存入table
SalesApDisplayImportExcelDto errorDto = buildSimpleErrorDto("导入失败:" + e.getMessage());
SalesApDisplayImportExcelDto errorDto = buildSimpleErrorDto(e.getMessage());
List<SalesApDisplayImportExcelDto> errorList = Collections.singletonList(errorDto);
resultMap.put("uuid", UUID.randomUUID().toString());
......
......@@ -60,46 +60,7 @@ public class ExcelUtils {
@Autowired
private OssConfigProperties ossConfig;
// public static String extractFileNameFromUrl(String filePathUrl) {
// if (StringUtils.isBlank(filePathUrl)) {
// return "未知文件";
// }
// try {
// // 先尝试URL解码,处理包含特殊字符或中文的文件名
// String decodedUrl = URLDecoder.decode(filePathUrl, StandardCharsets.UTF_8.name());
//
// // 方案1:标准文件路径(file:///C:/Users/xxx/Desktop/data.xlsx)
// if (decodedUrl.startsWith("file:")) {
// return new File(decodedUrl.substring(6)).getName();
// }
//
// // 方案2:普通绝对/相对路径(如"C:\\Users\\xxx\\Desktop\\data.xlsx"或"./data.xlsx")
// if (decodedUrl.contains(".") || decodedUrl.contains("/")) {
// return new File(decodedUrl).getName();
// }
//
// // 方案3:URL路径(如"http://example.com/path/data.xlsx?version=1")
// if (decodedUrl.contains("://")) {
// String pathPart = decodedUrl.substring(decodedUrl.indexOf("://") + 3);
// if (pathPart.contains("/")) {
// pathPart = pathPart.substring(pathPart.indexOf("/") + 1);
// }
// // 移除查询参数(?之后的部分)
// if (pathPart.contains("?")) {
// pathPart = pathPart.substring(0, pathPart.indexOf("?"));
// }
// return new File(pathPart).getName();
// }
//
// // 默认兜底方案:直接当作文件名处理
// return decodedUrl;
// } catch (Exception e) {
// log.warn("解析文件名异常,使用原始名称:{},错误:{}", filePathUrl, e.getMessage());
// return filePathUrl;
// }
//
//
// }
@PostConstruct
public void init() {
......@@ -371,7 +332,7 @@ public static <T> List<T> readApExcelWithColumnConfig(InputStream inputStream,
}
int lastRowNum = sheet.getLastRowNum();
if (lastRowNum <= HEADER_ROW) {
errorList.add("获取数据行数小于等于表头行数,文件内无内容,导入失败,请检查文件!");
errorList.add("获取数据行数为0,文件内无内容,导入失败,请检查文件!");
return dataList;
}
......@@ -390,7 +351,7 @@ public static <T> List<T> readApExcelWithColumnConfig(InputStream inputStream,
}
} catch (IOException e) {
log.error("导入Excel文件失败:{}", e.getMessage(), e);
errorList.add("文件读取失败:" + e.getMessage());
errorList.add(e.getMessage());
} finally {
// 关闭流
try {
......@@ -420,7 +381,7 @@ public static <T> List<T> readApExcelWithColumnConfig(InputStream inputStream,
*/
public static void exportExcelBytesToResponse(byte[] excelBytes, HttpServletResponse response, String fileNamePrefix) throws Exception {
if (excelBytes == null || excelBytes.length == 0) {
throw new Exception("导出Excel失败:生成的Excel字节数组为空");
throw new Exception("导出Excel失败");
}
if (StringUtils.isBlank(fileNamePrefix)) {
fileNamePrefix = "导出文件";
......@@ -447,7 +408,7 @@ public static <T> List<T> readApExcelWithColumnConfig(InputStream inputStream,
outputStream.flush();
} catch (Exception e) {
log.error("写入Excel响应流失败:{}", e.getMessage(), e);
throw new Exception("导出Excel失败:" + e.getMessage());
throw new Exception("导出Excel失败!");
}
}
......@@ -463,13 +424,13 @@ public static <T> List<T> readApExcelWithColumnConfig(InputStream inputStream,
public static byte[] generateExcelBytes(List<ExportColumnConfig> exportColumnConfigList, List<?> dataList, String sheetName) throws IOException {
//数据校验
if (CollectionUtils.isEmpty(exportColumnConfigList)) {
throw new IOException("导出Excel失败:没有配置导出列");
throw new IOException("导出Excel失败");
}
if (CollectionUtils.isEmpty(dataList)) {
throw new IOException("暂无数据,请检查查询条件");
}
if (sheetName == null || sheetName.trim().isEmpty()) {
throw new IOException("导出Excel失败:没有配置sheetName");
throw new IOException("导出Excel失败");
}
// 根据sheetName创建工作表
......@@ -507,7 +468,7 @@ public static <T> List<T> readApExcelWithColumnConfig(InputStream inputStream,
} catch (IOException e) {
log.error("生成Excel文件失败:{}", e.getMessage(), e);
throw new IOException("生成Excel文件失败:" + e.getMessage());
throw new IOException("生成Excel文件失败!");
}
}
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论