wlzboy
2025-11-08 cdcc529ce5fb9aa0dd1dea5bb2a620fc09b7d25c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
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<SysTaskAttachment> syncTaskAttachmentsToImageData(List<SysTaskAttachment> 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;
        }
    }
}