<?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.ruoyi.system.mapper.ColumnNavigationBarMapper">

    <resultMap type="ColumnNavigationBar" id="ColumnNavigationBarResult">
        <result property="columnId" column="column_id"/>
        <result property="parentId" column="parent_id"/>
        <result property="ancestors" column="ancestors"/>
        <result property="columnName" column="column_name"/>
        <result property="orderNum" column="order_num"/>
        <result property="isNavigationBar" column="is_navigation_bar"/>
        <result property="isTitle" column="is_title"/>
        <result property="isBottom" column="is_bottom"/>
        <result property="openMode" column="open_mode"/>
        <result property="status" column="status"/>
        <result property="delFlag" column="del_flag"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <sql id="selectColumnNavigationBarVo">
        select column_id, parent_id, ancestors, column_name, order_num,is_navigation_bar,is_title,is_bottom,open_mode, status, del_flag, create_by, create_time, update_by, update_time from column_navigation_bar
    </sql>

    <select id="selectColumnNavigationBarList" parameterType="ColumnNavigationBar"
            resultMap="ColumnNavigationBarResult">
        <include refid="selectColumnNavigationBarVo"/>
        <where>
        del_flag = 'N'
        <if test="parentId != null ">and parent_id = #{parentId}</if>
        <if test="ancestors != null  and ancestors != ''">and ancestors = #{ancestors}</if>
        <if test="columnName != null  and columnName != ''">and column_name like concat('%', #{columnName}, '%')</if>
        <if test="orderNum != null ">and order_num = #{orderNum}</if>
        <if test="isNavigationBar != null ">and is_navigation_bar = #{isNavigationBar}</if>
        <if test="isTitle != null ">and is_title = #{isTitle}</if>
        <if test="isBottom != null ">and is_bottom = #{isBottom}</if>
        </where>
    </select>

    <select id="selectColumnNavigationBarListMenHu" parameterType="ColumnNavigationBar"
            resultMap="ColumnNavigationBarResult">
        select column_id, column_name,open_mode from column_navigation_bar
        <where>
        status = '0'
        and del_flag = 'N'
        <if test="isTitle != null ">and is_title = #{isTitle}</if>
        <if test="isBottom != null ">and is_bottom = #{isBottom}</if>
        </where>
    </select>

    <select id="selectColumnNavigationBarByColumnId" parameterType="Long" resultMap="ColumnNavigationBarResult">
        <include refid="selectColumnNavigationBarVo"/>
        where column_id = #{columnId}  and del_flag = 'N'
    </select>

    <insert id="insertColumnNavigationBar" parameterType="ColumnNavigationBar" useGeneratedKeys="true"
            keyProperty="columnId">
        insert into column_navigation_bar
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="parentId != null">parent_id,</if>
            <if test="ancestors != null">ancestors,</if>
            <if test="columnName != null">column_name,</if>
            <if test="orderNum != null">order_num,</if>
            <if test="isNavigationBar != null">is_navigation_bar,</if>
            <if test="isTitle != null">is_title,</if>
            <if test="isBottom != null">is_bottom,</if>
            <if test="openMode != null">open_mode,</if>
            <if test="status != null">status,</if>
            <if test="delFlag != null">del_flag,</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>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="parentId != null">#{parentId},</if>
            <if test="ancestors != null">#{ancestors},</if>
            <if test="columnName != null">#{columnName},</if>
            <if test="orderNum != null">#{orderNum},</if>
            <if test="isNavigationBar != null">#{isNavigationBar},</if>
            <if test="isTitle != null">#{isTitle},</if>
            <if test="isBottom != null">#{isBottom},</if>
            <if test="openMode != null">#{openMode},</if>
            <if test="status != null">#{status},</if>
            <if test="delFlag != null">#{delFlag},</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>
        </trim>
    </insert>

    <update id="updateColumnNavigationBar" parameterType="ColumnNavigationBar">
        update column_navigation_bar
        <trim prefix="SET" suffixOverrides=",">
            <if test="parentId != null">parent_id = #{parentId},</if>
            <if test="ancestors != null">ancestors = #{ancestors},</if>
            <if test="columnName != null">column_name = #{columnName},</if>
            <if test="orderNum != null">order_num = #{orderNum},</if>
            <if test="isNavigationBar != null">is_navigation_bar = #{isNavigationBar},</if>
            <if test="isTitle != null">is_title = #{isTitle},</if>
            <if test="isBottom != null">is_bottom = #{isBottom},</if>
            <if test="openMode != null">open_mode = #{openMode},</if>
            <if test="status != null">status = #{status},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</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>
        </trim>
        where column_id = #{columnId}
    </update>

    <delete id="deleteColumnNavigationBarByColumnId" parameterType="Long">
    	update column_navigation_bar set del_flag = 'Y' where column_id = #{columnId}
    </delete>

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


    <select id="checkColumnNameUnique" resultMap="ColumnNavigationBarResult">
        <include refid="selectColumnNavigationBarVo"/>
        where column_name = #{columnName} and parent_id = #{parentId}  and del_flag = 'N' limit 1
    </select>

    <select id="selectNormalChildrenColumnById" parameterType="Long" resultType="int">
		select count(*) from column_navigation_bar where  del_flag = 'N' and find_in_set(#{columnId}, ancestors)
	</select>

    <select id="selectChildrenColumnById" parameterType="Long" resultMap="ColumnNavigationBarResult">
		select * from column_navigation_bar where find_in_set(#{columnId}, ancestors) and del_flag = 'N'
	</select>

    <update id="updateColumnChildren" parameterType="java.util.List">
        update column_navigation_bar set ancestors =
        <foreach collection="columns" item="item" index="index"
                 separator=" " open="case column_id" close="end">
            when #{item.columnId} then #{item.ancestors}
        </foreach>
        where column_id in
        <foreach collection="columns" item="item" index="index"
                 separator="," open="(" close=")">
            #{item.columnId}
        </foreach>
    </update>

    <update id="updateColumnStatusNormal" parameterType="Long">
        update column_navigation_bar set status = '0' where column_id in
        <foreach collection="array" item="columnId" open="(" separator="," close=")">
            #{columnId}
        </foreach>
    </update>

    <select id="hasChildByColumnId" parameterType="Long" resultType="int">
		select count(1) from column_navigation_bar
		where del_flag = 'N' and parent_id = #{columnId} limit 1
	</select>
</mapper>