import com.matrixone.apps.domain.util.FrameworkException; import com.matrixone.apps.domain.util.MqlUtil; import matrix.db.Context; import org.json.JSONArray; import org.json.JSONObject; import java.util.*; import java.util.logging.Logger; public class TMLMaintainMaterialPeptideAttr_mxJPO { private static final Logger LOGGER = Logger.getLogger(TMLMaintainMaterialPeptideAttr_mxJPO.class.getName()); public String updatePartAttr(Context context, String[] args) throws FrameworkException { String updateInfo = args[0]; LOGGER.info("updatePeptideAttr: " + updateInfo); MqlUtil.mqlCommand(context, updateInfo); return "success"; } public String updatePeptideAttr(Context context, String[] args) throws FrameworkException { String updateInfo = args[0]; LOGGER.info("updatePeptideAttr: " + updateInfo); MqlUtil.mqlCommand(context, updateInfo); return "success"; } public String getPeptideAttrMap(Context context, String[] args) throws FrameworkException { JSONArray peptideAttrArray = new JSONArray(); if(args != null && args.length > 0) { for (String itemValue : args) { if (itemValue == null || itemValue.trim().isEmpty()) { continue; } JSONObject peptideAttrObj = new JSONObject(); try { // 1. 获取item ID String getItemInfo = "temp query bus VPMReference * * where \"attribute[PLMEntity.V_description].value == '" + escapeMqlString(itemValue.trim()) + "'\" select id dump |"; String itemInfo = MqlUtil.mqlCommand(context, getItemInfo); if (itemInfo == null || itemInfo.trim().isEmpty()) { LOGGER.warning("未找到项目: " + itemValue); peptideAttrObj.put("itemValue", itemValue); peptideAttrObj.put("error", "未找到项目"); peptideAttrArray.put(peptideAttrObj); continue; } String[] itemInfoParts = itemInfo.split("\\|"); if (itemInfoParts.length <= 3) { LOGGER.warning("项目信息格式不正确: " + itemInfo); peptideAttrObj.put("itemValue", itemValue); peptideAttrObj.put("error", "项目信息格式不正确"); peptideAttrArray.put(peptideAttrObj); continue; } String itemId = itemInfoParts[3].trim(); // 2. 获取肽属性信息 String getMaterialPeptideAttrInfo = "print bus " + itemId + " select attribute[PLMEntity.V_Name].value " + "attribute[TMLVPMReference.TMLSpecification].value attribute[TMLVPMReference.TMLBrand].value " + "to[Classified Item].attribute to[Classified Item].attribute.value dump |"; String materialPeptideAttrInfo = MqlUtil.mqlCommand(context, getMaterialPeptideAttrInfo); // 添加基本信息 peptideAttrObj.put("itemValue", itemValue); peptideAttrObj.put("itemId", itemId); // 安全检查:确保返回结果不为空 if (materialPeptideAttrInfo != null && !materialPeptideAttrInfo.trim().isEmpty()) { try { // 使用 -1 保留所有空字符串元素 String[] materialPeptideAttrSplit = materialPeptideAttrInfo.split("\\|", -1); // 1. 先提取基本信息(前3个元素) if (materialPeptideAttrSplit.length >= 1) { peptideAttrObj.put("materialName", materialPeptideAttrSplit[0].trim()); } if (materialPeptideAttrSplit.length >= 2) { peptideAttrObj.put("brand", materialPeptideAttrSplit[1].trim()); } if (materialPeptideAttrSplit.length >= 3) { peptideAttrObj.put("specification", materialPeptideAttrSplit[2].trim()); } List> peptideProperties = new ArrayList<>(); // 2. 处理剩余的属性键值对(从第4个元素开始) // 确保有足够的元素形成键值对 int startIndex = 3; // 从第4个元素开始(索引3) if (materialPeptideAttrSplit.length > startIndex) { // 计算可用于形成键值对的元素数量 int availableElements = materialPeptideAttrSplit.length - startIndex; // 如果剩余元素是奇数,添加一个空字符串使其成为偶数 if (availableElements % 2 != 0) { materialPeptideAttrSplit = Arrays.copyOf( materialPeptideAttrSplit, materialPeptideAttrSplit.length + 1 ); materialPeptideAttrSplit[materialPeptideAttrSplit.length - 1] = ""; availableElements += 1; } // 处理键值对 int totalPairs = availableElements / 2; for (int j = 0; j < totalPairs; j++) { int keyIndex = startIndex + j; int valueIndex = startIndex + totalPairs + j; String key = materialPeptideAttrSplit[keyIndex].trim(); String value = materialPeptideAttrSplit[valueIndex].trim(); // 只有当键不为空时才添加到对象 if (!key.isEmpty()) { Map property = new HashMap<>(); property.put("name", key); property.put("value", value); peptideProperties.add(property); } } } // 3. 将多肽属性列表添加到结果对象 peptideAttrObj.put("peptideProperties", peptideProperties); } catch (Exception e) { // 异常处理 System.err.println("Error processing material peptide attributes for itemId: " + itemId); e.printStackTrace(); // 返回默认结构 peptideAttrObj.put("PLMEntity.V_Name", ""); peptideAttrObj.put("PLMEntity.V_Description", ""); peptideAttrObj.put("PLMEntity.V_Classification", ""); peptideAttrObj.put("TMLVPMReference.TMLBrand", ""); peptideAttrObj.put("peptideProperties", new ArrayList<>()); } } else { // MQL命令返回为空的情况 peptideAttrObj.put("PLMEntity.V_Name", ""); peptideAttrObj.put("PLMEntity.V_Description", ""); peptideAttrObj.put("PLMEntity.V_Classification", ""); peptideAttrObj.put("TMLVPMReference.TMLBrand", ""); peptideAttrObj.put("peptideProperties", new ArrayList<>()); } } catch (Exception e) { LOGGER.warning("处理项目 " + itemValue + " 时出错: " + e.getMessage()); try { peptideAttrObj.put("itemValue", itemValue); peptideAttrObj.put("error", e.getMessage()); } catch (Exception ex) { // 忽略 JSON 异常 } } peptideAttrArray.put(peptideAttrObj); } } // 直接返回 JSONArray 的字符串表示 System.out.println(peptideAttrArray); return peptideAttrArray.toString(); } public String getPeptideAttrMapWithWrapper(Context context, String[] args) throws FrameworkException { JSONObject result = new JSONObject(); try { JSONArray data = new JSONArray(getPeptideAttrMap(context, args)); result.put("success", true); result.put("code", 200); result.put("message", "查询成功"); result.put("data", data); result.put("total", data.length()); result.put("timestamp", System.currentTimeMillis()); } catch (Exception e) { result.put("success", false); result.put("code", 500); result.put("message", "查询失败: " + e.getMessage()); result.put("data", new JSONArray()); result.put("timestamp", System.currentTimeMillis()); } return result.toString(); } /** * 另一种实现:直接返回标准JSON格式 */ public String getPeptideAttrMapStandard(Context context, String[] args) throws FrameworkException { JSONObject response = new JSONObject(); JSONArray dataArray = new JSONArray(); try { if(args != null && args.length > 0) { for (String itemValue : args) { JSONObject itemData = processSingleItem(context, itemValue); dataArray.put(itemData); } } response.put("success", true); response.put("code", 200); response.put("message", "success"); response.put("data", dataArray); response.put("total", dataArray.length()); response.put("timestamp", System.currentTimeMillis()); } catch (Exception e) { response.put("success", false); response.put("code", 500); response.put("message", "Error: " + e.getMessage()); response.put("data", new JSONArray()); response.put("timestamp", System.currentTimeMillis()); } return response.toString(); } /** * 处理单个项目 */ private JSONObject processSingleItem(Context context, String itemValue) throws Exception { JSONObject itemObj = new JSONObject(); // 获取item ID String getItemInfo = "temp query bus VPMReference * * where \"attribute[PLMEntity.V_description].value == '" + escapeMqlString(itemValue.trim()) + "'\" select id dump |"; String itemInfo = MqlUtil.mqlCommand(context, getItemInfo); if (itemInfo == null || itemInfo.trim().isEmpty()) { itemObj.put("error", "Item not found"); return itemObj; } String[] itemInfoParts = itemInfo.split("\\|"); if (itemInfoParts.length <= 3) { itemObj.put("error", "Invalid item info format"); return itemObj; } String itemId = itemInfoParts[3].trim(); itemObj.put("itemId", itemId); itemObj.put("itemValue", itemValue); // 获取肽属性信息 String getMaterialPeptideAttrInfo = "print bus " + itemId + " select to[Classified Item].attribute to[Classified Item].attribute.value dump |"; String materialPeptideAttrInfo = MqlUtil.mqlCommand(context, getMaterialPeptideAttrInfo); if (materialPeptideAttrInfo != null && !materialPeptideAttrInfo.trim().isEmpty()) { JSONObject attributes = parseAttributes(materialPeptideAttrInfo); itemObj.put("attributes", attributes); } else { itemObj.put("attributes", new JSONObject()); } return itemObj; } /** * 解析属性字符串为 JSONObject */ private JSONObject parseAttributes(String attrInfo) throws Exception { JSONObject attributes = new JSONObject(); String[] parts = attrInfo.split("\\|", -1); if (parts.length % 2 != 0) { parts = Arrays.copyOf(parts, parts.length + 1); parts[parts.length - 1] = ""; } int totalPairs = parts.length / 2; for (int i = 0; i < totalPairs; i++) { String key = parts[i].trim(); String value = parts[totalPairs + i].trim(); if (!key.isEmpty()) { attributes.put(key, value); } } return attributes; } private String escapeMqlString(String input) { if (input == null) return ""; return input.replace("'", "''"); } }