<?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.storlead.user.mapper.DeptMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" extends="com.storlead.framework.mybatis.mapper.SysBaseFieldMapper.BaseResultMap" type="com.storlead.user.pojo.entity.DeptEntity">
        <id column="id" property="id" />
        <result column="name" property="name" />
        <result column="sub_company_id" property="subCompanyId" />
        <result column="pid" property="pid" />
        <result column="manager_id" property="managerId" />
        <result column="sub_manager_ids" property="subManagerIds" />
        <result column="route_code" property="routeCode" />
        <result column="company_id" property="companyId" />
        <result column="data_source" property="dataSource" />
        <result column="leader_name" property="leaderName" />
        <result column="dept_name_route" property="deptNameRoute" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id, `name`, sub_company_id,leader_name,dept_name_route,data_source, pid, enabledcreate_by, manager_id,sub_manager_ids,route_code,company_id, <include refid="com.storlead.framework.mybatis.mapper.SysBaseFieldMapper.Base_Column_List"></include>
    </sql>


    <select id="queryDeptBySubList" resultType="com.storlead.user.model.DeptTreeModel">
        SELECT
            id,name,sort,sub_company_id as subCompanyId
        FROM
            dept

        WHERE sub_company_id = #{subCompanyId}
    </select>
    <select id="queryDeptIdListByDeptId" resultType="java.lang.Long">
        select id from (
                select
                    t1.id,
                    t1.`name`,
                    if(find_in_set(pid, @pids) > 0, @pids := concat(@pids, ',', id), 0) as ischild
                from (
                         select
                             id,
                             pid,
                             `name`
                         from dept t order by pid, id
                     ) t1,
                     (select @pids := #{deptId}) t2
            ) t3 where ischild != 0 OR id = #{deptId}
    </select>

    <select id="findByPid" resultType="com.storlead.user.pojo.entity.DeptEntity">
        select * from dept where pid = #{id}
    </select>

    <select id="getDeptTreeIdsByDeptIds" resultType="com.storlead.user.pojo.entity.DeptEntity">
        SELECT d.*
        FROM dept d
                 JOIN (select * from dept
            <where>
                <if test="query.deptIds != null and query.deptIds.size > 0">
                    and id in
                    <foreach collection="query.deptIds" item="deptId" open="(" separator="," close=")">
                        #{deptId}
                    </foreach>
                </if>
          </where>
        ) p
        ON d.route_code LIKE CONCAT(p.route_code, '%');
        where d.is_delete = 0
    </select>

    <!-- 根据角色ID查询部门列表 -->
    <select id="findByRoleId" resultType="com.storlead.user.pojo.entity.DeptEntity">
        SELECT d.*
        FROM dept_role dr
                 LEFT JOIN dept d on dr.dept_id = d.id
        WHERE dr.role_id = #{roleId} and d.id is not null;
    </select>



</mapper>
