<?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.dao.ActivityEvaluationRecordDao">
    <resultMap id="BaseResultMap" type="com.yonge.cooleshow.biz.dal.entity.ActivityEvaluationRecord">
        <result column="id_" property="id" />
        <result column="activity_id_" property="activityId" />
        <result column="user_id_" property="userId" />
        <result column="evaluation_id_" property="evaluationId" />
        <result column="score_" property="score" />
        <result column="times_" property="times" />
        <result column="create_time_" property="createTime" />
    </resultMap>

    <!-- 表字段 -->
    <sql id="baseColumns">
        t.id_ as id
        , t.activity_id_ as activityId
        , t.user_id_ as userId
        , t.evaluation_id_ as evaluationId
        , t.score_ as score
        , t.times_ as times
        , t.create_time_ as createTime
    </sql>

    <select id="detail" resultType="com.yonge.cooleshow.biz.dal.vo.ActivityEvaluationRecordVo">
        SELECT
        <include refid="baseColumns"/>
        FROM activity_evaluation_record t
        where t.id_ = #{id}
    </select>

    <select id="selectPage" resultType="com.yonge.cooleshow.biz.dal.vo.ActivityEvaluationRecordVo">
        SELECT
        <include refid="baseColumns" />
        FROM activity_evaluation_record t
    </select>

    <select id="getJoinEvaluationIdList" resultType="java.lang.Long">
        select distinct evaluation_id_
        from activity_evaluation_record
        where activity_id_ = #{activityId} and user_id_ = #{userId}
    </select>

    <select id="queryRankingList" resultType="com.yonge.cooleshow.biz.dal.vo.ActivityRankingVo">
        SELECT t.* FROM
        (SELECT
            @rownum :=@rownum + 1 AS rownum,
            aer.id_,
            aer.score_ AS score,
            aer.user_id_ AS userId,
            u.avatar_ AS userAvatar,
            u.username_ AS username,
            aer.create_time_ AS joinDate,
            s.name_ AS userSubject,
            aer.times_ AS times
            FROM
            (SELECT @rownum := 0) r,
            activity_evaluation_record aer
            LEFT JOIN sys_user u ON aer.user_id_ = u.id_
            LEFT JOIN student stu ON stu.user_id_ = aer.user_id_
            LEFT JOIN `subject` s ON s.id_ = stu.subject_id_
            WHERE
            aer.activity_id_ = #{activityPlanId} AND aer.evaluation_id_ = #{activityEvaluationId} AND aer.score_ > 0
            ORDER BY
            aer.score_ DESC,
            <if test="rankRule != null and rankRule.code == 'EVALUATIONS_NUMBER'">
                times asc,
            </if>
            aer.id_ ASC
        ) t
        GROUP BY t.userId ORDER BY t.rownum ASC LIMIT #{limit}
    </select>

    <select id="queryUserRanking" resultType="com.yonge.cooleshow.biz.dal.vo.ActivityRankingVo">
		select aer.score_ score,aer.user_id_ userId,u.avatar_ userAvatar,u.username_ username, aer.create_time_ joinDate,s.name_ userSubject,aer.times_ times
		from activity_evaluation_record aer left join sys_user u on aer.user_id_ = u.id_
        left join student stu on stu.user_id_ = aer.user_id_
        left join subject s on s.id_ = stu.subject_id_
		where aer.id_ in (select * from (
		select aer.id_ from activity_evaluation_record aer where aer.activity_id_ = #{activityPlanId} and aer.evaluation_id_ = #{activityEvaluationId} and aer.user_id_ = #{userId} and aer.score_ >= 0
		order by aer.score_ desc,
        <if test="rankRule != null and rankRule.code == 'EVALUATIONS_NUMBER'">
            aer.times_ asc,
        </if>
         aer.id_ asc limit 1) a
		) 
    </select>

    <select id="queryLastestRecord" resultMap="BaseResultMap">
		select t.*
		from activity_evaluation_record t 
		where t.activity_id_ = #{activityPlanId} and t.user_id_ = #{userId} and t.score_ >= 0 order by id_ desc limit 1
    </select>

    <!--用户活动最高排名信息-->
    <select id="selectUserHighestRankingInfo"
            resultType="com.yonge.cooleshow.biz.dal.entity.ActivityEvaluationRecord">
        SELECT t.rownum, t.score, t.times, t.id, t.createTime, t.userId FROM
            (SELECT
                 @rownum :=@rownum + 1 AS rownum,
                 t1.score_ AS score,
                 t1.times_ AS times ,
                 t1.id_ AS id,
                 t1.user_id_ AS userId,
                 t1.create_time_ AS createTime,
                 t1.evaluation_id_ AS evaluationId
             FROM
                 (SELECT @rownum := 0) r,
                 activity_evaluation_record t1
             WHERE
                 t1.activity_id_ = #{activityId}
             ORDER BY
                 t1.score_ DESC,
                <if test="rankRule != null and rankRule.code == 'EVALUATIONS_NUMBER'">
                    t1.times_ asc,
                </if>
                 t1.id_ ASC
             ) t
        WHERE t.userId IN (<foreach collection="userIds" separator="," item="item">#{item}</foreach>)
        GROUP BY t.userId ORDER BY t.rownum ASC
    </select>
    <!--用户活动最高排名信息-->
</mapper>