From 0f58d77f1df46e82dad00b23f245abdcd6cb8a2c Mon Sep 17 00:00:00 2001 From: 13693261870 <252740454@qq.com> Date: 星期一, 20 二月 2023 18:49:18 +0800 Subject: [PATCH] 1 --- src/main/java/com/lf/server/service/show/DataLibService.java | 329 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 328 insertions(+), 1 deletions(-) diff --git a/src/main/java/com/lf/server/service/show/DataLibService.java b/src/main/java/com/lf/server/service/show/DataLibService.java index 0e3c79e..9fca96d 100644 --- a/src/main/java/com/lf/server/service/show/DataLibService.java +++ b/src/main/java/com/lf/server/service/show/DataLibService.java @@ -1,9 +1,23 @@ package com.lf.server.service.show; -import com.lf.server.helper.PathHelper; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.lf.server.entity.data.DownloadEntity; +import com.lf.server.entity.sys.UserEntity; +import com.lf.server.helper.*; +import com.lf.server.mapper.all.BasicMapper; +import com.lf.server.mapper.all.GeomBaseMapper; +import com.lf.server.mapper.data.DownloadMapper; +import com.lf.server.service.all.BaseQueryService; import com.lf.server.service.data.DownloadService; +import net.lingala.zip4j.ZipFile; +import net.lingala.zip4j.model.ZipParameters; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; + +import java.io.File; +import java.util.*; /** * 璧勬枡棣� @@ -15,5 +29,318 @@ PathHelper pathHelper; @Autowired + DownloadMapper downloadMapper; + + @Autowired DownloadService downloadService; + + @Autowired + BaseQueryService baseQueryService; + + private final static Log log = LogFactory.getLog(DataLibService.class); + + /** + * 鏌ヨDB涓孩鍑虹殑鍗曚綅缂栫爜 + */ + public List<String> selectDbOverflowDep(UserEntity ue, List<String> entities, String wkt) { + List<String> rs = new ArrayList<>(); + for (String enity : entities) { + try { + GeomBaseMapper<?> baseMapper = ClassHelper.getGeoBaseMapper(enity); + if (null == baseMapper) { + continue; + } + + QueryWrapper wrapper = new QueryWrapper(); + wrapper.select("depid"); + // wrapper.gt("depid", 0); wrapper.apply(String.format("depid != ALL(fn_rec_array(%d, 'dep'))", ue.getDepid())) + wrapper.apply("depid is not null and depid not like '" + ue.getDepcode() + "%'"); + wrapper.groupBy("depid"); + + Integer srid = baseQueryService.getSrid(baseMapper); + if (null != srid) { + wrapper.apply(String.format("ST_Intersects(ST_PolygonFromText('%s', %d), geom)", wkt, srid)); + } + + List<String> ids = baseMapper.selectObjs(wrapper); + + addDepCodes(rs, ids); + } catch (Exception ex) { + log.error(ex.getMessage(), ex); + } + } + + return rs; + } + + /** + * 娣诲姞鍗曚綅缂栫爜 + */ + private void addDepCodes(List<String> rs, List<String> ids) { + if (null == ids || ids.isEmpty()) { + return; + } + + for (String id : ids) { + if (!rs.contains(id)) { + rs.add(id); + } + } + } + + /** + * 鍒涘缓Zip鍖� + */ + public String createZipFile(UserEntity ue, List<String> entities, String depcode, String dirs, String wkt, String pwd) throws Exception { + Map<String, List<?>> map = queryData(entities, depcode, dirs, wkt); + if (map.size() == 0) { + return null; + } + + String tempName = StringHelper.YMDHMS2_FORMAT.format(new Date()); + String tempPath = pathHelper.getTempPath(tempName); + // String filePath = "D:\\LF\\temp\\20221219202706\\2022.gdb" + String filePath = tempPath + File.separator + tempName + ".gdb"; + + File file = new File(filePath); + if (file.exists() && file.isDirectory()) { + FileHelper.deleteDir(filePath); + } + GdbHelper.createGdb(filePath, map); + + String zipName = tempName + ".gdb.zip"; + String zipFile = pathHelper.getDownloadFullPath() + File.separator + zipName; + + ZipFile zip = Zip4jHelper.createZipFile(zipFile, pwd); + ZipParameters params = Zip4jHelper.getZipParams(); + addZipFiles(zip, params, file.listFiles()); + + String dbPwd = Md5Helper.reverse(Md5Helper.generate(pwd)); + DownloadEntity downloadEntity = getDownloadEntity(ue, zipFile, dbPwd); + int rows = downloadMapper.insert(downloadEntity); + + return rows > 0 ? downloadEntity.getGuid() : null; + } + + /** + * 鏌ヨ鏁版嵁 + */ + private Map<String, List<?>> queryData(List<String> entities, String depcode, String dirs, String wkt) { + Map<String, List<?>> map = new HashMap<>(5); + for (String enity : entities) { + try { + GeomBaseMapper<?> baseMapper = ClassHelper.getGeoBaseMapper(enity); + if (null == baseMapper) { + continue; + } + + QueryWrapper wrapper = createWrapper(baseMapper, depcode, dirs, wkt); + List<?> list = baseMapper.selectList(wrapper); + if (null == list || list.size() == 0) { + continue; + } + + if (!map.containsKey(enity)) { + map.put(enity, list); + } + } catch (Exception ex) { + log.error(ex.getMessage(), ex); + } + } + + return map; + } + + /** + * 鍒涘缓QueryWrapper + */ + private QueryWrapper createWrapper(BasicMapper baseMapper, String depcode, String dirs, String wkt) { + QueryWrapper wrapper = new QueryWrapper(); + if (baseMapper instanceof GeomBaseMapper) { + wrapper.select("ST_AsText(geom) as geom, *"); + + Integer srid = baseQueryService.getSrid((GeomBaseMapper) baseMapper); + if (null != srid) { + wrapper.apply(String.format("ST_Intersects(ST_PolygonFromText('%s', %d), geom)", wkt, srid)); + } + } + if (!StringHelper.isEmpty(depcode)) { + wrapper.likeRight("depid", depcode); + } + + dirs = DataLibService.copeCodes(dirs, "dirid"); + if (!StringHelper.isEmpty(dirs)) { + wrapper.apply(dirs); + } + + return wrapper; + } + + /** + * 娣诲姞Zip鏂囦欢 + */ + private void addZipFiles(ZipFile zip, ZipParameters params, File[] files) { + if (null == files || files.length == 0) { + return; + } + + for (File f : files) { + try { + zip.addFile(f, params); + } catch (Exception ex) { + log.error(ex.getMessage(), ex); + } + } + } + + /** + * 鑾峰彇涓嬭浇瀹炰綋绫� + */ + private DownloadEntity getDownloadEntity(UserEntity ue, String file, String pwd) throws Exception { + DownloadEntity de = new DownloadEntity(); + de.setName(FileHelper.getFileName(file)); + // 1-Shp鏂囦欢锛�2-涓撻鍥撅紝3-婧愭暟鎹紝4-涓氬姟鏁版嵁锛�5-绠¢亾鍒嗘瀽锛�6-缁熻鎶ュ憡 + de.setType(4); + de.setSizes(FileHelper.sizeToMb(new File(file).length())); + de.setDepid(ue.getDepid()); + de.setDcount(0); + de.setPwd(pwd); + de.setUrl(FileHelper.getRelativePath(file)); + de.setDescr("涓氬姟鏁版嵁鏂囦欢"); + de.setGuid(FileHelper.getFileMd5(file)); + de.setCreateUser(ue.getId()); + // de.setGeom(null) + + return de; + } + + /** + * 鎵撳寘DB鏁版嵁 + */ + public String zipDbData(UserEntity ue, String name, String depcode, String dirs, String filter, String pwd) throws Exception { + BasicMapper baseMapper = ClassHelper.getBasicMapper(name); + if (baseMapper == null) { + return null; + } + + QueryWrapper wrapper = new QueryWrapper(); + baseQueryService.addFilterWrapper(wrapper, filter); + if (baseMapper instanceof GeomBaseMapper) { + wrapper.select("ST_AsText(geom) as geom, *"); + } + if (!StringHelper.isEmpty(depcode)) { + wrapper.likeRight("depid", depcode); + } + + dirs = DataLibService.copeCodes(dirs, "dirid"); + if (!StringHelper.isEmpty(dirs)) { + wrapper.apply(dirs); + } + + List<?> list = baseMapper.selectList(wrapper); + if (null == list || 0 == list.size()) { + return null; + } + + Map<String, List<?>> map = new HashMap<>(1); + map.put(name, list); + + return zipData(ue, map, pwd); + } + + /** + * 鎵撳寘鏁版嵁 + */ + private String zipData(UserEntity ue, Map<String, List<?>> map, String pwd) throws Exception { + String tempName = StringHelper.YMDHMS2_FORMAT.format(new Date()); + String tempPath = pathHelper.getTempPath(tempName); + String filePath = tempPath + File.separator + tempName + ".gdb"; + + File file = new File(filePath); + if (file.exists() && file.isDirectory()) { + FileHelper.deleteDir(filePath); + } + GdbHelper.createGdb(filePath, map); + + String zipName = tempName + ".gdb.zip"; + String zipFile = pathHelper.getDownloadFullPath() + File.separator + zipName; + + ZipFile zip = Zip4jHelper.createZipFile(zipFile, pwd); + ZipParameters params = Zip4jHelper.getZipParams(); + addZipFiles(zip, params, file.listFiles()); + + String dbPwd = Md5Helper.reverse(Md5Helper.generate(pwd)); + DownloadEntity downloadEntity = getDownloadEntity(ue, zipFile, dbPwd); + int rows = downloadMapper.insert(downloadEntity); + + return rows > 0 ? downloadEntity.getGuid() : null; + } + + /** + * 澶勭悊缂栫爜 + */ + public static String copeCodes(String codes, String field) { + if (StringHelper.isEmpty(codes) || StringHelper.isSqlInjection(codes)) { + return null; + } + + List<String> list = codesAsList(codes); + removeDuplicate(list); + setRightLike(list, field); + + return StringHelper.join(list, " or "); + } + + /** + * 鍗曚綅缂栫爜杞泦鍚� + */ + private static List<String> codesAsList(String codes) { + List<String> list = Arrays.asList(codes.split(",")); + Set set = new HashSet(list); + + List<String> newList = new ArrayList<>(set); + Collections.sort(newList); + + return newList; + } + + /** + * 鍘婚櫎閲嶅 + */ + private static void removeDuplicate(List<String> list) { + int i = 0; + while (i < list.size()) { + int j = findStr(list, i); + if (j > -1) { + list.remove(j); + continue; + } + + i++; + } + } + + /** + * 鏌ユ壘瀛楃涓� + */ + private static int findStr(List<String> list, int i) { + String source = list.get(i); + for (int j = i + 1, c = list.size(); j < c; j++) { + if (list.get(j).startsWith(source)) { + return j; + } + } + + return -1; + } + + /** + * 璁剧疆 鍙砽ike + */ + private static void setRightLike(List<String> list, String field) { + for (int i = 0, c = list.size(); i < c; i++) { + String str = String.format("%s like '%s%%'", field, list.get(i)); + list.set(i, str); + } + } } -- Gitblit v1.9.3