返回数据修改

parent a17d29cb
......@@ -16,27 +16,27 @@ import java.util.Map;
@Slf4j
@UtilityClass
public class ParamValidator {
// ========== 基本参数验证 ==========
public static void requireNonNull(Object value, String fieldName) {
if (value == null) {
throw new IllegalArgumentException(fieldName + "不能为空");
}
}
public static void requireNotBlank(String value, String fieldName) {
if (value == null || value.trim().isEmpty()) {
throw new IllegalArgumentException(fieldName + "不能为空");
}
}
public static void requireNotEmpty(List<?> list, String fieldName) {
if (list == null || list.isEmpty()) {
throw new IllegalArgumentException(fieldName + "不能为空");
}
}
public static void requirePositive(Number number, String fieldName) {
if (number == null) {
throw new IllegalArgumentException(fieldName + "不能为空");
......@@ -45,7 +45,7 @@ public class ParamValidator {
throw new IllegalArgumentException(fieldName + "必须大于0");
}
}
public static void requireNonNegative(Number number, String fieldName) {
if (number == null) {
throw new IllegalArgumentException(fieldName + "不能为空");
......@@ -54,13 +54,13 @@ public class ParamValidator {
throw new IllegalArgumentException(fieldName + "不能为负数");
}
}
// ========== Map参数提取 ==========
public static String getString(Map<String, Object> params, String key, String fieldName) {
Object value = params.get(key);
requireNonNull(value, fieldName);
if (value instanceof String) {
String strValue = ((String) value).trim();
if (strValue.isEmpty()) {
......@@ -68,14 +68,14 @@ public class ParamValidator {
}
return strValue;
}
throw new IllegalArgumentException(fieldName + "必须是字符串类型");
}
public static Integer getInteger(Map<String, Object> params, String key, String fieldName) {
Object value = params.get(key);
requireNonNull(value, fieldName);
try {
if (value instanceof Number) {
return ((Number) value).intValue();
......@@ -92,11 +92,32 @@ public class ParamValidator {
throw new IllegalArgumentException(fieldName + "格式不正确: " + value);
}
}
public static Double getDouble(Map<String, Object> params, String key, String fieldName) {
Object value = params.get(key);
requireNonNull(value, fieldName);
try {
if (value instanceof Number) {
return ((Number) value).doubleValue();
} else if (value instanceof String) {
String str = ((String) value).trim();
if (str.isEmpty()) {
throw new IllegalArgumentException(fieldName + "不能为空字符串");
}
return Double.parseDouble(str);
} else {
throw new IllegalArgumentException(fieldName + "格式不正确,必须是数字或字符串");
}
} catch (NumberFormatException e) {
throw new IllegalArgumentException(fieldName + "格式不正确: " + value);
}
}
public static Long getLong(Map<String, Object> params, String key, String fieldName) {
Object value = params.get(key);
requireNonNull(value, fieldName);
try {
if (value instanceof Number) {
return ((Number) value).longValue();
......@@ -113,11 +134,11 @@ public class ParamValidator {
throw new IllegalArgumentException(fieldName + "格式不正确: " + value);
}
}
public static Boolean getBoolean(Map<String, Object> params, String key, String fieldName) {
Object value = params.get(key);
requireNonNull(value, fieldName);
if (value instanceof Boolean) {
return (Boolean) value;
} else if (value instanceof String) {
......@@ -138,33 +159,33 @@ public class ParamValidator {
throw new IllegalArgumentException(fieldName + "格式不正确");
}
}
public static List<?> getList(Map<String, Object> params, String key, String fieldName) {
Object value = params.get(key);
requireNonNull(value, fieldName);
if (value instanceof List) {
return (List<?>) value;
}
throw new IllegalArgumentException(fieldName + "必须是列表类型");
}
public static LocalDateTime getDateTime(Map<String, Object> params, String key, String fieldName) {
String dateTimeStr = getString(params, key, fieldName);
return parseDateTime(dateTimeStr, fieldName);
}
// ========== 日期时间解析 ==========
public static LocalDateTime parseDateTime(String dateTimeStr, String fieldName) {
try {
if (dateTimeStr == null || dateTimeStr.trim().isEmpty()) {
throw new IllegalArgumentException(fieldName + "不能为空");
}
String trimmed = dateTimeStr.trim();
// 支持ISO格式(带时区)
if (trimmed.endsWith("Z") || trimmed.contains("+") || trimmed.contains("-")) {
try {
......@@ -174,22 +195,22 @@ public class ParamValidator {
// 继续尝试其他格式
}
}
// 尝试标准ISO格式
try {
return LocalDateTime.parse(trimmed);
} catch (DateTimeParseException e) {
// 继续尝试其他格式
}
// 尝试常用格式
DateTimeFormatter[] formatters = {
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"),
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"),
DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"),
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss")
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"),
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"),
DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"),
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss")
};
for (DateTimeFormatter formatter : formatters) {
try {
return LocalDateTime.parse(trimmed, formatter);
......@@ -197,48 +218,48 @@ public class ParamValidator {
// 继续尝试下一个格式
}
}
throw new IllegalArgumentException(fieldName + "格式不正确: " + trimmed);
} catch (DateTimeParseException e) {
throw new IllegalArgumentException(fieldName + "格式不正确: " + dateTimeStr);
}
}
// ========== 批量参数验证 ==========
public static void validateSceneExists(SceneService sceneService, String sceneId) {
if (sceneId == null || sceneId.trim().isEmpty()) {
throw new IllegalArgumentException("场景ID不能为空");
}
if (sceneService == null) {
throw new IllegalArgumentException("sceneService不能为空");
}
if (sceneService.loadChromosomeFromFile(sceneId) == null) {
throw new RuntimeException("未找到对应的场景文件: " + sceneId);
}
}
public static Double[] convertToDoubleArray(List<?> sourceList, String fieldName) {
requireNotEmpty(sourceList, fieldName);
return sourceList.stream()
.map(item -> {
if (item instanceof Number) {
return ((Number) item).doubleValue();
} else if (item instanceof String) {
try {
return Double.parseDouble(((String) item).trim());
} catch (NumberFormatException e) {
throw new IllegalArgumentException(fieldName + "包含无效的数字: " + item);
.map(item -> {
if (item instanceof Number) {
return ((Number) item).doubleValue();
} else if (item instanceof String) {
try {
return Double.parseDouble(((String) item).trim());
} catch (NumberFormatException e) {
throw new IllegalArgumentException(fieldName + "包含无效的数字: " + item);
}
} else {
throw new IllegalArgumentException(fieldName + "包含不支持的数据类型: " +
(item != null ? item.getClass().getSimpleName() : "null"));
}
} else {
throw new IllegalArgumentException(fieldName + "包含不支持的数据类型: " +
(item != null ? item.getClass().getSimpleName() : "null"));
}
})
.toArray(Double[]::new);
})
.toArray(Double[]::new);
}
}
\ No newline at end of file
......@@ -300,12 +300,7 @@ public class RoutingDataService {
if(PlanResources1!=null&&PlanResources1.size()>0)
{
for (PlanResource PlanResource : PlanResources1) {
machine.setCode(PlanResource.getCode());
machine.setName(PlanResource.getTitle());
if(PlanResource.getWorkSchedId()==null)
{
continue;
}
List<MesShiftWorkSched> ShiftWorkScheds = MesShiftWorkScheds.stream()
.filter(t -> (long) t.getWeekWorkSchedId() == PlanResource.getWorkSchedId())
.collect(Collectors.toList());
......@@ -320,14 +315,7 @@ public class RoutingDataService {
}
}
if(shifts1.size()==0)
{
throw new RuntimeException(String.format(
"设备%s没有设置日历",
machine.getCode()+":"+machine.getName()));
}
machine.setShifts(shifts1);
List<ProdEquipSpecialCal> Holidays = ProdEquipSpecialCals.stream()
......
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