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

    <resultMap type="com.pcloud.book.book.entity.BookMinimumSupport" id="BookMinimumSupportMap">
        <result property="id" column="id" jdbcType="INTEGER"/>
        <result property="bookId" column="book_id" jdbcType="INTEGER"/>
        <result property="startTime" column="start_time" jdbcType="TIMESTAMP"/>
        <result property="endTime" column="end_time" jdbcType="TIMESTAMP"/>
        <result property="printAmount" column="print_amount" jdbcType="INTEGER"/>
        <result property="remark" column="remark" jdbcType="VARCHAR"/>
        <result property="createUser" column="create_user" jdbcType="INTEGER"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
        <result property="updateUser" column="update_user" jdbcType="INTEGER"/>
        <result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/>
    </resultMap>

    <resultMap id="BookMinimumSupportDTO" type="com.pcloud.book.book.dto.BookMinimumSupportDTO">
        <result property="id" column="id" jdbcType="INTEGER"/>
        <result property="bookId" column="book_id" jdbcType="INTEGER"/>
        <result property="startTime" column="start_time" jdbcType="TIMESTAMP"/>
        <result property="endTime" column="end_time" jdbcType="TIMESTAMP"/>
        <result property="printAmount" column="print_amount" jdbcType="INTEGER"/>
        <result property="remark" column="remark" jdbcType="VARCHAR"/>
        <result property="createUser" column="create_user" jdbcType="INTEGER"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
    </resultMap>

    <sql id="Base_Column_List">
        id, book_id, start_time, end_time, print_amount, remark, create_user, create_time
    </sql>

    <!--查询单个-->
    <select id="getById" resultMap="BookMinimumSupportMap">
        select
          <include refid="Base_Column_List"/>
        from book.book_minimum_support
        where id = #{id}
    </select>

    <!--新增所有列-->
    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        insert into book.book_minimum_support(book_id, start_time, end_time, print_amount, remark, create_user, create_time, update_user, update_time)
        values (#{bookId}, #{startTime}, #{endTime}, #{printAmount}, #{remark}, #{createUser}, now(), #{updateUser}, now())
    </insert>

    <!--通过主键修改数据-->
    <update id="update">
        update book.book_minimum_support
        <set>
            <if test="startTime != null">
                start_time = #{startTime},
            </if>
            <if test="endTime != null">
                end_time = #{endTime},
            </if>
            <if test="printAmount != null">
                print_amount = #{printAmount},
            </if>
            <if test="remark != null and remark != ''">
                remark = #{remark},
            </if>
            <if test="updateUser != null">
                update_user = #{updateUser},
            </if>
                update_time = now(),
        </set>
        where id = #{id}
    </update>

    <!--通过主键删除-->
    <delete id="deleteById">
        delete from book.book_minimum_support where id = #{id}
    </delete>

    <select id="getByBookId" resultMap="BookMinimumSupportDTO" parameterType="long">
        select
        <include refid="Base_Column_List"/>
        from book.book_minimum_support
        where book_id = #{bookId}
        ORDER BY start_time DESC
    </select>

    <select id="getMinimunListByMap" parameterType="map" resultMap="BookMinimumSupportDTO">
        SELECT
        <include refid="Base_Column_List"/>
        from book.book_minimum_support
        where book_id = #{bookId}
        and id != #{id}
    </select>
    <select id="getInfoByBookId" resultMap="BookMinimumSupportDTO" parameterType="java.lang.Long">
        SELECT
        <include refid="Base_Column_List"/>
        from book.book_minimum_support
        where book_id = #{bookId}
        ORDER BY start_time DESC
        LIMIT 1
    </select>

    <select id="getByParamMap" resultMap="BookMinimumSupportDTO" parameterType="map">
        SELECT
        <include refid="Base_Column_List"/>
        from book.book_minimum_support
        where book_id = #{bookId}
        <if test="date != null">
            and #{date} BETWEEN start_time AND end_time
        </if>
    </select>

    <select id="getByBookIds" parameterType="map" resultMap="BookMinimumSupportDTO">
        SELECT
        book_id,
        start_time,
        end_time
    FROM
        book_minimum_support
    WHERE
        book_id IN
        <foreach collection="bookIds" separator="," index="index" item="item" close=")" open="(">
            #{item}
        </foreach>
    ORDER BY
        create_time DESC
    </select>

</mapper>