USE mysql;

SET GLOBAL event_scheduler = on ;
SET GLOBAL event_scheduler = 1;

-- -----------------------------------------------------------------------------
--  우편 삭제 대기중인 rows 일괄 삭제
-- -----------------------------------------------------------------------------


DROP EVENT IF EXISTS `delete_post_event_schedule`;

DELIMITER $

CREATE EVENT `delete_post_event_schedule`
ON SCHEDULE EVERY 1 DAY
STARTS '2017-03-10 03:50:00'
DO
BEGIN
	CALL `eod_game`.usp_a_delete_post(100);
END$

DELIMITER;

-- -----------------------------------------------------------------------------
--  장비 삭제 대기중인 rows 일괄 삭제
-- -----------------------------------------------------------------------------


DROP EVENT IF EXISTS `delete_equipment_event_schedule`;

DELIMITER $

CREATE EVENT `delete_equipment_event_schedule`
ON SCHEDULE EVERY 1 DAY
STARTS '2017-03-10 04:00:00'
DO
BEGIN
	CALL `eod_game`.usp_a_delete_equipment(100);
END$

DELIMITER;


-- -----------------------------------------------------------------------------
--  패키지 초기화 상품 초기화
-- -----------------------------------------------------------------------------

DROP EVENT IF EXISTS `check_shop_package_init`;

DELIMITER $

CREATE EVENT `check_shop_package_init`
ON SCHEDULE EVERY 1 SECOND
STARTS '2020-12-02 17:00:00'
DO
BEGIN
	declare init_time_data int default 0;
    declare last_init_time datetime;
    -- declare current_time datetime;
    declare check_time datetime;
    
    -- set current_time = select now();
    
    -- 210102011
    select initialization_time into init_time_data from eod_configuration.data_shop_package where general_type_code = 210102011 and market_type = 2;
    select init_time into last_init_time from eod_configuration.table_shop_package_init where general_type_code = 210102011;
    set check_time = DATE_ADD(last_init_time, INTERVAL init_time_data SECOND);
    if(now() >= last_init_time) then
	update eod_game.table_shop_buy_count set count = 0, initialization_time = check_time where product_code = 210102011;
        update eod_configuration.table_shop_package_init set init_time = check_time where general_type_code = 210102011;
	end if;
    
    -- 210102031
    select initialization_time into init_time_data from eod_configuration.data_shop_package where general_type_code = 210102031 and market_type = 2;
    select init_time into last_init_time from eod_configuration.table_shop_package_init where general_type_code = 210102031;
    set check_time = DATE_ADD(last_init_time, INTERVAL init_time_data SECOND);
    if(now() >= last_init_time) then
	update eod_game.table_shop_buy_count set count = 0, initialization_time = check_time where product_code = 210102031;
        update eod_configuration.table_shop_package_init set init_time = check_time where general_type_code = 210102031;
	end if;
    
    -- 210102012
    select initialization_time into init_time_data from eod_configuration.data_shop_package where general_type_code = 210102012 and market_type = 2;
    select init_time into last_init_time from eod_configuration.table_shop_package_init where general_type_code = 210102012;
    set check_time = DATE_ADD(last_init_time, INTERVAL init_time_data SECOND);
    if(now() >= last_init_time) then
	update eod_game.table_shop_buy_count set count = 0, initialization_time = check_time where product_code = 210102012;
        update eod_configuration.table_shop_package_init set init_time = check_time where general_type_code = 210102012;
	end if;
    
    -- 210102013
    select initialization_time into init_time_data from eod_configuration.data_shop_package where general_type_code = 210102013 and market_type = 2;
    select init_time into last_init_time from eod_configuration.table_shop_package_init where general_type_code = 210102013;
    set check_time = DATE_ADD(last_init_time, INTERVAL init_time_data SECOND);
    if(now() >= last_init_time) then
	update eod_game.table_shop_buy_count set count = 0, initialization_time = check_time where product_code = 210102013;
        update eod_configuration.table_shop_package_init set init_time = check_time where general_type_code = 210102013;
	end if;
    
    -- 210102032
    select initialization_time into init_time_data from eod_configuration.data_shop_package where general_type_code = 210102032 and market_type = 2;
    select init_time into last_init_time from eod_configuration.table_shop_package_init where general_type_code = 210102032;
    set check_time = DATE_ADD(last_init_time, INTERVAL init_time_data SECOND);
    if(now() >= last_init_time) then
	update eod_game.table_shop_buy_count set count = 0, initialization_time = check_time where product_code = 210102032;
        update eod_configuration.table_shop_package_init set init_time = check_time where general_type_code = 210102032;
	end if;
    
    -- 210102033
    select initialization_time into init_time_data from eod_configuration.data_shop_package where general_type_code = 210102033 and market_type = 2;
    select init_time into last_init_time from eod_configuration.table_shop_package_init where general_type_code = 210102033;
    set check_time = DATE_ADD(last_init_time, INTERVAL init_time_data SECOND);
    if(now() >= last_init_time) then
	update eod_game.table_shop_buy_count set count = 0, initialization_time = check_time where product_code = 210102033;
        update eod_configuration.table_shop_package_init set init_time = check_time where general_type_code = 210102033;
	end if;
    
    -- 210102034
    select initialization_time into init_time_data from eod_configuration.data_shop_package where general_type_code = 210102034 and market_type = 2;
    select init_time into last_init_time from eod_configuration.table_shop_package_init where general_type_code = 210102034;
    set check_time = DATE_ADD(last_init_time, INTERVAL init_time_data SECOND);
    if(now() >= last_init_time) then
	update eod_game.table_shop_buy_count set count = 0, initialization_time = check_time where product_code = 210102034;
        update eod_configuration.table_shop_package_init set init_time = check_time where general_type_code = 210102034;
	end if;
    
    -- 210102014
    select initialization_time into init_time_data from eod_configuration.data_shop_package where general_type_code = 210102014 and market_type = 2;
    select init_time into last_init_time from eod_configuration.table_shop_package_init where general_type_code = 210102014;
    set check_time = DATE_ADD(last_init_time, INTERVAL init_time_data SECOND);
    if(now() >= last_init_time) then
	update eod_game.table_shop_buy_count set count = 0, initialization_time = check_time where product_code = 210102014;
        update eod_configuration.table_shop_package_init set init_time = check_time where general_type_code = 210102014;
	end if;
    
    -- 210102015
    select initialization_time into init_time_data from eod_configuration.data_shop_package where general_type_code = 210102015 and market_type = 2;
    select init_time into last_init_time from eod_configuration.table_shop_package_init where general_type_code = 210102015;
    set check_time = DATE_ADD(last_init_time, INTERVAL init_time_data SECOND);
    if(now() >= last_init_time) then
	update eod_game.table_shop_buy_count set count = 0, initialization_time = check_time where product_code = 210102015;
        update eod_configuration.table_shop_package_init set init_time = check_time where general_type_code = 210102015;
	end if;
    
    -- 210102016
    select initialization_time into init_time_data from eod_configuration.data_shop_package where general_type_code = 210102016 and market_type = 2;
    select init_time into last_init_time from eod_configuration.table_shop_package_init where general_type_code = 210102016;
    set check_time = DATE_ADD(last_init_time, INTERVAL init_time_data SECOND);
    if(now() >= last_init_time) then
	update eod_game.table_shop_buy_count set count = 0, initialization_time = check_time where product_code = 210102016;
        update eod_configuration.table_shop_package_init set init_time = check_time where general_type_code = 210102016;
	end if;
    
    -- 210102024
    select initialization_time into init_time_data from eod_configuration.data_shop_package where general_type_code = 210102024 and market_type = 2;
    select init_time into last_init_time from eod_configuration.table_shop_package_init where general_type_code = 210102024;
    set check_time = DATE_ADD(last_init_time, INTERVAL init_time_data SECOND);
    if(now() >= last_init_time) then
	update eod_game.table_shop_buy_count set count = 0, initialization_time = check_time where product_code = 210102024;
        update eod_configuration.table_shop_package_init set init_time = check_time where general_type_code = 210102024;
	end if;
    
    -- 210102025
    select initialization_time into init_time_data from eod_configuration.data_shop_package where general_type_code = 210102025 and market_type = 2;
    select init_time into last_init_time from eod_configuration.table_shop_package_init where general_type_code = 210102025;
    set check_time = DATE_ADD(last_init_time, INTERVAL init_time_data SECOND);
    if(now() >= last_init_time) then
	update eod_game.table_shop_buy_count set count = 0, initialization_time = check_time where product_code = 210102025;
        update eod_configuration.table_shop_package_init set init_time = check_time where general_type_code = 210102025;
	end if;
    
    -- 210102017
    select initialization_time into init_time_data from eod_configuration.data_shop_package where general_type_code = 210102017 and market_type = 2;
    select init_time into last_init_time from eod_configuration.table_shop_package_init where general_type_code = 210102017;
    set check_time = DATE_ADD(last_init_time, INTERVAL init_time_data SECOND);
    if(now() >= last_init_time) then
	update eod_game.table_shop_buy_count set count = 0, initialization_time = check_time where product_code = 210102017;
        update eod_configuration.table_shop_package_init set init_time = check_time where general_type_code = 210102017;
	end if;
    
    -- 210102018
    select initialization_time into init_time_data from eod_configuration.data_shop_package where general_type_code = 210102018 and market_type = 2;
    select init_time into last_init_time from eod_configuration.table_shop_package_init where general_type_code = 210102018;
    set check_time = DATE_ADD(last_init_time, INTERVAL init_time_data SECOND);
    if(now() >= last_init_time) then
	update eod_game.table_shop_buy_count set count = 0, initialization_time = check_time where product_code = 210102018;
        update eod_configuration.table_shop_package_init set init_time = check_time where general_type_code = 210102018;
	end if;
END$