客商委托方数据同步

This commit is contained in:
秦鸿展
2026-06-08 15:05:34 +08:00
parent 734be2b825
commit c59fa363ee
23 changed files with 959 additions and 17 deletions
@@ -0,0 +1,232 @@
package com.linke.thirdParty.application.server;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.linke.thirdParty.domain.departInterfaceInfo.entity.SysDepartInterfaceInfo;
import com.linke.thirdParty.domain.departInterfaceInfo.service.SysDepartInterfaceInfoDomainService;
import com.mhd.common.core.domain.thirdparty.MdmMerchantQueryDTO;
import com.mhd.common.core.domain.thirdparty.MdmMerchantQueryResultDTO;
import com.mhd.common.core.domain.thirdparty.MdmMerchantRecordDTO;
import com.mhd.common.core.exception.ServiceException;
import com.mhd.common.core.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
/**
* 北京三维天地 MDM 客商主数据查询
*/
@Service
@Slf4j
public class MdmMasterDataApplicationService {
private static final String INTERFACE_TYPE = "mdm_merchant";
private static final String DEFAULT_URL = "http://10.30.70.2/esbmule/services/query/mdm_nkks_query";
@Autowired
private SysDepartInterfaceInfoDomainService sysDepartInterfaceInfoDomainService;
public MdmMerchantQueryResultDTO queryMerchant(MdmMerchantQueryDTO queryDTO) {
MdmConfig config = loadConfig();
String requestBody = buildRequestBody(queryDTO);
log.info("MDM客商查询请求 orgNcCode={}, page={}, lastTime={}",
queryDTO.getOrgNcCode(), queryDTO.getCurrentPage(), queryDTO.getLastModifyRecordTime());
String responseBody = doPost(config.url, config.usercode, config.password, requestBody);
return parseResponse(responseBody);
}
private MdmConfig loadConfig() {
MdmConfig config = new MdmConfig();
config.url = DEFAULT_URL;
List<SysDepartInterfaceInfo> list = sysDepartInterfaceInfoDomainService.queryListByInterfaceType(INTERFACE_TYPE);
for (SysDepartInterfaceInfo info : list) {
if (info == null || StringUtils.isEmpty(info.getInterfaceTypeKey())) {
continue;
}
switch (info.getInterfaceTypeKey()) {
case "url":
config.url = info.getInterfaceTypeValue();
break;
case "usercode":
config.usercode = info.getInterfaceTypeValue();
break;
case "password":
config.password = info.getInterfaceTypeValue();
break;
default:
break;
}
}
if (StringUtils.isEmpty(config.usercode) || StringUtils.isEmpty(config.password)) {
throw new ServiceException("MDM客商接口未配置 usercode/password,请在三方接口配置中维护 interfaceType=mdm_merchant");
}
return config;
}
private String buildRequestBody(MdmMerchantQueryDTO queryDTO) {
JSONObject dataInfoItem = new JSONObject();
if (StringUtils.isNotEmpty(queryDTO.getOrgNcCode())) {
dataInfoItem.put("DESC24", queryDTO.getOrgNcCode());
}
if (StringUtils.isNotEmpty(queryDTO.getLastModifyRecordTime())) {
dataInfoItem.put("LASTMODIFYRECORDTIME", queryDTO.getLastModifyRecordTime());
}
JSONArray dataInfoArray = new JSONArray();
dataInfoArray.add(dataInfoItem);
JSONObject dataInfos = new JSONObject();
dataInfos.put("PUUID", UUID.randomUUID().toString().replace("-", ""));
dataInfos.put("DATAINFO", dataInfoArray);
JSONObject splitPage = new JSONObject();
splitPage.put("COUNTPERPAGE", String.valueOf(queryDTO.getCountPerPage() == null ? 500 : queryDTO.getCountPerPage()));
splitPage.put("CURRENTPAGE", String.valueOf(queryDTO.getCurrentPage() == null ? 1 : queryDTO.getCurrentPage()));
JSONObject data = new JSONObject();
data.put("DATAINFOS", dataInfos);
data.put("SPLITPAGE", splitPage);
JSONObject esb = new JSONObject();
esb.put("DATA", data);
JSONObject root = new JSONObject();
root.put("ESB", esb);
return root.toJSONString();
}
private String doPost(String url, String usercode, String password, String body) {
HttpURLConnection conn = null;
try {
conn = (HttpURLConnection) new URL(url).openConnection();
conn.setRequestMethod("POST");
conn.setConnectTimeout(30000);
conn.setReadTimeout(60000);
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestProperty("Content-Type", "application/json;charset=utf-8");
conn.setRequestProperty("usercode", usercode);
conn.setRequestProperty("password", password);
try (OutputStream os = conn.getOutputStream()) {
os.write(body.getBytes(StandardCharsets.UTF_8));
os.flush();
}
int code = conn.getResponseCode();
BufferedReader reader = new BufferedReader(new InputStreamReader(
code >= 400 ? conn.getErrorStream() : conn.getInputStream(), StandardCharsets.UTF_8));
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
reader.close();
if (code >= 400) {
throw new ServiceException("MDM客商接口HTTP异常,status=" + code + ", body=" + sb);
}
return sb.toString();
} catch (ServiceException e) {
throw e;
} catch (Exception e) {
log.error("MDM客商接口调用失败", e);
throw new ServiceException("MDM客商接口调用失败:" + e.getMessage());
} finally {
if (conn != null) {
conn.disconnect();
}
}
}
private MdmMerchantQueryResultDTO parseResponse(String responseBody) {
if (StringUtils.isEmpty(responseBody)) {
throw new ServiceException("MDM客商接口返回为空");
}
JSONObject root = JSON.parseObject(responseBody);
JSONObject esb = root.getJSONObject("ESB");
if (esb == null) {
throw new ServiceException("MDM客商接口返回格式异常:缺少ESB节点");
}
MdmMerchantQueryResultDTO resultDTO = new MdmMerchantQueryResultDTO();
resultDTO.setResult(esb.getString("RESULT"));
resultDTO.setDesc(esb.getString("DESC"));
if (!"S".equalsIgnoreCase(resultDTO.getResult())) {
throw new ServiceException("MDM客商接口返回失败:" + resultDTO.getDesc());
}
JSONObject data = esb.getJSONObject("DATA");
if (data == null) {
return resultDTO;
}
JSONObject splitPage = data.getJSONObject("SPLITPAGE");
if (splitPage != null) {
resultDTO.setCurrentPage(parseInt(splitPage.getString("CURRENTPAGE")));
resultDTO.setTotalPages(parseInt(splitPage.getString("TOTALPAGES")));
resultDTO.setTotalNumber(parseInt(splitPage.getString("TOTALNUMBER")));
}
JSONObject dataInfos = data.getJSONObject("DATAINFOS");
if (dataInfos == null) {
return resultDTO;
}
Object dataInfoObj = dataInfos.get("DATAINFO");
List<MdmMerchantRecordDTO> records = new ArrayList<>();
if (dataInfoObj instanceof JSONArray) {
JSONArray array = (JSONArray) dataInfoObj;
for (int i = 0; i < array.size(); i++) {
records.add(mapRecord(array.getJSONObject(i)));
}
} else if (dataInfoObj instanceof JSONObject) {
records.add(mapRecord((JSONObject) dataInfoObj));
}
resultDTO.setDataInfo(records);
return resultDTO;
}
private MdmMerchantRecordDTO mapRecord(JSONObject item) {
MdmMerchantRecordDTO record = new MdmMerchantRecordDTO();
record.setCode(item.getString("CODE"));
record.setDesc1(item.getString("DESC1"));
record.setDesc2(item.getString("DESC2"));
record.setDesc3(item.getString("DESC3"));
record.setDesc4(item.getString("DESC4"));
record.setDesc7(item.getString("DESC7"));
record.setDesc8(item.getString("DESC8"));
record.setDesc13(item.getString("DESC13"));
record.setDesc14(item.getString("DESC14"));
record.setDesc15(item.getString("DESC15"));
record.setDesc16(item.getString("DESC16"));
record.setDesc23(item.getString("DESC23"));
record.setDesc24(item.getString("DESC24"));
record.setDesc25(item.getString("DESC25"));
record.setDesc29(item.getString("DESC29"));
record.setDesc30(item.getString("DESC30"));
record.setLastModifyRecordTime(item.getString("LASTMODIFYRECORDTIME"));
record.setUuid(item.getString("UUID"));
return record;
}
private Integer parseInt(String value) {
if (StringUtils.isEmpty(value)) {
return null;
}
try {
return Integer.parseInt(value.trim());
} catch (NumberFormatException e) {
return null;
}
}
private static class MdmConfig {
private String url;
private String usercode;
private String password;
}
}
@@ -0,0 +1,36 @@
package com.linke.thirdParty.interfaces.facade;
import com.linke.thirdParty.application.server.MdmMasterDataApplicationService;
import com.mhd.common.core.domain.entity.R;
import com.mhd.common.core.domain.thirdparty.MdmMerchantQueryDTO;
import com.mhd.common.core.domain.thirdparty.MdmMerchantQueryResultDTO;
import com.mhd.common.core.web.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Api(tags = "MDM主数据")
@RestController
@RequestMapping("/mdmMasterData")
@Slf4j
public class MdmMasterDataApi extends BaseController {
@Autowired
private MdmMasterDataApplicationService mdmMasterDataApplicationService;
@ApiOperation("查询客商主数据")
@PostMapping("/queryMerchant")
public R<MdmMerchantQueryResultDTO> queryMerchant(@RequestBody MdmMerchantQueryDTO queryDTO) {
try {
return R.ok(mdmMasterDataApplicationService.queryMerchant(queryDTO));
} catch (Exception e) {
log.error("查询MDM客商主数据失败", e);
return R.fail(e.getMessage());
}
}
}