<?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.xmomen.module.account.mapper.UserMapper" >

	<resultMap type="com.xmomen.module.account.model.User" id="UserMap">
		 	<id column="id" property="id"/>
			<collection property="userGroups" ofType="com.xmomen.module.account.model.UserGroup" column="id" select="queryUserGroupsList"></collection>
    </resultMap>
    <!--    查询消息    -->
    <select id="getUsers" resultMap="UserMap" parameterType="java.util.HashMap">
        SELECT
        u.id,
        u.username,
        u.email,
        u.locked,
        u.realname,
        u.qq,
        u.phone_number,
        u.age,
        u.sex,
        u.office_tel,
        so.name as organization,
        so.id as organization_id
        FROM
        sys_users u
        left join sys_user_organization suo on u.id = suo.user_id
        left join sys_organization so on suo.organization_id = so.id
        <where>
              <if test="keyword">
                  AND u.username LIKE CONCAT('%', #{keyword}, '%') or u.email like CONCAT('%', #{keyword}, '%')
              </if>
              <if test="id">
                  AND m.ID = #{id}
              </if>
              <if test="organizationId">
              	suo.organization_id = #{organizationId}
              </if>
          </where>
    </select>


	<select id="queryUserGroupsList" resultType="com.xmomen.module.account.model.UserGroup" parameterType="Integer">
		select  
			sr.DESCRIPTION as userGorup,
			sr.id as user_Group_id
		from 
			sys_roles sr 
        left join 
        	sys_users_roles sur on sur.role_id = sr.id
		where 
			sur.user_id = #{id}			
	</select>
	
    <!--    查询关联关系表    -->
    <select id="findUsersByRolesId" resultType="com.xmomen.module.account.web.controller.vo.UserGroupRelation" parameterType="java.util.HashMap">
        SELECT
        u.ID as userId,
        u.realname as USERNAME,
        u.EMAIL
        FROM
        sys_users u
        <if test="id and chose">
            LEFT JOIN sys_users_roles ur
            ON u.ID = ur.USER_ID
        </if>
        <where>
            <if test="id and chose">
                AND ur.ROLE_ID = #{id}
            </if>
            <if test="!chose">
                AND u.ID NOT IN
                (SELECT
                u.ID AS user_id
                FROM
                sys_users u
                LEFT JOIN sys_users_roles ur
                ON u.ID = ur.USER_ID
                WHERE ur.ROLE_ID= #{id})
            </if>
        </where>
    </select>

    <!--    查询权限关系表    -->
    <select id="findPermissionByRole" resultType="com.xmomen.module.account.web.controller.vo.GroupPermissionRelation" parameterType="java.util.HashMap">
        select p.ID as permissionId,
              p.PERMISSION as permissionCode,
              p.DESCRIPTION as permissionDescription
        from sys_permissions p
        <if test="id and chose">
            left join sys_roles_permissions rp
              on rp.PERMISSION_ID=p.ID
        </if>
        <where>
            <if test="id and chose">
                AND rp.ROLE_ID = #{id}
            </if>
            <if test="!chose">
                AND p.ID NOT IN
                (SELECT
                r.ID AS permission_id
                FROM
                sys_permissions r
                LEFT JOIN sys_roles_permissions rp
                ON r.ID = rp.PERMISSION_ID
                WHERE rp.ROLE_ID= #{id})
            </if>
        </where>
    </select>
</mapper>