|
|
@@ -3,6 +3,7 @@ package com.yc.ship.module.trade.utils;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.yc.ship.framework.common.pojo.CommonResult;
|
|
|
import com.yc.ship.module.trade.api.insurance.dto.InsuranceApplyReqDTO;
|
|
|
+import com.yc.ship.module.trade.api.insurance.dto.InsuranceCancelReqDTO;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.http.*;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
@@ -29,6 +30,8 @@ public class InsuranceUtil {
|
|
|
|
|
|
public final String RECEIVE_URL = "/policy/receiveJson.do"; // JSON投/退保接口
|
|
|
|
|
|
+ public final String QUERY_URL = "/policy/querystatus.do"; // 保单状态查询接口
|
|
|
+
|
|
|
public final String APPID = "123456"; // appId
|
|
|
|
|
|
public final String KEY = "goldpalm"; // key
|
|
|
@@ -187,4 +190,132 @@ public class InsuranceUtil {
|
|
|
throw exception0(500,"发送校验请求失败: " + e.getMessage(), e);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送退保请求
|
|
|
+ * 将退保请求发送到阳光系统
|
|
|
+ * @param request 退保请求
|
|
|
+ * @return 退保响应
|
|
|
+ */
|
|
|
+ public CommonResult sendInsuranceCancel(InsuranceCancelReqDTO request) {
|
|
|
+ try {
|
|
|
+ String reqJson = JSONObject.toJSONString(request);
|
|
|
+ String sign = SignUtil.generateMD5Sign(APPID, reqJson, KEY);
|
|
|
+
|
|
|
+ MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
|
|
|
+ params.add("appId", APPID);
|
|
|
+ params.add("req", reqJson);
|
|
|
+ params.add("sign", sign);
|
|
|
+
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
|
|
|
+
|
|
|
+ HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<>(params, headers);
|
|
|
+
|
|
|
+ log.info("发送退保请求到阳光系统: {}", HOST + RECEIVE_URL);
|
|
|
+ log.info("请求参数: appId={}, req={}, sign={}", APPID, reqJson, sign);
|
|
|
+
|
|
|
+ // 发送请求并获取响应
|
|
|
+ ResponseEntity<byte[]> response = restTemplate.exchange(
|
|
|
+ HOST + RECEIVE_URL,
|
|
|
+ HttpMethod.POST,
|
|
|
+ entity,
|
|
|
+ byte[].class
|
|
|
+ );
|
|
|
+
|
|
|
+ log.info("阳光系统响应状态码: {}", response.getStatusCode());
|
|
|
+
|
|
|
+ // 手动处理响应字节流,确保使用UTF-8编码
|
|
|
+ byte[] responseBytes = response.getBody();
|
|
|
+ String responseBody = null;
|
|
|
+ if (responseBytes != null) {
|
|
|
+ try {
|
|
|
+ responseBody = new String(responseBytes, "UTF-8");
|
|
|
+ } catch (UnsupportedEncodingException e) {
|
|
|
+ log.error("解析响应内容编码失败", e);
|
|
|
+ responseBody = new String(responseBytes);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ log.info("阳光系统响应内容: {}", responseBody);
|
|
|
+ JSONObject responseBodyJson = JSONObject.parseObject(responseBody);
|
|
|
+
|
|
|
+ if (response.getStatusCode() == HttpStatus.OK) {
|
|
|
+ String status = responseBodyJson.getString("status");
|
|
|
+ if("FAIL".equals(status)) {
|
|
|
+ return CommonResult.error(500, "阳光系统退保返回错误: " + response.getStatusCode() + responseBody);
|
|
|
+ }else {
|
|
|
+ return CommonResult.success(responseBody);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ return CommonResult.error(500, "阳光系统退保返回错误: " + response.getStatusCode() + responseBody);
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("发送退保请求失败", e);
|
|
|
+ return CommonResult.error(500,"发送退保请求失败: " + e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public CommonResult queryInsurance(String orderNo) {
|
|
|
+ try {
|
|
|
+ JSONObject request = new JSONObject();
|
|
|
+ request.put("externalOrderNo", orderNo);
|
|
|
+ String reqJson = JSONObject.toJSONString(request);
|
|
|
+ String sign = SignUtil.generateMD5Sign(APPID, reqJson, KEY);
|
|
|
+
|
|
|
+ MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
|
|
|
+ params.add("appId", APPID);
|
|
|
+ params.add("req", reqJson);
|
|
|
+ params.add("sign", sign);
|
|
|
+
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
|
|
|
+
|
|
|
+ HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<>(params, headers);
|
|
|
+
|
|
|
+ log.info("发送退保请求到阳光系统: {}", HOST + RECEIVE_URL);
|
|
|
+ log.info("请求参数: appId={}, req={}, sign={}", APPID, reqJson, sign);
|
|
|
+
|
|
|
+ // 发送请求并获取响应
|
|
|
+ ResponseEntity<byte[]> response = restTemplate.exchange(
|
|
|
+ HOST + RECEIVE_URL,
|
|
|
+ HttpMethod.POST,
|
|
|
+ entity,
|
|
|
+ byte[].class
|
|
|
+ );
|
|
|
+
|
|
|
+ log.info("阳光系统响应状态码: {}", response.getStatusCode());
|
|
|
+
|
|
|
+ // 手动处理响应字节流,确保使用UTF-8编码
|
|
|
+ byte[] responseBytes = response.getBody();
|
|
|
+ String responseBody = null;
|
|
|
+ if (responseBytes != null) {
|
|
|
+ try {
|
|
|
+ responseBody = new String(responseBytes, "UTF-8");
|
|
|
+ } catch (UnsupportedEncodingException e) {
|
|
|
+ log.error("解析响应内容编码失败", e);
|
|
|
+ responseBody = new String(responseBytes);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ log.info("阳光系统响应内容: {}", responseBody);
|
|
|
+ JSONObject responseBodyJson = JSONObject.parseObject(responseBody);
|
|
|
+
|
|
|
+ if (response.getStatusCode() == HttpStatus.OK) {
|
|
|
+ String status = responseBodyJson.getString("status");
|
|
|
+ if("FAIL".equals(status)) {
|
|
|
+ return CommonResult.error(500, "阳光系统退保返回错误: " + response.getStatusCode() + responseBody);
|
|
|
+ }else {
|
|
|
+ return CommonResult.success(responseBody);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ return CommonResult.error(500, "阳光系统退保返回错误: " + response.getStatusCode() + responseBody);
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("发送退保请求失败", e);
|
|
|
+ return CommonResult.error(500,"发送退保请求失败: " + e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|