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

    <resultMap type="BasePosition" id="BasePositionResult">
        <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="id" column="id"/>
        <result property="name" column="name"/>
        <result property="parentId" column="parent_id"/>
        <result property="ancestors" column="ancestors"/>
        <result property="level" column="level"/>
        <result property="orderNum" column="order_num"/>
        <result property="positionType" column="position_type"/>
        <result property="channelType" column="channel_type"/>
        <result property="code" column="code"/>
        <result property="positionTypeDesc" column="position_type_desc"/>
        <result property="channelTypeDesc" column="channel_type_desc"/>
    </resultMap>

    <sql id="selectBasePositionVo">
        select tenant_id,
               revision,
               create_by,
               create_time,
               update_by,
               update_time,
               id,
               name,
               parent_id,
               ancestors,
               level,
               order_num,
               position_type,
               channel_type,
               code,
               position_type_desc,
               channel_type_desc
        from base_position
    </sql>

    <select id="selectBasePositionList" parameterType="BasePosition" resultMap="BasePositionResult">
        <include refid="selectBasePositionVo"/>
        <where>
            <if test="tenantId != null  and tenantId != ''">and tenant_id = #{tenantId}</if>
            <if test="revision != null ">and revision = #{revision}</if>
            <if test="name != null  and name != ''">and name like concat('%', #{name}, '%')</if>
            <if test="parentId != null ">and parent_id = #{parentId}</if>
            <if test="ancestors != null  and ancestors != ''">and ancestors = #{ancestors}</if>
            <if test="level != null ">and level = #{level}</if>
            <if test="orderNum != null ">and order_num = #{orderNum}</if>
            <if test="positionType != null  and positionType != ''">and position_type = #{positionType}</if>
            <if test="channelType != null  and channelType != ''">and channel_type = #{channelType}</if>
            <if test="code != null  and code != ''">and code = #{code}</if>
            <if test="positionTypeDesc != null  and positionTypeDesc != ''">and position_type_desc =
                #{positionTypeDesc}
            </if>
            <if test="channelTypeDesc != null  and channelTypeDesc != ''">and channel_type_desc = #{channelTypeDesc}
            </if>
        </where>
        order by parent_id, order_num
    </select>

    <select id="selectBasePositionById" parameterType="Long" resultMap="BasePositionResult">
        <include refid="selectBasePositionVo"/>
        where id = #{id}
    </select>

    <insert id="insertBasePosition" parameterType="BasePosition" useGeneratedKeys="true" keyProperty="id">
        insert into base_position
        <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="name != null and name != ''">name,</if>
            <if test="parentId != null">parent_id,</if>
            <if test="ancestors != null and ancestors != ''">ancestors,</if>
            <if test="level != null">level,</if>
            <if test="orderNum != null">order_num,</if>
            <if test="positionType != null and positionType != ''">position_type,</if>
            <if test="channelType != null and channelType != ''">channel_type,</if>
            <if test="code != null and code != ''">code,</if>
            <if test="positionTypeDesc != null and positionTypeDesc != ''">position_type_desc,</if>
            <if test="channelTypeDesc != null and channelTypeDesc != ''">channel_type_desc,</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="name != null and name != ''">#{name},</if>
            <if test="parentId != null">#{parentId},</if>
            <if test="ancestors != null and ancestors != ''">#{ancestors},</if>
            <if test="level != null">#{level},</if>
            <if test="orderNum != null">#{orderNum},</if>
            <if test="positionType != null and positionType != ''">#{positionType},</if>
            <if test="channelType != null and channelType != ''">#{channelType},</if>
            <if test="code != null and code != ''">#{code},</if>
            <if test="positionTypeDesc != null and positionTypeDesc != ''">#{positionTypeDesc},</if>
            <if test="channelTypeDesc != null and channelTypeDesc != ''">#{channelTypeDesc},</if>
        </trim>
    </insert>

    <update id="updateBasePosition" parameterType="BasePosition">
        update base_position
        <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="name != null and name != ''">name = #{name},</if>
            <if test="parentId != null">parent_id = #{parentId},</if>
            <if test="ancestors != null and ancestors != ''">ancestors = #{ancestors},</if>
            <if test="level != null">level = #{level},</if>
            <if test="orderNum != null">order_num = #{orderNum},</if>
            <if test="positionType != null and positionType != ''">position_type = #{positionType},</if>
            <choose>
                <when test="channelType != null and channelType != ''">
                    channel_type = #{channelType},
                </when>
                <otherwise>
                    channel_type = null,
                </otherwise>
            </choose>
            <if test="code != null and code != ''">code = #{code},</if>
            <if test="positionTypeDesc != null and positionTypeDesc != ''">position_type_desc = #{positionTypeDesc},
            </if>
            <choose>
                <when test="channelTypeDesc != null and channelTypeDesc != ''">
                    channel_type_desc = #{channelTypeDesc},
                </when>
                <otherwise>
                    channel_type_desc = null,
                </otherwise>
            </choose>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteBasePositionById" parameterType="Long">
        delete
        from base_position
        where id = #{id}
    </delete>

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

    <select id="selectChildrenById" parameterType="Long" resultMap="BasePositionResult">
        select *
        from base_position
        where find_in_set(#{id}, ancestors)
    </select>

    <update id="updateChildren" parameterType="java.util.List">
        update base_position set ancestors =
        <foreach collection="list" item="item" index="index"
                 separator=" " open="case id" close="end">
            when #{item.id} then #{item.ancestors}
        </foreach>,
        level =
        <foreach collection="list" item="item" index="index"
                 separator=" " open="case id" close="end">
            when #{item.id} then #{item.level}
        </foreach>
        where id in
        <foreach collection="list" item="item" index="index"
                 separator="," open="(" close=")">
            #{item.id}
        </foreach>
    </update>






    <select id="selectChannelsUnderArea" resultType="com.sundot.airport.system.domain.vo.PositionInfoVO">
        SELECT
            -- 大区域信息
            la.id,
            la.name,
            la.code,
            la.level,
            la.position_type,
            la.channel_type,
            la.position_type_desc,
            la.channel_type_desc,
            la.parent_id,
            la.tenant_id,
            la.revision,
            -- 小区域信息
            sa.id as small_area_id,
            sa.name as small_area_name,
            sa.code as small_area_code,
            sa.level as small_area_level,
            sa.position_type as small_area_position_type,
            sa.channel_type as small_area_channel_type,
            sa.position_type_desc as small_area_position_type_desc,
            sa.channel_type_desc as small_area_channel_type_desc,
            sa.parent_id as small_area_parent_id,
            -- 通道信息
            ch.id as channel_id,
            ch.name as channel_name,
            ch.code as channel_code,
            ch.level as channel_level,
            ch.position_type as channel_position_type,
            ch.channel_type as channel_channel_type,
            ch.position_type_desc as channel_position_type_desc,
            ch.channel_type_desc as channel_channel_type_desc,
            ch.parent_id as channel_parent_id
        FROM base_position la
                 LEFT JOIN base_position sa ON sa.parent_id = la.id AND sa.level = 2
                 LEFT JOIN base_position ch ON ch.parent_id = sa.id AND ch.level = 3
        WHERE la.level = 1 AND la.parent_id = 0
        <if test="regionalCode != null and !regionalCode.isEmpty()">
            AND sa.code IN
            <foreach item="code" collection="regionalCode" open="(" separator="," close=")">
                #{code}
            </foreach>
        </if>
        ORDER BY la.code, sa.code, ch.code
    </select>


</mapper>