Compare commits
1 Commits
feature_cj
...
main
| Author | SHA1 | Date |
|---|---|---|
|
|
eccbce8ad5 | 3 months ago |
@ -1,125 +0,0 @@
|
||||
package com.campusdelivery.controller;
|
||||
|
||||
import com.campusdelivery.dto.OrderDTO;
|
||||
import com.campusdelivery.dto.PaymentRequestDTO;
|
||||
import com.campusdelivery.dto.PaymentResponseDTO;
|
||||
import com.campusdelivery.model.Order;
|
||||
import com.campusdelivery.model.OrderStatus;
|
||||
import com.campusdelivery.service.OrderService;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 订单控制器
|
||||
*/
|
||||
public class OrderController {
|
||||
private final OrderService orderService;
|
||||
|
||||
/**
|
||||
* 构造函数
|
||||
* @param orderService 订单服务
|
||||
*/
|
||||
public OrderController(OrderService orderService) {
|
||||
this.orderService = orderService;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建订单
|
||||
* @param orderDTO 订单DTO
|
||||
* @return 创建的订单
|
||||
*/
|
||||
public Order createOrder(OrderDTO orderDTO) {
|
||||
return orderService.createOrder(orderDTO);
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理支付
|
||||
* @param paymentRequestDTO 支付请求DTO
|
||||
* @return 支付响应DTO
|
||||
*/
|
||||
public PaymentResponseDTO processPayment(PaymentRequestDTO paymentRequestDTO) {
|
||||
return orderService.processPayment(paymentRequestDTO);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新订单状态
|
||||
* @param orderId 订单ID
|
||||
* @param status 新状态
|
||||
* @return 更新后的订单
|
||||
*/
|
||||
public Order updateOrderStatus(Long orderId, OrderStatus status) {
|
||||
return orderService.updateOrderStatus(orderId, status);
|
||||
}
|
||||
|
||||
/**
|
||||
* 分配配送员
|
||||
* @param orderId 订单ID
|
||||
* @param delivererId 配送员ID
|
||||
* @return 更新后的订单
|
||||
*/
|
||||
public Order assignDeliverer(Long orderId, Long delivererId) {
|
||||
return orderService.assignDeliverer(orderId, delivererId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据ID查找订单
|
||||
* @param orderId 订单ID
|
||||
* @return 订单对象
|
||||
*/
|
||||
public Order findOrderById(Long orderId) {
|
||||
return orderService.findOrderById(orderId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据订单号查找订单
|
||||
* @param orderCode 订单号
|
||||
* @return 订单对象
|
||||
*/
|
||||
public Order findOrderByCode(String orderCode) {
|
||||
return orderService.findOrderByCode(orderCode);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据用户ID查找订单列表
|
||||
* @param userId 用户ID
|
||||
* @return 订单列表
|
||||
*/
|
||||
public List<Order> findOrdersByUserId(Long userId) {
|
||||
return orderService.findOrdersByUserId(userId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据配送员ID查找订单列表
|
||||
* @param delivererId 配送员ID
|
||||
* @return 订单列表
|
||||
*/
|
||||
public List<Order> findOrdersByDelivererId(Long delivererId) {
|
||||
return orderService.findOrdersByDelivererId(delivererId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查找所有待接单订单
|
||||
* @return 订单列表
|
||||
*/
|
||||
public List<Order> findPendingOrders() {
|
||||
return orderService.findPendingOrders();
|
||||
}
|
||||
|
||||
/**
|
||||
* 取消订单
|
||||
* @param orderId 订单ID
|
||||
* @param userId 用户ID
|
||||
* @return 是否取消成功
|
||||
*/
|
||||
public boolean cancelOrder(Long orderId, Long userId) {
|
||||
return orderService.cancelOrder(orderId, userId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查找所有订单
|
||||
* @return 订单列表
|
||||
*/
|
||||
public List<Order> findAllOrders() {
|
||||
return orderService.findAllOrders();
|
||||
}
|
||||
}
|
||||
@ -1,56 +0,0 @@
|
||||
package com.campusdelivery.dto;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
/**
|
||||
* 订单DTO
|
||||
*/
|
||||
public class OrderDTO {
|
||||
private String pickupLocation;
|
||||
private String deliveryLocation;
|
||||
private BigDecimal amount;
|
||||
private Long userId;
|
||||
|
||||
// getter和setter方法
|
||||
public String getPickupLocation() {
|
||||
return pickupLocation;
|
||||
}
|
||||
|
||||
public void setPickupLocation(String pickupLocation) {
|
||||
this.pickupLocation = pickupLocation;
|
||||
}
|
||||
|
||||
public String getDeliveryLocation() {
|
||||
return deliveryLocation;
|
||||
}
|
||||
|
||||
public void setDeliveryLocation(String deliveryLocation) {
|
||||
this.deliveryLocation = deliveryLocation;
|
||||
}
|
||||
|
||||
public BigDecimal getAmount() {
|
||||
return amount;
|
||||
}
|
||||
|
||||
public void setAmount(BigDecimal amount) {
|
||||
this.amount = amount;
|
||||
}
|
||||
|
||||
public Long getUserId() {
|
||||
return userId;
|
||||
}
|
||||
|
||||
public void setUserId(Long userId) {
|
||||
this.userId = userId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "OrderDTO{" +
|
||||
"pickupLocation='" + pickupLocation + '\'' +
|
||||
", deliveryLocation='" + deliveryLocation + '\'' +
|
||||
", amount=" + amount +
|
||||
", userId=" + userId +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@ -1,56 +0,0 @@
|
||||
package com.campusdelivery.dto;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
/**
|
||||
* 支付请求DTO
|
||||
*/
|
||||
public class PaymentRequestDTO {
|
||||
private String orderCode;
|
||||
private BigDecimal amount;
|
||||
private String paymentMethod;
|
||||
private Long userId;
|
||||
|
||||
// getter和setter方法
|
||||
public String getOrderCode() {
|
||||
return orderCode;
|
||||
}
|
||||
|
||||
public void setOrderCode(String orderCode) {
|
||||
this.orderCode = orderCode;
|
||||
}
|
||||
|
||||
public BigDecimal getAmount() {
|
||||
return amount;
|
||||
}
|
||||
|
||||
public void setAmount(BigDecimal amount) {
|
||||
this.amount = amount;
|
||||
}
|
||||
|
||||
public String getPaymentMethod() {
|
||||
return paymentMethod;
|
||||
}
|
||||
|
||||
public void setPaymentMethod(String paymentMethod) {
|
||||
this.paymentMethod = paymentMethod;
|
||||
}
|
||||
|
||||
public Long getUserId() {
|
||||
return userId;
|
||||
}
|
||||
|
||||
public void setUserId(Long userId) {
|
||||
this.userId = userId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "PaymentRequestDTO{" +
|
||||
"orderCode='" + orderCode + '\'' +
|
||||
", amount=" + amount +
|
||||
", paymentMethod='" + paymentMethod + '\'' +
|
||||
", userId=" + userId +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@ -1,65 +0,0 @@
|
||||
package com.campusdelivery.dto;
|
||||
|
||||
/**
|
||||
* 支付响应DTO
|
||||
*/
|
||||
public class PaymentResponseDTO {
|
||||
private boolean success;
|
||||
private String message;
|
||||
private String orderCode;
|
||||
private String transactionId;
|
||||
|
||||
// 构造函数
|
||||
public PaymentResponseDTO() {
|
||||
}
|
||||
|
||||
public PaymentResponseDTO(boolean success, String message, String orderCode, String transactionId) {
|
||||
this.success = success;
|
||||
this.message = message;
|
||||
this.orderCode = orderCode;
|
||||
this.transactionId = transactionId;
|
||||
}
|
||||
|
||||
// getter和setter方法
|
||||
public boolean isSuccess() {
|
||||
return success;
|
||||
}
|
||||
|
||||
public void setSuccess(boolean success) {
|
||||
this.success = success;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public String getOrderCode() {
|
||||
return orderCode;
|
||||
}
|
||||
|
||||
public void setOrderCode(String orderCode) {
|
||||
this.orderCode = orderCode;
|
||||
}
|
||||
|
||||
public String getTransactionId() {
|
||||
return transactionId;
|
||||
}
|
||||
|
||||
public void setTransactionId(String transactionId) {
|
||||
this.transactionId = transactionId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "PaymentResponseDTO{" +
|
||||
"success=" + success +
|
||||
", message='" + message + '\'' +
|
||||
", orderCode='" + orderCode + '\'' +
|
||||
", transactionId='" + transactionId + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@ -1,14 +0,0 @@
|
||||
package com.campusdelivery.exception;
|
||||
|
||||
/**
|
||||
* 订单相关异常
|
||||
*/
|
||||
public class OrderException extends RuntimeException {
|
||||
public OrderException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public OrderException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
||||
@ -1,14 +0,0 @@
|
||||
package com.campusdelivery.exception;
|
||||
|
||||
/**
|
||||
* 支付相关异常
|
||||
*/
|
||||
public class PaymentException extends RuntimeException {
|
||||
public PaymentException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public PaymentException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
||||
@ -1,145 +0,0 @@
|
||||
package com.campusdelivery.model;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* 订单模型类
|
||||
*/
|
||||
public class Order {
|
||||
private Long id;
|
||||
private String orderCode;
|
||||
private Long userId;
|
||||
private Long delivererId;
|
||||
private String pickupLocation;
|
||||
private String deliveryLocation;
|
||||
private BigDecimal amount;
|
||||
private OrderStatus status;
|
||||
private String paymentMethod;
|
||||
private String paymentTransactionId;
|
||||
private LocalDateTime createTime;
|
||||
private LocalDateTime updateTime;
|
||||
|
||||
// 构造函数
|
||||
public Order() {
|
||||
this.createTime = LocalDateTime.now();
|
||||
this.updateTime = LocalDateTime.now();
|
||||
this.status = OrderStatus.PENDING;
|
||||
}
|
||||
|
||||
// getter和setter方法
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getOrderCode() {
|
||||
return orderCode;
|
||||
}
|
||||
|
||||
public void setOrderCode(String orderCode) {
|
||||
this.orderCode = orderCode;
|
||||
}
|
||||
|
||||
public Long getUserId() {
|
||||
return userId;
|
||||
}
|
||||
|
||||
public void setUserId(Long userId) {
|
||||
this.userId = userId;
|
||||
}
|
||||
|
||||
public Long getDelivererId() {
|
||||
return delivererId;
|
||||
}
|
||||
|
||||
public void setDelivererId(Long delivererId) {
|
||||
this.delivererId = delivererId;
|
||||
}
|
||||
|
||||
public String getPickupLocation() {
|
||||
return pickupLocation;
|
||||
}
|
||||
|
||||
public void setPickupLocation(String pickupLocation) {
|
||||
this.pickupLocation = pickupLocation;
|
||||
}
|
||||
|
||||
public String getDeliveryLocation() {
|
||||
return deliveryLocation;
|
||||
}
|
||||
|
||||
public void setDeliveryLocation(String deliveryLocation) {
|
||||
this.deliveryLocation = deliveryLocation;
|
||||
}
|
||||
|
||||
public BigDecimal getAmount() {
|
||||
return amount;
|
||||
}
|
||||
|
||||
public void setAmount(BigDecimal amount) {
|
||||
this.amount = amount;
|
||||
}
|
||||
|
||||
public OrderStatus getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public void setStatus(OrderStatus status) {
|
||||
this.status = status;
|
||||
this.updateTime = LocalDateTime.now();
|
||||
}
|
||||
|
||||
public String getPaymentMethod() {
|
||||
return paymentMethod;
|
||||
}
|
||||
|
||||
public void setPaymentMethod(String paymentMethod) {
|
||||
this.paymentMethod = paymentMethod;
|
||||
}
|
||||
|
||||
public String getPaymentTransactionId() {
|
||||
return paymentTransactionId;
|
||||
}
|
||||
|
||||
public void setPaymentTransactionId(String paymentTransactionId) {
|
||||
this.paymentTransactionId = paymentTransactionId;
|
||||
}
|
||||
|
||||
public LocalDateTime getCreateTime() {
|
||||
return createTime;
|
||||
}
|
||||
|
||||
public void setCreateTime(LocalDateTime createTime) {
|
||||
this.createTime = createTime;
|
||||
}
|
||||
|
||||
public LocalDateTime getUpdateTime() {
|
||||
return updateTime;
|
||||
}
|
||||
|
||||
public void setUpdateTime(LocalDateTime updateTime) {
|
||||
this.updateTime = updateTime;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Order{" +
|
||||
"id=" + id +
|
||||
", orderCode='" + orderCode + '\'' +
|
||||
", userId=" + userId +
|
||||
", delivererId=" + delivererId +
|
||||
", pickupLocation='" + pickupLocation + '\'' +
|
||||
", deliveryLocation='" + deliveryLocation + '\'' +
|
||||
", amount=" + amount +
|
||||
", status=" + status +
|
||||
", paymentMethod='" + paymentMethod + '\'' +
|
||||
", paymentTransactionId='" + paymentTransactionId + '\'' +
|
||||
", createTime=" + createTime +
|
||||
", updateTime=" + updateTime +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@ -1,23 +0,0 @@
|
||||
package com.campusdelivery.model;
|
||||
|
||||
/**
|
||||
* 订单状态枚举类
|
||||
*/
|
||||
public enum OrderStatus {
|
||||
PENDING("待接单"),
|
||||
ACCEPTED("已接单"),
|
||||
IN_TRANSIT("配送中"),
|
||||
DELIVERED("已送达"),
|
||||
PAID("已支付"),
|
||||
CANCELLED("已取消");
|
||||
|
||||
private final String description;
|
||||
|
||||
OrderStatus(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
}
|
||||
@ -1,105 +0,0 @@
|
||||
package com.campusdelivery.repository.impl;
|
||||
|
||||
import com.campusdelivery.model.Order;
|
||||
import com.campusdelivery.model.OrderStatus;
|
||||
import com.campusdelivery.repository.OrderRepository;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
/**
|
||||
* 内存订单仓库实现
|
||||
*/
|
||||
public class InMemoryOrderRepository implements OrderRepository {
|
||||
private final Map<Long, Order> orderMap = new ConcurrentHashMap<>();
|
||||
private final AtomicLong idGenerator = new AtomicLong(1);
|
||||
|
||||
@Override
|
||||
public Order save(Order order) {
|
||||
if (order.getId() == null) {
|
||||
order.setId(idGenerator.getAndIncrement());
|
||||
}
|
||||
orderMap.put(order.getId(), order);
|
||||
return order;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Order> findById(Long id) {
|
||||
return Optional.ofNullable(orderMap.get(id));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Order> findByOrderCode(String orderCode) {
|
||||
return orderMap.values().stream()
|
||||
.filter(order -> order.getOrderCode().equals(orderCode))
|
||||
.findFirst();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Order> findByUserId(Long userId) {
|
||||
List<Order> orders = new ArrayList<>();
|
||||
for (Order order : orderMap.values()) {
|
||||
if (order.getUserId().equals(userId)) {
|
||||
orders.add(order);
|
||||
}
|
||||
}
|
||||
return orders;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Order> findByDelivererId(Long delivererId) {
|
||||
List<Order> orders = new ArrayList<>();
|
||||
for (Order order : orderMap.values()) {
|
||||
if (order.getDelivererId() != null && order.getDelivererId().equals(delivererId)) {
|
||||
orders.add(order);
|
||||
}
|
||||
}
|
||||
return orders;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Order> findByStatus(OrderStatus status) {
|
||||
List<Order> orders = new ArrayList<>();
|
||||
for (Order order : orderMap.values()) {
|
||||
if (order.getStatus() == status) {
|
||||
orders.add(order);
|
||||
}
|
||||
}
|
||||
return orders;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean updateStatus(Long id, OrderStatus status) {
|
||||
Order order = orderMap.get(id);
|
||||
if (order != null) {
|
||||
order.setStatus(status);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean updatePaymentInfo(Long id, String paymentMethod, String transactionId) {
|
||||
Order order = orderMap.get(id);
|
||||
if (order != null) {
|
||||
order.setPaymentMethod(paymentMethod);
|
||||
order.setPaymentTransactionId(transactionId);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean deleteById(Long id) {
|
||||
return orderMap.remove(id) != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Order> findAll() {
|
||||
return new ArrayList<>(orderMap.values());
|
||||
}
|
||||
}
|
||||
@ -1,92 +0,0 @@
|
||||
package com.campusdelivery.service;
|
||||
|
||||
import com.campusdelivery.dto.OrderDTO;
|
||||
import com.campusdelivery.dto.PaymentRequestDTO;
|
||||
import com.campusdelivery.dto.PaymentResponseDTO;
|
||||
import com.campusdelivery.model.Order;
|
||||
import com.campusdelivery.model.OrderStatus;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 订单服务接口
|
||||
*/
|
||||
public interface OrderService {
|
||||
/**
|
||||
* 创建订单
|
||||
* @param orderDTO 订单DTO
|
||||
* @return 创建的订单
|
||||
*/
|
||||
Order createOrder(OrderDTO orderDTO);
|
||||
|
||||
/**
|
||||
* 处理支付
|
||||
* @param paymentRequestDTO 支付请求DTO
|
||||
* @return 支付响应DTO
|
||||
*/
|
||||
PaymentResponseDTO processPayment(PaymentRequestDTO paymentRequestDTO);
|
||||
|
||||
/**
|
||||
* 更新订单状态
|
||||
* @param orderId 订单ID
|
||||
* @param status 新状态
|
||||
* @return 更新后的订单
|
||||
*/
|
||||
Order updateOrderStatus(Long orderId, OrderStatus status);
|
||||
|
||||
/**
|
||||
* 分配配送员
|
||||
* @param orderId 订单ID
|
||||
* @param delivererId 配送员ID
|
||||
* @return 更新后的订单
|
||||
*/
|
||||
Order assignDeliverer(Long orderId, Long delivererId);
|
||||
|
||||
/**
|
||||
* 根据ID查找订单
|
||||
* @param orderId 订单ID
|
||||
* @return 订单对象
|
||||
*/
|
||||
Order findOrderById(Long orderId);
|
||||
|
||||
/**
|
||||
* 根据订单号查找订单
|
||||
* @param orderCode 订单号
|
||||
* @return 订单对象
|
||||
*/
|
||||
Order findOrderByCode(String orderCode);
|
||||
|
||||
/**
|
||||
* 根据用户ID查找订单列表
|
||||
* @param userId 用户ID
|
||||
* @return 订单列表
|
||||
*/
|
||||
List<Order> findOrdersByUserId(Long userId);
|
||||
|
||||
/**
|
||||
* 根据配送员ID查找订单列表
|
||||
* @param delivererId 配送员ID
|
||||
* @return 订单列表
|
||||
*/
|
||||
List<Order> findOrdersByDelivererId(Long delivererId);
|
||||
|
||||
/**
|
||||
* 查找所有待接单订单
|
||||
* @return 订单列表
|
||||
*/
|
||||
List<Order> findPendingOrders();
|
||||
|
||||
/**
|
||||
* 取消订单
|
||||
* @param orderId 订单ID
|
||||
* @param userId 用户ID
|
||||
* @return 是否取消成功
|
||||
*/
|
||||
boolean cancelOrder(Long orderId, Long userId);
|
||||
|
||||
/**
|
||||
* 查找所有订单
|
||||
* @return 订单列表
|
||||
*/
|
||||
List<Order> findAllOrders();
|
||||
}
|
||||
@ -1,31 +0,0 @@
|
||||
package com.campusdelivery.service;
|
||||
|
||||
import com.campusdelivery.dto.PaymentRequestDTO;
|
||||
import com.campusdelivery.dto.PaymentResponseDTO;
|
||||
|
||||
/**
|
||||
* 支付服务接口
|
||||
*/
|
||||
public interface PaymentService {
|
||||
/**
|
||||
* 处理支付
|
||||
* @param paymentRequestDTO 支付请求DTO
|
||||
* @return 支付响应DTO
|
||||
*/
|
||||
PaymentResponseDTO processPayment(PaymentRequestDTO paymentRequestDTO);
|
||||
|
||||
/**
|
||||
* 验证支付状态
|
||||
* @param transactionId 交易ID
|
||||
* @return 支付是否成功
|
||||
*/
|
||||
boolean verifyPaymentStatus(String transactionId);
|
||||
|
||||
/**
|
||||
* 退款
|
||||
* @param transactionId 交易ID
|
||||
* @param amount 退款金额
|
||||
* @return 退款是否成功
|
||||
*/
|
||||
boolean refund(String transactionId, double amount);
|
||||
}
|
||||
@ -1,106 +0,0 @@
|
||||
package com.campusdelivery.service.impl;
|
||||
|
||||
import com.campusdelivery.dto.PaymentRequestDTO;
|
||||
import com.campusdelivery.dto.PaymentResponseDTO;
|
||||
import com.campusdelivery.exception.PaymentException;
|
||||
import com.campusdelivery.service.PaymentService;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* 支付服务实现
|
||||
*/
|
||||
public class PaymentServiceImpl implements PaymentService {
|
||||
private final Map<String, PaymentStatus> paymentStatusMap = new ConcurrentHashMap<>();
|
||||
|
||||
@Override
|
||||
public PaymentResponseDTO processPayment(PaymentRequestDTO paymentRequestDTO) {
|
||||
try {
|
||||
// 模拟支付处理
|
||||
validatePaymentRequest(paymentRequestDTO);
|
||||
|
||||
// 生成交易ID
|
||||
String transactionId = generateTransactionId();
|
||||
|
||||
// 模拟支付处理时间
|
||||
Thread.sleep(500);
|
||||
|
||||
// 模拟支付成功
|
||||
paymentStatusMap.put(transactionId, PaymentStatus.SUCCESS);
|
||||
|
||||
return new PaymentResponseDTO(
|
||||
true,
|
||||
"支付成功",
|
||||
paymentRequestDTO.getOrderCode(),
|
||||
transactionId
|
||||
);
|
||||
} catch (Exception e) {
|
||||
throw new PaymentException("支付处理失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean verifyPaymentStatus(String transactionId) {
|
||||
PaymentStatus status = paymentStatusMap.get(transactionId);
|
||||
return status != null && status == PaymentStatus.SUCCESS;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean refund(String transactionId, double amount) {
|
||||
try {
|
||||
// 模拟退款处理
|
||||
if (!paymentStatusMap.containsKey(transactionId)) {
|
||||
throw new PaymentException("交易不存在");
|
||||
}
|
||||
|
||||
// 模拟退款处理时间
|
||||
Thread.sleep(500);
|
||||
|
||||
// 模拟退款成功
|
||||
paymentStatusMap.put(transactionId, PaymentStatus.REFUNDED);
|
||||
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
throw new PaymentException("退款处理失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证支付请求
|
||||
*/
|
||||
private void validatePaymentRequest(PaymentRequestDTO paymentRequestDTO) {
|
||||
if (paymentRequestDTO.getOrderCode() == null || paymentRequestDTO.getOrderCode().isEmpty()) {
|
||||
throw new PaymentException("订单号不能为空");
|
||||
}
|
||||
|
||||
if (paymentRequestDTO.getAmount() == null || paymentRequestDTO.getAmount().doubleValue() <= 0) {
|
||||
throw new PaymentException("支付金额必须大于0");
|
||||
}
|
||||
|
||||
if (paymentRequestDTO.getPaymentMethod() == null || paymentRequestDTO.getPaymentMethod().isEmpty()) {
|
||||
throw new PaymentException("支付方式不能为空");
|
||||
}
|
||||
|
||||
if (paymentRequestDTO.getUserId() == null) {
|
||||
throw new PaymentException("用户ID不能为空");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成交易ID
|
||||
*/
|
||||
private String generateTransactionId() {
|
||||
return "TXN_" + UUID.randomUUID().toString().replace("-", "").substring(0, 16);
|
||||
}
|
||||
|
||||
/**
|
||||
* 支付状态枚举
|
||||
*/
|
||||
private enum PaymentStatus {
|
||||
SUCCESS,
|
||||
FAILED,
|
||||
REFUNDED
|
||||
}
|
||||
}
|
||||
@ -1,165 +0,0 @@
|
||||
package com.campusdelivery.integration;
|
||||
|
||||
import com.campusdelivery.dto.OrderDTO;
|
||||
import com.campusdelivery.dto.PaymentRequestDTO;
|
||||
import com.campusdelivery.dto.PaymentResponseDTO;
|
||||
import com.campusdelivery.model.Order;
|
||||
import com.campusdelivery.model.OrderStatus;
|
||||
import com.campusdelivery.repository.OrderRepository;
|
||||
import com.campusdelivery.repository.impl.InMemoryOrderRepository;
|
||||
import com.campusdelivery.service.OrderService;
|
||||
import com.campusdelivery.service.PaymentService;
|
||||
import com.campusdelivery.service.impl.OrderServiceImpl;
|
||||
import com.campusdelivery.service.impl.PaymentServiceImpl;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
/**
|
||||
* 订单支付集成测试
|
||||
*/
|
||||
public class OrderPaymentIntegrationTest {
|
||||
private OrderRepository orderRepository;
|
||||
private PaymentService paymentService;
|
||||
private OrderService orderService;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
// 初始化实际的服务和仓库
|
||||
orderRepository = new InMemoryOrderRepository();
|
||||
paymentService = new PaymentServiceImpl();
|
||||
orderService = new OrderServiceImpl(orderRepository, paymentService);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("测试完整的订单创建和支付流程")
|
||||
void testCompleteOrderCreateAndPaymentFlow() {
|
||||
// 1. 创建订单
|
||||
OrderDTO orderDTO = new OrderDTO();
|
||||
orderDTO.setUserId(1L);
|
||||
orderDTO.setPickupLocation("图书馆");
|
||||
orderDTO.setDeliveryLocation("12号楼");
|
||||
orderDTO.setAmount(new BigDecimal("10.00"));
|
||||
|
||||
Order createdOrder = orderService.createOrder(orderDTO);
|
||||
|
||||
// 验证订单创建成功
|
||||
assertNotNull(createdOrder);
|
||||
assertEquals(OrderStatus.PENDING, createdOrder.getStatus());
|
||||
assertNotNull(createdOrder.getOrderCode());
|
||||
|
||||
// 2. 处理支付
|
||||
PaymentRequestDTO paymentRequestDTO = new PaymentRequestDTO();
|
||||
paymentRequestDTO.setOrderCode(createdOrder.getOrderCode());
|
||||
paymentRequestDTO.setAmount(createdOrder.getAmount());
|
||||
paymentRequestDTO.setPaymentMethod("微信支付");
|
||||
paymentRequestDTO.setUserId(createdOrder.getUserId());
|
||||
|
||||
PaymentResponseDTO paymentResponse = orderService.processPayment(paymentRequestDTO);
|
||||
|
||||
// 验证支付成功
|
||||
assertNotNull(paymentResponse);
|
||||
assertTrue(paymentResponse.isSuccess());
|
||||
assertEquals("支付成功", paymentResponse.getMessage());
|
||||
assertNotNull(paymentResponse.getTransactionId());
|
||||
|
||||
// 3. 验证订单状态更新
|
||||
Order updatedOrder = orderService.findOrderById(createdOrder.getId());
|
||||
assertEquals(OrderStatus.PAID, updatedOrder.getStatus());
|
||||
assertEquals("微信支付", updatedOrder.getPaymentMethod());
|
||||
assertEquals(paymentResponse.getTransactionId(), updatedOrder.getPaymentTransactionId());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("测试订单状态流转")
|
||||
void testOrderStatusFlow() {
|
||||
// 1. 创建订单
|
||||
OrderDTO orderDTO = new OrderDTO();
|
||||
orderDTO.setUserId(1L);
|
||||
orderDTO.setPickupLocation("图书馆");
|
||||
orderDTO.setDeliveryLocation("12号楼");
|
||||
orderDTO.setAmount(new BigDecimal("10.00"));
|
||||
|
||||
Order createdOrder = orderService.createOrder(orderDTO);
|
||||
assertEquals(OrderStatus.PENDING, createdOrder.getStatus());
|
||||
|
||||
// 2. 分配配送员
|
||||
Order assignedOrder = orderService.assignDeliverer(createdOrder.getId(), 2L);
|
||||
assertEquals(OrderStatus.ACCEPTED, assignedOrder.getStatus());
|
||||
assertEquals(2L, assignedOrder.getDelivererId());
|
||||
|
||||
// 3. 处理支付
|
||||
PaymentRequestDTO paymentRequestDTO = new PaymentRequestDTO();
|
||||
paymentRequestDTO.setOrderCode(assignedOrder.getOrderCode());
|
||||
paymentRequestDTO.setAmount(assignedOrder.getAmount());
|
||||
paymentRequestDTO.setPaymentMethod("微信支付");
|
||||
paymentRequestDTO.setUserId(assignedOrder.getUserId());
|
||||
|
||||
PaymentResponseDTO paymentResponse = orderService.processPayment(paymentRequestDTO);
|
||||
assertTrue(paymentResponse.isSuccess());
|
||||
|
||||
Order paidOrder = orderService.findOrderById(assignedOrder.getId());
|
||||
assertEquals(OrderStatus.PAID, paidOrder.getStatus());
|
||||
|
||||
// 4. 更新为配送中
|
||||
Order inTransitOrder = orderService.updateOrderStatus(paidOrder.getId(), OrderStatus.IN_TRANSIT);
|
||||
assertEquals(OrderStatus.IN_TRANSIT, inTransitOrder.getStatus());
|
||||
|
||||
// 5. 更新为已送达
|
||||
Order deliveredOrder = orderService.updateOrderStatus(inTransitOrder.getId(), OrderStatus.DELIVERED);
|
||||
assertEquals(OrderStatus.DELIVERED, deliveredOrder.getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("测试订单取消流程")
|
||||
void testOrderCancellationFlow() {
|
||||
// 1. 创建订单
|
||||
OrderDTO orderDTO = new OrderDTO();
|
||||
orderDTO.setUserId(1L);
|
||||
orderDTO.setPickupLocation("图书馆");
|
||||
orderDTO.setDeliveryLocation("12号楼");
|
||||
orderDTO.setAmount(new BigDecimal("10.00"));
|
||||
|
||||
Order createdOrder = orderService.createOrder(orderDTO);
|
||||
assertEquals(OrderStatus.PENDING, createdOrder.getStatus());
|
||||
|
||||
// 2. 取消订单
|
||||
boolean cancellationResult = orderService.cancelOrder(createdOrder.getId(), 1L);
|
||||
assertTrue(cancellationResult);
|
||||
|
||||
// 3. 验证订单状态
|
||||
Order cancelledOrder = orderService.findOrderById(createdOrder.getId());
|
||||
assertEquals(OrderStatus.CANCELLED, cancelledOrder.getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("测试支付金额与订单金额不符")
|
||||
void testPaymentAmountMismatch() {
|
||||
// 1. 创建订单
|
||||
OrderDTO orderDTO = new OrderDTO();
|
||||
orderDTO.setUserId(1L);
|
||||
orderDTO.setPickupLocation("图书馆");
|
||||
orderDTO.setDeliveryLocation("12号楼");
|
||||
orderDTO.setAmount(new BigDecimal("10.00"));
|
||||
|
||||
Order createdOrder = orderService.createOrder(orderDTO);
|
||||
|
||||
// 2. 尝试用不同金额支付
|
||||
PaymentRequestDTO paymentRequestDTO = new PaymentRequestDTO();
|
||||
paymentRequestDTO.setOrderCode(createdOrder.getOrderCode());
|
||||
paymentRequestDTO.setAmount(new BigDecimal("20.00")); // 金额不符
|
||||
paymentRequestDTO.setPaymentMethod("微信支付");
|
||||
paymentRequestDTO.setUserId(createdOrder.getUserId());
|
||||
|
||||
// 验证抛出异常
|
||||
Exception exception = assertThrows(Exception.class, () -> {
|
||||
orderService.processPayment(paymentRequestDTO);
|
||||
});
|
||||
|
||||
assertEquals("支付金额与订单金额不符", exception.getMessage());
|
||||
}
|
||||
}
|
||||
@ -1,233 +0,0 @@
|
||||
package com.campusdelivery.system;
|
||||
|
||||
import com.campusdelivery.controller.OrderController;
|
||||
import com.campusdelivery.dto.OrderDTO;
|
||||
import com.campusdelivery.dto.PaymentRequestDTO;
|
||||
import com.campusdelivery.dto.PaymentResponseDTO;
|
||||
import com.campusdelivery.model.Order;
|
||||
import com.campusdelivery.model.OrderStatus;
|
||||
import com.campusdelivery.repository.OrderRepository;
|
||||
import com.campusdelivery.repository.impl.InMemoryOrderRepository;
|
||||
import com.campusdelivery.service.OrderService;
|
||||
import com.campusdelivery.service.PaymentService;
|
||||
import com.campusdelivery.service.impl.OrderServiceImpl;
|
||||
import com.campusdelivery.service.impl.PaymentServiceImpl;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.MethodOrderer;
|
||||
import org.junit.jupiter.api.Order;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
/**
|
||||
* 订单管理系统测试
|
||||
*/
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
public class OrderManagementSystemTest {
|
||||
private OrderRepository orderRepository;
|
||||
private PaymentService paymentService;
|
||||
private OrderService orderService;
|
||||
private OrderController orderController;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
// 初始化完整的系统组件
|
||||
orderRepository = new InMemoryOrderRepository();
|
||||
paymentService = new PaymentServiceImpl();
|
||||
orderService = new OrderServiceImpl(orderRepository, paymentService);
|
||||
orderController = new OrderController(orderService);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
@DisplayName("测试系统初始化")
|
||||
void testSystemInitialization() {
|
||||
// 验证所有组件初始化成功
|
||||
assertNotNull(orderRepository);
|
||||
assertNotNull(paymentService);
|
||||
assertNotNull(orderService);
|
||||
assertNotNull(orderController);
|
||||
|
||||
// 验证初始状态下没有订单
|
||||
List<Order> orders = orderController.findAllOrders();
|
||||
assertTrue(orders.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(2)
|
||||
@DisplayName("测试完整的订单管理流程")
|
||||
void testCompleteOrderManagementWorkflow() {
|
||||
// 1. 创建订单
|
||||
OrderDTO orderDTO = new OrderDTO();
|
||||
orderDTO.setUserId(1L);
|
||||
orderDTO.setPickupLocation("图书馆");
|
||||
orderDTO.setDeliveryLocation("12号楼");
|
||||
orderDTO.setAmount(new BigDecimal("10.00"));
|
||||
|
||||
Order createdOrder = orderController.createOrder(orderDTO);
|
||||
assertNotNull(createdOrder);
|
||||
assertEquals(OrderStatus.PENDING, createdOrder.getStatus());
|
||||
|
||||
// 2. 验证订单创建成功
|
||||
List<Order> allOrders = orderController.findAllOrders();
|
||||
assertEquals(1, allOrders.size());
|
||||
assertEquals(createdOrder.getId(), allOrders.get(0).getId());
|
||||
|
||||
// 3. 分配配送员
|
||||
Order assignedOrder = orderController.assignDeliverer(createdOrder.getId(), 2L);
|
||||
assertEquals(OrderStatus.ACCEPTED, assignedOrder.getStatus());
|
||||
assertEquals(2L, assignedOrder.getDelivererId());
|
||||
|
||||
// 4. 处理支付
|
||||
PaymentRequestDTO paymentRequestDTO = new PaymentRequestDTO();
|
||||
paymentRequestDTO.setOrderCode(assignedOrder.getOrderCode());
|
||||
paymentRequestDTO.setAmount(assignedOrder.getAmount());
|
||||
paymentRequestDTO.setPaymentMethod("微信支付");
|
||||
paymentRequestDTO.setUserId(assignedOrder.getUserId());
|
||||
|
||||
PaymentResponseDTO paymentResponse = orderController.processPayment(paymentRequestDTO);
|
||||
assertTrue(paymentResponse.isSuccess());
|
||||
|
||||
// 5. 验证支付后订单状态
|
||||
Order paidOrder = orderController.findOrderById(assignedOrder.getId());
|
||||
assertEquals(OrderStatus.PAID, paidOrder.getStatus());
|
||||
assertEquals("微信支付", paidOrder.getPaymentMethod());
|
||||
assertNotNull(paidOrder.getPaymentTransactionId());
|
||||
|
||||
// 6. 更新订单状态为配送中
|
||||
Order inTransitOrder = orderController.updateOrderStatus(paidOrder.getId(), OrderStatus.IN_TRANSIT);
|
||||
assertEquals(OrderStatus.IN_TRANSIT, inTransitOrder.getStatus());
|
||||
|
||||
// 7. 更新订单状态为已送达
|
||||
Order deliveredOrder = orderController.updateOrderStatus(inTransitOrder.getId(), OrderStatus.DELIVERED);
|
||||
assertEquals(OrderStatus.DELIVERED, deliveredOrder.getStatus());
|
||||
|
||||
// 8. 验证最终订单状态
|
||||
Order finalOrder = orderController.findOrderById(deliveredOrder.getId());
|
||||
assertEquals(OrderStatus.DELIVERED, finalOrder.getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
@DisplayName("测试订单查询功能")
|
||||
void testOrderQueryFunctions() {
|
||||
// 创建多个订单用于测试
|
||||
createTestOrders();
|
||||
|
||||
// 1. 按用户ID查询
|
||||
List<Order> userOrders = orderController.findOrdersByUserId(1L);
|
||||
assertEquals(2, userOrders.size());
|
||||
|
||||
// 2. 按配送员ID查询
|
||||
List<Order> delivererOrders = orderController.findOrdersByDelivererId(2L);
|
||||
assertEquals(1, delivererOrders.size());
|
||||
|
||||
// 3. 查询待接单订单
|
||||
List<Order> pendingOrders = orderController.findPendingOrders();
|
||||
assertEquals(1, pendingOrders.size());
|
||||
|
||||
// 4. 按订单号查询
|
||||
Order order = orderController.findOrderByCode("ORD_TEST_1");
|
||||
assertNotNull(order);
|
||||
assertEquals("ORD_TEST_1", order.getOrderCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(4)
|
||||
@DisplayName("测试订单取消功能")
|
||||
void testOrderCancellation() {
|
||||
// 创建测试订单
|
||||
OrderDTO orderDTO = new OrderDTO();
|
||||
orderDTO.setUserId(1L);
|
||||
orderDTO.setPickupLocation("图书馆");
|
||||
orderDTO.setDeliveryLocation("12号楼");
|
||||
orderDTO.setAmount(new BigDecimal("10.00"));
|
||||
|
||||
Order createdOrder = orderController.createOrder(orderDTO);
|
||||
|
||||
// 取消订单
|
||||
boolean cancellationResult = orderController.cancelOrder(createdOrder.getId(), 1L);
|
||||
assertTrue(cancellationResult);
|
||||
|
||||
// 验证订单状态
|
||||
Order cancelledOrder = orderController.findOrderById(createdOrder.getId());
|
||||
assertEquals(OrderStatus.CANCELLED, cancelledOrder.getStatus());
|
||||
|
||||
// 验证无法再次取消已取消的订单
|
||||
Exception exception = assertThrows(Exception.class, () -> {
|
||||
orderController.cancelOrder(cancelledOrder.getId(), 1L);
|
||||
});
|
||||
assertNotNull(exception);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(5)
|
||||
@DisplayName("测试系统边界情况")
|
||||
void testSystemBoundaryCases() {
|
||||
// 1. 测试创建订单时的边界值
|
||||
OrderDTO orderDTO = new OrderDTO();
|
||||
orderDTO.setUserId(1L);
|
||||
orderDTO.setPickupLocation(""); // 空的取货地点
|
||||
orderDTO.setDeliveryLocation("12号楼");
|
||||
orderDTO.setAmount(new BigDecimal("0.01")); // 最小金额
|
||||
|
||||
Order createdOrder = orderController.createOrder(orderDTO);
|
||||
assertNotNull(createdOrder);
|
||||
assertEquals("", createdOrder.getPickupLocation());
|
||||
assertEquals(new BigDecimal("0.01"), createdOrder.getAmount());
|
||||
|
||||
// 2. 测试支付时的边界值
|
||||
PaymentRequestDTO paymentRequestDTO = new PaymentRequestDTO();
|
||||
paymentRequestDTO.setOrderCode(createdOrder.getOrderCode());
|
||||
paymentRequestDTO.setAmount(createdOrder.getAmount());
|
||||
paymentRequestDTO.setPaymentMethod("微信支付");
|
||||
paymentRequestDTO.setUserId(createdOrder.getUserId());
|
||||
|
||||
PaymentResponseDTO paymentResponse = orderController.processPayment(paymentRequestDTO);
|
||||
assertTrue(paymentResponse.isSuccess());
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建测试订单
|
||||
*/
|
||||
private void createTestOrders() {
|
||||
// 创建第一个订单
|
||||
Order order1 = new Order();
|
||||
order1.setId(1L);
|
||||
order1.setOrderCode("ORD_TEST_1");
|
||||
order1.setUserId(1L);
|
||||
order1.setDelivererId(2L);
|
||||
order1.setPickupLocation("图书馆");
|
||||
order1.setDeliveryLocation("12号楼");
|
||||
order1.setAmount(new BigDecimal("10.00"));
|
||||
order1.setStatus(OrderStatus.ACCEPTED);
|
||||
orderRepository.save(order1);
|
||||
|
||||
// 创建第二个订单
|
||||
Order order2 = new Order();
|
||||
order2.setId(2L);
|
||||
order2.setOrderCode("ORD_TEST_2");
|
||||
order2.setUserId(1L);
|
||||
order2.setPickupLocation("食堂");
|
||||
order2.setDeliveryLocation("13号楼");
|
||||
order2.setAmount(new BigDecimal("15.00"));
|
||||
order2.setStatus(OrderStatus.PENDING);
|
||||
orderRepository.save(order2);
|
||||
|
||||
// 创建第三个订单
|
||||
Order order3 = new Order();
|
||||
order3.setId(3L);
|
||||
order3.setOrderCode("ORD_TEST_3");
|
||||
order3.setUserId(2L);
|
||||
order3.setPickupLocation("超市");
|
||||
order3.setDeliveryLocation("14号楼");
|
||||
order3.setAmount(new BigDecimal("20.00"));
|
||||
order3.setStatus(OrderStatus.PENDING);
|
||||
orderRepository.save(order3);
|
||||
}
|
||||
}
|
||||
@ -1,287 +0,0 @@
|
||||
package com.campusdelivery.unit;
|
||||
|
||||
import com.campusdelivery.dto.OrderDTO;
|
||||
import com.campusdelivery.dto.PaymentRequestDTO;
|
||||
import com.campusdelivery.dto.PaymentResponseDTO;
|
||||
import com.campusdelivery.exception.OrderException;
|
||||
import com.campusdelivery.model.Order;
|
||||
import com.campusdelivery.model.OrderStatus;
|
||||
import com.campusdelivery.repository.OrderRepository;
|
||||
import com.campusdelivery.service.OrderService;
|
||||
import com.campusdelivery.service.PaymentService;
|
||||
import com.campusdelivery.service.impl.OrderServiceImpl;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
/**
|
||||
* 订单服务单元测试
|
||||
*/
|
||||
public class OrderServiceTest {
|
||||
@Mock
|
||||
private OrderRepository orderRepository;
|
||||
|
||||
@Mock
|
||||
private PaymentService paymentService;
|
||||
|
||||
private OrderService orderService;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
MockitoAnnotations.openMocks(this);
|
||||
orderService = new OrderServiceImpl(orderRepository, paymentService);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("测试创建订单")
|
||||
void testCreateOrder() {
|
||||
// 准备测试数据
|
||||
OrderDTO orderDTO = new OrderDTO();
|
||||
orderDTO.setUserId(1L);
|
||||
orderDTO.setPickupLocation("图书馆");
|
||||
orderDTO.setDeliveryLocation("12号楼");
|
||||
orderDTO.setAmount(new BigDecimal("10.00"));
|
||||
|
||||
Order expectedOrder = new Order();
|
||||
expectedOrder.setId(1L);
|
||||
expectedOrder.setUserId(1L);
|
||||
expectedOrder.setPickupLocation("图书馆");
|
||||
expectedOrder.setDeliveryLocation("12号楼");
|
||||
expectedOrder.setAmount(new BigDecimal("10.00"));
|
||||
expectedOrder.setStatus(OrderStatus.PENDING);
|
||||
expectedOrder.setOrderCode("ORD20240101000000ABCDEF");
|
||||
|
||||
// 模拟行为
|
||||
when(orderRepository.save(any(Order.class))).thenReturn(expectedOrder);
|
||||
|
||||
// 执行测试
|
||||
Order actualOrder = orderService.createOrder(orderDTO);
|
||||
|
||||
// 验证结果
|
||||
assertNotNull(actualOrder);
|
||||
assertEquals(expectedOrder.getId(), actualOrder.getId());
|
||||
assertEquals(expectedOrder.getUserId(), actualOrder.getUserId());
|
||||
assertEquals(expectedOrder.getPickupLocation(), actualOrder.getPickupLocation());
|
||||
assertEquals(expectedOrder.getDeliveryLocation(), actualOrder.getDeliveryLocation());
|
||||
assertEquals(expectedOrder.getAmount(), actualOrder.getAmount());
|
||||
assertEquals(expectedOrder.getStatus(), actualOrder.getStatus());
|
||||
verify(orderRepository, times(1)).save(any(Order.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("测试处理支付成功")
|
||||
void testProcessPaymentSuccess() {
|
||||
// 准备测试数据
|
||||
String orderCode = "ORD20240101000000ABCDEF";
|
||||
BigDecimal amount = new BigDecimal("10.00");
|
||||
|
||||
PaymentRequestDTO paymentRequestDTO = new PaymentRequestDTO();
|
||||
paymentRequestDTO.setOrderCode(orderCode);
|
||||
paymentRequestDTO.setAmount(amount);
|
||||
paymentRequestDTO.setPaymentMethod("微信支付");
|
||||
paymentRequestDTO.setUserId(1L);
|
||||
|
||||
Order order = new Order();
|
||||
order.setId(1L);
|
||||
order.setOrderCode(orderCode);
|
||||
order.setUserId(1L);
|
||||
order.setAmount(amount);
|
||||
order.setStatus(OrderStatus.PENDING);
|
||||
|
||||
PaymentResponseDTO expectedResponse = new PaymentResponseDTO();
|
||||
expectedResponse.setSuccess(true);
|
||||
expectedResponse.setMessage("支付成功");
|
||||
expectedResponse.setOrderCode(orderCode);
|
||||
expectedResponse.setTransactionId("TXN1234567890");
|
||||
|
||||
// 模拟行为
|
||||
when(orderRepository.findByOrderCode(orderCode)).thenReturn(Optional.of(order));
|
||||
when(paymentService.processPayment(paymentRequestDTO)).thenReturn(expectedResponse);
|
||||
when(orderRepository.save(any(Order.class))).thenReturn(order);
|
||||
|
||||
// 执行测试
|
||||
PaymentResponseDTO actualResponse = orderService.processPayment(paymentRequestDTO);
|
||||
|
||||
// 验证结果
|
||||
assertNotNull(actualResponse);
|
||||
assertTrue(actualResponse.isSuccess());
|
||||
assertEquals(expectedResponse.getMessage(), actualResponse.getMessage());
|
||||
assertEquals(expectedResponse.getOrderCode(), actualResponse.getOrderCode());
|
||||
assertEquals(expectedResponse.getTransactionId(), actualResponse.getTransactionId());
|
||||
assertEquals(OrderStatus.PAID, order.getStatus());
|
||||
verify(orderRepository, times(1)).findByOrderCode(orderCode);
|
||||
verify(paymentService, times(1)).processPayment(paymentRequestDTO);
|
||||
verify(orderRepository, times(1)).save(order);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("测试处理支付失败")
|
||||
void testProcessPaymentFailure() {
|
||||
// 准备测试数据
|
||||
String orderCode = "ORD20240101000000ABCDEF";
|
||||
BigDecimal amount = new BigDecimal("10.00");
|
||||
|
||||
PaymentRequestDTO paymentRequestDTO = new PaymentRequestDTO();
|
||||
paymentRequestDTO.setOrderCode(orderCode);
|
||||
paymentRequestDTO.setAmount(amount);
|
||||
paymentRequestDTO.setPaymentMethod("微信支付");
|
||||
paymentRequestDTO.setUserId(1L);
|
||||
|
||||
Order order = new Order();
|
||||
order.setId(1L);
|
||||
order.setOrderCode(orderCode);
|
||||
order.setUserId(1L);
|
||||
order.setAmount(amount);
|
||||
order.setStatus(OrderStatus.PENDING);
|
||||
|
||||
PaymentResponseDTO expectedResponse = new PaymentResponseDTO();
|
||||
expectedResponse.setSuccess(false);
|
||||
expectedResponse.setMessage("支付失败");
|
||||
expectedResponse.setOrderCode(orderCode);
|
||||
expectedResponse.setTransactionId(null);
|
||||
|
||||
// 模拟行为
|
||||
when(orderRepository.findByOrderCode(orderCode)).thenReturn(Optional.of(order));
|
||||
when(paymentService.processPayment(paymentRequestDTO)).thenReturn(expectedResponse);
|
||||
|
||||
// 执行测试
|
||||
PaymentResponseDTO actualResponse = orderService.processPayment(paymentRequestDTO);
|
||||
|
||||
// 验证结果
|
||||
assertNotNull(actualResponse);
|
||||
assertFalse(actualResponse.isSuccess());
|
||||
assertEquals(expectedResponse.getMessage(), actualResponse.getMessage());
|
||||
assertEquals(OrderStatus.PENDING, order.getStatus());
|
||||
verify(orderRepository, times(1)).findByOrderCode(orderCode);
|
||||
verify(paymentService, times(1)).processPayment(paymentRequestDTO);
|
||||
verify(orderRepository, never()).save(any(Order.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("测试更新订单状态")
|
||||
void testUpdateOrderStatus() {
|
||||
// 准备测试数据
|
||||
Long orderId = 1L;
|
||||
Order order = new Order();
|
||||
order.setId(orderId);
|
||||
order.setStatus(OrderStatus.PENDING);
|
||||
|
||||
// 模拟行为
|
||||
when(orderRepository.findById(orderId)).thenReturn(Optional.of(order));
|
||||
when(orderRepository.save(order)).thenReturn(order);
|
||||
|
||||
// 执行测试
|
||||
Order updatedOrder = orderService.updateOrderStatus(orderId, OrderStatus.ACCEPTED);
|
||||
|
||||
// 验证结果
|
||||
assertNotNull(updatedOrder);
|
||||
assertEquals(OrderStatus.ACCEPTED, updatedOrder.getStatus());
|
||||
verify(orderRepository, times(1)).findById(orderId);
|
||||
verify(orderRepository, times(1)).save(order);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("测试更新订单状态失败 - 状态转换不合法")
|
||||
void testUpdateOrderStatusFailure_InvalidTransition() {
|
||||
// 准备测试数据
|
||||
Long orderId = 1L;
|
||||
Order order = new Order();
|
||||
order.setId(orderId);
|
||||
order.setStatus(OrderStatus.DELIVERED);
|
||||
|
||||
// 模拟行为
|
||||
when(orderRepository.findById(orderId)).thenReturn(Optional.of(order));
|
||||
|
||||
// 执行测试并验证异常
|
||||
assertThrows(OrderException.class, () -> {
|
||||
orderService.updateOrderStatus(orderId, OrderStatus.PENDING);
|
||||
});
|
||||
|
||||
// 验证结果
|
||||
verify(orderRepository, times(1)).findById(orderId);
|
||||
verify(orderRepository, never()).save(any(Order.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("测试分配配送员")
|
||||
void testAssignDeliverer() {
|
||||
// 准备测试数据
|
||||
Long orderId = 1L;
|
||||
Long delivererId = 2L;
|
||||
Order order = new Order();
|
||||
order.setId(orderId);
|
||||
order.setStatus(OrderStatus.PENDING);
|
||||
|
||||
// 模拟行为
|
||||
when(orderRepository.findById(orderId)).thenReturn(Optional.of(order));
|
||||
when(orderRepository.save(order)).thenReturn(order);
|
||||
|
||||
// 执行测试
|
||||
Order updatedOrder = orderService.assignDeliverer(orderId, delivererId);
|
||||
|
||||
// 验证结果
|
||||
assertNotNull(updatedOrder);
|
||||
assertEquals(delivererId, updatedOrder.getDelivererId());
|
||||
assertEquals(OrderStatus.ACCEPTED, updatedOrder.getStatus());
|
||||
verify(orderRepository, times(1)).findById(orderId);
|
||||
verify(orderRepository, times(1)).save(order);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("测试取消订单")
|
||||
void testCancelOrder() {
|
||||
// 准备测试数据
|
||||
Long orderId = 1L;
|
||||
Long userId = 1L;
|
||||
Order order = new Order();
|
||||
order.setId(orderId);
|
||||
order.setUserId(userId);
|
||||
order.setStatus(OrderStatus.PENDING);
|
||||
|
||||
// 模拟行为
|
||||
when(orderRepository.findById(orderId)).thenReturn(Optional.of(order));
|
||||
when(orderRepository.save(order)).thenReturn(order);
|
||||
|
||||
// 执行测试
|
||||
boolean result = orderService.cancelOrder(orderId, userId);
|
||||
|
||||
// 验证结果
|
||||
assertTrue(result);
|
||||
assertEquals(OrderStatus.CANCELLED, order.getStatus());
|
||||
verify(orderRepository, times(1)).findById(orderId);
|
||||
verify(orderRepository, times(1)).save(order);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("测试取消订单失败 - 无权取消")
|
||||
void testCancelOrderFailure_Unauthorized() {
|
||||
// 准备测试数据
|
||||
Long orderId = 1L;
|
||||
Long userId = 1L;
|
||||
Long wrongUserId = 2L;
|
||||
Order order = new Order();
|
||||
order.setId(orderId);
|
||||
order.setUserId(userId);
|
||||
order.setStatus(OrderStatus.PENDING);
|
||||
|
||||
// 模拟行为
|
||||
when(orderRepository.findById(orderId)).thenReturn(Optional.of(order));
|
||||
|
||||
// 执行测试并验证异常
|
||||
assertThrows(OrderException.class, () -> {
|
||||
orderService.cancelOrder(orderId, wrongUserId);
|
||||
});
|
||||
|
||||
// 验证结果
|
||||
verify(orderRepository, times(1)).findById(orderId);
|
||||
verify(orderRepository, never()).save(any(Order.class));
|
||||
}
|
||||
}
|
||||
@ -1,186 +0,0 @@
|
||||
package com.campusdelivery.unit;
|
||||
|
||||
import com.campusdelivery.dto.PaymentRequestDTO;
|
||||
import com.campusdelivery.dto.PaymentResponseDTO;
|
||||
import com.campusdelivery.exception.PaymentException;
|
||||
import com.campusdelivery.service.impl.PaymentServiceImpl;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
/**
|
||||
* 支付服务单元测试
|
||||
*/
|
||||
public class PaymentServiceTest {
|
||||
private PaymentServiceImpl paymentService;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
paymentService = new PaymentServiceImpl();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("测试处理支付成功")
|
||||
void testProcessPaymentSuccess() {
|
||||
// 准备测试数据
|
||||
PaymentRequestDTO paymentRequestDTO = new PaymentRequestDTO();
|
||||
paymentRequestDTO.setOrderCode("ORD20240101000000ABCDEF");
|
||||
paymentRequestDTO.setAmount(new BigDecimal("10.00"));
|
||||
paymentRequestDTO.setPaymentMethod("微信支付");
|
||||
paymentRequestDTO.setUserId(1L);
|
||||
|
||||
// 执行测试
|
||||
PaymentResponseDTO response = paymentService.processPayment(paymentRequestDTO);
|
||||
|
||||
// 验证结果
|
||||
assertNotNull(response);
|
||||
assertTrue(response.isSuccess());
|
||||
assertEquals("支付成功", response.getMessage());
|
||||
assertEquals(paymentRequestDTO.getOrderCode(), response.getOrderCode());
|
||||
assertNotNull(response.getTransactionId());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("测试处理支付失败 - 订单号为空")
|
||||
void testProcessPaymentFailure_EmptyOrderCode() {
|
||||
// 准备测试数据
|
||||
PaymentRequestDTO paymentRequestDTO = new PaymentRequestDTO();
|
||||
paymentRequestDTO.setOrderCode("");
|
||||
paymentRequestDTO.setAmount(new BigDecimal("10.00"));
|
||||
paymentRequestDTO.setPaymentMethod("微信支付");
|
||||
paymentRequestDTO.setUserId(1L);
|
||||
|
||||
// 执行测试并验证异常
|
||||
PaymentException exception = assertThrows(PaymentException.class, () -> {
|
||||
paymentService.processPayment(paymentRequestDTO);
|
||||
});
|
||||
|
||||
// 验证结果
|
||||
assertEquals("订单号不能为空", exception.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("测试处理支付失败 - 金额小于等于0")
|
||||
void testProcessPaymentFailure_InvalidAmount() {
|
||||
// 准备测试数据
|
||||
PaymentRequestDTO paymentRequestDTO = new PaymentRequestDTO();
|
||||
paymentRequestDTO.setOrderCode("ORD20240101000000ABCDEF");
|
||||
paymentRequestDTO.setAmount(new BigDecimal("0.00"));
|
||||
paymentRequestDTO.setPaymentMethod("微信支付");
|
||||
paymentRequestDTO.setUserId(1L);
|
||||
|
||||
// 执行测试并验证异常
|
||||
PaymentException exception = assertThrows(PaymentException.class, () -> {
|
||||
paymentService.processPayment(paymentRequestDTO);
|
||||
});
|
||||
|
||||
// 验证结果
|
||||
assertEquals("支付金额必须大于0", exception.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("测试处理支付失败 - 支付方式为空")
|
||||
void testProcessPaymentFailure_EmptyPaymentMethod() {
|
||||
// 准备测试数据
|
||||
PaymentRequestDTO paymentRequestDTO = new PaymentRequestDTO();
|
||||
paymentRequestDTO.setOrderCode("ORD20240101000000ABCDEF");
|
||||
paymentRequestDTO.setAmount(new BigDecimal("10.00"));
|
||||
paymentRequestDTO.setPaymentMethod("");
|
||||
paymentRequestDTO.setUserId(1L);
|
||||
|
||||
// 执行测试并验证异常
|
||||
PaymentException exception = assertThrows(PaymentException.class, () -> {
|
||||
paymentService.processPayment(paymentRequestDTO);
|
||||
});
|
||||
|
||||
// 验证结果
|
||||
assertEquals("支付方式不能为空", exception.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("测试处理支付失败 - 用户ID为空")
|
||||
void testProcessPaymentFailure_NullUserId() {
|
||||
// 准备测试数据
|
||||
PaymentRequestDTO paymentRequestDTO = new PaymentRequestDTO();
|
||||
paymentRequestDTO.setOrderCode("ORD20240101000000ABCDEF");
|
||||
paymentRequestDTO.setAmount(new BigDecimal("10.00"));
|
||||
paymentRequestDTO.setPaymentMethod("微信支付");
|
||||
paymentRequestDTO.setUserId(null);
|
||||
|
||||
// 执行测试并验证异常
|
||||
PaymentException exception = assertThrows(PaymentException.class, () -> {
|
||||
paymentService.processPayment(paymentRequestDTO);
|
||||
});
|
||||
|
||||
// 验证结果
|
||||
assertEquals("用户ID不能为空", exception.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("测试验证支付状态成功")
|
||||
void testVerifyPaymentStatusSuccess() {
|
||||
// 准备测试数据
|
||||
PaymentRequestDTO paymentRequestDTO = new PaymentRequestDTO();
|
||||
paymentRequestDTO.setOrderCode("ORD20240101000000ABCDEF");
|
||||
paymentRequestDTO.setAmount(new BigDecimal("10.00"));
|
||||
paymentRequestDTO.setPaymentMethod("微信支付");
|
||||
paymentRequestDTO.setUserId(1L);
|
||||
|
||||
// 先处理支付
|
||||
PaymentResponseDTO response = paymentService.processPayment(paymentRequestDTO);
|
||||
String transactionId = response.getTransactionId();
|
||||
|
||||
// 验证支付状态
|
||||
boolean status = paymentService.verifyPaymentStatus(transactionId);
|
||||
|
||||
// 验证结果
|
||||
assertTrue(status);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("测试验证支付状态失败 - 交易不存在")
|
||||
void testVerifyPaymentStatusFailure_TransactionNotFound() {
|
||||
// 验证不存在的交易
|
||||
boolean status = paymentService.verifyPaymentStatus("TXN1234567890");
|
||||
|
||||
// 验证结果
|
||||
assertFalse(status);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("测试退款成功")
|
||||
void testRefundSuccess() {
|
||||
// 准备测试数据
|
||||
PaymentRequestDTO paymentRequestDTO = new PaymentRequestDTO();
|
||||
paymentRequestDTO.setOrderCode("ORD20240101000000ABCDEF");
|
||||
paymentRequestDTO.setAmount(new BigDecimal("10.00"));
|
||||
paymentRequestDTO.setPaymentMethod("微信支付");
|
||||
paymentRequestDTO.setUserId(1L);
|
||||
|
||||
// 先处理支付
|
||||
PaymentResponseDTO response = paymentService.processPayment(paymentRequestDTO);
|
||||
String transactionId = response.getTransactionId();
|
||||
|
||||
// 执行退款
|
||||
boolean result = paymentService.refund(transactionId, 10.00);
|
||||
|
||||
// 验证结果
|
||||
assertTrue(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("测试退款失败 - 交易不存在")
|
||||
void testRefundFailure_TransactionNotFound() {
|
||||
// 执行退款
|
||||
PaymentException exception = assertThrows(PaymentException.class, () -> {
|
||||
paymentService.refund("TXN1234567890", 10.00);
|
||||
});
|
||||
|
||||
// 验证结果
|
||||
assertEquals("交易不存在", exception.getMessage());
|
||||
}
|
||||
}
|
||||
@ -1,81 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.campus.delivery</groupId>
|
||||
<artifactId>campus-delivery-system</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>11</maven.compiler.source>
|
||||
<maven.compiler.target>11</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<!-- JUnit测试框架 -->
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.13.2</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- 添加日志依赖 -->
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
<version>1.7.36</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-simple</artifactId>
|
||||
<version>1.7.36</version>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<!-- 编译插件 -->
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.8.1</version>
|
||||
<configuration>
|
||||
<source>${maven.compiler.source}</source>
|
||||
<target>${maven.compiler.target}</target>
|
||||
<encoding>${project.build.sourceEncoding}</encoding>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<!-- 打包插件 -->
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-assembly-plugin</artifactId>
|
||||
<version>3.3.0</version>
|
||||
<configuration>
|
||||
<archive>
|
||||
<manifest>
|
||||
<mainClass>com.campus.delivery.view.OrderPaymentView</mainClass>
|
||||
</manifest>
|
||||
</archive>
|
||||
<descriptorRefs>
|
||||
<descriptorRef>jar-with-dependencies</descriptorRef>
|
||||
</descriptorRefs>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>make-assembly</id>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>single</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
@ -1,64 +0,0 @@
|
||||
package com.campus.delivery.dao;
|
||||
|
||||
import com.campus.delivery.entity.Order;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface OrderDAO {
|
||||
/**
|
||||
* 创建新订单
|
||||
* @param order 订单对象
|
||||
* @return 创建成功的订单ID
|
||||
*/
|
||||
int createOrder(Order order);
|
||||
|
||||
/**
|
||||
* 根据ID获取订单
|
||||
* @param id 订单ID
|
||||
* @return 订单对象
|
||||
*/
|
||||
Order getOrderById(int id);
|
||||
|
||||
/**
|
||||
* 根据用户ID获取订单列表
|
||||
* @param userId 用户ID
|
||||
* @return 订单列表
|
||||
*/
|
||||
List<Order> getOrdersByUserId(int userId);
|
||||
|
||||
/**
|
||||
* 根据配送员ID获取订单列表
|
||||
* @param deliverId 配送员ID
|
||||
* @return 订单列表
|
||||
*/
|
||||
List<Order> getOrdersByDeliverId(int deliverId);
|
||||
|
||||
/**
|
||||
* 更新订单状态
|
||||
* @param id 订单ID
|
||||
* @param status 新状态
|
||||
* @return 是否更新成功
|
||||
*/
|
||||
boolean updateOrderStatus(int id, String status);
|
||||
|
||||
/**
|
||||
* 更新订单支付信息
|
||||
* @param id 订单ID
|
||||
* @param paymentMethod 支付方式
|
||||
* @return 是否更新成功
|
||||
*/
|
||||
boolean updateOrderPayment(int id, String paymentMethod);
|
||||
|
||||
/**
|
||||
* 删除订单
|
||||
* @param id 订单ID
|
||||
* @return 是否删除成功
|
||||
*/
|
||||
boolean deleteOrder(int id);
|
||||
|
||||
/**
|
||||
* 获取所有订单
|
||||
* @return 所有订单列表
|
||||
*/
|
||||
List<Order> getAllOrders();
|
||||
}
|
||||
@ -1,89 +0,0 @@
|
||||
package com.campus.delivery.dao;
|
||||
|
||||
import com.campus.delivery.entity.Order;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
public class OrderDAOImpl implements OrderDAO {
|
||||
private static List<Order> orderList = new ArrayList<>();
|
||||
private static AtomicInteger nextId = new AtomicInteger(1);
|
||||
|
||||
@Override
|
||||
public int createOrder(Order order) {
|
||||
int id = nextId.getAndIncrement();
|
||||
order.setId(id);
|
||||
order.setCreateTime(new Date());
|
||||
orderList.add(order);
|
||||
return id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Order getOrderById(int id) {
|
||||
for (Order order : orderList) {
|
||||
if (order.getId() == id) {
|
||||
return order;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Order> getOrdersByUserId(int userId) {
|
||||
List<Order> result = new ArrayList<>();
|
||||
for (Order order : orderList) {
|
||||
if (order.getUserId() == userId) {
|
||||
result.add(order);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Order> getOrdersByDeliverId(int deliverId) {
|
||||
List<Order> result = new ArrayList<>();
|
||||
for (Order order : orderList) {
|
||||
if (order.getDeliverId() == deliverId) {
|
||||
result.add(order);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean updateOrderStatus(int id, String status) {
|
||||
Order order = getOrderById(id);
|
||||
if (order != null) {
|
||||
order.setStatus(status);
|
||||
if ("PAID".equals(status)) {
|
||||
order.setPaymentTime(new Date());
|
||||
} else if ("COMPLETED".equals(status)) {
|
||||
order.setFinishTime(new Date());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean updateOrderPayment(int id, String paymentMethod) {
|
||||
Order order = getOrderById(id);
|
||||
if (order != null) {
|
||||
order.setPaymentMethod(paymentMethod);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean deleteOrder(int id) {
|
||||
return orderList.removeIf(order -> order.getId() == id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Order> getAllOrders() {
|
||||
return new ArrayList<>(orderList);
|
||||
}
|
||||
}
|
||||
@ -1,50 +0,0 @@
|
||||
package com.campus.delivery.dao;
|
||||
|
||||
import com.campus.delivery.entity.Payment;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface PaymentDAO {
|
||||
/**
|
||||
* 创建支付记录
|
||||
* @param payment 支付对象
|
||||
* @return 创建成功的支付ID
|
||||
*/
|
||||
int createPayment(Payment payment);
|
||||
|
||||
/**
|
||||
* 根据ID获取支付记录
|
||||
* @param id 支付ID
|
||||
* @return 支付对象
|
||||
*/
|
||||
Payment getPaymentById(int id);
|
||||
|
||||
/**
|
||||
* 根据订单ID获取支付记录
|
||||
* @param orderId 订单ID
|
||||
* @return 支付对象
|
||||
*/
|
||||
Payment getPaymentByOrderId(int orderId);
|
||||
|
||||
/**
|
||||
* 更新支付状态
|
||||
* @param id 支付ID
|
||||
* @param status 新状态
|
||||
* @param transactionId 交易ID
|
||||
* @return 是否更新成功
|
||||
*/
|
||||
boolean updatePaymentStatus(int id, String status, String transactionId);
|
||||
|
||||
/**
|
||||
* 获取所有支付记录
|
||||
* @return 所有支付记录列表
|
||||
*/
|
||||
List<Payment> getAllPayments();
|
||||
|
||||
/**
|
||||
* 根据用户ID获取支付记录列表
|
||||
* @param userId 用户ID
|
||||
* @return 支付记录列表
|
||||
*/
|
||||
List<Payment> getPaymentsByUserId(int userId);
|
||||
}
|
||||
@ -1,75 +0,0 @@
|
||||
package com.campus.delivery.dao;
|
||||
|
||||
import com.campus.delivery.entity.Payment;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
public class PaymentDAOImpl implements PaymentDAO {
|
||||
private static List<Payment> paymentList = new ArrayList<>();
|
||||
private static AtomicInteger nextId = new AtomicInteger(1);
|
||||
|
||||
@Override
|
||||
public int createPayment(Payment payment) {
|
||||
int id = nextId.getAndIncrement();
|
||||
payment.setId(id);
|
||||
paymentList.add(payment);
|
||||
return id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Payment getPaymentById(int id) {
|
||||
for (Payment payment : paymentList) {
|
||||
if (payment.getId() == id) {
|
||||
return payment;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Payment getPaymentByOrderId(int orderId) {
|
||||
for (Payment payment : paymentList) {
|
||||
if (payment.getOrderId() == orderId) {
|
||||
return payment;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean updatePaymentStatus(int id, String status, String transactionId) {
|
||||
Payment payment = getPaymentById(id);
|
||||
if (payment != null) {
|
||||
payment.setStatus(status);
|
||||
payment.setTransactionId(transactionId);
|
||||
if ("SUCCESS".equals(status)) {
|
||||
payment.setPaymentTime(new Date());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Payment> getAllPayments() {
|
||||
return new ArrayList<>(paymentList);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Payment> getPaymentsByUserId(int userId) {
|
||||
List<Payment> result = new ArrayList<>();
|
||||
OrderDAO orderDAO = new OrderDAOImpl();
|
||||
List<Order> userOrders = orderDAO.getOrdersByUserId(userId);
|
||||
|
||||
for (Order order : userOrders) {
|
||||
Payment payment = getPaymentByOrderId(order.getId());
|
||||
if (payment != null) {
|
||||
result.add(payment);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@ -1,125 +0,0 @@
|
||||
package com.campus.delivery.entity;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class Order {
|
||||
private int id;
|
||||
private int userId;
|
||||
private int deliverId;
|
||||
private String description;
|
||||
private double amount;
|
||||
private String status;
|
||||
private String paymentMethod;
|
||||
private Date createTime;
|
||||
private Date paymentTime;
|
||||
private Date finishTime;
|
||||
|
||||
public Order() {
|
||||
}
|
||||
|
||||
public Order(int userId, int deliverId, String description, double amount, String status) {
|
||||
this.userId = userId;
|
||||
this.deliverId = deliverId;
|
||||
this.description = description;
|
||||
this.amount = amount;
|
||||
this.status = status;
|
||||
this.createTime = new Date();
|
||||
}
|
||||
|
||||
// Getters and Setters
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public int getUserId() {
|
||||
return userId;
|
||||
}
|
||||
|
||||
public void setUserId(int userId) {
|
||||
this.userId = userId;
|
||||
}
|
||||
|
||||
public int getDeliverId() {
|
||||
return deliverId;
|
||||
}
|
||||
|
||||
public void setDeliverId(int deliverId) {
|
||||
this.deliverId = deliverId;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public double getAmount() {
|
||||
return amount;
|
||||
}
|
||||
|
||||
public void setAmount(double amount) {
|
||||
this.amount = amount;
|
||||
}
|
||||
|
||||
public String getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public void setStatus(String status) {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public String getPaymentMethod() {
|
||||
return paymentMethod;
|
||||
}
|
||||
|
||||
public void setPaymentMethod(String paymentMethod) {
|
||||
this.paymentMethod = paymentMethod;
|
||||
}
|
||||
|
||||
public Date getCreateTime() {
|
||||
return createTime;
|
||||
}
|
||||
|
||||
public void setCreateTime(Date createTime) {
|
||||
this.createTime = createTime;
|
||||
}
|
||||
|
||||
public Date getPaymentTime() {
|
||||
return paymentTime;
|
||||
}
|
||||
|
||||
public void setPaymentTime(Date paymentTime) {
|
||||
this.paymentTime = paymentTime;
|
||||
}
|
||||
|
||||
public Date getFinishTime() {
|
||||
return finishTime;
|
||||
}
|
||||
|
||||
public void setFinishTime(Date finishTime) {
|
||||
this.finishTime = finishTime;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Order{" +
|
||||
"id=" + id +
|
||||
", userId=" + userId +
|
||||
", deliverId=" + deliverId +
|
||||
", description='" + description + '\'' +
|
||||
", amount=" + amount +
|
||||
", status='" + status + '\'' +
|
||||
", paymentMethod='" + paymentMethod + '\'' +
|
||||
", createTime=" + createTime +
|
||||
", paymentTime=" + paymentTime +
|
||||
", finishTime=" + finishTime +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@ -1,93 +0,0 @@
|
||||
package com.campus.delivery.entity;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class Payment {
|
||||
private int id;
|
||||
private int orderId;
|
||||
private double amount;
|
||||
private String paymentMethod;
|
||||
private String transactionId;
|
||||
private String status;
|
||||
private Date paymentTime;
|
||||
|
||||
public Payment() {
|
||||
}
|
||||
|
||||
public Payment(int orderId, double amount, String paymentMethod) {
|
||||
this.orderId = orderId;
|
||||
this.amount = amount;
|
||||
this.paymentMethod = paymentMethod;
|
||||
this.status = "PENDING";
|
||||
}
|
||||
|
||||
// Getters and Setters
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public int getOrderId() {
|
||||
return orderId;
|
||||
}
|
||||
|
||||
public void setOrderId(int orderId) {
|
||||
this.orderId = orderId;
|
||||
}
|
||||
|
||||
public double getAmount() {
|
||||
return amount;
|
||||
}
|
||||
|
||||
public void setAmount(double amount) {
|
||||
this.amount = amount;
|
||||
}
|
||||
|
||||
public String getPaymentMethod() {
|
||||
return paymentMethod;
|
||||
}
|
||||
|
||||
public void setPaymentMethod(String paymentMethod) {
|
||||
this.paymentMethod = paymentMethod;
|
||||
}
|
||||
|
||||
public String getTransactionId() {
|
||||
return transactionId;
|
||||
}
|
||||
|
||||
public void setTransactionId(String transactionId) {
|
||||
this.transactionId = transactionId;
|
||||
}
|
||||
|
||||
public String getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public void setStatus(String status) {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public Date getPaymentTime() {
|
||||
return paymentTime;
|
||||
}
|
||||
|
||||
public void setPaymentTime(Date paymentTime) {
|
||||
this.paymentTime = paymentTime;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Payment{" +
|
||||
"id=" + id +
|
||||
", orderId=" + orderId +
|
||||
", amount=" + amount +
|
||||
", paymentMethod='" + paymentMethod + '\'' +
|
||||
", transactionId='" + transactionId + '\'' +
|
||||
", status='" + status + '\'' +
|
||||
", paymentTime=" + paymentTime +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@ -1,60 +0,0 @@
|
||||
package com.campus.delivery.service;
|
||||
|
||||
import com.campus.delivery.entity.Order;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface OrderService {
|
||||
/**
|
||||
* 创建新订单
|
||||
* @param userId 用户ID
|
||||
* @param deliverId 配送员ID
|
||||
* @param description 订单描述
|
||||
* @param amount 订单金额
|
||||
* @return 创建成功的订单ID
|
||||
*/
|
||||
int createOrder(int userId, int deliverId, String description, double amount);
|
||||
|
||||
/**
|
||||
* 获取订单详情
|
||||
* @param orderId 订单ID
|
||||
* @return 订单对象
|
||||
*/
|
||||
Order getOrderDetail(int orderId);
|
||||
|
||||
/**
|
||||
* 获取用户的所有订单
|
||||
* @param userId 用户ID
|
||||
* @return 订单列表
|
||||
*/
|
||||
List<Order> getUserOrders(int userId);
|
||||
|
||||
/**
|
||||
* 获取配送员的所有订单
|
||||
* @param deliverId 配送员ID
|
||||
* @return 订单列表
|
||||
*/
|
||||
List<Order> getDeliverOrders(int deliverId);
|
||||
|
||||
/**
|
||||
* 更新订单状态
|
||||
* @param orderId 订单ID
|
||||
* @param status 新状态
|
||||
* @return 是否更新成功
|
||||
*/
|
||||
boolean updateOrderStatus(int orderId, String status);
|
||||
|
||||
/**
|
||||
* 取消订单
|
||||
* @param orderId 订单ID
|
||||
* @return 是否取消成功
|
||||
*/
|
||||
boolean cancelOrder(int orderId);
|
||||
|
||||
/**
|
||||
* 完成订单
|
||||
* @param orderId 订单ID
|
||||
* @return 是否完成成功
|
||||
*/
|
||||
boolean completeOrder(int orderId);
|
||||
}
|
||||
@ -1,91 +0,0 @@
|
||||
package com.campus.delivery.service;
|
||||
|
||||
import com.campus.delivery.dao.OrderDAO;
|
||||
import com.campus.delivery.dao.OrderDAOImpl;
|
||||
import com.campus.delivery.entity.Order;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class OrderServiceImpl implements OrderService {
|
||||
private OrderDAO orderDAO;
|
||||
|
||||
public OrderServiceImpl() {
|
||||
this.orderDAO = new OrderDAOImpl();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int createOrder(int userId, int deliverId, String description, double amount) {
|
||||
if (amount <= 0) {
|
||||
throw new IllegalArgumentException("订单金额必须大于0");
|
||||
}
|
||||
if (description == null || description.trim().isEmpty()) {
|
||||
throw new IllegalArgumentException("订单描述不能为空");
|
||||
}
|
||||
|
||||
Order order = new Order(userId, deliverId, description, amount, "PENDING");
|
||||
return orderDAO.createOrder(order);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Order getOrderDetail(int orderId) {
|
||||
Order order = orderDAO.getOrderById(orderId);
|
||||
if (order == null) {
|
||||
throw new RuntimeException("订单不存在");
|
||||
}
|
||||
return order;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Order> getUserOrders(int userId) {
|
||||
return orderDAO.getOrdersByUserId(userId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Order> getDeliverOrders(int deliverId) {
|
||||
return orderDAO.getOrdersByDeliverId(deliverId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean updateOrderStatus(int orderId, String status) {
|
||||
validateOrderStatus(status);
|
||||
return orderDAO.updateOrderStatus(orderId, status);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean cancelOrder(int orderId) {
|
||||
Order order = orderDAO.getOrderById(orderId);
|
||||
if (order == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 只有待支付的订单可以取消
|
||||
if ("PENDING".equals(order.getStatus())) {
|
||||
return orderDAO.updateOrderStatus(orderId, "CANCELLED");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean completeOrder(int orderId) {
|
||||
Order order = orderDAO.getOrderById(orderId);
|
||||
if (order == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 只有已支付的订单可以完成
|
||||
if ("PAID".equals(order.getStatus())) {
|
||||
return orderDAO.updateOrderStatus(orderId, "COMPLETED");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private void validateOrderStatus(String status) {
|
||||
if (status == null || !isValidStatus(status)) {
|
||||
throw new IllegalArgumentException("无效的订单状态");
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isValidStatus(String status) {
|
||||
return "PENDING".equals(status) || "PAID".equals(status) || "COMPLETED".equals(status) || "CANCELLED".equals(status);
|
||||
}
|
||||
}
|
||||
@ -1,52 +0,0 @@
|
||||
package com.campus.delivery.service;
|
||||
|
||||
import com.campus.delivery.entity.Payment;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface PaymentService {
|
||||
/**
|
||||
* 处理订单支付
|
||||
* @param orderId 订单ID
|
||||
* @param paymentMethod 支付方式
|
||||
* @return 支付记录ID
|
||||
*/
|
||||
int processPayment(int orderId, String paymentMethod);
|
||||
|
||||
/**
|
||||
* 获取支付详情
|
||||
* @param paymentId 支付ID
|
||||
* @return 支付对象
|
||||
*/
|
||||
Payment getPaymentDetail(int paymentId);
|
||||
|
||||
/**
|
||||
* 获取订单的支付记录
|
||||
* @param orderId 订单ID
|
||||
* @return 支付对象
|
||||
*/
|
||||
Payment getPaymentByOrderId(int orderId);
|
||||
|
||||
/**
|
||||
* 确认支付成功
|
||||
* @param paymentId 支付ID
|
||||
* @param transactionId 交易ID
|
||||
* @return 是否确认成功
|
||||
*/
|
||||
boolean confirmPaymentSuccess(int paymentId, String transactionId);
|
||||
|
||||
/**
|
||||
* 处理支付失败
|
||||
* @param paymentId 支付ID
|
||||
* @param reason 失败原因
|
||||
* @return 是否处理成功
|
||||
*/
|
||||
boolean handlePaymentFailure(int paymentId, String reason);
|
||||
|
||||
/**
|
||||
* 获取用户的所有支付记录
|
||||
* @param userId 用户ID
|
||||
* @return 支付记录列表
|
||||
*/
|
||||
List<Payment> getUserPayments(int userId);
|
||||
}
|
||||
@ -1,113 +0,0 @@
|
||||
package com.campus.delivery.service;
|
||||
|
||||
import com.campus.delivery.dao.OrderDAO;
|
||||
import com.campus.delivery.dao.OrderDAOImpl;
|
||||
import com.campus.delivery.dao.PaymentDAO;
|
||||
import com.campus.delivery.dao.PaymentDAOImpl;
|
||||
import com.campus.delivery.entity.Order;
|
||||
import com.campus.delivery.entity.Payment;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
public class PaymentServiceImpl implements PaymentService {
|
||||
private PaymentDAO paymentDAO;
|
||||
private OrderDAO orderDAO;
|
||||
|
||||
public PaymentServiceImpl() {
|
||||
this.paymentDAO = new PaymentDAOImpl();
|
||||
this.orderDAO = new OrderDAOImpl();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int processPayment(int orderId, String paymentMethod) {
|
||||
// 验证订单是否存在
|
||||
Order order = orderDAO.getOrderById(orderId);
|
||||
if (order == null) {
|
||||
throw new RuntimeException("订单不存在");
|
||||
}
|
||||
|
||||
// 验证订单状态
|
||||
if (!"PENDING".equals(order.getStatus())) {
|
||||
throw new RuntimeException("订单状态不允许支付");
|
||||
}
|
||||
|
||||
// 验证支付方式
|
||||
validatePaymentMethod(paymentMethod);
|
||||
|
||||
// 创建支付记录
|
||||
Payment payment = new Payment(orderId, order.getAmount(), paymentMethod);
|
||||
int paymentId = paymentDAO.createPayment(payment);
|
||||
|
||||
// 更新订单支付方式
|
||||
orderDAO.updateOrderPayment(orderId, paymentMethod);
|
||||
|
||||
return paymentId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Payment getPaymentDetail(int paymentId) {
|
||||
Payment payment = paymentDAO.getPaymentById(paymentId);
|
||||
if (payment == null) {
|
||||
throw new RuntimeException("支付记录不存在");
|
||||
}
|
||||
return payment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Payment getPaymentByOrderId(int orderId) {
|
||||
Payment payment = paymentDAO.getPaymentByOrderId(orderId);
|
||||
if (payment == null) {
|
||||
throw new RuntimeException("该订单没有支付记录");
|
||||
}
|
||||
return payment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean confirmPaymentSuccess(int paymentId, String transactionId) {
|
||||
// 获取支付记录
|
||||
Payment payment = paymentDAO.getPaymentById(paymentId);
|
||||
if (payment == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 更新支付状态
|
||||
boolean paymentUpdated = paymentDAO.updatePaymentStatus(paymentId, "SUCCESS", transactionId);
|
||||
if (paymentUpdated) {
|
||||
// 更新订单状态为已支付
|
||||
orderDAO.updateOrderStatus(payment.getOrderId(), "PAID");
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean handlePaymentFailure(int paymentId, String reason) {
|
||||
// 获取支付记录
|
||||
Payment payment = paymentDAO.getPaymentById(paymentId);
|
||||
if (payment == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 生成失败交易ID
|
||||
String failedTransactionId = "FAIL_" + UUID.randomUUID().toString();
|
||||
|
||||
// 更新支付状态为失败
|
||||
return paymentDAO.updatePaymentStatus(paymentId, "FAILED", failedTransactionId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Payment> getUserPayments(int userId) {
|
||||
return paymentDAO.getPaymentsByUserId(userId);
|
||||
}
|
||||
|
||||
private void validatePaymentMethod(String paymentMethod) {
|
||||
if (paymentMethod == null || !isValidPaymentMethod(paymentMethod)) {
|
||||
throw new IllegalArgumentException("无效的支付方式");
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isValidPaymentMethod(String paymentMethod) {
|
||||
return "WECHAT_PAY".equals(paymentMethod) || "ALIPAY".equals(paymentMethod) || "CASH".equals(paymentMethod);
|
||||
}
|
||||
}
|
||||
@ -1,266 +0,0 @@
|
||||
package com.campus.delivery.view;
|
||||
|
||||
import com.campus.delivery.controller.OrderController;
|
||||
import com.campus.delivery.controller.PaymentController;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
public class OrderPaymentView {
|
||||
private OrderController orderController;
|
||||
private PaymentController paymentController;
|
||||
private Scanner scanner;
|
||||
|
||||
public OrderPaymentView() {
|
||||
this.orderController = new OrderController();
|
||||
this.paymentController = new PaymentController();
|
||||
this.scanner = new Scanner(System.in);
|
||||
}
|
||||
|
||||
/**
|
||||
* 显示主菜单
|
||||
*/
|
||||
public void showMainMenu() {
|
||||
while (true) {
|
||||
System.out.println("\n==================================");
|
||||
System.out.println(" 校园代取系统 - 订单支付与管理");
|
||||
System.out.println("==================================");
|
||||
System.out.println("1. 订单管理");
|
||||
System.out.println("2. 支付管理");
|
||||
System.out.println("3. 退出系统");
|
||||
System.out.print("请选择操作(1-3): ");
|
||||
|
||||
int choice = scanner.nextInt();
|
||||
scanner.nextLine(); // 消耗换行符
|
||||
|
||||
switch (choice) {
|
||||
case 1:
|
||||
showOrderMenu();
|
||||
break;
|
||||
case 2:
|
||||
showPaymentMenu();
|
||||
break;
|
||||
case 3:
|
||||
System.out.println("感谢使用校园代取系统!");
|
||||
return;
|
||||
default:
|
||||
System.out.println("无效的选择,请重新输入!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 显示订单管理菜单
|
||||
*/
|
||||
private void showOrderMenu() {
|
||||
while (true) {
|
||||
System.out.println("\n==================================");
|
||||
System.out.println(" 订单管理菜单");
|
||||
System.out.println("==================================");
|
||||
System.out.println("1. 创建新订单");
|
||||
System.out.println("2. 查询订单详情");
|
||||
System.out.println("3. 查询用户订单列表");
|
||||
System.out.println("4. 查询配送员订单列表");
|
||||
System.out.println("5. 更新订单状态");
|
||||
System.out.println("6. 取消订单");
|
||||
System.out.println("7. 完成订单");
|
||||
System.out.println("8. 返回主菜单");
|
||||
System.out.print("请选择操作(1-8): ");
|
||||
|
||||
int choice = scanner.nextInt();
|
||||
scanner.nextLine(); // 消耗换行符
|
||||
|
||||
switch (choice) {
|
||||
case 1:
|
||||
createOrder();
|
||||
break;
|
||||
case 2:
|
||||
getOrderDetail();
|
||||
break;
|
||||
case 3:
|
||||
getUserOrders();
|
||||
break;
|
||||
case 4:
|
||||
getDeliverOrders();
|
||||
break;
|
||||
case 5:
|
||||
updateOrderStatus();
|
||||
break;
|
||||
case 6:
|
||||
cancelOrder();
|
||||
break;
|
||||
case 7:
|
||||
completeOrder();
|
||||
break;
|
||||
case 8:
|
||||
return;
|
||||
default:
|
||||
System.out.println("无效的选择,请重新输入!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 显示支付管理菜单
|
||||
*/
|
||||
private void showPaymentMenu() {
|
||||
while (true) {
|
||||
System.out.println("\n==================================");
|
||||
System.out.println(" 支付管理菜单");
|
||||
System.out.println("==================================");
|
||||
System.out.println("1. 发起订单支付");
|
||||
System.out.println("2. 查询支付详情");
|
||||
System.out.println("3. 查询订单支付记录");
|
||||
System.out.println("4. 确认支付成功");
|
||||
System.out.println("5. 处理支付失败");
|
||||
System.out.println("6. 查询用户支付记录");
|
||||
System.out.println("7. 返回主菜单");
|
||||
System.out.print("请选择操作(1-7): ");
|
||||
|
||||
int choice = scanner.nextInt();
|
||||
scanner.nextLine(); // 消耗换行符
|
||||
|
||||
switch (choice) {
|
||||
case 1:
|
||||
processPayment();
|
||||
break;
|
||||
case 2:
|
||||
getPaymentDetail();
|
||||
break;
|
||||
case 3:
|
||||
getPaymentByOrderId();
|
||||
break;
|
||||
case 4:
|
||||
confirmPaymentSuccess();
|
||||
break;
|
||||
case 5:
|
||||
handlePaymentFailure();
|
||||
break;
|
||||
case 6:
|
||||
getUserPayments();
|
||||
break;
|
||||
case 7:
|
||||
return;
|
||||
default:
|
||||
System.out.println("无效的选择,请重新输入!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 订单管理功能实现
|
||||
private void createOrder() {
|
||||
System.out.print("请输入用户ID: ");
|
||||
int userId = scanner.nextInt();
|
||||
System.out.print("请输入配送员ID: ");
|
||||
int deliverId = scanner.nextInt();
|
||||
scanner.nextLine(); // 消耗换行符
|
||||
System.out.print("请输入订单描述: ");
|
||||
String description = scanner.nextLine();
|
||||
System.out.print("请输入订单金额: ");
|
||||
double amount = scanner.nextDouble();
|
||||
|
||||
String result = orderController.createOrder(userId, deliverId, description, amount);
|
||||
System.out.println(result);
|
||||
}
|
||||
|
||||
private void getOrderDetail() {
|
||||
System.out.print("请输入订单ID: ");
|
||||
int orderId = scanner.nextInt();
|
||||
String result = orderController.getOrderDetail(orderId);
|
||||
System.out.println(result);
|
||||
}
|
||||
|
||||
private void getUserOrders() {
|
||||
System.out.print("请输入用户ID: ");
|
||||
int userId = scanner.nextInt();
|
||||
String result = orderController.getUserOrders(userId);
|
||||
System.out.println(result);
|
||||
}
|
||||
|
||||
private void getDeliverOrders() {
|
||||
System.out.print("请输入配送员ID: ");
|
||||
int deliverId = scanner.nextInt();
|
||||
String result = orderController.getDeliverOrders(deliverId);
|
||||
System.out.println(result);
|
||||
}
|
||||
|
||||
private void updateOrderStatus() {
|
||||
System.out.print("请输入订单ID: ");
|
||||
int orderId = scanner.nextInt();
|
||||
scanner.nextLine(); // 消耗换行符
|
||||
System.out.print("请输入新状态(PENDING/PAID/COMPLETED/CANCELLED): ");
|
||||
String status = scanner.nextLine().toUpperCase();
|
||||
String result = orderController.updateOrderStatus(orderId, status);
|
||||
System.out.println(result);
|
||||
}
|
||||
|
||||
private void cancelOrder() {
|
||||
System.out.print("请输入订单ID: ");
|
||||
int orderId = scanner.nextInt();
|
||||
String result = orderController.cancelOrder(orderId);
|
||||
System.out.println(result);
|
||||
}
|
||||
|
||||
private void completeOrder() {
|
||||
System.out.print("请输入订单ID: ");
|
||||
int orderId = scanner.nextInt();
|
||||
String result = orderController.completeOrder(orderId);
|
||||
System.out.println(result);
|
||||
}
|
||||
|
||||
// 支付管理功能实现
|
||||
private void processPayment() {
|
||||
System.out.print("请输入订单ID: ");
|
||||
int orderId = scanner.nextInt();
|
||||
scanner.nextLine(); // 消耗换行符
|
||||
System.out.print("请选择支付方式(WECHAT_PAY/ALIPAY/CASH): ");
|
||||
String paymentMethod = scanner.nextLine().toUpperCase();
|
||||
String result = paymentController.processPayment(orderId, paymentMethod);
|
||||
System.out.println(result);
|
||||
}
|
||||
|
||||
private void getPaymentDetail() {
|
||||
System.out.print("请输入支付ID: ");
|
||||
int paymentId = scanner.nextInt();
|
||||
String result = paymentController.getPaymentDetail(paymentId);
|
||||
System.out.println(result);
|
||||
}
|
||||
|
||||
private void getPaymentByOrderId() {
|
||||
System.out.print("请输入订单ID: ");
|
||||
int orderId = scanner.nextInt();
|
||||
String result = paymentController.getPaymentByOrderId(orderId);
|
||||
System.out.println(result);
|
||||
}
|
||||
|
||||
private void confirmPaymentSuccess() {
|
||||
System.out.print("请输入支付ID: ");
|
||||
int paymentId = scanner.nextInt();
|
||||
String result = paymentController.confirmPaymentSuccess(paymentId);
|
||||
System.out.println(result);
|
||||
}
|
||||
|
||||
private void handlePaymentFailure() {
|
||||
System.out.print("请输入支付ID: ");
|
||||
int paymentId = scanner.nextInt();
|
||||
scanner.nextLine(); // 消耗换行符
|
||||
System.out.print("请输入失败原因: ");
|
||||
String reason = scanner.nextLine();
|
||||
String result = paymentController.handlePaymentFailure(paymentId, reason);
|
||||
System.out.println(result);
|
||||
}
|
||||
|
||||
private void getUserPayments() {
|
||||
System.out.print("请输入用户ID: ");
|
||||
int userId = scanner.nextInt();
|
||||
String result = paymentController.getUserPayments(userId);
|
||||
System.out.println(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 主入口方法
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
OrderPaymentView view = new OrderPaymentView();
|
||||
view.showMainMenu();
|
||||
}
|
||||
}
|
||||
@ -1,11 +0,0 @@
|
||||
com\campus\delivery\entity\Payment.class
|
||||
com\campus\delivery\view\OrderPaymentView.class
|
||||
com\campus\delivery\dao\PaymentDAO.class
|
||||
com\campus\delivery\dao\OrderDAO.class
|
||||
com\campus\delivery\controller\OrderController.class
|
||||
com\campus\delivery\entity\Order.class
|
||||
com\campus\delivery\service\OrderService.class
|
||||
com\campus\delivery\controller\PaymentController.class
|
||||
com\campus\delivery\service\OrderServiceImpl.class
|
||||
com\campus\delivery\service\PaymentService.class
|
||||
com\campus\delivery\service\PaymentServiceImpl.class
|
||||
@ -1,13 +0,0 @@
|
||||
C:\Users\11343\Desktop\实验四\src\main\java\com\campus\delivery\service\OrderService.java
|
||||
C:\Users\11343\Desktop\实验四\src\main\java\com\campus\delivery\service\OrderServiceImpl.java
|
||||
C:\Users\11343\Desktop\实验四\src\main\java\com\campus\delivery\entity\Payment.java
|
||||
C:\Users\11343\Desktop\实验四\src\main\java\com\campus\delivery\view\OrderPaymentView.java
|
||||
C:\Users\11343\Desktop\实验四\src\main\java\com\campus\delivery\controller\PaymentController.java
|
||||
C:\Users\11343\Desktop\实验四\src\main\java\com\campus\delivery\service\PaymentService.java
|
||||
C:\Users\11343\Desktop\实验四\src\main\java\com\campus\delivery\controller\OrderController.java
|
||||
C:\Users\11343\Desktop\实验四\src\main\java\com\campus\delivery\entity\Order.java
|
||||
C:\Users\11343\Desktop\实验四\src\main\java\com\campus\delivery\dao\OrderDAO.java
|
||||
C:\Users\11343\Desktop\实验四\src\main\java\com\campus\delivery\dao\PaymentDAO.java
|
||||
C:\Users\11343\Desktop\实验四\src\main\java\com\campus\delivery\dao\OrderDAOImpl.java
|
||||
C:\Users\11343\Desktop\实验四\src\main\java\com\campus\delivery\dao\PaymentDAOImpl.java
|
||||
C:\Users\11343\Desktop\实验四\src\main\java\com\campus\delivery\service\PaymentServiceImpl.java
|
||||
Binary file not shown.
File diff suppressed because it is too large
Load Diff
Binary file not shown.
Binary file not shown.
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
|
Before Width: | Height: | Size: 225 KiB |
|
Before Width: | Height: | Size: 73 KiB |
|
Before Width: | Height: | Size: 31 KiB |
Loading…
Reference in new issue