<?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.cms.i18n.mapper.I18nMapper">

  <resultMap id="i18nMap" type="com.zioinfo.cms.i18n.CmsI18n">
    <id property="id" column="id"/>
    <result property="locale" column="locale"/>
    <result property="resourceKey" column="resource_key"/>
    <result property="value" column="value"/>
    <result property="updatedAt" column="updated_at"/>
  </resultMap>

  <select id="findByLocale" resultMap="i18nMap">
    SELECT * FROM cms_i18n WHERE locale = #{locale} ORDER BY resource_key ASC
  </select>

  <select id="findLocales" resultType="string">
    SELECT DISTINCT locale FROM cms_i18n ORDER BY locale ASC
  </select>

  <select id="findById" resultMap="i18nMap">SELECT * FROM cms_i18n WHERE id = #{id}</select>

  <update id="upsert">
    INSERT INTO cms_i18n (locale, resource_key, value, updated_at)
    VALUES (#{locale}, #{resourceKey}, #{value}, NOW())
    ON CONFLICT (locale, resource_key) DO UPDATE SET value = #{value}, updated_at = NOW()
  </update>

  <delete id="delete">DELETE FROM cms_i18n WHERE id = #{id}</delete>

</mapper>
