<?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" />
        <result property="resourceCount" column="resource_count" jdbcType="INTEGER"/>
    </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, resource_count
    </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, resource_count)
        values (#{wechatUserId}, #{bookId}, #{channelId}, #{adviserId}, 0, #{accountSettingId}, NOW(), NOW(), #{resourceCount})
    </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>
        and subscribe_state = 1
    </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}
        and subscribe_state = 1
    </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}
        and subscribe_state = 1
    </select>

    <select id="getBookBySendState" resultMap="AnswerSubscribeMap" parameterType="integer">
        SELECT
        <include refid="Base_Column_List"/>
        FROM book.answer_subscribe
        WHERE send_state = #{sendState}
        and
        subscribe_state = 1
        group by book_id, channel_id, adviser_id
    </select>

    <select id="getSubscribeCount4Adviser" parameterType="map" resultType="com.pcloud.book.applet.dto.AnswerSubscribeCountDto">
        SELECT
            adviser_id adviserId,
            COUNT(DISTINCT wechat_user_id) userCount,
            book_id bookId,
            channel_id channelId
        FROM
            answer_subscribe
        WHERE
            adviser_id>0
            and
            subscribe_state = 1
        GROUP BY
            book_id, channel_id, adviser_id
        HAVING
            COUNT(DISTINCT wechat_user_id) >= #{limit}
    </select>

    <select id="getUserCountByBookAdviser" resultType="integer" parameterType="com.pcloud.book.applet.entity.AnswerSubscribe">
        SELECT  COUNT(DISTINCT wechat_user_id)
        FROM book.answer_subscribe
        WHERE book_id = #{bookId}
        and channel_id = #{channelId}
        and adviser_id = #{adviserId}
        and  subscribe_state = 1
    </select>

    <select id="cancelAnswerSubscribe" parameterType="com.pcloud.book.applet.dto.AnswerSubscribeDTO">
        update
        answer_subscribe
        set
        subscribe_state = 0,
        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>
        and subscribe_state = 1
    </select>
</mapper>