<?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="ink.wgink.module.examine.dao.question.IQuestionOptionsDao">

    <resultMap id="questionOptionsDTO" type="ink.wgink.module.examine.pojo.dtos.question.QuestionOptionsDTO">
        <id column="question_options_id" property="questionOptionsId"/>
        <result column="question_id" property="questionId"/>
        <result column="question_option" property="option"/>
        <result column="content" property="content"/>
    </resultMap>

    <!-- 建表 -->
    <update id="createTable">
        CREATE TABLE IF NOT EXISTS `exam_question_options` (
            `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '自增主键',
            `question_options_id` char(36) NOT NULL COMMENT '主键',
            `question_id` char(36) DEFAULT NULL COMMENT '试题',
            `question_option` varchar(2) DEFAULT NULL COMMENT '标识',
            `content` text COMMENT '内容',
            PRIMARY KEY (`id`,`question_options_id`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='试题选项';
    </update>

    <!-- 新增试题选项 -->
    <insert id="save" parameterType="map">
        INSERT INTO exam_question_options(
            question_options_id,
            question_id,
            question_option,
            content
        ) VALUES(
            #{questionOptionsId},
            #{questionId},
            #{option},
            #{content}
        )
    </insert>

    <!-- 删除试题选项（物理） -->
    <delete id="delete" parameterType="map">
        DELETE FROM
            exam_question_options
        WHERE
            question_options_id IN
        <foreach collection="questionOptionsIds" index="index" open="(" separator="," close=")">
            #{questionOptionsIds[${index}]}
        </foreach>
    </delete>

    <!-- 删除选项（通过试题ID） -->
    <delete id="deleteByQuestionId" parameterType="java.lang.String">
        DELETE FROM
            exam_question_options
        WHERE
            question_id = #{_parameter}
    </delete>

    <!-- 修改试题选项 -->
    <update id="update" parameterType="map">
        UPDATE
            exam_question_options
        <set>
            <if test="questionId != null and questionId != ''">
                question_id = #{questionId},
            </if>
            <if test="option != null and option != ''">
                question_option = #{option},
            </if>
            <if test="content != null and content != ''">
                content = #{content},
            </if>
        </set>
        WHERE
            question_options_id = #{questionOptionsId}
    </update>

    <!-- 试题选项详情 -->
    <select id="get" parameterType="map" resultMap="questionOptionsDTO">
        SELECT
            t1.question_id,
            t1.question_option,
            t1.content,
            t1.question_options_id
        FROM
            exam_question_options t1
        <where>
        <if test="questionOptionsId != null and questionOptionsId != ''">
            t1.question_options_id = #{questionOptionsId}
        </if>
        </where>
    </select>

    <!-- 试题选项列表 -->
    <select id="list" parameterType="map" resultMap="questionOptionsDTO">
        SELECT
            t1.question_id,
            t1.question_option,
            t1.content,
            t1.question_options_id
        FROM
            exam_question_options t1
        <where>
        <if test="questionId != null and questionId != ''">
            t1.question_id = #{questionId}
        </if>
        <if test="keywords != null and keywords != ''">
        AND (
            t1.content LIKE CONCAT('%', #{keywords}, '%')
        )
        </if>
        <if test="startTime != null and startTime != ''">
        AND
            LEFT(t1.gmt_create, 10) <![CDATA[ >= ]]> #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
        AND
            LEFT(t1.gmt_create, 10) <![CDATA[ <= ]]> #{endTime}
        </if>
        <if test="questionOptionsIds != null and questionOptionsIds.size > 0">
        AND
            t1.question_options_id IN
            <foreach collection="questionOptionsIds" index="index" open="(" separator="," close=")">
            #{questionOptionsIds[${index}]}
            </foreach>
        </if>
        <if test="questionIds != null and questionIds.size > 0">
        AND
            t1.question_id IN
            <foreach collection="questionIds" index="index" open="(" separator="," close=")">
            #{questionIds[${index}]}
            </foreach>
        </if>
        </where>
    </select>

    <!-- 试题选项统计  -->
    <select id="count" parameterType="map" resultType="Integer">
        SELECT
            COUNT(*)
        FROM
            exam_question_options t1
    </select>

</mapper>