<?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="1" author="rpapat">
        <sql dbms="mysql" endDelimiter=";;" splitStatements="true" stripComments="true">       
		USE `compilations`;;
		
		DROP PROCEDURE IF EXISTS `sp_update_label_split`;;
		
		CREATE PROCEDURE `sp_update_label_split`()
		BEGIN
				
		UPDATE `compilations`.`catalog_library` clu,(
		    SELECT (
			    SELECT `orchard_compilation_split` 
			    FROM art_relations.booked_vendor_contract_snapshot 
			    WHERE `vendor_contract_id` = vavc.`vendor_contract_id` 
			    ORDER BY `period_id` DESC LIMIT 1
		    ) AS  splitVal,
		    v.`vendor_id`
			FROM art_relations.vendor v
			LEFT JOIN art_relations.`vw_active_vendor_contract` vavc ON vavc.`vendor_id` = v.`vendor_id`
			WHERE v.vendor_id IN (SELECT DISTINCT label_id FROM `compilations`.`catalog_library`)
		) split
		SET clu.`label_splits` = split.splitVal
		WHERE clu.`label_id` = split.vendor_id;
		END;;
        </sql>
        <rollback>
            <sql dbms="mysql" endDelimiter=";;" splitStatements="true" stripComments="true">
               DROP PROCEDURE IF EXISTS `sp_update_label_split`;;
            </sql>
        </rollback>
    </changeSet>
    <changeSet id="2" author="rpapat">
        <sql dbms="mysql" endDelimiter=";;" splitStatements="true" stripComments="true">
		USE `compilations`;;
			
		DROP PROCEDURE IF EXISTS `sp_update_territory_carveouts`;;
			
		CREATE PROCEDURE `sp_update_territory_carveouts`()
		BEGIN
		-- Update release level store carveouts with vendor level carveouts.
			
		SET group_concat_max_len=1024*800;
		UPDATE compilations.territory_carveout t, (
		    SELECT carevout_data.label_id, carevout_data.upc, GROUP_CONCAT(c.name) AS c_names
		    FROM art_relations.country c, (
		        SELECT carveout.label_id, carveout.upc,
		        TRIM(BOTH ',' FROM CONCAT (IF(carveout.countries IS NULL, '',
		        carveout.countries),
		        ',', IF(carveout.existingids IS NULL, '', carveout.existingids))
		        ) AS carveout_ids
		        FROM (
		            SELECT tc.label_id, tc.upc, GROUP_CONCAT(rtr.country_id) AS countries,
		            tc.territory_carveouts AS existingids
		            FROM compilations.territory_carveout tc
		            LEFT JOIN art_relations.release_territory_restriction rtr ON rtr.upc = tc.upc
		            GROUP BY tc.upc
		        ) AS carveout
		        GROUP BY carveout.upc
		    ) AS carevout_data
		    WHERE FIND_IN_SET (c.id, carevout_data.carveout_ids)
		    GROUP BY carevout_data.upc
		) AS all_carveouts
		SET t.territory_carveouts = all_carveouts.c_names
		WHERE t.upc = all_carveouts.upc;
		
		END;;
        </sql>
        <rollback>
            <sql dbms="mysql" endDelimiter=";;" splitStatements="true" stripComments="true">
		DROP PROCEDURE IF EXISTS `sp_update_territory_carveouts`;;
            </sql>
        </rollback>
    </changeSet>
    <changeSet id="3" author="rpapat">
        <sql dbms="mysql" endDelimiter=";;" splitStatements="true" stripComments="true">
		USE `compilations`;;
		
		DROP PROCEDURE IF EXISTS `sp_update_dms_carveouts`;;
	
		CREATE PROCEDURE `sp_update_dms_carveouts`()
		BEGIN
		-- Update release level store carveouts with vendor level carveouts.
		SET group_concat_max_len=1024*800;
		UPDATE compilations.dms_carveout t, (
		    SELECT carevout_data.upc, GROUP_CONCAT(cm.`customer_name`) AS cust_names 
		    FROM art_relations.`customer_master_master` cm, (
		        SELECT carveout.label_id, carveout.upc, TRIM(BOTH ',' FROM CONCAT
		        (IF(carveout.dmsids IS NULL, '', carveout.dmsids),',',
		        IF(carveout.existingids IS NULL, '', carveout.existingids) )) AS carveout_ids
		        FROM (
		            SELECT dcr.label_id, dcr.upc, GROUP_CONCAT(DISTINCT rdmr.`customer_master_master_id`) AS dmsids, 
		            dcr.`dms_carveouts` AS existingids
		            FROM compilations.`dms_carveout` dcr
		            LEFT JOIN art_relations.release_dms_master_restriction rdmr ON dcr.`upc` = rdmr.`upc`
		            GROUP BY dcr.`upc`
		        ) AS carveout
		        GROUP BY carveout.upc
		    ) AS carevout_data
		    WHERE FIND_IN_SET (cm.customer_master_master_id, carevout_data.carveout_ids)
		    GROUP BY carevout_data.upc
		) AS all_carveouts
		SET t.`dms_carveouts` = all_carveouts.cust_names
		WHERE t.upc = all_carveouts.upc;
		
		-- Update store carveouts with vendor level substore carveouts .
		UPDATE `compilations`.`dms_carveout` dc, (
			SELECT dmsCarveoutIds.label_id, GROUP_CONCAT(DISTINCT cm.name) AS dmsCarveOutNames
			FROM (
				SELECT cl.label_id, vc.`dms_carve_out` AS dmsCarveouts
				FROM `art_relations`.vendor v
				INNER JOIN compilations.`catalog_library` cl ON cl.label_id = v.`vendor_id`
				INNER JOIN `art_relations`.vw_active_vendor_contract avc ON avc.vendor_id = cl.label_id
				LEFT JOIN `art_relations`.`vendor_contract` vc ON vc.`id` = avc.`vendor_contract_id`
				GROUP BY cl.`label_id`
			) AS dmsCarveoutIds,
			`art_relations`.`customer_master` cm
			WHERE FIND_IN_SET(cm.customer_id, dmsCarveoutIds.dmsCarveouts)
			GROUP BY dmsCarveoutIds.label_id
		) AS dmsCarveOutList
		SET dc.`dms_carveouts` = TRIM(BOTH ',' FROM CONCAT(
			IF(dc.dms_carveouts IS NULL, '', dc.dms_carveouts),',',
			IF(dmsCarveOutList.dmsCarveOutNames IS NULL, '', dmsCarveOutList.dmsCarveOutNames))
		)
		WHERE dc.`label_id` = dmsCarveOutList.label_id;
		
		END;;
        </sql>
        <rollback>
            <sql dbms="mysql" endDelimiter=";;" splitStatements="true" stripComments="true">
               DROP PROCEDURE IF EXISTS `sp_update_dms_carveouts`;;
            </sql>
        </rollback>
    </changeSet>
</databaseChangeLog>
