package com.ffii.fpsms.modules.jobOrder.service

import com.ffii.fpsms.modules.jobOrder.entity.JobOrderRepository
import com.ffii.fpsms.modules.jobOrder.web.model.PrintRequest
import com.ffii.fpsms.modules.jobOrder.web.model.LaserRequest
import org.springframework.stereotype.Service

import com.ffii.core.support.JdbcDao

@Service
open class PSService(
    private val jdbcDao: JdbcDao,
) {
    
    fun searchProductionSchedules(produceAt: String): List<Map<String, Any>> {
        val args = mapOf(
            "produceAt" to produceAt,
        )

        val sql = """
            SELECT 
                ps.*
            FROM production_schedule ps
            WHERE 1 
            and ps.produceAt >= :produceAt
            ORDER BY produceAt 
        """.trimIndent()
        
        return jdbcDao.queryForList(sql, args)
    }

    fun getProductionScheduleLines(psId: Int): List<Map<String, Any>> {
        val args = mapOf(
            "psId" to psId,
        )

        val sql = """
            SELECT 
                (select group_concat(distinct code) from job_order where prodScheduleLineId = psl.id) as joCode,
                it.code as itemCode,
                it.name as itemName,
                uc.udfudesc as stockUnit,
                psl.*
            FROM production_schedule_line psl
            left join items it on psl.itemId = it.id
            left join item_uom ib on it.id = ib.itemId and ib.stockUnit = 1
            left join uom_conversion uc on ib.uomId = uc.id
            WHERE psl.prodScheduleId = :psId
            ORDER BY psl.itemPriority DESC, itemCode ASC
        """.trimIndent()
        
        return jdbcDao.queryForList(sql, args)
    }

}