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

	<insert id="batchInsert" useGeneratedKeys="true" parameterType="java.util.List">
		INSERT INTO BOOK_AUTH_CODE (
		BOOK_ID, CHANNEL_ID,ADVISER_ID,BATCH_NUM,AUTH_CODE,FULL_CODE,CREATE_TYPE,CREATED_USER, CREATED_DATE
		)
		VALUES
		<foreach collection="list" item="item" index="index" separator="," >
			(#{item.bookId,jdbcType=BIGINT}, #{item.channelId,jdbcType=BIGINT}, #{item.adviserId,jdbcType=BIGINT},
			#{item.batchNum,jdbcType=VARCHAR},#{item.authCode,jdbcType=VARCHAR},#{item.fullCode,jdbcType=VARCHAR},
			#{item.createType,jdbcType=TINYINT},#{item.adviserId,jdbcType=BIGINT}, NOW())
		</foreach>
	</insert>


	<select id="getMaxBatchNum" resultType="Integer" parameterType="map">
		SELECT 
			IFNULL(max(batch_num), 0)+1
		FROM 
			BOOK_AUTH_CODE
		WHERE
			BOOK_ID = #{bookId, jdbcType=BIGINT}
		AND
			CHANNEL_ID = #{channelId, jdbcType=BIGINT}
		AND
			ADVISER_ID = #{adviserId, jdbcType=BIGINT}
	</select>

	<update id="updateUseCount" parameterType="map" flushCache="true">
        UPDATE BOOK_AUTH_CODE
        SET
            use_count = use_count + 1,
            LAST_MODIFIED_DATE = NOW(),
            LAST_MODIFIED_USER = #{adviserId}
        WHERE
       		BOOK_ID = #{bookId, jdbcType=BIGINT}
		AND
			CHANNEL_ID = #{channelId, jdbcType=BIGINT}
		AND
			ADVISER_ID = #{adviserId, jdbcType=BIGINT}
		AND
		 	FULL_CODE = #{code}
		AND
			use_count <![CDATA[ < ]]>  #{codeUseCount}
    </update>

	<select id="getIsHaveCode" resultType="Boolean" parameterType="map">
		SELECT
			COUNT(1)
		FROM
			BOOK_AUTH_CODE
		WHERE
			BOOK_ID = #{bookId, jdbcType=BIGINT}
		AND
			CHANNEL_ID = #{channelId, jdbcType=BIGINT}
		AND
			ADVISER_ID = #{adviserId, jdbcType=BIGINT}
		AND
		    FULL_CODE = #{code}
	</select>

	<select id="getCodeList" 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
		FROM
			book_auth_code
		WHERE
			book_id = #{bookId}
		AND channel_id = #{channelId}
		AND adviser_id = #{adviserId}
		<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>
		order by created_date desc
	</select>

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

	<select id="getBookAuthCodeList" 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,
			use_count useCount,
			created_date createdDate
		from book_auth_code
		where book_id= #{bookId,jdbcType=BIGINT} and channel_id = #{channelId} and adviser_id = #{adviserId}
		LIMIT  #{currentPage},#{numPerPage}
	</select>

	<select id="getBaseById" parameterType="long" 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,
			use_count useCount,
			created_date createdDate
		from book_auth_code
		where id = #{codeId}
	</select>
</mapper>