package com.moon.server.service.show; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.moon.server.entity.ctrl.DownloadReqEntity; import com.moon.server.entity.data.DownloadEntity; import com.moon.server.entity.sys.AttachEntity; import com.moon.server.entity.sys.UserEntity; import com.moon.server.mapper.all.BasicMapper; import com.moon.server.mapper.all.GeomBaseMapper; import com.moon.server.mapper.data.DownloadMapper; import com.moon.server.service.all.BaseQueryService; import com.moon.server.service.data.MetaService; import com.moon.server.helper.*; 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.*; @Service @SuppressWarnings("ALL") public class DataLibService { @Autowired PathHelper pathHelper; @Autowired MetaService metaService; @Autowired DownloadMapper downloadMapper; @Autowired BaseQueryService baseQueryService; private final static Log log = LogFactory.getLog(DataLibService.class); public List selectDbOverflowDep(UserEntity ue, DownloadReqEntity dr) { if (!StringHelper.isEmpty(dr.getWkt())) { return selectDbOverflowDep4Wkt(ue, dr); } return selectDbOverflowDep4Prop(ue, dr); } public List selectDbOverflowDep4Wkt(UserEntity ue, DownloadReqEntity dr) { List 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); wrapper.apply(String.format("ST_Intersects(ST_PolygonFromText('%s', %d), geom)", dr.getWkt(), srid)); List ids = baseMapper.selectObjs(wrapper); addDepCodes(rs, ids); } return rs; } public List selectDbOverflowDep4Prop(UserEntity ue, DownloadReqEntity dr) { List rs = new ArrayList<>(); BasicMapper baseMapper = ClassHelper.getBasicMapper(dr.getEntities().get(0)); if (null == baseMapper) { return rs; } QueryWrapper wrapper = getWrapper4DbOverflow(ue, dr); List ids = baseMapper.selectObjs(wrapper); addDepCodes(rs, ids); return rs; } private QueryWrapper getWrapper4DbOverflow(UserEntity ue, DownloadReqEntity dr) { QueryWrapper wrapper = new QueryWrapper(); 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 rs, List ids) { if (null == ids || ids.isEmpty()) { return; } for (String id : ids) { if (StringHelper.isEmpty(id)) { continue; } if (!rs.contains(id)) { rs.add(id); } } } public String downloadDbReq(UserEntity ue, DownloadReqEntity dr) throws Exception { Map> dataMap = new HashMap<>(2); Map> 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) metaService.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> dataMap, Map> annexMap) { for (String entity : dr.getEntities()) { try { BasicMapper baseMapper = ClassHelper.getBasicMapper(entity); if (null == baseMapper) { continue; } QueryWrapper wrapper = createQueryWrapper(baseMapper, dr); metaService.addData(entity, baseMapper, wrapper, dataMap, annexMap); } catch (Exception ex) { log.error(ex.getMessage(), ex); } } } private QueryWrapper createQueryWrapper(BasicMapper baseMapper, DownloadReqEntity dr) { QueryWrapper wrapper = new QueryWrapper(); 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) { wrapper.select("ST_AsText(geom) as geom, *"); if (!StringHelper.isEmpty(dr.getWkt())) { Integer srid = baseQueryService.getSrid((GeomBaseMapper) baseMapper); 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> 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-统计报告,7-附件文件,8-瓦片文件 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 list = codesAsList(codes); list = copeDirCodes(list); setRightLike(list, field); return "(" + StringHelper.join(list, " or ") + ")"; } private static List codesAsList(String codes) { List list = Arrays.asList(codes.split(",")); Set set = new HashSet(list); List newList = new ArrayList<>(set); Collections.sort(newList); return newList; } private static List copeDirCodes(List list) { List prjList = getDirCodesByLen(list, 0, 2); List appList = getDirCodesByLen(list, 3, 30); if (prjList.isEmpty() && appList.isEmpty()) { return list; } if (prjList.isEmpty()) { return appList; } if (appList.isEmpty()) { return prjList; } return filterCodes(prjList, appList); } private static List getDirCodesByLen(List list, int start, int end) { List rs = new ArrayList<>(); for (String code : list) { if (StringHelper.isEmpty(code)) { continue; } if (code.length() >= start && code.length() <= end) { rs.add(code); } } return rs; } private static List filterCodes(List prjList, List appList) { int i = 0; while (i < appList.size()) { boolean flag = false; for (String prj : prjList) { if (appList.get(i).startsWith(prj)) { flag = true; break; } } if (!flag) { appList.remove(i); continue; } i++; } return appList; } private static void setRightLike(List list, String field) { if (list.isEmpty()) { list.add("1 = 2"); return; } 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); } } }