|
|
@@ -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;
|
|
|
+ }
|
|
|
+}
|