package com.izouma.wenlvju.repo;

import com.izouma.wenlvju.domain.Rate;
import com.izouma.wenlvju.enums.RateStatus;
import lombok.NonNull;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import javax.transaction.Transactional;
import java.util.List;

public interface RateRepo extends JpaRepository<Rate, Long>, JpaSpecificationExecutor<Rate> {
    @Query("update Rate t set t.del = true where t.id = ?1")
    @Modifying
    @Transactional
    void softDelete(Long id);

    @Query("update Rate t set t.del = true where t.organizationId = ?1")
    @Modifying
    @Transactional
    void softDeleteByOrganizationId(Long organizationId);

    List<Rate> findAllByOrganizationId(@NonNull Long organizationId);

    Long countAllByOrganizationIdAndYearAndStatusNot(@NonNull Long organizationId, String year, RateStatus status);

    Long countAllByOrganizationIdAndYear(@NonNull Long organizationId, String year);

    @Query(nativeQuery = true, value = "select count(1) from rate where del=0 and (expert_user_id = ?1 or find_in_set(?1,expert_member_user_id))")
    Long countByExpertId(Long userId);
}