package com.ffii.fpsms.modules.stock.entity

import com.ffii.core.support.AbstractRepository
import org.springframework.data.jpa.repository.Query
import org.springframework.stereotype.Repository
import java.io.Serializable

@Repository
interface InventoryLotRepository: AbstractRepository<InventoryLot, Long> {
    fun findByStockInLineIdAndDeletedFalse(stockInLineId: Serializable): InventoryLot?

    fun findByItemIdAndDeletedFalse(itemId: Long): List<InventoryLot>

    @Query("""
        select il.lotNo from InventoryLot il where il.lotNo like :prefix% order by il.lotNo desc limit 1
    """)
    fun findLatestLotNoByPrefix(prefix: String): String?
    fun findAllByIdIn(ids: List<Long>): List<InventoryLot>
        @Query("""
        SELECT il FROM InventoryLot il 
        WHERE il.lotNo = :lotNo 
        AND il.item.id = :itemId 
        AND il.deleted = false
        ORDER BY il.id ASC
    """)
    fun findAllByLotNoAndItemIdAndDeletedFalseOrderByIdAsc(lotNo: String, itemId: Long): List<InventoryLot>

    fun findFirstByStockInLineIdAndDeletedFalseOrderByIdAsc(stockInLineId: Serializable): InventoryLot?
    fun findByIdAndDeletedFalse(id: Serializable): InventoryLot?
}