-- ============================================================
-- V10: 添加广场功能（用户作品展示与分享）
-- 描述: 用户可以将AI生成的作品发布到广场，支持按类型查询、点赞、浏览统计
-- 作者: 1818AI  
-- 日期: 2025-10-26
-- ============================================================

USE `1818ai`;

-- ============================================================
-- 1. 创建广场作品表
-- ============================================================

CREATE TABLE IF NOT EXISTS `plaza_work` (
  `id` BIGINT NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `work_no` VARCHAR(50) NOT NULL COMMENT '作品编号（唯一标识）',
  `user_id` BIGINT NOT NULL COMMENT '发布者用户ID',
  `task_no` VARCHAR(50) NOT NULL COMMENT '关联的任务编号',
  `task_type` VARCHAR(50) NOT NULL COMMENT '任务类型：text_to_image/image_to_image/text_to_video/image_to_video等',
  `model_name` VARCHAR(100) NOT NULL COMMENT '使用的模型名称',
  `prompt` TEXT NOT NULL COMMENT '生成提示词',
  `result_url` VARCHAR(500) NOT NULL COMMENT '作品结果URL（图片或视频）',
  `image_url` VARCHAR(500) DEFAULT NULL COMMENT '参考图URL（图生图/图生视频任务使用）',
  `aspect_ratio` VARCHAR(20) DEFAULT NULL COMMENT '宽高比：1:1/2:3/3:2/9:16/16:9等',
  
  `title` VARCHAR(200) DEFAULT NULL COMMENT '作品标题（可选）',
  `description` TEXT DEFAULT NULL COMMENT '作品描述（可选）',
  `tags` VARCHAR(500) DEFAULT NULL COMMENT '标签（JSON数组字符串）',
  
  `view_count` INT DEFAULT 0 COMMENT '浏览次数',
  `like_count` INT DEFAULT 0 COMMENT '点赞数',
  `share_count` INT DEFAULT 0 COMMENT '分享数',
  `comment_count` INT DEFAULT 0 COMMENT '评论数（预留）',
  
  `is_public` TINYINT(1) DEFAULT 1 COMMENT '是否公开：0-仅自己可见，1-公开',
  `status` VARCHAR(20) DEFAULT 'published' COMMENT '状态：draft-草稿，published-已发布，hidden-已隐藏',
  
  `create_time` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  `is_deleted` TINYINT(1) DEFAULT 0 COMMENT '是否删除：0-未删除，1-已删除',
  
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_work_no` (`work_no`),
  KEY `idx_user_id` (`user_id`),
  KEY `idx_task_no` (`task_no`),
  KEY `idx_task_type` (`task_type`),
  KEY `idx_create_time` (`create_time`),
  KEY `idx_like_count` (`like_count`),
  KEY `idx_status_public` (`status`, `is_public`, `is_deleted`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='广场作品表';

-- ============================================================
-- 2. 创建点赞表
-- ============================================================

CREATE TABLE IF NOT EXISTS `plaza_work_like` (
  `id` BIGINT NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `work_id` BIGINT NOT NULL COMMENT '作品ID',
  `user_id` BIGINT NOT NULL COMMENT '点赞用户ID',
  `create_time` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '点赞时间',
  
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_work_user` (`work_id`, `user_id`),
  KEY `idx_user_id` (`user_id`),
  KEY `idx_create_time` (`create_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='广场作品点赞表';

-- ============================================================
-- 3. 创建浏览记录表（可选，用于统计）
-- ============================================================

CREATE TABLE IF NOT EXISTS `plaza_work_view` (
  `id` BIGINT NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `work_id` BIGINT NOT NULL COMMENT '作品ID',
  `user_id` BIGINT DEFAULT NULL COMMENT '浏览用户ID（可为空，支持匿名浏览）',
  `ip_address` VARCHAR(50) DEFAULT NULL COMMENT 'IP地址',
  `user_agent` VARCHAR(500) DEFAULT NULL COMMENT '用户代理',
  `view_time` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '浏览时间',
  
  PRIMARY KEY (`id`),
  KEY `idx_work_id` (`work_id`),
  KEY `idx_user_id` (`user_id`),
  KEY `idx_view_time` (`view_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='广场作品浏览记录表';

-- ============================================================
-- 4. 插入示例数据（可选）
-- ============================================================

-- 假设用户ID 17563793187762127 发布了几个作品
-- INSERT INTO `plaza_work` (`work_no`, `user_id`, `task_no`, `task_type`, `model_name`, `prompt`, `result_url`, `title`, `tags`, `is_public`) VALUES
-- ('WORK-20251026-001', 17563793187762127, 'TASK-20251026183750127-8554', 'image_to_video', 'sc_sora2_img_landscape_15s_small', '根据参考图生成战场短视频', 'https://oss-1818ai-user-img.oss-cn-hangzhou.aliyuncs.com/result.mp4', '战场气氛短视频', '["视频","战争","特效"]', 1),
-- ('WORK-20251026-002', 17563793187762127, 'TASK-20251026120000000-0001', 'text_to_image', 'sc_soraimg_text_1x1', '一只可爱的橘猫在窗台晒太阳', 'https://oss-1818ai-user-img.oss-cn-hangzhou.aliyuncs.com/cat.png', '窗台上的橘猫', '["猫咪","温馨","治愈"]', 1);

-- ============================================================
-- 5. 添加索引优化查询性能
-- ============================================================

-- 复合索引：按任务类型和创建时间查询热门作品
CREATE INDEX `idx_type_like_time` ON `plaza_work`(`task_type`, `like_count` DESC, `create_time` DESC);

-- 复合索引：按状态和公开性查询
CREATE INDEX `idx_status_public_time` ON `plaza_work`(`status`, `is_public`, `create_time` DESC);

-- ============================================================
-- 6. 创建视图：热门作品
-- ============================================================

CREATE OR REPLACE VIEW `v_plaza_hot_works` AS
SELECT 
    pw.id,
    pw.work_no,
    pw.user_id,
    pw.task_type,
    pw.model_name,
    pw.title,
    pw.result_url,
    pw.like_count,
    pw.view_count,
    pw.create_time,
    u.nickname AS user_nickname,
    u.avatar_url AS user_avatar
FROM plaza_work pw
LEFT JOIN user u ON pw.user_id = u.id
WHERE pw.status = 'published' 
  AND pw.is_public = 1 
  AND pw.is_deleted = 0
ORDER BY pw.like_count DESC, pw.create_time DESC;

-- ============================================================
-- 7. 创建视图：最新作品
-- ============================================================

CREATE OR REPLACE VIEW `v_plaza_latest_works` AS
SELECT 
    pw.id,
    pw.work_no,
    pw.user_id,
    pw.task_type,
    pw.model_name,
    pw.title,
    pw.result_url,
    pw.like_count,
    pw.view_count,
    pw.create_time,
    u.nickname AS user_nickname,
    u.avatar_url AS user_avatar
FROM plaza_work pw
LEFT JOIN user u ON pw.user_id = u.id
WHERE pw.status = 'published' 
  AND pw.is_public = 1 
  AND pw.is_deleted = 0
ORDER BY pw.create_time DESC;

-- ============================================================
-- 验证表结构
-- ============================================================

SHOW CREATE TABLE plaza_work;
SHOW CREATE TABLE plaza_work_like;
SHOW CREATE TABLE plaza_work_view;

-- ============================================================
-- V10脚本结束
-- ============================================================

