| | |
| | | package com.lf.server.service.show; |
| | | |
| | | 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 com.lf.server.service.data.MetaService; |
| | | import net.lingala.zip4j.ZipFile; |
| | | import net.lingala.zip4j.model.ZipParameters; |
| | | import org.apache.commons.logging.Log; |
| | |
| | | PathHelper pathHelper; |
| | | |
| | | @Autowired |
| | | DownloadMapper downloadMapper; |
| | | MetaService metaService; |
| | | |
| | | @Autowired |
| | | DownloadService downloadService; |
| | | DownloadMapper downloadMapper; |
| | | |
| | | @Autowired |
| | | BaseQueryService baseQueryService; |
| | |
| | | private final static Log log = LogFactory.getLog(DataLibService.class); |
| | | |
| | | /** |
| | | * 查询DB中溢出的单位ID |
| | | * 查询DB中溢出的单位编码 |
| | | */ |
| | | public List<Integer> selectDbOverflowDep(UserEntity ue, List<String> entities, String wkt) { |
| | | List<Integer> rs = new ArrayList<>(); |
| | | for (String enity : entities) { |
| | | try { |
| | | GeomBaseMapper<?> baseMapper = ClassHelper.getGeoBaseMapper(enity); |
| | | if (null == baseMapper) { |
| | | continue; |
| | | } |
| | | public List<String> selectDbOverflowDep(UserEntity ue, DownloadReqEntity dr) { |
| | | if (!StringHelper.isEmpty(dr.getWkt())) { |
| | | return selectDbOverflowDep4Wkt(ue, dr); |
| | | } |
| | | |
| | | 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.groupBy("depid"); |
| | | return selectDbOverflowDep4Prop(ue, dr); |
| | | } |
| | | |
| | | Integer srid = baseQueryService.getSrid(baseMapper); |
| | | if (null != srid) { |
| | | wrapper.apply(String.format("ST_Intersects(ST_PolygonFromText('%s', %d), geom)", wkt, srid)); |
| | | } |
| | | |
| | | List<Integer> ids = baseMapper.selectObjs(wrapper); |
| | | |
| | | addDepIds(rs, ids); |
| | | } catch (Exception ex) { |
| | | log.error(ex.getMessage(), ex); |
| | | /** |
| | | * 查询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; |
| | | } |
| | | |
| | | /** |
| | | * 添加单位ID |
| | | * 查询DB中溢出的单位编码-属性查询 |
| | | */ |
| | | private void addDepIds(List<Integer> rs, List<Integer> ids) { |
| | | 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 (Integer id : ids) { |
| | | for (String id : ids) { |
| | | // || "00".equals(id) |
| | | if (StringHelper.isEmpty(id)) { |
| | | continue; |
| | | } |
| | | if (!rs.contains(id)) { |
| | | rs.add(id); |
| | | } |
| | |
| | | } |
| | | |
| | | /** |
| | | * 创建Zip包 |
| | | * 请求DB数据下载 |
| | | */ |
| | | public String createZipFile(UserEntity ue, List<String> entities, String wkt, String pwd) throws Exception { |
| | | Map<String, List<?>> map = queryData(entities, wkt); |
| | | if (map.size() == 0) { |
| | | 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 filePath = "D:\\LF\\temp\\20221219202706\\2022.gdb" |
| | | String filePath = tempPath + File.separator + tempName + ".gdb"; |
| | | String gdbPath = tempPath + File.separator + "tabs.gdb"; |
| | | |
| | | File file = new File(filePath); |
| | | if (file.exists() && file.isDirectory()) { |
| | | FileHelper.deleteDir(filePath); |
| | | File gdbFile = new File(gdbPath); |
| | | if (gdbFile.exists() && gdbFile.isDirectory()) { |
| | | FileHelper.deleteDir(gdbPath); |
| | | } |
| | | GdbHelper.createGdb(filePath, map); |
| | | GdbHelper.createGdb(gdbPath, dataMap); |
| | | |
| | | String zipName = tempName + ".gdb.zip"; |
| | | String zipFile = pathHelper.getDownloadFullPath() + File.separator + zipName; |
| | | 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) |
| | | metaService.addAnnex(zip, params, annexMap); |
| | | |
| | | ZipFile zip = Zip4jHelper.createZipFile(zipFile, pwd); |
| | | ZipParameters params = Zip4jHelper.getZipParams(); |
| | | addZipFiles(zip, params, file.listFiles()); |
| | | String dbPwd = Md5Helper.reverse(Md5Helper.generate(dr.getPwd())); |
| | | DownloadEntity de = getDownloadEntity(ue, zipFile, dbPwd); |
| | | int rows = downloadMapper.insert(de); |
| | | |
| | | String dbPwd = Md5Helper.reverse(Md5Helper.generate(pwd)); |
| | | DownloadEntity downloadEntity = getDownloadEntity(ue, zipFile, dbPwd); |
| | | int rows = downloadMapper.insert(downloadEntity); |
| | | |
| | | return rows > 0 ? downloadEntity.getGuid() : null; |
| | | return rows > 0 ? de.getGuid() : null; |
| | | } |
| | | |
| | | /** |
| | | * 查询数据 |
| | | * 查询数据+附件 |
| | | */ |
| | | private Map<String, List<?>> queryData(List<String> entities, String wkt) { |
| | | Map<String, List<?>> map = new HashMap<>(5); |
| | | for (String enity : entities) { |
| | | private void queryData(DownloadReqEntity dr, Map<String, List<?>> dataMap, Map<String, List<AttachEntity>> annexMap) { |
| | | for (String entity : dr.getEntities()) { |
| | | try { |
| | | GeomBaseMapper<?> baseMapper = ClassHelper.getGeoBaseMapper(enity); |
| | | BasicMapper baseMapper = ClassHelper.getBasicMapper(entity); |
| | | if (null == baseMapper) { |
| | | continue; |
| | | } |
| | | |
| | | QueryWrapper wrapper = createWrapper(baseMapper, wkt); |
| | | List<?> list = baseMapper.selectList(wrapper); |
| | | if (null == list || list.size() == 0) { |
| | | continue; |
| | | } |
| | | |
| | | if (!map.containsKey(enity)) { |
| | | map.put(enity, list); |
| | | } |
| | | QueryWrapper wrapper = createQueryWrapper(baseMapper, dr); |
| | | metaService.addData(entity, baseMapper, wrapper, dataMap, annexMap); |
| | | } catch (Exception ex) { |
| | | log.error(ex.getMessage(), ex); |
| | | } |
| | | } |
| | | |
| | | return map; |
| | | } |
| | | |
| | | /** |
| | | * 创建QueryWrapper |
| | | * 创建查询包装器 |
| | | */ |
| | | private QueryWrapper createWrapper(GeomBaseMapper baseMapper, String wkt) { |
| | | QueryWrapper wrapper = new QueryWrapper(); |
| | | wrapper.select("ST_AsText(geom) as geom, *"); |
| | | private <T> QueryWrapper<T> createQueryWrapper(BasicMapper baseMapper, DownloadReqEntity dr) { |
| | | QueryWrapper<T> wrapper = new QueryWrapper<T>(); |
| | | |
| | | Integer srid = baseQueryService.getSrid(baseMapper); |
| | | if (null != srid) { |
| | | wrapper.apply(String.format("ST_Intersects(ST_PolygonFromText('%s', %d), geom)", wkt, srid)); |
| | | 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; |
| | | } |
| | | |
| | | /** |
| | | * 添加Zip文件 |
| | | * 创建附件 * |
| | | */ |
| | | private void addZipFiles(ZipFile zip, ZipParameters params, File[] files) { |
| | | if (null == files || files.length == 0) { |
| | | private void createAnnex(String annexPath, Map<String, List<AttachEntity>> annexMap) { |
| | | if (annexMap.size() == 0) { |
| | | return; |
| | | } |
| | | |
| | | for (File f : files) { |
| | | try { |
| | | zip.addFile(f, params); |
| | | } catch (Exception ex) { |
| | | log.error(ex.getMessage(), ex); |
| | | 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) throws Exception { |
| | | private DownloadEntity getDownloadEntity(UserEntity ue, String file, String pwd) { |
| | | DownloadEntity de = new DownloadEntity(); |
| | | de.setName(FileHelper.getFileName(file)); |
| | | // 1-Shp文件,2-专题图,3-元数据,4-业务数据 |
| | | // 1-Shp文件,2-专题图,3-元数据,4-业务数据,5-管道分析,6-统计报告 |
| | | de.setType(4); |
| | | de.setSizes(FileHelper.sizeToMb(new File(file).length())); |
| | | de.setDepid(ue.getDepid()); |
| | |
| | | |
| | | 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; |
| | | } |
| | | |
| | | /** |
| | | * 设置 右like |
| | | */ |
| | | 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); |
| | | } |
| | | } |
| | | } |