<?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.github.tangyi.exam.mapper.ExamRecordMapper">
    <resultMap id="examRecordResultMap" type="com.github.tangyi.exam.api.module.ExamRecord">
        <id column="id" property="id"/>
        <result column="user_id" property="userId"/>
        <result column="examination_id" property="examinationId"/>
        <result column="examination_name" property="examinationName"/>
        <result column="course_id" property="courseId"/>
        <result column="start_time" property="startTime"/>
        <result column="end_time" property="endTime"/>
        <result column="score" property="score"/>
        <result column="correct_number" property="correctNumber"/>
        <result column="incorrect_number" property="inCorrectNumber"/>
        <result column="submit_status" property="submitStatus"/>
        <result column="creator" property="creator"/>
        <result column="create_date" property="createDate"/>
        <result column="modifier" property="modifier"/>
        <result column="modify_date" property="modifyDate"/>
        <result column="del_flag" property="delFlag"/>
        <result column="application_code" property="applicationCode"/>
        <result column="tenant_code" property="tenantCode"/>
    </resultMap>

    <sql id="examRecordColumns">
        a.id,
        a.user_id,
        a.examination_id,
        a.examination_name,
        a.course_id,
        a.start_time,
        a.end_time,
        a.score,
        a.correct_number,
        a.incorrect_number,
        a.submit_status,
        a.creator,
        a.create_date,
        a.modifier,
        a.modify_date,
        a.del_flag,
        a.application_code,
        a.tenant_code
    </sql>

    <!-- where 条件 -->
    <sql id="whereColumnList">
        <if test="userId != null and userId != ''">
            and a.user_id = #{userId}
        </if>
        <if test="examinationId != null and examinationId != ''">
            and a.examination_id = #{examinationId}
        </if>
        <if test="examinationName != null and examinationName != ''">
            and a.examination_name like CONCAT('%',#{examinationName},'%')
        </if>
        <if test="courseId != null and courseId != ''">
            and a.course_id = #{courseId}
        </if>
        <if test="submitStatus != null and submitStatus != ''">
            and a.submit_status = #{submitStatus}
        </if>
        <if test="tenantCode != null and tenantCode != ''">
            and a.tenant_code = #{tenantCode}
        </if>
    </sql>

    <select id="get" resultMap="examRecordResultMap">
        SELECT
        <include refid="examRecordColumns"/>
        FROM exam_record a
        WHERE a.id = #{id} and a.del_flag = 0
    </select>

    <select id="getByUserIdAndExaminationId" resultMap="examRecordResultMap">
        SELECT
        <include refid="examRecordColumns"/>
        FROM exam_record a
        WHERE a.user_id = #{userId} and examination_id = #{examinationId} and a.del_flag = 0
    </select>

    <select id="findList" resultMap="examRecordResultMap">
        SELECT
        <include refid="examRecordColumns"/>
        FROM exam_record a
        WHERE a.del_flag = 0
        <include refid="whereColumnList"/>
    </select>

    <select id="findListById" resultMap="examRecordResultMap">
        SELECT
        <include refid="examRecordColumns"/>
        FROM exam_record a
        WHERE a.id IN
        <foreach collection="ids" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>

    <insert id="insert">
        insert  into exam_record (
            id,
            user_id,
            examination_id,
            examination_name,
            course_id,
            start_time,
            end_time,
            score,
            correct_number,
            incorrect_number,
            submit_status,
            creator,
            create_date,
            modifier,
            modify_date,
            del_flag,
            application_code,
            tenant_code
        ) values (
            #{id},
            #{userId},
            #{examinationId},
            #{examinationName},
            #{courseId},
            #{startTime},
            #{endTime},
            #{score},
            #{correctNumber},
            #{inCorrectNumber},
            #{submitStatus},
            #{creator},
            #{createDate},
            #{modifier},
            #{modifyDate},
            #{delFlag},
            #{applicationCode},
            #{tenantCode}
        );
    </insert>

    <update id="update">
        UPDATE exam_record  SET
        <if test="userId != null">
            user_id = #{userId},
        </if>
        <if test="examinationId != null">
            examination_id = #{examinationId},
        </if>
        <if test="examinationName != null">
            examination_name = #{examinationName},
        </if>
        <if test="courseId != null">
            course_id = #{courseId},
        </if>
        <if test="startTime != null">
            start_time = #{startTime},
        </if>
        <if test="endTime != null">
            end_time = #{endTime},
        </if>
        <if test="score != null">
            score = #{score},
        </if>
        <if test="correctNumber != null">
            correct_number = #{correctNumber},
        </if>
        <if test="inCorrectNumber != null">
            incorrect_number = #{inCorrectNumber},
        </if>
        <if test="submitStatus != null">
            submit_status = #{submitStatus},
        </if>
        <if test="delFlag != null">
            del_flag = #{delFlag},
        </if>
        modifier = #{modifier},
        modify_date = #{modifyDate}
        WHERE id = #{id}
    </update>

    <delete id="delete">
        UPDATE exam_record SET
			del_flag = 1
		WHERE id = #{id}
    </delete>
</mapper>
