<?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="io.shinhanlife.axhub.biz.so.atm.domain.repository.AccessMgmtMapper">

    <resultMap id="roleResultMap" type="io.shinhanlife.axhub.biz.so.atm.dto.RoleListOutDto">
        <result column="SYST_ID" property="systId"/>
        <result column="ROLE_NO" property="roleNo"/>
        <result column="ROLE_NM" property="roleNm"/>
        <result column="ROLE_DS" property="roleDs"/>
        <result column="PUSE_YN" property="puseYn"/>
    </resultMap>

    <resultMap id="athrItemResultMap" type="io.shinhanlife.axhub.biz.so.atm.dto.AthrItemOutDto">
        <result column="RESOURCE_ID" property="resourceId"/>
        <result column="RESOURCE_NM" property="resourceNm"/>
        <result column="RESOURCE_DS" property="resourceDs"/>
        <result column="TYPE_CODE"   property="typeCode"/>
    </resultMap>

    <!-- 역할 목록 조회 -->
    <select id="selectRoles" parameterType="io.shinhanlife.axhub.biz.so.atm.dto.RoleListInDto"
            resultMap="roleResultMap">
        SELECT
            SYST_ID,
            ROLE_NO,
            ROLE_NM,
            ROLE_DS,
            PUSE_YN
        FROM
            S_EIAM_TIS.AA_ROLE
        <where>
            <if test="systId != null and systId != ''">
                AND SYST_ID = #{systId}
            </if>
            <if test="puseYn != null and puseYn != ''">
                AND PUSE_YN = #{puseYn}
            </if>
            <if test="keyword != null and keyword != ''">
                AND ROLE_NM LIKE '%' || #{keyword} || '%'
            </if>
        </where>
        ORDER BY ROLE_NM ASC
    </select>

    <!-- Tool 마스터 전체 조회 -->
    <select id="selectAllTools" resultMap="athrItemResultMap">
        SELECT
            TOOL_ID      AS RESOURCE_ID,
            TOOL_NM      AS RESOURCE_NM,
            TOOL_DS      AS RESOURCE_DS,
            TOOL_TYPE_CD AS TYPE_CODE
        FROM
            AX_TOOL_MST
        WHERE
            PUSE_YN = 'Y'
        ORDER BY
            TOOL_NM ASC
    </select>

    <!-- 지식 마스터 전체 조회 -->
    <select id="selectAllKnwls" resultMap="athrItemResultMap">
        SELECT
            KNWL_ID      AS RESOURCE_ID,
            KNWL_NM      AS RESOURCE_NM,
            KNWL_DS      AS RESOURCE_DS,
            KNWL_TYPE_CD AS TYPE_CODE
        FROM
            AX_KNWL_MST
        WHERE
            PUSE_YN = 'Y'
        ORDER BY
            KNWL_NM ASC
    </select>

    <!-- 역할에 부여된 Tool ID 목록 -->
    <select id="selectGrantedToolIds" parameterType="io.shinhanlife.axhub.biz.so.atm.dto.AthrSearchInDto"
            resultType="string">
        SELECT
            TOOL_ID
        FROM
            AX_ROLE_TOOL_ATHR
        WHERE
            SYST_ID = #{systId}
          AND ROLE_NO = #{roleNo}
          AND PUSE_YN = 'Y'
    </select>

    <!-- 역할에 부여된 지식 ID 목록 -->
    <select id="selectGrantedKnwlIds" parameterType="io.shinhanlife.axhub.biz.so.atm.dto.AthrSearchInDto"
            resultType="string">
        SELECT
            KNWL_ID
        FROM
            AX_ROLE_KNWL_ATHR
        WHERE
            SYST_ID = #{systId}
          AND ROLE_NO = #{roleNo}
          AND PUSE_YN = 'Y'
    </select>

    <!-- Tool 권한 전체 삭제 -->
    <delete id="deleteToolAthr">
        DELETE FROM AX_ROLE_TOOL_ATHR
        WHERE
            SYST_ID = #{systId}
          AND ROLE_NO = #{roleNo}
    </delete>

    <!-- Tool 권한 단건 삽입 -->
    <insert id="insertToolAthr" parameterType="io.shinhanlife.axhub.biz.so.atm.dto.RoleToolAthrInDto">
        INSERT INTO AX_ROLE_TOOL_ATHR (
            ROLE_TOOL_ATHR_ID,
            SYST_ID,
            ROLE_NO,
            TOOL_ID,
            PUSE_YN,
            SYST_RGI_DT,
            SYST_RGI_PRAF_NO,
            SYST_RGI_OGNZ_NO,
            SYST_RGI_SYST_CD,
            SYST_RGI_PRGR_ID,
            SYST_CHG_DT,
            SYST_CHG_PRAF_NO,
            SYST_CHG_OGNZ_NO,
            SYST_CHG_SYST_CD,
            SYST_CHG_PRGR_ID
        ) VALUES (
            #{roleToolAthrId},
            #{systId},
            #{roleNo},
            #{toolId},
            #{puseYn},
            #{systRgiDt},
            #{systRgiPrafNo},
            #{systRgiOgnzNo},
            #{systRgiSystCd},
            #{systRgiPrgrId},
            #{systChgDt},
            #{systChgPrafNo},
            #{systChgOgnzNo},
            #{systChgSystCd},
            #{systChgPrgrId}
        )
    </insert>

    <!-- 지식 권한 전체 삭제 -->
    <delete id="deleteKnwlAthr">
        DELETE FROM AX_ROLE_KNWL_ATHR
        WHERE
            SYST_ID = #{systId}
          AND ROLE_NO = #{roleNo}
    </delete>

    <!-- 지식 권한 단건 삽입 -->
    <insert id="insertKnwlAthr" parameterType="io.shinhanlife.axhub.biz.so.atm.dto.RoleKnwlAthrInDto">
        INSERT INTO AX_ROLE_KNWL_ATHR (
            ROLE_KNWL_ATHR_ID,
            SYST_ID,
            ROLE_NO,
            KNWL_ID,
            PUSE_YN,
            SYST_RGI_DT,
            SYST_RGI_PRAF_NO,
            SYST_RGI_OGNZ_NO,
            SYST_RGI_SYST_CD,
            SYST_RGI_PRGR_ID,
            SYST_CHG_DT,
            SYST_CHG_PRAF_NO,
            SYST_CHG_OGNZ_NO,
            SYST_CHG_SYST_CD,
            SYST_CHG_PRGR_ID
        ) VALUES (
            #{roleKnwlAthrId},
            #{systId},
            #{roleNo},
            #{knwlId},
            #{puseYn},
            #{systRgiDt},
            #{systRgiPrafNo},
            #{systRgiOgnzNo},
            #{systRgiSystCd},
            #{systRgiPrgrId},
            #{systChgDt},
            #{systChgPrafNo},
            #{systChgOgnzNo},
            #{systChgSystCd},
            #{systChgPrgrId}
        )
    </insert>

</mapper>
