-- Lane Carford AI系统基础架构数据库表结构
-- 创建数据库
CREATE DATABASE IF NOT EXISTS lanecarford CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
USE lanecarford;

-- 1. 导购表
CREATE TABLE sales (
    id BIGINT PRIMARY KEY AUTO_INCREMENT COMMENT '导购ID',
    username VARCHAR(50) NOT NULL UNIQUE COMMENT '用户名',
    password VARCHAR(255) NOT NULL COMMENT '密码(加密后)',
    real_name VARCHAR(100) NOT NULL COMMENT '真实姓名',
    employee_id VARCHAR(50) UNIQUE COMMENT '员工编号',
    store_id VARCHAR(50) COMMENT '门店ID',
    store_name VARCHAR(100) COMMENT '门店名称',
    phone VARCHAR(20) COMMENT '手机号',
    email VARCHAR(100) COMMENT '邮箱',
    is_active TINYINT DEFAULT 1 COMMENT '是否启用(0-禁用,1-启用)',
    created_time DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    updated_time DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    INDEX idx_username (username),
    INDEX idx_employee_id (employee_id),
    INDEX idx_store_id (store_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='导购表';

-- 2. 顾客表
CREATE TABLE customers (
    id BIGINT PRIMARY KEY AUTO_INCREMENT COMMENT '顾客ID',
    name VARCHAR(100) NOT NULL COMMENT '顾客姓名',
    email VARCHAR(100) NOT NULL COMMENT '顾客邮箱',
    phone VARCHAR(20) COMMENT '手机号',
    gender VARCHAR(10) COMMENT '性别',
    age_range VARCHAR(20) COMMENT '年龄段',
    created_time DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    updated_time DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    INDEX idx_email (email),
    INDEX idx_phone (phone),
    INDEX idx_name (name)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='顾客表';

-- 3. 进店记录表
CREATE TABLE visit_records (
    id BIGINT PRIMARY KEY AUTO_INCREMENT COMMENT '进店记录ID',
    customer_id BIGINT NOT NULL COMMENT '顾客ID',
    sales_id BIGINT NOT NULL COMMENT '导购ID',
    visit_date DATE NOT NULL COMMENT '进店日期',
    visit_time DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '进店时间',
    session_id VARCHAR(100) COMMENT '会话ID',
    status TINYINT DEFAULT 1 COMMENT '状态(0-已结束,1-进行中)',
    notes TEXT COMMENT '备注',
    created_time DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    updated_time DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    FOREIGN KEY (customer_id) REFERENCES customers(id) ON DELETE CASCADE,
    FOREIGN KEY (sales_id) REFERENCES sales(id) ON DELETE CASCADE,
    INDEX idx_customer_id (customer_id),
    INDEX idx_sales_id (sales_id),
    INDEX idx_visit_date (visit_date),
    INDEX idx_session_id (session_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='进店记录表';

-- 4. 风格配置表
CREATE TABLE styles (
    id BIGINT PRIMARY KEY AUTO_INCREMENT COMMENT '风格配置ID',
    customer_id BIGINT NOT NULL COMMENT '顾客ID',
    visit_record_id BIGINT NOT NULL COMMENT '进店记录ID',
    is_selected TINYINT DEFAULT 0 COMMENT '是否选中(0-未选中,1-已选中)',
    style_image_url VARCHAR(500) COMMENT '风格图片URL',
    python_request_id VARCHAR(100) COMMENT 'Python请求ID',
    generation_status TINYINT DEFAULT 0 COMMENT '生成状态(0-处理中,1-已完成,2-失败)',
    error_message TEXT COMMENT '错误信息',
    created_time DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    updated_time DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    FOREIGN KEY (customer_id) REFERENCES customers(id) ON DELETE CASCADE,
    FOREIGN KEY (visit_record_id) REFERENCES visit_records(id) ON DELETE CASCADE,
    INDEX idx_customer_id (customer_id),
    INDEX idx_visit_record_id (visit_record_id),
    INDEX idx_python_request_id (python_request_id),
    INDEX idx_is_selected (is_selected),
    INDEX idx_generation_status (generation_status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='风格配置表';

-- 5. 模特照片表
CREATE TABLE model_photos (
    id BIGINT PRIMARY KEY AUTO_INCREMENT COMMENT '模特照片ID',
    photo_url VARCHAR(500) NOT NULL COMMENT '模特照片URL',
    photo_name VARCHAR(200) COMMENT '照片名称',
    gender VARCHAR(10) NOT NULL COMMENT '性别',
    is_active TINYINT DEFAULT 1 COMMENT '是否启用(0-禁用,1-启用)',
    sort_order INT DEFAULT 0 COMMENT '排序权重',
    created_time DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    updated_time DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    INDEX idx_gender (gender),
    INDEX idx_is_active (is_active),
    INDEX idx_sort_order (sort_order)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='模特照片表';

-- 6. 顾客照片表
CREATE TABLE customer_photos (
    id BIGINT PRIMARY KEY AUTO_INCREMENT COMMENT '顾客照片ID',
    customer_id BIGINT NOT NULL COMMENT '顾客ID',
    visit_record_id BIGINT NOT NULL COMMENT '进店记录ID',
    photo_url VARCHAR(500) NOT NULL COMMENT '照片URL',
    is_primary TINYINT DEFAULT 0 COMMENT '是否为主照片(0-否,1-是)',
    upload_time DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '上传时间',
    created_time DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    FOREIGN KEY (customer_id) REFERENCES customers(id) ON DELETE CASCADE,
    FOREIGN KEY (visit_record_id) REFERENCES visit_records(id) ON DELETE CASCADE,
    INDEX idx_customer_id (customer_id),
    INDEX idx_visit_record_id (visit_record_id),
    INDEX idx_is_primary (is_primary)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='顾客照片表';



-- 8. 试穿效果表
CREATE TABLE try_on_effects (
    id BIGINT PRIMARY KEY AUTO_INCREMENT COMMENT '试穿效果ID',
    customer_id BIGINT NOT NULL COMMENT '顾客ID',
    visit_record_id BIGINT NOT NULL COMMENT '进店记录ID',
    styles_id BIGINT NOT NULL COMMENT '风格ID',
    model_photo_id BIGINT COMMENT '模特照片ID',
    customer_photo_id BIGINT COMMENT '顾客照片ID',
    prompt VARCHAR(500) COMMENT '提示词',
    original_try_on_id BIGINT COMMENT '原试穿效果ID,当is_regenerated为1时才会有值',
    is_regenerated TINYINT DEFAULT 0 COMMENT '是否由生成结果重新生成(0-否,1-是)',
    result_image_url VARCHAR(500) COMMENT '试穿结果图片URL',
    request_id VARCHAR(100) COMMENT '请求ID',
    generation_status VARCHAR(20) DEFAULT 'pending' COMMENT '生成状态(pending-等待中,processing-处理中,completed-已完成,failed-失败)',
    error_message TEXT COMMENT '错误信息',
    is_favorite TINYINT DEFAULT 0 COMMENT '是否喜欢的最终造型(0-否,1-是)',
    created_time DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    updated_time DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    FOREIGN KEY (customer_id) REFERENCES customers(id) ON DELETE CASCADE,
    FOREIGN KEY (visit_record_id) REFERENCES visit_records(id) ON DELETE CASCADE,
    FOREIGN KEY (styles_id) REFERENCES styles(id) ON DELETE CASCADE,
    FOREIGN KEY (model_photo_id) REFERENCES model_photos(id) ON DELETE SET NULL,
    FOREIGN KEY (customer_photo_id) REFERENCES customer_photos(id) ON DELETE SET NULL,
    INDEX idx_customer_id (customer_id),
    INDEX idx_visit_record_id (visit_record_id),
    INDEX idx_styles_id (styles_id),
    INDEX idx_request_id (request_id),
    INDEX idx_generation_status (generation_status),
    INDEX idx_is_favorite (is_favorite)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='试穿效果表';



