<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zhentao.mapper.UserDynamicsMapper">

    <!-- 结果映射 -->
    <resultMap id="DynamicWithUserMap" type="com.zhentao.entity.UserDynamics">
        <id property="dynamicId" column="dynamic_id"/>
        <result property="userId" column="user_id"/>
        <result property="content" column="content"/>
        <result property="mediaUrls" column="media_urls"/>
        <result property="mediaType" column="media_type"/>
        <result property="auditStatus" column="audit_status"/>
        <result property="auditRemark" column="audit_remark"/>
        <result property="auditTime" column="audit_time"/>
        <result property="auditAdminId" column="audit_admin_id"/>
        <result property="likeCount" column="like_count"/>
        <result property="commentCount" column="comment_count"/>
        <result property="favoriteCount" column="favorite_count"/>
        <result property="shareCount" column="share_count"/>
        <result property="viewCount" column="view_count"/>
        <result property="status" column="status"/>
        <result property="visibility" column="visibility"/>
        <result property="createdAt" column="created_at"/>
        <result property="updatedAt" column="updated_at"/>
        <result property="isLiked" column="is_liked"/>
        <result property="isFavorited" column="is_favorited"/>
        
        <!-- 关联用户信息 -->
        <association property="user" javaType="com.zhentao.entity.Users">
            <id property="userId" column="user_id"/>
            <result property="nickname" column="nickname"/>
            <result property="avatarUrl" column="avatar_url"/>
            <result property="gender" column="gender"/>
        </association>
    </resultMap>

    <!-- 分页查询动态列表（关联用户信息） -->
    <select id="selectDynamicsWithUserInfo" resultMap="DynamicWithUserMap">
        SELECT 
            d.dynamic_id,
            d.user_id,
            d.content,
            CAST(d.media_urls AS CHAR) as media_urls,
            d.media_type,
            d.audit_status,
            d.audit_remark,
            d.audit_time,
            d.audit_admin_id,
            d.like_count,
            d.comment_count,
            d.favorite_count,
            d.share_count,
            d.view_count,
            d.status,
            d.visibility,
            d.created_at,
            d.updated_at,
            u.nickname,
            u.avatar_url,
            u.gender,
            CASE WHEN dl.id IS NOT NULL THEN 1 ELSE 0 END as is_liked,
            CASE WHEN df.id IS NOT NULL THEN 1 ELSE 0 END as is_favorited
        FROM user_dynamics d
        LEFT JOIN users u ON d.user_id = u.user_id
        LEFT JOIN dynamic_likes dl ON d.dynamic_id = dl.dynamic_id AND dl.user_id = #{userId}
        LEFT JOIN dynamic_favorites df ON d.dynamic_id = df.dynamic_id AND df.user_id = #{userId}
        WHERE d.status = 1
        <if test="auditStatus != null">
            AND d.audit_status = #{auditStatus}
        </if>
        <if test="visibility != null">
            AND d.visibility = #{visibility}
        </if>
        <if test="targetGender != null">
            AND u.gender = #{targetGender}
        </if>
        ORDER BY d.created_at DESC
    </select>

    <!-- 根据动态ID查询动态详情 -->
    <select id="selectDynamicDetailById" resultMap="DynamicWithUserMap">
        SELECT 
            d.dynamic_id,
            d.user_id,
            d.content,
            CAST(d.media_urls AS CHAR) as media_urls,
            d.media_type,
            d.audit_status,
            d.audit_remark,
            d.audit_time,
            d.audit_admin_id,
            d.like_count,
            d.comment_count,
            d.favorite_count,
            d.share_count,
            d.view_count,
            d.status,
            d.visibility,
            d.created_at,
            d.updated_at,
            u.nickname,
            u.avatar_url,
            u.gender,
            CASE WHEN dl.id IS NOT NULL THEN 1 ELSE 0 END as is_liked,
            CASE WHEN df.id IS NOT NULL THEN 1 ELSE 0 END as is_favorited
        FROM user_dynamics d
        LEFT JOIN users u ON d.user_id = u.user_id
        LEFT JOIN dynamic_likes dl ON d.dynamic_id = dl.dynamic_id AND dl.user_id = #{userId}
        LEFT JOIN dynamic_favorites df ON d.dynamic_id = df.dynamic_id AND df.user_id = #{userId}
        WHERE d.dynamic_id = #{dynamicId}
        AND d.status = 1
    </select>

    <!-- 查询某个用户发布的动态列表 -->
    <select id="selectDynamicsByUserId" resultMap="DynamicWithUserMap">
        SELECT 
            d.dynamic_id,
            d.user_id,
            d.content,
            CAST(d.media_urls AS CHAR) as media_urls,
            d.media_type,
            d.audit_status,
            d.audit_remark,
            d.audit_time,
            d.audit_admin_id,
            d.like_count,
            d.comment_count,
            d.favorite_count,
            d.share_count,
            d.view_count,
            d.status,
            d.visibility,
            d.created_at,
            d.updated_at,
            u.nickname,
            u.avatar_url,
            u.gender,
            CASE WHEN dl.id IS NOT NULL THEN 1 ELSE 0 END as is_liked,
            CASE WHEN df.id IS NOT NULL THEN 1 ELSE 0 END as is_favorited
        FROM user_dynamics d
        LEFT JOIN users u ON d.user_id = u.user_id
        LEFT JOIN dynamic_likes dl ON d.dynamic_id = dl.dynamic_id AND dl.user_id = #{currentUserId}
        LEFT JOIN dynamic_favorites df ON d.dynamic_id = df.dynamic_id AND df.user_id = #{currentUserId}
        WHERE d.user_id = #{targetUserId}
        AND d.status = 1
        AND d.audit_status = 1
        ORDER BY d.created_at DESC
    </select>

  <!-- 管理员修改动态状态（2=封禁 3=删除），并记录备注到 audit_remark -->
  <update id="updateDynamicStatus">
    UPDATE user_dynamics
    SET status = #{status}, audit_remark = CONCAT(COALESCE(audit_remark,''), '\n[MOD]', #{remark}), updated_at = NOW()
    WHERE dynamic_id = #{dynamicId}
  </update>

  <!-- 根据动态ID列表查询动态详情（关联用户信息） -->
  <select id="selectDynamicsByIds" resultMap="DynamicWithUserMap">
    SELECT 
        d.dynamic_id,
        d.user_id,
        d.content,
        CAST(d.media_urls AS CHAR) as media_urls,
        d.media_type,
        d.audit_status,
        d.audit_remark,
        d.audit_time,
        d.audit_admin_id,
        d.like_count,
        d.comment_count,
        d.favorite_count,
        d.share_count,
        d.view_count,
        d.status,
        d.visibility,
        d.created_at,
        d.updated_at,
        u.nickname,
        u.avatar_url,
        u.gender,
        CASE WHEN dl.id IS NOT NULL THEN 1 ELSE 0 END as is_liked,
        CASE WHEN df.id IS NOT NULL THEN 1 ELSE 0 END as is_favorited
    FROM user_dynamics d
    LEFT JOIN users u ON d.user_id = u.user_id
    LEFT JOIN dynamic_likes dl ON d.dynamic_id = dl.dynamic_id AND dl.user_id = #{userId}
    LEFT JOIN dynamic_favorites df ON d.dynamic_id = df.dynamic_id AND df.user_id = #{userId}
    WHERE d.dynamic_id IN
        <foreach collection="dynamicIds" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    AND d.status = 1
    AND d.audit_status = 1
    ORDER BY d.created_at DESC
  </select>

</mapper>


