|
@@ -1,5 +1,7 @@
|
|
|
package com.fs.erp.service.impl;
|
|
package com.fs.erp.service.impl;
|
|
|
|
|
|
|
|
|
|
+import cn.hutool.core.map.MapUtil;
|
|
|
|
|
+import cn.hutool.http.HttpResponse;
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
import com.fasterxml.jackson.databind.SerializationFeature;
|
|
import com.fasterxml.jackson.databind.SerializationFeature;
|
|
@@ -8,6 +10,7 @@ import com.fs.erp.dto.tl.JstLogisticsPushRequest;
|
|
|
import com.fs.erp.dto.tl.TlCreateOrderRequest;
|
|
import com.fs.erp.dto.tl.TlCreateOrderRequest;
|
|
|
import com.fs.erp.dto.tl.TlCreateOrderResponse;
|
|
import com.fs.erp.dto.tl.TlCreateOrderResponse;
|
|
|
import com.fs.erp.service.TlErpOrderService;
|
|
import com.fs.erp.service.TlErpOrderService;
|
|
|
|
|
+import com.fs.erp.utils.SignUtil;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.codec.digest.DigestUtils;
|
|
import org.apache.commons.codec.digest.DigestUtils;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
@@ -16,10 +19,7 @@ import org.springframework.stereotype.Service;
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
|
|
|
|
import java.time.Instant;
|
|
import java.time.Instant;
|
|
|
-import java.util.ArrayList;
|
|
|
|
|
-import java.util.List;
|
|
|
|
|
-import java.util.Map;
|
|
|
|
|
-import java.util.TreeMap;
|
|
|
|
|
|
|
+import java.util.*;
|
|
|
|
|
|
|
|
@Slf4j
|
|
@Slf4j
|
|
|
@Service
|
|
@Service
|
|
@@ -38,77 +38,87 @@ public class TlErpOrderServiceImpl implements TlErpOrderService {
|
|
|
.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);
|
|
.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
- public CommonResponse<TlCreateOrderResponse> syncOrderToJst(TlCreateOrderRequest request) {
|
|
|
|
|
|
|
+ public HttpResponse syncOrderToJst(TlCreateOrderRequest request) {
|
|
|
try {
|
|
try {
|
|
|
- String corpId = request.getCorpId();
|
|
|
|
|
- List<?> rawData = request.getData();
|
|
|
|
|
-
|
|
|
|
|
- if (rawData == null || rawData.isEmpty()) {
|
|
|
|
|
- return new CommonResponse<TlCreateOrderResponse>()
|
|
|
|
|
- .setCode(-1)
|
|
|
|
|
- .setMsg("订单数据不能为空")
|
|
|
|
|
- .setData(null);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- List<Map<String, Object>> dataAsMaps = new ArrayList<>();
|
|
|
|
|
- for (Object item : rawData) {
|
|
|
|
|
- Map<String, Object> map = objectMapper.convertValue(item, Map.class);
|
|
|
|
|
- dataAsMaps.add(map);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- long timestamp = Instant.now().getEpochSecond();
|
|
|
|
|
- String sortedDataJson = sortAndSerializeData(dataAsMaps);
|
|
|
|
|
-
|
|
|
|
|
- String signSource = secretKey + corpId + sortedDataJson + timestamp;
|
|
|
|
|
- String sign = DigestUtils.md5Hex(signSource).toLowerCase();
|
|
|
|
|
-
|
|
|
|
|
- String url = jstApiBaseUrl + "/v1/mp/sync/order/jst/create?sign=" + sign + "&t=" + timestamp;
|
|
|
|
|
- String requestBody = objectMapper.writeValueAsString(request);
|
|
|
|
|
-
|
|
|
|
|
- log.info("请求URL: {}", url);
|
|
|
|
|
- log.info("请求Body: {}", requestBody);
|
|
|
|
|
- log.info("sign = MD5({})", signSource);
|
|
|
|
|
- log.info("最终 sign: {}, t: {}", sign, timestamp);
|
|
|
|
|
-
|
|
|
|
|
- HttpHeaders headers = new HttpHeaders();
|
|
|
|
|
- headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
|
|
- HttpEntity<String> entity = new HttpEntity<>(requestBody, headers);
|
|
|
|
|
-
|
|
|
|
|
- ResponseEntity<TlCreateOrderResponse> response = restTemplate.exchange(
|
|
|
|
|
- url,
|
|
|
|
|
- HttpMethod.POST,
|
|
|
|
|
- entity,
|
|
|
|
|
- TlCreateOrderResponse.class
|
|
|
|
|
- );
|
|
|
|
|
-
|
|
|
|
|
- log.info("响应状态: {}, body: {}", response.getStatusCode(), response.getBody());
|
|
|
|
|
-
|
|
|
|
|
- if (response.getStatusCode() == HttpStatus.OK && response.getBody() != null) {
|
|
|
|
|
- return new CommonResponse<TlCreateOrderResponse>()
|
|
|
|
|
- .setCode(200)
|
|
|
|
|
- .setMsg("success")
|
|
|
|
|
- .setData(response.getBody());
|
|
|
|
|
- } else {
|
|
|
|
|
- String errorMsg = "HTTP状态码非200或响应体为空";
|
|
|
|
|
- log.error("订单同步失败: {}", errorMsg);
|
|
|
|
|
- return new CommonResponse<TlCreateOrderResponse>()
|
|
|
|
|
- .setCode(-1)
|
|
|
|
|
- .setMsg(errorMsg)
|
|
|
|
|
- .setData(null);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- } catch (JsonProcessingException e) {
|
|
|
|
|
- log.error("JSON 序列化失败", e);
|
|
|
|
|
- return new CommonResponse<TlCreateOrderResponse>()
|
|
|
|
|
- .setCode(-2)
|
|
|
|
|
- .setMsg("订单数据格式异常: " + e.getMessage())
|
|
|
|
|
- .setData(null);
|
|
|
|
|
- } catch (Exception e) {
|
|
|
|
|
- log.error("调用同步兔灵(聚水潭)订单接口失败", e);
|
|
|
|
|
- return new CommonResponse<TlCreateOrderResponse>()
|
|
|
|
|
- .setCode(-3)
|
|
|
|
|
- .setMsg("同步订单到兔灵(聚水潭)失败: " + e.getMessage())
|
|
|
|
|
- .setData(null);
|
|
|
|
|
|
|
+ String corpId = request.getCorpId();
|
|
|
|
|
+ List<?> rawData = request.getData();
|
|
|
|
|
+
|
|
|
|
|
+// if (rawData == null || rawData.isEmpty()) {
|
|
|
|
|
+// return new CommonResponse<TlCreateOrderResponse>()
|
|
|
|
|
+// .setCode(-1)
|
|
|
|
|
+// .setMsg("订单数据不能为空")
|
|
|
|
|
+// .setData(null);
|
|
|
|
|
+// }
|
|
|
|
|
+
|
|
|
|
|
+ List<Map<String, Object>> dataAsMaps = new ArrayList<>();
|
|
|
|
|
+ for (Object item : rawData) {
|
|
|
|
|
+ Map<String, Object> map = objectMapper.convertValue(item, Map.class);
|
|
|
|
|
+ dataAsMaps.add(map);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ long timestamp = Instant.now().getEpochSecond();
|
|
|
|
|
+ String sortedDataJson = sortAndSerializeData(dataAsMaps);
|
|
|
|
|
+
|
|
|
|
|
+ String signSource = secretKey + corpId + sortedDataJson + timestamp;
|
|
|
|
|
+ String sign = DigestUtils.md5Hex(signSource).toLowerCase();
|
|
|
|
|
+
|
|
|
|
|
+ String url = jstApiBaseUrl + "/v1/mp/sync/order/jst/create?sign=" + sign + "&t=" + timestamp;
|
|
|
|
|
+ String requestBody = objectMapper.writeValueAsString(request);
|
|
|
|
|
+
|
|
|
|
|
+ log.info("请求URL: {}", url);
|
|
|
|
|
+ log.info("请求Body: {}", requestBody);
|
|
|
|
|
+ log.info("sign = MD5({})", signSource);
|
|
|
|
|
+ log.info("最终 sign: {}, t: {}", sign, timestamp);
|
|
|
|
|
+
|
|
|
|
|
+// HttpHeaders headers = new HttpHeaders();
|
|
|
|
|
+// headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
|
|
+// HttpEntity<String> entity = new HttpEntity<>(requestBody, headers);
|
|
|
|
|
+
|
|
|
|
|
+ // ResponseEntity<TlCreateOrderResponse> response = restTemplate.exchange(
|
|
|
|
|
+ // url,
|
|
|
|
|
+ // HttpMethod.POST,
|
|
|
|
|
+ // entity,
|
|
|
|
|
+ // TlCreateOrderResponse.class
|
|
|
|
|
+ // );
|
|
|
|
|
+ Map<String, String> headers = MapUtil.builder(new HashMap<String, String>())
|
|
|
|
|
+ .put("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8")
|
|
|
|
|
+ .build();
|
|
|
|
|
+ HttpResponse response = cn.hutool.http.HttpRequest.post(url)
|
|
|
|
|
+ .headerMap(headers, true)
|
|
|
|
|
+ .form(requestBody)
|
|
|
|
|
+ .execute();
|
|
|
|
|
+ return response;
|
|
|
|
|
+
|
|
|
|
|
+// log.info("响应状态: {}, body: {}", response.getStatusCode(), response.getBody());
|
|
|
|
|
+//
|
|
|
|
|
+// if (response.getStatusCode() == HttpStatus.OK && response.getBody() != null) {
|
|
|
|
|
+// return new CommonResponse<TlCreateOrderResponse>()
|
|
|
|
|
+// .setCode(200)
|
|
|
|
|
+// .setMsg("success")
|
|
|
|
|
+// .setData(response.getBody());
|
|
|
|
|
+// } else {
|
|
|
|
|
+// String errorMsg = "HTTP状态码非200或响应体为空";
|
|
|
|
|
+// log.error("订单同步失败: {}", errorMsg);
|
|
|
|
|
+// return new CommonResponse<TlCreateOrderResponse>()
|
|
|
|
|
+// .setCode(-1)
|
|
|
|
|
+// .setMsg(errorMsg)
|
|
|
|
|
+// .setData(null);
|
|
|
|
|
+// }
|
|
|
|
|
+//
|
|
|
|
|
+// } catch (JsonProcessingException e) {
|
|
|
|
|
+// log.error("JSON 序列化失败", e);
|
|
|
|
|
+// return new CommonResponse<TlCreateOrderResponse>()
|
|
|
|
|
+// .setCode(-2)
|
|
|
|
|
+// .setMsg("订单数据格式异常: " + e.getMessage())
|
|
|
|
|
+// .setData(null);
|
|
|
|
|
+// } catch (Exception e) {
|
|
|
|
|
+// log.error("调用同步兔灵(聚水潭)订单接口失败", e);
|
|
|
|
|
+// return new CommonResponse<TlCreateOrderResponse>()
|
|
|
|
|
+// .setCode(-3)
|
|
|
|
|
+// .setMsg("同步订单到兔灵(聚水潭)失败: " + e.getMessage())
|
|
|
|
|
+// .setData(null);
|
|
|
|
|
+ } catch (JsonProcessingException e) {
|
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|