Files
nuhodds_fi4/app/Filament/Imports/Helper/ImportDetails.php
T
2026-10-15 16:29:48 +02:00

63 lines
2.1 KiB
PHP

<?php
namespace App\Filament\Imports\Helper;
use App\Models\StatistikoDetail;
use Illuminate\Support\Carbon;
use Illuminate\Support\LazyCollection;
use Spatie\SimpleExcel\SimpleExcelReader;
class ImportDetails
{
public static $teams = [];
public static function run(string $filePath): void
{
StatistikoDetail::query()->update(['last_import' => 'no']);
self::$teams = [];
SimpleExcelReader::create(storage_path('app/public/'.$filePath))
->getRows()
->chunk(1000)
->each(function (LazyCollection $chunk) {
$collection = [];
foreach ($chunk as $row) {
self::$teams[] = $row['ss_team_id'];
if ($row['player_name'] != '') {
$row['is_active'] = 1;
$row['last_import'] = 'yes';
$row['birthdate'] = isset($row['birthday']) ? Carbon::parse($row['birthday'])->format('Y-m-d') : '';
unset($row['birthday']);
$row['last_update'] = Carbon::parse($row['last_update'])->format('Y-m-d H:i:s');
$collection[] = $row;
}
}
StatistikoDetail::upsert(
$collection,
uniqueBy: [
'ss_player_id',
],
update: [
'ss_team_id',
'player_name',
'ss_national_team_id',
'birthdate',
'height',
'nationality',
'position',
'preferred_foot',
'last_update',
'is_active',
'last_import',
]);
});
self::$teams = array_unique(self::$teams);
// StatistikoDetail::where('last_import', 'no')->whereIn('ss_team_id', self::$teams)->update(['is_active' => 0, 'ss_team_id' => 0]);
}
}