<?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.srs.comprehensive.mapper.SrsCourseScoreMapper">
	<resultMap type="SrsCourseScore" id="SrsCourseScoreResult">
		<result property="id" column="id"/>
		<result property="stuId" column="stu_id"/>
		<result property="avgScore" column="avg_score"/>
		<result property="stuYearId" column="stu_year_id"/>
		<result property="delFlag" column="del_flag"/>
		<result property="createBy" column="create_by"/>
		<result property="createTime" column="create_time"/>
		<result property="updateBy" column="update_by"/>
		<result property="updateTime" column="update_time"/>
		<result property="name" column="name"/>
		<result property="className" column="class_name"/>
		<result property="stuYearName" column="stu_year_name"/>
	</resultMap>
	<resultMap type="SrsCourseScoreStudentStuYear" id="SrsCourseScoreStudentStuYears">
		<result property="id" column="id"/>
		<result property="name" column="name"/>
		<result property="stuNo" column="stu_no"/>
		<result property="stuYearName" column="stu_year_name"/>
		<result property="avgScore" column="avg_score"/>
		<result property="stuId" column="stu_id"/>
		<result property="stuYearId" column="stu_year_id"/>
		<result property="className" column="class_name"/>
	</resultMap>
	<resultMap type="SrsCourseScoreVO" id="SrsCourseScoreStudentStuYearS">
		<result property="id" column="id"/>
		<result property="name" column="name"/>
		<result property="stuNo" column="stu_no"/>
		<result property="stuYearName" column="stu_year_name"/>
		<result property="avgScore" column="avg_score"/>
		<result property="stuId" column="stu_id"/>
		<result property="stuYearId" column="stu_year_id"/>
		<result property="className" column="class_name"/>
		<result property="classRank" column="class_rank"/>
		<result property="majorRank" column="major_rank"/>
	</resultMap>
	<resultMap type="SrsSportTestStudentStuYear" id="SrsSportTestStudentStuYearS">
		<result property="id" column="id"/>
		<result property="name" column="name"/>
		<result property="stuNo" column="stu_no"/>
		<result property="stuYearName" column="stu_year_name"/>
		<result property="testScore" column="test_score"/>
		<result property="stuId" column="stu_id"/>
		<result property="stuYearId" column="stu_year_id"/>
		<result property="className" column="class_name"/>
	</resultMap>
	<resultMap type="SrsStuYearS" id="SrsStuYearResults">
		<result property="id" column="id"/>
		<result property="name" column="name"/>
		<result property="stuNo" column="stu_no"/>
		<result property="stuYearName" column="stu_year_name"/>
		<result property="avgScore" column="avg_score"/>
	</resultMap>
	<resultMap type="sumCourseScore" id="sumCourseScoreS">
		<result property="id"    column="id"    />
		<result property="stuNo"    column="stu_no"    />
		<result property="avgScore"    column="avg_score"    />
		<result property="stuYearId"    column="stu_year_id"    />
		<result property="writeTime"    column="write_time"    />
		<result property="readTime"    column="read_time"    />
		<result property="dataStatus"    column="data_status"    />
		<result property="chmodStatus"    column="chmod_status"    />
	</resultMap>
	<resultMap type="SrsSportTest" id="SrsSportTestResult">
		<result property="id"    column="id"    />
		<result property="stuId"    column="stu_id"    />
		<result property="testScore"    column="test_score"    />
		<result property="stuYearId"    column="stu_year_id"    />
		<result property="delFlag"    column="del_flag"    />
		<result property="createBy"    column="create_by"    />
		<result property="createTime"    column="create_time"    />
		<result property="updateBy"    column="update_by"    />
		<result property="updateTime"    column="update_time"    />
		<association property="student" javaType="SrsStudent" resultMap="studentJoin" />
	</resultMap>
	<resultMap type="SrsStudent" id="studentJoin">
		<result property="stuId"    column="stu_id"    />
		<result property="name"    column="name"    />
		<result property="stuNo"    column="stu_no"    />
		<result property="gender"    column="gender"    />
		<result property="birthday"    column="birthday"    />
		<result property="idCard"    column="id_card"    />
		<result property="deptId"    column="dept_id"    />
		<result property="majorId"    column="major_id"    />
		<result property="phone"    column="phone"    />
		<result property="address"    column="address"    />
		<result property="status"    column="status"    />
		<result property="createBy"    column="create_by"    />
		<result property="createTime"    column="create_time"    />
		<result property="updateBy"    column="update_by"    />
		<result property="updateTime"    column="update_time"    />
		<result property="delFlag"    column="del_flag"    />
	</resultMap>
	
	
	<sql id="selectSrsCourseScoreVo">
		select id,
		       stu_id,
		       avg_score,
		       stu_year_id,
		       del_flag,
		       create_by,
		       create_time,
		       update_by,
		       update_time
		from srs_course_score
	</sql>
	
	<select id="selectSrsCourseScoreList" parameterType="SrsCourseScore" resultMap="SrsCourseScoreResult">
<!--		<include refid="selectSrsCourseScoreVo"/>-->
		select a.id, b.`name`, b.stu_no, c.stu_year_name, a.avg_score, d.class_name
		from srs_course_score as a
		left join srs_student as b on a.stu_id = b.stu_id
		left join srs_stu_year as c on a.stu_year_id = c.id
		left join srs_class as d on b.class_id = d.class_id
		<where>
			<if test="stuId != null ">and b.stu_id = #{stuId}</if>
			<if test="avgScore != null ">and a.avg_score = #{avgScore}</if>
			<if test="avgScoreFrom != null ">and a.avg_score &gt;= #{avgScoreFrom}</if>
			<if test="avgScoreTo != null ">and a.avg_score &lt;= #{avgScoreTo}</if>
			<if test="stuYearId != null ">and a.stu_year_id = #{stuYearId}</if>
		</where>
	</select>

	<select id="selectAllCourse" resultType="CphScoreVo">
		select b.stu_no,a.avg_score as score from srs_course_score as a
		left join srs_student as b on a.stu_id = b.stu_id
		where a.stu_year_id = #{year}
	</select>
	
	<select id="selectSrsCourseScoreById" parameterType="Long" resultMap="SrsCourseScoreResult">
		<include refid="selectSrsCourseScoreVo"/>
		where id = #{id}
	</select>
	<!--按学生id查询-->
	<select id="selectStuIds" parameterType="Long" resultMap="SrsCourseScoreResult">
		<include refid="selectSrsCourseScoreVo"/>
		where stu_id = #{stuId}
	</select>
	
	<insert id="insertSrsCourseScore" parameterType="SrsCourseScore" useGeneratedKeys="true" keyProperty="id">
		insert into srs_course_score
		<trim prefix="(" suffix=")" suffixOverrides=",">
			<if test="stuId != null">stu_id,</if>
			<if test="avgScore != null">avg_score,</if>
			<if test="stuYearId != null">stu_year_id,</if>
			<if test="delFlag != null">del_flag,</if>
			<if test="createBy != null">create_by,</if>
			<if test="createTime != null">create_time,</if>
			<if test="updateBy != null">update_by,</if>
			<if test="updateTime != null">update_time,</if>
		</trim>
		<trim prefix="values (" suffix=")" suffixOverrides=",">
			<if test="stuId != null">#{stuId},</if>
			<if test="avgScore != null">#{avgScore},</if>
			<if test="stuYearId != null">#{stuYearId},</if>
			<if test="delFlag != null">#{delFlag},</if>
			<if test="createBy != null">#{createBy},</if>
			<if test="createTime != null">#{createTime},</if>
			<if test="updateBy != null">#{updateBy},</if>
			<if test="updateTime != null">#{updateTime},</if>
		</trim>
	</insert>
	
	<update id="updateSrsCourseScore" parameterType="SrsCourseScore">
		update srs_course_score
		<trim prefix="SET" suffixOverrides=",">
			<if test="stuId != null">stu_id = #{stuId},</if>
			<if test="avgScore != null">avg_score = #{avgScore},</if>
			<if test="stuYearId != null">stu_year_id = #{stuYearId},</if>
			<if test="delFlag != null">del_flag = #{delFlag},</if>
			<if test="createBy != null">create_by = #{createBy},</if>
			<if test="createTime != null">create_time = #{createTime},</if>
			<if test="updateBy != null">update_by = #{updateBy},</if>
			<if test="updateTime != null">update_time = #{updateTime},</if>
		</trim>
		where id = #{id}
	</update>
	
	<delete id="deleteSrsCourseScoreById" parameterType="Long">
		delete
		from srs_course_score
		where id = #{id}
	</delete>
	
	<delete id="deleteSrsCourseScoreByIds" parameterType="String">
		delete from srs_course_score where id in
		<foreach item="id" collection="array" open="(" separator="," close=")">
			#{id}
		</foreach>
	</delete>
	<!--课程学习连表查询-->
	<select id="selectStuYearStudent" resultType="SrsStuYearS">
		select a.id, b.`name`, b.stu_no, c.stu_year_name, a.avg_score, a.stu_id, a.stu_year_id
		from srs_course_score as a
			     left join srs_student as b on a.stu_id = b.stu_id
			     left join srs_stu_year as c on a.stu_year_id = c.id
		where a.del_flag = 0
	</select>
	<!--   科学文化素质连表查询 -->
	<select id="selectSrsCourseScoreStudentStuYear" parameterType="srsCourseScore" resultMap="SrsCourseScoreStudentStuYearS">
		select * from (
		select a.id, b.`name`, b.stu_no, c.stu_year_name, a.avg_score, d.class_name,
		RANK() OVER(PARTITION BY b.class_id,a.stu_year_id,d.grade_id ORDER BY a.avg_score DESC ) as class_rank,
		RANK() OVER(PARTITION BY d.major_id,a.stu_year_id,d.grade_id ORDER BY a.avg_score DESC ) as major_rank,
		a.del_flag,
		b.class_id,
		a.stu_year_id
		from srs_course_score as a
		left join srs_student as b on a.stu_id = b.stu_id
		left join srs_stu_year as c on a.stu_year_id = c.id
		left join srs_class as d on b.class_id = d.class_id
		ORDER BY b.class_id,a.stu_year_id,a.avg_score desc
		) as t1
		<where>
			<if test=" delFlag != null "> and t1.del_flag = #{delFlag}</if>
			<if test="stuYearId != null "> and t1.stu_year_id = #{stuYearId}</if>
			<if test="student.classId != null "> and t1.class_id = #{student.classId}</if>
			<if test="student.stuNo != null and student.stuNo!=''"> and t1.stu_no = #{student.stuNo}</if>
			<if test="student.name != null ">and t1.name like concat('%',#{student.name},'%')</if>
			<if test="avgScoreFrom != null "> and t1.avg_score &gt;= #{avgScoreFrom}</if>
			<if test="avgScoreTo != null "> and t1.avg_score &lt;= #{avgScoreTo}</if>
		</where>
	</select>
	<!--    体能测试连表查询-->
	<select id="selectSrsSportTestStudentStuYear" parameterType="SrsSportTest" resultMap="SrsSportTestStudentStuYearS">
		select a.id, b.`name`, b.stu_no, c.stu_year_name, a.test_score, a.stu_id, a.stu_year_id, d.class_name
		from srs_sport_test as a
		     left join srs_student as b on a.stu_id = b.stu_id
		     left join srs_class as d on b.class_id = d.class_id
                 left join srs_majors as ma on d.major_id = ma.major_id
                 left join sys_dept as de on ma.college_id = de.dept_id
		     left join srs_stu_year as c on a.stu_year_id = c.id
		<where>
			<if test=" delFlag != null "> and a.del_flag = #{delFlag}</if>
			<if test="stuYearId != null "> and a.stu_year_id = #{stuYearId}</if>
			<if test="student.classId != null "> and b.class_id = #{student.classId}</if>
			<if test="student.stuNo != null and student.stuNo!=''"> and b.stu_no = #{student.stuNo}</if>
			<if test="student.name != null "> and b.name like #{student.name}</if>
			<if test="testScoreFrom != null "> and a.test_score &gt;= #{testScoreFrom}</if>
			<if test="testScoreTo != null "> and a.test_score &lt;= #{testScoreTo}</if>
			<if test="deptId != null "> and de.dept_id = #{deptId}</if>
			<if test="majorId != null "> and ma.major_id = #{majorId}</if>
		</where>
	</select>
	<select id="selectGradeRanking" resultType="com.srs.comprehensive.domain.Vo.SrsCeScoreClassDeptGradeMajorsStudentStuYear">
		SELECT cs.id,
		cs.avg_score score,
		s.name,
		y.stu_year_name stuYearName,
		m.major_name majorName,
		c.class_name className,
		g.grade_name gradeName,
		d.dept_name deptName,
		(SELECT COUNT(*)
		FROM (SELECT cs2.avg_score
		FROM srs_course_score cs2
		JOIN srs_student s2 ON cs2.stu_id = s2.stu_id
		JOIN srs_class c2 ON s2.class_id = c2.class_id
		JOIN srs_majors m2 ON c2.major_id = m2.major_id
		WHERE m2.major_id = m.major_id
		AND cs2.avg_score > cs.avg_score) AS temp) + 1 AS majorRanking,
		(SELECT COUNT(*)
		FROM (SELECT cs3.avg_score
		FROM srs_course_score cs3
		JOIN srs_student s3 ON cs3.stu_id = s3.stu_id
		JOIN srs_class c3 ON s3.class_id = c3.class_id
		JOIN srs_majors m3 ON c3.major_id = m3.major_id
		JOIN sys_dept d2 ON m3.college_id = d2.dept_id
		WHERE d2.dept_id = d.dept_id
		AND cs3.avg_score > cs.avg_score) AS temp2) + 1 AS deptRanking,
		(SELECT COUNT(*)
		FROM (SELECT cs4.avg_score
		FROM srs_course_score cs4
		JOIN srs_student s4 ON cs4.stu_id = s4.stu_id
		JOIN srs_class c4 ON s4.class_id = c4.class_id
		WHERE c4.class_id = c.class_id
		AND cs4.avg_score > cs.avg_score) AS temp3) + 1 AS classRanking
		FROM srs_course_score cs
		JOIN srs_student s ON cs.stu_id = s.stu_id
		JOIN srs_class c ON s.class_id = c.class_id
		JOIN srs_majors m ON c.major_id = m.major_id
		JOIN sys_dept d ON m.college_id = d.dept_id
		JOIN srs_stu_year y ON cs.stu_year_id = y.id
		JOIN srs_grade g ON c.grade_id = g.grade_id
		<where>
			<if test="stuId != null and stuId != ''">
				AND cs.stu_id = #{stuId}
			</if>
			<if test="classId != null and classId != ''">
				AND c.class_id = #{classId}
			</if>
			<if test="deptId != null and deptId != ''">
				AND d.dept_id = #{deptId}
			</if>
			<if test="gradeId != null and gradeId != ''">
				AND g.grade_id = #{gradeId}
			</if>
			<if test="stuYearId != null and stuYearId != ''">
				AND y.id = #{stuYearId}
			</if>
			<if test="majorId != null and majorId != ''">
				AND m.major_id = #{majorId}
			</if>
		</where>
		ORDER BY
		<if test="sortType == 0 || sortType == null">
			cs.avg_score DESC
		</if>
		<if test="sortType == 1">
			cs.avg_score ASC
		</if>
	
	</select>

	<select id="selectSumCourseScore" parameterType="sumCourseScore" resultMap="sumCourseScoreS">
		select * from sum_course_score where data_status='1' and chmod_status='1'
	</select>

	<update id="updateSum" parameterType="sumCourseScore">
		update sum_sport_test
		<trim prefix="SET" suffixOverrides=",">
			<if test="stuNo != null">stu_no = #{stuNo},</if>
			<if test="avgScore != null">avg_score = #{avgScore},</if>
			<if test="stuYearId != null">stu_year_id = #{stuYearId},</if>
			<if test="writeTime != null">write_time = #{writeTime},</if>
			<if test="readTime != null">read_time = #{readTime},</if>
			<if test="dataStatus != null">data_status = #{dataStatus},</if>
			<if test="chmodStatus != null">chmod_status = #{chmodStatus},</if>
		</trim>
		where id = #{id}
	</update>
</mapper>