<?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.health.mapper.EduStudentProjectTestResultMapper">

    <select id="findStuSelfTestProjectList" resultType="com.yida.data.school.vo.health.EduProjectVO">
    SELECT
      b.id as project_id,
      b.project_name
    FROM
      (
        SELECT
          project_id
        FROM
          edu_health_national_standard_project_score
        WHERE
          apply_sex =#{stuSex}
          AND record_grade_id =#{recordGradeId}
        GROUP BY
          project_id
      ) a
        LEFT JOIN edu_health_national_standard_project_weight b ON a.project_id = b.id
    </select>

    <select id="findStuNationalTest" resultType="com.yida.data.school.vo.health.EduNationalProjectVO">
        SELECT
        a.id,
        a.create_date,
        a.total_rank,
        a.total_score,
        a.student_sex,
		a.student_id,
	    a.record_grade_id
        FROM
        edu_health_student_physical_examination_grade_results a
        WHERE
        a.del_flag = 0
        AND a.id = #{id}
        ORDER BY
        a.create_date DESC
        LIMIT 0,1
    </select>

    <select id="findStuResultsList" resultType="com.yida.data.school.vo.health.EduSingleProjectVO">
    select
      *
    from edu_health_student_project_test_result
    where del_flag='0'
    and student_id=#{stuId}
    and project_id=#{projectId}
    order by create_date desc
    </select>

    <select id="getAddScoreNum" resultType="java.lang.Integer">
    SELECT count( 1 ) AS add_score_num
    FROM edu_health_student_project_test_result c
    LEFT JOIN edu_health_student_physical_examination_grade_results a ON a.id = c.grade_result_id
    WHERE  c.project_extra_score > 0
    AND a.id = #{id}
    </select>

    <select id="getFailNum" resultType="java.lang.Integer">
        SELECT
        count( 1 ) as fail_num
        FROM
        edu_health_student_project_test_result c
        LEFT JOIN edu_health_student_physical_examination_grade_results a ON a.id = c.grade_result_id
        WHERE
        c.project_score &lt; 60
        AND a.id = #{id}
    </select>

    <select id="findStuNational" resultType="com.yida.data.school.vo.health.EduStudentNationalVO">
    SELECT
      a.id as id,
      '1' as activity_type,
      '国标体测' as activity_name,
      a.create_date,
      a.total_score AS score,
      a.total_rank AS rank
    FROM
      edu_health_student_physical_examination_grade_results a
      LEFT JOIN edu_health_student_physical_examination_group b on a.group_id = b.id
    WHERE
      a.del_flag = '0'
      and b.`status` = 1
      AND a.source_type = '0'
      AND a.student_id = #{studentId}
    ORDER BY
      a.create_date DESC
      LIMIT 0,
      1
    </select>

    <select id="findStuSelfTest" resultType="com.yida.data.school.vo.health.EduStudentSelfVO">

    SELECT
      b.id as id,
       '2' as activity_type,
       '国标自测' as activity_name,
      b.create_date,
      b.weight AS weight,
      b.height AS height
    FROM
      edu_health_student_project_test_result b
    WHERE
      b.del_flag = '0'
      AND b.project_id = 1
      AND b.source_type = '1'
      AND b.student_id = #{studentId}
    ORDER BY
      b.create_date DESC
      LIMIT 0,
      1
    </select>
    <select id="findPageStuSelfTestProjectList" resultType="com.yida.data.school.vo.health.EduNationalProjectVO">
    SELECT
	id,
	create_date,
	record_grade_name,
	student_sex,
	student_name,
	total_score,
	total_rank,
	school_name,
	grade_name,
	class_name
    FROM
	`edu_health_student_physical_examination_grade_results`
    WHERE
	del_flag =0
	and school_id = #{schoolId}
	ORDER BY create_date DESC
    </select>
    <select id="findStudentResult" resultType="com.yida.data.school.vo.health.EduSingleProjectVO">
    SELECT
	*
    FROM
	`edu_health_student_project_test_result`
    WHERE
	del_flag = 0
	AND grade_result_id = #{id}
    </select>
    <select id="findPageStudent" resultType="com.yida.data.school.vo.health.StudentHealthListVO">
        SELECT DISTINCT
        school_id,
        school_name,
        a.student_id,
        student_name,
        grade_name,
        grade_id,
        class_name,
        class_id,
        a.create_date,
        student_sex
        FROM
        `edu_health_student_physical_examination_grade_results` a
        INNER JOIN ( SELECT student_id, MAX( create_date ) AS create_date FROM
        `edu_health_student_physical_examination_grade_results` GROUP BY student_id ) b ON a.student_id = b.student_id
        AND a.create_date = b.create_date
        WHERE
        del_flag =0
        AND school_id = #{schoolId}
        <if test="groupId != null">
            and group_id = #{groupId}
        </if>
        <if test="type">
            and source_type = #{type}
        </if>
        <if test="gradeId != null">
            AND grade_id =#{gradeId}
        </if>
        <if test="gradeId != null">
            and class_id =#{classId}
        </if>
        <if test="studentName !=null and studentName !=''">
            AND student_name like CONCAT("%",#{studentName},"%")
        </if>
        ORDER BY a.create_date desc

    </select>
    <select id="selectStudentList"
            resultType="com.yida.data.common.core.entity.health.EduStudentPhysicalExaminationGradeResults">
    SELECT
	    *
    FROM
	    edu_health_student_physical_examination_grade_results
     WHERE
	    student_id = #{studentId}
	    and del_flag = 0
	ORDER BY create_date DESC
    </select>
    <select id="allRecord" resultType="com.yida.data.school.vo.health.EduNationalProjectVO">
        SELECT
    *
    FROM
	`edu_health_student_physical_examination_grade_results`
    WHERE
	del_flag =0
	AND student_id = #{studentId}
	ORDER BY create_date DESC
    </select>
    <select id="findAllTest" resultType="com.yida.data.school.vo.health.EduSingleProjectVO">
    SELECT
	    a.* ,
	    b.code
    FROM
	    `edu_health_student_project_test_result` a
	    LEFT JOIN edu_health_national_standard_project_weight b ON a.project_id = b.id
    WHERE
	    grade_result_id = #{id}
	    and a.del_flag = 0
    </select>
    <select id="findAllResult" resultType="com.yida.data.school.vo.health.EduSingleProjectVO">
    SELECT
	    a.* ,
	    c.`code`
    FROM
	    `edu_health_student_project_test_result` a
	    LEFT JOIN edu_health_student_physical_examination_grade_results b ON a.grade_result_id = b.id
	    LEFT JOIN edu_health_national_standard_project_weight c ON a.project_id = c.id
    WHERE
        a.student_id =#{studentId}
        and a.del_flag = 0
	    AND b.source_type = 1
        AND a.project_rank != '未参加测试'
	    ORDER BY a.project_id,a.create_date DESC
    </select>
    <select id="findStudentHistoryResults" resultType="com.yida.data.school.vo.health.AppHistoryHealthVO">
    SELECT
	        a.*,
			b.code
    FROM
	    `edu_health_student_project_test_result` a
		LEFT JOIN edu_health_national_standard_project_weight b ON a.project_id = b.id
		LEFT JOIN edu_health_student_physical_examination_grade_results c on a.grade_result_id = c.id
		LEFT JOIN edu_health_student_physical_examination_group d on c.group_id = d.id
    WHERE
	    a.student_id = #{studentId}
	    and a.del_flag = 0
			and d.`status` = 1
	    AND a.project_rank != '未参加测试'
    ORDER BY
	    a.project_id,
	    a.create_date DESC
    </select>
    <select id="findLastEyesTest"
            resultType="com.yida.data.school.vo.health.EyeResultVO">
    SELECT
	    a.*
    FROM
	    `edu_health_student_project_test_result` a
			LEFT JOIN edu_health_student_physical_examination_grade_results b on a.grade_result_id = b.id
			LEFT JOIN edu_health_student_physical_examination_group c on b.group_id = c.id
    WHERE
	    a.student_id = #{studentId}
	    and a.del_flag = 0
		AND c.`status` = 1
	    AND a.project_id = #{projectId}
    ORDER BY
	    a.create_date desc
	    LIMIT #{num}
    </select>
    <select id="findOneStuSelfTestProject" resultType="com.yida.data.school.vo.health.EduNationalProjectVO">
    SELECT
	id,
	create_date,
	record_grade_id,
	record_grade_name,
	student_sex,
	student_name,
	student_id,
	total_score,
	total_rank,
	school_name,
	school_id,
	grade_name,
	grade_id,
	class_name,
	class_id
    FROM
	`edu_health_student_physical_examination_grade_results`
    WHERE
	del_flag =0
	and id= #{gradeResultId}
    </select>
    <select id="findEyesTest" resultType="com.yida.data.school.vo.health.EyeResultVO">
    SELECT
	    a.*
    FROM
	    `edu_health_student_project_test_result` a
			LEFT JOIN edu_health_student_physical_examination_grade_results b on a.grade_result_id = b.id
			LEFT JOIN edu_health_student_physical_examination_group c on b.group_id = c.id
    WHERE
	    a.student_id = #{studentId}
	    and a.del_flag = 0
		AND c.`status` = 1
	    AND a.project_id = #{i}

	union
    SELECT
	    *
    FROM
	    edu_health_student_project_test_result a
    WHERE
	    a.source_type = 1
	    and a.student_id = #{studentId}
	    AND a.del_flag = 0
	    AND a.project_id = #{i}

    ORDER BY
	    create_date desc
    </select>

</mapper>
