<?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.ym.mec.biz.dal.dao.SysMusicCompareWeekDataDao">
	
	<resultMap type="com.ym.mec.biz.dal.entity.SysMusicCompareWeekData" id="SysMusicCompareWeekData">
		<result column="user_id_" property="userId" />
		<result column="monday_" property="monday" />
		<result column="train_num_" property="trainNum" />
		<result column="train_days_" property="trainDays" />
		<result column="train_time_" property="trainTime" />
		<result column="beginner_max_score_" property="beginnerMaxScore" />
		<result column="advanced_max_score_" property="advancedMaxScore" />
		<result column="performer_max_score_" property="performerMaxScore" />
        <result column="tenant_id_" property="tenantId"/>
	</resultMap>
	
	
	<!-- 全查询 -->
	<select id="findAll" resultMap="SysMusicCompareWeekData">
		SELECT * FROM sys_music_compare_week_data where tenant_id_ = #{tenantId}
	</select>
	
	<!-- 向数据库增加一条记录 -->
	<insert id="insert" parameterType="com.ym.mec.biz.dal.entity.SysMusicCompareWeekData">
		<!--
		<selectKey resultClass="int" keyProperty="id" > 
		SELECT SEQ_WSDEFINITION_ID.nextval AS ID FROM DUAL 
		</selectKey>
		-->
		INSERT INTO sys_music_compare_week_data (user_id_,monday_,train_num_,train_days_,train_time_,
		                                         beginner_max_score_,beginner_max_score_id_,advanced_max_score_,advanced_max_score_id_,
		                                         performer_max_score_,performer_max_score_id_,
		                                         create_time_,update_time_,tenant_id_)
		VALUES(#{userId},#{monday},#{trainNum},#{trainDays},#{trainTime},#{beginnerMaxScore},#{beginnerMaxScoreId},
		       #{advancedMaxScore},#{advancedMaxScoreId},#{performerMaxScore},#{performerMaxScoreId},NOW(),NOW(),#{tenantId})
	</insert>
	
	<update id="update" parameterType="com.ym.mec.biz.dal.entity.SysMusicCompareWeekData">
		UPDATE sys_music_compare_week_data
		<set>
			<if test="trainNum!=null">
				train_num_ = #{trainNum},
			</if>
			<if test="trainDays!=null">
				train_days_ = #{trainDays},
			</if>
			<if test="trainTime!=null">
				train_time_ = #{trainTime},
			</if>
			<if test="beginnerMaxScore!=null">
				beginner_max_score_ = #{beginnerMaxScore},
			</if>
			<if test="beginnerMaxScoreId!=null">
				beginner_max_score_id_ = #{beginnerMaxScoreId},
			</if>
			<if test="advancedMaxScore!=null">
				advanced_max_score_ = #{advancedMaxScore},
			</if>
			<if test="advancedMaxScoreId!=null">
				advanced_max_score_id_ = #{advancedMaxScoreId},
			</if>
			<if test="performerMaxScore!=null">
				performer_max_score_ = #{performerMaxScore},
			</if>
			<if test="performerMaxScoreId!=null">
				performer_max_score_id_ = #{performerMaxScoreId},
			</if>
			update_time_ = NOW()
		</set>
		WHERE user_id_=#{userId} AND monday_ = #{monday} and tenant_id_ = #{tenantId}
	</update>
	
	<!-- 分页查询 -->
	<select id="queryPage" resultMap="SysMusicCompareWeekData" parameterType="map">
		SELECT * FROM sys_music_compare_week_data where tenant_id_ = #{tenantId} <include refid="global.limit"/>
	</select>
	
	<!-- 查询当前表的总记录数 -->
	<select id="queryCount" resultType="int">
		SELECT COUNT(*) FROM sys_music_compare_week_data where tenant_id_ = #{tenantId}
	</select>

	<select id="getWithUserAndMonday" resultMap="SysMusicCompareWeekData">
		SELECT * FROM sys_music_compare_week_data WHERE user_id_=#{userId} AND monday_=#{monday}
	</select>

	<select id="getUserTrainStat" resultType="com.ym.mec.biz.dal.dto.MusicCompareRankingDto">
		SELECT
		smcwd.user_id_ userId,
			su.avatar_ avatar,
			su.username_ studentName,
			<if test="orderType==1">
				sms.name_ musicScoreName,
				<if test="heardLevel==null">
					smcwd.advanced_max_score_ score,
				</if>
				<if test="heardLevel=='BEGINNER'">
					smcwd.beginner_max_score_ score,
				</if>
				<if test="heardLevel=='ADVANCED'">
					smcwd.advanced_max_score_ score,
				</if>
				<if test="heardLevel=='PERFORMER'">
					smcwd.performer_max_score_ score,
				</if>
			</if>
			smcwd.train_num_ trainNum,
			smcwd.train_days_ trainDays,
			smcwd.train_time_ trainTime,
			case when stu.membership_end_time_ &gt; now() then 1 else 0 end vipUser
		FROM sys_music_compare_week_data smcwd
		LEFT JOIN sys_user su ON smcwd.user_id_=su.id_
		LEFT JOIN student stu ON smcwd.user_id_ = stu.user_id_
		<if test="orderType==1">
			LEFT JOIN sys_music_score sms
			<if test="heardLevel==null">
				ON smcwd.advanced_max_score_id_ = sms.id_
			</if>
			<if test="heardLevel=='BEGINNER'">
				ON smcwd.beginner_max_score_id_ = sms.id_
			</if>
			<if test="heardLevel=='ADVANCED'">
				ON smcwd.advanced_max_score_id_ = sms.id_
			</if>
			<if test="heardLevel=='PERFORMER'">
				ON smcwd.performer_max_score_id_ = sms.id_
			</if>
		</if>
		WHERE smcwd.monday_ = #{monday}
			AND stu.user_id_ = smcwd.user_id_ and smcwd.tenant_id_ = #{tenantId}
			<if test="organId != null">
				AND su.organ_id_ = #{organId}
			</if>			<if test="orderType==1">
				<if test="heardLevel==null">
					AND smcwd.advanced_max_score_ > 0
				</if>
				<if test="heardLevel=='BEGINNER'">
					AND smcwd.beginner_max_score_ > 0
				</if>
				<if test="heardLevel=='ADVANCED'">
					AND smcwd.advanced_max_score_ > 0
				</if>
				<if test="heardLevel=='PERFORMER'">
					AND smcwd.performer_max_score_ > 0
				</if>
			</if>
			ORDER BY
			<if test="orderType==null">
				smcwd.train_time_ DESC,
			</if>
			<if test="orderType==0">
				smcwd.train_time_ DESC,
			</if>
			<if test="orderType==1">
				<if test="heardLevel==null">
					smcwd.advanced_max_score_ DESC,
				</if>
				<if test="heardLevel=='BEGINNER'">
					smcwd.beginner_max_score_ DESC,
				</if>
				<if test="heardLevel=='ADVANCED'">
					smcwd.advanced_max_score_ DESC,
				</if>
				<if test="heardLevel=='PERFORMER'">
					smcwd.performer_max_score_ DESC,
				</if>
			</if>
			<if test="orderType==2">
				smcwd.train_days_ DESC,
			</if>
			smcwd.user_id_
	</select>
</mapper>
