<?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.mmc.csf.release.dao.DynamicDAO">

    <insert id="insertPicture">
        insert into forum_image(dynamic_id, url)
        values (#{dynamicId}, #{url})
    </insert>
    <insert id="insertVideo">
        insert into forum_video(dynamic_id, url)
        values (#{dynamicId}, #{videoUrl})
    </insert>
    <insert id="insertDynamic" keyProperty="id" useGeneratedKeys="true" parameterType="com.mmc.csf.release.entity.ForumDynamicDO">
        INSERT INTO forum_dynamic(title, user_id, description, location, lat, lon)
        VALUES (#{title}, #{userId}, #{description}, #{location}, #{lat}, #{lon})
    </insert>
    <update id="updateDynamicLikeCount">
        update forum_dynamic
        set likes_count = likes_count + 1,
            pid         = pid + 1
        where id = #{dynamicId}
          and pid = #{pid};
    </update>
    <update id="updateSubDynamicLikeCount">
        update forum_dynamic
        set likes_count = likes_count - 1,
            pid         = pid + 1
        where id = #{dynamicId}
          and pid = #{pid};
    </update>
    <update id="updateDynamicCommentCount">
        update forum_dynamic
        set comments_count = comments_count + 1,
            pid         = pid + 1
        where id = #{dynamicId}
          and pid = #{pid};
    </update>
    <select id="getDynamicInfo" resultType="com.mmc.csf.release.entity.ForumDynamicDO">
        select id,
               pid,
               title,
               user_id,
               description,
               location,
               lat,
               lon,
               likes_count,
               comments_count
        from forum_dynamic
        where id = #{dynamicId}
    </select>
    <select id="countDynamicList" resultType="java.lang.Integer">
        select count(*)
        from forum_dynamic where is_deleted = 0
    </select>
    <select id="dynamicList" resultType="com.mmc.csf.release.entity.ForumDynamicDO">
        select id,
               pid,
               title,
               user_id,
               description,
               location,
               lat,
               lon,
               likes_count,
               comments_count,
               create_time
        from forum_dynamic
        where is_deleted = 0
        order by create_time desc
            LIMIT #{itemIndex}, #{pageSize}
    </select>

    <select id="listDynamicPicture" resultType="com.mmc.csf.release.entity.ForumImageDO">
        select id, url , dynamic_id
        from forum_image
        where
        is_deleted = 0 and dynamic_id in
        <foreach collection="list" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>
    <select id="listDynamicVideo" resultType="com.mmc.csf.release.entity.ForumVideoDO">
        select id, url , dynamic_id
        from forum_video
        where
        is_deleted = 0 and dynamic_id in
        <foreach collection="list" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>
</mapper>