package com.izouma.wenlvju.repo.performance;

import com.izouma.wenlvju.domain.performance.Programme;
import com.izouma.wenlvju.enums.ProgrammeStatus;
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.Collection;
import java.util.List;

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

    List<Programme> findAllByPerformanceIdAndProgrammeStatus(Long performanceId, ProgrammeStatus status);

    long countAllByArrangeIdAndSignedInIsNull(Long arrangeId);

    @Query("update Programme t set t.allSigned = true where t.arrangeId = ?1")
    @Modifying
    @Transactional
    void allSigned(Long arrangeId);

    @Query("update Programme t set t.arrangeId = ?1 where t.id in ?2")
    @Modifying
    @Transactional
    void setArrangeId(Long arrangeId, Collection<Long> ids);

    @Query("update Programme t set t.reviewArrangeId = ?1 where t.id in ?2")
    @Modifying
    @Transactional
    void setReviewArrangeId(Long arrangeId, Collection<Long> ids);

    @Query("update Programme t set t.score = ?1 where t.id = ?2")
    @Modifying
    @Transactional
    void score(double score, Long id);

    @Query("update Programme t set t.reviewScore = ?1 where t.id = ?2")
    @Modifying
    @Transactional
    void reviewScore(double score, Long id);

    List<Programme> findAllByPerformanceIdAndArrangeIdIsNull(Long performanceId);

    List<Programme> findAllByPerformanceIdAndProgrammeStatusAndArrangeIdIsNull(Long performanceId, ProgrammeStatus programmeStatus);

    List<Programme> findAllByPerformanceIdAndProgrammeStatusAndReviewArrangeIdIsNull(Long performanceId, ProgrammeStatus programmeStatus);

    List<Programme> findAllByArrangeId(Long arrangeId);

    List<Programme> findAllByReviewArrangeId(Long arrangeId);

    @Query("select id from Programme where awardId = ?1")
    List<Long> findAllByAwardId(Long awardId);

    @Query("select id from Programme where awardId = ?1 and parentSpecialtyId = ?2")
    List<Long> findAllByAwardIdAndParentSpecialtyId(Long awardId, Long parentSpecialtyId);

    @Query("select id from Programme where awardId = ?1 and specialtyId = ?2")
    List<Long> findAllByAwardIdAndSpecialtyId(Long awardId, Long specialtyId);

}