From 63fb6936a2b7935f73257c5712fd164982783c0b Mon Sep 17 00:00:00 2001 From: mzr Date: Tue, 16 Sep 2025 08:43:25 +0800 Subject: [PATCH] =?UTF-8?q?plm=E6=96=87=E4=BB=B6=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../action/MaterialPlmDownloadAction.java | 90 +++++++++++++------ 1 file changed, 65 insertions(+), 25 deletions(-) diff --git a/uapbd/src/client/nccloud/web/uapbd/material/action/MaterialPlmDownloadAction.java b/uapbd/src/client/nccloud/web/uapbd/material/action/MaterialPlmDownloadAction.java index 1d74ff0..229a605 100644 --- a/uapbd/src/client/nccloud/web/uapbd/material/action/MaterialPlmDownloadAction.java +++ b/uapbd/src/client/nccloud/web/uapbd/material/action/MaterialPlmDownloadAction.java @@ -33,7 +33,7 @@ import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; /** - * 获取物料PLM文件(同时下载SIPM1和SIPM2) + * 获取物料PLM文件 * * @author mzr * @date 2025/8/4 @@ -47,7 +47,7 @@ public class MaterialPlmDownloadAction implements ICommonAction { private static final String tokenUrl = "/sipmweb/api/oauth"; // 根据物料编码获取零部件ID private String materialIdUrl = "/sipmweb/api/{rid}/search/{t}"; - // 根据零部件ID获取图档ID及信息 + // 根据零部件ID获取文档ID及信息 private String materialFileIdUrl = "/sipmweb/api/{rid}/relation/{t}/{id}/data"; // 下载文件 private String downlownUrl = "/sipmweb/web/download"; @@ -102,6 +102,10 @@ public class MaterialPlmDownloadAction implements ICommonAction { /** * 处理多个文件并打包(同时包含SIPM1和SIPM2) + * + * @param materialCodeArr 物料编码数组 + * @return 打包后的zip文件WebFile对象 + * @throws Exception 异常 */ private WebFile processMultipleFiles(String[] materialCodeArr) throws Exception { // 在内存中的 ZIP 输出流 @@ -127,23 +131,31 @@ public class MaterialPlmDownloadAction implements ICommonAction { zipStream.close(); } + // 如果没有任何文件成功处理 if (successCount == 0) { throw new BusinessException("没有有效的文件可以打包"); } SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss"); String zipName = "图纸文档打包_" + sdf.format(new Date()); + // 创建 WebFile 对象用于 ZIP 文件 InputStream ins = new ByteArrayInputStream(zipOut.toByteArray()); return new WebFile(zipName + ".zip", ins); } /** - * 将单个物料的单个类型文件(SIPM1/SIPM2)添加到ZIP流中 + * 将单个物料文件添加到ZIP流中 + * + * @param materialCode 物料编码 + * @param index 索引 + * @param zipStream ZIP输出流 + * @return boolean 是否成功处理 + * @throws Exception 异常 */ private boolean processSingleMaterialToZip(String materialCode, int index, String fileType, ZipOutputStream zipStream) throws Exception { String materialId = getMaterialId(materialCode); if (StringUtils.isEmpty(materialId)) { - logger.warn("物料编码 " + materialCode + " 未查询到PLM物料ID"); + logger.error("物料编码 " + materialCode + " 未查询到PLM物料ID"); return false; } @@ -153,14 +165,14 @@ public class MaterialPlmDownloadAction implements ICommonAction { String list = fileJson.getString("list"); JSONArray jsonArray = JSONObject.parseArray(list); if (jsonArray == null || jsonArray.isEmpty()) { - logger.warn("物料编码 " + materialCode + " 未查询到" + fileType + "类型文件"); + logger.error("物料编码 " + materialCode + " 未查询到" + fileType + "类型文件"); return false; } JSONObject plmFileJson = jsonArray.getJSONObject(0); String objId = plmFileJson.getString("objId"); if (objId == null || objId.isEmpty()) { - logger.warn("物料编码 " + materialCode + " 的" + fileType + "文件ID为空"); + logger.error("物料编码 " + materialCode + " 的" + fileType + "文件ID为空"); return false; } @@ -168,12 +180,12 @@ public class MaterialPlmDownloadAction implements ICommonAction { String suffix = plmFileJson.getString("suffix"); byte[] fileBytes = downloadPlmFileByType(objId, fileType); if (fileBytes.length == 0) { - logger.warn("物料编码 " + materialCode + " 的" + fileType + "文件字节流为空"); + logger.error("物料编码 " + materialCode + " 的" + fileType + "文件字节流为空"); return false; } // 生成带类型标识的文件名,避免重复 - String fname = name + "_" + materialCode + "_" + fileType + "_" + index + "." + suffix; + String fname = name + "_" + materialCode + "_" + getTypeName(fileType) + "_" + index + "." + suffix; zipStream.putNextEntry(new ZipEntry(fname)); zipStream.write(fileBytes); zipStream.closeEntry(); @@ -202,11 +214,12 @@ public class MaterialPlmDownloadAction implements ICommonAction { private String getMaterialId(String materialCode) throws IOException, BusinessException { String fileUrl = plmBaseUrl + materialIdUrl; fileUrl = fileUrl.replace("{rid}", token); + // 对象表名 MPART(零部件) fileUrl = fileUrl.replace("{t}", "MPART"); Map map = new HashMap<>(); - map.put("key", materialCode); - map.put("start", "0"); - map.put("size", "10"); + 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); @@ -216,7 +229,11 @@ public class MaterialPlmDownloadAction implements ICommonAction { return ""; } JSONObject listJson = jsonArray.getJSONObject(0); - return listJson.getString("objId"); + String objId = listJson.getString("objId"); + if (objId == null || objId.isEmpty()) { + return ""; + } + return objId; } /** @@ -228,24 +245,24 @@ public class MaterialPlmDownloadAction implements ICommonAction { fileUrl = fileUrl.replace("{t}", "MPART"); fileUrl = fileUrl.replace("{id}", materialId); Map map = new HashMap<>(); - map.put("re", "MPART_" + fileType); // 动态生成关系ID - map.put("start", "0"); - map.put("item", fileType); // 动态传入文件类型 + map.put("re", "MPART_" + fileType); // 关系ID + map.put("start", "0");// 偏移量 + map.put("item", fileType); // 关联对象表名 String result = doGet(fileUrl, map); logger.error("GetPlmFileUtil-getFileId-" + fileType + "-result = " + result); return result; } /** - * 通用方法:根据类型下载文件(合并原doDownloadPlmFile和doDownloadPlmFile2) + * 调用PLM的下载文件接口 */ private byte[] downloadPlmFileByType(String fileId, String fileType) throws IOException { String fileUrl = plmBaseUrl + downlownUrl; Map map = new HashMap<>(); map.put("rid", token); - map.put("id", fileId); - map.put("t", fileType); // 动态传入文件类型 - map.put("type", "D"); + map.put("id", fileId);// 对象id + map.put("t", fileType);// 对象表名 + map.put("type", "D");// 文件类别(BD,D)(D是文件本身的文件,BD是PDF图) return getFileFromPlm(fileUrl, map); } @@ -272,9 +289,9 @@ public class MaterialPlmDownloadAction implements ICommonAction { cm.setMaxTotal(5000); cm.setDefaultMaxPerRoute(500); - RequestConfig globalConfig = RequestConfig.custom().setConnectionRequestTimeout(50000) - .setConnectTimeout(50000) - .setSocketTimeout(200000) + RequestConfig globalConfig = RequestConfig.custom().setConnectionRequestTimeout(50000) // 连接池获取连接超时 + .setConnectTimeout(50000) // 连接建立超时 + .setSocketTimeout(200000) // 等待响应超时 .setCookieSpec(CookieSpecs.IGNORE_COOKIES).build(); CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm) @@ -286,14 +303,20 @@ public class MaterialPlmDownloadAction implements ICommonAction { return responseString; } + /** + * 调用第三方文件接口并接收文件流 + * + * @param requestUrl 文件接口URL + * @return 文件流 + */ private byte[] getFileFromPlm(String requestUrl, Map paramMap) throws IOException { PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(); cm.setMaxTotal(5000); cm.setDefaultMaxPerRoute(500); - RequestConfig globalConfig = RequestConfig.custom().setConnectionRequestTimeout(50000) - .setConnectTimeout(50000) - .setSocketTimeout(200000) + RequestConfig globalConfig = RequestConfig.custom().setConnectionRequestTimeout(50000) // 连接池获取连接超时 + .setConnectTimeout(50000) // 连接建立超时 + .setSocketTimeout(200000) // 等待响应超时 .setCookieSpec(CookieSpecs.IGNORE_COOKIES).build(); CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm) @@ -305,6 +328,13 @@ public class MaterialPlmDownloadAction implements ICommonAction { return responseString; } + /** + * 构建带参数的URL + * + * @param baseUrl 基础URL + * @param paramMap 参数Map + * @return 完整URL + */ private String buildUrlWithParams(String baseUrl, Map paramMap) { StringBuilder param = new StringBuilder("?"); if (paramMap != null && !paramMap.isEmpty()) { @@ -318,4 +348,14 @@ public class MaterialPlmDownloadAction implements ICommonAction { } return baseUrl + param; } + + private String getTypeName(String fileType) { + // SIPM2是文件,SIPM1是图纸 + if ("SIPM1".equals(fileType)) { + return "图纸"; + } else if ("SIPM2".equals(fileType)) { + return "文件"; + } + return fileType; + } }