-- MySQL 8.0 建表语句
-- 库中项目表 - 存储各种类型的库中项目，支持按年龄、性别、场景、语种等维度筛选
drop table if exists dh_library_item;
create table dh_library_item (
    `id` bigint not null auto_increment comment '主键ID',
    `is_custom` tinyint(1) default 0 comment '是否自定义：0-否，1-是',
    `user_id` varchar(64) not null comment '用户ID',
    `type` varchar(32) not null comment '类型',
    `name` varchar(128) comment '名称',
    `age` varchar(16) comment '年龄：youth-青年，middle-中年，old-老年',
    `gender` varchar(16) comment '性别：male-男性，female-女性',
    `scene` varchar(16) comment '场景：emotion-情感，podcast-播客，education-教育',
    `language` varchar(16) comment '语种：chinese-中文，english-英语',
    `source_url` varchar(512) not null comment '资源URL',
    `cover_url` varchar(512) comment '封面URL',
    `create_user` bigint not null comment '创建者',
    `create_time` datetime not null default current_timestamp comment '创建时间',
    `update_user` bigint comment '更新者',
    `update_time` datetime not null default current_timestamp on update current_timestamp comment '更新时间',
    `is_deleted` tinyint(1) not null default 0 comment '删除标记：0-未删除，1-已删除',
    primary key (`id`),
    -- 单列索引，用于快速筛选
    index idx_user_id (`user_id`),
    index idx_type (`type`),
    index idx_age (`age`),
    index idx_gender (`gender`),
    index idx_scene (`scene`),
    index idx_language (`language`),
    index idx_create_time (`create_time`),
    index idx_is_deleted (`is_deleted`),
    -- 联合索引，用于多条件查询优化
    index idx_user_type (`user_id`, `type`),
    index idx_age_gender_scene (`age`, `gender`, `scene`),
    index idx_gender_scene_language (`gender`, `scene`, `language`)
) engine=innodb default charset=utf8mb4 collate=utf8mb4_0900_ai_ci comment='库中项目表';

-- 示例数据插入语句
insert into dh_library_item (is_custom, user_id, type, name, age, gender, scene, language, source_url, cover_url, create_user) values
(0, '123456', 'voice', '青年男声-情感中文', 'youth', 'male', 'emotion', 'chinese', 'https://example.com/voice/youth-male-emotion-chinese.mp3', 'https://example.com/cover/youth-male.jpg', 1),
(0, '123456', 'voice', '青年女声-播客中文', 'youth', 'female', 'podcast', 'chinese', 'https://example.com/voice/youth-female-podcast-chinese.mp3', 'https://example.com/cover/youth-female.jpg', 1),
(0, '123456', 'voice', '中年男声-教育中文', 'middle', 'male', 'education', 'chinese', 'https://example.com/voice/middle-male-education-chinese.mp3', 'https://example.com/cover/middle-male.jpg', 1),
(0, '123456', 'voice', '中年女声-情感英文', 'middle', 'female', 'emotion', 'english', 'https://example.com/voice/middle-female-emotion-english.mp3', 'https://example.com/cover/middle-female.jpg', 1),
(0, '123456', 'voice', '老年男声-教育英文', 'old', 'male', 'education', 'english', 'https://example.com/voice/old-male-education-english.mp3', 'https://example.com/cover/old-male.jpg', 1),
(1, '789012', 'voice', '自定义声音-青年女声-中文', 'youth', 'female', 'podcast', 'chinese', 'https://example.com/voice/custom-youth-female-chinese.mp3', 'https://example.com/cover/custom.jpg', 2);

-- 查询示例
-- 1. 查询青年男性的中文资源
-- select * from dh_library_item where age = 'youth' and gender = 'male' and language = 'chinese' and is_deleted = 0;

-- 2. 查询情感场景的女声资源
-- select * from dh_library_item where scene = 'emotion' and gender = 'female' and is_deleted = 0;

-- 3. 按创建时间倒序查询自定义资源
-- select * from dh_library_item where is_custom = 1 and is_deleted = 0 order by create_time desc;