<?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.javaclimb.music.dao.RankMapper">
    <resultMap id="BaseResultMap" type="com.javaclimb.music.domain.Rank" >
        <id column="id" jdbcType="INTEGER" property="id"/>
        <result column="song_list_id" jdbcType="INTEGER" property="songListId"/>
        <result column="consumer_id" jdbcType="INTEGER" property="consumerId"/>
        <result column="score" jdbcType="INTEGER" property="score"/>
    </resultMap>

    <insert id="insert" parameterType="com.javaclimb.music.domain.Rank">
        insert into rank
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">
                id,
            </if>
            <if test="songListId != null">
                song_list_id,
            </if>
            <if test="consumerId != null">
                consumer_id,
            </if>
            <if test="score != null">
                score,
            </if>
        </trim>
        <trim prefix=" values (" suffix=")" suffixOverrides=",">
            <if test="id != null">
                #{id},
            </if>
            <if test="songListId != null">
                #{songListId},
            </if>
            <if test="consumerId != null">
                #{consumerId},
            </if>
            <if test="score != null">
                #{score},
            </if>
        </trim>        
    </insert>

    <select id="selectScoreSum" resultType="java.lang.Integer" parameterType="java.lang.Integer">
        select COALESCE(sum(score),0) as score
        from rank
        where song_list_id = #{songListId}
    </select>

    <select id="selectRankNum" resultType="java.lang.Integer" parameterType="java.lang.Integer">
        select COALESCE(count(id),0) as num
        from rank
        where song_list_id = #{songListId}
    </select>
</mapper>
















