Files

47 lines
1.3 KiB
PHP

<?php
namespace App\Filament\Imports\Helper;
use App\Models\SofaScoreRosaTournament;
use Illuminate\Support\LazyCollection;
use Spatie\SimpleExcel\SimpleExcelReader;
class ImportTeamsTournaments
{
public static $active_tournament = [];
public static function run(string $filePath): void
{
SofaScoreRosaTournament::query()->update(['in_update' => 0]);
SimpleExcelReader::create(storage_path('app/public/'.$filePath))
->getRows()
->chunk(100)
->each(function (LazyCollection $chunk) {
foreach ($chunk as $row) {
$row = array_values($row);
$ss_tournament_id = (int) $row[0];
$ss_team_id = (int) $row[1];
if ($ss_tournament_id && $ss_team_id) {
self::$active_tournament[] = $row[0];
SofaScoreRosaTournament::updateOrCreate(
['ss_tournament_id' => $ss_tournament_id, 'ss_team_id' => $ss_team_id],
['in_update' => 1]
);
}
}
});
$tournaments = array_unique(self::$active_tournament);
SofaScoreRosaTournament::where('in_update', 0)->whereIn('ss_tournament_id', $tournaments)->delete();
}
}