公共接口

parent 480f12c6
package com.aps.controller.common;
import com.aps.common.util.R;
import com.aps.entity.common.Paged;
import com.aps.service.common.ChromosomeDataService;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Collections;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/queryChromosome")
@Tag(name = "染色体数据查询", description = "根据实体名称查询染色体中的数据")
public class ChromosomeDataController {
@Autowired
private ChromosomeDataService chromosomeDataService;
/**
* 通用接口,根据实体名称查询Chromosome中的数据,支持分页和条件过滤
* 示例: POST /queryChromosome/order/page
* Body: { "sceneId": "SCENE001", "pageIndex": 1, "pageSize": 10, "conditions": [...] }
*
* @param entityName 实体名称 (如: order, entry, machine等)
* @param paged 分页和条件对象
* @return 分页数据
*/
@PostMapping("/{entityName}/page")
public R<Map<String, Object>> queryChromosomeDataWithConditions(
@PathVariable String entityName,
@RequestBody Paged paged) {
String sceneId = paged.getCondition("sceneId") != null ?
paged.getCondition("sceneId").getFieldValue() : null;
if (sceneId == null || sceneId.isEmpty()) {
return R.failed("sceneId不能为空");
}
Map<String, Object> result = chromosomeDataService.queryChromosomeDataWithConditions(
sceneId, entityName, paged);
return R.ok(result);
}
/**
* 通用接口,根据实体名称查询Chromosome中的列表数据,支持条件过滤
* 示例: POST /queryChromosome/order/list
* Body: { "sceneId": "SCENE001", "conditions": [...] }
*
* @param entityName 实体名称 (如: order, entry, machine等)
* @param paged 条件对象
* @return 列表数据
*/
@PostMapping("/{entityName}/list")
public R<List<Object>> queryChromosomeDataList(
@PathVariable String entityName,
@RequestBody Paged paged) {
String sceneId = paged.getCondition("sceneId") != null ?
paged.getCondition("sceneId").getFieldValue() : null;
if (sceneId == null || sceneId.isEmpty()) {
return R.failed("sceneId不能为空");
}
Object result = chromosomeDataService.queryChromosomeData(sceneId, entityName);
// 如果结果是List类型,则应用条件过滤
if (result instanceof List) {
List<Object> dataList = (List<Object>) result;
// 应用条件过滤
if (!paged.getConditions().isEmpty()) {
// 这里可以调用服务层的过滤方法
Map<String, Object> filteredResult = chromosomeDataService.queryChromosomeDataWithConditions(
sceneId, entityName, paged);
dataList = (List<Object>) filteredResult.get("records");
}
return R.ok(dataList);
} else {
return R.ok(Collections.singletonList(result));
}
}
/**
* 通用接口,根据实体名称和ID查询Chromosome中的单个数据
* 示例: /queryChromosome/order/get?sceneId=xxx&id=123
*
* @param sceneId 场景ID
* @param entityName 实体名称 (如: order, entry, machine等)
* @param id 数据ID
* @return 单个数据对象
*/
@GetMapping("/{entityName}/get")
public R<Object> queryChromosomeDataById(
@RequestParam String sceneId,
@PathVariable String entityName,
@RequestParam String id) {
Object result = chromosomeDataService.queryChromosomeData(sceneId, entityName);
// 如果结果是List类型,则根据ID查找特定项
if (result instanceof List) {
List<Object> dataList = (List<Object>) result;
Object item = dataList.stream()
.filter(obj -> {
try {
// 尝试获取对象的id字段
java.lang.reflect.Field idField = obj.getClass().getDeclaredField("id");
idField.setAccessible(true);
Object itemId = idField.get(obj);
return itemId != null && itemId.toString().equals(id);
} catch (Exception e) {
// 如果没有id字段或访问失败,尝试使用Id字段
try {
java.lang.reflect.Field idField = obj.getClass().getDeclaredField("Id");
idField.setAccessible(true);
Object itemId = idField.get(obj);
return itemId != null && itemId.toString().equals(id);
} catch (Exception ex) {
return false;
}
}
})
.findFirst()
.orElse(null);
if (item != null) {
return R.ok(item);
} else {
return R.failed("未找到ID为 " + id + " 的数据");
}
} else {
// 如果不是列表,直接返回结果
return R.ok(result);
}
}
// /**
// * 通用接口,根据实体名称更新Chromosome中的数据
// * 示例: POST /queryChromosome/order/update
// * Body: { "sceneId": "SCENE001", "data": {...} }
// *
// * @param entityName 实体名称 (如: order, entry, machine等)
// * @param requestBody 包含sceneId和更新数据的请求体
// * @return 更新结果
// */
// @PostMapping("/{entityName}/update")
// public R<String> updateChromosomeData(
// @PathVariable String entityName,
// @RequestBody Map<String, Object> requestBody) {
//
// String sceneId = (String) requestBody.get("sceneId");
// @SuppressWarnings("unchecked")
// Map<String, Object> data = (Map<String, Object>) requestBody.get("data");
//
// if (sceneId == null || sceneId.isEmpty()) {
// return R.failed("sceneId不能为空");
// }
//
// if (data == null || data.isEmpty()) {
// return R.failed("更新数据不能为空");
// }
//
// boolean success = chromosomeDataService.updateChromosomeData(sceneId, entityName, data);
// if (success) {
// return R.ok("数据更新成功");
// } else {
// return R.failed("数据更新失败");
// }
// }
//
// /**
// * 通用接口,根据实体名称批量操作Chromosome中的数据
// * 示例: POST /queryChromosome/order/batch
// * Body: { "sceneId": "SCENE001", "data": [...] }
// *
// * @param entityName 实体名称 (如: order, entry, machine等)
// * @param requestBody 包含sceneId和批量数据的请求体
// * @return 批量操作结果
// */
// @PostMapping("/{entityName}/batch")
// public R<String> batchChromosomeData(
// @PathVariable String entityName,
// @RequestBody Map<String, Object> requestBody) {
//
// String sceneId = (String) requestBody.get("sceneId");
// @SuppressWarnings("unchecked")
// List<Map<String, Object>> data = (List<Map<String, Object>>) requestBody.get("data");
//
// if (sceneId == null || sceneId.isEmpty()) {
// return R.failed("sceneId不能为空");
// }
//
// if (data == null || data.isEmpty()) {
// return R.failed("批量数据不能为空");
// }
//
// int successCount = chromosomeDataService.batchChromosomeData(sceneId, entityName, data);
// return R.ok("批量操作成功,共处理 " + data.size() + " 条数据,成功更新 " + successCount + " 条");
// }
//
// /**
// * 通用接口,根据实体名称删除Chromosome中的数据
// * 示例: POST /queryChromosome/order/delete
// * Body: { "sceneId": "SCENE001", "ids": [...] }
// *
// * @param entityName 实体名称 (如: order, entry, machine等)
// * @param requestBody 包含sceneId和要删除的数据ID列表的请求体
// * @return 删除操作结果
// */
// @PostMapping("/{entityName}/delete")
// public R<String> deleteChromosomeData(
// @PathVariable String entityName,
// @RequestBody Map<String, Object> requestBody) {
//
// String sceneId = (String) requestBody.get("sceneId");
// @SuppressWarnings("unchecked")
// List<Object> ids = (List<Object>) requestBody.get("ids");
//
// if (sceneId == null || sceneId.isEmpty()) {
// return R.failed("sceneId不能为空");
// }
//
// if (ids == null || ids.isEmpty()) {
// return R.failed("删除ID列表不能为空");
// }
//
// int deleteCount = chromosomeDataService.deleteChromosomeData(sceneId, entityName, ids);
// return R.ok("删除成功,共删除 " + ids.size() + " 条数据,实际删除 " + deleteCount + " 条");
// }
}
\ No newline at end of file
package com.aps.entity.common;
public class ConditionEntity {
private String fieldName;
private String fieldValue;
private String conditionalType;
public String getFieldName() {
return fieldName;
}
public void setFieldName(String fieldName) {
this.fieldName = fieldName;
}
public String getFieldValue() {
return fieldValue;
}
public String getSafeValue() {
String safe=fieldValue.replaceAll("(?i)(delete|drop|\\s|%|;|or|‘)","");
return safe;
}
public void setFieldValue(String fieldValue) {
this.fieldValue = fieldValue;
}
public String getConditionalType() {
return conditionalType;
}
public void setConditionalType(String conditionalType) {
this.conditionalType = conditionalType;
}
@Override
public String toString() {
return "ConditionEntity{" +
"fieldName='" + fieldName + '\'' +
", fieldValue='" + fieldValue + '\'' +
", conditionalType='" + conditionalType + '\'' +
'}';
}
}
package com.aps.entity.common;
public enum ConditionEnum {
Equal("Equal",0), // 等于
NoEqual("NoEqual",10), // 等于
Like("Like",1), //模糊
GreaterThan("GreaterThan",2), //大于
GreaterThanOrEqual("GreaterThanOrEqual",3), //大于等于
LessThan("LessThan",4), //小于
LessThanOrEqual("LessThanOrEqual",5), //小于等于
In("In",6), //包含
NotIn("NotIn",7),
Between("Between",8), //范围查找
Keys("Keys",9), //字段 或 连接
InSql("InSql",10) // 工单执行,应用在根据派工人查询工单
,
IsEmpty("IsEmpty",11) ,// 是null 或者 ""
NotEmpty("NotEmpty",12),//不是 null 也不是""
Exists("Exists",13),
NotExists("NotExists",14),
InSet("InSet",15);
private String name;
private Integer value;
ConditionEnum(String name, Integer value) {
this.name=name;
this.value=value;
}
public static ConditionEnum getByName(String name) {
for (ConditionEnum status : ConditionEnum.values()) {
if (status.getName().equalsIgnoreCase(name)) {
return status;
}
}
return null;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getValue() {
return value;
}
public void setValue(Integer value) {
this.value = value;
}
}
package com.aps.entity.common;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
/**
*
* @Title: PagedGridResult.java
* @Package com.lei.utils
* @Description: 用来返回分页Grid的数据格式
*/
public class Paged {
private Integer pageIndex=1; //当前页
private Integer pageSize=1000; //每页多少条
private Integer total=0;// 特殊设置,总记录数,如果前台带有此值,则分页查询时不查询总数。
private String sortBy=""; //排序字段
private Boolean desc=true; //是否倒序
private String fields="";// 返回的字段
private String table;// 返回的表
//分组字段名称
private String groupName;
private List<ConditionEntity> conditions=new ArrayList<>(); //条件集合
public String getGroupName() {
return groupName;
}
public void setGroupName(String groupName) {
this.groupName = groupName;
}
public Integer getPageIndex() {
return pageIndex;
}
public void setPageIndex(Integer pageIndex) {
this.pageIndex = pageIndex;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
public String getSortBy() {
return sortBy;
}
public void setSortBy(String sortBy) {
this.sortBy = sortBy;
}
public Boolean getDesc() {
return desc;
}
public void setDesc(Boolean desc) {
this.desc = desc;
}
public List<ConditionEntity> getConditions() {
return conditions;
}
public ConditionEntity getCondition(String field){
Optional<ConditionEntity> first = conditions.stream().filter(s -> s.getFieldName().equalsIgnoreCase(field)).findFirst();
if(first.isPresent()){
return first.get();
}
return null;
}
public List<ConditionEntity> addCondition(String field, ConditionEnum conditionType,String sql) {
if(conditions==null){
conditions=new ArrayList<>();
}
ConditionEntity condition=new ConditionEntity();
condition.setFieldName(field);
condition.setConditionalType(conditionType.toString());
condition.setFieldValue(sql);
conditions.add(condition);
return conditions;
}
public void setConditions(List<ConditionEntity> conditions) {
this.conditions = conditions;
}
@Override
public String toString() {
return "Paged{" +
"pageIndex=" + pageIndex +
", pageSize=" + pageSize +
", sortBy='" + sortBy + '\'' +
", desc=" + desc +
", conditions=" + conditions +
'}';
}
public String getFields() {
return fields;
}
public void setFields(String fields) {
this.fields = fields;
}
public String getTable() {
return table;
}
public void setTable(String table) {
this.table = table;
}
public Integer getTotal() {
return total;
}
public void setTotal(Integer total) {
this.total = total;
}
}
package com.aps.service.common;
import com.aps.entity.Algorithm.Chromosome;
import com.aps.entity.common.Paged;
import com.aps.service.plan.SceneService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* Chromosome数据服务类
* 提供对Chromosome对象中各种实体数据的查询服务
*/
@Service
public class ChromosomeDataService {
@Autowired
private SceneService sceneService;
/**
* 根据场景ID和实体名称查询Chromosome中的数据
*
* @param sceneId 场景ID
* @param entityName 实体名称 (如: order, entry, machine等)
* @return 对应的数据对象
*/
public Object queryChromosomeData(String sceneId, String entityName) {
// 从文件中加载Chromosome对象
Chromosome chromosome = sceneService.loadChromosomeFromFile(sceneId);
if (chromosome == null) {
throw new RuntimeException("未找到场景ID为 " + sceneId + " 的Chromosome数据");
}
try {
// 根据实体名称映射到Chromosome中的字段名
String fieldName = mapEntityToField(entityName);
// 使用反射获取字段值
Field field = Chromosome.class.getDeclaredField(fieldName);
field.setAccessible(true);
return field.get(chromosome);
} catch (NoSuchFieldException e) {
throw new RuntimeException("Chromosome类中未找到字段: " + entityName, e);
} catch (IllegalAccessException e) {
throw new RuntimeException("访问Chromosome字段失败: " + e.getMessage(), e);
}
}
/**
* 根据场景ID和实体名称查询Chromosome中的分页数据
*
* @param sceneId 场景ID
* @param entityName 实体名称
* @param page 页码(从1开始)
* @param size 每页大小
* @return 分页数据
*/
public Map<String, Object> queryChromosomeDataWithPagination(String sceneId, String entityName, int page, int size) {
Object data = queryChromosomeData(sceneId, entityName);
Map<String, Object> result = new HashMap<>();
// 处理分页逻辑(如果数据是List类型)
if (data instanceof List) {
List<?> dataList = (List<?>) data;
int total = dataList.size();
int fromIndex = (page - 1) * size;
// 确保起始索引不超出范围
fromIndex = Math.min(fromIndex, total);
int toIndex = Math.min(fromIndex + size, total);
// 分页数据
List<?> pagedData = dataList.subList(fromIndex, toIndex);
result.put("records", pagedData);
result.put("total", total);
result.put("current", page);
result.put("size", size);
} else {
result.put("records", data);
result.put("total", 1);
result.put("current", 1);
result.put("size", 1);
}
return result;
}
/**
* 根据场景ID和实体名称查询Chromosome中的分页数据,带条件过滤
*
* @param sceneId 场景ID
* @param entityName 实体名称
* @param paged 分页和条件对象
* @return 分页数据
*/
public Map<String, Object> queryChromosomeDataWithConditions(String sceneId, String entityName, Paged paged) {
Object data = queryChromosomeData(sceneId, entityName);
Map<String, Object> result = new HashMap<>();
int page = paged.getPageIndex() != null ? paged.getPageIndex() : 1;
int size = paged.getPageSize() != null ? paged.getPageSize() : 10;
// 处理分页和条件逻辑(如果数据是List类型)
if (data instanceof List) {
List<?> dataList = (List<?>) data;
// 应用条件过滤
if (!CollectionUtils.isEmpty(paged.getConditions())) {
dataList = filterDataByConditions(dataList, paged.getConditions());
}
int total = dataList.size();
int fromIndex = (page - 1) * size;
// 确保起始索引不超出范围
fromIndex = Math.min(fromIndex, total);
int toIndex = Math.min(fromIndex + size, total);
// 分页数据
List<?> pagedData = dataList.subList(fromIndex, toIndex);
result.put("records", pagedData);
result.put("total", total);
result.put("current", page);
result.put("size", size);
} else {
result.put("records", data);
result.put("total", 1);
result.put("current", 1);
result.put("size", 1);
}
return result;
}
/**
* 根据条件过滤数据
* @param dataList 原始数据列表
* @param conditions 条件列表
* @return 过滤后的数据列表
*/
private List<?> filterDataByConditions(List<?> dataList, List<com.aps.entity.common.ConditionEntity> conditions) {
// 简单实现:按字段相等条件过滤
List<?> filteredList = dataList;
for (com.aps.entity.common.ConditionEntity condition : conditions) {
String fieldName = condition.getFieldName();
String fieldValue = condition.getFieldValue();
com.aps.entity.common.ConditionEnum conditionType =
com.aps.entity.common.ConditionEnum.getByName(condition.getConditionalType());
if (conditionType == com.aps.entity.common.ConditionEnum.Equal) {
filteredList = filteredList.stream()
.filter(item -> {
try {
Field field = item.getClass().getDeclaredField(fieldName);
field.setAccessible(true);
Object value = field.get(item);
return value != null && value.toString().equals(fieldValue);
} catch (Exception e) {
// 字段不存在或访问异常,跳过该条件
return true;
}
})
.collect(Collectors.toList());
}
// 可以继续添加其他条件类型的处理
}
return filteredList;
}
/**
* 将实体名称映射到Chromosome中的字段名
* @param entityName 实体名称
* @return Chromosome中的字段名
*/
private String mapEntityToField(String entityName) {
switch (entityName.toLowerCase()) {
case "order":
return "orders";
case "entry":
return "allOperations";
case "machine":
return "InitMachines";
case "globaloperationinfo":
return "globalOpList";
case "groupresult":
return "OperatRel";
default:
return entityName; // 如果名称相同则直接返回
}
}
/**
* 更新Chromosome中的数据
* @param sceneId 场景ID
* @param entityName 实体名称
* @param data 更新的数据
* @return 是否更新成功
*/
public boolean updateChromosomeData(String sceneId, String entityName, Map<String, Object> data) {
// 从文件中加载Chromosome对象
Chromosome chromosome = sceneService.loadChromosomeFromFile(sceneId);
if (chromosome == null) {
throw new RuntimeException("未找到场景ID为 " + sceneId + " 的Chromosome数据");
}
try {
// 根据实体名称映射到Chromosome中的字段名
String fieldName = mapEntityToField(entityName);
// 使用反射设置字段值
Field field = Chromosome.class.getDeclaredField(fieldName);
field.setAccessible(true);
// 获取原始数据
Object originalData = field.get(chromosome);
// 如果原始数据是List类型,并且传入的data包含id字段,则更新列表中的特定项
if (originalData instanceof List && data.containsKey("id")) {
List<Object> dataList = (List<Object>) originalData;
String id = data.get("id").toString();
// 查找并更新匹配的项
for (int i = 0; i < dataList.size(); i++) {
Object item = dataList.get(i);
try {
Field idField = item.getClass().getDeclaredField("id");
idField.setAccessible(true);
Object itemId = idField.get(item);
if (itemId != null && itemId.toString().equals(id)) {
// 找到匹配项,更新它
updateObjectFields(item, data);
break;
}
} catch (Exception e) {
// 如果没有id字段或访问失败,尝试使用Id字段
try {
Field idField = item.getClass().getDeclaredField("Id");
idField.setAccessible(true);
Object itemId = idField.get(item);
if (itemId != null && itemId.toString().equals(id)) {
// 找到匹配项,更新它
updateObjectFields(item, data);
break;
}
} catch (Exception ex) {
// 忽略异常,继续下一个
}
}
}
} else {
// 直接更新整个字段
field.set(chromosome, data);
}
// 保存更新后的Chromosome到文件
return sceneService.saveChromosomeToFile(chromosome, sceneId);
} catch (NoSuchFieldException e) {
throw new RuntimeException("Chromosome类中未找到字段: " + entityName, e);
} catch (IllegalAccessException e) {
throw new RuntimeException("访问Chromosome字段失败: " + e.getMessage(), e);
}
}
/**
* 批量操作Chromosome中的数据
* @param sceneId 场景ID
* @param entityName 实体名称
* @param dataList 批量数据
* @return 操作成功的数量
*/
public int batchChromosomeData(String sceneId, String entityName, List<Map<String, Object>> dataList) {
// 从文件中加载Chromosome对象
Chromosome chromosome = sceneService.loadChromosomeFromFile(sceneId);
if (chromosome == null) {
throw new RuntimeException("未找到场景ID为 " + sceneId + " 的Chromosome数据");
}
try {
// 根据实体名称映射到Chromosome中的字段名
String fieldName = mapEntityToField(entityName);
// 使用反射获取字段值
Field field = Chromosome.class.getDeclaredField(fieldName);
field.setAccessible(true);
// 获取原始数据
Object originalData = field.get(chromosome);
// 如果原始数据是List类型,则批量更新列表中的项
if (originalData instanceof List) {
List<Object> originalList = (List<Object>) originalData;
int successCount = 0;
for (Map<String, Object> data : dataList) {
try {
if (data.containsKey("id")) {
String id = data.get("id").toString();
// 查找并更新匹配的项
for (Object item : originalList) {
try {
Field idField = item.getClass().getDeclaredField("id");
idField.setAccessible(true);
Object itemId = idField.get(item);
if (itemId != null && itemId.toString().equals(id)) {
// 找到匹配项,更新它
updateObjectFields(item, data);
successCount++;
break;
}
} catch (Exception e) {
// 如果没有id字段或访问失败,尝试使用Id字段
try {
Field idField = item.getClass().getDeclaredField("Id");
idField.setAccessible(true);
Object itemId = idField.get(item);
if (itemId != null && itemId.toString().equals(id)) {
// 找到匹配项,更新它
updateObjectFields(item, data);
successCount++;
break;
}
} catch (Exception ex) {
// 忽略异常,继续下一个
}
}
}
}
} catch (Exception e) {
// 忽略单个数据的异常,继续处理下一个
}
}
// 保存更新后的Chromosome到文件
boolean saved = sceneService.saveChromosomeToFile(chromosome, sceneId);
return saved ? successCount : 0;
} else {
throw new RuntimeException("字段 " + entityName + " 不是列表类型,无法进行批量操作");
}
} catch (NoSuchFieldException e) {
throw new RuntimeException("Chromosome类中未找到字段: " + entityName, e);
} catch (IllegalAccessException e) {
throw new RuntimeException("访问Chromosome字段失败: " + e.getMessage(), e);
}
}
/**
* 删除Chromosome中的数据
* @param sceneId 场景ID
* @param entityName 实体名称
* @param ids 要删除的数据ID列表
* @return 删除成功的数量
*/
public int deleteChromosomeData(String sceneId, String entityName, List<Object> ids) {
// 从文件中加载Chromosome对象
Chromosome chromosome = sceneService.loadChromosomeFromFile(sceneId);
if (chromosome == null) {
throw new RuntimeException("未找到场景ID为 " + sceneId + " 的Chromosome数据");
}
try {
// 根据实体名称映射到Chromosome中的字段名
String fieldName = mapEntityToField(entityName);
// 使用反射获取字段值
Field field = Chromosome.class.getDeclaredField(fieldName);
field.setAccessible(true);
// 获取原始数据
Object originalData = field.get(chromosome);
// 如果原始数据是List类型,则从列表中删除指定项
if (originalData instanceof List) {
List<Object> originalList = (List<Object>) originalData;
int deleteCount = 0;
// 创建ID字符串列表便于比较
List<String> idStrings = ids.stream()
.map(Object::toString)
.collect(Collectors.toList());
// 使用迭代器安全地删除元素
java.util.Iterator<Object> iterator = originalList.iterator();
while (iterator.hasNext()) {
Object item = iterator.next();
try {
Field idField = item.getClass().getDeclaredField("id");
idField.setAccessible(true);
Object itemId = idField.get(item);
if (itemId != null && idStrings.contains(itemId.toString())) {
// 找到匹配项,删除它
iterator.remove();
deleteCount++;
}
} catch (Exception e) {
// 如果没有id字段或访问失败,尝试使用Id字段
try {
Field idField = item.getClass().getDeclaredField("Id");
idField.setAccessible(true);
Object itemId = idField.get(item);
if (itemId != null && idStrings.contains(itemId.toString())) {
// 找到匹配项,删除它
iterator.remove();
deleteCount++;
}
} catch (Exception ex) {
// 忽略异常,继续下一个
}
}
}
// 保存更新后的Chromosome到文件
boolean saved = sceneService.saveChromosomeToFile(chromosome, sceneId);
return saved ? deleteCount : 0;
} else {
throw new RuntimeException("字段 " + entityName + " 不是列表类型,无法进行删除操作");
}
} catch (NoSuchFieldException e) {
throw new RuntimeException("Chromosome类中未找到字段: " + entityName, e);
} catch (IllegalAccessException e) {
throw new RuntimeException("访问Chromosome字段失败: " + e.getMessage(), e);
}
}
/**
* 更新对象的字段值
* @param obj 要更新的对象
* @param data 新的字段值
*/
private void updateObjectFields(Object obj, Map<String, Object> data) {
Class<?> clazz = obj.getClass();
for (Map.Entry<String, Object> entry : data.entrySet()) {
String fieldName = entry.getKey();
Object fieldValue = entry.getValue();
try {
Field field = clazz.getDeclaredField(fieldName);
field.setAccessible(true);
field.set(obj, fieldValue);
} catch (Exception e) {
// 忽略无法设置的字段
}
}
}
}
\ No newline at end of file
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