From 32682a4137f9807ff2caa5f0211c44a6252e0d4e Mon Sep 17 00:00:00 2001 From: Nick Zeng Date: Wed, 12 Nov 2025 12:46:44 +0800 Subject: [PATCH] add order items table --- ..._11_12_021228_create_order_items_table.php | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 backend/migrations/2025_11_12_021228_create_order_items_table.php diff --git a/backend/migrations/2025_11_12_021228_create_order_items_table.php b/backend/migrations/2025_11_12_021228_create_order_items_table.php new file mode 100644 index 0000000..cdc2028 --- /dev/null +++ b/backend/migrations/2025_11_12_021228_create_order_items_table.php @@ -0,0 +1,62 @@ +bigIncrements('id')->comment('主键'); + $table->integer('company_id')->comment('公司 ID 与 Tools 保持一致'); + $table->integer('platform_id')->comment('平台 ID 与 Tools 保持一致'); + $table->integer('store_id')->comment('店铺 ID 与 Tools 保持一致'); + $table->integer('order_id')->comment('父订单 ID'); + $table->text('platform_order_id')->comment('平台父订单 ID'); + $table->text('sub_order_id')->nullable()->default(null)->comment('平台子订单 ID,部分平台有,如 Tmall'); + $table->integer('sub_order_type_id')->nullable()->default(null)->comment('平台子订单 类型 id,标记字段'); + $table->integer('product_id')->comment('商品 ID, 与 Tools 保持一致'); + $table->string('platform_product_id')->comment('平台商品 ID, 与 Tools 保持一致'); + $table->string('product_sku')->nullable()->default(null)->comment('商品 SKU 编码, 客户提供的内部商品编码'); + $table->string('product_barcode')->nullable()->default(null)->comment('商品 条形码 GTIN 等'); + + $table->double('unit_price', 10, 2)->default(0)->comment('单件价格'); + $table->integer('quantity')->default(0)->comment('商品子项总数量'); + $table->double('discount', 10, 2)->default(0)->comment('订单子项总的折扣金额'); + $table->double('total', 10, 2)->default(0)->comment('总金额 = 单价X数量 - 折扣'); + + $table->jsonb('ext')->nullable()->default(null)->comment('扩展字段'); + + $table->timestampsTz(); + + // 单字段索引 + $table->index('company_id'); + $table->index('platform_id'); + $table->index('store_id'); + $table->index('order_id'); + $table->index('product_id'); + + // 复合索引:支持按店铺统计 SKU 和 barcode(包含 NULL 值,支持 IS NULL 和 IS NOT NULL 查询) + $table->index(['store_id', 'product_sku'], 'order_items_store_sku_idx'); + $table->index(['store_id', 'product_barcode'], 'order_items_store_barcode_idx'); + + // GIN 索引需要使用原生 SQL + }); + + // 为 jsonb 字段创建 GIN 索引(PostgreSQL) + Schema::getConnection()->statement('CREATE INDEX order_items_ext_gin_idx ON order_items USING gin (ext)'); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('order_items'); + } +};