| | |
| | | package com.lf.server.service.show; |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.lf.server.entity.all.BaseEntity; |
| | | 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; |
| | |
| | | /** |
| | | * 查询DB中溢出的单位编码 |
| | | */ |
| | | public List<String> selectDbOverflowDep(UserEntity ue, List<String> entities, String wkt) { |
| | | 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 enity : entities) { |
| | | for (String entity : dr.getEntities()) { |
| | | try { |
| | | GeomBaseMapper<?> baseMapper = ClassHelper.getGeoBaseMapper(enity); |
| | | GeomBaseMapper<?> baseMapper = ClassHelper.getGeoBaseMapper(entity); |
| | | 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"); |
| | | |
| | | QueryWrapper wrapper = getWrapper4DbOverflow(ue, dr); |
| | | Integer srid = baseQueryService.getSrid(baseMapper); |
| | | if (null != srid) { |
| | | wrapper.apply(String.format("ST_Intersects(ST_PolygonFromText('%s', %d), geom)", wkt, srid)); |
| | | wrapper.apply(String.format("ST_Intersects(ST_PolygonFromText('%s', %d), geom)", dr.getWkt(), srid)); |
| | | } |
| | | |
| | | List<String> ids = baseMapper.selectObjs(wrapper); |
| | | |
| | | addDepCodes(rs, ids); |
| | | } catch (Exception ex) { |
| | | log.error(ex.getMessage(), ex); |
| | |
| | | } |
| | | |
| | | return rs; |
| | | } |
| | | |
| | | /** |
| | | * 查询DB中溢出的单位编码-属性查询 |
| | | */ |
| | | public List<String> selectDbOverflowDep4Prop(UserEntity ue, DownloadReqEntity dr) { |
| | | List<String> rs = new ArrayList<>(); |
| | | try { |
| | | 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); |
| | | } catch (Exception ex) { |
| | | log.error(ex.getMessage(), ex); |
| | | } |
| | | |
| | | 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; |
| | | } |
| | | |
| | | /** |
| | |
| | | } |
| | | |
| | | /** |
| | | * 创建Zip包 |
| | | * 请求DB数据下载 |
| | | */ |
| | | 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); |
| | | public String downloadDbReq(UserEntity ue, DownloadReqEntity dr) { |
| | | Map<String, List<?>> map = queryData(dr); |
| | | if (map.size() == 0) { |
| | | return null; |
| | | } |
| | | |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * 查询数据 |
| | | */ |
| | | private Map<String, List<?>> queryData(DownloadReqEntity dr) { |
| | | Map<String, List<?>> map = new HashMap<>(3); |
| | | for (String enity : dr.getEntities()) { |
| | | try { |
| | | BasicMapper baseMapper = ClassHelper.getBasicMapper(enity); |
| | | if (null == baseMapper) { |
| | | continue; |
| | | } |
| | | |
| | | QueryWrapper wrapper = createQueryWrapper(baseMapper, dr); |
| | | 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; |
| | | } |
| | | |
| | | /** |
| | | * 创建查询包装器 |
| | | */ |
| | | 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.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; |
| | | } |
| | | |
| | | /** |
| | | * 请求DB数据下载-空间查询 |
| | | */ |
| | | public String downloadDbReq4Wkt(UserEntity ue, DownloadReqEntity dr) throws Exception { |
| | | String depcode = null == dr.getDepcodes() || dr.getDepcodes().isEmpty() ? null : dr.getDepcodes().get(0); |
| | | Map<String, List<?>> map = queryData(dr.getEntities(), depcode, dr.getDirs(), dr.getWkt()); |
| | | if (map.size() == 0) { |
| | | return null; |
| | | } |
| | |
| | | String zipName = tempName + ".gdb.zip"; |
| | | String zipFile = pathHelper.getDownloadFullPath() + File.separator + zipName; |
| | | |
| | | ZipFile zip = Zip4jHelper.createZipFile(zipFile, pwd); |
| | | ZipFile zip = Zip4jHelper.createZipFile(zipFile, dr.getPwd()); |
| | | ZipParameters params = Zip4jHelper.getZipParams(true); |
| | | addZipFiles(zip, params, file.listFiles()); |
| | | |
| | | String dbPwd = Md5Helper.reverse(Md5Helper.generate(pwd)); |
| | | String dbPwd = Md5Helper.reverse(Md5Helper.generate(dr.getPwd())); |
| | | DownloadEntity downloadEntity = getDownloadEntity(ue, zipFile, dbPwd); |
| | | int rows = downloadMapper.insert(downloadEntity); |
| | | |
| | |
| | | } |
| | | |
| | | /** |
| | | * 添加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) { |
| | |
| | | } |
| | | |
| | | /** |
| | | * 打包DB数据 |
| | | * 请求DB数据下载-属性查询 |
| | | */ |
| | | public String zipDbData(UserEntity ue, String name, String depcode, String dirs, String filter, String pwd) throws Exception { |
| | | BasicMapper baseMapper = ClassHelper.getBasicMapper(name); |
| | | public String downloadDbReq4Prop(UserEntity ue, DownloadReqEntity dr) throws Exception { |
| | | String dirs = dr.getDirs(); |
| | | String entity = dr.getEntities().get(0); |
| | | String depcode = null == dr.getDepcodes() || dr.getDepcodes().isEmpty() ? null : dr.getDepcodes().get(0); |
| | | |
| | | BasicMapper baseMapper = ClassHelper.getBasicMapper(entity); |
| | | if (baseMapper == null) { |
| | | return null; |
| | | } |
| | | |
| | | QueryWrapper wrapper = new QueryWrapper(); |
| | | baseQueryService.addFilterWrapper(wrapper, filter); |
| | | baseQueryService.addFilterWrapper(wrapper, dr.getFilter()); |
| | | if (baseMapper instanceof GeomBaseMapper) { |
| | | wrapper.select("ST_AsText(geom) as geom, *"); |
| | | } |
| | |
| | | } |
| | | |
| | | String tab = BaseQueryService.getTabName(baseMapper); |
| | | String ids = getAnnexFilter(StringHelper.isEmpty(depcode) && StringHelper.isEmpty(dirs) && StringHelper.isEmpty(filter), list); |
| | | String ids = getAnnexFilter(StringHelper.isEmpty(depcode) && StringHelper.isEmpty(dirs) && StringHelper.isEmpty(dr.getFilter()), list); |
| | | List<AttachEntity> annex = baseQueryService.selectAnnexByTab(tab, ids); |
| | | |
| | | Map<String, List<?>> map = new HashMap<>(1); |
| | | map.put(name, list); |
| | | map.put(entity, list); |
| | | |
| | | return zipData(ue, map, pwd); |
| | | return zipData(ue, map, annex, dr.getPwd()); |
| | | } |
| | | |
| | | /** |
| | | * 打包数据 |
| | | */ |
| | | private String zipData(UserEntity ue, Map<String, List<?>> map, String pwd) throws Exception { |
| | | private String zipData(UserEntity ue, Map<String, List<?>> map, List<AttachEntity> annex, String pwd) throws Exception { |
| | | String tempName = StringHelper.YMDHMS2_FORMAT.format(new Date()); |
| | | String tempPath = pathHelper.getTempPath(tempName); |
| | | String filePath = tempPath + File.separator + tempName + ".gdb"; |
| | | String gdbPath = tempPath + File.separator + tempName + ".gdb"; |
| | | String annexPath = tempPath + File.separator + "annex"; |
| | | |
| | | File file = new File(filePath); |
| | | File file = new File(gdbPath); |
| | | if (file.exists() && file.isDirectory()) { |
| | | FileHelper.deleteDir(filePath); |
| | | FileHelper.deleteDir(gdbPath); |
| | | } |
| | | GdbHelper.createGdb(filePath, map); |
| | | file = new File(annexPath); |
| | | if (!file.exists() || !file.isDirectory()) { |
| | | file.mkdirs(); |
| | | } |
| | | |
| | | GdbHelper.createGdb(gdbPath, map); |
| | | createAnnex(annexPath, annex); |
| | | |
| | | String zipName = tempName + ".gdb.zip"; |
| | | String zipFile = pathHelper.getDownloadFullPath() + File.separator + zipName; |
| | | |
| | | ZipFile zip = Zip4jHelper.createZipFile(zipFile, pwd); |
| | | ZipParameters params = Zip4jHelper.getZipParams(true); |
| | | addZipFiles(zip, params, file.listFiles()); |
| | | // addZipFiles(zip, params, file.listFiles()) |
| | | zip.addFolder(new File(gdbPath), params); |
| | | zip.addFolder(new File(annexPath), params); |
| | | |
| | | String dbPwd = Md5Helper.reverse(Md5Helper.generate(pwd)); |
| | | DownloadEntity downloadEntity = getDownloadEntity(ue, zipFile, dbPwd); |
| | |
| | | } |
| | | |
| | | /** |
| | | * 添加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 static String getAnnexFilter(boolean flag, List<?> list) { |
| | | private String getAnnexFilter(boolean flag, List<?> list) { |
| | | if (flag) { |
| | | return null; |
| | | } |
| | | |
| | | List<Integer> gids = new ArrayList<>(); |
| | | for (Object obj : list) { |
| | | BaseEntity be = (BaseEntity) obj; |
| | | gids.add(be.getGid()); |
| | | } |
| | | |
| | | return ""; |
| | | return StringHelper.join(gids, ","); |
| | | } |
| | | |
| | | /** |
| | | * 添加附件文件 |
| | | */ |
| | | private void createAnnex(String targetPath, List<AttachEntity> list) { |
| | | if (null == list || list.isEmpty()) { |
| | | return; |
| | | } |
| | | |
| | | int i = 1; |
| | | String uploadPath = pathHelper.getConfig().getUploadPath(); |
| | | for (AttachEntity attach : list) { |
| | | try { |
| | | File srcFile = new File(uploadPath + File.separator + attach.getPath()); |
| | | if (!srcFile.exists() || srcFile.isDirectory()) { |
| | | continue; |
| | | } |
| | | |
| | | File destFile = new File(targetPath + File.separator + i++ + "_" + attach.getName()); |
| | | FileHelper.copyFile(srcFile, destFile); |
| | | } catch (Exception ex) { |
| | | log.error(ex.getMessage(), ex); |
| | | } |
| | | } |
| | | } |
| | | } |