<?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="cokr.xit.base.security.access.dao.AuthorityMapper">

<resultMap id="authRow" type="cokr.xit.base.security.Authority">
	<result property="type" column="AUTH_TYPE"/>
	<result property="id" column="AUTH_ID"/>
	<result property="name" column="AUTH_NM"/>
	<result property="infoScope" column="INF_SCP"/>
	<result property="userInfoScope" column="USER_INF_SCP"/>
	<result property="description" column="DSCRP"/>
	<result property="createdAt" column="REG_DT"/>
</resultMap>

<sql id="selectAuthorities"><include refid="utility.paging-prefix" />
SELECT * FROM (
    SELECT 0 AUTH_TYPE, 'ROLE_ADMIN' AUTH_ID, '시스템 관리자' AUTH_NM, '시스템 관리자' DSCRP, 'all' INF_SCP, 'all' USER_INF_SCP,<include refid="utility.now"/>REG_DT UNION
    SELECT 1 AUTH_TYPE, 'ROLE_ANONYMOUS' AUTH_ID, '익명 사용자' AUTH_NM, '모든 사용자' DSCRP, 'none' INF_SCP, 'none' USER_INF_SCP,<include refid="utility.now"/>REG_DT UNION
    SELECT 1 AUTH_TYPE, 'ROLE_USER' AUTH_ID, '시스템 사용자' AUTH_NM, '로그인한 사용자' DSCRP, 'self' INF_SCP, 'self' USER_INF_SCP,<include refid="utility.now"/>REG_DT UNION
    SELECT 2 AUTH_TYPE, AUTH_ID, AUTH_NM, DSCRP, INF_SCP, USER_INF_SCP, REG_DT
      FROM TB_AUTHORITY
) A
<where>
<if test="authIDs != null">AND AUTH_ID IN (<foreach collection="authIDs" item="authID" separator=",">#{authID}</foreach>)</if>
<if test="by != null and term != null">AND ${by} LIKE CONCAT('%', #{term}, '%')</if>
</where>
<include refid="utility.orderBy"/>
<include refid="utility.paging-suffix" /></sql>

<select id="getAuthorityList" parameterType="map" resultType="dataobject">/* 권한 목록 조회(authorityMapper.getAuthorityList) */
<include refid="selectAuthorities" /></select>

<select id="getAuthorities" parameterType="map" resultMap="authRow">/* 권한 가져오기(authorityMapper.getAuthorities) */
<include refid="selectAuthorities" /></select>

<insert id="insertAuthority" parameterType="cokr.xit.base.security.Authority">/* 권한 등록(authorityMapper.insertAuthority) */
INSERT INTO TB_AUTHORITY (
       AUTH_ID
     , AUTH_NM
     , DSCRP
     , INF_SCP
     , USER_INF_SCP
     , REG_DT
) VALUES (
       #{id}
     , #{name}
     , #{description}
     , #{infoScope}
     , #{userInfoScope}
     ,<include refid="utility.now" />
)</insert>

<update id="updateAuthority" parameterType="cokr.xit.base.security.Authority">/* 권한 수정(authorityMapper.updateAuthority) */
UPDATE TB_AUTHORITY SET
       AUTH_NM = #{name}
     , DSCRP = #{description}
     , INF_SCP = #{infoScope}
     , USER_INF_SCP = #{userInfoScope}
 WHERE AUTH_ID = #{id}</update>

<delete id="removeAuthorities" parameterType="map">/* 권한 삭제(authorityMapper.removeAuthorities) */
DELETE FROM TB_AUTHORITY
 WHERE AUTH_ID IN (<foreach collection="authIDs" item="authID" separator=",">#{authID}</foreach>)</delete>

<select id="getActionGroupList" parameterType="map" resultType="dataobject">/* 권한-기능그룹 가져오기(authorityMapper.getActionGroups) */
<include refid="utility.paging-prefix" />
SELECT *
  FROM TB_AUTH_ACTION
<if test="authIDs != null">WHERE AUTH_ID IN (<foreach collection="authIDs" item="authID" separator=",">#{authID}</foreach>)</if>
 ORDER BY AUTH_ID, GRP_ID
<include refid="utility.paging-suffix" /></select>

<insert id="addActionGroups" parameterType="map">/* 권한-기능그룹 추가(authorityMapper.addActionGroups) */
INSERT INTO TB_AUTH_ACTION (AUTH_ID, GRP_ID, REG_DT)
SELECT AUTH_ID, GRP_ID,<include refid="utility.now" />
  FROM (<foreach collection="groupIDs" item="groupID" separator=" UNION">
       SELECT #{authID} AUTH_ID, #{groupID} GRP_ID FROM DUAL</foreach>
       ) A
 WHERE NOT EXISTS (
	SELECT AUTH_ID, GRP_ID
	  FROM TB_AUTH_ACTION B
	 WHERE B.AUTH_ID = A.AUTH_ID
	   AND B.GRP_ID = A.GRP_ID
 )</insert>

<delete id="removeActionGroups" parameterType="map">/* 권한-기능그룹 삭제(authorityMapper.removeActionGroups) */
DELETE FROM TB_AUTH_ACTION
<where>
<if test="authIDs != null">AND AUTH_ID IN (<foreach collection="authIDs" item="authID" separator=",">#{authID}</foreach>)</if>
<if test="groupIDs != null">AND GRP_ID IN (<foreach collection="groupIDs" item="groupID" separator=",">#{groupID}</foreach>)</if>
</where>
</delete>

<select id="getActionList" resultType="dataobject">/* 권한-기능 가져오기(authorityMapper.getActionList) */
SELECT A.AUTH_ID
     , A.GRP_ID
     , C.ACTION
  FROM TB_AUTH_ACTION A
     , TB_ACTION_GRP B
	 , TB_GRP_ACTION C
 WHERE A.GRP_ID = B.GRP_ID  
   AND B.GRP_ID = C.GRP_ID
 ORDER BY A.AUTH_ID, A.GRP_ID, C.ACTION</select>

<sql id="selectAuthUser">
<include refid="utility.paging-prefix" />
SELECT A.*, USER_ACNT
  FROM TB_AUTH_USER A
     , TB_USER B
<where>
<if test="authIDs != null">AND AUTH_ID IN (<foreach collection="authIDs" item="authID" separator=",">#{authID}</foreach>)</if>
<if test="userIDs != null">AND A.USER_ID IN (<foreach collection="userIDs" item="userID" separator=",">#{userID}</foreach>)</if>
   AND A.USER_ID = B.USER_ID
</where>
<include refid="utility.orderBy"/>
<include refid="utility.paging-suffix" /></sql>

<select id="getUserList" parameterType="map" resultType="dataobject">/* 권한-사용자 가져오기(authorityMapper.getUserList) */
<include refid="selectAuthUser" /></select>

<select id="getUserAuths" parameterType="map" resultType="dataobject">/* 사용자-권한 가져오기(authorityMapper.getUserAuths) */
<include refid="selectAuthUser" /></select>

<insert id="addUsers" parameterType="map">/* 권한-사용자 추가(authorityMapper.addUsers) */
INSERT INTO TB_AUTH_USER (AUTH_ID, USER_ID, REG_DT)
SELECT AUTH_ID, USER_ID,<include refid="utility.now" />
  FROM (<foreach collection="userIDs" item="userID" separator="UNION">
       SELECT #{authID} AUTH_ID, #{userID} USER_ID FROM DUAL</foreach>
       ) A
 WHERE NOT EXISTS (
    SELECT AUTH_ID, USER_ID
      FROM TB_AUTH_USER B
     WHERE B.AUTH_ID = A.AUTH_ID
       AND B.USER_ID = A.USER_ID
 )</insert>

<delete id="removeUsers" parameterType="map">/* 권한-사용자 삭제(authorityMapper.removeUsers) */
DELETE FROM TB_AUTH_USER
<where>
<if test="authIDs != null">AND AUTH_ID IN (<foreach collection="authIDs" item="authID" separator=",">#{authID}</foreach>)</if>
<if test="userIDs != null">AND USER_ID IN (<foreach collection="userIDs" item="userID" separator=",">#{userID}</foreach>)</if>
</where>
</delete>

</mapper>