From 2d4859f2913ea497d90f59cb4206f27842e08a5c Mon Sep 17 00:00:00 2001 From: limeng <1131177064@qq.com> Date: Fri, 27 Nov 2026 16:35:58 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=9D=E6=8A=A4=E6=9C=AA=E5=8F=91=E5=87=BA?= =?UTF-8?q?=E5=8E=86=E5=8F=B2=E8=A1=A5=E5=BD=95=E5=B9=B6=E8=A1=94=E6=8E=A5?= =?UTF-8?q?=E6=AF=8F=E6=97=A5=E5=90=8C=E6=AD=A5=20(#1521)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Console/Commands/SeedUnshippedHistory.php | 52 +++++++++++++++++++ .../Business/UnshippedWaybillController.php | 2 + app/Support/UnshippedWaybills.php | 13 ++++- deploy/run-finance-batch.sh | 2 +- .../unshipped-waybills.blade.php | 3 ++ .../Business/UnshippedWaybillsTest.php | 19 +++++++ 6 files changed, 88 insertions(+), 3 deletions(-) create mode 100644 app/Console/Commands/SeedUnshippedHistory.php diff --git a/app/Console/Commands/SeedUnshippedHistory.php b/app/Console/Commands/SeedUnshippedHistory.php new file mode 100644 index 00000000..fc6df265 --- /dev/null +++ b/app/Console/Commands/SeedUnshippedHistory.php @@ -0,0 +1,52 @@ +argument('cutoff'); + $validator = Validator::make(['data' => $data, 'cutoff' => $cutoff], [ + 'cutoff' => ['required', 'date_format:Y-m-d', 'after_or_equal:'.UnshippedWaybills::START, 'before:today'], + 'data' => ['required', 'array:intercept,replacement,warehouse', 'size:3'], + 'data.*' => ['required', 'array', 'max:10000'], + 'data.*.*' => ['required', 'string', 'regex:/^[a-zA-Z0-9-]{7,96}$/', 'not_regex:/^\d+(?:\.\d+)?e[+-]?\d+$/i'], + 'data.intercept.*' => ['max:15'], + ]); + if ($validator->fails()) { + $this->error('清单格式、快递单号或截止日期校验失败。'); + + return self::FAILURE; + } + + $marker = UnshippedWaybills::HISTORY_PREFIX.$cutoff; + foreach ($data as $source => $numbers) { + $data[$source] = array_values(array_unique($numbers)); + $this->info(UnshippedWaybills::label($source).':'.count($data[$source]).' 条。'); + } + if (!$this->option('apply')) return self::SUCCESS; + + $service->connection()->transaction(function () use ($service, $data, $marker, $cutoff): void { + foreach ($data as $source => $numbers) { + foreach (array_chunk($numbers, 500) as $chunk) { + $service->results()->upsert(array_map(fn ($waybill) => [ + 'source' => $source, 'waybill_no' => $waybill, 'evidence_no' => $marker, + 'evidence_time' => $cutoff.' 00:00:00', 'evidence_text' => '历史导出补录,文件仅含单号和来源;日期为覆盖截止日。', + ], $chunk), ['source', 'evidence_no', 'waybill_no'], ['evidence_text']); + } + + } + }); + + return self::SUCCESS; + } +} diff --git a/app/Http/Controllers/Business/UnshippedWaybillController.php b/app/Http/Controllers/Business/UnshippedWaybillController.php index ce25c480..ba433c03 100644 --- a/app/Http/Controllers/Business/UnshippedWaybillController.php +++ b/app/Http/Controllers/Business/UnshippedWaybillController.php @@ -27,6 +27,8 @@ final class UnshippedWaybillController extends Controller try { $data['rows'] = $this->filtered($request)->orderByDesc('evidence_time')->orderByDesc('id')->paginate(50)->withQueryString(); $db = $this->waybills->connection(); + $data['history'] = $this->waybills->results()->where('evidence_no', 'like', UnshippedWaybills::HISTORY_PREFIX.'%') + ->selectRaw('count(*) as row_count, max(evidence_time) as cutoff')->first(); $data['states'] = $db->table('courier_unshipped_sync')->get()->keyBy('source'); $data['coverage'] = $db->table('courier_unshipped_order_logs')->selectRaw('min(operated_at) as start_at,max(operated_at) as end_at')->first(); } catch (\Illuminate\Database\QueryException $error) { diff --git a/app/Support/UnshippedWaybills.php b/app/Support/UnshippedWaybills.php index 3ef59436..bef3d1ac 100644 --- a/app/Support/UnshippedWaybills.php +++ b/app/Support/UnshippedWaybills.php @@ -9,6 +9,7 @@ use Throwable; final class UnshippedWaybills { public const START = '2026-09-01'; + public const HISTORY_PREFIX = 'history:'; public const SOURCES = ['intercept' => '现场拦截入库签收', 'replacement' => '换单未发出', 'warehouse' => '仓内未发']; private const WAREHOUSE_WORK_TYPE = '78FC156B524645E886F21A32ED67133F'; @@ -94,8 +95,16 @@ final class UnshippedWaybills 'replacement' => $this->replacements(), 'warehouse' => $this->warehouse(), }; + $historySource = isset(self::INTERCEPT_TABLES[$source]) ? 'intercept' : $source; + $history = $this->results()->where('source', $historySource)->where('evidence_no', 'like', self::HISTORY_PREFIX.'%'); + $cutoff = (clone $history)->max('evidence_time'); + if ($cutoff !== null) { + $covered = array_fill_keys((clone $history)->pluck('waybill_no')->all(), true); + $rows = array_filter($rows, fn ($row) => substr($row['evidence_time'], 0, 10) > substr($cutoff, 0, 10) + && !isset($covered[$row['waybill_no']])); + } $db->transaction(function () use ($db, $source, $rows, $missing): void { - $this->results()->where('source', $source)->delete(); + $this->results()->where('source', $source)->where('evidence_no', 'not like', self::HISTORY_PREFIX.'%')->delete(); foreach (array_chunk(array_values($rows), 500) as $chunk) { $this->results()->insert($chunk); } @@ -104,7 +113,7 @@ final class UnshippedWaybills ]); if (isset(self::INTERCEPT_TABLES[$source]) && $db->table('courier_unshipped_sync') ->whereIn('source', array_keys(self::INTERCEPT_TABLES))->whereNotNull('last_success_at')->count() === 2) { - $this->results()->where('source', 'intercept')->delete(); + $this->results()->where('source', 'intercept')->where('evidence_no', 'not like', self::HISTORY_PREFIX.'%')->delete(); } }); diff --git a/deploy/run-finance-batch.sh b/deploy/run-finance-batch.sh index e4565ff0..88022cf9 100644 --- a/deploy/run-finance-batch.sh +++ b/deploy/run-finance-batch.sh @@ -9,7 +9,7 @@ CAIWU_BATCH_CALLER_DIR="$PWD" export CAIWU_BATCH_CALLER_DIR case "${1:-}" in *:import|*:import-*|*:import:*|payables:final-import|payables:unpriced-cycle|product:monthly-weighted-average:base|planting-details:sync-work-orders|jst-order-labels:sync-daily) is_import=1 ;; - jushuitan:calc-freight|courier:sync-unshipped-waybills) ;; + jushuitan:calc-freight|courier:sync-unshipped-waybills|courier:seed-unshipped-history) ;; jushuitan:sync-shipping|store-receipt:order-match:sync|store-receipt:planting-daily|store-receipt:product-cost:sync|store-receipt:auto-resync|order-data:warm-shop-candidates|business-mirror:sync-tables|business-mirror:sync-promotion-expenses|business-mirror:sync-ding-process-details|business-mirror:process-ding-refresh-requests|business-mirror:index-work-order-waybills|business-mirror:refresh-paid-sales|ding-invoices:sync|ding-invoices:process-refresh-requests|account-bills:exports:run|finance-ledger:recalculate|product:monthly-weighted-average:auto|product:monthly-weighted-average:compute|product:monthly-weighted-average:report|finance:monthly-weighted-product-cost:auto) ;; *) echo 'Unsupported finance batch command' >&2; exit 64 ;; esac diff --git a/resources/views/business/pages/courier-payables/unshipped-waybills.blade.php b/resources/views/business/pages/courier-payables/unshipped-waybills.blade.php index f75e99e1..0cc83c86 100644 --- a/resources/views/business/pages/courier-payables/unshipped-waybills.blade.php +++ b/resources/views/business/pages/courier-payables/unshipped-waybills.blade.php @@ -7,6 +7,9 @@ @if($unavailable) @else + @if($history->row_count ?? 0) +

历史明细已补至 {{ substr($history->cutoff, 0, 10) }},共 {{ number_format($history->row_count) }} 行。

+ @endif
@foreach($syncSources as $key => $label) @php($state = $states->get($key)) diff --git a/tests/Feature/Business/UnshippedWaybillsTest.php b/tests/Feature/Business/UnshippedWaybillsTest.php index ec141a15..37c0455e 100644 --- a/tests/Feature/Business/UnshippedWaybillsTest.php +++ b/tests/Feature/Business/UnshippedWaybillsTest.php @@ -126,6 +126,25 @@ final class UnshippedWaybillsTest extends FeatureTestCase $this->assertSame('GD1', $service->results()->sole()->evidence_no); } + public function test_daily_sync_keeps_seeded_history_and_adds_only_new_days(): void + { + $this->travelTo(now()->setDate(2026, 11, 29)); + $service = app(UnshippedWaybills::class); + $service->results()->insert([ + 'source' => 'replacement', 'waybill_no' => 'HISTORY1', + 'evidence_no' => 'history:2026-11-26', 'evidence_time' => '2026-11-26 00:00:00', + ]); + foreach (['2026-11-26' => 'OLD26', '2026-11-27' => 'NEW27'] as $date => $old) { + $remark = '德邦:'.$old.'->圆通:FINAL'.$old; + $service->connection()->table('courier_unshipped_order_logs')->insert(UnshippedWaybills::parseRemark($remark) + [ + 'order_no' => $old, 'operated_at' => $date.' 10:00:00', 'remark' => $remark, + ]); + } + + $this->assertTrue($service->sync('replacement')); + $this->assertSame(['HISTORY1', 'NEW27'], $service->results()->orderBy('waybill_no')->pluck('waybill_no')->all()); + } + private function mirrorRows(string $table, array $fields, array $rows): void { $connection = app(UnshippedWaybills::class)->connection();