From ac20dc99bf1f463365dba071973e08fffbd294b4 Mon Sep 17 00:00:00 2001 From: 13693261870 <252740454@qq.com> Date: 星期五, 14 七月 2023 14:12:21 +0800 Subject: [PATCH] 1 --- src/main/java/com/lf/server/service/data/UploadService.java | 360 +++++++++++++++++++++++++++++++++++++++-------------------- 1 files changed, 236 insertions(+), 124 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 af502c7..6d1668b 100644 --- a/src/main/java/com/lf/server/service/data/UploadService.java +++ b/src/main/java/com/lf/server/service/data/UploadService.java @@ -1,5 +1,6 @@ package com.lf.server.service.data; +import com.google.common.collect.Lists; import com.lf.server.entity.all.BaseEntity; import com.lf.server.entity.all.StaticData; import com.lf.server.entity.data.*; @@ -9,6 +10,7 @@ import com.lf.server.mapper.data.UploadMapper; import com.lf.server.service.all.BaseQueryService; import com.lf.server.service.all.BaseUploadService; +import com.lf.server.service.sys.DepService; import org.apache.commons.io.FileUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -21,6 +23,7 @@ /** * 鏁版嵁涓婁紶鏈嶅姟绫� + * * @author WWW */ @Service @@ -33,6 +36,12 @@ @Autowired MetaService metaService; + + @Autowired + DepService depService; + + @Autowired + DirService dirService; @Override public List<CoordEntity> selectCoords(String zoning) { @@ -57,11 +66,15 @@ /** * 鎻掑叆鏂囦欢 */ - public List<MetaEntity> insertFiles(UserEntity ue, List<MetaFileEntity> list, HttpServletRequest req) { + public void insertFiles(UserEntity ue, List<MetaFileEntity> list, HttpServletRequest req) { checkMetaFiles(ue, list); - excelLoader(list, req); - - return dataLoader(list); + List<MetaFileEntity> xlsList = getExcelFiles(list); + loadData(list); + copyFiles(list); + insertMetas(list); + if (xlsList.size() > 0) { + String guid = excelLoader(xlsList, req); + } } /** @@ -74,25 +87,101 @@ for (MetaFileEntity mf : list) { mf.setCreateUser(ue.getId()); mf.setCreateTime(createTime); - mf.setDepid(ue.getDepid()); + mf.setDepcode(ue.getDepcode()); mf.setPath(tempPath + File.separator + mf.getPath()); + mf.setMsg(null); File f = new File(mf.getPath()); if (!f.exists()) { mf.setMsg("鏂囦欢涓㈠け"); - mf.setRows(-1); + } + + MetaEntity old = metaService.selectByGuid(mf.getGuid(), getDirCode(mf), null); + if (null != old) { + mf.setMsg("宸插瓨鍦�"); } } } /** - * 鍔犺浇鏁版嵁 + * 鑾峰彇鐩綍缂栫爜 */ - private List<MetaEntity> dataLoader(List<MetaFileEntity> list) { - loadData(list); - copyFiles(list); + private String getDirCode(MetaFileEntity mf) { + if (StringHelper.isEmpty(mf.getDircode())) { + return null; + } - return insertMetas(list); + 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) { + if (null == mf.getMsg() && isExcel(mf)) { + xlsList.add(mf); + } + } + if (xlsList.isEmpty()) { + return xlsList; + } + + String xlsBasePath = getXlsPath(xlsList.get(0).getPath()); + for (int i = 0, c = xlsList.size(); i < c; i++) { + MetaFileEntity mf = xlsList.get(i); + String xlsPath = copyXlsFile(xlsBasePath, i, mf); + mf.setXlsPath(xlsPath); + } + + 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()); + 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; + } } /** @@ -100,7 +189,7 @@ */ private void loadData(List<MetaFileEntity> list) { for (MetaFileEntity mf : list) { - if (StringHelper.isEmpty(mf.getEntity())) { + if (null != mf.getMsg() || StringHelper.isEmpty(mf.getEntity())) { continue; } if (StaticData.SHP.equals(mf.getExtName()) || StaticData.GDB.equals(mf.getExtName())) { @@ -112,7 +201,7 @@ /** * 鍔犺浇绌洪棿鏁版嵁 */ - private void loadSpatialData(MetaFileEntity mf) { + public void loadSpatialData(MetaFileEntity mf) { BasicMapper basicMapper = ClassHelper.getBasicMapper(mf.getEntity()); if (null == basicMapper) { return; @@ -125,22 +214,54 @@ return; } - List<?> list = null; + File file = new File(mf.getPath()); + if (!file.exists()) { + mf.setMsg("鏂囦欢涓㈠け"); + return; + } + + MetaEntity old = metaService.selectByGuid(mf.getGuid(), getDirCode(mf), tabName); + if (null != old) { + mf.setMsg("宸插瓨鍦�"); + return; + } + + 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); mf.setRows(rows); } + } + + /** + * 鎵归噺鎻掑叆 + */ + 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; } /** @@ -156,8 +277,8 @@ BaseEntity be = (BaseEntity) t; be.setEventid(StringHelper.getGuid()); be.setParentid(mf.getEventid()); - be.setDirid(mf.getDirid()); - be.setDepid(mf.getDepid()); + be.setDirid(mf.getDircode()); + be.setDepid(mf.getDepcode()); be.setVerid(mf.getVerid()); be.setCreateuser(mf.getCreateUser()); be.setCreatetime(mf.getCreateTime()); @@ -173,9 +294,16 @@ private void copyFiles(List<MetaFileEntity> list) { List<String> gdbList = new ArrayList<>(); for (MetaFileEntity mf : list) { + if (null != mf.getMsg()) { + continue; + } + switch (mf.getExtName()) { case StaticData.MPT: copyMultiFile(mf, StaticData.MPT_EXT); + break; + case StaticData.JPG: + copyMultiFile(mf, StaticData.JPG_EXT); break; case StaticData.IMG: copyMultiFile(mf, StaticData.IMG_EXT); @@ -189,13 +317,16 @@ case StaticData.SHP: copyMultiFile(mf, StaticData.SHP_EXT); break; + case StaticData.OSGB: + copyFolderFile(mf); + break; case StaticData.GDB: if (gdbList.contains(mf.getPath())) { String path = findPathByGuid(list, mf); if (null != path) { mf.setPath(path); + continue; } - continue; } gdbList.add(mf.getPath()); @@ -218,9 +349,10 @@ return -1; } - MetaEntity old = metaService.selectByGuid(mf.getGuid()); + MetaEntity old = metaService.selectByGuid(mf.getGuid(), null, null); if (null != old) { - mf.setPath(old.getPath()); + // mf.setMsg("宸插瓨鍦�") + setOldMeta(mf, old); file.delete(); return 0; } @@ -243,11 +375,20 @@ } /** + * 璁剧疆鏃у厓鏁版嵁淇℃伅 + */ + private void setOldMeta(MetaFileEntity mf, MetaEntity old) { + mf.setPath(old.getPath()); + mf.setTab(old.getTab()); + mf.setRows(old.getRows()); + mf.setEntity(old.getEventid()); + } + + /** * 澶嶅埗澶氫釜鏂囦欢 */ private void copyMultiFile(MetaFileEntity mf, List<String> extList) { String path = mf.getPath(); - int status = copySingleFile(mf); if (status < 1) { for (int i = 0, c = extList.size(); i < c; i++) { @@ -288,9 +429,10 @@ return; } - MetaEntity old = metaService.selectByGuid(mf.getGuid()); + MetaEntity old = metaService.selectByGuid(mf.getGuid(), null, null); if (null != old) { - mf.setPath(old.getPath()); + // mf.setMsg("宸插瓨鍦�") + setOldMeta(mf, old); FileHelper.deleteFiles(file); return; } @@ -306,15 +448,11 @@ 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); } @@ -324,7 +462,7 @@ */ private String findPathByGuid(List<MetaFileEntity> list, MetaFileEntity mf) { for (MetaFileEntity meta : list) { - if (meta.getGuid().equals(mf.getGuid())) { + if (meta.getGuid().equals(mf.getGuid()) && !meta.getPath().equals(mf.getPath())) { return meta.getPath(); } } @@ -333,32 +471,39 @@ } /** + * 鍒涘缓鏂囦欢閾炬帴 + */ + private void createFileLink(String source, String target) { + String cmd = String.format("cmd /c mklink \"%s\" \"%s\"", target, source); + WebHelper.exec(cmd); + } + + /** * 鎻掑叆鍏冩暟鎹� */ - private List<MetaEntity> insertMetas(List<MetaFileEntity> list) { - List<MetaEntity> metas = new ArrayList<>(); + private void insertMetas(List<MetaFileEntity> list) { + int metaId = insertParentMeta(list); for (MetaFileEntity mf : list) { - if (StringHelper.isEmpty(mf.getMsg())) { - metas.add(createMeta(mf)); + if (null != mf.getMsg()) { + continue; } - } - if (metas.isEmpty()) { - return null; - } - int rows = metaService.inserts(metas); - - return rows > 0 ? metas : null; + MetaEntity me = createMeta(mf, metaId); + 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) { + private MetaEntity createMeta(MetaFileEntity mf, int metaId) { MetaEntity me = new MetaEntity(); + me.setMetaid(metaId); me.setEventid(mf.getEventid()); - me.setDirid(mf.getDirid()); - me.setDepid(mf.getDepid()); + me.setDircode(mf.getDircode()); + me.setDepcode(mf.getDepcode()); me.setVerid(mf.getVerid()); me.setName(mf.getName()); me.setType(mf.getType()); @@ -368,6 +513,9 @@ if (mf.getRows() > 0) { me.setTab(mf.getTab()); } + if (!StringHelper.isEmpty(mf.getEntity())) { + me.setLayer(mf.getEntity()); + } me.setRows(mf.getRows()); me.setCreateUser(mf.getCreateUser()); me.setCreateTime(mf.getCreateTime()); @@ -376,108 +524,72 @@ } /** - * Excel鍏ュ簱 + * 鎻掑叆鐖跺厓鏁版嵁 */ - private String excelLoader(List<MetaFileEntity> list, HttpServletRequest req) { - List<MetaFileEntity> xlsList = getExcelFiles(list); - if (xlsList.isEmpty()) { - return ""; + private int insertParentMeta(List<MetaFileEntity> list) { + for (MetaFileEntity mf : list) { + if (null != mf.getMsg() || !mf.getIsMeta()) { + continue; + } + + MetaEntity me = createMeta(mf, 0); + me.setIsmeta((short) 1); + metaService.insert(me); + mf.setMsg(me.getId() > 0 ? "鎴愬姛" : "澶辫触"); + + return me.getId(); } - String guid = null; + return 0; + } + + /** + * Excel鍏ュ簱 + */ + private String excelLoader(List<MetaFileEntity> xlsList, HttpServletRequest req) { try { - MetaFileEntity meta = getExcelMeta(xlsList); - guid = fmeService.excelLoader(meta, req); + MetaFileEntity xlsMeta = getExcelMeta(xlsList); + if (null != xlsMeta) { + return fmeService.excelLoader(xlsMeta, req); + } } catch (Exception ex) { log.error(ex.getMessage(), ex); } - return guid; - } - - /** - * 鑾峰彇Excel鍏冩暟鎹枃浠� - */ - private List<MetaFileEntity> getExcelFiles(List<MetaFileEntity> list) { - List<MetaFileEntity> xlsList = new ArrayList<>(); - for (MetaFileEntity mf : list) { - boolean isXls = StaticData.XLS.equals(mf.getExtName()) || StaticData.XLSX.equals(mf.getExtName()); - if (mf.getRows() > -1 && isXls) { - xlsList.add(mf); - } - } - - return xlsList; + return null; } /** * 鑾峰彇Excel鐨勫厓鏁版嵁 */ private MetaFileEntity getExcelMeta(List<MetaFileEntity> xlsList) { + if (null == xlsList || xlsList.isEmpty()) { + return null; + } + List<String> pathList = new ArrayList<>(); List<String> dirList = new ArrayList<>(); List<String> pidList = new ArrayList<>(); - 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() + ""); + if ("鎴愬姛".equals(mf.getMsg()) && null != mf.getXlsPath()) { + pathList.add(mf.getXlsPath()); + dirList.add(mf.getDircode()); pidList.add(mf.getEventid()); } } + MetaFileEntity first = xlsList.get(0); MetaFileEntity meta = new MetaFileEntity(); meta.setPath(StringHelper.join(pathList, ",")); - meta.setEpsgCode(xlsList.get(0).getEpsgCode()); - meta.setName(StringHelper.join(dirList, ",")); - meta.setDepid(xlsList.get(0).getDepid()); - meta.setVerid(xlsList.get(0).getVerid()); - meta.setCreateUser(xlsList.get(0).getCreateUser()); - meta.setEventid(StringHelper.join(pidList, ",")); + meta.setEpsgCode(first.getEpsgCode()); + meta.setName(StringHelper.join(dirList, ";")); + meta.setDepcode(first.getDepcode()); + meta.setVerid(first.getVerid()); + meta.setCreateUser(first.getCreateUser()); + meta.setEventid(StringHelper.join(pidList, ";")); 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