返回数据格式修改

parent 66d8a496
package com.aps.common.exception;
import com.aps.common.util.R;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.binding.BindingException;
import org.mybatis.spring.MyBatisSystemException;
import org.springframework.dao.DataAccessException;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
import org.springframework.web.servlet.NoHandlerFoundException;
import java.sql.SQLException;
import java.util.Objects;
/**
* 全局异常处理器
* 统一处理系统中的各种异常,返回统一格式的错误信息
*/
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
/**
* 处理非法参数异常
*/
@ExceptionHandler(IllegalArgumentException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public R<Void> handleIllegalArgumentException(IllegalArgumentException e) {
log.debug("参数校验失败: {}", e.getMessage());
return R.failed(400, e.getMessage());
}
/**
* 处理场景生成异常
*/
@ExceptionHandler(SceneGenerationException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public R<Void> handleSceneGenerationException(SceneGenerationException e) {
log.debug("场景生成异常: {}", e.getMessage());
return R.failed(400, e.getMessage());
}
/**
* 处理自定义业务异常
*/
@ExceptionHandler(RuntimeException.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public R<Void> handleRuntimeException(RuntimeException e) {
log.debug("运行时异常: {}", e.getMessage());
return R.failed(500, e.getMessage());
}
/**
* 处理MyBatis绑定异常
*/
@ExceptionHandler(BindingException.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public R<Void> handleBindingException(BindingException e) {
log.debug("MyBatis绑定异常: {}", e.getMessage());
return R.failed(500, "数据绑定异常: " + e.getMessage());
}
/**
* 处理MyBatis系统异常
*/
@ExceptionHandler(MyBatisSystemException.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public R<Void> handleMyBatisSystemException(MyBatisSystemException e) {
log.debug("MyBatis系统异常: {}", e.getMessage());
Throwable cause = e.getCause();
if (cause != null && cause.getMessage().contains("ORA-17004")) {
return R.failed(500, "数据库列类型无效,请检查查询参数是否正确");
}
return R.failed(500, "数据库操作异常: " + (cause != null ? cause.getMessage() : e.getMessage()));
}
/**
* 处理SQL异常
*/
@ExceptionHandler(SQLException.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public R<Void> handleSQLException(SQLException e) {
log.debug("SQL异常: {}", e.getMessage());
return R.failed(500, "数据库访问异常: " + e.getMessage());
}
/**
* 处理数据访问异常
*/
@ExceptionHandler(DataAccessException.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public R<Void> handleDataAccessException(DataAccessException e) {
log.debug("数据访问异常: {}", e.getMessage());
return R.failed(500, "数据访问异常: " + e.getMessage());
}
/**
* 处理参数验证异常(MethodArgumentNotValidException)
*/
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public R<Void> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
log.debug("参数验证异常: {}", e.getMessage());
FieldError fieldError = e.getBindingResult().getFieldError();
if (fieldError != null) {
return R.failed(400, Objects.requireNonNull(fieldError.getDefaultMessage()));
}
return R.failed(400, "参数验证失败");
}
/**
* 处理参数绑定异常(BindException)
*/
@ExceptionHandler(BindException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public R<Void> handleBindException(BindException e) {
log.debug("参数绑定异常: {}", e.getMessage());
FieldError fieldError = e.getBindingResult().getFieldError();
if (fieldError != null) {
return R.failed(400, Objects.requireNonNull(fieldError.getDefaultMessage()));
}
return R.failed(400, "参数绑定失败");
}
/**
* 处理请求参数缺失异常
*/
@ExceptionHandler(MissingServletRequestParameterException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public R<Void> handleMissingServletRequestParameterException(MissingServletRequestParameterException e) {
log.debug("请求参数缺失: {}", e.getMessage());
return R.failed(400, "缺少必要参数: " + e.getParameterName());
}
/**
* 处理参数类型不匹配异常
*/
@ExceptionHandler(MethodArgumentTypeMismatchException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public R<Void> handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException e) {
log.debug("参数类型不匹配: {}", e.getMessage());
return R.failed(400, "参数类型不匹配: " + e.getName());
}
/**
* 处理HTTP消息不可读异常
*/
@ExceptionHandler(HttpMessageNotReadableException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public R<Void> handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {
log.debug("HTTP消息不可读: {}", e.getMessage());
return R.failed(400, "请求体格式错误");
}
/**
* 处理HTTP请求方法不支持异常
*/
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
public R<Void> handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
log.debug("HTTP请求方法不支持: {}", e.getMessage());
return R.failed(405, "请求方法不支持: " + e.getMethod());
}
/**
* 处理HTTP媒体类型不支持异常
*/
@ExceptionHandler(HttpMediaTypeNotSupportedException.class)
@ResponseStatus(HttpStatus.UNSUPPORTED_MEDIA_TYPE)
public R<Void> handleHttpMediaTypeNotSupportedException(HttpMediaTypeNotSupportedException e) {
log.debug("HTTP媒体类型不支持: {}", e.getMessage());
return R.failed(415, "媒体类型不支持: " + e.getContentType());
}
/**
* 处理404异常
*/
@ExceptionHandler(NoHandlerFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public R<Void> handleNoHandlerFoundException(NoHandlerFoundException e) {
log.debug("404异常: {}", e.getMessage());
return R.failed(404, "请求的资源不存在: " + e.getRequestURL());
}
/**
* 处理其他未捕获的异常
*/
@ExceptionHandler(Exception.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public R<Void> handleException(Exception e) {
log.debug("未知异常: {}", e.getMessage());
return R.failed(500, "系统异常,请联系管理员");
}
}
\ No newline at end of file
package com.aps.common.exception;
/**
* 场景生成异常类
* 用于标识场景生成过程中的业务异常
*/
public class SceneGenerationException extends RuntimeException {
public SceneGenerationException(String message) {
super(message);
}
public SceneGenerationException(String message, Throwable cause) {
super(message, cause);
}
}
\ No newline at end of file
...@@ -122,6 +122,18 @@ public class SwaggerMapParamConfig { ...@@ -122,6 +122,18 @@ public class SwaggerMapParamConfig {
"}" "}"
)); ));
break; break;
case "getResourceGantt":
case "getProductGantt":
properties.put("sceneId", new StringSchema().description("场景ID").example("B571EF6682DB463AB2977B1055A74112"));
examples.put("示例", createExample(
"请求示例",
"{\n" +
" \"sceneId\": \"B571EF6682DB463AB2977B1055A74112\"\n" +
"}"
));
break;
} }
if (!properties.isEmpty()) { if (!properties.isEmpty()) {
...@@ -160,6 +172,11 @@ public class SwaggerMapParamConfig { ...@@ -160,6 +172,11 @@ public class SwaggerMapParamConfig {
return "删除场景请求参数"; return "删除场景请求参数";
case "getAllScene": case "getAllScene":
return "获取场景列表请求参数"; return "获取场景列表请求参数";
case "getResourceGantt":
case "getProductGantt":
return "获取资源甘特图请求参数";
default: default:
return "请求参数"; return "请求参数";
} }
......
...@@ -37,10 +37,6 @@ public class LanuchController { ...@@ -37,10 +37,6 @@ public class LanuchController {
String sceneName = params.get("sceneName"); String sceneName = params.get("sceneName");
String userId = params.get("userId"); String userId = params.get("userId");
if (sceneName == null || sceneName.trim().isEmpty()) {
return R.failed("场景名称不能为空");
}
return lanuchService.lanuch(sceneName, userId); return lanuchService.lanuch(sceneName, userId);
} }
...@@ -52,16 +48,8 @@ public class LanuchController { ...@@ -52,16 +48,8 @@ public class LanuchController {
public R<Chromosome> schedule(@RequestBody Map<String, String> params) { public R<Chromosome> schedule(@RequestBody Map<String, String> params) {
String sceneId = params.get("sceneId"); String sceneId = params.get("sceneId");
if (sceneId == null || sceneId.trim().isEmpty()) {
return R.failed("场景ID不能为空");
}
try {
Chromosome scheduleChromosomes = planResultService.schedule(sceneId); Chromosome scheduleChromosomes = planResultService.schedule(sceneId);
return R.ok(scheduleChromosomes); return R.ok(scheduleChromosomes);
} catch (Exception e) {
return R.failed("排程执行失败: " + e.getMessage());
}
} }
/** /**
...@@ -74,16 +62,6 @@ public class LanuchController { ...@@ -74,16 +62,6 @@ public class LanuchController {
String newSceneName = params.get("newSceneName"); String newSceneName = params.get("newSceneName");
String userId = params.get("userId"); String userId = params.get("userId");
if (oldSceneId == null || oldSceneId.trim().isEmpty()) {
return R.failed("原场景ID不能为空");
}
if (newSceneName == null || newSceneName.trim().isEmpty()) {
return R.failed("新场景名称不能为空");
}
if (userId == null || userId.trim().isEmpty()) {
return R.failed("用户名不能为空");
}
return lanuchService.copyScene(oldSceneId, userId, newSceneName); return lanuchService.copyScene(oldSceneId, userId, newSceneName);
} }
...@@ -95,10 +73,6 @@ public class LanuchController { ...@@ -95,10 +73,6 @@ public class LanuchController {
public R<String> exportPlan(@RequestBody Map<String, String> params) { public R<String> exportPlan(@RequestBody Map<String, String> params) {
String sceneId = params.get("sceneId"); String sceneId = params.get("sceneId");
if (sceneId == null || sceneId.trim().isEmpty()) {
return R.failed("场景ID不能为空");
}
return lanuchService.exportPlan(sceneId); return lanuchService.exportPlan(sceneId);
} }
...@@ -110,12 +84,8 @@ public class LanuchController { ...@@ -110,12 +84,8 @@ public class LanuchController {
public R<Boolean> deleteScene(@RequestBody Map<String, String> params) { public R<Boolean> deleteScene(@RequestBody Map<String, String> params) {
String sceneId = params.get("sceneId"); String sceneId = params.get("sceneId");
if (sceneId == null || sceneId.trim().isEmpty()) {
return R.failed("场景ID不能为空");
}
boolean result = prodSceneConfigService.deleteSceneById(sceneId); boolean result = prodSceneConfigService.deleteSceneById(sceneId);
return result ? R.ok(true) : R.failed("删除场景失败"); return result ? R.ok(true) : R.ok(false, "删除场景失败");
} }
/** /**
...@@ -126,10 +96,6 @@ public class LanuchController { ...@@ -126,10 +96,6 @@ public class LanuchController {
public R<List<ProdSceneConfig>> getAllScene(@RequestBody Map<String, String> params) { public R<List<ProdSceneConfig>> getAllScene(@RequestBody Map<String, String> params) {
String userId = params.get("userId"); String userId = params.get("userId");
if (userId == null || userId.trim().isEmpty()) {
return R.failed("用户ID不能为空");
}
List<ProdSceneConfig> scenes = prodSceneConfigService.lambdaQuery() List<ProdSceneConfig> scenes = prodSceneConfigService.lambdaQuery()
.eq(ProdSceneConfig::getCreateUser, userId) .eq(ProdSceneConfig::getCreateUser, userId)
.list(); .list();
......
package com.aps.controller.gantt; package com.aps.controller.gantt;
import com.aps.common.util.R;
import com.aps.entity.Algorithm.Chromosome; import com.aps.entity.Algorithm.Chromosome;
import com.aps.entity.Algorithm.GAScheduleResult; import com.aps.entity.Algorithm.GAScheduleResult;
import com.aps.entity.basic.ScheduleChromosome; import com.aps.entity.basic.ScheduleChromosome;
...@@ -22,6 +23,7 @@ import java.time.LocalDateTime; ...@@ -22,6 +23,7 @@ import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit; import java.time.temporal.ChronoUnit;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@Slf4j @Slf4j
...@@ -464,35 +466,35 @@ public class ResourceGanttController { ...@@ -464,35 +466,35 @@ public class ResourceGanttController {
//
@GetMapping("/productGantt") // @GetMapping("/productGantt")
@Operation(summary = "获取资源甘特图数据", description = "获取资源甘特图数据") // @Operation(summary = "获取资源甘特图数据", description = "获取资源甘特图数据")
public List<ProductGanttVO> getProductGantt(@RequestParam String sceneId) { // public List<ProductGanttVO> getProductGantt(@RequestParam String sceneId) {
// 从PlanResultService获取ScheduleChromosome列表 // // 从PlanResultService获取ScheduleChromosome列表
Chromosome schedule = sceneService.loadChromosomeFromFile(sceneId); // Chromosome schedule = sceneService.loadChromosomeFromFile(sceneId);
//
// 转换为 ResourceGanttVO 格式 // // 转换为 ResourceGanttVO 格式
List<ProductGanttVO> productGanttVOList= new ArrayList<>(); // List<ProductGanttVO> productGanttVOList= new ArrayList<>();
List<ProductGanttVO> resourceGanttVOs = convertToProductGanttVO1(schedule); // List<ProductGanttVO> resourceGanttVOs = convertToProductGanttVO1(schedule);
productGanttVOList.addAll(resourceGanttVOs); // productGanttVOList.addAll(resourceGanttVOs);
//
return productGanttVOList; // return productGanttVOList;
} // }
//
//
@GetMapping("/resourceGantt") // @GetMapping("/resourceGantt")
@Operation(summary = "获取资源甘特图数据", description = "获取资源甘特图数据") // @Operation(summary = "获取资源甘特图数据", description = "获取资源甘特图数据")
public List<ResourceGanttVO> getResourceGantt(@RequestParam String sceneId) { // public List<ResourceGanttVO> getResourceGantt(@RequestParam String sceneId) {
// 从PlanResultService获取ScheduleChromosome列表 // // 从PlanResultService获取ScheduleChromosome列表
Chromosome schedule = sceneService.loadChromosomeFromFile(sceneId); // Chromosome schedule = sceneService.loadChromosomeFromFile(sceneId);
//
// 转换为 ResourceGanttVO 格式 // // 转换为 ResourceGanttVO 格式
List<ResourceGanttVO> resourceGanttVOList = new ArrayList<>(); // List<ResourceGanttVO> resourceGanttVOList = new ArrayList<>();
List<ResourceGanttVO> resourceGanttVOs = convertToResourceGanttVO1(schedule); // List<ResourceGanttVO> resourceGanttVOs = convertToResourceGanttVO1(schedule);
resourceGanttVOList.addAll(resourceGanttVOs); // resourceGanttVOList.addAll(resourceGanttVOs);
//
return resourceGanttVOList; // return resourceGanttVOList;
} // }
...@@ -683,6 +685,100 @@ public class ResourceGanttController { ...@@ -683,6 +685,100 @@ public class ResourceGanttController {
@GetMapping("/productGantt")
@Operation(summary = "获取资源甘特图数据", description = "获取资源甘特图数据")
public List<ProductGanttVO> productGantt(@RequestParam String sceneId) {
// 从PlanResultService获取ScheduleChromosome列表
Chromosome schedule = sceneService.loadChromosomeFromFile(sceneId);
if ( schedule == null) {
return new ArrayList<>();
}
// 转换为 ProductGanttVO 格式
List<ProductGanttVO> productGanttVOList= new ArrayList<>();
List<ProductGanttVO> resourceGanttVOs = planResultService.convertToProductGanttVO1(schedule);
productGanttVOList.addAll(resourceGanttVOs);
return productGanttVOList;
}
@GetMapping("/resourceGantt")
@Operation(summary = "获取资源甘特图数据", description = "获取资源甘特图数据")
public List<ResourceGanttVO> resourceGantt(@RequestParam String sceneId) {
// 从PlanResultService获取ScheduleChromosome列表
Chromosome schedule = sceneService.loadChromosomeFromFile(sceneId);
// 如果找不到对应的场景,返回空列表
if ( schedule == null) {
return new ArrayList<>();
}
List<Machine> machineList = planResultService.InitCalendarToAllMachines1(schedule.getScenarioID());
// 转换为 ResourceGanttVO 格式
List<ResourceGanttVO> resourceGanttVOList = new ArrayList<>();
List<ResourceGanttVO> resourceGanttVOs = planResultService.convertToResourceGanttVO1(schedule, machineList);
resourceGanttVOList.addAll(resourceGanttVOs);
return resourceGanttVOList;
}
@PostMapping("/getResourceGantt")
@Operation(summary = "获取资源甘特图数据", description = "获取资源甘特图数据")
public R<List<ResourceGanttVO>> getResourceGantt(@RequestBody Map<String, Object> params) {
// 从参数中获取sceneId
String sceneId = (String) params.get("sceneId");
// 校验sceneId是否存在
if (sceneId == null || sceneId.isEmpty()) {
throw new IllegalArgumentException("场景ID不能为空");
}
// 校验能否获取对应的文件
Chromosome schedule = sceneService.loadChromosomeFromFile(sceneId);
if (schedule == null) {
throw new RuntimeException("未找到对应的场景文件");
}
List<Machine> machineList = planResultService.InitCalendarToAllMachines1(schedule.getScenarioID());
// 转换为 ResourceGanttVO 格式
List<ResourceGanttVO> resourceGanttVOList = new ArrayList<>();
List<ResourceGanttVO> resourceGanttVOs = planResultService.convertToResourceGanttVO1(schedule, machineList);
resourceGanttVOList.addAll(resourceGanttVOs);
return R.ok(resourceGanttVOList);
}
@PostMapping("/getProductGantt")
@Operation(summary = "获取产品甘特图数据", description = "获取产品甘特图数据")
public R<List<ProductGanttVO>> getProductGantt(@RequestBody Map<String, Object> params) {
// 从参数中获取sceneId
String sceneId = (String) params.get("sceneId");
// 校验sceneId是否存在
if (sceneId == null || sceneId.isEmpty()) {
throw new IllegalArgumentException("场景ID不能为空");
}
// 校验能否获取对应的文件
Chromosome schedule = sceneService.loadChromosomeFromFile(sceneId);
if (schedule == null) {
throw new RuntimeException("未找到对应的场景文件");
}
// 转换为 ProductGanttVO 格式
List<ProductGanttVO> productGanttVOList= new ArrayList<>();
List<ProductGanttVO> resourceGanttVOs = planResultService.convertToProductGanttVO1(schedule);
productGanttVOList.addAll(resourceGanttVOs);
return R.ok(productGanttVOList);
}
......
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