package com.kospo.talk.repository;

import com.kospo.talk.model.Manual;
import com.kospo.talk.model.VManual;
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 VManualRepository extends JpaRepository<VManual, Integer> {
    @Query(value = "select id, work_id, sonm, svnm, onm, vnm, path, ins_date, delete_yn, size\n" +
            "from v_manual\n" +
            "where work_id = :workId\n" +
            "  and delete_yn = false\n" +
            "union all\n" +
            "select 0, work_id, '', '', onm, vnm, path, ins_date, delete_yn, size\n" +
            "from manual\n" +
            "where work_id = :workId\n" +
            "  and type = '0100'\n" +
            "  and delete_yn = false\n" +
            "order by ins_date desc\n" +
            "limit 5", nativeQuery = true)
    List<VManual> findByImageAndVideo(@Param("workId") String workId);

    List<VManual> findByWorkIdAndDeleteYn(String workId, boolean deleteYn);

    @Query(value = "select * from v_manual\n" +
            "where delete_yn = false\n" +
            "and (:search is null or :search = '' or onm like concat('%', :search, '%'))\n" +
            "and case\n" +
            " when :search = '' then\n" +
            "     work_id = :workId\n" +
            " else\n" +
            "     true\n" +
            "end", nativeQuery = true)
    List<VManual> findVideoByOnmAndWorkId(String search, String workId);



    @Query(value = "select *\n" +
            "from v_manual\n" +
            "where work_id in (select work_id\n" +
            "                  from sm_user\n" +
            "                  where sabun = :sabun\n" +
            "                    and (:workId is null or :workId = '' or work_id = :workId))"+
            " and delete_yn = :deleteYn order by ins_date desc ", nativeQuery = true)
    List<VManual> findBySabunAndWorkIdAndDeleteYn(@Param("workId") String workId, @Param("sabun") String sabun, @Param("deleteYn") boolean deleteYn);


    @Query(value = "select * from v_manual where work_id = :workId and onm = :onm", nativeQuery = true)
    VManual findByWorkIdAndOnm(@Param("workId") String workId, @Param("onm") String onm);
}