<?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.group.mapper.GroupMapper">

    <!-- 그룹 목록 조회 -->
    <select id="selectGroupList" parameterType="GroupSearchVO" resultType="GroupVO">
        select GROUP_ID
             , GROUP_NM
             , GROUP_DC
             , SORT_ORDR
             , USE_YN
             , REG_DTTM
             , RGTR
             , MDFCN_DTTM
             , MDFR
        from tb_group
        where 1 = 1
        <if test='searchUseYn != null and searchUseYn != ""'>
            and USE_YN = #{searchUseYn}
        </if>
        <if test='searchKeyword != null and searchKeyword != ""'>
            and (
                GROUP_ID like CONCAT('%', #{searchKeyword}, '%')
                or GROUP_NM like CONCAT('%', #{searchKeyword}, '%')
                or GROUP_DC like CONCAT('%', #{searchKeyword}, '%')
            )
        </if>
        <if test="sortAscending != null and sortColumn != null and sortColumn != ''">
            <choose>
                <when test="sortColumn == 'groupId'">order by GROUP_ID</when>
                <when test="sortColumn == 'groupNm'">order by GROUP_NM</when>
                <when test="sortColumn == 'sortOrdr'">order by SORT_ORDR</when>
                <otherwise>
                    order by SORT_ORDR, GROUP_ID
                </otherwise>
            </choose>
            <choose>
                <when test="sortAscending == true">ASC</when>
                <when test="sortAscending == false">DESC</when>
                <otherwise>
                    ASC
                </otherwise>
            </choose>
        </if>
        <if test='pagingYn != null and pagingYn == "Y"'>
            LIMIT #{startIndex}, #{perPage}
        </if>
    </select>

    <!-- 그룹 목록 총 개수 조회 -->
    <select id="selectGroupListTotalCount" parameterType="GroupSearchVO" resultType="int">
        select COUNT(*)
        from tb_group
        where 1 = 1
        <if test='searchUseYn != null and searchUseYn != ""'>
            and USE_YN = #{searchUseYn}
        </if>
        <if test='searchKeyword != null and searchKeyword != ""'>
            and (
                GROUP_ID like CONCAT('%', #{searchKeyword}, '%')
                or GROUP_NM like CONCAT('%', #{searchKeyword}, '%')
                or GROUP_DC like CONCAT('%', #{searchKeyword}, '%')
            )
        </if>
    </select>

    <!-- 그룹 등록 -->
    <insert id="insertGroup" parameterType="GroupVO">
        INSERT INTO tb_group (
            GROUP_ID,
            GROUP_NM,
            GROUP_DC,
            SORT_ORDR,
            USE_YN,
            REG_DTTM,
            RGTR,
            MDFCN_DTTM,
            MDFR
        ) VALUES (
            #{groupId},
            #{groupNm},
            #{groupDc},
            #{sortOrdr},
            #{useYn},
            NOW(),
            #{rgtr},
            NOW(),
            #{rgtr}
        )
    </insert>

    <!-- 그룹 수정 -->
    <update id="updateGroup" parameterType="GroupVO">
        UPDATE tb_group
        SET
            GROUP_NM = #{groupNm},
            <if test="groupDc != null">
            GROUP_DC = #{groupDc},
            </if>
            SORT_ORDR = #{sortOrdr},
            USE_YN = #{useYn},
            MDFCN_DTTM = NOW(),
            MDFR = #{mdfr}
        WHERE GROUP_ID = #{groupId}
    </update>

    <!-- 그룹 삭제 -->
    <delete id="deleteGroup" parameterType="String">
        DELETE FROM tb_group
        WHERE GROUP_ID = #{groupId}
    </delete>

    <!-- 그룹에 연결된 역할 매핑 삭제 -->
    <delete id="deleteGroupRoles" parameterType="String">
        DELETE FROM tb_group_role
        WHERE GROUP_ID = #{groupId}
    </delete>

    <!-- 그룹이 사용 중인지 확인 -->
    <select id="checkGroupInUse" parameterType="String" resultType="boolean">
        SELECT CASE WHEN COUNT(*) > 0 THEN 1 ELSE 0 END
        FROM tb_user
        WHERE USER_GROUP_ID = #{groupId}
        LIMIT 1
    </select>

    <!-- 그룹을 사용 중인 사용자 정보 조회 -->
    <select id="getGroupUsageInfo" parameterType="String" resultType="java.util.HashMap">
        SELECT 
            u.USER_ID, 
            u.USER_NM
        FROM 
            tb_user u
        WHERE 
            u.USER_GROUP_ID = #{groupId}
    </select>

    <!-- 그룹 ID 중복 확인 -->
    <select id="checkGroupIdDuplicate" parameterType="String" resultType="boolean">
        SELECT CASE WHEN COUNT(*) > 0 THEN 1 ELSE 0 END
        FROM tb_group
        WHERE GROUP_ID = #{groupId}
    </select>
</mapper>
