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

    <cache flushInterval="3600000"/>

    <resultMap id="rolePermissionDTO" type="ink.wgink.pojo.dtos.role.RolePermissionDTO">
        <result column="role_id" property="roleId"/>
        <result column="api_tag" property="apiTag"/>
        <result column="permission_id" property="permissionId"/>
        <result column="permission_type" property="permissionType"/>
    </resultMap>

    <!-- 建表 -->
    <update id="createTable">
        CREATE TABLE IF NOT EXISTS `sys_role_permission` (
            `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
            `role_id` char(36) DEFAULT NULL COMMENT '角色ID',
            `api_tag` varchar(255) DEFAULT NULL COMMENT '接口标识',
            `permission_id` char(36) DEFAULT NULL COMMENT '权限ID',
            `permission_type` char(36) DEFAULT NULL COMMENT '权限类型',
            PRIMARY KEY (`id`),
            KEY `role_id` (`role_id`),
            KEY `permission_type` (`permission_type`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    </update>

    <!-- 新增权限角色 -->
    <insert id="save" parameterType="map" flushCache="true">
        INSERT INTO sys_role_permission (
            role_id,
            api_tag,
            permission_id,
            permission_type
        ) VALUES(
            #{roleId},
            #{apiTag},
            #{permissionId},
            #{permissionType}
        )
    </insert>

    <!-- 删除 -->
    <delete id="delete" parameterType="map" flushCache="true">
        DELETE FROM
            sys_role_permission
        WHERE
            role_id = #{roleId}
        <if test="permissionType != null and permissionType != ''">
        AND
            permission_type = #{permissionType}
        </if>
        <if test="permissionIds != null and permissionIds.size > 0">
        AND
            permission_id IN
            <foreach collection="permissionIds" index="index" open="(" separator="," close=")">
                #{permissionType[${index}]}
            </foreach>
        </if>
    </delete>

    <!-- 权限列表 -->
    <select id="list" parameterType="map" resultMap="rolePermissionDTO" useCache="true">
        SELECT
            t1.role_id,
            t1.api_tag,
            t1.permission_id,
            t1.permission_type
        FROM
            sys_role_permission t1
        WHERE
            1 = 1
        <if test="roleId != null and roleId != ''">
        AND
            t1.role_id = #{roleId}
        </if>
        <if test="permissionType != null and permissionType != ''">
        AND
            t1.permission_type = #{permissionType}
        </if>
        <if test="permissionIds != null and permissionIds.size > 0">
        AND
            t1.permission_id IN
            <foreach collection="permissionIds" index="index" open="(" separator="," close=")">
                #{permissionType[${index}]}
            </foreach>
        </if>
    </select>

    <!-- 权限ID列表 -->
    <select id="listPermissionId" parameterType="map" resultType="java.lang.String">
        SELECT
            t1.permission_id
        FROM
            sys_role_permission t1
        WHERE
            1 = 1
        <if test="roleId != null and roleId != ''">
        AND
            t1.role_id = #{roleId}
        </if>
        <if test="permissionType != null and permissionType != ''">
        AND
            t1.permission_type = #{permissionType}
        </if>
        <if test="permissionIds != null and permissionIds.size > 0">
        AND
            t1.permission_id IN
            <foreach collection="permissionIds" index="index" open="(" separator="," close=")">
                #{permissionType[${index}]}
            </foreach>
        </if>
    </select>

</mapper>