<?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.AppletBookScoreMapper">
    <resultMap id="BaseResultMap" type="com.pcloud.book.applet.entity.AppletBookScore">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="name" jdbcType="VARCHAR" property="name"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
    </resultMap>
    <sql id="Base_Column_List">
        id
        , `name`, create_time, update_time
    </sql>
    <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from applet_book_score
        where id = #{id,jdbcType=BIGINT}
    </select>
    <delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
        delete
        from applet_book_score
        where id = #{id,jdbcType=BIGINT}
    </delete>
    <insert id="insert" keyColumn="id" keyProperty="id" parameterType="com.pcloud.book.applet.entity.AppletBookScore"
            useGeneratedKeys="true">
        insert into applet_book_score (`name`, create_time, update_time)
        values (#{name,jdbcType=VARCHAR}, #{createTime,jdbcType=TIMESTAMP}, #{updateTime,jdbcType=TIMESTAMP})
    </insert>
    <insert id="insertSelective" keyColumn="id" keyProperty="id"
            parameterType="com.pcloud.book.applet.entity.AppletBookScore" useGeneratedKeys="true">
        insert into applet_book_score
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="name != null">
                `name`,
            </if>
            <if test="createTime != null">
                create_time,
            </if>
            <if test="updateTime != null">
                update_time,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="name != null">
                #{name,jdbcType=VARCHAR},
            </if>
            <if test="createTime != null">
                #{createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="updateTime != null">
                #{updateTime,jdbcType=TIMESTAMP},
            </if>
        </trim>
    </insert>
    <update id="updateByPrimaryKeySelective" parameterType="com.pcloud.book.applet.entity.AppletBookScore">
        update applet_book_score
        <set>
            <if test="name != null">
                `name` = #{name,jdbcType=VARCHAR},
            </if>
            <if test="createTime != null">
                create_time = #{createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime,jdbcType=TIMESTAMP},
            </if>
        </set>
        where id = #{id,jdbcType=BIGINT}
    </update>
    <update id="updateByPrimaryKey" parameterType="com.pcloud.book.applet.entity.AppletBookScore">
        update applet_book_score
        set `name`      = #{name,jdbcType=VARCHAR},
            create_time = #{createTime,jdbcType=TIMESTAMP},
            update_time = #{updateTime,jdbcType=TIMESTAMP}
        where id = #{id,jdbcType=BIGINT}
    </update>


    <select id="listTags" resultType="com.pcloud.book.applet.dto.AppletBookScoreTagInfoDTO">
        SELECT a.id                   scoreId,
               a.name                 scoreName,
               b.id                   tagId,
               b.name                 tagName,
               c.update_time chosenTime,
               d.id subTagId,
               d.`name` subTagName,
               IF(c.id IS NULL, 0, 1) chosen
        FROM applet_book_score a
                 LEFT JOIN applet_book_score_tag b ON a.id = b.score_id
                 LEFT JOIN (SELECT score_tag_id, id, update_time
                            FROM applet_book_user_score
                            WHERE book_id = ${bookId}
                              AND channel_id = ${channelId}
                              AND wechat_user_id = ${wechatUserId}
                              AND adviser_id = ${adviserId}
        ) c ON c.score_tag_id = b.id
        LEFT JOIN applet_book_score_sub_tag d on d.tag_id = b.id
        ORDER BY a.id, b.id, d.id
    </select>
</mapper>