<?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.yida.data.school.exam.mapper.CoreExaminationMapper">
    <!--考试信息返回的map数据-->
    <resultMap id="BaseResultMap" type="com.yida.data.common.core.entity.exam.CoreExamination">
        <id column="id" property="id" jdbcType="INTEGER"/>
        <result column="create_id" property="createId" jdbcType="VARCHAR"/>
        <result column="create_date" property="createDate" jdbcType="TIMESTAMP"/>
        <result column="update_id" property="updateId" jdbcType="VARCHAR"/>
        <result column="update_date" property="updateDate" jdbcType="TIMESTAMP"/>
        <result column="del_flag" property="delFlag" jdbcType="CHAR"/>
        <result column="name" property="name" jdbcType="VARCHAR"/>
        <result column="start_date" property="startDate"/>
        <result column="end_date" property="endDate"/>
        <result column="type" property="type" jdbcType="INTEGER"/>
        <result column="school_id" property="schoolId" jdbcType="INTEGER"/>
        <result column="school_name" property="schoolName" jdbcType="VARCHAR"/>
        <result column="grade_id" property="gradeId" jdbcType="INTEGER"/>
        <result column="grade_name" property="gradeName" jdbcType="VARCHAR"/>
        <result column="score" property="score" jdbcType="INTEGER"/>
        <result column="publish" property="publish" jdbcType="CHAR"/>
    </resultMap>

    <!--H5返回的学生成绩map数据-->
    <resultMap id="studentGradeMap" type="com.yida.data.common.core.entity.exam.CoreStudentReturnData">
        <result column="examination_id" property="examinationId" jdbcType="INTEGER"/>
        <result column="examination_name" property="examinationName" jdbcType="VARCHAR"/>
        <result column="start_date" property="startDate" jdbcType="TIMESTAMP"/>
        <result column="end_date" property="endDate" jdbcType="TIMESTAMP"/>
        <result column="school_name" property="schoolName" jdbcType="VARCHAR"/>
        <result column="grade_name" property="gradeName" jdbcType="VARCHAR"/>
        <result column="class_name" property="className" jdbcType="VARCHAR"/>
        <result column="student_name" property="studentName" jdbcType="VARCHAR"/>
        <result column="total_score" property="totalScore" jdbcType="INTEGER"/>
        <result column="grade_rank" property="gradeRank" jdbcType="INTEGER"/>
        <result column="class_rank" property="classRank" jdbcType="INTEGER"/>
        <result column="examination_student_id" property="examinationStudentId" jdbcType="INTEGER"/>
        <result column="course_number" property="courseNumber" jdbcType="INTEGER"/>
        <result column="student_total_score" property="studentTotalScore" jdbcType="INTEGER"/>
        <result column="joint_rank_block" property="jointRankBlock" jdbcType="VARCHAR"/>
        <result column="school_rank_block" property="schoolRankBlock" jdbcType="VARCHAR"/>
        <result column="grade_rank_block" property="gradeRankBlock" jdbcType="VARCHAR"/>
        <result column="class_rank_block" property="classRankBlock" jdbcType="VARCHAR"/>
        <result column="score_block" property="scoreBlock"/>
        <result column="total_score_block" property="totalScoreBlock"/>
        <result column="receipt" property="receipt" jdbcType="VARCHAR"/>
        <result column="comment" property="comment" jdbcType="VARCHAR"/>
        <result column="comment_content" property="commentContent" jdbcType="VARCHAR"/>
        <result column="receipt_status" property="receiptStatus" jdbcType="VARCHAR"/>
        <result column="grade_level" property="gradeLevel"/>

        <collection property="courseDataList" javaType="java.util.List"
                    ofType="com.yida.data.common.core.entity.exam.CoreStudentReturnCourseData">
            <result column="course_name" property="courseName" jdbcType="VARCHAR"/>
            <result column="course_score" property="courseScore" jdbcType="INTEGER"/>
            <result column="student_course_score" property="studentCourseScore" jdbcType="INTEGER"/>
            <result column="c_grade_level" property="gradeLevel" jdbcType="VARCHAR"/>
            <result column="student_course_miss" property="studentCourseMiss" jdbcType="VARCHAR"/>
        </collection>
    </resultMap>


    <!--根据学生id和考试id查询H5学生成绩信息-->
    <select id="findStudentExamineGrade" resultMap="studentGradeMap">
        SELECT
        ce.id AS examination_id,
        ce.`name` AS examination_name,
        ce.start_date AS start_date,
        ce.end_date AS end_date,
        ce.school_name AS school_name,
        ce.grade_name AS grade_name,
        ces.class_name AS class_name,
        ces.student_name AS student_name,
        ce.score AS total_score,
        ces.grade_rank AS grade_rank,
        ces.class_rank AS class_rank,
        ces.score as student_total_score,
        ces.grade_level,
        ces.id AS examination_student_id,
        cepr.core_examination_course_name AS course_name,
        cepr.course_score AS course_score,
        cepr.score AS student_course_score,
        cepr.grade_level AS c_grade_level,
        cepr.miss AS student_course_miss,
        cep.joint_rank_block AS joint_rank_block,
        cep.school_rank_block AS school_rank_block,
        cep.grade_rank_block AS grade_rank_block,
        cep.class_rank_block AS class_rank_block,
        cep.score_block,
        cep.total_score_block,
        cep.receipt AS receipt,
        cep.`comment` AS `comment`,
        cep.comment_content AS comment_content,
        ces.receipt_status AS receipt_status,
        ces.grade_level
        FROM
        core_examination ce
        LEFT JOIN core_examination_publish cep ON ce.id = cep.core_examination_id
        LEFT JOIN core_examination_publish_record cepr ON cep.id = cepr.core_examination_publish_id
        LEFT JOIN core_examination_student ces ON cepr.core_student_id = ces.core_student_id and ce.id =
        ces.core_examination_id
        WHERE
        ce.del_flag = '0'
        AND cepr.del_flag = '0'
        AND ce.publish = '1'
        AND cep.student_block = '0'
        AND cepr.core_student_id = #{studentId}
        AND ces.del_flag = '0'
        AND cep.del_flag = '0'

        <if test='examinationId != null'>
            AND ce.id = #{examinationId}
        </if>
        AND
        IF (ces.miss = 1, 1 = 0, 1 = 1)
        <if test='examinationId == null'>
            ORDER BY
            cep.create_date desc
        </if>
    </select>

    <!--根据学生id查询考试列表数据-->
    <select id="findStudentExamineList" resultType="com.yida.data.school.vo.exam.StudentExamListVO">
        SELECT ce.`name` AS `examName`,
        ce.score AS totalScore,
        ces.score,
        ces.grade_level,
        ce.start_date examDate,
        ce.id examId,
        cep.score_block,
        cep.total_score_block
        FROM
        core_examination ce
        inner join core_examination_publish cep on cep.core_examination_id=ce.id
        inner JOIN core_examination_student ces ON ce.id = ces.core_examination_id
        WHERE
        ce.del_flag = '0'
        AND cep.del_flag = '0'
        AND ces.del_flag = '0'
        AND ce.publish = '1'
        AND ces.core_student_id = #{studentId}
        and cep.create_date between #{startDate} and #{endDate}
        <if test='examinationName != null and examinationName != ""'>
            AND ce.`name` LIKE CONCAT('%', #{examinationName}, '%')
        </if>
        and cep.create_date not in (
        select max(b.create_date)
        from core_examination a
        inner join core_examination_publish b on b.core_examination_id=a.id
        inner JOIN core_examination_student c ON a.id = c.core_examination_id
        where
        a.del_flag = '0'
        AND b.del_flag = '0'
        AND c.del_flag = '0'
        AND a.publish = '1'
        AND c.core_student_id = #{studentId}
        )
        ORDER BY
        cep.create_date DESC
    </select>

    <!--查询考试列表数据-->
    <select id="findExaminationPageList" parameterType="com.yida.data.common.core.entity.exam.CoreExamination"
            resultType="com.yida.data.common.core.entity.exam.CoreExamination">
        SELECT
        ce.id AS id,
        ce.`name` AS `name`,
        ce.start_date AS start_date,
        ce.end_date AS end_date,
        ce.grade_id AS grade_id,
        ce.`grade_name` AS grade_name,
        cet.`name` AS type_name,
        ce.score AS score,
        ce.publish as publish,
        GROUP_CONCAT(DISTINCT cec.core_course_name ORDER BY cec.id ASC SEPARATOR ',') AS course_name,
        GROUP_CONCAT(ces.student_name) as stuname
        FROM
        core_examination ce
        LEFT JOIN core_examination_type cet ON ce.type = cet.id
        LEFT JOIN (SELECT*FROM core_examination_course WHERE del_flag = '0') cec ON ce.id =
        cec.core_examination_id
        LEFT JOIN (SELECT * FROM core_examination_student WHERE del_flag = '0') ces on ce.id =
        ces.core_examination_id
        WHERE
        ce.del_flag = '0'

        <if test='dto.provinceId != null and dto.provinceId != 0'>
            AND ce.province_id = #{dto.provinceId}
        </if>
        <if test='dto.cityId != null and dto.cityId != 0'>
            AND ce.city_id = #{dto.cityId}
        </if>
        <if test='dto.countyId != null and dto.countyId != 0'>
            AND ce.county_id = #{dto.countyId}
        </if>
        <if test='dto.villageId != null and dto.villageId != 0'>
            AND ce.village_id = #{dto.villageId}
        </if>
        <if test='dto.schoolId != null and dto.schoolId != 0'>
            AND ce.school_id = #{dto.schoolId}
        </if>
        <if test='dto.gradeId != null and dto.gradeId != 0'>
            AND ce.grade_id = #{dto.gradeId}
        </if>
        <if test='dto.type != null and dto.type != 0'>
            AND ce.type = #{dto.type}
        </if>
        <if test='dto.schoolDistrict != null and dto.schoolDistrict != 0'>
            AND ce.school_district = #{dto.schoolDistrict}
        </if>
        <if test='dto.schoolParagraph != null and dto.schoolParagraph != 0'>
            AND ce.school_paragraph = #{dto.schoolParagraph}
        </if>
        <if test='dto.name != null and dto.name != ""'>
            AND ce.`name` LIKE CONCAT('%', #{dto.name}, '%')
        </if>
        GROUP BY
        id,
        `name`,
        start_date,
        end_date,
        grade_id,
        grade_name,
        type_name,
        score
        ORDER BY
        ce.create_date DESC
    </select>


    <!--根据考试主键id查询考试学科数-->
    <select id="countExaminationCourseByExaminationId" parameterType="java.lang.Integer"
            resultType="java.lang.Integer">
        SELECT COUNT(id)
        FROM core_examination_course
        WHERE del_flag = '0'
        AND core_examination_id = #{examinationId}
    </select>

    <!--根据考试主键id查询考试班级数-->
    <select id="countExaminationClassByExaminationId" parameterType="java.lang.Integer"
            resultType="java.lang.Integer">
        SELECT COUNT(co.id)
        FROM core_examination ce
        LEFT JOIN t_dept co ON ce.grade_id = co.parent_id
        WHERE ce.del_flag = '0'
        AND co.del_flag = '0'
        AND ce.id = #{examinationId}
    </select>

    <!--根据考试主键id查询考试学生数-->
    <select id="countExaminationStudentByExaminationId" parameterType="java.lang.Integer"
            resultType="java.lang.Integer">
        SELECT COUNT(id)
        FROM core_examination_student
        WHERE del_flag = '0'
        AND core_examination_id = #{examinationId}
    </select>

    <select id="findHistoryExamination" parameterType="java.lang.Long"
            resultType="com.yida.data.common.core.entity.exam.CoreExaminationStudent">
        select
        *
        from
        core_examination_student
        where school_id = #{schoolId}
        and core_student_id = #{studentId}
        ORDER BY
        create_date DESC
    </select>

    <select id="findClassExamination"
            resultType="com.yida.data.school.vo.exam.EduStudentExamVO">
        SELECT
        ROUND(AVG(a.score)) as average_score,
        a.total_score,
        MAX(a.score) as max_score,
        MIN(a.score) as min_score,
        COUNT(a.id) as total_student
        FROM
        (SELECT DISTINCT
        c.score AS total_score,
        d.id,
        d.score
        FROM `core_examination_publish_record` b
        LEFT JOIN core_examination c ON b.core_examination_id = c.id
        LEFT JOIN core_examination_student d ON b.core_examination_student_id = d.id
        WHERE b.core_examination_id = #{examId}
        and b.del_flag=0
        and b.create_date BETWEEN #{star} and #{end}
        <if test="courseId != null and courseId != ''">
            AND b.core_examination_course_id =
            #{courseId}
        </if>
        <if test="classId != null and classId != ''">
            AND b.class_id =
            #{classId}
        </if>
        )a
        GROUP BY a.total_score
    </select>


    <select id="findOneClassExamination" resultType="com.yida.data.school.vo.exam.EduStudentExamVO">
        SELECT
        ROUND(
        AVG(c.score)) AS average_score,
        MAX(c.score) AS max_score,
        MIN(c.score) AS min_score,
        COUNT(c.id) AS total_student,
        c.core_course_score as total_score
        FROM
        (
        SELECT DISTINCT
        a.score,
        b.core_course_score,
        a.id
        FROM
        `core_examination_publish_record` a
        LEFT JOIN core_examination_course b ON a.core_examination_id = b.core_examination_id and
        a.core_examination_course_id = b.core_course_id
        AND b.del_flag = 0
        WHERE
        a.core_examination_id = #{examId}
        <if test="courseId != null and courseId != ''">
            AND a.core_examination_course_id =#{courseId}
        </if>
        AND a.del_flag = 0
        and a.create_date BETWEEN #{star} and #{end}
        <if test="classId != null and classId != ''">
            AND a.class_id = #{classId}
        </if>
        )c
        GROUP BY c.core_course_score
    </select>
    <select id="selectGradeList" resultType="com.yida.data.common.core.entity.exam.CoreExamination">
        SELECT
        *
        FROM
        core_examination
        WHERE
        1=1
        and publish = 1
        and end_date BETWEEN #{start}
        AND #{end}
        <if test="gradeId != null">
            and grade_id IN
            <foreach collection="gradeId" separator="," item="item" close=")" open="(">
                #{item}
            </foreach>
        </if>
        <if test="name != null and name != ''">
            and `name` like concat('%',#{name},'%')
        </if>
        order by end_date desc
    </select>

    <select id="selectClassScore" resultType="com.yida.data.common.core.entity.exam.CoreExaminationStudent">
        SELECT
        DISTINCT
        a.id,
        a.core_student_id,
        a.core_examination_id,
        a.student_name,
        a.class_name,
        a.score,
        a.grade_rank,
        a.class_rank,
        a.class_id
        FROM
        core_examination_student a
        LEFT JOIN core_examination_publish_record b ON a.id = b.core_examination_student_id
        WHERE 1=1 and a.del_flag=0
        and a.core_examination_id=#{examId}
        <if test='classId != null and classId != ""'>
            and a.class_id IN
            <foreach collection="classId" item="item" separator="," close=")" open="(">
                #{item}
            </foreach>
        </if>
        <if test="studentName != null and studentName != ''">
            and a.student_name LIKE CONCAT('%',#{studentName},'%')
        </if>

        ORDER BY
        a.grade_rank
    </select>


    <select id="findHistoryExaminationList" resultType="com.yida.data.school.vo.exam.ExamVO">
        SELECT
        a.id as exam_id,
        a.`name`,
        a.start_date,
        a.end_date,
        a.score as total_score,
        c.score
        FROM
        core_examination a
        LEFT JOIN core_examination_publish b ON a.id = b.core_examination_id
        LEFT JOIN core_examination_student c ON a.id = c.core_examination_id
        WHERE
        a.del_flag = '0'
        AND b.del_flag = '0'
        AND c.del_flag = '0'
        and c.core_student_id = #{studentId}
        <if test='examName != null and examName != ""'>
            and c.student_name like concat('%',#{examName},'%')
        </if>
    </select>
    <select id="findAllScoringRate"
            resultType="com.yida.data.common.core.entity.exam.CoreExaminationCoreAverage">
        SELECT DISTINCT

        (sum(if(score >= (total*(#{great}/100)),1,0)))/COUNT(a.score)*100 as great,
        (sum(if(score >= (total*(#{good}/100)),1,0)))/COUNT(a.score)*100 as good,
        (sum(if(score >= (total*(#{pass}/100)),1,0)))/COUNT(a.score)*100 as pass,
        <![CDATA[(sum(if(score <= (total*(#{poor}/100)),1,0)))/COUNT(a.score)*100 as poor]]>
        FROM
        (
        SELECT DISTINCT
        c.score as total,
        d.*
        FROM
        `core_examination_publish_record` b
        LEFT JOIN core_examination c ON b.core_examination_id = c.id
        LEFT JOIN core_examination_student d ON b.core_examination_student_id = d.id
        WHERE
        b.core_examination_id = #{examId}
        AND b.del_flag = 0

        ) a
    </select>
    <select id="findOneScoringRate"
            resultType="com.yida.data.common.core.entity.exam.CoreExaminationCoreAverage">
        SELECT
        (sum(if(score >= (total*(#{great}/100)),1,0)))/COUNT(c.score)*100 as great,
        (sum(if(score >= (total*(#{good}/100)),1,0)))/COUNT(c.score)*100 as good,
        (sum(if(score >= (total*(#{pass}/100)),1,0)))/COUNT(c.score)*100 as pass,
        <![CDATA[(sum(if(score <= (total*(#{poor}/100)),1,0)))/COUNT(a.score)*100 as poor]]>
        FROM
        (
        SELECT DISTINCT
        a.score,
        b.core_course_score as total

        FROM
        `core_examination_publish_record` a
        LEFT JOIN core_examination_course b ON a.core_examination_id = b.core_examination_id and
        a.core_examination_course_id = b.core_course_id
        AND b.del_flag = 0
        WHERE
        a.core_examination_id = #{examId}
        AND a.core_examination_course_id =#{courseId}
        AND a.del_flag = 0)c
    </select>

    <select id="existPublishExam" resultType="java.lang.Integer">
        select 1
        from core_examination
        where del_flag=0
        and publish=1
        and id in
        <foreach collection="ids" separator="," open="(" close=")" item="item">
            #{item}
        </foreach>
        limit 1
    </select>
</mapper>