<?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.system.mapper.SysUserMapper">

    <resultMap type="com.ruoyi.common.core.domain.entity.SysUser" id="SysUserResult">
        <id property="userId" column="user_id"/>
        <result property="deptId" column="dept_id"/>
        <result property="userName" column="user_name"/>
        <result property="nickName" column="nick_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="status" column="status"/>
        <result property="delFlag" column="del_flag"/>
        <result property="loginIp" column="login_ip"/>
        <result property="loginDate" column="login_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"/>
        <result property="idCard" column="id_card"/>
        <result property="bankCard" column="bank_card"/>
        <result property="insure" column="insure"/>
        <result property="laborCost" column="labor_cost"/>
        <association property="dept" column="dept_id" javaType="com.ruoyi.common.core.domain.entity.SysDept" resultMap="deptResult"/>

        <!-- 嵌套查询加载 roles 列表 -->
        <collection property="roles"
                    ofType="com.ruoyi.common.core.domain.entity.SysRole"
                    column="user_id"
                    select="selectRolesByUserId"/>
    </resultMap>

    <resultMap id="deptResult" type="com.ruoyi.common.core.domain.entity.SysDept">
        <id property="deptId" column="dept_id"/>
        <result property="parentId" column="parent_id"/>
        <result property="deptName" column="dept_name"/>
        <result property="ancestors" column="ancestors"/>
        <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.entity.SysRole">
        <id property="roleId" 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>

    <resultMap type="com.ruoyi.common.core.domain.entity.SysUser" id="ClearUser">
        <id property="userId" column="user_id"/>
        <result property="userName" column="user_name"/>
        <result property="nickName" column="nick_name"/>
        <result property="userType" column="user_type"/>
        <result property="delFlag" column="del_flag"/>
        <result property="idCard" column="id_card"/>
        <result property="bankCard" column="bank_card"/>
        <result property="laborCost" column="labor_cost"/>
    </resultMap>

    <select id="selectPageUserList" resultMap="SysUserResult">
        SELECT
            u.*,
            d.dept_name,
            d.leader
        FROM (
                 -- 子查询：去重并分页
                 SELECT u.user_id
                 FROM sys_user u
                          LEFT JOIN sys_user_role sur
                                    ON sur.user_id = u.user_id
                          LEFT JOIN sys_role sr
                                    ON sr.role_id = sur.role_id
                     ${ew.getCustomSqlSegment}
                 GROUP BY u.user_id
             ) ids
                 -- 回联完整用户表（alias: u_full）
                 JOIN sys_user u
                      ON u.user_id = ids.user_id
            -- 回联部门表
                 LEFT JOIN sys_dept d
                           ON u.dept_id = d.dept_id
    </select>
    <!-- 嵌套查询：根据 user_id 加载角色 -->
    <select id="selectRolesByUserId"
            resultMap="RoleResult"
            parameterType="long">
        SELECT
            sr.role_id,
            sr.role_name,
            sr.data_scope,
        sr.role_key,
        sr.role_sort,
        sr.status as role_status
        FROM sys_role sr
                 INNER JOIN sys_user_role sur
                            ON sr.role_id = sur.role_id
        WHERE sur.user_id = #{user_id}
    </select>



    <sql id="selectUserVo">
        select u.user_id,
               u.dept_id,
               u.user_name,
               u.insure,
               u.nick_name,
               u.user_type,
               u.email,
               u.avatar,
               u.phonenumber,
               u.password,
               u.sex,
               u.status,
               u.del_flag,
               u.login_ip,
               u.login_date,
               u.labor_cost,
               u.create_by,
               u.create_time,
               u.bank_card,
               u.id_card,
               u.remark,
               d.dept_id,
               d.parent_id,
               d.ancestors,
               d.dept_name,
               d.order_num,
               d.leader,
               d.status as dept_status,
               r.role_id,
               r.role_name,
               r.role_key,
               r.role_sort,
               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 sur on u.user_id = sur.user_id
            left join sys_role r on r.role_id = sur.role_id
    </sql>


    <select id="selectUserList" resultMap="SysUserResult">
        select distinct u.user_id, u.insure, u.dept_id, u.nick_name, u.user_name, u.email, u.avatar, u.phonenumber, u.sex,
            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 sys_user u
            left join sys_dept d on u.dept_id = d.dept_id
        ${ew.getCustomSqlSegment}
    </select>

    <select id="selectAllocatedList" resultMap="SysUserResult">
        select distinct u.user_id, u.insure, u.dept_id, u.user_name, u.nick_name, u.email, u.phonenumber, 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 sur on u.user_id = sur.user_id
             left join sys_role r on r.role_id = sur.role_id
        ${ew.getCustomSqlSegment}
    </select>

    <select id="selectUnallocatedList" resultMap="SysUserResult">
        select distinct u.user_id, u.insure, u.dept_id, u.user_name, u.nick_name, u.email, u.phonenumber, 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 sur on u.user_id = sur.user_id
             left join sys_role r on r.role_id = sur.role_id
        ${ew.getCustomSqlSegment}
    </select>

    <select id="selectUserByUserName" parameterType="String" resultMap="SysUserResult">
        <include refid="selectUserVo"/>
        where u.del_flag = '0' and u.user_name = #{userName}
    </select>

    <select id="selectUserByPhonenumber" parameterType="String" resultMap="SysUserResult">
        <include refid="selectUserVo"/>
        where u.del_flag = '0' and u.phonenumber = #{phonenumber}
    </select>

    <select id="selectUserByEmail" parameterType="String" resultMap="SysUserResult">
        <include refid="selectUserVo"/>
        where u.del_flag = '0' and u.email = #{email}
    </select>

    <select id="selectUserByNickName" parameterType="String" resultMap="SysUserResult">
        <include refid="selectUserVo"/>
        where u.del_flag = '0' and u.nick_name = #{nickName}
    </select>

    <select id="selectUserById" parameterType="Long" resultMap="SysUserResult">
        <include refid="selectUserVo"/>
        where u.del_flag = '0' and u.user_id = #{userId}
    </select>
    <select id="selectUserByIdAndNotDelFlag" parameterType="Long" resultMap="SysUserResult">
        <include refid="selectUserVo"/>
        where u.user_id = #{userId}
    </select>


    <select id="selectListAndNotDel" resultMap="ClearUser">
        select user_id,user_name,insure,nick_name,labor_cost from sys_user where del_flag ='0'
    </select>


    <select id="selectUserByIdIncludingDel" resultType="com.ruoyi.common.core.domain.entity.SysUser">
        <include refid="selectUserVo"/>
        where u.user_id = #{userId}
    </select>


</mapper>
