-- ============================================================
-- 家具套餐：主表 + 搭配明细（幂等）
-- 金额一律整数分；original_amount 由明细自动算，package_amount 是独立报价
-- ============================================================

SET @db := DATABASE();

SET @exists := (
    SELECT COUNT(*) FROM information_schema.TABLES
    WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 'cc_package'
);
SET @sql := IF(@exists = 0,
    'CREATE TABLE `cc_package` (
        `id` int unsigned NOT NULL AUTO_INCREMENT,
        `name` varchar(100) NOT NULL DEFAULT '''' COMMENT ''套餐名'',
        `cover` varchar(500) NOT NULL DEFAULT '''' COMMENT ''封面'',
        `subtitle` varchar(200) NOT NULL DEFAULT '''' COMMENT ''副标题'',
        `remark` varchar(255) NOT NULL DEFAULT '''' COMMENT ''备注'',
        `original_amount` int NOT NULL DEFAULT 0 COMMENT ''原价合计（分，倍率=1）'',
        `package_amount` int NOT NULL DEFAULT 0 COMMENT ''套餐报价（分，倍率=1）'',
        `is_hot` tinyint NOT NULL DEFAULT 0 COMMENT ''1=首页热门'',
        `sort` int NOT NULL DEFAULT 0,
        `status` tinyint NOT NULL DEFAULT 0 COMMENT ''0=上架 1=下架'',
        `created_at` int NOT NULL DEFAULT 0,
        `updated_at` int NOT NULL DEFAULT 0,
        `deleted_at` int NOT NULL DEFAULT 0,
        PRIMARY KEY (`id`),
        KEY `idx_package_hot` (`is_hot`, `sort`, `status`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT=''家具套餐''',
    'SELECT ''skip cc_package'' AS msg'
);
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

SET @exists := (
    SELECT COUNT(*) FROM information_schema.TABLES
    WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 'cc_package_item'
);
SET @sql := IF(@exists = 0,
    'CREATE TABLE `cc_package_item` (
        `id` int unsigned NOT NULL AUTO_INCREMENT,
        `package_id` int NOT NULL DEFAULT 0 COMMENT ''套餐'',
        `catalogue_id` int NOT NULL DEFAULT 0 COMMENT ''商品图册'',
        `price_sheet_id` int NOT NULL DEFAULT 0 COMMENT ''规格行'',
        `material_key` varchar(50) NOT NULL DEFAULT ''routine'' COMMENT ''材质键'',
        `quantity` int NOT NULL DEFAULT 1 COMMENT ''数量'',
        `unit_price` int NOT NULL DEFAULT 0 COMMENT ''该规格基准单价快照（分）'',
        `sort` int NOT NULL DEFAULT 0,
        `remark` varchar(255) NOT NULL DEFAULT '''',
        `created_at` int NOT NULL DEFAULT 0,
        `updated_at` int NOT NULL DEFAULT 0,
        `deleted_at` int NOT NULL DEFAULT 0,
        PRIMARY KEY (`id`),
        KEY `idx_package_item_pkg` (`package_id`),
        KEY `idx_package_item_cat` (`catalogue_id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT=''套餐搭配明细''',
    'SELECT ''skip cc_package_item'' AS msg'
);
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
