<?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.sqx.modules.sys.dao.SysUserDao">
	<!-- 查询用户的所有权限 -->
	<select id="queryAllPerms" resultType="string">
		select m.perms from sys_user_role ur 
			LEFT JOIN sys_role_menu rm on ur.role_id = rm.role_id 
			LEFT JOIN sys_menu m on rm.menu_id = m.menu_id 
		where ur.user_id = #{userId}
	</select>
	
	<!-- 查询用户的所有菜单ID --> 
	<select id="queryAllMenuId" resultType="long">
		select distinct rm.menu_id from sys_user_role ur 
			LEFT JOIN sys_role_menu rm on ur.role_id = rm.role_id 
		where ur.user_id = #{userId}
	</select>
	
	<select id="queryByUserName" resultType="com.sqx.modules.sys.entity.SysUserEntity">
		select * from sys_user where username = #{username}
	</select>

	<select id="getAgentList" resultType="com.sqx.modules.sys.vo.SysUserEntityVo">
        select *
        from (SELECT su.*,
        c.`city_name` AS city,
        (SELECT city_name FROM city_info cn WHERE cn.id = c.`parent_id`) AS province,
        (select money from agent_user_money a where a.sys_user_id = su.user_id) as money
        FROM sys_user su
        LEFT JOIN city_info c
        ON su.user_id = c.agent_user_id,
        sys_user_role sur
        WHERE su.user_id = sur.user_id
        <if test="sysUserEntityVo.isAgent!=null and sysUserEntityVo.isAgent==1">
            AND sur.role_id =5
        </if>
        <if test="sysUserEntityVo.username!=null and sysUserEntityVo.username!=''">
            AND su.username like concat("%",#{sysUserEntityVo.username},"%")
        </if>
        <if test="sysUserEntityVo.isAgent!=null and sysUserEntityVo.isAgent==0">
            AND (sur.role_id =5 AND su.user_id NOT IN (SELECT agent_user_id FROM city_info WHERE agent_user_id IS NOT
            NULL))
        </if>
        ) t1
        <where>
            <if test="sysUserEntityVo.province!=null and sysUserEntityVo.province!=''">
                and t1.province = #{sysUserEntityVo.province}
            </if>
            <if test="sysUserEntityVo.city!=null and sysUserEntityVo.city!=''">
                and (t1.city = #{sysUserEntityVo.city} or t1.city = #{sysUserEntityVo.province})
            </if>
            <if test="sysUserEntityVo.city!=null and sysUserEntityVo.city!=''">
                and t1.username like concat("%",#{sysUserEntityVo.username},"%")
            </if>

        </where>
        ORDER BY t1.create_time DESC
    </select>


</mapper>