This commit is contained in:
2026-05-06 23:19:35 +02:00
parent 34e6b6373f
commit b1b5e416ba
16 changed files with 1448 additions and 65 deletions

View File

@@ -134,30 +134,22 @@ class Allegro {
$client_id = $mdb -> id();
}
if ( !$mdb -> count( 'products', [ 'AND' => [ 'client_id' => $client_id, 'offer_id' => $offer['offer_id'] ] ] ) )
$existing_id = (int) $mdb -> get( 'products', 'id', [ 'AND' => [ 'client_id' => $client_id, 'offer_id' => $offer['offer_id'] ] ] );
if ( !$existing_id )
{
$product_data = [
'client_id' => $client_id,
'offer_id' => $offer['offer_id'],
'name' => $offer['offer_name'],
];
if ( $mdb -> insert( 'products', $product_data ) )
{
$product_id = $mdb -> id();
$offers_added++;
}
$product_id = \factory\Products::ensure_product( $client_id, $offer['offer_id'], [ 'title' => $offer['offer_name'] ] );
if ( $product_id ) $offers_added++;
}
else
{
$product = $mdb -> get( 'products', [ 'id', 'name' ], [ 'AND' => [ 'client_id' => $client_id, 'offer_id' => $offer['offer_id'] ] ] );
$product = $mdb -> get( 'products', [ 'id', 'title' ], [ 'AND' => [ 'client_id' => $client_id, 'offer_id' => $offer['offer_id'] ] ] );
$product_id = $product['id'];
$offer_current_name = $product['name'];
$offer_current_name = $product['title'];
if ( $offer_current_name != $offer['offer_name'] and $offer['date_add'] == date( 'Y-m-d', strtotime( '-1 days', time() ) ) )
{
$mdb -> update( 'products', [ 'name' => $offer['offer_name'] ], [ 'AND' => [ 'client_id' => $client_id, 'offer_id' => $offer['offer_id'] ] ] );
$mdb -> update( 'products', [ 'title' => $offer['offer_name'] ], [ 'AND' => [ 'client_id' => $client_id, 'offer_id' => $offer['offer_id'] ] ] );
$mdb -> insert( 'products_comments', [ 'product_id' => $product_id, 'comment' => 'Zmiana nazwy oferty na: ' . $offer['offer_name'], 'type' => 2, 'date_add' => $offer['date_add'] ] );
}
}

View File

@@ -398,24 +398,7 @@ class Api
{
$offer_data = [];
if ( !$mdb -> count( 'products', [ 'AND' => [ 'client_id' => $data['client_id'], 'offer_id' => $offer['OfferId'] ] ] ) )
{
$offer_data['client_id'] = $data['client_id'];
$offer_data['offer_id'] = $offer['OfferId'];
$offer_data['offer_name'] = $offer['ProductTitle'];
$mdb -> insert( 'products', [
'client_id' => $data['client_id'],
'offer_id' => $offer['OfferId'],
'name' => $offer['ProductTitle']
] );
$offer_id = $mdb -> id();
}
else
{
$offer_id = $mdb -> get( 'products', 'id', [ 'AND' => [ 'client_id' => $data['client_id'], 'offer_id' => $offer['OfferId'] ] ] );
}
$offer_id = \factory\Products::ensure_product( $data['client_id'], $offer['OfferId'], [ 'title' => $offer['ProductTitle'] ] );
if ( $offer_id )
{
@@ -520,19 +503,7 @@ class Api
$product_title = $offer_external_id;
}
if ( !$mdb -> count( 'products', [ 'AND' => [ 'client_id' => $client_id, 'offer_id' => $offer_external_id ] ] ) )
{
$mdb -> insert( 'products', [
'client_id' => $client_id,
'offer_id' => $offer_external_id,
'name' => $product_title
] );
$product_id = $mdb -> id();
}
else
{
$product_id = $mdb -> get( 'products', 'id', [ 'AND' => [ 'client_id' => $client_id, 'offer_id' => $offer_external_id ] ] );
}
$product_id = \factory\Products::ensure_product( $client_id, $offer_external_id, [ 'title' => $product_title ] );
if ( !$product_id )
{

View File

@@ -1444,13 +1444,7 @@ class Cron
if ( !$existing_product )
{
$mdb -> insert( 'products', [
'client_id' => $client_id,
'offer_id' => $offer_external_id,
'title' => $product_title
] );
$product_id = $mdb -> id();
$product_id = \factory\Products::ensure_product( $client_id, $offer_external_id, [ 'title' => $product_title ] );
$products_by_offer_id[ $offer_external_id ] = [
'id' => (int) $product_id,

View File

@@ -21,6 +21,38 @@ class Products
return true;
}
/**
* Bezpieczne wstawienie/odczyt produktu po (client_id, offer_id).
* Wymaga UNIQUE KEY uk_products_client_offer (zob. migracja 031).
* Zwraca product_id istniejacego lub nowo wstawionego wiersza.
* Odporne na race condition: przy bledzie 1062 (Duplicate entry) robi SELECT.
*/
static public function ensure_product( $client_id, $offer_id, array $insert_data = [] )
{
global $mdb;
$client_id = (int) $client_id;
$offer_id = trim( (string) $offer_id );
if ( $client_id <= 0 || $offer_id === '' ) return 0;
$existing_id = (int) $mdb -> get( 'products', 'id', [ 'AND' => [ 'client_id' => $client_id, 'offer_id' => $offer_id ] ] );
if ( $existing_id > 0 ) return $existing_id;
$row = array_merge( $insert_data, [ 'client_id' => $client_id, 'offer_id' => $offer_id ] );
try
{
$mdb -> insert( 'products', $row );
$new_id = (int) $mdb -> id();
if ( $new_id > 0 ) return $new_id;
}
catch ( \PDOException $e )
{
if ( strpos( (string) $e -> getMessage(), '1062' ) === false ) throw $e;
}
return (int) $mdb -> get( 'products', 'id', [ 'AND' => [ 'client_id' => $client_id, 'offer_id' => $offer_id ] ] );
}
static public function delete_products( $product_ids ) {
global $mdb;
if ( empty( $product_ids ) || !is_array( $product_ids ) ) {

View File

@@ -440,18 +440,36 @@ class XmlFeedImporter
}
else
{
$insert_stmt -> execute( [
':client_id' => $client_id,
':offer_id' => $item['offer_id'],
':title' => $title,
':description' => $desc,
':custom_label_1' => $cl1,
':price' => $price,
] );
$inserted_count++;
if ( $is_debug_offer )
try
{
$report['debug_offer']['inserted'] = true;
$insert_stmt -> execute( [
':client_id' => $client_id,
':offer_id' => $item['offer_id'],
':title' => $title,
':description' => $desc,
':custom_label_1' => $cl1,
':price' => $price,
] );
$inserted_count++;
if ( $is_debug_offer ) $report['debug_offer']['inserted'] = true;
}
catch ( \PDOException $pe )
{
// 1062 = race condition z UNIQUE KEY (client_id, offer_id) - aktualizuj zamiast wstawiac
if ( strpos( (string) $pe -> getMessage(), '1062' ) === false ) throw $pe;
$existing_id = (int) $pdo -> query( 'SELECT id FROM products WHERE client_id = ' . (int) $client_id . ' AND offer_id = ' . $pdo -> quote( (string) $item['offer_id'] ) . ' LIMIT 1' ) -> fetchColumn();
if ( $existing_id > 0 )
{
$update_stmt -> execute( [
':title' => $title,
':description' => $desc,
':custom_label_1' => $cl1,
':price' => $price,
':id' => $existing_id,
] );
$updated_count++;
if ( $is_debug_offer ) $report['debug_offer']['updated_via_race_fallback'] = true;
}
}
}
}