<?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.exam.SubjectScoreMapper">

    <!--成绩管理-科目成绩-->
    <select id="listExamSubjectScore" resultType="java.util.Map">
        select s.msid,s.subjectid,s.subjectname,count(ps.mpsid) stunum,count(if(ps.sstate=3,ps.mpsid,null)) missnum,
        max(if(ps.sstate &lt; 3,ps.stuscore,null)) maxscore,min(if(ps.sstate &lt; 3,ps.stuscore,null)) minscore,
        ROUND(avg(if(ps.sstate &lt; 3,ps.stuscore,null)),2) avgscore
        from ms_subject s left join ms_paper_student ps on s.msid=ps.msid
        where s.examid=#{examid} group by s.subjectid order by s.msid
    </select>


    <!--成绩管理-科目客观题主观题平均分-->
    <select id="listSubjectQuestionAvgScore" resultType="java.util.Map">
        select ps.msid,q.subjectid,ROUND(avg(if(q.ctype in (1,2,4,5,6),q.stuscore,null)),2) avgobj,
        ROUND(avg(if(q.ctype in (3,13,14,15),q.stuscore,null)),2) avgsub
        from ms_paper_student_question q
        left join ms_paper_student ps on q.mpsid=ps.mpsid
        where ps.examid=#{examid} and ps.sstate &lt; 3 group by ps.subjectid order by ps.msid
    </select>

    <!--成绩管理-缺考学生-->
    <select id="listSubjectMissStudents" resultType="java.util.Map">
        select DISTINCT ps.studentid,cs.classid,u.username as studentname,u.usersex,u.examno,ps.batchid from ms_paper_student ps
        left join ms_class_student cs on ps.studentid=cs.studentid
        left join t_user u on cs.studentid=u.userid
        where ps.examid=#{stu.examid} and ps.subjectid=#{stu.subjectid} and ps.sstate=3
        <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 cs.mcsid
    </select>

    <!--成绩管理-缺考学生-导出-->
    <select id="listSubjectMissStudentExport" resultType="com.xhkjedu.smarking.vo.exam.MissExamVo">
        select distinct ps.mspid,ps.batchid,ps.studentid,cs.classid,u.username studentname,u.usersex,u.examno,c.classname,ps.subjectid,s.subjectname
        from ms_paper_student ps
        left join ms_class_student cs on ps.studentid=cs.studentid
        left join ms_class c on cs.classid=c.classid and ps.examid=c.examid
        left join t_user u on cs.studentid=u.userid
        left join ms_subject s on ps.subjectid=s.subjectid
        where ps.examid=#{stu.examid} and ps.sstate=3 and ps.subjectid in
        <foreach collection="stu.subjectids" item="s" open="(" close=")" separator=",">
            #{s}
        </foreach>
        order by ps.msid,c.classtype,c.classid,c.mcid,cs.mcsid
    </select>
</mapper>
