<?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.ruoyi.platform.mapper.PlatformUserMapper">

    <resultMap type="com.ruoyi.common.core.domain.platform.vo.PlatformUserVO" id="SysUserResult">
        <id     property="id"        column="user_id"         />
        <result property="deptId"        column="dept_id"         />
        <result property="loginName"     column="login_name"      />
        <result property="userName"      column="user_name"       />
        <result property="userType"      column="user_type"       />
        <result property="email"         column="email"           />
        <result property="phonenumber"   column="phonenumber"     />
        <result property="sex"           column="sex"             />
        <result property="avatar"        column="avatar"          />
        <result property="password"      column="password"        />
        <result property="salt"          column="salt"            />
        <result property="status"        column="status"          />
        <result property="delFlag"       column="del_flag"        />
        <result property="loginIp"       column="login_ip"        />
        <result property="loginDate"     column="login_date"      />
        <result property="pwdUpdateDate" column="pwd_update_date" />
        <result property="createBy"      column="create_by"       />
        <result property="createTime"    column="create_time"     />
        <result property="updateBy"      column="update_by"       />
        <result property="updateTime"    column="update_time"     />
        <result property="remark"        column="remark"          />
        <association property="dept"     column="dept_id" javaType="com.ruoyi.common.core.domain.platform.vo.PlatformDeptVO" resultMap="deptResult" />
        <collection  property="roles"   javaType="java.util.List"        resultMap="RoleResult" />
    </resultMap>

    <resultMap id="deptResult" type="com.ruoyi.common.core.domain.platform.vo.PlatformDeptVO">
        <id     property="id"   column="dept_id"     />
        <result property="parentId" column="parent_id"   />
        <result property="deptName" column="dept_name"   />
        <result property="orderNum" column="order_num"   />
        <result property="leader"   column="leader"      />
        <result property="status"   column="dept_status" />
    </resultMap>

    <resultMap id="RoleResult" type="com.ruoyi.common.core.domain.platform.vo.PlatformRoleVO">
        <id     property="id"       column="role_id"        />
        <result property="roleName"     column="role_name"      />
        <result property="roleKey"      column="role_key"       />
        <result property="roleSort"     column="role_sort"      />
        <result property="dataScope"    column="data_scope"     />
        <result property="status"       column="role_status"    />
    </resultMap>

    <sql id="selectUserVo">
        select  u.id AS user_id, u.dept_id, u.login_name, u.user_name, u.user_type, u.email, u.avatar, u.phonenumber,
                u.sex, u.password, u.salt, u.status, u.del_flag, u.login_ip, u.login_date, u.pwd_update_date,
                u.create_time, u.remark,
                d.id AS dept_id, d.parent_id, d.dept_name, d.order_num, d.leader, d.status as dept_status,
                r.id AS role_id, r.role_name, r.role_key, r.role_sort, r.data_scope, r.status as role_status
        from platform_user u
                 left join platform_dept d on u.dept_id = d.id
                 left join platform_user_role ur on u.id = ur.user_id
                 left join platform_role r on r.id = ur.role_id
    </sql>

    <select id="selectUserList" parameterType="com.ruoyi.common.core.domain.platform.dto.PlatformUserDTO" resultMap="SysUserResult">
        select u.id AS user_id, u.dept_id, u.login_name, u.user_name, u.user_type, u.email, u.avatar, u.phonenumber,
        u.password, u.sex, u.salt, u.status, u.del_flag, u.login_ip, u.login_date, u.create_by, u.create_time,
        u.remark, d.dept_name, d.leader from platform_user u
        left join platform_dept d on u.dept_id = d.id
        where u.del_flag = '0'
        <if test="loginName != null and loginName != ''">
            AND u.login_name like concat('%', #{loginName}, '%')
        </if>
        <if test="status != null and status != ''">
            AND u.status = #{status}
        </if>
        <if test="phonenumber != null and phonenumber != ''">
            AND u.phonenumber like concat('%', #{phonenumber}, '%')
        </if>
        <if test="params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 -->
            AND date_format(u.create_time,'%y%m%d') &gt;= date_format(#{params.beginTime},'%y%m%d')
        </if>
        <if test="params.endTime != null and params.endTime != ''"><!-- 结束时间检索 -->
            AND date_format(u.create_time,'%y%m%d') &lt;= date_format(#{params.endTime},'%y%m%d')
        </if>
        <if test="deptId != null and deptId != ''">
            AND (u.dept_id = #{deptId} OR u.dept_id IN ( SELECT t.id FROM platform_dept t WHERE FIND_IN_SET (#{deptId},ancestors) ))
        </if>
        <!-- 数据范围过滤 -->
        ${params.dataScope}
    </select>

    <select id="selectAllocatedList" parameterType="com.ruoyi.common.core.domain.platform.dto.PlatformUserDTO" resultMap="SysUserResult">
        select distinct u.id AS user_id, u.dept_id, u.login_name, u.user_name, u.user_type, u.email, u.avatar,
        u.phonenumber, u.status, u.create_time
        from platform_user u
        left join platform_dept d on u.dept_id = d.id
        left join platform_user_role ur on u.id = ur.user_id
        left join platform_role r on r.id = ur.role_id
        where u.del_flag = '0' and r.id = #{roleId}
        <if test="loginName != null and loginName != ''">
            AND u.login_name like concat('%', #{loginName}, '%')
        </if>
        <if test="phonenumber != null and phonenumber != ''">
            AND u.phonenumber like concat('%', #{phonenumber}, '%')
        </if>
        <!-- 数据范围过滤 -->
        ${params.dataScope}
    </select>

    <select id="selectUnallocatedList" parameterType="com.ruoyi.common.core.domain.platform.dto.PlatformUserDTO" resultMap="SysUserResult">
        select distinct u.id AS user_id, u.dept_id, u.login_name, u.user_name, u.user_type, u.email, u.avatar, u.phonenumber,
        u.status, u.create_time
        from platform_user u
        left join platform_dept d on u.dept_id = d.id
        left join platform_user_role ur on u.id = ur.user_id
        left join platform_role r on r.id = ur.role_id
        where u.del_flag = '0' and (r.id != #{roleId} or r.id IS NULL)
        and u.id not in (select u.id from platform_user u inner join platform_user_role ur on u.id = ur.user_id and ur.role_id = #{roleId})
        <if test="loginName != null and loginName != ''">
            AND u.login_name like concat('%', #{loginName}, '%')
        </if>
        <if test="phonenumber != null and phonenumber != ''">
            AND u.phonenumber like concat('%', #{phonenumber}, '%')
        </if>
        <!-- 数据范围过滤 -->
        ${params.dataScope}
    </select>

    <select id="selectUserById" parameterType="String" resultMap="SysUserResult">
        <include refid="selectUserVo"/>
        where u.id = #{userId}
    </select>

    <select id="selectUserByLoginName" parameterType="String" resultMap="SysUserResult">
        <include refid="selectUserVo"/>
        where u.login_name = #{loginName}
    </select>

</mapper>