From 20520fe986cbbf94eee1cbed69a41d3dee4c2db5 Mon Sep 17 00:00:00 2001 From: mzr Date: Mon, 13 Oct 2025 11:45:19 +0800 Subject: [PATCH] =?UTF-8?q?feat(material):=E4=BC=98=E5=8C=96=E7=89=A9?= =?UTF-8?q?=E6=96=99PLM=E6=96=87=E4=BB=B6=E4=B8=8B=E8=BD=BD=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 增加物料编码去重逻辑,避免重复下载- 为每个物料生成独立压缩包,再统一打包成ZIP文件 - 新增物料名称查询功能,用于构建更清晰的文件名 - 优化文件命名规则,包含物料编码和名称信息 - 增强PLM接口鉴权错误处理机制 --- .../action/MaterialPlmDownloadAction.java | 141 +++++++++++++----- 1 file changed, 104 insertions(+), 37 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 b6b0ba09..591b62b9 100644 --- a/uapbd/src/client/nccloud/web/uapbd/material/action/MaterialPlmDownloadAction.java +++ b/uapbd/src/client/nccloud/web/uapbd/material/action/MaterialPlmDownloadAction.java @@ -2,9 +2,11 @@ package nccloud.web.uapbd.material.action; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; +import nc.bs.dao.BaseDAO; import nc.bs.dao.DAOException; import nc.bs.logging.Log; import nc.bs.trade.business.HYSuperDMO; +import nc.jdbc.framework.processor.ColumnProcessor; import nc.vo.bd.defdoc.DefdocVO; import nc.vo.cmp.util.StringUtils; import nc.vo.ml.NCLangRes4VoTransl; @@ -26,9 +28,7 @@ import org.owasp.esapi.ESAPI; import java.io.*; import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.HashMap; -import java.util.Map; +import java.util.*; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; @@ -39,6 +39,7 @@ import java.util.zip.ZipOutputStream; * @date 2025/8/4 */ public class MaterialPlmDownloadAction implements ICommonAction { + private static final BaseDAO dao = new BaseDAO(); private static final String LOG_INFO_NAME = "dldzlog"; private static final Log logger = Log.getInstance(LOG_INFO_NAME); private String plmBaseUrl = ""; @@ -52,6 +53,7 @@ public class MaterialPlmDownloadAction implements ICommonAction { private String materialFileIdUrl = "/sipmweb/api/{rid}/relation/{t}/{id}/data"; // ļ private String downlownUrl = "/sipmweb/web/download"; + private Map materialMap = new HashMap<>(); @Override public Object doAction(IRequest request) { @@ -97,54 +99,119 @@ public class MaterialPlmDownloadAction implements ICommonAction { plmUser = configParams.get("plmUser"); passwd = configParams.getOrDefault("passwd", ""); token = getToken(); - - // ۵ǶϣΪZIPԭϷصļ޷ͬʱļ - return processMultipleFiles(materialCodeArr); + // ȥϱ + Set materialCodeSet = new HashSet<>(Arrays.asList(materialCodeArr)); + getMaterialMapInfo(materialCodeSet); + // ۵ǶϣΪZIP + return processMultipleFiles(materialCodeSet); } /** - * ļͬʱSIPM1SIPM2 + * ȡϵ * - * @param materialCodeArr ϱ - * @return zipļWebFile + * @param materialCodeSet + * @return + */ + private void getMaterialMapInfo(Set materialCodeSet) throws DAOException { + for (String code : materialCodeSet) { + String sql = "select name from bd_material where code = '" + code + "'"; + String materialName = (String) dao.executeQuery(sql, new ColumnProcessor("name")); + materialMap.put(code, materialName); + } + } + + /** + * ļΪÿϴѹȻѹһѹ + * + * @param materialCodeSet ϱ + * @return ѹWebFile * @throws Exception 쳣 */ - private WebFile processMultipleFiles(String[] materialCodeArr) throws Exception { - // ڴе ZIP - ByteArrayOutputStream zipOut = new ByteArrayOutputStream(); - ZipOutputStream zipStream = new ZipOutputStream(zipOut); - int successCount = 0; // ɹļ + private WebFile processMultipleFiles(Set materialCodeSet) throws Exception { + // ܵZIP + ByteArrayOutputStream mainZipOut = new ByteArrayOutputStream(); + ZipOutputStream mainZipStream = new ZipOutputStream(mainZipOut); + + int successCount = 0; // ɹ + try { - for (int i = 0; i < materialCodeArr.length; i++) { - String materialCode = materialCodeArr[i]; + for (String materialCode : materialCodeSet) { try { - // ͬʱǰϵSIPM1SIPM2 - boolean success1 = processSingleMaterialToZip(materialCode, i, "SIPM1", zipStream); - boolean success2 = processSingleMaterialToZip(materialCode, i, "SIPM2", zipStream); - if (success1) successCount++; - if (success2) successCount++; + // Ϊÿϴѹ + WebFile singleMaterialZip = createSingleMaterialZip(materialCode); + if (singleMaterialZip != null) { + // ϵѹӵѹ + mainZipStream.putNextEntry(new ZipEntry(singleMaterialZip.getFileName())); + byte[] buffer = new byte[1024]; + int bytesRead; + InputStream inputStream = singleMaterialZip.getInputStream(); + while ((bytesRead = inputStream.read(buffer)) != -1) { + mainZipStream.write(buffer, 0, bytesRead); + } + inputStream.close(); + mainZipStream.closeEntry(); + successCount++; + } } catch (BusinessException e) { logger.error("ϱ " + materialCode + " ʱ쳣: " + e.getMessage(), e); - // һ + // һ } } - zipStream.finish(); + + mainZipStream.finish(); } finally { - zipStream.close(); + mainZipStream.close(); } - // ûκļɹ + // ûκϳɹ if (successCount == 0) { - throw new BusinessException("ûЧļԴ"); + throw new BusinessException("ûЧļ"); } SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss"); - String zipName = "ͼֽĵ_" + sdf.format(new Date()); - // WebFile ZIP ļ - InputStream ins = new ByteArrayInputStream(zipOut.toByteArray()); + String zipName = "ĵ_" + sdf.format(new Date()); + + // صWebFile + InputStream ins = new ByteArrayInputStream(mainZipOut.toByteArray()); return new WebFile(zipName + ".zip", ins); } + /** + * Ϊϴѹ + * + * @param materialCode ϱ + * @return ϵѹWebFile + * @throws Exception 쳣 + */ + private WebFile createSingleMaterialZip(String materialCode) throws Exception { + // ϵZIP + ByteArrayOutputStream singleZipOut = new ByteArrayOutputStream(); + ZipOutputStream singleZipStream = new ZipOutputStream(singleZipOut); + + boolean hasFiles = false; + try { + // SIPM1͵ļͼֽ + boolean success1 = processSingleMaterialToZip(materialCode, 0, "SIPM1", singleZipStream); + // SIPM2͵ļļ + boolean success2 = processSingleMaterialToZip(materialCode, 0, "SIPM2", singleZipStream); + + hasFiles = success1 || success2; + + singleZipStream.finish(); + } finally { + singleZipStream.close(); + } + // ûκļ򷵻null + if (!hasFiles) { + return null; + } + // ϵѹļ + String fileName = materialCode + "_" + materialMap.get(materialCode) + ".zip"; + // WebFile + InputStream ins = new ByteArrayInputStream(singleZipOut.toByteArray()); + return new WebFile(fileName, ins); + } + /** * ļӵZIP * @@ -187,7 +254,7 @@ public class MaterialPlmDownloadAction implements ICommonAction { } // ɴͱʶļظ - String fname = name + "_" + materialCode + "_" + getTypeName(fileType) + "_" + (index + i) + "." + suffix; + String fname = materialCode + "_" + materialMap.get(materialCode) + "_" + name + "_" + (index + i) + "." + suffix; zipStream.putNextEntry(new ZipEntry(fname)); zipStream.write(fileBytes); zipStream.closeEntry(); @@ -201,13 +268,15 @@ public class MaterialPlmDownloadAction implements ICommonAction { private String getToken() throws IOException, BusinessException { Map tokenMap = new HashMap<>(); tokenMap.put("uname", plmUser); - if (!"passwd".equals(passwd) && !"null".equals(passwd) && !"~".equals(passwd)) { - tokenMap.put("passwd", passwd); - } + tokenMap.put("passwd", passwd); tokenMap.put("f", "true"); String tokenStr = doGet(plmBaseUrl + tokenUrl, tokenMap); JSONObject jsonObject = JSONObject.parseObject(tokenStr); String token = jsonObject.getString("errmsg"); + if (!"0".equals(jsonObject.get("errcode") + "")) { + // ׳Ϣ + throw new BusinessException(token); + } if (token == null || token.isEmpty()) { throw new BusinessException("PLMȨʧ"); } @@ -300,8 +369,7 @@ public class MaterialPlmDownloadAction implements ICommonAction { .setSocketTimeout(200000) // ȴӦʱ .setCookieSpec(CookieSpecs.IGNORE_COOKIES).build(); - CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm) - .setDefaultRequestConfig(globalConfig).build(); + CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm).setDefaultRequestConfig(globalConfig).build(); String url = buildUrlWithParams(requestUrl, paramMap); HttpGet get = new HttpGet(url); String responseString = httpClient.execute(get, response -> EntityUtils.toString(response.getEntity())); @@ -325,8 +393,7 @@ public class MaterialPlmDownloadAction implements ICommonAction { .setSocketTimeout(200000) // ȴӦʱ .setCookieSpec(CookieSpecs.IGNORE_COOKIES).build(); - CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm) - .setDefaultRequestConfig(globalConfig).build(); + CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm).setDefaultRequestConfig(globalConfig).build(); String url = buildUrlWithParams(requestUrl, paramMap); HttpGet get = new HttpGet(url); byte[] responseString = httpClient.execute(get, response -> EntityUtils.toByteArray(response.getEntity()));