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

    <!--获取部门分页列表-->
    <select id="page" resultType="DeptVO">
        SELECT sys_dept.*,
               sys_area.name areaName
        FROM sys_dept sys_dept
           ,sys_area sys_area
        WHERE sys_dept.area = sys_area.id
          AND ${ew.sqlSegment}
    </select>

<!--    &lt;!&ndash;查询排序&ndash;&gt;-->
<!--    <select id="getSortNum" resultType="int">-->
<!--        SELECT MAX(sys_dept.sort) sort-->
<!--        FROM sys_dept sys_dept-->
<!--        WHERE sys_dept.state = 1-->
<!--    </select>-->

    <select id="getIdString" resultType="String">
        SELECT sys_dept.pids pids
        FROM sys_dept sys_dept
        WHERE sys_dept.id = #{id}
    </select>

    <select id="getDeptTree" resultType="DeptTreeVO">
        SELECT
            T3.*,
            T5.id area,
            T5.name areaName
        FROM
            (
                SELECT
                    @codes AS _ids,
            (
                SELECT @codes := GROUP_CONCAT( id ) FROM sys_dept WHERE FIND_IN_SET( pid, @codes ) ) AS T1
                      FROM
                        sys_dept s,
                        ( SELECT @codes := #{id} ) T4
                      WHERE
                        @codes IS NOT NULL
                        <if test="type != null">
                            AND s.type in
                            <foreach collection="type" item="item" open="(" close=")" separator=",">
                                #{item}
                            </foreach>
                        </if>
                        ) T2,
                        sys_dept T3,
                        sys_area T5
        WHERE
            FIND_IN_SET( T3.id, T2._ids )
            AND t3.area = t5.id
            AND t3.State = 1
    </select>

    <select id="getAllDeptTree" resultType="DeptAllTreeVO">
        SELECT
        T3.*
        FROM
        (
        SELECT
        @codes AS _ids,
        (
        SELECT @codes := GROUP_CONCAT( id ) FROM sys_dept WHERE FIND_IN_SET( pid, @codes ) ) AS T1
        FROM
        sys_dept s,
        ( SELECT @codes := #{id} ) T4
        WHERE
        @codes IS NOT NULL
        <if test="type != null">
            AND s.type in
            <foreach collection="type" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        ) T2,
        sys_dept T3,
        sys_area T5
        WHERE
        FIND_IN_SET( T3.id, T2._ids )
        AND t3.area = t5.id
        AND t3.State = 1
    </select>

    <select id="deptArea" resultType="String">
        SELECT sys_dept.area
        FROM sys_dept sys_dept
        WHERE sys_dept.state = #{state}
    </select>

    <select id="selectPid" resultType="Dept">
        SELECT sys_dept.*
        FROM sys_dept sys_dept
        WHERE sys_dept.pid in
        <foreach item="item" index="index" collection="ids" open="("
                 separator="," close=")">
            #{item}
        </foreach>
        AND sys_dept.state = #{state}
    </select>

    <delete id="deleteDeptLine">
        DELETE FROM pq_dept_line where id in
        <foreach item="item" index="index" collection="ids" open="("
                 separator="," close=")">
            #{item}
        </foreach>
    </delete>

    <update id="deleteUserDept">
        update sys_user set Dept_Id = "" where Dept_Id in
        <foreach item="item" index="index" collection="ids" open="("
                 separator="," close=")">
            #{item}
        </foreach>
    </update>

    <select id="getDeptDescendantIndexes" resultType="DeptDTO">
        SELECT
            T3.id,
            T3.pid,
            T3.pids,
            T3.NAME,
            T3.type,
            T5.NAME area ,
            T3.Remark
        FROM
            (
                SELECT
                    @codes AS _ids,
		    (
		        SELECT @codes := GROUP_CONCAT( id ) FROM sys_dept WHERE FIND_IN_SET( pid, @codes ) ) AS T1
                    FROM
                        sys_dept s,
                        ( SELECT @codes := #{id} ) T4
                    WHERE
                        @codes IS NOT NULL
                    <if test="type != null">
                        AND s.type in
                        <foreach collection="type" item="item" open="(" close=")" separator=",">
                            #{item}
                        </foreach>
                    </if>
            ) T2,
            sys_dept T3,
            sys_area T5
        WHERE
            FIND_IN_SET( T3.id, T2._ids )
            AND t3.area = t5.id
            AND t3.State = 1
        Order by T3.sort asc
    </select>


</mapper>
