<?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.copyright.dao.impl.BookSceneAuthDaoImpl">

    <resultMap id="BaseResultMap" type="com.pcloud.book.copyright.entity.BookSceneAuth">
        <id column="id" property="id" jdbcType="INTEGER"/>
        <result column="book_id" property="bookId" jdbcType="INTEGER"/>
        <result column="scene_id" property="sceneId" jdbcType="INTEGER"/>
        <result column="channel_id" property="channelId" jdbcType="INTEGER"/>
        <result column="adviser_id" property="adviserId" jdbcType="INTEGER"/>
        <result column="max_user_count" property="maxUserCount" jdbcType="INTEGER"/>
        <result column="full_use_tips" property="fullUseTips" jdbcType="VARCHAR"/>
        <result column="created_user" property="createdUser" jdbcType="INTEGER"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
    </resultMap>
    
    <sql id="Base_Column_List">
        id, book_id, scene_id, channel_id, adviser_id, max_user_count, full_use_tips, created_user, create_time, update_time        
    </sql>

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

    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        INSERT INTO book_scene_auth(
            book_id,
            scene_id,
            channel_id,
            adviser_id,
            max_user_count,
            full_use_tips,
            created_user,
            create_time,
            update_time
        ) VALUES (
            #{bookId, jdbcType=INTEGER},
            #{sceneId, jdbcType=INTEGER},
            #{channelId, jdbcType=INTEGER},
            #{adviserId, jdbcType=INTEGER},
            #{maxUserCount, jdbcType=INTEGER},
            #{fullUseTips, jdbcType=VARCHAR},
            #{createdUser, jdbcType=INTEGER},
            now(),
            now()
        )
    </insert>
    
    <insert id="batchInsert" keyProperty="id" useGeneratedKeys="true">
        INSERT INTO book_scene_auth (
            book_id,
            scene_id,
            channel_id,
            adviser_id,
            max_user_count,
            full_use_tips,
            created_user,
            create_time,
            update_time
        ) VALUES 
        <foreach collection="list" item="item" separator=",">
        (
            #{item.bookId, jdbcType=INTEGER},
            #{item.sceneId, jdbcType=INTEGER},
            #{item.channelId, jdbcType=INTEGER},
            #{item.adviserId, jdbcType=INTEGER},
            #{item.maxUserCount, jdbcType=INTEGER},
            #{item.fullUseTips, jdbcType=VARCHAR},
            #{item.createdUser, jdbcType=INTEGER},
            now(),
            now()
        )
        </foreach>
    </insert>

    <!--通过主键修改数据-->
    <update id="update">
        UPDATE book_scene_auth
        <set>
            <if test="bookId != null">
                book_id = #{bookId},
            </if>
            <if test="sceneId != null">
                scene_id = #{sceneId},
            </if>
            <if test="channelId != null">
                channel_id = #{channelId},
            </if>
            <if test="adviserId != null">
                adviser_id = #{adviserId},
            </if>
            <if test="maxUserCount != null">
                max_user_count = #{maxUserCount},
            </if>
            <if test="fullUseTips != null and fullUseTips != ''">
                full_use_tips = #{fullUseTips},
            </if>
            <if test="createdUser != null">
                created_user = #{createdUser},
            </if>
            <if test="createTime != null">
                create_time = #{createTime},
            </if>
                update_time = now()
        </set>
        WHERE id = #{id}
    </update>

    <update id="batchUpdate" parameterType="list">
        <foreach collection="list" index="index" item="item" separator=";">
        UPDATE book_scene_auth
        <set>
            <if test="item.maxUserCount != null">
                max_user_count = #{item.maxUserCount},
            </if>
            <if test="item.fullUseTips != null and item.fullUseTips != ''">
                full_use_tips = #{item.fullUseTips},
            </if>
                update_time = now()
        </set>
        WHERE id = #{item.id}
        </foreach>
    </update>

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

    <select id="getAuthSceneInfo" parameterType="map" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from book_scene_auth
        where book_id=#{bookId}
        and scene_id=#{sceneId}
        limit 1
    </select>

    <select id="getAuthSceneIds" parameterType="map" resultMap="BaseResultMap">
        select <include refid="Base_Column_List"/>
        from book_scene_auth
        where book_id=#{bookId}
        <if test="sceneIds!=null and sceneIds.size()>0">
            and scene_id in
            <foreach collection="sceneIds" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
    </select>

</mapper>