<?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="ink.wgink.service.role.dao.IRoleMenuDao">

    <cache flushInterval="3600000"/>

    <resultMap id="roleMenuDTO" type="ink.wgink.pojo.dtos.role.RoleMenuDTO">
        <result column="role_id" property="roleId"/>
        <result column="menu_id" property="menuId"/>
    </resultMap>

    <!-- 建表 -->
    <update id="createTable">
        CREATE TABLE IF NOT EXISTS `sys_role_menu` (
            `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
            `role_id` char(36) DEFAULT NULL COMMENT '角色ID',
            `menu_id` char(36) DEFAULT NULL COMMENT '菜单ID',
            PRIMARY KEY (`id`),
            KEY `role_id` (`role_id`) USING BTREE
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    </update>

    <!-- 新增 -->
    <insert id="save" parameterType="map" flushCache="true">
        INSERT INTO sys_role_menu(
            role_id,
            menu_id
        ) VALUES(
            #{roleId},
            #{menuId}
        )
    </insert>

    <!-- 删除 -->
    <delete id="delete" parameterType="map" flushCache="true">
        DELETE FROM
            sys_role_menu
        WHERE
            role_id = #{roleId}
    </delete>

    <!-- 数据权限列表 -->
    <select id="list" parameterType="map" resultMap="roleMenuDTO" useCache="true">
        SELECT
            role_id,
            menu_id
        FROM
            sys_role_menu
        WHERE
            1 = 1
        <if test="roleId != null and roleId != ''">
        AND
            role_id = #{roleId}
        </if>
    </select>

    <!-- 菜单ID列表 -->
    <select id="listMenuId" parameterType="map" resultType="java.lang.String">
        SELECT
            menu_id
        FROM
            sys_role_menu
        WHERE
        <if test="roleId != null and roleId != ''">
            role_id = #{roleId}
        </if>
        <if test="roleIds != null and roleIds.size > 0">
            role_id IN
            <foreach collection="roleIds" index="index" open="(" separator="," close=")">
                #{roleIds[${index}]}
            </foreach>
        </if>
    </select>

</mapper>