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

    <cache flushInterval="3600000"/>

    <resultMap id="rolePO" type="ink.wgink.pojo.pos.RolePO">
        <id property="roleId" column="role_id"/>
        <result property="roleParentId" column="role_parent_id"/>
        <result property="roleParentName" column="role_parent_name"/>
        <result property="roleName" column="role_name"/>
        <result property="roleSummary" column="role_summary"/>
        <result property="roleCode" column="role_code"/>
        <result property="roleDataRight" column="role_data_right"/>
    </resultMap>

    <resultMap id="roleDTO" type="ink.wgink.pojo.dtos.role.RoleDTO">
        <id property="roleId" column="role_id"/>
        <result property="roleParentId" column="role_parent_id"/>
        <result property="roleParentName" column="role_parent_name"/>
        <result property="roleName" column="role_name"/>
        <result property="roleSummary" column="role_summary"/>
        <result property="roleCode" column="role_code"/>
        <result property="roleDataRight" column="role_data_right"/>
    </resultMap>

    <resultMap id="roleZTreeDTO" type="ink.wgink.pojo.dtos.ZTreeDTO">
        <id property="id" column="role_id"/>
        <result property="pId" column="role_parent_id"/>
        <result property="name" column="role_name"/>
    </resultMap>

    <!-- 建表 -->
    <update id="createTable">
        CREATE TABLE IF NOT EXISTS `sys_role` (
            `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
            `role_id` char(36) NOT NULL,
            `role_parent_id` char(36) DEFAULT '0',
            `role_name` varchar(255) DEFAULT NULL COMMENT '角色名称',
            `role_summary` varchar(255) DEFAULT NULL COMMENT '角色说明',
            `role_code` varchar(255) DEFAULT NULL COMMENT '角色编码',
            `role_data_right` varchar(20) DEFAULT 'self' COMMENT '数据权限',
            `creator` char(36) DEFAULT NULL,
            `gmt_create` datetime DEFAULT NULL,
            `modifier` char(36) DEFAULT NULL,
            `gmt_modified` datetime DEFAULT NULL,
            `is_delete` int(2) DEFAULT '0',
            PRIMARY KEY (`id`,`role_id`),
            UNIQUE KEY `role_id` (`role_id`)
        ) ENGINE=InnoDB AUTO_INCREMENT=30 DEFAULT CHARSET=utf8;
    </update>

    <!-- 新增角色 -->
    <insert id="save" parameterType="map" flushCache="true">
        INSERT INTO sys_role(
            role_id,
            role_parent_id,
            role_name,
            role_summary,
            role_code,
            creator,
            gmt_create,
            modifier,
            gmt_modified,
            is_delete
        ) VALUES(
            #{roleId},
            #{roleParentId},
            #{roleName},
            #{roleSummary},
            #{roleCode},
            #{creator},
            #{gmtCreate},
            #{modifier},
            #{gmtModified},
            #{isDelete}
        )
    </insert>

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

    <!-- 修改角色 -->
    <update id="update" parameterType="map" flushCache="true">
        UPDATE
            sys_role
        SET
        <if test="roleName != null and roleName != ''">
            role_name = #{roleName},
        </if>
        <if test="roleCode != null and roleCode != ''">
            role_code = #{roleCode},
        </if>
        <if test="roleSummary != null">
            role_summary = #{roleSummary},
        </if>
        <if test="roleDataRight != null and roleDataRight != ''">
            role_data_right = #{roleDataRight},
        </if>
            modifier = #{modifier},
            gmt_modified = #{gmtModified}
        WHERE
            role_id = #{roleId}
    </update>

    <!-- ztree列表 -->
    <select id="listZTree" parameterType="map" resultMap="roleZTreeDTO" useCache="false">
        SELECT
            *
        FROM
            sys_role
        WHERE
            is_delete = 0
        <if test="roleParentId != null and roleParentId != ''">
        AND
            role_parent_id = #{roleParentId}
        </if>
    </select>

    <!-- 角色列表 -->
    <select id="list" parameterType="map" resultMap="roleDTO" useCache="true">
        SELECT
            *
        FROM
            sys_role
        WHERE
            is_delete = 0
        <if test="roleIds != null and roleIds.size > 0">
        AND
            role_id IN
            <foreach collection="roleIds" index="index" open="(" separator="," close=")">
                #{roleIds[${index}]}
            </foreach>
        </if>
        <if test="roleParentId != null and roleParentId != ''">
        AND
            role_parent_id = #{roleParentId}
        </if>
        <if test="keywords != null and keywords != ''">
        AND
            role_name LIKE CONCAT('%', #{keywords}, '%')
        </if>
        <if test="startTime != null and startTime != ''">
        AND
            gmt_create <![CDATA[ >= ]]> #{startTime}
        </if>
        <if test="startTime != null and startTime != ''">
        AND
            gmt_create <![CDATA[ <= ]]> #{endTime}
        </if>
        <choose>
            <when test="sort != null and (sort == 'roleName' or sort == 'roleCode')">
                ORDER BY
                <if test="sort == 'roleName'">
                    role_name ${order}
                </if>
                <if test="sort == 'roleCode'">
                    role_code ${order}
                </if>
            </when>
        </choose>
    </select>

    <!-- 角色详情 -->
    <select id="get" parameterType="map" resultMap="roleDTO" useCache="false">
        SELECT
            t1.*,
            t2.role_name role_parent_name
        FROM
            sys_role t1
        LEFT JOIN
            sys_role t2
        ON
            t1.role_parent_id = t2.role_id
        AND
            t2.is_delete = 0
        WHERE
            t1.is_delete = 0
        <if test="roleId != null and roleId != ''">
        AND
            t1.role_id = #{roleId}
        </if>
    </select>

    <!-- 子节点数量 -->
    <select id="countByParentId" parameterType="String" resultType="Integer" useCache="false">
        SELECT
            COUNT(*)
        FROM
            sys_role
        WHERE
            is_delete = 0
        AND
            role_parent_id = #{_parameter}
    </select>

    <!-- 获取最后一个子角色，实际数据，包含已删除，方式编码重复 -->
    <select id="getLastByParentId" parameterType="String" resultMap="roleDTO" useCache="false">
        SELECT
            *
        FROM
            sys_role
        WHERE
            role_parent_id = #{_parameter}
        ORDER BY
            role_code desc
        LIMIT 0, 1
    </select>

    <!-- 统计角色 -->
    <select id="countRole" resultType="java.lang.Integer" useCache="true">
        SELECT
            COUNT(*)
        FROM
            sys_role
        WHERE
            is_delete = 0
    </select>

    <!-- 角色列表 -->
    <select id="listPO" parameterType="map" resultMap="rolePO">
        SELECT
            *
        FROM
            sys_role
        WHERE
            is_delete = 0
        <if test="roleParentId != null and roleParentId != ''">
        AND
            role_parent_id = #{roleParentId}
        </if>
        <if test="roleIds != null and roleIds.size > 0">
        AND
            role_id IN
            <foreach collection="roleIds" index="index" open="(" separator="," close=")">
                #{roleIds[${index}]}
            </foreach>
        </if>
    </select>

</mapper>