-- ============================================================
-- 数据库迁移脚本
-- 版本: v1.0
-- 日期: 2026-03-25
-- 描述: 为 v2_food 表新增嘌呤含量和重量基准字段
-- ============================================================

-- 1. 新增 purine 字段（嘌呤含量，单位 mg）
ALTER TABLE v2_food
    ADD COLUMN purine DECIMAL(10, 2) DEFAULT NULL COMMENT '嘌呤含量(mg)';

-- 2. 新增 serving_size 字段（营养成分对应重量基准）
ALTER TABLE v2_food
    ADD COLUMN serving_size VARCHAR(50) DEFAULT '每100g' COMMENT '营养成分对应的食物重量基准，如"每100g"、"每份(50g)"';

-- 验证
DESC v2_food;

-- ============================================================
-- 参考数据初始化示例（按需执行）
-- 数据来源: https://www.ishen365.com/article/cereal
-- ============================================================

-- 谷薯类
UPDATE v2_food SET calcium = 13,  iron = 2.3, vitamin_c = 0,  purine = 18.4, serving_size = '每100g' WHERE name = '大米'   AND category_name LIKE '%谷%';
UPDATE v2_food SET calcium = 34,  iron = 5.1, vitamin_c = 0,  purine = 25.0, serving_size = '每100g' WHERE name = '小米'   AND category_name LIKE '%谷%';
UPDATE v2_food SET calcium = 38,  iron = 5.9, vitamin_c = 0,  purine = 22.4, serving_size = '每100g' WHERE name = '玉米'   AND category_name LIKE '%谷%';
UPDATE v2_food SET calcium = 31,  iron = 3.5, vitamin_c = 0,  purine = 17.1, serving_size = '每100g' WHERE name = '面粉'   AND category_name LIKE '%谷%';

-- 蔬菜类
UPDATE v2_food SET calcium = 48,  iron = 1.2, vitamin_c = 14, purine = 10.1, serving_size = '每100g' WHERE name = '菠菜'   AND category_name LIKE '%蔬%';
UPDATE v2_food SET calcium = 36,  iron = 0.5, vitamin_c = 4,  purine = 5.5,  serving_size = '每100g' WHERE name = '西红柿' AND category_name LIKE '%蔬%';
UPDATE v2_food SET calcium = 21,  iron = 0.3, vitamin_c = 2,  purine = 3.4,  serving_size = '每100g' WHERE name = '土豆'   AND category_name LIKE '%蔬%';

-- 水果类
UPDATE v2_food SET calcium = 4,   iron = 0.6, vitamin_c = 8,  purine = 0.9,  serving_size = '每100g' WHERE name = '苹果'   AND category_name LIKE '%水果%';
UPDATE v2_food SET calcium = 7,   iron = 0.2, vitamin_c = 8,  purine = 1.9,  serving_size = '每100g' WHERE name = '香蕉'   AND category_name LIKE '%水果%';

-- 肉蛋类
UPDATE v2_food SET calcium = 11,  iron = 2.5, vitamin_c = 0,  purine = 122.5, serving_size = '每100g' WHERE name = '猪肉' AND category_name LIKE '%肉%';
UPDATE v2_food SET calcium = 9,   iron = 2.3, vitamin_c = 0,  purine = 107.6, serving_size = '每100g' WHERE name = '牛肉' AND category_name LIKE '%肉%';
UPDATE v2_food SET calcium = 56,  iron = 2.0, vitamin_c = 0,  purine = 2.6,   serving_size = '每100g' WHERE name = '鸡蛋' AND category_name LIKE '%蛋%';

-- 豆类
UPDATE v2_food SET calcium = 191, iron = 8.2, vitamin_c = 0,  purine = 166.5, serving_size = '每100g' WHERE name = '黄豆' AND category_name LIKE '%豆%';
UPDATE v2_food SET calcium = 164, iron = 6.4, vitamin_c = 0,  purine = 116.9, serving_size = '每100g' WHERE name = '豆腐' AND category_name LIKE '%豆%';

-- 将所有未设置 serving_size 的记录默认填充
UPDATE v2_food SET serving_size = '每100g' WHERE serving_size IS NULL OR serving_size = '';
