refactor: 删除 GetPlmFileUtil 类
This commit is contained in:
parent
df9a6f7d79
commit
3269e4cbd7
|
@ -1,283 +0,0 @@
|
||||||
package nc.bs.uapbd.util;
|
|
||||||
|
|
||||||
import com.alibaba.fastjson.JSONArray;
|
|
||||||
import com.alibaba.fastjson.JSONObject;
|
|
||||||
import nc.bs.dao.DAOException;
|
|
||||||
import nc.bs.logging.Log;
|
|
||||||
import nc.bs.trade.business.HYSuperDMO;
|
|
||||||
import nc.vo.bd.defdoc.DefdocVO;
|
|
||||||
import nc.vo.cmp.util.StringUtils;
|
|
||||||
import nc.vo.pub.BusinessException;
|
|
||||||
import nccloud.framework.core.io.WebFile;
|
|
||||||
import org.apache.http.client.config.CookieSpecs;
|
|
||||||
import org.apache.http.client.config.RequestConfig;
|
|
||||||
import org.apache.http.client.methods.HttpGet;
|
|
||||||
import org.apache.http.impl.client.CloseableHttpClient;
|
|
||||||
import org.apache.http.impl.client.HttpClients;
|
|
||||||
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
|
|
||||||
import org.apache.http.util.EntityUtils;
|
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
|
||||||
import java.io.ByteArrayOutputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.text.SimpleDateFormat;
|
|
||||||
import java.util.Date;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.zip.ZipEntry;
|
|
||||||
import java.util.zip.ZipOutputStream;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取PLM图纸的工具类
|
|
||||||
*
|
|
||||||
* @author mzr
|
|
||||||
* @date 2025/8/1
|
|
||||||
*/
|
|
||||||
public class GetPlmFileUtil {
|
|
||||||
private static final String LOG_INFO_NAME = "dldzlog";
|
|
||||||
private static final Log logger = Log.getInstance(LOG_INFO_NAME);
|
|
||||||
private String plmBaseUrl = "";
|
|
||||||
private String plmUser = "";
|
|
||||||
private String token = "";
|
|
||||||
private static final String tokenUrl = "/sipmweb/api/oauth";
|
|
||||||
// 根据物料编码获取零部件ID
|
|
||||||
private String materialIdUrl = "/sipmweb/api/{rid}/search/{t}";
|
|
||||||
// 根据零部件ID获取二维图档ID及信息
|
|
||||||
private String materialFileIdUrl = "/sipmweb/api/{rid}/relation/{t}/{id}/data";
|
|
||||||
// 下载文件
|
|
||||||
private String downlownUrl = "/sipmweb/web/download";
|
|
||||||
// http请求
|
|
||||||
private static final PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
|
|
||||||
private static final CloseableHttpClient httpClient;
|
|
||||||
|
|
||||||
static {
|
|
||||||
connectionManager.setMaxTotal(5000);
|
|
||||||
connectionManager.setDefaultMaxPerRoute(500);
|
|
||||||
|
|
||||||
RequestConfig globalConfig = RequestConfig.custom()
|
|
||||||
.setConnectionRequestTimeout(50000)
|
|
||||||
.setConnectTimeout(50000)
|
|
||||||
.setSocketTimeout(200000)
|
|
||||||
.setCookieSpec(CookieSpecs.IGNORE_COOKIES)
|
|
||||||
.build();
|
|
||||||
|
|
||||||
httpClient = HttpClients.custom()
|
|
||||||
.setConnectionManager(connectionManager)
|
|
||||||
.setDefaultRequestConfig(globalConfig)
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
|
|
||||||
public WebFile getPlmFiles(String[] materialCodeArr) throws Exception {
|
|
||||||
WebFile file = null;
|
|
||||||
if (materialCodeArr == null || materialCodeArr.length == 0) {
|
|
||||||
return file;
|
|
||||||
}
|
|
||||||
// 获取PLM的参数
|
|
||||||
Map<String, String> configParams = getConfigParams("Dldz-config");
|
|
||||||
if (configParams == null || configParams.isEmpty()) {
|
|
||||||
throw new BusinessException("未配置PLM参数");
|
|
||||||
}
|
|
||||||
plmBaseUrl = configParams.get("plmBaseUrl");
|
|
||||||
plmUser = configParams.get("plmUser");
|
|
||||||
token = getToken();
|
|
||||||
if (materialCodeArr.length == 1) {
|
|
||||||
String materialCode = materialCodeArr[0];
|
|
||||||
JSONObject plmFileJson = this.getPlmFile(materialCode);
|
|
||||||
String objId = plmFileJson.getString("objId");
|
|
||||||
String fname = plmFileJson.getString("fname");
|
|
||||||
|
|
||||||
byte[] fileBytes = this.doDownloadPlmFile(objId);
|
|
||||||
if (fileBytes.length == 0) {
|
|
||||||
throw new BusinessException("未查询到PLM的文件");
|
|
||||||
}
|
|
||||||
InputStream ins = new ByteArrayInputStream(fileBytes);
|
|
||||||
file = new WebFile(fname, ins);
|
|
||||||
} else {
|
|
||||||
// 多个物料合并成zip文件输出文件流
|
|
||||||
// 创建内存中的 ZIP 输出流
|
|
||||||
ByteArrayOutputStream zipOut = new ByteArrayOutputStream();
|
|
||||||
ZipOutputStream zipStream = new ZipOutputStream(zipOut);
|
|
||||||
try {
|
|
||||||
for (String materialCode : materialCodeArr) {
|
|
||||||
JSONObject plmFileJson = this.getPlmFile(materialCode);
|
|
||||||
String objId = plmFileJson.getString("objId");
|
|
||||||
String fname = plmFileJson.getString("fname");
|
|
||||||
byte[] fileBytes = this.doDownloadPlmFile(objId);
|
|
||||||
if (fileBytes.length == 0) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
zipStream.putNextEntry(new ZipEntry(fname));
|
|
||||||
zipStream.write(fileBytes);
|
|
||||||
zipStream.closeEntry();
|
|
||||||
}
|
|
||||||
zipStream.finish();
|
|
||||||
} finally {
|
|
||||||
zipStream.close();
|
|
||||||
}
|
|
||||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
|
|
||||||
String zipName = "物料图纸下载_" + sdf.format(new Date());
|
|
||||||
// 构造 WebFile 返回 ZIP 文件
|
|
||||||
InputStream ins = new ByteArrayInputStream(zipOut.toByteArray());
|
|
||||||
file = new WebFile(zipName + ".zip", ins);
|
|
||||||
}
|
|
||||||
|
|
||||||
return file;
|
|
||||||
}
|
|
||||||
|
|
||||||
public JSONObject getPlmFile(String materialCode) throws Exception {
|
|
||||||
String materialId = getMaterialId(materialCode);
|
|
||||||
return getFileId(materialId);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取token
|
|
||||||
*/
|
|
||||||
private String getToken() throws IOException, BusinessException {
|
|
||||||
Map<String, String> tokenMap = new HashMap<>();
|
|
||||||
tokenMap.put("uname", plmUser);
|
|
||||||
tokenMap.put("f", "true");
|
|
||||||
String tokenStr = doGet(plmBaseUrl + tokenUrl, tokenMap);
|
|
||||||
logger.error("GetPlmFileUtil-getToken-tokenStr = " + tokenStr);
|
|
||||||
JSONObject jsonObject = JSONObject.parseObject(tokenStr);
|
|
||||||
String token = jsonObject.getString("errmsg");
|
|
||||||
if (token == null || token.isEmpty()) {
|
|
||||||
throw new BusinessException("PLM鉴权失败");
|
|
||||||
}
|
|
||||||
return token;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 根据物料编码获取零部件ID
|
|
||||||
*/
|
|
||||||
private String getMaterialId(String materialCode) throws IOException, BusinessException {
|
|
||||||
String fileUrl = plmBaseUrl + materialIdUrl;
|
|
||||||
fileUrl = fileUrl.replace("{rid}", token);
|
|
||||||
// 对象表名 MPART(零部件)
|
|
||||||
fileUrl = fileUrl.replace("{t}", "MPART");
|
|
||||||
Map<String, String> map = new HashMap<>();
|
|
||||||
map.put("key", materialCode);// 搜索关键字
|
|
||||||
map.put("start", "0");// 偏移量
|
|
||||||
map.put("size", "10");// 数量
|
|
||||||
String result = doGet(fileUrl, map);
|
|
||||||
logger.error("GetPlmFileUtil-getMaterialId-result = " + result);
|
|
||||||
JSONObject jsonObject = JSONObject.parseObject(result);
|
|
||||||
String list = jsonObject.getString("list");
|
|
||||||
JSONArray jsonArray = JSONObject.parseArray(list);
|
|
||||||
if (jsonArray == null || jsonArray.isEmpty()) {
|
|
||||||
throw new BusinessException("获取PLM物料id失败");
|
|
||||||
}
|
|
||||||
JSONObject listJson = jsonArray.getJSONObject(0);
|
|
||||||
String objId = listJson.getString("objId");
|
|
||||||
if (objId == null || objId.isEmpty()) {
|
|
||||||
throw new BusinessException("获取PLM物料id失败");
|
|
||||||
}
|
|
||||||
return objId;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 根据零部件ID获取文件ID
|
|
||||||
*/
|
|
||||||
private JSONObject getFileId(String materialId) throws IOException, BusinessException {
|
|
||||||
String fileUrl = plmBaseUrl + materialFileIdUrl;
|
|
||||||
fileUrl = fileUrl.replace("{rid}", token);
|
|
||||||
// 对象表名 MPART(零部件)
|
|
||||||
fileUrl = fileUrl.replace("{t}", "MPART");
|
|
||||||
// 对象id
|
|
||||||
fileUrl = fileUrl.replace("{id}", materialId);
|
|
||||||
Map<String, String> map = new HashMap<>();
|
|
||||||
map.put("re", "MPART_SIPM1");// 关系ID 二维图档:MPART_SIPM1
|
|
||||||
map.put("start", "0");// 偏移量
|
|
||||||
map.put("item", "SIPM1");// 关联对象表名
|
|
||||||
String result = doGet(fileUrl, map);
|
|
||||||
logger.error("GetPlmFileUtil-getFileId-result = " + result);
|
|
||||||
JSONObject jsonObject = JSONObject.parseObject(result);
|
|
||||||
String list = jsonObject.getString("list");
|
|
||||||
JSONArray jsonArray = JSONObject.parseArray(list);
|
|
||||||
if (jsonArray == null || jsonArray.isEmpty()) {
|
|
||||||
throw new BusinessException("获取PLM物料的文件信息失败");
|
|
||||||
}
|
|
||||||
JSONObject listJson = jsonArray.getJSONObject(0);
|
|
||||||
String objId = listJson.getString("objId");
|
|
||||||
if (objId == null || objId.isEmpty()) {
|
|
||||||
throw new BusinessException("获取PLM物料的文件信息失败");
|
|
||||||
}
|
|
||||||
return listJson;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 调用PLM的下载文件接口
|
|
||||||
*/
|
|
||||||
private byte[] doDownloadPlmFile(String fileId) throws IOException {
|
|
||||||
String fileUrl = plmBaseUrl + downlownUrl;
|
|
||||||
Map<String, String> map = new HashMap<>();
|
|
||||||
map.put("rid", token);
|
|
||||||
map.put("id", fileId);// 对象id
|
|
||||||
map.put("t", "SIPM1");// 对象表名
|
|
||||||
map.put("type", "D");// 文件类别(BD,D)(D是文件本身的文件,BD是PDF图)
|
|
||||||
return getFileFromPlm(fileUrl, map);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Map<String, String> getConfigParams(String code) {
|
|
||||||
Map<String, String> map = new HashMap<>();
|
|
||||||
String strWhere = " pk_defdoclist in (select pk_defdoclist from bd_defdoclist where code='[code]' and dr=0 ) and dr=0";
|
|
||||||
strWhere = strWhere.replace("[code]", code);
|
|
||||||
try {
|
|
||||||
DefdocVO[] defdocVOs = (DefdocVO[]) new HYSuperDMO().queryByWhereClause(DefdocVO.class, strWhere);
|
|
||||||
if (defdocVOs != null) {
|
|
||||||
for (DefdocVO defdocVO : defdocVOs) {
|
|
||||||
String value = StringUtils.isEmpty(defdocVO.getMemo()) ? defdocVO.getName() : defdocVO.getMemo();
|
|
||||||
map.put(defdocVO.getCode().trim(), value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (DAOException e) {
|
|
||||||
logger.error("Failed to get config parameters for code: " + code, e);
|
|
||||||
}
|
|
||||||
return map;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private String doGet(String requestUrl, Map<String, String> paramMap) throws IOException {
|
|
||||||
StringBuilder param = new StringBuilder("?");
|
|
||||||
if (paramMap != null) {
|
|
||||||
for (Map.Entry<String, String> entry : paramMap.entrySet()) {
|
|
||||||
param.append(entry.getKey());
|
|
||||||
param.append("=");
|
|
||||||
param.append(entry.getValue());
|
|
||||||
param.append("&");
|
|
||||||
}
|
|
||||||
param.deleteCharAt(param.length() - 1);
|
|
||||||
}
|
|
||||||
String url = requestUrl + param;
|
|
||||||
HttpGet get = new HttpGet(url);
|
|
||||||
String responseString = httpClient.execute(get, response -> EntityUtils.toString(response.getEntity()));
|
|
||||||
get.releaseConnection();
|
|
||||||
return responseString;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 调用第三方文件接口并接收文件流
|
|
||||||
*
|
|
||||||
* @param requestUrl 文件接口URL
|
|
||||||
* @return 文件流
|
|
||||||
*/
|
|
||||||
private byte[] getFileFromPlm(String requestUrl, Map<String, String> paramMap) throws IOException {
|
|
||||||
StringBuilder param = new StringBuilder("?");
|
|
||||||
if (paramMap != null) {
|
|
||||||
for (Map.Entry<String, String> entry : paramMap.entrySet()) {
|
|
||||||
param.append(entry.getKey());
|
|
||||||
param.append("=");
|
|
||||||
param.append(entry.getValue());
|
|
||||||
param.append("&");
|
|
||||||
}
|
|
||||||
param.deleteCharAt(param.length() - 1);
|
|
||||||
}
|
|
||||||
String url = requestUrl + param;
|
|
||||||
HttpGet get = new HttpGet(url);
|
|
||||||
byte[] responseString = httpClient.execute(get, response -> EntityUtils.toByteArray(response.getEntity()));
|
|
||||||
get.releaseConnection();
|
|
||||||
return responseString;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
Loading…
Reference in New Issue