<?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.permission.dao.IPermissionDao">

    <cache flushInterval="3600000"/>

    <resultMap id="permissionDTO" type="ink.wgink.pojo.dtos.permission.PermissionDTO">
        <id column="permission_id" property="permissionId"/>
        <result column="permission_title" property="permissionTitle"/>
        <result column="permission_type" property="permissionType"/>
        <result column="permission_url" property="permissionUrl"/>
        <result column="api_tag" property="apiTag"/>
        <result column="system_tag" property="systemTag"/>
        <result column="is_public" property="isPublic"/>
    </resultMap>

    <!-- 建表 -->
    <update id="createTable">
        CREATE TABLE IF NOT EXISTS `sys_permission` (
            `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '自增主键',
            `permission_id` char(36) NOT NULL COMMENT '主键',
            `permission_title` varchar(255) DEFAULT NULL COMMENT '权限标题',
            `permission_type` varchar(255) DEFAULT NULL COMMENT '权限类型',
            `permission_url` varchar(255) DEFAULT NULL COMMENT '权限路径',
            `api_tag` varchar(255) DEFAULT NULL COMMENT 'API标识（Controller名称，用于对API进行分组）',
            `system_tag` varchar(255) DEFAULT NULL COMMENT '系统标识（系统路径），NULL表示本系统',
            `is_public` int(11) DEFAULT NULL COMMENT '是否公共',
            `creator` char(36) DEFAULT NULL,
            `gmt_create` datetime DEFAULT NULL,
            `modifier` char(36) DEFAULT NULL,
            `gmt_modified` datetime DEFAULT NULL,
            `is_delete` int(1) DEFAULT '0',
            PRIMARY KEY (`id`,`permission_id`),
            UNIQUE KEY `permission_id` (`permission_id`),
            KEY `system_tag` (`system_tag`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    </update>

    <!-- 新增权限 -->
    <insert id="save" parameterType="map" flushCache="true">
        INSERT INTO sys_permission(
            permission_id,
            permission_title,
            permission_type,
            permission_url,
            api_tag,
            system_tag,
            is_public,
            creator,
            gmt_create,
            modifier,
            gmt_modified,
            is_delete
        ) VALUES(
            #{permissionId},
            #{permissionTitle},
            #{permissionType},
            #{permissionUrl},
             #{apiTag},
            #{systemTag},
            #{isPublic},
            #{creator},
            #{gmtCreate},
            #{modifier},
            #{gmtModified},
            #{isDelete}
        )
    </insert>

    <!-- 删除权限 -->
    <update id="remove" parameterType="map" flushCache="true">
        UPDATE
            sys_permission
        SET
            is_delete = 1,
            modifier = #{modifier},
            gmt_modified = #{gmtModified}
        WHERE
            permission_id IN
        <foreach collection="permissionIds" index="index" open="(" separator="," close=")">
            #{permissionIds[${index}]}
        </foreach>
    </update>

    <!-- 删除权限（物理） -->
    <update id="delete" parameterType="map" flushCache="true">
        DELETE FROM
            sys_permission
        WHERE
            permission_id IN
        <foreach collection="permissionIds" index="index" open="(" separator="," close=")">
            #{permissionIds[${index}]}
        </foreach>
    </update>

    <!-- 修改权限 -->
    <update id="update" parameterType="map" flushCache="true">
        UPDATE
            sys_permission
        SET
        <if test="permissionTitle != null and permissionTitle != ''">
            permission_title = #{permissionTitle},
        </if>
        <if test="permissionType != null and permissionType != ''">
            permission_type = #{permissionType},
        </if>
        <if test="permissionUrl != null and permissionUrl != ''">
            permission_url = #{permissionUrl},
        </if>
        <if test="apiTag != null and apiTag != ''">
            api_tag = #{apiTag},
        </if>
        <if test="systemTag != null and systemTag != ''">
            system_tag = #{systemTag},
        </if>
        <if test="isPublic != null">
            is_public = #{isPublic},
        </if>
            modifier = #{modifier},
            gmt_modified = #{gmtModified}
        WHERE
            permission_id = #{permissionId}
    </update>

    <!-- 权限详情 -->
    <select id="get" parameterType="map" resultMap="permissionDTO" useCache="false">
        SELECT
            t1.permission_title,
            t1.permission_type,
            t1.permission_url,
            t1.api_tag,
            t1.system_tag,
            t1.is_public,
            t1.permission_id
        FROM
            sys_permission t1
        WHERE
            t1.is_delete = 0
        <if test="permissionId != null and permissionId != ''">
        AND
            t1.permission_id = #{permissionId}
        </if>
    </select>

    <!-- 权限列表 -->
    <select id="list" parameterType="map" resultMap="permissionDTO" useCache="true">
        SELECT
            t1.permission_title,
            t1.permission_type,
            t1.permission_url,
            t1.api_tag,
            t1.system_tag,
            t1.is_public,
            t1.permission_id
        FROM
            sys_permission t1
        WHERE
            t1.is_delete = 0
        <if test="keywords != null and keywords != ''">
        AND (
            t1.permission_title LIKE CONCAT('%', #{keywords}, '%')
            OR
            t1.permission_url LIKE CONCAT('%', #{keywords}, '%')
            OR
            t1.api_tag LIKE CONCAT('%', #{keywords}, '%')
            OR
            t1.system_tag LIKE CONCAT('%', #{keywords}, '%')
        )
        </if>
        <if test="permissionType != null and permissionType != ''">
        AND
            t1.permission_type = #{permissionType}
        </if>
        <if test="systemTag != null and systemTag != ''">
        AND
            t1.system_tag = #{systemTag}
        </if>
        <if test="apiTag != null and apiTag != ''">
        AND
            t1.api_tag = #{apiTag}
        </if>
        <if test="permissionUrl != null and permissionUrl != ''">
        AND
            t1.permission_url = #{permissionUrl}
        </if>
        <if test="isPublic != null">
        AND
            t1.is_public = #{isPublic}
        </if>
        <if test="startTime != null and startTime != ''">
        AND
            LEFT(t1.gmt_create, 10) <![CDATA[ >= ]]> #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
        AND
            LEFT(t1.gmt_create, 10) <![CDATA[ <= ]]> #{endTime}
        </if>
        <if test="permissionIds != null and permissionIds.size > 0">
        AND
            t1.permission_id IN
            <foreach collection="permissionIds" index="index" open="(" separator="," close=")">
            #{permissionIds[${index}]}
            </foreach>
        </if>
        ORDER BY
            t1.permission_url
    </select>

    <!-- 权限统计  -->
    <select id="count" parameterType="map" resultType="Integer" useCache="true">
        SELECT
            COUNT(*)
        FROM
            sys_permission t1
        WHERE
            t1.is_delete = 0
    </select>

</mapper>