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

	<insert id="insert" useGeneratedKeys="true" parameterType="bookExportRecord">
      INSERT INTO BOOK_EXPORT_RECORD(
			BOOK_ID, CHANNEL_ID,ADVISER_ID, BATCH_NUM, CREATED_USER, CREATED_DATE,code_count,is_have_bar_code,scene_id)
	    VALUES
       (#{bookId,jdbcType=BIGINT}, #{channelId,jdbcType=BIGINT}, #{adviserId,jdbcType=BIGINT},#{batchNum},
		#{adviserId}, NOW(),#{codeCount},#{isHaveBarCode},#{sceneId})
    </insert>

    <update id="updateRecordStatus" parameterType="bookAuthInfo" flushCache="true">
        UPDATE BOOK_EXPORT_RECORD
        SET
            EXPORT_STATUS = #{exportStatus},
            LAST_MODIFIED_DATE = NOW(),
            LAST_MODIFIED_USER = #{lastModifiedUser}
        WHERE
            BOOK_ID = #{bookId} AND CHANNEL_ID = #{channelId} AND ADVISER_ID = #{adviserId} AND BATCH_NUM = #{batchNum}
        <if test="sceneId !=null">
            and scene_id=#{sceneId}
        </if>
    </update>

	<update id="updateRecord" parameterType="bookAuthInfo" flushCache="true">
        UPDATE BOOK_EXPORT_RECORD
        SET
            EXPORT_STATUS = #{exportStatus},
            download_url = #{downloadUrl},
            LAST_MODIFIED_DATE = NOW(),
            LAST_MODIFIED_USER = #{lastModifiedUser}
        WHERE
           BOOK_ID = #{bookId} AND CHANNEL_ID = #{channelId} AND ADVISER_ID = #{adviserId} AND BATCH_NUM = #{batchNum}
           <if test="sceneId !=null">
               and scene_id=#{sceneId}
           </if>
    </update>

    <select id="getExportUrl" parameterType="map" resultType="String">
        select download_url
        from  BOOK_EXPORT_RECORD
        WHERE
        BOOK_ID = #{bookId} AND CHANNEL_ID = #{channelId} AND ADVISER_ID = #{adviserId}
        <if test="sceneId !=null">
            and scene_id=#{sceneId}
        </if>
        order by id desc limit 1
    </select>

    <select id="isHaveExport" resultType="Boolean" parameterType="map">
		SELECT
			COUNT(1)
		FROM
			BOOK_EXPORT_RECORD
		WHERE
			BOOK_ID = #{bookId, jdbcType=BIGINT}
		AND
			CHANNEL_ID = #{channelId, jdbcType=BIGINT}
		AND
			ADVISER_ID = #{adviserId, jdbcType=BIGINT}
		AND EXPORT_STATUS = 0
	</select>

    <update id="handleExportFail" >
		UPDATE BOOK_EXPORT_RECORD
		SET EXPORT_STATUS = 2
		WHERE EXPORT_STATUS = 0
	</update>

    <select id="getGenerateCodeHistory" parameterType="map" resultType="com.pcloud.book.copyright.dto.BookExportRecordDTO">
        SELECT
        download_url downloadUrl,
        code_count codeCount,
        is_have_bar_code isHaveBarCode,
        last_modified_date lastModifiedDate
        FROM
        BOOK_EXPORT_RECORD
        WHERE
        book_id = #{bookId}
        AND channel_id = #{channelId}
        AND adviser_id = #{adviserId}
        <if test="sceneId !=null">
            and scene_id=#{sceneId}
        </if>
        order by created_date desc
    </select>

</mapper>