From ed8c7a5effd0d423ce1118b680ecdca6fe732609 Mon Sep 17 00:00:00 2001 From: 13693261870 <252740454@qq.com> Date: 星期三, 02 七月 2025 16:43:13 +0800 Subject: [PATCH] Merge branch 'master' of http://192.168.11.205:9000/r/P2022036_Service --- src/main/java/com/lf/server/service/data/UploadService.java | 743 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 739 insertions(+), 4 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 eb001b4..336ea2d 100644 --- a/src/main/java/com/lf/server/service/data/UploadService.java +++ b/src/main/java/com/lf/server/service/data/UploadService.java @@ -1,26 +1,60 @@ package com.lf.server.service.data; -import com.lf.server.entity.data.CoordEntity; -import com.lf.server.entity.data.DirEntity; -import com.lf.server.entity.data.FmeLogEntity; +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.*; +import com.lf.server.entity.sys.UserEntity; +import com.lf.server.helper.*; +import com.lf.server.mapper.all.BasicMapper; 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; +import javax.servlet.http.HttpServletRequest; +import java.io.File; +import java.lang.reflect.Field; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.sql.Timestamp; +import java.util.ArrayList; import java.util.List; /** * 鏁版嵁涓婁紶鏈嶅姟绫� + * * @author WWW */ @Service -public class UploadService implements UploadMapper { +public class UploadService extends BaseUploadService implements UploadMapper { @Autowired UploadMapper uploadMapper; + + @Autowired + FmeService fmeService; + + @Autowired + MetaService metaService; + + @Autowired + DepService depService; + + @Autowired + DirService dirService; @Override public List<CoordEntity> selectCoords(String zoning) { return uploadMapper.selectCoords(zoning); + } + + @Override + public Integer selectCount4Coord(String epsgCode) { + return uploadMapper.selectCount4Coord(epsgCode); } @Override @@ -32,4 +66,705 @@ public List<FmeLogEntity> selectFmeLog(String parentid) { return uploadMapper.selectFmeLog(parentid); } + + /** + * 鎻掑叆鏂囦欢 + */ + public void insertFiles(UserEntity ue, List<MetaFileEntity> list, HttpServletRequest req) { + checkMetaFiles(ue, list); + List<MetaFileEntity> xlsList = getExcelFiles(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(); + + for (MetaFileEntity mf : list) { + mf.setCreateUser(ue.getId()); + mf.setCreateTime(createTime); + 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("鏂囦欢涓㈠け"); + } + 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) { + mf.setMsg("宸插瓨鍦�"); + } + } + } + + /** + * 鑾峰彇鐩綍缂栫爜 + */ + private String getDirCode(MetaFileEntity mf) { + if (StringHelper.isEmpty(mf.getDircode())) { + return null; + } + + 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; + } + } + + /** + * 鍔犺浇鏁版嵁 + */ + private void loadData(List<MetaFileEntity> list) { + for (MetaFileEntity mf : list) { + if (null != mf.getMsg() || StringHelper.isEmpty(mf.getEntity())) { + continue; + } + if (StaticData.SHP.equals(mf.getExtName()) || StaticData.GDB.equals(mf.getExtName())) { + loadSpatialData(mf); + } + } + } + + /** + * 鍔犺浇绌洪棿鏁版嵁 + */ + public void loadSpatialData(MetaFileEntity mf) { + BasicMapper basicMapper = ClassHelper.getBasicMapper(mf.getEntity()); + if (null == basicMapper) { + return; + } + + String tabName = BaseQueryService.getTabName(basicMapper); + String className = ClassHelper.getClassName(basicMapper); + Class clazz = ClassHelper.getEntityClass(className); + if (null == clazz || null == tabName) { + return; + } + + 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(), true); + } else { + list = GdbHelper.readData(clazz, mf.getPath(), mf.getTab(), true); + } + if (null == list || list.isEmpty()) { + return; + } + mf.setRecords(list.size()); + setCreateInfo(list, mf); + + 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; + } + + /** + * 璁剧疆鍒涘缓淇℃伅 + */ + private <T> void setCreateInfo(List<T> list, MetaFileEntity mf) { + try { + if (!(list.get(0) instanceof BaseEntity)) { + return; + } + + for (T t : list) { + BaseEntity be = (BaseEntity) t; + be.setEventid(StringHelper.getGuid()); + be.setParentid(mf.getEventid()); + be.setDirid(mf.getDircode()); + be.setDepid(mf.getDepcode()); + be.setVerid(mf.getVerid()); + be.setCreateuser(mf.getCreateUser()); + be.setCreatetime(mf.getCreateTime()); + } + } catch (Exception ex) { + log.error(ex.getMessage(), ex); + } + } + + /** + * 澶嶅埗鏂囦欢 + */ + 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); + break; + case StaticData.TIF: + copyMultiFile(mf, StaticData.TIF_EXT); + break; + case StaticData.TIFF: + copyMultiFile(mf, StaticData.TIFF_EXT); + break; + 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; + } + } + + gdbList.add(mf.getPath()); + copyFolderFile(mf); + break; + default: + copySingleFile(mf); + break; + } + } + } + + /** + * 澶嶅埗鍗曚釜鏂囦欢 + */ + private int copySingleFile(MetaFileEntity mf) { + File file = new File(mf.getPath()); + if (!file.exists()) { + mf.setMsg("鏂囦欢涓㈠け"); + return -1; + } + + MetaEntity old = metaService.selectByGuid(mf.getGuid(), null, null); + if (null != old) { + // mf.setMsg("宸插瓨鍦�") + setOldMeta(mf, old); + 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); + mf.setPath(subPath); + + return 1; + } + + /** + * 璁剧疆鏃у厓鏁版嵁淇℃伅 + */ + 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++) { + String subPath = path.replace(mf.getExtName(), extList.get(i)); + File file = new File(subPath); + if (file.exists()) { + file.delete(); + } + } + return; + } + + String uploadPath = pathHelper.getConfig().getUploadPath(); + for (int i = 0, c = extList.size(); i < c; i++) { + String sourcePath = path.replace(mf.getExtName(), extList.get(i)); + File file = new File(sourcePath); + if (!file.exists()) { + continue; + } + + String targetPath = uploadPath + File.separator + mf.getPath().replace(mf.getExtName(), extList.get(i)); + File newFile = new File(targetPath); + if (newFile.exists()) { + continue; + } + + file.renameTo(newFile); + } + } + + /** + * 澶嶅埗鏂囦欢澶规枃浠� + */ + private void copyFolderFile(MetaFileEntity mf) { + File file = new File(mf.getPath()); + if (!file.exists()) { + mf.setMsg("鏂囦欢涓㈠け"); + return; + } + + MetaEntity old = metaService.selectByGuid(mf.getGuid(), null, null); + if (null != old) { + // mf.setMsg("宸插瓨鍦�") + setOldMeta(mf, old); + 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; + } + + 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())) { + return meta.getPath(); + } + } + + 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); + if (StaticData.NGDB.equals(me.getType())) { + if (guids.contains(me.getGuid())) { + me.setIsmeta((short) -1); + } else { + guids.add(me.getGuid()); + } + } + + 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); + me.setEventid(mf.getEventid()); + me.setDircode(mf.getDircode()); + me.setDepcode(mf.getDepcode()); + 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()); + } + if (!StringHelper.isEmpty(mf.getEntity())) { + me.setLayer(mf.getEntity()); + } + me.setRows(mf.getRows()); + me.setCreateUser(mf.getCreateUser()); + me.setCreateTime(mf.getCreateTime()); + + return me; + } + + /** + * 鎻掑叆鐖跺厓鏁版嵁 + */ + 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(); + } + + return 0; + } + + /** + * Excel鍏ュ簱 + */ + private String excelLoader(List<MetaFileEntity> xlsList, HttpServletRequest req) { + try { + MetaFileEntity xlsMeta = getExcelMeta(xlsList); + if (null != xlsMeta) { + return fmeService.excelLoader(xlsMeta, req); + } + } catch (Exception ex) { + log.error(ex.getMessage(), ex); + } + + 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<>(); + + for (int i = 0, c = xlsList.size(); i < c; i++) { + MetaFileEntity mf = xlsList.get(i); + 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(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; + } + + /** + * 鎻掑叆KML鏂囦欢 + */ + public void insertKml(UserEntity ue, List<MetaFileEntity> list) { + checkMetaFiles(ue, list); + loadKml(list); + copyFiles(list); + insertMetas(list); + } + + /** + * 鍔犺浇Kml + */ + private void loadKml(List<MetaFileEntity> list) { + for (MetaFileEntity mf : list) { + if (null != mf.getMsg() || StringHelper.isEmpty(mf.getEntity()) || !StaticData.KML.equals(mf.getExtName())) { + continue; + } + + String wkt = readKml(mf.getPath()); + if (StringHelper.isEmpty(wkt)) { + continue; + } + + loadKmlData(mf, wkt); + } + } + + /** + * 璇诲彇KML + */ + private String readKml(String path) { + try { + List<String> list = Files.readAllLines(Paths.get(path), StandardCharsets.UTF_8); + for (String str : list) { + if (!str.contains("<coordinates>")) { + continue; + } + + return getWktFromCoordinates(str); + } + + return null; + } catch (Exception ex) { + log.error(ex.getMessage(), ex); + return null; + } + } + + /** + * 鑾峰彇KWT + */ + private String getWktFromCoordinates(String coords) { + String[] strs = coords.replace("\t", "").replace("<coordinates>", "").replace("</coordinates>", "").split(" "); + if (strs.length == 0) { + return null; + } + + // MULTILINESTRING((108.99843373100003 34.736292580000054,108.99818970700005 34.73764945000005,108.99831780400007 34.738880547000065,108.99812184300004 34.739564029000064)) + StringBuilder sb = new StringBuilder(); + sb.append("MULTILINESTRING(("); + for (String str : strs) { + if (StringHelper.isEmpty(str)) { + continue; + } + + String[] vals = str.split(StaticData.COMMA); + sb.append(vals[0]).append(" ").append(vals[1]).append(StaticData.COMMA); + } + sb.deleteCharAt(sb.length() - 1); + sb.append("))"); + + return sb.toString(); + } + + /** + * 鍔犺浇Kml鏁版嵁 + */ + private void loadKmlData(MetaFileEntity mf, String wkt) { + BasicMapper basicMapper = ClassHelper.getBasicMapper(mf.getEntity()); + if (null == basicMapper) { + return; + } + + String tabName = BaseQueryService.getTabName(basicMapper); + String className = ClassHelper.getClassName(basicMapper); + Class clazz = ClassHelper.getEntityClass(className); + if (null == clazz || null == tabName) { + return; + } + + String stage = getPipeStage(tabName); + String name = FileHelper.getName(mf.getPath()); + List<?> list = createKmlEntity(clazz, wkt, name, mf, stage); + if (null == list || list.isEmpty()) { + return; + } + mf.setRecords(list.size()); + setCreateInfo(list, mf); + + int rows = batchInserts(basicMapper, list); + if (rows > 0) { + mf.setEntity(mf.getTab()); + mf.setTab(tabName); + mf.setRows(rows); + } + } + + /** + * 鍒涘缓KML瀹炰綋绫� + */ + private <T> List<T> createKmlEntity(Class clazz, String wkt, String name, MetaFileEntity mf, String stage) { + try { + T t = (T) clazz.newInstance(); + + setEntityVal(clazz.getSuperclass(), t, "geom", null == wkt ? "null" : String.format("ST_GeomFromText('%s')", wkt)); + setEntityVal(clazz, t, "pipename", name); + setEntityVal(clazz, t, "projname", name); + setEntityVal(clazz, t, "medium", mf.getMedium()); + setEntityVal(clazz, t, "pipestage", stage); + + List<T> list = new ArrayList<>(); + list.add(t); + + return list; + } catch (Exception ex) { + log.error(ex.getMessage(), ex); + return null; + } + } + + /** + * 璁剧疆瀹炰綋鍊� + */ + private <T> void setEntityVal(Class clazz, T t, String field, String val) throws Exception { + Field pField = clazz.getDeclaredField(field); + pField.setAccessible(true); + pField.set(t, val); + } + + /** + * 鑾峰彇绠¢亾闃舵 + */ + private String getPipeStage(String tab) { + switch (tab) { + case "si.pl_pipeline_f": + return "鍙爺闃舵"; + case "si.pl_pipeline_b": + return "鍒濊闃舵"; + case "si.pl_pipeline_d": + return "鏂藉伐鍥鹃樁娈�"; + case "si.pl_pipeline_a": + return "绔e伐鍥鹃樁娈�"; + default: + return null; + } + } } -- Gitblit v1.9.3