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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
package com.dji.sample.media.service.impl;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dji.sample.common.model.Pagination;
import com.dji.sample.common.model.PaginationData;
import com.dji.sample.common.model.ResponseResult;
import com.dji.sample.component.oss.model.OssConfiguration;
import com.dji.sample.component.oss.service.impl.OssServiceContext;
import com.dji.sample.manage.model.dto.DeviceDictionaryDTO;
import com.dji.sample.manage.model.enums.DeviceDomainEnum;
import com.dji.sample.manage.service.IDeviceDictionaryService;
import com.dji.sample.media.dao.IFileMapper;
import com.dji.sample.media.dao.IMarkMapper;
import com.dji.sample.media.model.*;
import com.dji.sample.media.service.IFileService;
import com.dji.sample.media.util.ImgUtil;
import com.dji.sample.patches.dao.GetPatchesMapper;
import com.dji.sample.patches.model.entity.LotInfo;
import com.dji.sample.patches.utils.DistrictCodeUtils;
import com.dji.sample.patches.utils.TimerUtil;
import com.dji.sample.territory.pojo.TerritoryConfigPojo;
import com.dji.sample.territory.service.impl.TbFjServiceImpl;
import com.dji.sample.territory.utils.WaterMarkUtil;
import com.dji.sample.wayline.service.IWaylineFileService;
import com.dji.sample.wayline.service.IWaylineJobService;
import com.drew.imaging.ImageProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
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 java.awt.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;
import java.util.List;
import java.util.stream.Collectors;
 
/**
 * @author sean
 * @version 0.2
 * @date 2021/12/9
 */
@Service
@Transactional
public class FileServiceImpl implements IFileService {
 
    @Autowired
    private IFileMapper mapper;
    @Autowired
    private IMarkMapper markMapper;
    @Autowired
    private GetPatchesMapper patchesMapper;
    @Autowired
    private IDeviceDictionaryService deviceDictionaryService;
    @Autowired
    private OssServiceContext ossService;
    @Autowired
    private TerritoryConfigPojo territoryConfigPojo;
    @Autowired
    private IWaylineJobService waylineJobService;
    @Autowired
    private IWaylineFileService waylineFileService;
 
    private ObjectMapper objectMapper = new ObjectMapper();
 
 
    private Optional<MediaFileEntity> getMediaByFingerprint(String workspaceId, String fingerprint) {
        MediaFileEntity fileEntity = mapper.selectOne(new LambdaQueryWrapper<MediaFileEntity>()
                .eq(MediaFileEntity::getWorkspaceId, workspaceId)
                .eq(MediaFileEntity::getFingerprint, fingerprint));
        return Optional.ofNullable(fileEntity);
    }
 
    private Optional<MediaFileEntity> getMediaByFileId(String workspaceId, String fileId) {
        MediaFileEntity fileEntity = mapper.selectOne(new LambdaQueryWrapper<MediaFileEntity>()
                .eq(MediaFileEntity::getWorkspaceId, workspaceId)
                .eq(MediaFileEntity::getFileId, fileId));
        return Optional.ofNullable(fileEntity);
    }
 
    @Override
    public Boolean checkExist(String workspaceId, String fingerprint) {
        return this.getMediaByFingerprint(workspaceId, fingerprint).isPresent();
    }
 
    @Override
    public Integer saveFile(String workspaceId, FileUploadDTO file) {
        MediaFileEntity fileEntity = this.fileUploadConvertToEntity(file);
        fileEntity.setWorkspaceId(workspaceId);
        fileEntity.setFileId(UUID.randomUUID().toString());
        return mapper.insert(fileEntity);
    }
 
    public void saveMarkFile(String workspaceId, FileUploadDTO file) throws IOException, FontFormatException, ImageProcessingException {
        boolean endsWith = file.getObjectKey().endsWith(".mp4");
        if (endsWith) {
            MediaFileMarkEntity mediaFileMarkEntity = this.fileUploadConvertToMarkEntity(file);
            mediaFileMarkEntity.setWorkspaceId(workspaceId);
            mediaFileMarkEntity.setFileId(UUID.randomUUID().toString());
            markMapper.insert(mediaFileMarkEntity);
        } else {
            boolean contains = file.getName().contains("~");
            if (contains) {
                String name = TimerUtil.getDkbh(file.getName());
                List<LotInfo> lotInfos = patchesMapper.selectList(new LambdaQueryWrapper<LotInfo>().eq(LotInfo::getDkbh, name));
                if (!lotInfos.isEmpty()) {
                    LambdaUpdateWrapper<LotInfo> updateWrapper = new LambdaUpdateWrapper<>();
                    updateWrapper.eq(LotInfo::getDkbh, name)
                            .eq(LotInfo::getInvestigate, 0)
                            .set(LotInfo::getInvestigate, 1);
                    patchesMapper.update(null, updateWrapper);
                }
            }
            MediaFileMarkEntity mediaFileMarkEntity = this.fileUploadConvertToMarkEntity(file);
            String url = "http://139.196.74.78:9000/cloud-bucket" + file.getObjectKey();
            File file1 = TbFjServiceImpl.downloadFile(url);
            long timestamp = convertToTimestamp(file.getMetadata().getCreatedTime());
            File file2 = new File(WaterMarkUtil.addWatermark(file1, timestamp, file.getMetadata().getShootPosition().getLat(),
                    file.getMetadata().getShootPosition().getLng(), file.getMetadata().getGimbalYawDegree()).toURI());
            Object data = ImgUtil.getInfo(file1);
            mediaFileMarkEntity.setDronedata(data);
            mediaFileMarkEntity.setWorkspaceId(workspaceId);
            mediaFileMarkEntity.setIsadd(0);
            mediaFileMarkEntity.setFileId(UUID.randomUUID().toString());
            mediaFileMarkEntity.setObjectKey("/mark" + file.getPath() + "/" + file.getName());
            mediaFileMarkEntity.setFileName("mark" + file.getName());
            mediaFileMarkEntity.setFilePath("mark" + file.getPath());
            String endpoint = "http://139.196.74.78:9000";
            String accessKey = "sxkj";
            String secretKey = "sxkj2024";
            String bucketName = "cloud-bucket";
            String objectName = mediaFileMarkEntity.getObjectKey(); // 例如 "folder/file.txt"
            uploadFile(endpoint, accessKey, secretKey, bucketName, objectName, file2);
            markMapper.insert(mediaFileMarkEntity);
        }
    }
 
 
    @Override
    public Object mediaInfo(String filename) {
        String name = "mark" + filename;
        MediaFileMarkEntity entity = markMapper.selectOne(new LambdaQueryWrapper<MediaFileMarkEntity>()
                .eq(MediaFileMarkEntity::getFileName, name));
        return entity.getDronedata();
    }
 
    @Override
    public List<MediaFileDTO> getAllFilesByWorkspaceId(String workspaceId) {
        return mapper.selectList(new LambdaQueryWrapper<MediaFileEntity>()
                        .eq(MediaFileEntity::getWorkspaceId, workspaceId))
                .stream()
                .map(this::entityConvertToDto)
                .collect(Collectors.toList());
    }
 
    @Override
    public PaginationData<MediaFileEntity> MediaQuery(Integer page, Integer pageSize, Long updateStart, Long updateEnd, Long photoStart, Long photoEnd, String jobName, String workspaceId, String type) {
        // 创建分页对象
        Page<MediaFileEntity> pageObj = new Page<>(page, pageSize);
 
        // 创建查询条件对象
        LambdaQueryWrapper<MediaFileEntity> queryWrapper = new LambdaQueryWrapper<>();
 
        // 添加查询条件
        queryWrapper.eq(MediaFileEntity::getWorkspaceId, workspaceId);
 
        if (updateStart != null && updateEnd != null) {
            queryWrapper.between(MediaFileEntity::getCreateTime, updateStart, updateEnd);
        }
 
        if (photoStart != null && photoEnd != null) {
            queryWrapper.apply("JSON_UNQUOTE(JSON_EXTRACT(metadata, '$.createdTime')) BETWEEN {0} AND {1}", photoStart, photoEnd);
        }
 
        if (jobName != null && !jobName.isEmpty()) {
            List<String> jobIds = waylineJobService.getJobIds(jobName);
            // 检查 jobIds 列表是否为空
            if (jobIds.isEmpty()) {
                // 如果为空,则直接返回空的分页数据
                return new PaginationData<>(Collections.emptyList(), new Pagination(pageObj));
            }
 
            queryWrapper.in(MediaFileEntity::getJobId, jobIds);
        }
        if (type != null && !type.isEmpty()) {
            if ("图片".equals(type)) {
                queryWrapper.likeLeft(MediaFileEntity::getFileName, ".jpeg");
            } else if ("视频".equals(type)) {
                queryWrapper.likeLeft(MediaFileEntity::getFileName, ".mp4");
            }
        }
        queryWrapper.last("ORDER BY JSON_EXTRACT(metadata, '$.createdTime') DESC");
        // 执行分页查询
        Page<MediaFileEntity> resultPage = mapper.selectPage(pageObj, queryWrapper);
 
        // 处理查询结果
        List<MediaFileEntity> records = resultPage.getRecords()
                .stream()
                .peek(mediaFile -> {
                    // 获取任务名称并设置到fileId
                    String taskNameResult = waylineJobService.getName(mediaFile.getJobId());
                    mediaFile.setJobName(taskNameResult);
                })
                .collect(Collectors.toList()
                );
 
// 返回分页数据
        return new PaginationData<>(records, new Pagination(resultPage));
 
    }
 
 
    @Override
    public List<MediaFileEntity> listMediaFileEntity(String workspaceId, String jobId) {
        return mapper.selectList(new LambdaQueryWrapper<MediaFileEntity>()
                .eq(MediaFileEntity::getWorkspaceId, workspaceId).eq(MediaFileEntity::getJobId, jobId));
    }
 
    public List<MediaFileEntity> listByIsadd(String dkbh, String workspaceId) {
        return mapper.selectList(new LambdaQueryWrapper<MediaFileEntity>()
                .eq(MediaFileEntity::getWorkspaceId, workspaceId)
                .like(MediaFileEntity::getFileName, dkbh + "~"));
    }
 
    public ResponseResult updateExamByFileId(String fileId) {
        int examineStatus = getExamByFileId(fileId);
        if (examineStatus == 1) {
            LambdaUpdateWrapper<MediaFileEntity> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(MediaFileEntity::getFileId, fileId)
                    .set(MediaFileEntity::getExamine, 0);
            mapper.update(null, updateWrapper);
            return ResponseResult.success("审核未通过");
        } else if (examineStatus == 0) {
            LambdaUpdateWrapper<MediaFileEntity> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(MediaFileEntity::getFileId, fileId)
                    .set(MediaFileEntity::getExamine, 1);
            mapper.update(null, updateWrapper);
            return ResponseResult.success("审核通过");
        }
        return ResponseResult.error("媒体文件不存在");
    }
 
    public int getExamByFileId(String fileId) {
        MediaFileEntity entity = mapper.selectOne(new LambdaQueryWrapper<MediaFileEntity>()
                .eq(MediaFileEntity::getFileId, fileId));
        return entity.getExamine();
    }
 
    public String getDkbhName(String name) {
        int startIndex = name.indexOf("点") + 1;
        int endIndex = name.indexOf(".");
        return name.substring(startIndex, endIndex);
    }
 
    @Override
    public PaginationData<MediaFileEntity> getMediaFilesPaginationByWorkspaceId(String workspaceId, long page, long pageSize, MediaFileQueryParam mediaFileQueryParam) {
//        Page<MediaFileEntity> pageData = mapper.selectPage(
//                new Page<MediaFileEntity>(page, pageSize),
//                new LambdaQueryWrapper<MediaFileEntity>()
//                        .eq(MediaFileEntity::getWorkspaceId, workspaceId)
//                        .orderByDesc(MediaFileEntity::getId));
        Page<MediaFileEntity> pageData = mapper.selectPage(new Page<MediaFileEntity>(page, pageSize), new LambdaQueryWrapper<MediaFileEntity>()
                .eq(MediaFileEntity::getWorkspaceId, workspaceId)
                .orderByDesc(MediaFileEntity::getCreateTime));
 
        List<MediaFileEntity> records = pageData.getRecords()
                .stream()
                .peek(mediaFile -> {
                    // 获取任务名称并设置到fileId
                    String taskNameResult = waylineJobService.getName(mediaFile.getJobId());
                    mediaFile.setJobName(taskNameResult);
                })
                .collect(Collectors.toList());
        return new PaginationData<MediaFileEntity>(records, new Pagination(pageData));
    }
 
    public int deleteMedia(String workspaceId, String fileId) {
        int count = mapper.delete(new LambdaUpdateWrapper<MediaFileEntity>().eq(MediaFileEntity::getFileId, fileId)
                .eq(MediaFileEntity::getWorkspaceId, workspaceId));
        return count;
    }
 
    @Override
    public URL getObjectUrl(String workspaceId, String fileId) {
        Optional<MediaFileEntity> mediaFileOpt = getMediaByFileId(workspaceId, fileId);
        if (mediaFileOpt.isEmpty()) {
            throw new IllegalArgumentException("{} 不存在");
        }
 
        return ossService.getObjectUrl(OssConfiguration.bucket, mediaFileOpt.get().getObjectKey());
    }
 
    @Override
    public List<MediaFileDTO> getFilesByWorkspaceAndJobId(String workspaceId, String jobId) {
        return mapper.selectList(new LambdaQueryWrapper<MediaFileEntity>()
                        .eq(MediaFileEntity::getWorkspaceId, workspaceId)
                        .eq(MediaFileEntity::getJobId, jobId))
                .stream()
                .map(this::entityConvertToDto).collect(Collectors.toList());
    }
 
    @Override
    public Boolean updateMediaFile(String workspaceId, MediaFileEntity mediaFileEntity) {
 
        /*MediaFileEntity params = new MediaFileEntity();
        params.setFileId(mediaFileEntity.getFileId());
        params.setWorkspaceId(mediaFileEntity.getWorkspaceId());*/
        boolean update1 = new LambdaUpdateChainWrapper<>(mapper).eq(MediaFileEntity::getFileId, mediaFileEntity.getFileId())
                .set(MediaFileEntity::getFileName, mediaFileEntity.getFileName())
                .set(MediaFileEntity::getCollectStatus, mediaFileEntity.getCollectStatus())
                .set(MediaFileEntity::getUserId, mediaFileEntity.getUserId())
                .update();
//        int update = mapper.update(mediaFileEntity, Wrappers.update(params));
        return update1;
    }
 
    /**
     * Convert the received file object into a database entity object.
     *
     * @param file
     * @return
     */
    private MediaFileEntity fileUploadConvertToEntity(FileUploadDTO file) {
        MediaFileEntity.MediaFileEntityBuilder builder = MediaFileEntity.builder();
 
        if (file != null) {
            builder.fileName(file.getName())
                    .filePath(file.getPath())
                    .examine(0)
                    .fingerprint(file.getFingerprint())
                    .objectKey(file.getObjectKey())
                    .subFileType(file.getSubFileType())
                    .isOriginal(file.getExt().getIsOriginal())
                    .jobId(file.getExt().getFlightId())
                    .drone(file.getExt().getSn()).metadata(file.getMetadata())
                    .tinnyFingerprint(file.getExt().getTinnyFingerprint());
 
            // domain-type-subType
            int[] payloadModel = Arrays.stream(file.getExt().getPayloadModelKey().split("-"))
                    .map(Integer::valueOf)
                    .mapToInt(Integer::intValue)
                    .toArray();
            Optional<DeviceDictionaryDTO> payloadDict = deviceDictionaryService
                    .getOneDictionaryInfoByTypeSubType(DeviceDomainEnum.PAYLOAD.getVal(), payloadModel[1], payloadModel[2]);
            payloadDict.ifPresent(payload -> builder.payload(payload.getDeviceName()));
        }
        return builder.build();
    }
 
    private MediaFileMarkEntity fileUploadConvertToMarkEntity(FileUploadDTO file) {
        MediaFileMarkEntity.MediaFileMarkEntityBuilder builder = MediaFileMarkEntity.builder();
 
        if (file != null) {
            builder.fileName(file.getName())
                    .fingerprint(file.getFingerprint())
                    .objectKey(file.getObjectKey())
                    .subFileType(file.getSubFileType())
                    .isOriginal(file.getExt().getIsOriginal())
                    .jobId(file.getExt().getFlightId())
                    .drone(file.getExt().getSn()).metadata(file.getMetadata())
                    .tinnyFingerprint(file.getExt().getTinnyFingerprint());
 
            // domain-type-subType
            int[] payloadModel = Arrays.stream(file.getExt().getPayloadModelKey().split("-"))
                    .map(Integer::valueOf)
                    .mapToInt(Integer::intValue)
                    .toArray();
            Optional<DeviceDictionaryDTO> payloadDict = deviceDictionaryService
                    .getOneDictionaryInfoByTypeSubType(DeviceDomainEnum.PAYLOAD.getVal(), payloadModel[1], payloadModel[2]);
            payloadDict.ifPresent(payload -> builder.payload(payload.getDeviceName()));
        }
        return builder.build();
    }
 
 
    /**
     * Convert database entity objects into file data transfer object.
     *
     * @param entity
     * @return
     */
    private MediaFileDTO entityConvertToDto(MediaFileEntity entity) {
        MediaFileDTO.MediaFileDTOBuilder builder = MediaFileDTO.builder();
 
        if (entity != null) {
            builder.fileName(entity.getFileName())
                    .fileId(entity.getFileId())
                    .filePath(entity.getFilePath())
                    .isOriginal(entity.getIsOriginal())
                    .fingerprint(entity.getFingerprint())
                    .objectKey(entity.getObjectKey())
                    .tinnyFingerprint(entity.getTinnyFingerprint())
                    .payload(entity.getPayload())
                    .metadata(entity.getMetadata())
                    .createTime(LocalDateTime.ofInstant(
                            Instant.ofEpochMilli(entity.getCreateTime()), ZoneId.systemDefault()))
                    .drone(entity.getDrone())
                    .jobId(entity.getJobId());
 
        }
 
        return builder.build();
    }
 
    public static void uploadFile(String endpoint, String accessKey, String secretKey, String bucketName, String objectName, File file) {
        try {
            // 创建MinIO客户端实例
            MinioClient minioClient = MinioClient.builder()
                    .endpoint(endpoint)
                    .credentials(accessKey, secretKey)
                    .build();
            // 上传文件
            FileInputStream fileInputStream = new FileInputStream(file);
            minioClient.putObject(
                    PutObjectArgs.builder()
                            .bucket(bucketName)
                            .object(objectName)
                            .stream(fileInputStream, file.length(), -1)
                            .contentType("image/jpeg")
                            .build()
            );
            fileInputStream.close();
        } catch (Exception e) {
            throw new RuntimeException("上传文件至服务器失败" + e);
        }
    }
 
    public static long convertToTimestamp(Date date) {
        // 获取时间戳(毫秒级别)
        return date.getTime();
    }
 
    public List<MediaFileEntity> getSameJobId(String jobId) {
        return mapper.selectList(new LambdaQueryWrapper<MediaFileEntity>()
                .eq(MediaFileEntity::getJobId, jobId));
    }
 
 
    public void updateMediaFileNames(String jobId) {
        // 查询符合条件的数据
        List<MediaFileEntity> mediaFiles = mapper.selectList(new LambdaQueryWrapper<MediaFileEntity>()
                .eq(MediaFileEntity::getJobId, jobId));
        boolean allContainTilde = mediaFiles.stream().allMatch(file -> file.getFileName().contains("~"));
        boolean noneContainTilde = mediaFiles.stream().noneMatch(file -> file.getFileName().contains("~"));
 
        // 如果所有 fileName 都包含 '~' 或者都不包含 '~',将 is_add 字段改为 1
        if (allContainTilde || noneContainTilde) {
            return;
        }
 
        // 筛选出name字段不包含'~'的数据
        List<MediaFileEntity> filteredFiles = mediaFiles.stream()
                .filter(file -> !file.getFileName().contains("~"))
                .collect(Collectors.toList());
        for (MediaFileEntity currentFile : filteredFiles) {
            String currentName = currentFile.getFileName();
            Map<String, Object> currentMetadata = JSON.parseObject(JSON.toJSONString(currentFile.getMetadata()), Map.class);
            Long currentCreatedTime = (Long) currentMetadata.get("createdTime");
            // 找到metadata中的createdTime小于当前数据的createdTime且最接近的那条数据
            Optional<MediaFileEntity> closestFileOpt = mediaFiles.stream()
                    .filter(file -> {
                        Map<String, Object> metadata = JSON.parseObject(JSON.toJSONString(file.getMetadata()), Map.class);
                        Long createdTime = (Long) metadata.get("createdTime");
                        String filename = file.getFileName();
                        return createdTime < currentCreatedTime && filename.contains("~");
                    })
                    .min((file1, file2) -> {
                        Map<String, Object> metadata1 = JSON.parseObject(JSON.toJSONString(file1.getMetadata()), Map.class);
                        Map<String, Object> metadata2 = JSON.parseObject(JSON.toJSONString(file2.getMetadata()), Map.class);
                        Long time1 = (Long) metadata1.get("createdTime");
                        Long time2 = (Long) metadata2.get("createdTime");
                        return Long.compare(currentCreatedTime - time1, currentCreatedTime - time2);
                    });
            if (closestFileOpt.isEmpty()) {
                // 找不到小于的文件,尝试找大于且最接近的文件
                closestFileOpt = mediaFiles.stream()
                        .filter(file -> {
                            Map<String, Object> metadata = JSON.parseObject(JSON.toJSONString(file.getMetadata()), Map.class);
                            Long createdTime = (Long) metadata.get("createdTime");
                            String filename = file.getFileName();
                            return createdTime > currentCreatedTime && filename.contains("~");
                        })
                        .min((file1, file2) -> {
                            Map<String, Object> metadata1 = JSON.parseObject(JSON.toJSONString(file1.getMetadata()), Map.class);
                            Map<String, Object> metadata2 = JSON.parseObject(JSON.toJSONString(file2.getMetadata()), Map.class);
                            Long time1 = (Long) metadata1.get("createdTime");
                            Long time2 = (Long) metadata2.get("createdTime");
                            return Long.compare(time1 - currentCreatedTime, time2 - currentCreatedTime);
                        });
            }
            // 提取并替换name字段
            closestFileOpt.ifPresent(closestFile -> {
                String closestName = closestFile.getFileName();
                int startIndex = closestName.indexOf("V");
                if (startIndex == -1) startIndex = closestName.indexOf("W");
                if (startIndex == -1) startIndex = closestName.indexOf("Z");
                if (startIndex == -1) startIndex = closestName.indexOf("T");
                if (startIndex == -1) {
                    return;
                }
                int endIndex = closestName.indexOf(".", startIndex);
                if (endIndex == -1) {
                    return;
                }
                String replacement = closestName.substring(startIndex, endIndex);
                int currentStartIndex = currentName.indexOf("V");
                if (currentStartIndex == -1) currentStartIndex = currentName.indexOf("W");
                if (currentStartIndex == -1) currentStartIndex = currentName.indexOf("Z");
                if (currentStartIndex == -1) currentStartIndex = currentName.indexOf("T");
                if (currentStartIndex == -1) {
                    return;
                }
                int currentEndIndex = currentName.indexOf(".", currentStartIndex);
                if (currentEndIndex == -1) {
                    return;
                }
                String newName = currentName.substring(0, currentStartIndex)
                        + replacement
                        + currentName.substring(currentEndIndex);
                currentFile.setFileName(newName);
                updateMediaById(currentFile.getId(), currentFile);
            });
        }
    }
 
    @Override
    public void updateMarkMediaFileNames(String jobId) {
        try {
            // 查询符合条件的数据
            List<MediaFileMarkEntity> mediaFiles = markMapper.selectList(new LambdaQueryWrapper<MediaFileMarkEntity>()
                    .eq(MediaFileMarkEntity::getJobId, jobId));
 
            // 判断所有 fileName 是否都包含 '~' 或者都不包含 '~'
            boolean allContainTilde = mediaFiles.stream().allMatch(file -> file.getFileName().contains("~"));
            boolean noneContainTilde = mediaFiles.stream().noneMatch(file -> file.getFileName().contains("~"));
 
            // 如果所有 fileName 都包含 '~' 或者都不包含 '~',将 is_add 字段改为 1
            if (allContainTilde || noneContainTilde) {
                mediaFiles.forEach(file -> {
                    file.setIsadd(1);
                    updateById(file.getId(), file);
                });
                return;
            }
 
            // 筛选出name字段不包含'~'的数据
            List<MediaFileMarkEntity> filteredFiles = mediaFiles.stream()
                    .filter(file -> !file.getFileName().contains("~"))
                    .collect(Collectors.toList());
 
            for (MediaFileMarkEntity currentFile : filteredFiles) {
                String currentName = currentFile.getFileName();
                Map<String, Object> currentMetadata = JSON.parseObject(JSON.toJSONString(currentFile.getMetadata()), Map.class);
                Long currentCreatedTime = (Long) currentMetadata.get("createdTime");
 
                // 找到metadata中的createdTime小于当前数据的createdTime且最接近的那条数据
                Optional<MediaFileMarkEntity> closestFileOpt = mediaFiles.stream()
                        .filter(file -> {
                            Map<String, Object> metadata = JSON.parseObject(JSON.toJSONString(file.getMetadata()), Map.class);
                            Long createdTime = (Long) metadata.get("createdTime");
                            String filename = file.getFileName();
                            return createdTime < currentCreatedTime && filename.contains("~");
                        })
                        .min((file1, file2) -> {
                            Map<String, Object> metadata1 = JSON.parseObject(JSON.toJSONString(file1.getMetadata()), Map.class);
                            Map<String, Object> metadata2 = JSON.parseObject(JSON.toJSONString(file2.getMetadata()), Map.class);
                            Long time1 = (Long) metadata1.get("createdTime");
                            Long time2 = (Long) metadata2.get("createdTime");
                            return Long.compare(currentCreatedTime - time1, currentCreatedTime - time2);
                        });
 
                if (closestFileOpt.isEmpty()) {
                    // 找不到小于的文件,尝试找大于且最接近的文件
                    closestFileOpt = mediaFiles.stream()
                            .filter(file -> {
                                Map<String, Object> metadata = JSON.parseObject(JSON.toJSONString(file.getMetadata()), Map.class);
                                Long createdTime = (Long) metadata.get("createdTime");
                                String filename = file.getFileName();
                                return createdTime > currentCreatedTime && filename.contains("~");
                            })
                            .min((file1, file2) -> {
                                Map<String, Object> metadata1 = JSON.parseObject(JSON.toJSONString(file1.getMetadata()), Map.class);
                                Map<String, Object> metadata2 = JSON.parseObject(JSON.toJSONString(file2.getMetadata()), Map.class);
                                Long time1 = (Long) metadata1.get("createdTime");
                                Long time2 = (Long) metadata2.get("createdTime");
                                return Long.compare(time1 - currentCreatedTime, time2 - currentCreatedTime);
                            });
                }
 
                // 提取并替换name字段
                closestFileOpt.ifPresent(closestFile -> {
                    String closestName = closestFile.getFileName();
                    int startIndex = closestName.indexOf("V");
                    if (startIndex == -1) startIndex = closestName.indexOf("W");
                    if (startIndex == -1) startIndex = closestName.indexOf("Z");
                    if (startIndex == -1) startIndex = closestName.indexOf("T");
                    if (startIndex == -1) {
                        return;
                    }
                    int endIndex = closestName.indexOf(".", startIndex);
                    if (endIndex == -1) {
                        return;
                    }
                    String replacement = closestName.substring(startIndex, endIndex);
 
                    int currentStartIndex = currentName.indexOf("V");
                    if (currentStartIndex == -1) currentStartIndex = currentName.indexOf("W");
                    if (currentStartIndex == -1) currentStartIndex = currentName.indexOf("Z");
                    if (currentStartIndex == -1) currentStartIndex = currentName.indexOf("T");
                    if (currentStartIndex == -1) {
                        return;
                    }
                    int currentEndIndex = currentName.indexOf(".", currentStartIndex);
                    if (currentEndIndex == -1) {
                        return;
                    }
                    String newName = currentName.substring(0, currentStartIndex)
                            + replacement
                            + currentName.substring(currentEndIndex);
                    currentFile.setFileName(newName);
                    updateById(currentFile.getId(), currentFile);
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
 
    public void updateById(Integer id, MediaFileMarkEntity entity) {
        entity.setIsadd(1);
        UpdateWrapper<MediaFileMarkEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id);
        markMapper.update(entity, updateWrapper);
    }
 
    public void updateMediaById(Integer id, MediaFileEntity entity) {
        UpdateWrapper<MediaFileEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id);
        mapper.update(entity, updateWrapper);
    }
 
    public void getNoaddFile() {
        List<MediaFileMarkEntity> markEntities = markMapper.selectList(new LambdaQueryWrapper<MediaFileMarkEntity>().eq(MediaFileMarkEntity::getIsadd, 0));
        for (MediaFileMarkEntity mark : markEntities) {
            updateMarkMediaFileNames(mark.getJobId());
            updateMediaFileNames(mark.getJobId());
        }
    }
 
}