-- 订餐规格交易字段初始化
-- 说明：支持菜单项一层规格参与交易、库存控制与订单快照；可重复执行。

SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci;
SET CHARACTER SET utf8mb4;

CREATE TABLE IF NOT EXISTS `biz_food_meal_menu_item_spec` (
    `id` varchar(64) NOT NULL COMMENT '主键ID',
    `menu_item_id` varchar(64) NOT NULL COMMENT '菜单明细ID',
    `menu_id` varchar(64) NOT NULL COMMENT '菜单ID',
    `restaurant_id` varchar(64) DEFAULT 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 '规格库存，空表示不限量',
    `sold_quantity` int NOT NULL DEFAULT '0' 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_spec_menu_item` (`menu_item_id`),
    KEY `idx_food_meal_spec_menu` (`menu_id`),
    KEY `idx_food_meal_spec_restaurant` (`restaurant_id`),
    KEY `idx_food_meal_spec_tenant` (`tenant_code`, `del_flag`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='订餐菜单项规格';

SET @schema_name = DATABASE();

SET @sql = IF(
    EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema = @schema_name AND table_name = 'app_food_meal_order_item' AND column_name = 'spec_id'),
    'SELECT 1',
    'ALTER TABLE `app_food_meal_order_item` ADD COLUMN `spec_id` varchar(64) DEFAULT NULL COMMENT ''规格ID'' 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 = 'app_food_meal_order_item' AND column_name = 'spec_name'),
    'SELECT 1',
    'ALTER TABLE `app_food_meal_order_item` ADD COLUMN `spec_name` varchar(100) DEFAULT NULL COMMENT ''规格名称'' AFTER `spec_id`'
);
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 = 'app_food_meal_order_item' AND column_name = 'spec_description'),
    'SELECT 1',
    'ALTER TABLE `app_food_meal_order_item` ADD COLUMN `spec_description` varchar(255) DEFAULT NULL COMMENT ''规格描述'' AFTER `spec_name`'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @sql = IF(
    EXISTS (SELECT 1 FROM information_schema.statistics WHERE table_schema = @schema_name AND table_name = 'app_food_meal_order_item' AND index_name = 'idx_food_meal_order_item_spec'),
    'SELECT 1',
    'ALTER TABLE `app_food_meal_order_item` ADD KEY `idx_food_meal_order_item_spec` (`spec_id`)'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
