wlzboy
2025-11-07 2aebbc9601ab439707f69b08e467808df9f7549c
ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysTaskServiceImpl.java
@@ -6,7 +6,11 @@
import java.util.ArrayList;
import java.util.stream.Collectors;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import com.ruoyi.system.utils.TaskCodeGenerator;
import org.springframework.beans.factory.annotation.Autowired;
@@ -496,11 +500,12 @@
     * 
     * @param taskId 任务ID
     * @param file 文件
     * @param category 附件分类
     * @return 结果
     */
    @Override
    @Transactional
    public int uploadAttachment(Long taskId, MultipartFile file) {
    public int uploadAttachment(Long taskId, MultipartFile file, String category) {
        try {
            // 上传文件
            String fileName = FileUploadUtils.upload("/task", file);
@@ -512,6 +517,7 @@
            attachment.setFilePath(filePath);
            attachment.setFileSize(file.getSize());
            attachment.setFileType(getFileType(file.getOriginalFilename()));
            attachment.setAttachmentCategory(category);
            attachment.setUploadTime(DateUtils.getNowDate());
            attachment.setUploadBy(SecurityUtils.getUsername());
            
@@ -519,14 +525,112 @@
            
            // 记录操作日志
            if (result > 0) {
                String categoryDesc = getCategoryDesc(category);
                recordTaskLog(taskId, "UPDATE", "上传附件", null, 
                             "上传文件:" + file.getOriginalFilename(),
                             "上传文件:" + file.getOriginalFilename() + "(分类:" + categoryDesc + ")",
                             SecurityUtils.getUserId(), SecurityUtils.getUsername());
            }
            
            return result;
        } catch (IOException e) {
            throw new RuntimeException("文件上传失败:" + e.getMessage());
        }
    }
    /**
     * 从微信mediaId上传任务附件
     *
     * @param taskId 任务ID
     * @param accessToken 微信AccessToken
     * @param mediaId 微信mediaId
     * @param category 附件分类
     * @return 结果
     */
    @Override
    @Transactional
    public int uploadAttachmentFromWechat(Long taskId, String accessToken, String mediaId, String category) {
        try {
            // 从微信服务器下载文件
            String wechatUrl = String.format(
                "https://api.weixin.qq.com/cgi-bin/media/get?access_token=%s&media_id=%s",
                accessToken, mediaId
            );
            byte[] fileBytes = downloadFromUrl(wechatUrl);
            if (fileBytes == null || fileBytes.length == 0) {
                throw new RuntimeException("从微信下载文件失败");
            }
            // 生成文件名(使用mediaId作为文件名的一部分)
            String fileName = "wx_" + mediaId.substring(0, Math.min(20, mediaId.length())) + "_" + System.currentTimeMillis() + ".jpg";
            // 保存到本地
            String baseDir = FileUploadUtils.getDefaultBaseDir();
            String datePath = DateUtils.datePath();
            String uploadDir = baseDir + "/task/" + datePath;
            // 创建目录
            File uploadPath = new File(uploadDir);
            if (!uploadPath.exists()) {
                uploadPath.mkdirs();
            }
            // 保存文件
            String filePath = uploadDir + "/" + fileName;
            File file = new File(filePath);
            try (FileOutputStream fos = new FileOutputStream(file)) {
                fos.write(fileBytes);
            }
            // 保存附件记录
            SysTaskAttachment attachment = new SysTaskAttachment();
            attachment.setTaskId(taskId);
            attachment.setFileName(fileName);
            attachment.setFilePath(filePath);
            attachment.setFileSize((long) fileBytes.length);
            attachment.setFileType("jpg");
            attachment.setAttachmentCategory(category);
            attachment.setUploadTime(DateUtils.getNowDate());
            attachment.setUploadBy(SecurityUtils.getUsername());
            int result = sysTaskAttachmentMapper.insertSysTaskAttachment(attachment);
            // 记录操作日志
            if (result > 0) {
                String categoryDesc = getCategoryDesc(category);
                recordTaskLog(taskId, "UPDATE", "上传附件", null,
                             "通过微信上传文件:" + fileName + "(分类:" + categoryDesc + ")",
                             SecurityUtils.getUserId(), SecurityUtils.getUsername());
            }
            return result;
        } catch (Exception e) {
            throw new RuntimeException("从微信上传文件失败:" + e.getMessage());
        }
    }
    /**
     * 从 URL 下载文件
     */
    private byte[] downloadFromUrl(String fileUrl) throws IOException {
        URL url = new URL(fileUrl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.setConnectTimeout(10000);
        connection.setReadTimeout(30000);
        try (InputStream inputStream = connection.getInputStream()) {
            byte[] buffer = new byte[4096];
            int bytesRead;
            java.io.ByteArrayOutputStream outputStream = new java.io.ByteArrayOutputStream();
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }
            return outputStream.toByteArray();
        } finally {
            connection.disconnect();
        }
    }
@@ -561,6 +665,17 @@
        }
        
        return result;
    }
    /**
     * 根据ID获取附件详情
     *
     * @param attachmentId 附件ID
     * @return 附件详情
     */
    @Override
    public SysTaskAttachment getAttachmentById(Long attachmentId) {
        return sysTaskAttachmentMapper.selectSysTaskAttachmentByAttachmentId(attachmentId);
    }
    /**
@@ -1055,4 +1170,25 @@
        
        sysTaskWelfareMapper.insertSysTaskWelfare(welfareInfo);
    }
    /**
     * 获取附件分类描述
     *
     * @param category 附件分类代码
     * @return 分类描述
     */
    private String getCategoryDesc(String category) {
        if (category == null || category.isEmpty()) {
            return "未分类";
        }
        switch (category) {
            case "1": return "知情同意书";
            case "2": return "病人资料";
            case "3": return "操作记录";
            case "4": return "出车前";
            case "5": return "出车后";
            case "6": return "系安全带";
            default: return "其他";
        }
    }
}