dropIfExists($table); $schema->create(WorkOrderWaybillLookup::SOURCE, function (Blueprint $table): void { $table->char('_mirror_key', 64)->primary(); $table->unsignedBigInteger('Id'); $table->string('title'); $table->text('businessFieldList'); }); config(['system_database.mode' => 'shared']); (require base_path('database/migrations/2026_10_19_170000_create_work_order_waybill_lookup.php'))->up(); return app(WorkOrderWaybillLookup::class); } private function row(string $key, int $id, string $waybill, string $title = '查件'): array { return ['_mirror_key' => str_pad($key, 64, '0'), 'Id' => $id, 'title' => $title, 'businessFieldList' => json_encode([['fieldId' => 'lIdField', 'value' => $waybill]])]; } public function test_backfill_resumes_and_does_not_publish_an_incomplete_index(): void { $lookup = $this->setupLookup(); $db = DB::connection('business_mirror'); $db->table(WorkOrderWaybillLookup::SOURCE)->insert([ $this->row('a', 1, '001'), $this->row('b', 2, '001'), $this->row('c', 3, '003', '其它'), ]); $this->assertFalse($lookup->backfill(2, 1, 0)['complete']); $this->assertFalse($lookup->ready()); $this->assertTrue($lookup->backfill(2, 1, 0)['complete']); $this->assertTrue($lookup->ready()); $this->assertSame(2, $db->table(WorkOrderWaybillLookup::TABLE)->where('waybill', '001')->count()); $this->assertSame(0, $lookup->backfill(2, 1, 0)['rows']); } public function test_updates_remove_old_keys_and_preserve_last_field_wins_and_multiple_orders(): void { $lookup = $this->setupLookup(); $db = DB::connection('business_mirror'); $a = $this->row('a', 1, '001'); $b = $this->row('b', 2, '001'); $lookup->storeBatch([$a, $b]); $a['businessFieldList'] = json_encode([ ['fieldId' => 'lIdField', 'value' => 'wrong'], ['fieldId' => 'lIdField', 'value' => ' 002 '], ]); $lookup->storeBatch([$a]); $this->assertSame(1, $db->table(WorkOrderWaybillLookup::TABLE)->where('waybill', '001')->count()); $this->assertSame(1, $db->table(WorkOrderWaybillLookup::TABLE)->where('waybill', '002')->count()); $b['title'] = '其它'; $lookup->storeBatch([$b]); $this->assertSame(0, $db->table(WorkOrderWaybillLookup::TABLE)->where('waybill', '001')->count()); $a['businessFieldList'] = '{invalid'; $lookup->storeBatch([$a]); $this->assertSame(0, $db->table(WorkOrderWaybillLookup::TABLE)->count()); } public function test_full_source_pruning_removes_deleted_records(): void { $lookup = $this->setupLookup(); $db = DB::connection('business_mirror'); $a = $this->row('a', 1, '001'); $b = $this->row('b', 2, '002'); $db->table(WorkOrderWaybillLookup::SOURCE)->insert([$a]); $lookup->storeBatch([$a, $b]); $lookup->prune(); $this->assertSame(['001'], $db->table(WorkOrderWaybillLookup::TABLE)->pluck('waybill')->all()); } }