<?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.SrsCqScoreMapper">

	<resultMap type="SrsCqScore" id="SrsCqScoreResult">
		<result property="id" column="id"/>
		<result property="stuNo" column="stu_no"/>
		<result property="stuYearId" column="stu_year_id"/>
		<result property="cqScore" column="cq_score"/>
		<result property="createTime" column="create_time"/>
		<result property="createBy" column="create_by"/>
		<result property="updateTime" column="update_time"/>
		<result property="updateBy" column="update_by"/>
		<result property="delFlag" column="del_flag"/>
		<result property="gradeName" column="grade_name"/>
		<result property="deptName" column="dept_name"/>
		<result property="majorsName" column="major_name"/>
		<result property="className" column="class_name"/>
		<result property="stuYearName" column="stu_year_name"/>
		<result property="name" column="name"/>
	</resultMap>
	<sql id="selectSrsCqScoreVo">
		select cq.id,
		       cq.stu_no,
		       cq.stu_year_id,
		       cq_score,
		       cq.create_time,
		       cq.create_by,
		       cq.update_time,
		       cq.update_by,
		       cq.del_flag,
		       s.name,
		       g.grade_name,
		       d.dept_name,
		       m.major_name,
		       c.class_name,
		       stu_year_name

		from srs_cq_score cq
                 left join srs_student s
		on s.stu_no = cq.stu_no
			left JOIN srs_class c ON s.class_id = c.class_id
			left JOIN srs_grade g ON c.grade_id = g.grade_id
			left JOIN srs_majors m ON c.major_id = m.major_id
			left JOIN sys_dept d ON m.college_id = d.dept_id
			left JOIN srs_stu_year sy ON cq.stu_year_id = sy.id

	</sql>
	<select id="getComprehensiveSummary" parameterType="int" statementType="CALLABLE" resultType="java.util.Map">
		{CALL test2(#{id, mode=IN})}
	</select>

	<select id="selectAll" resultMap="SrsCqScoreResult" parameterType="integer">
		select * from srs_cq_score where stu_year_id = #{id}
	</select>
	<select id="generateComprehensiveSummary" parameterType="int" statementType="CALLABLE">
		{CALL test(#{id, mode=IN})}
	</select>

	<select id="getOwn" resultType="com.srs.comprehensive.domain.Vo.CphOwnScore">
		select t1.stu_year_id,t1.stu_year_name,t1.total_score,t1.iam_score,t1.sport_score,
			   t1.ce_score,t1.stu_score,t1.classtwo_score,t1.class_rank,t1.major_rank
		from (
				 select a.stu_year_id,b.stu_year_name,
						a.cph_score as total_score,
						a.iam_score as iam_score,
						a.sport_score as sport_score,
						a.ce_score as ce_score,
						a.stu_score as stu_score,
						a.classtwo_score as classtwo_score,
						a.stu_no,
						RANK() OVER(PARTITION BY a.class_id,a.stu_year_id,c.grade_id ORDER BY a.cph_score DESC ) as class_rank,
						 RANK() OVER(PARTITION BY a.major_id,a.stu_year_id,c.grade_id ORDER BY a.cph_score DESC ) as major_rank
				 from cph_total_score as a
						  left join srs_stu_year as b on a.stu_year_id = b.id
						  left join srs_class as c on a.class_code = c.class_code
						  left join srs_student as e on a.stu_no = e.stu_no
						  left join srs_majors as d on c.major_id = d.major_id
				 ORDER BY a.stu_year_id,a.stu_no
			 ) as t1
		where t1.stu_no = #{stuNo}
	</select>

	<select id="getOwn2" resultType="com.srs.comprehensive.domain.Vo.CphOwnScore">
		select t1.stu_year_id,t1.stu_year_name,t1.total_score,t1.iam_score,t1.sport_score,
			   t1.ce_score,t1.stu_score,t1.classtwo_score,t1.class_rank,t1.major_rank
		from (
				 select a.stu_year_id,b.stu_year_name,
						a.cph_score as total_score,
						a.iam_score as iam_score,
						a.sport_score as sport_score,
						a.ce_score as ce_score,
						a.stu_score as stu_score,
						a.classtwo_score as classtwo_score,
						a.stu_no,
						RANK() OVER(PARTITION BY a.class_id,a.stu_year_id,c.grade_id ORDER BY a.cph_score DESC ) as class_rank,
						 RANK() OVER(PARTITION BY a.major_id,a.stu_year_id,c.grade_id ORDER BY a.cph_score DESC ) as major_rank
				 from cph_total_score as a
						  left join srs_stu_year as b on a.stu_year_id = b.id
						  left join srs_class as c on a.class_code = c.class_code
						  left join srs_student as e on a.stu_no = e.stu_no
						  left join srs_majors as d on c.major_id = d.major_id
				 ORDER BY a.stu_year_id,a.stu_no
			 ) as t1
		where t1.stu_no = #{stuNo} order  by stu_year_name desc limit 3
	</select>


	<select id="selectSrsCqScoreList" parameterType="CphSearch" resultMap="SrsCqScoreResult">
		<include refid="selectSrsCqScoreVo"/>
		<where>
			<if test="stuNo != null ">and cq.stu_no like concat('%', #{stuNo}, '%')</if>
			<if test="stuYearId != null ">and cq.stu_year_id = #{stuYearId}</if>
			<if test="gradeId != null ">and g.grade_id = #{gradeId}</if>
			<if test="deptId != null  ">and d.dept_id = #{deptId}</if>
			<if test="majorId != null ">and m.major_id = #{majorId}</if>
			<if test="classId != null ">and c.class_id = #{classId}</if>
			<if test="stuName != null  ">and name like concat('%', #{stuName}, '%')</if>
		</where>
		order by cq.stu_year_id,d.dept_id,m.major_id,g.grade_id,c.class_id
	</select>

	<select id="countScoreDis" resultType="com.srs.comprehensive.domain.Vo.DataScreen">
		select '及格' as `name`, count(*) as`value` from cph_total_score as a where a.cph_score &gt;= 60 and a.stu_year_id = #{stuYearId}
		union
		select '不及格' as `name`, count(*) as`value` from cph_total_score as a where a.cph_score &lt; 60 and a.stu_year_id = #{stuYearId}
	</select>

	<select id="rankDis" parameterType="CphSearch" resultType="com.srs.comprehensive.domain.Vo.DataScreen">
		select concat(t2.`name`,'-班级第',t2.class_rank,'名--专业第',t2.major_rank,'名') as `name`,
		t2.`value` from
		(select * from (
		select concat(c.class_name,'--',b.`name`) as `name` ,a.cph_score as `value`,
		RANK() OVER(PARTITION BY c.class_id,a.stu_year_id,c.grade_id ORDER BY a.cph_score DESC ) as class_rank,
		RANK() OVER(PARTITION BY c.major_id,a.stu_year_id,c.grade_id ORDER BY a.cph_score DESC ) as major_rank,
		c.class_id,
		c.grade_id,
		c.major_id,
		a.stu_year_id
		from cph_total_score as a
		left join srs_student as b on a.stu_no = b.stu_no
		left join srs_class as c on b.class_id = c.class_id
		ORDER BY c.major_id,c.grade_id, RANK() OVER(PARTITION BY c.major_id,a.stu_year_id,c.grade_id ORDER BY a.cph_score DESC )
		) as t1
		<where>
			<if test="stuYearId != null ">and t1.stu_year_id = #{stuYearId}</if>
			<if test="gradeId != null ">and t1.grade_id = #{gradeId}</if>
			<if test="majorId != null ">and t1.major_id = #{majorId}</if>
			<if test="classId != null ">and t1.class_id = #{classId}</if>
		</where>
		) as t2
	</select>

	<select id="countScoreDetailDis" resultType="com.srs.comprehensive.domain.Vo.DataScreen">
		select '分数≥90' as `name`, count(*) as`value` from cph_total_score as a
		where a.cph_score >=90 and a.stu_year_id = #{stuYearId}
		union
		select '80≤分数&lt;90' as `name`, count(*) as`value` from cph_total_score as a
		where a.cph_score >=80 and a.cph_score &lt;90 and a.stu_year_id = #{stuYearId}
		union
		select '70≤分数&lt;80' as `name`, count(*) as`value` from cph_total_score as a
		where a.cph_score >=70 and a.cph_score &lt;80 and a.stu_year_id = #{stuYearId}
		union
		select '60≤分数&lt;70' as `name`, count(*) as`value` from cph_total_score as a
		where a.cph_score >=60 and a.cph_score &lt;70 and a.stu_year_id = #{stuYearId}
		union
		select '分数&lt;60' as `name`, count(*) as`value` from cph_total_score as a
		where a.cph_score &lt; 60 and a.stu_year_id = #{stuYearId}
	</select>

	<select id="selectSrsCqScoreById" parameterType="Long" resultMap="SrsCqScoreResult">
		<include refid="selectSrsCqScoreVo"/>
		where cq.id = #{id}
	</select>

	<select id="listCphScore" parameterType="CphSearch" resultType="com.srs.comprehensive.domain.Vo.CphTotalScoreInfo" >
		select * from (
		select a.stu_year_id,b.stu_year_name,
		a.iam_score as iam_score,
		a.sport_score as sport_score,
		a.ce_score as ce_score,
		a.stu_score as stu_score,
		a.classtwo_score as classtwo_score,
		a.cph_score as score,a.stu_no, e.`name` as stu_name,e.`status`,c.class_name,d.major_name,
		f.dept_name,
		RANK() OVER(PARTITION BY a.class_id,a.stu_year_id,c.grade_id ORDER BY a.cph_score DESC ) as class_rank,
		RANK() OVER(PARTITION BY a.major_id,a.stu_year_id,c.grade_id ORDER BY a.cph_score DESC ) as major_rank,
		c.class_id,
		d.major_id,
		c.grade_id,
		f.dept_id
		from cph_total_score as a
		left join srs_stu_year as b on a.stu_year_id = b.id
		left join srs_class as c on a.class_code = c.class_code
		left join srs_student as e on a.stu_no = e.stu_no
		left join srs_majors as d on c.major_id = d.major_id
		left join sys_dept as f on d.college_id = f.dept_id
		) as t1
		<where>
			<if test="stuNo != null ">and t1.stu_no like concat('%', #{stuNo}, '%')</if>
			<if test="stuYearId != null ">and t1.stu_year_id = #{stuYearId}</if>
			<if test="gradeId != null ">and t1.grade_id = #{gradeId}</if>
			<if test="deptId != null  ">and t1.dept_id = #{deptId}</if>
			<if test="majorId != null ">and t1.major_id = #{majorId}</if>
			<if test="classId != null ">and t1.class_id = #{classId}</if>
			<if test="stuName != null  ">and t1.stu_name like concat('%', #{stuName}, '%')</if>
		</where>

		ORDER BY t1.stu_year_id,t1.class_id,RANK() OVER(PARTITION BY t1.major_id,t1.stu_year_id ORDER BY t1.score DESC ),t1.stu_no
	</select>

	<select id="listCphScore30" parameterType="CphSearch" resultType="com.srs.comprehensive.domain.Vo.CphTotalScoreInfo">
		select * from(
			select *,(RANK() OVER (ORDER BY cph_score DESC)) AS ranks from cph_total_score
		    where class_id=#{classId}
			AND stu_year_id=#{stuYearId}) as pm
		where pm.ranks &lt;= 30
	</select>

	<select id="exportScore" resultType="hashmap" parameterType="com.srs.comprehensive.domain.Dto.CphExport">
		select t1.1学年,t1.2学院,t1.3专业,t1.4班级,t1.5学号,t1.6姓名,t1.7思想品德,t1.8体能素质,t1.9综合评价,
		t1.10科学文化素质,t1.11第二课堂,t1.12总成绩,t1.13班级排名,t1.14专业排名,t1.15学生状态,t1.16导出时间
		from (
		select b.stu_year_name as '1学年',
		f.dept_name as '2学院',
		d.major_name as '3专业',
		c.class_name as '4班级',
		a.stu_no as '5学号'
		, e.`name` as '6姓名',
		a.iam_score as '7思想品德',
		a.sport_score as '8体能素质',
		a.ce_score as '9综合评价',
		a.stu_score as '10科学文化素质',
		a.classtwo_score as '11第二课堂',
		a.cph_score as '12总成绩',
		RANK() OVER(PARTITION BY a.class_id,a.stu_year_id,c.grade_id ORDER BY a.cph_score DESC ) as '13班级排名' ,
		RANK() OVER(PARTITION BY a.major_id,a.stu_year_id,c.grade_id ORDER BY a.cph_score DESC ) as '14专业排名',
		a.stu_year_id,a.class_id,a.major_id,a.cph_score,c.grade_id,d.college_id,
		case
		when e.`status` = '01' then '在校'
		when e.`status` = '02' then '变更离校'
		when e.`status` = '03' then '毕业'
		end as '15学生状态',
		now() as '16导出时间'
		from cph_total_score as a
		left join srs_stu_year as b on a.stu_year_id = b.id
		left join srs_class as c on a.class_code = c.class_code
		left join srs_student as e on a.stu_no = e.stu_no
		left join srs_majors as d on c.major_id = d.major_id
		left join sys_dept as f on d.college_id = f.dept_id
		) as t1
		<where>
			<if test="stuYearId != null">and t1.stu_year_id = #{stuYearId}</if>
			<if test="classId != null">and t1.class_id = #{classId}</if>
			<if test="majorId != null">and t1.major_id = #{majorId}</if>
			<if test="gradeId != null">and t1.grade_id = #{gradeId}</if>
			<if test="deptId != null">and t1.college_id = #{deptId}</if>
		</where>
		ORDER BY t1.stu_year_id,t1.class_id,t1.cph_score desc
	</select>

	<select id="listOwnFive" resultType="com.srs.comprehensive.domain.Vo.CphStuScoreType">
		select * from (
						  select a.stu_no,a.iam_score as score,b.stu_year_name , '思想品德' as type_name from cph_total_score as a
																												  left join srs_stu_year as b on a.stu_year_id = b.id
						  where stu_no  = #{stuNo}
						  union
						  select a.stu_no,a.classtwo_score as score,b.stu_year_name , '第二课堂' as type_name from cph_total_score as a
																													   left join srs_stu_year as b on a.stu_year_id = b.id
						  where stu_no  = #{stuNo}
						  union
						  select a.stu_no,a.ce_score as score,b.stu_year_name , '综合评价' as type_name from cph_total_score as a
																												 left join srs_stu_year as b on a.stu_year_id = b.id
						  where stu_no  = #{stuNo}
						  union
						  select a.stu_no,a.stu_score as score,b.stu_year_name , '科学文化素质' as type_name from cph_total_score as a
																													  left join srs_stu_year as b on a.stu_year_id = b.id
						  where stu_no  = #{stuNo}
						  union
						  select a.stu_no,a.sport_score as score,b.stu_year_name , '体能素质' as type_name from cph_total_score as a
																													left join srs_stu_year as b on a.stu_year_id = b.id
						  where stu_no  = #{stuNo}
					  ) as t1
		ORDER BY t1.stu_year_name desc
	</select>

	<select id="listOwnMajor" resultType="SrsMajors">
		select a.major_id,a.major_name
		from srs_majors as a
		left join sys_dept_map as b on a.college_id = b.old_dept_id
		where b.new_dept_id =#{id}
	</select>

	<select id="listOwnClass" resultType="SrsClass" parameterType="CphSearch">
		select a.class_name,a.class_id
		from srs_class as a
		left join srs_majors as b on a.major_id = b.major_id
		left join sys_dept_map as c on b.college_id = c.old_dept_id
		where a.major_id = #{majorId} and c.new_dept_id = #{deptId}
		ORDER BY a.class_name desc
	</select>

	<select id="listOwnCollegeAudit"  parameterType="CphSearch" resultType="CphCollegeLook">
		select * from (
		select a.iam_score,a.material,a.description,a.audit_status,a.create_time ,b.`name` as stu_name,c.rule_name,
			   b.class_id,d.class_name,e.major_id,e.major_name,f.`name` as teacher_name,d.grade_id
		from cph_iam as a
				 left join srs_student as b on a.to_add = b.stu_id
				 left join cph_rules as c on a.rule_id = c.rule_id
				 left join srs_class as d on b.class_id = d.class_id
				 left join srs_majors as e on d.major_id = e.major_id
				 left join cph_teacher as f on d.teacher_id = f.teacher_id
				left join sys_dept_map as g on e.college_id = g.old_dept_id
		where g.new_dept_id = #{deptId} and a.audit_status not in (13)
		ORDER BY a.create_time desc ) as t1
		<where>
			<if test="stuNo != null">and t1.stu_no = #{stuNo}</if>
			<if test="stuName != null">and t1.stu_name like concat('%', #{stuName}, '%')</if>
			<if test="classId != null">and t1.class_id = #{classId}</if>
			<if test="gradeId != null">and t1.grade_id = #{gradeId}</if>
			<if test="majorId != null">and t1.major_id = #{majorId}</if>
			<if test="status != null">and t1.audit_status = #{status}</if>
		</where>

	</select>

	<select id="listOwnCollegeStu" parameterType="CphSearch" resultType="CphCollegeLook">
		select * from (
		select a.stu_no,a.`name` as stu_name,b.class_name,c.major_name,a.`status` as stu_status,
		b.class_id,c.major_id,b.grade_id
		from srs_student as a
				 left join srs_class as b on a.class_id = b.class_id
				 left join srs_majors as c on b.major_id = c.major_id
				 left join srs_grade as d on b.grade_id = d.grade_id
				left join sys_dept_map as e on c.college_id = e.old_dept_id
		where e.new_dept_id = #{deptId}
		ORDER BY b.class_name desc ) as t1
		<where>
			<if test="stuNo != null">and t1.stu_no = #{stuNo}</if>
			<if test="stuName != null">and t1.stu_name like concat('%', #{stuName}, '%')</if>
			<if test="classId != null">and t1.class_id = #{classId}</if>
			<if test="gradeId != null">and t1.grade_id = #{gradeId}</if>
			<if test="majorId != null">and t1.major_id = #{majorId}</if>
		</where>
	</select>


	<select id="listOwnCollegeScore" parameterType="com.srs.comprehensive.domain.Vo.CphSearch" resultType="com.srs.comprehensive.domain.Vo.CphCollegeLook">
		select * from (
		select t1.*,
			RANK() OVER(PARTITION BY t1.class_id,t1.stu_year_id,t1.grade_id ORDER BY t1.cph_score DESC ) as cph_class_rank,
			RANK() OVER(PARTITION BY t1.major_id,t1.stu_year_id,t1.grade_id ORDER BY t1.cph_score DESC ) as cph_major_rank,
			RANK() OVER(PARTITION BY t1.class_id,t1.stu_year_id,t1.grade_id ORDER BY t1.stu_score DESC ) as stu_class_rank,
			RANK() OVER(PARTITION BY t1.major_id,t1.stu_year_id,t1.grade_id ORDER BY t1.stu_score DESC ) as stu_major_rank
		from (
			select a.stu_no,a.iam_score,a.stu_score,a.ce_score,a.classtwo_score,a.sport_score,a.cph_score,b.`name` as stu_name,c.class_name,d.major_name,b.`status` as stu_status , e.stu_year_name,
				c.class_id,c.major_id,c.grade_id,a.stu_year_id
			from cph_total_score as a
			left join srs_student as b on a.stu_no = b.stu_no
			left join srs_class as c on b.class_id = c.class_id
			left join srs_majors as d on c.major_id = d.major_id
			left join srs_stu_year as e on a.stu_year_id = e.id
			left join srs_grade as f on c.grade_id = f.grade_id
			left join sys_dept_map as g on d.college_id = g.old_dept_id
			where g.new_dept_id = #{deptId}
			ORDER BY c.class_name desc,a.stu_year_id asc,a.cph_score desc ) as t1
		) as t2
		<where>
			<if test="stuYearId != null">and t2.stu_year_id = #{stuYearId}</if>
			<if test="stuNo != null">and t2.stu_no = #{stuNo}</if>
			<if test="stuName != null">and t2.stu_name like concat('%', #{stuName}, '%')</if>
			<if test="classId != null">and t2.class_id = #{classId}</if>
			<if test="gradeId != null">and t2.grade_id = #{gradeId}</if>
			<if test="majorId != null">and t2.major_id = #{majorId}</if>
		</where>

	</select>


	<insert id="insertSrsCqScore" parameterType="SrsCqScore">
		insert into srs_cq_score
		<trim prefix="(" suffix=")" suffixOverrides=",">
			<if test="id != null">id,</if>
			<if test="stuNo != null">stu_no,</if>
			<if test="stuYearId != null">stu_year_id,</if>
			<if test="cqScore != null">cq_score,</if>
			<if test="createTime != null">create_time,</if>
			<if test="createBy != null">create_by,</if>
			<if test="updateTime != null">update_time,</if>
			<if test="updateBy != null">update_by,</if>
			<if test="delFlag != null">del_flag,</if>
		</trim>
		<trim prefix="values (" suffix=")" suffixOverrides=",">
			<if test="id != null">#{id},</if>
			<if test="stuNo != null">#{stu_no},</if>
			<if test="stuYearId != null">#{stuYearId},</if>
			<if test="cqScore != null">#{cqScore},</if>
			<if test="createTime != null">#{createTime},</if>
			<if test="createBy != null">#{createBy},</if>
			<if test="updateTime != null">#{updateTime},</if>
			<if test="updateBy != null">#{updateBy},</if>
			<if test="delFlag != null">#{delFlag},</if>
		</trim>
	</insert>

	<update id="updateSrsCqScore" parameterType="SrsCqScore">
		update srs_cq_score
		<trim prefix="SET" suffixOverrides=",">
			<if test="stuNo != null">stu_id = #{stu_no},</if>
			<if test="stuYearId != null">stu_year_id = #{stuYearId},</if>
			<if test="cqScore != null">cq_score = #{cqScore},</if>
			<if test="createTime != null">create_time = #{createTime},</if>
			<if test="createBy != null">create_by = #{createBy},</if>
			<if test="updateTime != null">update_time = #{updateTime},</if>
			<if test="updateBy != null">update_by = #{updateBy},</if>
			<if test="delFlag != null">del_flag = #{delFlag},</if>
		</trim>
		where id = #{id}
	</update>

	<delete id="deleteSrsCqScoreById" parameterType="Long">
		delete
		from srs_cq_score
		where id = #{id}
	</delete>

	<delete id="deleteSrsCqScoreByIds" parameterType="String">
		delete from srs_cq_score where id in
		<foreach item="id" collection="array" open="(" separator="," close=")">
			#{id}
		</foreach>
	</delete>

	<select id="getScoreDistribution" resultType="java.util.Map">
		select s.stu_id, s.stu_no, s.name, s.class_id,
		COALESCE(ts.score, 0) + COALESCE(ci.score, 0) + COALESCE(st.score, 0) + COALESCE(cs.score, 0) +
		COALESCE(cos.score, 0) AS total_score,
		COALESCE(ts.score, 0) as class_score,
		COALESCE(ci.score, 0) as iam_score,
		COALESCE(st.score, 0) as ts_score,
		COALESCE(cs.score, 0) as evaluate_score,
		COALESCE(cos.score, 0) as course_score,
		RANK() OVER (ORDER BY COALESCE(ts.score, 0) + COALESCE(ci.score, 0) + COALESCE(st.score, 0) + COALESCE(cs.score,
		0) + COALESCE(cos.score, 0) DESC) AS ranking
		FROM srs_student AS s
		LEFT JOIN sys_dept sd ON s.dept_id = sd.dept_id
		LEFT JOIN srs_majors sm ON s.major_id = sm.major_id
		LEFT JOIN srs_class sc ON s.class_id = sc.class_id
		LEFT JOIN srs_grade sg ON sc.grade_id = sg.grade_id
		LEFT JOIN
		-- 第二课堂
		(SELECT to_add As stu_id, IFNULL(SUM(classtwo_score)*cph_rules.max_score, 0) AS score
		FROM cph_classtwo
		JOIN cph_rules on cph_rules.rule_id = 3
		WHERE stu_year_id = #{stuYearId}
		GROUP BY to_add) AS ts ON s.stu_id = ts.stu_id
		LEFT JOIN
		-- 思想品德
		(SELECT to_add As stu_id, IFNULL(SUM(iam_score)*cph_rules.max_score, 0) AS score
		FROM cph_iam
		JOIN cph_rules ON cph_rules.rule_id = 1
		WHERE stu_year_id = #{stuYearId}
		GROUP BY to_add) AS ci ON s.stu_id = ci.stu_id
		LEFT JOIN
		-- 成绩
		(SELECT stu_id As stu_id, IFNULL(avg_score *cph_rules.max_score, 0) AS score
		FROM `srs_course_score`
		JOIN cph_rules ON cph_rules.rule_id = 2
		WHERE stu_year_id = #{stuYearId}
		) AS cos ON s.stu_id = cos.stu_id
		LEFT JOIN
		-- 综合评价
		(SELECT stu_id, IFNULL(ce_score *cph_rules.max_score, 0) AS score
		FROM `srs_ce_score`
		JOIN cph_rules ON cph_rules.rule_id = 5
		WHERE stu_year_id = #{stuYearId}
		) AS cs ON s.stu_id = cs.stu_id
		LEFT JOIN
		-- 体测
		(SELECT stu_id, IFNULL(test_score *cph_rules.max_score, 0) AS score
		FROM srs_sport_test
		JOIN cph_rules ON cph_rules.rule_id = 4
		WHERE stu_year_id = #{stuYearId}
		) AS st ON s.stu_id = st.stu_id
		<where>
			<if test="deptId != null">and sd.dept_id = #{deptId}</if>
			<if test="classId != null">and sc.class_id = #{classId}</if>
			<if test="gradeId != null">and sg.grade_id = #{gradeId}</if>
			<if test="majorId != null">and sm.major_id = #{majorId}</if>
		</where>
	</select>

	<select id="selectPerson"
	        resultType="com.srs.comprehensive.domain.Vo.SrsCeScoreClassDeptGradeMajorsStudentStuYear">
		SELECT
		s.stu_id AS id,
		s.name,
		s.stu_no AS stuNo,
		m.major_name AS majorName,
		c.class_name AS className,
		g.grade_name AS gradeName,
		d.dept_name AS deptName,
		cq.cq_score AS score,
		y.stu_year_name AS stuYearName,
		RANK() OVER (ORDER BY cq.cq_score DESC) AS ranking
		FROM
		srs_student s
		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_grade g ON c.grade_id = g.grade_id
		LEFT JOIN srs_cq_score cq ON s.stu_no = cq.stu_no
		LEFT JOIN srs_stu_year y ON cq.stu_year_id = y.id
		<where>
			y.id = #{stuYearId}
			<if test="deptId != null">and d.dept_id = #{deptId}</if>
			<if test="classId != null">and c.class_id = #{classId}</if>
			<if test="gradeId != null">and g.grade_id = #{gradeId}</if>
			<if test="majorId != null">and m.major_id = #{majorId}</if>
		</where>
		ORDER BY
		<if test="sortType == 0">cq.cq_score DESC,</if>
		<if test="sortType == 1">cq.cq_score,</if>
		s.stu_id ASC
	</select>
	<select id="cphTotalScoreClassIdInfo" resultType="java.lang.Integer">
		SELECT class_id FROM cph_total_score WHERE stu_no=#{stuNo}
	</select>

	<!-- 批量更新 -->
	<update id="batchUpdateCqScore" parameterType="java.util.List">
		<foreach collection="list" item="item" separator=";">
			UPDATE srs_cq_score
			SET cq_score = #{item.cqScore}
			WHERE id = #{item.id}
		</foreach>
	</update>

	<!-- 批量插入 -->
	<insert id="batchInsertCqScore" parameterType="java.util.List">

		INSERT INTO srs_cq_score (stu_year_id, stu_no, cq_score,create_time)
		VALUES
		<foreach collection="list" item="item" separator=",">
			(#{item.stuYearId}, #{item.stuNo}, #{item.cqScore}, NOW())
		</foreach>;
	</insert>

	<select id="listCphScore55" parameterType="CphSearch" resultType="com.srs.comprehensive.domain.Vo.CphTotalScoreInfo">
		select * from(
						 select *,(RANK() OVER (ORDER BY ce_score DESC)) AS ranks from cph_total_score
						 where class_id=#{classId}
						   AND stu_year_id=#{stuYearId}) as pm
		where pm.ranks &lt;= 55
	</select>
	<select id="classData" resultType="com.srs.comprehensive.domain.Vo.CphTotalScoreInfo">
		select * from cph_total_score where class_id=#{classId}
										AND stu_year_id=#{stuYearId}
	</select>

	<select id="getcphiamByOne" resultType="com.srs.comprehensive.domain.CphAuditDetails" parameterType="String">
		select c.rule_name as rule_name, g.stu_year_name as stu_year_name,a.iam_score as operate_score
		from cph_iam as a
				 left join srs_student as b on a.to_add = b.stu_id
				 left join cph_rules as c on a.rule_id = c.rule_id
				 left join srs_class as d on b.class_id = d.class_id
				 left join srs_majors as e on d.major_id = e.major_id
				 left join sys_dept as f on e.college_id = f.dept_id
				 left join srs_stu_year as g on a.stu_year_id = g.id
				 left join cph_audit_details as h on a.id = h.project_id
		where b.stu_no = #{stuNo} and audit_status=6
		ORDER BY a.create_time desc
	</select>
	<select id="getStuData" resultType="com.srs.comprehensive.domain.SrsCqScore" parameterType="String">
		select  a.*,b.stu_year_name from cph_total_score as a left join srs_stu_year as b on a.stu_year_id = b.id
		where stu_no=#{stuNo} order by b.stu_year_name desc limit 3
	</select>
</mapper>
