47 lines
1.6 KiB
PHP
47 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Models\Locations\Location;
|
|
use Illuminate\Console\Command;
|
|
|
|
class SyncLocationDataCommand extends Command
|
|
{
|
|
protected $signature = 'locations:sync-hierarchy {--force : Force sync even if data exists}';
|
|
protected $description = 'Sync missing location hierarchy data (i.e. divisions, countries, from cities).';
|
|
|
|
public function handle()
|
|
{
|
|
$query = Location::query();
|
|
|
|
if (!$this->option('force')) {
|
|
// Only sync locations that are missing division data
|
|
$query->whereNotNull('city_id')->whereNull('division_id');
|
|
}
|
|
|
|
$locations = $query->get();
|
|
$syncedCount = 0;
|
|
$totalSynced = [];
|
|
|
|
$this->info("Processing {$locations->count()} locations...");
|
|
|
|
foreach ($locations as $location) {
|
|
try {
|
|
$synced = $location->syncAllLocationData();
|
|
if (!empty($synced)) {
|
|
$syncedCount++;
|
|
$totalSynced = array_merge($totalSynced, $synced);
|
|
$this->line("Location ID {$location->id}: " . implode(', ', $synced));
|
|
}
|
|
} catch (\Exception $e) {
|
|
$this->error("Failed to sync location ID {$location->id}: " . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
$syncStats = array_count_values($totalSynced);
|
|
$this->info("\nCompleted syncing {$syncedCount} locations:");
|
|
foreach ($syncStats as $type => $count) {
|
|
$this->info(" - {$count} locations synced {$type}");
|
|
}
|
|
}
|
|
} |