<?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.BookSceneAuthCodeDaoImpl">

    <resultMap id="BaseResultMap" type="com.pcloud.book.copyright.entity.BookSceneAuthCode">
        <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="auth_code" property="authCode" jdbcType="CHAR"/>
        <result column="batch_num" property="batchNum" jdbcType="CHAR"/>
        <result column="full_code" property="fullCode" jdbcType="CHAR"/>
        <result column="use_count" property="useCount" jdbcType="INTEGER"/>
        <result column="create_type" property="createType" jdbcType="INTEGER"/>
        <result column="created_user" property="createdUser" jdbcType="INTEGER"/>
        <result column="created_date" property="createdDate" jdbcType="TIMESTAMP"/>
        <result column="last_modified_user" property="lastModifiedUser" jdbcType="INTEGER"/>
        <result column="last_modified_date" property="lastModifiedDate" jdbcType="TIMESTAMP"/>
        <result column="is_paper_book" property="isPaperBook" jdbcType="INTEGER"/>
        <result column="is_group_book" property="isGroupBook" jdbcType="INTEGER"/>
        <result column="url" property="url" jdbcType="CHAR"/>
        <result column="qrcode_url" property="qrcodeUrl" jdbcType="CHAR"/>
    </resultMap>
    
    <sql id="Base_Column_List">
        id, book_id, scene_id, channel_id, adviser_id, auth_code, batch_num, full_code, use_count, create_type, created_user, created_date, last_modified_user, last_modified_date, is_paper_book, is_group_book,url,qrcode_url
    </sql>

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

    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        INSERT INTO book_scene_auth_code(
            book_id,
            scene_id,
            channel_id,
            adviser_id,
            auth_code,
            batch_num,
            full_code,
            use_count,
            create_type,
            created_user,
            created_date,
            last_modified_user,
            last_modified_date,
            is_paper_book,
            is_group_book
        ) VALUES (
            #{bookId, jdbcType=INTEGER},
            #{sceneId, jdbcType=INTEGER},
            #{channelId, jdbcType=INTEGER},
            #{adviserId, jdbcType=INTEGER},
            #{authCode, jdbcType=CHAR},
            #{batchNum, jdbcType=CHAR},
            #{fullCode, jdbcType=CHAR},
            0,
            #{createType, jdbcType=INTEGER},
            #{createdUser, jdbcType=INTEGER},
            now(),
            #{lastModifiedUser, jdbcType=INTEGER},
             now(),
            #{isPaperBook, jdbcType=INTEGER},
            #{isGroupBook, jdbcType=INTEGER}
        )
    </insert>
    
    <insert id="batchInsert" keyProperty="id" useGeneratedKeys="true">
        INSERT INTO book_scene_auth_code (
            book_id,
            scene_id,
            channel_id,
            adviser_id,
            auth_code,
            batch_num,
            full_code,
            use_count,
            create_type,
            created_user,
            created_date,
            last_modified_user,
            last_modified_date,
            is_paper_book,
            is_group_book
        ) VALUES 
        <foreach collection="list" item="item" separator=",">
        (
            #{item.bookId, jdbcType=INTEGER},
            #{item.sceneId, jdbcType=INTEGER},
            #{item.channelId, jdbcType=INTEGER},
            #{item.adviserId, jdbcType=INTEGER},
            #{item.authCode, jdbcType=CHAR},
            #{item.batchNum, jdbcType=CHAR},
            #{item.fullCode, jdbcType=CHAR},
            0,
            #{item.createType, jdbcType=INTEGER},
            #{item.createdUser, jdbcType=INTEGER},
            now(),
            #{item.lastModifiedUser, jdbcType=INTEGER},
            now(),
            #{item.isPaperBook, jdbcType=INTEGER},
            #{item.isGroupBook, jdbcType=INTEGER}
        )
        </foreach>
    </insert>

    <!--通过主键修改数据-->
    <update id="update">
        UPDATE book_scene_auth_code
        <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="authCode != null and authCode != ''">
                auth_code = #{authCode},
            </if>
            <if test="batchNum != null and batchNum != ''">
                batch_num = #{batchNum},
            </if>
            <if test="fullCode != null and fullCode != ''">
                full_code = #{fullCode},
            </if>
            <if test="useCount != null">
                use_count = #{useCount},
            </if>
            <if test="createType != null">
                create_type = #{createType},
            </if>
            <if test="createdUser != null">
                created_user = #{createdUser},
            </if>
            <if test="createdDate != null">
                created_date = #{createdDate},
            </if>
            <if test="lastModifiedUser != null">
                last_modified_user = #{lastModifiedUser},
            </if>
            <if test="lastModifiedDate != null">
                last_modified_date = #{lastModifiedDate},
            </if>
            <if test="isPaperBook != null">
                is_paper_book = #{isPaperBook},
            </if>
            <if test="isGroupBook != null">
                is_group_book = #{isGroupBook},
            </if>
        </set>
        WHERE id = #{id}
    </update>

    <update id="batchUpdate" parameterType="list">
        <foreach collection="list" index="index" item="item" separator=";">
        UPDATE book_scene_auth_code
        <set>
            <if test="item.url != null">
                url = #{item.url},
            </if>
            <if test="item.qrcodeUrl != null">
                qrcode_url = #{item.qrcodeUrl},
            </if>
        </set>
        WHERE id = #{item.id}
        </foreach>
    </update>

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

    <select id="getMaxBatchNum" resultType="Integer" parameterType="map">
        SELECT
        IFNULL(max(batch_num), 0)+1
        FROM
        book_scene_auth_code
        WHERE
        BOOK_ID = #{bookId}
        AND
        CHANNEL_ID = #{channelId}
        AND
        ADVISER_ID = #{adviserId}
        and scene_id=#{sceneId}
        <if test="isPaperBook != null">
            AND
            is_paper_book = #{isPaperBook}
        </if>
        <if test="isGroupBook!=null">
            AND
            is_group_book = #{isGroupBook}
        </if>
    </select>

    <select id="listSceneAuthCode" parameterType="map" resultType="com.pcloud.book.copyright.dto.BookAuthCodeDTO">
        SELECT
        id id,
        book_id bookId,
        channel_id channelId,
        adviser_id adviserId,
        auth_code authCode,
        batch_num batchNum,
        full_code fullCode,
        use_count useCount,
        create_type createType,
        created_date createdDate,
        qrcode_url qrcodeUrl
        FROM
        book_scene_auth_code
        WHERE
        book_id = #{bookId}
        AND channel_id = #{channelId}
        AND adviser_id = #{adviserId}
        and scene_id=#{sceneId}
        <if test="keyword != null">
            AND full_code LIKE CONCAT('%', #{keyword}, '%')
        </if>
        <if test="state != null">
            <choose>
                <when test="state == 0">
                    and use_count = 0
                </when>
                <otherwise>
                    and use_count > 0
                </otherwise>
            </choose>
        </if>
        <if test="isPaperBook != null">
            AND
            is_paper_book = #{isPaperBook}
        </if>
        <if test="isGroupBook!=null">
            AND
            is_group_book = #{isGroupBook}
        </if>
        order by created_date desc
    </select>

    <delete id="deleteSceneAuthCode" parameterType="list">
        DELETE FROM book_scene_auth_code
        WHERE id in
        <foreach collection="list" open="(" item="item" close=")" separator=",">
            ${item}
        </foreach>
        AND use_count = 0
    </delete>

    <select id="getCodeIdList" parameterType="map" resultType="Long">
        SELECT
        id
        FROM
        book_scene_auth_code
        WHERE
        book_id = #{bookId}
        AND channel_id = #{channelId}
        AND adviser_id = #{adviserId}
        and scene_id=#{sceneId}
        and use_count = 0
        <if test="keyword != null">
            AND full_code LIKE CONCAT('%', #{keyword}, '%')
        </if>
        <if test="isPaperBook != null">
            AND
            is_paper_book = #{isPaperBook}
        </if>
        <if test="isGroupBook!=null">
            AND
            is_group_book = #{isGroupBook}
        </if>
    </select>

    <select id="getByCode" parameterType="map" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM book_scene_auth_code
        WHERE full_code = #{code}
        limit 1
    </select>

    <update id="updateUseCount" parameterType="Long">
         UPDATE book_scene_auth_code
        SET
            use_count = ifnull(use_count,0) + 1,
            LAST_MODIFIED_DATE = NOW()
        WHERE
       		id = #{id}
    </update>

    <select id="getCodeCount" parameterType="map" resultType="com.pcloud.book.copyright.dto.SceneAuthCodeResponseDTO">
        select
        scene_id sceneId,
        ifnull(count(1),0) counts
        from book_scene_auth_code
        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>
        group by scene_id
    </select>

    <select id="getBookSceneAuthCodeList" parameterType="map" resultType="com.pcloud.book.copyright.dto.BookAuthCodeDTO">
        select
        book_id bookId,
        channel_id channelId,
        adviser_id adviserId,
        create_type createType,
        auth_code authCode,
        batch_num batchNum,
        full_code fullCode,
        ifnull(use_count,0) useCount,
        created_date createdDate
        from book_scene_auth_code
        where book_id= #{bookId,jdbcType=BIGINT} and channel_id = #{channelId} and adviser_id = #{adviserId}
        <if test="sceneId !=null">
            and scene_id=#{sceneId}
        </if>
        <if test="isPaperBook != null">
            AND
            is_paper_book = #{isPaperBook}
        </if>
        <if test="isGroupBook!=null">
            AND
            is_group_book = #{isGroupBook}
        </if>
        LIMIT  #{currentPage},#{numPerPage}
    </select>
</mapper>