Commit ec824a2e by 章家宝

feat:代码优化

1 parent 1de1f9eb
package com.glsx.usedcar.advice;
import com.glsx.usedcar.resp.BaseResp;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.HibernateException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.*;
import java.util.stream.Collectors;
/**
* 全局异常处理器,捕获所有Controller层未处理的异常
*/
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
// 捕获所有未指定的异常(兜底)
@ExceptionHandler(Exception.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) // 明确返回500状态码
public BaseResp<Void> handleAllUncaughtException(Exception e) {
// 打印完整异常堆栈
log.error("全局捕获未处理异常:", e);
// 返回标准化错误响应
return BaseResp.fail("服务器内部错误:" + e.getMessage());
}
// 捕获参数校验异常(比如@Valid校验失败)
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public BaseResp<Void> handleValidationException(MethodArgumentNotValidException e) {
log.error("参数校验失败:", e);
String errorMsg = e.getBindingResult().getFieldErrors().stream()
.map(fieldError -> fieldError.getField() + ":" + fieldError.getDefaultMessage())
.collect(Collectors.joining(","));
return BaseResp.fail("参数错误:" + errorMsg);
}
// 捕获JPA/Hibernate相关异常
@ExceptionHandler(HibernateException.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public BaseResp<Void> handleHibernateException(HibernateException e) {
log.error("数据库查询异常:", e);
return BaseResp.fail("数据库操作失败:" + e.getMessage());
}
}
\ No newline at end of file \ No newline at end of file
...@@ -4,10 +4,8 @@ import com.alibaba.excel.annotation.ExcelProperty; ...@@ -4,10 +4,8 @@ import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth; import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.glsx.usedcar.module.inspect.vo.InspectionWORespVO; import com.glsx.usedcar.module.inspect.vo.InspectionWORespVO;
import com.glsx.usedcar.module.vehicle.entity.Vehicle; import com.glsx.usedcar.module.vehicle.entity.Vehicle;
import lombok.AllArgsConstructor; import lombok.*;
import lombok.Builder; import org.hibernate.Hibernate;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serial; import java.io.Serial;
import java.io.Serializable; import java.io.Serializable;
...@@ -194,8 +192,21 @@ public class InspectionWorkOrderExportBean implements Serializable { ...@@ -194,8 +192,21 @@ public class InspectionWorkOrderExportBean implements Serializable {
} }
// 所属门店 // 所属门店
this.venueName = vehicle.getVenue() != null ? vehicle.getVenue().getVenueName() : ""; this.venueName = "";
if (vehicle.getVenue() != null) {
try {
// 检查并初始化懒加载对象(需确保Hibernate依赖已引入)
if (Hibernate.isInitialized(vehicle.getVenue())) {
this.venueName = vehicle.getVenue().getVenueName();
} else {
// 若未初始化,赋值为空(或根据业务处理)
this.venueName = "";
}
} catch (Exception e) {
// 捕获懒加载异常,兜底处理
this.venueName = "";
}
}
// 保管人信息 // 保管人信息
if (vehicle.getCustodianInfo() != null) { if (vehicle.getCustodianInfo() != null) {
this.custodianName = vehicle.getCustodianInfo().getCustodianName(); this.custodianName = vehicle.getCustodianInfo().getCustodianName();
......
...@@ -54,25 +54,6 @@ public class InspectionReportController { ...@@ -54,25 +54,6 @@ public class InspectionReportController {
} }
} }
@Operation(summary = "删除检测报告", description = "逻辑删除检测报告(已绑定的报告不允许删除)")
@GetMapping("/delete/{id}")
public BaseResp<Void> delete(
@Parameter(description = "检测报告ID", required = true) @PathVariable("id") Long id,
Authentication authentication) {
log.info("【接口请求】删除检测报告,操作人={},报告ID={}", getUserName(authentication), id);
try {
User user = (User) authentication.getPrincipal();
inspectionReportService.deleteInspectionReport(id, (long)user.getUserId(), user.getUserName(), InspectionPlatTypeEnum.BACKEND.getCode());
return BaseResp.success();
} catch (BusinessCodeException be) {
log.error("【接口响应】删除检测报告失败!操作人={},业务异常!", getUserName(authentication), be);
return BaseResp.fail(StrUtil.toString(be.getCode()), be.getMessage());
} catch (Exception e) {
log.error("【接口响应】删除检测报告失败!操作人={},系统异常!", getUserName(authentication), e);
return BaseResp.fail("系统异常: " + e.getMessage());
}
}
@Operation(summary = "分页查询检测报告", description = "支持关键词模糊查询(报告编号、工单号、VIN等)") @Operation(summary = "分页查询检测报告", description = "支持关键词模糊查询(报告编号、工单号、VIN等)")
@PostMapping("/page") @PostMapping("/page")
public Page<InspectionReportRespVO> pageList(@Valid @RequestBody Pagination<InspectionReport> pagination, Authentication authentication) { public Page<InspectionReportRespVO> pageList(@Valid @RequestBody Pagination<InspectionReport> pagination, Authentication authentication) {
......
...@@ -120,7 +120,7 @@ public class InspectionWorkOrderController { ...@@ -120,7 +120,7 @@ public class InspectionWorkOrderController {
@Operation(summary = "获取检测工单详情", description = "获取检测工单详情") @Operation(summary = "获取检测工单详情", description = "获取检测工单详情")
@GetMapping("/detail/{id}") @GetMapping("/detail/{id}")
public BaseResp<InspectionWODetailRespVO> pageList(@PathVariable("id") Long id) { public BaseResp<InspectionWODetailRespVO> detail(@PathVariable("id") Long id) {
log.info("获取检测工单详情: {}", id); log.info("获取检测工单详情: {}", id);
try { try {
InspectionWODetailRespVO result = inspectionWorkOrderService.detail(id); InspectionWODetailRespVO result = inspectionWorkOrderService.detail(id);
......
...@@ -25,8 +25,7 @@ import org.summerframework.core.exception.BusinessCodeException; ...@@ -25,8 +25,7 @@ import org.summerframework.core.exception.BusinessCodeException;
public class InspectionReportController { public class InspectionReportController {
private final InspectionReportService inspectionReportService; private final InspectionReportService inspectionReportService;
// ========== 原有接口(省略,保持不变) ==========
/** /**
* 根据车辆ID查询绑定检测报告的评分/评级 * 根据车辆ID查询绑定检测报告的评分/评级
*/ */
......
package com.glsx.usedcar.module.inspect.vo; package com.glsx.usedcar.module.inspect.vo;
import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.annotation.JsonFormat;
import com.glsx.usedcar.module.inspect.entity.Inspector;
import com.glsx.usedcar.module.vehicle.entity.Vehicle; import com.glsx.usedcar.module.vehicle.entity.Vehicle;
import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*; import lombok.*;
...@@ -26,7 +25,7 @@ public class InspectionWORespVO { ...@@ -26,7 +25,7 @@ public class InspectionWORespVO {
private Vehicle vehicle; private Vehicle vehicle;
@Schema(description = "检测师信息") @Schema(description = "检测师信息")
private Inspector inspector; private InspectorRespVO inspector;
@Schema(description = "woId") @Schema(description = "woId")
private String woId; private String woId;
...@@ -82,4 +81,8 @@ public class InspectionWORespVO { ...@@ -82,4 +81,8 @@ public class InspectionWORespVO {
@Schema(description = "更新时间") @Schema(description = "更新时间")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private LocalDateTime updateTime; private LocalDateTime updateTime;
@Schema(description = "备注")
private String remark;
} }
...@@ -42,8 +42,8 @@ public class WebConfig implements WebMvcConfigurer { ...@@ -42,8 +42,8 @@ public class WebConfig implements WebMvcConfigurer {
registry.addInterceptor(loginInterceptor) registry.addInterceptor(loginInterceptor)
.addPathPatterns("/**") .addPathPatterns("/**")
.excludePathPatterns( .excludePathPatterns(
"/api/auth/send-code", "/rest/auth/send-code",
"/api/auth/login" "/rest/auth/login"
); );
} }
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!