<?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.template.mapper.SmartScoreMapper">


    <resultMap type="com.template.model.vo.ScorePageVo" id="smartScorePageMap">
        <result property="id" column="id"/>
        <result property="semester" column="semester"/>
        <result property="semesterId" column="semester_id"/>
        <result property="grade" column="grade"/>
        <result property="gradeId" column="grade_id"/>
        <result property="schoolClass" column="school_class"/>
        <result property="schoolClassId" column="school_class_id"/>
        <result property="subjectId" column="subject_id"/>
        <result property="examType" column="exam_type"/>
        <result property="name" column="name"/>
        <result property="userId" column="user_id"/>
        <result property="cardNo" column="card_no"/>
        <result property="score" column="score"/>
    </resultMap>
    <select id="querySmartScorePage" resultType="com.template.model.vo.ScorePageVo" resultMap="smartScorePageMap">
        select ss.semester_id,ss.user_id,ss.id,ss.semester,ss.grade_id,ss.school_class_id,ss.grade,ss.school_class,ss.subject_id,ss.exam_type,su.name,ss.card_no,ss.score from
        smart_score ss
        left join smart_user su on su.deleted = 0 and su.id = ss.user_id
        where ss.deleted = 0
        <if test="name != null and name != ''">
            and (ss.name like '%' #{name} '%' or ss.card_no like '%' #{name} '%')
        </if>
        <if test="semester != null and semester != ''">
            and ss.semester like '%' #{semester} '%'
        </if>
        <if test="userGradeId != null">
            and ss.grade_id = #{userGradeId}
        </if>
        <if test="grade != null and grade != ''">
            and ss.grade like '%' #{grade} '%'
        </if>
        <if test="userSchoolClass != null">
            and ss.school_class_id = #{userSchoolClass}
        </if>
        <if test="schoolClass != null and schoolClass != ''">
            and ss.school_class like '%' #{schoolClass} '%'
        </if>
        <if test="subject != null and subject != ''">
            and ss.subject_id = #{subject}
        </if>
        <if test="examType != null and examType != ''">
            and ss.exam_type = #{examType}
        </if>
        <if test="MinScore != null and MinScore != ''">
            and ss.score >= #{MinScore}
        </if>
        <if test="MaxScore != null and MaxScore != ''">
            and ss.score &lt;= #{MaxScore}
        </if>
    </select>


    <select id="querySmartScores" resultType="com.template.model.vo.ScorePageVo" resultMap="smartScorePageMap">
        select ss.semester_id,ss.user_id,ss.id,ss.semester,ss.grade_id,ss.grade,ss.school_class_id,ss.school_class,ss.subject_id,ss.exam_type,su.name,ss.card_no,ss.score from
        smart_score ss
        left join smart_user su on su.deleted = 0 and su.id = ss.user_id
        where ss.deleted = 0
        <if test="name != null and name != ''">
            and (ss.name like '%' #{name} '%' or ss.card_no like '%' #{name} '%')
        </if>
        <if test="semester != null and semester != ''">
            and ss.semester like '%' #{semester} '%'
        </if>
        <if test="grade != null and grade != ''">
            and ss.grade like '%' #{grade} '%'
        </if>
        <if test="schoolClass != null and schoolClass != ''">
            and ss.school_class like '%' #{schoolClass} '%'
        </if>
        <if test="subject != null and subject != ''">
            and ss.subject_id = #{subject}
        </if>
        <if test="examType != null and examType != ''">
            and ss.exam_type = #{examType}
        </if>
        <if test="MinScore != null and MinScore != ''">
            and ss.score >= #{MinScore}
        </if>
        <if test="MaxScore != null and MaxScore != ''">
            and ss.score &lt;= #{MaxScore}
        </if>
    </select>

    <select id="getAverageScore" resultType="java.lang.Double">
        SELECT ROUND(AVG(score), 2) as score
        FROM `smart_score`
        WHERE user_id = #{userId}
          AND subject_id = #{smartSemesterId}
          AND semester_id = #{semesterId} AND deleted=0

    </select>

    <select id="getStudentExamName" resultType="com.template.model.vo.StudentExamNameVo">
        SELECT
            ss.semester_id as semesterId,
            ss.semester as semester,
            ss.exam_type as examTypeId,
            me.`name` as examTypeName ,
            CONCAT(ss.semester,me.`name`) as totalName
        FROM
            `smart_score` ss
                LEFT JOIN smart_examtype me ON ss.exam_type = me.id
        where  ss.deleted=0 AND ss.user_id=#{userId}
        GROUP BY
            ss.semester_id,
            ss.exam_type
    </select>
    <select id="getHistoricalExam" resultType="com.template.model.vo.HistoricalExamVo">
        SELECT
            ss.score,
            s.`name` as subjectName
        FROM
            `smart_score` ss
                LEFT JOIN smart_subject s ON ss.subject_id = s.id
        WHERE
            user_id = #{userId}
          AND ss.semester_id = #{semesterId}
          AND ss.exam_type = #{examTypeId}
          AND ss.deleted=0
    </select>

    <select id="getTeachingResultsExamName" resultType="com.template.model.vo.StudentExamNameVo">
        SELECT
            ss.semester_id AS semesterId,
            ss.semester AS semester,
            ss.exam_type AS examTypeId,
            me.`name` AS examTypeName,
            CONCAT( ss.semester, me.`name` ) AS totalName,
            ss.school_class AS schoolClass,
            ss.school_class_id AS schoolClassId
        FROM
            `smart_score` ss
                LEFT JOIN smart_examtype me ON ss.exam_type = me.id
        WHERE
            ss.deleted = 0
          AND ss.school_class_id = #{schoolClassId}
        GROUP BY
            ss.semester_id,
            ss.exam_type
    </select>


</mapper>
