<?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.yida.data.system.mapper.RoleMapper">
    <resultMap id="roleMap" type="com.yida.data.common.core.entity.system.Role">
        <!--
          WARNING - @mbg.generated
        -->
        <result column="ROLE_ID" jdbcType="DECIMAL" property="roleId"/>
        <result column="ROLE_NAME" jdbcType="VARCHAR" property="roleName"/>
        <result column="ROLE_PERMS" jdbcType="VARCHAR" property="rolePerms"/>
        <result column="REMARK" jdbcType="VARCHAR" property="remark"/>
        <result column="CREATE_TIME" jdbcType="TIMESTAMP" property="createDate"/>
        <result column="MODIFY_TIME" jdbcType="TIMESTAMP" property="updateDate"/>
    </resultMap>

    <select id="findUserRole" resultMap="roleMap">
        SELECT
        r.*
        FROM
        t_role r
        LEFT JOIN t_user_role ur ON (r.role_id = ur.role_id)
        LEFT JOIN t_user u ON (u.user_id = ur.user_id)
        WHERE
        u.username = #{username}
    </select>

    <select id="findRolePage" parameterType="com.yida.data.common.core.entity.system.Role"
            resultType="com.yida.data.common.core.entity.system.Role">
        SELECT
        r.role_id roleId,
        r.role_name roleName,
        r.remark,
        r.create_time createDate,
        r.modify_time updateDate,
        GROUP_CONCAT( rm.menu_id ) menuIds
        FROM
        t_role r
        LEFT JOIN t_role_menu rm ON ( r.role_id = rm.role_id )
        <where>
            <if test="role.roleName != null and role.roleName != ''">
                AND r.role_name like CONCAT('%',#{role.roleName},'%')
            </if>
        </where>
        GROUP BY
        r.role_id
    </select>

    <select id="listMenuByRole" resultType="com.yida.data.common.core.entity.system.Menu">
        select tm.*
        from t_menu tm inner join t_role_menu trm on tm.MENU_ID = trm.MENU_ID
        where trm.ROLE_ID in
        <foreach collection="roleIds" open="(" close=")" separator="," item="item">
            #{item}
        </foreach>
        order by tm.ORDER_NUM
    </select>

    <select id="listMenuByRoleName" resultType="com.yida.data.common.core.entity.system.Menu">
        select tm.*
        from t_menu tm inner join t_role_menu trm on tm.MENU_ID = trm.MENU_ID
        left join t_role tr on trm.ROLE_ID=tr.ROLE_ID
        where tr.ROLE_NAME in
        <foreach collection="roleNames" open="(" close=")" separator="," item="item">
            #{item}
        </foreach>
        order by tm.ORDER_NUM
    </select>
    
    <!-- 查询自己创建的角色+当前登陆账号部门以下的角色 -->
    <select id="findRoleCheckPage" resultType="com.yida.data.common.core.entity.system.Role">
        SELECT
        r.role_id roleId,
        r.role_name roleName,
        r.remark,
        r.create_time createDate,
        r.modify_time updateDate,
        GROUP_CONCAT( rm.menu_id ) menuIds
        FROM
        (
        SELECT DISTINCT
        *
        FROM
        t_role
        WHERE
        create_id = #{currentUser.userId}
        <if test='childIdList != null and childIdList.size > 0 '>
            or create_dept_id IN
            <foreach collection="childIdList" open="(" close=")" separator="," item="deptId">
                #{deptId}
            </foreach>
        </if>
        ) r
        LEFT JOIN t_role_menu rm ON ( r.role_id = rm.role_id )
        <where>
            <if test="role.roleName != null and role.roleName != ''">
                AND r.role_name like CONCAT('%',#{role.roleName},'%')
            </if>
        </where>
        GROUP BY
        r.role_id
    </select>
    
    <!-- 获取主角色信息 -->
    <select id="findMainRole"
            resultType="com.yida.data.common.core.entity.system.Role">
        SELECT
            r.ROLE_PERMS
        FROM
            t_user u
        INNER JOIN t_user_role ur ON u.USER_ID = ur.USER_ID
        INNER JOIN t_role r ON ur.ROLE_ID = r.ROLE_ID
        WHERE
         u.DEPT_ID = ( SELECT main_dept_id FROM t_user WHERE USERNAME = #{username} )
    </select>
</mapper>