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

    <resultMap id="questionTypeDTO" type="ink.wgink.module.examine.pojo.dtos.question.QuestionTypeDTO">
        <result column="question_type_id" property="questionTypeId"/>
        <result column="question_type_parent_id" property="questionTypeParentId"/>
        <result column="question_type_parent_name" property="questionTypeParentName"/>
        <result column="type_name" property="typeName"/>
        <result column="type_summary" property="typeSummary"/>
        <result column="creator" property="creator"/>
        <result column="gmt_create" property="gmtCreate"/>
        <result column="modifier" property="modifier"/>
        <result column="gmt_modified" property="gmtModified"/>
        <result column="is_delete" property="isDelete"/>
    </resultMap>

    <resultMap id="questionTypeBO" type="ink.wgink.module.examine.pojo.bos.question.QuestionTypeBO">
        <result column="question_type_id" property="questionTypeId"/>
        <result column="question_type_parent_id" property="questionTypeParentId"/>
        <result column="type_name" property="typeName"/>
        <result column="type_summary" property="typeSummary"/>
        <result column="creator" property="creator"/>
        <result column="gmt_create" property="gmtCreate"/>
        <result column="modifier" property="modifier"/>
        <result column="gmt_modified" property="gmtModified"/>
        <result column="is_delete" property="isDelete"/>
    </resultMap>

    <resultMap id="questionTypePO" type="ink.wgink.module.examine.pojo.pos.question.QuestionTypePO">
        <result column="question_type_id" property="questionTypeId"/>
        <result column="question_type_parent_id" property="questionTypeParentId"/>
        <result column="type_name" property="typeName"/>
        <result column="type_summary" property="typeSummary"/>
        <result column="creator" property="creator"/>
        <result column="gmt_create" property="gmtCreate"/>
        <result column="modifier" property="modifier"/>
        <result column="gmt_modified" property="gmtModified"/>
        <result column="is_delete" property="isDelete"/>
    </resultMap>

    <resultMap id="zTreeDTO" type="ink.wgink.pojo.dtos.ZTreeDTO">
        <id property="id" column="question_type_id"/>
        <result property="pId" column="question_type_parent_id"/>
        <result property="name" column="type_name"/>
    </resultMap>

    <!-- 建表 -->
    <update id="createTable">
        CREATE TABLE IF NOT EXISTS `exam_question_type` (
            `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
            `question_type_id` char(36) CHARACTER SET utf8mb4 DEFAULT NULL COMMENT '主键',
            `question_type_parent_id` char(36) CHARACTER SET utf8mb4 DEFAULT '0' COMMENT '上级ID',
            `type_name` varchar(255) CHARACTER SET utf8mb4 DEFAULT NULL COMMENT '类型名称',
            `type_summary` varchar(255) CHARACTER SET utf8mb4 DEFAULT NULL COMMENT '说明',
            `creator` char(36) CHARACTER SET utf8mb4 DEFAULT NULL COMMENT '创建人',
            `gmt_create` datetime DEFAULT NULL COMMENT '创建时间',
            `modifier` char(36) CHARACTER SET utf8mb4 DEFAULT NULL COMMENT '修改人',
            `gmt_modified` datetime DEFAULT NULL COMMENT '修改时间',
            `is_delete` int(1) DEFAULT '0' COMMENT '是否删除',
            PRIMARY KEY (`id`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='试题类型';
    </update>

    <!-- 新增试题类型 -->
    <insert id="save" parameterType="map">
        INSERT INTO exam_question_type(
            question_type_id,
            question_type_parent_id,
            type_name,
            type_summary,
            creator,
            gmt_create,
            modifier,
            gmt_modified,
            is_delete
        ) VALUES(
            #{questionTypeId},
            #{questionTypeParentId},
            #{typeName},
            #{typeSummary},
            #{creator},
            #{gmtCreate},
            #{modifier},
            #{gmtModified},
            #{isDelete}
        )
    </insert>

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

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

    <!-- 修改试题类型 -->
    <update id="update" parameterType="map">
        UPDATE
            exam_question_type
        SET
        <if test="questionTypeParentId != null and questionTypeParentId != ''">
            question_type_parent_id = #{questionTypeParentId},
        </if>
        <if test="typeName != null and typeName != ''">
            type_name = #{typeName},
        </if>
        <if test="typeSummary != null and typeSummary != ''">
            type_summary = #{typeSummary},
        </if>
            gmt_modified = #{gmtModified},
            modifier = #{modifier},
            question_type_id = question_type_id
        WHERE
            question_type_id = #{questionTypeId}
    </update>

    <!-- 试题类型详情 -->
    <select id="get" parameterType="map" resultMap="questionTypeDTO">
        SELECT
            t1.question_type_parent_id,
            t1.type_name,
            t1.type_summary,
            t1.question_type_id,
            jt1.type_name question_type_parent_name
        FROM
            exam_question_type t1
        LEFT JOIN
            exam_question_type jt1
        ON
            t1.question_type_parent_id = jt1.question_type_id
        AND
            jt1.is_delete = 0
        WHERE
            t1.is_delete = 0
            <if test="questionTypeId != null and questionTypeId != ''">
        AND
            t1.question_type_id = #{questionTypeId}
            </if>
    </select>

    <!-- 试题类型详情 -->
    <select id="getBO" parameterType="map" resultMap="questionTypeBO">
        SELECT
            t1.question_type_id,
            t1.question_type_parent_id,
            t1.type_name,
            t1.type_summary,
            t1.creator,
            t1.gmt_create,
            t1.modifier,
            t1.gmt_modified,
            t1.is_delete
        FROM
            exam_question_type t1
        WHERE
            t1.is_delete = 0
            <if test="questionTypeId != null and questionTypeId != ''">
        AND
            t1.question_type_id = #{questionTypeId}
            </if>
    </select>

    <!-- 试题类型详情 -->
    <select id="getPO" parameterType="map" resultMap="questionTypePO">
        SELECT
            t1.question_type_id,
            t1.question_type_parent_id,
            t1.type_name,
            t1.type_summary,
            t1.creator,
            t1.gmt_create,
            t1.modifier,
            t1.gmt_modified,
            t1.is_delete
        FROM
            exam_question_type t1
        WHERE
            t1.is_delete = 0
        <if test="questionTypeId != null and questionTypeId != ''">
        AND
            t1.question_type_id = #{questionTypeId}
        </if>
        <if test="creator != null and creator != ''">
        AND
            t1.creator = #{creator}
        </if>
        <if test="typeName != null and typeName != ''">
        AND
            t1.type_name = #{typeName}
        </if>
    </select>

    <!-- 试题类型列表 -->
    <select id="list" parameterType="map" resultMap="questionTypeDTO">
        SELECT
            t1.question_type_id,
            t1.question_type_parent_id,
            t1.type_name,
            t1.type_summary,
            t1.creator,
            t1.gmt_create,
            jt1.type_name question_type_parent_name
        FROM
            exam_question_type t1
        LEFT JOIN
            exam_question_type jt1
        ON
            t1.question_type_parent_id = jt1.question_type_id
        AND
            t1.is_delete = 0
        WHERE
            t1.is_delete = 0
        <if test="keywords != null and keywords != ''">
        AND (
            <!-- 这里添加其他条件 -->
            t1.id 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="questionTypeIds != null and questionTypeIds.size > 0">
        AND
            t1.question_type_id IN
            <foreach collection="questionTypeIds" index="index" open="(" separator="," close=")">
                #{questionTypeIds[${index}]}
            </foreach>
        </if>
        <if test="questionTypeParentId != null and questionTypeParentId != ''">
        AND
            t1.question_type_parent_id = #{questionTypeParentId}
        </if>
        <if test="creator != null and creator != ''">
        AND
            t1.creator = #{creator}
        </if>
    </select>

    <!-- 试题类型列表 -->
    <select id="listBO" parameterType="map" resultMap="questionTypeBO">
        SELECT
            t1.question_type_id,
            t1.question_type_parent_id,
            t1.type_name,
            t1.type_summary,
            t1.creator,
            t1.gmt_create,
            t1.modifier,
            t1.gmt_modified,
            t1.is_delete
        FROM
            exam_question_type t1
        WHERE
            t1.is_delete = 0
        <if test="keywords != null and keywords != ''">
        AND (
            <!-- 这里添加其他条件 -->
            t1.id 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="questionTypeIds != null and questionTypeIds.size > 0">
        AND
            t1.question_type_id IN
            <foreach collection="questionTypeIds" index="index" open="(" separator="," close=")">
                #{questionTypeIds[${index}]}
            </foreach>
        </if>
    </select>

    <!-- 试题类型列表 -->
    <select id="listPO" parameterType="map" resultMap="questionTypePO">
        SELECT
            t1.question_type_id,
            t1.question_type_parent_id,
            t1.type_name,
            t1.type_summary,
            t1.creator,
            t1.gmt_create,
            t1.modifier,
            t1.gmt_modified,
            t1.is_delete
        FROM
            exam_question_type t1
        WHERE
            t1.is_delete = 0
        <if test="keywords != null and keywords != ''">
        AND (
            <!-- 这里添加其他条件 -->
            t1.id 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="questionTypeIds != null and questionTypeIds.size > 0">
        AND
            t1.question_type_id IN
            <foreach collection="questionTypeIds" index="index" open="(" separator="," close=")">
                #{questionTypeIds[${index}]}
            </foreach>
        </if>
        <if test="creator != null and creator != ''">
        AND
            t1.creator = #{creator}
        </if>
        <if test="typeName != null and typeName != ''">
        AND
            t1.type_name = #{typeName}
        </if>
    </select>

    <!-- 试题类型统计  -->
    <select id="count" parameterType="map" resultType="Integer">
        SELECT
            COUNT(*)
        FROM
            exam_question_type t1
        WHERE
            t1.is_delete = 0
    </select>

    <!-- ztree列表 -->
    <select id="listZTree" parameterType="map" resultMap="zTreeDTO">
        SELECT
            *
        FROM
            exam_question_type
        WHERE
            is_delete = 0
        <if test="questionTypeParentId != null and questionTypeParentId != ''">
        AND
            question_type_parent_id = #{questionTypeParentId}
        </if>
        <if test="creator != null and creator != ''">
        AND
            creator = #{creator}
        </if>
    </select>

    <!-- 子节点数量 -->
    <select id="countByParentId" parameterType="map" resultType="Integer">
        SELECT
            COUNT(*)
        FROM
            exam_question_type
        WHERE
            is_delete = 0
        AND
            question_type_parent_id = #{questionTypeParentId}
        AND
            creator = #{creator}
    </select>

</mapper>