<?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.gameplatform.server.mapper.admin.SystemConfigMapper">
    <resultMap id="SystemConfigMap" type="com.gameplatform.server.model.entity.admin.SystemConfig">
        <id property="id" column="id" />
        <result property="configKey" column="config_key" />
        <result property="configValue" column="config_value" />
        <result property="configType" column="config_type" />
        <result property="description" column="description" />
        <result property="isSystem" column="is_system" />
        <result property="createdAt" column="created_at" />
        <result property="updatedAt" column="updated_at" />
    </resultMap>

    <select id="findById" parameterType="long" resultMap="SystemConfigMap">
        SELECT id, config_key, config_value, config_type, description, is_system, created_at, updated_at
        FROM system_config
        WHERE id = #{id}
        LIMIT 1
    </select>

    <select id="findByKey" parameterType="string" resultMap="SystemConfigMap">
        SELECT id, config_key, config_value, config_type, description, is_system, created_at, updated_at
        FROM system_config
        WHERE config_key = #{configKey}
        LIMIT 1
    </select>

    <select id="findByType" parameterType="string" resultMap="SystemConfigMap">
        SELECT id, config_key, config_value, config_type, description, is_system, created_at, updated_at
        FROM system_config
        WHERE config_type = #{configType}
        ORDER BY config_key ASC
    </select>

    <select id="findAll" resultMap="SystemConfigMap">
        SELECT id, config_key, config_value, config_type, description, is_system, created_at, updated_at
        FROM system_config
        ORDER BY config_key ASC
        LIMIT #{size} OFFSET #{offset}
    </select>

    <select id="countAll" resultType="long">
        SELECT COUNT(1) FROM system_config
    </select>

    <insert id="insert" parameterType="com.gameplatform.server.model.entity.admin.SystemConfig" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO system_config (config_key, config_value, config_type, description, is_system)
        VALUES (#{configKey}, #{configValue}, #{configType}, #{description}, #{isSystem})
    </insert>

    <update id="update" parameterType="com.gameplatform.server.model.entity.admin.SystemConfig">
        UPDATE system_config
        SET config_value = #{configValue},
            config_type = #{configType},
            description = #{description},
            is_system = #{isSystem}
        WHERE id = #{id}
    </update>

    <delete id="deleteById" parameterType="long">
        DELETE FROM system_config WHERE id = #{id}
    </delete>

    <delete id="deleteByKey" parameterType="string">
        DELETE FROM system_config WHERE config_key = #{configKey}
    </delete>
</mapper>
