-- ========================================
-- 智能餐饮管理系统 - 数据库初始化脚本
-- ========================================

-- 创建数据库
CREATE DATABASE IF NOT EXISTS restaurant_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
USE restaurant_db;

-- ========================================
-- 1. 用户表
-- ========================================
CREATE TABLE users (
    id INT PRIMARY KEY AUTO_INCREMENT COMMENT '用户ID',
    username VARCHAR(50) UNIQUE NOT NULL COMMENT '用户名',
    password VARCHAR(255) NOT NULL COMMENT '密码（加密）',
    real_name VARCHAR(50) COMMENT '真实姓名',
    phone VARCHAR(20) COMMENT '手机号',
    email VARCHAR(100) COMMENT '邮箱',
    role ENUM('admin', 'waiter', 'customer') NOT NULL DEFAULT 'customer' COMMENT '角色',
    avatar VARCHAR(255) COMMENT '头像URL',
    status TINYINT DEFAULT 1 COMMENT '状态：1启用/0禁用',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    INDEX idx_username (username)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户表';

-- ========================================
-- 2. 菜品分类表
-- ========================================
CREATE TABLE dish_categories (
    id INT PRIMARY KEY AUTO_INCREMENT COMMENT '分类ID',
    name VARCHAR(50) NOT NULL COMMENT '分类名称',
    description VARCHAR(200) COMMENT '分类描述',
    sort_order INT DEFAULT 0 COMMENT '排序序号',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='菜品分类表';

-- ========================================
-- 3. 菜品表
-- ========================================
CREATE TABLE dishes (
    id INT PRIMARY KEY AUTO_INCREMENT COMMENT '菜品ID',
    category_id INT COMMENT '分类ID',
    name VARCHAR(100) NOT NULL COMMENT '菜品名称',
    price DECIMAL(10,2) NOT NULL COMMENT '价格',
    image VARCHAR(255) COMMENT '菜品图片URL',
    description TEXT COMMENT '菜品描述',
    taste VARCHAR(100) COMMENT '口味',
    ingredients TEXT COMMENT '原材料清单',
    cooking_time INT COMMENT '预计制作时间（分钟）',
    is_available TINYINT DEFAULT 1 COMMENT '是否可售：1是/0否',
    status TINYINT DEFAULT 1 COMMENT '状态：1启用/0禁用',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    FOREIGN KEY (category_id) REFERENCES dish_categories(id) ON DELETE SET NULL,
    INDEX idx_category (category_id),
    INDEX idx_name (name)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='菜品表';

-- ========================================
-- 4. 菜单表
-- ========================================
CREATE TABLE menus (
    id INT PRIMARY KEY AUTO_INCREMENT COMMENT '菜单ID',
    name VARCHAR(100) NOT NULL COMMENT '菜单名称',
    type ENUM('lunch', 'dinner', 'holiday') NOT NULL COMMENT '类型',
    start_date DATE COMMENT '开始日期',
    end_date DATE COMMENT '结束日期',
    is_active TINYINT DEFAULT 1 COMMENT '是否启用',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='菜单表';

-- ========================================
-- 5. 菜单-菜品关联表
-- ========================================
CREATE TABLE menu_dishes (
    id INT PRIMARY KEY AUTO_INCREMENT COMMENT '关联ID',
    menu_id INT NOT NULL COMMENT '菜单ID',
    dish_id INT NOT NULL COMMENT '菜品ID',
    special_price DECIMAL(10,2) COMMENT '特价',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    FOREIGN KEY (menu_id) REFERENCES menus(id) ON DELETE CASCADE,
    FOREIGN KEY (dish_id) REFERENCES dishes(id) ON DELETE CASCADE,
    INDEX idx_menu (menu_id),
    INDEX idx_dish (dish_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='菜单-菜品关联表';

-- ========================================
-- 6. 订单表
-- ========================================
CREATE TABLE orders (
    id INT PRIMARY KEY AUTO_INCREMENT COMMENT '订单ID',
    order_no VARCHAR(50) UNIQUE NOT NULL COMMENT '订单编号',
    customer_id INT COMMENT '顾客ID',
    waiter_id INT COMMENT '服务员ID',
    table_number VARCHAR(20) COMMENT '桌号',
    total_amount DECIMAL(10,2) NOT NULL COMMENT '总金额',
    discount_amount DECIMAL(10,2) DEFAULT 0 COMMENT '折扣金额',
    final_amount DECIMAL(10,2) NOT NULL COMMENT '实付金额',
    status ENUM('pending', 'confirmed', 'cooking', 'served', 'paid', 'cancelled') DEFAULT 'pending' COMMENT '订单状态',
    special_request TEXT COMMENT '特殊要求',
    order_type ENUM('dine_in', 'takeaway') DEFAULT 'dine_in' COMMENT '订单类型',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT '下单时间',
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    FOREIGN KEY (customer_id) REFERENCES users(id) ON DELETE SET NULL,
    FOREIGN KEY (waiter_id) REFERENCES users(id) ON DELETE SET NULL,
    INDEX idx_order_no (order_no),
    INDEX idx_customer (customer_id),
    INDEX idx_created (created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='订单表';

-- ========================================
-- 7. 订单明细表
-- ========================================
CREATE TABLE order_items (
    id INT PRIMARY KEY AUTO_INCREMENT COMMENT '明细ID',
    order_id INT NOT NULL COMMENT '订单ID',
    dish_id INT COMMENT '菜品ID',
    dish_name VARCHAR(100) NOT NULL COMMENT '菜品名称',
    price DECIMAL(10,2) NOT NULL COMMENT '单价',
    quantity INT NOT NULL COMMENT '数量',
    subtotal DECIMAL(10,2) NOT NULL COMMENT '小计',
    special_request VARCHAR(200) COMMENT '特殊要求',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    FOREIGN KEY (order_id) REFERENCES orders(id) ON DELETE CASCADE,
    FOREIGN KEY (dish_id) REFERENCES dishes(id) ON DELETE SET NULL,
    INDEX idx_order (order_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='订单明细表';

-- ========================================
-- 8. 食材库存表
-- ========================================
CREATE TABLE inventory (
    id INT PRIMARY KEY AUTO_INCREMENT COMMENT '库存ID',
    name VARCHAR(100) NOT NULL COMMENT '食材名称',
    unit VARCHAR(20) COMMENT '单位',
    quantity DECIMAL(10,2) DEFAULT 0 COMMENT '当前库存数量',
    min_quantity DECIMAL(10,2) COMMENT '最小库存预警值',
    unit_price DECIMAL(10,2) COMMENT '单价',
    expiry_date DATE COMMENT '过期日期',
    status TINYINT DEFAULT 1 COMMENT '状态：1正常/0停用',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    INDEX idx_name (name)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='食材库存表';

-- ========================================
-- 9. 库存变动记录表
-- ========================================
CREATE TABLE inventory_logs (
    id INT PRIMARY KEY AUTO_INCREMENT COMMENT '记录ID',
    inventory_id INT NOT NULL COMMENT '库存ID',
    type ENUM('in', 'out') NOT NULL COMMENT '类型：in入库/out出库',
    quantity DECIMAL(10,2) NOT NULL COMMENT '数量',
    operator_id INT COMMENT '操作人ID',
    remark VARCHAR(200) COMMENT '备注',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT '操作时间',
    FOREIGN KEY (inventory_id) REFERENCES inventory(id) ON DELETE CASCADE,
    FOREIGN KEY (operator_id) REFERENCES users(id) ON DELETE SET NULL,
    INDEX idx_inventory (inventory_id),
    INDEX idx_created (created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='库存变动记录表';

-- ========================================
-- 10. 预订表
-- ========================================
CREATE TABLE reservations (
    id INT PRIMARY KEY AUTO_INCREMENT COMMENT '预订ID',
    reservation_no VARCHAR(50) UNIQUE COMMENT '预订编号',
    customer_id INT COMMENT '顾客ID',
    type ENUM('table', 'takeaway') NOT NULL COMMENT '类型',
    reservation_date DATE NOT NULL COMMENT '预订日期',
    reservation_time TIME NOT NULL COMMENT '预订时间',
    people_count INT COMMENT '人数',
    table_number VARCHAR(20) COMMENT '桌号',
    contact_phone VARCHAR(20) COMMENT '联系电话',
    special_request TEXT COMMENT '特殊要求',
    status ENUM('pending', 'confirmed', 'cancelled', 'completed') DEFAULT 'pending' COMMENT '状态',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    FOREIGN KEY (customer_id) REFERENCES users(id) ON DELETE SET NULL,
    INDEX idx_reservation_no (reservation_no),
    INDEX idx_date (reservation_date)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='预订表';

-- ========================================
-- 11. 会员信息表
-- ========================================
CREATE TABLE members (
    id INT PRIMARY KEY AUTO_INCREMENT COMMENT '会员ID',
    user_id INT UNIQUE COMMENT '用户ID',
    member_no VARCHAR(50) UNIQUE COMMENT '会员编号',
    points INT DEFAULT 0 COMMENT '积分',
    level ENUM('bronze', 'silver', 'gold', 'diamond') DEFAULT 'bronze' COMMENT '会员等级',
    total_spent DECIMAL(10,2) DEFAULT 0 COMMENT '累计消费',
    join_date DATE NOT NULL COMMENT '加入日期',
    status TINYINT DEFAULT 1 COMMENT '状态：1正常/0禁用',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
    INDEX idx_user (user_id),
    INDEX idx_member_no (member_no)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='会员信息表';

-- ========================================
-- 12. 优惠券表
-- ========================================
CREATE TABLE coupons (
    id INT PRIMARY KEY AUTO_INCREMENT COMMENT '优惠券ID',
    name VARCHAR(100) NOT NULL COMMENT '优惠券名称',
    type ENUM('discount', 'cash') NOT NULL COMMENT '类型：discount折扣/cash代金券',
    discount_value DECIMAL(10,2) NOT NULL COMMENT '折扣值',
    min_amount DECIMAL(10,2) DEFAULT 0 COMMENT '最低消费金额',
    start_date DATE NOT NULL COMMENT '开始日期',
    end_date DATE NOT NULL COMMENT '结束日期',
    total_quantity INT NOT NULL COMMENT '总发放数量',
    used_quantity INT DEFAULT 0 COMMENT '已使用数量',
    status TINYINT DEFAULT 1 COMMENT '状态：1启用/0禁用',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='优惠券表';

-- ========================================
-- 13. 用户优惠券表
-- ========================================
CREATE TABLE user_coupons (
    id INT PRIMARY KEY AUTO_INCREMENT COMMENT '记录ID',
    user_id INT NOT NULL COMMENT '用户ID',
    coupon_id INT NOT NULL COMMENT '优惠券ID',
    is_used TINYINT DEFAULT 0 COMMENT '是否已使用：1是/0否',
    used_at TIMESTAMP NULL COMMENT '使用时间',
    order_id INT COMMENT '使用的订单ID',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT '领取时间',
    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
    FOREIGN KEY (coupon_id) REFERENCES coupons(id) ON DELETE CASCADE,
    FOREIGN KEY (order_id) REFERENCES orders(id) ON DELETE SET NULL,
    INDEX idx_user (user_id),
    INDEX idx_coupon (coupon_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户优惠券表';

-- ========================================
-- 14. 支付记录表
-- ========================================
CREATE TABLE payments (
    id INT PRIMARY KEY AUTO_INCREMENT COMMENT '支付ID',
    order_id INT COMMENT '订单ID',
    payment_no VARCHAR(50) UNIQUE COMMENT '支付单号',
    amount DECIMAL(10,2) NOT NULL COMMENT '支付金额',
    payment_method ENUM('cash', 'card', 'wechat', 'alipay') NOT NULL COMMENT '支付方式',
    status ENUM('pending', 'success', 'failed') DEFAULT 'pending' COMMENT '支付状态',
    transaction_id VARCHAR(100) COMMENT '第三方交易号',
    paid_at TIMESTAMP NULL COMMENT '支付时间',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    FOREIGN KEY (order_id) REFERENCES orders(id) ON DELETE SET NULL,
    INDEX idx_payment_no (payment_no),
    INDEX idx_order (order_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='支付记录表';

-- ========================================
-- 15. 发票表
-- ========================================
CREATE TABLE invoices (
    id INT PRIMARY KEY AUTO_INCREMENT COMMENT '发票ID',
    order_id INT COMMENT '订单ID',
    invoice_no VARCHAR(50) UNIQUE COMMENT '发票号码',
    title VARCHAR(200) NOT NULL COMMENT '发票抬头',
    tax_id VARCHAR(50) COMMENT '税号',
    amount DECIMAL(10,2) NOT NULL COMMENT '金额',
    invoice_type ENUM('personal', 'company') DEFAULT 'personal' COMMENT '发票类型',
    status TINYINT DEFAULT 1 COMMENT '状态：1已开/0作废',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT '开票时间',
    FOREIGN KEY (order_id) REFERENCES orders(id) ON DELETE SET NULL,
    INDEX idx_invoice_no (invoice_no)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='发票表';

-- ========================================
-- 插入初始数据
-- ========================================

-- 插入默认管理员账户（密码: [REDACTED]）
INSERT INTO users (username, password, real_name, role, phone, email) VALUES
('admin', '$2a$10$8YvZ9qP7qH5LqJxZ5xYqVOxKpYqXqXqXqXqXqXqXqXqXqXqXqXqXq', '系统管理员', 'admin', '13800138000', 'admin@restaurant.com'),
('waiter', '$2a$10$8YvZ9qP7qH5LqJxZ5xYqVOxKpYqXqXqXqXqXqXqXqXqXqXqXqXqXq', '服务员小王', 'waiter', '13800138001', 'waiter@restaurant.com'),
('customer', '$2a$10$8YvZ9qP7qH5LqJxZ5xYqVOxKpYqXqXqXqXqXqXqXqXqXqXqXqXqXq', '张三', 'customer', '13800138002', 'customer@restaurant.com');

-- 插入菜品分类
INSERT INTO dish_categories (name, description, sort_order) VALUES
('川菜', '麻辣鲜香的川菜系列', 1),
('粤菜', '清淡爽口的粤菜系列', 2),
('凉菜', '开胃小菜', 3),
('主食', '米饭面条等主食', 4),
('饮品', '各类饮品', 5);

-- 插入示例菜品
INSERT INTO dishes (category_id, name, price, description, taste, ingredients, cooking_time, is_available) VALUES
(1, '宫保鸡丁', 38.00, '经典川菜，鸡肉配花生米', '微辣', '鸡肉,花生米,辣椒,葱姜蒜', 15, 1),
(1, '麻婆豆腐', 28.00, '麻辣鲜香的豆腐', '中辣', '豆腐,猪肉末,豆瓣酱,花椒', 12, 1),
(2, '白切鸡', 48.00, '原汁原味的白切鸡', '清淡', '走地鸡,姜葱', 30, 1),
(2, '清蒸鱼', 68.00, '新鲜海鱼清蒸', '清淡', '鲈鱼,姜葱,蒸鱼豉油', 20, 1),
(3, '拍黄瓜', 12.00, '清爽开胃小菜', '微辣', '黄瓜,蒜末,辣椒油', 5, 1),
(4, '白米饭', 3.00, '东北优质大米', '原味', '大米', 2, 1),
(5, '可乐', 8.00, '冰镇可乐', '甜', '可口可乐', 1, 1);

-- 插入示例菜单
INSERT INTO menus (name, type, is_active) VALUES
('午市套餐', 'lunch', 1),
('晚市套餐', 'dinner', 1);

-- 关联菜单和菜品
INSERT INTO menu_dishes (menu_id, dish_id) VALUES
(1, 1), (1, 2), (1, 5), (1, 6), (1, 7),
(2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (2, 6), (2, 7);

-- 插入示例库存
INSERT INTO inventory (name, unit, quantity, min_quantity, unit_price) VALUES
('鸡肉', 'kg', 50.00, 10.00, 18.00),
('豆腐', '块', 30.00, 5.00, 3.50),
('鲈鱼', 'kg', 20.00, 5.00, 35.00),
('黄瓜', 'kg', 15.00, 3.00, 4.00),
('大米', 'kg', 100.00, 20.00, 5.00);

-- 创建会员账户示例
INSERT INTO members (user_id, member_no, points, level, join_date) VALUES
(3, 'M2024010001', 500, 'bronze', CURDATE());

-- 插入示例优惠券
INSERT INTO coupons (name, type, discount_value, min_amount, start_date, end_date, total_quantity) VALUES
('新用户立减10元', 'cash', 10.00, 50.00, CURDATE(), DATE_ADD(CURDATE(), INTERVAL 30 DAY), 100),
('满100减20', 'cash', 20.00, 100.00, CURDATE(), DATE_ADD(CURDATE(), INTERVAL 30 DAY), 50),
('8折优惠券', 'discount', 0.80, 0.00, CURDATE(), DATE_ADD(CURDATE(), INTERVAL 30 DAY), 200);

-- ========================================
-- 完成
-- ========================================
SELECT '数据库初始化完成！' AS Message;
