[ 'driver' => 'sqlite', 'database' => ':memory:', 'prefix' => '', ], 'upstream_mirrors.connection' => 'business_mirror', ]); DB::purge('business_mirror'); $schema = Schema::connection('business_mirror'); $schema->create(GoodsProfitAfsLookupSchema::TABLE, function (Blueprint $table): void { $table->integer('id')->primary(); $table->string('_mirror_key')->unique(); foreach ([ 'undefined1', 'undefined2', 'undefined3', 'undefined4', 'undefined5', 'undefined6', 'undefined7', 'undefined8', 'undefined10', 'undefined11', 'undefined12', 'undefined13', 'undefined18', 'undefined20', 'undefined21', 'undefined22', 'undefined43', 'undefined45', 'undefined46', 'undefined47', 'undefined48', 'undefined49', 'undefined50', 'undefined51', 'undefined52', 'undefined53', 'undefined54', 'undefined56', 'undefined59', 'undefined61', 'undefined66', 'undefined72', 'undefined73', 'undefined151', ] as $column) { $table->string($column)->nullable(); } foreach ([ GoodsProfitAfsLookupSchema::ORDER_DATE_COLUMN, GoodsProfitAfsLookupSchema::PAY_DATE_COLUMN, GoodsProfitAfsLookupSchema::SEND_DATE_COLUMN, ] as $column) { $table->string($column, 19)->nullable(); } }); $schema->create('ops_jst_goods_profit_kpi_items_afs_schemas', function (Blueprint $table): void { $table->integer('id'); $table->string('column'); $table->string('name'); }); $schema->create('business_mirror_table_states', function (Blueprint $table): void { $table->string('dataset_key'); $table->dateTime('initial_completed_at')->nullable(); $table->dateTime('last_succeeded_at')->nullable(); $table->string('last_error_code')->nullable(); }); $schema->create(ShengsuanOrderProfitRowMapper::TABLE, function (Blueprint $table): void { $table->bigIncrements('id'); $table->string('shop_name', 191)->default(''); $table->string('order_key', 191)->default(''); $table->decimal('matched_package_fee', 18, 2)->nullable(); $table->decimal('matched_freight_fee', 18, 2)->nullable(); $table->decimal('matched_bill_fee', 18, 2)->nullable(); $table->dateTime('synced_at')->nullable(); $table->timestamps(); }); $db = DB::connection('business_mirror'); $db->table('business_mirror_table_states')->insert([ 'dataset_key' => GoodsProfitAfsRepository::MAIN_DATASET, 'initial_completed_at' => '2026-10-01 00:00:00', 'last_succeeded_at' => '2026-10-30 10:00:00', ]); $headers = [ 'undefined1' => '内部订单号', 'undefined2' => '线上订单号', 'undefined3' => '子订单号', 'undefined4' => '售后单号', 'undefined5' => '支付时间', 'undefined6' => '发货时间', 'undefined7' => '订单时间', 'undefined8' => '店铺名称', 'undefined10' => '商品编码', 'undefined11' => '款式编码', 'undefined12' => '店铺商品编码', 'undefined13' => '店铺款式编码', 'undefined18' => '颜色及规格', 'undefined20' => '商品数量', 'undefined21' => '商品金额', 'undefined22' => '商品成本', 'undefined43' => '发货前退款金额', 'undefined45' => '发货前退平台补贴', 'undefined46' => '发货后申请数量', 'undefined47' => '发货后申请金额', 'undefined48' => '发货后申请成本', 'undefined49' => '发货后退回数量', 'undefined50' => '发货后退回金额', 'undefined51' => '发货后退回成本', 'undefined52' => '发货后退款金额', 'undefined53' => '发货后退款成本', 'undefined54' => '发货后退平台补贴', 'undefined56' => '退款类型', 'undefined59' => '分类单', 'undefined61' => '组合装', 'undefined66' => '发货仓库', 'undefined72' => '快递单号', 'undefined73' => '快递公司', 'undefined151' => '商品名称', ]; foreach ($headers as $index => $name) { $db->table('ops_jst_goods_profit_kpi_items_afs_schemas')->insert([ 'id' => (int) substr($index, 9), 'column' => $index, 'name' => $name, ]); } $base = [ 'undefined2' => '26092142570188', 'undefined3' => '26092142570188_SKU-1E73E04C8008B3AF', 'undefined5' => '2026/9/21 9:41:00', 'undefined6' => '2026/9/22 10:00:00', 'undefined7' => '2026/9/21 9:40:36', '_mirror_afs_order_time' => '2026-09-21 09:40:36', '_mirror_afs_pay_time' => '2026-09-21 09:41:00', '_mirror_afs_send_time' => '2026-09-22 10:00:00', 'undefined8' => '唯品会MP-卓禾家具特卖旗舰店', 'undefined10' => 'BJ018706N68V01', 'undefined11' => 'BJ0187', 'undefined12' => 'SKU-1E73E04C8008B3AF', 'undefined13' => 'SPU-1E73E04C800003AF', 'undefined18' => '胡桃色', 'undefined59' => '否', 'undefined61' => '非组合', 'undefined66' => '简尚-崔桥工厂仓', 'undefined72' => '76001654919378', 'undefined73' => '新北中通', 'undefined151' => '款式一 60/40/53 胡桃色', ]; $db->table(GoodsProfitAfsLookupSchema::TABLE)->insert(array_replace($base, [ 'id' => 1, '_mirror_key' => 'row-1', 'undefined1' => '9470588', 'undefined4' => '', 'undefined20' => '1', 'undefined21' => '259', 'undefined22' => '75', 'undefined43' => '0', 'undefined45' => '0', 'undefined46' => '0', 'undefined47' => '0', 'undefined48' => '0', 'undefined49' => '0', 'undefined50' => '0', 'undefined51' => '0', 'undefined52' => '0', 'undefined53' => '0', 'undefined54' => '0', 'undefined56' => '', ])); $db->table(GoodsProfitAfsLookupSchema::TABLE)->insert(array_replace($base, [ 'id' => 2, '_mirror_key' => 'row-2', 'undefined1' => '9470588', 'undefined4' => '743208893', 'undefined20' => '0', 'undefined21' => '0', 'undefined22' => '0', 'undefined43' => '0', 'undefined45' => '0', 'undefined46' => '0', 'undefined47' => '178.3', 'undefined48' => '0', 'undefined49' => '0', 'undefined50' => '0', 'undefined51' => '0', 'undefined52' => '20', 'undefined53' => '0', 'undefined54' => '0', 'undefined56' => '仅退款', ])); } public function test_order_rows_are_aggregated_from_item_and_refund_lines(): void { $repository = app(OrderProfitRepository::class); $result = $repository->paginate([ 'date_field' => 'pay_time', 'from' => '2026-09-01', 'to' => '2026-09-30', 'order_no' => '26092142570188', 'shop_name' => '', 'sku_id' => '', ], 1, 25); $this->assertCount(1, $result['rows']); $row = $result['rows'][0]; $this->assertSame('唯品会MP-卓禾家具特卖旗舰店', $row['shop_name']); $this->assertSame('26092142570188', $row['online_order_no']); $this->assertSame('9470588', $row['internal_order_no']); $this->assertEquals(75.0, $row['cost_amount']); $this->assertEquals(20.0, $row['refund_amount']); $this->assertEquals(178.3, $row['post_application_amount']); $this->assertEquals(0.0, $row['post_return_amount']); $this->assertSame('仅退款', $row['refund_type']); $this->assertCount(2, $row['items']); $this->assertSame('2026/9/21 9:41:00', $row['items'][0]['pay_time']); $this->assertSame('2026/9/22 10:00:00', $row['items'][0]['send_time']); $this->assertSame(['唯品会MP-卓禾家具特卖旗舰店'], $repository->shopNames()); } public function test_send_time_filter_uses_its_own_indexed_date_column(): void { $repository = app(OrderProfitRepository::class); $inside = $repository->paginate([ 'date_field' => 'send_time', 'from' => '2026-09-22', 'to' => '2026-09-22', 'order_no' => '', 'shop_name' => '', 'sku_id' => '', ], 1, 25); $outside = $repository->paginate([ 'date_field' => 'send_time', 'from' => '2026-09-23', 'to' => '2026-09-23', 'order_no' => '', 'shop_name' => '', 'sku_id' => '', ], 1, 25); $this->assertCount(1, $inside['rows']); $this->assertSame('26092142570188', $inside['rows'][0]['online_order_no']); $this->assertSame([], $outside['rows']); } public function test_order_groups_are_paginated_before_wide_sums_are_calculated(): void { DB::connection('business_mirror')->table(GoodsProfitAfsLookupSchema::TABLE)->insert([ 'id' => 3, '_mirror_key' => 'row-3', 'undefined1' => '9999', 'undefined2' => 'ORDER-NEW', 'undefined5' => '2026/9/23 8:00:00', 'undefined6' => '2026/9/23 10:00:00', 'undefined7' => '2026/9/23 7:59:00', '_mirror_afs_order_time' => '2026-09-23 07:59:00', '_mirror_afs_pay_time' => '2026-09-23 08:00:00', '_mirror_afs_send_time' => '2026-09-23 10:00:00', 'undefined8' => '唯品会MP-卓禾家具特卖旗舰店', 'undefined10' => 'SKU-NEW', 'undefined20' => '1', 'undefined21' => '10', 'undefined22' => '5', 'undefined52' => '0', 'undefined54' => '0', 'undefined56' => '', 'undefined59' => '否', ]); $repository = app(OrderProfitRepository::class); $filters = [ 'date_field' => 'send_time', 'from' => '2026-09-22', 'to' => '2026-09-23', 'order_no' => '', 'shop_name' => '', 'sku_id' => '', ]; $first = $repository->paginate($filters, 1, 1); $second = $repository->paginate($filters, 2, 1); $this->assertSame('ORDER-NEW', $first['rows'][0]['online_order_no']); $this->assertTrue($first['has_more']); $this->assertSame('26092142570188', $second['rows'][0]['online_order_no']); $this->assertFalse($second['has_more']); } public function test_matched_fees_come_from_shengsuan_page_rows(): void { DB::connection('business_mirror')->table(ShengsuanOrderProfitRowMapper::TABLE)->insert([ 'shop_name' => '唯品会MP-卓禾家具特卖旗舰店', 'order_key' => '唯品会MP-卓禾家具特卖旗舰店|26092142570188', 'matched_package_fee' => 0, 'matched_freight_fee' => 10.5, 'matched_bill_fee' => -0.5, 'synced_at' => now(), 'created_at' => now(), 'updated_at' => now(), ]); $repository = app(OrderProfitRepository::class); $result = $repository->paginate([ 'date_field' => 'order_time', 'from' => '2026-09-01', 'to' => '2026-09-30', 'order_no' => '26092142570188', 'shop_name' => '', 'sku_id' => '', ], 1, 25); $this->assertSame( '唯品会MP-卓禾家具特卖旗舰店|26092142570188', $result['rows'][0]['shop_name'] . '|' . $result['rows'][0]['order_key'], ); $this->assertSame(0.0, $result['rows'][0]['matched_package_fee']); $this->assertSame(10.5, $result['rows'][0]['matched_freight_fee']); $this->assertSame(-0.5, $result['rows'][0]['matched_bill_fee']); } }