管道基础大数据平台系统开发-【后端】-Server
1
13693261870
2022-11-01 b1bdad2ff512e73a2dd9049f2ccbddd4634969f7
1
已修改11个文件
176 ■■■■ 文件已修改
data/db_fn.sql 6 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/lf/server/config/InitConfig.java 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/lf/server/controller/show/MarkController.java 44 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/lf/server/helper/FileHelper.java 17 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/lf/server/helper/WebHelper.java 11 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/lf/server/mapper/data/DownloadMapper.java 9 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/lf/server/service/all/UploadService.java 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/lf/server/service/data/DownloadService.java 5 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/lf/server/service/show/MarkService.java 75 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/resources/mapper/data/DownloadMapper.xml 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/resources/mapper/sys/AttachMapper.xml 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
data/db_fn.sql
@@ -251,9 +251,9 @@
select st_astext(geom) from bd.dlg_25w_hfcl limit 10;
select * from lf.sys_attach where 1=1 limit 1
select * from lf.sys_download;
select * from lf.sys_user;
src/main/java/com/lf/server/config/InitConfig.java
@@ -9,6 +9,7 @@
import com.lf.server.entity.all.ResAuthEntity;
import com.lf.server.entity.bd.DlgAgnpEntity;
import com.lf.server.helper.AesHelper;
import com.lf.server.helper.FileHelper;
import com.lf.server.helper.PathHelper;
import com.lf.server.mapper.bd.DlgAgnpMapper;
import com.lf.server.service.all.BaseQueryService;
src/main/java/com/lf/server/controller/show/MarkController.java
@@ -5,9 +5,12 @@
import com.lf.server.entity.all.HttpStatus;
import com.lf.server.entity.all.ResponseMsg;
import com.lf.server.entity.ctrl.MarkJsonEntity;
import com.lf.server.entity.data.DownloadEntity;
import com.lf.server.entity.show.MarkEntity;
import com.lf.server.entity.sys.UserEntity;
import com.lf.server.helper.StringHelper;
import com.lf.server.helper.WebHelper;
import com.lf.server.service.data.DownloadService;
import com.lf.server.service.show.MarkService;
import com.lf.server.service.sys.TokenService;
import io.swagger.annotations.Api;
@@ -34,6 +37,9 @@
    @Autowired
    TokenService tokenService;
    @Autowired
    DownloadService downloadService;
    @SysLog()
    @ApiOperation(value = "分页查询并返回记录数")
@@ -238,15 +244,47 @@
                return fail("找不到标绘数据", null);
            }
            String str = markService.downloadShp(ue, list, req, res);
            String guid = markService.downloadShp(ue, list, req, res);
            if (StringHelper.isNull(guid)) {
                return fail("生成ShapeFile文件失败", null);
            }
            return success(str);
            return success(guid);
        } catch (Exception ex) {
            return fail(ex.getMessage(), null);
        }
    }
    public void downloadFile(String guid) {
    @SysLog()
    @ApiOperation(value = "下载文件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "guid", value = "文件GUID", dataType = "String", paramType = "query")
    })
    @RequestMapping(value = "/downloadFile", method = RequestMethod.GET)
    public void downloadFile(String guid, HttpServletRequest req, HttpServletResponse res) {
        try {
            UserEntity ue = tokenService.getCurrentUser(req);
            if (ue == null) {
                WebHelper.write2Page(res, WebHelper.getErrJson(HttpStatus.UNAUTHORIZED, "用户未登录"));
            }
            DownloadEntity de = downloadService.selectByGuid(guid);
            if (de == null) {
                WebHelper.write2Page(res, WebHelper.getErrJson(HttpStatus.NOT_FOUND, "文件不存在"));
            }
            de.setDcount(de.getDcount() + 1);
            de.setDownloadUser(ue.getId());
            int rows = downloadService.update(de);
            String filePath = markService.getDownloadFilePath(de);
            WebHelper.download(filePath, de.getName(), res);
        } catch (Exception ex) {
            try {
                WebHelper.write2Page(res, WebHelper.getErrJson(HttpStatus.UNAUTHORIZED, ex.getMessage()));
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
        }
    }
}
src/main/java/com/lf/server/helper/FileHelper.java
@@ -259,4 +259,21 @@
        file.delete();
    }
    /**
     * 获取相对路径
     *
     * @param file 文件
     * @return 相对路径
     */
    public static String getRelativePath(String file) {
        if (StringHelper.isEmpty(file)) {
            return null;
        }
        int idx = file.lastIndexOf(File.separator);
        int start = file.lastIndexOf(File.separator, idx - 1);
        return file.substring(start + 1);
    }
}
src/main/java/com/lf/server/helper/WebHelper.java
@@ -286,12 +286,15 @@
    /**
     * 下载文件
     *
     * @param file 文件
     * @param res  响应
     * @param file     文件
     * @param fileName 文件名
     * @param res      响应
     * @throws Exception 异常
     */
    public static void download(String file, HttpServletResponse res) throws Exception {
        String fileName = URLEncoder.encode(FileHelper.getFileName(file), "UTF-8");
    public static void download(String file, String fileName, HttpServletResponse res) throws Exception {
        if (StringHelper.isNull(fileName)) {
            fileName = URLEncoder.encode(FileHelper.getFileName(file), "UTF-8");
        }
        // 设置响应头中文件的下载方式为附件方式,以及设置文件名
        res.setHeader("Content-Disposition", "attachment; filename=" + fileName);
src/main/java/com/lf/server/mapper/data/DownloadMapper.java
@@ -1,6 +1,7 @@
package com.lf.server.mapper.data;
import com.lf.server.entity.data.DownloadEntity;
import com.lf.server.entity.sys.AttachEntity;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
@@ -47,6 +48,14 @@
    public DownloadEntity selectById(int id);
    /**
     * 根据Guid查询
     *
     * @param guid
     * @return
     */
    public DownloadEntity selectByGuid(String guid);
    /**
     * 插入一条
     *
     * @param entity
src/main/java/com/lf/server/service/all/UploadService.java
@@ -115,7 +115,7 @@
            }
            String file = pathHelper.getConfig().getUploadPath() + File.separator + entity.getPath();
            WebHelper.download(file, res);
            WebHelper.download(file, entity.getName(), res);
        } catch (Exception ex) {
            try {
                String msg = JSON.toJSONString(new ResponseMsg<String>(HttpStatus.ERROR, "文件下载出错"));
src/main/java/com/lf/server/service/data/DownloadService.java
@@ -42,6 +42,11 @@
    }
    @Override
    public DownloadEntity selectByGuid(String guid) {
        return downloadMapper.selectByGuid(guid);
    }
    @Override
    public Integer insert(DownloadEntity entity) {
        return downloadMapper.insert(entity);
    }
src/main/java/com/lf/server/service/show/MarkService.java
@@ -1,10 +1,12 @@
package com.lf.server.service.show;
import com.lf.server.entity.ctrl.MarkJsonEntity;
import com.lf.server.entity.data.DownloadEntity;
import com.lf.server.entity.show.MarkEntity;
import com.lf.server.entity.sys.UserEntity;
import com.lf.server.helper.*;
import com.lf.server.mapper.show.MarkMapper;
import com.lf.server.service.data.DownloadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@@ -25,6 +27,9 @@
    @Autowired
    PathHelper pathHelper;
    @Autowired
    DownloadService downloadService;
    @Override
    public Integer selectCount(Integer uid) {
@@ -76,8 +81,42 @@
        return markMapper.updates(list);
    }
    /**
     * 下载ShapeFile文件
     *
     * @param ue   用户实体
     * @param list 标绘JSON实体类集合
     * @param req  请求
     * @param res  响应
     * @return GUID
     * @throws Exception 异常
     */
    public String downloadShp(UserEntity ue, List<MarkJsonEntity> list, HttpServletRequest req, HttpServletResponse res) throws Exception {
        String parent = pathHelper.getTempPath(ue.getId());
        String path = createShapeFiles(ue, list, parent);
        File[] files = new File(path).listFiles();
        if (files == null || files.length == 0) {
            return null;
        }
        String zip = getZip();
        ZipHelper.zip(zip, parent);
        FileHelper.deleteDir(parent);
        String guid = FileHelper.getFileMd5(zip);
        DownloadEntity entity = downloadService.selectByGuid(guid);
        if (entity != null) {
            return entity.getGuid();
        }
        DownloadEntity de = getDownloadEntity(ue, zip);
        int rows = downloadService.insert(de);
        return rows > 0 ? de.getGuid() : null;
    }
    private String createShapeFiles(UserEntity ue, List<MarkJsonEntity> list, String parent) {
        String path = getShpDir(ue, parent);
        List<MarkJsonEntity> points = getMarkByType(list, "POINT");
@@ -93,17 +132,7 @@
            String polygonFile = GdalHelper.createShp(polygons, path, "POLYGON");
        }
        File[] files = new File(path).listFiles();
        if (files == null || files.length == 0) {
            return "Shp文件生成失败";
        }
        String zip = getZip();
        ZipHelper.zip(zip, parent);
        //download(res, zip);
        FileHelper.deleteDir(parent);
        return "";
        return path;
    }
    private String getShpDir(UserEntity ue, String parent) {
@@ -142,5 +171,29 @@
        return path;
    }
    private DownloadEntity getDownloadEntity(UserEntity ue, String file) throws Exception {
        DownloadEntity de = new DownloadEntity();
        de.setName(FileHelper.getFileName(file));
        de.setType(1);
        de.setDepid(ue.getDepid());
        de.setDcount(0);
        // de.setPwd(null)
        de.setUrl(FileHelper.getRelativePath(file));
        de.setDescr("下载ShapeFile文件");
        de.setGuid(FileHelper.getFileMd5(file));
        de.setCreateUser(ue.getId());
        // de.setGeom(null)
        return de;
    }
    /**
     * 获取下载文件路径
     *
     * @param de 下载实体类
     * @return 下载文件路径
     */
    public String getDownloadFilePath(DownloadEntity de) {
        return pathHelper.getConfig().getDownloadPath() + File.separator + de.getUrl();
    }
}
src/main/resources/mapper/data/DownloadMapper.xml
@@ -29,6 +29,10 @@
        select * from lf.sys_download where id = #{id}
    </select>
    <select id="selectByGuid" resultType="com.lf.server.entity.data.DownloadEntity">
        select * from lf.sys_download where guid = #{guid} limit 1
    </select>
    <insert id="insert" parameterType="com.lf.server.entity.data.DownloadEntity">
       insert into lf.sys_download
       (name,type,depid,dcount,pwd,url,descr,guid,create_user,create_time,download_user,download_time,geom,bak)
src/main/resources/mapper/sys/AttachMapper.xml
@@ -30,7 +30,7 @@
    </select>
    <select id="selectByGuid" resultType="com.lf.server.entity.sys.AttachEntity">
        select * from lf.sys_attach where guid = #{guid}
        select * from lf.sys_attach where guid = #{guid} limit 1
    </select>
    <insert id="insert" parameterType="com.lf.server.entity.sys.AttachEntity">