吉安感知网项目-后端
rain
2026-01-23 fc32ccf83f95b46500cbb5b397a43a38a150beac
附件上传
2 files modified
1 files added
128 ■■■■■ changed files
drone-service/drone-gd/pom.xml 4 ●●●● patch | view | raw | blame | history
drone-service/drone-gd/src/main/java/org/sxkj/gd/common/GdMultipartFileUtil.java 46 ●●●●● patch | view | raw | blame | history
drone-service/drone-gd/src/main/java/org/sxkj/gd/workorder/service/impl/GdPatrolTaskServiceImpl.java 78 ●●●●● patch | view | raw | blame | history
drone-service/drone-gd/pom.xml
@@ -113,6 +113,10 @@
            <artifactId>javase</artifactId>
            <version>3.5.3</version>
        </dependency>
        <dependency>
            <groupId>org.springblade</groupId>
            <artifactId>drone-resource-api</artifactId>
        </dependency>
    </dependencies>
    <build>
drone-service/drone-gd/src/main/java/org/sxkj/gd/common/GdMultipartFileUtil.java
New file
@@ -0,0 +1,46 @@
/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.sxkj.gd.common;
import org.springblade.core.tool.utils.StringUtil;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class GdMultipartFileUtil {
    private GdMultipartFileUtil() {
    }
    public static MultipartFile fromFile(File file, String contentType) throws IOException {
        if (file == null || !file.exists()) {
            throw new RuntimeException("文件不存在");
        }
        String fileName = file.getName();
        String finalContentType = StringUtil.isBlank(contentType) ? "application/octet-stream" : contentType;
        try (FileInputStream inputStream = new FileInputStream(file)) {
            return new MockMultipartFile(fileName, fileName, finalContentType, inputStream);
        }
    }
    public static MultipartFile fromFile(File file) throws IOException {
        return fromFile(file, "application/octet-stream");
    }
}
drone-service/drone-gd/src/main/java/org/sxkj/gd/workorder/service/impl/GdPatrolTaskServiceImpl.java
@@ -21,6 +21,7 @@
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.commons.lang3.StringUtils;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.oss.model.BladeFile;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
@@ -30,6 +31,7 @@
import org.sxkj.common.utils.OrderNumUtils;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.constant.BladeConstant;
import org.springblade.core.tool.utils.FileUtil;
import org.springblade.core.tool.utils.StringUtil;
import org.sxkj.gd.workorder.entity.GdPatrolTaskEntity;
import org.sxkj.gd.workorder.entity.GdTaskResultEntity;
@@ -47,12 +49,16 @@
import org.sxkj.gd.workorder.service.IGdWorkOrderFlowService;
import org.sxkj.gd.workorder.utils.GdPatrolReportWordUtil;
import org.sxkj.gd.workorder.vo.GdPatrolTaskVO;
import org.sxkj.gd.common.GdMultipartFileUtil;
import org.sxkj.resource.entity.Attach;
import org.sxkj.resource.feign.IAttachClient;
import org.sxkj.system.entity.Dept;
import org.sxkj.system.entity.User;
import org.sxkj.system.feign.ISysClient;
import org.sxkj.system.feign.IUserClient;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
@@ -74,6 +80,8 @@
    private ISysClient sysClient;
    @Autowired
    private IUserClient userClient;
    @Autowired
    private IAttachClient attachClient;
    @Autowired
    private IGdTaskResultService gdTaskResultService;
    @Override
@@ -368,7 +376,9 @@
        String creatorName = getUserName(taskEntity.getCreateUser());
        String deptName = getDeptName(taskEntity.getCreateDept());
        try {
            return GdPatrolReportWordUtil.generateReportFile(taskEntity, resultList, creatorName, deptName);
            File reportFile = GdPatrolReportWordUtil.generateReportFile(taskEntity, resultList, creatorName, deptName);
            saveAttachFile(reportFile, taskEntity);
            return reportFile;
        } catch (Exception e) {
            throw new RuntimeException("生成巡查报告失败", e);
        }
@@ -476,5 +486,71 @@
        return StringUtil.isBlank(realName) ? "/" : realName;
    }
    private BladeFile saveAttachFile(File reportFile, GdPatrolTaskEntity taskEntity) throws IOException {
        if (reportFile == null || !reportFile.exists()) {
            throw new RuntimeException("巡查报告文件不存在");
        }
        String fileName = reportFile.getName();
        BladeFile bladeFile = attachClient.putFile(
            GdMultipartFileUtil.fromFile(reportFile,
                "application/vnd.openxmlformats-officedocument.wordprocessingml.document"),
            fileName
        );
        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);
        Boolean saved = attachClient.saveAttachInfo(attach);
        if (!Boolean.TRUE.equals(saved)) {
            log.error("附件信息保存失败,报告文件:"+fileName);
        }
        return bladeFile;
    }
    private Attach buildAttachInfo(GdPatrolTaskEntity taskEntity, File reportFile, BladeFile bladeFile, Integer resultType) {
        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.setAttachSize(reportFile.length());
        attach.setExtension(FileUtil.getFileExtension(reportFile.getName()));
        attach.setResultType(resultType);
        Long createUser = taskEntity != null ? taskEntity.getCreateUser() : null;
        Long updateUser = taskEntity != null ? taskEntity.getUpdateUser() : null;
        Long createDept = taskEntity != null ? taskEntity.getCreateDept() : null;
        if (createUser == null) {
            createUser = AuthUtil.getUserId();
        }
        if (updateUser == null) {
            updateUser = createUser;
        }
        if (createDept == null) {
            createDept = Long.valueOf(AuthUtil.getDeptId());
        }
        Date now = new Date();
        attach.setCreateUser(createUser);
        attach.setUpdateUser(updateUser);
        attach.setCreateDept(createDept);
        attach.setCreateTime(now);
        attach.setUpdateTime(now);
        return attach;
    }
    private Integer parseResultType(String patrolTaskType) {
        if (StringUtil.isBlank(patrolTaskType)) {
            return null;
        }
        try {
            Integer type = Integer.valueOf(patrolTaskType);
            if (type < 1 || type > 5) {
                return null;
            }
            return type;
        } catch (NumberFormatException ex) {
            return null;
        }
    }
}