init
parent
dcef8041b2
commit
65af700145
19
pom.xml
19
pom.xml
|
|
@ -46,5 +46,24 @@
|
|||
<artifactId>slf4j-simple</artifactId>
|
||||
<version>1.7.36</version>
|
||||
</dependency>
|
||||
|
||||
<!-- HTTP客户端 -->
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpclient</artifactId>
|
||||
<version>4.5.13</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpmime</artifactId>
|
||||
<version>4.5.13</version>
|
||||
</dependency>
|
||||
|
||||
<!-- JSON解析 -->
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
<version>2.13.3</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
|
@ -0,0 +1,687 @@
|
|||
package org.example;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.apache.commons.collections4.CollectionUtils;
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.entity.ContentType;
|
||||
import org.apache.http.entity.mime.MultipartEntityBuilder;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.net.URLEncoder;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.Timestamp;
|
||||
import java.util.*;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 照片文件导入器 - 先上传后绑定到业务
|
||||
*/
|
||||
public class FileImporter {
|
||||
|
||||
// 配置常量
|
||||
private static final String UPLOAD_API_URL = "http://223.75.53.141:81/shzh/jcsj/fileAssociations/uploadFile/new";
|
||||
private static final String DEFAULT_USER_ID = "231"; // 上传用户ID
|
||||
private static final ObjectMapper objectMapper = new ObjectMapper();
|
||||
private static Connection conn;
|
||||
|
||||
// 数据库查询SQL
|
||||
private static final String QUERY_HDP_POINT_SQL =
|
||||
"SELECT GUID FROM SHZH_JCSJ.IA_SHZH_HDP_POINT " +
|
||||
"WHERE NAME = ? AND WSCD = ? AND TYPE = ? AND STATUS = '1'";
|
||||
|
||||
private static final String QUERY_RBAD_INFO_SQL =
|
||||
"SELECT GUID FROM SHZH_JCSJ.IA_RBAD_INFO " +
|
||||
"WHERE CD = ? AND WSCD = ? AND STATUS = '1'";
|
||||
|
||||
// FILE_ASSOCIATIONS 表相关SQL
|
||||
private static final String CHECK_FILE_ASSOC_SQL =
|
||||
"SELECT COUNT(*) FROM SHZH_JCSJ.FILE_ASSOCIATIONS " +
|
||||
"WHERE FILE_ID = ? AND TABLE_NAME = ? AND BUSINESS_ID = ? AND DEL = '1'";
|
||||
|
||||
private static final String INSERT_FILE_ASSOC_SQL =
|
||||
"INSERT INTO SHZH_JCSJ.FILE_ASSOCIATIONS (" +
|
||||
"ID, FILE_ID, SORT_ON, TM, TABLE_NAME, DEL, BUSINESS_ID, TYPE" +
|
||||
") VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
args = new String[]{"E:\\999"};
|
||||
// 命令行参数处理
|
||||
if (args.length == 0) {
|
||||
System.out.println("请提供照片目录路径作为参数");
|
||||
return;
|
||||
}
|
||||
|
||||
String baseDir = args[0];
|
||||
String userId = DEFAULT_USER_ID;
|
||||
|
||||
File baseDirectory = new File(baseDir);
|
||||
|
||||
if (!baseDirectory.exists() || !baseDirectory.isDirectory()) {
|
||||
System.out.println("指定的目录不存在或不是有效目录: " + baseDir);
|
||||
return;
|
||||
}
|
||||
|
||||
System.out.println("开始处理目录: " + baseDir);
|
||||
System.out.println("上传用户ID: " + userId);
|
||||
|
||||
try {
|
||||
// 初始化数据库连接
|
||||
conn = DatabaseUtil.getConnection();
|
||||
System.out.println("数据库连接成功");
|
||||
|
||||
// 处理目录
|
||||
processAllCounties(baseDirectory, userId);
|
||||
|
||||
System.out.println("\n=== 所有目录处理完成 ===");
|
||||
} catch (Exception e) {
|
||||
System.err.println("处理过程中发生错误: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
closeResources();
|
||||
}
|
||||
}
|
||||
|
||||
private static void closeResources() {
|
||||
try {
|
||||
if (conn != null && !conn.isClosed()) {
|
||||
conn.close();
|
||||
System.out.println("数据库连接已关闭");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.err.println("关闭资源时出错: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private static void processAllCounties(File baseDir, String userId) {
|
||||
File[] countyDirs = baseDir.listFiles(File::isDirectory);
|
||||
if (countyDirs == null || countyDirs.length == 0) {
|
||||
System.out.println("在基础目录下未找到任何县市文件夹");
|
||||
// 尝试直接处理当前目录(可能是直接包含小流域的目录)
|
||||
try {
|
||||
processDirectoryDirectly(baseDir, userId);
|
||||
} catch (Exception e) {
|
||||
System.err.println("直接处理目录失败: " + e.getMessage());
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
int totalProcessed = 0;
|
||||
int totalSuccess = 0;
|
||||
int totalPhotos = 0;
|
||||
int totalBindings = 0;
|
||||
|
||||
System.out.println("找到 " + countyDirs.length + " 个县市文件夹");
|
||||
|
||||
for (File countyDir : countyDirs) {
|
||||
String countyName = countyDir.getName();
|
||||
System.out.println("\n=== 处理县市: " + countyName + " ===");
|
||||
|
||||
try {
|
||||
ProcessResult result = processCountyData(countyDir, countyName, userId);
|
||||
totalProcessed += result.processed;
|
||||
totalPhotos += result.photosUploaded;
|
||||
totalBindings += result.bindingsCreated;
|
||||
totalSuccess++;
|
||||
|
||||
System.out.println("✓ 成功处理 " + countyName +
|
||||
",导入 " + result.processed + " 个目录," +
|
||||
result.photosUploaded + " 张照片," +
|
||||
result.bindingsCreated + " 条绑定关系");
|
||||
} catch (Exception e) {
|
||||
System.err.println("✗ 处理县市 " + countyName + " 时出错: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("\n=== 处理完成 ===");
|
||||
System.out.println("成功处理县市: " + totalSuccess + " 个");
|
||||
System.out.println("处理目录: " + totalProcessed + " 个");
|
||||
System.out.println("上传照片: " + totalPhotos + " 张");
|
||||
System.out.println("创建绑定: " + totalBindings + " 条");
|
||||
}
|
||||
|
||||
private static void processDirectoryDirectly(File dir, String userId) throws Exception {
|
||||
System.out.println("直接处理目录: " + dir.getAbsolutePath());
|
||||
|
||||
// 查找小流域目录
|
||||
File watershedDir = findDirectory(dir, "小流域");
|
||||
if (watershedDir != null) {
|
||||
System.out.println("找到小流域目录: " + watershedDir.getName());
|
||||
|
||||
// 查找水系目录
|
||||
File[] sxDirs = findSxDirectories(watershedDir);
|
||||
if (sxDirs != null && sxDirs.length > 0) {
|
||||
ProcessResult totalResult = new ProcessResult(0, 0, 0);
|
||||
for (File sxDir : sxDirs) {
|
||||
System.out.println("处理水系: " + sxDir.getName());
|
||||
ProcessResult result = processSxData(sxDir, userId);
|
||||
totalResult.add(result);
|
||||
}
|
||||
System.out.println("水系处理完成,照片: " + totalResult.photosUploaded +
|
||||
",绑定: " + totalResult.bindingsCreated);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static ProcessResult processCountyData(File countyDir, String countyName, String userId) throws Exception {
|
||||
ProcessResult totalResult = new ProcessResult(0, 0, 0);
|
||||
|
||||
// 查找小流域目录
|
||||
File watershedDir = findDirectory(countyDir, "小流域");
|
||||
if (watershedDir == null) {
|
||||
System.out.println("在 " + countyName + " 下未找到小流域目录");
|
||||
return totalResult;
|
||||
}
|
||||
System.out.println("找到小流域目录: " + watershedDir.getName());
|
||||
|
||||
// 查找所有水系目录
|
||||
File[] sxDirs = findSxDirectories(watershedDir);
|
||||
if (sxDirs == null || sxDirs.length == 0) {
|
||||
System.out.println("在 " + watershedDir.getPath() + " 下未找到水系目录");
|
||||
return totalResult;
|
||||
}
|
||||
|
||||
System.out.println("找到 " + sxDirs.length + " 个水系文件夹:");
|
||||
for (File sxDir : sxDirs) {
|
||||
System.out.println(" - " + sxDir.getName());
|
||||
}
|
||||
|
||||
// 遍历处理每个水系文件夹
|
||||
for (File sxDir : sxDirs) {
|
||||
System.out.println("\n处理水系: " + sxDir.getName());
|
||||
ProcessResult result = processSxData(sxDir, userId);
|
||||
totalResult.add(result);
|
||||
System.out.println("水系 " + sxDir.getName() + " 完成,照片: " +
|
||||
result.photosUploaded + ",绑定: " + result.bindingsCreated);
|
||||
}
|
||||
|
||||
return totalResult;
|
||||
}
|
||||
|
||||
private static File[] findSxDirectories(File watershedDir) {
|
||||
File[] subDirs = watershedDir.listFiles(File::isDirectory);
|
||||
if (subDirs != null) {
|
||||
List<File> sxDirs = new ArrayList<>();
|
||||
for (File subDir : subDirs) {
|
||||
if (subDir.getName().contains("水系") || subDir.getName().matches("^HBW.*")) {
|
||||
sxDirs.add(subDir);
|
||||
}
|
||||
}
|
||||
return sxDirs.toArray(new File[0]);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static ProcessResult processSxData(File sxDir, String userId) throws Exception {
|
||||
ProcessResult result = new ProcessResult(1, 0, 0); // 至少处理了一个目录
|
||||
|
||||
try {
|
||||
// 查找电子数据目录
|
||||
File eleDataDir = findDirectory(sxDir, "电子数据");
|
||||
if (eleDataDir == null) {
|
||||
System.out.println("在 " + sxDir.getPath() + " 下未找到电子数据目录");
|
||||
return result;
|
||||
}
|
||||
System.out.println("找到电子数据目录: " + eleDataDir.getPath());
|
||||
|
||||
File photoDir = findDirectory(eleDataDir, "照片");
|
||||
if (photoDir == null) {
|
||||
System.out.println("在 " + sxDir.getPath() + " 下未找到照片目录");
|
||||
return result;
|
||||
}
|
||||
System.out.println("找到照片目录: " + photoDir.getPath());
|
||||
|
||||
// 提取流域编码
|
||||
String watershedCode = extractWatershedCode(sxDir.getName());
|
||||
System.out.println("流域编码: " + watershedCode);
|
||||
|
||||
File[] subDirs = photoDir.listFiles(File::isDirectory);
|
||||
if (subDirs != null) {
|
||||
for (File subDir : subDirs) {
|
||||
String subDirName = subDir.getName();
|
||||
try {
|
||||
if (subDirName.contains("弱势群体")) {
|
||||
ProcessResult dirResult = handleData(subDir, "IA_SHZH_HDP_POINT", watershedCode, "1", userId);
|
||||
result.add(dirResult);
|
||||
} else if (subDirName.contains("房屋结构")) {
|
||||
ProcessResult dirResult = handleData(subDir, "IA_SHZH_HDP_POINT", watershedCode, "2", userId);
|
||||
result.add(dirResult);
|
||||
} else if (subDirName.contains("危险地建房")) {
|
||||
ProcessResult dirResult = handleData(subDir, "IA_SHZH_HDP_POINT", watershedCode, "3", userId);
|
||||
result.add(dirResult);
|
||||
} else if (subDirName.contains("跨沟道路和桥涵")) {
|
||||
ProcessResult dirResult = handleRbadData(subDir, "IA_RBAD_INFO", watershedCode, userId);
|
||||
result.add(dirResult);
|
||||
} else {
|
||||
System.out.println("跳过未知类型的目录: " + subDirName);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.out.println("处理子目录 " + subDirName + " 失败: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.out.println("处理水系 " + sxDir.getName() + " 时发生严重错误: " + e.getMessage());
|
||||
throw e;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据文件夹类型收集照片文件
|
||||
*/
|
||||
private static List<File> collectImageFiles(File dir) {
|
||||
List<File> imgList = new ArrayList<>();
|
||||
|
||||
// 处理危险地建房(特殊:有下一层文件夹)
|
||||
if (dir.getName().contains("危险地建房")) {
|
||||
File[] subDirs = dir.listFiles(File::isDirectory);
|
||||
if (subDirs != null) {
|
||||
for (File subDir : subDirs) {
|
||||
File[] innerImgs = subDir.listFiles((d, name) ->
|
||||
name.toLowerCase().endsWith(".jpg") ||
|
||||
name.toLowerCase().endsWith(".jpeg") ||
|
||||
name.toLowerCase().endsWith(".png"));
|
||||
if (innerImgs != null) {
|
||||
imgList.addAll(Arrays.asList(innerImgs));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// 处理普通照片文件夹(房屋结构、弱势群体)
|
||||
else {
|
||||
File[] imgs = dir.listFiles((d, name) ->
|
||||
name.toLowerCase().endsWith(".jpg") ||
|
||||
name.toLowerCase().endsWith(".jpeg") ||
|
||||
name.toLowerCase().endsWith(".png"));
|
||||
if (imgs != null) {
|
||||
imgList.addAll(Arrays.asList(imgs));
|
||||
}
|
||||
}
|
||||
|
||||
return imgList;
|
||||
}
|
||||
|
||||
private static ProcessResult handleData(File subDir, String groupId, String watershedCode, String type, String userId) throws Exception {
|
||||
String subDirName = subDir.getName();
|
||||
System.out.println("处理文件夹: " + subDirName + " (类型: " + type + ")");
|
||||
|
||||
ProcessResult result = new ProcessResult(1, 0, 0);
|
||||
|
||||
// 收集照片
|
||||
List<File> imgList = collectImageFiles(subDir);
|
||||
|
||||
if (CollectionUtils.isEmpty(imgList)) {
|
||||
System.out.println("文件夹 " + subDirName + " 中没有找到照片");
|
||||
return result;
|
||||
}
|
||||
|
||||
System.out.println("在 " + subDirName + " 中找到 " + imgList.size() + " 张照片");
|
||||
|
||||
// 按照片名称分组处理
|
||||
Map<String, List<File>> filesByName = imgList.stream()
|
||||
.collect(Collectors.groupingBy(file -> {
|
||||
String name = file.getName();
|
||||
int dotIndex = name.lastIndexOf('.');
|
||||
return dotIndex > 0 ? name.substring(0, dotIndex) : name;
|
||||
}));
|
||||
|
||||
for (Map.Entry<String, List<File>> entry : filesByName.entrySet()) {
|
||||
String fileName = entry.getKey();
|
||||
List<File> files = entry.getValue();
|
||||
|
||||
// 根据文件名+流域编码查询业务数据
|
||||
List<String> businessIds = queryBusinessIds(fileName, watershedCode, type, groupId);
|
||||
|
||||
if (CollectionUtils.isEmpty(businessIds)) {
|
||||
System.out.println("未找到文件名 " + fileName + " 对应的业务数据 (流域编码: " + watershedCode + ", 类型: " + type + ")");
|
||||
continue;
|
||||
}
|
||||
|
||||
System.out.println("找到 " + businessIds.size() + " 条与文件 " + fileName + " 相关的业务记录");
|
||||
|
||||
// 先上传所有照片,再绑定到每个业务
|
||||
List<String> fileIds = new ArrayList<>();
|
||||
for (File file : files) {
|
||||
try {
|
||||
// 上传照片
|
||||
String fileId = uploadPhoto(file, groupId, userId, null); // 不传businessId
|
||||
if (fileId != null) {
|
||||
fileIds.add(fileId);
|
||||
result.photosUploaded++;
|
||||
System.out.println("✓ 上传成功: " + file.getName() + " -> " + fileId);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.err.println("✗ 上传失败: " + file.getName() + ", 错误: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
// 为每个业务ID绑定所有上传的文件
|
||||
for (String businessId : businessIds) {
|
||||
for (int i = 0; i < fileIds.size(); i++) {
|
||||
String fileId = fileIds.get(i);
|
||||
try {
|
||||
// 绑定文件到业务
|
||||
bindFileToBusiness(fileId, groupId, businessId, null, i + 1);
|
||||
result.bindingsCreated++;
|
||||
System.out.println("✓ 绑定成功: 文件 " + fileId + " -> 业务 " + businessId);
|
||||
} catch (Exception e) {
|
||||
System.err.println("✗ 绑定失败: 文件 " + fileId + " -> 业务 " + businessId + ", 错误: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询业务数据ID
|
||||
*/
|
||||
private static List<String> queryBusinessIds(String fileName, String watershedCode, String type, String groupId) {
|
||||
List<String> businessIds = new ArrayList<>();
|
||||
|
||||
try {
|
||||
PreparedStatement pstmt = null;
|
||||
ResultSet rs = null;
|
||||
|
||||
if ("IA_SHZH_HDP_POINT".equals(groupId)) {
|
||||
pstmt = conn.prepareStatement(QUERY_HDP_POINT_SQL);
|
||||
pstmt.setString(1, fileName);
|
||||
pstmt.setString(2, watershedCode);
|
||||
pstmt.setString(3, type);
|
||||
} else if ("IA_RBAD_INFO".equals(groupId)) {
|
||||
pstmt = conn.prepareStatement(QUERY_RBAD_INFO_SQL);
|
||||
pstmt.setString(1, fileName);
|
||||
pstmt.setString(2, watershedCode);
|
||||
} else {
|
||||
System.out.println("未知的group ID: " + groupId);
|
||||
return businessIds;
|
||||
}
|
||||
|
||||
rs = pstmt.executeQuery();
|
||||
while (rs.next()) {
|
||||
businessIds.add(rs.getString("GUID"));
|
||||
}
|
||||
|
||||
if (pstmt != null) pstmt.close();
|
||||
if (rs != null) rs.close();
|
||||
|
||||
} catch (Exception e) {
|
||||
System.err.println("查询业务数据失败: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return businessIds;
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传照片到HTTP接口(解决中文文件名问题)
|
||||
*/
|
||||
private static String uploadPhoto(File photoFile, String groupId, String userId, String businessId) throws Exception {
|
||||
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
|
||||
HttpPost uploadFile = new HttpPost(UPLOAD_API_URL);
|
||||
|
||||
// 使用自定义的MultipartEntityBuilder,支持中文文件名
|
||||
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
|
||||
|
||||
// 关键:设置字符编码为UTF-8
|
||||
builder.setCharset(java.nio.charset.StandardCharsets.UTF_8);
|
||||
builder.setContentType(ContentType.MULTIPART_FORM_DATA.withCharset(java.nio.charset.StandardCharsets.UTF_8));
|
||||
|
||||
// 获取原始文件名(可能包含中文)
|
||||
String originalFileName = photoFile.getName();
|
||||
|
||||
// 方法1:直接使用原始文件名(推荐)
|
||||
builder.addBinaryBody(
|
||||
"file",
|
||||
new FileInputStream(photoFile),
|
||||
ContentType.APPLICATION_OCTET_STREAM,
|
||||
originalFileName // 使用原始中文文件名
|
||||
);
|
||||
|
||||
// 方法2:对文件名进行URL编码(备选)
|
||||
// String encodedFileName = URLEncoder.encode(originalFileName, "UTF-8");
|
||||
// builder.addBinaryBody(
|
||||
// "file",
|
||||
// new FileInputStream(photoFile),
|
||||
// ContentType.APPLICATION_OCTET_STREAM,
|
||||
// encodedFileName
|
||||
// );
|
||||
|
||||
// 其他参数
|
||||
builder.addTextBody("groupId", groupId, ContentType.TEXT_PLAIN.withCharset("UTF-8"));
|
||||
builder.addTextBody("fileName", originalFileName, ContentType.TEXT_PLAIN.withCharset("UTF-8"));
|
||||
builder.addTextBody("userId", userId, ContentType.TEXT_PLAIN.withCharset("UTF-8"));
|
||||
|
||||
HttpEntity multipart = builder.build();
|
||||
uploadFile.setEntity(multipart);
|
||||
|
||||
// 设置请求头
|
||||
uploadFile.setHeader("Accept-Charset", "UTF-8");
|
||||
|
||||
try (CloseableHttpResponse response = httpClient.execute(uploadFile)) {
|
||||
HttpEntity responseEntity = response.getEntity();
|
||||
String responseString = EntityUtils.toString(responseEntity, "UTF-8");
|
||||
|
||||
System.out.println("上传响应状态: " + response.getStatusLine().getStatusCode());
|
||||
System.out.println("上传文件名: " + originalFileName);
|
||||
|
||||
if (response.getStatusLine().getStatusCode() == 200) {
|
||||
JsonNode root = objectMapper.readTree(responseString);
|
||||
int code = root.path("code").asInt();
|
||||
|
||||
if (code == 200) {
|
||||
JsonNode data = root.path("data");
|
||||
String fileId = data.path("fileId").asText();
|
||||
String fileName = data.path("fileName").asText();
|
||||
|
||||
System.out.println("上传成功,文件ID: " + fileId + ", 文件名: " + fileName);
|
||||
|
||||
if (fileId != null && !fileId.isEmpty()) {
|
||||
return fileId;
|
||||
} else {
|
||||
throw new Exception("响应中没有fileId字段");
|
||||
}
|
||||
} else {
|
||||
String msg = root.path("msg").asText();
|
||||
throw new Exception("接口返回错误: " + msg);
|
||||
}
|
||||
} else {
|
||||
throw new Exception("HTTP请求失败,状态码: " + response.getStatusLine().getStatusCode());
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new Exception("上传文件失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 绑定文件到业务(插入FILE_ASSOCIATIONS表)
|
||||
*/
|
||||
private static void bindFileToBusiness(String fileId, String tableName, String businessId, String type, int sortOrder) throws Exception {
|
||||
// 检查是否已绑定
|
||||
if (isAlreadyBound(fileId, tableName, businessId)) {
|
||||
System.out.println("文件 " + fileId + " 已绑定到业务 " + businessId + ",跳过");
|
||||
return;
|
||||
}
|
||||
|
||||
PreparedStatement pstmt = null;
|
||||
|
||||
try {
|
||||
// 生成主键ID
|
||||
String id = UUID.randomUUID().toString().replace("-", "");
|
||||
|
||||
pstmt = conn.prepareStatement(INSERT_FILE_ASSOC_SQL);
|
||||
pstmt.setString(1, id); // ID
|
||||
pstmt.setString(2, fileId); // FILE_ID
|
||||
pstmt.setInt(3, sortOrder); // SORT_ON
|
||||
pstmt.setTimestamp(4, new Timestamp(System.currentTimeMillis())); // TM
|
||||
pstmt.setString(5, tableName); // TABLE_NAME
|
||||
pstmt.setString(6, "1"); // DEL (1:未删除)
|
||||
pstmt.setString(7, businessId.trim().toUpperCase(Locale.ROOT)); // BUSINESS_ID
|
||||
pstmt.setString(8, type); // TYPE
|
||||
|
||||
int affectedRows = pstmt.executeUpdate();
|
||||
if (affectedRows == 1) {
|
||||
// 成功
|
||||
} else {
|
||||
throw new Exception("插入失败,影响行数: " + affectedRows);
|
||||
}
|
||||
} finally {
|
||||
if (pstmt != null) pstmt.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 检查是否已绑定
|
||||
*/
|
||||
private static boolean isAlreadyBound(String fileId, String tableName, String businessId) throws Exception {
|
||||
PreparedStatement pstmt = null;
|
||||
ResultSet rs = null;
|
||||
|
||||
try {
|
||||
pstmt = conn.prepareStatement(CHECK_FILE_ASSOC_SQL);
|
||||
pstmt.setString(1, fileId);
|
||||
pstmt.setString(2, tableName);
|
||||
pstmt.setString(3, businessId);
|
||||
|
||||
rs = pstmt.executeQuery();
|
||||
if (rs.next()) {
|
||||
return rs.getInt(1) > 0;
|
||||
}
|
||||
} finally {
|
||||
if (rs != null) rs.close();
|
||||
if (pstmt != null) pstmt.close();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static ProcessResult handleRbadData(File subDir, String groupId, String watershedCode, String userId) throws Exception {
|
||||
ProcessResult result = new ProcessResult(1, 0, 0);
|
||||
|
||||
// 下面还有两层文件夹
|
||||
File[] firDirs = subDir.listFiles(File::isDirectory);
|
||||
if (firDirs != null) {
|
||||
for (File fdir : firDirs) {
|
||||
File[] sdirs = fdir.listFiles(File::isDirectory);
|
||||
if (sdirs != null) {
|
||||
for (File sdir : sdirs) {
|
||||
// 编码
|
||||
String cd = sdir.getName();
|
||||
System.out.println("处理跨沟道路和桥涵编码: " + cd);
|
||||
|
||||
// 查询业务数据
|
||||
List<String> businessIds = queryBusinessIds(cd, watershedCode, null, groupId);
|
||||
|
||||
if (CollectionUtils.isEmpty(businessIds)) {
|
||||
System.out.println("未找到编码为 " + cd + " 的跨沟道路和桥涵数据");
|
||||
continue;
|
||||
}
|
||||
|
||||
// 获取该文件夹下的所有照片
|
||||
List<File> photoFiles = collectImageFiles(sdir);
|
||||
if (!photoFiles.isEmpty()) {
|
||||
System.out.println("找到 " + photoFiles.size() + " 张照片");
|
||||
|
||||
// 先上传所有照片
|
||||
List<String> fileIds = new ArrayList<>();
|
||||
for (int i = 0; i < photoFiles.size(); i++) {
|
||||
File photoFile = photoFiles.get(i);
|
||||
try {
|
||||
String fileId = uploadPhoto(photoFile, groupId, userId, null);
|
||||
if (fileId != null) {
|
||||
fileIds.add(fileId);
|
||||
result.photosUploaded++;
|
||||
System.out.println("✓ 上传成功: " + photoFile.getName() + " -> " + fileId);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.err.println("✗ 上传失败: " + photoFile.getName() + ", 错误: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
// 为每个业务ID绑定所有照片
|
||||
for (String businessId : businessIds) {
|
||||
for (int i = 0; i < fileIds.size(); i++) {
|
||||
String fileId = fileIds.get(i);
|
||||
try {
|
||||
bindFileToBusiness(fileId, groupId, businessId, null, i + 1);
|
||||
result.bindingsCreated++;
|
||||
System.out.println("✓ 绑定成功: 文件 " + fileId + " -> 业务 " + businessId);
|
||||
} catch (Exception e) {
|
||||
System.err.println("✗ 绑定失败: 文件 " + fileId + " -> 业务 " + businessId + ", 错误: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
System.out.println("编码 " + cd + " 下没有找到照片");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static File findDirectory(File parentDir, String name) {
|
||||
File[] subDirs = parentDir.listFiles(File::isDirectory);
|
||||
if (subDirs != null) {
|
||||
for (File subDir : subDirs) {
|
||||
if (subDir.getName().contains(name)) {
|
||||
return subDir;
|
||||
}
|
||||
}
|
||||
if (subDirs.length > 0) return subDirs[0];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static String extractWatershedCode(String dirName) {
|
||||
Pattern pattern = Pattern.compile("^[a-zA-Z0-9]+");
|
||||
Matcher matcher = pattern.matcher(dirName);
|
||||
if (matcher.find()) {
|
||||
return matcher.group();
|
||||
} else {
|
||||
System.out.println("小流域编码文件夹有问题: " + dirName);
|
||||
}
|
||||
return dirName;
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理结果类
|
||||
*/
|
||||
private static class ProcessResult {
|
||||
int processed; // 处理的目录数
|
||||
int photosUploaded; // 上传的照片数
|
||||
int bindingsCreated; // 创建的绑定关系数
|
||||
|
||||
ProcessResult(int processed, int photosUploaded, int bindingsCreated) {
|
||||
this.processed = processed;
|
||||
this.photosUploaded = photosUploaded;
|
||||
this.bindingsCreated = bindingsCreated;
|
||||
}
|
||||
|
||||
void add(ProcessResult other) {
|
||||
this.processed += other.processed;
|
||||
this.photosUploaded += other.photosUploaded;
|
||||
this.bindingsCreated += other.bindingsCreated;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -15,7 +15,7 @@ public class GetWscdDataImporter {
|
|||
}
|
||||
|
||||
// 测试用的固定路径
|
||||
args = new String[]{"D:\\1111-湖工大-已通过初检-空间格式通过-已统一表头-重新入库\\1111-湖工大-已通过初检-空间格式通过-已统一表头-重新入库"};
|
||||
args = new String[]{"F:\\04 小流域成果\\2024年度\\第二批次成果\\6666-华科26条1201-已通过初检-空间格式通过-已统一表头-待入库"};
|
||||
|
||||
if (args.length == 0) {
|
||||
System.out.println("请提供湖工大9.29目录路径作为参数");
|
||||
|
|
|
|||
|
|
@ -45,7 +45,7 @@ public class IaEftsTownImporter {
|
|||
// args = new String[]{"F:\\1111-湖工大50条成果-已通过初检-待入库"};
|
||||
// args = new String[]{"F:\\3333-2024年度咸宁市8条20251017-已通过初检-待入库"};
|
||||
// args = new String[]{"F:\\2222-华科-黄石局-14条小流域(10.20)-已通过初检-已统一表头-待入库"};
|
||||
args = new String[]{"D:\\1111-湖工大-已通过初检-空间格式通过-已统一表头-重新入库\\1111-湖工大-已通过初检-空间格式通过-已统一表头-重新入库"};
|
||||
args = new String[]{"D:\\华科-苦水河2\\华科-苦水河2"};
|
||||
|
||||
|
||||
if (args.length == 0) {
|
||||
|
|
|
|||
|
|
@ -33,7 +33,7 @@ public class IaGlaInfoDataImporter {
|
|||
// args = new String[]{"F:\\3333-2024年度咸宁市8条20251017-已通过初检-待入库"};
|
||||
// args = new String[]{"F:\\4444-长江科学院-黄冈28条-已通过初检-已统一表头-待入库"};
|
||||
// args = new String[]{"F:\\小流域\\5555-长江科学院20条10.23-已通过初检-已统一表头-待入库\\5555-长江科学院20条10.23-已通过初检-已统一表头-待入库"};
|
||||
args = new String[]{"D:\\1111-湖工大-已通过初检-空间格式通过-已统一表头-重新入库\\1111-湖工大-已通过初检-空间格式通过-已统一表头-重新入库"};
|
||||
args = new String[]{"F:\\04 小流域成果\\2024年度\\第二批次成果\\4444-长江科学院-黄冈28条1201-已通过初检-空间格式通过-已统一表头-待入库"};
|
||||
|
||||
if (args.length == 0) {
|
||||
System.out.println("请提供湖工大9.29目录路径作为参数");
|
||||
|
|
@ -355,8 +355,8 @@ public class IaGlaInfoDataImporter {
|
|||
try {
|
||||
|
||||
// 检查编码(C列)是否为空
|
||||
String name = getCellStringValue(row.getCell(2));
|
||||
if (name == null || name.trim().isEmpty()) {
|
||||
String name = getCellStringValue(row.getCell(1));
|
||||
if (name == null || name.trim().isEmpty() || "无".equals(name)) {
|
||||
System.out.println("第 " + (row.getRowNum() + 1) + " 行编码为空,跳过该行");
|
||||
return null;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -39,7 +39,7 @@ public class IaHdpPointDataImporter {
|
|||
// args = new String[]{"F:\\2222-华科-黄石局-14条小流域(10.20)-已通过初检-已统一表头-已入库"};
|
||||
// args = new String[]{"F:\\3333-华科-地大9条-20251021-已通过初检-已统一表头-已入库"};
|
||||
// args = new String[]{"F:\\4444-长江科学院-黄冈28条-已通过初检-已统一表头-待入库"};
|
||||
args = new String[]{"D:\\1111-湖工大-已通过初检-空间格式通过-已统一表头-重新入库\\1111-湖工大-已通过初检-空间格式通过-已统一表头-重新入库"};
|
||||
args = new String[]{"D:\\华科-苦水河2\\华科-苦水河2"};
|
||||
|
||||
if (args.length == 0) {
|
||||
System.out.println("请提供湖工大9.29目录路径作为参数");
|
||||
|
|
|
|||
|
|
@ -34,7 +34,7 @@ public class IaRbadInfoDataImporter {
|
|||
// args = new String[]{"F:\\4444-长江科学院-黄冈28条-已通过初检-已统一表头-待入库"};
|
||||
// args = new String[]{"F:\\3333-华科-地大9条-20251021-已通过初检-已统一表头-待入库"};
|
||||
// args = new String[]{"F:\\小流域\\5555-长江科学院20条10.23-已通过初检-已统一表头-待入库\\5555-长江科学院20条10.23-已通过初检-已统一表头-待入库"};
|
||||
args = new String[]{"D:\\1111-湖工大-已通过初检-空间格式通过-已统一表头-重新入库\\1111-湖工大-已通过初检-空间格式通过-已统一表头-重新入库"};
|
||||
args = new String[]{"D:\\华科-苦水河2\\华科-苦水河2"};
|
||||
|
||||
if (args.length == 0) {
|
||||
System.out.println("请提供湖工大9.29目录路径作为参数");
|
||||
|
|
|
|||
|
|
@ -56,7 +56,7 @@ public class IaShzhHdpDataImporter {
|
|||
// args = new String[]{"F:\\4444-长江科学院-黄冈28条-已通过初检-已统一表头-待入库"};
|
||||
// args = new String[]{"F:\\3333-华科-地大9条-20251021-已通过初检-已统一表头-待入库"};
|
||||
// args = new String[]{"F:\\小流域\\5555-长江科学院20条10.23-已通过初检-已统一表头-待入库\\5555-长江科学院20条10.23-已通过初检-已统一表头-待入库"};
|
||||
args = new String[]{"D:\\1111-湖工大-已通过初检-空间格式通过-已统一表头-重新入库\\1111-湖工大-已通过初检-空间格式通过-已统一表头-重新入库"};
|
||||
args = new String[]{"D:\\华科-苦水河2\\华科-苦水河2"};
|
||||
|
||||
if (args.length == 0) {
|
||||
System.out.println("请提供湖工大9.29目录路径作为参数");
|
||||
|
|
|
|||
|
|
@ -58,7 +58,7 @@ public class IaShzhHsurfaceDataImporter {
|
|||
// args = new String[]{"C:\\Users\\gsiot\\Desktop\\项目资料\\山洪\\小流域\\222"};
|
||||
// args = new String[]{"F:\\1111-湖工大50条成果-已通过初检-待入库"};--湖工大
|
||||
// args = new String[]{"F:\\4444-长江科学院-黄冈28条-已通过初检-已统一表头-待入库"};
|
||||
args = new String[]{"D:\\1111-湖工大-已通过初检-空间格式通过-已统一表头-重新入库\\1111-湖工大-已通过初检-空间格式通过-已统一表头-重新入库"};
|
||||
args = new String[]{"D:\\华科-苦水河2\\华科-苦水河2"};
|
||||
|
||||
if (args.length == 0) {
|
||||
System.out.println("请提供湖工大9.29目录路径作为参数");
|
||||
|
|
|
|||
|
|
@ -50,7 +50,7 @@ public class IaShzhVsurfaceDataImporter {
|
|||
// args = new String[]{"F:\\3333-2024年度咸宁市8条20251017-已通过初检-待入库"};
|
||||
// args = new String[]{"F:\\4444-长江科学院-黄冈28条-已通过初检-已统一表头-待入库"};
|
||||
|
||||
args = new String[]{"D:\\1111-湖工大-已通过初检-空间格式通过-已统一表头-重新入库\\1111-湖工大-已通过初检-空间格式通过-已统一表头-重新入库"};
|
||||
args = new String[]{"D:\\华科-苦水河2\\华科-苦水河2"};
|
||||
if (args.length == 0) {
|
||||
System.out.println("请提供湖工大9.29目录路径作为参数");
|
||||
return;
|
||||
|
|
|
|||
|
|
@ -42,7 +42,7 @@ public class IaStHdpDataImporter {
|
|||
// 测试用的固定路径
|
||||
// args = new String[]{"F:\\1111-湖工大50条成果-已通过初检-待入库"};
|
||||
// args = new String[]{"F:\\4444-长江科学院-黄冈28条-已通过初检-已统一表头-待入库"};
|
||||
args = new String[]{"D:\\1111-湖工大-已通过初检-空间格式通过-已统一表头-重新入库\\1111-湖工大-已通过初检-空间格式通过-已统一表头-重新入库"};
|
||||
args = new String[]{"D:\\华科-苦水河2\\华科-苦水河2"};
|
||||
|
||||
if (args.length == 0) {
|
||||
System.out.println("请提供湖工大9.29目录路径作为参数");
|
||||
|
|
|
|||
|
|
@ -272,57 +272,7 @@ public class WatershedDataCleaner {
|
|||
*/
|
||||
public static void main(String[] args) {
|
||||
// 测试数据
|
||||
List<String> testWatershedCodes = Arrays.asList(
|
||||
"HBWFF1400123GD0000000",
|
||||
"HBWFF1400125D00000000",
|
||||
"HBWFF1400126000000000",
|
||||
"HBWFF14302PC000000000",
|
||||
"HBWFF14303G0000000000",
|
||||
"HBWFA861031kA00000000",
|
||||
"HBWFA8610l10000000000",
|
||||
"HBWFA86206f0000000000",
|
||||
"HBWFA8300114M00000000",
|
||||
"HBWFA830011GP00000000",
|
||||
"HBWFABV3011I000000000",
|
||||
"HBWFA85001211FC000000",
|
||||
"HBWFA85001231lB000000",
|
||||
"HBWFA85001241LC000000",
|
||||
"HBWFA85001251IC000000",
|
||||
"HBWFA85001261JC000000",
|
||||
"HBWFA85001281dBH00000",
|
||||
"HBWFA8580500000000000",
|
||||
"HBWFA8580C00000000000",
|
||||
"HBWFA85001241hBE00000",
|
||||
"HBWFA850012A1hB000000",
|
||||
"HBWFA850012B1kB000000",
|
||||
"HBWFA85405G0000000000",
|
||||
"HBWFA8540800000000000",
|
||||
"HBWFA7800126000000000",
|
||||
"HBWFA7800127000000000",
|
||||
"HBWFA85001221IA000001",
|
||||
"HBWFA85001241GA000000",
|
||||
"HBWFA850012C100000000",
|
||||
"HBWFE22001261G0000000",
|
||||
"HBWFE2200128100000000",
|
||||
"HBWFE2400121000000000",
|
||||
"HBWFE240012P000000000",
|
||||
"HBWFA85001231SB000001",
|
||||
"HBWFA85202D0000000000",
|
||||
"HBWFA85206E0000000000",
|
||||
"HBWFA8520B00000000000",
|
||||
"HBWFAHV00125000000000",
|
||||
"HBWFA03002016T0000000",
|
||||
"HBWFA03002018Z0000000",
|
||||
"HBWFA8200115P00000000",
|
||||
"HBWFA820011C000000000",
|
||||
"HBWFA85001261RB000000",
|
||||
"HBWFA850012A1dB000000",
|
||||
"HBWFA85001241lA000000",
|
||||
"HBWFF12A0153I00000000",
|
||||
"HBWFF12A0155NE0000000",
|
||||
"HBWFF12A0156L00000000",
|
||||
"HBWFF12A0156N00000000",
|
||||
"HBWFF12A0157M00000000"
|
||||
List<String> testWatershedCodes = Arrays.asList("HBWFG290012AZP0000000"
|
||||
);
|
||||
|
||||
System.out.println("开始测试删除操作...");
|
||||
|
|
|
|||
Loading…
Reference in New Issue