<?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.lideeyunji.core.framework.mapper.AdapterMapper">

    <select id="getUserName" resultType="String">
        select username from system_users where id = #{userId}
    </select>
    <select id="getNickname" resultType="String">
        select nickname from system_users where id = #{userId}
    </select>

    <select id="getAllNickNameList" resultType="map">
        select id,nickname from system_users
    </select>

    <select id="getDeptName" resultType="String">
        select name from system_dept where id =#{deptId}
    </select>

    <select id="getAllDeptNameList" resultType="map">
        select id,name from system_dept
    </select>



    <select id="getUserIdByNikeName" resultType="long">
        select id from system_users where nickname = #{nikeName} and tenant_id = #{tenantId}
    </select>

    <select id="getDeptIdByDeptName" resultType="long">
        select id from system_dept where name = #{deptName} and tenant_id = #{tenantId}
    </select>

    <select id="getRoleCodeByUser" resultType="string">
        SELECT
          sr.`code`
        FROM
          system_user_role sur
          INNER JOIN system_role sr ON sur.role_id = sr.id
        WHERE
          sur.is_deleted = 0
          AND sr.is_deleted = 0
          and sur.user_id=#{userId}
          <if test="deptId!=null">
              and sur.dept_id =#{deptId}
          </if>
          <if test="roleId!=null">
              and sur.role_id = #{roleId}
          </if>
    </select>

    <select id="getRoleIdByUser" resultType="string">
        SELECT
        sur.`role_id`
        FROM
        system_user_role sur
        WHERE
        sur.is_deleted = 0
        and sur.user_id=#{userId}
        <if test="deptId!=null">
            and sur.dept_id =#{deptId}
        </if>
        <if test="roleId!=null">
            and sur.role_id = #{roleId}
        </if>
    </select>



    <select id="getUserPageByDeptId" resultType="com.lideeyunji.tool.framework.yunji.model.lideeYunJiUser">
        SELECT
        s_user.id as "userId",
        s_user.nickname as "nickName",
        s_user.mobile as "mobile",
        s_user.email as "email",
        s_user.avatar as "avatar"
        FROM
        system_users  s_user
        where s_user.is_deleted =0
        and s_user.tenant_id =#{tenantId}
        <if test="params.nickName!=null and params.nickName!=''">
            and s_user.nickname like #{params.nickName}
        </if>
        <if test="params.mobile!=null and params.mobile!=''">
            and s_user.mobile like #{params.mobile}
        </if>
        <if test="deptIdList!=null and deptIdList.size()>0">
            and s_user.id in(
            select user_id from system_user_dept where dept_id in
            <foreach collection="deptIdList" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
            )
        </if>
    </select>

    <select id="getUserPageByRoleId" resultType="com.lideeyunji.tool.framework.yunji.model.lideeYunJiUser">
        SELECT
        s_user.id as "userId",
        s_user.nickname as "nickName",
        s_user.mobile as "mobile",
        s_user.email as "email",
        s_user.avatar as "avatar"
        FROM
        system_users  s_user
        INNER JOIN (select user_id from system_user_role where is_deleted = 0 AND role_id = #{roleId} group by user_id) sur ON s_user.id = sur.user_id
        where s_user.is_deleted =0
        and s_user.tenant_id =#{tenantId}
        <if test="params.nickName!=null and params.nickName!=''">
            and s_user.nickname like #{params.nickName}
        </if>
        <if test="params.mobile!=null and params.mobile!=''">
            and s_user.mobile like #{params.mobile}
        </if>
        <if test="deptIdList!=null and deptIdList.size()>0">
            and s_user.id in(
            select user_id from system_user_dept where dept_id in
            <foreach collection="deptIdList" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
            )
        </if>
    </select>

    <select id="getSysDictMap" resultType="java.util.Map">
        SELECT
            d.value,
            d.label
        FROM
            system_dict_data d
        WHERE
            d.is_deleted = 0
            AND d.dict_type=#{dictCode}
    </select>

    <select id="getMenuI18nMapList" resultType="map">
        SELECT
          id,
          NAME,
          name_i18n
        FROM
          system_menu
        order by id asc
    </select>

    <update id="updateMenuI18n">
        update system_menu set name_i18n =#{i18n} where id =#{id}
    </update>

    <select id="getUserIdAndDeptName" resultType="java.util.Map">
        SELECT
        sud.user_id,
        sd.name dept_name
        FROM
        system_user_dept sud
        INNER JOIN system_dept sd on sud.dept_id =sd.id
        where sud.is_deleted=0 and sd.is_deleted=0
        and sd.tenant_id =#{tenantId}
        <if test="deptIdList!=null and deptIdList.size()>0">
            and sud.dept_id in
            <foreach collection="deptIdList" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
    </select>

    <select id="getUserIdAndPostName" resultType="java.util.Map">
        SELECT
            sup.user_id,
            sp.name as post_name
        FROM
            system_user_post sup
                INNER JOIN system_post sp on sup.post_id =sp.id
        where sup.is_deleted=0 and sp.is_deleted=0
          and sp.tenant_id =#{tenantId}
    </select>
</mapper>
