订单系统完整表结构.sql 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. -- =============================================
  2. -- 订单系统完整表结构(最终版本)
  3. -- 包含所有新增表和优化后的表结构
  4. -- =============================================
  5. -- =============================================
  6. -- 一、新增表
  7. -- =============================================
  8. -- 1. 购物车表
  9. CREATE TABLE IF NOT EXISTS `store_cart` (
  10. `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  11. `table_id` int(11) NOT NULL COMMENT '桌号ID',
  12. `store_id` int(11) NOT NULL COMMENT '门店ID',
  13. `cuisine_id` int(11) NOT NULL COMMENT '菜品ID',
  14. `cuisine_name` varchar(200) NOT NULL COMMENT '菜品名称',
  15. `cuisine_image` varchar(500) DEFAULT NULL COMMENT '菜品图片',
  16. `unit_price` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '单价',
  17. `quantity` int(11) NOT NULL DEFAULT '1' COMMENT '数量',
  18. `subtotal_amount` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '小计金额',
  19. `add_user_id` int(11) DEFAULT NULL COMMENT '添加该菜品的用户ID',
  20. `add_user_phone` varchar(20) DEFAULT NULL COMMENT '添加该菜品的用户手机号',
  21. `remark` varchar(500) DEFAULT NULL COMMENT '备注',
  22. `delete_flag` tinyint(4) NOT NULL DEFAULT '0' COMMENT '删除标记, 0:未删除, 1:已删除',
  23. `created_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  24. `created_user_id` int(11) DEFAULT NULL COMMENT '创建人ID',
  25. `updated_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
  26. `updated_user_id` int(11) DEFAULT NULL COMMENT '修改人ID',
  27. PRIMARY KEY (`id`),
  28. KEY `idx_table_id` (`table_id`),
  29. KEY `idx_store_id` (`store_id`),
  30. KEY `idx_cuisine_id` (`cuisine_id`),
  31. KEY `idx_add_user_id` (`add_user_id`),
  32. KEY `idx_created_time` (`created_time`),
  33. KEY `idx_table_delete` (`table_id`, `delete_flag`)
  34. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='购物车表';
  35. -- 2. 优惠券使用记录表
  36. CREATE TABLE IF NOT EXISTS `store_coupon_usage` (
  37. `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  38. `table_id` int(11) NOT NULL COMMENT '桌号ID',
  39. `store_id` int(11) NOT NULL COMMENT '门店ID',
  40. `order_id` int(11) DEFAULT NULL COMMENT '订单ID',
  41. `coupon_id` int(11) NOT NULL COMMENT '优惠券ID',
  42. `coupon_name` varchar(200) DEFAULT NULL COMMENT '优惠券名称',
  43. `discount_amount` decimal(10,2) DEFAULT '0.00' COMMENT '优惠金额',
  44. `usage_status` tinyint(4) NOT NULL DEFAULT '0' COMMENT '使用状态(0:已标记使用, 1:已下单, 2:已支付, 3:已取消)',
  45. `from_table_id` int(11) DEFAULT NULL COMMENT '换桌前的桌号ID',
  46. `migrate_time` datetime DEFAULT NULL COMMENT '换桌迁移时间',
  47. `delete_flag` tinyint(4) NOT NULL DEFAULT '0' COMMENT '删除标记, 0:未删除, 1:已删除',
  48. `created_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  49. `created_user_id` int(11) DEFAULT NULL COMMENT '创建人ID',
  50. `updated_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
  51. `updated_user_id` int(11) DEFAULT NULL COMMENT '修改人ID',
  52. PRIMARY KEY (`id`),
  53. UNIQUE KEY `uk_table_coupon` (`table_id`, `coupon_id`, `delete_flag`),
  54. KEY `idx_table_id` (`table_id`),
  55. KEY `idx_store_id` (`store_id`),
  56. KEY `idx_order_id` (`order_id`),
  57. KEY `idx_coupon_id` (`coupon_id`),
  58. KEY `idx_usage_status` (`usage_status`),
  59. KEY `idx_created_time` (`created_time`)
  60. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='优惠券使用记录表';
  61. -- 3. 订单锁定记录表
  62. CREATE TABLE IF NOT EXISTS `store_order_lock` (
  63. `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  64. `table_id` int(11) DEFAULT NULL COMMENT '桌号ID',
  65. `order_id` int(11) DEFAULT NULL COMMENT '订单ID',
  66. `lock_type` tinyint(4) NOT NULL COMMENT '锁定类型(1:下单锁定, 2:结算锁定)',
  67. `lock_user_id` int(11) NOT NULL COMMENT '锁定用户ID',
  68. `lock_user_phone` varchar(20) DEFAULT NULL COMMENT '锁定用户手机号',
  69. `lock_expire_time` datetime NOT NULL COMMENT '锁定过期时间',
  70. `lock_status` tinyint(4) NOT NULL DEFAULT '1' COMMENT '锁定状态(0:已释放, 1:锁定中)',
  71. `release_time` datetime DEFAULT NULL COMMENT '释放时间',
  72. `delete_flag` tinyint(4) NOT NULL DEFAULT '0' COMMENT '删除标记, 0:未删除, 1:已删除',
  73. `created_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  74. `updated_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
  75. PRIMARY KEY (`id`),
  76. KEY `idx_table_id` (`table_id`),
  77. KEY `idx_order_id` (`order_id`),
  78. KEY `idx_lock_type` (`lock_type`),
  79. KEY `idx_lock_status` (`lock_status`),
  80. KEY `idx_lock_expire_time` (`lock_expire_time`),
  81. KEY `idx_table_lock` (`table_id`, `lock_type`, `lock_status`)
  82. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='订单锁定记录表';
  83. -- =============================================
  84. -- 二、订单表(store_order)- 完整结构
  85. -- =============================================
  86. CREATE TABLE IF NOT EXISTS `store_order` (
  87. `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  88. `order_no` varchar(64) NOT NULL COMMENT '订单号',
  89. `store_id` int(11) NOT NULL COMMENT '门店ID',
  90. `table_id` int(11) NOT NULL COMMENT '桌号ID',
  91. `table_number` varchar(50) DEFAULT NULL COMMENT '桌号',
  92. `diner_count` int(11) DEFAULT NULL COMMENT '就餐人数',
  93. `pay_user_id` int(11) DEFAULT NULL COMMENT '支付用户ID',
  94. `pay_user_phone` varchar(20) DEFAULT NULL COMMENT '支付用户手机号',
  95. `contact_phone` varchar(20) DEFAULT NULL COMMENT '联系电话',
  96. `tableware_fee` decimal(10,2) DEFAULT '0.00' COMMENT '餐具费',
  97. `order_status` tinyint(4) NOT NULL DEFAULT '0' COMMENT '订单状态(0:待支付, 1:已支付, 2:已取消, 3:已完成)',
  98. `total_amount` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '订单总金额',
  99. `coupon_id` int(11) DEFAULT NULL COMMENT '优惠券ID',
  100. `current_coupon_id` int(11) DEFAULT NULL COMMENT '当前使用的优惠券ID',
  101. `discount_amount` decimal(10,2) DEFAULT '0.00' COMMENT '优惠金额',
  102. `pay_amount` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '实付金额',
  103. `pay_type` tinyint(4) DEFAULT NULL COMMENT '支付方式(1:微信, 2:支付宝, 3:现金)',
  104. `pay_status` tinyint(4) NOT NULL DEFAULT '0' COMMENT '支付状态(0:未支付, 1:已支付, 2:已退款)',
  105. `pay_time` datetime DEFAULT NULL COMMENT '支付时间',
  106. `pay_trade_no` varchar(128) DEFAULT NULL COMMENT '支付交易号',
  107. `lock_user_id` int(11) DEFAULT NULL COMMENT '锁定用户ID',
  108. `lock_expire_time` datetime DEFAULT NULL COMMENT '锁定过期时间',
  109. `remark` varchar(500) DEFAULT NULL COMMENT '备注',
  110. `delete_flag` tinyint(4) NOT NULL DEFAULT '0' COMMENT '删除标记, 0:未删除, 1:已删除',
  111. `created_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  112. `created_user_id` int(11) DEFAULT NULL COMMENT '创建人ID',
  113. `updated_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
  114. `updated_user_id` int(11) DEFAULT NULL COMMENT '修改人ID',
  115. PRIMARY KEY (`id`),
  116. UNIQUE KEY `uk_order_no` (`order_no`),
  117. KEY `idx_store_id` (`store_id`),
  118. KEY `idx_table_id` (`table_id`),
  119. KEY `idx_pay_user_id` (`pay_user_id`),
  120. KEY `idx_order_status` (`order_status`),
  121. KEY `idx_created_time` (`created_time`),
  122. KEY `idx_current_coupon_id` (`current_coupon_id`),
  123. KEY `idx_lock_user_id` (`lock_user_id`),
  124. KEY `idx_table_status` (`table_id`, `order_status`),
  125. KEY `idx_store_status` (`store_id`, `order_status`)
  126. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='订单表';
  127. -- =============================================
  128. -- 三、订单明细表(store_order_detail)- 完整结构
  129. -- =============================================
  130. CREATE TABLE IF NOT EXISTS `store_order_detail` (
  131. `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  132. `order_id` int(11) NOT NULL COMMENT '订单ID',
  133. `order_no` varchar(64) NOT NULL COMMENT '订单号',
  134. `cuisine_id` int(11) NOT NULL COMMENT '菜品ID',
  135. `cuisine_name` varchar(200) NOT NULL COMMENT '菜品名称',
  136. `cuisine_type` tinyint(4) NOT NULL COMMENT '菜品类型(1:单品, 2:套餐)',
  137. `cuisine_image` varchar(500) DEFAULT NULL COMMENT '菜品图片',
  138. `unit_price` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '单价',
  139. `quantity` int(11) NOT NULL DEFAULT '1' COMMENT '数量',
  140. `subtotal_amount` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '小计金额',
  141. `add_user_id` int(11) DEFAULT NULL COMMENT '添加该菜品的用户ID',
  142. `add_user_phone` varchar(20) DEFAULT NULL COMMENT '添加该菜品的用户手机号',
  143. `is_add_dish` tinyint(4) NOT NULL DEFAULT '0' COMMENT '是否加餐(0:初始下单, 1:加餐)',
  144. `add_dish_time` datetime DEFAULT NULL COMMENT '加餐时间',
  145. `remark` varchar(500) DEFAULT NULL COMMENT '备注',
  146. `delete_flag` tinyint(4) NOT NULL DEFAULT '0' COMMENT '删除标记, 0:未删除, 1:已删除',
  147. `created_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  148. `created_user_id` int(11) DEFAULT NULL COMMENT '创建人ID',
  149. `updated_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
  150. `updated_user_id` int(11) DEFAULT NULL COMMENT '修改人ID',
  151. PRIMARY KEY (`id`),
  152. KEY `idx_order_id` (`order_id`),
  153. KEY `idx_order_no` (`order_no`),
  154. KEY `idx_cuisine_id` (`cuisine_id`),
  155. KEY `idx_add_user_id` (`add_user_id`),
  156. KEY `idx_created_time` (`created_time`),
  157. KEY `idx_order_cuisine` (`order_id`, `cuisine_id`),
  158. KEY `idx_is_add_dish` (`is_add_dish`),
  159. KEY `idx_add_dish_time` (`add_dish_time`)
  160. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='订单明细表';
  161. -- =============================================
  162. -- 四、桌号表(store_table)- 新增字段说明
  163. -- =============================================
  164. -- 注意:此表为已存在的表,以下为新增字段和索引
  165. -- 如果表已存在,请使用 ALTER TABLE 语句添加字段
  166. -- 新增字段(如果表已存在,请执行以下SQL):
  167. -- ALTER TABLE `store_table`
  168. -- ADD COLUMN `current_coupon_id` int(11) DEFAULT NULL COMMENT '当前使用的优惠券ID' AFTER `current_order_id`,
  169. -- ADD COLUMN `cart_item_count` int(11) DEFAULT '0' COMMENT '购物车商品数量' AFTER `current_coupon_id`,
  170. -- ADD COLUMN `cart_total_amount` decimal(10,2) DEFAULT '0.00' COMMENT '购物车总金额' AFTER `cart_item_count`;
  171. -- 就餐人数(首客选桌时填写,就餐中时后续用户共用;结账/清桌后清空):
  172. -- ALTER TABLE `store_table` ADD COLUMN `diner_count` int(11) DEFAULT NULL COMMENT '当前就餐人数' AFTER `status`;
  173. -- 新增索引(如果表已存在,请执行以下SQL):
  174. -- ALTER TABLE `store_table`
  175. -- ADD INDEX `idx_current_coupon_id` (`current_coupon_id`),
  176. -- ADD INDEX `idx_store_status` (`store_id`, `status`);
  177. -- =============================================
  178. -- 表结构说明
  179. -- =============================================
  180. -- 1. store_cart: 购物车表,用于持久化购物车数据
  181. -- 2. store_coupon_usage: 优惠券使用记录表,记录每张桌子的优惠券使用情况
  182. -- 3. store_order_lock: 订单锁定记录表,记录下单和结算时的锁定信息
  183. -- 4. store_order: 订单表,包含所有订单信息(已优化)
  184. -- 5. store_order_detail: 订单明细表,包含订单明细信息(已优化)
  185. -- 6. store_table: 桌号表,包含桌号信息(需添加新字段)