<?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.ga.common.file.FileMapper">

    <resultMap id="FileMap" type="com.ga.common.file.FileVO"/>

    <insert id="insert" parameterType="com.ga.common.file.FileVO" useGeneratedKeys="true" keyProperty="fileId">
        INSERT INTO file_storage (
            file_group, original_name, stored_name, file_path, file_size,
            content_type, extension, download_count, is_deleted, created_at, created_by
        ) VALUES (
            #{fileGroup}, #{originalName}, #{storedName}, #{filePath}, #{fileSize},
            #{contentType}, #{extension}, 0, 'N', #{createdAt}, #{createdBy}
        )
    </insert>

    <select id="selectById" resultMap="FileMap">
        SELECT * FROM file_storage WHERE file_id = #{fileId} AND is_deleted = 'N'
    </select>

    <select id="selectByGroup" resultMap="FileMap">
        SELECT * FROM file_storage WHERE file_group = #{fileGroup} AND is_deleted = 'N'
        ORDER BY created_at DESC
    </select>

    <update id="markDeleted">
        UPDATE file_storage SET is_deleted = 'Y' WHERE file_id = #{fileId}
    </update>

    <update id="incrementDownloadCount">
        UPDATE file_storage SET download_count = download_count + 1 WHERE file_id = #{fileId}
    </update>

</mapper>
