<?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="com.gxwebsoft.app.mapper.AppConfigMapper">

    <!-- 关联查询sql -->
    <sql id="selectSql">
        SELECT a.*
        FROM app_config a
        <where>
            a.deleted = 0
            <if test="param.configId != null">
                AND a.config_id = #{param.configId}
            </if>
            <if test="param.websiteId != null">
                AND a.website_id = #{param.websiteId}
            </if>
            <if test="param.configKey != null and param.configKey != ''">
                AND a.config_key LIKE CONCAT('%', #{param.configKey}, '%')
            </if>
            <if test="param.configType != null and param.configType != ''">
                AND a.config_type = #{param.configType}
            </if>
            <if test="param.isSecret != null">
                AND a.is_secret = #{param.isSecret}
            </if>
            <if test="param.tenantId != null">
                AND a.tenant_id = #{param.tenantId}
            </if>
            <if test="param.createTimeStart != null">
                AND a.create_time &gt;= #{param.createTimeStart}
            </if>
            <if test="param.createTimeEnd != null">
                AND a.create_time &lt;= #{param.createTimeEnd}
            </if>
            <if test="param.keywords != null and param.keywords != ''">
                AND a.config_key LIKE CONCAT('%', #{param.keywords}, '%')
            </if>
        </where>
    </sql>

    <!-- 分页查询 -->
    <select id="selectPageRel" resultType="com.gxwebsoft.app.entity.AppConfig">
        <include refid="selectSql"></include>
    </select>

    <!-- 查询全部 -->
    <select id="selectListRel" resultType="com.gxwebsoft.app.entity.AppConfig">
        <include refid="selectSql"></include>
    </select>

    <!-- 批量获取应用配置（自动解密） -->
    <select id="selectConfigsByWebsiteId" resultType="java.util.HashMap">
        SELECT
            config_key as configKey,
            config_value as configValue,
            config_type as configType,
            is_encrypted as isEncrypted,
            is_secret as isSecret,
            description
        FROM app_config
        WHERE website_id = #{websiteId}
          AND deleted = 0
        ORDER BY config_type, sort_number, config_id
    </select>

</mapper>
