<?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.book.dao.impl.BookCoverDaoImpl">

    <resultMap id="BaseResultMap" type="com.pcloud.book.book.dto.BookCoverDTO">
        <id column="COVER_ID" property="coverId" jdbcType="BIGINT" />
        <result column="TEMPLET_ID" property="templetId" jdbcType="BIGINT" />
        <result column="TEMPLET_NAME" property="templetName" jdbcType="VARCHAR" />
        <result column="COVER_IMG" property="coverImg" jdbcType="VARCHAR" />
        <result column="CREATED_Time" property="createdTime" jdbcType="TIMESTAMP" />
        <result column="UPDATE_TIME" property="updateTime" jdbcType="TIMESTAMP" />
        <result column="book_label_id" property="bookLabelId" jdbcType="BIGINT" />
    </resultMap>

    <select id="selectAllCover" resultMap="BaseResultMap">
        select
        COVER_ID,TEMPLET_ID,TEMPLET_NAME,COVER_IMG,UPDATE_TIME,CREATED_Time, book_label_id
        from
        book_cover
    </select>

    <select id="selectByTempletId"  parameterType="map" resultMap="BaseResultMap">
        select
        COVER_ID,TEMPLET_ID,TEMPLET_NAME,COVER_IMG,UPDATE_TIME,CREATED_Time, book_label_id
        from
        book_cover
        where 1=1
        <if test="templetId != null" >
        AND TEMPLET_ID = #{templetId}
        </if>
        and book_label_id IS NULL
    </select>

    <insert id="insert" parameterType="com.pcloud.book.book.dto.BookCoverDTO" useGeneratedKeys="true" keyProperty="COVER_ID" >
    insert into
    book_cover (COVER_ID,TEMPLET_ID,TEMPLET_NAME,COVER_IMG,CREATED_Time,UPDATE_TIME)
    values
    (#{coverId,jdbcType=BIGINT}, #{templetId,jdbcType=BIGINT}, #{templetName,jdbcType=VARCHAR},
      #{coverImg,jdbcType=VARCHAR}, now(), #{updateTime,jdbcType=TIMESTAMP})
    </insert>

    <select id="selectById" resultMap="BaseResultMap">
        SELECT
        COVER_ID,TEMPLET_ID,TEMPLET_NAME,COVER_IMG,UPDATE_TIME,CREATED_Time
        FROM
        book_cover
        WHERE
        COVER_ID = #{coverId}
    </select>

    <select id="selectByTempletIdAndCoverImg" parameterType="map" resultMap="BaseResultMap" >
        SELECT
        COVER_ID,TEMPLET_ID,TEMPLET_NAME,COVER_IMG,UPDATE_TIME,CREATED_Time
        FROM
        book_cover
        WHERE
        COVER_ID = #{coverId}
        AND
        TEMPLET_ID = #{templetId}
    </select>

    <delete id="deleteById" parameterType="string" >
        delete from
        book_cover
        where
        COVER_ID=#{coverId}
    </delete>

    <update id="updateCover" parameterType="com.pcloud.book.book.dto.BookCoverDTO" flushCache="true">
		UPDATE book_cover
		SET
		    TEMPLET_ID = #{templetId,jdbcType=VARCHAR},
			COVER_IMG = #{coverImg,jdbcType=VARCHAR},
			UPDATE_TIME = now()
		WHERE
			COVER_ID=#{coverId}
	</update>

    <select id="selectByTempletIdAndLabel"  parameterType="map" resultMap="BaseResultMap">
        SELECT
        COVER_ID,
        TEMPLET_ID,
        TEMPLET_NAME,
        COVER_IMG,
        UPDATE_TIME,
        CREATED_Time,
        book_label_id
        FROM
        book_cover
        WHERE
        TEMPLET_ID = #{templetId}
        AND (book_label_id = #{bookLabelId} OR book_label_id IS NULL)
        ORDER BY book_label_id DESC
    </select>

</mapper>