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

    <resultMap type="com.pcloud.book.applet.entity.AnswerSubscribePopup" id="AnswerSubscribePopupMap">
        <result property="id" column="id" jdbcType="INTEGER"/>
        <result property="wechatUserId" column="wechat_user_id" jdbcType="INTEGER"/>
        <result property="accountSettingId" column="account_setting_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="subscribeType" column="subscribe_type" jdbcType="INTEGER"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
        <result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/>
    </resultMap>

    <!--查询单个-->
    <select id="getById" resultMap="AnswerSubscribePopupMap">
        select
          id, wechat_user_id, account_setting_id, book_id, channel_id, adviser_id, subscribe_type, create_time, update_time
        from book.answer_subscribe_popup
        where id = #{id}
    </select>


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

    <!--通过主键修改数据-->
    <update id="update">
        update book.answer_subscribe_popup
        <set>
            <if test="wechatUserId != null">
                wechat_user_id = #{wechatUserId},
            </if>
            <if test="accountSettingId != null">
                account_setting_id = #{accountSettingId},
            </if>
            <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="subscribeType != null">
                subscribe_type = #{subscribeType},
            </if>
                update_time = now(),
        </set>
        where id = #{id}
    </update>

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


    <select id="getLastRecord" resultMap="AnswerSubscribePopupMap" parameterType="long">
        SELECT
            id, wechat_user_id, account_setting_id, book_id, channel_id, adviser_id, subscribe_type
        FROM
            answer_subscribe_popup
        WHERE
            wechat_user_id = #{wechatUserId}
        ORDER BY
            create_time DESC
        LIMIT 1
    </select>
</mapper>