吉安感知网项目-后端
rain
2026-01-23 9afbe6e5183fbc3dbccb74169e0079d600169f25
附件上传优化
4 files modified
88 ■■■■■ changed files
drone-service/drone-gd/src/main/java/org/sxkj/gd/workorder/controller/GdPatrolTaskController.java 47 ●●●●● patch | view | raw | blame | history
drone-service/drone-gd/src/main/java/org/sxkj/gd/workorder/service/IGdPatrolTaskService.java 5 ●●●●● patch | view | raw | blame | history
drone-service/drone-gd/src/main/java/org/sxkj/gd/workorder/service/impl/GdPatrolTaskServiceImpl.java 32 ●●●●● patch | view | raw | blame | history
drone-service/drone-gd/src/main/java/org/sxkj/gd/workorder/utils/GdPatrolReportWordUtil.java 4 ●●●● patch | view | raw | blame | history
drone-service/drone-gd/src/main/java/org/sxkj/gd/workorder/controller/GdPatrolTaskController.java
@@ -35,13 +35,11 @@
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.sxkj.gd.common.IdParam;
import org.sxkj.gd.workorder.entity.GdPatrolTaskEntity;
import org.sxkj.gd.workorder.entity.GdWorkOrderEntity;
import org.sxkj.gd.workorder.enums.PatrolTaskStatusEnum;
import org.sxkj.gd.workorder.param.GdPatrolTaskAddParam;
import org.sxkj.gd.workorder.param.GdPatrolTaskAuditParam;
import org.sxkj.gd.workorder.param.GdPatrolTaskPageParam;
import org.sxkj.gd.workorder.param.GdPatrolTaskUpdateParam;
import org.sxkj.gd.workorder.vo.GdClueEventVO;
import org.sxkj.gd.workorder.vo.GdPatrolTaskVO;
import org.sxkj.gd.workorder.excel.GdPatrolTaskExcel;
import org.sxkj.gd.workorder.wrapper.GdPatrolTaskWrapper;
@@ -50,15 +48,9 @@
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.excel.util.ExcelUtil;
import org.springblade.core.tool.constant.BladeConstant;
import org.springblade.core.tool.utils.StringUtil;
import org.sxkj.system.cache.SysCache;
import springfox.documentation.annotations.ApiIgnore;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.Map;
import java.util.List;
@@ -192,44 +184,11 @@
    @PostMapping("/export-report")
    @ApiOperationSupport(order = 13)
    @ApiOperation(value = "导出巡查报告", notes = "传入巡查任务id")
    public R<String> exportReport(@RequestParam Long id) {
    public R<Boolean> exportReport(@RequestParam Long id) {
        if (id == null) {
            return R.fail("巡查任务id不能为空");
        }
        File reportFile = gdPatrolTaskService.exportPatrolReport(id);
        File savedFile = saveReportToResources(reportFile, id);
        return R.data(savedFile.getAbsolutePath());
        boolean saved = gdPatrolTaskService.exportPatrolReport(id);
        return R.data(saved);
    }
    private File saveReportToResources(File reportFile, Long patrolTaskId) {
        if (reportFile == null || !reportFile.exists()) {
            throw new RuntimeException("生成的巡查报告文件不存在");
        }
        Path resourcesDir = resolveResourcesDir();
        String fileName = "gd_patrol_report_" + patrolTaskId + "_" + DateUtil.time() + ".docx";
        Path targetPath = resourcesDir.resolve(fileName);
        try {
            Files.createDirectories(resourcesDir);
            Files.copy(reportFile.toPath(), targetPath, StandardCopyOption.REPLACE_EXISTING);
            return targetPath.toFile();
        } catch (Exception e) {
            throw new RuntimeException("保存巡查报告失败", e);
        }
    }
    private Path resolveResourcesDir() {
        String userDir = System.getProperty("user.dir");
        if (StringUtil.isBlank(userDir)) {
            throw new RuntimeException("无法获取工作目录");
        }
        Path basePath = Paths.get(userDir);
        if (basePath.endsWith("drone-gd")) {
            return basePath.resolve("src").resolve("main").resolve("resources");
        }
        if (basePath.endsWith("drone-service")) {
            return basePath.resolve("drone-gd").resolve("src").resolve("main").resolve("resources");
        }
        return basePath.resolve("drone-service").resolve("drone-gd").resolve("src").resolve("main").resolve("resources");
    }
}
drone-service/drone-gd/src/main/java/org/sxkj/gd/workorder/service/IGdPatrolTaskService.java
@@ -25,7 +25,6 @@
import org.sxkj.gd.workorder.excel.GdPatrolTaskExcel;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.core.mp.base.BaseService;
import java.io.File;
import java.util.List;
/**
@@ -78,8 +77,8 @@
     * 生成巡查报告
     *
     * @param patrolTaskId 巡查任务主键
     * @return 报告文件
     * @return 是否上传附件成果成功
     */
    File exportPatrolReport(Long patrolTaskId);
    boolean exportPatrolReport(Long patrolTaskId);
}
drone-service/drone-gd/src/main/java/org/sxkj/gd/workorder/service/impl/GdPatrolTaskServiceImpl.java
@@ -362,7 +362,7 @@
    }
    @Override
    public File exportPatrolReport(Long patrolTaskId) {
    public boolean exportPatrolReport(Long patrolTaskId) {
        if (patrolTaskId == null) {
            throw new RuntimeException("巡查任务主键不能为空");
        }
@@ -377,8 +377,7 @@
        String deptName = getDeptName(taskEntity.getCreateDept());
        try {
            File reportFile = GdPatrolReportWordUtil.generateReportFile(taskEntity, resultList, creatorName, deptName);
            saveAttachFile(reportFile, taskEntity);
            return reportFile;
            return saveAttachFile(reportFile, taskEntity);
        } catch (Exception e) {
            throw new RuntimeException("生成巡查报告失败", e);
        }
@@ -486,34 +485,35 @@
        return StringUtil.isBlank(realName) ? "/" : realName;
    }
    private BladeFile saveAttachFile(File reportFile, GdPatrolTaskEntity taskEntity) throws IOException {
    private boolean saveAttachFile(File reportFile, GdPatrolTaskEntity taskEntity) throws IOException {
        if (reportFile == null || !reportFile.exists()) {
            throw new RuntimeException("巡查报告文件不存在");
        }
        String fileName = reportFile.getName();
        String desiredName = buildReportAttachName(taskEntity, reportFile.getName());
        BladeFile bladeFile = attachClient.putFile(
            GdMultipartFileUtil.fromFile(reportFile,
                "application/vnd.openxmlformats-officedocument.wordprocessingml.document"),
            fileName
            desiredName
        );
        if (bladeFile == null || StringUtil.isBlank(bladeFile.getLink())) {
            throw new RuntimeException("附件上传失败");
        }
        Integer resultType = parseResultType(taskEntity != null ? taskEntity.getPatrolTaskType() : null);
        Attach attach = buildAttachInfo(taskEntity, reportFile, bladeFile, resultType);
        Attach attach = buildAttachInfo(taskEntity, reportFile, bladeFile, resultType, desiredName);
        Boolean saved = attachClient.saveAttachInfo(attach);
        if (!Boolean.TRUE.equals(saved)) {
            log.error("附件信息保存失败,报告文件:"+fileName);
            log.error("附件信息保存失败,报告文件:" + desiredName);
            return false;
        }
        return bladeFile;
        return true;
    }
    private Attach buildAttachInfo(GdPatrolTaskEntity taskEntity, File reportFile, BladeFile bladeFile, Integer resultType) {
    private Attach buildAttachInfo(GdPatrolTaskEntity taskEntity, File reportFile, BladeFile bladeFile, Integer resultType, String desiredName) {
        Attach attach = new Attach();
        attach.setDomainUrl(bladeFile.getDomain());
        attach.setLink(bladeFile.getLink());
        attach.setName(bladeFile.getName());
        attach.setOriginalName(StringUtil.isBlank(bladeFile.getOriginalName()) ? reportFile.getName() : bladeFile.getOriginalName());
        attach.setName(desiredName);
        attach.setOriginalName(desiredName);
        attach.setAttachSize(reportFile.length());
        attach.setExtension(FileUtil.getFileExtension(reportFile.getName()));
        attach.setResultType(resultType);
@@ -538,6 +538,14 @@
        return attach;
    }
    private String buildReportAttachName(GdPatrolTaskEntity taskEntity, String fallbackName) {
        String taskNo = taskEntity != null ? taskEntity.getTaskNo() : null;
        if (StringUtil.isBlank(taskNo)) {
            return fallbackName;
        }
        return taskNo + ".docx";
    }
    private Integer parseResultType(String patrolTaskType) {
        if (StringUtil.isBlank(patrolTaskType)) {
            return null;
drone-service/drone-gd/src/main/java/org/sxkj/gd/workorder/utils/GdPatrolReportWordUtil.java
@@ -129,8 +129,8 @@
        return File.createTempFile(safePrefix, ".docx");
    }
    private static String buildSafeFilePrefix(String text) {
        String baseName = StringUtil.isBlank(text) ? "gd_patrol_report" : "gd_patrol_report_" + text;
    private static String buildSafeFilePrefix(String taskNo) {
        String baseName = StringUtil.isBlank(taskNo) ? "gd_patrol_report" : taskNo;
        String safeName = baseName.replaceAll("[^a-zA-Z0-9_-]", "_");
        if (safeName.length() < 3) {
            safeName = "gd_patrol_report";