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.domain.SysTaskEmergency; 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 public class TaskAttachmentSyncServiceImpl implements ITaskAttachmentSyncService { private static final Logger log = LoggerFactory.getLogger(TaskAttachmentSyncServiceImpl.class); @Autowired private SysTaskAttachmentMapper taskAttachmentMapper; @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; } /** * 批量同步待同步的任务附件到ImageData */ @Override public int batchSyncPendingAttachments() { log.info("开始执行批量附件同步任务"); try { // 查询待同步的附件列表 List pendingAttachments = taskAttachmentMapper.selectPendingSyncAttachments(); if (pendingAttachments == null || pendingAttachments.isEmpty()) { log.info("没有待同步的附件"); return 0; } log.info("查询到 {} 个待同步的附件", pendingAttachments.size()); // 按任务ID分组同步 int successCount = 0; Long currentTaskId = null; Long serviceOrderId = null; Long dispatchOrdId = null; Integer oaUserId = null; for (SysTaskAttachment attachment : pendingAttachments) { try { // 如果是新任务,需要获取任务信息 if (!attachment.getTaskId().equals(currentTaskId)) { currentTaskId = attachment.getTaskId(); // 通过联表查询已经包含了任务信息,这里需要单独查询 SysTaskEmergency emergencyInfo = getEmergencyInfoByTaskId(currentTaskId); if (emergencyInfo != null) { serviceOrderId = emergencyInfo.getLegacyServiceOrdId(); dispatchOrdId = emergencyInfo.getLegacyDispatchOrdId(); // 获取任务创建人的OA用户ID oaUserId = getCreatorOaUserId(currentTaskId); } else { log.warn("任务ID={} 的急救转运信息为空,跳过", currentTaskId); continue; } } // 同步单个附件 Long imageDataId = syncAttachmentToImageData(attachment, serviceOrderId, dispatchOrdId, oaUserId); if (imageDataId != null && imageDataId > 0) { // 更新附件同步状态 attachment.setSyncedToImageData(1); attachment.setSyncTime(new Date()); attachment.setImageDataId(imageDataId); taskAttachmentMapper.updateSysTaskAttachment(attachment); successCount++; log.info("附件ID={} 同步成功,ImageDataId={}", attachment.getAttachmentId(), imageDataId); } } catch (Exception e) { log.error("同步附件ID={} 失败", attachment.getAttachmentId(), e); // 继续处理下一个附件 } } log.info("附件同步完成,成功同步 {}/{} 个附件", successCount, pendingAttachments.size()); return successCount; } catch (Exception e) { log.error("批量附件同步异常", e); return 0; } } /** * 根据任务ID获取急救转运信息 */ @Autowired private com.ruoyi.system.mapper.SysTaskEmergencyMapper taskEmergencyMapper; private SysTaskEmergency getEmergencyInfoByTaskId(Long taskId) { return taskEmergencyMapper.selectSysTaskEmergencyByTaskId(taskId); } /** * 获取任务创建人的OA用户ID */ @Autowired private com.ruoyi.system.mapper.SysTaskMapper taskMapper; @Autowired private com.ruoyi.system.mapper.SysUserMapper userMapper; private Integer getCreatorOaUserId(Long taskId) { com.ruoyi.system.domain.SysTask task = taskMapper.selectSysTaskByTaskId(taskId); if (task != null && task.getCreatorId() != null) { com.ruoyi.common.core.domain.entity.SysUser user = userMapper.selectUserById(task.getCreatorId()); if (user != null) { return user.getOaUserId(); } } 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; } } }