is_numeric($seconds) ? $seconds : 300, $this->option('sleep')); while (true) { try { /** @var \Illuminate\Database\Eloquent\Collection */ $devices = Device::supplierBy('device-supplier-yunfei')->get(); foreach ($devices as $device) { $this->createReport($device); } } catch (Throwable $e) { report($e); } sleep($seconds); }; } protected function createReport(Device $device): void { [$lastReportedAt, $latestReportedAt] = value(function (Device $device) { $lastReportedAt = null; $latestReportedAt = null; switch ($device->type) { case DeviceType::InsecticidalLamp: $lastReportedAt = InsecticidalLampDailyReport::where('device_id', $device->id) ->latest('reported_at') ->value('reported_at'); $lastReportedAt ??= InsecticidalLampReport::where('device_id', $device->id) ->oldest('reported_at') ->value('reported_at'); if ($lastReportedAt) { $latestReportedAt = InsecticidalLampReport::where('device_id', $device->id) ->latest('reported_at') ->value('reported_at'); } break; } return [$lastReportedAt, $latestReportedAt]; }, $device); if ($lastReportedAt === null || $latestReportedAt === null) { return; } $service = new YunFeiDeviceService(); /** @var \Carbon\Carbon */ $startAt = $lastReportedAt->copy()->startOfDay(); do { $service->createDailyReport($device, $startAt->copy()); $startAt->addDay(); } while ($latestReportedAt->gte($startAt)); } }