获取物料PLM文件
This commit is contained in:
parent
2b301aa939
commit
a6f637ba97
|
@ -1,14 +1,37 @@
|
|||
package nccloud.web.uapbd.material.action;
|
||||
|
||||
import nc.bs.logging.Logger;
|
||||
import nc.bs.uapbd.util.GetPlmFileUtil;
|
||||
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.baseapp.core.log.NCCForUAPLogger;
|
||||
import nccloud.framework.core.exception.ExceptionUtils;
|
||||
import nccloud.framework.core.io.WebFile;
|
||||
import nccloud.framework.web.action.itf.ICommonAction;
|
||||
import nccloud.framework.web.container.IRequest;
|
||||
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.Arrays;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
/**
|
||||
* »ñÈ¡ÎïÁÏPLMÎļþ
|
||||
|
@ -17,22 +40,270 @@ import java.util.Map;
|
|||
* @date 2025/8/4
|
||||
*/
|
||||
public class MaterialPlmDownloadAction implements ICommonAction {
|
||||
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";
|
||||
|
||||
@Override
|
||||
public Object doAction(IRequest request) {
|
||||
WebFile files = null;
|
||||
try {
|
||||
Map<String, String[]> params_1 = request.readParameters();
|
||||
String[] pks = params_1.get("materialCode"); // »ñÈ¡ËùÓÐ pk
|
||||
NCCForUAPLogger.debug("»ñÈ¡ËùÓÐ pk:" + Arrays.toString(pks));
|
||||
String materialCode = "101092250323,101092250321,101092250322";
|
||||
String[] materialCodeArr = materialCode.split(",", -1);
|
||||
GetPlmFileUtil fileUtil = new GetPlmFileUtil();
|
||||
files = fileUtil.getPlmFiles(materialCodeArr);
|
||||
String[] materialCodeArr = params_1.get("materialCode"); // 获取所有 pk
|
||||
if (materialCodeArr == null || materialCodeArr.length == 0) {
|
||||
ExceptionUtils.wrapBusinessException("物料编码不能为空");
|
||||
}
|
||||
NCCForUAPLogger.debug("MaterialPlmDownloadAction-pk = " + Arrays.toString(materialCodeArr));
|
||||
// String materialCode = "101092250323,101092250321,101092250322";
|
||||
// String[] materialCodeArr = materialCode.split(",", -1);
|
||||
files = this.getPlmFiles(materialCodeArr);
|
||||
} catch (Exception e) {
|
||||
Logger.error("MaterialPlmDownloadAction-exp:" + e.getMessage());
|
||||
throw new RuntimeException(e);
|
||||
logger.error("MaterialPlmDownloadAction-exp:" + e.getMessage());
|
||||
ExceptionUtils.wrapException(e);
|
||||
}
|
||||
return files;
|
||||
}
|
||||
|
||||
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 {
|
||||
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
|
||||
cm.setMaxTotal(5000);
|
||||
cm.setDefaultMaxPerRoute(500);
|
||||
|
||||
RequestConfig globalConfig = RequestConfig.custom().setConnectionRequestTimeout(50000) // 连接池获取连接超时
|
||||
.setConnectTimeout(50000) // 连接建立超时
|
||||
.setSocketTimeout(200000) // 等待响应超时
|
||||
.setCookieSpec(CookieSpecs.IGNORE_COOKIES).build();
|
||||
|
||||
CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm)
|
||||
.setDefaultRequestConfig(globalConfig).build();
|
||||
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 {
|
||||
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
|
||||
cm.setMaxTotal(5000);
|
||||
cm.setDefaultMaxPerRoute(500);
|
||||
|
||||
RequestConfig globalConfig = RequestConfig.custom().setConnectionRequestTimeout(50000) // 连接池获取连接超时
|
||||
.setConnectTimeout(50000) // 连接建立超时
|
||||
.setSocketTimeout(200000) // 等待响应超时
|
||||
.setCookieSpec(CookieSpecs.IGNORE_COOKIES).build();
|
||||
|
||||
CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm)
|
||||
.setDefaultRequestConfig(globalConfig).build();
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,7 +17,6 @@ import org.apache.http.impl.client.HttpClients;
|
|||
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
|
||||
import javax.xml.bind.DatatypeConverter;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
|
@ -89,8 +88,10 @@ public class GetPlmFileUtil {
|
|||
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文件输出文件流
|
||||
|
@ -103,6 +104,9 @@ public class GetPlmFileUtil {
|
|||
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();
|
||||
|
@ -276,22 +280,4 @@ public class GetPlmFileUtil {
|
|||
return responseString;
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析文件流
|
||||
*
|
||||
* @param inputStream 文件流
|
||||
* @return 解析结果
|
||||
*/
|
||||
private byte[] parseFileStream(InputStream inputStream) throws IOException {
|
||||
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
|
||||
int nRead;
|
||||
byte[] data = new byte[1024];
|
||||
while ((nRead = inputStream.read(data, 0, data.length)) != -1) {
|
||||
buffer.write(data, 0, nRead);
|
||||
}
|
||||
byte[] byteArray = buffer.toByteArray();
|
||||
buffer.close();
|
||||
return byteArray;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue