You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
2.5 KiB

1 day ago
<?php
namespace App\Console\Commands;
use App\Services\PeopleCounting\PeopleCountingSyncService;
use Illuminate\Console\Command;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Log;
use InvalidArgumentException;
use Throwable;
class PeopleCountingSyncDailyCommand extends Command
{
use PeopleCountingDateRange;
protected $signature = 'people-counting:sync-daily
{--date= : 单日 Y-m-d默认今天}
{--from= : 区间起始 Y-m-d}
{--to= : 区间结束 Y-m-d缺省则到今天}
{--force : 关闭当天「已有非 0 被全 0 覆盖」保护}';
protected $description = '从客流 HTTP 拉取场馆日合计,按 (venue_id, 日期) 覆盖写入 people_counting_daily';
public function handle(PeopleCountingSyncService $sync): int
{
try {
$days = $this->peopleCountingDays(
$this->option('date'),
$this->option('from'),
$this->option('to'),
Carbon::today()
);
} catch (InvalidArgumentException $e) {
$this->error($e->getMessage());
return self::FAILURE;
}
$force = (bool) $this->option('force');
$protect = ! $force;
$sleepMs = $this->peopleCountingSleepMs();
$today = Carbon::today()->toDateString();
$ok = 0;
$fail = 0;
foreach ($days as $i => $day) {
$d = $day->toDateString();
if ($d > $today) {
$this->warn("跳过未来日期 {$d}");
continue;
}
try {
$r = $sync->syncDailyForDate($day, $protect);
$ok++;
$this->info(sprintf(
'%s written=%d skipped=%d venues=%d%s',
$r['date'],
$r['written'],
$r['skipped'],
$r['venues'],
$force ? ' (force)' : ''
));
Log::info('people-counting.sync-daily', $r);
} catch (Throwable $e) {
$fail++;
$this->error("{$d} 失败: ".$e->getMessage());
Log::warning('people-counting.sync-daily.failed', [
'date' => $d,
'error' => $e->getMessage(),
]);
}
if ($i < count($days) - 1 && $sleepMs > 0) {
usleep($sleepMs * 1000);
}
}
return $fail > 0 && $ok === 0 ? self::FAILURE : self::SUCCESS;
}
}