<?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.yami.shop.dao.DeptMapper">

    <resultMap id="BaseResultMap" type="com.yami.shop.bean.model.Dept">
        <id column="dept_id" property="deptId"/>
        <result column="dept_name" property="deptName"/>
        <result column="description" property="description"/>
        <result column="parent_id" property="parentId"/>
        <result column="level" property="level"/>
        <result column="seq" property="seq"/>
        <result column="status" property="status"/>
        <result column="is_delete" property="isDelete"/>
        <result column="leader_user_id" property="leaderUserId"/>
        <result column="phone" property="phone"/>
        <result column="email" property="email"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
    </resultMap>

    <resultMap id="DeptWithLeaderMap" type="com.yami.shop.bean.model.Dept" extends="BaseResultMap">
        <result column="leader_user_name" property="leaderUserName"/>
    </resultMap>

    <!-- 根据父级部门ID获取子部门列表 -->
    <select id="selectByParentId" resultMap="BaseResultMap">
        SELECT  dept_id, dept_name, description, parent_id, level, seq, status, is_delete, leader_user_id, phone, email, create_time, update_time
        FROM tz_sys_dept
        WHERE parent_id = #{parentId} AND is_delete = 0
        ORDER BY seq
    </select>

    <!-- 获取部门树结构（包含负责人姓名） -->
    <select id="selectDeptTree" resultMap="DeptWithLeaderMap">
        SELECT
            d.dept_id,
            d.dept_name,
            d.description,
            d.parent_id,
            d.level,
            d.seq,
            d.status,
            d.is_delete,
            d.leader_user_id,
            d.phone,
            d.email,
            d.create_time,
            d.update_time,
            u.username as leader_user_name
        FROM tz_sys_dept d
                 LEFT JOIN tz_sys_user u ON d.leader_user_id = u.user_id
        WHERE d.status = 1 AND d.is_delete = 0
        ORDER BY d.parent_id, d.seq
    </select>

    <!-- 根据用户ID获取所属部门列表（包含负责人姓名） -->
    <select id="selectDeptsByUserId" resultMap="DeptWithLeaderMap">
        SELECT
            d.dept_id,
            d.dept_name,
            d.description,
            d.parent_id,
            d.level,
            d.seq,
            d.status,
            d.is_delete,
            d.leader_user_id,
            d.phone,
            d.email,
            d.create_time,
            d.update_time,
            u.username as leader_user_name
        FROM tz_sys_dept d
                 INNER JOIN tz_sys_dept_user du ON d.dept_id = du.dept_id
                 LEFT JOIN tz_sys_user u ON d.leader_user_id = u.user_id
        WHERE du.user_id = #{userId} AND d.is_delete = 0
        ORDER BY d.parent_id, d.seq
    </select>
    <select id="selectListAll" resultType="com.yami.shop.bean.model.Dept">
        SELECT
            dept.dept_id,
            dept.dept_name,
            dept.description,
            dept.parent_id,
            dept.level,
            dept.seq,
            dept.status,
            dept.is_delete,
            dept.leader_user_id,
            dept.phone,
            dept.email,
            dept.create_time,
            dept.update_time,
            tsu.username as leaderUserName
        FROM tz_sys_dept dept left join tz_sys_user tsu on dept.leader_user_id = tsu.user_id
        WHERE dept.is_delete = 0
        ORDER BY dept.seq
    </select>

</mapper>
