管道基础大数据平台系统开发-【后端】-Server
1
13693261870
2023-02-03 9bce48a9f30f7d80c43f43f46d40df20fcb00e15
src/main/java/com/lf/server/service/show/DataLibService.java
@@ -1,19 +1,22 @@
package com.lf.server.service.show;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.GeomBaseMapper;
import com.lf.server.mapper.data.DownloadMapper;
import com.lf.server.service.all.BaseQueryService;
import com.lf.server.service.data.DownloadService;
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.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
/**
 * 资料馆
@@ -25,15 +28,69 @@
    PathHelper pathHelper;
    @Autowired
    BaseQueryService baseQueryService;
    DownloadMapper downloadMapper;
    @Autowired
    DownloadService downloadService;
    @Autowired
    BaseQueryService baseQueryService;
    private final static Log log = LogFactory.getLog(DataLibService.class);
    /**
     * 查询DB中溢出的单位ID
     */
    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;
                }
                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");
                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);
            }
        }
        return rs;
    }
    /**
     * 添加单位ID
     */
    private void addDepIds(List<Integer> rs, List<Integer> ids) {
        if (null == ids || ids.isEmpty()) {
            return;
        }
        for (Integer id : ids) {
            if (!rs.contains(id)) {
                rs.add(id);
            }
        }
    }
    /**
     * 创建Zip包
     */
    public String createZipFile(UserEntity ue, List<String> entities, String wkt, String pwd) {
    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) {
            return null;
@@ -41,10 +98,27 @@
        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";
        File file = new File(filePath);
        if (file.exists() && file.isDirectory()) {
            FileHelper.deleteDir(filePath);
        }
        GdbHelper.createGdb(filePath, map);
        return null;
        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;
    }
    /**
@@ -54,7 +128,7 @@
        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;
                }
@@ -69,7 +143,7 @@
                    map.put(enity, list);
                }
            } catch (Exception ex) {
                //
                log.error(ex.getMessage(), ex);
            }
        }
@@ -81,10 +155,51 @@
     */
    private QueryWrapper createWrapper(GeomBaseMapper baseMapper, String wkt) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.select("ST_AsText(geom) as geom");
        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;
    }
    /**
     * 添加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) 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;
    }
}