53 lines
2.4 KiB
PHP
53 lines
2.4 KiB
PHP
<?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;
|
||
}
|
||
}
|