<?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.jeecg.modules.system.mapper.SysDepartMapper">

	<select id="queryUserDeparts" parameterType="String" resultType="org.jeecg.modules.system.entity.SysDepart">
	   select * from sys_depart where id IN ( select dep_id from sys_user_depart where user_id = #{userId} )
	</select>

    <!-- 根据username查询所拥有的部门 -->
    <select id="queryDepartsByUsername" parameterType="String" resultType="org.jeecg.modules.system.entity.SysDepart">
        SELECT *
        FROM sys_depart
        WHERE id IN (
            SELECT dep_id
            FROM sys_user_depart
            WHERE user_id = (
                SELECT id
                FROM sys_user
                WHERE username = #{username}
            )
        )
    </select>

    <!-- 根据部门Id查询,当前和下级所有部门IDS -->
    <select id="getSubDepIdsByDepId" resultType="java.lang.String">
		select id from sys_depart where del_flag = '0' and org_code like concat((select org_code from sys_depart where id=#{departId}),'%')
	</select>

    <!--根据部门编码获取我的部门下所有部门ids -->
    <select id="getSubDepIdsByOrgCodes" resultType="java.lang.String">
		select id from sys_depart where del_flag = '0' and
        <foreach collection="orgCodes" item="item" index="index"  open="(" separator="or" close=")">
            org_code LIKE CONCAT(#{item},'%')
        </foreach>
	</select>
     <!--根据parent_id查询下级部门-->
    <select id="queryTreeListByPid" parameterType="Object"  resultType="org.jeecg.modules.system.entity.SysDepart">
        SELECT * FROM  sys_depart where del_flag = '0'
        <choose>
            <when test="parentId != null and parentId != ''">
                AND parent_id =  #{parentId,jdbcType=VARCHAR}
            </when>
            <otherwise>
                AND parent_id is null or parent_id=''
            </otherwise>
        </choose>
        order by depart_order
    </select>

    <!-- 根据OrgCod查询公司信息 -->
    <select id="queryCompByOrgCode" resultType="org.jeecg.modules.system.entity.SysDepart">
		select * from sys_depart where del_flag = '0' and org_category='1' and org_code= #{orgCode,jdbcType=VARCHAR}
	</select>
    <select id="getValidDeptById" resultType="org.jeecg.modules.system.entity.SysDepart">
        select * from  sys_depart  where id = #{id} and del_flag = 0 and status = '1'
    </select>
    <select id="getDeptByUserId" resultType="org.jeecg.modules.system.entity.SysDepart">
        SELECT * FROM sys_depart a
            LEFT JOIN sys_user_depart b ON  b.dep_id = a.id
            LEFT JOIN sys_user c ON c.id=b.user_id
        WHERE c.id=#{userId}
    </select>
    <select id="getUserByDept" resultType="java.lang.String">
        SELECT a.username FROM sys_user a
            LEFT JOIN sys_user_depart b ON a.id = b.user_id
            LEFT JOIN sys_depart c ON c.id = b.dep_id
        WHERE c.id=#{id}
    </select>
    <select id="queryDepartList" resultType="org.jeecg.modules.system.entity.SysDepart">
        SELECT *
        FROM sys_depart
        WHERE status = "1"
          AND depart_simple_name LIKE CONCAT('%', #{departName}, '%')
          AND area_id IN
              (select id
               from (select t1.id,
                            if(find_in_set(parent_id, @pids) > 0, @pids := concat(@pids, ',', id), 0) as ischild
                     from (select id, parent_id, area_level
                           from sys_area
                           ORDER BY if(isnull(area_level), 1, 0), area_level) t1,
                          (select @pids := #{area_id}) t2) t3
               where ischild != 0
               union
               select id
               from sys_area
               where id = #{area_id})
    </select>
    <select id="queryUserDeptAreaInfo" resultType="java.util.Map" parameterType="java.lang.String">
        /*获取用户部门和区划信息*/
        SELECT
            c.area_name,c.area_code,c.depart_name,c.depart_code,c.area_level
        FROM
            sys_user a
                inner join sys_user_depart b on a.id = b.user_id
                inner join sys_depart c on b.dep_id = c.id

        where a.username = #{username}
    </select>
    <select id="queryUserAreaAuth" resultType="java.lang.String" parameterType="java.lang.String">
        select area_code
        from sys_area
        where (id in (SELECT c.area_id
                      FROM sys_user a
                               inner join sys_user_depart b on a.id = b.user_id
                               inner join sys_depart c on b.dep_id = c.id

                      where a.username = #{username}) or parent_id in (SELECT c.area_id
                                                                       FROM sys_user a
                 inner join sys_user_depart b on a.id = b.user_id
                 inner join sys_depart c on b.dep_id = c.id

         where a.username = #{username})) and del_flag = 0
    </select>
</mapper>