package com.ffii.fpsms.py

import com.ffii.core.support.AbstractRepository
import com.ffii.fpsms.py.entity.PyJobOrderPrintSubmit
import org.springframework.data.jpa.repository.Query
import org.springframework.data.repository.query.Param

interface PyJobOrderPrintSubmitRepository : AbstractRepository<PyJobOrderPrintSubmit, Long> {

    @Query(
        value =
            "SELECT job_order_id, COALESCE(SUM(qty), 0) FROM py_job_order_print_submit " +
                "WHERE deleted = 0 AND job_order_id IN (:ids) AND print_channel = :channel " +
                "GROUP BY job_order_id",
        nativeQuery = true,
    )
    fun sumQtyGroupedByJobOrderId(
        @Param("ids") ids: List<Long>,
        @Param("channel") channel: String,
    ): List<Array<Any>>

    /**
     * One row per (job_order_id, print_channel) with summed qty.
     */
    @Query(
        value =
            "SELECT job_order_id, print_channel, COALESCE(SUM(qty), 0) FROM py_job_order_print_submit " +
                "WHERE deleted = 0 AND job_order_id IN (:ids) " +
                "GROUP BY job_order_id, print_channel",
        nativeQuery = true,
    )
    fun sumQtyGroupedByJobOrderIdAndChannel(
        @Param("ids") ids: List<Long>,
    ): List<Array<Any>>
}
