<?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="com.pjilisense.flxai.dao.ChunksDao">

    <resultMap type="com.pjilisense.flxai.entity.ChunksEntity" id="chunksMap">
        <result property="id" column="id" typeHandler="com.pjilisense.flxai.handle.UUIDTypeHandler"/>
        <result property="text" column="text"/>
        <result property="abstractx" column="abstract"/>
        <result property="metadata" column="metadata" typeHandler="com.pjilisense.flxai.handle.JsonbTypeHandler"/>
        <result property="index" column="index"/>
        <result property="type" column="type"/>
        <result property="createdAt" column="created_at" jdbcType="TIMESTAMP_WITH_TIMEZONE" javaType="java.time.OffsetDateTime" />
        <result property="updatedAt" column="updated_at" jdbcType="TIMESTAMP_WITH_TIMEZONE" javaType="java.time.OffsetDateTime" />
        <result property="userId" column="user_id"/>
        <result property="accessedAt" column="accessed_at" jdbcType="TIMESTAMP_WITH_TIMEZONE" javaType="java.time.OffsetDateTime"/>
    </resultMap>

    <resultMap type="java.util.Map" id="BaseResultMap">
        <result property="id" column="id" typeHandler="com.pjilisense.flxai.handle.UUIDTypeHandler"/>
        <result property="text" column="text"/>
        <result property="fid" column="fid"/>
        <result property="fname" column="fname" />
    </resultMap>

    <select id="queryCsv" resultMap="BaseResultMap" parameterType="java.util.Map">
        select a.id ,a."text" ,f.id as fid ,f."name" as fname from chunks a left join file_chunks fc on fc.chunk_id =a.id
        left join files f on f.id =fc.file_id
        where f.file_type ='text/plain' and a.user_id =#{userid}
        <if test="wordList != null">
            AND
            <foreach collection="wordList" item="word" index="index" open="(" close=")" separator="or">
                a."text" like '%${word}%'
            </foreach>
        </if>
    </select>

</mapper>