|
@@ -38,6 +38,8 @@ import java.io.File;
|
|
import java.io.FileInputStream;
|
|
import java.io.FileInputStream;
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.io.InputStream;
|
|
|
|
+import java.nio.file.Files;
|
|
|
|
+import java.nio.file.StandardCopyOption;
|
|
import java.sql.Connection;
|
|
import java.sql.Connection;
|
|
import java.sql.DriverManager;
|
|
import java.sql.DriverManager;
|
|
import java.sql.SQLException;
|
|
import java.sql.SQLException;
|
|
@@ -167,7 +169,7 @@ public class KpiComputeImportServiceImpl implements KpiComputeImportService {
|
|
assert fileName != null;
|
|
assert fileName != null;
|
|
File file1 = new File(fileName);
|
|
File file1 = new File(fileName);
|
|
FileUtils.copyInputStreamToFile(file.getInputStream(), file1);
|
|
FileUtils.copyInputStreamToFile(file.getInputStream(), file1);
|
|
- String excelSql = readExcelSql(file1, tableName, id,computeDate);
|
|
|
|
|
|
+ String excelSql = readExcelSql(file1, tableName, id, computeDate);
|
|
SqlRunner sqlRunner = new SqlRunner(getConnection());
|
|
SqlRunner sqlRunner = new SqlRunner(getConnection());
|
|
sqlRunner.insert(excelSql);
|
|
sqlRunner.insert(excelSql);
|
|
|
|
|
|
@@ -248,8 +250,14 @@ public class KpiComputeImportServiceImpl implements KpiComputeImportService {
|
|
String uploadFileUrl = uploadFile(file, fileName);
|
|
String uploadFileUrl = uploadFile(file, fileName);
|
|
List<SheetImportResultVO> sheetResults = new ArrayList<>();
|
|
List<SheetImportResultVO> sheetResults = new ArrayList<>();
|
|
try {
|
|
try {
|
|
- File tempFile = File.createTempFile("excel-", ".tmp");
|
|
|
|
- file.transferTo(tempFile);
|
|
|
|
|
|
+ // 使用系统临时目录显式创建临时文件
|
|
|
|
+ File tempDir = new File(System.getProperty("java.io.tmpdir"));
|
|
|
|
+ File tempFile = new File(tempDir, "excel-" + UUID.randomUUID() + ".tmp");
|
|
|
|
+
|
|
|
|
+ // 确保文件存在并正确关闭流
|
|
|
|
+ try (InputStream inputStream = file.getInputStream()) {
|
|
|
|
+ Files.copy(inputStream, tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
|
|
|
|
+ }
|
|
|
|
|
|
try (Workbook workbook = WorkbookFactory.create(tempFile)) {
|
|
try (Workbook workbook = WorkbookFactory.create(tempFile)) {
|
|
for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
|
|
for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
|
|
@@ -265,7 +273,7 @@ public class KpiComputeImportServiceImpl implements KpiComputeImportService {
|
|
.findFirst();
|
|
.findFirst();
|
|
|
|
|
|
if (!mapping.isPresent()) {
|
|
if (!mapping.isPresent()) {
|
|
- log.warn("未找到sheet页 '{}' 对应的数据表配置,跳过导入", sheetName);
|
|
|
|
|
|
+ log.warn("未找到sheet页 '{%s}' 对应的数据表配置,跳过导入", sheetName);
|
|
resultItem.setSuccess(false);
|
|
resultItem.setSuccess(false);
|
|
resultItem.setErrorMessage("未找到对应的数据库表配置");
|
|
resultItem.setErrorMessage("未找到对应的数据库表配置");
|
|
sheetResults.add(resultItem);
|
|
sheetResults.add(resultItem);
|
|
@@ -296,28 +304,32 @@ public class KpiComputeImportServiceImpl implements KpiComputeImportService {
|
|
resultItem.setImportTime(kpiComputeImport.getCreateTime());
|
|
resultItem.setImportTime(kpiComputeImport.getCreateTime());
|
|
|
|
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
- log.error(String.format("sheet页 '{ %s}' 导入失败: %s", sheetName, e.getMessage()));
|
|
|
|
|
|
+ log.error(String.format("sheet页 '%s' 导入失败: %s", sheetName, e.getMessage()));
|
|
resultItem.setSuccess(false);
|
|
resultItem.setSuccess(false);
|
|
resultItem.setErrorMessage("导入失败: " + e.getMessage());
|
|
resultItem.setErrorMessage("导入失败: " + e.getMessage());
|
|
}
|
|
}
|
|
sheetResults.add(resultItem);
|
|
sheetResults.add(resultItem);
|
|
}
|
|
}
|
|
|
|
+ } finally {
|
|
|
|
+ // 删除临时文件
|
|
|
|
+ if (tempFile.exists()) {
|
|
|
|
+ tempFile.delete();
|
|
|
|
+ }
|
|
}
|
|
}
|
|
- // 删除临时文件
|
|
|
|
- tempFile.delete();
|
|
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
- throw new RuntimeException("导入发生异常" + e.getMessage());
|
|
|
|
|
|
+ throw new RuntimeException("导入发生异常: " + e.getMessage(), e);
|
|
}
|
|
}
|
|
return sheetResults;
|
|
return sheetResults;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+
|
|
@SneakyThrows
|
|
@SneakyThrows
|
|
private void insertExcelDataForSheet(Integer id, String tableName, MultipartFile file, String fileName, String computeDate, Sheet sheet) {
|
|
private void insertExcelDataForSheet(Integer id, String tableName, MultipartFile file, String fileName, String computeDate, Sheet sheet) {
|
|
// 将MultipartFile转换为InputStream
|
|
// 将MultipartFile转换为InputStream
|
|
InputStream inputStream = file.getInputStream();
|
|
InputStream inputStream = file.getInputStream();
|
|
|
|
|
|
// 这里假设有一个方法可以从给定的InputStream和sheet对象中读取SQL语句
|
|
// 这里假设有一个方法可以从给定的InputStream和sheet对象中读取SQL语句
|
|
- List<String> excelSql = readExcelSqlFromSheet(inputStream, sheet, tableName, id,computeDate);
|
|
|
|
|
|
+ List<String> excelSql = readExcelSqlFromSheet(inputStream, sheet, tableName, id, computeDate);
|
|
|
|
|
|
// 执行SQL插入操作
|
|
// 执行SQL插入操作
|
|
SqlRunner sqlRunner = new SqlRunner(getConnection());
|
|
SqlRunner sqlRunner = new SqlRunner(getConnection());
|
|
@@ -329,7 +341,7 @@ public class KpiComputeImportServiceImpl implements KpiComputeImportService {
|
|
String updateSql = "UPDATE `" + tableName + "` SET " +
|
|
String updateSql = "UPDATE `" + tableName + "` SET " +
|
|
"`create_user` = '" + UserContext.getCurrentUser().getId() + "', " +
|
|
"`create_user` = '" + UserContext.getCurrentUser().getId() + "', " +
|
|
"`create_time` = '" + DateUtils.formatDate2String(new Date()) + "' " +
|
|
"`create_time` = '" + DateUtils.formatDate2String(new Date()) + "' " +
|
|
- "WHERE `import_id` = '"+id+"';";
|
|
|
|
|
|
+ "WHERE `import_id` = '" + id + "';";
|
|
sqlRunner.run(updateSql);
|
|
sqlRunner.run(updateSql);
|
|
}
|
|
}
|
|
|
|
|
|
@@ -391,7 +403,7 @@ public class KpiComputeImportServiceImpl implements KpiComputeImportService {
|
|
|
|
|
|
// 添加 import_id 和 hosp_id
|
|
// 添加 import_id 和 hosp_id
|
|
valueSb.append(", ").append(id)
|
|
valueSb.append(", ").append(id)
|
|
- .append(", ").append(UserContext.getCurrentLoginHospId());
|
|
|
|
|
|
+ .append(", ").append(UserContext.getCurrentLoginHospId());
|
|
|
|
|
|
// 如果字段中没有 compute_date,才手动添加
|
|
// 如果字段中没有 compute_date,才手动添加
|
|
if (!hasComputeDateField) {
|
|
if (!hasComputeDateField) {
|
|
@@ -518,7 +530,7 @@ public class KpiComputeImportServiceImpl implements KpiComputeImportService {
|
|
i++;
|
|
i++;
|
|
}
|
|
}
|
|
//组装记录表和hospId
|
|
//组装记录表和hospId
|
|
- setCommonData(id, dataList,computeDate);
|
|
|
|
|
|
+ setCommonData(id, dataList, computeDate);
|
|
sql = insertFromMap(dataList, tableName);
|
|
sql = insertFromMap(dataList, tableName);
|
|
|
|
|
|
}
|
|
}
|
|
@@ -559,7 +571,7 @@ public class KpiComputeImportServiceImpl implements KpiComputeImportService {
|
|
}
|
|
}
|
|
i++;
|
|
i++;
|
|
}
|
|
}
|
|
- setCommonData(id, dataList,computeDate);
|
|
|
|
|
|
+ setCommonData(id, dataList, computeDate);
|
|
sql = insertFromMap(dataList, tableName);
|
|
sql = insertFromMap(dataList, tableName);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -571,7 +583,7 @@ public class KpiComputeImportServiceImpl implements KpiComputeImportService {
|
|
dataList.forEach(stringObjectMap -> {
|
|
dataList.forEach(stringObjectMap -> {
|
|
stringObjectMap.put("hosp_id", UserContext.getCurrentLoginHospId());
|
|
stringObjectMap.put("hosp_id", UserContext.getCurrentLoginHospId());
|
|
//Excel字段里没有传核算年月的,用传入的核算年月
|
|
//Excel字段里没有传核算年月的,用传入的核算年月
|
|
- if(!stringObjectMap.containsKey("compute_date")) {
|
|
|
|
|
|
+ if (!stringObjectMap.containsKey("compute_date")) {
|
|
stringObjectMap.put("compute_date", computeDate);
|
|
stringObjectMap.put("compute_date", computeDate);
|
|
}
|
|
}
|
|
stringObjectMap.put("import_id", id);
|
|
stringObjectMap.put("import_id", id);
|
|
@@ -675,5 +687,5 @@ public class KpiComputeImportServiceImpl implements KpiComputeImportService {
|
|
private Connection getConnection() throws ClassNotFoundException, SQLException {
|
|
private Connection getConnection() throws ClassNotFoundException, SQLException {
|
|
Class.forName(driver);
|
|
Class.forName(driver);
|
|
return DriverManager.getConnection(url, username, password);
|
|
return DriverManager.getConnection(url, username, password);
|
|
- }
|
|
|
|
|
|
+ }
|
|
}
|
|
}
|