89a22c7b11
在多个文件中添加了检查并创建日志目录的逻辑,防止因目录不存在而导致日志写入失败
96 lines
4.2 KiB
PHP
96 lines
4.2 KiB
PHP
<?php
|
|
// update_booking.php - 处理预约更新功能
|
|
require_once 'db_connect.php';
|
|
|
|
// 只接受POST请求
|
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
|
header('HTTP/1.1 405 Method Not Allowed');
|
|
echo json_encode(['status' => 'error', 'message' => '只允许POST请求']);
|
|
exit;
|
|
}
|
|
|
|
// 检查必要的参数
|
|
if (!isset($_POST['booking_id']) || !isset($_POST['action'])) {
|
|
echo json_encode(['status' => 'error', 'message' => '缺少必要参数']);
|
|
exit;
|
|
}
|
|
|
|
$booking_id = $_POST['booking_id'];
|
|
$action = $_POST['action'];
|
|
$response = ['status' => 'error', 'message' => '未知操作'];
|
|
|
|
try {
|
|
// 验证预约ID是否存在
|
|
$stmt = $pdo->prepare("SELECT id FROM bookings WHERE id = ?");
|
|
$stmt->execute([$booking_id]);
|
|
if ($stmt->rowCount() === 0) {
|
|
$response['message'] = '预约不存在';
|
|
echo json_encode($response);
|
|
exit;
|
|
}
|
|
|
|
if (in_array($action, ['已确认', '已完成', '已取消'])) {
|
|
// 更新预约状态
|
|
$stmt = $pdo->prepare("UPDATE bookings SET status = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ?");
|
|
$stmt->execute([$action, $booking_id]);
|
|
$response = ['status' => 'success', 'message' => '预约状态更新成功!'];
|
|
} elseif (in_array($action, ['已付款', '未付款'])) {
|
|
// 更新付款状态
|
|
$stmt = $pdo->prepare("UPDATE bookings SET payment_status = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ?");
|
|
$stmt->execute([$action, $booking_id]);
|
|
$response = ['status' => 'success', 'message' => '付款状态更新成功!'];
|
|
} elseif ($action == 'update_time' && isset($_POST['new_start_time']) && isset($_POST['new_end_time'])) {
|
|
// 更新预约时间
|
|
$new_start_time = $_POST['new_start_time'];
|
|
$new_end_time = $_POST['new_end_time'];
|
|
|
|
// 验证时间有效性
|
|
$start_dt = new DateTime($new_start_time);
|
|
$end_dt = new DateTime($new_end_time);
|
|
$now = new DateTime();
|
|
|
|
if ($start_dt < $now) {
|
|
$response['message'] = '开始时间不能早于当前时间';
|
|
echo json_encode($response);
|
|
exit;
|
|
}
|
|
|
|
if ($end_dt <= $start_dt) {
|
|
$response['message'] = '结束时间必须晚于开始时间';
|
|
echo json_encode($response);
|
|
exit;
|
|
}
|
|
|
|
// 计算新的持续时间(分钟)
|
|
// #region agent log
|
|
$log_data = json_encode(['location' => 'update_booking.php:65', 'message' => 'Calculating duration', 'data' => ['start_time' => $new_start_time, 'end_time' => $new_end_time], 'timestamp' => time() * 1000, 'sessionId' => 'debug-session', 'runId' => 'run1', 'hypothesisId' => 'C']);
|
|
// 确保日志目录存在
|
|
$log_dir = '.cursor';
|
|
if (!file_exists($log_dir)) {
|
|
mkdir($log_dir, 0777, true);
|
|
}
|
|
file_put_contents('.cursor/debug.log', $log_data . "\n", FILE_APPEND);
|
|
// #endregion
|
|
$interval = $start_dt->diff($end_dt);
|
|
$duration = ($interval->days * 24 * 60) + ($interval->h * 60) + $interval->i;
|
|
// #region agent log
|
|
$log_data = json_encode(['location' => 'update_booking.php:67', 'message' => 'Duration calculated', 'data' => ['duration' => $duration, 'days' => $interval->days, 'hours' => $interval->h, 'minutes' => $interval->i], 'timestamp' => time() * 1000, 'sessionId' => 'debug-session', 'runId' => 'run1', 'hypothesisId' => 'C']);
|
|
// 确保日志目录存在
|
|
$log_dir = '.cursor';
|
|
if (!file_exists($log_dir)) {
|
|
mkdir($log_dir, 0777, true);
|
|
}
|
|
file_put_contents('.cursor/debug.log', $log_data . "\n", FILE_APPEND);
|
|
// #endregion
|
|
|
|
// 更新预约时间和持续时间
|
|
$stmt = $pdo->prepare("UPDATE bookings SET start_time = ?, end_time = ?, duration = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ?");
|
|
$stmt->execute([$new_start_time, $new_end_time, $duration, $booking_id]);
|
|
$response = ['status' => 'success', 'message' => '预约时间更新成功!'];
|
|
}
|
|
} catch (Exception $e) {
|
|
$response['message'] = '操作失败:' . $e->getMessage();
|
|
}
|
|
|
|
echo json_encode($response);
|
|
?>
|