<?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.config.TbSysConfigMapper">

    <!-- 结果映射 -->
    <resultMap id="BaseResultMap" type="org.xyzh.common.dto.sys.TbSysConfigDTO">
        <id column="config_id" property="configId" jdbcType="VARCHAR"/>
        <result column="key" property="key" jdbcType="VARCHAR"/>
        <result column="name" property="name" jdbcType="VARCHAR"/>
        <result column="value" property="value" jdbcType="VARCHAR"/>
        <result column="config_type" property="configType" jdbcType="VARCHAR"/>
        <result column="render_type" property="renderType" jdbcType="VARCHAR"/>
        <result column="description" property="description" jdbcType="VARCHAR"/>
        <result column="re" property="re" typeHandler="org.xyzh.common.jdbc.handler.FastJson2TypeHandler"/>
        <result column="options" property="options" typeHandler="org.xyzh.common.jdbc.handler.FastJson2TypeHandler"/>
        <result column="group" property="group" jdbcType="VARCHAR"/>
        <result column="module_id" property="moduleId" jdbcType="VARCHAR"/>
        <result column="order_num" property="orderNum" jdbcType="INTEGER"/>
        <result column="status" property="status" jdbcType="INTEGER"/>
        <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="ConfigVOResultMap" type="org.xyzh.api.system.vo.SysConfigVO">
        <id column="config_id" property="configId" jdbcType="VARCHAR"/>
        <result column="key" property="key" jdbcType="VARCHAR"/>
        <result column="name" property="name" jdbcType="VARCHAR"/>
        <result column="value" property="value" jdbcType="VARCHAR"/>
        <result column="config_type" property="configType" jdbcType="VARCHAR"/>
        <result column="render_type" property="renderType" jdbcType="VARCHAR"/>
        <result column="description" property="description" jdbcType="VARCHAR"/>
        <result column="re" property="re" typeHandler="org.xyzh.common.jdbc.handler.FastJson2TypeHandler"/>
        <result column="options" property="options" typeHandler="org.xyzh.common.jdbc.handler.FastJson2TypeHandler"/>
        <result column="group" property="group" jdbcType="VARCHAR"/>
        <result column="module_id" property="moduleId" jdbcType="VARCHAR"/>
        <result column="order_num" property="orderNum" jdbcType="INTEGER"/>
        <result column="status" property="status" jdbcType="INTEGER"/>
        <result column="module_name" property="moduleName" jdbcType="VARCHAR"/>
        <result column="module_description" property="moduleDescription" 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>

    <!-- 基础列 -->
    <sql id="Base_Column_List">
        config_id, key, name, value, config_type, render_type, description, re, options, "group", module_id, order_num, status,
        optsn, creator, updater, dept_path, remark, create_time, update_time, delete_time, deleted
    </sql>

    <!-- selectSysConfigByKey --> 

    <select id="selectSysConfigByKey" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List" />
        FROM config.tb_sys_config
        WHERE key = #{configKey}
          AND (deleted IS NULL OR deleted = false)
    </select>

    <!-- 插入系统配置 -->
    <insert id="insertConfig" parameterType="TbSysConfigDTO">
        INSERT INTO config.tb_sys_config
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <!-- 无默认值字段：必填固定列 -->
            config_id,
            key,
            name,
            value,
            config_type,
            render_type,
            description,
            "group",
            module_id,
            order_num,
            optsn,
            remark,
            <!-- 有默认值/可空字段：根据是否传值动态拼接 -->
            <if test="re != null">re,</if>
            <if test="options != null">options,</if>
            <if test="status != null">status,</if>
            <if test="creator != null and creator != ''">creator,</if>
            <if test="updater != null and updater != ''">updater,</if>
            <if test="deptPath != null and deptPath != ''">dept_path,</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=",">
            <!-- 无默认值字段：必填固定值 -->
            #{configId},
            #{key},
            #{name},
            #{value},
            #{configType},
            #{renderType},
            #{description},
            #{group},
            #{moduleId},
            #{orderNum},
            #{optsn},
            #{remark},
            <!-- 有默认值/可空字段：根据是否传值动态拼接 -->
            <if test="re != null">#{re},</if>
            <if test="options != null">#{options},</if>
            <if test="status != null">#{status},</if>
            <if test="creator != null and creator != ''">#{creator},</if>
            <if test="updater != null and updater != ''">#{updater},</if>
            <if test="deptPath != null and deptPath != ''">#{deptPath},</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="updateConfig" parameterType="TbSysConfigDTO">
        UPDATE config.tb_sys_config
        <set>
            <if test="key != null and key != ''">key = #{key},</if>
            <if test="name != null and name != ''">name = #{name},</if>
            <if test="value != null and value != ''">value = #{value},</if>
            <if test="configType != null and configType != ''">config_type = #{configType},</if>
            <if test="renderType != null and renderType != ''">render_type = #{renderType},</if>
            <if test="description != null and description != ''">description = #{description},</if>
            <if test="re != null">re = #{re},</if>
            <if test="options != null">options = #{options},</if>
            <if test="group != null and group != ''">"group" = #{group},</if>
            <if test="moduleId != null and moduleId != ''">module_id = #{moduleId},</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="deptPath != null and deptPath != ''">dept_path = #{deptPath},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="deleteTime != null">delete_time = #{deleteTime},</if>
            <if test="deleted != null">deleted = #{deleted},</if>
        </set>
        WHERE config_id = #{configId}
        <if test="deleted != null">
            AND deleted = #{deleted}
        </if>
    </update>

    <!-- 删除系统配置 -->
    <update id="deleteConfig" parameterType="TbSysConfigDTO">
        UPDATE config.tb_sys_config
        SET deleted = true,
            delete_time = NOW()
        WHERE config_id = #{configId}
    </update>

    <!-- 根据条件查询系统配置列表 -->
    <select id="getConfigByFilter" resultMap="ConfigVOResultMap" parameterType="TbSysConfigDTO">
        SELECT
            c.config_id, c.key, c.name, c.value, c.config_type, c.render_type, c.description, c.re, c.options, c."group", c.module_id, c.order_num, c.status,
            c.optsn, c.creator, c.updater, c.dept_path, c.remark, c.create_time, c.update_time, c.delete_time, c.deleted,
            m.name AS module_name, m.description AS module_description
        FROM config.tb_sys_config c
        LEFT JOIN sys.tb_sys_module m ON c.module_id = m.module_id AND (m.deleted IS NULL OR m.deleted = false)
        <where>
            <if test="filter.configId != null and filter.configId != ''">
                AND c.config_id = #{filter.configId}
            </if>
            <if test="filter.key != null and filter.key != ''">
                AND c.key LIKE CONCAT('%', #{filter.key}, '%')
            </if>
            <if test="filter.configType != null and filter.configType != ''">
                AND c.config_type = #{filter.configType}
            </if>
            <if test="filter.moduleId != null and filter.moduleId != ''">
                AND c.module_id = #{filter.moduleId}
            </if>
            <if test="filter.status != null and filter.status != ''">
                AND c.status = #{filter.status}
            </if>
            <if test="filter.deptPath != null and filter.deptPath != ''">
                AND c.dept_path LIKE CONCAT(#{filter.deptPath}, '%')
            </if>
            AND (c.deleted IS NULL OR c.deleted = false)
        </where>
        ORDER BY c.order_num ASC, c.create_time DESC
    </select>

    <!-- 根据条件查询系统配置分页列表 -->
    <select id="getConfigPageByFilter" resultMap="ConfigVOResultMap">
        SELECT
            c.config_id, c.key, c.name, c.value, c.config_type, c.render_type, c.description, c.re, c.options, c."group", c.module_id, c.order_num, c.status,
            c.optsn, c.creator, c.updater, c.dept_path, c.remark, c.create_time, c.update_time, c.delete_time, c.deleted,
            m.name AS module_name, m.description AS module_description
        FROM config.tb_sys_config c
        LEFT JOIN sys.tb_sys_module m ON c.module_id = m.module_id AND (m.deleted IS NULL OR m.deleted = false)
        <where>
            <if test="filter.configId != null and filter.configId != ''">
                AND c.config_id = #{filter.configId}
            </if>
            <if test="filter.key != null and filter.key != ''">
                AND c.key LIKE CONCAT('%', #{filter.key}, '%')
            </if>
            <if test="filter.configType != null and filter.configType != ''">
                AND c.config_type = #{filter.configType}
            </if>
            <if test="filter.moduleId != null and filter.moduleId != ''">
                AND c.module_id = #{filter.moduleId}
            </if>
            <if test="filter.status != null and filter.status != ''">
                AND c.status = #{filter.status}
            </if>
            <if test="filter.deptPath != null and filter.deptPath != ''">
                AND c.dept_path LIKE CONCAT(#{filter.deptPath}, '%')
            </if>
            AND (c.deleted IS NULL OR c.deleted = false)
        </where>
        ORDER BY c.order_num ASC, c.create_time DESC
        LIMIT #{pageParam.pageSize} OFFSET #{pageParam.offset}
    </select>

    <!-- 根据条件查询系统配置数量 -->
    <select id="getConfigCount" resultType="java.lang.Integer" parameterType="TbSysConfigDTO">
        SELECT COUNT(1)
        FROM config.tb_sys_config
        <where>
            <if test="configId != null and configId != ''">
                AND config_id = #{configId}
            </if>
            <if test="key != null and key != ''">
                AND key LIKE CONCAT('%', #{key}, '%')
            </if>
            <if test="configType != null and configType != ''">
                AND config_type = #{configType}
            </if>
            <if test="moduleId != null and moduleId != ''">
                AND module_id = #{moduleId}
            </if>
            <if test="status != null and status != ''">
                AND status = #{status}
            </if>
            <if test="deptPath != null and deptPath != ''">
                AND dept_path LIKE CONCAT(#{deptPath}, '%')
            </if>
            AND (deleted IS NULL OR deleted = false)
        </where>
    </select>

</mapper>
