<?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.devplatform.admin.modules.sys.dao.SysUserDao">
    <!-- 查询用户的所有权限 -->
    <select id="queryAllPerms" resultType="string">
        SELECT m.perms
        FROM sys_user_role ur
                 LEFT JOIN sys_role_menu rm ON ur.role_id = rm.role_id
                 LEFT JOIN sys_menu m ON rm.menu_id = m.menu_id
        WHERE ur.user_id = #{userId}
    </select>

    <!-- 查询用户的所有菜单ID -->
    <select id="queryAllMenuId" resultType="java.lang.String">
        SELECT DISTINCT rm.menu_id
        FROM sys_user_role ur
                 LEFT JOIN sys_role_menu rm ON ur.role_id = rm.role_id
        WHERE ur.user_id = #{userId}
    </select>

    <select id="queryByUserName" resultType="com.devplatform.admin.modules.sys.bean.SysUserEntity">
        SELECT *
        FROM sys_user
        WHERE deleted = 0
        AND username = #{username}
    </select>


    <select id="getById" resultType="com.devplatform.admin.modules.sys.bean.SysUserEntity">
        SELECT su.user_id,
               ls.station_name AS org_id,
               su.name,
               su.username,
               su.password,
               su.job,
               su.work,
               su.mobile,
               su.id_card,
               su.email,
               su.education,
               su.nation,
               su.entry_time,
               su.job_number,
               su.politic_countenance,
               su.gender,
               su.status,
               su.salt,
               su.detail,
               su.create_user_id,
               su.create_time,
               su.update_user_id,
               su.update_time,
               su.deleted,
               su.sys_sign,
               su.station_id,
               su.byx1,
               su.byx2,
               su.byx3
        FROM sys_user su
                 LEFT JOIN li_station ls ON ls.id = su.org_id
        WHERE su.deleted = 0
          AND su.user_id = #{userId}
    </select>

    <select id="queryUserById" resultType="int">
        SELECT count(1)
        FROM sys_user u
        WHERE u.deleted = 0
          AND username = #{username}
    </select>

    <update id="updatePassword">
        UPDATE sys_user
        SET password = #{password},
            sys_sign=#{sysSign}
        WHERE username = #{username}
    </update>


    <select id="queryByList" resultType="com.devplatform.admin.modules.sys.bean.SysUserEntity">
        SELECT t1.*,
        (SELECT t4.role_name from sys_user_role t3 LEFT JOIN sys_role t4 on t3.role_id = t4.role_id WHERE t3.user_id =
        t1.user_id GROUP BY t3.user_id) roleName
        from sys_user t1
        where t1.deleted = 0
        <if test="model.username!=null and model.username!=''">and INSTR(t1.username, #{model.username})
        </if>
        <if test="model.createUserId!=null and model.createUserId!=''">and t1.create_user_id = #{model.createUserId}
        </if>
        <if test="model.orgId != null and model.orgId != ''">and t1.org_id = #{model.orgId}</if>
        <if test="model.stationId != null and model.stationId != ''">and t1.station_id = #{model.stationId}</if>
        order by t1.create_time desc
    </select>

    <select id="getUserInfoByOrgId" resultType="hashmap">
        SELECT t1.*
        FROM sys_user t1
        WHERE t1.deleted = 0
          AND org_id = #{org_id};
    </select>

    <select id="getAllUserByOrgIdPage" resultType="java.util.HashMap">
        SELECT u.*,sd.name AS jobName ,sn.name AS nationName,
        (SELECT GROUP_CONCAT(sr.role_name) from sys_role sr where sr.role_id in (select sur.role_id from sys_user_role sur where sur.user_id = u.user_id)) AS roleNames
        FROM sys_user u
        LEFT JOIN sys_dictionary sd ON u.job = sd.id AND sd.deleted = 0 AND sd.station_id = u.station_id
        LEFT JOIN sys_nation sn ON u.nation = sn.id
        <where>
            AND u.deleted = 0 AND u.user_id not in(1, 2)
            AND u.station_id = #{map.stationId}
            <if test="map.username !=null and map.username != ''">
                AND INSTR(u.username, #{map.username})
            </if>
            <if test="map.name !=null and map.name != ''">
                AND INSTR(u.name, #{map.name})
            </if>
            <if test="map.jobNumber !=null and map.jobNumber != ''">
                AND INSTR(u.job_number, #{map.jobNumber})
            </if>
            <if test="map.gender !=null">
                AND u.gender = #{map.gender}
            </if>
            <if test="map.status !=null and map.status != ''">
                AND u.status = #{map.status}
            </if>
            <choose>
                <when test="map.sort != null and map.sort != '' and map.order != null and map.order != ''">
                    ORDER BY ${map.sort} ${map.order}, u.create_time desc
                </when>
                <otherwise>
                    ORDER BY u.create_time desc
                </otherwise>
            </choose>
        </where>
    </select>

    <select id="getAllUserByOrgId" resultType="java.util.HashMap">
        SELECT u.*,sd.name AS jobName ,sn.name AS nationName
        FROM sys_user u
        LEFT JOIN sys_dictionary sd ON u.job = sd.id AND sd.deleted = 0
        LEFT JOIN sys_nation sn ON u.nation = sn.id
        <where>
            AND u.deleted = 0 AND u.user_id &lt;&gt; 1
            <if test="map.username !=null and map.username != ''">
                AND INSTR(u.username, #{map.username})
            </if>
            <if test="map.name !=null and map.name != ''">
                AND INSTR(u.name, #{map.name})
            </if>
            <if test="map.jobNumber !=null and map.jobNumber != ''">
                AND INSTR(u.job_number, #{map.jobNumber})
            </if>
            <if test="map.gender !=null">
                AND u.gender = #{map.gender}
            </if>
            <if test="map.status !=null and map.status != ''">
                AND u.status = #{map.status}
            </if>
            <if test="map.stationId !=null and map.stationId != ''">
                AND u.station_id = #{map.stationId}
            </if>
            <choose>
                <when test="map.sort != null and map.sort != '' and map.order != null and map.order != ''">
                    ORDER BY ${map.sort} ${map.order}, u.create_time desc
                </when>
                <otherwise>
                    ORDER BY u.create_time desc
                </otherwise>
            </choose>
        </where>
    </select>

    <update id="updatePw">
        UPDATE sys_user
        SET password = #{password}, salt = #{salt}, update_time = now(), update_user_id = #{userId},sys_sign=#{sysSign}
        WHERE user_id IN
        <foreach close=")" collection="split" index="index" item="item" open="(" separator=",">
            #{item}
        </foreach>
    </update>

</mapper>