<?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.wzj.soopin.content.mapper.VlogMapper">
<!--  通用更新  -->
  <update id="dynamicUpdate">
    UPDATE cont_vlog
    <set>
      <foreach collection="updateFields" index="key" item="value" separator=",">
        ${key} = #{value}
      </foreach>
    </set>
    WHERE ${primaryKey} = #{idValue}
  </update>

  <update id="updateVlogByConditions">
    UPDATE cont_vlog
    <set>
      <foreach collection="updates" item="value" index="key" separator=",">
          ${key} = #{value}
      </foreach>
    </set>
    <where>
      <foreach collection="conditions" item="value" index="key" separator=" AND ">
        <choose>
          <!-- 处理null值条件 -->
          <when test="value == null">
            ${key} IS NULL
          </when>
          <!-- 处理IN条件 -->
          <when test="value instanceof java.util.Collection">
            ${key} IN
            <foreach collection="value" item="item" open="(" separator="," close=")">
              #{item}
            </foreach>
          </when>
          <!-- 处理普通条件 -->
          <otherwise>
            ${key} = #{value}
          </otherwise>
        </choose>
      </foreach>
    </where>
  </update>

  <!-- 单条插入 -->
  <insert id="dynamicInsert">
    INSERT INTO cont_vlog
    <foreach collection="vlogData.keys" item="key" open="(" separator="," close=")">
      ${key}
    </foreach>
    VALUES
    <foreach collection="vlogData.values" item="value" open="(" separator="," close=")">
      #{value}
    </foreach>
  </insert>

  <!-- 批量插入 -->
  <insert id="dynamicBatchInsert">
    INSERT INTO cont_vlog
    <foreach collection="vlogList[0].keys" item="key" open="(" separator="," close=")">
      ${key}
    </foreach>
    VALUES
    <foreach collection="vlogList" item="vlogData" separator=",">
      <foreach collection="vlogData.values" item="value" open="(" separator="," close=")">
        #{value}
      </foreach>
    </foreach>
  </insert>

<!-- 查询 -->
  <select id="selectMyPublic" parameterType="map" resultType="com.wzj.soopin.content.domain.vo.IndexVlogVO">
    SELECT
    v.id as vlogId,
    v.member_id as vlogerId,
    m.avatar as vlogerFace,
    m.nickname as vlogerName,
    v.title as content,
    v.url as mediaUrl,
    v.cover as cover,
    v.width as width,
    v.height as height,
    v.like_counts as likeCounts,
    v.comments_counts as commentCounts,
    v.is_private as isPrivate,
    v.city_code as cityCode,
    v.reason as reason,
    v.file_id as fileId,
    v.status as status,
    v.first_frame_img as firstFrameImg,
    v.id as vlogId
    FROM
    cont_vlog v
    LEFT JOIN
    ums_member m
    ON
    v.member_id = m.id
    WHERE
    v.is_private = 0
    AND v.member_id = #{paramMap.vlogerId}
    <choose>
      <when test="paramMap.status != null and paramMap.status!=''">
        AND v.status = #{paramMap.status}
      </when>
      <otherwise>
        AND v.status = 1
      </otherwise>
    </choose>
    <if test="paramMap.cityCode != null and paramMap.cityCode != ''">
      AND v.city_code = #{paramMap.cityCode}
    </if>
    <if test="paramMap.search != null and paramMap.search != ''">
      AND v.title like '%${paramMap.search}%'
    </if>
    <if test="paramMap.blockVd != null and paramMap.blockVd.size() > 0">
      AND v.id NOT IN
      <foreach collection="paramMap.blockVd" item="vlogId" open="(" separator="," close=")">
        #{vlogId}
      </foreach>
    </if>
    ORDER BY
    v.create_time
    DESC
  </select>


  <select id="getVlogDetailFromId" parameterType="map" resultType="com.wzj.soopin.content.domain.vo.IndexVlogVO">
    SELECT
    v.id ,
    v.member_id ,
    m.avatar ,
    m.nickname ,
    v.title as content,
    v.url as mediaUrl,
    v.cover as cover,
    v.width as width,
    v.height as height,
    v.like_counts as likeCounts,
    v.comments_counts as commentCounts,
    v.is_private as isPrivate,
    v.city_code as cityCode,
    v.reason as reason,
    v.file_id as fileId,
    v.status as status,
    v.first_frame_img as firstFrameImg,
    v.id as vlogId
    FROM
    cont_vlog v
    LEFT JOIN
    ums_member m
    ON
    v.member_id = m.id
    WHERE
    v.id = #{paramMap.vlogId}
    AND v.status = 1
    AND v.first_frame_img IS NOT NULL
  </select>

  <!-- 更新视频审核状态 -->
  <update id="updateVlogStatus">
    UPDATE t_vlog
    SET status = #{params.status},
        reason = #{params.reason},
        audit_time = #{params.auditTime}
    WHERE id = #{params.vlogId}
  </update>

  <!-- 根据fileId查询Vlog信息 -->
  <select id="selectByFileId" parameterType="String" resultType="com.wzj.soopin.content.domain.po.Vlog">
    /* @SqlParser(filter=true) */
    SELECT * FROM cont_vlog WHERE file_id = #{fileId}
  </select>

  <select id="selectByMobileAndVlogId" resultType="com.wzj.soopin.content.domain.vo.CommentVO">
    SELECT
        c.id,
        c.id as commentId,
        c.member_id as vlogerId,
        c.father_comment_id as fatherCommentId,
        c.vlog_id as vlogId,
        c.comment_user_id as commentUserId,
        m.nickname as commentUserNickname,
        m.avatar as commentUserFace,
        c.content,
        c.like_counts as likeCounts,
        c.create_time as createTime
    FROM cont_comment c
    LEFT JOIN ums_member m ON c.comment_user_id = m.id
    <where>
        c.father_comment_id = '0'
        <if test="vlogId != null and vlogId != ''">
            AND c.vlog_id = #{vlogId}
        </if>
        <if test="mobile != null and mobile != ''">
            AND m.phone_hidden LIKE CONCAT('%', #{mobile}, '%')
        </if>
    </where>
    ORDER BY c.create_time DESC
  </select>

    <select id="selectVlogListWithAggregatedData" resultType="java.util.Map">
        SELECT
        v.id,
        v.member_id,
        v.url as mediaUrl,
        v.cover,
        v.title,
        v.width,
        v.height,
        COUNT(DISTINCT mlv.id) AS likeCounts,
        COUNT(DISTINCT c.id) AS commentCounts,
        v.file_id as fileId,
        v.status,
        v.reason,
        v.city_code,
        v.first_frame_img as coverUrl,
        v.create_time as createTime,
        v.update_time,
        v.create_by,
        v.update_by,
        m.nickname,
        m.phone_hidden as mobile
        FROM
        cont_vlog v
        LEFT JOIN
        ums_member m ON v.member_id = m.id
        LEFT JOIN
        cont_my_liked_vlog mlv ON v.id = mlv.vlog_id
        LEFT JOIN
        cont_comment c ON v.id = c.vlog_id AND (c.father_comment_id = '0' OR c.father_comment_id IS NULL)
        <where>
            <if test="vlogBO.mobile != null and vlogBO.mobile != ''">
                AND m.phone_hidden LIKE CONCAT('%', #{vlogBO.mobile}, '%')
            </if>
            <if test="vlogBO.nickname != null and vlogBO.nickname != ''">
                AND m.nickname LIKE CONCAT('%', #{vlogBO.nickname}, '%')
            </if>
            <if test="vlogBO.titleQuery != null and vlogBO.titleQuery != ''">
                AND v.title LIKE CONCAT('%', #{vlogBO.titleQuery}, '%')
            </if>
            <if test="vlogBO.status != null">
                AND v.status = #{vlogBO.status}
            </if>
            <if test="vlogBO.startTime != null and vlogBO.startTime != '' and vlogBO.endTime != null and vlogBO.endTime != ''">
                AND v.create_time BETWEEN #{vlogBO.startTime} AND #{vlogBO.endTime}
            </if>
        </where>
        GROUP BY
        v.id,
        v.member_id,
        v.url,
        v.cover,
        v.title,
        v.width,
        v.height,
        v.file_id,
        v.status,
        v.reason,
        v.city_code,
        v.first_frame_img,
        v.create_time,
        v.update_time,
        v.create_by,
        v.update_by,
        m.nickname,
        m.phone_hidden as mobile
        ORDER BY
        <choose>
            <when test="vlogBO.column == 'likeCounts'">
                likeCounts ${vlogBO.asc ? 'ASC' : 'DESC'}
            </when>
            <when test="vlogBO.column == 'commentCounts'">
                commentCounts ${vlogBO.asc ? 'ASC' : 'DESC'}
            </when>
            <when test="vlogBO.column == 'status'">
                FIELD(v.status, 0, 1, 2, 3) ${vlogBO.asc ? 'ASC' : 'DESC'}
            </when>
            <otherwise>
                v.create_time DESC
            </otherwise>
        </choose>
    </select>

    <select id="getVlogForUser" resultType="java.lang.String">
        SELECT
            v.id
        FROM
            cont_vlog v
        WHERE
            NOT EXISTS (
                SELECT 1 FROM cms_vlog_member m
                WHERE m.vlog_id = v.id AND m.member_id = ${memberId}
            )
          AND NOT EXISTS (
            SELECT 1 FROM ums_block b
            WHERE b.block_member_id = v.id AND b.member_id = ${memberId}
        )
        ORDER BY
            v.create_time DESC

    </select>

    <update id="readVlog">
        insert into cms_vlog_member (vlog_id, member_id,status) values (#{vlogId}, #{memberId},1)
    </update>
</mapper>

