package com.izouma.nineth.repo;

import com.izouma.nineth.domain.ActivityCollection;
import com.izouma.nineth.domain.Collection;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.jpa.repository.*;

import javax.persistence.LockModeType;
import javax.transaction.Transactional;
import java.util.Optional;

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

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

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

    /**
     * 锁定记录，不能缓存
     * @param id
     * @return
     */
    @Lock(LockModeType.PESSIMISTIC_WRITE)
    @Query("select ac from ActivityCollection ac where ac.id = ?1 and ac.del = false")
    Optional<ActivityCollection> findByIdForUpdate(Long id);



}