| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- -- =============================================
- -- 订单系统完整表结构(最终版本)
- -- 包含所有新增表和优化后的表结构
- -- =============================================
- -- =============================================
- -- 一、新增表
- -- =============================================
- -- 1. 购物车表
- CREATE TABLE IF NOT EXISTS `store_cart` (
- `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
- `table_id` int(11) NOT NULL COMMENT '桌号ID',
- `store_id` int(11) NOT NULL COMMENT '门店ID',
- `cuisine_id` int(11) NOT NULL COMMENT '菜品ID',
- `cuisine_name` varchar(200) NOT NULL COMMENT '菜品名称',
- `cuisine_image` varchar(500) DEFAULT NULL COMMENT '菜品图片',
- `unit_price` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '单价',
- `quantity` int(11) NOT NULL DEFAULT '1' COMMENT '数量',
- `subtotal_amount` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '小计金额',
- `add_user_id` int(11) DEFAULT NULL COMMENT '添加该菜品的用户ID',
- `add_user_phone` varchar(20) DEFAULT NULL COMMENT '添加该菜品的用户手机号',
- `remark` varchar(500) DEFAULT NULL COMMENT '备注',
- `delete_flag` tinyint(4) NOT NULL DEFAULT '0' COMMENT '删除标记, 0:未删除, 1:已删除',
- `created_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
- `created_user_id` int(11) DEFAULT NULL COMMENT '创建人ID',
- `updated_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
- `updated_user_id` int(11) DEFAULT NULL COMMENT '修改人ID',
- PRIMARY KEY (`id`),
- KEY `idx_table_id` (`table_id`),
- KEY `idx_store_id` (`store_id`),
- KEY `idx_cuisine_id` (`cuisine_id`),
- KEY `idx_add_user_id` (`add_user_id`),
- KEY `idx_created_time` (`created_time`),
- KEY `idx_table_delete` (`table_id`, `delete_flag`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='购物车表';
- -- 2. 优惠券使用记录表
- CREATE TABLE IF NOT EXISTS `store_coupon_usage` (
- `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
- `table_id` int(11) NOT NULL COMMENT '桌号ID',
- `store_id` int(11) NOT NULL COMMENT '门店ID',
- `order_id` int(11) DEFAULT NULL COMMENT '订单ID',
- `coupon_id` int(11) NOT NULL COMMENT '优惠券ID',
- `coupon_name` varchar(200) DEFAULT NULL COMMENT '优惠券名称',
- `discount_amount` decimal(10,2) DEFAULT '0.00' COMMENT '优惠金额',
- `usage_status` tinyint(4) NOT NULL DEFAULT '0' COMMENT '使用状态(0:已标记使用, 1:已下单, 2:已支付, 3:已取消)',
- `from_table_id` int(11) DEFAULT NULL COMMENT '换桌前的桌号ID',
- `migrate_time` datetime DEFAULT NULL COMMENT '换桌迁移时间',
- `delete_flag` tinyint(4) NOT NULL DEFAULT '0' COMMENT '删除标记, 0:未删除, 1:已删除',
- `created_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
- `created_user_id` int(11) DEFAULT NULL COMMENT '创建人ID',
- `updated_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
- `updated_user_id` int(11) DEFAULT NULL COMMENT '修改人ID',
- PRIMARY KEY (`id`),
- UNIQUE KEY `uk_table_coupon` (`table_id`, `coupon_id`, `delete_flag`),
- KEY `idx_table_id` (`table_id`),
- KEY `idx_store_id` (`store_id`),
- KEY `idx_order_id` (`order_id`),
- KEY `idx_coupon_id` (`coupon_id`),
- KEY `idx_usage_status` (`usage_status`),
- KEY `idx_created_time` (`created_time`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='优惠券使用记录表';
- -- 3. 订单锁定记录表
- CREATE TABLE IF NOT EXISTS `store_order_lock` (
- `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
- `table_id` int(11) DEFAULT NULL COMMENT '桌号ID',
- `order_id` int(11) DEFAULT NULL COMMENT '订单ID',
- `lock_type` tinyint(4) NOT NULL COMMENT '锁定类型(1:下单锁定, 2:结算锁定)',
- `lock_user_id` int(11) NOT NULL COMMENT '锁定用户ID',
- `lock_user_phone` varchar(20) DEFAULT NULL COMMENT '锁定用户手机号',
- `lock_expire_time` datetime NOT NULL COMMENT '锁定过期时间',
- `lock_status` tinyint(4) NOT NULL DEFAULT '1' COMMENT '锁定状态(0:已释放, 1:锁定中)',
- `release_time` datetime DEFAULT NULL COMMENT '释放时间',
- `delete_flag` tinyint(4) NOT NULL DEFAULT '0' COMMENT '删除标记, 0:未删除, 1:已删除',
- `created_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
- `updated_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
- PRIMARY KEY (`id`),
- KEY `idx_table_id` (`table_id`),
- KEY `idx_order_id` (`order_id`),
- KEY `idx_lock_type` (`lock_type`),
- KEY `idx_lock_status` (`lock_status`),
- KEY `idx_lock_expire_time` (`lock_expire_time`),
- KEY `idx_table_lock` (`table_id`, `lock_type`, `lock_status`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='订单锁定记录表';
- -- =============================================
- -- 二、订单表(store_order)- 完整结构
- -- =============================================
- CREATE TABLE IF NOT EXISTS `store_order` (
- `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
- `order_no` varchar(64) NOT NULL COMMENT '订单号',
- `store_id` int(11) NOT NULL COMMENT '门店ID',
- `table_id` int(11) NOT NULL COMMENT '桌号ID',
- `table_number` varchar(50) DEFAULT NULL COMMENT '桌号',
- `diner_count` int(11) DEFAULT NULL COMMENT '就餐人数',
- `pay_user_id` int(11) DEFAULT NULL COMMENT '支付用户ID',
- `pay_user_phone` varchar(20) DEFAULT NULL COMMENT '支付用户手机号',
- `contact_phone` varchar(20) DEFAULT NULL COMMENT '联系电话',
- `tableware_fee` decimal(10,2) DEFAULT '0.00' COMMENT '餐具费',
- `order_status` tinyint(4) NOT NULL DEFAULT '0' COMMENT '订单状态(0:待支付, 1:已支付, 2:已取消, 3:已完成)',
- `total_amount` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '订单总金额',
- `coupon_id` int(11) DEFAULT NULL COMMENT '优惠券ID',
- `current_coupon_id` int(11) DEFAULT NULL COMMENT '当前使用的优惠券ID',
- `discount_amount` decimal(10,2) DEFAULT '0.00' COMMENT '优惠金额',
- `pay_amount` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '实付金额',
- `pay_type` tinyint(4) DEFAULT NULL COMMENT '支付方式(1:微信, 2:支付宝, 3:现金)',
- `pay_status` tinyint(4) NOT NULL DEFAULT '0' COMMENT '支付状态(0:未支付, 1:已支付, 2:已退款)',
- `pay_time` datetime DEFAULT NULL COMMENT '支付时间',
- `pay_trade_no` varchar(128) DEFAULT NULL COMMENT '支付交易号',
- `lock_user_id` int(11) DEFAULT NULL COMMENT '锁定用户ID',
- `lock_expire_time` datetime DEFAULT NULL COMMENT '锁定过期时间',
- `remark` varchar(500) DEFAULT NULL COMMENT '备注',
- `delete_flag` tinyint(4) NOT NULL DEFAULT '0' COMMENT '删除标记, 0:未删除, 1:已删除',
- `created_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
- `created_user_id` int(11) DEFAULT NULL COMMENT '创建人ID',
- `updated_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
- `updated_user_id` int(11) DEFAULT NULL COMMENT '修改人ID',
- PRIMARY KEY (`id`),
- UNIQUE KEY `uk_order_no` (`order_no`),
- KEY `idx_store_id` (`store_id`),
- KEY `idx_table_id` (`table_id`),
- KEY `idx_pay_user_id` (`pay_user_id`),
- KEY `idx_order_status` (`order_status`),
- KEY `idx_created_time` (`created_time`),
- KEY `idx_current_coupon_id` (`current_coupon_id`),
- KEY `idx_lock_user_id` (`lock_user_id`),
- KEY `idx_table_status` (`table_id`, `order_status`),
- KEY `idx_store_status` (`store_id`, `order_status`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='订单表';
- -- =============================================
- -- 三、订单明细表(store_order_detail)- 完整结构
- -- =============================================
- CREATE TABLE IF NOT EXISTS `store_order_detail` (
- `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
- `order_id` int(11) NOT NULL COMMENT '订单ID',
- `order_no` varchar(64) NOT NULL COMMENT '订单号',
- `cuisine_id` int(11) NOT NULL COMMENT '菜品ID',
- `cuisine_name` varchar(200) NOT NULL COMMENT '菜品名称',
- `cuisine_type` tinyint(4) NOT NULL COMMENT '菜品类型(1:单品, 2:套餐)',
- `cuisine_image` varchar(500) DEFAULT NULL COMMENT '菜品图片',
- `unit_price` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '单价',
- `quantity` int(11) NOT NULL DEFAULT '1' COMMENT '数量',
- `subtotal_amount` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '小计金额',
- `add_user_id` int(11) DEFAULT NULL COMMENT '添加该菜品的用户ID',
- `add_user_phone` varchar(20) DEFAULT NULL COMMENT '添加该菜品的用户手机号',
- `is_add_dish` tinyint(4) NOT NULL DEFAULT '0' COMMENT '是否加餐(0:初始下单, 1:加餐)',
- `add_dish_time` datetime DEFAULT NULL COMMENT '加餐时间',
- `remark` varchar(500) DEFAULT NULL COMMENT '备注',
- `delete_flag` tinyint(4) NOT NULL DEFAULT '0' COMMENT '删除标记, 0:未删除, 1:已删除',
- `created_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
- `created_user_id` int(11) DEFAULT NULL COMMENT '创建人ID',
- `updated_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
- `updated_user_id` int(11) DEFAULT NULL COMMENT '修改人ID',
- PRIMARY KEY (`id`),
- KEY `idx_order_id` (`order_id`),
- KEY `idx_order_no` (`order_no`),
- KEY `idx_cuisine_id` (`cuisine_id`),
- KEY `idx_add_user_id` (`add_user_id`),
- KEY `idx_created_time` (`created_time`),
- KEY `idx_order_cuisine` (`order_id`, `cuisine_id`),
- KEY `idx_is_add_dish` (`is_add_dish`),
- KEY `idx_add_dish_time` (`add_dish_time`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='订单明细表';
- -- =============================================
- -- 四、桌号表(store_table)- 新增字段说明
- -- =============================================
- -- 注意:此表为已存在的表,以下为新增字段和索引
- -- 如果表已存在,请使用 ALTER TABLE 语句添加字段
- -- 新增字段(如果表已存在,请执行以下SQL):
- -- ALTER TABLE `store_table`
- -- ADD COLUMN `current_coupon_id` int(11) DEFAULT NULL COMMENT '当前使用的优惠券ID' AFTER `current_order_id`,
- -- ADD COLUMN `cart_item_count` int(11) DEFAULT '0' COMMENT '购物车商品数量' AFTER `current_coupon_id`,
- -- ADD COLUMN `cart_total_amount` decimal(10,2) DEFAULT '0.00' COMMENT '购物车总金额' AFTER `cart_item_count`;
- -- 就餐人数(首客选桌时填写,就餐中时后续用户共用;结账/清桌后清空):
- -- ALTER TABLE `store_table` ADD COLUMN `diner_count` int(11) DEFAULT NULL COMMENT '当前就餐人数' AFTER `status`;
- -- 新增索引(如果表已存在,请执行以下SQL):
- -- ALTER TABLE `store_table`
- -- ADD INDEX `idx_current_coupon_id` (`current_coupon_id`),
- -- ADD INDEX `idx_store_status` (`store_id`, `status`);
- -- =============================================
- -- 表结构说明
- -- =============================================
- -- 1. store_cart: 购物车表,用于持久化购物车数据
- -- 2. store_coupon_usage: 优惠券使用记录表,记录每张桌子的优惠券使用情况
- -- 3. store_order_lock: 订单锁定记录表,记录下单和结算时的锁定信息
- -- 4. store_order: 订单表,包含所有订单信息(已优化)
- -- 5. store_order_detail: 订单明细表,包含订单明细信息(已优化)
- -- 6. store_table: 桌号表,包含桌号信息(需添加新字段)
|