feat: 新增食物库模块,含模型、服务、API及初始化数据

This commit is contained in:
richarjiang
2025-08-29 09:06:18 +08:00
parent a1c21d8a23
commit 74faebd73d
10 changed files with 721 additions and 0 deletions

View File

@@ -0,0 +1,80 @@
-- 创建食物分类表
-- 该表用于存储食物的分类信息,如常见、水果蔬菜、肉蛋奶等
CREATE TABLE IF NOT EXISTS `t_food_categories` (
`key` varchar(50) NOT NULL COMMENT '分类唯一键(英文/下划线)',
`name` varchar(50) NOT NULL COMMENT '分类中文名称',
`icon` varchar(100) DEFAULT NULL COMMENT '分类图标',
`sort_order` int NOT NULL DEFAULT '0' COMMENT '排序(升序)',
`is_system` tinyint(1) NOT NULL DEFAULT '1' COMMENT '是否系统分类1系统0用户自定义',
`created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (`key`),
KEY `idx_sort_order` (`sort_order`),
KEY `idx_is_system` (`is_system`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='食物分类表';
-- 创建食物库表
-- 该表用于存储食物的基本信息和营养成分
CREATE TABLE IF NOT EXISTS `t_food_library` (
`id` bigint NOT NULL AUTO_INCREMENT COMMENT '主键ID',
`name` varchar(100) NOT NULL COMMENT '食物名称',
`description` varchar(500) DEFAULT NULL COMMENT '食物描述',
`category_key` varchar(50) NOT NULL COMMENT '分类键',
`calories_per_100g` float DEFAULT NULL COMMENT '每100克热量卡路里',
`protein_per_100g` float DEFAULT NULL COMMENT '每100克蛋白质含量',
`carbohydrate_per_100g` float DEFAULT NULL COMMENT '每100克碳水化合物含量',
`fat_per_100g` float DEFAULT NULL COMMENT '每100克脂肪含量',
`fiber_per_100g` float DEFAULT NULL COMMENT '每100克膳食纤维含量',
`sugar_per_100g` float DEFAULT NULL COMMENT '每100克糖分含量',
`sodium_per_100g` float DEFAULT NULL COMMENT '每100克钠含量毫克',
`additional_nutrition` json DEFAULT NULL COMMENT '其他营养信息(维生素、矿物质等)',
`is_common` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否常见食物1常见0不常见',
`image_url` varchar(500) DEFAULT NULL COMMENT '食物图片URL',
`sort_order` int NOT NULL DEFAULT '0' COMMENT '排序(分类内)',
`created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (`id`),
KEY `idx_category_key` (`category_key`),
KEY `idx_is_common` (`is_common`),
KEY `idx_name` (`name`),
KEY `idx_category_sort` (`category_key`, `sort_order`),
CONSTRAINT `fk_food_category` FOREIGN KEY (`category_key`) REFERENCES `t_food_categories` (`key`) ON DELETE RESTRICT ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='食物库表';
-- 插入食物分类数据
INSERT INTO `t_food_categories` (`key`, `name`, `sort_order`, `is_system`) VALUES
('common', '常见', 1, 1),
('fruits_vegetables', '水果蔬菜', 2, 1),
('meat_eggs_dairy', '肉蛋奶', 3, 1),
('beans_nuts', '豆类坚果', 4, 1),
('snacks_drinks', '零食饮料', 5, 1),
('staple_food', '主食', 6, 1),
('dishes', '菜肴', 7, 1);
-- 插入示例食物数据
INSERT INTO `t_food_library` (`name`, `category_key`, `calories_per_100g`, `protein_per_100g`, `carbohydrate_per_100g`, `fat_per_100g`, `fiber_per_100g`, `sugar_per_100g`, `sodium_per_100g`, `is_common`, `sort_order`) VALUES
-- 常见食物
('无糖美式咖啡', 'common', 1, 0.1, 0, 0, 0, 0, 2, 1, 1),
('荷包蛋(油煎)', 'common', 195, 13.3, 0.7, 15.3, 0, 0.7, 124, 1, 2),
('鸡蛋', 'common', 139, 13.3, 2.8, 8.8, 0, 2.8, 131, 1, 3),
-- 水果蔬菜
('香蕉', 'fruits_vegetables', 93, 1.4, 22.8, 0.2, 1.7, 17.2, 1, 1, 1),
('猕猴桃', 'fruits_vegetables', 61, 1.1, 14.7, 0.5, 3, 9, 3, 1, 2),
('苹果', 'fruits_vegetables', 53, 0.3, 14.1, 0.2, 2.4, 10.4, 1, 1, 3),
('草莓', 'fruits_vegetables', 32, 0.7, 7.7, 0.3, 2, 4.9, 1, 1, 4),
-- 主食
('蛋烧麦', 'staple_food', 157, 6.2, 22.2, 5.2, 1.1, 1.8, 230, 1, 1),
('米饭', 'staple_food', 116, 2.6, 25.9, 0.3, 0.3, 0.1, 5, 1, 2),
-- 零食饮料
('无糖美式咖啡', 'snacks_drinks', 1, 0.1, 0, 0, 0, 0, 2, 0, 1),
-- 肉蛋奶
('鸡蛋', 'meat_eggs_dairy', 139, 13.3, 2.8, 8.8, 0, 2.8, 131, 0, 1),
('荷包蛋(油煎)', 'meat_eggs_dairy', 195, 13.3, 0.7, 15.3, 0, 0.7, 124, 0, 2);
-- 创建索引以优化查询性能
CREATE INDEX `idx_food_common_category` ON `t_food_library` (`is_common`, `category_key`);
CREATE INDEX `idx_food_name_search` ON `t_food_library` (`name`);