<?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.miaxis.score.mapper.ScoreInfoMapper">

    <resultMap type="ScoreInfo" id="ScoreInfoResult">
        <result property="id"    column="id"    />
        <result property="userId"    column="user_id"    />
        <result property="type"    column="type"    />
        <result property="kskm"    column="kskm"    />
        <result property="score"    column="score"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>


    <resultMap type="ScoreInfoH5Vo" id="ScoreInfoH5Result">
        <result property="id"    column="id"    />
        <result property="userId"    column="user_id"    />
        <result property="headImage"    column="head_Image"    />
        <result property="nickName"    column="nick_name"    />
        <result property="type"    column="type"    />
        <result property="kskm"    column="kskm"    />
        <result property="score"    column="score"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>


    <sql id="selectScoreInfoVo">
        select * from score_info
    </sql>

    <select id="selectScoreInfoList" parameterType="ScoreInfo" resultMap="ScoreInfoResult">
        <include refid="selectScoreInfoVo"/>
        <where>
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="type != null  and type != ''"> and type = #{type}</if>
            <if test="kskm != null  and kskm != ''"> and kskm = #{kskm}</if>
            <if test="score != null "> and score = #{score}</if>
        </where>
    </select>


    <select id="getLast10Score" parameterType="ScoreInfo" resultType="java.lang.Integer">
        select score from
        (select score from score_info s
        <where>
            <if test="userId != null "> and user_id = #{userId}</if>
        </where>
        order by id desc limit 0,10) x order by score
    </select>

    <select id="getAvgScore" parameterType="ScoreInfo" resultType="int">
        select IFNULL(ROUND(avg(score),0),0) from score_info s
        <where>
            <if test="userId != null "> and user_id = #{userId}</if>
        </where>
    </select>



    <select id="getMaxScore" parameterType="ScoreInfo" resultType="int">
        select IFNULL(max(score),0) from score_info s
        <where>
            <if test="userId != null "> and user_id = #{userId}</if>
        </where>
    </select>


    <select id="selectScoreInfoPcList" parameterType="ScoreInfoPcDTO" resultType="com.miaxis.score.vo.ScoreInfoPcVo">
        SELECT s.id,s.user_id,u.head_image,u.nick_name,s.type,s.kskm,s.score,s.create_time
                FROM user_info u JOIN score_info s ON u.id = s.user_id
        <where>
            <if test="cityName != null and cityName != ''"> and u.city_name like concat('%', #{cityName}, '%')</if>
            <if test="areaName != null and areaName != ''"> and u.area_name like concat('%', #{areaName}, '%')</if>
            <if test="schoolName != null and schoolName != ''"> and u.school_name like concat('%', #{schoolName}, '%')</if>
            <if test="nickName != null and nickName != ''"> and u.nick_name like concat('%', #{nickName}, '%')</if>
            <if test="type != null and type != ''"> and s.type = #{type} </if>
        </where>
        order by user_id,create_time desc
    </select>

    <select id="getScoreInfoByUserId" parameterType="ScoreInfoUserIdDTO" resultMap="ScoreInfoH5Result">
    SELECT s.id,s.user_id,u.head_image,u.nick_name,s.type,s.kskm,s.score,s.create_time
    FROM user_info u JOIN score_info s ON u.id = s.user_id
    <where>
        <if test="userId != null"> and s.user_id =  #{userId} </if>
    </where>
    order by user_id,create_time desc
</select>

    <select id="selectStudentList" resultType="com.miaxis.score.vo.ScoreIStudentInfoPcVo" >
        SELECT s.user_id,head_image,nick_name,real_name,max(score) as highScore,min(score) as lowScore,count(1) as times,avg(score) as averageScore
         FROM user_info u  JOIN score_info s ON u.id = s.user_id
        <where>
            <if test="cityName != null and cityName != ''"> and u.city_name like concat('%', #{cityName}, '%')</if>
            <if test="areaName != null and areaName != ''"> and u.area_name like concat('%', #{areaName}, '%')</if>
            <if test="schoolName != null and schoolName != ''"> and u.school_name like concat('%', #{schoolName}, '%')</if>
            <if test="nickName != null and nickName != ''"> and u.nick_name like concat('%', #{nickName}, '%')</if>
            <if test="realName != null and realName != ''"> and u.real_name like concat('%', #{realName}, '%')</if>
            <!--<if test="type != null and type != ''"> and s.type = #{type} </if>-->
        </where>
        group by u.id desc
    </select>




</mapper>
