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

    <resultMap type="com.pcloud.book.applet.entity.AnswerSubscribeAdviser" id="AnswerSubscribeAdviserMap">
        <result property="id" column="id" jdbcType="INTEGER"/>
        <result property="adviserId" column="adviser_id" jdbcType="INTEGER"/>
        <result property="sendState" column="send_state" jdbcType="INTEGER"/>
        <result property="sendType" column="send_type" jdbcType="INTEGER"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
        <result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/>
        <result property="bookId" column="book_id" jdbcType="INTEGER"/>
        <result property="channelId" column="channel_id" jdbcType="INTEGER"/>
    </resultMap>

    <!--查询单个-->
    <select id="getById" resultMap="AnswerSubscribeAdviserMap">
        select
          id, adviser_id, send_state, send_type, create_time, update_time, book_id, channel_id
        from book.answer_subscribe_adviser
        where id = #{id}
    </select>

    <!--通过实体作为筛选条件查询-->
    <select id="queryAll" resultMap="AnswerSubscribeAdviserMap">
        select
          id, adviser_id, send_state, send_type, create_time, update_time, book_id, channel_id
        from book.answer_subscribe_adviser
        <where>
            <if test="adviserId != null">
                and adviser_id = #{adviserId}
            </if>
            <if test="bookId != null">
                and book_id = #{bookId}
            </if>
            <if test="channelId != null">
                and channel_id = #{channelId}
            </if>
            <if test="sendState != null">
                and send_state = #{sendState}
            </if>
            <if test="sendType != null">
                and send_type = #{sendType}
            </if>
        </where>
        limit 1
    </select>

    <!--新增所有列-->
    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        insert into book.answer_subscribe_adviser(adviser_id, send_state, send_type, create_time, update_time,book_id, channel_id)
        values (#{adviserId}, #{sendState}, #{sendType}, now(), now(), #{bookId}, #{channelId})
    </insert>

    <!--通过主键修改数据-->
    <update id="update">
        update book.answer_subscribe_adviser
        <set>
            <if test="sendState != null">
                send_state = #{sendState},
            </if>
            <if test="sendType != null">
                send_type = #{sendType},
            </if>
                update_time = now(),
        </set>
        where id = #{id}
    </update>

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

    <select id="getRecord4Send" resultMap="AnswerSubscribeAdviserMap">
        select
          id, adviser_id, send_state, send_type, create_time, update_time, book_id, channel_id
        from book.answer_subscribe_adviser
        WHERE send_state = 0
        AND adviser_id>0
        GROUP BY
            book_id, channel_id, adviser_id
    </select>

</mapper>