From c509bd8047cfd582c59bba66d148b236e45d038d Mon Sep 17 00:00:00 2001 From: 13693261870 <252740454@qq.com> Date: 星期四, 12 一月 2023 10:15:15 +0800 Subject: [PATCH] 1 --- src/main/java/com/lf/server/service/data/UploadService.java | 146 ++++++++++++++++++++++++++++++++++++++++-------- 1 files changed, 122 insertions(+), 24 deletions(-) diff --git a/src/main/java/com/lf/server/service/data/UploadService.java b/src/main/java/com/lf/server/service/data/UploadService.java index 874160e..af502c7 100644 --- a/src/main/java/com/lf/server/service/data/UploadService.java +++ b/src/main/java/com/lf/server/service/data/UploadService.java @@ -9,6 +9,7 @@ import com.lf.server.mapper.data.UploadMapper; import com.lf.server.service.all.BaseQueryService; import com.lf.server.service.all.BaseUploadService; +import org.apache.commons.io.FileUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -56,10 +57,11 @@ /** * 鎻掑叆鏂囦欢 */ - public void insertFiles(UserEntity ue, List<MetaFileEntity> list, HttpServletRequest req) { + public List<MetaEntity> insertFiles(UserEntity ue, List<MetaFileEntity> list, HttpServletRequest req) { checkMetaFiles(ue, list); - dataLoader(list); excelLoader(list, req); + + return dataLoader(list); } /** @@ -71,7 +73,7 @@ for (MetaFileEntity mf : list) { mf.setCreateUser(ue.getId()); - mf.setCreatetime(createTime); + mf.setCreateTime(createTime); mf.setDepid(ue.getDepid()); mf.setPath(tempPath + File.separator + mf.getPath()); @@ -86,10 +88,11 @@ /** * 鍔犺浇鏁版嵁 */ - private void dataLoader(List<MetaFileEntity> list) { + private List<MetaEntity> dataLoader(List<MetaFileEntity> list) { loadData(list); copyFiles(list); - insertMetas(list); + + return insertMetas(list); } /** @@ -97,6 +100,9 @@ */ private void loadData(List<MetaFileEntity> list) { for (MetaFileEntity mf : list) { + if (StringHelper.isEmpty(mf.getEntity())) { + continue; + } if (StaticData.SHP.equals(mf.getExtName()) || StaticData.GDB.equals(mf.getExtName())) { loadSpatialData(mf); } @@ -123,7 +129,7 @@ if (StaticData.SHP.equals(mf.getExtName())) { list = ShpHelper.readData(clazz, mf.getPath()); } else { - GdbHelper.readData(clazz, mf.getPath(), mf.getTab()); + list = GdbHelper.readData(clazz, mf.getPath(), mf.getTab()); } if (null == list || list.isEmpty()) { return; @@ -154,7 +160,7 @@ be.setDepid(mf.getDepid()); be.setVerid(mf.getVerid()); be.setCreateuser(mf.getCreateUser()); - be.setCreatetime(mf.getCreatetime()); + be.setCreatetime(mf.getCreateTime()); } } catch (Exception ex) { log.error(ex.getMessage(), ex); @@ -185,9 +191,9 @@ break; case StaticData.GDB: if (gdbList.contains(mf.getPath())) { - MetaEntity meta = metaService.selectByGuid(mf.getGuid()); - if (null != meta) { - mf.setPath(meta.getPath()); + String path = findPathByGuid(list, mf); + if (null != path) { + mf.setPath(path); } continue; } @@ -214,23 +220,23 @@ MetaEntity old = metaService.selectByGuid(mf.getGuid()); if (null != old) { - mf.setMsg("鏂囦欢宸插瓨鍦�"); + mf.setPath(old.getPath()); file.delete(); return 0; } String uploadPath = pathHelper.getUploadFullPath(); String targetPath = uploadPath + File.separator + mf.getGuid() + mf.getExtName(); + String subPath = FileHelper.getRelativePath(targetPath); File newFile = new File(targetPath); if (newFile.exists()) { + mf.setPath(subPath); file.delete(); return 0; } file.renameTo(newFile); - - String subPath = FileHelper.getRelativePath(targetPath); mf.setPath(subPath); return 1; @@ -284,16 +290,18 @@ MetaEntity old = metaService.selectByGuid(mf.getGuid()); if (null != old) { - mf.setMsg("鏂囦欢宸插瓨鍦�"); + mf.setPath(old.getPath()); FileHelper.deleteFiles(file); return; } String uploadPath = pathHelper.getUploadFullPath(); String targetPath = uploadPath + File.separator + mf.getGuid() + mf.getExtName(); + String subPath = FileHelper.getRelativePath(targetPath); File newFile = new File(targetPath); if (newFile.exists() && newFile.isDirectory()) { + mf.setPath(subPath); FileHelper.deleteFiles(file); return; } @@ -305,21 +313,66 @@ } for (File f : files) { - String subPath = targetPath + File.separator + FileHelper.getFileName(f.getPath()); - f.renameTo(new File(subPath)); + String subFile = targetPath + File.separator + FileHelper.getFileName(f.getPath()); + f.renameTo(new File(subFile)); + } + mf.setPath(subPath); + } + + /** + * 鏍规嵁GUID鏌ユ壘璺緞 + */ + private String findPathByGuid(List<MetaFileEntity> list, MetaFileEntity mf) { + for (MetaFileEntity meta : list) { + if (meta.getGuid().equals(mf.getGuid())) { + return meta.getPath(); + } } - String subPath = FileHelper.getRelativePath(targetPath); - mf.setPath(subPath); + return null; } /** * 鎻掑叆鍏冩暟鎹� */ - private void insertMetas(List<MetaFileEntity> list) { + private List<MetaEntity> insertMetas(List<MetaFileEntity> list) { + List<MetaEntity> metas = new ArrayList<>(); for (MetaFileEntity mf : list) { - // + if (StringHelper.isEmpty(mf.getMsg())) { + metas.add(createMeta(mf)); + } } + if (metas.isEmpty()) { + return null; + } + + int rows = metaService.inserts(metas); + + return rows > 0 ? metas : null; + } + + /** + * 鍒涘缓鍏冩暟鎹� + */ + private MetaEntity createMeta(MetaFileEntity mf) { + MetaEntity me = new MetaEntity(); + me.setEventid(mf.getEventid()); + me.setDirid(mf.getDirid()); + me.setDepid(mf.getDepid()); + me.setVerid(mf.getVerid()); + me.setName(mf.getName()); + me.setType(mf.getType()); + me.setGuid(mf.getGuid()); + me.setPath(mf.getPath()); + me.setSizes(mf.getSizes()); + if (mf.getRows() > 0) { + me.setTab(mf.getTab()); + } + me.setRows(mf.getRows()); + me.setCreateUser(mf.getCreateUser()); + me.setCreateTime(mf.getCreateTime()); + + return me; } /** @@ -365,10 +418,16 @@ List<String> dirList = new ArrayList<>(); List<String> pidList = new ArrayList<>(); - for (MetaFileEntity mf : xlsList) { - pathList.add(mf.getPath()); - dirList.add(mf.getDirid() + ""); - pidList.add(mf.getEventid()); + String xlsBasePath = getXlsPath(xlsList.get(0).getPath()); + for (int i = 0, c = xlsList.size(); i < c; i++) { + MetaFileEntity mf = xlsList.get(i); + + String path = copyXlsFile(xlsBasePath, i, mf); + if (null != path) { + pathList.add(path); + dirList.add(mf.getDirid() + ""); + pidList.add(mf.getEventid()); + } } MetaFileEntity meta = new MetaFileEntity(); @@ -382,4 +441,43 @@ return meta; } + + /** + * 鑾峰彇Xls鐩綍 + */ + private String getXlsPath(String filePath) { + String tempPath = pathHelper.getConfig().getTempPath() + File.separator; + String subPath = filePath.substring(tempPath.length()); + subPath = tempPath + subPath.substring(0, subPath.indexOf(File.separator)).replace("_zip", "") + "_xls"; + + File f = new File(subPath); + if (!f.exists() || !f.isDirectory()) { + f.mkdirs(); + } + + return subPath; + } + + /** + * 澶嶅埗Xls鏂囦欢 + */ + private String copyXlsFile(String xlsBasePath, int i, MetaFileEntity mf) { + try { + String xlsPath = xlsBasePath + File.separator + i; + + File file = new File(xlsPath); + if (!file.exists() || !file.isDirectory()) { + file.mkdirs(); + } + file = new File(mf.getPath()); + + File newFile = new File(xlsPath + File.separator + FileHelper.getFileName(file.getPath())); + FileUtils.copyFile(file, newFile); + + return newFile.getPath(); + } catch (Exception ex) { + log.error(ex.getMessage(), ex); + return null; + } + } } -- Gitblit v1.9.3