From aba5f5bf919b7023744dc70ea28d479656e07c9b Mon Sep 17 00:00:00 2001
From: rain <167982779@qq.com>
Date: Mon, 08 Jul 2024 18:36:21 +0800
Subject: [PATCH] 更新图片信息返回提示信息

---
 src/main/java/com/dji/sample/media/service/impl/FileServiceImpl.java |  320 ++++++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 280 insertions(+), 40 deletions(-)

diff --git a/src/main/java/com/dji/sample/media/service/impl/FileServiceImpl.java b/src/main/java/com/dji/sample/media/service/impl/FileServiceImpl.java
index 500c5da..20eca7e 100644
--- a/src/main/java/com/dji/sample/media/service/impl/FileServiceImpl.java
+++ b/src/main/java/com/dji/sample/media/service/impl/FileServiceImpl.java
@@ -1,6 +1,9 @@
 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;
@@ -28,8 +31,12 @@
 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;
@@ -72,6 +79,9 @@
     @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)
@@ -92,22 +102,12 @@
     }
 
     @Override
-    public Integer saveFile(String workspaceId, FileUploadDTO file) {
+    public Integer saveFile(String workspaceId, FileUploadDTO file) throws ImageProcessingException, IOException {
         MediaFileEntity fileEntity = this.fileUploadConvertToEntity(file);
         fileEntity.setWorkspaceId(workspaceId);
         fileEntity.setFileId(UUID.randomUUID().toString());
-        updateInvestigate(file.getExt().getFlightId());
-        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 {
+        Integer count= mapper.insert(fileEntity);
+        try {
             boolean contains = file.getName().contains("~");
             if (contains) {
                 String name = TimerUtil.getDkbh(file.getName());
@@ -120,9 +120,22 @@
                     patchesMapper.update(null, updateWrapper);
                 }
             }
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return count;
+    }
 
+    public void saveMarkFile(String workspaceId, FileUploadDTO file) throws IOException, FontFormatException, ImageProcessingException {
+        boolean endsWith = file.getObjectKey().endsWith(".mp4");
+        if (endsWith) {
             MediaFileMarkEntity mediaFileMarkEntity = this.fileUploadConvertToMarkEntity(file);
-            String url = "http://dev.jxpskj.com:9000/cloud-bucket" + file.getObjectKey();
+            mediaFileMarkEntity.setWorkspaceId(workspaceId);
+            mediaFileMarkEntity.setFileId(UUID.randomUUID().toString());
+            markMapper.insert(mediaFileMarkEntity);
+        }
+            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(),
@@ -130,26 +143,32 @@
             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://dev.jxpskj.com:9000";
-            String accessKey = "pskj";
-            String secretKey = "pskj@2021";
+            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;
+        String name = "mark"+filename;
         MediaFileMarkEntity entity = markMapper.selectOne(new LambdaQueryWrapper<MediaFileMarkEntity>()
                 .eq(MediaFileMarkEntity::getFileName, name));
-        return entity.getDronedata();
+        if (entity!=null){
+        return entity.getDronedata();}
+        else {
+            return "该图片信息正在加载";
+        }
     }
 
     @Override
@@ -162,7 +181,7 @@
     }
 
     @Override
-    public PaginationData<MediaFileEntity> MediaQuery(Integer page, Integer pageSize, Long updateStart, Long updateEnd, Long photoStart, Long photoEnd, String jobName, String workspaceId,String type) {
+    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);
 
@@ -170,7 +189,7 @@
         LambdaQueryWrapper<MediaFileEntity> queryWrapper = new LambdaQueryWrapper<>();
 
         // 添加查询条件
-        queryWrapper.eq(MediaFileEntity::getWorkspaceId,workspaceId);
+        queryWrapper.eq(MediaFileEntity::getWorkspaceId, workspaceId);
 
         if (updateStart != null && updateEnd != null) {
             queryWrapper.between(MediaFileEntity::getCreateTime, updateStart, updateEnd);
@@ -182,7 +201,13 @@
 
         if (jobName != null && !jobName.isEmpty()) {
             List<String> jobIds = waylineJobService.getJobIds(jobName);
-                queryWrapper.in(MediaFileEntity::getJobId, jobIds);
+            // 检查 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)) {
@@ -191,7 +216,7 @@
                 queryWrapper.likeLeft(MediaFileEntity::getFileName, ".mp4");
             }
         }
-        queryWrapper.orderByDesc(MediaFileEntity::getCreateTime);
+        queryWrapper.last("ORDER BY JSON_EXTRACT(metadata, '$.createdTime') DESC");
         // 执行分页查询
         Page<MediaFileEntity> resultPage = mapper.selectPage(pageObj, queryWrapper);
 
@@ -209,21 +234,6 @@
 // 返回分页数据
         return new PaginationData<>(records, new Pagination(resultPage));
 
-    }
-
-    public void updateInvestigate(String jobId){
-        String waylineId=waylineJobService.getWaylineId(jobId);
-        String patchesId=waylineFileService.getPatchesId(waylineId);
-        if (patchesId==null){
-            return;
-        }
-        List<Long> ids = Arrays.stream(patchesId.split(","))
-                .map(Long::parseLong)
-                .collect(Collectors.toList());
-        LambdaUpdateWrapper<LotInfo> updateWrapper = new LambdaUpdateWrapper<>();
-        updateWrapper.in(LotInfo::getId, ids)
-                .set(LotInfo::getInvestigate, 1);
-        patchesMapper.update(null, updateWrapper);
     }
 
 
@@ -261,6 +271,12 @@
         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
@@ -337,7 +353,7 @@
         if (file != null) {
             builder.fileName(file.getName())
                     .filePath(file.getPath())
-                    .examine(1)
+                    .examine(0)
                     .fingerprint(file.getFingerprint())
                     .objectKey(file.getObjectKey())
                     .subFileType(file.getSubFileType())
@@ -427,6 +443,7 @@
                             .bucket(bucketName)
                             .object(objectName)
                             .stream(fileInputStream, file.length(), -1)
+                            .contentType("image/jpeg")
                             .build()
             );
             fileInputStream.close();
@@ -439,6 +456,229 @@
         // 获取时间戳(毫秒级别)
         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());
+        }
+    }
+
 }
 
 
+
+
+

--
Gitblit v1.9.3