<?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="ink.wgink.common.dao.env.IEnvDao">

    <cache flushInterval="3600000"/>

    <resultMap id="envDTO" type="ink.wgink.common.pojo.dtos.env.EnvDTO">
        <result column="env_key" property="envKey"/>
        <result column="env_explain" property="envExplain"/>
        <result column="env_value" property="envValue"/>
    </resultMap>

    <!-- 建表 -->
    <update id="createTable">
        CREATE TABLE IF NOT EXISTS `sys_env` (
            `env_key` varchar(255) NOT NULL COMMENT '环境变量Key',
            `env_explain` varchar(255) DEFAULT NULL COMMENT '环境变量说明',
            `env_value` longtext COMMENT '环境变量值',
            PRIMARY KEY (`env_key`),
            UNIQUE KEY `env_key` (`env_key`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='系统环境变量';
    </update>

    <!-- 新增环境变量 -->
    <insert id="save" parameterType="map" flushCache="true">
        INSERT INTO sys_env(
            env_key,
            env_explain,
            env_value
        ) VALUES(
            #{envKey},
            #{envExplain},
            #{envValue}
        )
    </insert>

    <!-- 删除环境变量 -->
    <delete id="delete" parameterType="map" flushCache="true">
        DELETE FROM sys_env
    </delete>

    <!-- 更新环境变量 -->
    <update id="update" parameterType="map" flushCache="true">
        UPDATE
            sys_env
        SET
            env_value = #{envValue},
            env_explain = #{envExplain}
        WHERE
            env_key = #{envKey}
    </update>

    <!-- 环境变量列表 -->
    <select id="list" resultMap="envDTO" useCache="true">
        SELECT
            env_key,
            env_explain,
            env_value
        FROM
            sys_env
    </select>

    <!-- 获取环境变量 -->
    <select id="get" parameterType="String" resultMap="envDTO" useCache="true">
        SELECT
            env_key,
            env_explain,
            env_value
        FROM
            sys_env
        WHERE
            env_key = #{_parameter}
    </select>
</mapper>