package com.zioinfo.kintex.system.setting;

import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/** 시스템 설정 매퍼. */
@Mapper
public interface SettingMapper {

    @Select("SELECT setting_key AS \"settingKey\", setting_value AS \"settingValue\", value_type AS \"valueType\", "
            + "description, secret_yn AS \"secretYn\" FROM sys_setting ORDER BY setting_key")
    List<Map<String, Object>> findAll();

    @Select("SELECT setting_key AS \"settingKey\", setting_value AS \"settingValue\", value_type AS \"valueType\", "
            + "description, secret_yn AS \"secretYn\" FROM sys_setting WHERE setting_key = #{key}")
    Map<String, Object> findByKey(@Param("key") String key);

    @Insert("INSERT INTO sys_setting (setting_key, setting_value, value_type, description, secret_yn, updated_by) "
            + "VALUES (#{settingKey}, #{settingValue}, COALESCE(#{valueType},'STRING'), #{description}, "
            + "COALESCE(#{secretYn},'N'), #{updatedBy}) "
            + "ON CONFLICT (setting_key) DO UPDATE SET setting_value=EXCLUDED.setting_value, "
            + "value_type=EXCLUDED.value_type, description=EXCLUDED.description, secret_yn=EXCLUDED.secret_yn, "
            + "updated_at=now(), updated_by=EXCLUDED.updated_by")
    int upsert(Map<String, Object> p);

    @Delete("DELETE FROM sys_setting WHERE setting_key = #{key}")
    int delete(@Param("key") String key);
}
