<?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.koobietech.eas.dao.mapper.ScoresMapper">

    <resultMap id="scoreMap" type="com.koobietech.eas.dao.pojo.ScoresPojo">
        <id property="id" column="id"/>
        <result property="groupId" column="group_id"/>
        <result property="categoryId" column="category_id"/>
        <result property="subjectId" column="subject_id"/>
        <result property="testDate" column="test_date"/>
        <result property="type" column="type"/>
        <result property="studentId" column="student_id"/>
        <result property="groupName" column="group_name"/>
        <result property="studentName" column="student_name"/>
        <result property="score" column="score"/>
        <result property="passRate" column="pass_rate"/>
        <result property="excelRate" column="excel_rate"/>
        <result property="comment" column="comment"/>
        <result property="categoryName" column="category_name"/>
        <result property="subjectsName" column="subjects_name"/>
        <result property="studentNumber" column="student_number"/>
        <result property="createTime" column="create_time"/>
        <result property="modifyTime" column="modify_time"/>
        <result property="scheduleId" column="schedule_id"/>
    </resultMap>

    <select id="queryStudentScores" parameterType="com.koobietech.eas.dao.dto.ScoresDto" resultMap="scoreMap">
        SELECT
            eas_arc_tls_scores.id,
            eas_edu_class.id AS group_id,
            eas_edu_category.id AS category_id,
            eas_edu_subjects.id AS subject_id,
            eas_arc_tls_scores.test_date,
            eas_edu_clt_relation.type,
            eas_edu_clt_relation.student_id,
            eas_edu_class.`name` AS group_name,
            eas_sys_student.student_name,
            eas_arc_tls_scores.score,
            eas_arc_tls_scores.pass_rate,
            eas_arc_tls_scores.excel_rate,
            eas_arc_tls_scores.`comment`,
            eas_edu_category.`name` AS category_name,
            eas_edu_subjects.`name` AS subjects_name,
            eas_edu_schedule.id AS schedule_id,
            eas_sys_student.student_number,
            eas_arc_tls_scores.create_time,
            eas_arc_tls_scores.modify_time
        FROM
            eas_edu_clt_relation
        LEFT JOIN eas_sys_student ON eas_sys_student.id = eas_edu_clt_relation.student_id
        LEFT JOIN eas_edu_class ON eas_edu_class.id = eas_edu_clt_relation.class_id
        LEFT JOIN eas_edu_schedule ON eas_edu_schedule.id IN (SELECT MIN(id) AS id FROM eas_edu_schedule  WHERE class_id = eas_edu_class.id GROUP BY subjects_id)
        LEFT JOIN eas_edu_category ON eas_edu_category.id = eas_edu_schedule.category_id
        LEFT JOIN eas_edu_subjects ON  eas_edu_subjects.id = eas_edu_schedule.subjects_id
        LEFT JOIN eas_arc_tls_scores ON eas_arc_tls_scores.subject_id = eas_edu_schedule.subjects_id AND eas_arc_tls_scores.student_number = eas_sys_student.student_number
        WHERE 1 = 1
            <if test="groupId != null and groupId > 0" >
                AND eas_edu_clt_relation.class_id = #{groupId}
            </if>
            <if test="studentId != null and studentId > 0" >
                AND eas_edu_clt_relation.student_id = #{studentId}
            </if>
            <if test="studentNumber != null and studentNumber != ''" >
                AND eas_sys_student.student_number =  #{studentNumber}
            </if>
            <if test="studentName != null and studentName != ''" >
                AND eas_sys_student.student_name LIKE  CONCAT('%', #{studentName},'%')
            </if>
            <if test="score != null and score > 0" >
                AND eas_arc_tls_scores.score > #{score}
            </if>
            <if test="subjectId != null and subjectId > 0" >
                AND eas_edu_subjects.id = #{subjectId}
            </if>
            <if test="categoryId != null and categoryId > 0" >
                AND eas_edu_category.id = #{categoryId}
            </if>
            <if test="categoryName != null and categoryName != ''" >
                AND eas_edu_category.name = #{categoryName}
            </if>
            <if test="subjectsName != null and subjectsName != ''" >
                AND eas_edu_subjects.name = #{subjectsName}
            </if>
            <if test="groupName != null and groupName != ''" >
                AND eas_edu_class.name = #{groupName}
            </if>
    </select>

    <select id="queryStudentScoresCount" parameterType="com.koobietech.eas.dao.dto.ScoresDto"  resultType="integer"  >
        SELECT
            count(*) AS total
        FROM
        eas_edu_clt_relation
        LEFT JOIN eas_sys_student ON eas_sys_student.id = eas_edu_clt_relation.student_id
        LEFT JOIN eas_edu_class ON eas_edu_class.id = eas_edu_clt_relation.class_id
        LEFT JOIN eas_edu_schedule ON eas_edu_schedule.id IN (SELECT MIN(id) AS id FROM eas_edu_schedule  WHERE class_id = eas_edu_class.id GROUP BY subjects_id)
        LEFT JOIN eas_edu_category ON eas_edu_category.id = eas_edu_schedule.category_id
        LEFT JOIN eas_edu_subjects ON  eas_edu_subjects.id = eas_edu_schedule.subjects_id
        LEFT JOIN eas_arc_tls_scores ON eas_arc_tls_scores.subject_id = eas_edu_schedule.subjects_id AND eas_arc_tls_scores.student_number = eas_sys_student.student_number
        WHERE 1 = 1
            <if test="groupId != null and groupId > 0" >
                AND eas_edu_clt_relation.class_id = #{groupId}
            </if>
            <if test="studentId != null and studentId > 0" >
                AND eas_edu_clt_relation.student_id = #{studentId}
            </if>
            <if test="studentName != null and studentName != ''" >
                AND eas_sys_student.student_name =  CONCAT('%', #{studentName},'%')
            </if>
            <if test="studentNumber != null and studentNumber != ''" >
                AND eas_sys_student.student_number =  #{studentNumber}
            </if>
            <if test="score != null and score > 0" >
                AND eas_arc_tls_scores.score > #{score}
            </if>
            <if test="subjectId != null and subjectId > 0" >
                AND eas_edu_subjects.id = #{subjectId}
            </if>
            <if test="categoryId != null and categoryId > 0" >
                AND eas_edu_category.id = #{categoryId}
            </if>
            <if test="categoryName != null and categoryName != ''" >
                AND eas_edu_category.name = #{categoryName}
            </if>
            <if test="subjectsName != null and subjectsName != ''" >
                AND eas_edu_subjects.name = #{subjectsName}
            </if>
            <if test="groupName != null and groupName != ''" >
                AND eas_edu_class.name = #{groupName}
            </if>
    </select>

</mapper>