From 796b44ea813a1133beae4f3a67f1c0263510c0c7 Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期日, 17 十一月 2024 09:45:07 +0800
Subject: [PATCH] 1

---
 src/main/java/com/moon/server/service/data/UploadService.java |  170 ++++++++++++++++++++++++++------------------------------
 1 files changed, 79 insertions(+), 91 deletions(-)

diff --git a/src/main/java/com/moon/server/service/data/UploadService.java b/src/main/java/com/moon/server/service/data/UploadService.java
index bb5e2b4..71ebfba 100644
--- a/src/main/java/com/moon/server/service/data/UploadService.java
+++ b/src/main/java/com/moon/server/service/data/UploadService.java
@@ -1,31 +1,29 @@
 package com.moon.server.service.data;
 
+import com.google.common.collect.Lists;
 import com.moon.server.entity.all.BaseEntity;
 import com.moon.server.entity.all.StaticData;
+import com.moon.server.entity.data.*;
 import com.moon.server.entity.sys.UserEntity;
+import com.moon.server.helper.*;
 import com.moon.server.mapper.all.BasicMapper;
 import com.moon.server.mapper.data.UploadMapper;
 import com.moon.server.service.all.BaseQueryService;
 import com.moon.server.service.all.BaseUploadService;
 import com.moon.server.service.sys.DepService;
-import com.moon.server.entity.data.*;
-import com.moon.server.helper.*;
 import org.apache.commons.io.FileUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
+import javax.annotation.Resource;
 import javax.servlet.http.HttpServletRequest;
 import java.io.File;
 import java.sql.Timestamp;
 import java.util.ArrayList;
 import java.util.List;
 
-/**
- * 鏁版嵁涓婁紶鏈嶅姟绫�
- *
- * @author WWW
- */
 @Service
+@SuppressWarnings("ALL")
 public class UploadService extends BaseUploadService implements UploadMapper {
     @Autowired
     UploadMapper uploadMapper;
@@ -41,6 +39,9 @@
 
     @Autowired
     DirService dirService;
+
+    @Resource
+    ReadRasterService readRasterService;
 
     @Override
     public List<CoordEntity> selectCoords(String zoning) {
@@ -62,23 +63,14 @@
         return uploadMapper.selectFmeLog(parentid);
     }
 
-    /**
-     * 鎻掑叆鏂囦欢
-     */
     public void insertFiles(UserEntity ue, List<MetaFileEntity> list, HttpServletRequest req) {
         checkMetaFiles(ue, list);
-        List<MetaFileEntity> xlsList = getExcelFiles(list);
+        readRasterInfo(list);
         loadData(list);
         copyFiles(list);
         insertMetas(list);
-        if (xlsList.size() > 0) {
-            String guid = excelLoader(xlsList, req);
-        }
     }
 
-    /**
-     * 妫�鏌ュ厓鏁版嵁鏂囦欢
-     */
     private void checkMetaFiles(UserEntity ue, List<MetaFileEntity> list) {
         Timestamp createTime = WebHelper.getCurrentTimestamp();
         String tempPath = pathHelper.getConfig().getTempPath();
@@ -94,6 +86,9 @@
             if (!f.exists()) {
                 mf.setMsg("鏂囦欢涓㈠け");
             }
+            if (f.exists() && StringHelper.isNull(mf.getGuid())) {
+                mf.setGuid(FileHelper.getFileMd5(f.getPath()));
+            }
 
             MetaEntity old = metaService.selectByGuid(mf.getGuid(), getDirCode(mf), null);
             if (null != old) {
@@ -102,9 +97,18 @@
         }
     }
 
-    /**
-     * 鑾峰彇鐩綍缂栫爜
-     */
+    private void readRasterInfo(List<MetaFileEntity> list) {
+        for (MetaFileEntity mf : list) {
+            if (null != mf.getMsg()) {
+                continue;
+            }
+
+            if (StaticData.RASTER_EXT.contains("." + mf.getType())) {
+                readRasterService.readRasterInfo(mf, mf.getPath());
+            }
+        }
+    }
+
     private String getDirCode(MetaFileEntity mf) {
         if (StringHelper.isEmpty(mf.getDircode())) {
             return null;
@@ -113,9 +117,6 @@
         return StringHelper.getRightLike(mf.getDircode().substring(0, 2));
     }
 
-    /**
-     * 鑾峰彇Excel鍏冩暟鎹枃浠�
-     */
     private List<MetaFileEntity> getExcelFiles(List<MetaFileEntity> list) {
         List<MetaFileEntity> xlsList = new ArrayList<>();
         for (MetaFileEntity mf : list) {
@@ -137,16 +138,10 @@
         return xlsList;
     }
 
-    /**
-     * 鏄�/鍚︿负Excel
-     */
     private boolean isExcel(MetaFileEntity mf) {
         return StaticData.XLS.equals(mf.getExtName()) || StaticData.XLSX.equals(mf.getExtName());
     }
 
-    /**
-     * 鑾峰彇Xls鐩綍
-     */
     private String getXlsPath(String filePath) {
         String tempPath = pathHelper.getConfig().getTempPath() + File.separator;
         String subPath = filePath.substring(tempPath.length());
@@ -160,9 +155,6 @@
         return subPath;
     }
 
-    /**
-     * 澶嶅埗Xls鏂囦欢
-     */
     private String copyXlsFile(String xlsBasePath, int i, MetaFileEntity mf) {
         try {
             String xlsPath = xlsBasePath + File.separator + i;
@@ -183,9 +175,6 @@
         }
     }
 
-    /**
-     * 鍔犺浇鏁版嵁
-     */
     private void loadData(List<MetaFileEntity> list) {
         for (MetaFileEntity mf : list) {
             if (null != mf.getMsg() || StringHelper.isEmpty(mf.getEntity())) {
@@ -197,9 +186,6 @@
         }
     }
 
-    /**
-     * 鍔犺浇绌洪棿鏁版嵁
-     */
     public void loadSpatialData(MetaFileEntity mf) {
         BasicMapper basicMapper = ClassHelper.getBasicMapper(mf.getEntity());
         if (null == basicMapper) {
@@ -227,16 +213,17 @@
 
         List<?> list;
         if (StaticData.SHP.equals(mf.getExtName())) {
-            list = ShpHelper.readData(clazz, mf.getPath());
+            list = ShpHelper.readData(clazz, mf.getPath(), true);
         } else {
-            list = GdbHelper.readData(clazz, mf.getPath(), mf.getTab());
+            list = GdbHelper.readData(clazz, mf.getPath(), mf.getTab(), true);
         }
         if (null == list || list.isEmpty()) {
             return;
         }
+        mf.setRecords(list.size());
         setCreateInfo(list, mf);
 
-        int rows = basicMapper.insertBatch(list);
+        int rows = batchInserts(basicMapper, list);
         if (rows > 0) {
             mf.setEntity(mf.getTab());
             mf.setTab(tabName);
@@ -244,9 +231,21 @@
         }
     }
 
-    /**
-     * 璁剧疆鍒涘缓淇℃伅
-     */
+    private <T> int batchInserts(BasicMapper basicMapper, List<T> list) {
+        int rows = 0;
+        //int count = (int) Math.ceil(list.size() / StaticData.D100)
+        List<List<T>> subLists = Lists.partition(list, StaticData.I50);
+        for (List<T> sub : subLists) {
+            try {
+                rows += basicMapper.insertBatch(sub);
+            } catch (Exception ex) {
+                log.error(ex);
+            }
+        }
+
+        return rows;
+    }
+
     private <T> void setCreateInfo(List<T> list, MetaFileEntity mf) {
         try {
             if (!(list.get(0) instanceof BaseEntity)) {
@@ -268,9 +267,6 @@
         }
     }
 
-    /**
-     * 澶嶅埗鏂囦欢
-     */
     private void copyFiles(List<MetaFileEntity> list) {
         List<String> gdbList = new ArrayList<>();
         for (MetaFileEntity mf : list) {
@@ -285,6 +281,9 @@
                 case StaticData.JPG:
                     copyMultiFile(mf, StaticData.JPG_EXT);
                     break;
+                case StaticData.JP2:
+                    copyMultiFile(mf, StaticData.JP2_EXT);
+                    break;
                 case StaticData.IMG:
                     copyMultiFile(mf, StaticData.IMG_EXT);
                     break;
@@ -296,6 +295,9 @@
                     break;
                 case StaticData.SHP:
                     copyMultiFile(mf, StaticData.SHP_EXT);
+                    break;
+                case StaticData.OSGB:
+                    copyFolderFile(mf);
                     break;
                 case StaticData.GDB:
                     if (gdbList.contains(mf.getPath())) {
@@ -316,9 +318,6 @@
         }
     }
 
-    /**
-     * 澶嶅埗鍗曚釜鏂囦欢
-     */
     private int copySingleFile(MetaFileEntity mf) {
         File file = new File(mf.getPath());
         if (!file.exists()) {
@@ -351,9 +350,6 @@
         return 1;
     }
 
-    /**
-     * 璁剧疆鏃у厓鏁版嵁淇℃伅
-     */
     private void setOldMeta(MetaFileEntity mf, MetaEntity old) {
         mf.setPath(old.getPath());
         mf.setTab(old.getTab());
@@ -361,9 +357,6 @@
         mf.setEntity(old.getEventid());
     }
 
-    /**
-     * 澶嶅埗澶氫釜鏂囦欢
-     */
     private void copyMultiFile(MetaFileEntity mf, List<String> extList) {
         String path = mf.getPath();
         int status = copySingleFile(mf);
@@ -396,9 +389,6 @@
         }
     }
 
-    /**
-     * 澶嶅埗鏂囦欢澶规枃浠�
-     */
     private void copyFolderFile(MetaFileEntity mf) {
         File file = new File(mf.getPath());
         if (!file.exists()) {
@@ -425,22 +415,15 @@
             return;
         }
 
-        newFile.mkdirs();
-        File[] files = file.listFiles();
-        if (null == files || files.length == 0) {
-            return;
-        }
-
-        for (File f : files) {
-            String subFile = targetPath + File.separator + FileHelper.getFileName(f.getPath());
-            f.renameTo(new File(subFile));
+        try {
+            // newFile.mkdirs()
+            FileUtils.moveDirectory(file, newFile);
+        } catch (Exception ex) {
+            log.error(ex);
         }
         mf.setPath(subPath);
     }
 
-    /**
-     * 鏍规嵁GUID鏌ユ壘璺緞
-     */
     private String findPathByGuid(List<MetaFileEntity> list, MetaFileEntity mf) {
         for (MetaFileEntity meta : list) {
             if (meta.getGuid().equals(mf.getGuid()) && !meta.getPath().equals(mf.getPath())) {
@@ -451,34 +434,35 @@
         return null;
     }
 
-    /**
-     * 鍒涘缓鏂囦欢閾炬帴
-     */
     private void createFileLink(String source, String target) {
         String cmd = String.format("cmd /c mklink \"%s\" \"%s\"", target, source);
         WebHelper.exec(cmd);
     }
 
-    /**
-     * 鎻掑叆鍏冩暟鎹�
-     */
     private void insertMetas(List<MetaFileEntity> list) {
         int metaId = insertParentMeta(list);
+
+        List<String> guids = new ArrayList<>();
         for (MetaFileEntity mf : list) {
             if (null != mf.getMsg()) {
                 continue;
             }
 
             MetaEntity me = createMeta(mf, metaId);
-            metaService.insert(me);
+            if (StaticData.NGDB.equals(me.getType())) {
+                if (guids.contains(me.getGuid())) {
+                    me.setIsmeta((short) -1);
+                } else {
+                    guids.add(me.getGuid());
+                }
+            }
 
-            mf.setMsg(me.getId() > 0 ? "鎴愬姛" : "澶辫触");
+            metaService.insert(me);
+            String err = mf.getRows() < mf.getRecords() ? "(" + (mf.getRecords() - mf.getRows()) + " 鏉″け璐�)" : "";
+            mf.setMsg(me.getId() > 0 ? String.format("鎴愬姛%s", err) : "澶辫触");
         }
     }
 
-    /**
-     * 鍒涘缓鍏冩暟鎹�
-     */
     private MetaEntity createMeta(MetaFileEntity mf, int metaId) {
         MetaEntity me = new MetaEntity();
         me.setMetaid(metaId);
@@ -500,13 +484,23 @@
         me.setRows(mf.getRows());
         me.setCreateUser(mf.getCreateUser());
         me.setCreateTime(mf.getCreateTime());
+        // 鏍呮牸淇℃伅
+        me.setGeom(StringHelper.isEmpty(mf.getGeom()) ? "null" : mf.getGeom());
+        me.setSensortype(mf.getSensortype());
+        me.setAcqTime(mf.getAcqTime());
+        me.setResolution(mf.getResolution());
+        me.setGridsize(mf.getGridsize());
+        me.setCoorSys(mf.getCoorSys());
+        me.setEpsg(mf.getEpsg());
+        me.sethDatum(mf.gethDatum());
+        me.setMataType(mf.getMataType());
+        me.setBands(mf.getBands());
+        me.setBandType(mf.getBandType());
+        me.setCt(mf.getCt());
 
         return me;
     }
 
-    /**
-     * 鎻掑叆鐖跺厓鏁版嵁
-     */
     private int insertParentMeta(List<MetaFileEntity> list) {
         for (MetaFileEntity mf : list) {
             if (null != mf.getMsg() || !mf.getIsMeta()) {
@@ -524,9 +518,6 @@
         return 0;
     }
 
-    /**
-     * Excel鍏ュ簱
-     */
     private String excelLoader(List<MetaFileEntity> xlsList, HttpServletRequest req) {
         try {
             MetaFileEntity xlsMeta = getExcelMeta(xlsList);
@@ -540,9 +531,6 @@
         return null;
     }
 
-    /**
-     * 鑾峰彇Excel鐨勫厓鏁版嵁
-     */
     private MetaFileEntity getExcelMeta(List<MetaFileEntity> xlsList) {
         if (null == xlsList || xlsList.isEmpty()) {
             return null;

--
Gitblit v1.9.3