package com.kospo.talk.repository;

import com.kospo.talk.model.TalkAttach;
import com.kospo.talk.model.TalkAttachId;
import jakarta.transaction.Transactional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

public interface TalkAttachRepository extends JpaRepository<TalkAttach, TalkAttachId> {
    List<TalkAttach> findByIdTalkId(String idTalkId);;

    @Query(value = "select * from talk_attach where talk_id = :talkId and sabun = :sabun and ins_date = :insDate order by sort", nativeQuery = true)
    List<TalkAttach> findByList(@Param("talkId") String talkId, @Param("sabun") String sabun, @Param("insDate") LocalDateTime insDate);

    @Transactional
    @Modifying
    @Query(value="insert into talk_attach\n" +
            "select :talkId, sabun, sort, onm, vnm, path, type, size, ins_date from talk_attach where talk_id = :pTalkId", nativeQuery = true)
    int dataCopy(@Param("pTalkId") String pTalkId, @Param("talkId") String talkId);
}