Jelajahi Sumber

推送地磅数据

chenxiaofei 1 bulan lalu
induk
melakukan
5a2fceb578

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

@@ -2,6 +2,7 @@ package com.platform;
 
 import org.mybatis.spring.annotation.MapperScan;
 import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
+import org.springframework.cloud.openfeign.EnableFeignClients;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.scheduling.annotation.EnableAsync;
@@ -19,6 +20,7 @@ import org.springframework.scheduling.annotation.EnableScheduling;
 @EnableScheduling
 @EnableAsync
 @EnableDiscoveryClient
+@EnableFeignClients
 public class IotPlatformManagerApplication {
     public static void main(String[] args) {
         SpringApplication.run(IotPlatformManagerApplication.class, args);

+ 4 - 0
iot-platform-manager/src/main/java/com/platform/api/manager/WeighbridgeRecordManage.java

@@ -15,6 +15,7 @@ import com.platform.enums.ErrorCodeEnum;
 import com.platform.enums.WeighbridgeEnum;
 import com.platform.exception.IotException;
 import com.platform.api.response.LicensePlateValidateResponse;
+import com.platform.external.service.WeighbridgePushService;
 import com.platform.result.PageDataResult;
 import com.platform.service.ValidateLicensePlateService;
 import com.platform.service.KwsWeighbridgeRepository;
@@ -47,6 +48,8 @@ public class WeighbridgeRecordManage {
     private final KwsWeighbridgeRepository kwsWeighbridgeRepository;
 
     private final ValidateLicensePlateService validateLicensePlateService;
+
+    private final WeighbridgePushService externalWeighbridgePushService;
     
 
     @Resource
@@ -150,6 +153,7 @@ public class WeighbridgeRecordManage {
                 data.setVoice_message("数据上报成功");
                 licensePlateValidateResponse.setData(data);
                 log.info("地磅数据保存成功 - ID: {}, 车牌: {}", record.getId(), record.getLicensePlate());
+                externalWeighbridgePushService.pushWeighbridgeRecord(record);
                 return licensePlateValidateResponse;
             } else {
                 log.error("地磅数据保存失败 - 车牌: {}", request.getLicensePlate());

+ 28 - 0
iot-platform-manager/src/main/java/com/platform/external/client/ExternalWeighbridgeClient.java

@@ -0,0 +1,28 @@
+package com.platform.external.client;
+
+import com.platform.external.request.WaybillOrderProcessParam;
+import com.platform.result.BaseResult;
+import org.springframework.cloud.openfeign.FeignClient;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+
+/**
+ * 外部系统地磅数据 Feign 客户端。
+ *
+ * @author PC
+ */
+@FeignClient(
+        name = "${external-system.weighbridge.service-id:sckw-ng-transport}",
+        path = "${external-system.weighbridge.base-path:/appKwtWayBill}"
+)
+public interface ExternalWeighbridgeClient {
+
+    /**
+     * 推送地磅记录到外部系统。
+     *
+     * @param request 地磅记录推送请求
+     * @return 外部系统响应
+     */
+    @PostMapping("${external-system.weighbridge.push-path:/loading}")
+    BaseResult pushWeighbridgeRecord(@RequestBody WaybillOrderProcessParam request);
+}

+ 21 - 0
iot-platform-manager/src/main/java/com/platform/external/config/ExternalWeighbridgeProperties.java

@@ -0,0 +1,21 @@
+package com.platform.external.config;
+
+import lombok.Data;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.stereotype.Component;
+
+/**
+ * 外部系统地磅数据推送配置。
+ *
+ * @author PC
+ */
+@Data
+@Component
+@ConfigurationProperties(prefix = "external-system.weighbridge")
+public class ExternalWeighbridgeProperties {
+
+    /**
+     * 是否启用外部系统推送,默认关闭,避免未配置外部系统时影响现有业务流程。
+     */
+    private Boolean enabled = Boolean.FALSE;
+}

+ 50 - 0
iot-platform-manager/src/main/java/com/platform/external/request/WaybillOrderProcessParam.java

@@ -0,0 +1,50 @@
+package com.platform.external.request;
+
+import io.swagger.v3.oas.annotations.media.Schema;
+import lombok.Data;
+
+import java.io.Serial;
+import java.io.Serializable;
+
+/**
+ * @author :donglang
+ * @version :1.0
+ * @description 参数基类
+ * @create :2025-11-13 08:59:00
+ */
+@Data
+public class WaybillOrderProcessParam implements Serializable {
+
+    @Serial
+    private static final long serialVersionUID = -114986619800571178L;
+
+    /**
+     * 运单id
+     */
+    @Schema(description = "运单id")
+    private Long waybillOrderId;
+
+    /**
+     * 过磅id
+     */
+    @Schema(description = "过磅id")
+    private Long weighbridgeId;
+
+    /**
+     * 过磅名称
+     */
+    @Schema(description = "过磅名称")
+    private String weighbridgeName;
+
+    /**
+     * 地磅图片
+     */
+    @Schema(description = "地磅图片")
+    private String weighUrl;
+    /**
+     * 车辆号
+     */
+    @Schema(description = "车辆号")
+    private String truckNo;
+
+}

+ 84 - 0
iot-platform-manager/src/main/java/com/platform/external/service/WeighbridgePushService.java

@@ -0,0 +1,84 @@
+package com.platform.external.service;
+
+import com.alibaba.fastjson2.JSON;
+import com.platform.entity.WeighbridgeRecord;
+import com.platform.exception.IotException;
+import com.platform.external.client.ExternalWeighbridgeClient;
+import com.platform.external.config.ExternalWeighbridgeProperties;
+import com.platform.external.request.WaybillOrderProcessParam;
+import com.platform.result.BaseResult;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.stereotype.Service;
+
+import java.util.Objects;
+
+/**
+ * 外部系统地磅数据推送服务。
+ *
+ * @author PC
+ */
+@Slf4j
+@Service
+@RequiredArgsConstructor
+public class WeighbridgePushService {
+
+    private final ExternalWeighbridgeClient externalWeighbridgeClient;
+
+    private final ExternalWeighbridgeProperties externalWeighbridgeProperties;
+
+    /**
+     * 推送地磅记录到外部系统。
+     *
+     * <p>该方法作为现有地磅上报流程末尾的外部系统扩展点,异常仅记录日志,不向上抛出,
+     * 避免外部系统不可用影响本系统原有保存与响应逻辑。</p>
+     *
+     * @param record 已保存的地磅记录
+     */
+    public void pushWeighbridgeRecord(WeighbridgeRecord record)  {
+        if (!Boolean.TRUE.equals(externalWeighbridgeProperties.getEnabled())) {
+            log.debug("外部系统地磅数据推送未启用,跳过推送");
+            return;
+        }
+        if (Objects.isNull(record) || Objects.isNull(record.getId())) {
+            log.warn("外部系统地磅数据推送参数为空,跳过推送");
+            return;
+        }
+        BaseResult response  = null;
+        try {
+            WaybillOrderProcessParam request = buildPushRequest(record);
+            log.info("外部系统地磅数据推送开始,记录ID: {}, 请求参数: {}",
+                    record.getId(), JSON.toJSONString( request));
+            response = externalWeighbridgeClient.pushWeighbridgeRecord(request);
+            log.info("外部系统地磅数据推送完成,记录ID: {}, 响应码: {}, 响应消息: {}",
+                    record.getId(),
+                    Objects.nonNull(response) ? response.getCode() : null,
+                    Objects.nonNull(response) ? response.getMessage() : null);
+        } catch (Exception e) {
+            log.error("外部系统地磅数据推送异常,记录ID: {}, 车牌号: {}",
+                    record.getId(), record.getLicensePlate(), e);
+            throw new IotException("外部系统地磅数据推送异常");
+        }
+        if (Objects.nonNull(response) && !response.isSuccess()){
+            log.error("外部系统地磅数据推送失败,记录ID: {}, 车牌号: {}",
+                    record.getId(), record.getLicensePlate());
+            throw new IotException("外部系统地磅数据推送失败");
+        }
+    }
+
+    /**
+     * 构建外部系统推送请求。
+     *
+     * @param record 地磅记录
+     * @return 外部系统推送请求
+     */
+    private WaybillOrderProcessParam buildPushRequest(WeighbridgeRecord record) {
+        WaybillOrderProcessParam request = new WaybillOrderProcessParam();
+        request.setTruckNo(record.getLicensePlate());
+        request.setWeighbridgeId(StringUtils.isNotBlank(record.getWeighbridgeCode()) ? Long.valueOf(record.getWeighbridgeCode()) : null);
+        request.setWeighbridgeName(record.getWeighbridgeName());
+        request.setWeighUrl(record.getPhotoUrls());
+        return request;
+    }
+}