<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
        xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
        xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.0.xsd
        http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">
    <changeSet id="3" author="pdumoulin">
        <sql dbms="mysql" endDelimiter=";;" splitStatements="true" stripComments="true">
        <![CDATA[
            DROP PROCEDURE IF EXISTS sp_get_jobs_for_nonbatch_delivery;;

            CREATE DEFINER='theorchard_dd'@'127.0.0.1' PROCEDURE sp_get_jobs_for_nonbatch_delivery(IN dmsId INT, IN limitTo INT, IN encoder_ids TEXT)
            BEGIN
                SET @stmtStr := '';
                SELECT CONCAT('
                    SELECT eqd.encoding_queue_detail_id, jp.priority, eqd.dms_master_master_id, eq.encoding_order_type, eq.encoder_id, 0 AS delivery_batch_id, cmm.priority as dms_priority
                    FROM encoding_queue_detail eqd
                    INNER JOIN encoding_queue eq ON eqd.encoding_queue_id = eq.encoding_queue_id
                    INNER JOIN customer_master_master cmm ON eqd.dms_master_master_id = cmm.customer_master_master_id
                    INNER JOIN job_priority jp ON eqd.encoding_queue_detail_id = jp.encoding_queue_detail_id
                    INNER JOIN dms_delivery_spec dds ON dds.dms_master_master_id = cmm.customer_master_master_id
                        AND eq.encoding_order_type = dds.order_type
                        AND dds.batch_delivery = "N"
                        AND dds.delivery = "Y"
                    WHERE eqd.status = "ready_to_deliver"
                        AND cmm.customer_master_master_id = ', dmsId, '
                        AND eq.encoder_id IN (', encoder_ids, ')
                    ORDER BY jp.priority, eqd.encoding_queue_detail_id LIMIT ', limitTo) INTO @stmtStr;
                PREPARE stmt FROM @stmtStr;
                EXECUTE stmt;
            END;;
        ]]>
        </sql>
        <rollback>
            <sql dbms="mysql" endDelimiter=";;" splitStatements="true" stripComments="true">
            <![CDATA[
                DROP PROCEDURE IF EXISTS sp_get_jobs_for_nonbatch_delivery;;

                CREATE DEFINER='theorchard_dd'@'127.0.0.1' PROCEDURE sp_get_jobs_for_nonbatch_delivery(IN dmsId INT, IN limitTo INT, IN encoder_ids TEXT)
                BEGIN
                    SET @stmtStr := '';
                    SELECT CONCAT('
                        SELECT eqd.encoding_queue_detail_id, jp.priority, eqd.dms_master_master_id, eq.encoding_order_type, eq.encoder_id, 0 AS delivery_batch_id, cmm.priority as dms_priority
                        FROM encoding_queue_detail eqd
                        INNER JOIN encoding_queue eq ON eqd.encoding_queue_id = eq.encoding_queue_id
                        INNER JOIN customer_master_master cmm ON eqd.dms_master_master_id = cmm.customer_master_master_id
                        INNER JOIN job_priority jp ON eqd.encoding_queue_detail_id = jp.encoding_queue_detail_id
                        INNER JOIN dms_delivery_spec dds ON dds.dms_master_master_id = cmm.customer_master_master_id
                            AND eq.encoding_order_type = dds.order_type
                            AND dds.batch_delivery = "N"
                            AND dds.delivery = "Y"
                        WHERE eqd.status = "ready_to_deliver"
                            AND cmm.customer_master_master_id = ', dmsId, '
                            AND eq.encoder_id IN (', encoder_ids, ')
                        ORDER BY jp.priority LIMIT ', limitTo) INTO @stmtStr;
                    PREPARE stmt FROM @stmtStr;
                    EXECUTE stmt;
                END;;
            ]]>
            </sql>
        </rollback>
    </changeSet>
</databaseChangeLog>
