USE travel_content;

-- 查询选题的完整信息，包括关联的景区、产品、风格和目标受众
SELECT 
    CONCAT('【', t.id, '】') AS '选题ID',
    t.title AS '选题标题',
    t.description AS '选题描述',
    CASE 
        WHEN t.status = 'published' THEN '已发布'
        WHEN t.status = 'draft' THEN '草稿'
        ELSE t.status
    END AS '状态',
    u.userName AS '创建者',
    s.name AS '关联景区',
    p.name AS '关联产品',
    CONCAT(p.originPrice, ' → ', p.realPrice) AS '价格(原价→实际)',
    cs.styleName AS '内容风格',
    GROUP_CONCAT(DISTINCT ta.audienceName SEPARATOR '、') AS '目标受众'
FROM 
    topics t
JOIN 
    user u ON t.userId = u.id
JOIN 
    topicScenic ts ON t.id = ts.topicId
JOIN 
    scenicSpot s ON ts.scenicId = s.id
JOIN 
    topicProduct tp ON t.id = tp.topicId
JOIN 
    product p ON tp.productId = p.id
JOIN 
    topicStyle tst ON t.id = tst.topicId
JOIN 
    contentStyle cs ON tst.styleId = cs.id
LEFT JOIN 
    topicAudience taud ON t.id = taud.topicId
LEFT JOIN 
    targetAudience ta ON taud.audienceId = ta.id
WHERE 
    t.isDelete = 0
GROUP BY 
    t.id, t.title, t.description, t.status, u.userName, s.name, p.name, p.originPrice, p.realPrice, cs.styleName
ORDER BY 
    t.status = 'published' DESC, t.id; 