82 lines
2.6 KiB
PHP
82 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Business\Piaoju\PiaojuSellerQuotaClient;
|
|
use App\Business\Piaoju\PiaojuSellerQuotaImporter;
|
|
use Illuminate\Console\Command;
|
|
use Throwable;
|
|
|
|
class SyncPiaojuSellerQuotas extends Command
|
|
{
|
|
protected $signature = 'piaoju:sync-invoice-quotas
|
|
{--file= : 票聚销方列表 JSON 文件;省略时调用票聚接口}
|
|
{--stdin : 从标准输入读取票聚销方列表 JSON}
|
|
{--dry-run : 只读取并校验票聚响应,不写共享镜像}';
|
|
|
|
protected $description = '同步票聚销方管理的最新可用发票额度到共享业务镜像';
|
|
|
|
public function handle(PiaojuSellerQuotaClient $client, PiaojuSellerQuotaImporter $importer): int
|
|
{
|
|
try {
|
|
$rows = $this->inputRows($client);
|
|
$values = $importer->values($rows);
|
|
|
|
if ((bool) $this->option('dry-run')) {
|
|
$this->info(sprintf('票聚销方额度读取成功:%d 条,未写入镜像。', count($values)));
|
|
|
|
return self::SUCCESS;
|
|
}
|
|
|
|
$written = $importer->replace($rows);
|
|
} catch (Throwable $exception) {
|
|
report($exception);
|
|
$this->error('票聚销方额度同步失败,已保留上一版镜像。');
|
|
|
|
return self::FAILURE;
|
|
}
|
|
|
|
$this->info(sprintf('票聚销方额度同步完成:%d 条。', $written));
|
|
|
|
return self::SUCCESS;
|
|
}
|
|
|
|
/**
|
|
* @return list<array<string,mixed>>
|
|
*/
|
|
private function inputRows(PiaojuSellerQuotaClient $client): array
|
|
{
|
|
$fromStdin = (bool) $this->option('stdin');
|
|
$path = trim((string) $this->option('file'));
|
|
if (! $fromStdin && $path === '') {
|
|
return $client->fetchAll();
|
|
}
|
|
|
|
$raw = $fromStdin ? stream_get_contents(STDIN) : file_get_contents($path);
|
|
if (! is_string($raw) || trim($raw) === '') {
|
|
throw new \RuntimeException('piaoju_input_empty');
|
|
}
|
|
|
|
$decoded = json_decode($raw, true, 512, JSON_THROW_ON_ERROR);
|
|
$rows = null;
|
|
if (is_array($decoded)) {
|
|
if (array_is_list($decoded)) {
|
|
$rows = $decoded;
|
|
} else {
|
|
$data = $decoded['data'] ?? null;
|
|
if (is_array($data)) {
|
|
$rows = array_is_list($data)
|
|
? $data
|
|
: ($data['list'] ?? $data['records'] ?? null);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (! is_array($rows)) {
|
|
throw new \RuntimeException('piaoju_input_invalid');
|
|
}
|
|
|
|
return array_values(array_filter($rows, 'is_array'));
|
|
}
|
|
}
|