<?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.dept.TbSysDeptMapper">

    <!-- 结果映射 -->
    <resultMap id="BaseResultMap" type="org.xyzh.common.dto.sys.TbSysDeptDTO">
        <!-- 部门字段 -->
        <id 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="optsn" property="optsn" jdbcType="VARCHAR"/>
        <result column="creator" property="creator" jdbcType="VARCHAR"/>
        <result column="updater" property="updater" jdbcType="VARCHAR"/>
        <result column="dept_path" property="deptPath" jdbcType="VARCHAR"/>
        <result column="remark" property="remark" jdbcType="VARCHAR"/>
        <result column="create_time" property="createTime" jdbcType="DATE"/>
        <result column="update_time" property="updateTime" jdbcType="DATE"/>
        <result column="delete_time" property="deleteTime" jdbcType="DATE"/>
        <result column="deleted" property="deleted" jdbcType="BOOLEAN"/>
    </resultMap>

    <!-- VO结果映射（用于前端展示） -->
    <resultMap id="PermissionVOResultMap" type="org.xyzh.api.system.vo.PermissionVO">
        <!-- 部门字段 -->
        <id column="dept_id" property="deptId" jdbcType="VARCHAR"/>
        <result column="name" property="deptName" jdbcType="VARCHAR"/>
        <result column="parent_id" property="deptParentId" jdbcType="VARCHAR"/>
        <result column="description" property="deptDescription" jdbcType="VARCHAR"/>
        <!-- BaseVO 基础字段 -->
        <result column="optsn" property="optsn" jdbcType="VARCHAR"/>
        <result column="creator" property="creator" jdbcType="VARCHAR"/>
        <result column="updater" property="updater" jdbcType="VARCHAR"/>
        <result column="dept_path" property="deptPath" jdbcType="VARCHAR"/>
        <result column="remark" property="remark" jdbcType="VARCHAR"/>
        <result column="create_time" property="createTime" jdbcType="DATE"/>
        <result column="update_time" property="updateTime" jdbcType="DATE"/>
        <result column="delete_time" property="deleteTime" jdbcType="DATE"/>
        <result column="deleted" property="deleted" jdbcType="BOOLEAN"/>
    </resultMap>

    <!-- 基础列 -->
    <sql id="Base_Column_List">
        dept_id, name, parent_id, description,
        optsn, creator, updater, dept_path, remark, create_time, update_time, delete_time, deleted
    </sql>

    <!-- 插入系统部门（按表字段 + 默认值动态列，仅修改 insert） -->
    <insert id="insertDept" parameterType="org.xyzh.common.dto.sys.TbSysDeptDTO">
        INSERT INTO sys.tb_sys_dept
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <!-- 必填字段（NOT NULL 且无默认值）：根据建表约束，这里视 dept_id, name, optsn 为必填 -->
            dept_id,
            name,
            optsn,
            <!-- 其他字段（可空或有默认值）：动态拼接 -->
            <if test="parentId != null and parentId != ''">parent_id,</if>
            <if test="description != null and description != ''">description,</if>
            <if test="creator != null and creator != ''">creator,</if>
            <if test="deptPath != null and deptPath != ''">dept_path,</if>
            <if test="remark != null and remark != ''">remark,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="deleteTime != null">delete_time,</if>
            <if test="deleted != null">deleted,</if>
        </trim>
        VALUES
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <!-- 必填字段对应的值 -->
            #{deptId},
            #{name},
            #{optsn},
            <!-- 其他字段对应的值 -->
            <if test="parentId != null and parentId != ''">#{parentId},</if>
            <if test="description != null and description != ''">#{description},</if>
            <if test="creator != null and creator != ''">#{creator},</if>
            <if test="deptPath != null and deptPath != ''">#{deptPath},</if>
            <if test="remark != null and remark != ''">#{remark},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="deleteTime != null">#{deleteTime},</if>
            <if test="deleted != null">#{deleted},</if>
        </trim>
    </insert>

    <!-- 更新系统部门 -->
    <update id="updateDept" parameterType="org.xyzh.common.dto.sys.TbSysDeptDTO">
        UPDATE sys.tb_sys_dept
        <set>
            <if test="name != null and name != ''">
                name = #{name},
            </if>
            <if test="parentId != null">
                parent_id = #{parentId},
            </if>
            <if test="description != null">
                description = #{description},
            </if>
            <if test="updater != null and updater != ''">
                updater = #{updater},
            </if>
            <if test="deptPath != null and deptPath != ''">
                dept_path = #{deptPath},
            </if>
            <if test="remark != null">
                remark = #{remark},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime},
            </if>
        </set>
        WHERE dept_id = #{deptId}
        <if test="deleted != null">
            AND deleted = #{deleted}
        </if>
    </update>

    <!-- 删除系统部门 -->
    <update id="deleteDept" parameterType="org.xyzh.common.dto.sys.TbSysDeptDTO">
        UPDATE sys.tb_sys_dept
        SET deleted = true,
            delete_time = NOW()
        WHERE dept_id = #{deptId}
    </update>

    <!-- 根据条件查询系统部门列表 -->
    <select id="getDeptByFilter" resultMap="PermissionVOResultMap" parameterType="org.xyzh.common.dto.sys.TbSysDeptDTO">
        SELECT
        <include refid="Base_Column_List"/>
        FROM sys.tb_sys_dept
        <where>
            <if test="filter.deptId != null and filter.deptId != ''">
                AND dept_id = #{filter.deptId}
            </if>
            <if test="filter.name != null and filter.name != ''">
                AND name LIKE CONCAT('%', #{filter.name}, '%')
            </if>
            <if test="filter.parentId != null and filter.parentId != ''">
                AND parent_id = #{filter.parentId}
            </if>
            <if test="filter.deptPath != null and filter.deptPath != ''">
                AND dept_path LIKE CONCAT(#{filter.deptPath}, '%')
            </if>
            AND (deleted IS NULL OR deleted = false)
        </where>
        ORDER BY create_time DESC
    </select>

    <!-- 根据条件查询系统部门分页列表 -->
    <select id="getDeptPageByFilter" resultMap="PermissionVOResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM sys.tb_sys_dept
        <where>
            <if test="filter.deptId != null and filter.deptId != ''">
                AND dept_id = #{filter.deptId}
            </if>
            <if test="filter.name != null and filter.name != ''">
                AND name LIKE CONCAT('%', #{filter.name}, '%')
            </if>
            <if test="filter.parentId != null and filter.parentId != ''">
                AND parent_id = #{filter.parentId}
            </if>
            <if test="filter.deptPath != null and filter.deptPath != ''">
                AND dept_path LIKE CONCAT(#{filter.deptPath}, '%')
            </if>
            AND (deleted IS NULL OR deleted = false)
        </where>
        ORDER BY create_time DESC
        LIMIT #{pageParam.pageSize} OFFSET #{pageParam.offset}
    </select>

    <!-- 根据条件查询系统部门数量 -->
    <select id="getDeptCount" resultType="java.lang.Integer" parameterType="org.xyzh.common.dto.sys.TbSysDeptDTO">
        SELECT COUNT(1)
        FROM sys.tb_sys_dept
        <where>
            <if test="filter.deptId != null and filter.deptId != ''">
                AND dept_id = #{filter.deptId}
            </if>
            <if test="filter.name != null and filter.name != ''">
                AND name LIKE CONCAT('%', #{filter.name}, '%')
            </if>
            <if test="filter.parentId != null and filter.parentId != ''">
                AND parent_id = #{filter.parentId}
            </if>
            <if test="filter.deptPath != null and filter.deptPath != ''">
                AND dept_path LIKE CONCAT(#{filter.deptPath}, '%')
            </if>
            AND (deleted IS NULL OR deleted = false)
        </where>
    </select>

</mapper>

