<?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.sstudy.mapper.paperstudent.PaperStudentScantronMapper">

    <!-- 获取学生答题卡信息 -->
    <select id="listPaperStudentScantrons" resultType="com.xhkjedu.sstudy.vo.paperstudent.PaperStudentScantronVo">
        SELECT pss.pssid,pss.paperid,pss.studentid,pss.psid,pss.qn,pss.qorder,pss.answered,pss.answertype,
               pss.useranswer,pss.converted,pss.useranswertext,pss.useranswertxt,pss.good,
        pss.answertime,pss.costtime,pss.checked,pss.stuscore,pss.qscore,pss.pscaid,pss.checkcomm,
        psc.qtypeid,psc.qtypename,psc.ctype,psc.psanswer,psc.optionnum
        FROM t_paper_student_scantron pss
        LEFT JOIN t_paper_scantron psc ON pss.pscaid = psc.pscaid
        WHERE pss.psid=#{psid} order by pss.qorder
    </select>

    <!--获取学生答题卡信息 无正确答案-->
    <select id="listPaperStudentScantronsNoAnswer" resultType="java.util.Map">
        SELECT pss.pssid,pss.paperid,pss.studentid,pss.psid,pss.qn,pss.qorder,pss.answered,pss.answertype,
        pss.useranswer,pss.converted,pss.useranswertxt,pss.qscore,pss.pscaid,pss.checkcomm,
        psc.qtypeid,psc.qtypename,psc.ctype,psc.optionnum,0 as costtime,0 as stuscore,'[]' as psanswer
        FROM t_paper_student_scantron pss
        LEFT JOIN t_paper_scantron psc ON pss.pscaid = psc.pscaid
        WHERE pss.psid=#{psid} order by pss.qorder
    </select>

    <!-- 获取学生答题卡待批改试题列表- -->
    <select id="sListCorrectQuestion" resultType="com.xhkjedu.sstudy.vo.paperstudent.PaperStudentScantronVo">
        SELECT pss.pssid,pss.paperid,pss.studentid,pss.psid,pss.qn,pss.qorder,pss.answered,pss.useranswer,pss.answertype,pss.converted,
        pss.useranswertext,pss.useranswertxt,pss.answertime,pss.costtime,pss.checked,pss.stuscore,pss.qscore,pss.pscaid,pss.checkcomm,
        pss.good,psc.qtypeid,psc.qtypename,psc.ctype,psc.psanswer
        FROM t_paper_student_scantron pss
        LEFT JOIN t_paper_scantron psc ON pss.pscaid = psc.pscaid
        WHERE pss.psid=#{psid} and pss.answered=1 and psc.ctype in(3,13,14,15)
        order by pss.qorder
    </select>

    <!--获取原试卷中试题-->
    <select id="listPaperScantrons" resultType="com.xhkjedu.sstudy.model.paperstudent.TPaperStudentScantron">
        SELECT psc.pscaid, psc.paperid,psc.ctype,psc.psorder as qorder,psc.qn,psc.psscore as qscore,
        ps.classid,ps.studentid,ps.schoolid,ps.psid
        FROM t_paper_scantron psc LEFT JOIN t_paper_student ps ON psc.paperid=ps.paperid
        WHERE ps.psid=#{psid}
    </select>

    <!-- 批量保存学生试题关联信息 -->
    <insert id="saveBatchStudentScantron" parameterType="java.util.List">
        INSERT INTO t_paper_student_scantron (paperid,classid,studentid,pscaid,qorder,qn,qscore,schoolid,psid)
        VALUES
        <foreach collection ="list" item="r" index= "index" separator =",">
            (#{r.paperid},#{r.classid},#{r.studentid},#{r.pscaid},#{r.qorder},#{r.qn},#{r.qscore},#{r.schoolid},#{r.psid})
        </foreach>
    </insert>

    <!--自动批改学生未做试题-->
    <update id="updateScantronPSQByNotDo">
        UPDATE t_paper_student_scantron pss
        SET pss.costtime=0,pss.checked=1,pss.stuscore=0,pss.checkid=0,pss.checktime=#{checktime}
        WHERE pss.psid=#{psid} AND pss.answered=0
    </update>
    <!--获取该作业该学生未批改作业数量-->
    <select id="getScantronNotCorrect" resultType="java.lang.Integer">
        SELECT count(*) FROM t_paper_student_scantron pss
        WHERE pss.psid=#{psid} AND checked!=1
    </select>

    <!--批量更新试题批改信息-->
    <update id="updateScantronQuestionBatch" parameterType="java.util.List">
        UPDATE t_paper_student_scantron
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="stuscore = CASE " suffix="END,">
                <foreach collection="list" index="index" item="item" >
                    when pssid = #{item.pssid} then #{item.stuscore}
                </foreach>
            </trim>
            <trim prefix="checked = CASE " suffix="END,">
                <foreach collection="list" index="index" item="item" >
                    when pssid = #{item.pssid} then #{item.checked}
                </foreach>
            </trim>
            <trim prefix="checkid = CASE " suffix="END,">
                <foreach collection="list" index="index" item="item" >
                    when pssid = #{item.pssid} then #{item.checkid}
                </foreach>
            </trim>
            <trim prefix="checktime = CASE " suffix="END,">
                <foreach collection="list" index="index" item="item" >
                    when pssid = #{item.pssid} then #{item.checktime}
                </foreach>
            </trim>
            <trim prefix="checkcomm = CASE " suffix="END,">
                <foreach collection="list" index="index" item="item" >
                    <if test="item.checkcomm!=null">when pssid = #{item.pssid} then #{item.checkcomm}</if>
                </foreach>
            </trim>
            <trim prefix="good = CASE " suffix="END,">
                <foreach collection="list" index="index" item="item" >
                    <if test="item.good!=null">when pssid = #{item.pssid} then #{item.good}</if>
                </foreach>
            </trim>
        </trim>
        where
        <foreach collection="list" item="item" index="index" separator="or">
            pssid =#{item.pssid}
        </foreach>
    </update>

    <!--计算所有试题用时-->
    <select id="getSumScantronsCosttime" resultType="java.lang.Integer">
         SELECT SUM(pss.costtime) FROM t_paper_student_scantron pss
         WHERE pss.psid=#{psid}
    </select>

    <!--修改学生答案图片，添加教师批阅-->
    <update id="updateUseranswer">
        UPDATE t_paper_student_scantron SET useranswer=#{useranswer} WHERE pssid=#{pssid}
    </update>

    <!--获取学生未提交试题集合-->
    <select id="listPSQByNotDo" resultType="com.xhkjedu.sstudy.model.paperstudent.TPaperStudentScantron">
        SELECT pss.pscaid,pss.qscore,ps.qtypeid,ps.psorder as qorder
        FROM t_paper_student_scantron pss left join t_paper_scantron ps on pss.pscaid = ps.pscaid
        WHERE pss.pscaid=#{psid} AND pss.checked=0 AND pss.answered=0
    </select>
    <!--批量修改答题卡作业学生作答信息-->
    <update id="batchUpdateQuestion">
        UPDATE t_paper_student_scantron
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="answered = CASE " suffix="END,">
                <foreach collection="list" index="index" item="item">
                    when pssid = #{item.pssid} then #{item.answered}
                </foreach>
            </trim>
            <trim prefix="useranswer = CASE " suffix="END,">
                <foreach collection="list" index="index" item="item">
                    when pssid = #{item.pssid} then #{item.useranswer}
                </foreach>
            </trim>
            <trim prefix="converted = CASE " suffix="END,">
                <foreach collection="list" index="index" item="item">
                    when pssid = #{item.pssid} then #{item.converted}
                </foreach>
            </trim>
            <trim prefix="useranswertext = CASE " suffix="END,">
                <foreach collection="list" index="index" item="item">
                    when pssid = #{item.pssid} then #{item.useranswertext}
                </foreach>
            </trim>
            <trim prefix="useranswertxt = CASE " suffix="END,">
                <foreach collection="list" index="index" item="item">
                    when pssid = #{item.pssid} then #{item.useranswertxt}
                </foreach>
            </trim>
            <trim prefix="answertype = CASE " suffix="END,">
                <foreach collection="list" index="index" item="item">
                    when pssid=#{item.pssid} then #{item.answertype}
                </foreach>
            </trim>
            <trim prefix="answertime = CASE " suffix="END,">
                <foreach collection="list" index="index" item="item">
                    when pssid = #{item.pssid} then if(#{item.answertime} is null
                    or #{item.answertime}=0,answertime,#{item.answertime})
                </foreach>
            </trim>
            <trim prefix="stuscore = CASE " suffix="END,">
                <foreach collection="list" index="index" item="item">
                    when pssid = #{item.pssid} then ifnull(#{item.stuscore},stuscore)
                </foreach>
            </trim>
            <trim prefix="checked = CASE " suffix="END,">
                <foreach collection="list" index="index" item="item">
                    when pssid = #{item.pssid} then ifnull(#{item.checked},checked)
                </foreach>
            </trim>
            <trim prefix="checkid = CASE " suffix="END,">
                <foreach collection="list" index="index" item="item">
                    when pssid = #{item.pssid} then ifnull(#{item.checkid},checkid)
                </foreach>
            </trim>
            <trim prefix="checktime = CASE " suffix="END,">
                <foreach collection="list" index="index" item="item">
                    when pssid = #{item.pssid} then ifnull(#{item.checktime},checktime)
                </foreach>
            </trim>
        </trim>
        where
        <foreach collection="list" item="item" index="index" separator="or">
            pssid = #{item.pssid}
        </foreach>
    </update>

    <!--附件作业 按题批阅列表-->
    <resultMap id="questionResult" type="java.util.Map">
        <result property="qscore" column="qscore"/>
        <result property="qorder" column="qorder"/>
        <result property="qn" column="qn"/>
        <collection property="students" ofType="java.util.Map" javaType="java.util.List">
            <result property="stuscore" column="stuscore"/>
            <result property="answered" column="answered"/>
            <result property="checked" column="checked"/>
            <result property="checkcomm" column="checkcomm"/>
            <result property="psid" column="psid"/>
            <result property="pssid" column="pssid"/>
            <result property="answertype" column="answertype"/>
            <result property="useranswer" column="useranswer"/>
            <result property="converted" column="converted"/>
            <result property="useranswertext" column="useranswertext"/>
            <result property="useranswertxt" column="useranswertxt"/>
            <result property="good" column="good"/>
            <result property="studentname" column="username"/>
        </collection>
    </resultMap>
    <select id="sqcorrectList" resultMap="questionResult">
        select pss.qscore,pss.qn,pss.qorder,pss.stuscore,pss.answered,pss.checked,pss.checkcomm,pss.psid,pss.pssid
        ,pss.answertype,pss.useranswer,pss.converted,pss.useranswertext,pss.useranswertxt,pss.good,u.username
        from t_paper_student_scantron pss left join t_paper_scantron ps on pss.pscaid = ps.pscaid
        left join t_user u on pss.studentid = u.userid
        where pss.psid in
        <foreach collection="psids" item="psid" index="index" open="(" separator="," close=")">${psid}</foreach>
        and ps.ctype in(3,13,14,15)
        order by pss.checked,pss.qorder,convert(u.username using gbk),u.userid
    </select>
    <!--获取学生作业未批改试题数量-->
    <select id="getNoCheckNumByPsId" resultType="integer">
        select count(*) from t_paper_student_scantron where psid=#{psid} and answered=1 and checked!=1
    </select>
    <!--获取未合并的学生答案-->
    <select id="listNoMergeUserAnswer" resultType="com.xhkjedu.sstudy.model.paperstudent.TPaperStudentScantron">
        select pssid,useranswer from t_paper_student_scantron where converted=0
        <if test="psid!=0">and psid=#{psid}</if>
    </select>

    <!--获取作业所有学生试题作答情况-->
    <select id="listAllStuAnswers" resultType="com.xhkjedu.sstudy.vo.paperreport.PaperRStuQuestionVo">
        select s.pssid as psqid,s.paperid,s.classid,s.studentid,s.psid,s.qn,s.qorder,s.answered,s.answertype,s.useranswer,
        s.useranswertext,s.useranswertxt,s.costtime,s.stuscore,s.qscore,s.schoolid,s.checked,s.pscaid as ptqid,p.ctype
        from t_paper_student_scantron s left join t_paper_scantron p on s.paperid=p.paperid and s.pscaid=p.pscaid
        where s.paperid=#{paperid} order by s.studentid, s.pscaid,s.qorder;
    </select>
    <!--获取作业中所有试题-->
    <select id="listAllQuesByPaperid" resultType="com.xhkjedu.sstudy.vo.paperreport.PaperRQuestionVo">
        select pscaid ptqid,qtypename,ctype,qn,psorder qorder,psscore qscore
        from t_paper_scantron where paperid=#{paperid} order by psorder
    </select>

    <!--清空指定作业所有优秀作答-->
    <update id="updatePaperStudentGoodByPsid">
        update t_paper_student_scantron set good=0 where psid=#{psid} and good=1
    </update>

    <!--标记单题是否优秀作答-->
    <update id="updatePaperStudentGoodByPsqid">
        update t_paper_student_scantron set good=#{good} where pssid=#{psqid}
    </update>


    <resultMap id="typeSQuestion" type="com.xhkjedu.sstudy.vo.paperstudent.StudentPaperTypeVo">
        <result property="qtypename" column="qtypename"/>
        <result property="ptorder" column="ptorder"/>
        <result property="ptscore" column="ptscore"/>
        <result property="ptnum" column="ptnum"/>
        <collection property="scantrons" ofType="com.xhkjedu.sstudy.vo.paperstudent.PaperStudentScantronVo">
            <result property="pssid" column="pssid"/>
            <result property="paperid" column="paperid"/>
            <result property="studentid" column="studentid"/>
            <result property="psid" column="psid"/>
            <result property="pscaid" column="pscaid"/>
            <result property="qn" column="qn"/>
            <result property="qorder" column="qorder"/>
            <result property="answered" column="answered"/>
            <result property="answertype" column="answertype"/>
            <result property="useranswer" column="useranswer"/>
            <result property="converted" column="converted"/>
            <result property="useranswertxt" column="useranswertxt"/>
            <result property="qscore" column="qscore"/>
            <result property="qtypeid" column="qtypeid"/>
            <result property="qtypename" column="qtypename"/>
            <result property="ctype" column="ctype"/>
            <result property="optionnum" column="optionnum"/>
            <result property="checkcomm" column="checkcomm"/>
        </collection>
    </resultMap>
    <!--学生作答时作业试题-->
    <select id="listPaperStuSacnsByPsid" resultMap="typeSQuestion">
        SELECT psq.qtypename,psq.pstorder ptorder,psq.pstscore ptscore,psq.pstnum ptnum,
        pss.pssid,pss.paperid,pss.studentid,pss.psid,pss.qn,pss.qorder,pss.answered,pss.answertype,pss.answertime,pss.useranswer,pss.converted,pss.useranswertxt,
        pss.qscore,pss.pscaid,pss.checkcomm,psc.qtypeid,psc.ctype,psc.optionnum
        FROM t_paper_student_scantron pss LEFT JOIN t_paper_scantron psc ON pss.pscaid = psc.pscaid
        LEFT JOIN t_paper_scantron_qtype psq ON psc.paperid = psq.paperid AND psc.psqtypeid=psq.psqtypeid
        WHERE pss.psid=#{psid} ORDER BY psq.pstorder,pss.qorder
    </select>

</mapper>
