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

    <resultMap id="BaseResultMap" type="com.pcloud.book.book.entity.BookSerialNumber">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="isbn" property="isbn" jdbcType="VARCHAR"/>
        <result column="serial_number" property="serialNumber" jdbcType="BIGINT"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
    </resultMap>
    
    <sql id="Base_Column_List">
        id, isbn, serial_number, create_time
    </sql>

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

    <select id="getByIsbn" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM book_serial_number
        WHERE isbn = #{isbn}
    </select>

    <select id="getList" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM book_serial_number
    </select>

    <insert id="insert" parameterType="com.pcloud.book.book.entity.BookSerialNumber">
        INSERT INTO book_serial_number
        (isbn, serial_number, create_time)
        VALUES
        (#{isbn, jdbcType=VARCHAR}, #{serialNumber, jdbcType=BIGINT},NOW())
    </insert>
    
    <insert id="batchInsert">
        INSERT INTO book_serial_number (
            isbn, 
            serial_number, 
            create_time
        ) VALUES 
        <foreach collection="list" item="item" separator=",">
        (
            #{item.isbn, jdbcType=VARCHAR}, 
            #{item.serialNumber, jdbcType=BIGINT}, 
            #{item.markId, jdbcType=VARCHAR},
            NOW()
        )
        </foreach>
    </insert>

    <!--通过主键修改数据-->
    <update id="update" parameterType="com.pcloud.book.book.entity.BookSerialNumber">
        UPDATE book_serial_number SET serial_number = serial_number + 1 WHERE isbn = #{isbn}
        <selectKey resultType="Long" keyProperty="serialNumber" order="AFTER">
            SELECT serial_number FROM book_serial_number WHERE isbn = #{isbn}
        </selectKey>
    </update>

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

</mapper>
