store 6
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\PriceSyncLog;
|
||||
use App\Models\PriceSyncLogItem;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class PriceSyncLogService
|
||||
{
|
||||
public function isAvailable(): bool
|
||||
{
|
||||
return Schema::hasTable('price_sync_logs') && Schema::hasTable('price_sync_log_items');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, array<string, mixed>> $items
|
||||
*/
|
||||
public function record(string $source, ?string $sourceUrl, int $liquidGoldPrice, array $items): ?PriceSyncLog
|
||||
{
|
||||
if (! $this->isAvailable() || $items === []) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$changedItems = array_values(array_filter(
|
||||
$items,
|
||||
static fn (array $item) => (bool) ($item['changed'] ?? false)
|
||||
));
|
||||
|
||||
if ($changedItems === []) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$log = PriceSyncLog::create([
|
||||
'source' => $source,
|
||||
'source_url' => $sourceUrl,
|
||||
'liquid_gold_price' => $liquidGoldPrice,
|
||||
'products_updated' => count($changedItems),
|
||||
'created_at' => now(),
|
||||
]);
|
||||
|
||||
foreach ($changedItems as $item) {
|
||||
PriceSyncLogItem::create([
|
||||
'log_id' => $log->id,
|
||||
'product_id' => $item['product_id'] ?? null,
|
||||
'product_title' => $item['product_title'] ?? null,
|
||||
'reading_method' => $item['reading_method'] ?? null,
|
||||
'source_name' => $item['source_name'] ?? null,
|
||||
'base_price' => (int) ($item['base_price'] ?? 0),
|
||||
'base_profit' => (int) ($item['base_profit'] ?? 0),
|
||||
'offset_percent' => (float) ($item['offset_percent'] ?? 0),
|
||||
'stored_price' => (int) ($item['new_price'] ?? 0),
|
||||
'previous_price' => (int) ($item['old_price'] ?? 0),
|
||||
]);
|
||||
}
|
||||
|
||||
return $log->load('items');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user