<?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.AnswerMapper">
    <resultMap id="answerResultMap" type="com.github.tangyi.exam.api.module.Answer">
        <id column="id" property="id"/>
        <result column="user_id" property="userId"/>
        <result column="examination_id" property="examinationId"/>
        <result column="exam_record_id" property="examRecordId"/>
        <result column="course_id" property="courseId"/>
        <result column="subject_id" property="subjectId"/>
        <result column="answer" property="answer"/>
        <result column="option_answer" property="optionAnswer"/>
        <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="answerColumns">
        a.id,
        a.user_id,
        a.examination_id,
        a.exam_record_id,
        a.course_id,
        a.subject_id,
        a.answer,
        a.option_answer,
        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="examRecordId != null and examRecordId != ''">
            and a.exam_record_id = #{examRecordId}
        </if>
        <if test="courseId != null and courseId != ''">
            and a.course_id = #{courseId}
        </if>
        <if test="subjectId != null and subjectId != ''">
            and a.subject_id = #{subjectId}
        </if>
        <if test="tenantCode != null and tenantCode != ''">
            and a.tenant_code = #{tenantCode}
        </if>
    </sql>

    <select id="get" resultMap="answerResultMap">
        SELECT
        <include refid="answerColumns"/>
        FROM answer a
        WHERE a.id = #{id} and a.del_flag = 0
    </select>

    <select id="getAnswer" resultMap="answerResultMap">
        SELECT
        <include refid="answerColumns"/>
        FROM answer a
        WHERE a.user_id = #{userId}
        and a.examination_id = #{examinationId}
        and a.exam_record_id = #{examRecordId}
        and a.subject_id = #{subjectId}
</select>

    <select id="findList" resultMap="answerResultMap">
        SELECT
        <include refid="answerColumns"/>
        FROM answer a
        WHERE a.del_flag = 0
        <include refid="whereColumnList"/>
    </select>

    <insert id="insert">
        insert  into answer (
            id,
            user_id,
            examination_id,
            exam_record_id,
            course_id,
            subject_id,
            answer,
            option_answer,
            creator,
            create_date,
            modifier,
            modify_date,
            del_flag,
            application_code,
            tenant_code,
        ) values (
            #{id},
            #{userId},
            #{examinationId},
            #{examRecordId},
            #{courseId},
            #{subjectId},
            #{answer},
            #{optionAnswer},
            #{creator},
            #{createDate},
            #{modifier},
            #{modifyDate},
            #{delFlag},
            #{applicationCode},
            #{tenantCode}
        );
    </insert>

    <update id="update">
        UPDATE answer  SET
        <if test="userId != null">
            user_id = #{userId},
        </if>
        <if test="examinationId != null">
            examination_id = #{examinationId},
        </if>
        <if test="examRecordId != null">
            exam_record_id = #{examRecordId},
        </if>
        <if test="courseId != null">
            course_id = #{courseId},
        </if>
        <if test="subjectId != null">
            subject_id = #{subjectId},
        </if>
        <if test="answer != null">
            answer = #{answer},
        </if>
        <if test="optionAnswer != null">
            option_answer = #{optionAnswer},
        </if>
        <if test="delFlag != null">
            del_flag = #{delFlag},
        </if>
        modifier = #{modifier},
        modify_date = #{modifyDate}
        WHERE id = #{id}
    </update>

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