Bläddra i källkod

提交地磅计量初始化

chenxiaofei 4 månader sedan
förälder
incheckning
681bd6a3fe

+ 2 - 0
iot-platform-manager/src/main/java/com/platform/IotPlatformManagerApplication.java

@@ -3,6 +3,7 @@ package com.platform;
 import org.mybatis.spring.annotation.MapperScan;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.scheduling.annotation.EnableAsync;
 import org.springframework.scheduling.annotation.EnableScheduling;
 
 /**
@@ -15,6 +16,7 @@ import org.springframework.scheduling.annotation.EnableScheduling;
 @SpringBootApplication
 @MapperScan("com.platform.mapper")
 @EnableScheduling
+@EnableAsync
 public class IotPlatformManagerApplication {
     public static void main(String[] args) {
         SpringApplication.run(IotPlatformManagerApplication.class, args);

+ 59 - 11
iot-platform-manager/src/main/java/com/platform/api/manager/WeighbridgeRecordManage.java

@@ -14,6 +14,7 @@ import com.platform.utils.FileUtils;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.StringUtils;
+import org.springframework.scheduling.annotation.Async;
 import org.springframework.stereotype.Service;
 import org.springframework.web.multipart.MultipartFile;
 
@@ -21,6 +22,7 @@ import java.time.Instant;
 import java.time.LocalDateTime;
 import java.time.ZoneId;
 import java.util.*;
+import java.util.concurrent.CompletableFuture;
 
 /**
  * 地磅记录业务编排类
@@ -57,8 +59,9 @@ public class WeighbridgeRecordManage {
             }
             // 保存到数据库
             boolean saved = weighbridgeRecordService.save(record);
-            
             if (saved) {
+                // 异步处理图片上传
+                updateImageUrls(request, record);
                 licensePlateValidateResponse.setStatus(true);
                 licensePlateValidateResponse.setCode(200);
                 licensePlateValidateResponse.setMessage("上报成功,请放行");
@@ -81,6 +84,24 @@ public class WeighbridgeRecordManage {
         }
     }
 
+    private void updateImageUrls(WeighbridgePushRequest request, WeighbridgeRecord record) {
+        if (request.getImages() != null && request.getImages().length > 0) {
+            log.info("开始处理 {} 张图片上传,车牌号: {}", request.getImages().length, request.getLicensePlate());
+            processImageFilesAsync(request.getImages())
+                    .thenAccept(photoUrls -> {
+                        log.info("图片上传完成,车牌号: {},照片数量: {}", request.getLicensePlate(),
+                                photoUrls != null ? photoUrls.split(",").length : 0);
+                        record.setPhotoUrls(photoUrls);
+                        // 更新数据库中的图片URL
+                        updatePhotoUrls(record.getId(), photoUrls);
+                    })
+                    .exceptionally(throwable -> {
+                        log.error("异步上传图片失败,车牌号: {}", request.getLicensePlate(), throwable);
+                        return null;
+                    });
+        }
+    }
+
     private static LicensePlateValidateResponse getLicensePlateValidateResponse(WeighbridgePushRequest request, LicensePlateValidateResponse licensePlateValidateResponse) {
 
         licensePlateValidateResponse.setStatus(false);
@@ -100,8 +121,8 @@ public class WeighbridgeRecordManage {
      */
     private WeighbridgeRecord buildWeighbridgeRecord(WeighbridgePushRequest request) {
         WeighbridgeRecord record = new WeighbridgeRecord();
-        
-        // 基础信息
+
+        // 基础信息设置
         record.setLicensePlate(request.getLicensePlate());
         record.setWeighbridgeCode(request.getWeighbridgeCode());
         String weighbridgeName = WeighbridgeEnum.getByCode(request.getWeighbridgeCode());
@@ -109,21 +130,48 @@ public class WeighbridgeRecordManage {
         record.setWeight(request.getGrossWeight());
         record.setTag(request.getTag());
 
-        // 时间戳转换:自动识别秒/毫秒
+        // 时间戳转换
         LocalDateTime weighTime = convertTimestamp(request.getTimestamp());
-        log.info("称重时间戳:{};转换 - 时间: {}",request.getTimestamp(), weighTime);
+        log.info("称重时间戳:{};转换 - 时间: {}", request.getTimestamp(), weighTime);
         record.setWeighTime(weighTime);
 
-        // 处理图片文件名(仅保存文件名,逗号分隔)
-        if (request.getImages() != null && request.getImages().length > 0) {
-            String photoUrls = processImageFiles(request.getImages());
-            record.setPhotoUrls(photoUrls);
-            log.info("处理图片文件 - 数量: {}, 文件名: {}", request.getImages().length, photoUrls);
-        }
+
 
         return record;
     }
 
+    /**
+     * 异步处理图片文件上传
+     */
+    @Async("taskExecutor")
+    public CompletableFuture<String> processImageFilesAsync(MultipartFile[] images) {
+        List<String> resultList = new ArrayList<>();
+        Arrays.stream(images).forEach(image -> {
+            String ossUrl = null;
+            try {
+                ossUrl = FileUtils.uploadFile(image);
+            } catch (Exception e) {
+                log.error("上传文件异常", e);
+                throw new IotException(ErrorCodeEnum.SYSTEM_ERROR, "上传文件异常");
+            }
+            if (StringUtils.isNotBlank(ossUrl)) {
+                resultList.add(ossUrl);
+            }
+        });
+        return CompletableFuture.completedFuture(String.join(",", resultList));
+    }
+    /**
+     * 异步更新图片URL
+     */
+    private void updatePhotoUrls(Long recordId, String photoUrls) {
+        // 更新记录的图片URL
+        WeighbridgeRecord updateRecord = new WeighbridgeRecord();
+        updateRecord.setId(recordId);
+        updateRecord.setPhotoUrls(photoUrls);
+        log.info("更新图片URL - ID: {}, 图片URL: {}", recordId, photoUrls);
+        weighbridgeRecordService.updateById(updateRecord);
+    }
+
     /**
      * 处理图片文件,提取文件名并拼接
      * @param images 图片文件数组

+ 23 - 0
iot-platform-manager/src/main/java/com/platform/config/AsyncConfig.java

@@ -0,0 +1,23 @@
+package com.platform.config;
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.core.task.TaskExecutor;
+import org.springframework.scheduling.annotation.EnableAsync;
+import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
+
+@Configuration
+@EnableAsync
+public class AsyncConfig {
+    
+    @Bean
+    public TaskExecutor taskExecutor() {
+        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
+        executor.setCorePoolSize(4);
+        executor.setMaxPoolSize(8);
+        executor.setQueueCapacity(100);
+        executor.setThreadNamePrefix("image-upload-");
+        executor.initialize();
+        return executor;
+    }
+}