<?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="ru.nbch.indicsearchservice.repository.bank.LocalizationObjectMapper">
    <resultMap id="localizationObjectResultMap" type="ru.nbch.indicsearchservice.model.LocalizationObject">
        <id column="ID" property="id"/>
        <result column="VERSION" property="version"/>
        <result column="CODE" property="code"/>
        <result column="DATE_CREATED" property="dateCreated"
                typeHandler="ru.nbch.indicsearchservice.util.typehandler.NullableLocalDateTimeTypeHandler"/>
        <result column="LAST_UPDATED" property="lastUpdated"
                typeHandler="ru.nbch.indicsearchservice.util.typehandler.NullableLocalDateTimeTypeHandler"/>
        <result column="LOC" property="loc"/>
        <result column="RELEVANCE" property="relevance"/>
        <result column="TEXT" property="text"/>
    </resultMap>

    <sql id="baseSelect">
        SELECT ID,
               VERSION,
               CODE,
               DATE_CREATED,
               LAST_UPDATED,
               LOC,
               RELEVANCE,
               TEXT
        FROM LOCALIZATION_OBJECT
    </sql>

    <select id="findByCode" resultMap="localizationObjectResultMap">
        <include refid="baseSelect"/>
        WHERE CODE = #{code}
        WITH UR
    </select>

    <select id="findAllByCodes" resultMap="localizationObjectResultMap">
        <include refid="baseSelect"/>
        WHERE CODE IN
        <foreach item='code' collection='codes' open='(' separator=',' close=')'>#{code}</foreach>
    </select>

    <insert id="insert" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO LOCALIZATION_OBJECT (VERSION,
                                         CODE,
                                         DATE_CREATED,
                                         LAST_UPDATED,
                                         LOC,
                                         RELEVANCE,
                                         TEXT)
        VALUES (0,
                #{code},
                CURRENT_TIMESTAMP,
                CURRENT_TIMESTAMP,
                #{loc},
                #{relevance},
                #{text})
    </insert>

    <insert id="insertBatch" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO LOCALIZATION_OBJECT (VERSION,
        CODE,
        DATE_CREATED,
        LAST_UPDATED,
        LOC,
        RELEVANCE,
        TEXT)
        VALUES
        <foreach collection="list" item="l" separator=",">
            (0,
            #{l.code},
            CURRENT_TIMESTAMP,
            CURRENT_TIMESTAMP,
            #{l.loc},
            #{l.relevance},
            #{l.text})
        </foreach>
    </insert>
</mapper>
