<?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="cn.reghao.tnb.content.app.exam.db.mapper.PaperResultMapper">
    <insert id="save" useGeneratedKeys="true" keyProperty="id">
        insert into exam_paper_result
        (`paper_id`,`objective_score`,`subjective_score`,`user_score`,`exam_time`,`consumed_time`,`create_by`,`marked`)
        values 
        (#{paperId},#{objectiveScore},#{subjectiveScore},#{userScore},#{examTime},#{consumedTime},#{createBy},#{marked})
    </insert>
    <insert id="saveAll" useGeneratedKeys="true" keyProperty="id">
        insert ignore into exam_paper_result
        (`paper_id`,`objective_score`,`subjective_score`,`user_score`,`exam_time`,`consumed_time`,`create_by`,`marked`)
        values
        <foreach collection="list" item="item" index="index" separator=",">
            (#{item.paperId},#{item.objectiveScore},#{item.subjectiveScore},#{item.userScore},#{item.examTime},#{item.consumedTime},#{item.createBy},#{item.marked})
        </foreach>
    </insert>

    <update id="updateSetResultMarked">
        update exam_paper_result
        set update_time=now(),`marked`=1
        where id=#{resultId}
    </update>
    
    <select id="findAll" resultType="cn.reghao.tnb.content.app.exam.model.po.PaperResult">
        select *
        from exam_paper_result
    </select>
    <select id="findById" resultType="cn.reghao.tnb.content.app.exam.model.po.PaperResult">
        select *
        from exam_paper_result
        where id=#{id}
    </select>
    <select id="findByPaperId" resultType="cn.reghao.tnb.content.app.exam.model.po.PaperResult">
        select *
        from exam_paper_result
        where paper_id=#{paperId}
        order by create_time desc
    </select>

    <select id="countByCriteria" resultType="java.lang.Integer">
        select count(*)
        from exam_paper_result
        where 1=1
        <if test="marked != null">
            and marked=#{marked}
        </if>
        <if test="paperId != null">
            and paper_id=#{paperId}
        </if>
        <if test="loginUser != null">
            and create_by=#{loginUser}
        </if>
    </select>
    <select id="findExamResultsByPage" resultType="cn.reghao.tnb.content.app.exam.model.vo.ExamResults">
        select paper.`name`,paper.total_score,
               result.paper_id,date_format(result.exam_time,'%Y-%m-%d %H:%i:%s') as exam_time,
               result.create_by as student,result.user_score,result.id as resultId
        from exam_paper_result result
        inner join exam_paper paper
        on result.paper_id=paper.id
        where 1=1
        <if test="examQuery.marked != null">
            and marked=#{examQuery.marked}
        </if>
        <if test="examQuery.paperId != null">
            and paper_id=#{examQuery.paperId}
        </if>
    </select>
</mapper>