diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index ccb500d..55d8992 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -36,8 +36,8 @@ class Kernel extends ConsoleKernel $schedule->job(CafelEggBreederJob::class, 'cafel')->onOneServer()->everyThreeHours(); $schedule->job(CafelPoultryBreederJob::class, 'cafel')->onOneServer()->everyThreeHours(); - $schedule->job(XmlFluxEggJob::class, 'tracea')->dailyAt('02:00')->onOneServer(); - $schedule->job(XmlFluxPoultryJob::class, 'tracea')->dailyAt('22:00')->onOneServer(); + $schedule->job(XmlFluxEggJob::class, 'tracea')->onOneServer()->dailyAt('02:00'); + $schedule->job(XmlFluxPoultryJob::class, 'tracea')->onOneServer()->dailyAt('22:00'); } /** diff --git a/app/Jobs/Arcadia/XmlFluxEggJob.php b/app/Jobs/Arcadia/XmlFluxEggJob.php index 7087bac..ee2c1b6 100644 --- a/app/Jobs/Arcadia/XmlFluxEggJob.php +++ b/app/Jobs/Arcadia/XmlFluxEggJob.php @@ -48,9 +48,7 @@ class XmlFluxEggJob extends XmlJobs * * @var array */ - protected $uniqueKeyField = [ - 'box_code' - ]; + protected $uniqueKeyField = []; /** * Execute the job. @@ -59,6 +57,9 @@ class XmlFluxEggJob extends XmlJobs */ public function handle() { - $this->parseData('ML', TraceaEgg::class); + // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed + $this->parseData('ML', TraceaEgg::class, null, function ($uniqueColumnsKey, $line) { + TraceaEgg::insert($line); + }); } } diff --git a/app/Jobs/Arcadia/XmlJobs.php b/app/Jobs/Arcadia/XmlJobs.php index 0b1852e..9c6f263 100644 --- a/app/Jobs/Arcadia/XmlJobs.php +++ b/app/Jobs/Arcadia/XmlJobs.php @@ -61,6 +61,7 @@ abstract class XmlJobs extends XArcadiaFlux * @param callable $afterSaveCallback After save is done. * @return void * @throws InvalidXmlException If xml file is not valid. + * @throws \ErrorException If no unique columns nor $saveCallback are provided. * @phpcs:disable Generic.Metrics.CyclomaticComplexity.MaxExceeded */ protected function parseData( @@ -145,18 +146,21 @@ abstract class XmlJobs extends XArcadiaFlux foreach ($this->uniqueKeyField as $uField) { $uniqueColumnsKey[$uField] = $line[$uField]; } - if (count($uniqueColumnsKey)) { - if ($saveCallback) { - $saveCallback($uniqueColumnsKey, $line); - } else { - forward_static_call_array( - [$modelClass, 'updateOrCreate'], - [$uniqueColumnsKey, $line] - ); - } + if ($saveCallback) { + $saveCallback($uniqueColumnsKey, $line); if ($afterSaveCallback) { $afterSaveCallback($xmlContent, $line); } + } elseif (count($uniqueColumnsKey)) { + forward_static_call_array( + [$modelClass, 'updateOrCreate'], + [$uniqueColumnsKey, $line] + ); + if ($afterSaveCallback) { + $afterSaveCallback($xmlContent, $line); + } + } else { + throw new \ErrorException('Data not saved'); } } //end foreach } catch (InvalidXmlException $e) { diff --git a/app/Models/TraceaEgg.php b/app/Models/TraceaEgg.php index 45d1b8f..80dd859 100644 --- a/app/Models/TraceaEgg.php +++ b/app/Models/TraceaEgg.php @@ -12,7 +12,7 @@ class TraceaEgg extends Model * * @var string */ - protected $primaryKey = 'box_code'; + protected $primaryKey = null; /** * Indicates if the IDs are auto-incrementing. diff --git a/database/migrations/2022_03_08_121533_create_tracea_eggs_table.php b/database/migrations/2022_03_08_121533_create_tracea_eggs_table.php index 1c74f9b..d2235e3 100644 --- a/database/migrations/2022_03_08_121533_create_tracea_eggs_table.php +++ b/database/migrations/2022_03_08_121533_create_tracea_eggs_table.php @@ -1,9 +1,7 @@ id(); $table->unsignedBigInteger('box_code') - ->unique() ->comment('Le code de la boite d\'oeufs xs:integer [0-9]{9}|[0-9]{10}'); $table->string('ede_code', 10) ->index() @@ -90,26 +87,9 @@ class CreateTraceaEggsTable extends Migration ->comment('Le code de la boite d\'oeufs xs:integer [0-9]{9}|[0-9]{10}') ->change(); }); - collect(DB::select( - 'select box_code from tracea_eggs group by box_code having count(box_code) > 1' - ))->chunk(8000)->map(function (Collection $collection) { - // * Remove duplicate code_boite but keep latest - // ! MASS DELETE - TraceaEgg::whereIn( - 'id', - TraceaEgg::select(['id', 'box_code'])->whereIn( - 'box_code', - $collection->pluck('box_code')->all() - )->get()->groupBy('box_code')->map(function (Collection $codeBoiteGroup) { - // * Remove last entry - $codeBoiteGroup->pop(); - return $codeBoiteGroup; - })->flatten(1)->pluck('id')->all() - )->delete(); - }); Schema::table('tracea_eggs', function (Blueprint $table) { $table->unsignedBigInteger('box_code') - ->unique() + ->index() ->comment('Le code de la boite d\'oeufs xs:integer [0-9]{9}|[0-9]{10}') ->change(); $table->dropColumn(['id']);