This commit is contained in:
@@ -8,7 +8,9 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
@@ -24,6 +26,39 @@ public class OrderController {
|
||||
@Autowired
|
||||
private OrderService orderService;
|
||||
|
||||
/**
|
||||
* 创建新订单
|
||||
* @param orderRequest 订单信息
|
||||
* @return 创建结果
|
||||
*/
|
||||
@PostMapping
|
||||
public ResponseEntity<Map<String, Object>> createOrder(@RequestBody OrderRequest orderRequest) {
|
||||
Order order = new Order();
|
||||
// 设置订单属性
|
||||
order.setWarehouseId(orderRequest.getStartPoint().getId());
|
||||
order.setWarehouseName(orderRequest.getStartPoint().getName());
|
||||
order.setStartPointName(orderRequest.getStartPoint().getName());
|
||||
order.setStartPointLongitude(orderRequest.getStartPoint().getLongitude());
|
||||
order.setStartPointLatitude(orderRequest.getStartPoint().getLatitude());
|
||||
|
||||
order.setEndPointName(orderRequest.getEndPoint().getName());
|
||||
order.setEndPointLongitude(orderRequest.getEndPoint().getLongitude());
|
||||
order.setEndPointLatitude(orderRequest.getEndPoint().getLatitude());
|
||||
|
||||
order.setStatus(orderRequest.getStatus());
|
||||
order.setGoodsType(orderRequest.getGoodsType());
|
||||
order.setGoodsWeight(orderRequest.getGoodsWeight());
|
||||
order.setCreateTime(System.currentTimeMillis());
|
||||
|
||||
Order savedOrder = orderService.createOrder(order);
|
||||
|
||||
Map<String, Object> response = new HashMap<>();
|
||||
response.put("success", true);
|
||||
response.put("message", "订单创建成功");
|
||||
response.put("data", Map.of("id", savedOrder.getId()));
|
||||
return ResponseEntity.ok(response);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有待分配订单。
|
||||
* @return 待分配订单列表
|
||||
@@ -143,6 +178,20 @@ public class OrderController {
|
||||
}).collect(Collectors.toList());
|
||||
return ResponseEntity.ok(orders);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除指定ID的订单
|
||||
* @param id 订单ID
|
||||
* @return 删除结果
|
||||
*/
|
||||
@DeleteMapping("/{id}")
|
||||
public ResponseEntity<Map<String, Object>> deleteOrder(@PathVariable Long id) {
|
||||
orderService.deleteOrder(id);
|
||||
Map<String, Object> response = new HashMap<>();
|
||||
response.put("success", true);
|
||||
response.put("message", "订单删除成功");
|
||||
return ResponseEntity.ok(response);
|
||||
}
|
||||
|
||||
/**
|
||||
* 指派请求体
|
||||
@@ -151,4 +200,31 @@ public class OrderController {
|
||||
public static class AssignRequest {
|
||||
private Long deliveryPersonId;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建订单请求体
|
||||
*/
|
||||
@Data
|
||||
public static class OrderRequest {
|
||||
private StartPoint startPoint;
|
||||
private EndPoint endPoint;
|
||||
private String status;
|
||||
private String goodsType;
|
||||
private Double goodsWeight;
|
||||
|
||||
@Data
|
||||
public static class StartPoint {
|
||||
private Long id;
|
||||
private String name;
|
||||
private Double longitude;
|
||||
private Double latitude;
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class EndPoint {
|
||||
private String name;
|
||||
private Double longitude;
|
||||
private Double latitude;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -17,12 +17,8 @@ public class Order {
|
||||
private String orderNo;
|
||||
/** 仓库ID */
|
||||
private Long warehouseId;
|
||||
/** 仓库名称 */
|
||||
private String warehouseName;
|
||||
/** 配送员ID */
|
||||
private Long deliveryPersonId;
|
||||
/** 配送员名称 */
|
||||
private String deliveryPersonName;
|
||||
/** 订单状态(pending/assigned/in_transit/delivered) */
|
||||
private String status;
|
||||
/** 客户姓名 */
|
||||
@@ -47,8 +43,6 @@ public class Order {
|
||||
private String goodsType;
|
||||
/** 货物重量(公斤) */
|
||||
private Double goodsWeight;
|
||||
/** 起点名称(仓库) */
|
||||
private String startPointName;
|
||||
/** 起点经度 */
|
||||
private Double startPointLongitude;
|
||||
/** 起点纬度 */
|
||||
@@ -59,4 +53,14 @@ public class Order {
|
||||
private Double endPointLongitude;
|
||||
/** 终点纬度 */
|
||||
private Double endPointLatitude;
|
||||
|
||||
// 以下字段不再存储在数据库中,而是通过关联查询实时获取
|
||||
@Transient
|
||||
private String warehouseName;
|
||||
|
||||
@Transient
|
||||
private String deliveryPersonName;
|
||||
|
||||
@Transient
|
||||
private String startPointName;
|
||||
}
|
||||
@@ -44,4 +44,17 @@ public interface OrderService {
|
||||
* @return 订单列表
|
||||
*/
|
||||
List<Order> getAllOrders();
|
||||
|
||||
/**
|
||||
* 删除指定ID的订单
|
||||
* @param id 订单ID
|
||||
*/
|
||||
void deleteOrder(Long id);
|
||||
|
||||
/**
|
||||
* 创建新订单
|
||||
* @param order 订单对象
|
||||
* @return 保存后的订单对象
|
||||
*/
|
||||
Order createOrder(Order order);
|
||||
}
|
||||
@@ -1,7 +1,15 @@
|
||||
package com.light.delivery.service.impl;
|
||||
|
||||
import com.light.delivery.model.DeliveryPerson;
|
||||
import com.light.delivery.model.Employee;
|
||||
import com.light.delivery.model.Order;
|
||||
import com.light.delivery.model.User;
|
||||
import com.light.delivery.model.Warehouse;
|
||||
import com.light.delivery.repository.DeliveryPersonRepository;
|
||||
import com.light.delivery.repository.EmployeeRepository;
|
||||
import com.light.delivery.repository.OrderRepository;
|
||||
import com.light.delivery.repository.UserRepository;
|
||||
import com.light.delivery.repository.WarehouseRepository;
|
||||
import com.light.delivery.service.OrderService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
@@ -16,6 +24,18 @@ import java.util.Optional;
|
||||
public class OrderServiceImpl implements OrderService {
|
||||
@Autowired
|
||||
private OrderRepository orderRepository;
|
||||
|
||||
@Autowired
|
||||
private WarehouseRepository warehouseRepository;
|
||||
|
||||
@Autowired
|
||||
private DeliveryPersonRepository deliveryPersonRepository;
|
||||
|
||||
@Autowired
|
||||
private EmployeeRepository employeeRepository;
|
||||
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
|
||||
/**
|
||||
* 获取所有待分配订单。
|
||||
@@ -23,7 +43,10 @@ public class OrderServiceImpl implements OrderService {
|
||||
*/
|
||||
@Override
|
||||
public List<Order> getPendingOrders() {
|
||||
return orderRepository.findByStatus("PENDING");
|
||||
List<Order> orders = orderRepository.findByStatus("PENDING");
|
||||
// 为每个订单填充仓库和配送员信息
|
||||
orders.forEach(this::populateOrderDetails);
|
||||
return orders;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -39,7 +62,14 @@ public class OrderServiceImpl implements OrderService {
|
||||
|
||||
@Override
|
||||
public Order getOrderById(Long id) {
|
||||
return orderRepository.findById(id).orElse(null);
|
||||
Optional<Order> orderOptional = orderRepository.findById(id);
|
||||
if (orderOptional.isPresent()) {
|
||||
Order order = orderOptional.get();
|
||||
// 实时获取并设置仓库名称和配送员名称
|
||||
populateOrderDetails(order);
|
||||
return order;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -54,7 +84,10 @@ public class OrderServiceImpl implements OrderService {
|
||||
|
||||
@Override
|
||||
public List<Order> getOrdersByDeliveryPerson(Long deliveryPersonId) {
|
||||
return orderRepository.findByDeliveryPersonId(deliveryPersonId);
|
||||
List<Order> orders = orderRepository.findByDeliveryPersonId(deliveryPersonId);
|
||||
// 为每个订单填充仓库和配送员信息
|
||||
orders.forEach(this::populateOrderDetails);
|
||||
return orders;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -63,6 +96,69 @@ public class OrderServiceImpl implements OrderService {
|
||||
*/
|
||||
@Override
|
||||
public List<Order> getAllOrders() {
|
||||
return orderRepository.findAll();
|
||||
List<Order> orders = orderRepository.findAll();
|
||||
// 为每个订单填充仓库和配送员信息
|
||||
orders.forEach(this::populateOrderDetails);
|
||||
return orders;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除指定ID的订单
|
||||
* @param id 订单ID
|
||||
*/
|
||||
@Override
|
||||
public void deleteOrder(Long id) {
|
||||
orderRepository.deleteById(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建新订单
|
||||
* @param order 订单对象
|
||||
* @return 保存后的订单对象
|
||||
*/
|
||||
@Override
|
||||
public Order createOrder(Order order) {
|
||||
return orderRepository.save(order);
|
||||
}
|
||||
|
||||
/**
|
||||
* 填充订单的仓库名称和配送员名称等详细信息
|
||||
* @param order 订单对象
|
||||
*/
|
||||
private void populateOrderDetails(Order order) {
|
||||
// 获取仓库名称
|
||||
if (order.getWarehouseId() != null) {
|
||||
Optional<Warehouse> warehouseOptional = warehouseRepository.findById(order.getWarehouseId());
|
||||
if (warehouseOptional.isPresent()) {
|
||||
Warehouse warehouse = warehouseOptional.get();
|
||||
order.setWarehouseName(warehouse.getName());
|
||||
order.setStartPointName(warehouse.getName());
|
||||
order.setStartPointLongitude(warehouse.getLongitude());
|
||||
order.setStartPointLatitude(warehouse.getLatitude());
|
||||
}
|
||||
}
|
||||
|
||||
// 获取配送员名称
|
||||
if (order.getDeliveryPersonId() != null) {
|
||||
Optional<DeliveryPerson> deliveryPersonOptional = deliveryPersonRepository.findById(order.getDeliveryPersonId());
|
||||
if (deliveryPersonOptional.isPresent()) {
|
||||
DeliveryPerson deliveryPerson = deliveryPersonOptional.get();
|
||||
// 通过配送员关联的用户ID获取用户信息
|
||||
Optional<User> userOptional = userRepository.findById(deliveryPerson.getUserId());
|
||||
if (userOptional.isPresent()) {
|
||||
User user = userOptional.get();
|
||||
// 通过用户关联的员工ID获取员工信息
|
||||
Optional<Employee> employeeOptional = employeeRepository.findById(user.getEmployeeId());
|
||||
if (employeeOptional.isPresent()) {
|
||||
Employee employee = employeeOptional.get();
|
||||
order.setDeliveryPersonName(employee.getName());
|
||||
} else {
|
||||
order.setDeliveryPersonName("Unknown Employee");
|
||||
}
|
||||
} else {
|
||||
order.setDeliveryPersonName("Unknown User");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user