package com.izouma.nineth.repo;

import com.izouma.nineth.domain.BlindBoxItem;
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 BlindBoxItemRepo extends JpaRepository<BlindBoxItem, Long>, JpaSpecificationExecutor<BlindBoxItem> {
    @Query("update BlindBoxItem t set t.del = true where t.id = ?1")
    @Modifying
    @Transactional
    void softDelete(Long id);

    List<BlindBoxItem> findByBlindBoxId(Long blindBoxId);

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


    @Query("update BlindBoxItem b set b.stock = b.stock - ?2, b.sale = b.sale + ?2 where b.id = ?1")
    @Modifying
    @Transactional
    void decreaseStockAndIncreaseSale(Long id, int num);

    /**
     * 查询盲盒条目数量
     * @param blindBoxId
     * @return
     */
    @Query("select count(t) from BlindBoxItem t where t.del = false and  t.blindBoxId = ?1 ")
    Integer countBlindBoxItem(Long blindBoxId);

}