|
@@ -1,10 +1,23 @@
|
|
|
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.beans.entity.StGddk;
|
|
|
import com.zjugis.yzt.service.StGddkService;
|
|
|
+import com.zjugis.yzt.utils.ExcelUtils;
|
|
|
+import com.zjugis.yzt.utils.geocomm.ParseResult;
|
|
|
+import com.zjugis.yzt.utils.geocomm.TxtReader;
|
|
|
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.util.*;
|
|
|
+import java.util.zip.ZipEntry;
|
|
|
+import java.util.zip.ZipInputStream;
|
|
|
|
|
|
/**
|
|
|
* @program: yh_yzt
|
|
@@ -14,4 +27,91 @@ import org.springframework.stereotype.Service;
|
|
|
**/
|
|
|
@Service
|
|
|
public class StGddkServiceImpl extends ServiceImpl<StGddkMapper, StGddk> implements StGddkService {
|
|
|
-}
|
|
|
+ @Override
|
|
|
+ public boolean uploadGdData(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 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;
|
|
|
+
|
|
|
+ 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 读取到数据。");
|
|
|
+ }
|
|
|
+
|
|
|
+ // --- End: 使用 ExcelUtils.importExcel 解析 XLSX 文件 ---
|
|
|
+
|
|
|
+
|
|
|
+ return true;
|
|
|
+
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error("文件上传或处理失败", e);
|
|
|
+ return false;
|
|
|
+ } finally {
|
|
|
+ // 清理临时文件
|
|
|
+ if (tempDir != null && tempDir.exists()) {
|
|
|
+// deleteDirectory(tempDir);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|