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

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

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

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

    <select id="getList" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM applet_book_ignor
        where is_delete = 0
        order by create_time desc
    </select>

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

    <insert id="batchInsert">
        INSERT INTO applet_book_ignor (
        book_id,
        channel_id,
        adviser_id,
        book_adviser_id,
        create_time,
        update_time,
        is_delete
        ) VALUES
        <foreach collection="list" item="item" separator=",">
            (
            #{item.bookId, jdbcType=BIGINT},
            #{item.channelId, jdbcType=BIGINT},
            #{item.adviserId, jdbcType=BIGINT},
            #{item.bookAdviserId, jdbcType=BIGINT},
            #{item.createTime, jdbcType=TIMESTAMP},
            #{item.updateTime, jdbcType=TIMESTAMP},
            #{item.isDelete, jdbcType=INTEGER}
            )
        </foreach>
    </insert>

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

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

    <!--通过主键软删除-->
    <delete id="softDeleteById">
        update applet_book_ignor set is_delete  = 1, update_time = now() where id = #{id}
    </delete>

    <select id="getAllIgnorBookIds" resultType="long">
        select
        distinct book_id
        from
        applet_book_ignor
        where
        is_delete = 0
    </select>


</mapper>