<?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.applet.dao.impl.ReadBookRecordDaoImpl">

    <resultMap id="BaseResultMap" type="com.pcloud.book.applet.entity.ReadBookRecord">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="wechat_user_id" property="wechatUserId" jdbcType="BIGINT"/>
        <result column="book_id" property="bookId" jdbcType="BIGINT"/>
        <result column="channel_id" property="channelId" jdbcType="BIGINT"/>
        <result column="adviser_id" property="adviserId" jdbcType="BIGINT"/>
        <result column="start_time" property="startTime" jdbcType="TIMESTAMP"/>
        <result column="end_time" property="endTime" jdbcType="TIMESTAMP"/>
        <result column="record_time" property="recordTime" jdbcType="BIGINT"/>
        <result column="record_source" property="recordSource" jdbcType="INTEGER"/>
        <result column="leave_type" property="leaveType" jdbcType="INTEGER"/>
    </resultMap>

    <sql id="Base_Column_List">
        id, wechat_user_id, book_id, channel_id, adviser_id, start_time, end_time, record_time, record_source, leave_type        
    </sql>

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

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

    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        INSERT INTO read_book_record(
            wechat_user_id, 
            book_id, 
            channel_id, 
            adviser_id, 
            start_time, 
            end_time, 
            record_time, 
            record_source, 
            leave_type
        ) VALUES (
            #{wechatUserId, jdbcType=BIGINT}, 
            #{bookId, jdbcType=BIGINT}, 
            #{channelId, jdbcType=BIGINT}, 
            #{adviserId, jdbcType=BIGINT}, 
            #{startTime, jdbcType=TIMESTAMP}, 
            #{endTime, jdbcType=TIMESTAMP}, 
            #{recordTime, jdbcType=BIGINT}, 
            #{recordSource, jdbcType=INTEGER}, 
            #{leaveType, jdbcType=INTEGER}
        )
    </insert>

    <insert id="batchInsert">
        INSERT INTO read_book_record (
        wechat_user_id,
        book_id,
        channel_id,
        adviser_id,
        start_time,
        end_time,
        record_time,
        record_source,
        leave_type
        ) VALUES
        <foreach collection="list" item="item" separator=",">
            (
            #{item.wechatUserId, jdbcType=BIGINT},
            #{item.bookId, jdbcType=BIGINT},
            #{item.channelId, jdbcType=BIGINT},
            #{item.adviserId, jdbcType=BIGINT},
            #{item.startTime, jdbcType=TIMESTAMP},
            #{item.endTime, jdbcType=TIMESTAMP},
            #{item.recordTime, jdbcType=BIGINT},
            #{item.recordSource, jdbcType=INTEGER},
            #{item.leaveType, jdbcType=INTEGER}
            )
        </foreach>
    </insert>

    <!--通过主键修改数据-->
    <update id="update">
        UPDATE read_book_record
        <set>
            <if test="wechatUserId != null">
                wechat_user_id = #{wechatUserId},
            </if>
            <if test="bookId != null">
                book_id = #{bookId},
            </if>
            <if test="channelId != null">
                channel_id = #{channelId},
            </if>
            <if test="adviserId != null">
                adviser_id = #{adviserId},
            </if>
            <if test="startTime != null">
                start_time = #{startTime},
            </if>
            <if test="endTime != null">
                end_time = #{endTime},
            </if>
            <if test="recordTime != null">
                record_time = #{recordTime},
            </if>
            <if test="recordSource != null">
                record_source = #{recordSource},
            </if>
            <if test="leaveType != null">
                leave_type = #{leaveType},
            </if>
        </set>
        WHERE id = #{id}
    </update>

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

    <update id="updateRecordTime">
        UPDATE read_book_record
        <set>
            <if test="endTime != null">
                end_time = #{endTime},
                record_time = #{recordTime}
            </if>
        </set>
        WHERE id = #{id}
    </update>

    <update id="batchUpdateRecordTime" parameterType="com.pcloud.book.applet.entity.ReadBookRecord">
        UPDATE read_book_record
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="end_time =case" suffix="end,">
                <foreach collection="list" item="item">
                    <if test="item.endTime!=null">
                        when id=#{item.id} then #{item.endTime}
                    </if>
                </foreach>
            </trim>
            record_time  = 60,
            leave_type = 1,
            update_time = now(),
        </trim>
        WHERE
        <foreach collection="list" separator="or" item="item">
        id = #{item.id}
        </foreach>
    </update>
</mapper>