多物料下载改为前端批量调用
This commit is contained in:
		
							parent
							
								
									ed57650cb2
								
							
						
					
					
						commit
						e39327ab16
					
				|  | @ -6,7 +6,7 @@ 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.util.mmf.framework.base.MMValueCheck; | ||||
| import nc.vo.bd.defdoc.DefdocVO; | ||||
| import nc.vo.cmp.util.StringUtils; | ||||
| import nc.vo.ml.NCLangRes4VoTransl; | ||||
|  | @ -27,8 +27,8 @@ import org.apache.http.util.EntityUtils; | |||
| import org.owasp.esapi.ESAPI; | ||||
| 
 | ||||
| import java.io.*; | ||||
| import java.text.SimpleDateFormat; | ||||
| import java.util.*; | ||||
| import java.util.HashMap; | ||||
| import java.util.Map; | ||||
| import java.util.zip.ZipEntry; | ||||
| import java.util.zip.ZipOutputStream; | ||||
| 
 | ||||
|  | @ -53,18 +53,21 @@ public class MaterialPlmDownloadAction implements ICommonAction { | |||
|     private String materialFileIdUrl = "/sipmweb/api/{rid}/relation/{t}/{id}/data"; | ||||
|     // 下载文件 | ||||
|     private String downlownUrl = "/sipmweb/web/download"; | ||||
|     private Map<String, String> materialMap = new HashMap<>(); | ||||
| 
 | ||||
|     @Override | ||||
|     public Object doAction(IRequest request) { | ||||
|         WebFile files = null; | ||||
|         try { | ||||
|             Map<String, String[]> params_1 = request.readParameters(); | ||||
|             String[] materialCodeArr = params_1.get("materialCode"); // 获取所有物料编码 | ||||
|             if (materialCodeArr == null || materialCodeArr.length == 0) { | ||||
|                 ExceptionUtils.wrapBusinessException("物料编码不能为空"); | ||||
|             Map<String, String[]> parameters = request.readParameters(); | ||||
|             String[] materialCodes = parameters.get("materialCode"); | ||||
|             String[] materialName = parameters.get("materialName"); | ||||
|             if (MMValueCheck.isEmpty(materialCodes) || MMValueCheck.isEmpty(materialName)) { | ||||
|                 ExceptionUtils.wrapBusinessException("物料编码或名称不能为空"); | ||||
|             } | ||||
|             files = this.getPlmFiles(materialCodeArr); | ||||
|             Map<String, String> maps = new HashMap<>(); | ||||
|             maps.put("materialCode", materialCodes[0]); | ||||
|             maps.put("materialName", materialName[0]); | ||||
|             files = this.getPlmFiles(maps); | ||||
|         } catch (Exception ex) { | ||||
|             logger.error("MaterialPlmDownloadAction-exp:" + ex.getMessage(), ex); | ||||
|             // 将异常转换为WebFile的形式返回,以便前端展示报错 | ||||
|  | @ -86,10 +89,7 @@ public class MaterialPlmDownloadAction implements ICommonAction { | |||
|         return files; | ||||
|     } | ||||
| 
 | ||||
|     public WebFile getPlmFiles(String[] materialCodeArr) throws Exception { | ||||
|         if (materialCodeArr == null || materialCodeArr.length == 0) { | ||||
|             return null; | ||||
|         } | ||||
|     public WebFile getPlmFiles(Map<String, String> maps) throws Exception { | ||||
|         // 获取PLM的参数 | ||||
|         Map<String, String> configParams = getConfigParams("Dldz-config"); | ||||
|         if (configParams == null || configParams.isEmpty()) { | ||||
|  | @ -99,129 +99,58 @@ public class MaterialPlmDownloadAction implements ICommonAction { | |||
|         plmUser = configParams.get("plmUser"); | ||||
|         passwd = configParams.getOrDefault("passwd", ""); | ||||
|         token = getToken(); | ||||
|         // 去重物料编码 | ||||
|         Set<String> materialCodeSet = new HashSet<>(Arrays.asList(materialCodeArr)); | ||||
|         getMaterialMapInfo(materialCodeSet); | ||||
|         // 无论单个还是多个物料,均打包为ZIP | ||||
|         return processMultipleFiles(materialCodeSet); | ||||
|         return processMultipleFiles(maps); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * 获取物料的名称 | ||||
|      * 处理多个文件并打包(同时包含SIPM1和SIPM2) | ||||
|      * | ||||
|      * @param materialCodeSet | ||||
|      * @return | ||||
|      */ | ||||
|     private void getMaterialMapInfo(Set<String> 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对象 | ||||
|      * @return 打包后的zip文件WebFile对象 | ||||
|      * @throws Exception 异常 | ||||
|      */ | ||||
|     private WebFile processMultipleFiles(Set<String> materialCodeSet) throws Exception { | ||||
|         // 创建总的ZIP输出流 | ||||
|         ByteArrayOutputStream mainZipOut = new ByteArrayOutputStream(); | ||||
|         ZipOutputStream mainZipStream = new ZipOutputStream(mainZipOut); | ||||
| 
 | ||||
|         int successCount = 0; // 成功处理的物料数 | ||||
| 
 | ||||
|     private WebFile processMultipleFiles(Map<String, String> maps) throws Exception { | ||||
|         String materialCode = maps.get("materialCode"); | ||||
|         String materialName = maps.get("materialName"); | ||||
|         // 在内存中的 ZIP 输出流 | ||||
|         ByteArrayOutputStream zipOut = new ByteArrayOutputStream(); | ||||
|         ZipOutputStream zipStream = new ZipOutputStream(zipOut); | ||||
|         int successCount = 0; // 成功处理的文件数 | ||||
|         try { | ||||
|             for (String materialCode : materialCodeSet) { | ||||
|                 try { | ||||
|                     // 为每个物料创建独立的压缩包 | ||||
|                     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); | ||||
|                     // 继续处理下一个物料 | ||||
|                 } | ||||
|             } | ||||
| 
 | ||||
|             mainZipStream.finish(); | ||||
|             // 同时处理当前物料的SIPM1和SIPM2 | ||||
|             boolean success1 = processSingleMaterialToZip(materialCode, materialName, "SIPM1", zipStream); | ||||
|             boolean success2 = processSingleMaterialToZip(materialCode, materialName, "SIPM2", zipStream); | ||||
|             if (success1) successCount++; | ||||
|             if (success2) successCount++; | ||||
|             zipStream.finish(); | ||||
|         } catch (Exception ex) { | ||||
|             logger.error("MaterialPlmDownloadAction-exp:" + ex.getMessage(), ex); | ||||
|         } finally { | ||||
|             mainZipStream.close(); | ||||
|             zipStream.close(); | ||||
|         } | ||||
| 
 | ||||
|         // 如果没有任何物料成功处理 | ||||
|         // 如果没有任何文件成功处理 | ||||
|         if (successCount == 0) { | ||||
|             throw new BusinessException("没有有效的文件可下载"); | ||||
|             throw new BusinessException("物料编码:" + materialCode + ",物料名称:" + materialName + ",没有有效的文件可以打包"); | ||||
|         } | ||||
| 
 | ||||
|         SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss"); | ||||
|         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); | ||||
|         // SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss"); | ||||
|         // String zipName = "图纸文档打包_" + sdf.format(new Date()); | ||||
|         // 创建 WebFile 对象用于 ZIP 文件 | ||||
|         InputStream ins = new ByteArrayInputStream(zipOut.toByteArray()); | ||||
|         return new WebFile(materialCode + "_" + materialName + ".zip", ins); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * 将单个物料文件添加到ZIP流中 | ||||
|      * | ||||
|      * @param materialCode 物料编码 | ||||
|      * @param index        索引 | ||||
|      * @param materialName 物料名称 | ||||
|      * @param zipStream    ZIP输出流 | ||||
|      * @return boolean 是否成功处理 | ||||
|      * @throws Exception 异常 | ||||
|      */ | ||||
|     private boolean processSingleMaterialToZip(String materialCode, int index, String fileType, ZipOutputStream zipStream) throws Exception { | ||||
|     private boolean processSingleMaterialToZip(String materialCode, String materialName, String fileType, ZipOutputStream zipStream) throws Exception { | ||||
|         String materialId = getMaterialId(materialCode); | ||||
|         if (StringUtils.isEmpty(materialId)) { | ||||
|             logger.error("物料编码 " + materialCode + " 未查询到PLM物料ID"); | ||||
|  | @ -254,7 +183,7 @@ public class MaterialPlmDownloadAction implements ICommonAction { | |||
|             } | ||||
| 
 | ||||
|             // 生成带类型标识的文件名,避免重复 | ||||
|             String fname = materialCode + "_" + materialMap.get(materialCode) + "_" + name + "_" + (index + i) + "." + suffix; | ||||
|             String fname = materialCode + "_" + materialName + "_" + name + "_" + i + "." + suffix; | ||||
|             zipStream.putNextEntry(new ZipEntry(fname)); | ||||
|             zipStream.write(fileBytes); | ||||
|             zipStream.closeEntry(); | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue