<?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.datamate.datamanagement.infrastructure.persistence.mapper.DatasetFileMapper">
    <sql id="Base_Column_List">
      id, dataset_id, file_name, file_path, file_type, file_size, check_sum, tags, metadata, status,
          upload_time, last_access_time, created_at, updated_at
    </sql>

  <select id="findById" parameterType="string"
          resultType="com.datamate.datamanagement.domain.model.dataset.DatasetFile">
        SELECT <include refid="Base_Column_List"/>
        FROM t_dm_dataset_files
        WHERE id = #{id}
    </select>

  <select id="findByDatasetId" parameterType="string"
          resultType="com.datamate.datamanagement.domain.model.dataset.DatasetFile">
        SELECT <include refid="Base_Column_List"/>
        FROM t_dm_dataset_files
        WHERE dataset_id = #{datasetId}
        ORDER BY upload_time DESC
    </select>

  <select id="findByDatasetIdAndStatus" resultType="com.datamate.datamanagement.domain.model.dataset.DatasetFile">
        SELECT <include refid="Base_Column_List"/>
        FROM t_dm_dataset_files
        WHERE dataset_id = #{datasetId}
          AND status = #{status}
        ORDER BY upload_time DESC
    </select>

  <select id="findByDatasetIdAndFileType" resultType="com.datamate.datamanagement.domain.model.dataset.DatasetFile">
        SELECT <include refid="Base_Column_List"/>
        FROM t_dm_dataset_files
        WHERE dataset_id = #{datasetId}
          AND file_type = #{fileType}
        ORDER BY upload_time DESC
    </select>

    <select id="countByDatasetId" parameterType="string" resultType="long">
        SELECT COUNT(*) FROM t_dm_dataset_files WHERE dataset_id = #{datasetId}
    </select>

    <select id="countCompletedByDatasetId" parameterType="string" resultType="long">
        SELECT COUNT(*) FROM t_dm_dataset_files WHERE dataset_id = #{datasetId} AND status = 'COMPLETED'
    </select>

    <select id="sumSizeByDatasetId" parameterType="string" resultType="long">
        SELECT COALESCE(SUM(file_size), 0) FROM t_dm_dataset_files WHERE dataset_id = #{datasetId}
    </select>

  <select id="findByDatasetIdAndFileName" resultType="com.datamate.datamanagement.domain.model.dataset.DatasetFile">
        SELECT <include refid="Base_Column_List"/>
        FROM t_dm_dataset_files
        WHERE dataset_id = #{datasetId} AND file_name = #{fileName}
        LIMIT 1
    </select>

  <select id="findAllByDatasetId" parameterType="string"
          resultType="com.datamate.datamanagement.domain.model.dataset.DatasetFile">
        SELECT <include refid="Base_Column_List"/>
        FROM t_dm_dataset_files
        WHERE dataset_id = #{datasetId}
        ORDER BY upload_time DESC
    </select>

  <select id="findByCriteria" resultType="com.datamate.datamanagement.domain.model.dataset.DatasetFile">
        SELECT <include refid="Base_Column_List"/>
        FROM t_dm_dataset_files
        WHERE dataset_id = #{datasetId}
        <!-- Replace invalid XML '&&' with 'and' for MyBatis OGNL -->
        <if test="fileType != null and fileType != ''">
            AND file_type = #{fileType}
        </if>
        <if test="status != null and status != ''">
            AND status = #{status}
        </if>
        ORDER BY upload_time DESC
    </select>


    <update id="update" parameterType="com.datamate.datamanagement.domain.model.dataset.DatasetFile">
        UPDATE t_dm_dataset_files
        SET file_name = #{fileName},
            file_path = #{filePath},
            file_type = #{fileType},
            file_size = #{fileSize},
            upload_time = #{uploadTime},
            last_access_time = #{lastAccessTime},
            status = #{status}
        WHERE id = #{id}
    </update>

    <delete id="deleteById" parameterType="string">
        DELETE FROM t_dm_dataset_files WHERE id = #{id}
    </delete>
</mapper>
