<?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.xyzh.system.mapper.DepartmentMapper">

    <!-- 基础结果映射 -->
    <resultMap id="BaseResultMap" type="org.xyzh.common.dto.dept.TbSysDept">
        <id column="id" property="id" jdbcType="VARCHAR"/>
        <result column="dept_id" property="deptID" jdbcType="VARCHAR"/>
        <result column="name" property="name" jdbcType="VARCHAR"/>
        <result column="parent_id" property="parentID" jdbcType="VARCHAR"/>
        <result column="description" property="description" jdbcType="VARCHAR"/>
        <result column="creator" property="creator" jdbcType="VARCHAR"/>
        <result column="updater" property="updater" jdbcType="VARCHAR"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
        <result column="delete_time" property="deleteTime" jdbcType="TIMESTAMP"/>
        <result column="deleted" property="deleted" jdbcType="BOOLEAN"/>
    </resultMap>

    <!-- 基础字段 -->
    <sql id="Base_Column_List">
        id, dept_id, name, parent_id, description, creator, updater, 
        create_time, update_time, delete_time, deleted
    </sql>

    <!-- 通用条件 -->
    <sql id="Where_Clause">
        <where>
            deleted = 0
            <if test="deptID != null and deptID != ''">
                AND dept_id = #{deptID}
            </if>
            <if test="parentID != null and parentID != ''">
                AND parent_id = #{parentID}
            </if>
            <if test="name != null and name != ''">
                AND name LIKE CONCAT('%', #{name}, '%')
            </if>
        </where>
    </sql>

    <!-- selectDepts --> 

    <select id="selectDepts">
        SELECT 
        <include refid="Base_Column_List"/>
        FROM tb_sys_dept
        <include refid="Where_Clause"/>
        ORDER BY dept_id,create_time DESC
    </select>

    <!-- 根据父部门ID查询子部门列表 -->
    <select id="selectByParentId" resultMap="BaseResultMap">
        SELECT 
        <include refid="Base_Column_List"/>
        FROM tb_sys_dept
        WHERE deleted = 0 
        AND parent_id = #{parentId}
        ORDER BY dept_id,create_time DESC
    </select>

    <!-- 检查部门名称是否存在 -->
    <select id="countByDeptName" resultType="int">
        SELECT COUNT(1)
        FROM tb_sys_dept
        WHERE deleted = 0 
        AND name = #{deptName}
        <if test="excludeId != null and excludeId != ''">
            AND id != #{excludeId}
        </if>
    </select>

    <!-- 根据部门ID查询部门信息（包含父部门信息） -->
    <select id="selectDeptWithParent" resultMap="BaseResultMap">
        SELECT 
            d.id, d.dept_id, d.parent_id, d.name, d.description, 
            d.creator, d.updater, d.create_time, d.update_time, 
            d.delete_time, d.deleted,
            p.name AS parent_name
        FROM tb_sys_dept d
        LEFT JOIN tb_sys_dept p ON d.parent_id = p.dept_id AND p.deleted = 0
        WHERE d.deleted = 0 
        AND d.dept_id = #{deptId}
    </select>

    <!-- 查询部门树结构 -->
    <select id="selectDeptTree" resultMap="BaseResultMap">
        SELECT 
        <include refid="Base_Column_List"/>
        FROM tb_sys_dept
        WHERE deleted = 0
        ORDER BY 
            CASE WHEN parent_id IS NULL OR parent_id = '' THEN 0 ELSE 1 END,
            parent_id,
            create_time ASC
    </select>

    <!-- 批量删除部门（逻辑删除） -->
    <update id="batchDeleteByIds">
        UPDATE tb_sys_dept 
        SET deleted = 1,
            delete_time = NOW(),
            updater = #{updater}
        WHERE deleted = 0
        AND dept_id IN
        <foreach collection="deptIds" item="deptId" open="(" separator="," close=")">
            #{deptId}
        </foreach>
    </update>

    <!-- 插入部门 -->
    <insert id="insert" parameterType="org.xyzh.common.dto.dept.TbSysDept">
        INSERT INTO tb_sys_dept
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="deptID != null">dept_id,</if>
            <if test="parentID != null">parent_id,</if>
            <if test="name != null">name,</if>
            <if test="description != null">description,</if>
            <if test="creator != null">creator,</if>
            <if test="createTime != null">create_time,</if>
            deleted
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="deptID != null">#{deptID},</if>
            <if test="parentID != null">#{parentID},</if>
            <if test="name != null">#{name},</if>
            <if test="description != null">#{description},</if>
            <if test="creator != null">#{creator},</if>
            <if test="createTime != null">#{createTime},</if>
            0
        </trim>
    </insert>

    <!-- 更新部门 -->
    <update id="updateById" parameterType="org.xyzh.common.dto.dept.TbSysDept">
        UPDATE tb_sys_dept
        <set>
            <if test="deptID != null">dept_id = #{deptID},</if>
            <if test="parentID != null">parent_id = #{parentID},</if>
            <if test="name != null">name = #{name},</if>
            <if test="description != null">description = #{description},</if>
            <if test="updater != null">updater = #{updater},</if>
            update_time = NOW()
        </set>
        WHERE dept_id = #{deptID} AND deleted = 0
    </update>

    <!-- 根据ID删除（逻辑删除） -->
    <update id="deleteById">
        UPDATE tb_sys_dept 
        SET deleted = 1,
            delete_time = NOW()
        WHERE dept_id = #{deptID} AND deleted = 0
    </update>

</mapper>
