<?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.cmeeting.mapper.primary.SysUserSyncMapper">

    <select id="getParentDeptId" resultType="java.lang.String">
        SELECT parent_id
        FROM sys_user_sync_category
        WHERE dept_id = #{deptId}
        LIMIT 1
    </select>
    <select id="getSubDeptId" resultType="java.lang.String">
        SELECT dept_id
        FROM sys_user_sync_category
        WHERE parent_id = #{deptId}
    </select>
    <select id="getUsersByDept" resultType="java.lang.String">
        select t1.user_id as wId
        from sys_user_sync t1
        where t1.dept_id in
        <foreach collection="deptIds" item="deptId" separator="," open="(" close=")">
            #{deptId}
        </foreach>
    </select>
    <select id="getUserEmail" resultType="com.cmeeting.dto.UserDTO">
        select t1.user_id as wId, IFNULL(t1.email, t1.company_email) as email
        from sys_user_sync t1
        where (t1.email is not null
            or t1.company_email is not null)

    </select>
    <select id="getSimpleUserList" resultType="com.cmeeting.ad.entity.SysUserSync">
        select t1.user_id as userId, t1.name
        from sys_user_sync t1
        group by t1.user_id
    </select>
    <select id="getSimpleDeptList" resultType="com.cmeeting.ad.entity.SysDept">
        SELECT susc.dept_id,
               susc.`name`
        FROM sys_user_sync_category AS susc
    </select>
    <select id="findName" resultType="com.cmeeting.dto.PermissionCheckedDTO$User">
        select distinct t1.user_id as id, t1.name, t2.dept_id as deptId, t2.name as deptName
        from sys_user_sync t1
        left join sys_user_sync_category t2 on t1.dept_id = t2.dept_id
        <where>
            <if test="categoryId != null and categoryId != ''">
                and t1.dept_id = #{categoryId}
            </if>
            <if test="deptIds!=null and deptIds.size > 0">
                and t2.dept_id in
                <foreach collection="deptIds" open="(" close=")" item="id" separator=",">#{id}</foreach>
            </if>
            <if test="userList != null and userList.size > 0">
                and t1.user_id in
                <foreach collection="userList" item="id" close=")" open="(" separator=",">
                    #{id}
                </foreach>
            </if>
            <if test="search != null and search!=''">
                and t1.name like concat('%',#{search},'%')
            </if>
        </where>
    </select>

    <select id="findInUserIdsOrShareCateIds" resultType="com.cmeeting.dto.PermissionCheckedDTO$User">
        select distinct t1.user_id as id,t1.name,t2.name as deptName
        from sys_user_sync t1
        left join sys_user_sync_category t2 on t1.dept_id = t2.dept_id
        <where>
            (
            t1.user_id in
            <foreach collection="userIds" item="id" close=")" open="(" separator=",">
                #{id}
            </foreach>
            or
            t1.dept_id in
            <foreach collection="shareCateIds" item="id" close=")" open="(" separator=",">
                #{id}
            </foreach>
            )
            <if test="search != null and search!=''">
                and t1.name like concat('%',#{search},'%')
            </if>
        </where>
    </select>

    <select id="findByParam" resultType="com.cmeeting.dto.PermissionCheckedDTO$User">
        select distinct t1.user_id as id,t1.name,t2.name as deptName
        from sys_user_sync t1
        left join sys_user_sync_category t2 on t1.dept_id = t2.dept_id
        <where>
        <if test="categoryId != null and categoryId!=''">
            and t1.dept_id = #{categoryId}
        </if>
        <if test="userIds != null and userIds.size > 0">
            and t1.user_id in
            <foreach collection="userIds" item="id" close=")" open="(" separator=",">
                #{id}
            </foreach>
        </if>
        <if test="cateIds != null and cateIds.size > 0">
            and t1.dept_id in
            <foreach collection="cateIds" item="id" close=")" open="(" separator=",">
                #{id}
            </foreach>
        </if>
        <if test="search != null and search!=''">
            and t1.name like concat('%',#{search},'%')
        </if>
        <if test="notInCateIds != null and notInCateIds.size > 0">
            and t1.dept_id not in
            <foreach collection="notInCateIds" item="id" close=")" open="(" separator=",">
                #{id}
            </foreach>
        </if>
        </where>
    </select>
    <select id="selectByDeptId" resultType="com.cmeeting.dto.SysUserSyncDTO">
        select DISTINCT t1.*
        from sys_user_sync t1
        <if test="deptIds!=null and deptIds.size > 0">
            inner join sys_user_sync_category t2
            on t1.dept_id = t2.dept_id
            and t2.dept_id in
            <foreach collection="deptIds" open="(" close=")" item="id" separator=",">
                #{id}
            </foreach>
        </if>
        <where>
            <if test="search!=null and search!=''">
                (t1.user_id like concat('%',#{search},'%') or t1.name like concat('%',#{search},'%') or t1.nick_name
                like concat('%',#{search},'%'))
            </if>
        </where>
    </select>


    <select id="selectParamByUserIdList" resultType="java.util.LinkedHashMap">
        SELECT
        sys_user_sync_category.`dept_id` as deptId,
        sys_user_sync.`name`,
        sys_user_sync.user_id as userId
        FROM
        sys_user_sync,
        sys_user_sync_category
        <where>
            sys_user_sync.dept_id = sys_user_sync_category.dept_id
            <if test="searchValue != null and searchValue != ''">
                and (sys_user_sync.user_id like concat('%',#{searchValue},'%') or sys_user_sync.name like
                concat('%',#{searchValue},'%') or sys_user_sync_category.name like concat('%',#{searchValue},'%'))
            </if>
            <if test="userIdList.size() > 0">
                and sys_user_sync.user_id in
                <foreach collection="userIdList" item="userId" open="(" separator="," close=")" index="index">
                    #{userId}
                </foreach>
            </if>
        </where>
    </select>
    <select id="listByUserIds" resultType="com.cmeeting.ad.entity.SysUserSync">
        select t1.user_id, t1.name, t2.name as department
        from sys_user_sync t1
        left join sys_user_sync_category t2 on t1.dept_id = t2.dept_id
        <where>
            t1.user_id in
            <foreach collection="ids" separator="," item="id" close=")" open="(">
                #{id}
            </foreach>
            <if test="search != null and search != ''">
                and t1.name like concat('%',#{search},'%');
            </if>
        </where>
    </select>
    <select id="getParDeptId" resultType="java.lang.String">
        SELECT parent_id
        FROM sys_user_sync_category
        WHERE dept_id = #{deptId}
    </select>
</mapper>