package com.crunii.micro.domain.repo.main;

import com.crunii.micro.domain.common.BaseRepo;
import com.crunii.micro.domain.entity.main.TsmPostRole;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;


@Repository
public interface TsmPostRoleRepo extends BaseRepo<TsmPostRole, String> {

    @Modifying
    @Query(value = "delete from tsm_post_role where post_id =:postId", nativeQuery = true)
    void deletePostId(@Param("postId") Long postId);


    @Query(value = "select * from tsm_post_role where post_id =:postId", nativeQuery = true)
    List<TsmPostRole> findByPostId(@Param("postId") Long postId);

    @Query(value = "select b.* from tsm_post_worker a " +
            " left join tsm_post_role b on a.post_id=b.post_id" +
            " where worker_id=:workerId and a.post_id !=:postId and b.id is not null", nativeQuery = true)
    List<TsmPostRole> findPostAndRoleByWorker(@Param("workerId") Long workerId,@Param("postId") Long postId);


}
