diff --git a/modules/EcommerceFinance/Repositories/DingOnlineDaifaPriceMirrorRepository.php b/modules/EcommerceFinance/Repositories/DingOnlineDaifaPriceMirrorRepository.php index 435c3ac3..3e92c34e 100644 --- a/modules/EcommerceFinance/Repositories/DingOnlineDaifaPriceMirrorRepository.php +++ b/modules/EcommerceFinance/Repositories/DingOnlineDaifaPriceMirrorRepository.php @@ -38,6 +38,9 @@ class DingOnlineDaifaPriceMirrorRepository private const EXPORT_HYDRATE_BATCH = 200; + /** 核查索引一次关联聚水潭订单的行数(与订单查询分块一致,减少往返次数)。 */ + private const AUDIT_HYDRATE_BATCH = 500; + /** 报销成本核查结果:代发订单金额 vs Σ(实际发货子商品成本价 × 数量)。 */ public const AUDIT_OVER = '多报销'; @@ -74,7 +77,19 @@ class DingOnlineDaifaPriceMirrorRepository ); } - /** @return array{data:list>,total:int,status:string,last_synced_at:?string,summary:array,source_statuses:array>} */ + /** + * 页面分页。 + * + * 不再把两张来源表整表 UNION 成派生表再数数、汇总、排序(每次打开都要把两边全量 + * 物化三遍,连同审批 JSON 一起落盘排序,数据一多就超时)。改为: + * - 筛选条件分别下推到钉钉、采购两个来源; + * - 总数、合计按来源各算一次再相加; + * - 两边各按同一排序只取前 (页码×每页) 条主键,合并后取当前页; + * - 只给当前页的行回表取完整字段(含审批 JSON)。 + * 结果与原先整表合并后的排序、筛选、合计一致。 + * + * @return array{data:list>,total:int,status:string,last_synced_at:?string,summary:array,source_statuses:array>} + */ public function paginate(array $filters, int $page, int $perPage): array { if (! $this->ready()) { @@ -83,7 +98,10 @@ class DingOnlineDaifaPriceMirrorRepository $page = max($page, 1); $perPage = min(max($perPage, 1), 100); - $total = (int) $this->filteredQuery($filters)->count(); + $sources = $this->sourceStatuses(); + $ding = (bool) ($sources['ding']['ready'] ?? false) ? $this->dingFiltered($filters) : null; + $purchase = (bool) ($sources['purchase']['ready'] ?? false) ? $this->purchaseFiltered($filters) : null; + $summary = [ 'count' => 0, 'matched_count' => 0, @@ -93,40 +111,33 @@ class DingOnlineDaifaPriceMirrorRepository 'paid_amount' => 0.0, 'price_diff' => 0.0, ]; - $pageRows = []; - if ($total > 0) { - $aggregate = $this->filteredQuery($filters) + if ($ding !== null) { + $aggregate = (clone $ding) ->selectRaw( 'COUNT(*) AS row_count, ' - . "SUM(CASE WHEN source_type = 'purchase' THEN 1 ELSE 0 END) AS purchase_count, " - . "SUM(CASE WHEN source_type = 'ding' THEN 1 ELSE 0 END) AS matchable_count, " - . "SUM(CASE WHEN source_type = 'ding' AND product_id IS NOT NULL AND product_id <> '' " - . "AND product_name IS NOT NULL AND product_name <> '' THEN 1 ELSE 0 END) AS matched_count, " - . "COALESCE(SUM(CASE WHEN source_type = 'ding' THEN daifa_amount ELSE 0 END), 0) AS daifa_amount, " - . "COALESCE(SUM(CASE WHEN source_type = 'ding' THEN paid_amount ELSE 0 END), 0) AS paid_amount, " - . "COALESCE(SUM(CASE WHEN source_type = 'ding' THEN price_diff ELSE 0 END), 0) AS price_diff" + . "SUM(CASE WHEN d.product_id IS NOT NULL AND d.product_id <> '' " + . "AND d.product_name IS NOT NULL AND d.product_name <> '' THEN 1 ELSE 0 END) AS matched_count, " + . 'COALESCE(SUM(d.daifa_amount), 0) AS daifa_amount, ' + . 'COALESCE(SUM(d.paid_amount), 0) AS paid_amount, ' + . 'COALESCE(SUM(d.price_diff), 0) AS price_diff' ) ->first(); + $summary['matchable_count'] = (int) ($aggregate->row_count ?? 0); + $summary['matched_count'] = (int) ($aggregate->matched_count ?? 0); + $summary['daifa_amount'] = round((float) ($aggregate->daifa_amount ?? 0), 2); + $summary['paid_amount'] = round((float) ($aggregate->paid_amount ?? 0), 2); + $summary['price_diff'] = round((float) ($aggregate->price_diff ?? 0), 2); + } + if ($purchase !== null) { + $summary['purchase_count'] = (int) (clone $purchase)->count(); + } + $total = $summary['matchable_count'] + $summary['purchase_count']; + $summary['count'] = $total; - $summary = [ - 'count' => (int) ($aggregate->row_count ?? 0), - 'matched_count' => (int) ($aggregate->matched_count ?? 0), - 'matchable_count' => (int) ($aggregate->matchable_count ?? 0), - 'purchase_count' => (int) ($aggregate->purchase_count ?? 0), - 'daifa_amount' => round((float) ($aggregate->daifa_amount ?? 0), 2), - 'paid_amount' => round((float) ($aggregate->paid_amount ?? 0), 2), - 'price_diff' => round((float) ($aggregate->price_diff ?? 0), 2), - ]; - - if (($filters['audit'] ?? '') === '') { - $pageRows = $this->filteredQuery($filters) - ->orderByDesc('workflow_created_at') - ->orderByDesc('process_instance_id') - ->orderByDesc('detail_index') - ->forPage($page, $perPage) - ->get(); - } + $pageRows = []; + if ($total > 0 && ($filters['audit'] ?? '') === '') { + $pageRows = $this->pageRows($ding, $purchase, $page, $perPage); } $audit = $total > 0 ? $this->auditIndex($filters) : ['keys' => [], 'counts' => [], 'over_amount' => 0.0]; @@ -145,7 +156,6 @@ class DingOnlineDaifaPriceMirrorRepository $this->attachJstOrderDetails($data); $this->attachCostAudit($data); - $sources = $this->sourceStatuses(); $readySources = array_filter($sources, static fn (array $source): bool => (bool) ($source['ready'] ?? false)); $synced = array_values(array_filter(array_column($readySources, 'last_synced_at'))); @@ -161,6 +171,195 @@ class DingOnlineDaifaPriceMirrorRepository ]; } + /** + * 两个来源各取前 N 条主键(只带排序列,排序很轻),合并排序后截出当前页, + * 再按主键回表取完整行。排序口径与原 UNION 一致:创建时间、单号(按文本)、明细序号均倒序。 + * + * @return list + */ + private function pageRows(?Builder $ding, ?Builder $purchase, int $page, int $perPage): array + { + $need = $page * $perPage; + $branches = []; + + if ($ding !== null) { + $branches[] = (clone $ding) + ->select([ + DB::raw("'ding' AS source_type"), + 'd.process_instance_id', + 'd.detail_index', + 'd.workflow_created_at', + ]) + ->orderByDesc('d.workflow_created_at') + ->orderByDesc('d.process_instance_id') + ->orderByDesc('d.detail_index') + ->limit($need); + } + if ($purchase !== null) { + $created = 'COALESCE(p.document_created_at, p.purchase_updated_at)'; + $branches[] = (clone $purchase) + ->select([ + DB::raw("'purchase' AS source_type"), + DB::raw('CAST(p.purchase_order_id AS CHAR) AS process_instance_id'), + 'p.item_id as detail_index', + DB::raw($created . ' AS workflow_created_at'), + ]) + ->orderByRaw($created . ' DESC') + ->orderByRaw('CAST(p.purchase_order_id AS CHAR) DESC') + ->orderByDesc('p.item_id') + ->limit($need); + } + if ($branches === []) { + return []; + } + + $union = array_shift($branches); + foreach ($branches as $branch) { + $union->unionAll($branch); + } + + $keys = $this->connection->query() + ->fromSub($union, 'online_daifa_keys') + ->orderByDesc('workflow_created_at') + ->orderByDesc('process_instance_id') + ->orderByDesc('detail_index') + ->offset(($page - 1) * $perPage) + ->limit($perPage) + ->get(); + + $dingIds = []; + $itemIds = []; + foreach ($keys as $key) { + if ((string) $key->source_type === 'purchase') { + $itemIds[(int) $key->detail_index] = true; + } else { + $dingIds[(string) $key->process_instance_id] = true; + } + } + + $full = []; + if ($dingIds !== []) { + foreach ($this->dingQuery()->whereIn('d.process_instance_id', array_keys($dingIds))->get() as $row) { + $full['ding|' . $row->process_instance_id . '|' . (int) $row->detail_index] = $row; + } + } + if ($itemIds !== []) { + foreach ($this->purchaseQuery()->whereIn('p.item_id', array_keys($itemIds))->get() as $row) { + $full['purchase|' . (int) $row->detail_index] = $row; + } + } + + $rows = []; + foreach ($keys as $key) { + $lookup = (string) $key->source_type === 'purchase' + ? 'purchase|' . (int) $key->detail_index + : 'ding|' . $key->process_instance_id . '|' . (int) $key->detail_index; + if (isset($full[$lookup])) { + $rows[] = $full[$lookup]; + } + } + + return $rows; + } + + /** 钉钉来源加上页面筛选;该来源被筛选条件整体排除时返回 null。 */ + private function dingFiltered(array $filters): ?Builder + { + if (($filters['status'] ?? '') !== '' && strtoupper((string) $filters['status']) !== $this->dingStatusKey()) { + return null; + } + + $query = $this->connection->table(self::TARGET_TABLE . ' as d'); + if (($filters['product_id'] ?? '') !== '') { + $query->where('d.product_id', (string) $filters['product_id']); + } + if (($filters['shop_name'] ?? '') !== '') { + $query->where('d.shop_name', (string) $filters['shop_name']); + } + if (($filters['supplier_name'] ?? '') !== '') { + $query->whereRaw("d.supplier_name LIKE ? ESCAPE '!'", [$this->contains((string) $filters['supplier_name'])]); + } + if (($filters['material_no'] ?? '') !== '') { + $query->where('d.material_no', (string) $filters['material_no']); + } + if (($filters['keyword'] ?? '') !== '') { + $like = '%' . (string) $filters['keyword'] . '%'; + $query->where(function ($q) use ($like): void { + $q->where('d.process_instance_id', 'like', $like) + ->orWhere('d.daifa_trade_no', 'like', $like) + ->orWhere('d.shop_trade_no', 'like', $like); + }); + } + if (($filters['created_from'] ?? '') !== '') { + $query->where('d.workflow_created_at', '>=', (string) $filters['created_from']); + } + if (($filters['created_to'] ?? '') !== '') { + $query->where('d.workflow_created_at', '<', $this->createdToBound((string) $filters['created_to'])); + } + + return $query; + } + + /** 采购单来源加上页面筛选;该来源被筛选条件整体排除时返回 null。 */ + private function purchaseFiltered(array $filters): ?Builder + { + // 采购单行没有商品ID,按商品ID筛选时不会有采购单行。 + if (($filters['product_id'] ?? '') !== '') { + return null; + } + + $query = $this->connection->table('product_purchase_price_mirror_rows as p') + ->whereIn('p.document_status', ['Confirmed', 'Finished']); + // 只有按店铺/关键字筛选时才需要关联订单表。 + if (($filters['shop_name'] ?? '') !== '' || ($filters['keyword'] ?? '') !== '') { + $query->leftJoin('ops_jst_orders as o', 'o.o_id', '=', 'p.source_order_no'); + } + if (($filters['shop_name'] ?? '') !== '') { + $query->where('o.shop_name', (string) $filters['shop_name']); + } + if (($filters['supplier_name'] ?? '') !== '') { + $query->whereRaw("p.supplier_name LIKE ? ESCAPE '!'", [$this->contains((string) $filters['supplier_name'])]); + } + if (($filters['material_no'] ?? '') !== '') { + $query->where('p.sku_id', (string) $filters['material_no']); + } + if (($filters['keyword'] ?? '') !== '') { + $like = '%' . (string) $filters['keyword'] . '%'; + $query->where(function ($q) use ($like): void { + $q->where('p.purchase_order_id', 'like', $like) + ->orWhere('o.so_id', 'like', $like) + ->orWhere('p.source_order_no', 'like', $like); + }); + } + if (($filters['status'] ?? '') !== '') { + $query->whereRaw('UPPER(p.document_status) = ?', [strtoupper((string) $filters['status'])]); + } + if (($filters['created_from'] ?? '') !== '') { + $query->whereRaw('COALESCE(p.document_created_at, p.purchase_updated_at) >= ?', [(string) $filters['created_from']]); + } + if (($filters['created_to'] ?? '') !== '') { + $query->whereRaw( + 'COALESCE(p.document_created_at, p.purchase_updated_at) < ?', + [$this->createdToBound((string) $filters['created_to'])], + ); + } + + return $query; + } + + private function createdToBound(string $date): string + { + return Carbon::parse($date)->addDay()->startOfDay()->format('Y-m-d H:i:s'); + } + + private function dingStatusKey(): string + { + return strtoupper(trim((string) config( + 'upstream_mirrors.ding_online_daifa_price.only_status', + 'COMPLETED', + ))) ?: 'COMPLETED'; + } + /** @return \Generator> */ public function exportRows(array $filters): \Generator { @@ -491,7 +690,7 @@ class DingOnlineDaifaPriceMirrorRepository $query->where( 'workflow_created_at', '<', - Carbon::parse((string) $filters['created_to'])->addDay()->startOfDay()->format('Y-m-d H:i:s'), + $this->createdToBound((string) $filters['created_to']), ); } @@ -524,10 +723,7 @@ class DingOnlineDaifaPriceMirrorRepository private function dingQuery(): Builder { - $status = strtoupper(trim((string) config( - 'upstream_mirrors.ding_online_daifa_price.only_status', - 'COMPLETED', - ))) ?: 'COMPLETED'; + $status = $this->dingStatusKey(); return $this->connection->table(self::TARGET_TABLE . ' as d')->select([ DB::raw("'ding' AS source_type"), @@ -785,12 +981,13 @@ class DingOnlineDaifaPriceMirrorRepository } $buffer = []; }; - $rows = $this->filteredQuery($base) - ->where('source_type', 'ding') - ->orderByDesc('workflow_created_at') - ->orderByDesc('process_instance_id') - ->orderByDesc('detail_index') - ->get(['source_type', 'process_instance_id', 'detail_index', 'shop_trade_no', 'material_no', 'daifa_amount']); + // 只核查钉钉行:直接查钉钉镜像,不再经过与采购单整表合并的派生表。 + $ding = (bool) ($this->sourceStatuses()['ding']['ready'] ?? false) ? $this->dingFiltered($base) : null; + $rows = $ding === null ? [] : $ding + ->orderByDesc('d.workflow_created_at') + ->orderByDesc('d.process_instance_id') + ->orderByDesc('d.detail_index') + ->get(['d.process_instance_id', 'd.detail_index', 'd.shop_trade_no', 'd.material_no', 'd.daifa_amount']); foreach ($rows as $row) { $buffer[] = [ 'source_type' => 'ding', @@ -800,7 +997,7 @@ class DingOnlineDaifaPriceMirrorRepository 'material_no' => (string) ($row->material_no ?? ''), 'daifa_amount' => $row->daifa_amount === null ? null : (float) $row->daifa_amount, ]; - if (count($buffer) >= self::EXPORT_HYDRATE_BATCH) { + if (count($buffer) >= self::AUDIT_HYDRATE_BATCH) { $flush(); } } @@ -827,19 +1024,14 @@ class DingOnlineDaifaPriceMirrorRepository if ($keys === []) { return []; } - $base = $filters; - unset($base['audit']); - $pairs = array_map(static fn (string $key): array => explode('|', $key, 2), $keys); - $rows = $this->filteredQuery($base) - ->where('source_type', 'ding') - ->where(function ($query) use ($pairs): void { - foreach ($pairs as [$id, $index]) { - $query->orWhere(function ($pair) use ($id, $index): void { - $pair->where('process_instance_id', $id)->where('detail_index', (int) $index); - }); - } - }) - ->get(); + // 键来自当前筛选条件下的核查索引,按流程ID直接回钉钉镜像取完整行即可。 + $ids = array_values(array_unique(array_map(static fn (string $key): string => explode('|', $key, 2)[0], $keys))); + $rows = []; + foreach (array_chunk($ids, self::JST_LOOKUP_CHUNK) as $chunk) { + foreach ($this->dingQuery()->whereIn('d.process_instance_id', $chunk)->get() as $row) { + $rows[] = $row; + } + } $byKey = []; foreach ($rows as $row) { $byKey[$row->process_instance_id . '|' . $row->detail_index] = $row; diff --git a/tests/Unit/EcommerceFinance/OnlineDaifaPricePaginationTest.php b/tests/Unit/EcommerceFinance/OnlineDaifaPricePaginationTest.php new file mode 100644 index 00000000..778e8d87 --- /dev/null +++ b/tests/Unit/EcommerceFinance/OnlineDaifaPricePaginationTest.php @@ -0,0 +1,234 @@ + ['driver' => 'sqlite', 'database' => ':memory:', 'prefix' => ''], + 'upstream_mirrors.connection' => 'business_mirror', + ]); + DB::purge('business_mirror'); + + $this->createSchema(); + $this->seedRows(); + } + + /** @return iterable}> */ + public static function filterProvider(): iterable + { + yield 'no filter' => [[]]; + yield 'date range' => [['created_from' => '2026-10-03', 'created_to' => '2026-10-06']]; + yield 'supplier' => [['supplier_name' => '供应商A']]; + yield 'shop' => [['shop_name' => '店铺甲']]; + yield 'keyword' => [['keyword' => '77']]; + yield 'material' => [['material_no' => 'SKU-2']]; + yield 'ding status only' => [['status' => 'COMPLETED']]; + yield 'purchase status only' => [['status' => 'FINISHED']]; + yield 'product id' => [['product_id' => 'PID-1']]; + } + + /** @dataProvider filterProvider */ + public function test_pages_match_the_fully_merged_order_totals_and_summary(array $filters): void + { + $repository = app(DingOnlineDaifaPriceMirrorRepository::class); + $expected = array_map( + static fn (array $row): string => $row['source_type'] . ':' . $row['process_instance_id'] . ':' . $row['detail_index'], + iterator_to_array($repository->exportRows($filters), false), + ); + + $perPage = 4; + $first = $repository->paginate($filters, 1, $perPage); + $this->assertSame(count($expected), $first['total']); + + $actual = []; + $pages = max((int) ceil($first['total'] / $perPage), 1) + 1; // one empty page past the end + for ($page = 1; $page <= $pages; $page++) { + foreach ($repository->paginate($filters, $page, $perPage)['data'] as $row) { + $actual[] = $row['source_type'] . ':' . $row['process_instance_id'] . ':' . $row['detail_index']; + } + } + $this->assertSame($expected, $actual); + + $exported = iterator_to_array($repository->exportRows($filters), false); + $ding = array_values(array_filter($exported, static fn (array $r): bool => $r['source_type'] === 'ding')); + $summary = $first['summary']; + $this->assertSame(count($exported), $summary['count']); + $this->assertSame(count($exported) - count($ding), $summary['purchase_count']); + $this->assertSame(count($ding), $summary['matchable_count']); + $this->assertSame( + count(array_filter($ding, static fn (array $r): bool => ($r['matched_product']['matched'] ?? false) === true)), + $summary['matched_count'], + ); + $this->assertSame(round(array_sum(array_column($ding, 'daifa_amount')), 2), $summary['daifa_amount']); + $this->assertSame(round(array_sum(array_column($ding, 'paid_amount')), 2), $summary['paid_amount']); + $this->assertSame(round(array_sum(array_column($ding, 'price_diff')), 2), $summary['price_diff']); + } + + public function test_page_rows_keep_their_full_fields(): void + { + $data = app(DingOnlineDaifaPriceMirrorRepository::class)->paginate([], 1, 100)['data']; + + $ding = collect($data)->first(fn (array $r): bool => $r['source_type'] === 'ding' && $r['process_instance_id'] === 'D1' && $r['detail_index'] === 2); + $this->assertSame('PID-1', $ding['product_id']); + $this->assertSame('供应商A', $ding['supplier_name']); + $this->assertSame(['status' => 'COMPLETED', 'result' => 'agree'], $ding['workflow']); + + $purchase = collect($data)->first(fn (array $r): bool => $r['source_type'] === 'purchase' && $r['detail_index'] === 110); + $this->assertSame(9000, $purchase['purchase_order_id']); + $this->assertSame('店铺甲', $purchase['shop_name']); + $this->assertSame('77001', $purchase['shop_trade_no']); + $this->assertSame('已完成', $purchase['status_label']); + } + + private function createSchema(): void + { + $schema = Schema::connection('business_mirror'); + $schema->create('business_mirror_table_states', function (Blueprint $table): void { + $table->string('dataset_key')->primary(); + $table->string('status'); + $table->dateTime('initial_completed_at')->nullable(); + $table->dateTime('last_succeeded_at')->nullable(); + }); + $schema->create(DingOnlineDaifaPriceMirrorRepository::TARGET_TABLE, function (Blueprint $table): void { + $table->string('process_instance_id'); + $table->unsignedInteger('detail_index'); + $table->string('product_id')->nullable(); + $table->string('product_name')->nullable(); + $table->string('daifa_trade_no')->nullable(); + $table->string('shop_trade_no')->nullable(); + $table->string('shop_name')->nullable(); + $table->string('payee_name')->nullable(); + $table->string('supplier_name')->nullable(); + $table->string('material_no')->nullable(); + $table->integer('ship_qty')->nullable(); + $table->decimal('paid_amount', 20, 2)->nullable(); + $table->decimal('daifa_amount', 20, 2)->nullable(); + $table->decimal('price_diff', 20, 2)->nullable(); + $table->text('workflow_payload')->nullable(); + $table->text('scheme_payload')->nullable(); + $table->text('task_payload')->nullable(); + $table->string('workflow_created_at', 32)->nullable(); + }); + $schema->create('product_purchase_price_sync_states', function (Blueprint $table): void { + $table->string('dataset_key')->primary(); + $table->string('status'); + $table->unsignedTinyInteger('projection_version')->default(1); + $table->dateTime('initial_completed_at')->nullable(); + $table->dateTime('last_succeeded_at')->nullable(); + }); + $schema->create('product_purchase_price_mirror_rows', function (Blueprint $table): void { + $table->unsignedBigInteger('item_id')->primary(); + $table->unsignedBigInteger('purchase_order_id'); + $table->unsignedBigInteger('source_order_no')->nullable(); + $table->string('supplier_name')->nullable(); + $table->string('sku_id')->nullable(); + $table->string('style_id')->nullable(); + $table->decimal('quantity', 20, 4)->nullable(); + $table->decimal('unit_price', 20, 4)->nullable(); + $table->string('document_status')->nullable(); + $table->text('labels')->nullable(); + $table->dateTime('document_created_at')->nullable(); + $table->dateTime('finished_at')->nullable(); + $table->dateTime('purchase_updated_at'); + }); + $schema->create('ops_jst_orders', function (Blueprint $table): void { + $table->unsignedBigInteger('Id')->primary(); + $table->unsignedBigInteger('o_id')->index(); + $table->string('so_id')->nullable()->index(); + $table->string('shop_name')->nullable(); + }); + } + + private function seedRows(): void + { + $db = DB::connection('business_mirror'); + $db->table('business_mirror_table_states')->insert([ + [ + 'dataset_key' => DingOnlineDaifaPriceMirrorRepository::DATASET_KEY, + 'status' => 'succeeded', + 'initial_completed_at' => '2026-10-01 00:00:00', + 'last_succeeded_at' => '2026-10-28 09:00:00', + ], + [ + 'dataset_key' => 'operations_replica.rpt.jst_Orders', + 'status' => 'succeeded', + 'initial_completed_at' => '2026-10-01 00:00:00', + 'last_succeeded_at' => '2026-10-28 09:00:00', + ], + ]); + $db->table('product_purchase_price_sync_states')->insert([ + 'dataset_key' => ProductPurchasePriceMirrorRepository::DATASET_KEY, + 'status' => 'succeeded', + 'projection_version' => ProductPurchasePriceMirrorRepository::CURRENT_PROJECTION_VERSION, + 'initial_completed_at' => '2026-10-01 00:00:00', + 'last_succeeded_at' => '2026-10-28 10:00:00', + ]); + + $ding = []; + // 11 张审批、每张 1~3 条明细;部分创建时间与采购单完全相同,用来检验同一时间的先后。 + for ($i = 1; $i <= 11; $i++) { + for ($d = 1; $d <= ($i % 3) + 1; $d++) { + $ding[] = [ + 'process_instance_id' => 'D' . $i, + 'detail_index' => $d, + 'product_id' => $i % 2 === 0 ? '' : 'PID-' . ($i % 4), + 'product_name' => $i % 5 === 0 ? null : '商品' . $i, + 'daifa_trade_no' => 'DF' . (770 + $i), + 'shop_trade_no' => 'SO' . (100 + $i), + 'shop_name' => $i % 2 === 0 ? '店铺甲' : '店铺乙', + 'payee_name' => '收款人', + 'supplier_name' => $i % 3 === 0 ? '供应商B' : '供应商A', + 'material_no' => 'SKU-' . ($d + $i % 2), + 'ship_qty' => 1, + 'paid_amount' => 10 + $i + $d / 10, + 'daifa_amount' => 8 + $i, + 'price_diff' => 2 + $d / 10, + 'workflow_payload' => '{"status":"COMPLETED","result":"agree"}', + 'scheme_payload' => '[]', + 'task_payload' => '[]', + 'workflow_created_at' => sprintf('2026-10-%02d 10:00:00', ($i % 7) + 1), + ]; + } + } + $db->table(DingOnlineDaifaPriceMirrorRepository::TARGET_TABLE)->insert($ding); + + $db->table('ops_jst_orders')->insert([ + ['Id' => 1, 'o_id' => 5001, 'so_id' => '77001', 'shop_name' => '店铺甲'], + ['Id' => 2, 'o_id' => 5002, 'so_id' => '88002', 'shop_name' => '店铺乙'], + ]); + $purchase = []; + $statuses = ['Confirmed', 'Finished', 'Cancelled']; + for ($i = 1; $i <= 14; $i++) { + $purchase[] = [ + 'item_id' => 100 + $i, + // 9001 与 10001 一起检验单号按文本排序(与原合并口径一致) + 'purchase_order_id' => $i % 4 === 0 ? 10000 + $i : 9000 + ($i % 5), + 'source_order_no' => $i % 3 === 0 ? null : 5000 + ($i % 2) + 1, + 'supplier_name' => $i % 2 === 0 ? '供应商A' : '供应商C', + 'sku_id' => 'SKU-' . ($i % 3), + 'quantity' => 1, + 'unit_price' => 5 + $i, + 'document_status' => $statuses[$i % 3], + 'document_created_at' => $i % 6 === 0 ? null : sprintf('2026-10-%02d 10:00:00', ($i % 7) + 1), + 'purchase_updated_at' => sprintf('2026-10-%02d 09:00:00', ($i % 8) + 1), + ]; + } + $db->table('product_purchase_price_mirror_rows')->insert($purchase); + } +}