<?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.xhkjedu.smarking.mapper.stupaper.MsPaperStudentScoreMapper">

    <!--补录成绩-列表-->
    <resultMap id="stuScoreResult" type="java.util.Map">
        <result column="examid" property="examid" />
        <result column="studentid" property="studentid" />
        <result column="studentname" property="studentname" />
        <result column="examno" property="examno" />
        <result column="classname" property="classname" />
        <collection property="subjects" ofType="java.util.Map" javaType="java.util.ArrayList">
            <result column="mpsid" property="mpsid" />
            <result column="subjectid" property="subjectid" />
            <result column="subjectname" property="subjectname" />
            <result column="stuscore" property="stuscore" />
            <result column="stuscoretype" property="stuscoretype" />
        </collection>
    </resultMap>
    <select id="listStuScores" resultMap="stuScoreResult">
        select ss.studentid,ss.examid,u.username as studentname,u.examno,c.classname,
        ps.mpsid,ps.subjectid,s.subjectname,ps.stuscore,ps.stuscoretype
        from ms_paper_student_score ss
        left join ms_paper_student ps on ss.examid=ps.examid and ss.studentid=ps.studentid
        left join ms_subject s on ss.examid=s.examid and ps.subjectid=s.subjectid
        left join t_user u on ss.studentid=u.userid
        left join ms_class_student cs on ss.examid=cs.examid and ss.studentid=cs.studentid
        left join ms_class c on cs.classid=c.classid
        where ss.examid=#{examid} group by ps.mpsid order by ss.createtime desc,c.classtype
    </select>

    <!--补录成绩-补录历史-->
    <select id="listStuScoresHistory" resultType="java.util.Map">
        select ss.fillscore,ss.createtime,s.subjectname,ss.studentid,u.username as studentname,
        u.usersex,u.examno,c.classname
        from ms_paper_student_score ss
        left join ms_subject s on ss.examid=s.examid and ss.subjectid=s.subjectid
        left join t_user u on ss.studentid=u.userid
        left join ms_class_student cs on ss.examid=cs.examid and ss.studentid=cs.studentid
        left join ms_class c on cs.classid=c.classid
        where ss.examid=#{stu.examid}
        <if test="stu.subjectid!=null and stu.subjectid!=''">
            and ss.subjectid=#{stu.subjectid}
        </if>
        <if test="stu.classid!=null and stu.classid!=0">
            and cs.classid=#{stu.classid}
        </if>
        <if test="stu.studentname!=null and stu.studentname!=''">
            and (u.username like concat('%', #{stu.studentname}, '%') or u.examno like concat('%',#{stu.studentname},'%')
        </if>
        order by  ss.createtime desc,c.classtype
    </select>

    <!--补录成绩-删除学生补录成绩-->
    <delete id="deleteStuScoreByExamid">
        delete from ms_paper_student_score where examid=#{examid} and studentid=#{studentid}
    </delete>

    <!--补录成绩-新增补录-班级学生-->
    <select id="listStudentsForAdd" resultType="java.util.Map">
        select * from (select cs.studentid,cs.mcsid,u.username as studentname,u.examno,
        (select count(*) from ms_paper_student ps where ps.examid=#{examid} and ps.studentid=cs.studentid
        and ps.stuscore=0 and ps.stuscoretype=0) haszero
        from ms_class_student cs
        left join t_user u on cs.studentid=u.userid
        where cs.examid=#{examid} and cs.classid=#{classid} group by cs.studentid order by cs.mcsid ) a order by a.haszero desc,a.mcsid
    </select>

    <!--补录成绩-新增补录-学生科目分数-->
    <select id="listStudentSubjectScores" resultType="java.util.Map">
        select ps.mpsid,ps.stuscore,ps.stuscoretype,ps.subjectid,s.subjectname
        from ms_paper_student ps left join ms_subject s on ps.examid=s.examid and ps.subjectid=s.subjectid
        where ps.examid=#{examid} and ps.studentid=#{studentid} order by s.msid
    </select>

    <!--补录成绩-导入-考试科目-->
    <select id="listSubjectsByExamid" resultType="com.xhkjedu.smarking.vo.exam.ESubjectVo">
        select subjectid,subjectname from ms_subject where examid=#{examid} order by msid
    </select>
    <!--补录成绩-导入-考试班级-->
    <select id="listClassesByExamid" resultType="com.xhkjedu.smarking.model.exam.MsClass">
        select * from ms_class where examid=#{examid} order by classtype
    </select>
    <!--补录成绩-导入-考试学生-->
    <select id="listStudentScoreByExamid" resultType="com.xhkjedu.smarking.vo.stupaper.PaperStuScoreVo">
        select distinct cs.studentid,cs.subjectid,cs.examid,cs.classid,c.classname,ps.mpsid,ps.mpid,ps.subjectid,
        ps.stuscore,ps.stuscoretype,u.username as studentname,u.usersex,u.examno
        from ms_class_student cs
        left join ms_paper_student ps on ps.examid=cs.examid and ps.subjectid=cs.subjectid and ps.studentid=cs.studentid
        left join ms_class c on ps.examid=c.examid and ps.subjectid=c.subjectid and cs.classid=c.classid
        left join t_user u on ps.studentid=u.userid
        where cs.examid=#{examid} order by cs.mcsid
    </select>


</mapper>
