<?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.module.TbSysModuleMapper">

    <!-- 结果映射 -->
    <resultMap id="BaseResultMap" type="org.xyzh.common.dto.sys.TbSysModuleDTO">
        <!-- 模块字段 -->
        <id column="module_id" property="moduleId" jdbcType="VARCHAR"/>
        <result column="name" property="name" 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="module_id" property="moduleId" jdbcType="VARCHAR"/>
        <result column="name" property="moduleName" jdbcType="VARCHAR"/>
        <result column="description" property="moduleDescription" 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">
        module_id, name, description,
        optsn, creator, updater, dept_path, remark, create_time, update_time, delete_time, deleted
    </sql>

    <!-- 插入系统模块（必填 + 可选字段动态列，仅修改 insert） -->
    <insert id="insertModule" parameterType="org.xyzh.common.dto.sys.TbSysModuleDTO">
        INSERT INTO sys.tb_sys_module
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <!-- 必填字段：module_id, name, optsn -->
            module_id,
            name,
            optsn,
            <!-- 可选字段：description 及基础字段 -->
            <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=",">
            <!-- 必填字段对应的值 -->
            #{moduleId},
            #{name},
            #{optsn},
            <!-- 可选字段对应的值 -->
            <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="updateModule" parameterType="org.xyzh.common.dto.sys.TbSysModuleDTO">
        UPDATE sys.tb_sys_module
        <set>
            <if test="name != null and name != ''">
                name = #{name},
            </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 module_id = #{moduleId}
        <if test="deleted != null">
            AND deleted = #{deleted}
        </if>
    </update>

    <!-- 删除系统模块 -->
    <update id="deleteModule" parameterType="org.xyzh.common.dto.sys.TbSysModuleDTO">
        UPDATE sys.tb_sys_module
        SET deleted = true,
            delete_time = NOW()
        WHERE module_id = #{moduleId}
    </update>

    <!-- 根据模块ID查询系统模块 -->
    <select id="getModuleById" resultMap="PermissionVOResultMap" parameterType="java.lang.String">
        SELECT
        <include refid="Base_Column_List"/>
        FROM sys.tb_sys_module
        WHERE module_id = #{moduleId}
        AND (deleted IS NULL OR deleted = false)
    </select>

    <!-- 根据条件查询系统模块列表 -->
    <select id="getModuleByFilter" resultMap="PermissionVOResultMap" parameterType="org.xyzh.common.dto.sys.TbSysModuleDTO">
        SELECT
        <include refid="Base_Column_List"/>
        FROM sys.tb_sys_module
        <where>
            <if test="filter.moduleId != null and filter.moduleId != ''">
                AND module_id = #{filter.moduleId}
            </if>
            <if test="filter.name != null and filter.name != ''">
                AND name LIKE CONCAT('%', #{filter.name}, '%')
            </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="getModulePageByFilter" resultMap="PermissionVOResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM sys.tb_sys_module
        <where>
            <if test="filter.moduleId != null and filter.moduleId != ''">
                AND module_id = #{filter.moduleId}
            </if>
            <if test="filter.name != null and filter.name != ''">
                AND name LIKE CONCAT('%', #{filter.name}, '%')
            </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="getModuleCount" resultType="java.lang.Integer" parameterType="org.xyzh.common.dto.sys.TbSysModuleDTO">
        SELECT COUNT(1)
        FROM sys.tb_sys_module
        <where>
            <if test="filter.moduleId != null and filter.moduleId != ''">
                AND module_id = #{filter.moduleId}
            </if>
            <if test="filter.name != null and filter.name != ''">
                AND name LIKE CONCAT('%', #{filter.name}, '%')
            </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>

