<?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.PolicyMenuMapper">
    
    <resultMap type="PolicyMenu" id="PolicyMenuResult">
        <result property="menuId"    column="menu_id"    />
        <result property="menuName"    column="menu_name"    />
        <result property="menuIntroduce"    column="menu_introduce"    />
        <result property="parentId"    column="parent_id"    />
        <result property="orderNum"    column="order_num"    />
        <result property="visible"    column="visible"    />
        <result property="status"    column="status"    />
        <result property="photo"    column="photo"    />
        <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="remark"    column="remark"    />
        <result property="ancestors"    column="ancestors"    />
    </resultMap>

    <sql id="selectPolicyMenuVo">
        select menu_id, menu_name, menu_introduce, parent_id, order_num, visible, status, photo, create_by, create_time, update_by, update_time, remark,ancestors from policy_menu
    </sql>

    <select id="selectPolicyMenuList" parameterType="PolicyMenu" resultMap="PolicyMenuResult">
        <include refid="selectPolicyMenuVo"/>
        <where>
            status = '0'
            and visible = '0'
            <if test="menuName != null  and menuName != ''"> and menu_name like concat(#{menuName}, '%')</if>
            <if test="parentId != null"> and parent_id = #{parentId}</if>
        </where>
        order by parent_id,order_num
    </select>
    
    <select id="selectPolicyMenuByMenuId" parameterType="Long" resultMap="PolicyMenuResult">
        <include refid="selectPolicyMenuVo"/>
        where menu_id = #{menuId}
    </select>

    <select id="checkMenuNameUnique" resultType="com.ruoyi.common.core.domain.entity.PolicyMenu">
        <include refid="selectPolicyMenuVo"/>
        where menu_name=#{menuName} and parent_id = #{parentId} and status = '0' limit 1
    </select>
    <select id="selectNormalChildrenMenuById" resultType="java.lang.Integer">
    select count(*) from policy_menu where status = '0'  and find_in_set(#{menuId}, ancestors)
    </select>

    <select id="selectChildrenMenuById" resultType="com.ruoyi.common.core.domain.entity.PolicyMenu">
        select  menu_id, menu_name, menu_introduce, parent_id, order_num, visible, status, photo, create_by, create_time, update_by, update_time, remark,ancestors from policy_menu where find_in_set(#{menuId}, ancestors)
    </select>

    <insert id="insertPolicyMenu" parameterType="PolicyMenu" useGeneratedKeys="true" keyProperty="menuId">
        insert into policy_menu
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="menuName != null and menuName != ''">menu_name,</if>
            <if test="menuIntroduce != null and menuIntroduce != ''">menu_introduce,</if>
            <if test="parentId != null">parent_id,</if>
            <if test="orderNum != null">order_num,</if>
            <if test="visible != null">visible,</if>
            <if test="status != null">status,</if>
            <if test="photo != null">photo,</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="remark != null">remark,</if>
            <if test="ancestors != null">ancestors,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="menuName != null and menuName != ''">#{menuName},</if>
            <if test="menuIntroduce != null and menuIntroduce != ''">#{menuIntroduce},</if>
            <if test="parentId != null">#{parentId},</if>
            <if test="orderNum != null">#{orderNum},</if>
            <if test="visible != null">#{visible},</if>
            <if test="status != null">#{status},</if>
            <if test="photo != null">#{photo},</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="remark != null">#{remark},</if>
            <if test="ancestors != null">#{ancestors},</if>
         </trim>
    </insert>

    <update id="updatePolicyMenu" parameterType="PolicyMenu">
        update policy_menu
        <trim prefix="SET" suffixOverrides=",">
            <if test="menuName != null and menuName != ''">menu_name = #{menuName},</if>
            <if test="menuIntroduce != null and menuIntroduce != ''">menu_introduce = #{menuIntroduce},</if>
            <if test="parentId != null">parent_id = #{parentId},</if>
            <if test="orderNum != null">order_num = #{orderNum},</if>
            <if test="visible != null">visible = #{visible},</if>
            <if test="status != null">status = #{status},</if>
            <if test="photo != null">photo = #{photo},</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="remark != null">remark = #{remark},</if>
            <if test="ancestors != null">ancestors = #{ancestors},</if>
        </trim>
        where menu_id = #{menuId}
    </update>
    <update id="updateMenuChildren">
        update policy_menu set ancestors =
        <foreach collection="menus" item="item" index="index"
                 separator=" " open="case menu_id" close="end">
            when #{item.menuId} then #{item.ancestors}
        </foreach>
        where menu_id in
        <foreach collection="menus" item="item" index="index"
                 separator="," open="(" close=")">
            #{item.menuId}
        </foreach>
    </update>
    <update id="updateMenuStatusNormal" parameterType="Long">
        update policy_menu set status = '0' where menu_id in
        <foreach collection="array" item="menuId" open="(" separator="," close=")">
            #{menuId}
        </foreach>
    </update>

    <delete id="deletePolicyMenuByMenuId" parameterType="Long">
        delete from policy_menu where menu_id = #{menuId}
    </delete>

    <delete id="deletePolicyMenuByMenuIds" parameterType="String">
        delete from policy_menu where menu_id in 
        <foreach item="menuId" collection="array" open="(" separator="," close=")">
            #{menuId}
        </foreach>
    </delete>
</mapper>