<?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="com.sundot.airport.system.mapper.BaseDefaultChoiseMapper">

    <resultMap type="BaseDefaultChoise" id="BaseDefaultChoiseResult">
        <result property="id" column="id"/>
        <result property="tenantId" column="tenant_id"/>
        <result property="revision" column="revision"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="categoryType" column="category_type"/>
        <result property="categoryId" column="category_id"/>
    </resultMap>

    <sql id="selectBaseDefaultChoiseVo">
        select id,
               tenant_id,
               revision,
               create_by,
               create_time,
               update_by,
               update_time,
               category_type,
               category_id
        from base_default_choise
    </sql>

    <select id="selectBaseDefaultChoiseList" parameterType="BaseDefaultChoise" resultMap="BaseDefaultChoiseResult">
        <include refid="selectBaseDefaultChoiseVo"/>
        <where>
            <if test="tenantId != null and tenantId != ''">and tenant_id = #{tenantId}</if>
            <if test="revision != null">and revision = #{revision}</if>
            <if test="categoryType != null">and category_type = #{categoryType}</if>
            <if test="categoryId != null">and category_id = #{categoryId}</if>
        </where>
    </select>

    <select id="selectBaseDefaultChoiseById" parameterType="Long" resultMap="BaseDefaultChoiseResult">
        <include refid="selectBaseDefaultChoiseVo"/>
        where id = #{id}
    </select>

    <insert id="insertBaseDefaultChoise" parameterType="BaseDefaultChoise" useGeneratedKeys="true" keyProperty="id">
        insert into base_default_choise
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="tenantId != null">tenant_id,</if>
            <if test="revision != null">revision,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="categoryType != null">category_type,</if>
            <if test="categoryId != null">category_id,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="tenantId != null">#{tenantId},</if>
            <if test="revision != null">#{revision},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="categoryType != null">#{categoryType},</if>
            <if test="categoryId != null">#{categoryId},</if>
        </trim>
    </insert>

    <update id="updateBaseDefaultChoise" parameterType="BaseDefaultChoise">
        update base_default_choise
        <trim prefix="SET" suffixOverrides=",">
            <if test="tenantId != null">tenant_id = #{tenantId},</if>
            <if test="revision != null">revision = #{revision},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="categoryType != null">category_type = #{categoryType},</if>
            <if test="categoryId != null">category_id = #{categoryId},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteBaseDefaultChoiseById" parameterType="Long">
        delete
        from base_default_choise
        where id = #{id}
    </delete>

    <delete id="deleteBaseDefaultChoiseByIds" parameterType="String">
        delete from base_default_choise where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <!-- 获取默认选择的分类信息（包含ID和名称及父类信息） -->
    <select id="selectDefaultCategoryWithInfo" parameterType="Integer" resultMap="DefaultCategoryInfoResult">
        SELECT bdc.id  as default_id,
               bdc.category_id,
               bdc.category_type,
               CASE
                   WHEN bdc.category_type = 1 THEN bsc.id
                   WHEN bdc.category_type = 2 THEN bcp.id
                   END as category_id_value,
               CASE
                   WHEN bdc.category_type = 1 THEN bsc.name
                   WHEN bdc.category_type = 2 THEN bcp.name
                   END as category_name,
               CASE
                   WHEN bdc.category_type = 1 THEN bsc.parent_id
                   WHEN bdc.category_type = 2 THEN bcp.parent_id
                   END as parent_id,
               CASE
                   WHEN bdc.category_type = 1 THEN parent_bsc.name
                   WHEN bdc.category_type = 2 THEN parent_bcp.name
                   END as parent_name
        FROM base_default_choise bdc
                 LEFT JOIN base_seize_category bsc ON bdc.category_type = 1 AND bdc.category_id = bsc.id
                 LEFT JOIN base_check_point bcp ON bdc.category_type = 2 AND bdc.category_id = bcp.id
                 LEFT JOIN base_seize_category parent_bsc ON bdc.category_type = 1 AND bsc.parent_id = parent_bsc.id
                 LEFT JOIN base_check_point parent_bcp ON bdc.category_type = 2 AND bcp.parent_id = parent_bcp.id
        WHERE bdc.category_type = #{categoryType}
    </select>

    <!-- 扩展的结果映射，包含分类信息 -->
    <resultMap type="BaseDefaultChoise" id="DefaultCategoryInfoResult" extends="BaseDefaultChoiseResult">
        <result property="categoryName" column="category_name"/>
        <result property="parentId" column="parent_id"/>
        <result property="parentName" column="parent_name"/>
    </resultMap>

</mapper>