-- 奶茶店管理系统数据库表结构

-- 用户表
CREATE TABLE IF NOT EXISTS `user` (
    `id` BIGINT PRIMARY KEY AUTO_INCREMENT,
    `username` VARCHAR(50) UNIQUE NOT NULL COMMENT '用户名',
    `password` VARCHAR(255) NOT NULL COMMENT '密码（加密）',
    `name` VARCHAR(50) COMMENT '姓名',
    `gender` TINYINT COMMENT '性别：0-女，1-男',
    `phone` VARCHAR(20) COMMENT '手机号',
    `email` VARCHAR(100) COMMENT '邮箱',
    `avatar` VARCHAR(255) COMMENT '头像路径',
    `address` VARCHAR(255) COMMENT '默认地址',
    `create_time` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `update_time` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户表';

-- 用户地址表
CREATE TABLE IF NOT EXISTS `user_address` (
    `id` BIGINT PRIMARY KEY AUTO_INCREMENT,
    `user_id` BIGINT NOT NULL COMMENT '用户ID',
    `address` VARCHAR(255) NOT NULL COMMENT '详细地址',
    `contact_name` VARCHAR(50) COMMENT '联系人姓名',
    `contact_phone` VARCHAR(20) COMMENT '联系人电话',
    `is_default` TINYINT DEFAULT 0 COMMENT '是否默认：0-否，1-是',
    `create_time` DATETIME DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (`user_id`) REFERENCES `user`(`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户地址表';

-- 商家表
CREATE TABLE IF NOT EXISTS `merchant` (
    `id` BIGINT PRIMARY KEY AUTO_INCREMENT,
    `username` VARCHAR(50) UNIQUE NOT NULL COMMENT '用户名',
    `password` VARCHAR(255) NOT NULL COMMENT '密码（加密）',
    `store_name` VARCHAR(100) NOT NULL COMMENT '门店名称',
    `address` VARCHAR(255) COMMENT '门店地址',
    `phone` VARCHAR(20) COMMENT '联系电话',
    `email` VARCHAR(100) COMMENT '邮箱',
    `manager_name` VARCHAR(50) COMMENT '负责人姓名',
    `avatar` VARCHAR(255) COMMENT '头像路径',
    `create_time` DATETIME DEFAULT CURRENT_TIMESTAMP,
    `update_time` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='商家表';

-- 商品表
CREATE TABLE IF NOT EXISTS `product` (
    `id` BIGINT PRIMARY KEY AUTO_INCREMENT,
    `merchant_id` BIGINT NOT NULL COMMENT '商家ID',
    `name` VARCHAR(100) NOT NULL COMMENT '商品名称',
    `category` VARCHAR(50) COMMENT '分类',
    `description` TEXT COMMENT '商品描述',
    `base_price` DECIMAL(10,2) NOT NULL COMMENT '基础价格',
    `image` VARCHAR(255) COMMENT '商品图片',
    `status` TINYINT DEFAULT 1 COMMENT '状态：0-下架，1-上架',
    `create_time` DATETIME DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (`merchant_id`) REFERENCES `merchant`(`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='商品表';

-- 商品规格表
CREATE TABLE IF NOT EXISTS `product_spec` (
    `id` BIGINT PRIMARY KEY AUTO_INCREMENT,
    `product_id` BIGINT NOT NULL COMMENT '商品ID',
    `spec_name` VARCHAR(50) NOT NULL COMMENT '规格名称（大杯/中杯/小杯）',
    `price_adjust` DECIMAL(10,2) DEFAULT 0 COMMENT '价格调整（相对于基础价格）',
    FOREIGN KEY (`product_id`) REFERENCES `product`(`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='商品规格表';

-- 配料表
CREATE TABLE IF NOT EXISTS `product_topping` (
    `id` BIGINT PRIMARY KEY AUTO_INCREMENT,
    `product_id` BIGINT NOT NULL COMMENT '商品ID',
    `topping_name` VARCHAR(50) NOT NULL COMMENT '配料名称',
    `price` DECIMAL(10,2) DEFAULT 0 COMMENT '配料价格',
    FOREIGN KEY (`product_id`) REFERENCES `product`(`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='配料表';

-- 商品定制选项表
CREATE TABLE IF NOT EXISTS `product_custom` (
    `id` BIGINT PRIMARY KEY AUTO_INCREMENT,
    `product_id` BIGINT NOT NULL COMMENT '商品ID',
    `option_type` VARCHAR(20) NOT NULL COMMENT '选项类型：sweetness-甜度，ice-冰度',
    `option_value` VARCHAR(50) NOT NULL COMMENT '选项值',
    `price_adjust` DECIMAL(10,2) DEFAULT 0 COMMENT '价格调整',
    FOREIGN KEY (`product_id`) REFERENCES `product`(`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='商品定制选项表';

-- 购物车表
CREATE TABLE IF NOT EXISTS `cart` (
    `id` BIGINT PRIMARY KEY AUTO_INCREMENT,
    `user_id` BIGINT NOT NULL COMMENT '用户ID',
    `product_id` BIGINT NOT NULL COMMENT '商品ID',
    `spec_id` BIGINT COMMENT '规格ID',
    `quantity` INT DEFAULT 1 COMMENT '数量',
    `custom_sweetness` VARCHAR(20) COMMENT '甜度定制',
    `custom_ice` VARCHAR(20) COMMENT '冰度定制',
    `toppings` TEXT COMMENT '配料JSON',
    `create_time` DATETIME DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (`user_id`) REFERENCES `user`(`id`) ON DELETE CASCADE,
    FOREIGN KEY (`product_id`) REFERENCES `product`(`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='购物车表';

-- 订单表
CREATE TABLE IF NOT EXISTS `order` (
    `id` BIGINT PRIMARY KEY AUTO_INCREMENT,
    `order_no` VARCHAR(50) UNIQUE NOT NULL COMMENT '订单号',
    `user_id` BIGINT NOT NULL COMMENT '用户ID',
    `merchant_id` BIGINT NOT NULL COMMENT '商家ID',
    `total_amount` DECIMAL(10,2) NOT NULL COMMENT '订单总金额',
    `payment_method` VARCHAR(20) COMMENT '支付方式：wechat/alipay',
    `payment_status` TINYINT DEFAULT 0 COMMENT '支付状态：0-未支付，1-已支付',
    `order_status` VARCHAR(20) DEFAULT 'PENDING_PAY' COMMENT '订单状态：PENDING_PAY-待支付，MAKING-制作中，READY-待取餐，COMPLETED-已完成，CANCELLED-已取消',
    `address` VARCHAR(255) NOT NULL COMMENT '收货地址',
    `contact_phone` VARCHAR(20) NOT NULL COMMENT '联系电话',
    `create_time` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `pay_time` DATETIME COMMENT '支付时间',
    `complete_time` DATETIME COMMENT '完成时间',
    FOREIGN KEY (`user_id`) REFERENCES `user`(`id`),
    FOREIGN KEY (`merchant_id`) REFERENCES `merchant`(`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='订单表';

-- 订单明细表
CREATE TABLE IF NOT EXISTS `order_item` (
    `id` BIGINT PRIMARY KEY AUTO_INCREMENT,
    `order_id` BIGINT NOT NULL COMMENT '订单ID',
    `product_id` BIGINT NOT NULL COMMENT '商品ID',
    `product_name` VARCHAR(100) NOT NULL COMMENT '商品名称',
    `spec_name` VARCHAR(50) COMMENT '规格名称',
    `quantity` INT NOT NULL COMMENT '数量',
    `price` DECIMAL(10,2) NOT NULL COMMENT '单价',
    `custom_sweetness` VARCHAR(20) COMMENT '甜度',
    `custom_ice` VARCHAR(20) COMMENT '冰度',
    `toppings` TEXT COMMENT '配料JSON',
    `subtotal` DECIMAL(10,2) NOT NULL COMMENT '小计',
    FOREIGN KEY (`order_id`) REFERENCES `order`(`id`) ON DELETE CASCADE,
    FOREIGN KEY (`product_id`) REFERENCES `product`(`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='订单明细表';

-- 订单跟踪表
CREATE TABLE IF NOT EXISTS `order_track` (
    `id` BIGINT PRIMARY KEY AUTO_INCREMENT,
    `order_id` BIGINT NOT NULL COMMENT '订单ID',
    `status` VARCHAR(20) NOT NULL COMMENT '状态',
    `description` VARCHAR(255) COMMENT '描述',
    `create_time` DATETIME DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (`order_id`) REFERENCES `order`(`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='订单跟踪表';

-- 公告表
CREATE TABLE IF NOT EXISTS `announcement` (
    `id` BIGINT PRIMARY KEY AUTO_INCREMENT,
    `merchant_id` BIGINT NOT NULL COMMENT '商家ID',
    `title` VARCHAR(200) NOT NULL COMMENT '标题',
    `content` TEXT COMMENT '内容',
    `type` VARCHAR(20) DEFAULT 'NOTICE' COMMENT '类型：ACTIVITY-活动，NOTICE-通知',
    `status` TINYINT DEFAULT 1 COMMENT '状态：0-下架，1-发布',
    `create_time` DATETIME DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (`merchant_id`) REFERENCES `merchant`(`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='公告表';

-- 留言反馈表
CREATE TABLE IF NOT EXISTS `message` (
    `id` BIGINT PRIMARY KEY AUTO_INCREMENT,
    `user_id` BIGINT NOT NULL COMMENT '用户ID',
    `merchant_id` BIGINT NOT NULL COMMENT '商家ID',
    `order_id` BIGINT COMMENT '订单ID',
    `content` TEXT NOT NULL COMMENT '留言内容',
    `reply` TEXT COMMENT '回复内容',
    `status` TINYINT DEFAULT 0 COMMENT '状态：0-未回复，1-已回复',
    `create_time` DATETIME DEFAULT CURRENT_TIMESTAMP,
    `reply_time` DATETIME COMMENT '回复时间',
    FOREIGN KEY (`user_id`) REFERENCES `user`(`id`),
    FOREIGN KEY (`merchant_id`) REFERENCES `merchant`(`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='留言反馈表';

-- 评价表
CREATE TABLE IF NOT EXISTS `review` (
    `id` BIGINT PRIMARY KEY AUTO_INCREMENT,
    `user_id` BIGINT NOT NULL COMMENT '用户ID',
    `order_id` BIGINT NOT NULL COMMENT '订单ID',
    `product_id` BIGINT NOT NULL COMMENT '商品ID',
    `rating` TINYINT NOT NULL COMMENT '评分：1-5',
    `content` TEXT COMMENT '评价内容',
    `reply` TEXT COMMENT '商家回复',
    `create_time` DATETIME DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (`user_id`) REFERENCES `user`(`id`),
    FOREIGN KEY (`order_id`) REFERENCES `order`(`id`),
    FOREIGN KEY (`product_id`) REFERENCES `product`(`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='评价表';

-- 库存表
CREATE TABLE IF NOT EXISTS `inventory` (
    `id` BIGINT PRIMARY KEY AUTO_INCREMENT,
    `merchant_id` BIGINT NOT NULL COMMENT '商家ID',
    `material_name` VARCHAR(100) NOT NULL COMMENT '原料名称',
    `quantity` DECIMAL(10,2) NOT NULL COMMENT '数量',
    `unit` VARCHAR(20) COMMENT '单位',
    `min_threshold` DECIMAL(10,2) DEFAULT 0 COMMENT '最低库存阈值',
    `last_in_time` DATETIME COMMENT '最后入库时间',
    `last_out_time` DATETIME COMMENT '最后出库时间',
    FOREIGN KEY (`merchant_id`) REFERENCES `merchant`(`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='库存表';

-- 库存操作日志表
CREATE TABLE IF NOT EXISTS `inventory_log` (
    `id` BIGINT PRIMARY KEY AUTO_INCREMENT,
    `inventory_id` BIGINT NOT NULL COMMENT '库存ID',
    `operation_type` VARCHAR(20) NOT NULL COMMENT '操作类型：IN-入库，OUT-出库',
    `quantity` DECIMAL(10,2) NOT NULL COMMENT '操作数量',
    `operator` VARCHAR(50) COMMENT '操作人',
    `create_time` DATETIME DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (`inventory_id`) REFERENCES `inventory`(`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='库存操作日志表';

-- 用户行为表（用于协同过滤）
CREATE TABLE IF NOT EXISTS `user_behavior` (
    `id` BIGINT PRIMARY KEY AUTO_INCREMENT,
    `user_id` BIGINT NOT NULL COMMENT '用户ID',
    `product_id` BIGINT NOT NULL COMMENT '商品ID',
    `behavior_type` VARCHAR(20) NOT NULL COMMENT '行为类型：VIEW-浏览，PURCHASE-购买，RATING-评分',
    `score` DECIMAL(5,2) DEFAULT 0 COMMENT '评分（0-5）',
    `create_time` DATETIME DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (`user_id`) REFERENCES `user`(`id`) ON DELETE CASCADE,
    FOREIGN KEY (`product_id`) REFERENCES `product`(`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户行为表';

-- 用户相似度缓存表（可选）
CREATE TABLE IF NOT EXISTS `user_similarity` (
    `user1_id` BIGINT NOT NULL COMMENT '用户1ID',
    `user2_id` BIGINT NOT NULL COMMENT '用户2ID',
    `similarity_score` DECIMAL(10,6) NOT NULL COMMENT '相似度分数',
    `update_time` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (`user1_id`, `user2_id`),
    FOREIGN KEY (`user1_id`) REFERENCES `user`(`id`) ON DELETE CASCADE,
    FOREIGN KEY (`user2_id`) REFERENCES `user`(`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户相似度缓存表';

-- 创建索引
CREATE INDEX idx_user_username ON `user`(`username`);
CREATE INDEX idx_merchant_username ON `merchant`(`username`);
CREATE INDEX idx_product_merchant ON `product`(`merchant_id`);
CREATE INDEX idx_product_status ON `product`(`status`);
CREATE INDEX idx_cart_user ON `cart`(`user_id`);
CREATE INDEX idx_order_user ON `order`(`user_id`);
CREATE INDEX idx_order_merchant ON `order`(`merchant_id`);
CREATE INDEX idx_order_status ON `order`(`order_status`);
CREATE INDEX idx_order_no ON `order`(`order_no`);
CREATE INDEX idx_user_behavior_user ON `user_behavior`(`user_id`);
CREATE INDEX idx_user_behavior_product ON `user_behavior`(`product_id`);
