<?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.yonge.cooleshow.biz.dal.mapper.MusicCompareRecordStatMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.yonge.cooleshow.biz.dal.entity.MusicCompareRecordStat">
        <id column="id_" property="id" />
        <result column="music_sheet_id_" property="musicSheetId" />
        <result column="client_id_" property="clientId" />
        <result column="user_id_" property="userId" />
        <result column="train_duration_" property="trainDuration" />
        <result column="train_frequency_" property="trainFrequency" />
        <result column="evaluate_duration_" property="evaluateDuration" />
        <result column="evaluate_frequency_" property="evaluateFrequency" />
        <result column="recent_time_" property="recentTime" />
        <result column="create_time_" property="createTime" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id_, music_sheet_id_, client_id_, user_id_, train_duration_, train_frequency_, evaluate_duration_, evaluate_frequency_, recent_time_, create_time_
    </sql>

    <!--曲目统计查询-->
    <resultMap id="MusicCompareRecordPageResultMap"
               type="com.yonge.cooleshow.biz.dal.wrapper.music.MusicCompareWrapper$RecordInfo">
        <id column="id_" property="id" />
        <result column="music_sheet_id_" property="musicSheetId" />
        <result column="train_duration_" property="trainDuration" />
        <result column="train_frequency_" property="trainFrequency" />
        <result column="evaluate_duration_" property="evaluateDuration" />
        <result column="evaluate_frequency_" property="evaluateFrequency" />
        <result column="recent_time_" property="recentTime" />
        <result column="music_sheet_name_" property="musicName" />
        <result column="composer_" property="composer" />
        <result column="music_tag_" property="musicTag" />
    </resultMap>
    <select id="selectMusicCompareRecordPage" resultMap="MusicCompareRecordPageResultMap">
        SELECT t1.id_, t1.music_sheet_id_, t1.user_id_, t1.train_duration_, t1.train_frequency_, t1.evaluate_duration_, t1.evaluate_frequency_, t1.recent_time_, t2.music_sheet_name_, t2.composer_, t2.music_tag_
        FROM music_compare_record_stat t1 LEFT JOIN music_sheet t2 ON t1.music_sheet_id_ = t2.id_
        <where>
            <if test="record.clientType != null">
                AND t1.client_id_ = #{record.clientType}
            </if>
            <if test="record.userId != null">
                AND t1.user_id_ = #{record.userId}
            </if>
            <if test="record.startTime != null and record.endTime != null">
                AND (t1.recent_time_ >= #{record.startTime} AND  t1.recent_time_ &lt;= #{record.endTime})
            </if>
            <if test="record.musicId != null">
                AND t2.id_ = #{record.musicId}
            </if>
            <if test="record.musicName != null">
                AND t2.music_sheet_name_ LIKE '%${record.musicName}%'
            </if>
        </where>
        ORDER BY t1.${record.compareField} ${record.sortType}
    </select>
    <!--曲目统计查询-->

    <!--用户曲目统计汇总-->
    <select id="selectMusicCompareRecordStatInfo"
            resultType="com.yonge.cooleshow.biz.dal.wrapper.StatGroupWrapper">
        SELECT t1.user_id_ AS id, COUNT(DISTINCT t1.music_sheet_id_) AS musicNum,
                <if test="record.trainMusic != null">
                   SUM(t1.train_duration_) AS durationNum, SUM(t1.train_frequency_) AS frequencyNum
                </if>
                <if test="record.evaluateMusic != null">
                   SUM(t1.evaluate_duration_) AS durationNum, SUM(t1.evaluate_frequency_) AS frequencyNum
                </if>
        FROM music_compare_record_stat t1
        <where>
            <if test="record.clientType != null">
                AND t1.client_id_ = #{record.clientType}
            </if>
            <if test="record.userId != null">
                AND t1.user_id_ = #{record.userId}
            </if>
            <if test="record.trainMusic != null">
                AND t1.train_frequency_ > 0
            </if>
            <if test="record.evaluateMusic != null">
                AND t1.evaluate_frequency_ > 0
            </if>
        </where>
        GROUP BY t1.user_id_
    </select>
    <!--用户曲目统计汇总-->

    <!--曲目评测记录查询-->
    <resultMap id="MusicCompareEvaluatePageResultMap"
               type="com.yonge.cooleshow.biz.dal.wrapper.music.MusicCompareWrapper$RecordEvaluate">
        <id column="id_" property="id" />
        <result column="score_" property="score" />
        <result column="intonation_" property="intonation" />
        <result column="cadence_" property="cadence" />
        <result column="integrity_" property="integrity" />
        <result column="heard_level_" property="heardLevel" />
        <result column="create_time_" property="createTime" />
    </resultMap>
    <select id="selectMusicCompareEvaluatePage" resultMap="MusicCompareEvaluatePageResultMap">
        SELECT t1.id_, t1.score_, t1.intonation_, t1.cadence_, t1.integrity_, t1.heard_level_, t1.create_time_
        FROM sys_music_compare_record t1
        <where>
            <if test="record.clientType != null">
                AND t1.client_id_ = #{record.clientType}
            </if>
            <if test="record.userId != null">
                AND t1.user_id_ = #{record.userId}
            </if>
            <if test="record.musicId != null">
                AND t1.music_sheet_id_ = #{record.musicId}
            </if>
            <if test="record.heardLevel != null">
                AND t1.heard_level_ = #{record.heardLevel}
            </if>
            <if test="record.scoreData != null">
                AND t1.score_data_ != ''
            </if>
            <if test="record.feature != null">
                AND t1.feature_ = #{record.feature}
            </if>
            <if test="record.startTime != null and record.endTime != null">
                AND (t1.create_time_ >= #{record.startTime} AND t1.create_time_ &lt;= #{record.endTime})
            </if>
        </where>
    </select>
    <!--曲目评测记录查询-->
</mapper>
