<?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.jeelowcode.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="getDeptName" resultType="String">
        select name from system_dept where id =#{deptId}
    </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="getUserPageByDeptId" resultType="com.jeelowcode.framework.utils.model.JeeLowCodeUser">
        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",
            sex_data.label as "sex",
            t_post.postStr as "post",
            t_dept.deptStr as "deptName"
        FROM
        system_users  s_user
        LEFT JOIN (
            SELECT
            sud.user_id,
        LISTAGG(sd.name , '、') as deptStr
            FROM
            system_user_dept sud
            INNER JOIN system_dept sd on sud.dept_id =sd.id
            where sud.deleted=0 and sd.deleted=0
            <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>
            group by sud.user_id
        )t_dept on s_user.id =t_dept.user_id
        LEFT JOIN (
            SELECT
            sup.user_id,
        LISTAGG(sp.name , '、') as postStr
            FROM
            system_user_post sup
            INNER JOIN system_post sp on sup.post_id =sp.id
            where sup.deleted=0 and sp.deleted=0
            group by sup.user_id
        )t_post on s_user.id =t_post.user_id
        LEFT JOIN system_dict_data sex_data on sex_data.value =s_user.sex  AND sex_data.dict_type ='system_user_sex'
        where s_user.deleted =0
        and s_user.tenant_id =#{tenantId}
        <if test="params.nickName!=null and params.nickName!=''">
            and s_user.nickname like concat('%', #{params.nickName}, '%')
        </if>
        <if test="params.mobile!=null and params.mobile!=''">
            and s_user.mobile like concat('%', #{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.jeelowcode.framework.utils.model.JeeLowCodeUser">
        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",
        sex_data.label as "sex",
        t_post.postStr as "post",
        t_dept.deptStr as "deptName"
        FROM
        system_users  s_user
        INNER JOIN system_user_role sur on s_user.id =sur.user_id
        LEFT JOIN (
            SELECT
            sud.user_id,
        LISTAGG(sd.name , '、') as deptStr
            FROM
            system_user_dept sud
            INNER JOIN system_dept sd on sud.dept_id =sd.id
            where sud.deleted=0 and sd.deleted=0
            <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>
            group by sud.user_id
        )t_dept on s_user.id =t_dept.user_id

        LEFT JOIN (
            SELECT
            sup.user_id,
        LISTAGG(sp.name , '、') as postStr
            FROM
            system_user_post sup
            INNER JOIN system_post sp on sup.post_id =sp.id
            where sup.deleted=0 and sp.deleted=0
            group by sup.user_id
        )t_post on s_user.id =t_post.user_id
        LEFT JOIN system_dict_data sex_data on sex_data.value =s_user.sex  AND sex_data.dict_type ='system_user_sex'

        where s_user.deleted =0 and sur.deleted =0
        and s_user.tenant_id =#{tenantId}
        AND sur.role_id = #{roleId}
        <if test="params.nickName!=null and params.nickName!=''">
            and s_user.nickname like concat('%', #{params.nickName}, '%')
        </if>
        <if test="params.mobile!=null and params.mobile!=''">
            and s_user.mobile like concat('%', #{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="getUserPageByUserIds" resultType="com.jeelowcode.framework.utils.model.JeeLowCodeUser">
        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",
        sex_data.label as "sex",
        t_post.postStr as "post",
        t_dept.deptStr as "deptName",
        s_user.dept_id as "deptId"
        FROM
        system_users  s_user
        INNER JOIN system_user_role sur on s_user.id =sur.user_id
        LEFT JOIN (
        SELECT
        sud.user_id,
        LISTAGG(sd.name , '、') as deptStr
        FROM
        system_user_dept sud
        INNER JOIN system_dept sd on sud.dept_id =sd.id
        where sud.deleted=0 and sd.deleted=0
        group by sud.user_id
        )t_dept on s_user.id =t_dept.user_id
        LEFT JOIN (
        SELECT
        sup.user_id,
        LISTAGG(sp.name , '、') as postStr
        FROM
        system_user_post sup
        INNER JOIN system_post sp on sup.post_id =sp.id
        where sup.deleted=0 and sp.deleted=0
        group by sup.user_id
        )t_post on s_user.id =t_post.user_id
        LEFT JOIN system_dict_data sex_data on sex_data.value =s_user.sex  AND sex_data.dict_type ='system_user_sex'
        where s_user.deleted =0 and sur.deleted =0
        and s_user.tenant_id =#{tenantId}
        <if test="userIds!=null and userIds.size()>0">
            and s_user.id in
            <foreach collection="userIds" 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.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>

</mapper>
