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

import com.ffii.core.support.AbstractRepository
import com.ffii.fpsms.modules.master.web.models.ItemType
import org.springframework.data.jpa.repository.Query
import org.springframework.stereotype.Repository
import java.io.Serializable

@Repository
interface ItemsRepository : AbstractRepository<Items, Long> {
    fun findAllByDeletedFalse(): List<Items>;

    fun findByIdAndDeletedFalse(id: Long): Items?;

    fun findByCodeAndTypeAndDeletedFalse(code: String, type: String): Items?;

    fun findByM18IdAndDeletedIsFalse(m18Id: Long): Items?;

    @Query(
        """
            select i from Items i
            join i.itemUoms iu
            join iu.uom u
            where i.name = :name and u.m18Id = :m18UomId and i.deleted = false 
            order by i.id 
            limit 1
        """
    )
    fun findByNameExactlyAndUomM18Id(name: String, m18UomId: Long): Items?;

    @Query(
        """
            select i from Items i
            join i.itemUoms iu
            join iu.uom u
            where i.name like %:name% and u.m18Id = :m18UomId and i.deleted = false 
            order by i.id 
            limit 1 
        """
    )
    fun findByNameLikeAndUomM18Id(name: String, m18UomId: Long): Items?;

    @Query(
        nativeQuery = true,
        value =
        """
            select i.* from items i
            where :m18BomCode like concat('%',i.code,'%')
            and i.deleted = false 
            order by i.id 
            limit 1
        """
    )
    fun findByM18BomCode(m18BomCode: String): Items?;
}