<?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.ExaminationMapper">
    <resultMap id="examinationResultMap" type="com.github.tangyi.exam.api.module.Examination">
        <id column="id" property="id"/>
        <result column="examination_name" property="examinationName"/>
        <result column="type" property="type"/>
        <result column="attention" property="attention"/>
        <result column="start_time" property="startTime"/>
        <result column="end_time" property="endTime"/>
        <result column="duration" property="duration"/>
        <result column="total_score" property="totalScore"/>
        <result column="total_subject" property="totalSubject"/>
        <result column="status" property="status"/>
        <result column="avatar" property="avatar"/>
        <result column="avatar_id" property="avatarId"/>
        <result column="college_id" property="collegeId"/>
        <result column="major_id" property="majorId"/>
        <result column="course_id" property="courseId"/>
        <result column="remark" property="remark"/>
        <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="examinationColumns">
        a.id,
        a.examination_name,
        a.type,
        a.attention,
        a.start_time,
        a.end_time,
        a.duration,
        a.total_score,
        a.total_subject,
        a.status,
        a.avatar,
        a.avatar_id,
        a.college_id,
        a.major_id,
        a.course_id,
        a.remark,
        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="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="status != null and status != ''">
            and a.status = #{status}
        </if>
        <if test="type != null and type != ''">
            and a.type = #{type}
        </if>
        <if test="tenantCode != null and tenantCode != ''">
            and a.tenant_code = #{tenantCode}
        </if>
    </sql>

    <select id="get" resultMap="examinationResultMap">
        SELECT
        <include refid="examinationColumns"/>
        FROM examination a
        WHERE a.id = #{id} and a.del_flag = 0
    </select>

    <select id="findList" resultMap="examinationResultMap">
        SELECT
        <include refid="examinationColumns"/>
        FROM examination a
        WHERE a.del_flag = 0
        <include refid="whereColumnList"/>
    </select>

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

    <select id="findExaminationCount" resultType="java.lang.Integer">
        SELECT COUNT(*) FROM examination a
        WHERE a.del_flag = 0
        <include refid="whereColumnList"/>
    </select>

    <insert id="insert">
        insert  into examination (
            id,
            examination_name,
            type,
            attention,
            start_time,
            end_time,
            duration,
            total_score,
            total_subject,
            status,
            avatar,
            avatar_id,
            college_id,
            major_id,
            course_id,
            remark,
            creator,
            create_date,
            modifier,
            modify_date,
            del_flag,
            application_code,
            tenant_code
        ) values (
            #{id},
            #{examinationName},
            #{type},
            #{attention},
            #{startTime},
            #{endTime},
            #{duration},
            #{totalScore},
            #{totalSubject},
            #{status},
            #{avatar},
            #{avatarId},
            #{collegeId},
            #{majorId},
            #{courseId},
            #{remark},
            #{creator},
            #{createDate},
            #{modifier},
            #{modifyDate},
            #{delFlag},
            #{applicationCode},
            #{tenantCode}
        );
    </insert>

    <update id="update">
        UPDATE examination  SET
        <if test="examinationName != null">
            examination_name = #{examinationName},
        </if>
        <if test="type != null">
            type = #{type},
        </if>
        <if test="attention != null">
            attention = #{attention},
        </if>
        <if test="startTime != null">
            start_time = #{startTime},
        </if>
        <if test="endTime != null">
            end_time = #{endTime},
        </if>
        <if test="duration != null">
            duration = #{duration},
        </if>
        <if test="totalScore != null">
            total_score = #{totalScore},
        </if>
        <if test="totalSubject != null">
            total_subject = #{totalSubject},
        </if>
        <if test="status != null">
            status = #{status},
        </if>
        <if test="avatar != null">
            avatar = #{avatar},
        </if>
        <if test="avatarId != null">
            avatar_id = #{avatarId},
        </if>
        <if test="collegeId != null">
            college_id = #{collegeId},
        </if>
        <if test="majorId != null">
            major_id = #{majorId},
        </if>
        <if test="courseId != null">
            course_id = #{courseId},
        </if>
        <if test="remark != null">
            remark = #{remark},
        </if>
        <if test="delFlag != null">
            del_flag = #{delFlag},
        </if>
        modifier = #{modifier},
        modify_date = #{modifyDate}
        WHERE id = #{id}
    </update>

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

    <delete id="deleteAll">
        UPDATE examination SET
        del_flag = 1
        WHERE id in
        <foreach item="item" index="index" collection="array" open="("
                 separator="," close=")"> #{item}
        </foreach>
    </delete>
</mapper>
