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

    <resultMap type="com.pcloud.book.applet.entity.AnswerSubscribe" id="AnswerSubscribeMap">
        <result property="id" column="id" jdbcType="INTEGER"/>
        <result property="wechatUserId" column="wechat_user_id" jdbcType="INTEGER"/>
        <result property="bookId" column="book_id" jdbcType="INTEGER"/>
        <result property="channelId" column="channel_id" jdbcType="INTEGER"/>
        <result property="adviserId" column="adviser_id" jdbcType="INTEGER"/>
        <result property="sendState" column="send_state" jdbcType="INTEGER"/>
        <result property="accountSettingId" column="account_setting_id" jdbcType="INTEGER"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
        <result property="updateTime" column="update_time"  jdbcType="TIMESTAMP" />
    </resultMap>

    <sql id="Base_Column_List">
       id, wechat_user_id, book_id, channel_id, adviser_id, send_state, account_setting_id, create_time, update_time
    </sql>

    <!--新增所有列-->
    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        insert into book.answer_subscribe(wechat_user_id, book_id, channel_id, adviser_id, send_state,
         account_setting_id, create_time, update_time)
        values (#{wechatUserId}, #{bookId}, #{channelId}, #{adviserId}, 0, #{accountSettingId}, NOW(), NOW())
    </insert>


    <update id="updateSendState" parameterType="com.pcloud.book.applet.entity.AnswerSubscribe">
        UPDATE book.answer_subscribe
        SET send_state = #{sendState},
        update_time = NOW()
        WHERE
        book_id = #{bookId}
        and channel_id = #{channelId}
        and adviser_id = #{adviserId}
        <if test="wechatUserId != null">
            and wechat_user_id = #{wechatUserId}
        </if>
    </update>

    <select id="getRecordByUserBook" parameterType="com.pcloud.book.applet.entity.AnswerSubscribe" resultMap="AnswerSubscribeMap">
        SELECT <include refid="Base_Column_List"/>
        FROM book.answer_subscribe
        WHERE wechat_user_id = #{wechatUserId}
        and  book_id = #{bookId}
        and channel_id = #{channelId}
        and adviser_id = #{adviserId}
    </select>

    <select id="getBySendStateAndBook" resultMap="AnswerSubscribeMap" parameterType="com.pcloud.book.applet.entity.AnswerSubscribe">
        SELECT <include refid="Base_Column_List"/>
        FROM book.answer_subscribe
        WHERE send_state = #{sendState}
        and  book_id = #{bookId}
        and channel_id = #{channelId}
        and adviser_id = #{adviserId}
    </select>

    <select id="getBookBySendState" resultType="com.pcloud.book.applet.entity.AnswerSubscribe" parameterType="integer">
        SELECT
        book_id bookId,
        channel_id channelId,
        adviser_id adviserId
        FROM book.answer_subscribe
        WHERE send_state = #{sendState}
        group by book_id, channel_id, adviser_id
    </select>

</mapper>