| | |
| | | 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 org.springframework.stereotype.Service; |
| | | |
| | | import java.io.File; |
| | | import java.util.Date; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.*; |
| | | |
| | | /** |
| | | * 资料馆 |
| | |
| | | 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包 |
| | |
| | | Map<String, List<?>> map = new HashMap<>(5); |
| | | for (String enity : entities) { |
| | | try { |
| | | GeomBaseMapper baseMapper = ClassHelper.getGeoBaseMapper(enity); |
| | | GeomBaseMapper<?> baseMapper = ClassHelper.getGeoBaseMapper(enity); |
| | | if (null == baseMapper) { |
| | | continue; |
| | | } |
| | |
| | | map.put(enity, list); |
| | | } |
| | | } catch (Exception ex) { |
| | | // |
| | | log.error(ex.getMessage(), ex); |
| | | } |
| | | } |
| | | |
| | |
| | | private QueryWrapper createWrapper(GeomBaseMapper baseMapper, String wkt) { |
| | | QueryWrapper wrapper = new QueryWrapper(); |
| | | wrapper.select("ST_AsText(geom) as geom, *"); |
| | | |
| | | Integer srid = baseQueryService.getSrid(baseMapper); |
| | | wrapper.apply(String.format("ST_Intersects(ST_PolygonFromText('%s', %d), geom)", wkt, srid)); |
| | | if (null != srid) { |
| | | wrapper.apply(String.format("ST_Intersects(ST_PolygonFromText('%s', %d), geom)", wkt, srid)); |
| | | } |
| | | |
| | | return wrapper; |
| | | } |
| | |
| | | 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-业务数据 |
| | | // 1-Shp文件,2-专题图,3-源数据,4-业务数据,5-管道分析,6-统计报告 |
| | | de.setType(4); |
| | | de.setSizes(FileHelper.sizeToMb(new File(file).length())); |
| | | de.setDepid(ue.getDepid()); |
| | |
| | | |
| | | return de; |
| | | } |
| | | |
| | | /** |
| | | * 打包DB数据 |
| | | */ |
| | | public String zipDbData(UserEntity ue, String name, 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, *"); |
| | | } |
| | | |
| | | 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; |
| | | } |
| | | |
| | | /** |
| | | * 设置 右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); |
| | | } |
| | | } |
| | | } |