修改工单订单设备

parent bc3adbc4
...@@ -8,6 +8,7 @@ import com.aps.entity.common.Paged; ...@@ -8,6 +8,7 @@ import com.aps.entity.common.Paged;
import com.aps.service.common.ChromosomeDataService; import com.aps.service.common.ChromosomeDataService;
import com.aps.service.plan.PlanResultService; import com.aps.service.plan.PlanResultService;
import com.aps.service.plan.SceneService; import com.aps.service.plan.SceneService;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.Parameter;
...@@ -15,7 +16,8 @@ import io.swagger.v3.oas.annotations.tags.Tag; ...@@ -15,7 +16,8 @@ import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.ArrayList; import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
...@@ -163,40 +165,27 @@ public class ChromosomeDataController { ...@@ -163,40 +165,27 @@ public class ChromosomeDataController {
// 文件实体必须要有sceneId // 文件实体必须要有sceneId
if (isFileEntity(entityName) && (sceneId == null || sceneId.isEmpty())) { if (isFileEntity(entityName) && (sceneId == null || sceneId.isEmpty())) {
return R.failed("文件实体更新时sceneId不能为空"); throw new RuntimeException("文件实体更新时sceneId不能为空");
} }
if (entityName.equalsIgnoreCase("machineOption")){ if (entityName.equalsIgnoreCase("machine")){
return updateMachineOption(sceneId, taskId, data); return updateMachineOption(sceneId, taskId, data);
} }
try {
boolean success = chromosomeDataService.updateChromosomeData(sceneId, entityName, data); boolean success = chromosomeDataService.updateChromosomeData(sceneId, entityName, data);
if (success) { return R.ok("更新成功");
return R.ok("更新成功");
} else {
return R.failed("更新失败");
}
} catch (Exception e) {
return R.failed("更新失败: " + e.getMessage());
}
} }
private R<String> updateMachineOption(String sceneId, String taskId, Map<String, Object> data) { private R<String> updateMachineOption(String sceneId, String taskId, Map<String, Object> data) {
// 验证taskId不能为空
if (taskId == null || taskId.isEmpty()) {
return R.failed("更新machineOption时taskId不能为空");
}
try {
// 1. 加载Chromosome对象 // 1. 加载Chromosome对象
Chromosome chromosome = sceneService.loadChromosomeFromFile(sceneId); Chromosome chromosome = sceneService.loadChromosomeFromFile(sceneId);
if (chromosome == null) {
return R.failed("未找到场景ID为 " + sceneId + " 的Chromosome数据");
}
// 2. 根据taskId找到对应的entry // 2. 根据taskId找到对应的entry
Entry targetEntry = null; Entry targetEntry = null;
...@@ -207,150 +196,233 @@ public class ChromosomeDataController { ...@@ -207,150 +196,233 @@ public class ChromosomeDataController {
} }
} }
if (targetEntry == null) { Long machineId = null;
return R.failed("未找到taskId为 " + taskId + " 的entry数据"); Object machineIdObj = data.get("machineId");
if (machineIdObj instanceof Long) {
machineId = (Long) machineIdObj;
} else if (machineIdObj instanceof Integer) {
machineId = ((Integer) machineIdObj).longValue();
} else if (machineIdObj instanceof String) {
machineId = Long.parseLong((String) machineIdObj);
} }
// 3. 获取machineOption的id
String machineOptionId = String.valueOf(data.get("machineId"));
// 4. 在entry的machineOptions列表中找到对应的machineOption // 4. 在entry的machineOptions列表中找到对应的machineOption
MachineOption targetMachineOption = null; MachineOption targetMachineOption = null;
for (MachineOption machineOption : targetEntry.getMachineOptions()) { for (MachineOption machineOption : targetEntry.getMachineOptions()) {
if (String.valueOf(machineOption.getMachineId()).equals(machineOptionId)) { if (machineOption.getMachineId().equals(machineId)) {
targetMachineOption = machineOption; targetMachineOption = machineOption;
break; break;
} }
} }
if (targetMachineOption == null) {
return R.failed("未找到id为 " + machineOptionId + " 的machineOption数据");
}
// 5. 更新machineOption的字段
ObjectMapper objectMapper = new ObjectMapper();
MachineOption updatedMachineOption = objectMapper.convertValue(data, MachineOption.class);
// 6. 替换原来的machineOption // 5. 只更新传入的字段,保留原有字段值
int index = targetEntry.getMachineOptions().indexOf(targetMachineOption); updateMachineOptionFields(targetMachineOption, data);
targetEntry.getMachineOptions().set(index, updatedMachineOption);
planResultService.editMachine(chromosome, sceneId,targetEntry); // 6. 调用editMachine方法
planResultService.editMachine(chromosome, sceneId, targetEntry);
// 7. 保存更新后的Chromosome // 7. 保存更新后的Chromosome
boolean saved = sceneService.saveChromosomeToFile(chromosome, sceneId); boolean saved = sceneService.saveChromosomeToFile(chromosome, sceneId);
if (saved) {
return R.ok("更新成功"); return R.ok("更新成功");
} else {
return R.failed("更新失败");
}
} catch (Exception e) {
return R.failed("更新machineOption失败: " + e.getMessage());
}
} }
/** /**
* 同一entry下的多个machineOption更新 * 只更新MachineOption的传入字段,保留原有字段值
*/ */
private R<String> updateMultipleMachineOptionsInEntry(String sceneId, String taskId, Map<String, Object> data) { private void updateMachineOptionFields(MachineOption machineOption, Map<String, Object> data) {
try { // 处理runtime字段
// 1. 加载Chromosome对象 if (data.containsKey("runtime")) {
Chromosome chromosome = sceneService.loadChromosomeFromFile(sceneId); Object runtimeObj = data.get("runtime");
if (chromosome == null) { if (runtimeObj instanceof Integer) {
return R.failed("未找到场景ID为 " + sceneId + " 的Chromosome数据"); machineOption.setRuntime(new BigDecimal((Integer) runtimeObj));
} else if (runtimeObj instanceof Long) {
machineOption.setRuntime(new BigDecimal((Long) runtimeObj));
} else if (runtimeObj instanceof Double) {
machineOption.setRuntime(new BigDecimal((Double) runtimeObj));
} else if (runtimeObj instanceof String) {
machineOption.setRuntime(new BigDecimal((String) runtimeObj));
} }
}
// 2. 根据taskId找到对应的entry // 处理singleOut字段
Entry targetEntry = null; if (data.containsKey("singleOut")) {
for (Entry entry : chromosome.getAllOperations()) { Object singleOutObj = data.get("singleOut");
if (String.valueOf(entry.getId()).equals(taskId)) { if (singleOutObj instanceof Integer) {
targetEntry = entry; machineOption.setSingleOut(new BigDecimal((Integer) singleOutObj));
break; } else if (singleOutObj instanceof Long) {
} machineOption.setSingleOut(new BigDecimal((Long) singleOutObj));
} else if (singleOutObj instanceof Double) {
machineOption.setSingleOut(new BigDecimal((Double) singleOutObj));
} else if (singleOutObj instanceof String) {
machineOption.setSingleOut(new BigDecimal((String) singleOutObj));
} }
}
if (targetEntry == null) { // 处理preTime字段
return R.failed("未找到taskId为 " + taskId + " 的entry数据"); if (data.containsKey("preTime")) {
Object preTimeObj = data.get("preTime");
if (preTimeObj instanceof Integer) {
machineOption.setPreTime((Integer) preTimeObj);
} else if (preTimeObj instanceof Long) {
machineOption.setPreTime(((Long) preTimeObj).intValue());
} else if (preTimeObj instanceof String) {
machineOption.setPreTime(Integer.parseInt((String) preTimeObj));
} }
}
// 3. 获取machineOptions列表 // 处理processingTime字段 - 修改为:时间(runtime) ÷ 数量(singleOut)
Object machineOptionsObj = data.get("machineOptions"); if (data.containsKey("processingTime") || data.containsKey("singleOut") || data.containsKey("runtime")) {
if (!(machineOptionsObj instanceof List)) { // 1. 获取singleOut值
return R.failed("machineOptions必须是数组格式"); BigDecimal singleOut = machineOption.getSingleOut();
if (data.containsKey("singleOut")) {
Object singleOutObj = data.get("singleOut");
if (singleOutObj instanceof Integer) {
singleOut = new BigDecimal((Integer) singleOutObj);
} else if (singleOutObj instanceof Long) {
singleOut = new BigDecimal((Long) singleOutObj);
} else if (singleOutObj instanceof Double) {
singleOut = new BigDecimal((Double) singleOutObj);
} else if (singleOutObj instanceof String) {
singleOut = new BigDecimal((String) singleOutObj);
} else if (singleOutObj instanceof BigDecimal) {
singleOut = (BigDecimal) singleOutObj;
}
} }
List<?> machineOptionsList = (List<?>) machineOptionsObj; // 2. 获取runtime值
if (machineOptionsList.isEmpty()) { BigDecimal runtime = machineOption.getRuntime();
return R.failed("machineOptions数组不能为空"); if (data.containsKey("runtime")) {
Object runtimeObj = data.get("runtime");
if (runtimeObj instanceof Integer) {
runtime = new BigDecimal((Integer) runtimeObj);
} else if (runtimeObj instanceof Long) {
runtime = new BigDecimal((Long) runtimeObj);
} else if (runtimeObj instanceof Double) {
runtime = new BigDecimal((Double) runtimeObj);
} else if (runtimeObj instanceof String) {
runtime = new BigDecimal((String) runtimeObj);
} else if (runtimeObj instanceof BigDecimal) {
runtime = (BigDecimal) runtimeObj;
}
} }
// 4. 更新当前entry下的所有machineOption // 3. 计算processingTime = runtime ÷ singleOut(时间 ÷ 数量)
ObjectMapper objectMapper = new ObjectMapper(); double processingTime = 0.0;
boolean allSuccess = true; if (singleOut != null && runtime != null && runtime.compareTo(BigDecimal.ZERO) != 0) {
List<String> errorMessages = new ArrayList<>(); // 考虑runtime单位转换:如果runtime是毫秒,转换为秒
BigDecimal runtimeSeconds = runtime.divide(new BigDecimal(1000), 6, RoundingMode.HALF_UP);
for (Object optionObj : machineOptionsList) { // 关键修改:时间(秒) ÷ 数量,保留2位小数
if (!(optionObj instanceof Map)) { processingTime = runtimeSeconds.divide(singleOut, 2, RoundingMode.HALF_UP).doubleValue();
allSuccess = false; }
errorMessages.add("machineOptions中的元素必须是对象格式");
continue;
}
Map<String, Object> optionMap = (Map<String, Object>) optionObj; // 4. 设置计算后的processingTime
machineOption.setProcessingTime(processingTime);
}
// 获取machineOption的machineId
String machineOptionId = String.valueOf(optionMap.get("machineId"));
if (machineOptionId == null) {
allSuccess = false;
errorMessages.add("machineOption必须包含machineId字段");
continue;
}
// 在entry的machineOptions列表中找到对应的machineOption //
MachineOption targetMachineOption = null; // // 处理setupTime字段
for (MachineOption machineOption : targetEntry.getMachineOptions()) { // if (data.containsKey("setupTime")) {
if (String.valueOf(machineOption.getMachineId()).equals(machineOptionId)) { // Object setupTimeObj = data.get("setupTime");
targetMachineOption = machineOption; // if (setupTimeObj instanceof Integer) {
break; // machineOption.setSetupTime((Integer) setupTimeObj);
} // } else if (setupTimeObj instanceof Long) {
} // machineOption.setSetupTime(((Long) setupTimeObj).intValue());
// } else if (setupTimeObj instanceof String) {
// machineOption.setSetupTime(Integer.parseInt((String) setupTimeObj));
// }
// }
//
// // 处理teardownTime字段
// if (data.containsKey("teardownTime")) {
// Object teardownTimeObj = data.get("teardownTime");
// if (teardownTimeObj instanceof Integer) {
// machineOption.setTeardownTime((Integer) teardownTimeObj);
// } else if (teardownTimeObj instanceof Long) {
// machineOption.setTeardownTime(((Long) teardownTimeObj).intValue());
// } else if (teardownTimeObj instanceof String) {
// machineOption.setTeardownTime(Integer.parseInt((String) teardownTimeObj));
// }
// }
//
// // 处理contantTime字段
// if (data.containsKey("contantTime")) {
// Object contantTimeObj = data.get("contantTime");
// if (contantTimeObj instanceof Integer) {
// machineOption.setContantTime((Integer) contantTimeObj);
// } else if (contantTimeObj instanceof Long) {
// machineOption.setContantTime(((Long) contantTimeObj).intValue());
// } else if (contantTimeObj instanceof String) {
// machineOption.setContantTime(Integer.parseInt((String) contantTimeObj));
// }
// }
// // 处理equipCode字段
// if (data.containsKey("equipCode")) {
// machineOption.setEquipCode(String.valueOf(data.get("equipCode")));
// }
//
// // 处理resourceCode字段
// if (data.containsKey("resourceCode")) {
// machineOption.setResourceCode(String.valueOf(data.get("resourceCode")));
// }
// 忽略id等未知字段
}
if (targetMachineOption == null) {
allSuccess = false;
errorMessages.add("未找到machineId为 " + machineOptionId + " 的machineOption数据");
continue;
}
// 更新machineOption的字段 private boolean updateMachineOptionsForEntry(Entry targetEntry, List<?> machineOptions, ObjectMapper objectMapper) {
MachineOption updatedMachineOption = objectMapper.convertValue(optionMap, MachineOption.class); boolean allSuccess = true;
// 替换原来的machineOption for (Object optionObj : machineOptions) {
int index = targetEntry.getMachineOptions().indexOf(targetMachineOption); if (!(optionObj instanceof Map)) {
targetEntry.getMachineOptions().set(index, updatedMachineOption); allSuccess = false;
continue;
} }
if (!allSuccess) { Map<?, ?> optionMap = (Map<?, ?>) optionObj;
return R.failed("部分更新失败: " + String.join("; ", errorMessages));
// 获取machineId
Object machineIdObj = optionMap.get("machineId");
if (machineIdObj == null) {
allSuccess = false;
continue;
} }
// 5. 调用editMachine方法处理entry更新 String machineOptionId = String.valueOf(machineIdObj);
planResultService.editMachine(chromosome, sceneId, targetEntry);
// 6. 保存更新后的Chromosome // 在entry的machineOptions列表中找到对应的machineOption
boolean saved = sceneService.saveChromosomeToFile(chromosome, sceneId); MachineOption targetMachineOption = null;
if (saved) { for (MachineOption machineOption : targetEntry.getMachineOptions()) {
return R.ok("批量更新成功"); if (String.valueOf(machineOption.getMachineId()).equals(machineOptionId)) {
} else { targetMachineOption = machineOption;
return R.failed("批量更新失败: 保存Chromosome数据失败"); break;
}
}
if (targetMachineOption == null) {
allSuccess = false;
continue;
} }
} catch (Exception e) { // 更新machineOption的字段
return R.failed("批量更新machineOption失败: " + e.getMessage()); MachineOption updatedMachineOption = objectMapper.convertValue(optionMap, MachineOption.class);
// 替换原来的machineOption
int index = targetEntry.getMachineOptions().indexOf(targetMachineOption);
targetEntry.getMachineOptions().set(index, updatedMachineOption);
} }
return allSuccess;
} }
/** /**
* 判断是否为文件实体 * 判断是否为文件实体
*/ */
......
...@@ -129,7 +129,7 @@ public class KpiCalculator { ...@@ -129,7 +129,7 @@ public class KpiCalculator {
.mapToLong(GAScheduleResult::getChangeOverTime) .mapToLong(GAScheduleResult::getChangeOverTime)
.filter(changeOverTime -> changeOverTime > 0) .filter(changeOverTime -> changeOverTime > 0)
.count(); .count();
addKpi(kpiMetrics,"换型次数", changeOverTimeCount, "总的换型次数", 2, 1); addKpi(kpiMetrics,"换型次数", changeOverTimeCount, "总的换型次数", 1, 1);
} }
......
package com.aps.service.common; package com.aps.service.common;
import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.bean.BeanUtil;
import com.aps.common.util.R;
import com.aps.entity.Algorithm.Chromosome; import com.aps.entity.Algorithm.Chromosome;
import com.aps.entity.ProdProcessExec; import com.aps.entity.ProdProcessExec;
import com.aps.entity.basic.Entry; import com.aps.entity.basic.Entry;
...@@ -20,6 +21,8 @@ import org.springframework.stereotype.Service; ...@@ -20,6 +21,8 @@ import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
...@@ -47,18 +50,18 @@ public class ChromosomeDataService { ...@@ -47,18 +50,18 @@ public class ChromosomeDataService {
ObjectMapper objectMapper = new ObjectMapper() ObjectMapper objectMapper = new ObjectMapper()
.registerModule(new JavaTimeModule()) .registerModule(new JavaTimeModule())
.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
public Object queryChromosomeData(String sceneId, String entityName) { // public Object queryChromosomeData(String sceneId, String entityName) {
EntityConfig config = getEntityConfig(entityName); // EntityConfig config = getEntityConfig(entityName);
//
if (config.getDataSource() == DataSourceType.FILE) { // if (config.getDataSource() == DataSourceType.FILE) {
return queryFileData(sceneId, config); // return queryFileData(sceneId, config);
} else { // } else {
// 数据库实体不需要sceneId,创建空的Paged对象 // // 数据库实体不需要sceneId,创建空的Paged对象
Paged paged = new Paged(); // Paged paged = new Paged();
List<Object> result = databaseQueryService.queryDatabaseDataList(config, paged); // List<Object> result = databaseQueryService.queryDatabaseDataList(config, paged);
return result; // return result;
} // }
} // }
/** /**
* 驼峰命名转下划线大写:prodProduct -> PROD_PRODUCT * 驼峰命名转下划线大写:prodProduct -> PROD_PRODUCT
*/ */
...@@ -306,38 +309,6 @@ public class ChromosomeDataService { ...@@ -306,38 +309,6 @@ public class ChromosomeDataService {
return camelCaseToUnderScoreUpperCase(processedName); return camelCaseToUnderScoreUpperCase(processedName);
} }
/**
* 驼峰命名转下划线大写:ProdProduct -> PROD_PRODUCT
*/
// private String camelCaseToUnderScoreUpperCase(String camelCase) {
// if (camelCase == null || camelCase.isEmpty()) {
// return camelCase;
// }
//
// StringBuilder result = new StringBuilder();
// for (int i = 0; i < camelCase.length(); i++) {
// char c = camelCase.charAt(i);
// if (Character.isUpperCase(c) && i > 0) {
// result.append('_');
// }
// result.append(Character.toUpperCase(c));
// }
// return result.toString();
// }
/** /**
* 查询文件数据 * 查询文件数据
*/ */
...@@ -350,11 +321,11 @@ public class ChromosomeDataService { ...@@ -350,11 +321,11 @@ public class ChromosomeDataService {
try { try {
String fieldName = config.getFieldName(); String fieldName = config.getFieldName();
// 特殊处理:当实体是ProdProcessExec时,使用allOperations字段 // // 特殊处理:当实体是ProdProcessExec时,使用allOperations字段
if ("prodprocessexec".equalsIgnoreCase(config.getEntityName())) { // if ("prodprocessexec".equalsIgnoreCase(config.getEntityName())) {
fieldName = "allOperations"; // fieldName = "allOperations";
} // }
//
// 特殊处理:当实体是MachineOption时,使用allOperations字段 // 特殊处理:当实体是MachineOption时,使用allOperations字段
if ("machineoption".equalsIgnoreCase(config.getEntityName())) { if ("machineoption".equalsIgnoreCase(config.getEntityName())) {
fieldName = "allOperations"; fieldName = "allOperations";
...@@ -364,10 +335,10 @@ public class ChromosomeDataService { ...@@ -364,10 +335,10 @@ public class ChromosomeDataService {
field.setAccessible(true); field.setAccessible(true);
Object result = field.get(chromosome); Object result = field.get(chromosome);
// 如果实体是ProdProcessExec,但数据源是Entry,则进行转换 // // 如果实体是ProdProcessExec,但数据源是Entry,则进行转换
if ("prodprocessexec".equalsIgnoreCase(config.getEntityName())) { // if ("prodprocessexec".equalsIgnoreCase(config.getEntityName())) {
return convertEntryToProdProcessExec(result); // return convertEntryToProdProcessExec(result);
} // }
// 如果实体是MachineOption,提取Entry中的MachineOption列表 // 如果实体是MachineOption,提取Entry中的MachineOption列表
if ("machineoption".equalsIgnoreCase(config.getEntityName())) { if ("machineoption".equalsIgnoreCase(config.getEntityName())) {
...@@ -555,27 +526,6 @@ public class ChromosomeDataService { ...@@ -555,27 +526,6 @@ public class ChromosomeDataService {
} }
return null; // 找不到数据时返回null而不是抛出异常 return null; // 找不到数据时返回null而不是抛出异常
} }
// // 特殊处理:当实体是Order时,使用orderId字段进行查询
// if ("order".equalsIgnoreCase(config.getEntityName())) {
// Object result = queryFileData(sceneId, config);
//
// if (result instanceof List) {
// List<?> orderList = (List<?>) result;
// // 查找orderId匹配的Order
// for (Object obj : orderList) {
// if (obj instanceof Order) {
// Order order = (Order) obj;
// // 检查Order的orderId是否匹配
// if (order.getOrderId() != null && order.getOrderId().equals(id)) {
// // 如果orderId匹配,返回该Order
// return order;
// }
// }
// }
// }
// return null; // 找不到数据时返回null而不是抛出异常
// }
Object result = queryFileData(sceneId, config); Object result = queryFileData(sceneId, config);
...@@ -888,30 +838,49 @@ public class ChromosomeDataService { ...@@ -888,30 +838,49 @@ public class ChromosomeDataService {
} }
if (entityName.equals("order")) { if (entityName.equals("order")) {
chromosome = planResultService.editOrder(chromosome, sceneId, BeanUtil.toBean(data, Order.class)); Order bean = BeanUtil.toBean(data, Order.class);
System.out.println("test1123"); Order order = chromosome.getOrders().stream().filter(o -> o.getId() == (bean.getId())).findFirst().orElse(null);
chromosome = planResultService.editOrder(chromosome, sceneId, order);
}else if (entityName.equals("entry")) { }else if (entityName.equals("entry")) {
System.out.println("5757"); // 获取原有entry对象
Entry entry = objectMapper.convertValue(data, Entry.class); List<Object> dataList = (List<Object>) originalData;
String id = data.get("id").toString();
// 2. 手动转换machineOptions字段 Entry originalEntry = null;
if (data.containsKey("machineOptions")) { for (Object item : dataList) {
List<?> machineOptionsList = (List<?>) data.get("machineOptions"); if (item instanceof Entry) {
List<MachineOption> convertedOptions = new ArrayList<>(); Entry entry = (Entry) item;
for (Object item : machineOptionsList) { if (String.valueOf(entry.getId()).equals(id)) {
// 手动转换每个MachineOption对象 originalEntry = entry;
MachineOption option = objectMapper.convertValue(item, MachineOption.class); break;
convertedOptions.add(option); }
} }
// 设置转换后的machineOptions
entry.setMachineOptions(convertedOptions);
} }
chromosome = planResultService.editOperation(chromosome, sceneId, entry ); // 如果找到了原有entry对象,直接使用它
}else if (entityName.equals("machineOption")) { if (originalEntry != null) {
// 如果传入了machineOptions字段,需要特殊处理
// chromosome =planResultService.editMachine(chromosome, sceneId, BeanUtil.toBean(data, MachineOption.class)); if (data.containsKey("machineOptions")) {
List<?> machineOptionsList = (List<?>) data.get("machineOptions");
List<MachineOption> convertedOptions = new ArrayList<>();
for (Object item : machineOptionsList) {
// 手动转换每个MachineOption对象
MachineOption option = objectMapper.convertValue(item, MachineOption.class);
convertedOptions.add(option);
}
// 设置转换后的machineOptions
originalEntry.setMachineOptions(convertedOptions);
// 从data中移除machineOptions,避免updateObjectFields再次处理
data.remove("machineOptions");
}
// 更新其他传入的字段
updateObjectFields(originalEntry, data);
// 调用editOperation方法
chromosome = planResultService.editOperation(chromosome, sceneId, originalEntry );
}
// 其他字段通过updateObjectFields更新
} }
...@@ -1150,37 +1119,52 @@ public class ChromosomeDataService { ...@@ -1150,37 +1119,52 @@ public class ChromosomeDataService {
Object fieldValue = entry.getValue(); Object fieldValue = entry.getValue();
try { try {
Field field = clazz.getDeclaredField(fieldName); // 尝试直接查找字段(区分大小写)
field.setAccessible(true); Field field = null;
try {
// 简单的类型转换处理 field = clazz.getDeclaredField(fieldName);
if (fieldValue != null) { } catch (NoSuchFieldException e) {
Class<?> fieldType = field.getType(); // 如果找不到,尝试忽略大小写查找
if (fieldType == String.class) { for (Field declaredField : clazz.getDeclaredFields()) {
field.set(obj, fieldValue.toString()); if (declaredField.getName().equalsIgnoreCase(fieldName)) {
} else if (fieldType == Integer.class || fieldType == int.class) { field = declaredField;
field.set(obj, Integer.parseInt(fieldValue.toString())); break;
} else if (fieldType == Long.class || fieldType == long.class) { }
field.set(obj, Long.parseLong(fieldValue.toString())); }
} else if (fieldType == Double.class || fieldType == double.class) { }
field.set(obj, Double.parseDouble(fieldValue.toString()));
} else if (fieldType == Boolean.class || fieldType == boolean.class) { if (field != null) {
field.set(obj, Boolean.parseBoolean(fieldValue.toString())); field.setAccessible(true);
} else if (fieldType == List.class) {
// 特殊处理List类型字段,特别是MachineOption列表 // 简单的类型转换处理
List<?> valueList = (List<?>) fieldValue; if (fieldValue != null) {
if (!valueList.isEmpty() && valueList.get(0) instanceof Map) { Class<?> fieldType = field.getType();
// 如果列表中的元素是Map(如JSON反序列化结果),需要特殊处理 if (fieldType == String.class) {
// 对于MachineOption这类复杂对象,我们暂时直接赋值 field.set(obj, fieldValue.toString());
field.set(obj, fieldValue); } else if (fieldType == Integer.class || fieldType == int.class) {
field.set(obj, Integer.parseInt(fieldValue.toString()));
} else if (fieldType == Long.class || fieldType == long.class) {
field.set(obj, Long.parseLong(fieldValue.toString()));
} else if (fieldType == Double.class || fieldType == double.class) {
field.set(obj, Double.parseDouble(fieldValue.toString()));
} else if (fieldType == Boolean.class || fieldType == boolean.class) {
field.set(obj, Boolean.parseBoolean(fieldValue.toString()));
} else if (fieldType == List.class) {
// 特殊处理List类型字段,特别是MachineOption列表
List<?> valueList = (List<?>) fieldValue;
if (!valueList.isEmpty() && valueList.get(0) instanceof Map) {
// 如果列表中的元素是Map(如JSON反序列化结果),需要特殊处理
// 对于MachineOption这类复杂对象,我们暂时直接赋值
field.set(obj, fieldValue);
} else {
field.set(obj, fieldValue);
}
} else { } else {
field.set(obj, fieldValue); field.set(obj, fieldValue);
} }
} else { } else {
field.set(obj, fieldValue); field.set(obj, null);
} }
} else {
field.set(obj, null);
} }
} catch (Exception e) { } catch (Exception e) {
// 忽略无法设置的字段 // 忽略无法设置的字段
...@@ -1300,4 +1284,202 @@ public class ChromosomeDataService { ...@@ -1300,4 +1284,202 @@ public class ChromosomeDataService {
} }
} }
public R<String> updateMachineOption(String sceneId, String taskId, Map<String, Object> data) {
// 1. 加载Chromosome对象
Chromosome chromosome = sceneService.loadChromosomeFromFile(sceneId);
// 2. 根据taskId找到对应的entry
Entry targetEntry = null;
for (Entry entry : chromosome.getAllOperations()) {
if (String.valueOf(entry.getId()).equals(taskId)) {
targetEntry = entry;
break;
}
}
Long machineId = null;
Object machineIdObj = data.get("machineId");
if (machineIdObj instanceof Long) {
machineId = (Long) machineIdObj;
} else if (machineIdObj instanceof Integer) {
machineId = ((Integer) machineIdObj).longValue();
} else if (machineIdObj instanceof String) {
machineId = Long.parseLong((String) machineIdObj);
}
// 4. 在entry的machineOptions列表中找到对应的machineOption
MachineOption targetMachineOption = null;
for (MachineOption machineOption : targetEntry.getMachineOptions()) {
if (machineOption.getMachineId().equals(machineId)) {
targetMachineOption = machineOption;
break;
}
}
// 5. 只更新传入的字段,保留原有字段值
updateMachineOptionFields(targetMachineOption, data);
// 6. 调用editMachine方法
planResultService.editMachine(chromosome, sceneId, targetEntry);
// 7. 保存更新后的Chromosome
boolean saved = sceneService.saveChromosomeToFile(chromosome, sceneId);
return R.ok("更新成功");
}
/**
* 只更新MachineOption的传入字段,保留原有字段值
*/
private void updateMachineOptionFields(MachineOption machineOption, Map<String, Object> data) {
// 处理runtime字段
if (data.containsKey("runtime")) {
Object runtimeObj = data.get("runtime");
if (runtimeObj instanceof Integer) {
machineOption.setRuntime(new BigDecimal((Integer) runtimeObj));
} else if (runtimeObj instanceof Long) {
machineOption.setRuntime(new BigDecimal((Long) runtimeObj));
} else if (runtimeObj instanceof Double) {
machineOption.setRuntime(new BigDecimal((Double) runtimeObj));
} else if (runtimeObj instanceof String) {
machineOption.setRuntime(new BigDecimal((String) runtimeObj));
}
}
// 处理singleOut字段
if (data.containsKey("singleOut")) {
Object singleOutObj = data.get("singleOut");
if (singleOutObj instanceof Integer) {
machineOption.setSingleOut(new BigDecimal((Integer) singleOutObj));
} else if (singleOutObj instanceof Long) {
machineOption.setSingleOut(new BigDecimal((Long) singleOutObj));
} else if (singleOutObj instanceof Double) {
machineOption.setSingleOut(new BigDecimal((Double) singleOutObj));
} else if (singleOutObj instanceof String) {
machineOption.setSingleOut(new BigDecimal((String) singleOutObj));
}
}
// 处理preTime字段
if (data.containsKey("preTime")) {
Object preTimeObj = data.get("preTime");
if (preTimeObj instanceof Integer) {
machineOption.setPreTime((Integer) preTimeObj);
} else if (preTimeObj instanceof Long) {
machineOption.setPreTime(((Long) preTimeObj).intValue());
} else if (preTimeObj instanceof String) {
machineOption.setPreTime(Integer.parseInt((String) preTimeObj));
}
}
// 处理processingTime字段 - 修改为:时间(runtime) ÷ 数量(singleOut)
if (data.containsKey("processingTime") || data.containsKey("singleOut") || data.containsKey("runtime")) {
// 1. 获取singleOut值
BigDecimal singleOut = machineOption.getSingleOut();
if (data.containsKey("singleOut")) {
Object singleOutObj = data.get("singleOut");
if (singleOutObj instanceof Integer) {
singleOut = new BigDecimal((Integer) singleOutObj);
} else if (singleOutObj instanceof Long) {
singleOut = new BigDecimal((Long) singleOutObj);
} else if (singleOutObj instanceof Double) {
singleOut = new BigDecimal((Double) singleOutObj);
} else if (singleOutObj instanceof String) {
singleOut = new BigDecimal((String) singleOutObj);
} else if (singleOutObj instanceof BigDecimal) {
singleOut = (BigDecimal) singleOutObj;
}
}
// 2. 获取runtime值
BigDecimal runtime = machineOption.getRuntime();
if (data.containsKey("runtime")) {
Object runtimeObj = data.get("runtime");
if (runtimeObj instanceof Integer) {
runtime = new BigDecimal((Integer) runtimeObj);
} else if (runtimeObj instanceof Long) {
runtime = new BigDecimal((Long) runtimeObj);
} else if (runtimeObj instanceof Double) {
runtime = new BigDecimal((Double) runtimeObj);
} else if (runtimeObj instanceof String) {
runtime = new BigDecimal((String) runtimeObj);
} else if (runtimeObj instanceof BigDecimal) {
runtime = (BigDecimal) runtimeObj;
}
}
// 3. 计算processingTime = runtime ÷ singleOut(时间 ÷ 数量)
double processingTime = 0.0;
if (singleOut != null && runtime != null && runtime.compareTo(BigDecimal.ZERO) != 0) {
// 考虑runtime单位转换:如果runtime是毫秒,转换为秒
BigDecimal runtimeSeconds = runtime.divide(new BigDecimal(1000), 6, RoundingMode.HALF_UP);
// 关键修改:时间(秒) ÷ 数量,保留2位小数
processingTime = runtimeSeconds.divide(singleOut, 2, RoundingMode.HALF_UP).doubleValue();
}
// 4. 设置计算后的processingTime
machineOption.setProcessingTime(processingTime);
}
//
// // 处理setupTime字段
// if (data.containsKey("setupTime")) {
// Object setupTimeObj = data.get("setupTime");
// if (setupTimeObj instanceof Integer) {
// machineOption.setSetupTime((Integer) setupTimeObj);
// } else if (setupTimeObj instanceof Long) {
// machineOption.setSetupTime(((Long) setupTimeObj).intValue());
// } else if (setupTimeObj instanceof String) {
// machineOption.setSetupTime(Integer.parseInt((String) setupTimeObj));
// }
// }
//
// // 处理teardownTime字段
// if (data.containsKey("teardownTime")) {
// Object teardownTimeObj = data.get("teardownTime");
// if (teardownTimeObj instanceof Integer) {
// machineOption.setTeardownTime((Integer) teardownTimeObj);
// } else if (teardownTimeObj instanceof Long) {
// machineOption.setTeardownTime(((Long) teardownTimeObj).intValue());
// } else if (teardownTimeObj instanceof String) {
// machineOption.setTeardownTime(Integer.parseInt((String) teardownTimeObj));
// }
// }
//
// // 处理contantTime字段
// if (data.containsKey("contantTime")) {
// Object contantTimeObj = data.get("contantTime");
// if (contantTimeObj instanceof Integer) {
// machineOption.setContantTime((Integer) contantTimeObj);
// } else if (contantTimeObj instanceof Long) {
// machineOption.setContantTime(((Long) contantTimeObj).intValue());
// } else if (contantTimeObj instanceof String) {
// machineOption.setContantTime(Integer.parseInt((String) contantTimeObj));
// }
// }
// // 处理equipCode字段
// if (data.containsKey("equipCode")) {
// machineOption.setEquipCode(String.valueOf(data.get("equipCode")));
// }
//
// // 处理resourceCode字段
// if (data.containsKey("resourceCode")) {
// machineOption.setResourceCode(String.valueOf(data.get("resourceCode")));
// }
// 忽略id等未知字段
}
} }
\ No newline at end of file
...@@ -444,7 +444,7 @@ order.setDueDate(LocalDateTime.of(2025, 12, 1,0,0,0)); ...@@ -444,7 +444,7 @@ order.setDueDate(LocalDateTime.of(2025, 12, 1,0,0,0));
} }
GlobalParam globalParam=new GlobalParam(); GlobalParam globalParam=new GlobalParam();
ScheduleOperationService ScheduleOperation=new ScheduleOperationService(); ScheduleOperationService ScheduleOperation=new ScheduleOperationService();
System.out.println(operation.getSelectMachineID());
ScheduleOperation.editMachineOption(chromosome,operation,operation.getSelectMachineID(),globalParam); ScheduleOperation.editMachineOption(chromosome,operation,operation.getSelectMachineID(),globalParam);
return redecodeChromosome(chromosome,SceneId); return redecodeChromosome(chromosome,SceneId);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment