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

    <resultMap type="com.pcloud.book.book.entity.BookLink" id="BookLinkMap">
        <result property="id" column="id" jdbcType="INTEGER"/>
        <result property="bookId" column="book_id" jdbcType="INTEGER"/>
        <result property="linkFrom" column="link_from" jdbcType="INTEGER"/>
        <result property="linkUrl" column="link_url" jdbcType="VARCHAR"/>
        <result property="createUser" column="create_user" jdbcType="INTEGER"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
        <result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/>
    </resultMap>

    <!--查询单个-->
    <select id="getById" resultMap="BookLinkMap">
        select
          id, book_id, link_from, link_url, create_user, create_time, update_time
        from book.book_link
        where id = #{id}
    </select>

    <!--新增所有列-->
    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        insert into book.book_link(book_id, link_from, link_url, create_user, create_time, update_time)
        values (#{bookId}, #{linkFrom}, #{linkUrl}, #{createUser}, now(), now())
    </insert>

    <!--通过主键修改数据-->
    <update id="update">
        update book.book_link
        <set>
            <if test="bookId != null">
                book_id = #{bookId},
            </if>
            <if test="linkFrom != null">
                link_from = #{linkFrom},
            </if>
            <if test="linkUrl != null and linkUrl != ''">
                link_url = #{linkUrl},
            </if>
                update_time = now(),
        </set>
        where id = #{id}
    </update>

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

    <select id="getByBookIdAndAdviser" resultMap="BookLinkMap" parameterType="map">
        select
        id, book_id, link_from, link_url, create_user
        from book.book_link
        where book_id = #{bookId}
        and create_user = #{createUser}
    </select>

    <select id="getBookLinkMap" resultMap="BookLinkMap" parameterType="map">
        select
        id, book_id, link_from, link_url, create_user
        from book.book_link
        where
        create_user = #{createUser}
        and book_id in
        <foreach collection="bookIds" open="(" close=")" separator="," index="index" item="item">
            #{item}
        </foreach>
    </select>

</mapper>