wlzboy
7 小时以前 5f2ee03958a1a16dc27195c76ea7cffb422c95d1
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
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<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;
    }
    
    /**
     * 批量同步待同步的任务附件到ImageData
     */
    @Override
    public int batchSyncPendingAttachments() {
        log.info("开始执行批量附件同步任务");
        
        try {
            // 查询待同步的附件列表
            List<SysTaskAttachment> 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;
        }
    }
}