-- carwash_db.sql - 数据库创建脚本
CREATE DATABASE IF NOT EXISTS carwash_booking;
USE carwash_booking;

-- 创建VIP客户表
CREATE TABLE IF NOT EXISTS vip_customers (
    id INT AUTO_INCREMENT PRIMARY KEY,
    customer_name VARCHAR(100) NOT NULL COMMENT '客户姓名',
    phone VARCHAR(20) NOT NULL COMMENT '联系电话',
    car_model VARCHAR(50) COMMENT '车型',
    car_number VARCHAR(20) COMMENT '车牌号',
    email VARCHAR(100) COMMENT '邮箱地址',
    birthday DATE COMMENT '生日',
    notes TEXT COMMENT '备注信息',
    is_active BOOLEAN DEFAULT TRUE,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NULL
);

-- 更新VIP客户表的索引结构
-- 1. 移除phone字段的UNIQUE约束（如果存在）
ALTER TABLE vip_customers DROP INDEX IF EXISTS phone;

-- 2. 添加phone和car_number的复合唯一索引
ALTER TABLE vip_customers ADD UNIQUE INDEX idx_phone_car_number (phone, car_number);

-- 3. 验证索引添加成功
SHOW INDEX FROM vip_customers;

-- 创建套餐表
CREATE TABLE IF NOT EXISTS packages (
    id INT AUTO_INCREMENT PRIMARY KEY,
    package_name VARCHAR(100) NOT NULL,
    description TEXT,
    base_duration INT NOT NULL COMMENT '基础服务时长(分钟)',
    price DECIMAL(10,2) NOT NULL,
    services TEXT NOT NULL COMMENT '包含的服务项目(用逗号分隔)',
    is_active BOOLEAN DEFAULT TRUE,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NULL
);

-- 修改预约表支持时间段、会员类型和来源
DROP TABLE IF EXISTS bookings;
CREATE TABLE IF NOT EXISTS bookings (
    id INT AUTO_INCREMENT PRIMARY KEY,
    customer_name VARCHAR(100) NOT NULL,
    phone VARCHAR(20) NOT NULL,
    car_model VARCHAR(50) NOT NULL,
    car_number VARCHAR(20) NOT NULL,
    package_id INT,
    custom_services TEXT COMMENT '自定义服务内容',
    start_time DATETIME NOT NULL,
    end_time DATETIME NOT NULL,
    duration INT NOT NULL COMMENT '实际服务时长(分钟)',
    total_price DECIMAL(10,2) NOT NULL,
    notes TEXT,
    status ENUM('待确认', '已确认', '进行中', '已完成', '已取消') DEFAULT '待确认',
    member_type ENUM('普通客户', 'VIP会员') DEFAULT '普通客户' COMMENT '会员类型',
    source ENUM('抖音', '微信', '快手', '朋友介绍', '其他') DEFAULT '其他' COMMENT '客户来源',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NULL,
    FOREIGN KEY (package_id) REFERENCES packages(id) ON DELETE SET NULL
);

-- 创建WPS表单数据表
CREATE TABLE IF NOT EXISTS wps_form_submissions (
    id INT AUTO_INCREMENT PRIMARY KEY,
    rid VARCHAR(50) NOT NULL COMMENT '表单提交ID',
    form_id VARCHAR(50) NOT NULL COMMENT '表单ID',
    form_title VARCHAR(255) NOT NULL COMMENT '表单标题',
    creator_id VARCHAR(50) NOT NULL COMMENT '创建者ID',
    create_time DATETIME NOT NULL COMMENT '创建时间',
    update_time DATETIME NOT NULL COMMENT '更新时间',
    mobile VARCHAR(20) COMMENT '请输入手机号',
    name VARCHAR(255) COMMENT '怎么称呼您',
    license_plate VARCHAR(20) COMMENT '车牌号',
    date DATE COMMENT '日期',
    time_slot VARCHAR(20) COMMENT '时间段',
    car_type VARCHAR(50) COMMENT '车型',
    has_car_coat VARCHAR(10) COMMENT '是否有车衣',
    car_wash_habit VARCHAR(10) COMMENT '有无自己撸车习惯',
    car_wash_experience VARCHAR(50) COMMENT '撸车经验',
    wash_frequency VARCHAR(50) COMMENT '洗车频率',
    age_group VARCHAR(20) COMMENT '请选择年龄段',
    remarks TEXT COMMENT '备注内容',
    auto_number VARCHAR(50) COMMENT '自动编号',
    status VARCHAR(20) DEFAULT 'pending' COMMENT '状态',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT '记录创建时间',
    updated_at DATETIME NOT NULL COMMENT '记录更新时间',
    INDEX idx_rid (rid),
    INDEX idx_license_plate (license_plate),
    INDEX idx_date (date),
    INDEX idx_mobile (mobile)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- 插入示例套餐数据
INSERT INTO packages (package_name, description, base_duration, price, services) VALUES
('基础洗车', '基础外观清洗', 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, member_type, source) VALUES
('张三', '13800138001', '大众朗逸', '京A12345', 1, '2024-12-20 09:00:00', '2024-12-20 09:30:00', 30, 50.00, '第一次来', '普通客户', '抖音'),
('李四', '13800138002', '丰田凯美瑞', '京B67890', 2, '2024-12-20 10:30:00', '2024-12-20 12:00:00', 90, 150.00, '需要特别清洗内饰', 'VIP会员', '朋友介绍'),
('王五', '13800138003', '宝马X3', '沪C88888', 3, '2024-12-21 14:00:00', '2024-12-21 17:00:00', 180, 300.00, 'VIP客户，定期保养', 'VIP会员', '微信'),
('赵六', '13800138004', '本田雅阁', '粤A66666', 1, '2024-12-21 09:30:00', '2024-12-21 10:00:00', 30, 50.00, '快手看到广告来的', '普通客户', '快手');

-- 插入示例VIP客户数据
INSERT INTO vip_customers (customer_name, phone, car_model, car_number, email, birthday, notes) VALUES
('张总', '13900139001', '奔驰S500', '京V88888', 'zhang@example.com', '1980-05-15', '长期VIP客户，商务人士'),
('王女士', '13900139002', '奥迪A6L', '京A66666', 'wang@example.com', '1985-03-20', '女企业家，每月定期保养'),
('李先生', '13900139003', 'BMW X5', '沪B99999', 'li@example.com', '1978-11-10', '房地产公司老板，对服务要求较高');