<?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="org.jeecg.modules.resManager.catalog.mapper.RescatalogMapper">

    <update id="updateTreeNodeStatus" parameterType="java.lang.String">
        update rescatalog
        set has_child = #{status}
        where id = #{id}
    </update>

    <update id="subLayerIdAdditive">
        update rescatalog t
        set t.sublayerid = t.sublayerid + 1
        where t.sublayerid >= #{addid}
          and t.sublayerid is not null
    </update>

    <update id="subLayerIdDecrement">
        update rescatalog t
        set t.sublayerid = t.sublayerid - 1
        where t.sublayerid > #{delid}
          and t.sublayerid is not null
    </update>

    <!-- 【vue3专用】 -->
    <select id="queryListByPid" parameterType="java.lang.Object"
            resultType="org.jeecg.common.system.vo.SelectTreeModel">
        select
        id as "key",
        alias as "title",
        (case when has_child = '1' then 0 else 1 end) as isLeaf,
        pid as parentId
        from rescatalog
        where pid = #{pid}
        <if test="query != null">
            <foreach collection="query.entrySet()" item="value" index="key">
                and ${key} = #{value}
            </foreach>
        </if>
    </select>

    <select id="queryByUser" parameterType="java.lang.String">
        select t.*
        from RESCATALOG t
        where t.id in
              (
                  select r.rescatalog_id
                  from role_rescatalog r
                  where r.role_id in (select b.role_id
                                      from SYS_USER_ROLE b
                                      where b.user_id = (select a.id from sys_user a where a.username = #{username})))
    </select>

    <select id="queryCountByUsername" parameterType="java.lang.Object" resultType="int">
        select count(r.rescatalog_id)
        from role_rescatalog r
        where r.role_id in
              (select b.role_id
               from SYS_USER_ROLE b
               where b.user_id =
                     (select a.id from sys_user a where a.username = #{username}))
          and r.rescatalog_id = #{resId}
    </select>

</mapper>