You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

276 lines
11 KiB
Java

import com.matrixone.apps.domain.util.FrameworkException;
import com.matrixone.apps.domain.util.MqlUtil;
import matrix.db.Context;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class AegBomConn_mxJPO {
private static final String PIPE_DELIMITER = "\\|";
private static final String NEWLINE_DELIMITER = "\n";
public void bomConn(Context context, String[] args) throws IOException, FrameworkException {
FileInputStream fileInputStream = new FileInputStream("/jpo/6000.xlsx");
Workbook workbook = new XSSFWorkbook(fileInputStream);
Sheet sheet = workbook.getSheetAt(0);
String firstPartNumber = "";
String firstObjId = "";
for (Row row : sheet){
String firstColumnValue = getCellValueAsString(row.getCell(0));
if(!firstPartNumber.equals(firstColumnValue)) {
String getObjId = "temp query bus VPMReference * * where 'attribute[EnterpriseExtension.V_PartNumber].value == " + firstColumnValue + "' select id dump |";
String objInfo = MqlUtil.mqlCommand(context, getObjId);
if(objInfo.isEmpty()) {
System.out.println("==>" + firstColumnValue);
continue;
}
firstObjId = getObjId(objInfo);
firstPartNumber = firstColumnValue;
}
// 获取第五列单元格
String fifthColumnValue = getCellValueAsString(row.getCell(3));
String getObjId = "temp query bus VPMReference * * where 'attribute[EnterpriseExtension.V_PartNumber].value == " + fifthColumnValue + "' select id dump |";
String childObjInfo = MqlUtil.mqlCommand(context, getObjId);
if(childObjInfo.isEmpty()){
System.out.println("==" + fifthColumnValue);
continue;
}
String childObjId = getObjId(childObjInfo);
String connBo = "add connection VPMInstance from " + firstObjId + " to " + childObjId;
MqlUtil.mqlCommand(context, connBo);
}
}
public void addConnInfo(Context context, String[] args){
Map<String, List<String>> map = new HashMap<>();
List<String> list = new ArrayList<>();
list.add("81000005");
list.add("9970001");
list.add("9970002");
list.add("81000023");
list.add("10041423");
list.add("81100005");
map.put("81100121C",list);
System.out.println();
map.forEach((key, values) -> {
String getParentObjId = "temp query bus VPMReference * * where 'attribute[EnterpriseExtension.V_PartNumber].value == " + key + "' select id dump |";
try {
String fatherObjIdInfo = MqlUtil.mqlCommand(context, getParentObjId);
values.forEach(value -> {
String getChildObjId = "temp query bus VPMReference * * where 'attribute[EnterpriseExtension.V_PartNumber].value == " + value + "' select id dump |";
String childObjIdInfo = null;
try {
childObjIdInfo = MqlUtil.mqlCommand(context, getChildObjId);
if(!fatherObjIdInfo.isEmpty() && !childObjIdInfo.isEmpty()){
String fatherItemId = getObjId(fatherObjIdInfo);
System.out.println(fatherItemId);
String childItemId = getObjId(childObjIdInfo);
System.out.println("==>" + childItemId);
String connBo = "add connection VPMInstance from " + fatherItemId + " to " + childItemId;
MqlUtil.mqlCommand(context, connBo);
}
} catch (FrameworkException e) {
throw new RuntimeException(e);
}
});
} catch (FrameworkException e) {
throw new RuntimeException(e);
}
});
}
public void getFatherObjId(Context context, String[] args) throws Exception {
FileInputStream fileInputStream = new FileInputStream("/jpo/6000.xlsx");
Workbook workbook = new XSSFWorkbook(fileInputStream);
Sheet sheet = workbook.getSheetAt(0);
String firstPartNumber = "";
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("/jpo/1.txt"));
for (Row row : sheet){
String firstColumnValue = getCellValueAsString(row.getCell(0));
if(!firstPartNumber.equals(firstColumnValue)) {
// String getObjId = "temp query bus VPMReference * * where 'attribute[EnterpriseExtension.V_PartNumber].value == " + firstColumnValue + "' select id dump |";
// String objInfo = MqlUtil.mqlCommand(context, getObjId);
// System.out.println(objInfo);
//
// if(objInfo.isEmpty()) {
// System.out.println("==>" + firstColumnValue);
// bufferedWriter.write(firstColumnValue);
// bufferedWriter.newLine();
// continue;
// }
System.out.println(firstColumnValue);
firstPartNumber = firstColumnValue;
}
}
bufferedWriter.flush();
}
public void getAllChildItemIds(Context context, String[] args) throws FrameworkException {
String itemId = args[0];
List<String> allChildIds = new ArrayList<>();
getAllChildIdsRecursive(context, itemId, allChildIds);
// System.out.println(111);
// for (String value : allChildIds){
// System.out.println(value);
// }
}
public void addInterfaceAttr(Context context, String[] args) throws Exception {
FileInputStream fileInputStream = new FileInputStream("/jpo/6000.xlsx");
Workbook workbook = new XSSFWorkbook(fileInputStream);
Sheet sheet = workbook.getSheetAt(0);
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("/jpo/1.txt"));
String fatherValue = "";
for (Row row : sheet){
String firstColumnValue = getCellValueAsString(row.getCell(0));
if(!fatherValue.equals(firstColumnValue)){
fatherValue = firstColumnValue;
String getItemId = "temp query bus VPMReference * * where 'attribute[EnterpriseExtension.V_PartNumber].value == " + firstColumnValue + "' select id dump |";
String itemId = MqlUtil.mqlCommand(context, getItemId);
if(!itemId.isEmpty()){
List<String> allChildIds = new ArrayList<>();
getAllChildIdsRecursive(context, getObjId(itemId), allChildIds);
}
}
}
}
public void getSixInfo(Context context, String[] args) throws Exception {
FileInputStream fileInputStream = new FileInputStream("/jpo/6000.xlsx");
Workbook workbook = new XSSFWorkbook(fileInputStream);
Sheet sheet = workbook.getSheetAt(0);
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("/jpo/1.txt"));
for (Row row : sheet){
String firstColumnValue = getCellValueAsString(row.getCell(0));
bufferedWriter.write(firstColumnValue);
bufferedWriter.newLine();
}
bufferedWriter.flush();
}
private void getAllChildIdsRecursive(Context context, String currentItemId, List<String> accumulator) throws FrameworkException {
List<String> directChildIds = getDirectChildItemIds(context, currentItemId);
if (directChildIds != null) {
for (String childId : directChildIds) {
// 避免循环引用导致的无限递归
if (!accumulator.contains(childId)) {
accumulator.add(childId);
// 递归获取子级的子级
getAllChildIdsRecursive(context, childId, accumulator);
}
}
}
}
private List<String> getDirectChildItemIds(Context context, String itemId) throws FrameworkException {
List<String> list = new ArrayList<>();
String getChildIds = "query connection relationship VPMInstance from " + itemId + " select id to.id dump |";
String childIds = MqlUtil.mqlCommand(context, getChildIds);
if (!childIds.isEmpty()) {
String[] lines = childIds.split(NEWLINE_DELIMITER);
for (String line : lines) {
String connId = line.split("\\|")[1];
// String getConnInterface = "print connection " + connId + " select interface dump |";
// String connInterface = MqlUtil.mqlCommand(context, getConnInterface);
// if (connInterface == null || connInterface.isEmpty()){
//// String addInterfaceAttribute = "modify connection " + connId + " add interface AEGVPMInstance";
//// MqlUtil.mqlCommand(context, addInterfaceAttribute);
////
//// String modifyAttributeInfo = "modify connection " + connId + " AEGVPMInstance.AEGunit_consumption 1.0 AEGVPMInstance.AEGdepletion 0.0 AEGVPMInstance.AEGproject_type L";
//// MqlUtil.mqlCommand(context, modifyAttributeInfo);
// System.out.println(line.split("\\|")[2]);
// }
String modifyAttributeInfo = "modify connection " + connId + "AEGVPMInstance.AEGdepletion 1.0";
MqlUtil.mqlCommand(context, modifyAttributeInfo);
String childId = line.split("\\|")[2];
list.add(childId);
}
return list;
}
return null;
}
private static String getObjId(String value){
String[] lines = value.split(NEWLINE_DELIMITER);
String targetLine = lines.length > 1 ? lines[lines.length - 1] : lines[0];
String objId = targetLine.split(PIPE_DELIMITER)[3];
return objId;
}
private static String getCellValueAsString(Cell cell) {
if (cell == null) {
return "";
}
CellType cellType = cell.getCellType();
switch (cellType) {
case STRING:
return cell.getStringCellValue();
case NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
return cell.getDateCellValue().toString();
} else {
return String.valueOf(cell.getNumericCellValue());
}
case BOOLEAN:
return String.valueOf(cell.getBooleanCellValue());
case FORMULA:
return cell.getCellFormula();
default:
return "";
}
}
}