package com.ffii.fpsms.modules.master.entity

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

@Repository
interface ItemsQcCategoryMappingRepository : AbstractRepository<ItemsQcCategoryMapping, Long> {
    fun countByQcCategoryId(qcCategoryId: Long): Long
    
    fun findAllByQcCategoryId(qcCategoryId: Long): List<ItemsQcCategoryMapping>
    
    fun findAllByItemId(itemId: Long): List<ItemsQcCategoryMapping>
    
    fun findByItemIdAndQcCategoryIdAndType(itemId: Long, qcCategoryId: Long, type: String): ItemsQcCategoryMapping?
    
    @Query(
        """
        SELECT map FROM ItemsQcCategoryMapping map
        WHERE (:qcCategoryId IS NULL OR map.qcCategoryId = :qcCategoryId)
        AND (:itemId IS NULL OR map.itemId = :itemId)
        """
    )
    fun findMappings(qcCategoryId: Long?, itemId: Long?): List<ItemsQcCategoryMapping>

    fun findByItemIdAndType(itemId: Long, type: String): ItemsQcCategoryMapping?
    fun findByItemId(itemId: Long): List<ItemsQcCategoryMapping>
    fun findFirstByItemId(itemId: Long): ItemsQcCategoryMapping?
}


