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_meal_menu_item' AND COLUMN_NAME = 'images_json'),
    'SELECT 1',
    'ALTER TABLE biz_food_meal_menu_item ADD COLUMN images_json text DEFAULT NULL COMMENT ''轮播图JSON'' AFTER item_image'
);
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_meal_menu_item' AND COLUMN_NAME = 'tags_json'),
    'SELECT 1',
    'ALTER TABLE biz_food_meal_menu_item ADD COLUMN tags_json text DEFAULT NULL COMMENT ''标签JSON'' AFTER images_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_meal_menu_item' AND COLUMN_NAME = 'description_html'),
    'SELECT 1',
    'ALTER TABLE biz_food_meal_menu_item ADD COLUMN description_html text DEFAULT NULL COMMENT ''富文本描述'' AFTER tags_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_meal_menu_item' AND COLUMN_NAME = 'publish_status'),
    'SELECT 1',
    'ALTER TABLE biz_food_meal_menu_item ADD COLUMN publish_status varchar(32) NOT NULL DEFAULT ''published'' COMMENT ''发布状态：draft草稿 published已发布 offline已下线'' AFTER description_html'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

UPDATE biz_food_meal_menu_item
SET publish_status = 'published'
WHERE publish_status IS NULL OR publish_status = '';

UPDATE biz_food_meal_menu_item
SET images_json = JSON_ARRAY(item_image)
WHERE (images_json IS NULL OR images_json = '')
  AND item_image IS NOT NULL
  AND item_image <> '';
