<?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.chushang.system.mapper.SysUserMapper">
    <resultMap type="com.chushang.security.entity.po.SysUser" id="SysUserResult">
        <id property="userId" column="user_id"/>
        <result property="deptId" column="dept_id"/>
        <result property="username" column="username"/>
        <result property="password" column="password"/>
        <result property="status" column="status"/>
        <result property="createBy" column="create_by"/>
        <result property="delState" column="del_state"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
        <result property="salt" column="salt"/>
        <result property="phone" column="phone"/>
        <result property="email" column="email"/>
        <result property="nickName" column="nick_name"/>
        <result property="gender" column="gender"/>
        <result property="avatar" column="avatar"/>
        <result property="isPlatform" column="is_platform"/>
        <association property="dept" resultMap="deptResult"/>
        <collection property="roles" javaType="java.util.List" resultMap="RoleResult"/>
    </resultMap>

    <resultMap id="deptResult" type="com.chushang.security.entity.po.SysDept">
        <id property="deptId" column="dept_id"/>
        <result property="parentId" column="parent_dept_id"/>
        <result property="deptName" column="dept_name"/>
        <result property="ancestors" column="ancestors"/>
        <result property="orderNum" column="dept_order_num"/>
        <result property="status" column="dept_status"/>
    </resultMap>

    <resultMap id="RoleResult" type="com.chushang.security.entity.po.SysRole">
        <id property="roleId" column="role_id"/>
        <result property="roleName" column="role_name"/>
        <result property="roleKey" column="role_key"/>
        <result property="orderNum" column="role_order_num"/>
        <result property="dataScope" column="data_scope"/>
        <result property="status" column="role_status"/>
    </resultMap>

    <sql id="selectUserVo">
        select u.user_id, u.dept_id, u.username, u.password, u.status, u.del_state, u.create_by, u.create_time,u.update_time,u.salt,
               u.phone,u.nick_name,u.gender,u.avatar,u.email, u.is_platform,
               d.parent_dept_id, d.ancestors, d.dept_name, d.order_num as dept_order_num,d.status as dept_status,
               r.role_id, r.role_name, r.role_key,r.order_num as role_order_num, r.data_scope, r.status as role_status
        from sys_user u
                 left join sys_dept d on u.dept_id = d.dept_id
                 left join sys_user_role ur on u.user_id = ur.user_id
                 left join sys_role r on r.role_id = ur.role_id
    </sql>
    <select id="selectUserByUserName" resultMap="SysUserResult">
        <include refid="selectUserVo"/>
        where u.username = #{userName} and u.del_state = FALSE
    </select>

    <select id="selectUserById" resultMap="SysUserResult">
        <include refid="selectUserVo"/>
        where u.user_id = #{userId} and u.del_state = FALSE
    </select>

    <select id="selectUserList" parameterType="com.chushang.security.entity.po.SysUser" resultMap="SysUserResult">
        select u.user_id, u.dept_id, u.username, u.status, u.del_state, u.create_by, u.create_time,
        d.dept_name from sys_user u
        left join sys_dept d on u.dept_id = d.dept_id
        where u.del_state = FALSE
        <if test="userId != null and userId != 0">
            AND u.user_id = #{userId}
        </if>
        <if test="status != null and status != ''">
            AND u.status = #{status}
        </if>
        <if test="deptId != null and deptId != 0">
            AND (u.dept_id = #{deptId} OR u.dept_id IN ( SELECT t.dept_id FROM sys_dept t WHERE find_in_set(#{deptId}, ancestors) ))
        </if>

        <if test="username != null and username != ''">
            AND u.username like concat('%', #{username}, '%')
        </if>
        <!-- 数据范围过滤 -->
        ${sqlParam.get('dataScope')}
    </select>

    <select id="listUser" parameterType="com.chushang.system.entity.dto.ListUserDTO" resultMap="SysUserResult">
        select u.user_id, u.dept_id, u.username, u.phone,u.nick_name,u.gender,u.email,u.avatar, u.status, u.del_state, u.create_by, u.create_time, d.dept_name
        from sys_user u
        left join sys_dept d on u.dept_id = d.dept_id
        where u.del_state = FALSE
        <if test="listUser.username != null and listUser.username != ''">
            AND u.username like concat('%',#{listUser.username},'%')
        </if>
        <if test="listUser.deptId != null">
            AND d.dept_id = #{listUser.deptId}
        </if>
        <if test="listUser.status != null">
            AND u.status = #{listUser.status}
        </if>
        <if test="listUser.phone != null and listUser.phone != ''">
            AND u.phone like concat('%', #{listUser.phone} , '%')
        </if>
        <if test="listUser.createTimes != null">
            <if test="listUser.createTimes.size() == 2">
                AND u.create_time between #{listUser.createTimes[0]} and #{listUser.createTimes[1]}
            </if>
        </if>
        ${listUser.sqlParam.dataScope}

    </select>

    <select id="selectAllocatedList" resultMap="SysUserResult">
        select distinct u.user_id, u.dept_id, u.username, u.status, u.create_time
        from sys_user u
        left join sys_dept d on u.dept_id = d.dept_id
        left join sys_user_role ur on u.user_id = ur.user_id
        left join sys_role r on r.role_id = ur.role_id
        where u.del_state = FALSE and r.role_id = #{listUser.roleId}
        <if test="listUser.username != null and listUser.username != ''">
            AND u.username like concat('%',#{listUser.username},'%')
        </if>
        ${listUser.sqlParam.dataScope}
    </select>

    <select id="selectUnallocatedList" resultMap="SysUserResult">
        select distinct u.user_id, u.dept_id, u.username, u.status, u.create_time
        from sys_user u
        left join sys_dept d on u.dept_id = d.dept_id
        left join sys_user_role ur on u.user_id = ur.user_id
        left join sys_role r on r.role_id = ur.role_id
        where u.del_state = FALSE
            and (r.role_id != #{listUser.roleId} or r.role_id IS NULL)
            and u.user_id in (select u.user_id from sys_user u LEFT join sys_user_role ur on u.user_id = ur.user_id and ur.role_id != #{listUser.roleId})

        <if test="listUser.username != null and listUser.username != ''">
            AND u.username like concat('%',#{listUser.username},'%')
        </if>
        ${listUser.sqlParam.dataScope}
    </select>
</mapper>
