package com.ruoyi.system.service.impl; import com.ruoyi.common.annotation.DataSource; import com.ruoyi.common.config.LegacySystemConfig; import com.ruoyi.common.config.RuoYiConfig; import com.ruoyi.common.enums.DataSourceType; import com.ruoyi.system.domain.ImageData; import com.ruoyi.system.domain.SysTaskAttachment; import com.ruoyi.system.file.FileUploadResponse; import com.ruoyi.system.file.IFileUploadService; import com.ruoyi.system.imagedata.IImageDataService; import com.ruoyi.system.mapper.SysTaskAttachmentMapper; import com.ruoyi.system.service.ITaskAttachmentSyncService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.StringUtils; import java.io.File; import java.util.Date; import java.util.List; /** * 任务附件同步服务实现 * 将任务附件同步到ImageData表 * * @author ruoyi */ @Service @DataSource(DataSourceType.SQLSERVER) public class TaskAttachmentSyncServiceImpl implements ITaskAttachmentSyncService { private static final Logger log = LoggerFactory.getLogger(TaskAttachmentSyncServiceImpl.class); @Autowired private IImageDataService imageDataService; @Autowired private LegacySystemConfig legacyConfig; @Autowired private IFileUploadService fileUploadService; @Autowired private RuoYiConfig ruoYiConfig; /** * 同步单个附件到ImageData */ @Override public Long syncAttachmentToImageData(SysTaskAttachment attachment, Long serviceOrderId, Long dispatchOrdId,Integer oaUserId) { if (attachment == null) { log.error("附件对象为空,无法同步"); return 0L; } // 检查是否已同步 if (attachment.getSyncedToImageData() != null && attachment.getSyncedToImageData() == 1) { log.info("附件ID={} 已同步,跳过", attachment.getAttachmentId()); return 0L; } // 检查必要参数 if (dispatchOrdId == null || serviceOrderId == null) { log.error("调度单ID和服务单ID都为空,无法同步附件ID={}", attachment.getAttachmentId()); return 0L; } try { // 获取附件分类对应的ImageType Integer imageType = getImageTypeByCategory(attachment.getAttachmentCategory()); // 获取附件本地文件路径 String filePath = attachment.getFilePath(); if (!StringUtils.hasText(filePath)) { log.error("附件ID={} 的文件路径为空", attachment.getAttachmentId()); return 0L; } // 构建完整的本地文件路径 String fullFilePath = ruoYiConfig.getProfile() + filePath; File file = new File(fullFilePath); if (!file.exists()) { log.error("附件ID={} 对应的文件不存在: {}", attachment.getAttachmentId(), fullFilePath); return 0L; } // 生成目标路径(使用dispatchOrdId作为目录) String targetPath = dispatchOrdId != null ? dispatchOrdId.toString() : "default"; // 使用文件上传服务上传到文件服务器(包含缩略图生成) log.info("开始上传附件ID={} 到文件服务器,目标路径={}", attachment.getAttachmentId(), targetPath); FileUploadResponse uploadResponse = fileUploadService.uploadLocalFileWithThumbnail(file, targetPath); if (!uploadResponse.isSuccess()) { log.error("附件ID={} 上传到文件服务器失败: {}", attachment.getAttachmentId(), uploadResponse.getMessage()); return 0L; } log.info("附件ID={} 上传成功,原图路径={}, 缩略图路径={}", attachment.getAttachmentId(), uploadResponse.getFilePath(), uploadResponse.getThumbnailPath()); // 创建ImageData对象 ImageData imageData = new ImageData(); imageData.setDOrdIDDt(dispatchOrdId); imageData.setSOrdIDDt(serviceOrderId); imageData.setImageType(imageType); imageData.setImageUrl(uploadResponse.getFilePath()); imageData.setImageUrls(uploadResponse.getThumbnailPath()); // 缩略图路径 imageData.setUpImageTime(attachment.getUploadTime() != null ? attachment.getUploadTime() : new Date()); imageData.setUpImageOAid(oaUserId); imageData.setImageDel(0); // 插入ImageData int result = imageDataService.insertImageData(imageData); if (result > 0) { log.info("成功同步附件ID={} 到ImageData,ImageDataId={}", attachment.getAttachmentId(), imageData.getId()); return imageData.getId(); } else { log.error("同步附件ID={} 到ImageData失败", attachment.getAttachmentId()); return 0L; } } catch (Exception e) { log.error("同步附件ID={} 到ImageData时发生异常", attachment.getAttachmentId(), e); return 0L; } } /** * 批量同步任务的所有附件到ImageData */ @Override public List syncTaskAttachmentsToImageData(List attachments, Long serviceOrderId, Long dispatchOrdId,Integer oaUserId) { if (attachments == null || attachments.isEmpty()) { log.info("没有附件,无需同步"); return null; } int successCount = 0; for (SysTaskAttachment attachment : attachments) { Long imageDataId = syncAttachmentToImageData(attachment, serviceOrderId, dispatchOrdId,oaUserId); if (imageDataId>0) { attachment.setSyncedToImageData(1); attachment.setSyncTime(new Date()); attachment.setImageDataId(imageDataId); successCount++; } } log.info(" 共同步 {}/{} 个附件到ImageData", successCount, attachments.size()); return null; } /** * 根据附件分类获取对应的ImageType * * 附件分类映射: * 1-知情同意书 -> 0 * 2-病人资料 -> 1 * 3-操作记录 -> 2 * 4-出车前 -> 3 * 5-出车后 -> 4 * 6-系安全带 -> 5 */ private Integer getImageTypeByCategory(String category) { if (category == null) { return 1; } switch (category) { case "1": // 知情同意书 return 1; case "2": // 病人资料 return 2; case "3": // 操作记录 return 3; case "4": // 出车前 return 4; case "5": // 出车后 return 5; case "6": // 系安全带 return 6; default: return 1; } } }