-- 用户表
CREATE TABLE `user` (
                        `id` bigint NOT NULL AUTO_INCREMENT COMMENT '用户ID',
                        `username` varchar(50) NOT NULL COMMENT '用户名',
                        `password` varchar(100) NOT NULL COMMENT '密码',
                        `nickname` varchar(50) DEFAULT NULL COMMENT '昵称',
                        `avatar` varchar(255) DEFAULT NULL COMMENT '头像URL',
                        `gender` tinyint DEFAULT '0' COMMENT '性别：0-未知，1-男，2-女',
                        `email` varchar(100) DEFAULT NULL COMMENT '邮箱',
                        `phone` varchar(20) DEFAULT NULL COMMENT '手机号',
                        `birth_date` date DEFAULT NULL COMMENT '出生日期',
                        `introduction` varchar(500) DEFAULT NULL COMMENT '个人简介',
                        `role` tinyint DEFAULT '0' COMMENT '角色：0-普通用户，1-管理员',
                        `status` tinyint DEFAULT '1' COMMENT '状态：0-禁用，1-正常',
                        `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
                        `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
                        PRIMARY KEY (`id`),
                        UNIQUE KEY `uk_username` (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户表';

-- 音乐类型表
CREATE TABLE `music_type` (
                              `id` bigint NOT NULL AUTO_INCREMENT COMMENT '类型ID',
                              `name` varchar(50) NOT NULL COMMENT '类型名称',
                              `description` varchar(255) DEFAULT NULL COMMENT '类型描述',
                              `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
                              `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
                              PRIMARY KEY (`id`),
                              UNIQUE KEY `uk_name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='音乐类型表';

-- 歌手类型表
CREATE TABLE `singer_type` (
                               `id` bigint NOT NULL AUTO_INCREMENT COMMENT '类型ID',
                               `name` varchar(50) NOT NULL COMMENT '类型名称',
                               `description` varchar(255) DEFAULT NULL COMMENT '类型描述',
                               `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
                               `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
                               PRIMARY KEY (`id`),
                               UNIQUE KEY `uk_name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='歌手类型表';

-- 歌单类型表
CREATE TABLE `playlist_type` (
                                 `id` bigint NOT NULL AUTO_INCREMENT COMMENT '类型ID',
                                 `name` varchar(50) NOT NULL COMMENT '类型名称',
                                 `description` varchar(255) DEFAULT NULL COMMENT '类型描述',
                                 `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
                                 `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
                                 PRIMARY KEY (`id`),
                                 UNIQUE KEY `uk_name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='歌单类型表';

-- 歌手表
CREATE TABLE `singer` (
                          `id` bigint NOT NULL AUTO_INCREMENT COMMENT '歌手ID',
                          `name` varchar(100) NOT NULL COMMENT '歌手名称',
                          `avatar` varchar(255) DEFAULT NULL COMMENT '歌手头像URL',
                          `introduction` text COMMENT '歌手简介',
                          `type_id` bigint DEFAULT NULL COMMENT '歌手类型ID',
                          `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
                          `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
                          PRIMARY KEY (`id`),
                          KEY `idx_type_id` (`type_id`),
                          CONSTRAINT `fk_singer_type` FOREIGN KEY (`type_id`) REFERENCES `singer_type` (`id`) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='歌手表';

-- 音乐表
CREATE TABLE `music` (
                         `id` bigint NOT NULL AUTO_INCREMENT COMMENT '音乐ID',
                         `name` varchar(100) NOT NULL COMMENT '音乐名称',
                         `cover` varchar(255) DEFAULT NULL COMMENT '封面URL',
                         `url` varchar(255) NOT NULL COMMENT '音乐文件URL',
                         `lyric` text COMMENT '歌词',
                         `duration` int DEFAULT '0' COMMENT '时长(秒)',
                         `play_count` bigint DEFAULT '0' COMMENT '播放次数',
                         `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
                         `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
                         PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='音乐表';

-- 音乐-歌手关联表
CREATE TABLE `music_singer` (
                                `id` bigint NOT NULL AUTO_INCREMENT COMMENT 'ID',
                                `music_id` bigint NOT NULL COMMENT '音乐ID',
                                `singer_id` bigint NOT NULL COMMENT '歌手ID',
                                `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
                                PRIMARY KEY (`id`),
                                UNIQUE KEY `uk_music_singer` (`music_id`,`singer_id`),
                                KEY `idx_singer_id` (`singer_id`),
                                CONSTRAINT `fk_ms_music_id` FOREIGN KEY (`music_id`) REFERENCES `music` (`id`) ON DELETE CASCADE,
                                CONSTRAINT `fk_ms_singer_id` FOREIGN KEY (`singer_id`) REFERENCES `singer` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='音乐-歌手关联表';

-- 音乐-类型关联表
CREATE TABLE `music_type_relation` (
                                       `id` bigint NOT NULL AUTO_INCREMENT COMMENT 'ID',
                                       `music_id` bigint NOT NULL COMMENT '音乐ID',
                                       `type_id` bigint NOT NULL COMMENT '类型ID',
                                       `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
                                       PRIMARY KEY (`id`),
                                       UNIQUE KEY `uk_music_type` (`music_id`,`type_id`),
                                       KEY `idx_type_id` (`type_id`),
                                       CONSTRAINT `fk_mtr_music_id` FOREIGN KEY (`music_id`) REFERENCES `music` (`id`) ON DELETE CASCADE,
                                       CONSTRAINT `fk_mtr_type_id` FOREIGN KEY (`type_id`) REFERENCES `music_type` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='音乐-类型关联表';

-- 歌单表
CREATE TABLE `playlist` (
                            `id` bigint NOT NULL AUTO_INCREMENT COMMENT '歌单ID',
                            `name` varchar(100) NOT NULL COMMENT '歌单名称',
                            `cover` varchar(255) DEFAULT NULL COMMENT '封面URL',
                            `description` varchar(500) DEFAULT NULL COMMENT '歌单描述',
                            `creator_id` bigint DEFAULT NULL COMMENT '创建者ID',
                            `type_id` bigint DEFAULT NULL COMMENT '歌单类型ID',
                            `is_official` tinyint DEFAULT '0' COMMENT '是否官方歌单：0-否，1-是',
                            `play_count` bigint DEFAULT '0' COMMENT '播放次数',
                            `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
                            `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
                            PRIMARY KEY (`id`),
                            KEY `idx_creator_id` (`creator_id`),
                            KEY `idx_type_id` (`type_id`),
                            CONSTRAINT `fk_playlist_creator` FOREIGN KEY (`creator_id`) REFERENCES `user` (`id`) ON DELETE SET NULL,
                            CONSTRAINT `fk_playlist_type` FOREIGN KEY (`type_id`) REFERENCES `playlist_type` (`id`) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='歌单表';

-- 歌单-音乐关联表
CREATE TABLE `playlist_music` (
                                  `id` bigint NOT NULL AUTO_INCREMENT COMMENT 'ID',
                                  `playlist_id` bigint NOT NULL COMMENT '歌单ID',
                                  `music_id` bigint NOT NULL COMMENT '音乐ID',
                                  `order_num` int DEFAULT '0' COMMENT '排序号',
                                  `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
                                  PRIMARY KEY (`id`),
                                  UNIQUE KEY `uk_playlist_music` (`playlist_id`,`music_id`),
                                  KEY `idx_music_id` (`music_id`),
                                  CONSTRAINT `fk_pm_music_id` FOREIGN KEY (`music_id`) REFERENCES `music` (`id`) ON DELETE CASCADE,
                                  CONSTRAINT `fk_pm_playlist_id` FOREIGN KEY (`playlist_id`) REFERENCES `playlist` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='歌单-音乐关联表';

-- 用户收藏音乐表
CREATE TABLE `user_collect_music` (
                                      `id` bigint NOT NULL AUTO_INCREMENT COMMENT 'ID',
                                      `user_id` bigint NOT NULL COMMENT '用户ID',
                                      `music_id` bigint NOT NULL COMMENT '音乐ID',
                                      `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
                                      PRIMARY KEY (`id`),
                                      UNIQUE KEY `uk_user_music` (`user_id`,`music_id`),
                                      KEY `idx_music_id` (`music_id`),
                                      CONSTRAINT `fk_ucm_music_id` FOREIGN KEY (`music_id`) REFERENCES `music` (`id`) ON DELETE CASCADE,
                                      CONSTRAINT `fk_ucm_user_id` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户收藏音乐表';

-- 用户收藏歌单表
CREATE TABLE `user_collect_playlist` (
                                         `id` bigint NOT NULL AUTO_INCREMENT COMMENT 'ID',
                                         `user_id` bigint NOT NULL COMMENT '用户ID',
                                         `playlist_id` bigint NOT NULL COMMENT '歌单ID',
                                         `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
                                         PRIMARY KEY (`id`),
                                         UNIQUE KEY `uk_user_playlist` (`user_id`,`playlist_id`),
                                         KEY `idx_playlist_id` (`playlist_id`),
                                         CONSTRAINT `fk_ucp_playlist_id` FOREIGN KEY (`playlist_id`) REFERENCES `playlist` (`id`) ON DELETE CASCADE,
                                         CONSTRAINT `fk_ucp_user_id` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户收藏歌单表';

-- 用户收藏歌手表
CREATE TABLE `user_collect_singer` (
                                       `id` bigint NOT NULL AUTO_INCREMENT COMMENT 'ID',
                                       `user_id` bigint NOT NULL COMMENT '用户ID',
                                       `singer_id` bigint NOT NULL COMMENT '歌手ID',
                                       `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
                                       PRIMARY KEY (`id`),
                                       UNIQUE KEY `uk_user_singer` (`user_id`,`singer_id`),
                                       KEY `idx_singer_id` (`singer_id`),
                                       CONSTRAINT `fk_ucs_singer_id` FOREIGN KEY (`singer_id`) REFERENCES `singer` (`id`) ON DELETE CASCADE,
                                       CONSTRAINT `fk_ucs_user_id` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户收藏歌手表';

-- 评论表
CREATE TABLE `comment` (
                           `id` bigint NOT NULL AUTO_INCREMENT COMMENT '评论ID',
                           `content` varchar(500) NOT NULL COMMENT '评论内容',
                           `user_id` bigint NOT NULL COMMENT '评论用户ID',
                           `target_id` bigint NOT NULL COMMENT '评论目标ID',
                           `target_type` tinyint NOT NULL COMMENT '评论目标类型：1-音乐，2-歌单，3-歌手',
                           `parent_id` bigint DEFAULT NULL COMMENT '父评论ID',
                           `status` tinyint DEFAULT '1' COMMENT '状态：0-禁用，1-正常',
                           `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
                           `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
                           PRIMARY KEY (`id`),
                           KEY `idx_user_id` (`user_id`),
                           KEY `idx_target` (`target_id`,`target_type`),
                           KEY `idx_parent_id` (`parent_id`),
                           CONSTRAINT `fk_comment_user` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='评论表';

-- 轮播图表
CREATE TABLE `banner` (
                          `id` bigint NOT NULL AUTO_INCREMENT COMMENT '轮播图ID',
                          `title` varchar(100) DEFAULT NULL COMMENT '标题',
                          `image_url` varchar(255) NOT NULL COMMENT '图片URL',
                          `link_url` varchar(255) DEFAULT NULL COMMENT '链接URL',
                          `target_id` bigint DEFAULT NULL COMMENT '目标ID',
                          `target_type` tinyint DEFAULT NULL COMMENT '目标类型：1-音乐，2-歌单，3-歌手',
                          `order_num` int DEFAULT '0' COMMENT '排序号',
                          `status` tinyint DEFAULT '1' COMMENT '状态：0-禁用，1-正常',
                          `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
                          `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
                          PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='轮播图表';

-- 播放历史表
CREATE TABLE `play_history` (
                                `id` bigint NOT NULL AUTO_INCREMENT COMMENT 'ID',
                                `user_id` bigint NOT NULL COMMENT '用户ID',
                                `music_id` bigint NOT NULL COMMENT '音乐ID',
                                `play_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '播放时间',
                                PRIMARY KEY (`id`),
                                KEY `idx_user_id` (`user_id`),
                                KEY `idx_music_id` (`music_id`),
                                CONSTRAINT `fk_ph_music_id` FOREIGN KEY (`music_id`) REFERENCES `music` (`id`) ON DELETE CASCADE,
                                CONSTRAINT `fk_ph_user_id` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='播放历史表';

-- 系统日志表
CREATE TABLE `system_log` (
                              `id` bigint NOT NULL AUTO_INCREMENT COMMENT '日志ID',
                              `user_id` bigint DEFAULT NULL COMMENT '用户ID',
                              `operation` varchar(50) DEFAULT NULL COMMENT '操作类型',
                              `method` varchar(100) DEFAULT NULL COMMENT '请求方法',
                              `params` varchar(2000) DEFAULT NULL COMMENT '请求参数',
                              `ip` varchar(50) DEFAULT NULL COMMENT 'IP地址',
                              `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
                              PRIMARY KEY (`id`),
                              KEY `idx_user_id` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='系统日志表';