<?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.auth.mapper.UserMapper">
    <select id="findByName" parameterType="string"
            resultType="com.yida.data.common.core.entity.system.SystemUser">
        SELECT u.user_id userId,
        u.username,
        u.nickname,
        u.email,
        u.mobile,
        u.password,
        u.status,
        u.create_time createTime,
        u.ssex sex,
        u.dept_id deptId,
        u.last_login_time lastLoginTime,
        u.modify_time modifyTime,
        u.description,
        u.avatar,
        d.dept_type deptType,
        d.dept_name deptName,
        u.agent_id agentId,
        GROUP_CONCAT(r.role_id) roleId,
        GROUP_CONCAT(r.ROLE_NAME) roleName,
        GROUP_CONCAT( r.ROLE_PERMS ) rolePerms,
        u.create_id,
        u.create_dept_id,
        u.main_dept_id,
        u.open_id
        FROM t_user u
        LEFT JOIN t_dept d ON (u.dept_id = d.dept_id)
        LEFT JOIN t_user_role ur ON (u.user_id = ur.user_id)
        LEFT JOIN t_role r ON r.role_id = ur.role_id
        WHERE u.username = #{username}
        group by u.username, u.user_id, u.email, u.mobile, u.password, u.status, u.create_time, u.ssex,
        u.dept_id
        , u.last_login_time, u.modify_time, u.description, u.avatar
    </select>

    <select id="findUserDataPermissions" parameterType="long"
            resultType="com.yida.data.common.core.entity.system.UserDataPermission">
        select user_id userId, dept_id deptId
        from t_user_data_permission
        where user_id = #{userId}
    </select>

    <select id="listUserByPerms" resultType="com.yida.data.common.core.entity.system.SystemUser">
        select a.*
        from t_user a left join t_user_role b on a.USER_ID=b.USER_ID
        left join t_role c on b.ROLE_ID=c.ROLE_ID
        where c.ROLE_PERMS=#{perms}
        <if test="deptId != null">
            and a.DEPT_ID=#{deptId}
        </if>
        order by a.USER_ID
    </select>
    
    <!-- 查询主角色code字符串 -->
	<select id="findMainRole"
            resultType="java.lang.String">
        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 = #{mainDeptId}
    </select>
</mapper>
