SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci;
SET CHARACTER SET utf8mb4;

SET @schema_name = DATABASE();

SET @sql = IF(
    EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = @schema_name AND TABLE_NAME = 'biz_food_restaurant' AND COLUMN_NAME = 'meal_service_enabled'),
    'SELECT 1',
    'ALTER TABLE biz_food_restaurant ADD COLUMN meal_service_enabled tinyint DEFAULT 1 COMMENT ''是否启用订餐服务区域：0-否 1-是'' AFTER policy_json'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @sql = IF(
    EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = @schema_name AND TABLE_NAME = 'biz_food_restaurant' AND COLUMN_NAME = 'service_icon'),
    'SELECT 1',
    'ALTER TABLE biz_food_restaurant ADD COLUMN service_icon varchar(500) DEFAULT NULL COMMENT ''服务区域图标'' AFTER meal_service_enabled'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @sql = IF(
    EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = @schema_name AND TABLE_NAME = 'biz_food_restaurant' AND COLUMN_NAME = 'floor_label'),
    'SELECT 1',
    'ALTER TABLE biz_food_restaurant ADD COLUMN floor_label varchar(100) DEFAULT NULL COMMENT ''楼层/区域位置'' AFTER service_icon'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @sql = IF(
    EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = @schema_name AND TABLE_NAME = 'biz_food_restaurant' AND COLUMN_NAME = 'pickup_location'),
    'SELECT 1',
    'ALTER TABLE biz_food_restaurant ADD COLUMN pickup_location varchar(255) DEFAULT NULL COMMENT ''取餐点'' AFTER floor_label'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @sql = IF(
    EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = @schema_name AND TABLE_NAME = 'biz_food_restaurant' AND COLUMN_NAME = 'meal_open_status_text'),
    'SELECT 1',
    'ALTER TABLE biz_food_restaurant ADD COLUMN meal_open_status_text varchar(100) DEFAULT NULL COMMENT ''订餐开放状态文案'' AFTER pickup_location'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

UPDATE biz_food_restaurant
SET meal_service_enabled = 1
WHERE meal_service_enabled IS NULL;
