<?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.SysConfigMapper">

    <!-- 结果映射 -->
    <resultMap id="BaseResultMap" type="org.xyzh.common.dto.system.TbSysConfig">
        <id column="id" property="ID" />
        <result column="config_key" property="configKey" />
        <result column="config_name" property="configName" />
        <result column="config_value" property="configValue" />
        <result column="config_type" property="configType" />
        <result column="render_type" property="renderType" />
        <result column="config_group" property="configGroup" />
        <result column="description" property="description" />
        <result column="placeholder" property="placeholder" />
        <result column="remark" property="remark" />
        <result column="rows" property="rows" />
        <result column="min_value" property="minValue" />
        <result column="max_value" property="maxValue" />
        <result column="unit" property="unit" />
        <result column="options" property="options" />
        <result column="order_num" property="orderNum" />
        <result column="is_system" property="isSystem" />
        <result column="creator" property="creator" />
        <result column="updater" property="updater" />
        <result column="create_time" property="createTime" />
        <result column="update_time" property="updateTime" />
        <result column="delete_time" property="deleteTime" />
        <result column="deleted" property="deleted" />
    </resultMap>

    <!-- 字段列表 -->
    <sql id="Base_Column_List">
        id, config_key, config_name, config_value, config_type, render_type, config_group, 
        description, placeholder, remark, `rows`, min_value, max_value, unit, `options`, order_num,
        is_system, creator, updater, create_time, update_time, delete_time, deleted
    </sql>

    <!-- 查询条件 -->
    <sql id="Base_Where_Clause">
        <where>
            deleted = 0
            <if test="filter.configKey != null and filter.configKey != ''">
                AND config_key = #{filter.configKey}
            </if>
            <if test="filter.configGroup != null and filter.configGroup != ''">
                AND config_group = #{filter.configGroup}
            </if>
            <if test="filter.configType != null and filter.configType != ''">
                AND config_type = #{filter.configType}
            </if>
            <if test="filter.isSystem != null">
                AND is_system = #{filter.isSystem}
            </if>
        </where>
    </sql>

    <!-- 插入系统配置 -->
    <insert id="insertSysConfig" parameterType="org.xyzh.common.dto.system.TbSysConfig">
        INSERT INTO tb_sys_config (
            id, config_key, config_name, config_value, config_type, render_type, config_group, 
            description, placeholder, remark, `rows`, min_value, max_value, unit, `options`, order_num,
            is_system, creator, create_time
        ) VALUES (
            #{config.ID}, #{config.configKey}, #{config.configName}, #{config.configValue}, 
            #{config.configType}, #{config.renderType}, #{config.configGroup}, #{config.description}, 
            #{config.placeholder}, #{config.remark}, #{config.rows}, #{config.minValue}, 
            #{config.maxValue}, #{config.unit}, #{config.options}, #{config.orderNum},
            #{config.isSystem}, #{config.creator}, NOW()
        )
    </insert>

    <!-- 更新系统配置 -->
    <update id="updateSysConfig" parameterType="org.xyzh.common.dto.system.TbSysConfig">
        UPDATE tb_sys_config
        SET config_name = #{config.configName},
            config_value = #{config.configValue},
            config_type = #{config.configType},
            render_type = #{config.renderType},
            config_group = #{config.configGroup},
            description = #{config.description},
            placeholder = #{config.placeholder},
            remark = #{config.remark},
            `rows` = #{config.rows},
            min_value = #{config.minValue},
            max_value = #{config.maxValue},
            unit = #{config.unit},
            `options` = #{config.options},
            order_num = #{config.orderNum},
            is_system = #{config.isSystem},
            updater = #{config.updater},
            update_time = NOW()
        WHERE id = #{config.ID}
          AND deleted = 0
    </update>

    <!-- 删除系统配置（逻辑删除） -->
    <update id="deleteSysConfig">
        UPDATE tb_sys_config
        SET deleted = 1,
            delete_time = NOW()
        WHERE id = #{id}
          AND deleted = 0
    </update>

    <!-- 根据配置键查询 -->
    <select id="selectSysConfigByKey" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List" />
        FROM tb_sys_config
        WHERE config_key = #{configKey}
          AND deleted = 0
    </select>

    <!-- 根据ID查询 -->
    <select id="selectSysConfigById" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List" />
        FROM tb_sys_config
        WHERE id = #{id}
          AND deleted = 0
    </select>

    <!-- 查询所有系统配置 -->
    <select id="selectAllSysConfig" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List" />
        FROM tb_sys_config
        WHERE deleted = 0
        ORDER BY config_group, order_num, config_key
    </select>

    <!-- 根据分组查询 -->
    <select id="selectSysConfigByGroup" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List" />
        FROM tb_sys_config
        WHERE config_group = #{configGroup}
          AND deleted = 0
        ORDER BY order_num, config_key
    </select>

    <!-- 查询系统配置列表 -->
    <select id="selectSysConfigs" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List" />
        FROM tb_sys_config
        <include refid="Base_Where_Clause" />
        ORDER BY config_group, order_num, config_key
    </select>

    <!-- 分页查询 -->
    <select id="selectSysConfigPage" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List" />
        FROM tb_sys_config
        <include refid="Base_Where_Clause" />
        ORDER BY config_group, order_num, config_key
        LIMIT #{pageParam.offset}, #{pageParam.pageSize}
    </select>

    <!-- 查询总数 -->
    <select id="countSelectSysConfig" resultType="int">
        SELECT COUNT(*)
        FROM tb_sys_config
        <include refid="Base_Where_Clause" />
    </select>

</mapper>
