<?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.ActionGroupMapper">

<resultMap id="groupRow" type="cokr.xit.base.security.access.ActionGroup">
	<result property="id" column="GRP_ID"/>
	<result property="name" column="GRP_NM"/>
	<result property="description" column="DSCRP"/>
	<result property="createdAt" column="REG_DT"/>
</resultMap>

<sql id="selectGroups"><include refid="utility.paging-prefix" />
SELECT A.*
  FROM TB_ACTION_GRP A
<where>
<if test="groupIDs != null">AND GRP_ID IN (<foreach collection="groupIDs" item="groupID" separator=",">#{groupID}</foreach>)</if>
<if test="by != null and term != null">AND ${by} LIKE CONCAT('%', #{term}, '%')</if>
</where>
<include refid="utility.paging-suffix" /></sql>

<select id="getGroupList" parameterType="map" resultType="dataobject">/* 기능그룹 목록 조회(actionGroupMapper.getGroupList) */
<include refid="selectGroups" /></select>

<select id="getGroups" parameterType="map" resultMap="groupRow">/* 기능그룹 가져오기(actionGroupMapper.getGroups) */
<include refid="selectGroups" /></select>

<insert id="insertGroup" parameterType="cokr.xit.base.security.access.ActionGroup">/* 기능그룹 등록(actionGroupMapper.insertGroup) */
INSERT INTO TB_ACTION_GRP (
       GRP_ID
     , GRP_NM
     , DSCRP
     , REG_DT
) VALUES (
       #{id}
     , #{name}
     , #{description}
     ,<include refid="utility.now" />
)</insert>

<update id="updateGroup" parameterType="cokr.xit.base.security.access.ActionGroup">/* 기능그룹 수정(actionGroupMapper.updateGroup) */
UPDATE TB_ACTION_GRP SET
       GRP_NM = #{name}
     , DSCRP = #{description}
 WHERE GRP_ID = #{id}</update>

<delete id="removeGroups" parameterType="map">/* 기능그룹 삭제(actionGroupMapper.removeGroups) */
DELETE FROM TB_ACTION_GRP
 WHERE GRP_ID IN (<foreach collection="groupIDs" item="groupID" separator=",">#{groupID}</foreach>)</delete>

<select id="getActionList" parameterType="map" resultType="dataobject">/* 그룹별 기능 가져오기(actionGroupMapper.getActionList) */
SELECT *
  FROM TB_GRP_ACTION
<if test="groupIDs != null">WHERE GRP_ID IN (<foreach collection="groupIDs" item="groupID" separator=",">#{groupID}</foreach>)</if>
 ORDER BY GRP_ID, ACTION
</select>

<insert id="addActions" parameterType="map">/* 그룹별 기능 추가(actionGroupMapper.addActions) */
INSERT INTO TB_GRP_ACTION (GRP_ID, ACTION, REG_DT, RGTR)
SELECT GRP_ID, ACTION,<include refid="utility.now" />, #{currentUser.id}
  FROM (<foreach collection="actions" item="action" separator="UNION">
       SELECT #{groupID} GRP_ID, #{action} ACTION FROM DUAL</foreach>
       ) A
 WHERE NOT EXISTS (
	SELECT GRP_ID, ACTION
	  FROM TB_GRP_ACTION B
	 WHERE B.GRP_ID = A.GRP_ID
	   AND B.ACTION = A.ACTION
 )</insert>

<delete id="removeActions" parameterType="map">/* 그룹별 기능 삭제(actionGroupMapper.removeActions) */
DELETE FROM TB_GRP_ACTION
<where>
<if test="groupIDs != null">AND GRP_ID IN (<foreach collection="groupIDs" item="groupID" separator=",">#{groupID}</foreach>)</if>
<if test="actions != null">AND ACTION IN (<foreach collection="actions" item="action" separator=",">#{action}</foreach>)</if>
</where>
</delete>

</mapper>