CaiWuXiTong/app/Console/Commands/ArchiveStoreInformationDaily.php

51 lines
1.6 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\Business\StoreInformationDailyArchive;
use Carbon\CarbonImmutable;
use Illuminate\Console\Command;
use Throwable;
class ArchiveStoreInformationDaily extends Command
{
protected $signature = 'business-mirror:archive-store-information
{--date= : 快照日期,格式 YYYY-MM-DD;默认今天}';
protected $description = '每日保存一份店铺基础信息完整快照,便于后续复查';
public function handle(StoreInformationDailyArchive $archive): int
{
$date = trim((string) ($this->option('date') ?: now('Asia/Shanghai')->format('Y-m-d')));
if (! preg_match('/^\d{4}-\d{2}-\d{2}$/D', $date)) {
$this->error('快照日期格式应为 YYYY-MM-DD。');
return self::FAILURE;
}
try {
$snapshotDate = CarbonImmutable::createFromFormat('!Y-m-d', $date, 'Asia/Shanghai');
} catch (Throwable) {
$snapshotDate = null;
}
if ($snapshotDate === null || $snapshotDate->format('Y-m-d') !== $date) {
$this->error('快照日期不是有效日期。');
return self::FAILURE;
}
try {
$count = $archive->capture($snapshotDate);
} catch (Throwable $exception) {
report($exception);
$this->error('店铺基础信息存档失败:' . $exception->getMessage());
return self::FAILURE;
}
$this->info(sprintf('已保存 %s 店铺基础信息快照,共 %d 条。', $date, $count));
return self::SUCCESS;
}
}