donglang 1 тиждень тому
батько
коміт
52455b238b

+ 10 - 12
iot-platform-manager/src/main/java/com/platform/api/controller/WeighbridgeV2Controller.java

@@ -112,20 +112,17 @@ public class WeighbridgeV2Controller {
        // return weighbridgeRecordManage.handleValidateLicensePlate(request);
     }
 
-    @Operation(summary = "地磅过磅数据上报", description = "接收地磅设备上报的过磅数据和图片")
+    @Operation(summary = "地磅过磅数据上报V2", description = "接收地磅设备上报的过磅数据和图片文件")
     @PostMapping("/weighBridgePushV2")
     public LicensePlateValidateResponse weighBridgePushV2(
-            @Parameter(
-                    name = SIGNATURE,
-                    in = ParameterIn.HEADER)
-            @RequestHeader(value = SIGNATURE, required = false)  String printReceiptAuthSn,
+            @Parameter(name = SIGNATURE, in = ParameterIn.HEADER)
+            @RequestHeader(value = SIGNATURE, required = false) String printReceiptAuthSn,
             @Parameter(description = "车牌号") @RequestParam("licensePlate") String licensePlate,
             @Parameter(description = "地磅编号") @RequestParam("weighbridgeCode") String weighbridgeCode,
             @Parameter(description = "称重重量(吨)") @RequestParam("grossWeight") String grossWeight,
             @Parameter(description = "时间戳(秒或毫秒)") @RequestParam("timestamp") String timestamp,
             @Parameter(description = "处理标签") @RequestParam(value = "tag", required = false) String tag,
             @Parameter(description = "车辆照片") @RequestParam(value = "images[]", required = false) MultipartFile[] images
-
     ) {
         WeighbridgeRecordReq req = new WeighbridgeRecordReq();
         req.setLicensePlate(licensePlate);
@@ -135,20 +132,21 @@ public class WeighbridgeV2Controller {
         req.setTag(tag);
         req.setImages(images);
 
-        log.info("[地磅过磅数据上报]开始地磅过磅数据上报,入参参数:{}", JSON.toJSONString(req));
+        log.info("[地磅过磅数据上报V2]开始地磅过磅数据上报,入参参数:{}", JSON.toJSONString(req));
         assertPrintReceiptAuthorized(printReceiptAuthSn);
+
         // 构建请求对象
         WeighbridgePushRequest request = new WeighbridgePushRequest();
-        String lience = StringUtils.isNotBlank(req.getLicensePlate()) ? req.getLicensePlate().trim().replace("\\r", "").replace("\\n", ""):"";
+        String lience = StringUtils.isNotBlank(req.getLicensePlate()) ? req.getLicensePlate().trim().replace("\\r", "").replace("\\n", "") : "";
         request.setLicensePlate(lience);
-        String weighCode =StringUtils.isNotBlank(req.getWeighbridgeCode()) ?  req.getWeighbridgeCode().trim().replace("\\r", "").replace("\\n", ""):"";
+        String weighCode = StringUtils.isNotBlank(req.getWeighbridgeCode()) ? req.getWeighbridgeCode().trim().replace("\\r", "").replace("\\n", "") : "";
         request.setWeighbridgeCode(weighCode);
-        String rossWeight = StringUtils.isNotBlank(req.getGrossWeight()) ? req.getGrossWeight().trim().replace("\\r", "").replace("\\n", ""):"";
+        String rossWeight = StringUtils.isNotBlank(req.getGrossWeight()) ? req.getGrossWeight().trim().replace("\\r", "").replace("\\n", "") : "";
         request.setGrossWeight(new BigDecimal(rossWeight));
-        String time = StringUtils.isNotBlank(req.getTimestamp()) ? req.getTimestamp().trim().replace("\\r", "").replace("\\n", ""):"";
+        String time = StringUtils.isNotBlank(req.getTimestamp()) ? req.getTimestamp().trim().replace("\\r", "").replace("\\n", "") : "";
         request.setTimestamp(Long.valueOf(time));
         request.setTag(req.getTag());
-        request.setBase64Images(req.getImages());
+        request.setImages(req.getImages());  // 改为设置images
 
         // 调用业务层处理
         return weighbridgeRecordManage.handleWeighbridgePushV2(request);

+ 63 - 22
iot-platform-manager/src/main/java/com/platform/api/manager/WeighbridgeRecordManage.java

@@ -121,25 +121,32 @@ public class WeighbridgeRecordManage {
         }
     }
 
+    /**
+     * 处理地磅过磅数据上报V2(使用MultipartFile图片)
+     */
     public LicensePlateValidateResponse handleWeighbridgePushV2(WeighbridgePushRequest request) {
-        log.info("处理地磅数据上报 - 车牌:{}, 地磅编号:{}, 重量:{}, 时间戳:{},图片:{}",
+        log.info("处理地磅数据上报V2 - 车牌:{}, 地磅编号:{}, 重量:{}, 时间戳:{},图片数量:{}",
                 request.getLicensePlate(),
                 request.getWeighbridgeCode(),
                 request.getGrossWeight(),
                 request.getTimestamp(),
-                JSON.toJSONString(request.getImages()));
+                request.getImages() != null ? request.getImages().length : 0);
+
         LicensePlateValidateResponse licensePlateValidateResponse = new LicensePlateValidateResponse();
-        if (StringUtils.isAnyBlank(request.getLicensePlate(), request.getWeighbridgeCode()) || Objects.isNull(request.getGrossWeight()) || Objects.isNull(request.getTimestamp())) {
+        if (StringUtils.isAnyBlank(request.getLicensePlate(), request.getWeighbridgeCode())
+                || Objects.isNull(request.getGrossWeight()) || Objects.isNull(request.getTimestamp())) {
             return getLicensePlateValidateResponse(request, licensePlateValidateResponse);
         }
-        //查询车牌是否在验证列表中
+
+        // 查询车牌是否在验证列表中
         ValidateLicensePlate validateLicensePlate = validateLicensePlateService.queryByLicensePlate(request.getLicensePlate());
         try {
             // 构建实体对象
             WeighbridgeRecord record = buildYsWeighbridgeRecord(request);
-            if(Objects.isNull(validateLicensePlate)){
+            if (Objects.isNull(validateLicensePlate)) {
                 record.setCheckStatus(1);
             }
+
             // 保存到数据库
             boolean saved = weighbridgeRecordService.save(record);
             LambdaQueryWrapper<KwsWeighbridge> wrapper = Wrappers.<KwsWeighbridge>lambdaQuery()
@@ -150,19 +157,19 @@ public class WeighbridgeRecordManage {
             Long entId = Optional.ofNullable(kwsWeighbridge)
                     .map(KwsWeighbridge::getEntId)
                     .orElse(null);
+
             if (saved) {
-                // 异步处理图片上传
-                updateBase64ImageUrls(request, record);
+                // 异步处理图片上传(改为处理MultipartFile)
+                updateImageUrlsV2(request, record);
+
                 licensePlateValidateResponse.setStatus(true);
                 licensePlateValidateResponse.setCode(200);
                 licensePlateValidateResponse.setMessage("数据上报成功");
                 LicensePlateValidateResponse.Data data = new LicensePlateValidateResponse.Data();
                 String timestampStr = request.getTimestamp().toString();
                 if (timestampStr.length() == 10) {
-                    // 秒级时间戳
                     data.setTimestamp(request.getTimestamp());
                 } else if (timestampStr.length() == 13) {
-                    // 毫秒级时间戳
                     long l = request.getTimestamp() / 1000;
                     data.setTimestamp(l);
                 }
@@ -170,6 +177,7 @@ public class WeighbridgeRecordManage {
                 data.setVoice_message("数据上报成功");
                 licensePlateValidateResponse.setData(data);
                 log.info("地磅数据保存成功 - ID: {}, 车牌: {}", record.getId(), record.getLicensePlate());
+
                 record.setEntId(entId);
                 externalWeighbridgePushService.pushWeighbridgeRecord(record);
                 WeighbridgeRecord weighbridgeRecord = new WeighbridgeRecord();
@@ -181,18 +189,22 @@ public class WeighbridgeRecordManage {
                 log.error("地磅数据保存失败 - 车牌: {}", request.getLicensePlate());
                 return getLicensePlateValidateResponse(request, licensePlateValidateResponse);
             }
-
         } catch (Exception e) {
             log.error("地磅数据上报处理异常", e);
             return getLicensePlateValidateResponse(request, licensePlateValidateResponse);
-            //throw new IotException(ErrorCodeEnum.SYSTEM_ERROR, "地磅数据上报异常: " + e.getMessage());
         }
     }
 
+    /**
+     * 异步处理MultipartFile图片上传(V2版本)
+     */
+    private void updateImageUrlsV2(WeighbridgePushRequest request, WeighbridgeRecord record) {
+        log.info("[地磅图片保存V2]开始保存地磅图片,图片数量: {}",
+                request.getImages() != null ? request.getImages().length : 0);
 
-    private void updateImageUrls(WeighbridgePushRequest request, WeighbridgeRecord record) {
         if (request.getImages() != null && request.getImages().length > 0) {
             log.info("开始处理 {} 张图片上传,车牌号: {}", request.getImages().length, request.getLicensePlate());
+
             // 将MultipartFile数组转换为字节数组,防止异步处理时临时文件被清理
             List<byte[]> imageBytesList = new ArrayList<>();
             for (MultipartFile image : request.getImages()) {
@@ -202,13 +214,14 @@ public class WeighbridgeRecordManage {
                     log.error("读取图片文件失败,车牌号: {}", request.getLicensePlate(), e);
                 }
             }
+
+            // 调用现有的异步上传方法
             uploadService.processImageFilesAsync(imageBytesList, request.getLicensePlate())
                     .thenAccept(photoUrls -> {
                         log.info("图片上传完成,车牌号: {},照片数量: {}", request.getLicensePlate(),
                                 photoUrls != null ? photoUrls.split(",").length : 0);
                         record.setPhotoUrls(photoUrls);
-                        // 更新数据库中的图片URL
-                        updatePhotoUrls(record.getId(), photoUrls,record.getLicensePlate());
+                        updatePhotoUrls(record.getId(), photoUrls, record.getLicensePlate());
                     })
                     .exceptionally(throwable -> {
                         log.error("异步上传图片失败,车牌号: {}", request.getLicensePlate(), throwable);
@@ -217,24 +230,52 @@ public class WeighbridgeRecordManage {
         }
     }
 
-    private void updateBase64ImageUrls(WeighbridgePushRequest request, WeighbridgeRecord record) {
-        log.info("[地磅图片保存]开始保存地磅图片,request:{}, record:{}", JSON.toJSONString(request), JSON.toJSONString(record));
-        if (request.getBase64Images() != null && !request.getBase64Images().isEmpty()) {
-            log.info("开始处理 {} 张Base64图片保存,车牌号: {}", request.getBase64Images().size(), request.getLicensePlate());
-            uploadService.processBase64ImagesToLocalAsync(request.getBase64Images(), request.getLicensePlate())
+
+    private void updateImageUrls(WeighbridgePushRequest request, WeighbridgeRecord record) {
+        if (request.getImages() != null && request.getImages().length > 0) {
+            log.info("开始处理 {} 张图片上传,车牌号: {}", request.getImages().length, request.getLicensePlate());
+            // 将MultipartFile数组转换为字节数组,防止异步处理时临时文件被清理
+            List<byte[]> imageBytesList = new ArrayList<>();
+            for (MultipartFile image : request.getImages()) {
+                try {
+                    imageBytesList.add(image.getBytes());
+                } catch (IOException e) {
+                    log.error("读取图片文件失败,车牌号: {}", request.getLicensePlate(), e);
+                }
+            }
+            uploadService.processImageFilesAsync(imageBytesList, request.getLicensePlate())
                     .thenAccept(photoUrls -> {
-                        log.info("Base64图片保存完成,车牌号: {},照片数量: {}", request.getLicensePlate(),
+                        log.info("图片上传完成,车牌号: {},照片数量: {}", request.getLicensePlate(),
                                 photoUrls != null ? photoUrls.split(",").length : 0);
                         record.setPhotoUrls(photoUrls);
-                        updatePhotoUrls(record.getId(), photoUrls, request.getLicensePlate());
+                        // 更新数据库中的图片URL
+                        updatePhotoUrls(record.getId(), photoUrls,record.getLicensePlate());
                     })
                     .exceptionally(throwable -> {
-                        log.error("异步保存Base64图片失败,车牌号: {}", request.getLicensePlate(), throwable);
+                        log.error("异步上传图片失败,车牌号: {}", request.getLicensePlate(), throwable);
                         return null;
                     });
         }
     }
 
+//    private void updateBase64ImageUrls(WeighbridgePushRequest request, WeighbridgeRecord record) {
+//        log.info("[地磅图片保存]开始保存地磅图片,request:{}, record:{}", JSON.toJSONString(request), JSON.toJSONString(record));
+//        if (request.getBase64Images() != null && !request.getBase64Images().isEmpty()) {
+//            log.info("开始处理 {} 张Base64图片保存,车牌号: {}", request.getBase64Images().size(), request.getLicensePlate());
+//            uploadService.processBase64ImagesToLocalAsync(request.getBase64Images(), request.getLicensePlate())
+//                    .thenAccept(photoUrls -> {
+//                        log.info("Base64图片保存完成,车牌号: {},照片数量: {}", request.getLicensePlate(),
+//                                photoUrls != null ? photoUrls.split(",").length : 0);
+//                        record.setPhotoUrls(photoUrls);
+//                        updatePhotoUrls(record.getId(), photoUrls, request.getLicensePlate());
+//                    })
+//                    .exceptionally(throwable -> {
+//                        log.error("异步保存Base64图片失败,车牌号: {}", request.getLicensePlate(), throwable);
+//                        return null;
+//                    });
+//        }
+//    }
+
     private static LicensePlateValidateResponse getLicensePlateValidateResponse(WeighbridgePushRequest request, LicensePlateValidateResponse licensePlateValidateResponse) {
 
         licensePlateValidateResponse.setStatus(false);

+ 3 - 2
iot-platform-manager/src/main/java/com/platform/api/request/WeighbridgePushRequest.java

@@ -52,10 +52,11 @@ public class WeighbridgePushRequest {
     private String tag;
 
     /**
-     * 车辆图片
+     * 车辆图片数组
      */
     @Schema(description = "车辆图片数组")
     private MultipartFile[] images;
 
-    private List<String> base64Images;
+//    private List<String> base64Images;
+
 }