获取物料PLM文件调整

This commit is contained in:
mzr 2025-08-05 14:37:04 +08:00
parent 6d36b643f0
commit 51b9d9367e
1 changed files with 33 additions and 20 deletions

View File

@ -1,5 +1,6 @@
package nc.bs.uapbd.util; package nc.bs.uapbd.util;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import nc.bs.dao.DAOException; import nc.bs.dao.DAOException;
import nc.bs.logging.Log; import nc.bs.logging.Log;
@ -41,11 +42,11 @@ public class GetPlmFileUtil {
private String token = ""; private String token = "";
private static final String tokenUrl = "/sipmweb/api/oauth"; private static final String tokenUrl = "/sipmweb/api/oauth";
// 根据物料编码获取零部件ID // 根据物料编码获取零部件ID
private String materialIdUrl = "/sipmweb/api/{rid}/search/{t}?key={key}&start={start}&size={size}"; private String materialIdUrl = "/sipmweb/api/{rid}/search/{t}";
// 根据零部件ID获取二维图档ID及信息 // 根据零部件ID获取二维图档ID及信息
private String materialFileIdUrl = "/sipmweb/api/{rid}/relation/{t}/{id}/data?re={re}&start={start}&item={item}"; private String materialFileIdUrl = "/sipmweb/api/{rid}/relation/{t}/{id}/data";
// 下载文件 // 下载文件
private String downlownUrl = "/sipmweb/web/download?rid={rid}&id={id}&t={t}&type={type}"; private String downlownUrl = "/sipmweb/web/download";
// http请求 // http请求
private static final PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(); private static final PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
private static final CloseableHttpClient httpClient; private static final CloseableHttpClient httpClient;
@ -93,29 +94,29 @@ public class GetPlmFileUtil {
ByteArrayOutputStream zipOut = new ByteArrayOutputStream(); ByteArrayOutputStream zipOut = new ByteArrayOutputStream();
ZipOutputStream zipStream = new ZipOutputStream(zipOut); ZipOutputStream zipStream = new ZipOutputStream(zipOut);
try { try {
for (String materialCode : materialCodeArr) { for (String materialCode : materialCodeArr) {
JSONObject plmFileJson = this.getPlmFile(materialCode); JSONObject plmFileJson = this.getPlmFile(materialCode);
String objId = plmFileJson.getString("objId"); String objId = plmFileJson.getString("objId");
String fname = plmFileJson.getString("fname"); String fname = plmFileJson.getString("fname");
InputStream ins = this.doDownloadPlmFile(objId); InputStream ins = this.doDownloadPlmFile(objId);
try { try {
byte[] bytes = parseFileStream(ins); byte[] bytes = parseFileStream(ins);
zipStream.putNextEntry(new ZipEntry(fname)); zipStream.putNextEntry(new ZipEntry(fname));
zipStream.write(bytes); zipStream.write(bytes);
zipStream.closeEntry(); zipStream.closeEntry();
} finally { } finally {
if (ins != null) { if (ins != null) {
try { try {
ins.close(); ins.close();
} catch (IOException e) { } catch (IOException e) {
logger.error("Failed to close input stream: " + e.getMessage()); logger.error("Failed to close input stream: " + e.getMessage());
} }
} }
} }
} }
zipStream.finish(); zipStream.finish();
} finally { } finally {
zipStream.close(); zipStream.close();
} }
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss"); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
String zipName = "物料图纸下载_" + sdf.format(new Date()); String zipName = "物料图纸下载_" + sdf.format(new Date());
@ -165,7 +166,13 @@ public class GetPlmFileUtil {
String result = doGet(fileUrl, map); String result = doGet(fileUrl, map);
logger.error("GetPlmFileUtil-getMaterialId-result = " + result); logger.error("GetPlmFileUtil-getMaterialId-result = " + result);
JSONObject jsonObject = JSONObject.parseObject(result); JSONObject jsonObject = JSONObject.parseObject(result);
String objId = jsonObject.getString("objId"); 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()) { if (objId == null || objId.isEmpty()) {
throw new BusinessException("获取PLM物料id失败"); throw new BusinessException("获取PLM物料id失败");
} }
@ -189,11 +196,17 @@ public class GetPlmFileUtil {
String result = doGet(fileUrl, map); String result = doGet(fileUrl, map);
logger.error("GetPlmFileUtil-getFileId-result = " + result); logger.error("GetPlmFileUtil-getFileId-result = " + result);
JSONObject jsonObject = JSONObject.parseObject(result); JSONObject jsonObject = JSONObject.parseObject(result);
String objId = jsonObject.getString("objId"); 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()) { if (objId == null || objId.isEmpty()) {
throw new BusinessException("获取PLM物料的文件信息失败"); throw new BusinessException("获取PLM物料的文件信息失败");
} }
return jsonObject; return listJson;
} }
/** /**