package com.kospo.talk.repository;

import com.kospo.talk.model.AutoReply;
import com.kospo.talk.model.AutoReplyId;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface AutoReplyRepository extends JpaRepository<AutoReply, AutoReplyId> {

    @Query(value = "select * from auto_reply where work_id = (select work_id from talk where talk_id = :talkId)", nativeQuery = true)
    List<AutoReply> findByTalkId(@Param("talkId") String talkId);

    @Query(value = "select * from auto_reply\n" +
            "where (:type is null or :type = '' or type = :type)\n"+
            "and (:workId is null or :workId = '' or work_id = :workId) order by type asc", nativeQuery = true)
    List<AutoReply> findByWorkIdAndType(@Param("workId") String workId, @Param("type") String type);

    @Query(value = "select c.*\n" +
            "from work a\n" +
            "         inner join sm_user b on a.work_id = b.work_id\n" +
            "         inner join auto_reply c on a.work_id = c.work_id\n" +
            "    and b.sabun = :sabun", nativeQuery = true)
    List<AutoReply> findBySabun(@Param("sabun") String sabun);
}