<?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.ModuleMapper">
    
    <!-- 基础结果映射 -->
    <resultMap id="BaseResultMap" type="org.xyzh.common.dto.system.TbSysModule">
        <id column="id" property="id" jdbcType="VARCHAR"/>
        <result column="module_id" property="moduleID" jdbcType="VARCHAR"/>
        <result column="name" property="name" jdbcType="VARCHAR"/>
        <result column="code" property="code" jdbcType="VARCHAR"/>
        <result column="description" property="description" jdbcType="VARCHAR"/>
        <result column="icon" property="icon" jdbcType="VARCHAR"/>
        <result column="order_num" property="orderNum" jdbcType="INTEGER"/>
        <result column="status" property="status" jdbcType="INTEGER"/>
        <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, module_id, name, code, description, icon, order_num, status, creator, updater,
        create_time, update_time, delete_time, deleted
    </sql>

    <!-- 基础WHERE条件 -->
    <sql id="Base_Where_Clause">
        <where>
            <if test="filter != null">
                <if test="filter.id != null and filter.id != ''">
                    AND id = #{filter.id}
                </if>
                <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.code != null and filter.code != ''">
                    AND code = #{filter.code}
                </if>
                <if test="filter.status != null">
                    AND status = #{filter.status}
                </if>
                <if test="filter.deleted != null">
                    AND deleted = #{filter.deleted}
                </if>
            </if>
            AND deleted = 0
        </where>
    </sql>

    <!-- 查询模块列表 -->
    <select id="selectModules" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_sys_module
        <include refid="Base_Where_Clause" />
        ORDER BY order_num ASC, create_time DESC
    </select>

    <!-- 根据模块ID查询模块信息 -->
    <select id="selectByModuleID" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_sys_module
        WHERE module_id = #{moduleID} AND deleted = 0
    </select>

    <!-- 根据模块代码查询模块信息 -->
    <select id="selectByCode" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_sys_module
        WHERE code = #{code} AND deleted = 0
    </select>

    <!-- 查询启用的模块列表 -->
    <select id="selectActiveModules" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_sys_module
        WHERE status = 1 AND deleted = 0
        ORDER BY order_num ASC, create_time DESC
    </select>

    <!-- 检查模块代码是否存在 -->
    <select id="countByCode" resultType="int">
        SELECT COUNT(1)
        FROM tb_sys_module
        WHERE code = #{code} AND deleted = 0
        <if test="excludeID != null and excludeID != ''">
            AND id != #{excludeID}
        </if>
    </select>

    <!-- 插入模块 -->
    <insert id="insertModule" parameterType="org.xyzh.common.dto.system.TbSysModule">
        INSERT INTO tb_sys_module (
            id, module_id, name, code, description, icon, order_num, status,
            creator, updater, create_time, update_time, deleted
        ) VALUES (
            #{id}, #{moduleID}, #{name}, #{code}, #{description}, #{icon}, #{orderNum}, #{status},
            #{creator}, #{updater}, #{createTime}, #{updateTime}, #{deleted}
        )
    </insert>

    <!-- 更新模块 -->
    <update id="updateModule" parameterType="org.xyzh.common.dto.system.TbSysModule">
        UPDATE tb_sys_module
        <set>
            <if test="name != null and name != ''">
                name = #{name},
            </if>
            <if test="code != null and code != ''">
                code = #{code},
            </if>
            <if test="description != null">
                description = #{description},
            </if>
            <if test="icon != null">
                icon = #{icon},
            </if>
            <if test="orderNum != null">
                order_num = #{orderNum},
            </if>
            <if test="status != null">
                status = #{status},
            </if>
            <if test="updater != null and updater != ''">
                updater = #{updater},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime},
            </if>
        </set>
        WHERE id = #{id} AND deleted = 0
    </update>

    <!-- 删除模块 -->
    <update id="deleteModule" parameterType="org.xyzh.common.dto.system.TbSysModule">
        UPDATE tb_sys_module
        SET deleted = 1, delete_time = #{deleteTime}
        WHERE id = #{id} AND deleted = 0
    </update>

    <!-- 批量插入模块 -->
    <insert id="batchInsertModules" parameterType="java.util.List">
        INSERT INTO tb_sys_module (
            id, module_id, name, code, description, icon, order_num, status,
            creator, updater, create_time, update_time, deleted
        ) VALUES
        <foreach collection="moduleList" item="module" separator=",">
            (
                #{module.id}, #{module.moduleID}, #{module.name}, #{module.code}, #{module.description},
                #{module.icon}, #{module.orderNum}, #{module.status}, #{module.creator}, #{module.updater},
                #{module.createTime}, #{module.updateTime}, #{module.deleted}
            )
        </foreach>
    </insert>

    <!-- 批量删除模块 -->
    <update id="batchDeleteModules">
        UPDATE tb_sys_module
        SET deleted = 1, delete_time = NOW()
        WHERE id IN
        <foreach collection="moduleIDs" item="moduleID" open="(" separator="," close=")">
            #{moduleID}
        </foreach>
        AND deleted = 0
    </update>

    <!-- 分页查询模块列表 -->
    <select id="selectModulesPage" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_sys_module
        <include refid="Base_Where_Clause" />
        ORDER BY order_num ASC, create_time DESC
        LIMIT #{pageParam.pageSize} OFFSET #{pageParam.offset}
    </select>

    <!-- 统计模块数量 -->
    <select id="countModules" resultType="long">
        SELECT COUNT(1)
        FROM tb_sys_module
        <include refid="Base_Where_Clause" />
    </select>

    <!-- 更新模块状态 -->
    <update id="updateModuleStatus">
        UPDATE tb_sys_module
        SET status = #{status}, update_time = NOW()
        WHERE module_id = #{moduleID} AND deleted = 0
    </update>

    <!-- 更新模块排序 -->
    <update id="updateModuleOrder">
        UPDATE tb_sys_module
        SET order_num = #{orderNum}, update_time = NOW()
        WHERE module_id = #{moduleID} AND deleted = 0
    </update>

</mapper>
