<?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.ruoyi.mxjb.mapper.MxjbPaperTestMapper">
    
    <resultMap type="MxjbPaperTest" id="MxjbPaperTestResult">
        <result property="paperId"    column="paper_id"    />
        <result property="subjectid"    column="subjectId"    />
        <result property="gradeId"    column="grade_id"    />
        <result property="termId"    column="term_id"    />
        <result property="name"    column="name"    />
        <result property="state"    column="state"    />
        <result property="knownledges"    column="knownledges"    />
        <result property="round"    column="round"    />
        <result property="step"    column="step"    />
        <result property="creatorId"    column="creator_id"    />
        <result property="createdTime"    column="created_time"    />
    </resultMap>

    <sql id="selectMxjbPaperTestVo">
        select paper_id, subjectId, grade_id, term_id, name, state, knownledges, round, step, creator_id, created_time from mxjb_paper_test
    </sql>

    <select id="selectMxjbPaperTestList" parameterType="MxjbPaperTest" resultMap="MxjbPaperTestResult">
        <include refid="selectMxjbPaperTestVo"/>
        <where>  
            <if test="subjectid != null "> and subjectId = #{subjectid}</if>
            <if test="gradeId != null "> and grade_id = #{gradeId}</if>
            <if test="termId != null "> and term_id = #{termId}</if>
            <if test="name != null  and name != ''"> and name like concat('%', #{name}, '%')</if>
            <if test="state != null "> and state = #{state}</if>
            <if test="knownledges != null  and knownledges != ''"> and knownledges = #{knownledges}</if>
            <if test="round != null "> and round = #{round}</if>
            <if test="step != null "> and step = #{step}</if>
            <if test="creatorId != null "> and creator_id = #{creatorId}</if>
            <if test="createdTime != null "> and created_time = #{createdTime}</if>
        </where>
        order by round desc
    </select>

    <select id="selectExamPaperTestList" parameterType="map" resultMap="MxjbPaperTestResult">
        SELECT p.paper_id, p.subjectId, p.grade_id, p.term_id, p.name, IF(e.`score_level` = 'W' AND e.state = 6, 7, e.`state`) state, p.knownledges, p.round, p.step, p.creator_id, p.created_time
        FROM mxjb_paper_test p
        JOIN `mxjb_paper_examinee` e ON e.`paper_id` = p.`paper_id` AND e.`customer_code` = #{customerCode}
        <where> p.`creator_id` = #{creatorId}
            <if test="knownledges != null  and knownledges != ''"> and p.`knownledges` = #{knownledges}</if>
            <if test="subjectid != null "> and p.`subjectId` = #{subjectid}</if>
         </where>
        order by round desc
    </select>

    <select id="selectTopMxjbPaperTest" parameterType="MxjbPaperTest" resultMap="MxjbPaperTestResult">
        <include refid="selectMxjbPaperTestVo"/>
        <where>
            <if test="subjectid != null "> and subjectId = #{subjectid}</if>
            <if test="gradeId != null "> and grade_id = #{gradeId}</if>
            <if test="termId != null "> and term_id = #{termId}</if>
            <if test="name != null  and name != ''"> and name like concat('%', #{name}, '%')</if>
            <if test="state != null "> and state = #{state}</if>
            <if test="knownledges != null  and knownledges != ''"> and knownledges = #{knownledges}</if>
            <if test="round != null "> and round = #{round}</if>
            <if test="step != null "> and step = #{step}</if>
            <if test="creatorId != null "> and creator_id = #{creatorId}</if>
            <if test="createdTime != null "> and created_time = #{createdTime}</if>
        </where>
        order by created_time desc
        limit 1
    </select>

    <select id="selectMxjbPaperTestById" parameterType="Long" resultMap="MxjbPaperTestResult">
        <include refid="selectMxjbPaperTestVo"/>
        where paper_id = #{paperId}
    </select>
        
    <insert id="insertMxjbPaperTest" parameterType="MxjbPaperTest" useGeneratedKeys="true" keyProperty="paperId">
        insert into mxjb_paper_test
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="paperId != null">paper_id,</if>
            <if test="subjectid != null">subjectId,</if>
            <if test="gradeId != null">grade_id,</if>
            <if test="termId != null">term_id,</if>
            <if test="name != null">name,</if>
            <if test="state != null">state,</if>
            <if test="knownledges != null">knownledges,</if>
            <if test="round != null">round,</if>
            <if test="step != null">step,</if>
            <if test="creatorId != null">creator_id,</if>
            <if test="createdTime != null">created_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="paperId != null">#{paperId},</if>
            <if test="subjectid != null">#{subjectid},</if>
            <if test="gradeId != null">#{gradeId},</if>
            <if test="termId != null">#{termId},</if>
            <if test="name != null">#{name},</if>
            <if test="state != null">#{state},</if>
            <if test="knownledges != null">#{knownledges},</if>
            <if test="round != null">#{round},</if>
            <if test="step != null">#{step},</if>
            <if test="creatorId != null">#{creatorId},</if>
            <if test="createdTime != null">#{createdTime},</if>
         </trim>
    </insert>

    <update id="updateMxjbPaperTest" parameterType="MxjbPaperTest">
        update mxjb_paper_test
        <trim prefix="SET" suffixOverrides=",">
            <if test="subjectid != null">subjectId = #{subjectid},</if>
            <if test="gradeId != null">grade_id = #{gradeId},</if>
            <if test="termId != null">term_id = #{termId},</if>
            <if test="name != null">name = #{name},</if>
            <if test="state != null">state = #{state},</if>
            <if test="knownledges != null">knownledges = #{knownledges},</if>
            <if test="round != null">round = #{round},</if>
            <if test="step != null">step = #{step},</if>
            <if test="creatorId != null">creator_id = #{creatorId},</if>
            <if test="createdTime != null">created_time = #{createdTime},</if>
        </trim>
        where paper_id = #{paperId}
    </update>

    <delete id="deleteMxjbPaperTestById" parameterType="Long">
        delete from mxjb_paper_test where paper_id = #{paperId}
    </delete>

    <delete id="deleteMxjbPaperTestByIds" parameterType="String">
        delete from mxjb_paper_test where paper_id in 
        <foreach item="paperId" collection="array" open="(" separator="," close=")">
            #{paperId}
        </foreach>
    </delete>
</mapper>