feat: 添加调试日志并改进数据库处理逻辑
- 在多个文件中添加调试日志记录功能 - 将数据库连接统一迁移到db_connect.php - 改进预约时间冲突检测逻辑 - 优化VIP客户数据处理 - 增强套餐查询的健壮性 - 更新预约状态处理流程
This commit is contained in:
+71
-74
@@ -1,12 +1,9 @@
|
||||
<?php
|
||||
// 配置数据库连接参数
|
||||
$servername = "localhost";
|
||||
$username = "root";
|
||||
$password = "";
|
||||
$dbname = "car_wash_db";
|
||||
// 加载数据库配置
|
||||
require_once 'db_connect.php';
|
||||
|
||||
// 设置响应头为JSON
|
||||
header('Content-Type: application/json');
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
|
||||
// 获取请求参数
|
||||
$phone = $_GET['phone'] ?? '';
|
||||
@@ -20,82 +17,82 @@ if (empty($phone)) {
|
||||
'pending' => 0,
|
||||
'completed' => 0,
|
||||
'bookings' => []
|
||||
]);
|
||||
], JSON_UNESCAPED_UNICODE);
|
||||
exit;
|
||||
}
|
||||
|
||||
// 创建数据库连接
|
||||
$conn = new mysqli($servername, $username, $password, $dbname);
|
||||
try {
|
||||
// 准备SQL查询 - 查询用户的预约记录(使用新的数据库结构)
|
||||
$sql = "SELECT b.*, p.package_name
|
||||
FROM bookings b
|
||||
LEFT JOIN packages p ON b.package_id = p.id
|
||||
WHERE b.phone = ?
|
||||
ORDER BY b.start_time DESC, b.id DESC";
|
||||
|
||||
// 检查连接
|
||||
if ($conn->connect_error) {
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->execute([$phone]);
|
||||
$results = $stmt->fetchAll();
|
||||
|
||||
$bookings = [];
|
||||
$pending = 0;
|
||||
$completed = 0;
|
||||
|
||||
// 处理查询结果
|
||||
foreach ($results as $row) {
|
||||
// 格式化日期和时间
|
||||
$booking_date = $row['start_time'] ? date('Y-m-d', strtotime($row['start_time'])) : '';
|
||||
$time_slot = '';
|
||||
if ($row['start_time'] && $row['end_time']) {
|
||||
$start_time = date('H:i', strtotime($row['start_time']));
|
||||
$end_time = date('H:i', strtotime($row['end_time']));
|
||||
$time_slot = $start_time . '-' . $end_time;
|
||||
}
|
||||
|
||||
$booking = [
|
||||
'id' => $row['id'],
|
||||
'booking_date' => $booking_date,
|
||||
'phone' => $row['phone'],
|
||||
'car_number' => $row['car_number'] ?? '',
|
||||
'car_model' => $row['car_model'] ?? '',
|
||||
'time_slot' => $time_slot,
|
||||
'status' => $row['status'] ?? '未知',
|
||||
'package_name' => $row['package_name'] ?? '',
|
||||
'notes' => $row['notes'] ?? ''
|
||||
];
|
||||
|
||||
// 统计不同状态的预约数量
|
||||
if ($booking['status'] === '待确认' || $booking['status'] === '已确认' || $booking['status'] === '进行中') {
|
||||
$pending++;
|
||||
} elseif ($booking['status'] === '已完成') {
|
||||
$completed++;
|
||||
}
|
||||
|
||||
$bookings[] = $booking;
|
||||
}
|
||||
|
||||
// 计算总预约数
|
||||
$total = count($bookings);
|
||||
|
||||
// 构建响应数据
|
||||
$response = [
|
||||
'success' => true,
|
||||
'total' => $total,
|
||||
'pending' => $pending,
|
||||
'completed' => $completed,
|
||||
'bookings' => $bookings
|
||||
];
|
||||
|
||||
// 返回JSON数据
|
||||
echo json_encode($response, JSON_UNESCAPED_UNICODE);
|
||||
|
||||
} catch (Exception $e) {
|
||||
echo json_encode([
|
||||
'success' => false,
|
||||
'message' => '数据库连接失败',
|
||||
'message' => '查询失败:' . $e->getMessage(),
|
||||
'total' => 0,
|
||||
'pending' => 0,
|
||||
'completed' => 0,
|
||||
'bookings' => []
|
||||
]);
|
||||
exit;
|
||||
], JSON_UNESCAPED_UNICODE);
|
||||
}
|
||||
|
||||
// 准备SQL查询 - 查询用户的预约记录
|
||||
// 假设预约表名为bookings,套餐表名为packages
|
||||
$sql = "SELECT b.*, p.package_name
|
||||
FROM bookings b
|
||||
LEFT JOIN packages p ON b.package_id = p.id
|
||||
WHERE b.phone = ?
|
||||
ORDER BY b.booking_date DESC, b.id DESC";
|
||||
|
||||
$stmt = $conn->prepare($sql);
|
||||
$stmt->bind_param("s", $phone);
|
||||
$stmt->execute();
|
||||
$result = $stmt->get_result();
|
||||
|
||||
$bookings = [];
|
||||
$pending = 0;
|
||||
$completed = 0;
|
||||
|
||||
// 处理查询结果
|
||||
while ($row = $result->fetch_assoc()) {
|
||||
$booking = [
|
||||
'id' => $row['id'],
|
||||
'booking_date' => $row['booking_date'],
|
||||
'phone' => $row['phone'],
|
||||
'car_number' => $row['car_number'] ?? '',
|
||||
'car_model' => $row['car_model'] ?? '',
|
||||
'time_slot' => $row['time_slot'] ?? '',
|
||||
'status' => $row['status'] ?? '未知',
|
||||
'package_name' => $row['package_name'] ?? '',
|
||||
'notes' => $row['notes'] ?? ''
|
||||
];
|
||||
|
||||
// 统计不同状态的预约数量
|
||||
if ($booking['status'] === '待服务') {
|
||||
$pending++;
|
||||
} elseif ($booking['status'] === '已完成') {
|
||||
$completed++;
|
||||
}
|
||||
|
||||
$bookings[] = $booking;
|
||||
}
|
||||
|
||||
// 计算总预约数
|
||||
$total = count($bookings);
|
||||
|
||||
// 构建响应数据
|
||||
$response = [
|
||||
'success' => true,
|
||||
'total' => $total,
|
||||
'pending' => $pending,
|
||||
'completed' => $completed,
|
||||
'bookings' => $bookings
|
||||
];
|
||||
|
||||
// 返回JSON数据
|
||||
echo json_encode($response);
|
||||
|
||||
// 关闭数据库连接
|
||||
$stmt->close();
|
||||
$conn->close();
|
||||
?>
|
||||
|
||||
Reference in New Issue
Block a user