管道基础大数据平台系统开发-【后端】-Server
1
13693261870
2023-03-24 e26af85e049516e6ce2b082bc2bc90bf71643e95
src/main/java/com/lf/server/service/show/DataLibService.java
@@ -1,7 +1,9 @@
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.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;
@@ -40,10 +42,10 @@
    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<>();
    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);
@@ -53,8 +55,8 @@
                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.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);
@@ -62,9 +64,9 @@
                    wrapper.apply(String.format("ST_Intersects(ST_PolygonFromText('%s', %d), geom)", wkt, srid));
                }
                List<Integer> ids = baseMapper.selectObjs(wrapper);
                List<String> ids = baseMapper.selectObjs(wrapper);
                addDepIds(rs, ids);
                addDepCodes(rs, ids);
            } catch (Exception ex) {
                log.error(ex.getMessage(), ex);
            }
@@ -74,14 +76,14 @@
    }
    /**
     * 添加单位ID
     * 添加单位编码
     */
    private void addDepIds(List<Integer> rs, List<Integer> ids) {
    private void addDepCodes(List<String> rs, List<String> ids) {
        if (null == ids || ids.isEmpty()) {
            return;
        }
        for (Integer id : ids) {
        for (String id : ids) {
            if (!rs.contains(id)) {
                rs.add(id);
            }
@@ -91,8 +93,8 @@
    /**
     * 创建Zip包
     */
    public String createZipFile(UserEntity ue, List<String> entities, String wkt, String pwd) throws Exception {
        Map<String, List<?>> map = queryData(entities, wkt);
    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);
        if (map.size() == 0) {
            return null;
        }
@@ -112,7 +114,7 @@
        String zipFile = pathHelper.getDownloadFullPath() + File.separator + zipName;
        ZipFile zip = Zip4jHelper.createZipFile(zipFile, pwd);
        ZipParameters params = Zip4jHelper.getZipParams();
        ZipParameters params = Zip4jHelper.getZipParams(true);
        addZipFiles(zip, params, file.listFiles());
        String dbPwd = Md5Helper.reverse(Md5Helper.generate(pwd));
@@ -125,7 +127,7 @@
    /**
     * 查询数据
     */
    private Map<String, List<?>> queryData(List<String> entities, String wkt) {
    private Map<String, List<?>> queryData(List<String> entities, String depcode, String dirs, String wkt) {
        Map<String, List<?>> map = new HashMap<>(5);
        for (String enity : entities) {
            try {
@@ -134,7 +136,7 @@
                    continue;
                }
                QueryWrapper wrapper = createWrapper(baseMapper, wkt);
                QueryWrapper wrapper = createWrapper(baseMapper, depcode, dirs, wkt);
                List<?> list = baseMapper.selectList(wrapper);
                if (null == list || list.size() == 0) {
                    continue;
@@ -154,16 +156,190 @@
    /**
     * 创建QueryWrapper
     */
    private QueryWrapper createWrapper(GeomBaseMapper baseMapper, String wkt) {
    private QueryWrapper createWrapper(BasicMapper baseMapper, String depcode, String dirs, String wkt) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.select("ST_AsText(geom) as geom, *");
        if (baseMapper instanceof GeomBaseMapper) {
            wrapper.select("ST_AsText(geom) as geom, *");
        Integer srid = baseQueryService.getSrid(baseMapper);
        if (null != srid) {
            wrapper.apply(String.format("ST_Intersects(ST_PolygonFromText('%s', %d), geom)", wkt, srid));
            Integer srid = baseQueryService.getSrid((GeomBaseMapper) baseMapper);
            if (null != srid) {
                wrapper.apply(String.format("ST_Intersects(ST_PolygonFromText('%s', %d), geom)", wkt, srid));
            }
        }
        if (!StringHelper.isEmpty(depcode)) {
            wrapper.likeRight("depid", depcode);
        }
        dirs = DataLibService.copeCodes(dirs, "dirid");
        if (!StringHelper.isEmpty(dirs)) {
            wrapper.apply(dirs);
        }
        return wrapper;
    }
    /**
     * 获取下载实体类
     */
    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;
    }
    /**
     * 打包DB数据
     */
    public String zipDbData(UserEntity ue, String name, String depcode, String dirs, 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, *");
        }
        if (!StringHelper.isEmpty(depcode)) {
            wrapper.likeRight("depid", depcode);
        }
        dirs = DataLibService.copeCodes(dirs, "dirid");
        if (!StringHelper.isEmpty(dirs)) {
            wrapper.apply(dirs);
        }
        List<?> list = baseMapper.selectList(wrapper);
        if (null == list || 0 == list.size()) {
            return null;
        }
        String tab = BaseQueryService.getTabName(baseMapper);
        String ids = getAnnexFilter(StringHelper.isEmpty(depcode) && StringHelper.isEmpty(dirs) && StringHelper.isEmpty(filter), list);
        List<AttachEntity> annex = baseQueryService.selectAnnexByTab(tab, ids);
        Map<String, List<?>> map = new HashMap<>(1);
        map.put(name, list);
        return zipData(ue, map, annex, pwd);
    }
    /**
     * 打包数据
     */
    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 gdbPath = tempPath + File.separator + tempName + ".gdb";
        String annexPath = tempPath + File.separator + "annex";
        File file = new File(gdbPath);
        if (file.exists() && file.isDirectory()) {
            FileHelper.deleteDir(gdbPath);
        }
        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())
        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);
        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);
        }
    }
    /**
@@ -184,43 +360,44 @@
    }
    /**
     * 获取下载实体类
     * 获取查询附件条件
     */
    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-业务数据
        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;
    }
    /**
     * 打包DB数据
     */
    public String zipDbData(UserEntity ue, BasicMapper baseMapper, String filter, String pwd) {
        boolean flag = baseMapper instanceof GeomBaseMapper;
        QueryWrapper wrapper = new QueryWrapper();
        baseQueryService.addFilterWrapper(wrapper, filter);
    private String getAnnexFilter(boolean flag, List<?> list) {
        if (flag) {
            wrapper.select("ST_AsText(geom) as geom, *");
        }
        List<?> list = baseMapper.selectList(wrapper);
        if (null == list || 0 == list.size()) {
            return null;
        }
        return null;
        List<Integer> gids = new ArrayList<>();
        for (Object obj : list) {
            BaseEntity be = (BaseEntity) obj;
            gids.add(be.getGid());
        }
        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);
            }
        }
    }
}