Files
car-maintenance-system/backend/src/main/java/com/carmaintenance/controller/VehicleController.java
wangziqi cfae122685 Initial commit: Car Maintenance Management System
Author: Yang Lu

School: Liaoning Institute of Science and Technology

Major: Computer Science and Technology

Class: BZ246

Tech Stack:

- Backend: Spring Boot 2.7.18 + JPA + MySQL

- Frontend: HTML5 + CSS3 + JavaScript

Features:

- User Management (Admin/Staff/Customer roles)

- Vehicle Archive Management

- Service Order Management

- Parts Inventory Management

- Online Appointment Service

- Data Statistics and Analysis

Generated with Claude Code

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-01-07 14:28:50 +08:00

113 lines
3.7 KiB
Java

package com.carmaintenance.controller;
import com.carmaintenance.dto.Result;
import com.carmaintenance.entity.Vehicle;
import com.carmaintenance.repository.VehicleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* 车辆管理控制器
*/
@RestController
@RequestMapping("/vehicles")
@CrossOrigin
public class VehicleController {
@Autowired
private VehicleRepository vehicleRepository;
/**
* 获取所有车辆
*/
@GetMapping
public Result<List<Vehicle>> getAllVehicles() {
List<Vehicle> vehicles = vehicleRepository.findAll();
return Result.success(vehicles);
}
/**
* 根据ID获取车辆
*/
@GetMapping("/{id}")
public Result<Vehicle> getVehicleById(@PathVariable Integer id) {
Vehicle vehicle = vehicleRepository.findById(id).orElse(null);
if (vehicle == null) {
return Result.notFound("车辆不存在");
}
return Result.success(vehicle);
}
/**
* 根据客户ID获取车辆列表
*/
@GetMapping("/customer/{customerId}")
public Result<List<Vehicle>> getVehiclesByCustomerId(@PathVariable Integer customerId) {
List<Vehicle> vehicles = vehicleRepository.findByCustomerId(customerId);
return Result.success(vehicles);
}
/**
* 根据车牌号查询车辆
*/
@GetMapping("/plate/{licensePlate}")
public Result<Vehicle> getVehicleByPlate(@PathVariable String licensePlate) {
Vehicle vehicle = vehicleRepository.findByLicensePlate(licensePlate).orElse(null);
if (vehicle == null) {
return Result.notFound("车辆不存在");
}
return Result.success(vehicle);
}
/**
* 创建车辆档案
*/
@PostMapping
public Result<Vehicle> createVehicle(@RequestBody Vehicle vehicle) {
if (vehicleRepository.findByLicensePlate(vehicle.getLicensePlate()).isPresent()) {
return Result.error("车牌号已存在");
}
if (vehicle.getVin() != null && vehicleRepository.findByVin(vehicle.getVin()).isPresent()) {
return Result.error("车架号已存在");
}
Vehicle savedVehicle = vehicleRepository.save(vehicle);
return Result.success("创建成功", savedVehicle);
}
/**
* 更新车辆信息
*/
@PutMapping("/{id}")
public Result<Vehicle> updateVehicle(@PathVariable Integer id, @RequestBody Vehicle vehicle) {
Vehicle existingVehicle = vehicleRepository.findById(id).orElse(null);
if (existingVehicle == null) {
return Result.notFound("车辆不存在");
}
if (vehicle.getColor() != null) existingVehicle.setColor(vehicle.getColor());
if (vehicle.getMileage() != null) existingVehicle.setMileage(vehicle.getMileage());
if (vehicle.getLastMaintenanceDate() != null)
existingVehicle.setLastMaintenanceDate(vehicle.getLastMaintenanceDate());
if (vehicle.getNextMaintenanceDate() != null)
existingVehicle.setNextMaintenanceDate(vehicle.getNextMaintenanceDate());
if (vehicle.getStatus() != null) existingVehicle.setStatus(vehicle.getStatus());
Vehicle updatedVehicle = vehicleRepository.save(existingVehicle);
return Result.success("更新成功", updatedVehicle);
}
/**
* 删除车辆
*/
@DeleteMapping("/{id}")
public Result<Void> deleteVehicle(@PathVariable Integer id) {
if (!vehicleRepository.existsById(id)) {
return Result.notFound("车辆不存在");
}
vehicleRepository.deleteById(id);
return Result.success("删除成功");
}
}