prepare("SELECT id FROM vip_customers WHERE phone = ? AND car_number = ?"); $stmt->execute([$phone, $car_number]); if ($stmt->fetch()) { throw new Exception('该手机号和车牌号组合已经是VIP客户'); } // 插入VIP客户 $stmt = $pdo->prepare("INSERT INTO vip_customers (customer_name, phone, car_model, car_number, email, birthday, notes) VALUES (?, ?, ?, ?, ?, ?, ?)"); try { $stmt->execute([$customer_name, $phone, $car_model, $car_number, $email, $birthday, $notes]); $success_message = "VIP客户录入成功!"; } catch (PDOException $e) { // 捕获唯一约束错误 if ($e->errorInfo[1] == 1062) { // MySQL唯一键约束错误码 throw new Exception('该手机号和车牌号组合已经是VIP客户'); } throw $e; // 重新抛出其他类型的异常 } } elseif ($action === 'update_vip') { $id = (int)$_POST['vip_id']; $customer_name = trim($_POST['customer_name']); $phone = trim($_POST['phone']); $car_model = trim($_POST['car_model']); $car_number = trim($_POST['car_number']); $email = trim($_POST['email'] ?? ''); $birthday = $_POST['birthday'] ?? ''; $notes = trim($_POST['notes'] ?? ''); if (empty($customer_name) || empty($phone)) { throw new Exception('请填写客户姓名和联系电话'); } // 检查手机号和车牌号组合是否与其他VIP客户冲突 $stmt = $pdo->prepare("SELECT id FROM vip_customers WHERE phone = ? AND car_number = ? AND id != ?"); $stmt->execute([$phone, $car_number, $id]); if ($stmt->fetch()) { throw new Exception('该手机号和车牌号组合已经被其他VIP客户使用'); } // 更新VIP客户信息 $stmt = $pdo->prepare("UPDATE vip_customers SET customer_name = ?, phone = ?, car_model = ?, car_number = ?, email = ?, birthday = ?, notes = ?, updated_at = NOW() WHERE id = ?"); try { $stmt->execute([$customer_name, $phone, $car_model, $car_number, $email, $birthday, $notes, $id]); $success_message = "VIP客户信息更新成功!"; } catch (PDOException $e) { // 捕获唯一约束错误 if ($e->errorInfo[1] == 1062) { // MySQL唯一键约束错误码 throw new Exception('该手机号和车牌号组合已经被其他VIP客户使用'); } throw $e; // 重新抛出其他类型的异常 } } elseif ($action === 'delete_vip') { $id = (int)$_POST['vip_id']; $stmt = $pdo->prepare("DELETE FROM vip_customers WHERE id = ?"); $stmt->execute([$id]); $success_message = "VIP客户删除成功!"; } elseif ($action === 'convert_from_booking') { // 从预约记录转换为VIP客户 $booking_id = isset($_POST['booking_id']) ? (int)$_POST['booking_id'] : 0; if ($booking_id <= 0) { throw new Exception('无效的预约ID'); } // 获取预约信息 $stmt = $pdo->prepare("SELECT customer_name, phone, car_model, car_number FROM bookings WHERE id = ?"); $stmt->execute([$booking_id]); $booking = $stmt->fetch(); if (!$booking) { throw new Exception('预约记录不存在'); } // 检查是否已经是VIP客户 $stmt = $pdo->prepare("SELECT id FROM vip_customers WHERE phone = ? AND car_number = ?"); $stmt->execute([$booking['phone'], $booking['car_number']]); if ($stmt->fetch()) { throw new Exception('该客户已经是VIP客户'); } // 插入VIP客户 $stmt = $pdo->prepare("INSERT INTO vip_customers (customer_name, phone, car_model, car_number, notes) VALUES (?, ?, ?, ?, ?)"); try { $notes = '从预约记录 #' . $booking_id . ' 自动转换'; $stmt->execute([ $booking['customer_name'], $booking['phone'], $booking['car_model'] ?? '', $booking['car_number'] ?? '', $notes ]); // 更新该客户的所有预约记录的member_type为VIP会员 $stmt = $pdo->prepare("UPDATE bookings SET member_type = 'VIP会员' WHERE phone = ? AND car_number = ?"); $stmt->execute([$booking['phone'], $booking['car_number']]); $success_message = "客户已成功转换为VIP客户!"; } catch (PDOException $e) { if ($e->errorInfo[1] == 1062) { throw new Exception('该客户已经是VIP客户'); } throw $e; } } } } catch (Exception $e) { $message = $e->getMessage(); } } // 获取搜索参数 $search_phone = isset($_GET['search_phone']) ? trim($_GET['search_phone']) : ''; $search_car_number = isset($_GET['search_car_number']) ? trim($_GET['search_car_number']) : ''; // 获取所有VIP客户(支持搜索) try { // 构建查询语句 $query = "SELECT * FROM vip_customers WHERE 1=1"; $params = []; if (!empty($search_phone)) { $query .= " AND phone LIKE :phone"; $params[':phone'] = '%' . $search_phone . '%'; } if (!empty($search_car_number)) { $query .= " AND car_number LIKE :car_number"; $params[':car_number'] = '%' . $search_car_number . '%'; } $query .= " ORDER BY created_at DESC"; $stmt = $pdo->prepare($query); $stmt->execute($params); $vip_customers = $stmt->fetchAll(); } catch (Exception $e) { $error_message = '获取VIP客户列表失败:' . $e->getMessage(); $vip_customers = []; } ?>
选择预约记录,快速将普通客户转换为VIP客户
prepare("SELECT DISTINCT b.id, b.customer_name, b.phone, b.car_model, b.car_number, COUNT(*) as booking_count, MAX(b.start_time) as last_booking_time FROM bookings b LEFT JOIN vip_customers v ON b.phone = v.phone AND b.car_number = v.car_number AND v.is_active = 1 WHERE b.member_type = '普通客户' AND v.id IS NULL AND b.start_time >= DATE_SUB(NOW(), INTERVAL 30 DAY) GROUP BY b.phone, b.car_number, b.customer_name, b.car_model ORDER BY last_booking_time DESC LIMIT 20"); $stmt->execute(); $convertible_bookings = $stmt->fetchAll(); } catch (Exception $e) { $convertible_bookings = []; } ?>