<?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}
        and sys_dept.state = 1
    </select>

    <select id="getDeptTree" resultType="DeptTreeVO">
        SELECT
        T3.*,
        T5.id area,
        T5.Short_Name areaName
        FROM
        sys_dept T3,
        sys_area T5
        WHERE
        FIND_IN_SET( #{id}, T3.pids )
        and t3.type in
        <foreach collection="type" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        AND t3.area = t5.id
        AND t3.State = 1
        UNION
        SELECT
        T3.*,
        T5.id area,
        T5.Short_Name areaName
        FROM
        sys_dept T3,
        sys_area T5
        WHERE
        t3.area = t5.id
        and T3.id = #{id}
        AND t3.State = 1
    </select>

    <select id="getAllDeptTree" resultType="DeptAllTreeVO">
        SELECT
        T3.*
        FROM
        sys_dept T3,
        sys_area T5
        WHERE
        FIND_IN_SET( #{id}, T3.pids )
        and t3.type in
        <foreach collection="type" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        AND t3.area = t5.id
        AND t3.State = 1
        UNION
        SELECT
        T3.*
        FROM
        sys_dept T3,
        sys_area T5
        WHERE
        t3.area = t5.id
        and T3.id = #{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
        *
        FROM
        (
        SELECT
        T3.id,
        T3.pid,
        T3.pids,
        T3.NAME,
        T3.CODE,
        T3.type,
        T5.Short_Name area,
        T3.Remark,
        T3.sort
        FROM
        sys_dept T3,
        sys_area T5
        WHERE
        FIND_IN_SET( #{id}, T3.pids )
        and t3.type in
        <foreach collection="type" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        AND t3.area = t5.id
        AND t3.State = 1
        UNION
        SELECT
        T3.id,
        T3.pid,
        T3.pids,
        T3.NAME,
        T3.CODE,
        T3.type,
        T5.Short_Name area,
        T3.Remark,
        T3.sort
        FROM
        sys_dept T3,
        sys_area T5
        WHERE
        T3.area = T5.id
        AND T3.id = #{id}
        AND T3.State = 1
        ) T3
        ORDER BY
        T3.sort ASC;
    </select>


    <select id="allDeptList" resultType="PvTerminalTreeVO">
        select id,name,pid,pids,code from sys_dept where state = 1 order by sort
    </select>


    <select id="getDeptSonIds" resultType="String">
     select id from sys_dept where find_in_set(#{deptId},pids) and state  = 1
    </select>

    <select id="getDeptSonSlfeIds" resultType="String">
     select code from sys_dept where find_in_set(#{deptId},pids) and state  = 1
    </select>



    <select id="getDeptSonId" resultType="String">
        select id from sys_dept where sys_dept.Pid=#{deptId} and state  = 1
    </select>

</mapper>
