public class OrderService {
public Order createOrder(Long userId, Long productId, int quantity) {
// 快速失败:前置检查
if (userId == null) {
throw new IllegalArgumentException("用户ID不能为空");
}
if (productId == null) {
throw new IllegalArgumentException("商品ID不能为空");
}
if (quantity <= 0) {
throw new IllegalArgumentException("数量必须大于0");
}
// 检查库存
if (!inventoryService.checkStock(productId, quantity)) {
throw new InsufficientStockException("库存不足");
}
// 检查用户状态
User user = userService.getUser(userId);
if (!user.isActive()) {
throw new UserInactiveException("用户账号已禁用");
}
// 执行创建订单逻辑
return doCreateOrder(userId, productId, quantity);
}
}