public function addRecord($operationType, $documentType, $documentId, DateTime $dateTime, array $shopIds = null)
    {
        if (empty($shopIds)) {
            $shopIds = [null];
        }

        $connection = $this->getConnection();

        foreach ($shopIds as $shopId) {
            $tableName = $connection->quoteIdentifier($this->getTableName($shopId));

            try {
                $sql = sprintf(
                    "INSERT INTO {$tableName}
                        (`type`, `document_type`, `document_id`, `timestamp`, `status`)
                    VALUES
                        (:operationType, :documentType, :documentId, :timestamp, :status)"
                );
                $statement = $connection->prepare($sql);
                $statement->execute(
                    [
                        'operationType' => $operationType,
                        'documentType' => $documentType,
                        'documentId' => $documentId,
                        'timestamp' => $dateTime->format('Y-m-d H:i:s'),
                        'status' => self::STATUS_NEW,
                    ]
                );

                if ($operationType === 'D') {
                    $this->deductionForDeletion($connection, $tableName, $documentType, $documentId, $shopId);
                }
            } catch (DBALException $e) {
                // Record exists, check if update is needed.
                $sql = sprintf(
                    "SELECT COUNT(*) AS count FROM {$tableName}
                    WHERE
                        `type` = :operationType
                        AND `document_type` = :documentType
                        AND `document_id` = :documentId
                        AND `status` = :status
                        AND `timestamp` >= :dateTime"
                );
                $statement = $connection->prepare($sql);
                $statement->execute(
                    [
                        'operationType' => $operationType,
                        'documentType' => $documentType,
                        'documentId' => $documentId,
                        'status' => self::STATUS_NEW,
                        'dateTime' => $dateTime->format('Y-m-d H:i:s'),
                    ]
                );
                $newerRecordExists = $statement->fetchColumn(0) > 0;
                if ($newerRecordExists) {
                    continue;
                }

                // More recent record info, attempt to update existing record.
                $sql = sprintf(
                    "UPDATE {$tableName}
                    SET `timestamp` = :dateTime
                    WHERE
                        `type` = :operationType
                        AND `document_type` = :documentType
                        AND `document_id` = :documentId
                        AND `status` = :status"
                );
                $statement = $connection->prepare($sql);
                $statement->execute(
                    [
                        'dateTime' => $dateTime->format('Y-m-d H:i:s'),
                        'operationType' => $operationType,
                        'documentType' => $documentType,
                        'documentId' => $documentId,
                        'status' => self::STATUS_NEW,
                    ]
                );
            }
        }
    }
public function addRecord($operationType, $documentType, $documentId, DateTime $dateTime, array $shopIds = null)
    {
        if (empty($shopIds)) {
            $shopIds = [null];
        }

        $connection = $this->getConnection();

        foreach ($shopIds as $shopId) {
            $tableName = $connection->quoteIdentifier($this->getTableName($shopId));

            try {
                $sql = sprintf(
                    "INSERT INTO {$tableName}
                        (`type`, `document_type`, `document_id`, `timestamp`, `status`)
                    VALUES
                        (:operationType, :documentType, :documentId, :timestamp, :status)"
                );
                $statement = $connection->prepare($sql);
                $statement->execute(
                    [
                        'operationType' => $operationType,
                        'documentType' => $documentType,
                        'documentId' => $documentId,
                        'timestamp' => $dateTime->format('Y-m-d H:i:s'),
                        'status' => self::STATUS_NEW,
                    ]
                );

                if ($operationType === 'D') {
                    $this->deductionForDeletion($connection, $tableName, $documentType, $documentId, $shopId);
                }
            } catch (DBALException $e) {
                // Record exists, check if update is needed.
                $sql = sprintf(
                    "SELECT COUNT(*) AS count FROM {$tableName}
                    WHERE
                        `type` = :operationType
                        AND `document_type` = :documentType
                        AND `document_id` = :documentId
                        AND `status` = :status
                        AND `timestamp` >= :dateTime"
                );
                $statement = $connection->prepare($sql);
                $statement->execute(
                    [
                        'operationType' => $operationType,
                        'documentType' => $documentType,
                        'documentId' => $documentId,
                        'status' => self::STATUS_NEW,
                        'dateTime' => $dateTime->format('Y-m-d H:i:s'),
                    ]
                );
                $newerRecordExists = $statement->fetchColumn(0) > 0;
                if ($newerRecordExists) {
                    continue;
                }

                // More recent record info, attempt to update existing record.
                $sql = sprintf(
                    "UPDATE {$tableName}
                    SET `timestamp` = :dateTime
                    WHERE
                        `type` = :operationType
                        AND `document_type` = :documentType
                        AND `document_id` = :documentId
                        AND `status` = :status"
                );
                $statement = $connection->prepare($sql);
                $statement->execute(
                    [
                        'dateTime' => $dateTime->format('Y-m-d H:i:s'),
                        'operationType' => $operationType,
                        'documentType' => $documentType,
                        'documentId' => $documentId,
                        'status' => self::STATUS_NEW,
                    ]
                );
            }
        }
    }
{@inheritdoc}