<?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.paper.question.IPaperQuestionDao">

    <resultMap id="paperQuestionDTO" type="ink.wgink.module.examine.pojo.dtos.paper.question.PaperQuestionDTO">
        <id column="paper_question_id" property="paperQuestionId"/>
        <result column="paper_id" property="paperId"/>
        <result column="question_id" property="questionId"/>
        <result column="question_sort" property="questionSort"/>
        <result column="question_subject" property="questionSubject"/>
        <result column="question_type" property="questionType"/>
        <result column="question_choice_type" property="questionChoiceType"/>
        <result column="question_difficulty" property="questionDifficulty"/>
        <result column="question_source" property="questionSource"/>
    </resultMap>

    <resultMap id="paperQuestionPO" type="ink.wgink.module.examine.pojo.pos.paper.PaperQuestionPO">
        <id column="paper_question_id" property="paperQuestionId"/>
        <result column="paper_id" property="paperId"/>
        <result column="question_id" property="questionId"/>
        <result column="question_sort" property="questionId"/>
    </resultMap>

    <!-- 建表 -->
    <update id="createTable">
        CREATE TABLE IF NOT EXISTS `exam_paper_question` (
            `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '自增主键',
            `paper_question_id` char(36) NOT NULL COMMENT '主键',
            `paper_id` char(36) DEFAULT NULL COMMENT '试卷ID',
            `question_id` varchar(255) DEFAULT NULL COMMENT '试题ID',
            `question_sort` varchar(255) DEFAULT 'ZZZ-000' COMMENT '试题排序',
            PRIMARY KEY (`id`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='试卷试题';
    </update>

    <!-- 新增试卷 -->
    <insert id="save" parameterType="map">
        INSERT INTO exam_paper_question(
            paper_question_id,
            paper_id,
            question_id,
            question_sort
        ) VALUES(
            #{paperQuestionId},
            #{paperId},
            #{questionId},
            #{questionSort}
        )
    </insert>

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

    <!-- 修改试题排序 -->
    <update id="updateQuestionSort" parameterType="map">
        UPDATE
            exam_paper_question
        SET
            question_sort = #{questionSort}
        WHERE
            paper_question_id = #{paperQuestionId}
    </update>

    <!-- 试卷试题列表 -->
    <select id="listPO" parameterType="map" resultMap="paperQuestionPO">
        SELECT
            paper_question_id,
            paper_id,
            question_id,
            question_sort
        FROM
            exam_paper_question
        <where>
            <if test="paperId != null and paperId != ''">
                paper_id = #{paperId}
            </if>
            <if test="paperIds != null and paperIds.size > 0">
            AND
                paper_id IN
                <foreach collection="paperIds" index="index" open="(" separator="," close=")">
                    #{paperIds[${index}]}
                </foreach>
            </if>
        </where>
    </select>

    <!-- 试卷试题列表 -->
    <select id="list" parameterType="map" resultMap="paperQuestionDTO">
        SELECT
            t1.paper_question_id,
            t1.paper_id,
            t1.question_id,
            t1.question_sort,
            jt1.subject question_subject,
            jt1.type question_type,
            jt1.choice_type question_choice_type,
            jt1.difficulty question_difficulty,
            jt1.source question_source
        FROM
            exam_paper_question t1
        INNER JOIN
            exam_question jt1
        ON
            t1.question_id = jt1.question_id
        WHERE
            jt1.is_delete = 0
        <if test="paperId != null and paperId != ''">
        AND
            t1.paper_id = #{paperId}
        </if>
        <if test="paperIds != null and paperIds.size > 0">
        AND
            t1.paper_id IN
            <foreach collection="paperIds" index="index" open="(" separator="," close=")">
                #{paperIds[${index}]}
            </foreach>
        </if>
        <if test="keywords != null and keywords != ''">
        AND
            jt1.subject LIKE CONCAT('%', #{keywords}, '%')
        </if>
        <if test="questionType != null and questionType != ''">
        AND
            jt1.type = #{questionType}
        </if>
        <if test="questionChoiceType != null and questionChoiceType != ''">
        AND
            jt1.choice_type = #{questionChoiceType}
        </if>
        <if test="sort != null and (sort == 'questionSort')">
        ORDER BY
            <if test="sort == 'questionSort'">
            t1.question_sort ${order}
            </if>
        </if>
    </select>

    <!-- 试题管理列表 -->
    <select id="listQuestion" parameterType="map" resultMap="ink.wgink.module.examine.dao.question.IQuestionDao.questionDTO">
        SELECT
            t1.subject,
            t1.type,
            t1.choice_type,
            t1.parent_id,
            t1.difficulty,
            t1.source,
            LEFT(t1.gmt_create, 19) gmt_create,
            t1.question_id
        FROM
            exam_question t1
        WHERE
            t1.is_delete = 0
        <if test="excludePaperId != null and excludePaperId != ''">
        AND
            t1.question_id NOT IN (
                SELECT
                    st1.question_id
                FROM
                    exam_paper_question st1
                WHERE
                    paper_id = #{excludePaperId}
            )
        </if>
        <if test="keywords != null and keywords != ''">
        AND (
            t1.subject 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="type != null and type != ''">
        AND
            t1.type = #{type}
        </if>
        <if test="choiceType != null and choiceType != ''">
        AND
            t1.choice_type = #{choiceType}
        </if>
        <if test="questionType != null and questionType != ''">
        AND
            t1.question_type = #{questionType}
        </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>
        <if test="creator != null and creator != ''">
            AND
            t1.creator = #{creator}
        </if>
        <if test="creators != null and creators.size > 0">
            AND
            t1.creator IN
            <foreach collection="creators" index="index" open="(" separator="," close=")">
                #{creators[${index}]}
            </foreach>
        </if>
        <if test="difficulty != null">
            AND
            t1.difficulty = #{difficulty}
        </if>
        <if test="questionTypeIds != null and questionTypeIds.size > 0">
        AND
            t1.question_id IN (
            SELECT
                st1.question_id
            FROM
                exam_question_type_question st1
            WHERE
                st1.question_type_id IN
            <foreach collection="questionTypeIds" index="index" open="(" separator="," close=")">
                #{questionTypeIds[${index}]}
            </foreach>
            )
        </if>
    </select>

    <!-- 试题ID列表 -->
    <select id="listId" parameterType="map" resultType="java.lang.String">
        SELECT
            t1.paper_question_id
        FROM
            exam_paper_question t1
        <if test="questionType != null and questionType != ''">
        INNER JOIN
            exam_question jt1
        ON
            t1.question_id = jt1.question_id
        </if>
        <where>
            <if test="paperId != null and paperId != ''">
                t1.paper_id = #{paperId}
            </if>
            <if test="questionType != null and questionType != ''">
            AND
                jt1.type = #{questionType}
                <if test="questionChoiceType != null and questionChoiceType != ''">
            AND
                jt1.choice_type = #{questionChoiceType}
                </if>
            </if>
        </where>
    </select>

</mapper>