diff --git a/uapbd/src/public/nc/bs/uapbd/util/GetPlmFileUtil.java b/uapbd/src/public/nc/bs/uapbd/util/GetPlmFileUtil.java new file mode 100644 index 0000000..bc6787b --- /dev/null +++ b/uapbd/src/public/nc/bs/uapbd/util/GetPlmFileUtil.java @@ -0,0 +1,192 @@ +package nc.bs.uapbd.util; + +import com.alibaba.fastjson.JSONObject; +import nc.bs.dao.DAOException; +import nc.bs.logging.Log; +import nc.bs.logging.Logger; +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.framework.core.io.WebFile; +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.IOException; +import java.util.HashMap; +import java.util.Map; + +/** + * 获取PLM图纸的工具类 + * + * @author mzr + * @date 2025/8/1 + */ +public class GetPlmFileUtil { + 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 static final String tokenUrl = "/sipmweb/api/oauth"; + // 根据物料编码获取零部件ID + private String materialIdUrl = "/sipmweb/api/{rid}/search/{t}?key={key}&start={start}&size={size}"; + // 根据零部件ID获取二维图档ID及信息 + private String materialFileIdUrl = "/sipmweb/api/{rid}/relation/{t}/{id}/data?re={re}&start={start}&item={item}"; + // 下载文件 + private String downlownUrl = "/sipmweb/web/download?rid={rid}&id={id}&t={t}&type={type}"; + + public WebFile getPlmFile(String materialCode) { + try { + // 获取PLM的参数 + Map configParams = getConfigParams("Dldz-config"); + if (configParams == null || configParams.isEmpty()) { + throw new BusinessException("未配置PLM参数"); + } + plmBaseUrl = configParams.get("plmBaseUrl"); + plmUser = configParams.get("plmUser"); + // 获取token + String token = getToken(); + String materialId = getMaterialId(token, materialCode); + String fileId = getFileId(token, materialId); + downloadFile(token, fileId); + } catch (BusinessException | IOException e) { + logger.error("GetPlmFileUtil-getFile-exp:" + e.getMessage()); + throw new RuntimeException(e); + } + WebFile file = null; + return file; + } + + + /** + * 获取token + */ + private String getToken() throws IOException, BusinessException { + Map 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 token, 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");// 数量 + String result = doGet(fileUrl, map); + logger.error("GetPlmFileUtil-getMaterialId-result = " + result); + JSONObject jsonObject = JSONObject.parseObject(result); + String objId = jsonObject.getString("objId"); + if (objId == null || objId.isEmpty()) { + throw new BusinessException("获取PLM物料id失败"); + } + return objId; + } + + /** + * 根据零部件ID获取文件ID + */ + private String getFileId(String token, 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 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 objId = jsonObject.getString("objId"); + if (objId == null || objId.isEmpty()) { + throw new BusinessException("获取PLM物料的文件id失败"); + } + return objId; + } + + /** + * 调用PLM的下载文件接口 + */ + private String downloadFile(String token, String fileId) throws IOException, BusinessException { + String fileUrl = plmBaseUrl + downlownUrl; + Map 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图) + String result = doGet(fileUrl, map); + logger.error("GetPlmFileUtil-getFileId-result = " + result); + + return result; + } + + public Map getConfigParams(String code) { + Map 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("getConfigParams-exp:" + e.getMessage()); + } + return map; + } + + + private String doGet(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) // 等待响应超时 + .setCookieSpec(CookieSpecs.IGNORE_COOKIES).build(); + + CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm) + .setDefaultRequestConfig(globalConfig).build(); + StringBuilder param = new StringBuilder("?"); + if (paramMap != null) { + for (Map.Entry 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; + } +}