<?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.applet.dao.impl.SpecialBookDaoImpl">

    <resultMap id="BaseResultMap" type="com.pcloud.book.applet.entity.SpecialBook">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="book_id" property="bookId" jdbcType="BIGINT"/>
        <result column="adviser_id" property="adviserId" jdbcType="BIGINT"/>
        <result column="channel_id" property="channelId" jdbcType="BIGINT"/>
        <result column="is_delete" property="isDelete" jdbcType="INTEGER"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
    </resultMap>

    <resultMap id="BaseDTOResultMap" type="com.pcloud.book.applet.dto.SpecialBookDTO">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="book_id" property="bookId" jdbcType="BIGINT"/>
        <result column="adviser_id" property="adviserId" jdbcType="BIGINT"/>
        <result column="channel_id" property="channelId" jdbcType="BIGINT"/>

    </resultMap>

    <sql id="Base_Column_List">
        id, book_id, adviser_id, channel_id, is_delete, create_time, update_time        
    </sql>

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

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

    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        INSERT INTO special_book(
            book_id, 
            adviser_id, 
            channel_id, 
            is_delete, 
            create_time, 
            update_time
        ) VALUES (
            #{bookId, jdbcType=BIGINT}, 
            #{adviserId, jdbcType=BIGINT}, 
            #{channelId, jdbcType=BIGINT}, 
            0,
             now(),
             now()
        )
    </insert>

    <insert id="batchInsert">
        INSERT INTO special_book (
        book_id,
        adviser_id,
        channel_id,
        is_delete,
        create_time,
        update_time
        ) VALUES
        <foreach collection="list" item="item" separator=",">
            (
            #{item.bookId, jdbcType=BIGINT},
            #{item.adviserId, jdbcType=BIGINT},
            #{item.channelId, jdbcType=BIGINT},
            0 ,
         now(),
         now()
            )
        </foreach>
    </insert>

    <!--通过主键修改数据-->
    <update id="update">
        UPDATE special_book
        <set>
            <if test="bookId != null">
                book_id = #{bookId},
            </if>
            <if test="adviserId != null">
                adviser_id = #{adviserId},
            </if>
            <if test="channelId != null">
                channel_id = #{channelId},
            </if>
            <if test="isDelete != null">
                is_delete = #{isDelete},
            </if>
            <if test="createTime != null">
                create_time = #{createTime},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime},
            </if>
        </set>
        WHERE id = #{id}
    </update>

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


    <select id="listSpecialBook" parameterType="map" resultType="com.pcloud.book.applet.dto.SpecialBookDTO">
        SELECT
         a.id,
         a.book_id bookId,
         b.book_name bookName,
         a.adviser_id adviserId,
         a.channel_id channelId,
         b.isbn,
         c.id bookGroupId,
         c.group_qrcode_url bookGroupUrl
        FROM special_book a
        left join book b on a.book_id = b.book_id
        left join book_group  c on a.book_id = c.book_id and a.channel_id = c.channel_id and c.is_delete = 0
        where
        a.is_delete = 0
        <if test="queryName  != null">
            and (
            b.book_name like concat("%",#{queryName},"%")
            or
            b.isbn like concat("%",#{queryName},"%")
            )
        </if>
    </select>

    <select id="listByBookIds" parameterType="list" resultType="com.pcloud.book.applet.entity.SpecialBook">
        select
        a.id,
        a.book_id bookId,
        b.book_name bookName
        from
        special_book a
        left join book b on a.book_id = b.book_id
        where
        a.book_id
        in
        <foreach collection="list" index="index" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
    </select>

</mapper>