package com.izouma.nineth.repo;

import com.izouma.nineth.domain.MintActivity;
import org.springframework.cache.annotation.CacheEvict;
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;
import java.util.Optional;

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

    Optional<MintActivity> findByIdAndDelFalse(Long id);

    @Query("update MintActivity t set t.stock = t.stock + 1 where t.id = ?1")
    @Modifying
    @Transactional
    void addStock(Long id);

    List<MintActivity> findAllByIdInAndDelFalse(Collection<Long> id);

    @Query("select c.stock from MintActivity c where c.id = ?1")
    Integer getStock(Long id);

    @Query("update MintActivity c set c.stock = ?2 where c.id = ?1")
    @Transactional
    @Modifying
    int updateStock(Long id, int stock);

    @Transactional
    @Modifying
    @Query("update MintActivity c set c.scheduleSale = false, c.startTime = null, c.onShelf = true where c.id = ?1")
    @CacheEvict(value = "mintActivity", key = "#id")
    void scheduleOnShelf(Long id);

}