-- =====================================================================
-- 订餐独立服务区域、菜单模板与发布实例字段迁移
-- 说明：MySQL 兼容，使用 INFORMATION_SCHEMA 幂等判断，可重复执行。
-- =====================================================================

SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci;
SET CHARACTER SET utf8mb4;

SET @schema_name = DATABASE();

CREATE TABLE IF NOT EXISTS `biz_food_meal_service_area` (
    `id` varchar(64) NOT NULL COMMENT '主键ID',
    `area_name` varchar(100) NOT NULL COMMENT '服务区域名称',
    `area_code` varchar(64) DEFAULT NULL COMMENT '服务区域编码',
    `service_icon` varchar(500) DEFAULT NULL COMMENT '服务图标',
    `location_label` varchar(255) DEFAULT NULL COMMENT '位置标签',
    `floor_label` varchar(100) DEFAULT NULL COMMENT '楼层标签',
    `pickup_location` varchar(255) DEFAULT NULL COMMENT '取餐/配送点',
    `phone` varchar(50) DEFAULT NULL COMMENT '联系电话',
    `open_hours` varchar(100) DEFAULT NULL COMMENT '服务时间',
    `open_status_text` varchar(100) DEFAULT NULL COMMENT '开放状态文案',
    `status` tinyint NOT NULL DEFAULT '1' COMMENT '状态：0禁用 1启用',
    `sort_no` int DEFAULT '0' COMMENT '排序号',
    `create_by` varchar(64) DEFAULT NULL COMMENT '创建者',
    `create_date` datetime DEFAULT NULL COMMENT '创建时间',
    `update_by` varchar(64) DEFAULT NULL COMMENT '更新者',
    `update_date` datetime DEFAULT NULL COMMENT '更新时间',
    `del_flag` char(1) NOT NULL DEFAULT '0' COMMENT '删除标记',
    `remarks` varchar(500) DEFAULT NULL COMMENT '备注',
    `tenant_code` varchar(64) DEFAULT NULL COMMENT '租户编码',
    `tenant_id` varchar(64) DEFAULT NULL COMMENT '租户ID',
    `organize_id` varchar(64) DEFAULT NULL COMMENT '组织ID',
    PRIMARY KEY (`id`),
    KEY `idx_food_meal_area_code` (`area_code`),
    KEY `idx_food_meal_area_status` (`status`, `del_flag`),
    KEY `idx_food_meal_area_tenant` (`tenant_code`, `del_flag`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='订餐服务区域';

CREATE TABLE IF NOT EXISTS `biz_food_meal_menu_template` (
    `id` varchar(64) NOT NULL COMMENT '主键ID',
    `template_name` varchar(100) NOT NULL COMMENT '模板名称',
    `template_code` varchar(64) DEFAULT NULL COMMENT '模板编码',
    `meal_type` varchar(32) DEFAULT NULL COMMENT '餐别',
    `scope_type` varchar(32) DEFAULT NULL COMMENT '适用范围',
    `description` varchar(500) DEFAULT NULL COMMENT '模板描述',
    `status` tinyint NOT NULL DEFAULT '1' COMMENT '状态：0禁用 1启用',
    `sort_no` int DEFAULT '0' COMMENT '排序号',
    `create_by` varchar(64) DEFAULT NULL COMMENT '创建者',
    `create_date` datetime DEFAULT NULL COMMENT '创建时间',
    `update_by` varchar(64) DEFAULT NULL COMMENT '更新者',
    `update_date` datetime DEFAULT NULL COMMENT '更新时间',
    `del_flag` char(1) NOT NULL DEFAULT '0' COMMENT '删除标记',
    `remarks` varchar(500) DEFAULT NULL COMMENT '备注',
    `tenant_code` varchar(64) DEFAULT NULL COMMENT '租户编码',
    `tenant_id` varchar(64) DEFAULT NULL COMMENT '租户ID',
    `organize_id` varchar(64) DEFAULT NULL COMMENT '组织ID',
    PRIMARY KEY (`id`),
    KEY `idx_food_meal_template_code` (`template_code`),
    KEY `idx_food_meal_template_type` (`meal_type`, `scope_type`),
    KEY `idx_food_meal_template_status` (`status`, `del_flag`),
    KEY `idx_food_meal_template_tenant` (`tenant_code`, `del_flag`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='订餐菜单模板';

CREATE TABLE IF NOT EXISTS `biz_food_meal_menu_template_item` (
    `id` varchar(64) NOT NULL COMMENT '主键ID',
    `template_id` varchar(64) NOT NULL COMMENT '模板ID',
    `item_type` varchar(32) NOT NULL COMMENT '条目类型：dish/package',
    `dish_id` varchar(64) DEFAULT NULL COMMENT '菜品ID',
    `package_id` varchar(64) DEFAULT NULL COMMENT '套餐ID',
    `item_name` varchar(100) NOT NULL COMMENT '菜品/套餐名称快照',
    `item_image` varchar(500) DEFAULT NULL COMMENT '菜品/套餐图片快照',
    `images_json` text DEFAULT NULL COMMENT '轮播图JSON',
    `tags_json` text DEFAULT NULL COMMENT '标签JSON',
    `description_html` text DEFAULT NULL COMMENT '富文本描述',
    `sale_price` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '模板售价',
    `stock_quantity` int DEFAULT NULL COMMENT '模板库存，空表示不限量',
    `limit_quantity_per_user` int DEFAULT NULL COMMENT '每人限购数量',
    `sort_no` int DEFAULT '0' COMMENT '排序号',
    `create_by` varchar(64) DEFAULT NULL COMMENT '创建者',
    `create_date` datetime DEFAULT NULL COMMENT '创建时间',
    `update_by` varchar(64) DEFAULT NULL COMMENT '更新者',
    `update_date` datetime DEFAULT NULL COMMENT '更新时间',
    `del_flag` char(1) NOT NULL DEFAULT '0' COMMENT '删除标记',
    `remarks` varchar(500) DEFAULT NULL COMMENT '备注',
    `tenant_code` varchar(64) DEFAULT NULL COMMENT '租户编码',
    `tenant_id` varchar(64) DEFAULT NULL COMMENT '租户ID',
    `organize_id` varchar(64) DEFAULT NULL COMMENT '组织ID',
    PRIMARY KEY (`id`),
    KEY `idx_food_meal_template_item_template` (`template_id`),
    KEY `idx_food_meal_template_item_source` (`dish_id`, `package_id`),
    KEY `idx_food_meal_template_item_tenant` (`tenant_code`, `del_flag`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='订餐菜单模板明细';

CREATE TABLE IF NOT EXISTS `biz_food_meal_menu_template_item_spec` (
    `id` varchar(64) NOT NULL COMMENT '主键ID',
    `template_id` varchar(64) NOT NULL COMMENT '模板ID',
    `template_item_id` varchar(64) NOT NULL COMMENT '模板明细ID',
    `spec_name` varchar(100) NOT NULL COMMENT '规格名称',
    `spec_description` varchar(255) DEFAULT NULL COMMENT '规格描述',
    `sale_price` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '规格售价',
    `stock_quantity` int DEFAULT NULL COMMENT '规格库存，空表示不限量',
    `limit_quantity_per_user` int DEFAULT NULL COMMENT '每人限购数量',
    `sort_no` int DEFAULT '0' COMMENT '排序号',
    `create_by` varchar(64) DEFAULT NULL COMMENT '创建者',
    `create_date` datetime DEFAULT NULL COMMENT '创建时间',
    `update_by` varchar(64) DEFAULT NULL COMMENT '更新者',
    `update_date` datetime DEFAULT NULL COMMENT '更新时间',
    `del_flag` char(1) NOT NULL DEFAULT '0' COMMENT '删除标记',
    `remarks` varchar(500) DEFAULT NULL COMMENT '备注',
    `tenant_code` varchar(64) DEFAULT NULL COMMENT '租户编码',
    `tenant_id` varchar(64) DEFAULT NULL COMMENT '租户ID',
    `organize_id` varchar(64) DEFAULT NULL COMMENT '组织ID',
    PRIMARY KEY (`id`),
    KEY `idx_food_meal_template_spec_template` (`template_id`),
    KEY `idx_food_meal_template_spec_item` (`template_item_id`),
    KEY `idx_food_meal_template_spec_tenant` (`tenant_code`, `del_flag`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='订餐菜单模板规格';

SET @sql = IF(
    EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = @schema_name AND TABLE_NAME = 'biz_food_meal_menu')
    AND NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = @schema_name AND TABLE_NAME = 'biz_food_meal_menu' AND COLUMN_NAME = 'service_area_id'),
    'ALTER TABLE `biz_food_meal_menu` ADD COLUMN `service_area_id` varchar(64) DEFAULT NULL COMMENT ''服务区域ID'' AFTER `restaurant_id`',
    'SELECT 1'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @sql = IF(
    EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = @schema_name AND TABLE_NAME = 'biz_food_meal_menu')
    AND NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = @schema_name AND TABLE_NAME = 'biz_food_meal_menu' AND COLUMN_NAME = 'service_area_name'),
    'ALTER TABLE `biz_food_meal_menu` ADD COLUMN `service_area_name` varchar(100) DEFAULT NULL COMMENT ''服务区域名称快照'' AFTER `service_area_id`',
    'SELECT 1'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @sql = IF(
    EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = @schema_name AND TABLE_NAME = 'biz_food_meal_menu')
    AND NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = @schema_name AND TABLE_NAME = 'biz_food_meal_menu' AND COLUMN_NAME = 'template_id'),
    'ALTER TABLE `biz_food_meal_menu` ADD COLUMN `template_id` varchar(64) DEFAULT NULL COMMENT ''菜单模板ID'' AFTER `company_name`',
    'SELECT 1'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @sql = IF(
    EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = @schema_name AND TABLE_NAME = 'biz_food_meal_menu')
    AND NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = @schema_name AND TABLE_NAME = 'biz_food_meal_menu' AND COLUMN_NAME = 'template_name'),
    'ALTER TABLE `biz_food_meal_menu` ADD COLUMN `template_name` varchar(100) DEFAULT NULL COMMENT ''菜单模板名称快照'' AFTER `template_id`',
    'SELECT 1'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @sql = IF(
    EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = @schema_name AND TABLE_NAME = 'biz_food_meal_menu_item')
    AND NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = @schema_name AND TABLE_NAME = 'biz_food_meal_menu_item' AND COLUMN_NAME = 'service_area_id'),
    'ALTER TABLE `biz_food_meal_menu_item` ADD COLUMN `service_area_id` varchar(64) DEFAULT NULL COMMENT ''服务区域ID'' AFTER `restaurant_id`',
    'SELECT 1'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @sql = IF(
    EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = @schema_name AND TABLE_NAME = 'biz_food_meal_menu_item')
    AND NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = @schema_name AND TABLE_NAME = 'biz_food_meal_menu_item' AND COLUMN_NAME = 'template_id'),
    'ALTER TABLE `biz_food_meal_menu_item` ADD COLUMN `template_id` varchar(64) DEFAULT NULL COMMENT ''菜单模板ID'' AFTER `service_area_id`',
    'SELECT 1'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @sql = IF(
    EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = @schema_name AND TABLE_NAME = 'biz_food_meal_menu_item')
    AND NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = @schema_name AND TABLE_NAME = 'biz_food_meal_menu_item' AND COLUMN_NAME = 'template_item_id'),
    'ALTER TABLE `biz_food_meal_menu_item` ADD COLUMN `template_item_id` varchar(64) DEFAULT NULL COMMENT ''菜单模板明细ID'' AFTER `template_id`',
    'SELECT 1'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @sql = IF(
    EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = @schema_name AND TABLE_NAME = 'biz_food_meal_menu_item_spec')
    AND NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = @schema_name AND TABLE_NAME = 'biz_food_meal_menu_item_spec' AND COLUMN_NAME = 'service_area_id'),
    'ALTER TABLE `biz_food_meal_menu_item_spec` ADD COLUMN `service_area_id` varchar(64) DEFAULT NULL COMMENT ''服务区域ID'' AFTER `restaurant_id`',
    'SELECT 1'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @sql = IF(
    EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = @schema_name AND TABLE_NAME = 'biz_food_meal_menu_item_spec')
    AND NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = @schema_name AND TABLE_NAME = 'biz_food_meal_menu_item_spec' AND COLUMN_NAME = 'template_id'),
    'ALTER TABLE `biz_food_meal_menu_item_spec` ADD COLUMN `template_id` varchar(64) DEFAULT NULL COMMENT ''菜单模板ID'' AFTER `service_area_id`',
    'SELECT 1'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @sql = IF(
    EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = @schema_name AND TABLE_NAME = 'biz_food_meal_menu_item_spec')
    AND NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = @schema_name AND TABLE_NAME = 'biz_food_meal_menu_item_spec' AND COLUMN_NAME = 'template_item_id'),
    'ALTER TABLE `biz_food_meal_menu_item_spec` ADD COLUMN `template_item_id` varchar(64) DEFAULT NULL COMMENT ''菜单模板明细ID'' AFTER `template_id`',
    'SELECT 1'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @sql = IF(
    EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = @schema_name AND TABLE_NAME = 'biz_food_meal_menu_item_spec')
    AND NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = @schema_name AND TABLE_NAME = 'biz_food_meal_menu_item_spec' AND COLUMN_NAME = 'template_spec_id'),
    'ALTER TABLE `biz_food_meal_menu_item_spec` ADD COLUMN `template_spec_id` varchar(64) DEFAULT NULL COMMENT ''菜单模板规格ID'' AFTER `template_item_id`',
    'SELECT 1'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @sql = IF(
    EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = @schema_name AND TABLE_NAME = 'biz_food_meal_rule')
    AND NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = @schema_name AND TABLE_NAME = 'biz_food_meal_rule' AND COLUMN_NAME = 'service_area_id'),
    'ALTER TABLE `biz_food_meal_rule` ADD COLUMN `service_area_id` varchar(64) DEFAULT NULL COMMENT ''服务区域ID'' AFTER `restaurant_id`',
    'SELECT 1'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @sql = IF(
    EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = @schema_name AND TABLE_NAME = 'biz_food_meal_rule')
    AND NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = @schema_name AND TABLE_NAME = 'biz_food_meal_rule' AND COLUMN_NAME = 'service_area_name'),
    'ALTER TABLE `biz_food_meal_rule` ADD COLUMN `service_area_name` varchar(100) DEFAULT NULL COMMENT ''服务区域名称快照'' AFTER `service_area_id`',
    'SELECT 1'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @sql = IF(
    EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = @schema_name AND TABLE_NAME = 'app_food_meal_order')
    AND NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = @schema_name AND TABLE_NAME = 'app_food_meal_order' AND COLUMN_NAME = 'service_area_id'),
    'ALTER TABLE `app_food_meal_order` ADD COLUMN `service_area_id` varchar(64) DEFAULT NULL COMMENT ''服务区域ID'' AFTER `restaurant_name`',
    'SELECT 1'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @sql = IF(
    EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = @schema_name AND TABLE_NAME = 'app_food_meal_order')
    AND NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = @schema_name AND TABLE_NAME = 'app_food_meal_order' AND COLUMN_NAME = 'service_area_name'),
    'ALTER TABLE `app_food_meal_order` ADD COLUMN `service_area_name` varchar(100) DEFAULT NULL COMMENT ''服务区域名称快照'' AFTER `service_area_id`',
    'SELECT 1'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @sql = IF(EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = @schema_name AND TABLE_NAME = 'biz_food_meal_menu') AND NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.STATISTICS WHERE TABLE_SCHEMA = @schema_name AND TABLE_NAME = 'biz_food_meal_menu' AND INDEX_NAME = 'idx_food_meal_menu_service_area'), 'ALTER TABLE `biz_food_meal_menu` ADD KEY `idx_food_meal_menu_service_area` (`service_area_id`)', 'SELECT 1');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @sql = IF(EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = @schema_name AND TABLE_NAME = 'biz_food_meal_menu') AND NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.STATISTICS WHERE TABLE_SCHEMA = @schema_name AND TABLE_NAME = 'biz_food_meal_menu' AND INDEX_NAME = 'idx_food_meal_menu_template'), 'ALTER TABLE `biz_food_meal_menu` ADD KEY `idx_food_meal_menu_template` (`template_id`)', 'SELECT 1');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @sql = IF(EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = @schema_name AND TABLE_NAME = 'biz_food_meal_menu_item') AND NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.STATISTICS WHERE TABLE_SCHEMA = @schema_name AND TABLE_NAME = 'biz_food_meal_menu_item' AND INDEX_NAME = 'idx_food_meal_menu_item_template'), 'ALTER TABLE `biz_food_meal_menu_item` ADD KEY `idx_food_meal_menu_item_template` (`template_id`, `template_item_id`)', 'SELECT 1');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @sql = IF(EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = @schema_name AND TABLE_NAME = 'biz_food_meal_menu_item_spec') AND NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.STATISTICS WHERE TABLE_SCHEMA = @schema_name AND TABLE_NAME = 'biz_food_meal_menu_item_spec' AND INDEX_NAME = 'idx_food_meal_menu_spec_template'), 'ALTER TABLE `biz_food_meal_menu_item_spec` ADD KEY `idx_food_meal_menu_spec_template` (`template_id`, `template_item_id`, `template_spec_id`)', 'SELECT 1');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @sql = IF(EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = @schema_name AND TABLE_NAME = 'biz_food_meal_rule') AND NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.STATISTICS WHERE TABLE_SCHEMA = @schema_name AND TABLE_NAME = 'biz_food_meal_rule' AND INDEX_NAME = 'idx_food_meal_rule_service_area'), 'ALTER TABLE `biz_food_meal_rule` ADD KEY `idx_food_meal_rule_service_area` (`service_area_id`)', 'SELECT 1');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @sql = IF(EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = @schema_name AND TABLE_NAME = 'app_food_meal_order') AND NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.STATISTICS WHERE TABLE_SCHEMA = @schema_name AND TABLE_NAME = 'app_food_meal_order' AND INDEX_NAME = 'idx_food_meal_order_service_area'), 'ALTER TABLE `app_food_meal_order` ADD KEY `idx_food_meal_order_service_area` (`service_area_id`)', 'SELECT 1');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
