<?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="org.jeecg.modules.system.mapper.SysUserMapper">


    <select id="selectAllUser" resultType="org.jeecg.modules.system.entity.SysUser">
		select * from  sys_user
	</select>


    <!-- 根据用户名查询 -->
    <select id="getUserByName" resultType="org.jeecg.modules.system.entity.SysUser">
		select * from  sys_user  where username = #{username} and del_flag = '0'
	</select>

    <!-- 根据部门Id查询 -->
    <select id="getUserByDepId" resultType="org.jeecg.modules.system.entity.SysUser">

        select * from sys_user where del_flag = '0' and id in (select user_id from sys_user_depart where
        dep_id=#{departId})
        <if test="username!=null and username!=''">
            and username = #{username}
        </if>
    </select>

    <!-- 根据角色Id查询 -->
    <select id="getUserByRoleId" resultType="org.jeecg.modules.system.entity.SysUser">
        select * from sys_user where del_flag = '0' and id in (select user_id from sys_user_role where
        role_id=#{roleId})
        <if test="username!=null and username!=''">
            and username = #{username}
        </if>
    </select>

    <!--  修改用户部门code -->
    <update id="updateUserDepart">
		UPDATE sys_user SET org_code = #{orgCode} where username = #{username}
	</update>
    <!-- 根据手机号查询 -->
    <select id="getUserByPhone" resultType="org.jeecg.modules.system.entity.SysUser">
		select * from  sys_user  where phone = #{phone} and del_flag = '0'
	</select>

    <!-- 根据邮箱查询用户信息 -->
    <select id="getUserByEmail" resultType="org.jeecg.modules.system.entity.SysUser">
	select * from  sys_user  where email = #{email} and del_flag = '0'
	</select>


    <select id="queryRoleCode" parameterType="Object" resultType="java.util.Map">
SELECT
   role_code roleCode,
   role_name roleName,
   id roleId
   FROM
   sys_role
   WHERE
   id = (
   SELECT
   t2.role_id
   FROM
   sys_user t1
   LEFT JOIN sys_user_role t2 ON t1.id = t2.user_id
   WHERE
   t1.id = #{userId,jdbcType=VARCHAR}
   )
</select>


    <select id="getAllUser" resultType="cn.com.ctop.common.module.vo.SysUserVo">

      SELECT
      t1.id userId,
      t1.username userName,
      t1.realname realName,
      (SELECT role_name FROM sys_role WHERE id = t2.role_id ) roleName
    FROM sys_user t1
    LEFT JOIN sys_user_role t2 on t1.id = t2.user_id;


</select>


    <select id="getUserListByCompany" resultType="cn.com.ctop.common.module.vo.SysUserVo">
   SELECT
      t2.id userId,
      t2.username userName,
      t2.realname realName,
 (SELECT role_name FROM sys_role WHERE id = t3.role_id ) roleName
    FROM user_company t1 LEFT JOIN
    sys_user t2 ON
    t1.user_id = t2.id
    LEFT JOIN sys_user_role t3 on t1.user_id = t3.user_id
    WHERE t1.company_id = #{companyId}
    </select>
    <select id="getAllSale" resultType="cn.com.ctop.common.module.vo.SysUserVo">
        SELECT
            t1.id userId,
            t1.username userName,
            t1.realname realName,
            t3.role_name as  roleName
        FROM sys_user t1
        LEFT JOIN sys_user_role t2 on t1.id = t2.user_id
        left join sys_role t3 on t3.id = t2.role_id
        where t3.role_name like '销售%';
    </select>


</mapper>
