add
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
package com.carmaintenance.controller;
|
||||
|
||||
import com.carmaintenance.dto.Result;
|
||||
import com.carmaintenance.entity.Customer;
|
||||
import com.carmaintenance.repository.CustomerRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 客户管理控制器
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/customers")
|
||||
@CrossOrigin
|
||||
public class CustomerController {
|
||||
|
||||
@Autowired
|
||||
private CustomerRepository customerRepository;
|
||||
|
||||
/**
|
||||
* 获取所有客户
|
||||
*/
|
||||
@GetMapping
|
||||
public Result<List<Customer>> getAllCustomers() {
|
||||
List<Customer> customers = customerRepository.findAll();
|
||||
return Result.success(customers);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据ID获取客户
|
||||
*/
|
||||
@GetMapping("/{id}")
|
||||
public Result<Customer> getCustomerById(@PathVariable Integer id) {
|
||||
Customer customer = customerRepository.findById(id).orElse(null);
|
||||
if (customer == null) {
|
||||
return Result.notFound("客户不存在");
|
||||
}
|
||||
return Result.success(customer);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据用户ID获取客户信息
|
||||
*/
|
||||
@GetMapping("/user/{userId}")
|
||||
public Result<Customer> getCustomerByUserId(@PathVariable Integer userId) {
|
||||
Customer customer = customerRepository.findByUserId(userId).orElse(null);
|
||||
if (customer == null) {
|
||||
return Result.notFound("客户信息不存在");
|
||||
}
|
||||
return Result.success(customer);
|
||||
}
|
||||
}
|
||||
@@ -2,10 +2,13 @@ package com.carmaintenance.controller;
|
||||
|
||||
import com.carmaintenance.dto.Result;
|
||||
import com.carmaintenance.entity.ServiceOrder;
|
||||
import com.carmaintenance.entity.Vehicle;
|
||||
import com.carmaintenance.repository.ServiceOrderRepository;
|
||||
import com.carmaintenance.repository.VehicleRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.List;
|
||||
@@ -21,6 +24,9 @@ public class ServiceOrderController {
|
||||
@Autowired
|
||||
private ServiceOrderRepository serviceOrderRepository;
|
||||
|
||||
@Autowired
|
||||
private VehicleRepository vehicleRepository;
|
||||
|
||||
/**
|
||||
* 获取所有工单
|
||||
*/
|
||||
@@ -98,6 +104,9 @@ public class ServiceOrderController {
|
||||
return Result.notFound("工单不存在");
|
||||
}
|
||||
|
||||
// 记录原状态,用于后续判断是否需要更新车辆保养时间
|
||||
ServiceOrder.OrderStatus oldStatus = existingOrder.getStatus();
|
||||
|
||||
if (order.getStaffId() != null) existingOrder.setStaffId(order.getStaffId());
|
||||
if (order.getArrivalTime() != null) existingOrder.setArrivalTime(order.getArrivalTime());
|
||||
if (order.getStartTime() != null) existingOrder.setStartTime(order.getStartTime());
|
||||
@@ -114,6 +123,31 @@ public class ServiceOrderController {
|
||||
if (order.getRemark() != null) existingOrder.setRemark(order.getRemark());
|
||||
|
||||
ServiceOrder updatedOrder = serviceOrderRepository.save(existingOrder);
|
||||
|
||||
// 如果工单状态变更为"已完成",且服务类型为"保养维护",则更新车辆的上次保养时间
|
||||
if (oldStatus != ServiceOrder.OrderStatus.completed
|
||||
&& updatedOrder.getStatus() == ServiceOrder.OrderStatus.completed
|
||||
&& updatedOrder.getServiceType() == ServiceOrder.ServiceType.maintenance) {
|
||||
|
||||
Vehicle vehicle = vehicleRepository.findById(updatedOrder.getVehicleId()).orElse(null);
|
||||
if (vehicle != null) {
|
||||
// 使用完成时间作为上次保养时间,如果没有完成时间则使用当前时间
|
||||
LocalDate maintenanceDate = updatedOrder.getCompleteTime() != null
|
||||
? updatedOrder.getCompleteTime().toLocalDate()
|
||||
: LocalDate.now();
|
||||
|
||||
vehicle.setLastMaintenanceDate(maintenanceDate);
|
||||
|
||||
// 更新车辆里程(如果工单中有记录当前里程)
|
||||
if (updatedOrder.getCurrentMileage() != null
|
||||
&& updatedOrder.getCurrentMileage().signum() > 0) {
|
||||
vehicle.setMileage(updatedOrder.getCurrentMileage());
|
||||
}
|
||||
|
||||
vehicleRepository.save(vehicle);
|
||||
}
|
||||
}
|
||||
|
||||
return Result.success("更新成功", updatedOrder);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user