|
@@ -1,31 +1,29 @@
|
|
|
package com.zjugis.yzt.service.impl;
|
|
|
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
-import com.zjugis.yzt.beans.entity.TempLand;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
import com.zjugis.yzt.dao.SsnydNewMapper;
|
|
|
import com.zjugis.yzt.beans.entity.SsnydNew;
|
|
|
-import com.zjugis.yzt.dao.TempLandMapper;
|
|
|
import com.zjugis.yzt.service.SsnydService;
|
|
|
import com.zjugis.yzt.utils.ExcelUtils;
|
|
|
import com.zjugis.yzt.utils.geocomm.ParseResult;
|
|
|
import com.zjugis.yzt.utils.geocomm.TxtReader;
|
|
|
+import com.zjugis.yzt.utils.ZipFileProcessor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
-import org.springframework.transaction.annotation.Transactional;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import java.io.File;
|
|
|
import java.io.FileInputStream;
|
|
|
-import java.io.FileOutputStream;
|
|
|
import java.io.IOException;
|
|
|
import java.text.ParseException;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
-import java.util.HashMap;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Date;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
-import java.util.UUID;
|
|
|
-import java.util.zip.ZipEntry;
|
|
|
-import java.util.zip.ZipInputStream;
|
|
|
+import java.util.Locale;
|
|
|
+
|
|
|
|
|
|
/**
|
|
|
* 设施农用地Service实现类
|
|
@@ -35,58 +33,27 @@ import java.util.zip.ZipInputStream;
|
|
|
public class SsnydServiceImpl extends ServiceImpl<SsnydNewMapper, SsnydNew> implements SsnydService {
|
|
|
|
|
|
@Override
|
|
|
- public boolean uploadSsnydData(MultipartFile zipFile) {
|
|
|
+ public List<String> uploadSsnydData(MultipartFile zipFile) {
|
|
|
+ List<String> errorList = new ArrayList<>();
|
|
|
if (zipFile.isEmpty()) {
|
|
|
- log.error("上传文件为空");
|
|
|
- return false;
|
|
|
+ String msg = "上传文件为空";
|
|
|
+ log.error(msg);
|
|
|
+ errorList.add(msg);
|
|
|
+ return errorList;
|
|
|
}
|
|
|
|
|
|
File tempDir = null;
|
|
|
try {
|
|
|
- // 创建临时目录
|
|
|
- tempDir = new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString());
|
|
|
- if (!tempDir.exists()) {
|
|
|
- tempDir.mkdirs();
|
|
|
- }
|
|
|
-
|
|
|
- // 解压ZIP文件
|
|
|
- Map<String, File> txtFiles = new HashMap<>();
|
|
|
- File xlsFile = null;
|
|
|
-
|
|
|
- try (ZipInputStream zis = new ZipInputStream(zipFile.getInputStream())) {
|
|
|
- ZipEntry entry;
|
|
|
- while ((entry = zis.getNextEntry()) != null) {
|
|
|
- String fileName = entry.getName();
|
|
|
- File file = new File(tempDir, fileName);
|
|
|
-
|
|
|
- // 创建父目录
|
|
|
- File parent = file.getParentFile();
|
|
|
- if (!parent.exists()) {
|
|
|
- parent.mkdirs();
|
|
|
- }
|
|
|
-
|
|
|
- // 写入文件
|
|
|
- try (FileOutputStream fos = new FileOutputStream(file)) {
|
|
|
- byte[] buffer = new byte[1024];
|
|
|
- int len;
|
|
|
- while ((len = zis.read(buffer)) > 0) {
|
|
|
- fos.write(buffer, 0, len);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // 分类存储文件引用
|
|
|
- if (fileName.toLowerCase().endsWith(".xls") || fileName.toLowerCase().endsWith(".xlsx")) {
|
|
|
- xlsFile = file;
|
|
|
- } else if (fileName.toLowerCase().endsWith(".txt")) {
|
|
|
- String projectCode = fileName.substring(0, fileName.lastIndexOf("."));
|
|
|
- txtFiles.put(projectCode, file);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
+ ZipFileProcessor.ZipProcessResult zipProcessResult = ZipFileProcessor.processZipFile(zipFile);
|
|
|
+ Map<String, File> txtFiles = zipProcessResult.getTxtFiles();
|
|
|
+ File xlsFile = zipProcessResult.getXlsxFile();
|
|
|
+ tempDir = zipProcessResult.getTempDir();
|
|
|
|
|
|
if (xlsFile == null) {
|
|
|
- log.error("ZIP包中未找到Excel文件");
|
|
|
- return false;
|
|
|
+ String msg = "ZIP包中未找到Excel文件";
|
|
|
+ log.error(msg);
|
|
|
+ errorList.add(msg);
|
|
|
+ return errorList;
|
|
|
}
|
|
|
|
|
|
// 读取Excel数据
|
|
@@ -98,67 +65,65 @@ public class SsnydServiceImpl extends ServiceImpl<SsnydNewMapper, SsnydNew> impl
|
|
|
for (Map<String, Object> row : batchDataList) {
|
|
|
SsnydNew ssnydNew = new SsnydNew();
|
|
|
// 字段映射
|
|
|
- ssnydNew.setXmbh(getString(row, "项目名称"));
|
|
|
- ssnydNew.setBah(getString(row, "备案号"));
|
|
|
+ ssnydNew.setXmmc(getString(row, "项目名称"));
|
|
|
+ ssnydNew.setBah(getString(row, "备案号"));
|
|
|
ssnydNew.setSzs(getString(row, "市"));
|
|
|
ssnydNew.setXzq(getString(row, "县区"));
|
|
|
ssnydNew.setYt(getString(row, "用途"));
|
|
|
ssnydNew.setBamj(getDouble(row, "备案面积"));
|
|
|
-
|
|
|
+ ssnydNew.setXmbh(getString(row, "项目索引"));
|
|
|
// 处理日期字段
|
|
|
- String basjStr = getString(row, "备案时间");
|
|
|
- if (basjStr != null) {
|
|
|
+ if (setBasj(errorList, row, ssnydNew)) continue;
|
|
|
+
|
|
|
+ // 保存前先删除已存在数据
|
|
|
+ String xmbh = ssnydNew.getXmbh();
|
|
|
+ if (xmbh != null) {
|
|
|
+ QueryWrapper<SsnydNew> queryWrapper = new QueryWrapper<>();
|
|
|
+ queryWrapper.eq("xmbh", xmbh);
|
|
|
try {
|
|
|
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
|
|
|
- ssnydNew.setBasj(sdf.parse(basjStr));
|
|
|
- } catch (ParseException e) {
|
|
|
- log.error("解析备案时间失败: {}", basjStr);
|
|
|
- return false;
|
|
|
+ this.remove(queryWrapper);
|
|
|
+ } catch (Exception e) {
|
|
|
+ String msg = String.format("删除项目索引为 %s 的旧数据失败: %s", xmbh, e.getMessage());
|
|
|
+ log.error(msg, e);
|
|
|
+ errorList.add(msg);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 先保存SsnydNew
|
|
|
- if (!this.save(ssnydNew)) {
|
|
|
- log.error("保存设施农用地数据失败:{}", ssnydNew.getXmbh());
|
|
|
- return false;
|
|
|
+ try {
|
|
|
+ if (!this.save(ssnydNew)) {
|
|
|
+ String msg = String.format("保存设施农用地数据失败:%s", ssnydNew.getXmbh());
|
|
|
+ log.error(msg);
|
|
|
+ errorList.add(msg);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ String msg = String.format("保存设施农用地数据异常:%s,错误:%s", ssnydNew.getXmbh(), e.getMessage());
|
|
|
+ log.error(msg, e);
|
|
|
+ errorList.add(msg);
|
|
|
+ continue;
|
|
|
}
|
|
|
|
|
|
Integer objectId = ssnydNew.getObjectid();
|
|
|
if (objectId == null) {
|
|
|
- log.error("获取保存后的objectid失败:{}", ssnydNew.getXmbh());
|
|
|
- return false;
|
|
|
+ String msg = String.format("获取保存后的objectid失败:%s", ssnydNew.getXmbh());
|
|
|
+ log.error(msg);
|
|
|
+ errorList.add(msg);
|
|
|
+ continue;
|
|
|
}
|
|
|
|
|
|
// 解析txt为Geometry并持久化
|
|
|
String projectIndex = ssnydNew.getXmbh();
|
|
|
File txtFile = txtFiles.get(projectIndex);
|
|
|
- if (txtFile != null) {
|
|
|
- try (FileInputStream txtInputStream = new FileInputStream(txtFile)) {
|
|
|
- TxtReader txtReader = new TxtReader(txtInputStream);
|
|
|
- ParseResult parseResult = txtReader.read();
|
|
|
- if (parseResult != null && parseResult.getGeometry() != null) {
|
|
|
- String wkt = parseResult.getGeometry().toText();
|
|
|
- // 使用updateShapeById更新shape字段
|
|
|
- int updateCount = ((SsnydNewMapper) this.baseMapper).updateShapeById(objectId, wkt, 4528);
|
|
|
- if (updateCount <= 0) {
|
|
|
- log.warn("项目索引 {} 的shape字段持久化失败。", projectIndex);
|
|
|
- }
|
|
|
- } else {
|
|
|
- log.warn("项目索引 {} 的TXT文件解析失败或未获取到Geometry数据。", projectIndex);
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("解析项目索引 {} 的TXT文件失败: {}", projectIndex, e.getMessage());
|
|
|
- return false;
|
|
|
- }
|
|
|
- } else {
|
|
|
- log.warn("未找到项目索引 {} 对应的TXT界址点文件。", projectIndex);
|
|
|
- }
|
|
|
+ updateShape(errorList, objectId, projectIndex, txtFile);
|
|
|
}
|
|
|
|
|
|
- return true;
|
|
|
+ return errorList;
|
|
|
} catch (IOException e) {
|
|
|
- log.error("处理上传文件失败", e);
|
|
|
- return false;
|
|
|
+ String msg = "处理上传文件失败: " + e.getMessage();
|
|
|
+ log.error(msg, e);
|
|
|
+ errorList.add(msg);
|
|
|
+ return errorList;
|
|
|
} finally {
|
|
|
// 清理临时文件
|
|
|
if (tempDir != null && tempDir.exists()) {
|
|
@@ -167,6 +132,73 @@ public class SsnydServiceImpl extends ServiceImpl<SsnydNewMapper, SsnydNew> impl
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 设置备案时间
|
|
|
+ * @param errorList
|
|
|
+ * @param row
|
|
|
+ * @param ssnydNew
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private boolean setBasj(List<String> errorList, Map<String, Object> row, SsnydNew ssnydNew) {
|
|
|
+ String basjStr = getString(row, "备案时间");
|
|
|
+ if (basjStr != null) {
|
|
|
+ Date basj = null;
|
|
|
+ try {
|
|
|
+ // 先尝试 yyyy-MM-dd
|
|
|
+ basj = new SimpleDateFormat("yyyy-MM-dd").parse(basjStr);
|
|
|
+ } catch (ParseException e1) {
|
|
|
+ try {
|
|
|
+ // 再尝试 EEE MMM dd HH:mm:ss z yyyy
|
|
|
+ basj = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH).parse(basjStr);
|
|
|
+ } catch (ParseException e2) {
|
|
|
+ String msg = "解析备案时间失败: " + basjStr;
|
|
|
+ log.error(msg);
|
|
|
+ errorList.add(msg);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ ssnydNew.setBasj(basj);
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 更新shape字段
|
|
|
+ * @param errorList
|
|
|
+ * @param objectId
|
|
|
+ * @param projectIndex
|
|
|
+ * @param txtFile
|
|
|
+ */
|
|
|
+ private void updateShape(List<String> errorList, Integer objectId, String projectIndex, File txtFile) {
|
|
|
+ if (txtFile != null) {
|
|
|
+ try (FileInputStream txtInputStream = new FileInputStream(txtFile)) {
|
|
|
+ TxtReader txtReader = new TxtReader(txtInputStream);
|
|
|
+ ParseResult parseResult = txtReader.read();
|
|
|
+ if (parseResult != null && parseResult.getGeometry() != null) {
|
|
|
+ String wkt = parseResult.getGeometry().toText();
|
|
|
+ int updateCount = ((SsnydNewMapper) this.baseMapper).updateShapeById(objectId, wkt, 4528);
|
|
|
+ if (updateCount <= 0) {
|
|
|
+ String msg = String.format("项目索引 %s 的shape字段持久化失败。", projectIndex);
|
|
|
+ log.warn(msg);
|
|
|
+ errorList.add(msg);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ String msg = String.format("项目索引 %s 的TXT文件解析失败或未获取到Geometry数据。", projectIndex);
|
|
|
+ log.warn(msg);
|
|
|
+ errorList.add(msg);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ String msg = String.format("解析项目索引 %s 的TXT文件失败: %s", projectIndex, e.getMessage());
|
|
|
+ log.error(msg, e);
|
|
|
+ errorList.add(msg);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ String msg = String.format("未找到项目索引 %s 对应的TXT界址点文件。", projectIndex);
|
|
|
+ log.warn(msg);
|
|
|
+ errorList.add(msg);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
private String getString(Map<String, Object> map, String key) {
|
|
|
Object value = map.get(key);
|
|
|
return value != null ? String.valueOf(value) : null;
|