<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zioinfo.signage.admin.mapper.SettingMapper">

  <resultMap id="settingMap" type="com.zioinfo.signage.admin.dto.SignageSetting">
    <id     property="settingKey"   column="setting_key"/>
    <result property="settingValue" column="setting_value"/>
    <result property="category"     column="category"/>
    <result property="description"  column="description"/>
    <result property="updatedAt"    column="updated_at"/>
  </resultMap>

  <select id="findAll" resultMap="settingMap">
    SELECT setting_key, setting_value, category, description, updated_at
    FROM signage_setting
    <where>
      <if test="category != null and category != ''">AND category = #{category}</if>
    </where>
    ORDER BY category ASC, setting_key ASC
  </select>

  <select id="findByKey" resultMap="settingMap">
    SELECT setting_key, setting_value, category, description, updated_at
    FROM signage_setting
    WHERE setting_key = #{settingKey}
  </select>

  <update id="upsert">
    INSERT INTO signage_setting (setting_key, setting_value, category, description, updated_at)
    VALUES (#{settingKey}, #{settingValue}, #{category}, #{description}, NOW())
    ON CONFLICT (setting_key) DO UPDATE
      SET setting_value = EXCLUDED.setting_value,
          updated_at    = NOW()
  </update>

</mapper>
