From 05b7f36c1fdb1dae4fd2131f63e10f72f85ee42c Mon Sep 17 00:00:00 2001 From: 13693261870 <252740454@qq.com> Date: 星期一, 03 四月 2023 16:49:14 +0800 Subject: [PATCH] 1 --- src/main/java/com/lf/server/service/show/DataLibService.java | 331 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 330 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..cd2f6b0 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,25 @@ 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.ctrl.DownloadReqEntity; +import com.lf.server.entity.data.DownloadEntity; +import com.lf.server.entity.sys.AttachEntity; +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 +31,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, DownloadReqEntity dr) { + if (!StringHelper.isEmpty(dr.getWkt())) { + return selectDbOverflowDep4Wkt(ue, dr); + } + + return selectDbOverflowDep4Prop(ue, dr); + } + + /** + * 鏌ヨDB涓孩鍑虹殑鍗曚綅缂栫爜-绌洪棿鏌ヨ + */ + public List<String> selectDbOverflowDep4Wkt(UserEntity ue, DownloadReqEntity dr) { + List<String> rs = new ArrayList<>(); + for (String entity : dr.getEntities()) { + GeomBaseMapper<?> baseMapper = ClassHelper.getGeoBaseMapper(entity); + if (null == baseMapper) { + continue; + } + + QueryWrapper wrapper = getWrapper4DbOverflow(ue, dr); + Integer srid = baseQueryService.getSrid(baseMapper); + if (null != srid) { + wrapper.apply(String.format("ST_Intersects(ST_PolygonFromText('%s', %d), geom)", dr.getWkt(), srid)); + } + + List<String> ids = baseMapper.selectObjs(wrapper); + addDepCodes(rs, ids); + } + + return rs; + } + + /** + * 鏌ヨDB涓孩鍑虹殑鍗曚綅缂栫爜-灞炴�ф煡璇� + */ + public List<String> selectDbOverflowDep4Prop(UserEntity ue, DownloadReqEntity dr) { + List<String> rs = new ArrayList<>(); + BasicMapper<?> baseMapper = ClassHelper.getBasicMapper(dr.getEntities().get(0)); + if (null == baseMapper) { + return rs; + } + + QueryWrapper wrapper = getWrapper4DbOverflow(ue, dr); + List<String> ids = baseMapper.selectObjs(wrapper); + addDepCodes(rs, ids); + + return rs; + } + + /** + * 鏌ヨDB涓孩鍑哄崟浣嶇紪鐮佺殑鏌ヨ鍖呰鍣� + */ + private <T> QueryWrapper<T> getWrapper4DbOverflow(UserEntity ue, DownloadReqEntity dr) { + QueryWrapper<T> wrapper = new QueryWrapper<T>(); + wrapper.select("depid"); + wrapper.groupBy("depid"); + wrapper.apply("depid is not null and depid not like '" + ue.getDepcode() + "%'"); + + if (null != dr.getIds() && dr.getIds().size() > 0) { + wrapper.apply(String.format("gid in (%s)", StringHelper.join(dr.getIds(), ","))); + } else { + baseQueryService.addFilterWrapper(wrapper, dr.getFilter()); + } + + String dirs = copeCodes(dr.getDirs(), "dirid"); + if (!StringHelper.isEmpty(dirs)) { + wrapper.apply(dirs); + } + if (!StringHelper.isEmpty(dr.getDepcode())) { + wrapper.likeRight("depid", dr.getDepcode()); + } + + return wrapper; + } + + /** + * 娣诲姞鍗曚綅缂栫爜 + */ + private void addDepCodes(List<String> rs, List<String> ids) { + if (null == ids || ids.isEmpty()) { + return; + } + + for (String id : ids) { + if (StringHelper.isEmpty(id) || "00".equals(id)) { + continue; + } + if (!rs.contains(id)) { + rs.add(id); + } + } + } + + /** + * 璇锋眰DB鏁版嵁涓嬭浇 + */ + public String downloadDbReq(UserEntity ue, DownloadReqEntity dr) throws Exception { + Map<String, List<?>> dataMap = new HashMap<>(2); + Map<String, List<AttachEntity>> annexMap = new HashMap<>(2); + queryData(dr, dataMap, annexMap); + if (dataMap.size() == 0) { + return null; + } + + String tempName = StringHelper.YMDHMS2_FORMAT.format(new Date()); + String tempPath = pathHelper.getTempPath(tempName); + String gdbPath = tempPath + File.separator + "tabs.gdb"; + + File gdbFile = new File(gdbPath); + if (gdbFile.exists() && gdbFile.isDirectory()) { + FileHelper.deleteDir(gdbPath); + } + GdbHelper.createGdb(gdbPath, dataMap); + + String zipFile = pathHelper.getDownloadFullPath() + File.separator + tempName + ".gdb.zip"; + ZipFile zip = Zip4jHelper.createZipFile(zipFile, dr.getPwd()); + ZipParameters params = Zip4jHelper.getZipParams(true); + zip.addFolder(new File(gdbPath), params); + // zip.addFolder(new File(annexPath), params) + downloadService.addAnnex(zip, params, annexMap); + + String dbPwd = Md5Helper.reverse(Md5Helper.generate(dr.getPwd())); + DownloadEntity de = getDownloadEntity(ue, zipFile, dbPwd); + int rows = downloadMapper.insert(de); + + return rows > 0 ? de.getGuid() : null; + } + + /** + * 鏌ヨ鏁版嵁+闄勪欢 + */ + private void queryData(DownloadReqEntity dr, Map<String, List<?>> dataMap, Map<String, List<AttachEntity>> annexMap) { + for (String entity : dr.getEntities()) { + try { + BasicMapper baseMapper = ClassHelper.getBasicMapper(entity); + if (null == baseMapper) { + continue; + } + + QueryWrapper wrapper = createQueryWrapper(baseMapper, dr); + downloadService.addData(entity, baseMapper, wrapper, dataMap, annexMap); + } catch (Exception ex) { + log.error(ex.getMessage(), ex); + } + } + } + + /** + * 鍒涘缓鏌ヨ鍖呰鍣� + */ + private <T> QueryWrapper<T> createQueryWrapper(BasicMapper baseMapper, DownloadReqEntity dr) { + QueryWrapper<T> wrapper = new QueryWrapper<T>(); + + String dirs = copeCodes(dr.getDirs(), "dirid"); + if (!StringHelper.isEmpty(dirs)) { + wrapper.apply(dirs); + } + if (!StringHelper.isEmpty(dr.getDepcode())) { + // wrapper.apply(String.format("depid like '%s'", StringHelper.getRightLike(dr.getDepcode()))) + wrapper.likeRight("depid", dr.getDepcode()); + } + if (baseMapper instanceof GeomBaseMapper && !StringHelper.isEmpty(dr.getWkt())) { + 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)", dr.getWkt(), srid)); + } + } + if (null != dr.getIds() && dr.getIds().size() > 0) { + wrapper.apply(String.format("gid in (%s)", StringHelper.join(dr.getIds(), ","))); + } else { + baseQueryService.addFilterWrapper(wrapper, dr.getFilter()); + } + + return wrapper; + } + + /** + * 鍒涘缓闄勪欢 * + */ + private void createAnnex(String annexPath, Map<String, List<AttachEntity>> annexMap) { + if (annexMap.size() == 0) { + return; + } + + String uploadPath = pathHelper.getConfig().getUploadPath(); + for (String key : annexMap.keySet()) { + String targetPath = annexPath + File.separator + key; + File targetFile = new File(targetPath); + if (!targetFile.exists() || !targetFile.isDirectory()) { + targetFile.mkdirs(); + } + + for (AttachEntity ae : annexMap.get(key)) { + try { + File srcFile = new File(uploadPath + File.separator + ae.getPath()); + if (!srcFile.exists() || srcFile.isDirectory()) { + continue; + } + + File destFile = new File(targetPath + File.separator + ae.getName()); + if (destFile.exists() && !destFile.isDirectory()) { + continue; + } + + FileHelper.copyFile(srcFile, destFile); + } catch (Exception ex) { + log.error(ex.getMessage(), ex); + } + } + } + } + + /** + * 鑾峰彇涓嬭浇瀹炰綋绫� + */ + private DownloadEntity getDownloadEntity(UserEntity ue, String file, String pwd) { + 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; + } + + /** + * 澶勭悊缂栫爜 + */ + 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