联查合同接口开发;

This commit is contained in:
王奎兴
2026-09-04 19:16:57 +08:00
parent 5a329904c3
commit befdb82b53
5 changed files with 688 additions and 30 deletions
@@ -4,6 +4,7 @@ import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
@@ -11,6 +12,7 @@ import com.mhd.basic.domain.contractManage.entity.ContractManage;
import com.mhd.basic.domain.contractManage.repository.mapper.ContractManageMapper;
import com.mhd.basic.domain.contractManage.repository.po.ContractManagePO;
import com.mhd.basic.domain.contractManage.repository.todo.ContractManageDO;
import com.mhd.basic.domain.contractManage.repository.util.HttpWebServiceUtil;
import com.mhd.basic.domain.contractManage.service.ContractManageDomainService;
import com.mhd.basic.domain.contractManageDetail.entity.ContractManageDetail;
import com.mhd.basic.domain.contractManageDetail.repository.mapper.ContractManageDetailMapper;
@@ -1348,12 +1350,14 @@ public class ContractManageApplicationService {
String warehouseTempLayerType;
String serviceItemsName;
Boolean isGc = false;
if ("冻仓计费配置".equals(parametersName)) {
warehouseTempLayerType = "DC";
serviceItemsName = "冻仓仓租费用";
} else if ("干仓计费配置".equals(parametersName)) {
warehouseTempLayerType = "GC";
serviceItemsName = "干仓仓租费用";
isGc=true;
} else {
log.warn("未知的结算对象参数类型,parametersName={}", parametersName);
continue;
@@ -1379,7 +1383,7 @@ public class ContractManageApplicationService {
String parametersJson = contractManageParameters.getParametersJson();
Map<String, Object> parametersMap = JSON.parseObject(parametersJson, new TypeReference<Map<String, Object>>() {});
BigDecimal hyj = (BigDecimal) parametersMap.get("hyj");
buildBusinessDocument(hyj, settlementCustomers, contractManage, "0D081", "干仓仓租费用", warehouseTempLayerType, serviceItemsName);
buildBusinessDocument(hyj, settlementCustomers, contractManage, "0D01", "租金(固定租)", warehouseTempLayerType, serviceItemsName);
}
}
}
@@ -1425,4 +1429,196 @@ public class ContractManageApplicationService {
log.error("保存业务单据失败: {}", ajaxResult != null ? ajaxResult.get("msg") : "feign调用异常");
}
}
private static final String WEBSERVICE_URL = "http://10.33.0.116:8080/sys/webservice/ngLegalByFindWebserviceService";
public List<String> getContractNosByCompanyName(String companyName) {
List<String> contractNos = new ArrayList<>();
try {
log.info("=== 开始查询合同编号 ===");
log.info("查询公司: {}", companyName);
// 1. 构建JSON参数 - 使用绝对正确的格式
String fdRelativePersonJson = "{\"fdName\":\"" + companyName + "\"}";
log.info("JSON参数: {}", fdRelativePersonJson);
// 2. 构建XML请求 - 使用和Postman完全相同的格式
String httpRequest = buildXmlRequest(companyName);
log.info("XML请求: {}", httpRequest);
log.info("XML请求生成完成,长度: {} 字符", httpRequest.length());
// 3. 调用WebService
log.info("正在调用WebService...");
String httpResponse = HttpWebServiceUtil.callWebService(httpRequest, WEBSERVICE_URL);
// 4. 提取JSON - 使用更可靠的提取方法
String jsonData = extractJsonFromResponse(httpResponse);
log.info("正在解析JSON..."+jsonData);
//jsonData内容
// {
// "returnState": "2",
// "data": [
// {
// "contract_Id": "16f830763e15793f5aafab14b309075c",
// "contract_Name": "档案保管服务协议",
// "contract_No": "HT20200108130",
// "contract_Total": 134323.36,
// "contract_Star": "20181101",
// "contract_End": "20231101",
// "contract_relative": [
// {
// "relativeName": "珠海通宇物流有限公司"
// }
// ],
// "contract_Operator": "蔡振燕",
// "contract_capitalFlows": "支出",
// "contract_SigningName": "南光置业有限公司-本部",
// "contract_pay": [],
// "contract_purchases": []
// }
// ]
// }
if (jsonData == null || jsonData.trim().isEmpty()) {
log.warn("未提取到JSON数据,返回空列表");
return contractNos;
}
log.info("提取到JSON数据,长度: {} 字符", jsonData.length());
// 5. 解析JSON
List<String> result = parseContractNosFromJson(jsonData);
contractNos.addAll(result);
log.info("=== 查询完成 ===");
log.info("找到 {} 个合同编号", contractNos.size());
} catch (Exception e) {
log.error("查询合同编号失败", e);
throw new RuntimeException("查询合同编号失败: " + e.getMessage(), e);
}
return contractNos;
}
/**
* 构建XML请求 - 硬编码确保和Postman完全一致
*/
private String buildXmlRequest(String companyName) {
// 使用和Postman完全相同的格式
String jsonParam = "{\"fdName\":\"" + companyName + "\"}";
return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"" +
" xmlns:web=\"http://webservice.legal.ng.kmss.landray.com/\">" +
"<soapenv:Body>" +
"<web:findNgLegal>" +
"<arg0 xmlns=\"\">" +
"<fdPageNo>1</fdPageNo>" +
"<fdRowSize>10</fdRowSize>" +
"<fdRelativePerson><![CDATA[" + jsonParam + "]]></fdRelativePerson>" +
"<fdContractNo></fdContractNo>" +
"<fdPersonNo>NA0301</fdPersonNo>" +
"<fdSponsor></fdSponsor>" +
"<docCreateTimeStar></docCreateTimeStar>" +
"<docCreateTimeEnd></docCreateTimeEnd>" +
"</arg0>" +
"</web:findNgLegal>" +
"</soapenv:Body>" +
"</soapenv:Envelope>";
}
/**
* 从SOAP响应中提取JSON
*/
private String extractJsonFromResponse(String soapResponse) {
if (soapResponse == null || soapResponse.isEmpty()) {
return null;
}
// 直接查找JSON部分
int jsonStart = soapResponse.indexOf("{\"returnState\":");
int jsonEnd = soapResponse.indexOf("}</return>");
if (jsonStart != -1 && jsonEnd != -1) {
// 找到JSON结束位置
jsonEnd = jsonEnd + 1; // 包含右大括号
String json = soapResponse.substring(jsonStart, jsonEnd);
log.info("直接提取JSON成功");
return json;
}
// 备用方法:查找<return>标签
int start = soapResponse.indexOf("<return>");
int end = soapResponse.indexOf("</return>");
if (start != -1 && end != -1) {
start += "<return>".length();
String json = soapResponse.substring(start, end);
// 解码XML实体
json = json.replace("&lt;", "<")
.replace("&gt;", ">")
.replace("&amp;", "&")
.replace("&quot;", "\"")
.replace("&apos;", "'");
log.info("从<return>标签提取JSON成功");
return json.trim();
}
log.warn("无法从响应中提取JSON");
return null;
}
/**
* 解析JSON获取合同编号
*/
private List<String> parseContractNosFromJson(String jsonData) {
List<String> contractNos = new ArrayList<>();
try {
com.alibaba.fastjson.JSONObject jsonObject = com.alibaba.fastjson.JSONObject.parseObject(jsonData);
// 检查返回状态
String returnState = jsonObject.getString("returnState");
if (!"2".equals(returnState)) {
log.warn("返回状态异常: {}", returnState);
return contractNos;
}
// 获取data数组
JSONArray dataArray = jsonObject.getJSONArray("data");
if (dataArray == null || dataArray.isEmpty()) {
log.info("查询成功,但未找到合同数据");
return contractNos;
}
log.info("找到 {} 个合同", dataArray.size());
// 遍历提取合同编号
for (int i = 0; i < dataArray.size(); i++) {
com.alibaba.fastjson.JSONObject contract = dataArray.getJSONObject(i);
String contractNo = contract.getString("contract_No");
String contractName = contract.getString("contract_Name");
if (contractNo != null && !contractNo.trim().isEmpty()) {
contractNos.add(contractNo.trim() + " " + contractName.trim());
log.info("合同 {}: {}", i + 1, contractNo);
} else {
log.warn("第 {} 个合同没有编号", i + 1);
}
}
} catch (Exception e) {
log.error("JSON解析失败", e);
log.error("原始JSON: {}", jsonData);
}
return contractNos;
}
}
@@ -0,0 +1,96 @@
package com.mhd.basic.domain.contractManage.repository.util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.zip.GZIPInputStream;
@Slf4j
@Component
public class HttpWebServiceUtil {
/**
* 调用HTTP WebService
*/
public static String callWebService(String requestBody, String endpointUrl) {
try {
// 创建RestTemplate
RestTemplate restTemplate = new RestTemplate();
// 设置请求头
HttpHeaders headers = new HttpHeaders();
// headers.setContentType(MediaType.TEXT_XML);
// headers.setAccept(Collections.singletonList(MediaType.TEXT_XML));
headers.setContentType(MediaType.parseMediaType("text/xml; charset=UTF-8")); // 核心修复
headers.setAccept(Collections.singletonList(MediaType.ALL));
headers.set("SOAPAction", "");
headers.set("User-Agent", "PostmanRuntime/7.26.10");
headers.set("Cache-Control", "no-cache");
headers.set("Accept-Encoding", "gzip, deflate");
headers.set("Connection", "keep-alive");
// 创建请求实体
HttpEntity<String> entity = new HttpEntity<>(requestBody, headers);
log.info("发送请求到: {}", endpointUrl);
// 发送请求
ResponseEntity<byte[]> response = restTemplate.exchange(
endpointUrl,
HttpMethod.POST,
entity,
byte[].class
);
log.info("响应状态: {}", response.getStatusCode());
// 处理响应
byte[] rawResponse = response.getBody();
if (rawResponse == null) {
return "";
}
// 检查是否gzip压缩
String contentEncoding = response.getHeaders().getFirst("Content-Encoding");
String responseBody;
if ("gzip".equalsIgnoreCase(contentEncoding)) {
responseBody = decompressGzip(rawResponse);
log.info("已解压gzip响应,长度: {} 字符", responseBody.length());
} else {
responseBody = new String(rawResponse, StandardCharsets.UTF_8);
log.info("响应长度: {} 字符", responseBody.length());
}
return responseBody;
} catch (Exception e) {
log.error("WebService调用失败", e);
throw new RuntimeException("WebService调用失败: " + e.getMessage(), e);
}
}
/**
* 解压缩GZIP内容
*/
private static String decompressGzip(byte[] compressedData) throws IOException {
try (GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(compressedData));
InputStreamReader reader = new InputStreamReader(gis, StandardCharsets.UTF_8)) {
StringBuilder sb = new StringBuilder();
char[] buffer = new char[1024];
int len;
while ((len = reader.read(buffer)) != -1) {
sb.append(buffer, 0, len);
}
return sb.toString();
}
}
}
@@ -8,6 +8,7 @@ import com.mhd.basic.interfaces.assember.contractManage.ContractManageAssembler;
import com.mhd.basic.interfaces.dto.contractManage.ContractManageExportVO;
import com.mhd.basic.interfaces.dto.contractManage.SubjectAndPriceDTO;
import com.mhd.basic.interfaces.dto.contractManage.SubjectAndPriceReturn;
import com.mhd.common.core.utils.sms.Result;
import com.mhd.system.api.domain.ContractManageFeignDTO;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
@@ -272,4 +273,17 @@ public class ContractManageApi extends BaseController{
}
}
/**
* 根据公司名称获取合同编号列表
* @param companyName 公司名称
* @return 合同编号列表
*/
@GetMapping("/contractNos")
public AjaxResult getContractNosByCompanyName(@RequestParam String companyName) {
contractManageApplicationService.getContractNosByCompanyName(companyName);
return AjaxResult.success();
}
}