<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.caliverse.admin.domain.dao.admin.GroupMapper">
    <resultMap id="GroupsResultMap" type="com.caliverse.admin.domain.entity.Groups">
        <id property="groupId" column="id"/>
        <result property="rowNum" column="row_num"/>
        <result property="groupNm" column="name"/>
        <result property="desc" column="desc"/>
        <result property="memberCnt" column="member_cnt"/>
        <result property="createDt" column="create_dt"/>
        <result property="updateDt" column="update_dt"/>


    </resultMap>

    <resultMap id="AuthResultMap" type="com.caliverse.admin.domain.entity.Authority">
        <id property="id" column="id"/>
        <result property="authMenu" column="auth_menu"/>
        <result property="authName" column="auth_name"/>
    </resultMap>

    <!--권한 그룹 조회 by id -->
    <select id="getGroupInfo" resultType="map" parameterType="java.lang.Long">
        SELECT * FROM groups WHERE id = #{groupId} and deleted = 0
    </select>

    <!--권한 조회 by admin_id-->
    <select id="getGroupAuth" resultMap="AuthResultMap" parameterType="java.lang.Long">
        SELECT a.id, a.auth_menu, a.auth_name FROM authority a, group_auth b WHERE a.id = b.auth_id and b.group_id = #{groupId}
    </select>

    <!--권한 조회 by name-->
    <select id="findGroupName" resultType="java.lang.Integer" parameterType="java.lang.String">
        SELECT count(*) FROM groups WHERE deleted = 0 and name = #{groupNm}
    </select>

    <select id="getAllCnt" resultType="java.lang.Integer">
        SELECT count(*) FROM groups WHERE deleted = 0
    </select>

    <!--권한 리스트 조회-->
    <select id="getGroupList" resultMap="GroupsResultMap" parameterType="map">
        SELECT (@row_number:=@row_number + 1) AS row_num ,c.* FROM
        (
            SELECT
                a.*
                ,COUNT(b.id) AS member_cnt
                FROM groups a
                LEFT JOIN admin b ON a.id = b.group_id AND b.deleted = 0
                WHERE a.deleted = 0
                GROUP BY a.id
                ORDER BY a.create_dt
        ) c
        ,(SELECT @row_number:=0) AS t

        <if test="orderby != null and orderby != ''">
            ORDER BY row_num ${orderby}
        </if>
        <if test="pageSize != null and pageSize != ''">
            LIMIT ${pageSize} OFFSET ${offset}
        </if>

    </select>
    <select id="getGroupDetail" resultMap="AuthResultMap" parameterType="java.lang.String">
        SELECT a.*
        FROM authority a
           ,group_auth b
        WHERE a.id = b.auth_id
            and b.group_id = #{groupId}
    </select>

    <insert id="postAdminGroup" parameterType="com.caliverse.admin.domain.request.GroupRequest" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO groups (name,description,create_by) values (#{groupNm} ,#{description}, #{createBy})
    </insert>

    <delete id="deleteGroupAuth" parameterType="java.lang.String">
        DELETE FROM group_auth WHERE group_id = #{groupId}
    </delete>

    <insert id="insertGroupAuth" parameterType="map">
        INSERT INTO group_auth (group_id, auth_id) values (#{groupId}, #{authId})
    </insert>

    <update id="deleteGroup" parameterType="java.lang.Long">
        UPDATE groups set deleted = 1 WHERE  id = #{groupId}
    </update>
</mapper>
