<?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.mapper.AppletBookUserScoreMapper">
    <resultMap id="BaseResultMap" type="com.pcloud.book.applet.entity.AppletBookUserScore">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="wechat_user_id" jdbcType="BIGINT" property="wechatUserId"/>
        <result column="book_id" jdbcType="BIGINT" property="bookId"/>
        <result column="channel_id" jdbcType="BIGINT" property="channelId"/>
        <result column="adviser_id" jdbcType="BIGINT" property="adviserId"/>
        <result column="score_tag_id" jdbcType="BIGINT" property="scoreTagId"/>
        <result column="sub_tag_id" jdbcType="INTEGER" property="subTagId"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
    </resultMap>
    <sql id="Base_Column_List">
        id
        , wechat_user_id, book_id, channel_id, adviser_id, score_tag_id, sub_tag_id, create_time, update_time
    </sql>
    <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from applet_book_user_score
        where id = #{id,jdbcType=BIGINT}
    </select>
    <delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
        delete
        from applet_book_user_score
        where id = #{id,jdbcType=BIGINT}
    </delete>
    <insert id="insert" keyColumn="id" keyProperty="id"
            parameterType="com.pcloud.book.applet.entity.AppletBookUserScore" useGeneratedKeys="true">
        insert into applet_book_user_score (wechat_user_id, book_id, channel_id,
                                            adviser_id, score_tag_id, sub_tag_id, create_time,
                                            update_time)
        values (#{wechatUserId,jdbcType=BIGINT}, #{bookId,jdbcType=BIGINT}, #{channelId,jdbcType=BIGINT},
                #{adviserId,jdbcType=BIGINT}, #{scoreTagId,jdbcType=BIGINT}, #{subTagId,jdbcType=INTEGER}, NOW(), NOW())
    </insert>
    <insert id="insertSelective" keyColumn="id" keyProperty="id"
            parameterType="com.pcloud.book.applet.entity.AppletBookUserScore" useGeneratedKeys="true">
        insert into applet_book_user_score
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="wechatUserId != null">
                wechat_user_id,
            </if>
            <if test="bookId != null">
                book_id,
            </if>
            <if test="channelId != null">
                channel_id,
            </if>
            <if test="adviserId != null">
                adviser_id,
            </if>
            <if test="scoreTagId != null">
                score_tag_id,
            </if>
            <if test="subTagId != null">
                sub_tag_id,
            </if>
            create_time,
            update_time
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="wechatUserId != null">
                #{wechatUserId,jdbcType=BIGINT},
            </if>
            <if test="bookId != null">
                #{bookId,jdbcType=BIGINT},
            </if>
            <if test="channelId != null">
                #{channelId,jdbcType=BIGINT},
            </if>
            <if test="adviserId != null">
                #{adviserId,jdbcType=BIGINT},
            </if>
            <if test="scoreTagId != null">
                #{scoreTagId,jdbcType=BIGINT},
            </if>
            <if test="subTagId != null">
                #{subTagId,jdbcType=INTEGER},
            </if>
            NOW(),
            NOW()
        </trim>
    </insert>
    <update id="updateByPrimaryKeySelective" parameterType="com.pcloud.book.applet.entity.AppletBookUserScore">
        update applet_book_user_score
        <set>
            <if test="wechatUserId != null">
                wechat_user_id = #{wechatUserId,jdbcType=BIGINT},
            </if>
            <if test="bookId != null">
                book_id = #{bookId,jdbcType=BIGINT},
            </if>
            <if test="channelId != null">
                channel_id = #{channelId,jdbcType=BIGINT},
            </if>
            <if test="adviserId != null">
                adviser_id = #{adviserId,jdbcType=BIGINT},
            </if>
            <if test="scoreTagId != null">
                score_tag_id = #{scoreTagId,jdbcType=BIGINT},
            </if>
            <if test="subTagId != null">
                sub_tag_id = #{subTagId,jdbcType=INTEGER},
            </if>
            update_time = NOW()
        </set>
        where id = #{id,jdbcType=BIGINT}
    </update>
    <update id="updateByPrimaryKey" parameterType="com.pcloud.book.applet.entity.AppletBookUserScore">
        update applet_book_user_score
        set wechat_user_id = #{wechatUserId,jdbcType=BIGINT},
            book_id        = #{bookId,jdbcType=BIGINT},
            channel_id     = #{channelId,jdbcType=BIGINT},
            adviser_id     = #{adviserId,jdbcType=BIGINT},
            score_tag_id   = #{scoreTagId,jdbcType=BIGINT},
            sub_tag_id   = #{subTagId,jdbcType=INTEGER},
            update_time    = NOW()
        where id = #{id,jdbcType=BIGINT}
    </update>

    <select id="selectPrimaryKey" resultType="java.lang.Long">
        select id
        from applet_book_user_score
        where wechat_user_id = #{wechatUserId,jdbcType=BIGINT}
          and book_id = #{bookId,jdbcType=BIGINT}
          and channel_id = #{channelId,jdbcType=BIGINT}
          and adviser_id = #{adviserId,jdbcType=BIGINT}
          and score_tag_id = #{scoreTagId,jdbcType=BIGINT}
    </select>

    <select id="getSubTagChosen" resultType="com.pcloud.book.applet.dto.AppletBookScoreTagInfoDTO">
        select
            score_tag_id tagId,
            sub_tag_id subTagId
        from applet_book_user_score
        where wechat_user_id = #{wechatUserId,jdbcType=BIGINT}
          and book_id = #{bookId,jdbcType=BIGINT}
          and channel_id = #{channelId,jdbcType=BIGINT}
          and adviser_id = #{adviserId,jdbcType=BIGINT}
    </select>

    <select id="listBookUserScore" resultType="com.pcloud.book.applet.dto.AppletBookUserScoreExportDTO">
        SELECT
            c.id scoreId,
            c.`name` scoreName,
            b.id tagId,
            b.`name` tagName,
            d.id subTagId,
            d.`name` subTagName,
            a.wechat_user_id wechatUserId,
            a.book_id bookId,
            a.adviser_id adviserId,
            a.channel_id channelId,
            a.update_time updateTime
        FROM
            applet_book_user_score a
                LEFT JOIN applet_book_score_tag b ON a.score_tag_id = b.id
                LEFT JOIN applet_book_score c ON b.score_id = c.id
                LEFT JOIN applet_book_score_sub_tag d ON d.id = a.sub_tag_id
    </select>
    <select id="listTagUser" resultType="com.pcloud.book.applet.dto.AppletBookTagUserExportDTO">
        SELECT
            t1.`scoreName`,
            t1.`tagName`,
            t1.`subTagName`,
            count( t2.wechat_user_id ) userCount
        FROM
            (
                SELECT
                    a.`name` scoreName,
                    b.`name` tagName,
                    IFNULL( c.`name`, '' ) subTagName,
                    b.id tagId,
                    c.id subId
                FROM
                    applet_book_score a
                        LEFT JOIN applet_book_score_tag b ON a.id = b.score_id
                        LEFT JOIN applet_book_score_sub_tag c ON b.id = c.tag_id
            ) t1
                LEFT JOIN applet_book_user_score t2 ON t1.tagId = t2.score_tag_id
                AND IFNULL( t1.subId, 0 ) = IFNULL( t2.sub_tag_id, 0 )
        GROUP BY
            t1.`scoreName`,
            t1.`tagName`,
            t1.`subTagName`
    </select>

    <delete id="deleteChosen">
        DELETE FROM
            applet_book_user_score
        WHERE
                score_tag_id IN ( SELECT id FROM applet_book_score_tag WHERE score_id NOT IN ( SELECT DISTINCT score_id FROM applet_book_score_tag WHERE id = #{scoreTagId,jdbcType=BIGINT} ) )
          AND wechat_user_id = #{wechatUserId,jdbcType=BIGINT}
          and book_id = #{bookId,jdbcType=BIGINT}
          and channel_id = #{channelId,jdbcType=BIGINT}
          and adviser_id = #{adviserId,jdbcType=BIGINT}
    </delete>
    <delete id="deleteChosenTag">
        DELETE FROM
            applet_book_user_score
        WHERE
                score_tag_id != #{scoreTagId,jdbcType=BIGINT}
          AND wechat_user_id = #{wechatUserId,jdbcType=BIGINT}
          and book_id = #{bookId,jdbcType=BIGINT}
          and channel_id = #{channelId,jdbcType=BIGINT}
          and adviser_id = #{adviserId,jdbcType=BIGINT}
    </delete>


</mapper>