<?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)
	    VALUES
       (#{bookId,jdbcType=BIGINT}, #{channelId,jdbcType=BIGINT}, #{adviserId,jdbcType=BIGINT},#{batchNum},
		#{adviserId}, NOW())
    </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}
    </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}
    </update>

    <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>



</mapper>