CaiWuXiTong/app/Console/Commands/ImportCourierSummaryTemplates.php

45 lines
2.1 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Console\Commands;
use App\Support\CourierBills\CourierBillStore;
use Illuminate\Console\Command;
/**
* 导入手工小汇总类目(JSON:[{"group":"分组名","title":"块标题","labels":["运费10月",…]}]),当作某月的小汇总类目模板。
* 页面上「导入手工小汇总」读的是发送版账单本身;这个命令给已经整理好的类目清单用(如 10 月首批)。
*/
final class ImportCourierSummaryTemplates extends Command
{
protected $signature = 'courier-bills:import-summary-templates {file : JSON 文件} {--month= : 这些小汇总是哪个月的账单,如 2026-10}';
protected $description = '导入手工小汇总类目,作为快递账单对账小汇总的类目模板';
public function handle(CourierBillStore $store): int
{
$month = (string) $this->option('month');
if (! preg_match('/^20\d{2}-(0[1-9]|1[0-2])$/', $month)) {
$this->error('--month 要写成 2026-10 这样');
return self::FAILURE;
}
$blocks = json_decode((string) @file_get_contents((string) $this->argument('file')), true);
if (! is_array($blocks)) {
$this->error('读不了 JSON 文件');
return self::FAILURE;
}
$blocks = array_values(array_filter(array_map(static fn ($b): ?array => is_array($b) && isset($b['title'], $b['labels']) && is_array($b['labels'])
? ['title' => (string) $b['title'], 'labels' => array_map('strval', $b['labels']), 'group' => isset($b['group']) ? (string) $b['group'] : null,
'source' => isset($b['source']) ? (string) $b['source'] : null]
: null, $blocks)));
$result = $store->saveSummaryTemplates($blocks, $month);
$this->info('读到 ' . $result['blocks'] . ' 块,记下 ' . count($result['saved']) . ' 家 ' . $month . ' 的类目:' . implode('、', $result['saved']));
if ($result['unknown'] !== []) {
$this->warn('没找到分组:' . implode('、', $result['unknown']));
}
return self::SUCCESS;
}
}