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

    <resultMap id="questionDTO" type="ink.wgink.module.examine.pojo.dtos.question.QuestionDTO">
        <id column="question_id" property="questionId"/>
        <result column="subject" property="subject"/>
        <result column="custom_question_type" property="customQuestionType"/>
        <result column="type" property="type"/>
        <result column="choice_type" property="choiceType"/>
        <result column="analysis" property="analysis"/>
        <result column="parent_id" property="parentId"/>
        <result column="difficulty" property="difficulty"/>
        <result column="source" property="source"/>
        <result column="answer" property="answer"/>
        <result column="gmt_create" property="gmtCreate"/>
    </resultMap>

    <resultMap id="questionPO" type="ink.wgink.module.examine.pojo.pos.question.QuestionPO">
        <id column="question_id" property="questionId"/>
        <result column="subject" property="subject"/>
        <result column="custom_question_type" property="customQuestionType"/>
        <result column="type" property="type"/>
        <result column="choice_type" property="choiceType"/>
        <result column="analysis" property="analysis"/>
        <result column="parent_id" property="parentId"/>
        <result column="difficulty" property="difficulty"/>
        <result column="source" property="source"/>
        <result column="answer" property="answer"/>
        <result column="gmt_create" property="gmtCreate"/>
    </resultMap>

    <!-- 建表 -->
    <update id="createTable">
        CREATE TABLE IF NOT EXISTS `exam_question` (
            `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '自增主键',
            `question_id` char(36) NOT NULL COMMENT '主键',
            `subject` longtext COMMENT '题目',
            `type` varchar(255) DEFAULT NULL COMMENT '种类',
            `choice_type` varchar(255) DEFAULT NULL COMMENT '选择类别',
            `analysis` longtext COMMENT '解析',
            `parent_id` varchar(255) DEFAULT NULL COMMENT '上级试题',
            `difficulty` int(11) DEFAULT NULL COMMENT '难度',
            `source` varchar(255) DEFAULT NULL COMMENT '来源',
            `answer` varchar(255) DEFAULT NULL COMMENT '答案',
            `creator` char(36) DEFAULT NULL,
            `gmt_create` datetime DEFAULT NULL,
            `modifier` char(36) DEFAULT NULL,
            `gmt_modified` datetime DEFAULT NULL,
            `is_delete` int(1) DEFAULT '0',
            PRIMARY KEY (`id`,`question_id`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='试题';
    </update>

    <!-- 新增试题管理 -->
    <insert id="save" parameterType="map">
        INSERT INTO exam_question(
            question_id,
            subject,
            type,
            choice_type,
            analysis,
            parent_id,
            difficulty,
            source,
            answer,
            creator,
            gmt_create,
            modifier,
            gmt_modified,
            is_delete
        ) VALUES(
            #{questionId},
            #{subject},
            #{type},
            #{choiceType},
            #{analysis},
            #{parentId},
            #{difficulty},
            #{source},
            #{answer},
            #{creator},
            #{gmtCreate},
            #{modifier},
            #{gmtModified},
            #{isDelete}
        )
    </insert>

    <!-- 删除试题管理 -->
    <update id="remove" parameterType="map">
        UPDATE
            exam_question
        SET
            is_delete = 1,
            modifier = #{modifier},
            gmt_modified = #{gmtModified}
        WHERE
            question_id IN
        <foreach collection="questionIds" index="index" open="(" separator="," close=")">
            #{questionIds[${index}]}
        </foreach>
    </update>

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

    <!-- 修改试题管理 -->
    <update id="update" parameterType="map">
        UPDATE
            exam_question
        SET
        <if test="subject != null and subject != ''">
            subject = #{subject},
        </if>
        <if test="type != null and type != ''">
            type = #{type},
        </if>
        <if test="choiceType != null and choiceType != ''">
            choice_type = #{choiceType},
        </if>
        <if test="analysis != null and analysis != ''">
            analysis = #{analysis},
        </if>
        <if test="parentId != null and parentId != ''">
            parent_id = #{parentId},
        </if>
        <if test="difficulty != null">
            difficulty = #{difficulty},
        </if>
        <if test="source != null and source != ''">
            source = #{source},
        </if>
        <if test="answer != null and answer != ''">
            answer = #{answer},
        </if>
            modifier = #{modifier},
            gmt_modified = #{gmtModified}
        WHERE
            question_id = #{questionId}
    </update>

    <!-- 试题管理详情 -->
    <select id="get" parameterType="map" resultMap="questionDTO">
        SELECT
            t1.subject,
            t1.type,
            t1.choice_type,
            t1.analysis,
            t1.parent_id,
            t1.difficulty,
            t1.source,
            t1.answer,
            t1.question_id
        FROM
            exam_question t1
        WHERE
            t1.is_delete = 0
        <if test="questionId != null and questionId != ''">
        AND
            t1.question_id = #{questionId}
        </if>
    </select>

    <!-- 试题管理详情 -->
    <select id="getPO" parameterType="map" resultMap="questionPO">
        SELECT
            t1.subject,
            t1.type,
            t1.choice_type,
            t1.analysis,
            t1.parent_id,
            t1.difficulty,
            t1.source,
            t1.answer,
            t1.question_id
        FROM
            exam_question t1
        WHERE
            t1.is_delete = 0
        <if test="questionId != null and questionId != ''">
        AND
            t1.question_id = #{questionId}
        </if>
        <if test="subject != null and subject != ''">
        AND
            t1.subject = #{subject}
        </if>
    </select>

    <!-- 试题管理列表 -->
    <select id="list" parameterType="map" resultMap="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="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="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="withoutQuestionIds != null and withoutQuestionIds.size > 0">
        AND
            t1.question_id NOT IN
            <foreach collection="withoutQuestionIds" index="index" open="(" separator="," close=")">
                #{withoutQuestionIds[${index}]}
            </foreach>
        </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>

    <!-- 试题管理统计  -->
    <select id="count" parameterType="map" resultType="Integer">
        SELECT
            COUNT(*)
        FROM
            exam_question t1
        WHERE
            t1.is_delete = 0
        <if test="creator != null and creator != ''">
        AND
            t1.creator = #{creator}
        </if>
    </select>

    <!-- 试题ID列表 -->
    <select id="listIds" parameterType="map" resultType="java.lang.String">
        SELECT
            question_id
        FROM
            exam_question
        WHERE
            is_delete = 0
        <if test="type != null and type != ''">
        AND
            type = #{type}
        </if>
        <if test="choiceType != null and choiceType != ''">
        AND
            choice_type = #{choiceType}
        </if>
    </select>

    <!-- 试题列表 -->
    <select id="listPO" parameterType="map" resultMap="questionPO">
        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="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="withoutQuestionIds != null and withoutQuestionIds.size > 0">
        AND
            t1.question_id NOT IN
            <foreach collection="withoutQuestionIds" index="index" open="(" separator="," close=")">
                #{withoutQuestionIds[${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>
        <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>

</mapper>