<?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.sexam.mapper.report.EReportGenerateMapper">
    <!--考试科目-->
    <select id="listSubject" resultType="java.util.Map">
        select s.subjectid,s.subjectname,p.epid,p.ptype,p.pscore from e_subject s left join e_paper p on s.esid=p.esid
        where s.examid=#{examid}
        <if test="exammode==1">
            and s.pstate=2
        </if>
        order by s.begintime,s.esid
    </select>
    <!--删除考试报告-->
    <delete id="deleteExamReport">
        delete from e_rbase where examid=#{examid};
        delete from e_rclass where examid=#{examid};
        delete from e_rclass_action where examid=#{examid};
        delete from e_rclass_complexity where examid=#{examid};
        delete from e_rclass_point where examid=#{examid};
        delete from e_rclass_qtype where examid=#{examid};
        delete from e_rclass_question where examid=#{examid};
        delete from e_rclass_rank where examid=#{examid};
        delete from e_rrank where examid=#{examid};
        delete from e_rstudent where examid=#{examid};
        delete from e_rstudent_action where examid=#{examid};
        delete from e_rstudent_complexity where examid=#{examid};
        delete from e_rstudent_point where examid=#{examid};
        delete from e_rstudent_qtype where examid=#{examid};
        delete from e_rsubject where examid=#{examid};
    </delete>
    <!--学生上次考试排名及目标-->
    <select id="listStuRankAndGoal" resultType="com.xhkjedu.sexam.model.reportstu.ERstudent">
        select er.studentid,er.subjectid,er.schoolrank,er.nextgoal,er.classrank
        from(select max(er.rsid)rsid from e_class_student ecs left join e_rstudent er on ecs.studentid = er.studentid
        where ecs.examid=#{examid} and ecs.examid!=er.examid and er.rsid is not null group by er.studentid,er.subjectid)a
        left join e_rstudent er on a.rsid=er.rsid
    </select>
    <!--学生违纪行为-->
    <select id="listBadActionByExamId" resultType="java.util.Map">
        select studentid,classid,es.subjectid,es.subjectname,count(esb.esbsid)num
        from e_subject_badstudent esb left join e_subject es on esb.esid = es.esid
        where esb.examid=#{examid}
        group by studentid, esb.esid
        order by es.begintime,es.esid,esb.esid
    </select>
    <!--学生优秀试卷-->
    <select id="listGoodActionByExamId" resultType="java.util.Map">
        select studentid,classid,epsq.subjectid,s.subjectname,count(distinct epsq.qorder)num
        from e_paper_student_question epsq left join e_subject s on epsq.examid=s.examid and epsq.subjectid=s.subjectid
        where epsq.examid=#{examid} and good=1
        group by studentid, epsq.subjectid
        order by s.begintime,s.esid,epsq.epsid
    </select>
    <!--学生违规试卷-->
    <select id="listBadQuesActionByExamId" resultType="java.util.Map">
        select studentid,classid,epsq.subjectid,s.subjectname,count(distinct epsq.qorder)num
        from e_paper_student_question epsq left join e_subject s on epsq.examid=s.examid and epsq.subjectid=s.subjectid
        where epsq.examid=#{examid} and bad=1
        group by studentid, epsq.subjectid
        order by s.begintime,s.esid,epsq.epsid
    </select>
    <!--获取试卷分数-->
    <select id="getScoreByExamAndSubjectId" resultType="java.lang.Double">
        select sum(ep.pscore) from e_paper ep where ep.examid=#{examid}
        <if test="subjectid!='zf'">
            and ep.subjectid=#{subjectid}
        </if>
    </select>
    <!--获取考试发布学生数量-->
    <select id="getStuNumByExamId" resultType="java.lang.Integer">
        select sum(classnum) from e_class where examid=#{examid}
    </select>
    <!--根据考试id获取考试模式-->
    <select id="getExamModeById" resultType="java.lang.Integer">
        select exammode from e_base where examid=#{examid}
    </select>
    <!--获取未处理的学生作答情况-->
    <select id="listAnswerJson" resultType="com.xhkjedu.sexam.model.reportclass.ERclassQuestion">
        select q.answerjson,q.rcqid,q.examid,q.epid,q.classid,q.qn,q.score,q.eptqid
        ,(case when q.eptqid is not null then (select count(*) from e_paper_student_question s
        where s.eptqid=q.eptqid and s.qscore=s.stuscore)
        else 0 end)fullscorenum
        from e_rclass_question q where q.answerjson not like '%numrate%'
    </select>
    <!--获取复合题满分人数-->
    <select id="getFullscorenum" resultType="java.lang.Integer">
        select count(*) from(select sum(s.stuscore)sscore,s.studentid
        from e_paper_student_question s where s.epid=#{q.epid} and find_in_set(s.qn,#{q.qn})
        group by s.studentid )a where a.sscore=#{q.score}
    </select>
    <!--获取考试班级集合-->
    <select id="listClassByExamIds" resultType="com.xhkjedu.sexam.model.exam.EClass">
        select ec.classid,ec.classnum,ec.examid from e_class ec where ec.examid in
        <foreach collection="examids" item="examid" open="(" separator="," close=")">#{examid}</foreach>
    </select>
    <!--更新班级试题作答详情和满分-->
    <update id="updateClassQuestion">
        update e_rclass_question set answerjson=#{q.answerjson},fullscorenum=#{q.fullscorenum} where rcqid=#{q.rcqid}
    </update>
    <!--获取违规试卷关联的考试-->
    <select id="listBadQuesExam" resultType="com.xhkjedu.sexam.vo.report.EClassActionVo">
        select q.examid,q.classid,q.studentid,q.epid,s.subjectid,s.subjectname,u.username studentname,count(*) num
        from e_paper_student_question q
                 left join e_subject s on q.epid=s.esid
                 left join t_user u on q.studentid=u.userid
                 left join e_base e on q.examid=e.examid
        where e.deleted=1 and e.examstate=3 and q.bad=1
        GROUP BY q.epsid order by e.examid,q.epid,q.studentid
    </select>

    <!--数据表是否已经生成-->
    <select id="listBadQuesExam3" resultType="com.xhkjedu.sexam.model.reportclass.ERclassAction">
        select * from e_rclass_action where examid=#{examid} and actiontype=3
    </select>

    <!--获取报告保存路径-->
    <select id="getSaveFolder" resultType="java.lang.String">
        select concat(c.cloudcode,'/',b.schoolid,'/exam/',year(now()),'/reportpdf/',b.examid)
        from e_base b,t_cloud c where b.examid=#{examid} limit 1
    </select>
    <!--修改考试pdf地址-->
    <update id="updatePdf">
        <if test="report.type==1">
            update e_base set reportschool=#{report.pdfpath}
            where examid=#{report.examid}
        </if>
        <if test="report.type==2">
            update e_class set reportclass=#{report.pdfpath}
            where examid=#{report.examid} and classid=#{report.classid}
        </if>
        <if test="report.type==3">
            update e_class_student set reportstu=#{report.pdfpath}
            where examid=#{report.examid} and classid=#{report.classid} and studentid=#{report.studentid}
        </if>
        <if test="report.type==4">
            update e_class set reportstu=#{report.pdfpath}
            where examid=#{report.examid} and classid=#{report.classid}
        </if>
        <if test="report.type==5">
            insert into e_class_subject(examid, classid, subjectid, reporturl)
            values (#{report.examid},#{report.classid},#{report.subjectid},#{report.pdfpath})
            on duplicate key update reporturl=#{report.pdfpath}
        </if>
    </update>
    <!--考试班级未生成pdf的数量-->
    <select id="getNoPdfNumByExamAndClassId" resultType="java.lang.Integer">
        select count(cs.ecsid)
        from e_class_student cs
        where cs.examid=#{examid} and cs.classid=#{classid} and cs.reportstu is null
    </select>
    <!--考试未生成pdf的数量-->
    <select id="getNoPdfNumByExamId" resultType="java.lang.Integer">
        select count(*) from e_class where examid=#{examid} and reportstu is null
    </select>
    <!--考试学生列表-->
    <select id="listStudentByExamId" resultType="com.xhkjedu.sexam.model.reportstu.ERstudent">
        select s.classid,s.studentid,concat(u.loginname,'_',u.username)studentname
        from e_class_student s left join t_user u on s.studentid=u.userid
        where s.examid=#{examid}
    </select>
    <!--获取考试科目id列表-->
    <select id="listSubjectIdByExamId" resultType="java.lang.String">
        select subjectid from e_subject where examid=#{examid}
    </select>
</mapper>
