<?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.pcloud.book.book.dao.impl.BookUserBrowseRecordDaoImpl">

    <resultMap id="BaseResultMap" type="com.pcloud.book.book.entity.BookUserBrowseRecord">
        <result column="book_id" property="bookId" jdbcType="BIGINT"/>
        <result column="user_id" property="userId" jdbcType="BIGINT"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
    </resultMap>

    <sql id="Base_Column_List">
        book_id, user_id, update_time
    </sql>

    <select id="getById" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM book_user_browse_record
        WHERE book_id = #{bookId}
    </select>

    <select id="getList" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM book_user_browse_record
    </select>

    <insert id="insert" keyProperty="bookId" useGeneratedKeys="true">
        INSERT INTO book_user_browse_record(update_time)
        VALUES (#{updateTime, jdbcType=TIMESTAMP})
    </insert>

    <insert id="batchInsert">
        INSERT INTO book_user_browse_record (
        update_time
        ) VALUES
        <foreach collection="list" item="item" separator=",">
            (
            #{item.updateTime, jdbcType=TIMESTAMP}
            )
        </foreach>
    </insert>

    <!--通过主键修改数据-->
    <update id="update">
        UPDATE book_user_browse_record
        <set>
            <if test="updateTime != null">
                update_time = #{updateTime},
            </if>
        </set>
        WHERE book_id = #{bookId}
    </update>

    <!--通过主键删除-->
    <delete id="deleteById">
        DELETE
        FROM book_user_browse_record
        where book_id = #{bookId}
    </delete>

    <!--通过主键软删除-->
    <delete id="softDeleteById">
        update book_user_browse_record
        set is_delete   = 1,
            update_time = now()
        where book_id = #{bookId}
    </delete>

    <select id="getUserIdsByBookId" parameterType="list" resultType="long">
        select
        distinct  user_id
        from
        book_user_browse_record
        where
        book_id in
        <foreach collection="list" index="index" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
    </select>

</mapper>
