<?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.SubjectBankMapper">
    <resultMap id="subjectBankResultMap" type="com.github.tangyi.exam.api.module.SubjectBank">
        <id column="id" property="id"/>
        <result column="category_id" property="categoryId"/>
        <result column="serial_number" property="serialNumber"/>
        <result column="subject_name" property="subjectName"/>
        <result column="type" property="type"/>
        <result column="content" property="content"/>
        <result column="option_a" property="optionA"/>
        <result column="option_b" property="optionB"/>
        <result column="option_c" property="optionC"/>
        <result column="option_d" property="optionD"/>
        <result column="option_e" property="optionE"/>
        <result column="option_f" property="optionF"/>
        <result column="answer" property="answer"/>
        <result column="score" property="score"/>
        <result column="analysis" property="analysis"/>
        <result column="level" property="level"/>
        <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="subjectBankColumns">
        a.id,
        a.category_id,
        a.serial_number,
        a.subject_name,
        a.type,
        a.content,
        a.option_a,
        a.option_b,
        a.option_c,
        a.option_d,
        a.option_e,
        a.option_f,
        a.answer,
        a.score,
        a.analysis,
        a.level,
        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="categoryId != null and categoryId != ''">
            and f.category_id = #{categoryId}
        </if>
        <if test="serialNumber != null and serialNumber != ''">
            and f.serial_number = #{serialNumber}
        </if>
        <if test="subjectName != null and subjectName != ''">
            and f.subject_name like CONCAT('%',#{subjectName},'%')
        </if>
        <if test="tenantCode != null and tenantCode != ''">
            and a.tenant_code = #{tenantCode}
        </if>
    </sql>

    <select id="get" resultMap="subjectBankResultMap">
        SELECT
        <include refid="subjectBankColumns"/>
        FROM subject_bank a
        WHERE a.id = #{id} and a.del_flag = 0
    </select>

    <select id="findList" resultMap="subjectBankResultMap">
        SELECT
        a.*
        FROM (
        SELECT f.id, f.category_id, f.serial_number, f.subject_name, f.type, f.content, f.option_a, f.option_b, f.option_c, f.option_d, f.option_e, f.option_f,
        f.answer, f.score, f.analysis, f.level, f.creator, f.create_date, f.modifier, f.modify_date, f.del_flag, f.application_code, c.category_name
        FROM subject_bank f LEFT JOIN subject_category c ON c.del_flag = 0 AND c.id = f.category_id
        WHERE f.del_flag = 0
        <include refid="whereColumnList"/>
        ) a
    </select>

    <insert id="insert">
        insert into subject_bank (
            id,
            category_id,
            serial_number,
            subject_name,
            type,
            content,
            option_a,
            option_b,
            option_c,
            option_d,
            option_e,
            option_f,
            answer,
            score,
            analysis,
            level,
            creator,
            create_date,
            modifier,
            modify_date,
            del_flag,
            application_code,
            tenant_code
        ) values (
            #{id},
            #{categoryId},
            #{serialNumber},
            #{subjectName},
            #{type},
            #{content},
            #{optionA},
            #{optionB},
            #{optionC},
            #{optionD},
            #{optionE},
            #{optionF},
            #{answer},
            #{score},
            #{analysis},
            #{level},
            #{creator},
            #{createDate},
            #{modifier},
            #{modifyDate},
            #{delFlag},
            #{applicationCode},
            #{tenantCode}
        );
    </insert>

    <update id="update">
        UPDATE subject_bank  SET
        <if test="categoryId != null">
            category_id = #{categoryId},
        </if>
        <if test="serialNumber != null">
            serial_number = #{serialNumber},
        </if>
        <if test="subjectName != null">
            subject_name = #{subjectName},
        </if>
        <if test="type != null">
            type = #{type},
        </if>
        <if test="content != null">
            content = #{content},
        </if>
        <if test="optionA != null">
            option_a = #{optionA},
        </if>
        <if test="optionB != null">
            option_b = #{optionB},
        </if>
        <if test="optionC != null">
            option_c = #{optionC},
        </if>
        <if test="optionD != null">
            option_d = #{optionD},
        </if>
        <if test="optionE != null">
            option_e = #{optionE},
        </if>
        <if test="optionF != null">
            option_f = #{optionF},
        </if>
        <if test="answer != null">
            answer = #{answer},
        </if>
        <if test="score != null">
            score = #{score},
        </if>
        <if test="analysis != null">
            analysis = #{analysis},
        </if>
        <if test="level != null">
            level = #{level},
        </if>
        <if test="delFlag != null">
            del_flag = #{delFlag},
        </if>
        modifier = #{modifier},
        modify_date = #{modifyDate}
        WHERE id = #{id}
    </update>

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

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