<?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="ink.wgink.service.department.dao.IDepartmentDao">

    <cache flushInterval="3600000"/>

    <resultMap id="departmentDTO" type="ink.wgink.pojo.dtos.department.DepartmentDTO">
        <id property="departmentId" column="department_id"/>
        <result property="departmentParentId" column="department_parent_id"/>
        <result property="departmentParentName" column="department_parent_name"/>
        <result property="departmentName" column="department_name"/>
        <result property="departmentSummary" column="department_summary"/>
        <result property="departmentCode" column="department_code"/>
        <result property="departmentNameEn" column="department_name_en"/>
        <result property="departmentNameOther" column="department_name_other"/>
        <result property="departmentSummary" column="department_summary"/>
        <result property="departmentLogo" column="department_logo"/>
        <result property="departmentLogoHover" column="department_logo_hover"/>
        <result property="departmentType" column="department_type"/>
        <result property="departmentState" column="department_state"/>
        <result property="departmentFax" column="department_fax"/>
        <result property="departmentTel" column="department_tel"/>
        <result property="departmentAddress" column="department_address"/>
        <result property="departmentMaster" column="department_master"/>
        <result property="departmentMasterName" column="department_master_name"/>
        <result property="departmentDuty" column="department_duty"/>
        <result property="departmentArea1Id" column="department_area1_id"/>
        <result property="departmentArea1Code" column="department_area1_code"/>
        <result property="departmentArea1Name" column="department_area1_name"/>
        <result property="departmentArea2Id" column="department_area2_id"/>
        <result property="departmentArea2Code" column="department_area2_code"/>
        <result property="departmentArea2Name" column="department_area2_name"/>
        <result property="departmentArea3Id" column="department_area3_id"/>
        <result property="departmentArea3Code" column="department_area3_code"/>
        <result property="departmentArea3Name" column="department_area3_name"/>
        <result property="departmentArea4Id" column="department_area4_id"/>
        <result property="departmentArea4Code" column="department_area4_code"/>
        <result property="departmentArea4Name" column="department_area4_name"/>
        <result property="departmentArea5Id" column="department_area5_id"/>
        <result property="departmentArea5Code" column="department_area5_code"/>
        <result property="departmentArea5Name" column="department_area5_name"/>
        <result property="departmentLongitude" column="department_longitude"/>
        <result property="departmentLatitude" column="department_latitude"/>
        <result property="departmentOrder" column="department_order"/>
    </resultMap>

    <resultMap id="departmentPO" type="ink.wgink.pojo.pos.DepartmentPO">
        <id property="departmentId" column="department_id"/>
        <result property="departmentIntId" column="department_int_id"/>
        <result property="departmentParentId" column="department_parent_id"/>
        <result property="departmentParentName" column="department_parent_name"/>
        <result property="departmentName" column="department_name"/>
        <result property="departmentSummary" column="department_summary"/>
        <result property="departmentCode" column="department_code"/>
        <result property="departmentNameEn" column="department_name_en"/>
        <result property="departmentNameOther" column="department_name_other"/>
        <result property="departmentSummary" column="department_summary"/>
        <result property="departmentLogo" column="department_logo"/>
        <result property="departmentLogoHover" column="department_logo_hover"/>
        <result property="departmentType" column="department_type"/>
        <result property="departmentState" column="department_state"/>
        <result property="departmentFax" column="department_fax"/>
        <result property="departmentTel" column="department_tel"/>
        <result property="departmentAddress" column="department_address"/>
        <result property="departmentMaster" column="department_master"/>
        <result property="departmentMasterName" column="department_master_name"/>
        <result property="departmentDuty" column="department_duty"/>
        <result property="departmentArea1Id" column="department_area1_id"/>
        <result property="departmentArea1Code" column="department_area1_code"/>
        <result property="departmentArea1Name" column="department_area1_name"/>
        <result property="departmentArea2Id" column="department_area2_id"/>
        <result property="departmentArea2Code" column="department_area2_code"/>
        <result property="departmentArea2Name" column="department_area2_name"/>
        <result property="departmentArea3Id" column="department_area3_id"/>
        <result property="departmentArea3Code" column="department_area3_code"/>
        <result property="departmentArea3Name" column="department_area3_name"/>
        <result property="departmentArea4Id" column="department_area4_id"/>
        <result property="departmentArea4Code" column="department_area4_code"/>
        <result property="departmentArea4Name" column="department_area4_name"/>
        <result property="departmentArea5Id" column="department_area5_id"/>
        <result property="departmentArea5Code" column="department_area5_code"/>
        <result property="departmentArea5Name" column="department_area5_name"/>
        <result property="departmentLongitude" column="department_longitude"/>
        <result property="departmentLatitude" column="department_latitude"/>
        <result property="departmentOrder" column="department_order"/>
    </resultMap>

    <resultMap id="departmentZTreeDTO" type="ink.wgink.pojo.dtos.ZTreeDTO">
        <id property="id" column="department_id"/>
        <result property="pId" column="department_parent_id"/>
        <result property="name" column="department_name"/>
    </resultMap>

    <!-- 建表 -->
    <update id="createTable">
        CREATE TABLE IF NOT EXISTS `sys_department` (
            `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
            `department_id` char(36) NOT NULL,
            `department_parent_id` char(36) NOT NULL COMMENT '上级部门',
            `department_name` varchar(255) DEFAULT NULL COMMENT '机构名称',
            `department_name_en` varchar(255) DEFAULT NULL COMMENT '部门名称(英文)',
            `department_name_other` varchar(255) DEFAULT NULL COMMENT '部门名称(其他)',
            `department_summary` text COMMENT '部门简介',
            `department_code` varchar(255) DEFAULT NULL COMMENT '部门编码',
            `department_logo` char(36) DEFAULT NULL COMMENT '部门logo',
            `department_logo_hover` char(36) DEFAULT NULL COMMENT '部门logo:hover',
            `department_type` int(2) DEFAULT '1' COMMENT '部门类别,1:机构,2:部门',
            `department_state` int(2) DEFAULT '1' COMMENT '部门状态,1:正常,2:锁定,3:冻结',
            `department_fax` varchar(20) DEFAULT NULL COMMENT '部门传真',
            `department_tel` varchar(20) DEFAULT NULL COMMENT '部门电话',
            `department_address` varchar(255) DEFAULT NULL COMMENT '部门地址',
            `department_master` char(36) DEFAULT '0' COMMENT '部门负责人',
            `department_duty` varchar(255) DEFAULT NULL COMMENT '部门职责',
            `department_longitude` varchar(255) DEFAULT '0' COMMENT '部门经度',
            `department_latitude` varchar(255) DEFAULT '0' COMMENT '部门纬度',
            `department_order` int(11) DEFAULT '0' COMMENT '部门排序',
            `department_area1_id` bigint(20) DEFAULT NULL COMMENT '1级区域ID',
            `department_area1_code` varchar(255) DEFAULT NULL COMMENT '1级区域编码',
            `department_area1_name` varchar(255) DEFAULT NULL COMMENT '1级区域名称',
            `department_area2_id` bigint(20) DEFAULT NULL COMMENT '2级区域ID',
            `department_area2_code` varchar(255) DEFAULT NULL COMMENT '2级区域编码',
            `department_area2_name` varchar(255) DEFAULT NULL COMMENT '2级区域名称',
            `department_area3_id` bigint(20) DEFAULT NULL COMMENT '3级区域ID',
            `department_area3_code` varchar(255) DEFAULT NULL COMMENT '3级区域编码',
            `department_area3_name` varchar(255) DEFAULT NULL COMMENT '3级区域名称',
            `department_area4_id` bigint(20) DEFAULT NULL COMMENT '4级区域ID',
            `department_area4_code` varchar(255) DEFAULT NULL COMMENT '4级区域编码',
            `department_area4_name` varchar(255) DEFAULT NULL COMMENT '4级区域名称',
            `department_area5_id` bigint(20) DEFAULT NULL COMMENT '5级区域ID',
            `department_area5_code` varchar(255) DEFAULT NULL COMMENT '5级区域编码',
            `department_area5_name` varchar(255) DEFAULT NULL COMMENT '5级区域名称',
            `creator` char(36) DEFAULT NULL,
            `gmt_create` datetime DEFAULT NULL,
            `modifier` char(36) DEFAULT NULL,
            `gmt_modified` datetime DEFAULT NULL,
            `is_delete` int(2) DEFAULT '0',
            PRIMARY KEY (`id`,`department_id`),
            UNIQUE KEY `department_id` (`department_id`) USING BTREE
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    </update>

    <!-- 新增组织部门 -->
    <insert id="save" parameterType="map" flushCache="true">
        INSERT INTO sys_department(
            department_id,
            department_parent_id,
            department_name,
            department_name_en,
            department_name_other,
            department_summary,
            department_code,
            department_logo,
            department_logo_hover,
            department_type,
            department_state,
            department_fax,
            department_tel,
            department_address,
            department_master,
            department_duty,
            department_area1_id,
            department_area1_code,
            department_area1_name,
            department_area2_id,
            department_area2_code,
            department_area2_name,
            department_area3_id,
            department_area3_code,
            department_area3_name,
            department_area4_id,
            department_area4_code,
            department_area4_name,
            department_area5_id,
            department_area5_code,
            department_area5_name,
            department_longitude,
            department_latitude,
            department_order,
            creator,
            gmt_create,
            modifier,
            gmt_modified,
            is_delete
        ) VALUES(
            #{departmentId},
            #{departmentParentId},
            #{departmentName},
            #{departmentNameEn},
            #{departmentNameOther},
            #{departmentSummary},
            #{departmentCode},
            #{departmentLogo},
            #{departmentLogoHover},
            #{departmentType},
            #{departmentState},
            #{departmentFax},
            #{departmentTel},
            #{departmentAddress},
            #{departmentMaster},
            #{departmentDuty},
            #{departmentArea1Id},
            #{departmentArea1Code},
            #{departmentArea1Name},
            #{departmentArea2Id},
            #{departmentArea2Code},
            #{departmentArea2Name},
            #{departmentArea3Id},
            #{departmentArea3Code},
            #{departmentArea3Name},
            #{departmentArea4Id},
            #{departmentArea4Code},
            #{departmentArea4Name},
            #{departmentArea5Id},
            #{departmentArea5Code},
            #{departmentArea5Name},
            #{departmentLongitude},
            #{departmentLatitude},
            #{departmentOrder},
            #{creator},
            #{gmtCreate},
            #{modifier},
            #{gmtModified},
            #{isDelete}
        )
    </insert>

    <!-- 删除组织部门 -->
    <update id="remove" parameterType="map" flushCache="true">
        UPDATE
            sys_department
        SET
            is_delete = 1,
            modifier = #{modifier},
            gmt_modified = #{gmtModified}
        WHERE
            department_id IN
        <foreach collection="departmentIds" index="index" open="(" separator="," close=")">
            #{departmentIds[${index}]}
        </foreach>
    </update>

    <!-- 修改组织部门 -->
    <update id="update" parameterType="map" flushCache="true">
        UPDATE
            sys_department
        SET
        <if test="departmentName != null">
            department_name = #{departmentName},
        </if>
        <if test="departmentNameEn != null">
            department_name_en = #{departmentNameEn},
        </if>
        <if test="departmentNameOther != null">
            department_name_other = #{departmentNameOther},
        </if>
        <if test="departmentSummary != null">
            department_summary = #{departmentSummary},
        </if>
        <if test="departmentCode != null and departmentCode != ''">
            department_code = #{departmentCode},
        </if>
        <if test="departmentLogo != null and departmentLogo != ''">
            department_logo = #{departmentLogo},
        </if>
        <if test="departmentLogoHover != null">
            department_logo_hover = #{departmentLogoHover},
        </if>
        <if test="departmentType != null">
            department_type = #{departmentType},
        </if>
        <if test="departmentState != null">
            department_state = #{departmentState},
        </if>
        <if test="departmentFax != null">
            department_fax = #{departmentFax},
        </if>
        <if test="departmentTel != null">
            department_tel = #{departmentTel},
        </if>
        <if test="departmentAddress != null">
            department_address = #{departmentAddress},
        </if>
        <if test="departmentMaster != null">
            department_master = #{departmentMaster},
        </if>
        <if test="departmentDuty != null">
            department_duty = #{departmentDuty},
        </if>
        department_area1_id = #{departmentArea1Id},
        <if test="departmentArea1Code != null">
            department_area1_code = #{departmentArea1Code},
        </if>
        <if test="departmentArea1Name != null">
            department_area1_name = #{departmentArea1Name},
        </if>
        department_area2_id = #{departmentArea2Id},
        <if test="departmentArea2Code != null">
            department_area2_code = #{departmentArea2Code},
        </if>
        <if test="departmentArea2Name != null">
            department_area2_name = #{departmentArea2Name},
        </if>
        department_area3_id = #{departmentArea3Id},
        <if test="departmentArea3Code != null">
            department_area3_code = #{departmentArea3Code},
        </if>
        <if test="departmentArea3Name != null">
            department_area3_name = #{departmentArea3Name},
        </if>
        department_area4_id = #{departmentArea4Id},
        <if test="departmentArea4Code != null">
            department_area4_code = #{departmentArea4Code},
        </if>
        <if test="departmentArea4Name != null">
            department_area4_name = #{departmentArea4Name},
        </if>
        department_area5_id = #{departmentArea5Id},
        <if test="departmentArea5Code != null">
            department_area5_code = #{departmentArea5Code},
        </if>
        <if test="departmentArea5Name != null">
            department_area5_name = #{departmentArea5Name},
        </if>
        <if test="departmentLongitude != null">
            department_longitude = #{departmentLongitude},
        </if>
        <if test="departmentLatitude != null">
            department_latitude = #{departmentLatitude},
        </if>
        <if test="departmentOrder != null">
            department_order = #{departmentOrder},
        </if>
            modifier = #{modifier},
            gmt_modified = #{gmtModified}
        WHERE
            department_id = #{departmentId}
    </update>

    <!-- 组织部门编码更新 -->
    <update id="updateCode" parameterType="map" flushCache="true">
        UPDATE
            sys_department
        SET
            department_code = #{departmentCode}
        WHERE
            department_id = #{departmentId}
    </update>

    <!-- 更新部门状态 -->
    <update id="updateState" parameterType="map" flushCache="true">
        UPDATE
            sys_department
        SET
            department_state = #{departmentState}
        WHERE
            department_id = #{departmentId}
    </update>

    <!-- ztree列表 -->
    <select id="listZTree" parameterType="map" resultMap="departmentZTreeDTO" useCache="true">
        SELECT
            *
        FROM
            sys_department
        WHERE
            is_delete = 0
        <if test="departmentParentId != null and departmentParentId != ''">
        AND
            department_parent_id = #{departmentParentId}
        </if>
        <if test="departmentIds != null and departmentIds.size > 0">
        AND
            department_id IN
            <foreach collection="departmentIds" index="index" open="(" separator="," close=")">
                #{departmentIds[${index}]}
            </foreach>
        </if>
        ORDER BY
            department_order
    </select>

    <!-- ztree详情 -->
    <select id="getZTree" parameterType="map" resultMap="departmentZTreeDTO" useCache="false">
        SELECT
            department_id,
            department_parent_id,
            department_name,
            department_name_en,
            department_name_other,
            department_summary,
            department_code,
            department_logo,
            department_logo_hover,
            department_type,
            department_state,
            department_fax,
            department_tel,
            department_address,
            department_master,
            department_duty,
            department_longitude,
            department_latitude,
            department_order,
            creator,
            gmt_create,
            modifier,
            gmt_modified,
            is_delete
        FROM
            sys_department
        WHERE
            is_delete = 0
        <if test="departmentId != null and departmentId != ''">
        AND
            department_id = #{departmentId}
        </if>
        <if test="departmentState != null">
        AND
            department_state = #{departmentState}
        </if>
    </select>

    <!-- 组织部门列表 -->
    <select id="list" parameterType="map" resultMap="departmentDTO" useCache="true">
        SELECT
            department_id,
            department_parent_id,
            department_name,
            department_name_en,
            department_name_other,
            department_summary,
            department_code,
            department_logo,
            department_logo_hover,
            department_type,
            department_state,
            department_fax,
            department_tel,
            department_address,
            department_master,
            department_duty,
            department_area1_id,
            department_area1_code,
            department_area1_name,
            department_area2_id,
            department_area2_code,
            department_area2_name,
            department_area3_id,
            department_area3_code,
            department_area3_name,
            department_area4_id,
            department_area4_code,
            department_area4_name,
            department_area5_id,
            department_area5_code,
            department_area5_name,
            department_longitude,
            department_latitude,
            department_order,
            creator,
            gmt_create,
            modifier,
            gmt_modified,
            is_delete
        FROM
            sys_department
        WHERE
            is_delete = 0
        <if test="departmentParentId != null and departmentParentId != ''">
        AND
            department_parent_id = #{departmentParentId}
        </if>
        <if test="departmentCode != null and departmentCode != ''">
        AND
            department_code LIKE CONCAT(#{departmentCode}, '%')
        </if>
        <if test="departmentIds != null and departmentIds.size > 0">
        AND
            department_id IN
            <foreach collection="departmentIds" index="index" open="(" separator="," close=")">
                #{departmentIds[${index}]}
            </foreach>
        </if>
        <if test="departmentName != null and departmentName != ''">
        AND
            department_name = #{departmentName}
        </if>
        <if test="departmentState != null">
        AND
            department_state = #{departmentState}
        </if>
        <if test="keywords != null and keywords != ''">
        AND
            department_name LIKE CONCAT('%', #{keywords}, '%')
        </if>
        <if test="startTime != null and startTime != ''">
        AND
            gmt_create <![CDATA[ >= ]]> #{startTime}
        </if>
        <if test="startTime != null and startTime != ''">
        AND
            gmt_create <![CDATA[ <= ]]> #{endTime}
        </if>
        <choose>
            <when test="sort != null and (sort == 'departmentName' or sort == 'departmentNameEn' or sort == 'departmentNameOther' or sort == 'departmentCode' or sort == 'departmentType' or sort == 'departmentState')">
                ORDER BY
                <if test="sort == 'departmentName'">
                    department_name ${order}
                </if>
                <if test="sort == 'departmentNameEn'">
                    department_name_en ${order}
                </if>
                <if test="sort == 'departmentNameOther'">
                    department_name_other ${order}
                </if>
                <if test="sort == 'departmentCode'">
                    department_code ${order}
                </if>
                <if test="sort == 'departmentType'">
                    department_type ${order}
                </if>
                <if test="sort == 'departmentState'">
                    department_state ${order}
                </if>
            </when>
            <otherwise>
            ORDER BY
                department_order ASC
            </otherwise>
        </choose>
    </select>

    <!-- 组织部门详情 -->
    <select id="get" parameterType="map" resultMap="departmentDTO" useCache="false">
        SELECT
            t1.*,
            jt1.department_name department_parent_name,
            jt2.user_name department_master_name
        FROM
            sys_department t1
        LEFT JOIN
            sys_department jt1
        ON
            t1.department_parent_id = jt1.department_id
        AND
            jt1.is_delete = 0
        LEFT JOIN
            sys_user jt2
        ON
            t1.department_master = jt2.user_id
        AND
            jt2.is_delete = 0
        WHERE
            t1.is_delete = 0
        <if test="departmentId != null and departmentId != ''">
        AND
            t1.department_id = #{departmentId}
        </if>
        <if test="departmentParentId != null and departmentParentId != ''">
        AND
            t1.department_parent_id = #{departmentParentId}
        </if>
        <if test="excludeDepartmentId != null and excludeDepartmentId != ''">
        AND
            t1.department_id != #{excludeDepartmentId}
        </if>
        <if test="departmentName != null and departmentName != ''">
        AND
            t1.department_name = #{departmentName}
        </if>
        <if test="departmentState != null">
        AND
            t1.department_state = #{departmentState}
        </if>
    </select>

    <!-- 获取组织机构通过名称和上级名称 -->
    <select id="getByNameAndParentName" parameterType="map" resultMap="departmentDTO" useCache="false">
        SELECT
            t1.*,
            jt1.department_name department_parent_name,
            jt2.user_name department_master_name
        FROM
            sys_department t1
        LEFT JOIN
            sys_department jt1
        ON
            t1.department_parent_id = jt1.department_id
        AND
            jt1.is_delete = 0
        LEFT JOIN
            sys_user jt2
        ON
            t1.department_master = jt2.user_id
        AND
            jt2.is_delete = 0
        WHERE
            t1.is_delete = 0
        AND
            t1.department_name = #{departmentName}
        <if test="departmentParentName != null and departmentParentName != ''">
        AND
            jt1.department_name = #{departmentParentName}
        </if>
        <if test="departmentParentName == null or departmentParentName == ''">
        AND
            jt1.department_name IS NULL
        </if>
        <if test="departmentState != null">
        AND
            t1.department_state = #{departmentState}
        </if>
    </select>

    <!-- 子节点数量 -->
    <select id="countByParentId" parameterType="String" resultType="Integer" useCache="false">
        SELECT
            COUNT(*)
        FROM
            sys_department
        WHERE
            is_delete = 0
        AND
            department_parent_id = #{_parameter}
    </select>

    <!-- 获取最后一个子组织部门，实际数据，包含已删除，方式编码重复 -->
    <select id="getLastByParentId" parameterType="String" resultMap="departmentDTO" useCache="false">
        SELECT
            *
        FROM
            sys_department
        WHERE
            department_parent_id = #{_parameter}
        ORDER BY
            department_code DESC
        LIMIT 0, 1
    </select>

    <!-- 获取组织机构详情（简单类型） -->
    <select id="getSimple" parameterType="map" resultMap="departmentPO" useCache="false">
        SELECT
            id department_int_id,
            department_id,
            department_parent_id,
            department_name,
            department_name_en,
            department_name_other,
            department_summary,
            department_code,
            department_logo,
            department_logo_hover,
            department_type,
            department_state,
            department_fax,
            department_tel,
            department_address,
            department_master,
            department_duty,
            department_area1_id,
            department_area1_code,
            department_area1_name,
            department_area2_id,
            department_area2_code,
            department_area2_name,
            department_area3_id,
            department_area3_code,
            department_area3_name,
            department_area4_id,
            department_area4_code,
            department_area4_name,
            department_area5_id,
            department_area5_code,
            department_area5_name,
            department_longitude,
            department_latitude,
            department_order
        FROM
            sys_department
        WHERE
            is_delete = 0
        <if test="departmentId != null and departmentId != ''">
        AND
            department_id = #{departmentId}
        </if>
        <if test="departmentIntId != null">
        AND
            id = #{departmentIntId}
        </if>
    </select>

    <!-- 获取组织机构列表（简单类型） -->
    <select id="listPO" parameterType="map" resultMap="departmentPO" useCache="false">
        SELECT
            id department_int_id,
            department_id,
            department_parent_id,
            department_name,
            department_name_en,
            department_name_other,
            department_summary,
            department_code,
            department_logo,
            department_logo_hover,
            department_type,
            department_state,
            department_fax,
            department_tel,
            department_address,
            department_master,
            department_duty,
            department_area1_id,
            department_area1_code,
            department_area1_name,
            department_area2_id,
            department_area2_code,
            department_area2_name,
            department_area3_id,
            department_area3_code,
            department_area3_name,
            department_area4_id,
            department_area4_code,
            department_area4_name,
            department_area5_id,
            department_area5_code,
            department_area5_name,
            department_longitude,
            department_latitude,
            department_order
        FROM
            sys_department
        WHERE
            is_delete = 0
        <if test="departmentParentId != null and departmentParentId != ''">
        AND
            department_parent_id = #{departmentParentId}
        </if>
        <if test="departmentCode != null and departmentCode != ''">
        AND
            department_code LIKE CONCAT(#{departmentCode}, '%')
        </if>
        <if test="departmentIds != null and departmentIds.size > 0">
        AND
            department_id IN
            <foreach collection="departmentIds" index="index" open="(" separator="," close=")">
                #{departmentIds[${index}]}
            </foreach>
        </if>
        <if test="departmentName != null and departmentName != ''">
        AND
            department_name = #{departmentName}
        </if>
        <if test="keywords != null and keywords != ''">
        AND
            department_name LIKE CONCAT('%', #{keywords}, '%')
        </if>
        <if test="departmentState != null">
        AND
            department_state = #{departmentState}
        </if>
        <if test="startTime != null and startTime != ''">
        AND
            gmt_create <![CDATA[ >= ]]> #{startTime}
        </if>
        <if test="startTime != null and startTime != ''">
        AND
            gmt_create <![CDATA[ <= ]]> #{endTime}
        </if>
        <if test="addDate != null and addDate != ''">
        AND
            gmt_create <![CDATA[ >= ]]> #{addDate}
        </if>
        <if test="updateDate != null and updateDate != ''">
        AND
            gmt_modified <![CDATA[ >= ]]> #{updateDate}
        </if>
        <if test="orderByCode != null">
        ORDER BY
            department_code
        </if>
    </select>

    <!-- 统计部门数量 -->
    <select id="count" parameterType="map" resultType="java.lang.Integer" useCache="true">
        SELECT
            COUNT(*)
        FROM
            sys_department
        WHERE
            is_delete = 0
        <if test="departmentId != null and departmentId != ''">
        AND
            department_id = #{departmentId}
        </if>
        <if test="departmentParentId != null and departmentParentId != ''">
        AND
            department_parent_id = #{departmentParentId}
        </if>
        <if test="departmentName != null and departmentName != ''">
        AND
            department_name = #{departmentName}
        </if>
        <if test="noDepartmentId != null and noDepartmentId != ''">
        AND
            department_id != #{noDepartmentId}
        </if>
        <if test="departmentType != null">
        AND
            department_type = #{departmentType}
        </if>
        <if test="departmentState != null">
        AND
            department_state = #{departmentState}
        </if>
    </select>

    <!-- 部门ID列表 -->
    <select id="listId" parameterType="map" resultType="java.lang.String" useCache="true">
        SELECT
            department_id
        FROM
            sys_department
        WHERE
            is_delete = 0
        <if test="area1 != null">
        AND (
            department_area1_id = #{area1}
            <if test="local != null">
            AND ((
                    department_area2_id IS NULL
                    OR
                    department_area2_id = ''
                )
                AND (
                    department_area3_id IS NULL
                    OR
                    department_area3_id = ''
                )
                AND (
                    department_area4_id IS NULL
                    OR
                    department_area4_id = ''
                )
                AND (
                    department_area5_id IS NULL
                    OR
                    department_area5_id = ''
                )
            )
            </if>
        )
        </if>
        <if test="area2 != null">
        AND (
            department_area2_id = #{area2}
            <if test="local != null">
            AND ((
                    department_area3_id IS NULL
                    OR
                    department_area3_id = ''
                )
                AND (
                    department_area4_id IS NULL
                    OR
                    department_area4_id = ''
                )
                AND (
                    department_area5_id IS NULL
                    OR
                    department_area5_id = ''
                )
            )
            </if>
        )
        </if>
        <if test="area3 != null">
        AND (
            department_area3_id = #{area3}
            <if test="local != null">
            AND ((
                    department_area4_id IS NULL
                    OR
                    department_area4_id = ''
                )
                AND (
                    department_area5_id IS NULL
                    OR
                    department_area5_id = ''
                )
            )
            </if>
        )
        </if>
        <if test="area4 != null">
        AND (
            department_area4_id = #{area4}
            <if test="local != null">
            AND ((
                    department_area5_id IS NULL
                    OR
                    department_area5_id = ''
                )
            )
            </if>
        )
        </if>
        <if test="area5 != null">
        AND
            department_area5_id = #{area5}
        </if>
    </select>

</mapper>