|
@@ -2,9 +2,25 @@ package com.zjugis.yzt.service.impl;
|
|
|
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import com.zjugis.yzt.beans.entity.StNzydk;
|
|
|
+import com.zjugis.yzt.dao.StGddkMapper;
|
|
|
import com.zjugis.yzt.dao.StNzydkMapper;
|
|
|
import com.zjugis.yzt.service.StNzydkService;
|
|
|
+import com.zjugis.yzt.utils.ExcelUtils;
|
|
|
+import com.zjugis.yzt.utils.geocomm.ParseResult;
|
|
|
+import com.zjugis.yzt.utils.geocomm.TxtReader;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileInputStream;
|
|
|
+import java.io.FileOutputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.util.*;
|
|
|
+import java.util.zip.ZipEntry;
|
|
|
+import java.util.zip.ZipInputStream;
|
|
|
|
|
|
/**
|
|
|
* @program: yh_yzt
|
|
@@ -13,5 +29,185 @@ import org.springframework.stereotype.Service;
|
|
|
* @create: 2024-10-11 14:45
|
|
|
**/
|
|
|
@Service
|
|
|
+@Slf4j
|
|
|
public class StNzydkServiceImpl extends ServiceImpl<StNzydkMapper, StNzydk> implements StNzydkService {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean uploadNzydkData(MultipartFile zipFile) {
|
|
|
+ if (zipFile.isEmpty()) {
|
|
|
+ log.error("上传文件为空");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ File tempDir = null;
|
|
|
+ try {
|
|
|
+ // 创建临时目录
|
|
|
+ tempDir = new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString());
|
|
|
+ System.out.println("tempDir:" + tempDir.getAbsolutePath());
|
|
|
+ if (!tempDir.exists()) {
|
|
|
+ tempDir.mkdirs();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 解压ZIP文件
|
|
|
+ Map<String, File> txtFiles = new HashMap<>();
|
|
|
+ File xlsxFile = null;
|
|
|
+
|
|
|
+ try (ZipInputStream zis = new ZipInputStream(zipFile.getInputStream())) {
|
|
|
+ ZipEntry entry;
|
|
|
+ while ((entry = zis.getNextEntry()) != null) {
|
|
|
+ File newFile = new File(tempDir, entry.getName());
|
|
|
+ if (entry.isDirectory()) {
|
|
|
+ newFile.mkdirs();
|
|
|
+ } else {
|
|
|
+ // 确保父目录存在
|
|
|
+ new File(newFile.getParent()).mkdirs();
|
|
|
+ try (FileOutputStream fos = new FileOutputStream(newFile)) {
|
|
|
+ byte[] buffer = new byte[1024];
|
|
|
+ int len;
|
|
|
+ while ((len = zis.read(buffer)) > 0) {
|
|
|
+ fos.write(buffer, 0, len);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (newFile.getName().toLowerCase().endsWith(".xlsx")) {
|
|
|
+ xlsxFile = newFile;
|
|
|
+ } else if (newFile.getName().toLowerCase().endsWith(".txt")) {
|
|
|
+ String fileNameWithoutExt = newFile.getName().substring(0, newFile.getName().lastIndexOf('.'));
|
|
|
+ txtFiles.put(fileNameWithoutExt, newFile);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (xlsxFile == null) {
|
|
|
+ log.error("ZIP包中未找到XLSX文件");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // --- Start: 使用 ExcelUtils.importExcel 解析 XLSX 文件 ---
|
|
|
+ Map<String, Map<String, Object>> batchInfoMap = new HashMap<>();
|
|
|
+ List<Map<String, Object>> projectInfoList = new ArrayList<>();
|
|
|
+
|
|
|
+
|
|
|
+ int batchSheetIndex = 0;
|
|
|
+ int projectSheetIndex = 1;
|
|
|
+
|
|
|
+ List<Map<String, Object>> batchDataList = ExcelUtils.importExcel(
|
|
|
+ new FileInputStream(xlsxFile), xlsxFile.getName(), 0, 1, batchSheetIndex
|
|
|
+ );
|
|
|
+ if (!batchDataList.isEmpty()) {
|
|
|
+ Map<String, Object> batchData = batchDataList.get(0);
|
|
|
+ String pcbh = batchData.containsKey("批次索引") ? String.valueOf(batchData.get("批次索引")) : null;
|
|
|
+ if (pcbh != null && !pcbh.isEmpty()) {
|
|
|
+ batchInfoMap.put(pcbh, batchData);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ log.warn("未能从 '农转用批次信息' sheet 读取到数据。");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ projectInfoList = ExcelUtils.importExcel(
|
|
|
+ new FileInputStream(xlsxFile), xlsxFile.getName(), 0, 1, projectSheetIndex
|
|
|
+ );
|
|
|
+ if (projectInfoList.isEmpty()) {
|
|
|
+ log.warn("未能从 '农转用项目信息' sheet 读取到数据。");
|
|
|
+ }
|
|
|
+
|
|
|
+ // --- End: 使用 ExcelUtils.importExcel 解析 XLSX 文件 ---
|
|
|
+
|
|
|
+ for (Map<String, Object> projectInfo : projectInfoList) {
|
|
|
+ StNzydk stNzydk = new StNzydk();
|
|
|
+
|
|
|
+ // 批次信息 (通过批次索引关联)
|
|
|
+ String pcbh = projectInfo.containsKey("批次索引") ? String.valueOf(projectInfo.get("批次索引")) : null;
|
|
|
+ Map<String, Object> batchInfo = batchInfoMap.get(pcbh);
|
|
|
+ if (batchInfo != null) {
|
|
|
+ stNzydk.setPcbh(batchInfo.containsKey("批次索引") ? String.valueOf(batchInfo.get("批次索引")) : null);
|
|
|
+ stNzydk.setPch(batchInfo.containsKey("批次名称") ? String.valueOf(batchInfo.get("批次名称")) : null);
|
|
|
+ stNzydk.setLx(batchInfo.containsKey("类型") ? String.valueOf(batchInfo.get("类型")) : null);
|
|
|
+ stNzydk.setPwh(batchInfo.containsKey("批准文号") ? String.valueOf(batchInfo.get("批准文号")) : null);
|
|
|
+ stNzydk.setPzrq(batchInfo.containsKey("批准日期") ? String.valueOf(batchInfo.get("批准日期")) : null);
|
|
|
+ stNzydk.setPzMj(parseBigDecimal(batchInfo.containsKey("批次总面积(公顷)") ? String.valueOf(batchInfo.get("批次总面积(公顷)")) : null));
|
|
|
+ stNzydk.setNmjf(batchInfo.containsKey("是否农民建房") ? String.valueOf(batchInfo.get("是否农民建房")) : null);
|
|
|
+ } else {
|
|
|
+ log.warn("未找到批次索引为 {} 的批次信息,项目 {} 将跳过批次信息填充。", pcbh, projectInfo.containsKey("项目编号") ? String.valueOf(projectInfo.get("项目编号")) : "未知项目");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 项目信息
|
|
|
+ stNzydk.setDkBh(projectInfo.containsKey("项目编号") ? String.valueOf(projectInfo.get("项目编号")) : null);
|
|
|
+ stNzydk.setDkMc(projectInfo.containsKey("项目名称") ? String.valueOf(projectInfo.get("项目名称")) : null);
|
|
|
+ stNzydk.setXzz(projectInfo.containsKey("乡镇街道") ? String.valueOf(projectInfo.get("乡镇街道")) : null);
|
|
|
+ stNzydk.setDkYt(projectInfo.containsKey("土地用途") ? String.valueOf(projectInfo.get("土地用途")) : null);
|
|
|
+ stNzydk.setTdZl(projectInfo.containsKey("土地坐落") ? String.valueOf(projectInfo.get("土地坐落")) : null);
|
|
|
+ stNzydk.setDkMj(parseBigDecimal(projectInfo.containsKey("项目面积(公顷)") ? String.valueOf(projectInfo.get("项目面积(公顷)")) : null));
|
|
|
+ stNzydk.setDkQs(projectInfo.containsKey("用地主体") ? String.valueOf(projectInfo.get("用地主体")) : null);
|
|
|
+ stNzydk.setBz(projectInfo.containsKey("备注") ? String.valueOf(projectInfo.get("备注")) : null);
|
|
|
+ // 保存到数据库
|
|
|
+ if (!this.save(stNzydk)) {
|
|
|
+ log.error("保存农转用数据失败:{}", stNzydk.getDkBh());
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ // 获取保存后的objectid
|
|
|
+ Integer objectId = stNzydk.getObjectid();
|
|
|
+ if (objectId == null) {
|
|
|
+ log.error("获取保存后的objectid失败:{}", stNzydk.getDkBh());
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ // 解析图形信息
|
|
|
+ String projectCode = stNzydk.getDkBh();
|
|
|
+ File txtFile = txtFiles.get(projectCode);
|
|
|
+ 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();
|
|
|
+ ((StNzydkMapper) this.baseMapper).updateShapeById(objectId, wkt, 4528);
|
|
|
+ } else {
|
|
|
+ log.warn("项目 {} 的TXT文件解析失败或未获取到Geometry数据。", projectCode);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("解析项目 {} 的TXT文件失败: {}", projectCode, e.getMessage());
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ log.warn("未找到项目 {} 对应的TXT界址点文件。", projectCode);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ return true;
|
|
|
+
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error("文件上传或处理失败", e);
|
|
|
+ return false;
|
|
|
+ } finally {
|
|
|
+ // 清理临时文件
|
|
|
+ if (tempDir != null && tempDir.exists()) {
|
|
|
+ deleteDirectory(tempDir);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private BigDecimal parseBigDecimal(String value) {
|
|
|
+ if (value == null || value.isEmpty()) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ return new BigDecimal(value);
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
+ log.warn("无法将字符串 '{}' 转换为 BigDecimal。", value);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void deleteDirectory(File directory) {
|
|
|
+ File[] allContents = directory.listFiles();
|
|
|
+ if (allContents != null) {
|
|
|
+ for (File file : allContents) {
|
|
|
+ deleteDirectory(file);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ directory.delete();
|
|
|
+ }
|
|
|
}
|