|
@@ -1,15 +1,210 @@
|
|
|
package com.zjugis.yzt.service.impl;
|
|
|
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
-import com.zjugis.yzt.dao.TempLandMapper;
|
|
|
import com.zjugis.yzt.beans.entity.TempLand;
|
|
|
+import com.zjugis.yzt.dao.TempLandMapper;
|
|
|
import com.zjugis.yzt.service.TempLandService;
|
|
|
+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.*;
|
|
|
+import java.util.zip.ZipEntry;
|
|
|
+import java.util.zip.ZipInputStream;
|
|
|
|
|
|
/**
|
|
|
- * 临时用地Service实现类
|
|
|
+ * @description: 临时用地Service实现类
|
|
|
*/
|
|
|
@Service
|
|
|
+@Slf4j
|
|
|
public class TempLandServiceImpl extends ServiceImpl<TempLandMapper, TempLand> implements TempLandService {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean uploadTemplandData(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();
|
|
|
+
|
|
|
+ // 处理每一行数据
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
|
|
|
+ for (Map<String, Object> row : batchDataList) {
|
|
|
+ TempLand tempLand = new TempLand();
|
|
|
+
|
|
|
+ // 字段映射
|
|
|
+ tempLand.setXmbh(getString(row, "项目索引"));
|
|
|
+ tempLand.setXmmc(getString(row, "项目名称"));
|
|
|
+ tempLand.setPzwh(getString(row, "批复文号"));
|
|
|
+ tempLand.setYddw(getString(row, "用地单位"));
|
|
|
+ tempLand.setPzmj(getDouble(row, "批准面积"));
|
|
|
+ tempLand.setPzdw(getString(row, "批准单位"));
|
|
|
+ tempLand.setZlwz(getString(row, "坐落位置"));
|
|
|
+ tempLand.setRknf(getString(row, "入库年份"));
|
|
|
+
|
|
|
+ try {
|
|
|
+ String approvalDate = getString(row, "审批时间");
|
|
|
+ if (approvalDate != null && !approvalDate.isEmpty()) {
|
|
|
+ tempLand.setPzsj(sdf.parse(approvalDate));
|
|
|
+ }
|
|
|
+
|
|
|
+ String expiryDate = getString(row, "到期时间");
|
|
|
+ if (expiryDate != null && !expiryDate.isEmpty()) {
|
|
|
+ tempLand.setDqsj(sdf.parse(expiryDate));
|
|
|
+ }
|
|
|
+
|
|
|
+ String storageDate = getString(row, "入库时间");
|
|
|
+ if (storageDate != null && !storageDate.isEmpty()) {
|
|
|
+ tempLand.setRksj(sdf.parse(storageDate));
|
|
|
+ }
|
|
|
+ } catch (ParseException e) {
|
|
|
+ log.error("日期解析错误", e);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ tempLand.setRkry(getString(row, "入库人员"));
|
|
|
+ tempLand.setBz(getString(row, "备注"));
|
|
|
+
|
|
|
+ // 保存临时用地数据
|
|
|
+ if (!this.save(tempLand)) {
|
|
|
+ log.error("保存临时用地数据失败:{}", tempLand.getXmbh());
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取保存后的ID
|
|
|
+ Integer objectId = tempLand.getObjectid();
|
|
|
+ if (objectId == null) {
|
|
|
+ log.error("获取保存后的objectId失败:{}", tempLand.getXmbh());
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 解析txt为Geometry并持久化
|
|
|
+ String projectIndex = tempLand.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 = ((TempLandMapper) 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();
|
|
|
+ }
|
|
|
}
|