refactor(套餐服务): 将JSON存储改为逗号分隔字符串存储

修改数据库结构将services字段从JSON类型改为TEXT类型,使用逗号分隔存储服务项目
调整相关代码逻辑处理新的存储格式,包括数据插入、更新和展示
添加额外的trim处理确保服务项目数据整洁
This commit is contained in:
2025-11-19 01:13:45 +08:00
parent 8ffad04df3
commit 1999d75e32
3 changed files with 21 additions and 14 deletions
+4 -4
View File
@@ -9,7 +9,7 @@ CREATE TABLE IF NOT EXISTS packages (
description TEXT,
base_duration INT NOT NULL COMMENT '基础服务时长(分钟)',
price DECIMAL(10,2) NOT NULL,
services JSON NOT NULL COMMENT '包含的服务项目',
services TEXT NOT NULL COMMENT '包含的服务项目(用逗号分隔)',
is_active BOOLEAN DEFAULT TRUE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
@@ -38,9 +38,9 @@ CREATE TABLE IF NOT EXISTS bookings (
-- 插入示例套餐数据
INSERT INTO packages (package_name, description, base_duration, price, services) VALUES
('基础洗车', '基础外观清洗', 30, 50.00, '["外观冲洗", "泡沫清洁", "内饰吸尘"]'),
('精洗套餐', '全面深度清洗', 90, 150.00, '["外观精洗", "内饰深度清洁", "轮胎清洁", "打蜡"]'),
('VIP套餐', '顶级豪华洗护', 180, 300.00, '["全套精洗", "抛光打蜡", "内饰护理", "发动机清洁", "真皮护理"]');
('基础洗车', '基础外观清洗', 30, 50.00, '外观冲洗,泡沫清洁,内饰吸尘'),
('精洗套餐', '全面深度清洗', 90, 150.00, '外观精洗,内饰深度清洁,轮胎清洁,打蜡'),
('VIP套餐', '顶级豪华洗护', 180, 300.00, '全套精洗,抛光打蜡,内饰护理,发动机清洁,真皮护理');
-- 插入示例预约数据
INSERT INTO bookings (customer_name, phone, car_model, car_number, package_id, start_time, end_time, duration, total_price, notes) VALUES
+1 -1
View File
@@ -100,7 +100,7 @@ $booking_schedule = $stmt->fetchAll(PDO::FETCH_KEY_PAIR);
// 获取套餐信息用于JavaScript
$packages_json = json_encode(array_map(function($package) {
$package['services'] = json_decode($package['services'], true);
$package['services'] = array_filter(array_map('trim', explode(',', $package['services'])));
return $package;
}, $packages));
?>
+16 -9
View File
@@ -20,7 +20,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$description = trim($_POST['description']);
$base_duration = (int)$_POST['base_duration'];
$price = (float)$_POST['price'];
$services = json_encode(array_filter(array_map('trim', $_POST['services'] ?? [])));
$services = implode(',', array_filter(array_map('trim', $_POST['services'] ?? [])));
$stmt = $pdo->prepare("INSERT INTO packages (package_name, description, base_duration, price, services) VALUES (?, ?, ?, ?, ?)");
$stmt->execute([$package_name, $description, $base_duration, $price, $services]);
@@ -32,7 +32,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$description = trim($_POST['description']);
$base_duration = (int)$_POST['base_duration'];
$price = (float)$_POST['price'];
$services = json_encode(array_filter(array_map('trim', $_POST['services'] ?? [])));
$services = implode(',', array_filter(array_map('trim', $_POST['services'] ?? [])));
$is_active = isset($_POST['is_active']) ? 1 : 0;
$stmt = $pdo->prepare("UPDATE packages SET package_name = ?, description = ?, base_duration = ?, price = ?, services = ?, is_active = ? WHERE id = ?");
@@ -166,14 +166,16 @@ $packages = $stmt->fetchAll();
</div>
<?php
$services = json_decode($package['services'], true);
if ($services):
$services = explode(',', $package['services']);
if ($services && !empty(trim($services[0]))):
?>
<div class="package-services">
<span class="detail-label">包含服务:</span>
<div class="services-tags">
<?php foreach ($services as $service): ?>
<span class="service-tag"><?= htmlspecialchars($service) ?></span>
<?php if (trim($service)): ?>
<span class="service-tag"><?= htmlspecialchars(trim($service)) ?></span>
<?php endif; ?>
<?php endforeach; ?>
</div>
</div>
@@ -219,13 +221,18 @@ $packages = $stmt->fetchAll();
<label>服务项目:</label>
<div class="services-container">
<?php
$services = json_decode($package['services'], true) ?: [];
foreach ($services as $service): ?>
$services = explode(',', $package['services']);
foreach ($services as $service):
if (trim($service)):
?>
<div class="service-item">
<input type="text" name="services[]" value="<?= htmlspecialchars($service) ?>">
<input type="text" name="services[]" value="<?= htmlspecialchars(trim($service)) ?>">
<button type="button" class="btn-remove" onclick="removeService(this)">删除</button>
</div>
<?php endforeach; ?>
<?php
endif;
endforeach;
?>
</div>
<button type="button" class="btn-add" onclick="addService()">+ 添加服务项目</button>
</div>