<?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.IQuestionTypeQuestionDao">

    <resultMap id="questionTypeQuestionPO" type="ink.wgink.module.examine.pojo.pos.question.QuestionTypeQuestionPO">
        <id column="id" property="id"/>
        <result column="question_type_id" property="questionTypeId"/>
        <result column="question_id" property="questionId"/>
    </resultMap>

    <resultMap id="questionTypeQuestionDTO" type="ink.wgink.module.examine.pojo.dtos.question.QuestionTypeQuestionDTO">
        <result column="question_id" property="questionId"/>
        <result column="question_type_id" property="questionTypeId"/>
        <result column="question_parent_id" property="questionTypeParentId"/>
        <result column="type_name" property="typeName"/>
        <result column="type_summary" property="typeSummary"/>
    </resultMap>

    <!-- 建表 -->
    <update id="createTable">
        CREATE TABLE IF NOT EXISTS `exam_question_type_question` (
            `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
            `question_type_id` char(36) DEFAULT NULL,
            `question_id` char(36) DEFAULT NULL,
            PRIMARY KEY (`id`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='试题类型与试题绑定';
    </update>

    <!-- 新增 -->
    <insert id="save" parameterType="map">
        INSERT INTO exam_question_type_question(
            question_type_id,
            question_id
        ) VALUES(
            #{questionTypeId},
            #{questionId}
        )
    </insert>


    <!-- 删除（物理） -->
    <update id="delete" parameterType="map">
        DELETE FROM
            exam_question_type_question
        WHERE
        <if test="questionIds != null and questionIds != ''">
            question_id IN
            <foreach collection="questionIds" index="index" open="(" separator="," close=")">
                #{questionIds[${index}]}
            </foreach>
        </if>
        <if test="questionTypeIds != null and questionTypeIds != ''">
            question_type_id IN
            <foreach collection="questionTypeIds" index="index" open="(" separator="," close=")">
                #{questionTypeIds[${index}]}
            </foreach>
        </if>
    </update>

    <!-- 试题类型ID列表 -->
    <select id="listQuestionTypeId" parameterType="map" resultType="java.lang.String">
        SELECT
            question_type_id
        FROM
            exam_question_type_question
        <where>
            <if test="questionId != null and questionId != ''">
            question_id = #{questionId}
            </if>
            <if test="questionIds != null and questionIds.size > 0">
        AND
            question_id IN
            <foreach collection="questionIds" index="index" open="(" separator="," close=")">
                #{questionIds[${index}]}
            </foreach>
            </if>
        </where>
    </select>

    <!-- 试题类型列表 -->
    <select id="list" parameterType="map" resultMap="questionTypeQuestionDTO">
        SELECT
            t1.question_type_id,
            t1.question_type_parent_id,
            t1.type_name,
            t1.type_summary,
            jt1.question_id
        FROM
            exam_question_type t1
        INNER JOIN
            exam_question_type_question jt1
        ON
            t1.question_type_id = jt1.question_type_id
        WHERE
            t1.is_delete = 0
        AND
            t1.question_type_id IN (
                SELECT
                    question_type_id
                FROM
                    exam_question_type_question
                <where>
                <if test="questionId != null and questionId != ''">
                    question_id = #{questionId}
                </if>
                <if test="questionIds != null and questionIds != ''">
                AND
                    question_id IN
                    <foreach collection="questionIds" index="index" open="(" separator="," close=")">
                        #{questionIds[${index}]}
                    </foreach>
                </if>
                </where>
            )
    </select>

</mapper>