保护未发出历史补录并衔接每日同步 (#1521)
This commit is contained in:
parent
c7f011fc93
commit
2d4859f291
52
app/Console/Commands/SeedUnshippedHistory.php
Normal file
52
app/Console/Commands/SeedUnshippedHistory.php
Normal file
@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Support\UnshippedWaybills;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
|
||||
final class SeedUnshippedHistory extends Command
|
||||
{
|
||||
protected $signature = 'courier:seed-unshipped-history {cutoff : 历史明细截止日期} {--apply : 确认写入共享镜像}';
|
||||
protected $description = '从标准输入读取核对后的分来源单号清单,补录历史未发出明细';
|
||||
|
||||
public function handle(UnshippedWaybills $service): int
|
||||
{
|
||||
$data = json_decode(stream_get_contents(STDIN, 1048577), true);
|
||||
$cutoff = $this->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;
|
||||
}
|
||||
}
|
||||
@ -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) {
|
||||
|
||||
@ -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();
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@ -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
|
||||
|
||||
@ -7,6 +7,9 @@
|
||||
@if($unavailable)
|
||||
<p role="alert" class="rounded bg-yellow-50 p-3 text-yellow-900">未发出明细暂不可用,请确认共享镜像结果表可读取。</p>
|
||||
@else
|
||||
@if($history->row_count ?? 0)
|
||||
<p class="mb-3">历史明细已补至 {{ substr($history->cutoff, 0, 10) }},共 {{ number_format($history->row_count) }} 行。</p>
|
||||
@endif
|
||||
<div class="grid gap-3 md:grid-cols-3">
|
||||
@foreach($syncSources as $key => $label)
|
||||
@php($state = $states->get($key))
|
||||
|
||||
@ -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();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user