|
@@ -1,15 +1,200 @@
|
|
|
package com.zjugis.yzt.service.impl;
|
|
|
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.zjugis.yzt.beans.entity.TempLand;
|
|
|
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 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.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.UUID;
|
|
|
+import java.util.zip.ZipEntry;
|
|
|
+import java.util.zip.ZipInputStream;
|
|
|
|
|
|
/**
|
|
|
* 设施农用地Service实现类
|
|
|
*/
|
|
|
@Service
|
|
|
+@Slf4j
|
|
|
public class SsnydServiceImpl extends ServiceImpl<SsnydNewMapper, SsnydNew> implements SsnydService {
|
|
|
-}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean uploadSsnydData(MultipartFile zipFile) {
|
|
|
+ if (zipFile.isEmpty()) {
|
|
|
+ log.error("上传文件为空");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (xlsFile == null) {
|
|
|
+ log.error("ZIP包中未找到Excel文件");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 读取Excel数据
|
|
|
+ FileInputStream excelStream = new FileInputStream(xlsFile);
|
|
|
+ List<Map<String, Object>> batchDataList = ExcelUtils.importExcel(excelStream, xlsFile.getName(), 0, 1, 0);
|
|
|
+ excelStream.close();
|
|
|
+
|
|
|
+ // 处理每一行数据
|
|
|
+ for (Map<String, Object> row : batchDataList) {
|
|
|
+ SsnydNew ssnydNew = new SsnydNew();
|
|
|
+ // 字段映射
|
|
|
+ ssnydNew.setXmbh(getString(row, "项目名称"));
|
|
|
+ ssnydNew.setBah(getString(row, "备案号"));
|
|
|
+ ssnydNew.setSzs(getString(row, "市"));
|
|
|
+ ssnydNew.setXzq(getString(row, "县区"));
|
|
|
+ ssnydNew.setYt(getString(row, "用途"));
|
|
|
+ ssnydNew.setBamj(getDouble(row, "备案面积"));
|
|
|
+
|
|
|
+ // 处理日期字段
|
|
|
+ String basjStr = getString(row, "备案时间");
|
|
|
+ if (basjStr != null) {
|
|
|
+ try {
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
|
|
|
+ ssnydNew.setBasj(sdf.parse(basjStr));
|
|
|
+ } catch (ParseException e) {
|
|
|
+ log.error("解析备案时间失败: {}", basjStr);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 先保存SsnydNew
|
|
|
+ if (!this.save(ssnydNew)) {
|
|
|
+ log.error("保存设施农用地数据失败:{}", ssnydNew.getXmbh());
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ Integer objectId = ssnydNew.getObjectid();
|
|
|
+ if (objectId == null) {
|
|
|
+ log.error("获取保存后的objectid失败:{}", ssnydNew.getXmbh());
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 解析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);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return true;
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error("处理上传文件失败", e);
|
|
|
+ return false;
|
|
|
+ } finally {
|
|
|
+ // 清理临时文件
|
|
|
+ if (tempDir != null && tempDir.exists()) {
|
|
|
+ deleteDirectory(tempDir);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getString(Map<String, Object> map, String key) {
|
|
|
+ Object value = map.get(key);
|
|
|
+ return value != null ? String.valueOf(value) : null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Double getDouble(Map<String, Object> map, String key) {
|
|
|
+ Object value = map.get(key);
|
|
|
+ if (value == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ return Double.valueOf(String.valueOf(value));
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void deleteDirectory(File directory) {
|
|
|
+ File[] files = directory.listFiles();
|
|
|
+ if (files != null) {
|
|
|
+ for (File file : files) {
|
|
|
+ if (file.isDirectory()) {
|
|
|
+ deleteDirectory(file);
|
|
|
+ } else {
|
|
|
+ file.delete();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ directory.delete();
|
|
|
+ }
|
|
|
+}
|