package com.izouma.nineth.repo;

import com.izouma.nineth.domain.PriceList;
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.Iterator;
import java.util.List;

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

    @Query(value = "select c.asset_id " +
            "from collection_info c " +
            "where c.name like ?1 " +
            "  and c.salable = true " +
            "  and c.source = 'TRANSFER' " +
            "  and c.stock > 0 " +
            "order by c.price " +
            "limit 20 ", nativeQuery = true)
    List<Long> lowest20(String search);

    @Query(value = "select count(o.id) from order_info o where o.asset_id in ?1 and status != 'CANCELLED'", nativeQuery = true)
    long lockNum(Iterable<Long> assetIds);
}