<?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="go.kr.project.system.user.mapper.UserGroupMapper">

    <!-- 그룹 목록 조회 -->
    <select id="selectGroupList" parameterType="UserGroupPopupSearchVO" resultType="GroupVO">
        SELECT
            GROUP_ID,
            GROUP_NM,
            GROUP_DC,
            USE_YN
        FROM TB_GROUP
        WHERE 1=1
        AND USE_YN = 'Y'
        <if test="searchKeyword != null and searchKeyword != ''">
            AND (
                GROUP_ID LIKE CONCAT('%', #{searchKeyword}, '%')
                OR GROUP_NM LIKE CONCAT('%', #{searchKeyword}, '%')
            )
        </if>
        <if test="excludeGroupId != null and excludeGroupId != ''">
            AND GROUP_ID != #{excludeGroupId}
        </if>
        ORDER BY SORT_ORDR
    </select>

    <!-- 그룹별 역할 목록 조회 -->
    <select id="selectGroupRoleList" parameterType="UserGroupPopupSearchVO" resultType="GroupRoleVO">
        SELECT
            gr.GROUP_ID,
            gr.ROLE_ID,
            r.ROLE_NM,
            r.ROLE_DC
        FROM TB_GROUP_ROLE gr
        JOIN TB_ROLE r ON gr.ROLE_ID = r.ROLE_ID
        WHERE gr.GROUP_ID = #{groupId}
        ORDER BY r.SORT_ORDR
    </select>

    <!-- 역할별 메뉴 목록 조회 -->
    <select id="selectRoleMenuList" parameterType="UserGroupPopupSearchVO" resultType="RoleMenuVO">
        SELECT
            m.MENU_ID,
            m.MENU_NM,
            m.UPPER_MENU_ID,
            m.MENU_LEVEL,
            m.SORT_ORDR,
            m.MENU_URL,
            m.MENU_DC,
            m.MENU_ICON
        FROM TB_ROLE_MENU rm
        JOIN TB_MENU m ON rm.MENU_ID = m.MENU_ID
        WHERE rm.ROLE_ID = #{roleId}
        ORDER BY m.MENU_LEVEL, m.SORT_ORDR
    </select>

</mapper>