<?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="com.lantone.security.mapper.RoleMapper">
    <select id="getRolePage" resultType="com.lantone.security.dto.GetRolePageDTO">
        SELECT
            r.*,
            IF (
            r.`status` = 1,
            "启用",
            "禁用"
            ) statusName
        FROM
            sys_role r
        WHERE
            r.is_deleted = 'N'
        AND (r.remark != 'ManagementRole' OR r.remark IS NULL)
        AND r.creator = #{getRolePageVO.creator}
        <if test="getRolePageVO.name!=null and getRolePageVO.name!=''">
            AND r.`name` LIKE concat('%',#{getRolePageVO.name},'%')
        </if>
        <if test="getRolePageVO.status!=null and getRolePageVO.status!=''">
            AND r.`status` = #{getRolePageVO.status}
        </if>
    </select>
    <select id="getCreateRoles" resultType="com.lantone.security.dto.GetCreateRoleDTO">
        SELECT DISTINCT
            r.id,
            r.`name`
        FROM
            sys_role r,
            sys_role_software_menu rsm,
         sys_software_menu sm
        WHERE
            r.is_deleted = 'N'
        AND rsm.is_deleted = 'N'
        AND sm.is_deleted = 'N'
        AND r.`status` = '1'
        AND rsm.role_id = r.id
        AND rsm.software_menu_id = sm.id
        <if test="softwareId!=null">
            AND sm.software_id = #{softwareId}
        </if>
        AND r.creator = #{creator}
        AND (r.remark != 'ManagementRole' OR r.remark IS NULL)
    </select>
    <select id="getDataAuthRole" resultType="com.lantone.security.dto.UserRoleDTO">
        SELECT DISTINCT
            r.id roleId,
            r.`name` roleName
        FROM
            sys_role r,
            sys_role_software_data_auth rsda,
            sys_software_data_auth sda,
            sys_data_auth_detail detail
        WHERE
            r.is_deleted = 'N'
        AND rsda.is_deleted = 'N'
        AND sda.is_deleted = 'N'
        AND detail.is_deleted = 'N'
        AND r.`status` = 1
        AND r.id = rsda.role_id
        AND rsda.software_data_auth_id = sda.id
        AND sda.id = detail.software_data_auth_id
        AND r.creator = #{creator}
        <if test="dataTypes !=null and dataTypes.size!=0">
            AND detail.data_type IN
            <foreach collection="dataTypes" item="id" open="(" close=")" separator=",">
                #{id}
            </foreach>
        </if>
        <if test="roles !=null and roles.size!=0">
            AND r.id IN
            <foreach collection="roles" item="id" open="(" close=")" separator=",">
                #{id}
            </foreach>
        </if>
    </select>
    <select id="getCount" resultType="java.lang.Integer">
        SELECT
            count(id) num
        FROM
            sys_role r
        WHERE
        r.is_deleted = #{delete}
        <if test="roles !=null and roles.size!=0">
            AND r.id IN
            <foreach collection="roles" item="id" open="(" close=")" separator=",">
                #{id}
            </foreach>
        </if>
		AND r.`status` = #{status}
    </select>
</mapper>