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

    <resultMap type="org.dromara.system.domain.vo.SysUserVo" id="SysUserResult">
        <id property="userId" column="user_id"/>
    </resultMap>
    <resultMap type="org.dromara.system.domain.vo.SysUserExportVo" id="SysUserExportResult">
        <id property="userId" column="user_id"/>
    </resultMap>

    <select id="selectPageUserList" resultMap="SysUserResult">
        select
        <if test="ew.getSqlSelect != null">
            ${ew.getSqlSelect}
        </if>
        <if test="ew.getSqlSelect == null">
            u.user_id, 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
        </if>
        from sys_user u
        ${ew.getCustomSqlSegment}
    </select>

    <select id="selectUserList" resultMap="SysUserResult">
        select
        <if test="ew.getSqlSelect != null">
            ${ew.getSqlSelect}
        </if>
        <if test="ew.getSqlSelect == null">
            u.user_id, 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
        </if>
        from sys_user u
        ${ew.getCustomSqlSegment}
    </select>

    <select id="selectUserExportList" resultMap="SysUserExportResult">
        select u.user_id, 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, u1.user_name as leaderName
        from sys_user u
                 left join sys_dept d on u.dept_id = d.dept_id
                 left join sys_user u1 on u1.user_id = d.leader
            ${ew.getCustomSqlSegment}
    </select>

    <select id="selectAllocatedList" resultMap="SysUserResult">
        select distinct u.user_id, 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.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="countUserById" resultType="Long">
        select count(*) from sys_user where del_flag = '0' and user_id = #{userId}
    </select>
    <select id="selectListByUserNames" resultType="org.dromara.system.domain.SysUser">
        select *
        from sys_user
        where del_flag = '0' and user_name in
        <foreach collection="userNameList" open="(" separator="," close=")" item="userName">
                #{userName}
        </foreach>
    </select>
    <select id="getExpertStatus" resultType="java.lang.Long">
        SELECT
            expert_id
        FROM
            zjk_expert
        WHERE
            (
                delete_flag = 0
                    AND user_id = #{userId}
                    AND (status = 2 OR (approval_type = 1 AND status IN (0, 1, 2, 3, 4, 5))))
    </select>

    <select id="selectUserById" resultMap="SysUserResult" parameterType="map">
        SELECT
            user_id, dept_id, user_name, nick_name,
            user_type, email, phonenumber, sex, avatar,
            password, status, del_flag, login_ip, source,
            source_tenant, id_card, login_date, remark,
            is_first_login, password_update_time, tenant_id,
            create_dept, create_by, create_time, update_by, update_time
        FROM sys_user
        WHERE user_id = #{userId}
          AND del_flag = '0'

    </select>

    <update id="updatePasswordById">
        UPDATE sys_user
        SET password = #{password},
            password_update_time = NOW()
        WHERE user_id = #{userId}
    </update>

    <update id="updateHmacById">
        UPDATE sys_user
        SET hmac = #{hmac},
            update_time = NOW()
        WHERE user_id = #{userId}
    </update>


</mapper>
